Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

time_util.h

Go to the documentation of this file.
00001 /*
00002  * time_util.h
00003  *
00004  * Copyright (c) 2003 The University of Utah and the Flux Group.
00005  * All rights reserved.
00006  *
00007  * This file is licensed under the terms of the GNU Public License.  
00008  * See the file "license.terms" for restrictions on redistribution 
00009  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
00010  */
00011 
00012 /**
00013  * @file time_util.h
00014  *
00015  * Inline functions for manipulating time values.
00016  */
00017 
00018 #ifndef _time_util_h
00019 #define _time_util_h
00020 
00021 #ifdef __cplusplus
00022 extern "C" {
00023 #endif
00024 
00025 #include "assert_pp.h"
00026 
00027 /**
00028  * The number of microseconds per second.
00029  */
00030 #define MICROS_PER_SECOND 1000000
00031 
00032 /**
00033  * The number of nanoseconds per microsecond.
00034  */
00035 #define NANOS_PER_MICRO 1000
00036 
00037 /**
00038  * Convert a microsecond value to a struct timespec.
00039  *
00040  * @param microseconds The microsecond value to convert.
00041  * @return A timespec that represents the given number of microseconds.
00042  */
00043 static inline
00044 struct timespec microsec_to_timespec(unsigned long long microseconds)
00045 {
00046     struct timespec retval;
00047     
00048     retval.tv_sec =  (int)(microseconds / MICROS_PER_SECOND);
00049     retval.tv_nsec = (int)(microseconds % MICROS_PER_SECOND) * NANOS_PER_MICRO;
00050     return retval;
00051 }
00052 
00053 /**
00054  * Convert a timespec to a microsecond value.  @e NOTE: There is a loss of
00055  * precision in the conversion.
00056  *
00057  * @param time_spec The timespec to convert.
00058  * @return The number of microseconds specified by the timespec.
00059  */
00060 static inline
00061 unsigned long long timespec_to_microsec(struct timespec *time_spec)
00062 {
00063     long long retval;
00064 
00065     require(time_spec != 0);
00066     
00067     retval = time_spec->tv_sec * MICROS_PER_SECOND;
00068     retval += time_spec->tv_nsec / NANOS_PER_MICRO;
00069     return retval;
00070 }
00071 
00072 /**
00073  * Convert a string to a microsecond value.  The string format is a number
00074  * followed by an optional unit notation, where microseconds is the default
00075  * unit.  For example:
00076  *
00077  * @li 10 = 10 microseconds
00078  * @li 10us = 10 microseconds
00079  * @li 10ms = 10,000 microseconds
00080  * @li 10s = 10,000,000 microseconds
00081  *
00082  * @param us_out The destination for the microsecond value.
00083  * @param str The string to parse.
00084  * @return True if the string was successfully parsed, false otherwise.
00085  */
00086 int string_to_microsec(unsigned long long *us_out, const char *str);
00087 
00088 /**
00089  * Compare two timespec's.
00090  *
00091  * @param tvp Pointer to the first timespec.
00092  * @param uvp Pointer to the second timespec.
00093  * @param cmp The comparison operator.
00094  */
00095 #define tscmp(tvp, uvp, cmp)                                            \
00096         (((tvp)->tv_sec == (uvp)->tv_sec) ?                             \
00097             ((tvp)->tv_nsec cmp (uvp)->tv_nsec) :                       \
00098             ((tvp)->tv_sec cmp (uvp)->tv_sec))
00099 
00100 #ifdef __cplusplus
00101 }
00102 #endif
00103 
00104 #endif

Generated on Fri Oct 22 07:50:25 2004 for CPU Broker by  doxygen 1.3.9.1