2 * Mausezahn - A fast versatile traffic generator
3 * Copyright (C) 2008-2010 Herbert Haas
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License version 2 as published by the
7 * Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, see http://www.gnu.org/licenses/gpl-2.0.html
23 // Check if current system supports the nanosecond timer functions.
24 // Additionally, measure the precision.
25 // This function should be called upon program start.
32 // Check if the glibc is recent enough:
33 #ifdef _POSIX_C_SOURCE
35 if (_POSIX_C_SOURCE
>= 199309L) {
36 r
= clock_getres(CLOCK_MONOTONIC
, &res
);
37 if (r
!=0) perror(" mz/check_timer:");
39 fprintf(stderr
, " This system supports a high resolution clock.\n");
40 fprintf(stderr
, " The clock resolution is %li nanoseconds.\n",
46 " WARNING: Your system does NOT support the newer high resolution clock\n"
47 " Please inform the author: herbert@perihel.at\n");
57 // This is the replacement for gettimeofday() which would result in 'jumps' if
58 // the system clock is adjusted (e. g. via a NTP process) and finally the jitter
59 // measurement would include wrong datapoints.
61 // Furthermore the function below utilizes the newer hi-res nanosecond timers.
62 inline void getcurtime (struct mz_timestamp
*t
)
65 clock_gettime(CLOCK_MONOTONIC
, &ct
);
73 //////////////////////////////////////////////////////////////////////////////////////
74 // Purpose: Calculate time deltas of two timestamps stored in struct timeval.
76 // Subtract the "struct timeval" values X and Y, storing the result in RESULT,
81 // Sign: 1 = negative, 0 = positive
82 // Error: -1 due to a wrong timestamp (i. e. nsec > 999999999L)
84 inline int timestamp_subtract (struct mz_timestamp
*x
, struct mz_timestamp
*y
, struct mz_timestamp
*result
)
89 // Check for wrong timestamps
90 if ((x
->nsec
>999999999L) || (y
->nsec
>999999999L)) return -1;
92 if (y
->sec
> x
->sec
) sign
=1;
93 else if ((y
->sec
== x
->sec
) && (y
->nsec
> x
->nsec
)) sign
=1;
95 ndiff
= x
->nsec
- y
->nsec
;
96 if ((ndiff
>0) && (sign
)) carry
=1;
97 if ((ndiff
<0) && (sign
)) ndiff
= y
->nsec
- x
->nsec
;
98 if ((ndiff
<0) && (!sign
)) {
99 ndiff
= 1000000000L + ndiff
;
104 result
->sec
= y
->sec
- x
->sec
- carry
;
106 result
->sec
= x
->sec
- y
->sec
- carry
;
108 result
->nsec
= ndiff
;
113 // Add two variables of type struct mz_timestamp: x+y=result.
115 inline void timestamp_add (struct mz_timestamp
*x
, struct mz_timestamp
*y
, struct mz_timestamp
*result
)
120 c
= x
->nsec
+ y
->nsec
;
123 result
->nsec
=c
-1000000000;
124 } else result
->nsec
=c
;
126 result
->sec
= x
->sec
+ y
->sec
+ carry
;
131 // Returns a human readable timestamp in the string result.
132 // Optionally a prefix can be specified, for example if the
133 // timestamp is part of a filename.
136 // char myTimeStamp[128];
138 // timestamp_human(myTimeStamp, NULL);
140 // => "20080718_155521"
142 // /* or with prefix */
144 // timestamp_human(myTimeStamp, "MZ_RTP_jitter_");
146 // => "MZ_RTP_jitter_20080718_155521"
148 int timestamp_human(char* result
, const char* prefix
)
151 struct tm curtime_broken
;
152 char curtime_str
[32];
155 localtime_r (&curtime
, &curtime_broken
);
157 sprintf(curtime_str
, "%4i%02i%02i-%02i%02i%02i",
158 curtime_broken
.tm_year
+1900,
159 curtime_broken
.tm_mon
+1,
160 curtime_broken
.tm_mday
,
161 curtime_broken
.tm_hour
,
162 curtime_broken
.tm_min
,
163 curtime_broken
.tm_sec
);
167 strncpy(result
, curtime_str
, 32);
171 strncpy(result
, prefix
, 32);
172 strncat(result
, curtime_str
, 32);
179 // Creates a human readable timestamp in the string result.
180 // Optionally a prefix can be specified, for example if the
181 // timestamp is part of a filename.
184 // char myTimeStamp[9];
186 // timestamp_hms (myTimeStamp);
189 int timestamp_hms(char* result
)
192 struct tm curtime_broken
;
193 char curtime_str
[32];
196 localtime_r (&curtime
, &curtime_broken
);
198 sprintf(curtime_str
, "%02i:%02i:%02i",
199 curtime_broken
.tm_hour
,
200 curtime_broken
.tm_min
,
201 curtime_broken
.tm_sec
);
203 strncpy(result
, curtime_str
, 9);