xref: /aosp_15_r20/external/iperf3/src/timer.c (revision 7ab6e6ace082586527a400463bc693a412a40341)
1*7ab6e6acSAndroid Build Coastguard Worker /*
2*7ab6e6acSAndroid Build Coastguard Worker  * iperf, Copyright (c) 2014, The Regents of the University of
3*7ab6e6acSAndroid Build Coastguard Worker  * California, through Lawrence Berkeley National Laboratory (subject
4*7ab6e6acSAndroid Build Coastguard Worker  * to receipt of any required approvals from the U.S. Dept. of
5*7ab6e6acSAndroid Build Coastguard Worker  * Energy).  All rights reserved.
6*7ab6e6acSAndroid Build Coastguard Worker  *
7*7ab6e6acSAndroid Build Coastguard Worker  * If you have questions about your rights to use or distribute this
8*7ab6e6acSAndroid Build Coastguard Worker  * software, please contact Berkeley Lab's Technology Transfer
9*7ab6e6acSAndroid Build Coastguard Worker  * Department at [email protected].
10*7ab6e6acSAndroid Build Coastguard Worker  *
11*7ab6e6acSAndroid Build Coastguard Worker  * NOTICE.  This software is owned by the U.S. Department of Energy.
12*7ab6e6acSAndroid Build Coastguard Worker  * As such, the U.S. Government has been granted for itself and others
13*7ab6e6acSAndroid Build Coastguard Worker  * acting on its behalf a paid-up, nonexclusive, irrevocable,
14*7ab6e6acSAndroid Build Coastguard Worker  * worldwide license in the Software to reproduce, prepare derivative
15*7ab6e6acSAndroid Build Coastguard Worker  * works, and perform publicly and display publicly.  Beginning five
16*7ab6e6acSAndroid Build Coastguard Worker  * (5) years after the date permission to assert copyright is obtained
17*7ab6e6acSAndroid Build Coastguard Worker  * from the U.S. Department of Energy, and subject to any subsequent
18*7ab6e6acSAndroid Build Coastguard Worker  * five (5) year renewals, the U.S. Government is granted for itself
19*7ab6e6acSAndroid Build Coastguard Worker  * and others acting on its behalf a paid-up, nonexclusive,
20*7ab6e6acSAndroid Build Coastguard Worker  * irrevocable, worldwide license in the Software to reproduce,
21*7ab6e6acSAndroid Build Coastguard Worker  * prepare derivative works, distribute copies to the public, perform
22*7ab6e6acSAndroid Build Coastguard Worker  * publicly and display publicly, and to permit others to do so.
23*7ab6e6acSAndroid Build Coastguard Worker  *
24*7ab6e6acSAndroid Build Coastguard Worker  * This code is distributed under a BSD style license, see the LICENSE
25*7ab6e6acSAndroid Build Coastguard Worker  * file for complete information.
26*7ab6e6acSAndroid Build Coastguard Worker  *
27*7ab6e6acSAndroid Build Coastguard Worker  * Based on timers.c by Jef Poskanzer. Used with permission.
28*7ab6e6acSAndroid Build Coastguard Worker  */
29*7ab6e6acSAndroid Build Coastguard Worker 
30*7ab6e6acSAndroid Build Coastguard Worker #include <sys/types.h>
31*7ab6e6acSAndroid Build Coastguard Worker #include <stdlib.h>
32*7ab6e6acSAndroid Build Coastguard Worker 
33*7ab6e6acSAndroid Build Coastguard Worker #include "timer.h"
34*7ab6e6acSAndroid Build Coastguard Worker #include "iperf_time.h"
35*7ab6e6acSAndroid Build Coastguard Worker 
36*7ab6e6acSAndroid Build Coastguard Worker static Timer* timers = NULL;
37*7ab6e6acSAndroid Build Coastguard Worker static Timer* free_timers = NULL;
38*7ab6e6acSAndroid Build Coastguard Worker 
39*7ab6e6acSAndroid Build Coastguard Worker TimerClientData JunkClientData;
40*7ab6e6acSAndroid Build Coastguard Worker 
41*7ab6e6acSAndroid Build Coastguard Worker 
42*7ab6e6acSAndroid Build Coastguard Worker 
43*7ab6e6acSAndroid Build Coastguard Worker /* This is an efficiency tweak.  All the routines that need to know the
44*7ab6e6acSAndroid Build Coastguard Worker ** current time get passed a pointer to a struct iperf_time.  If it's non-NULL
45*7ab6e6acSAndroid Build Coastguard Worker ** it gets used, otherwise we do our own iperf_time_now() to fill it in.
46*7ab6e6acSAndroid Build Coastguard Worker ** This lets the caller avoid extraneous iperf_time_now()s when efficiency
47*7ab6e6acSAndroid Build Coastguard Worker ** is needed, and not bother with the extra code when efficiency doesn't
48*7ab6e6acSAndroid Build Coastguard Worker ** matter too much.
49*7ab6e6acSAndroid Build Coastguard Worker */
50*7ab6e6acSAndroid Build Coastguard Worker static void
getnow(struct iperf_time * nowP,struct iperf_time * nowP2)51*7ab6e6acSAndroid Build Coastguard Worker getnow( struct iperf_time* nowP, struct iperf_time* nowP2 )
52*7ab6e6acSAndroid Build Coastguard Worker {
53*7ab6e6acSAndroid Build Coastguard Worker     if ( nowP != NULL )
54*7ab6e6acSAndroid Build Coastguard Worker 	*nowP2 = *nowP;
55*7ab6e6acSAndroid Build Coastguard Worker     else
56*7ab6e6acSAndroid Build Coastguard Worker 	iperf_time_now(nowP2);
57*7ab6e6acSAndroid Build Coastguard Worker }
58*7ab6e6acSAndroid Build Coastguard Worker 
59*7ab6e6acSAndroid Build Coastguard Worker 
60*7ab6e6acSAndroid Build Coastguard Worker static void
list_add(Timer * t)61*7ab6e6acSAndroid Build Coastguard Worker list_add( Timer* t )
62*7ab6e6acSAndroid Build Coastguard Worker {
63*7ab6e6acSAndroid Build Coastguard Worker     Timer* t2;
64*7ab6e6acSAndroid Build Coastguard Worker     Timer* t2prev;
65*7ab6e6acSAndroid Build Coastguard Worker 
66*7ab6e6acSAndroid Build Coastguard Worker     if ( timers == NULL ) {
67*7ab6e6acSAndroid Build Coastguard Worker 	/* The list is empty. */
68*7ab6e6acSAndroid Build Coastguard Worker 	timers = t;
69*7ab6e6acSAndroid Build Coastguard Worker 	t->prev = t->next = NULL;
70*7ab6e6acSAndroid Build Coastguard Worker     } else {
71*7ab6e6acSAndroid Build Coastguard Worker 	if (iperf_time_compare(&t->time, &timers->time) < 0) {
72*7ab6e6acSAndroid Build Coastguard Worker 	    /* The new timer goes at the head of the list. */
73*7ab6e6acSAndroid Build Coastguard Worker 	    t->prev = NULL;
74*7ab6e6acSAndroid Build Coastguard Worker 	    t->next = timers;
75*7ab6e6acSAndroid Build Coastguard Worker 	    timers->prev = t;
76*7ab6e6acSAndroid Build Coastguard Worker 	    timers = t;
77*7ab6e6acSAndroid Build Coastguard Worker 	} else {
78*7ab6e6acSAndroid Build Coastguard Worker 	    /* Walk the list to find the insertion point. */
79*7ab6e6acSAndroid Build Coastguard Worker 	    for ( t2prev = timers, t2 = timers->next; t2 != NULL;
80*7ab6e6acSAndroid Build Coastguard Worker 		  t2prev = t2, t2 = t2->next ) {
81*7ab6e6acSAndroid Build Coastguard Worker 		if (iperf_time_compare(&t->time, &t2->time) < 0) {
82*7ab6e6acSAndroid Build Coastguard Worker 		    /* Found it. */
83*7ab6e6acSAndroid Build Coastguard Worker 		    t2prev->next = t;
84*7ab6e6acSAndroid Build Coastguard Worker 		    t->prev = t2prev;
85*7ab6e6acSAndroid Build Coastguard Worker 		    t->next = t2;
86*7ab6e6acSAndroid Build Coastguard Worker 		    t2->prev = t;
87*7ab6e6acSAndroid Build Coastguard Worker 		    return;
88*7ab6e6acSAndroid Build Coastguard Worker 		}
89*7ab6e6acSAndroid Build Coastguard Worker 	    }
90*7ab6e6acSAndroid Build Coastguard Worker 	    /* Oops, got to the end of the list.  Add to tail. */
91*7ab6e6acSAndroid Build Coastguard Worker 	    t2prev->next = t;
92*7ab6e6acSAndroid Build Coastguard Worker 	    t->prev = t2prev;
93*7ab6e6acSAndroid Build Coastguard Worker 	    t->next = NULL;
94*7ab6e6acSAndroid Build Coastguard Worker 	}
95*7ab6e6acSAndroid Build Coastguard Worker     }
96*7ab6e6acSAndroid Build Coastguard Worker }
97*7ab6e6acSAndroid Build Coastguard Worker 
98*7ab6e6acSAndroid Build Coastguard Worker 
99*7ab6e6acSAndroid Build Coastguard Worker static void
list_remove(Timer * t)100*7ab6e6acSAndroid Build Coastguard Worker list_remove( Timer* t )
101*7ab6e6acSAndroid Build Coastguard Worker {
102*7ab6e6acSAndroid Build Coastguard Worker     if ( t->prev == NULL )
103*7ab6e6acSAndroid Build Coastguard Worker 	timers = t->next;
104*7ab6e6acSAndroid Build Coastguard Worker     else
105*7ab6e6acSAndroid Build Coastguard Worker 	t->prev->next = t->next;
106*7ab6e6acSAndroid Build Coastguard Worker     if ( t->next != NULL )
107*7ab6e6acSAndroid Build Coastguard Worker 	t->next->prev = t->prev;
108*7ab6e6acSAndroid Build Coastguard Worker }
109*7ab6e6acSAndroid Build Coastguard Worker 
110*7ab6e6acSAndroid Build Coastguard Worker 
111*7ab6e6acSAndroid Build Coastguard Worker static void
list_resort(Timer * t)112*7ab6e6acSAndroid Build Coastguard Worker list_resort( Timer* t )
113*7ab6e6acSAndroid Build Coastguard Worker {
114*7ab6e6acSAndroid Build Coastguard Worker     /* Remove the timer from the list. */
115*7ab6e6acSAndroid Build Coastguard Worker     list_remove( t );
116*7ab6e6acSAndroid Build Coastguard Worker     /* And add it back in, sorted correctly. */
117*7ab6e6acSAndroid Build Coastguard Worker     list_add( t );
118*7ab6e6acSAndroid Build Coastguard Worker }
119*7ab6e6acSAndroid Build Coastguard Worker 
120*7ab6e6acSAndroid Build Coastguard Worker 
121*7ab6e6acSAndroid Build Coastguard Worker Timer*
tmr_create(struct iperf_time * nowP,TimerProc * timer_proc,TimerClientData client_data,int64_t usecs,int periodic)122*7ab6e6acSAndroid Build Coastguard Worker tmr_create(
123*7ab6e6acSAndroid Build Coastguard Worker     struct iperf_time* nowP, TimerProc* timer_proc, TimerClientData client_data,
124*7ab6e6acSAndroid Build Coastguard Worker     int64_t usecs, int periodic )
125*7ab6e6acSAndroid Build Coastguard Worker {
126*7ab6e6acSAndroid Build Coastguard Worker     struct iperf_time now;
127*7ab6e6acSAndroid Build Coastguard Worker     Timer* t;
128*7ab6e6acSAndroid Build Coastguard Worker 
129*7ab6e6acSAndroid Build Coastguard Worker     getnow( nowP, &now );
130*7ab6e6acSAndroid Build Coastguard Worker 
131*7ab6e6acSAndroid Build Coastguard Worker     if ( free_timers != NULL ) {
132*7ab6e6acSAndroid Build Coastguard Worker 	t = free_timers;
133*7ab6e6acSAndroid Build Coastguard Worker 	free_timers = t->next;
134*7ab6e6acSAndroid Build Coastguard Worker     } else {
135*7ab6e6acSAndroid Build Coastguard Worker 	t = (Timer*) malloc( sizeof(Timer) );
136*7ab6e6acSAndroid Build Coastguard Worker 	if ( t == NULL )
137*7ab6e6acSAndroid Build Coastguard Worker 	    return NULL;
138*7ab6e6acSAndroid Build Coastguard Worker     }
139*7ab6e6acSAndroid Build Coastguard Worker 
140*7ab6e6acSAndroid Build Coastguard Worker     t->timer_proc = timer_proc;
141*7ab6e6acSAndroid Build Coastguard Worker     t->client_data = client_data;
142*7ab6e6acSAndroid Build Coastguard Worker     t->usecs = usecs;
143*7ab6e6acSAndroid Build Coastguard Worker     t->periodic = periodic;
144*7ab6e6acSAndroid Build Coastguard Worker     t->time = now;
145*7ab6e6acSAndroid Build Coastguard Worker     iperf_time_add_usecs(&t->time, usecs);
146*7ab6e6acSAndroid Build Coastguard Worker     /* Add the new timer to the active list. */
147*7ab6e6acSAndroid Build Coastguard Worker     list_add( t );
148*7ab6e6acSAndroid Build Coastguard Worker 
149*7ab6e6acSAndroid Build Coastguard Worker     return t;
150*7ab6e6acSAndroid Build Coastguard Worker }
151*7ab6e6acSAndroid Build Coastguard Worker 
152*7ab6e6acSAndroid Build Coastguard Worker 
153*7ab6e6acSAndroid Build Coastguard Worker struct timeval*
tmr_timeout(struct iperf_time * nowP)154*7ab6e6acSAndroid Build Coastguard Worker tmr_timeout( struct iperf_time* nowP )
155*7ab6e6acSAndroid Build Coastguard Worker {
156*7ab6e6acSAndroid Build Coastguard Worker     struct iperf_time now, diff;
157*7ab6e6acSAndroid Build Coastguard Worker     int64_t usecs;
158*7ab6e6acSAndroid Build Coastguard Worker     int past;
159*7ab6e6acSAndroid Build Coastguard Worker     static struct timeval timeout;
160*7ab6e6acSAndroid Build Coastguard Worker 
161*7ab6e6acSAndroid Build Coastguard Worker     getnow( nowP, &now );
162*7ab6e6acSAndroid Build Coastguard Worker     /* Since the list is sorted, we only need to look at the first timer. */
163*7ab6e6acSAndroid Build Coastguard Worker     if ( timers == NULL )
164*7ab6e6acSAndroid Build Coastguard Worker 	return NULL;
165*7ab6e6acSAndroid Build Coastguard Worker     past = iperf_time_diff(&timers->time, &now, &diff);
166*7ab6e6acSAndroid Build Coastguard Worker     if (past)
167*7ab6e6acSAndroid Build Coastguard Worker         usecs = 0;
168*7ab6e6acSAndroid Build Coastguard Worker     else
169*7ab6e6acSAndroid Build Coastguard Worker         usecs = iperf_time_in_usecs(&diff);
170*7ab6e6acSAndroid Build Coastguard Worker     timeout.tv_sec = usecs / 1000000LL;
171*7ab6e6acSAndroid Build Coastguard Worker     timeout.tv_usec = usecs % 1000000LL;
172*7ab6e6acSAndroid Build Coastguard Worker     return &timeout;
173*7ab6e6acSAndroid Build Coastguard Worker }
174*7ab6e6acSAndroid Build Coastguard Worker 
175*7ab6e6acSAndroid Build Coastguard Worker 
176*7ab6e6acSAndroid Build Coastguard Worker void
tmr_run(struct iperf_time * nowP)177*7ab6e6acSAndroid Build Coastguard Worker tmr_run( struct iperf_time* nowP )
178*7ab6e6acSAndroid Build Coastguard Worker {
179*7ab6e6acSAndroid Build Coastguard Worker     struct iperf_time now;
180*7ab6e6acSAndroid Build Coastguard Worker     Timer* t;
181*7ab6e6acSAndroid Build Coastguard Worker     Timer* next;
182*7ab6e6acSAndroid Build Coastguard Worker 
183*7ab6e6acSAndroid Build Coastguard Worker     getnow( nowP, &now );
184*7ab6e6acSAndroid Build Coastguard Worker     for ( t = timers; t != NULL; t = next ) {
185*7ab6e6acSAndroid Build Coastguard Worker 	next = t->next;
186*7ab6e6acSAndroid Build Coastguard Worker 	/* Since the list is sorted, as soon as we find a timer
187*7ab6e6acSAndroid Build Coastguard Worker 	** that isn't ready yet, we are done.
188*7ab6e6acSAndroid Build Coastguard Worker 	*/
189*7ab6e6acSAndroid Build Coastguard Worker 	if (iperf_time_compare(&t->time, &now) > 0)
190*7ab6e6acSAndroid Build Coastguard Worker 	    break;
191*7ab6e6acSAndroid Build Coastguard Worker 	(t->timer_proc)( t->client_data, &now );
192*7ab6e6acSAndroid Build Coastguard Worker 	if ( t->periodic ) {
193*7ab6e6acSAndroid Build Coastguard Worker 	    /* Reschedule. */
194*7ab6e6acSAndroid Build Coastguard Worker 	    iperf_time_add_usecs(&t->time, t->usecs);
195*7ab6e6acSAndroid Build Coastguard Worker 	    list_resort( t );
196*7ab6e6acSAndroid Build Coastguard Worker 	} else
197*7ab6e6acSAndroid Build Coastguard Worker 	    tmr_cancel( t );
198*7ab6e6acSAndroid Build Coastguard Worker     }
199*7ab6e6acSAndroid Build Coastguard Worker }
200*7ab6e6acSAndroid Build Coastguard Worker 
201*7ab6e6acSAndroid Build Coastguard Worker 
202*7ab6e6acSAndroid Build Coastguard Worker void
tmr_reset(struct iperf_time * nowP,Timer * t)203*7ab6e6acSAndroid Build Coastguard Worker tmr_reset( struct iperf_time* nowP, Timer* t )
204*7ab6e6acSAndroid Build Coastguard Worker {
205*7ab6e6acSAndroid Build Coastguard Worker     struct iperf_time now;
206*7ab6e6acSAndroid Build Coastguard Worker 
207*7ab6e6acSAndroid Build Coastguard Worker     getnow( nowP, &now );
208*7ab6e6acSAndroid Build Coastguard Worker     t->time = now;
209*7ab6e6acSAndroid Build Coastguard Worker     iperf_time_add_usecs( &t->time, t->usecs );
210*7ab6e6acSAndroid Build Coastguard Worker     list_resort( t );
211*7ab6e6acSAndroid Build Coastguard Worker }
212*7ab6e6acSAndroid Build Coastguard Worker 
213*7ab6e6acSAndroid Build Coastguard Worker 
214*7ab6e6acSAndroid Build Coastguard Worker void
tmr_cancel(Timer * t)215*7ab6e6acSAndroid Build Coastguard Worker tmr_cancel( Timer* t )
216*7ab6e6acSAndroid Build Coastguard Worker {
217*7ab6e6acSAndroid Build Coastguard Worker     /* Remove it from the active list. */
218*7ab6e6acSAndroid Build Coastguard Worker     list_remove( t );
219*7ab6e6acSAndroid Build Coastguard Worker     /* And put it on the free list. */
220*7ab6e6acSAndroid Build Coastguard Worker     t->next = free_timers;
221*7ab6e6acSAndroid Build Coastguard Worker     free_timers = t;
222*7ab6e6acSAndroid Build Coastguard Worker     t->prev = NULL;
223*7ab6e6acSAndroid Build Coastguard Worker }
224*7ab6e6acSAndroid Build Coastguard Worker 
225*7ab6e6acSAndroid Build Coastguard Worker 
226*7ab6e6acSAndroid Build Coastguard Worker void
tmr_cleanup(void)227*7ab6e6acSAndroid Build Coastguard Worker tmr_cleanup( void )
228*7ab6e6acSAndroid Build Coastguard Worker {
229*7ab6e6acSAndroid Build Coastguard Worker     Timer* t;
230*7ab6e6acSAndroid Build Coastguard Worker 
231*7ab6e6acSAndroid Build Coastguard Worker     while ( free_timers != NULL ) {
232*7ab6e6acSAndroid Build Coastguard Worker 	t = free_timers;
233*7ab6e6acSAndroid Build Coastguard Worker 	free_timers = t->next;
234*7ab6e6acSAndroid Build Coastguard Worker 	free( (void*) t );
235*7ab6e6acSAndroid Build Coastguard Worker     }
236*7ab6e6acSAndroid Build Coastguard Worker }
237*7ab6e6acSAndroid Build Coastguard Worker 
238*7ab6e6acSAndroid Build Coastguard Worker 
239*7ab6e6acSAndroid Build Coastguard Worker void
tmr_destroy(void)240*7ab6e6acSAndroid Build Coastguard Worker tmr_destroy( void )
241*7ab6e6acSAndroid Build Coastguard Worker {
242*7ab6e6acSAndroid Build Coastguard Worker     while ( timers != NULL )
243*7ab6e6acSAndroid Build Coastguard Worker 	tmr_cancel( timers );
244*7ab6e6acSAndroid Build Coastguard Worker     tmr_cleanup();
245*7ab6e6acSAndroid Build Coastguard Worker }
246