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 — 'itimer' profiling mode (like 'cpu', but does not require perf_events… · jonoffcpu/async-profiler@a2c70c9

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • arguments.h
    • itimer.cpp
    • itimer.h
    • perfEvents.h
    • perfEvents_linux.cpp
    • perfEvents_macos.cpp
    • profiler.cpp
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/arguments.h‎

Copy file name to clipboardExpand all lines: src/arguments.h
+5-4Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
const long DEFAULT_INTERVAL = 10000000; // 10 ms
2424
const int DEFAULT_FRAMEBUF = 1000000;
2525

26-
const char* const EVENT_CPU = "cpu";
27-
const char* const EVENT_ALLOC = "alloc";
28-
const char* const EVENT_LOCK = "lock";
29-
const char* const EVENT_WALL = "wall";
26+
const char* const EVENT_CPU = "cpu";
27+
const char* const EVENT_ALLOC = "alloc";
28+
const char* const EVENT_LOCK = "lock";
29+
const char* const EVENT_WALL = "wall";
30+
const char* const EVENT_ITIMER = "itimer";
3031

3132
enum Action {
3233
ACTION_NONE,
Collapse file

‎src/itimer.cpp‎

Copy file name to clipboard
+49Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2018 Andrei Pangin
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <sys/time.h>
18+
#include "itimer.h"
19+
#include "os.h"
20+
#include "profiler.h"
21+
22+
23+
long ITimer::_interval;
24+
25+
26+
void ITimer::signalHandler(int signo, siginfo_t* siginfo, void* ucontext) {
27+
Profiler::_instance.recordSample(ucontext, _interval, 0, NULL);
28+
}
29+
30+
Error ITimer::start(Arguments& args) {
31+
if (args._interval < 0) {
32+
return Error("interval must be positive");
33+
}
34+
_interval = args._interval ? args._interval : DEFAULT_INTERVAL;
35+
36+
OS::installSignalHandler(SIGPROF, signalHandler);
37+
38+
long sec = _interval / 1000000000;
39+
long usec = (_interval % 1000000000) / 1000;
40+
struct itimerval tv = {{sec, usec}, {sec, usec}};
41+
setitimer(ITIMER_PROF, &tv, NULL);
42+
43+
return Error::OK;
44+
}
45+
46+
void ITimer::stop() {
47+
struct itimerval tv = {{0, 0}, {0, 0}};
48+
setitimer(ITIMER_PROF, &tv, NULL);
49+
}
Collapse file

‎src/itimer.h‎

Copy file name to clipboard
+43Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2018 Andrei Pangin
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef _ITIMER_H
18+
#define _ITIMER_H
19+
20+
#include <signal.h>
21+
#include "engine.h"
22+
23+
24+
class ITimer : public Engine {
25+
private:
26+
static long _interval;
27+
28+
static void signalHandler(int signo, siginfo_t* siginfo, void* ucontext);
29+
30+
public:
31+
const char* name() {
32+
return "itimer";
33+
}
34+
35+
const char* units() {
36+
return "ns";
37+
}
38+
39+
Error start(Arguments& args);
40+
void stop();
41+
};
42+
43+
#endif // _ITIMER_H
Collapse file

‎src/perfEvents.h‎

Copy file name to clipboardExpand all lines: src/perfEvents.h
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ class PerfEvents : public Engine {
5454
int getNativeTrace(void* ucontext, int tid, const void** callchain, int max_depth,
5555
const void* jit_min_address, const void* jit_max_address);
5656

57-
static const char** getAvailableEvents();
57+
static bool supported();
58+
static const char* getEventName(int event_id);
5859

5960
static void JNICALL ThreadStart(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {
6061
createForThread(OS::threadId());
Collapse file

‎src/perfEvents_linux.cpp‎

Copy file name to clipboardExpand all lines: src/perfEvents_linux.cpp
+21-14Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ struct PerfEventType {
110110
static FunctionWithCounter KNOWN_FUNCTIONS[];
111111

112112
// Find which argument of a known function serves as a profiling counter,
113-
// e.g. the first argument of malloc() is allocation size
113+
// e.g. the first argument of malloc() is allocation size
114114
static int findCounterArg(const char* name) {
115115
for (FunctionWithCounter* func = KNOWN_FUNCTIONS; func->name != NULL; func++) {
116116
if (strcmp(name, func->name) == 0) {
@@ -308,19 +308,24 @@ bool PerfEvents::createForThread(int tid) {
308308
return false;
309309
}
310310

311+
PerfEventType* event_type = _event_type;
312+
if (event_type == NULL) {
313+
return false;
314+
}
315+
311316
struct perf_event_attr attr = {0};
312317
attr.size = sizeof(attr);
313-
attr.type = _event_type->type;
318+
attr.type = event_type->type;
314319

315320
if (attr.type == PERF_TYPE_BREAKPOINT) {
316-
attr.bp_addr = _event_type->config;
317-
attr.bp_type = _event_type->bp_type;
318-
attr.bp_len = _event_type->bp_len;
321+
attr.bp_addr = event_type->config;
322+
attr.bp_type = event_type->bp_type;
323+
attr.bp_len = event_type->bp_len;
319324
} else {
320-
attr.config = _event_type->config;
325+
attr.config = event_type->config;
321326
}
322327

323-
attr.precise_ip = _event_type->precise_ip;
328+
attr.precise_ip = event_type->precise_ip;
324329
attr.sample_period = _interval;
325330
attr.sample_type = PERF_SAMPLE_CALLCHAIN;
326331
attr.disabled = 1;
@@ -468,6 +473,8 @@ Error PerfEvents::start(Arguments& args) {
468473
jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_THREAD_END, NULL);
469474

470475
if (!createForAllThreads()) {
476+
jvmti->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_THREAD_START, NULL);
477+
jvmti->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_THREAD_END, NULL);
471478
return Error("Perf events unavailable. See stderr of the target process.");
472479
}
473480
return Error::OK;
@@ -525,15 +532,15 @@ int PerfEvents::getNativeTrace(void* ucontext, int tid, const void** callchain,
525532
return depth;
526533
}
527534

528-
const char** PerfEvents::getAvailableEvents() {
529-
int count = sizeof(PerfEventType::AVAILABLE_EVENTS) / sizeof(PerfEventType);
530-
const char** available_events = new const char*[count];
535+
bool PerfEvents::supported() {
536+
return true;
537+
}
531538

532-
for (int i = 0; i < count; i++) {
533-
available_events[i] = PerfEventType::AVAILABLE_EVENTS[i].name;
539+
const char* PerfEvents::getEventName(int event_id) {
540+
if (event_id >= 0 && event_id < sizeof(PerfEventType::AVAILABLE_EVENTS) / sizeof(PerfEventType)) {
541+
return PerfEventType::AVAILABLE_EVENTS[event_id].name;
534542
}
535-
536-
return available_events;
543+
return NULL;
537544
}
538545

539546
#endif // __linux__
Collapse file

‎src/perfEvents_macos.cpp‎

Copy file name to clipboardExpand all lines: src/perfEvents_macos.cpp
+8-29Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616

1717
#ifdef __APPLE__
1818

19-
#include <string.h>
20-
#include <sys/time.h>
2119
#include "perfEvents.h"
22-
#include "profiler.h"
2320

2421

2522
int PerfEvents::_max_events;
@@ -37,48 +34,30 @@ void PerfEvents::destroyForAllThreads() {}
3734

3835

3936
void PerfEvents::signalHandler(int signo, siginfo_t* siginfo, void* ucontext) {
40-
Profiler::_instance.recordSample(ucontext, _interval, 0, NULL);
4137
}
4238

4339
const char* PerfEvents::units() {
4440
return "ns";
4541
}
4642

4743
Error PerfEvents::start(Arguments& args) {
48-
if (strcmp(args._event, EVENT_CPU) != 0) {
49-
return Error("Event is not supported on this platform");
50-
}
51-
52-
if (args._interval < 0) {
53-
return Error("interval must be positive");
54-
}
55-
_interval = args._interval ? args._interval : DEFAULT_INTERVAL;
56-
57-
OS::installSignalHandler(SIGPROF, signalHandler);
58-
59-
long sec = _interval / 1000000000;
60-
long usec = (_interval % 1000000000) / 1000;
61-
struct itimerval tv = {{sec, usec}, {sec, usec}};
62-
setitimer(ITIMER_PROF, &tv, NULL);
63-
64-
return Error::OK;
44+
return Error("PerfEvents are unsupported on macOS");
6545
}
6646

6747
void PerfEvents::stop() {
68-
struct itimerval tv = {{0, 0}, {0, 0}};
69-
setitimer(ITIMER_PROF, &tv, NULL);
7048
}
7149

7250
int PerfEvents::getNativeTrace(void* ucontext, int tid, const void** callchain, int max_depth,
7351
const void* jit_min_address, const void* jit_max_address) {
74-
return Engine::getNativeTrace(ucontext, tid, callchain, max_depth, jit_min_address, jit_max_address);
52+
return 0;
53+
}
54+
55+
bool PerfEvents::supported() {
56+
return false;
7557
}
7658

77-
const char** PerfEvents::getAvailableEvents() {
78-
const char** available_events = new const char*[2];
79-
available_events[0] = "cpu";
80-
available_events[1] = NULL;
81-
return available_events;
59+
const char* PerfEvents::getEventName(int event_id) {
60+
return NULL;
8261
}
8362

8463
#endif // __APPLE__
Collapse file

‎src/profiler.cpp‎

Copy file name to clipboardExpand all lines: src/profiler.cpp
+25-17Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "allocTracer.h"
2727
#include "lockTracer.h"
2828
#include "wallClock.h"
29+
#include "itimer.h"
2930
#include "flameGraph.h"
3031
#include "flightRecorder.h"
3132
#include "frameName.h"
@@ -412,21 +413,24 @@ void Profiler::initJvmtiFunctions(NativeCodeCache* libjvm) {
412413
}
413414

414415
Engine* Profiler::selectEngine(const char* event_name) {
415-
static Engine* PERF_EVENTS = new PerfEvents();
416-
static Engine* ALLOC_TRACER = new AllocTracer();
417-
static Engine* LOCK_TRACER = new LockTracer();
418-
static Engine* WALL_CLOCK = new WallClock();
416+
static PerfEvents perf_events;
417+
static AllocTracer alloc_tracer;
418+
static LockTracer lock_tracer;
419+
static WallClock wall_clock;
420+
static ITimer itimer;
419421

420422
if (strcmp(event_name, EVENT_CPU) == 0) {
421-
return PERF_EVENTS;
423+
return PerfEvents::supported() ? (Engine*)&perf_events : (Engine*)&itimer;
422424
} else if (strcmp(event_name, EVENT_ALLOC) == 0) {
423-
return ALLOC_TRACER;
425+
return &alloc_tracer;
424426
} else if (strcmp(event_name, EVENT_LOCK) == 0) {
425-
return LOCK_TRACER;
427+
return &lock_tracer;
426428
} else if (strcmp(event_name, EVENT_WALL) == 0) {
427-
return WALL_CLOCK;
429+
return &wall_clock;
430+
} else if (strcmp(event_name, EVENT_ITIMER) == 0) {
431+
return &itimer;
428432
} else {
429-
return PERF_EVENTS;
433+
return &perf_events;
430434
}
431435
}
432436

@@ -699,17 +703,21 @@ void Profiler::runInternal(Arguments& args, std::ostream& out) {
699703
break;
700704
}
701705
case ACTION_LIST: {
702-
out << "Perf events:" << std::endl;
703-
const char** perf_events = PerfEvents::getAvailableEvents();
704-
for (const char** event = perf_events; *event != NULL; event++) {
705-
out << " " << *event << std::endl;
706-
}
707-
delete[] perf_events;
708-
709-
out << "Java events:" << std::endl;
706+
out << "Basic events:" << std::endl;
707+
out << " " << EVENT_CPU << std::endl;
710708
out << " " << EVENT_ALLOC << std::endl;
711709
out << " " << EVENT_LOCK << std::endl;
712710
out << " " << EVENT_WALL << std::endl;
711+
out << " " << EVENT_ITIMER << std::endl;
712+
713+
if (PerfEvents::supported()) {
714+
out << "Perf events:" << std::endl;
715+
for (int event_id = 1; ; event_id++) {
716+
const char* event_name = PerfEvents::getEventName(event_id);
717+
if (event_name == NULL) break;
718+
out << " " << event_name << std::endl;
719+
}
720+
}
713721
break;
714722
}
715723
case ACTION_VERSION:

0 commit comments

Comments
0 (0)
You are visitor 01402792 — 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.