Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AdpfWrapper: Add trace for actualDurationNanos #2143

Merged
merged 4 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/OboeTester/app/src/main/cpp/jni-bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ Java_com_mobileer_oboetester_OboeAudioStream_close(JNIEnv *env, jobject, jint st

JNIEXPORT void JNICALL
Java_com_mobileer_oboetester_TestAudioActivity_setUseAlternativeAdpf(JNIEnv *env, jobject, jboolean enabled) {
AdpfWrapper::setUseAlternative(enabled);
oboe::AdpfWrapper::setUseAlternative(enabled);
}

JNIEXPORT jint JNICALL
Expand Down
9 changes: 9 additions & 0 deletions src/common/AdpfWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
#include "oboe/AudioClock.h"
#include "AdpfWrapper.h"
#include "OboeDebug.h"
#include "Trace.h"

using namespace oboe;

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

gAPerformanceHintBindingInitialized = true;

Trace::initialize();

return 0;
}

Expand Down Expand Up @@ -95,6 +101,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
105 changes: 54 additions & 51 deletions src/common/AdpfWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,66 +24,69 @@
#include <unistd.h>
#include <mutex>

struct APerformanceHintManager;
struct APerformanceHintSession;
namespace oboe {

typedef struct APerformanceHintManager APerformanceHintManager;
typedef struct APerformanceHintSession APerformanceHintSession;
struct APerformanceHintManager;
struct APerformanceHintSession;

class AdpfWrapper {
public:
/**
* Create an ADPF session that can be used to boost performance.
* @param threadId
* @param targetDurationNanos - nominal period of isochronous task
* @return zero or negative error
*/
int open(pid_t threadId,
int64_t targetDurationNanos);
typedef struct APerformanceHintManager APerformanceHintManager;
typedef struct APerformanceHintSession APerformanceHintSession;

bool isOpen() const {
return (mHintSession != nullptr);
}
class AdpfWrapper {
public:
/**
* Create an ADPF session that can be used to boost performance.
* @param threadId
* @param targetDurationNanos - nominal period of isochronous task
* @return zero or negative error
*/
int open(pid_t threadId,
int64_t targetDurationNanos);

void close();
bool isOpen() const {
return (mHintSession != nullptr);
}

/**
* Call this at the beginning of the callback that you are measuring.
*/
void onBeginCallback();
void close();

/**
* Call this at the end of the callback that you are measuring.
* It is OK to skip this if you have a short callback.
*/
void onEndCallback(double durationScaler);
/**
* Call this at the beginning of the callback that you are measuring.
*/
void onBeginCallback();

/**
* For internal use only!
* This is a hack for communicating with experimental versions of ADPF.
* @param enabled
*/
static void setUseAlternative(bool enabled) {
sUseAlternativeHack = enabled;
}
/**
* Call this at the end of the callback that you are measuring.
* It is OK to skip this if you have a short callback.
*/
void onEndCallback(double durationScaler);

/**
* Report the measured duration of a callback.
* This is normally called by onEndCallback().
* You may want to call this directly in order to give an advance hint of a jump in workload.
* @param actualDurationNanos
*/
void reportActualDuration(int64_t actualDurationNanos);
/**
* For internal use only!
* This is a hack for communicating with experimental versions of ADPF.
* @param enabled
*/
static void setUseAlternative(bool enabled) {
sUseAlternativeHack = enabled;
}

void reportWorkload(int32_t appWorkload);
/**
* Report the measured duration of a callback.
* This is normally called by onEndCallback().
* You may want to call this directly in order to give an advance hint of a jump in workload.
* @param actualDurationNanos
*/
void reportActualDuration(int64_t actualDurationNanos);

private:
std::mutex mLock;
APerformanceHintSession* mHintSession = nullptr;
int64_t mBeginCallbackNanos = 0;
static bool sUseAlternativeHack;
int32_t mPreviousWorkload = 0;
double mNanosPerWorkloadUnit = 0.0;
};
void reportWorkload(int32_t appWorkload);

private:
std::mutex mLock;
APerformanceHintSession *mHintSession = nullptr;
int64_t mBeginCallbackNanos = 0;
static bool sUseAlternativeHack;
int32_t mPreviousWorkload = 0;
double mNanosPerWorkloadUnit = 0.0;
};

}
#endif //SYNTHMARK_ADPF_WRAPPER_H
46 changes: 37 additions & 9 deletions src/common/Trace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,59 @@
#include "Trace.h"
#include "OboeDebug.h"

using namespace oboe;

static char buffer[256];

// Tracing functions
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)();

bool Trace::mIsTracingSupported = false;
typedef void *(*fp_ATrace_setCounter)(const char *counterName, int64_t counterValue);

void Trace::beginSection(const char *format, ...){
typedef bool *(*fp_ATrace_isEnabled)(void);

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

if (mIsTracingSupported) {
void Trace::beginSection(const char *format, ...){
if (mIsTracingEnabled) {
va_list va;
va_start(va, format);
vsprintf(buffer, format, va);
ATrace_beginSection(buffer);
va_end(va);
} else {
} else if (!mHasErrorBeenShown) {
LOGE("Tracing is either not initialized (call Trace::initialize()) "
"or not supported on this device");
mHasErrorBeenShown = true;
}
}

void Trace::endSection() {

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

void Trace::initialize() {
void Trace::setCounter(const char *counterName, int64_t counterValue) {
if (mIsSetCounterSupported) {
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 +84,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){
mIsTracingSupported = true;
if (ATrace_beginSection != nullptr && ATrace_endSection != nullptr && ATrace_isEnabled != nullptr && ATrace_isEnabled()){
mIsTracingEnabled = true;
if (ATrace_setCounter != nullptr) {
mIsSetCounterSupported = true;
} else {
LOGE("setCounter not supported");
}
}
}
}
27 changes: 19 additions & 8 deletions src/common/Trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,26 @@
#ifndef OBOE_TRACE_H
#define OBOE_TRACE_H

class Trace {
#include <cstdint>

public:
static void beginSection(const char *format, ...);
static void endSection();
static void initialize();
namespace oboe {

private:
static bool mIsTracingSupported;
};
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 mIsTracingEnabled;
static bool mIsSetCounterSupported;
static bool mHasErrorBeenShown;
};

}
#endif //OBOE_TRACE_H
Loading