File: timer.c

package info (click to toggle)
gavl 1.4.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 14,912 kB
  • sloc: ansic: 437,319; sh: 10,487; makefile: 238
file content (185 lines) | stat: -rw-r--r-- 4,127 bytes parent folder | download | duplicates (3)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*****************************************************************
 * gavl - a general purpose audio/video processing library
 *
 * Copyright (c) 2001 - 2011 Members of the Gmerlin project
 * gmerlin-general@lists.sourceforge.net
 * http://gmerlin.sourceforge.net
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * *****************************************************************/

#include <config.h>

#include <inttypes.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#ifdef HAVE_SYS_TIMES_H
#include <sys/times.h>
#endif
#include <sys/time.h>

#include <gavl.h>

#ifdef HAVE_CLOCK_MONOTONIC

/* High precision version */
static void get_time(gavl_time_t * ret)
  {
  struct timespec time;
  clock_gettime(CLOCK_MONOTONIC, &time);
  *ret = (int64_t)(time.tv_sec)*1000000LL + (time.tv_nsec)/1000;
  }

#else
static void get_time(gavl_time_t * ret)
  {
  struct timeval time;
  gettimeofday(&time, NULL);
  *ret = (int64_t)(time.tv_sec)*1000000LL + time.tv_usec;
  }
#endif

struct gavl_timer_s
  {
  int64_t start_time_real;
  int64_t start_time;
  int is_running;
  };

gavl_timer_t * gavl_timer_create()
  {
  gavl_timer_t * ret = calloc(1, sizeof(*ret));
  return ret;
  }

void gavl_timer_destroy(gavl_timer_t * t)
  {
  free(t);
  }

void gavl_timer_start(gavl_timer_t * t)
  {
  get_time(&t->start_time_real);
  t->is_running = 1;
  }

void gavl_timer_stop(gavl_timer_t * t)
  {
  gavl_time_t tmp;
  tmp = gavl_timer_get(t);
  t->start_time = tmp;
  t->is_running = 0;
  }

gavl_time_t gavl_timer_get(gavl_timer_t * t)
  {
  gavl_time_t ret;
  if(t->is_running)
    {
    get_time(&ret);
    ret -= t->start_time_real;
    ret += t->start_time;
    return ret;
    }
  else
    return t->start_time;
  }

void gavl_timer_set(gavl_timer_t * t, gavl_time_t v)
  {
  t->start_time = v;
  }

/* */

#ifdef HAVE_CLOCK_GETTIME

uint64_t gavl_benchmark_get_time(int config_flags)
  {
  struct timespec ts;
#if defined(CLOCK_PROCESS_CPUTIME_ID)
  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
#elif defined(CLOCK_PROF)
  clock_gettime(CLOCK_PROF, &ts);
#else
  clockgettime(CLOCK_REALTIME, &ts);
#endif

  return (uint64_t)(ts.tv_sec) * 1000000000 + ts.tv_nsec;
  }

const char * gavl_benchmark_get_desc(int flags)
  {
  return "nanoseconds returned by clock_gettime with CLOCK_PROCESS_CPUTIME_ID";
  }

#elif defined(ARCH_X86) || defined(HAVE_SYS_TIMES_H)

uint64_t gavl_benchmark_get_time(int config_flags)
  {
  struct timeval tv;
#ifdef ARCH_X86
  uint64_t x;
  /* that should prevent us from trying cpuid with old cpus */
  if( config_flags & GAVL_ACCEL_MMX )
    {
    __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
    return x;
    }
  else
    {
#endif
    gettimeofday(&tv, NULL);
    return (uint64_t)(tv.tv_sec) * 1000000 + tv.tv_usec;
#ifdef ARCH_X86
  }
#endif
  }

const char * gavl_benchmark_get_desc(int config_flags)
  {
#ifdef ARCH_X86
  /* that should prevent us from trying cpuid with old cpus */
  if( config_flags & GAVL_ACCEL_MMX )
    {
    return "Units returned by rdtsc";
    }
  else
    {
#endif
    return "microseconds returned by gettimeofday";
#ifdef ARCH_X86
    }
#endif
  }

#else /* gettimeofday */

uint64_t gavl_benchmark_get_time(int config_flags)
  {
  struct timeval tv;
  gettimeofday(&tv, NULL);
  return (uint64_t)(tv.tv_sec) * 1000000 + tv.tv_usec;
  /* FIXME: implement an equivalent for using optimized memcpy on other
     architectures */
  }

const char * gavl_benchmark_get_desc(int flags)
  {
  return "microseconds returned by gettimeofday";
  }

#endif