Skip to content
This repository has been archived by the owner on May 1, 2023. It is now read-only.

Commit

Permalink
Allowing fast CPU sampling rates and supplying on-device CPU clock re…
Browse files Browse the repository at this point in the history
…solution

Summary:
The clock tick from `sysconf(_SC_CLK_TCK)` is always 10ms which doesn't represent real jiffy interval configured for the kernel.  Cleaning up this limitation as redundant and getting the actual jiffy value from device logged as annotation `provider.stack_trace.cpu_timer_res_us`.
Turned out `CLOCK_REALTIME_COARSE` clock resolution matches kernel configuration frequency for jiffy (`CONFIG_HZ`).

Reviewed By: BurntBrunch

Differential Revision: D32772404

fbshipit-source-id: 1a11d995fcb081502c1af40f1c3385477e959fe5
  • Loading branch information
aandreyeu authored and facebook-github-bot committed Dec 8, 2021
1 parent bab82f6 commit 1f8ff3c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
7 changes: 3 additions & 4 deletions cpp/profiler/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ const char* StackTraceWhitelist =

namespace {

int32_t getSystemClockTickIntervalMs(facebook::jni::alias_ref<jobject>) {
return systemClockTickIntervalMs();
int32_t getCpuClockResolutionMicros(facebook::jni::alias_ref<jobject>) {
return cpuClockResolutionMicros();
}

std::unordered_map<int32_t, std::shared_ptr<BaseTracer>> makeAvailableTracers(
Expand Down Expand Up @@ -197,8 +197,7 @@ JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void*) {
StackFrameThreadType,
{
makeNativeMethod(
"nativeSystemClockTickIntervalMs",
getSystemClockTickIntervalMs),
"nativeCpuClockResolutionMicros", getCpuClockResolutionMicros),
});
fbjni::registerNatives(
StackTraceWhitelist,
Expand Down
12 changes: 12 additions & 0 deletions cpp/util/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <fb/log.h>
#include <pthread.h>
#include <sys/stat.h>
#include <time.h>
#include <algorithm>
#include <chrono>
#include <sstream>
Expand Down Expand Up @@ -110,6 +111,17 @@ int32_t systemClockTickIntervalMs() {
#error "Do not have systemClockTickIntervalMs implementation for this platform"
#endif

int32_t cpuClockResolutionMicros() {
timespec clock_res_timespec;
// It was empirically determined that this clock resolution is equal to actual
// size of kernel jiffy.
auto res = clock_getres(CLOCK_REALTIME_COARSE, &clock_res_timespec);
if (res != 0) {
return -1;
}
return clock_res_timespec.tv_nsec / 1000;
}

#if defined(ANDROID)
#include <sys/system_properties.h>

Expand Down
7 changes: 7 additions & 0 deletions cpp/util/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,16 @@ namespace facebook {
namespace profilo {

int64_t monotonicTime();

int32_t threadID();

// Returns 0 if value was not found, and 1 if value <= 1, actual value otherwise
int32_t systemClockTickIntervalMs();

// Determines the kernel jiffy value and returns it's value in microseconds.
// Returns -1 if unable to determine the actual value.
int32_t cpuClockResolutionMicros();

std::string get_system_property(const char* key);

// Given a path, create the directory specified by it, along with all
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,6 @@ private synchronized boolean enableInternal(
cpuClockModeEnabled = true;
break;
}
if (cpuClockModeEnabled) {
if (mSystemClockTimeIntervalMs == -1) {
mSystemClockTimeIntervalMs = nativeSystemClockTickIntervalMs();
}
sampleRateMs = Math.max(sampleRateMs, mSystemClockTimeIntervalMs);
}
}
// For now, we'll just keep an eye on the main thread. Eventually we
// might want to pass a list of all the interesting threads.
Expand Down Expand Up @@ -255,6 +249,11 @@ protected void onTraceEnded(TraceContext context, ExtraDataFileProvider dataFile
Integer.toBinaryString(
providersToTracers(context.enabledProviders) & CPUProfiler.getAvailableTracers()));
}
if ((context.enabledProviders & getSupportedProviders()) != 0) {
logAnnotation(
"provider.stack_trace.cpu_timer_res_us",
Integer.toString(nativeCpuClockResolutionMicros()));
}
}

private void logAnnotation(String key, String value) {
Expand Down Expand Up @@ -320,5 +319,5 @@ protected void onTraceStarted(TraceContext context, ExtraDataFileProvider dataFi
}

@DoNotStrip
private static native int nativeSystemClockTickIntervalMs();
private static native int nativeCpuClockResolutionMicros();
}

0 comments on commit 1f8ff3c

Please sign in to comment.