diff --git a/apps/OboeTester/app/src/main/cpp/jni-bridge.cpp b/apps/OboeTester/app/src/main/cpp/jni-bridge.cpp index fad629efa..74c03eb42 100644 --- a/apps/OboeTester/app/src/main/cpp/jni-bridge.cpp +++ b/apps/OboeTester/app/src/main/cpp/jni-bridge.cpp @@ -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 diff --git a/src/common/AdpfWrapper.cpp b/src/common/AdpfWrapper.cpp index 4bb7c22c1..e03880a50 100644 --- a/src/common/AdpfWrapper.cpp +++ b/src/common/AdpfWrapper.cpp @@ -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*, @@ -64,6 +67,9 @@ static int loadAphFunctions() { } gAPerformanceHintBindingInitialized = true; + + Trace::initialize(); + return 0; } @@ -95,9 +101,12 @@ 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 lock(mLock); + Trace::beginSection("reportActualDuration"); + Trace::setCounter("actualDurationNanos", actualDurationNanos); if (mHintSession != nullptr) { gAPH_reportActualWorkDurationFn(mHintSession, actualDurationNanos); } + Trace::endSection(); } void AdpfWrapper::close() { diff --git a/src/common/AdpfWrapper.h b/src/common/AdpfWrapper.h index 3beb15706..ef5da705e 100644 --- a/src/common/AdpfWrapper.h +++ b/src/common/AdpfWrapper.h @@ -24,66 +24,69 @@ #include #include -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 diff --git a/src/common/Trace.cpp b/src/common/Trace.cpp index f08f36dc5..bf81f22d6 100644 --- a/src/common/Trace.cpp +++ b/src/common/Trace.cpp @@ -19,6 +19,8 @@ #include "Trace.h" #include "OboeDebug.h" +using namespace oboe; + static char buffer[256]; // Tracing functions @@ -26,35 +28,50 @@ 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); @@ -67,9 +84,21 @@ void Trace::initialize() { ATrace_endSection = reinterpret_cast( dlsym(lib, "ATrace_endSection")); + ATrace_setCounter = + reinterpret_cast( + dlsym(lib, "ATrace_setCounter")); + ATrace_isEnabled = + reinterpret_cast( + 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"); + } } } } diff --git a/src/common/Trace.h b/src/common/Trace.h index dad6c0071..d3c1dd77e 100644 --- a/src/common/Trace.h +++ b/src/common/Trace.h @@ -17,15 +17,29 @@ #ifndef OBOE_TRACE_H #define OBOE_TRACE_H +#include + +namespace oboe { + +/** + * Wrapper for tracing use with Perfetto + */ 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 mIsTracingEnabled; + static bool mIsSetCounterSupported; + static bool mHasErrorBeenShown; }; +} #endif //OBOE_TRACE_H