summaryrefslogtreecommitdiffstats
path: root/src/iperf_time.c
blob: a435dd30d6f153ba1cddeb2e7aeecb1d0047412d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * iperf, Copyright (c) 2014-2018, The Regents of the University of
 * California, through Lawrence Berkeley National Laboratory (subject
 * to receipt of any required approvals from the U.S. Dept. of
 * Energy).  All rights reserved.
 *
 * If you have questions about your rights to use or distribute this
 * software, please contact Berkeley Lab's Technology Transfer
 * Department at TTD@lbl.gov.
 *
 * NOTICE.  This software is owned by the U.S. Department of Energy.
 * As such, the U.S. Government has been granted for itself and others
 * acting on its behalf a paid-up, nonexclusive, irrevocable,
 * worldwide license in the Software to reproduce, prepare derivative
 * works, and perform publicly and display publicly.  Beginning five
 * (5) years after the date permission to assert copyright is obtained
 * from the U.S. Department of Energy, and subject to any subsequent
 * five (5) year renewals, the U.S. Government is granted for itself
 * and others acting on its behalf a paid-up, nonexclusive,
 * irrevocable, worldwide license in the Software to reproduce,
 * prepare derivative works, distribute copies to the public, perform
 * publicly and display publicly, and to permit others to do so.
 *
 * This code is distributed under a BSD style license, see the LICENSE
 * file for complete information.
 */


#include <stddef.h>

#include "iperf_config.h"
#include "iperf_time.h"

#ifdef HAVE_CLOCK_GETTIME

#include <time.h>

int
iperf_time_now(struct iperf_time *time1)
{
    struct timespec ts;
    int result;
    result = clock_gettime(CLOCK_MONOTONIC, &ts);
    if (result == 0) {
        time1->secs = (uint32_t) ts.tv_sec;
        time1->usecs = (uint32_t) ts.tv_nsec / 1000;
    }
    return result;
}

#else

#include <sys/time.h>

int
iperf_time_now(struct iperf_time *time1)
{
    struct timeval tv;
    int result;
    result = gettimeofday(&tv, NULL);
    time1->secs = tv.tv_sec;
    time1->usecs = tv.tv_usec;
    return result;
}

#endif

/* iperf_time_add_usecs
 *
 * Add a number of microseconds to a iperf_time.
 */
void
iperf_time_add_usecs(struct iperf_time *time1, uint64_t usecs)
{
    time1->secs += usecs / 1000000L;
    time1->usecs += usecs % 1000000L;
    if ( time1->usecs >= 1000000L ) {
        time1->secs += time1->usecs / 1000000L;
        time1->usecs %= 1000000L;
    }
}

uint64_t
iperf_time_in_usecs(struct iperf_time *time)
{
    return time->secs * 1000000LL + time->usecs;
}

double
iperf_time_in_secs(struct iperf_time *time)
{
    return time->secs + time->usecs / 1000000.0;
}

/* iperf_time_compare
 *
 * Compare two timestamps
 *
 * Returns -1 if time1 is earlier, 1 if time1 is later,
 * or 0 if the timestamps are equal.
 */
int
iperf_time_compare(struct iperf_time *time1, struct iperf_time *time2)
{
    if (time1->secs < time2->secs)
        return -1;
    if (time1->secs > time2->secs)
        return 1;
    if (time1->usecs < time2->usecs)
        return -1;
    if (time1->usecs > time2->usecs)
        return 1;
    return 0;
}

/* iperf_time_diff
 *
 * Calculates the time from time2 to time1, assuming time1 is later than time2.
 * The diff will always be positive, so the return value should be checked
 * to determine if time1 was earlier than time2.
 *
 * Returns 1 if the time1 is less than or equal to time2, otherwise 0.
 */
int
iperf_time_diff(struct iperf_time *time1, struct iperf_time *time2, struct iperf_time *diff)
{
    int past = 0;
    int cmp = 0;

    cmp = iperf_time_compare(time1, time2);
    if (cmp == 0) {
        diff->secs = 0;
        diff->usecs = 0;
        past = 1;
    }
    else if (cmp == 1) {
        diff->secs = time1->secs - time2->secs;
        diff->usecs = time1->usecs;
        if (diff->usecs < time2->usecs) {
            diff->secs -= 1;
            diff->usecs += 1000000;
        }
        diff->usecs = diff->usecs - time2->usecs;
    } else {
        diff->secs = time2->secs - time1->secs;
        diff->usecs = time2->usecs;
        if (diff->usecs < time1->usecs) {
            diff->secs -= 1;
            diff->usecs += 1000000;
        }
        diff->usecs = diff->usecs - time1->usecs;
        past = 1;
    }

    return past;
}