GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — async-profiler/src/ctimer_linux.cpp at jonoffcpu-dev · jonoffcpu/async-profiler

Expand file tree

/

ctimer_linux.cpp

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
122 lines (100 loc) · 3.09 KB
/

ctimer_linux.cpp

Copy path

File metadata and controls

122 lines (100 loc) · 3.09 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
* Copyright The async-profiler authors
* SPDX-License-Identifier: Apache-2.0
*/
#ifdef __linux__
#include <stdlib.h>
#include <sys/syscall.h>
#include <time.h>
#include <unistd.h>
#include "ctimer.h"
#include "j9StackTraces.h"
#include "profiler.h"
#include "stackWalker.h"
#ifndef SIGEV_THREAD_ID
#define SIGEV_THREAD_ID 4
#endif
static inline clockid_t thread_cpu_clock(unsigned int tid) {
return ((~tid) << 3) | 6; // CPUCLOCK_SCHED | CPUCLOCK_PERTHREAD_MASK
}
int CTimer::_max_timers = 0;
int* CTimer::_timers = NULL;
int CTimer::createForThread(int tid) {
if (tid >= _max_timers) {
Log::warn("tid[%d] > pid_max[%d]. Restart profiler after changing pid_max", tid, _max_timers);
return -1;
}
struct sigevent sev;
sev.sigev_value.sival_ptr = NULL;
sev.sigev_signo = _signal;
sev.sigev_notify = SIGEV_THREAD_ID;
(&sev.sigev_notify)[1] = tid;
// Use raw syscalls, since libc wrapper allows only predefined clocks
clockid_t clock = thread_cpu_clock(tid);
int timer;
if (syscall(__NR_timer_create, clock, &sev, &timer) < 0) {
return -1;
}
// Kernel timer ID may start with zero, but we use zero as an empty slot
if (!__sync_bool_compare_and_swap(&_timers[tid], 0, timer + 1)) {
// Lost race
syscall(__NR_timer_delete, timer);
return -1;
}
struct itimerspec ts;
ts.it_interval.tv_sec = (time_t)(_interval / 1000000000);
ts.it_interval.tv_nsec = _interval % 1000000000;
ts.it_value = ts.it_interval;
syscall(__NR_timer_settime, timer, 0, &ts, NULL);
return 0;
}
void CTimer::destroyForThread(int tid) {
if (tid >= _max_timers) {
return;
}
int timer = _timers[tid];
if (timer != 0 && __sync_bool_compare_and_swap(&_timers[tid], timer--, 0)) {
syscall(__NR_timer_delete, timer);
}
}
Error CTimer::start(Arguments& args) {
if (args._interval < 0) {
return Error("interval must be positive");
}
_interval = args._interval ? args._interval : DEFAULT_INTERVAL;
_cstack = args._cstack;
_signal = args._signal == 0 ? OS::getProfilingSignal(0) : args._signal & 0xff;
_count_overrun = true;
int max_timers = OS::getMaxThreadId();
if (max_timers != _max_timers) {
free(_timers);
_timers = (int*)calloc(max_timers, sizeof(int));
_max_timers = max_timers;
}
if (VM::isOpenJ9()) {
OS::installSignalHandler(_signal, signalHandlerJ9);
Error error = J9StackTraces::start(args);
if (error) {
return error;
}
} else {
OS::installSignalHandler(_signal, signalHandler);
}
// Let pthread hook create timers for new threads before traversing existing threads
enableThreadEvents();
// Create timers for all existing threads
int err = createForAllThreads();
if (err) {
stop();
return Error("Failed to create CPU timer");
}
return Error::OK;
}
void CTimer::stop() {
disableThreadEvents();
for (int i = 0; i < _max_timers; i++) {
destroyForThread(i);
}
J9StackTraces::stop();
}
#endif // __linux__
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.