Skip to content

Commit

Permalink
AdpfWrapper: Add trace for actualDurationNanos
Browse files Browse the repository at this point in the history
  • Loading branch information
robertwu1 committed Jan 22, 2025
1 parent 71d6185 commit 6639e15
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
7 changes: 7 additions & 0 deletions src/common/AdpfWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "oboe/AudioClock.h"
#include "AdpfWrapper.h"
#include "OboeDebug.h"
#include "Trace.h"

typedef APerformanceHintManager* (*APH_getManager)();
typedef APerformanceHintSession* (*APH_createSession)(APerformanceHintManager*, const int32_t*,
Expand Down Expand Up @@ -64,6 +65,9 @@ static int loadAphFunctions() {
}

gAPerformanceHintBindingInitialized = true;

Trace::initialize();

return 0;
}

Expand Down Expand Up @@ -95,6 +99,9 @@ int AdpfWrapper::open(pid_t threadId,
void AdpfWrapper::reportActualDuration(int64_t actualDurationNanos) {
//LOGD("ADPF Oboe %s(dur=%lld)", __func__, (long long)actualDurationNanos);
std::lock_guard<std::mutex> lock(mLock);
Trace::beginSection("reportActualDuration");
Trace::setCounter("actualDurationNanos", actualDurationNanos);
Trace::endSection();
if (mHintSession != nullptr) {
gAPH_reportActualWorkDurationFn(mHintSession, actualDurationNanos);
}
Expand Down
47 changes: 37 additions & 10 deletions src/common/Trace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,51 @@ static void *(*ATrace_beginSection)(const char *sectionName);

static void *(*ATrace_endSection)();

static void *(*ATrace_setCounter)(const char *counterName, int64_t counterValue);

static bool *(*ATrace_isEnabled)(void);

typedef void *(*fp_ATrace_beginSection)(const char *sectionName);

typedef void *(*fp_ATrace_endSection)();

typedef void *(*fp_ATrace_setCounter)(const char *counterName, int64_t counterValue);

typedef bool *(*fp_ATrace_isEnabled)(void);

bool Trace::mIsTracingSupported = false;
bool Trace::mIsSetCounterSupported = false;
bool Trace::mIsTracingEnabled = false;

void Trace::beginSection(const char *format, ...){

if (mIsTracingSupported) {
va_list va;
va_start(va, format);
vsprintf(buffer, format, va);
ATrace_beginSection(buffer);
va_end(va);
if (mIsTracingEnabled) {
va_list va;
va_start(va, format);
vsprintf(buffer, format, va);
ATrace_beginSection(buffer);
va_end(va);
}
} else {
LOGE("Tracing is either not initialized (call Trace::initialize()) "
"or not supported on this device");
}
}

void Trace::endSection() {

if (mIsTracingSupported) {
if (mIsTracingSupported && mIsTracingEnabled) {
ATrace_endSection();
}
}

void Trace::initialize() {
void Trace::setCounter(const char *counterName, int64_t counterValue) {
if (mIsSetCounterSupported && mIsTracingEnabled) {
ATrace_setCounter(counterName, counterValue);
}
}

void Trace::initialize() {
//LOGE("Trace::initialize");
// Using dlsym allows us to use tracing on API 21+ without needing android/trace.h which wasn't
// published until API 23
void *lib = dlopen("libandroid.so", RTLD_NOW | RTLD_LOCAL);
Expand All @@ -67,9 +83,20 @@ void Trace::initialize() {
ATrace_endSection =
reinterpret_cast<fp_ATrace_endSection >(
dlsym(lib, "ATrace_endSection"));
ATrace_setCounter =
reinterpret_cast<fp_ATrace_setCounter >(
dlsym(lib, "ATrace_setCounter"));
ATrace_isEnabled =
reinterpret_cast<fp_ATrace_isEnabled >(
dlsym(lib, "ATrace_isEnabled"));

if (ATrace_beginSection != nullptr && ATrace_endSection != nullptr){
if (ATrace_beginSection != nullptr && ATrace_endSection != nullptr && ATrace_isEnabled != nullptr){
mIsTracingEnabled = ATrace_isEnabled();
LOGD("Trace::initialize isEnabled: %d", mIsTracingEnabled);
mIsTracingSupported = true;
if (ATrace_setCounter != nullptr) {
mIsSetCounterSupported = true;
}
}
}
}
5 changes: 5 additions & 0 deletions src/common/Trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,20 @@
#ifndef OBOE_TRACE_H
#define OBOE_TRACE_H

#include <cstdint>

class Trace {

public:
static void beginSection(const char *format, ...);
static void endSection();
static void setCounter(const char *counterName, int64_t counterValue);
static void initialize();

private:
static bool mIsTracingSupported;
static bool mIsSetCounterSupported;
static bool mIsTracingEnabled;
};

#endif //OBOE_TRACE_H

0 comments on commit 6639e15

Please sign in to comment.