FD.io VPP  v16.06
Vector Packet Processing
time.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*
16  Copyright (c) 2005 Eliot Dresselhaus
17 
18  Permission is hereby granted, free of charge, to any person obtaining
19  a copy of this software and associated documentation files (the
20  "Software"), to deal in the Software without restriction, including
21  without limitation the rights to use, copy, modify, merge, publish,
22  distribute, sublicense, and/or sell copies of the Software, and to
23  permit persons to whom the Software is furnished to do so, subject to
24  the following conditions:
25 
26  The above copyright notice and this permission notice shall be
27  included in all copies or substantial portions of the Software.
28 
29  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 */
37 
38 #include <vppinfra/os.h>
39 #include <vppinfra/time.h>
40 #include <vppinfra/format.h>
41 
42 #ifdef CLIB_UNIX
43 
44 #include <math.h>
45 #include <sys/time.h>
46 #include <fcntl.h>
47 
48 /* Not very accurate way of determining cpu clock frequency
49  for unix. Better to use /proc/cpuinfo on linux. */
50 static f64 estimate_clock_frequency (f64 sample_time)
51 {
52  /* Round to nearest 100KHz. */
53  const f64 round_to_units = 100e5;
54 
55  f64 time_now, time_start, time_limit, freq;
56  u64 ifreq, t[2];
57 
58  time_start = time_now = unix_time_now ();
59  time_limit = time_now + sample_time;
60  t[0] = clib_cpu_time_now ();
61  while (time_now < time_limit)
62  time_now = unix_time_now ();
63  t[1] = clib_cpu_time_now ();
64 
65  freq = (t[1] - t[0]) / (time_now - time_start);
66  ifreq = flt_round_nearest (freq / round_to_units);
67  freq = ifreq * round_to_units;
68 
69  return freq;
70 }
71 
72 /* Fetch cpu frequency via parseing /proc/cpuinfo.
73  Only works for Linux. */
75 {
76  f64 cpu_freq=1e9; /* better than 40... */
77  f64 ppc_timebase=0; /* warnings be gone */
78  int fd;
79  unformat_input_t input;
80 
81 /* $$$$ aarch64 kernel doesn't report "cpu MHz" */
82 #if defined(__aarch64__)
83  return 0.0;
84 #endif
85 
86  cpu_freq = 0;
87  fd = open ("/proc/cpuinfo", 0);
88  if (fd < 0)
89  return cpu_freq;
90 
91  unformat_init_unix_file (&input, fd);
92 
93  ppc_timebase = 0;
95  {
96  if (unformat (&input, "cpu MHz : %f", &cpu_freq))
97  cpu_freq *= 1e6;
98  else if (unformat (&input, "timebase : %f", &ppc_timebase))
99  ;
100  else
101  unformat_skip_line (&input);
102  }
103 
104  unformat_free (&input);
105 
106  close (fd);
107 
108  /* Override CPU frequency with time base for PPC. */
109  if (ppc_timebase != 0)
110  cpu_freq = ppc_timebase;
111 
112  return cpu_freq;
113 }
114 
115 /* Fetch cpu frequency via reading /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
116  Only works for Linux. */
118 {
119  f64 cpu_freq;
120  int fd;
121  unformat_input_t input;
122 
123  /* Time stamp always runs at max frequency. */
124  cpu_freq = 0;
125  fd = open ("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq", 0);
126  if (fd < 0)
127  goto done;
128 
129  unformat_init_unix_file (&input, fd);
130  unformat (&input, "%f", &cpu_freq);
131  cpu_freq *= 1e3; /* measured in kHz */
132  unformat_free (&input);
133  close (fd);
134  done:
135  return cpu_freq;
136 }
137 
139 {
140  f64 cpu_freq;
141 
142  /* First try /sys version. */
144  if (cpu_freq != 0)
145  return cpu_freq;
146 
147  /* Next try /proc version. */
149  if (cpu_freq != 0)
150  return cpu_freq;
151 
152  /* If /proc/cpuinfo fails (e.g. not running on Linux) fall back to
153  gettimeofday based estimated clock frequency. */
154  return estimate_clock_frequency (1e-3);
155 }
156 
157 #endif /* CLIB_UNIX */
158 
159 /* Initialize time. */
161 {
162  memset (c, 0, sizeof (c[0]));
166 
167  /* Initially verify frequency every sec */
169 
173 }
174 
176 {
177  f64 now_reference = unix_time_now ();
178  f64 dtr = now_reference - c->last_verify_reference_time;
179  f64 dtr_max;
180  u64 dtc = c->last_cpu_time - c->last_verify_cpu_time;
181  f64 round_units = 100e5;
182 
184  c->last_verify_reference_time = now_reference;
185 
186  /*
187  * Is the reported reference interval non-positive,
188  * or off by a factor of two - or 8 seconds - whichever is larger?
189  * Someone reset the clock behind our back.
190  */
191  dtr_max = (f64)(2ULL<<c->log2_clocks_per_frequency_verify) /
192  (f64)(1ULL<<c->log2_clocks_per_second);
193  dtr_max = dtr_max > 8.0 ? dtr_max : 8.0;
194 
195  if (dtr <= 0.0 || dtr > dtr_max)
196  {
198  return;
199  }
200 
201  c->clocks_per_second = flt_round_nearest ((f64) dtc / (dtr * round_units)) * round_units;
203 
204  /* Double time between verifies; max at 64 secs ~ 1 minute. */
207 }
always_inline f64 unix_time_now(void)
Definition: time.h:208
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:942
always_inline void unformat_free(unformat_input_t *i)
Definition: format.h:160
#define UNFORMAT_END_OF_INPUT
Definition: format.h:142
f64 clocks_per_second
Definition: time.h:52
u32 log2_clocks_per_second
Definition: time.h:66
u64 last_verify_cpu_time
Definition: time.h:61
always_inline uword unformat_check_input(unformat_input_t *i)
Definition: format.h:168
unsigned long u64
Definition: types.h:89
u32 log2_clocks_per_frequency_verify
Definition: time.h:66
f64 seconds_per_clock
Definition: time.h:56
always_inline void unformat_skip_line(unformat_input_t *i)
Definition: format.h:217
void clib_time_init(clib_time_t *c)
Definition: time.c:160
void clib_time_verify_frequency(clib_time_t *c)
Definition: time.c:175
always_inline u64 min_log2_u64(u64 x)
Definition: clib.h:224
void unformat_init_unix_file(unformat_input_t *input, int file_descriptor)
Definition: unformat.c:1026
static f64 clock_frequency_from_sys_filesystem(void)
Definition: time.c:117
u64 last_cpu_time
Definition: time.h:49
f64 os_cpu_clock_frequency(void)
Definition: time.c:138
static f64 clock_frequency_from_proc_filesystem(void)
Definition: time.c:74
f64 last_verify_reference_time
Definition: time.h:64
double f64
Definition: types.h:140
static f64 estimate_clock_frequency(f64 sample_time)
Definition: time.c:50
always_inline word flt_round_nearest(f64 x)
Definition: clib.h:282
u64 init_cpu_time
Definition: time.h:59
struct _unformat_input_t unformat_input_t
always_inline u64 clib_cpu_time_now(void)
Definition: time.h:71