-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
pal_time.c
178 lines (148 loc) · 5.42 KB
/
pal_time.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#include "pal_config.h"
#include "pal_time.h"
#include "pal_utilities.h"
#include <assert.h>
#include <utime.h>
#include <time.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/resource.h>
#if HAVE_CLOCK_GETTIME_NSEC_NP
#include <time.h>
#endif
enum
{
MicroSecondsToNanoSeconds = 1000, // 10^3
SecondsToNanoSeconds = 1000000000, // 10^9
SecondsToTicks = 10000000, // 10^7
TicksToNanoSeconds = 100, // 10^2
};
int32_t SystemNative_UTimensat(const char* path, TimeSpec* times)
{
int32_t result;
#if HAVE_UTIMENSAT
struct timespec updatedTimes[2];
updatedTimes[0].tv_sec = (time_t)times[0].tv_sec;
updatedTimes[0].tv_nsec = (long)times[0].tv_nsec;
updatedTimes[1].tv_sec = (time_t)times[1].tv_sec;
updatedTimes[1].tv_nsec = (long)times[1].tv_nsec;
while (CheckInterrupted(result = utimensat(AT_FDCWD, path, updatedTimes, AT_SYMLINK_NOFOLLOW)));
#else
struct timeval updatedTimes[2];
updatedTimes[0].tv_sec = (long)times[0].tv_sec;
updatedTimes[0].tv_usec = (int)times[0].tv_nsec / 1000;
updatedTimes[1].tv_sec = (long)times[1].tv_sec;
updatedTimes[1].tv_usec = (int)times[1].tv_nsec / 1000;
while (CheckInterrupted(result =
#if HAVE_LUTIMES
lutimes
#else
utimes
#endif
(path, updatedTimes)));
#endif
return result;
}
int32_t SystemNative_FUTimens(intptr_t fd, TimeSpec* times)
{
int32_t result;
#if HAVE_FUTIMENS
struct timespec updatedTimes[2];
updatedTimes[0].tv_sec = (time_t)times[0].tv_sec;
updatedTimes[0].tv_nsec = (long)times[0].tv_nsec;
updatedTimes[1].tv_sec = (time_t)times[1].tv_sec;
updatedTimes[1].tv_nsec = (long)times[1].tv_nsec;
while (CheckInterrupted(result = futimens(ToFileDescriptor(fd), updatedTimes)));
#else
// Fallback on unsupported platforms (e.g. iOS, tvOS, watchOS)
// to futimes (lower precision)
struct timeval updatedTimes[2];
updatedTimes[0].tv_sec = (long)times[0].tv_sec;
updatedTimes[0].tv_usec = (int)times[0].tv_nsec / 1000;
updatedTimes[1].tv_sec = (long)times[1].tv_sec;
updatedTimes[1].tv_usec = (int)times[1].tv_nsec / 1000;
while (CheckInterrupted(result = futimes(ToFileDescriptor(fd), updatedTimes)));
#endif
return result;
}
uint64_t SystemNative_GetTimestamp(void)
{
#if HAVE_CLOCK_GETTIME_NSEC_NP
return clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
#else
struct timespec ts;
int result = clock_gettime(CLOCK_MONOTONIC, &ts);
assert(result == 0); // only possible errors are if MONOTONIC isn't supported or &ts is an invalid address
(void)result; // suppress unused parameter warning in release builds
return ((uint64_t)(ts.tv_sec) * SecondsToNanoSeconds) + (uint64_t)(ts.tv_nsec);
#endif
}
int64_t SystemNative_GetBootTimeTicks(void)
{
#if defined(TARGET_LINUX) || defined(TARGET_ANDROID)
struct timespec ts;
int result = clock_gettime(CLOCK_BOOTTIME, &ts);
assert(result == 0); // only possible errors are if the given clockId isn't supported or &ts is an invalid address
(void)result; // suppress unused parameter warning in release builds
int64_t sinceBootTicks = ((int64_t)ts.tv_sec * SecondsToTicks) + (ts.tv_nsec / TicksToNanoSeconds);
result = clock_gettime(CLOCK_REALTIME_COARSE, &ts);
assert(result == 0);
int64_t sinceEpochTicks = ((int64_t)ts.tv_sec * SecondsToTicks) + (ts.tv_nsec / TicksToNanoSeconds);
const int64_t UnixEpochTicks = 621355968000000000;
return UnixEpochTicks + sinceEpochTicks - sinceBootTicks;
#else
return -1;
#endif
}
double SystemNative_GetCpuUtilization(ProcessCpuInformation* previousCpuInfo)
{
#if defined(HAVE_GETRUSAGE) && !defined(HOST_BROWSER)
uint64_t kernelTime = 0;
uint64_t userTime = 0;
struct rusage resUsage;
if (getrusage(RUSAGE_SELF, &resUsage) == -1)
{
assert(false);
return 0;
}
else
{
kernelTime =
((uint64_t)(resUsage.ru_stime.tv_sec) * SecondsToNanoSeconds) +
((uint64_t)(resUsage.ru_stime.tv_usec) * MicroSecondsToNanoSeconds);
userTime =
((uint64_t)(resUsage.ru_utime.tv_sec) * SecondsToNanoSeconds) +
((uint64_t)(resUsage.ru_utime.tv_usec) * MicroSecondsToNanoSeconds);
}
uint64_t currentTime = SystemNative_GetTimestamp();
uint64_t lastRecordedCurrentTime = previousCpuInfo->lastRecordedCurrentTime;
uint64_t lastRecordedKernelTime = previousCpuInfo->lastRecordedKernelTime;
uint64_t lastRecordedUserTime = previousCpuInfo->lastRecordedUserTime;
uint64_t cpuTotalTime = 0;
if (currentTime > lastRecordedCurrentTime)
{
cpuTotalTime = (currentTime - lastRecordedCurrentTime);
}
uint64_t cpuBusyTime = 0;
if (userTime >= lastRecordedUserTime && kernelTime >= lastRecordedKernelTime)
{
cpuBusyTime = (userTime - lastRecordedUserTime) + (kernelTime - lastRecordedKernelTime);
}
double cpuUtilization = 0.0;
if (cpuTotalTime > 0 && cpuBusyTime > 0)
{
cpuUtilization = ((double)cpuBusyTime * 100.0 / (double)cpuTotalTime);
}
previousCpuInfo->lastRecordedCurrentTime = currentTime;
previousCpuInfo->lastRecordedUserTime = userTime;
previousCpuInfo->lastRecordedKernelTime = kernelTime;
return cpuUtilization;
#else
(void)previousCpuInfo; // unused
assert(false);
return 0;
#endif
}