-
-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathPlatform.cpp
125 lines (107 loc) · 2.95 KB
/
Platform.cpp
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
#include "Platform.h"
#include <memory>
#include <stdio.h>
#include <math.h> // for lrint
#include <assert.h>
long getenvlong(const char *name, long minval, long defval, long maxval)
{
assert(minval <= defval && defval <= maxval);
const char *s = getenv(name);
if (!s)
return defval;
char *tail;
long l = strtol(s, &tail, 0);
if (*tail)
return defval;
if (l < minval) l = minval;
if (l > maxval) l = maxval;
return l;
}
struct StampedRdtsc
{
const uint64_t ticks, ns;
StampedRdtsc() : ticks(timer_start()), ns(timeofday()) { }
bool GoodDelta(const StampedRdtsc& t) const {
constexpr uint64_t ms20 = 20*1000*1000; // 20ms, two ticks of HZ=100
constexpr uint64_t mln2 = 2*1000*1000; // 20ms @ 100 MHz = 2M ticks
const uint64_t dtick = timer_sub(ticks, t.ticks);
return mln2 <= dtick && dtick != timer_inf && ms20 <= (ns - t.ns);
}
unsigned int FreqMHzSince(const StampedRdtsc& t) const {
return lrint(double(timer_sub(ticks, t.ticks)) / (ns - t.ns) * 1e9 / 1e6);
}
};
static unsigned int CpuFreqMHz;
static std::unique_ptr<StampedRdtsc> BaseTS;
void SampleCpuFreq ( void )
{
if (!BaseTS) {
if (CpuFreqMHz)
return;
else
BaseTS.reset(new StampedRdtsc());
}
StampedRdtsc now;
if (now.GoodDelta(*BaseTS))
CpuFreqMHz = now.FreqMHzSince(*BaseTS);
}
unsigned int GetCpuFreqMHz( void )
{
if (BaseTS)
BaseTS.reset();
return CpuFreqMHz;
}
#if defined(_WIN32)
#include <windows.h>
void SetAffinity ( int cpu )
{
SetProcessAffinityMask(GetCurrentProcess(),cpu);
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
}
#if NCPU > 1
void SetThreadAffinity ( std::thread &t, int cpu )
{
SetThreadIdealProcessor((HANDLE)t.native_handle(), (DWORD)cpu);
}
#endif
uint64_t timeofday(void)
{
// GetTickCount ~ Windows 2000+
// GetTickCount64 ~ Vista+, Server 2008+
// QueryUnbiasedInterruptTime ~ Windows 7+, Server 2008 R2
// see https://learn.microsoft.com/en-us/windows/win32/winprog/using-the-windows-headers
unsigned long long ns100;
if (QueryUnbiasedInterruptTime(&ns100))
return ns100 * 100u;
return GetTickCount64() * 1000000U;
}
#else
#include <sched.h>
void SetAffinity ( int /*cpu*/ )
{
#if !defined(__CYGWIN__) && !defined(__APPLE__) && !defined(__FreeBSD__)
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(2,&mask);
if( sched_setaffinity(0,sizeof(mask),&mask) == -1)
{
printf("WARNING: Could not set CPU affinity\n");
}
#endif
}
#if NCPU > 1
void SetThreadAffinity ( std::thread &t, int cpu )
{
#if !defined(__CYGWIN__) && !defined(__APPLE__) && !defined(__FreeBSD__)
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(cpu, &cpuset);
pthread_setaffinity_np(t.native_handle(), sizeof(cpu_set_t), &cpuset);
#elif defined(__APPLE__)
thread_affinity_policy_data_t policy = { cpu };
thread_policy_set(pthread_mach_thread_np(t.native_handle()), THREAD_AFFINITY_POLICY,
(thread_policy_t)&policy, 1);
#endif
}
#endif
#endif