GeezerVison is showing github.com — 'itimer' profiling mode (like 'cpu', but does not require perf_events… · jonoffcpu/async-profiler@a2c70c9
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/arguments.h +5-4Lines changed: 5 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 23 | 23 | const long DEFAULT_INTERVAL = 10000000; // 10 ms | 24 | 24 | const int DEFAULT_FRAMEBUF = 1000000; | 25 | 25 |
| 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"; | 30 | 31 |
| 31 | 32 | enum Action { | 32 | 33 | ACTION_NONE, | |
Collapse file Copy file name to clipboard +49Lines changed: 49 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 Copy file name to clipboard +43Lines changed: 43 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 Copy file name to clipboardExpand all lines: src/perfEvents.h +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -54,7 +54,8 @@ class PerfEvents : public Engine { | 54 | 54 | int getNativeTrace(void* ucontext, int tid, const void** callchain, int max_depth, | 55 | 55 | const void* jit_min_address, const void* jit_max_address); | 56 | 56 |
| 57 | | - static const char** getAvailableEvents(); | | 57 | + static bool supported(); | | 58 | + static const char* getEventName(int event_id); | 58 | 59 |
| 59 | 60 | static void JNICALL ThreadStart(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) { | 60 | 61 | createForThread(OS::threadId()); | |
Collapse file Copy file name to clipboardExpand all lines: src/perfEvents_linux.cpp +21-14Lines changed: 21 additions & 14 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -110,7 +110,7 @@ struct PerfEventType { | 110 | 110 | static FunctionWithCounter KNOWN_FUNCTIONS[]; | 111 | 111 |
| 112 | 112 | // 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 | 114 | 114 | static int findCounterArg(const char* name) { | 115 | 115 | for (FunctionWithCounter* func = KNOWN_FUNCTIONS; func->name != NULL; func++) { | 116 | 116 | if (strcmp(name, func->name) == 0) { | @@ -308,19 +308,24 @@ bool PerfEvents::createForThread(int tid) { | 308 | 308 | return false; | 309 | 309 | } | 310 | 310 |
| | 311 | + PerfEventType* event_type = _event_type; | | 312 | + if (event_type == NULL) { | | 313 | + return false; | | 314 | + } | | 315 | + | 311 | 316 | struct perf_event_attr attr = {0}; | 312 | 317 | attr.size = sizeof(attr); | 313 | | - attr.type = _event_type->type; | | 318 | + attr.type = event_type->type; | 314 | 319 |
| 315 | 320 | 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; | 319 | 324 | } else { | 320 | | - attr.config = _event_type->config; | | 325 | + attr.config = event_type->config; | 321 | 326 | } | 322 | 327 |
| 323 | | - attr.precise_ip = _event_type->precise_ip; | | 328 | + attr.precise_ip = event_type->precise_ip; | 324 | 329 | attr.sample_period = _interval; | 325 | 330 | attr.sample_type = PERF_SAMPLE_CALLCHAIN; | 326 | 331 | attr.disabled = 1; | @@ -468,6 +473,8 @@ Error PerfEvents::start(Arguments& args) { | 468 | 473 | jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_THREAD_END, NULL); | 469 | 474 |
| 470 | 475 | if (!createForAllThreads()) { | | 476 | + jvmti->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_THREAD_START, NULL); | | 477 | + jvmti->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_THREAD_END, NULL); | 471 | 478 | return Error("Perf events unavailable. See stderr of the target process."); | 472 | 479 | } | 473 | 480 | return Error::OK; | @@ -525,15 +532,15 @@ int PerfEvents::getNativeTrace(void* ucontext, int tid, const void** callchain, | 525 | 532 | return depth; | 526 | 533 | } | 527 | 534 |
| 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 | +} | 531 | 538 |
| 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; | 534 | 542 | } | 535 | | - | 536 | | - return available_events; | | 543 | + return NULL; | 537 | 544 | } | 538 | 545 |
| 539 | 546 | #endif // __linux__ |
Collapse file Copy file name to clipboardExpand all lines: src/perfEvents_macos.cpp +8-29Lines changed: 8 additions & 29 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 16 | 16 |
| 17 | 17 | #ifdef __APPLE__ | 18 | 18 |
| 19 | | -#include <string.h> | 20 | | -#include <sys/time.h> | 21 | 19 | #include "perfEvents.h" | 22 | | -#include "profiler.h" | 23 | 20 |
| 24 | 21 |
| 25 | 22 | int PerfEvents::_max_events; | @@ -37,48 +34,30 @@ void PerfEvents::destroyForAllThreads() {} | 37 | 34 |
| 38 | 35 |
| 39 | 36 | void PerfEvents::signalHandler(int signo, siginfo_t* siginfo, void* ucontext) { | 40 | | - Profiler::_instance.recordSample(ucontext, _interval, 0, NULL); | 41 | 37 | } | 42 | 38 |
| 43 | 39 | const char* PerfEvents::units() { | 44 | 40 | return "ns"; | 45 | 41 | } | 46 | 42 |
| 47 | 43 | 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"); | 65 | 45 | } | 66 | 46 |
| 67 | 47 | void PerfEvents::stop() { | 68 | | - struct itimerval tv = {{0, 0}, {0, 0}}; | 69 | | - setitimer(ITIMER_PROF, &tv, NULL); | 70 | 48 | } | 71 | 49 |
| 72 | 50 | int PerfEvents::getNativeTrace(void* ucontext, int tid, const void** callchain, int max_depth, | 73 | 51 | 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; | 75 | 57 | } | 76 | 58 |
| 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; | 82 | 61 | } | 83 | 62 |
| 84 | 63 | #endif // __APPLE__ |
Collapse file Copy file name to clipboardExpand all lines: src/profiler.cpp +25-17Lines changed: 25 additions & 17 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 26 | 26 | #include "allocTracer.h" | 27 | 27 | #include "lockTracer.h" | 28 | 28 | #include "wallClock.h" | | 29 | +#include "itimer.h" | 29 | 30 | #include "flameGraph.h" | 30 | 31 | #include "flightRecorder.h" | 31 | 32 | #include "frameName.h" | @@ -412,21 +413,24 @@ void Profiler::initJvmtiFunctions(NativeCodeCache* libjvm) { | 412 | 413 | } | 413 | 414 |
| 414 | 415 | 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; | 419 | 421 |
| 420 | 422 | if (strcmp(event_name, EVENT_CPU) == 0) { | 421 | | - return PERF_EVENTS; | | 423 | + return PerfEvents::supported() ? (Engine*)&perf_events : (Engine*)&itimer; | 422 | 424 | } else if (strcmp(event_name, EVENT_ALLOC) == 0) { | 423 | | - return ALLOC_TRACER; | | 425 | + return &alloc_tracer; | 424 | 426 | } else if (strcmp(event_name, EVENT_LOCK) == 0) { | 425 | | - return LOCK_TRACER; | | 427 | + return &lock_tracer; | 426 | 428 | } 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; | 428 | 432 | } else { | 429 | | - return PERF_EVENTS; | | 433 | + return &perf_events; | 430 | 434 | } | 431 | 435 | } | 432 | 436 |
| @@ -699,17 +703,21 @@ void Profiler::runInternal(Arguments& args, std::ostream& out) { | 699 | 703 | break; | 700 | 704 | } | 701 | 705 | 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; | 710 | 708 | out << " " << EVENT_ALLOC << std::endl; | 711 | 709 | out << " " << EVENT_LOCK << std::endl; | 712 | 710 | 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 | + } | 713 | 721 | break; | 714 | 722 | } | 715 | 723 | case ACTION_VERSION: | |
|