OpenOCD
time_support.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2006 by Dominic Rath *
5  * Dominic.Rath@gmx.de *
6  * *
7  * Copyright (C) 2007,2008 Øyvind Harboe *
8  * oyvind.harboe@zylin.com *
9  * *
10  * Copyright (C) 2008 by Spencer Oliver *
11  * spen@spen-soft.co.uk *
12  ***************************************************************************/
13 
14 #ifdef HAVE_CONFIG_H
15 #include "config.h"
16 #endif
17 
18 #include <helper/log.h>
19 
20 #include "time_support.h"
21 
22 /* calculate difference between two struct timeval values */
23 int timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y)
24 {
25  if (x->tv_usec < y->tv_usec) {
26  int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
27  y->tv_usec -= 1000000 * nsec;
28  y->tv_sec += nsec;
29  }
30  if (x->tv_usec - y->tv_usec > 1000000) {
31  int nsec = (x->tv_usec - y->tv_usec) / 1000000;
32  y->tv_usec += 1000000 * nsec;
33  y->tv_sec -= nsec;
34  }
35 
36  result->tv_sec = x->tv_sec - y->tv_sec;
37  result->tv_usec = x->tv_usec - y->tv_usec;
38 
39  /* Return 1 if result is negative. */
40  return x->tv_sec < y->tv_sec;
41 }
42 
43 int timeval_add_time(struct timeval *result, long sec, long usec)
44 {
45  result->tv_sec += sec;
46  result->tv_usec += usec;
47 
48  while (result->tv_usec > 1000000) {
49  result->tv_usec -= 1000000;
50  result->tv_sec++;
51  }
52 
53  return 0;
54 }
55 
56 /* compare two timevals and return -1/0/+1 accordingly */
57 int timeval_compare(const struct timeval *x, const struct timeval *y)
58 {
59  if (x->tv_sec < y->tv_sec)
60  return -1;
61  else if (x->tv_sec > y->tv_sec)
62  return 1;
63  else if (x->tv_usec < y->tv_usec)
64  return -1;
65  else if (x->tv_usec > y->tv_usec)
66  return 1;
67  else
68  return 0;
69 }
70 
72 {
73  if (gettimeofday(&duration->start, NULL) != 0)
74  return ERROR_FAIL;
75 
76  return ERROR_OK;
77 }
78 
80 {
81  struct timeval end;
82  if (gettimeofday(&end, NULL) != 0)
83  return ERROR_FAIL;
84 
86 
87  return ERROR_OK;
88 }
89 
90 float duration_elapsed(const struct duration *duration)
91 {
92  float t = duration->elapsed.tv_sec;
93  t += (float)duration->elapsed.tv_usec / 1000000.0;
94  return t;
95 }
96 
97 float duration_kbps(const struct duration *duration, size_t count)
98 {
99  return count / (1024.0 * duration_elapsed(duration));
100 }
#define ERROR_FAIL
Definition: log.h:188
#define ERROR_OK
Definition: log.h:182
int gettimeofday(struct timeval *tv, struct timezone *tz)
struct timeval elapsed
Definition: time_support.h:33
struct timeval start
Definition: time_support.h:32
long tv_sec
Definition: replacements.h:46
long tv_usec
Definition: replacements.h:47
int timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y)
Definition: time_support.c:23
float duration_elapsed(const struct duration *duration)
Definition: time_support.c:90
int timeval_compare(const struct timeval *x, const struct timeval *y)
Definition: time_support.c:57
int timeval_add_time(struct timeval *result, long sec, long usec)
Definition: time_support.c:43
int duration_measure(struct duration *duration)
Update the duration->elapsed field to finish the duration measurement.
Definition: time_support.c:79
int duration_start(struct duration *duration)
Update the duration->start field to start the duration measurement.
Definition: time_support.c:71
float duration_kbps(const struct duration *duration, size_t count)
Definition: time_support.c:97
#define NULL
Definition: usb.h:16
uint8_t count[4]
Definition: vdebug.c:22