Flowgrind
Advanced TCP traffic generator
fg_time.h File Reference

Timing related routines used by Flowgrind. More...

#include "config.h"
#include <sys/time.h>
#include <time.h>
#include <stdbool.h>

Go to the source code of this file.

Macros

#define NSEC_PER_SEC   1000000000L
 Number of nanoseconds per second. More...
 

Functions

const char * ctimenow (bool ns)
 Returns the current wall-clock time as null-terminated string. More...
 
const char * ctimenow_r (char *buf, size_t size, bool ns)
 Returns the current wall-clock time as null-terminated string. More...
 
const char * ctimespec (const struct timespec *tp, bool ns)
 Converts timespec struct tp into a null-terminated string. More...
 
const char * ctimespec_r (const struct timespec *tp, char *buf, size_t size, bool ns)
 Converts timespec struct tp into a null-terminated string. More...
 
int gettime (struct timespec *tp)
 Returns the current wall-clock time with nanosecond precision. More...
 
bool normalize_tp (struct timespec *tp)
 Normalizes timespec struct tp. More...
 
void time_add (struct timespec *tp, double seconds)
 Add an amount of time seconds to a specific point in time tp. More...
 
double time_diff (const struct timespec *tp1, const struct timespec *tp2)
 Returns the time difference between two the specific points in time tp1 and tp2. More...
 
double time_diff_now (const struct timespec *tp)
 Returns time difference between now and the specific point in time tp. More...
 
bool time_is_after (const struct timespec *tp1, const struct timespec *tp2)
 Returns true if second point in time tp2 is chronologically after the first point in time tp1. More...
 

Detailed Description

Timing related routines used by Flowgrind.

Definition in file fg_time.h.

Macro Definition Documentation

◆ NSEC_PER_SEC

#define NSEC_PER_SEC   1000000000L

Number of nanoseconds per second.

Definition at line 41 of file fg_time.h.

Function Documentation

◆ ctimenow()

const char* ctimenow ( bool  ns)

Returns the current wall-clock time as null-terminated string.

The function returns a pointer to static data and hence is not thread-safe. The thread-safe version is ctimenow_r().

Parameters
[in]nsif returned time string should have nanosecond precision
Returns
string of the form '2013-12-09-12:00:48[.34369902]'
See also
ctimenow_r

Definition at line 56 of file fg_time.c.

57 {
58  struct timespec now;
59 
60  gettime(&now);
61 
62  return ctimespec(&now, ns);
63 }

◆ ctimenow_r()

const char* ctimenow_r ( char *  buf,
size_t  size,
bool  ns 
)

Returns the current wall-clock time as null-terminated string.

It stores the string in a user-supplied buffer buf.

Parameters
[out]bufbuffer with room for at least 30 bytes
[in]sizesize of the buffer
[in]nsif returned time string should have nanosecond precision
Returns
string of the form '2013-12-09-12:00:48[.34369902]'

Definition at line 47 of file fg_time.c.

48 {
49  struct timespec now;
50 
51  gettime(&now);
52 
53  return ctimespec_r(&now, buf, size, ns);
54 }

◆ ctimespec()

const char* ctimespec ( const struct timespec *  tp,
bool  ns 
)

Converts timespec struct tp into a null-terminated string.

The function returns a pointer to static data and hence is not thread-safe. The thread-safe version is ctimespec_r().

Parameters
[in]tppoint in time
[in]nsif returned time string should have nanosecond precision
Returns
string of the form '2013-12-09-12:00:48[.34369902]'
See also
ctimespec_r

Definition at line 86 of file fg_time.c.

87 {
88  static char buf[30];
89 
90  ctimespec_r(tp, buf, sizeof(buf), ns);
91 
92  return buf;
93 }

◆ ctimespec_r()

const char* ctimespec_r ( const struct timespec *  tp,
char *  buf,
size_t  size,
bool  ns 
)

Converts timespec struct tp into a null-terminated string.

It stores the string in a user-supplied buffer buf.

Parameters
[in]tppoint in time
[out]bufbuffer with room for at least 30 bytes
[in]sizesize of the buffer
[in]nsif returned time string should have nanosecond precision
Returns
string of the form '2013-12-09-12:00:48i[.34369902]'

Definition at line 66 of file fg_time.c.

68 {
69  struct tm tm;
70 
71  /* Converts the calendar time to broken-down time representation,
72  * expressed relative to the user's specified timezone */
73  tzset();
74  localtime_r(&tp->tv_sec, &tm);
75 
76  /* Converts broken-down time representation into a string */
77  size_t len = strftime(buf, size, "%F-%T", &tm);
78 
79  /* Append nanoseconds to string */
80  if (ns)
81  snprintf(buf+len, size-len, ".%09ld", tp->tv_nsec);
82 
83  return buf;
84 }

◆ gettime()

int gettime ( struct timespec *  tp)

Returns the current wall-clock time with nanosecond precision.

Since the returned time is retrieved from a system-wide clock that measures real time, the time is may be affected by discontinuous jumps in the system time (e.g., if admin manually changes the clock), and by the incremental adjustments performed by NTP.

Parameters
[out]tpcurrent time in seconds and nanoseconds since the Epoch
Returns
return 0 for success, or -1 for failure

Definition at line 145 of file fg_time.c.

146 {
147  static struct timespec res = {.tv_sec = 0, .tv_nsec = 0};
148 
149  /* Find out clock resolution. Will only be retrieved on first call */
150  if (!res.tv_sec && !res.tv_nsec) {
151  clock_getres(CLOCK_REALTIME, &res);
152  /* Clock resolution is lower than expected (1ns) */
153  assert(res.tv_nsec == 1);
154  }
155 
156  /* Get wall-clock time */
157  return clock_gettime(CLOCK_REALTIME, tp);
158 }

◆ normalize_tp()

bool normalize_tp ( struct timespec *  tp)

Normalizes timespec struct tp.

Ensures that the equation 0 <= tp->tv_nsec < NSEC_PER_SEC holds, meaning that the amount of nanoseconds is not negative less than one second.

Parameters
[in,out]tppoint in time
Returns
true if timespec struct was already normalized, otherwise false

Definition at line 119 of file fg_time.c.

120 {
121  bool normalized = true;
122 
123  while (tp->tv_nsec >= (long) NSEC_PER_SEC) {
124  tp->tv_nsec -= (long) NSEC_PER_SEC;
125  tp->tv_sec++;
126  normalized = false;
127  }
128  while (tp->tv_nsec < 0) {
129  tp->tv_nsec += (long) NSEC_PER_SEC;
130  tp->tv_sec--;
131  normalized = false;
132  }
133  return normalized;
134 }

◆ time_add()

void time_add ( struct timespec *  tp,
double  seconds 
)

Add an amount of time seconds to a specific point in time tp.

Parameters
[in,out]tppoint in time
[in]secondsamount of time in seconds

Definition at line 136 of file fg_time.c.

137 {
138  tp->tv_sec += (time_t) seconds;
139  tp->tv_nsec += (long) ((seconds - (time_t) seconds) * (long) NSEC_PER_SEC);
140  normalize_tp(tp);
141 }

◆ time_diff()

double time_diff ( const struct timespec *  tp1,
const struct timespec *  tp2 
)

Returns the time difference between two the specific points in time tp1 and tp2.

Negative if the first point in time is chronologically after the second one.

Parameters
[in]tp1point in time
[in]tp2point in time
Returns
time difference in nanoseconds

Definition at line 95 of file fg_time.c.

96 {
97  return (double) (tp2->tv_sec - tp1->tv_sec)
98  + (double) (tp2->tv_nsec - tp1->tv_nsec) / (long) NSEC_PER_SEC;
99 }

◆ time_diff_now()

double time_diff_now ( const struct timespec *  tp)

Returns time difference between now and the specific point in time tp.

Parameters
[in]tppoint in time
Returns
time difference in nanoseconds

Definition at line 101 of file fg_time.c.

102 {
103  struct timespec now;
104 
105  gettime(&now);
106  return (double) (now.tv_sec - tp->tv_sec)
107  + (double) (now.tv_nsec - tp->tv_nsec) / (long) NSEC_PER_SEC;
108 }

◆ time_is_after()

bool time_is_after ( const struct timespec *  tp1,
const struct timespec *  tp2 
)

Returns true if second point in time tp2 is chronologically after the first point in time tp1.

Parameters
[in]tp1point in time
[in]tp2point in time
Returns
true or false

Definition at line 110 of file fg_time.c.

111 {
112  if (tp1->tv_sec > tp2->tv_sec)
113  return true;
114  if (tp1->tv_sec < tp2->tv_sec)
115  return false;
116  return tp1->tv_nsec > tp2->tv_nsec;
117 }
NSEC_PER_SEC
#define NSEC_PER_SEC
Number of nanoseconds per second.
Definition: fg_time.h:41
ctimespec_r
const char * ctimespec_r(const struct timespec *tp, char *buf, size_t size, bool ns)
Converts timespec struct tp into a null-terminated string.
Definition: fg_time.c:66
normalize_tp
bool normalize_tp(struct timespec *tp)
Normalizes timespec struct tp.
Definition: fg_time.c:119
ctimespec
const char * ctimespec(const struct timespec *tp, bool ns)
Converts timespec struct tp into a null-terminated string.
Definition: fg_time.c:86
gettime
int gettime(struct timespec *tp)
Returns the current wall-clock time with nanosecond precision.
Definition: fg_time.c:145