forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOSCompatPosix.cpp
More file actions
638 lines (537 loc) · 15.6 KB
/
OSCompatPosix.cpp
File metadata and controls
638 lines (537 loc) · 15.6 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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#if !defined(_WINDOWS) && !defined(__EMSCRIPTEN__)
#include "hermes/Support/Compiler.h"
#include "hermes/Support/ErrorHandling.h"
#include "hermes/Support/OSCompat.h"
#include <cassert>
#include <vector>
#include <signal.h>
#include <sys/mman.h>
#include <sys/resource.h>
#if defined(__linux__)
#if !defined(RUSAGE_THREAD)
#define RUSAGE_THREAD 1
#endif
#endif // __linux__
#include <sys/types.h>
#include <unistd.h>
#ifdef __MACH__
#include <mach/mach.h>
#ifdef __APPLE__
#include <pthread.h>
#endif // __APPLE__
#endif // __MACH__
#ifdef __linux__
#if !defined(_POSIX_TIMERS) || _POSIX_TIMERS <= 0
#error "Timers not supported on this Android platform."
#endif
#ifndef CLOCK_THREAD_CPUTIME_ID
#error "CLOCK_THREAD_CPUTIME_ID not supported by clock_gettime"
#endif
#include <sys/syscall.h>
#include <time.h>
#endif // __linux__
#if defined(__linux__) || defined(__ANDROID__)
#include <sys/prctl.h>
#endif
#ifdef __ANDROID__
#ifndef PR_SET_VMA
#define PR_SET_VMA 0x53564d41
#endif
#ifndef PR_SET_VMA_ANON_NAME
#define PR_SET_VMA_ANON_NAME 0
#endif
#endif // __ANDROID__
#include "llvh/Support/raw_ostream.h"
namespace hermes {
namespace oscompat {
#ifndef NDEBUG
static size_t testPgSz = 0;
void set_test_page_size(size_t pageSz) {
testPgSz = pageSz;
}
void reset_test_page_size() {
testPgSz = 0;
}
#endif
static inline size_t page_size_real() {
return getpagesize();
}
size_t page_size() {
#ifndef NDEBUG
if (testPgSz != 0) {
return testPgSz;
}
#endif
return page_size_real();
}
#ifndef NDEBUG
static constexpr size_t unsetVMAllocLimit = std::numeric_limits<size_t>::max();
static size_t totalVMAllocLimit = unsetVMAllocLimit;
void set_test_vm_allocate_limit(size_t totSz) {
totalVMAllocLimit = totSz;
}
void unset_test_vm_allocate_limit() {
totalVMAllocLimit = unsetVMAllocLimit;
}
#endif // !NDEBUG
static llvh::ErrorOr<void *> vm_allocate_impl(size_t sz) {
#ifndef NDEBUG
if (LLVM_UNLIKELY(sz > totalVMAllocLimit)) {
return make_error_code(OOMError::TestVMLimitReached);
} else if (LLVM_UNLIKELY(totalVMAllocLimit != unsetVMAllocLimit)) {
totalVMAllocLimit -= sz;
}
#endif // !NDEBUG
void *result = mmap(
nullptr, sz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (result == MAP_FAILED) {
// Since mmap is a POSIX API, even on MacOS, errno should use the POSIX
// generic_category.
return std::error_code(errno, std::generic_category());
}
return result;
}
static char *alignAlloc(void *p, size_t alignment) {
return reinterpret_cast<char *>(
llvh::alignTo(reinterpret_cast<uintptr_t>(p), alignment));
}
llvh::ErrorOr<void *> vm_allocate(size_t sz) {
assert(sz % page_size() == 0);
#ifndef NDEBUG
if (testPgSz != 0 && testPgSz > static_cast<size_t>(page_size_real())) {
return vm_allocate_aligned(sz, testPgSz);
}
#endif // !NDEBUG
return vm_allocate_impl(sz);
}
llvh::ErrorOr<void *> vm_allocate_aligned(size_t sz, size_t alignment) {
assert(sz > 0 && sz % page_size() == 0);
assert(alignment > 0 && alignment % page_size() == 0);
// Opportunistically allocate without alignment constraint,
// and see if the memory happens to be aligned.
// While this may be unlikely on the first allocation request,
// subsequent allocation requests have a good chance.
auto result = vm_allocate_impl(sz);
if (!result) {
return result;
}
void *mem = *result;
if (mem == alignAlloc(mem, alignment)) {
return mem;
}
// Free the oppotunistic allocation.
oscompat::vm_free(mem, sz);
// This time, allocate a larger section to ensure that it contains
// a subsection that satisfies the request.
// Use *real* page size here since that's what vm_allocate_impl guarantees.
const size_t excessSize = sz + alignment - page_size_real();
result = vm_allocate_impl(excessSize);
if (!result)
return result;
void *raw = *result;
char *aligned = alignAlloc(raw, alignment);
size_t excessAtFront = aligned - static_cast<char *>(raw);
size_t excessAtBack = excessSize - excessAtFront - sz;
if (excessAtFront)
oscompat::vm_free(raw, excessAtFront);
if (excessAtBack)
oscompat::vm_free(aligned + sz, excessAtBack);
return aligned;
}
void vm_free(void *p, size_t sz) {
auto ret = munmap(p, sz);
assert(!ret && "Failed to free memory region.");
(void)ret;
#ifndef NDEBUG
if (LLVM_UNLIKELY(totalVMAllocLimit != unsetVMAllocLimit) && p) {
totalVMAllocLimit += sz;
}
#endif
}
void vm_free_aligned(void *p, size_t sz) {
vm_free(p, sz);
}
void vm_hugepage(void *p, size_t sz) {
assert(
reinterpret_cast<uintptr_t>(p) % page_size() == 0 &&
"Precondition: pointer is page-aligned.");
#if defined(__linux__) || defined(__ANDROID__)
// Since the alloc is aligned, it may benefit from huge pages.
madvise(p, sz, MADV_HUGEPAGE);
#endif
}
void vm_unused(void *p, size_t sz) {
#ifndef NDEBUG
const size_t PS = page_size();
assert(
reinterpret_cast<intptr_t>(p) % PS == 0 &&
"Precondition: pointer is page-aligned.");
#endif
/// Change the flag we pass to \p madvise based on the platform, so that we are
/// always acting to reduce memory pressure, as perceived by that platform.
#if defined(__MACH__)
/// On the mach kernel, \p MADV_FREE causes the OS to deduct this memory from
/// the process's physical footprint.
#define MADV_UNUSED MADV_FREE
#elif defined(__linux__)
/// On linux, telling the OS that we \p MADV_DONTNEED some pages will cause it
/// to immediately deduct their size from the process's resident set.
#define MADV_UNUSED MADV_DONTNEED
#else
#error "Don't know how to return memory to the OS on this platform."
#endif // __MACH__, __linux__
madvise(p, sz, MADV_UNUSED);
#undef MADV_UNUSED
}
void vm_prefetch(void *p, size_t sz) {
assert(
reinterpret_cast<intptr_t>(p) % page_size() == 0 &&
"Precondition: pointer is page-aligned.");
madvise(p, sz, MADV_WILLNEED);
}
void vm_name(void *p, size_t sz, const char *name) {
#ifdef __ANDROID__
prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, p, sz, name);
#else
(void)p;
(void)sz;
(void)name;
#endif // __ANDROID__
}
bool vm_protect(void *p, size_t sz, ProtectMode mode) {
auto prot = PROT_NONE;
if (mode == ProtectMode::ReadWrite) {
prot = PROT_WRITE | PROT_READ;
}
int err = mprotect(p, sz, prot);
return err != -1;
}
bool vm_madvise(void *p, size_t sz, MAdvice advice) {
#ifndef NDEBUG
const size_t PS = page_size();
assert(
reinterpret_cast<intptr_t>(p) % PS == 0 &&
"Precondition: pointer is page-aligned.");
#endif
int param = MADV_NORMAL;
switch (advice) {
case MAdvice::Random:
param = MADV_RANDOM;
break;
case MAdvice::Sequential:
param = MADV_SEQUENTIAL;
break;
}
return madvise(p, sz, param) == 0;
}
int pages_in_ram(const void *p, size_t sz, llvh::SmallVectorImpl<int> *runs) {
const auto PS = page_size();
{
// Align region start down to page boundary.
const uintptr_t addr = reinterpret_cast<uintptr_t>(p);
const size_t adjust = addr % PS;
p = reinterpret_cast<const void *>(addr - adjust);
sz += adjust;
}
// Total number of pages that the region overlaps.
const size_t mapSize = (sz + PS - 1) / PS;
#ifdef __linux__
using MapElm = unsigned char;
#else
using MapElm = char;
#endif
std::vector<MapElm> bitMap(mapSize);
if (mincore(const_cast<void *>(p), sz, bitMap.data())) {
return -1;
}
// Total pages in RAM.
int totalIn = 0;
bool currentRunStatus = true;
if (runs)
runs->push_back(0);
for (auto elm : bitMap) {
// Lowest bit tells whether in RAM.
bool thisStatus = (elm & 1);
totalIn += thisStatus;
if (runs) {
if (thisStatus != currentRunStatus)
runs->push_back(0);
currentRunStatus = thisStatus;
++runs->back();
}
}
return totalIn;
}
uint64_t peak_rss() {
rusage ru;
if (getrusage(RUSAGE_SELF, &ru)) {
// failed
return 0;
}
uint64_t rss = ru.ru_maxrss;
#if !defined(__APPLE__) || !defined(__MACH__)
// Linux maxrss is in kilobytes, expand into bytes.
rss *= 1024;
#endif
return rss;
}
uint64_t current_rss() {
#if defined(__APPLE__) && defined(__MACH__)
struct mach_task_basic_info info;
mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT;
if (task_info(
mach_task_self(),
MACH_TASK_BASIC_INFO,
(task_info_t)&info,
&infoCount) != KERN_SUCCESS)
return 0;
return info.resident_size * page_size_real();
#else
FILE *fp = fopen("/proc/self/statm", "r");
if (!fp) {
return 0;
}
long rss = 0;
// The first field is total program size, second field is resident set size.
if (fscanf(fp, "%*ld %ld", &rss) != 1) {
fclose(fp);
return 0;
}
fclose(fp);
// The RSS number from from statm is in number of pages. Multiply by the real
// page size to get the number in bytes.
return rss * page_size_real();
#endif
}
uint64_t current_private_dirty() {
#if defined(__linux__)
uint64_t sum = 0;
FILE *fp = fopen("/proc/self/smaps", "r");
static const char kPrefix[] = "Private_Dirty:";
constexpr size_t kPrefixLen = sizeof(kPrefix) - 1;
char buf[128]; // Just needs to fit the lines we care about.
while (fgets(buf, sizeof(buf), fp))
if (strncmp(buf, kPrefix, kPrefixLen) == 0)
sum += atoll(buf + kPrefixLen);
fclose(fp);
return sum * 1024;
#else
return 0;
#endif
}
#if defined(__linux__)
static bool overlap(uintptr_t a, size_t asize, uintptr_t b, size_t bsize) {
// An empty interval has no overlap.
if (asize == 0 || bsize == 0)
return false;
// Order by start address.
if (a > b)
return overlap(b, bsize, a, asize);
// Overlap iff the first interval extends beyond the start of the second.
return a + asize > b;
}
#endif
std::vector<std::string> get_vm_protect_modes(const void *p, size_t sz) {
std::vector<std::string> modes;
#if defined(__linux__)
FILE *fp = fopen("/proc/self/maps", "r");
unsigned long long begin;
unsigned long long end;
char mode[4 + 1];
while (fscanf(fp, "%llx-%llx %4s", &begin, &end, mode) == 3) {
if (overlap(
reinterpret_cast<uintptr_t>(p),
sz,
static_cast<uintptr_t>(begin),
static_cast<size_t>(end - begin))) {
modes.push_back(mode);
}
// Discard remainder of the line.
int result;
do {
result = fgetc(fp);
} while (result != '\n' && result > 0);
}
#endif
return modes;
}
bool num_context_switches(long &voluntary, long &involuntary) {
voluntary = involuntary = -1;
rusage ru;
// Only Linux is known to have RUSAGE_THREAD.
#if defined(__linux__)
const int who = RUSAGE_THREAD;
#else
const int who = RUSAGE_SELF;
#endif
if (getrusage(who, &ru)) {
// failed
return false;
}
voluntary = ru.ru_nvcsw;
involuntary = ru.ru_nivcsw;
return true;
}
// Platform-specific implementations of thread_id
#if defined(__APPLE__) && defined(__MACH__)
uint64_t thread_id() {
uint64_t tid = 0;
auto ret = pthread_threadid_np(nullptr, &tid);
assert(ret == 0 && "pthread_threadid_np shouldn't fail for current thread");
(void)ret;
return tid;
}
#elif defined(__ANDROID__)
uint64_t thread_id() {
return gettid();
}
#elif defined(__linux__)
uint64_t thread_id() {
return syscall(__NR_gettid);
}
#else
#error "Thread ID not supported on this platform"
#endif
void set_thread_name(const char *name) {
// Set the thread name for TSAN. It doesn't share the same name mapping as the
// OS does. This macro expands to nothing if TSAN isn't on.
TsanThreadName(name);
#if defined(__linux__) || defined(__ANDROID__)
prctl(PR_SET_NAME, name);
#elif defined(__APPLE__)
::pthread_setname_np(name);
#endif
// Do nothing if the platform doesn't support it.
}
// Platform-specific implementations of thread_cpu_time
#if defined(__APPLE__) && defined(__MACH__)
std::chrono::microseconds thread_cpu_time() {
using namespace std::chrono;
struct thread_basic_info tbi;
mach_port_t self = pthread_mach_thread_np(pthread_self());
mach_msg_type_number_t fields = THREAD_BASIC_INFO_COUNT;
if (thread_info(self, THREAD_BASIC_INFO, (thread_info_t)&tbi, &fields) !=
KERN_SUCCESS) {
return microseconds::max();
}
microseconds::rep total = 0;
total += tbi.user_time.microseconds;
total += tbi.user_time.seconds * 1000000;
total += tbi.system_time.microseconds;
total += tbi.system_time.seconds * 1000000;
return microseconds(total);
}
#elif defined(__linux__) // !(__APPLE__ && __MACH__)
std::chrono::microseconds thread_cpu_time() {
using namespace std::chrono;
struct timespec ts;
if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) != 0) {
return microseconds::max();
}
microseconds::rep total = 0;
total += ts.tv_nsec / 1000;
total += ts.tv_sec * 1000000;
return microseconds(total);
}
#else // !(__APPLE__ && __MACH__), !__linux__
#error "Thread CPU Time not supported on this platform"
#endif // thread_cpu_time: (__APPLE__ && __MACH__), __linux__
// Platform-specific implementations of thread_page_fault_count
#if defined(__APPLE__) && defined(__MACH__)
bool thread_page_fault_count(int64_t *outMinorFaults, int64_t *outMajorFaults) {
task_events_info eventsInfo;
mach_msg_type_number_t count = TASK_EVENTS_INFO_COUNT;
kern_return_t kr = task_info(
mach_task_self(), TASK_EVENTS_INFO, (task_info_t)&eventsInfo, &count);
if (kr == KERN_SUCCESS) {
*outMinorFaults = eventsInfo.faults;
*outMajorFaults = eventsInfo.pageins;
}
return kr == KERN_SUCCESS;
}
#elif defined(__linux__) // !(__APPLE__ && __MACH__)
bool thread_page_fault_count(int64_t *outMinorFaults, int64_t *outMajorFaults) {
struct rusage stats = {};
int ret = getrusage(RUSAGE_THREAD, &stats);
if (ret == 0) {
*outMinorFaults = stats.ru_minflt;
*outMajorFaults = stats.ru_majflt;
}
return ret == 0;
}
#else // !(__APPLE__ && __MACH__), !__linux__
#error "Thread page fault count not supported on this platform"
#endif // thread_page_fault_count: (__APPLE__ && __MACH__), __linux__
std::string thread_name() {
constexpr int kMaxThreadNameSize = 100;
int ret = 0;
char threadName[kMaxThreadNameSize];
#ifdef __ANDROID__
ret = prctl(PR_GET_NAME, threadName);
#else
ret = pthread_getname_np(pthread_self(), threadName, sizeof(threadName));
#endif
if (ret != 0) {
// thread name error should be non-fatal, simply return empty thread name.
perror("thread_name failed");
return "";
}
return threadName;
}
#ifdef __linux__
std::vector<bool> sched_getaffinity() {
std::vector<bool> v;
cpu_set_t mask;
CPU_ZERO(&mask);
int status = ::sched_getaffinity(0, sizeof(mask), &mask);
if (status != 0) {
return v;
}
int lastSet = -1;
for (int cpu = 0; cpu < CPU_SETSIZE; ++cpu) {
v.push_back(CPU_ISSET(cpu, &mask));
if (v.back())
lastSet = cpu;
}
// Trim trailing zeroes.
v.resize(lastSet + 1);
return v;
}
int sched_getcpu() {
return ::sched_getcpu();
}
#else
std::vector<bool> sched_getaffinity() {
// Not yet supported.
return std::vector<bool>();
}
int sched_getcpu() {
// Not yet supported.
return -1;
}
#endif
bool set_env(const char *name, const char *value) {
// Enforce the contract of this function that value must not be empty
assert(*value != '\0' && "value cannot be empty string");
return setenv(name, value, 1) == 0;
}
bool unset_env(const char *name) {
return unsetenv(name) == 0;
}
/*static*/
void *SigAltStackLeakSuppressor::stackRoot_{nullptr};
SigAltStackLeakSuppressor::~SigAltStackLeakSuppressor() {
stack_t oldAltStack;
if (sigaltstack(nullptr, &oldAltStack) == 0) {
stackRoot_ = oldAltStack.ss_sp;
}
}
} // namespace oscompat
} // namespace hermes
#endif // not _WINDOWS