/* * Copyright (c) 2009-2011, 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. * * This code is distributed under a BSD style license, see the LICENSE file * for complete information. */ #include #include #include #include #include #include #include #include #include #include "timer.h" #include "iperf_api.h" int timer_expired(struct timer * tp) { if (tp == NULL) return 0; struct timeval now; int64_t end = 0, current = 0; gettimeofday(&now, NULL); end += tp->end.tv_sec * 1000000; end += tp->end.tv_usec; current += now.tv_sec * 1000000; current += now.tv_usec; return current > end; } int update_timer(struct timer * tp, time_t sec, suseconds_t usec) { if (gettimeofday(&tp->begin, NULL) < 0) { i_errno = IEUPDATETIMER; return (-1); } tp->end.tv_sec = tp->begin.tv_sec + (time_t) sec; tp->end.tv_usec = tp->begin.tv_usec + (time_t) usec; tp->expired = timer_expired; return (0); } struct timer * new_timer(time_t sec, suseconds_t usec) { struct timer *tp = NULL; tp = (struct timer *) calloc(1, sizeof(struct timer)); if (tp == NULL) { i_errno = IENEWTIMER; return (NULL); } if (gettimeofday(&tp->begin, NULL) < 0) { i_errno = IENEWTIMER; return (NULL); } tp->end.tv_sec = tp->begin.tv_sec + (time_t) sec; tp->end.tv_usec = tp->begin.tv_usec + (time_t) usec; tp->expired = timer_expired; return tp; } void free_timer(struct timer * tp) { free(tp); } int64_t timer_remaining(struct timer * tp) { struct timeval now; long int end_time = 0, current_time = 0, diff = 0; gettimeofday(&now, NULL); end_time += tp->end.tv_sec * 1000000; end_time += tp->end.tv_usec; current_time += now.tv_sec * 1000000; current_time += now.tv_usec; diff = end_time - current_time; if (diff > 0) return diff; else return 0; }