Skip to content

Commit 877ed2e

Browse files
committed
json: add UNIX timestamp in milliseconds
This extends the "timestamp" field with a UNIX timestamp in milliseconds representing the current local wall clock time. The "timestamp" field now has a new subkey: "timestamp": { "time": "Tue, 25 Feb 2025 11:49:59 GMT", "timesecs": 1740484199, // new "timemillisecs": 1740484199926 }, This is helpful when one does fine-grained network tests with iperf, where iperf events need to be aligned with events coming from other resources. A time resolution based on seconds is too coarse-grained for that. It is enough to use milliseconds here, as networking always comes with small timing offsets and delays. Using microseconds wouldn't add a benefit. It is sufficient to only change the code at this place, as the rel time offset for all further events already includes milliseconds. Currently, iperf3 prints this time offset in seconds encoded as a double with a microsecond granularity. Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de>
1 parent 7b2af28 commit 877ed2e

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/iperf_api.c

+8-4
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,7 @@ mapped_v4_to_regular_v4(char *str)
944944
void
945945
iperf_on_connect(struct iperf_test *test)
946946
{
947-
time_t now_secs;
947+
struct timespec time_spec;
948948
const char* rfc1123_fmt = "%a, %d %b %Y %H:%M:%S %Z";
949949
char now_str[100];
950950
char ipr[INET6_ADDRSTRLEN];
@@ -953,11 +953,15 @@ iperf_on_connect(struct iperf_test *test)
953953
struct sockaddr_in *sa_inP;
954954
struct sockaddr_in6 *sa_in6P;
955955
socklen_t len;
956+
unsigned long long now_millisecs;
956957

957-
now_secs = time((time_t*) 0);
958-
(void) strftime(now_str, sizeof(now_str), rfc1123_fmt, gmtime(&now_secs));
958+
clock_gettime(CLOCK_REALTIME, &time_spec);
959+
// Unix timestamp in milliseconds (local wall clock time)
960+
now_millisecs = time_spec.tv_sec * 1000 + time_spec.tv_nsec / 1000000;
961+
962+
(void) strftime(now_str, sizeof(now_str), rfc1123_fmt, gmtime(&time_spec.tv_sec));
959963
if (test->json_output)
960-
cJSON_AddItemToObject(test->json_start, "timestamp", iperf_json_printf("time: %s timesecs: %d", now_str, (int64_t) now_secs));
964+
cJSON_AddItemToObject(test->json_start, "timestamp", iperf_json_printf("time: %s timesecs: %d timemillisecs: %d", now_str, (int64_t) time_spec.tv_sec, now_millisecs));
961965
else if (test->verbose)
962966
iperf_printf(test, report_time, now_str);
963967

src/iperf_time.h

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ int iperf_time_diff(struct iperf_time *time1, struct iperf_time *time2, struct i
4444

4545
uint64_t iperf_time_in_usecs(struct iperf_time *time);
4646

47+
/**
48+
* Returns the time in seconds as double type with a microsecond granularity.
49+
*/
4750
double iperf_time_in_secs(struct iperf_time *time);
4851

4952
#endif

0 commit comments

Comments
 (0)