forked from shimonran/RDMA_Hello
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_clock.c
235 lines (209 loc) · 5.58 KB
/
get_clock.c
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* vim: set noet: */
/*
* Copyright (c) 2005 Mellanox Technologies Ltd. All rights reserved.
*
* This software is available to you under a choice of one of two
* licenses. You may choose to be licensed under the terms of the GNU
* General Public License (GPL) Version 2, available from the file
* COPYING in the main directory of this source tree, or the
* OpenIB.org BSD license below:
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* $Id$
*
* Author: Michael S. Tsirkin <mst@mellanox.co.il>
*/
/* #define DEBUG 1 */
/* #define DEBUG_DATA 1 */
/* #define GET_CPU_MHZ_FROM_PROC 1 */
/* For gettimeofday */
#define _DEFAULT_SOURCE
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include "get_clock.h"
#ifndef DEBUG
#define DEBUG 0
#endif
#ifndef DEBUG_DATA
#define DEBUG_DATA 0
#endif
#define MEASUREMENTS 200
#define USECSTEP 10
#define USECSTART 100
/*
Use linear regression to calculate cycles per microsecond.
http://en.wikipedia.org/wiki/Linear_regression#Parameter_estimation
*/
static double sample_get_cpu_mhz(void)
{
struct timeval tv1, tv2;
cycles_t start;
double sx = 0, sy = 0, sxx = 0, syy = 0, sxy = 0;
double tx, ty;
int i;
/* Regression: y = a + b x */
long x[MEASUREMENTS];
cycles_t y[MEASUREMENTS];
double a; /* system call overhead in cycles */
double b; /* cycles per microsecond */
double r_2;
for (i = 0; i < MEASUREMENTS; ++i) {
start = get_cycles();
if (gettimeofday(&tv1, NULL)) {
fprintf(stderr, "gettimeofday failed.\n");
return 0;
}
do {
if (gettimeofday(&tv2, NULL)) {
fprintf(stderr, "gettimeofday failed.\n");
return 0;
}
} while ((tv2.tv_sec - tv1.tv_sec) * 1000000 +
(tv2.tv_usec - tv1.tv_usec) < USECSTART + i * USECSTEP);
x[i] = (tv2.tv_sec - tv1.tv_sec) * 1000000 +
tv2.tv_usec - tv1.tv_usec;
y[i] = get_cycles() - start;
if (DEBUG_DATA)
fprintf(stderr, "x=%ld y=%Ld\n", x[i], (long long)y[i]);
}
for (i = 0; i < MEASUREMENTS; ++i) {
tx = x[i];
ty = y[i];
sx += tx;
sy += ty;
sxx += tx * tx;
syy += ty * ty;
sxy += tx * ty;
}
b = (MEASUREMENTS * sxy - sx * sy) / (MEASUREMENTS * sxx - sx * sx);
a = (sy - b * sx) / MEASUREMENTS;
if (DEBUG)
fprintf(stderr, "a = %g\n", a);
if (DEBUG)
fprintf(stderr, "b = %g\n", b);
if (DEBUG)
fprintf(stderr, "a / b = %g\n", a / b);
r_2 = (MEASUREMENTS * sxy - sx * sy) * (MEASUREMENTS * sxy - sx * sy) /
(MEASUREMENTS * sxx - sx * sx) /
(MEASUREMENTS * syy - sy * sy);
if (DEBUG)
fprintf(stderr, "r^2 = %g\n", r_2);
if (r_2 < 0.9) {
fprintf(stderr,"Correlation coefficient r^2: %g < 0.9\n", r_2);
return 0;
}
return b;
}
#if !defined(__s390x__) && !defined(__s390__)
static double proc_get_cpu_mhz(int no_cpu_freq_warn)
{
FILE* f;
char buf[256];
double mhz = 0.0;
int print_flag = 0;
double delta;
#if defined(__FreeBSD__)
f = popen("/sbin/sysctl hw.clockrate","r");
#else
f = fopen("/proc/cpuinfo","r");
#endif
if (!f)
return 0.0;
while(fgets(buf, sizeof(buf), f)) {
double m;
int rc;
#if defined (__ia64__)
/* Use the ITC frequency on IA64 */
rc = sscanf(buf, "itc MHz : %lf", &m);
#elif defined (__PPC__) || defined (__PPC64__)
/* PPC has a different format as well */
rc = sscanf(buf, "clock : %lf", &m);
#elif defined (__sparc__) && defined (__arch64__)
/*
* on sparc the /proc/cpuinfo lines that hold
* the cpu freq in HZ are as follow:
* Cpu{cpu-num}ClkTck : 00000000a9beeee4
*/
char *s;
s = strstr(buf, "ClkTck\t: ");
if (!s)
continue;
s += (strlen("ClkTck\t: ") - strlen("0x"));
strncpy(s, "0x", strlen("0x"));
rc = sscanf(s, "%lf", &m);
m /= 1000000;
#else
#if defined (__FreeBSD__)
rc = sscanf(buf, "hw.clockrate: %lf", &m);
#else
rc = sscanf(buf, "cpu MHz : %lf", &m);
#endif
#endif
if (rc != 1)
continue;
if (mhz == 0.0) {
mhz = m;
continue;
}
delta = mhz > m ? mhz - m : m - mhz;
if ((delta / mhz > 0.02) && (print_flag ==0)) {
print_flag = 1;
if (!no_cpu_freq_warn) {
fprintf(stderr, "Conflicting CPU frequency values"
" detected: %lf != %lf. CPU Frequency is not max.\n", mhz, m);
}
continue;
}
}
#if defined(__FreeBSD__)
pclose(f);
#else
fclose(f);
#endif
return mhz;
}
#endif
double get_cpu_mhz(int no_cpu_freq_warn)
{
#if defined(__s390x__) || defined(__s390__)
return sample_get_cpu_mhz();
#else
double sample, proc, delta;
sample = sample_get_cpu_mhz();
proc = proc_get_cpu_mhz(no_cpu_freq_warn);
#ifdef __aarch64__
if (proc < 1)
proc = sample;
#endif
if (!proc || !sample)
return 0;
delta = proc > sample ? proc - sample : sample - proc;
if (delta / proc > 0.02) {
return sample;
}
return proc;
#endif
}