-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathos_linux.cpp
More file actions
367 lines (308 loc) · 8.14 KB
/
os_linux.cpp
File metadata and controls
367 lines (308 loc) · 8.14 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
* Copyright 2018 Andrei Pangin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef __linux__
#include "os.h"
#include <arpa/inet.h>
#include <byteswap.h>
#include <dirent.h>
#include <fcntl.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/sendfile.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <sys/times.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#ifndef __musl__
#include <malloc.h>
#endif
#ifdef __LP64__
#define MMAP_SYSCALL __NR_mmap
#else
#define MMAP_SYSCALL __NR_mmap2
#endif
class LinuxThreadList : public ThreadList {
private:
DIR *_dir;
int _thread_count;
int getThreadCount() {
char buf[512];
int fd = open("/proc/self/stat", O_RDONLY);
if (fd == -1) {
return 0;
}
int thread_count = 0;
if (read(fd, buf, sizeof(buf)) > 0) {
char *s = strchr(buf, ')');
if (s != NULL) {
// Read 18th integer field after the command name
for (int field = 0; *s != ' ' || ++field < 18; s++)
;
thread_count = atoi(s + 1);
}
}
close(fd);
return thread_count;
}
public:
LinuxThreadList() {
_dir = opendir("/proc/self/task");
_thread_count = -1;
}
~LinuxThreadList() {
if (_dir != NULL) {
closedir(_dir);
}
}
void rewind() {
if (_dir != NULL) {
rewinddir(_dir);
}
_thread_count = -1;
}
int next() {
if (_dir != NULL) {
struct dirent *entry;
while ((entry = readdir(_dir)) != NULL) {
if (entry->d_name[0] != '.') {
return atoi(entry->d_name);
}
}
}
return -1;
}
int size() {
if (_thread_count < 0) {
_thread_count = getThreadCount();
}
return _thread_count;
}
};
JitWriteProtection::JitWriteProtection(bool enable) {
// Not used on Linux
}
JitWriteProtection::~JitWriteProtection() {
// Not used on Linux
}
const size_t OS::page_size = sysconf(_SC_PAGESIZE);
const size_t OS::page_mask = OS::page_size - 1;
u64 OS::nanotime() {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (u64)ts.tv_sec * 1000000000 + ts.tv_nsec;
}
u64 OS::cputime() {
struct timespec ts;
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
return (u64)ts.tv_sec * 1000000000 + ts.tv_nsec;
}
u64 OS::micros() {
struct timeval tv;
gettimeofday(&tv, NULL);
return (u64)tv.tv_sec * 1000000 + tv.tv_usec;
}
u64 OS::processStartTime() {
static u64 start_time = 0;
if (start_time == 0) {
char buf[64];
snprintf(buf, sizeof(buf), "/proc/%d", processId());
struct stat st;
if (stat(buf, &st) == 0) {
start_time = (u64)st.st_mtim.tv_sec * 1000 + st.st_mtim.tv_nsec / 1000000;
}
}
return start_time;
}
void OS::sleep(u64 nanos) {
struct timespec ts = {(time_t)(nanos / 1000000000),
(long)(nanos % 1000000000)};
nanosleep(&ts, NULL);
}
u64 OS::hton64(u64 x) { return htonl(1) == 1 ? x : bswap_64(x); }
u64 OS::ntoh64(u64 x) { return ntohl(1) == 1 ? x : bswap_64(x); }
int OS::getMaxThreadId() {
static volatile int maxThreadId = -1;
if (__atomic_load_n(&maxThreadId, __ATOMIC_ACQUIRE) == -1) {
char buf[16] = "65536";
int fd = open("/proc/sys/kernel/pid_max", O_RDONLY);
if (fd != -1) {
ssize_t r = read(fd, buf, sizeof(buf) - 1);
(void)r;
close(fd);
}
__atomic_store_n(&maxThreadId, atoi(buf), __ATOMIC_RELEASE);
}
return maxThreadId;
}
int OS::processId() {
static const int self_pid = getpid();
return self_pid;
}
int OS::threadId() { return syscall(__NR_gettid); }
const char *OS::schedPolicy(int thread_id) {
int sched_policy = sched_getscheduler(thread_id);
if (sched_policy >= SCHED_BATCH) {
return sched_policy >= SCHED_IDLE ? "SCHED_IDLE" : "SCHED_BATCH";
}
return "SCHED_OTHER";
}
bool OS::threadName(int thread_id, char *name_buf, size_t name_len) {
char buf[64];
snprintf(buf, sizeof(buf), "/proc/self/task/%d/comm", thread_id);
int fd = open(buf, O_RDONLY);
if (fd == -1) {
return false;
}
ssize_t r = read(fd, name_buf, name_len);
close(fd);
if (r > 0) {
name_buf[r - 1] = 0;
return true;
}
return false;
}
ThreadList *OS::listThreads() { return new LinuxThreadList(); }
bool OS::isLinux() { return true; }
SigAction OS::installSignalHandler(int signo, SigAction action,
SigHandler handler) {
struct sigaction sa;
struct sigaction oldsa;
sigemptyset(&sa.sa_mask);
if (handler != NULL) {
sa.sa_handler = handler;
sa.sa_flags = 0;
} else {
sa.sa_sigaction = action;
sa.sa_flags = SA_SIGINFO | SA_RESTART;
}
sigaction(signo, &sa, &oldsa);
return oldsa.sa_sigaction;
}
SigAction OS::replaceSigsegvHandler(SigAction action) {
struct sigaction sa;
sigaction(SIGSEGV, NULL, &sa);
SigAction old_action = sa.sa_sigaction;
sa.sa_sigaction = action;
sa.sa_flags = SA_SIGINFO;
sigaction(SIGSEGV, &sa, NULL);
return old_action;
}
SigAction OS::replaceSigbusHandler(SigAction action) {
struct sigaction sa;
sigaction(SIGBUS, NULL, &sa);
SigAction old_action = sa.sa_sigaction;
sa.sa_sigaction = action;
sigaction(SIGBUS, &sa, NULL);
return old_action;
}
bool OS::sendSignalToThread(int thread_id, int signo) {
return syscall(__NR_tgkill, processId(), thread_id, signo) == 0;
}
void *OS::safeAlloc(size_t size) {
// Naked syscall can be used inside a signal handler.
// Also, we don't want to catch our own calls when profiling mmap.
intptr_t result = syscall(MMAP_SYSCALL, NULL, size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (result < 0 && result > -4096) {
return NULL;
}
return (void *)result;
}
void OS::safeFree(void *addr, size_t size) { syscall(__NR_munmap, addr, size); }
int OS::mprotect(void *addr, size_t len, int prot) {
return mprotect(addr, len, prot);
}
bool OS::getCpuDescription(char *buf, size_t size) {
int fd = open("/proc/cpuinfo", O_RDONLY);
if (fd == -1) {
return false;
}
ssize_t r = read(fd, buf, size);
close(fd);
if (r <= 0) {
return false;
}
buf[r < size ? r : size - 1] = 0;
char *c;
do {
c = strchr(buf, '\n');
} while (c != NULL && *(buf = c + 1) != '\n');
*buf = 0;
return true;
}
u64 OS::getProcessCpuTime(u64 *utime, u64 *stime) {
struct tms buf;
clock_t real = times(&buf);
*utime = buf.tms_utime;
*stime = buf.tms_stime;
return real;
}
u64 OS::getTotalCpuTime(u64 *utime, u64 *stime) {
int fd = open("/proc/stat", O_RDONLY);
if (fd == -1) {
return (u64)-1;
}
u64 real = (u64)-1;
char buf[512];
if (read(fd, buf, sizeof(buf)) >= 12) {
u64 user, nice, system, idle;
if (sscanf(buf + 4, "%llu %llu %llu %llu", &user, &nice, &system, &idle) ==
4) {
*utime = user + nice;
*stime = system;
real = user + nice + system + idle;
}
}
close(fd);
return real;
}
void OS::copyFile(int src_fd, int dst_fd, off_t offset, size_t size) {
// copy_file_range() is probably better, but not supported on all kernels
while (size > 0) {
ssize_t bytes = sendfile(dst_fd, src_fd, &offset, size);
if (bytes <= 0) {
break;
}
size -= (size_t)bytes;
}
}
int OS::truncateFile(int fd) {
int rslt = ftruncate(fd, 0);
if (rslt == 0) {
return lseek(fd, 0, SEEK_SET);
}
return rslt;
}
int OS::fileSize(int fd) {
struct stat fileinfo = {0};
fstat(fd, &fileinfo);
return fileinfo.st_size;
}
void OS::freePageCache(int fd, off_t start_offset) {
posix_fadvise(fd, start_offset & ~page_mask, 0, POSIX_FADV_DONTNEED);
}
void OS::mallocArenaMax(int arena_max) {
#ifndef __musl__
mallopt(M_ARENA_MAX, arena_max);
#endif
}
#endif // __linux__