Skip to content

Commit 19a5786

Browse files
authored
Allow sharing of the Trace API for Perfetto (#2210)
Use Singleton design pattern and early initialization in Trace API Add a "Workload" trace for OboeTester CPU LOAD test
1 parent 0e29ae2 commit 19a5786

5 files changed

Lines changed: 92 additions & 68 deletions

File tree

apps/OboeTester/app/src/main/cpp/NativeAudioContext.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <vector>
2525

2626
#include "common/OboeDebug.h"
27+
#include "common/Trace.h"
2728
#include "oboe/Oboe.h"
2829

2930
#include "aaudio/AAudioExtensions.h"
@@ -173,6 +174,10 @@ class ActivityContext {
173174

174175
void setWorkload(int32_t workload) {
175176
oboeCallbackProxy.setWorkload(workload);
177+
bool traceEnabled = oboe::Trace::getInstance().isEnabled();
178+
if (traceEnabled) {
179+
oboe::Trace::getInstance().setCounter("Workload", workload);
180+
}
176181
}
177182

178183
void setHearWorkload(bool enabled) {

src/common/AdpfWrapper.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ static int loadAphFunctions() {
6868

6969
gAPerformanceHintBindingInitialized = true;
7070

71-
Trace::initialize();
72-
7371
return 0;
7472
}
7573

@@ -101,12 +99,17 @@ int AdpfWrapper::open(pid_t threadId,
10199
void AdpfWrapper::reportActualDuration(int64_t actualDurationNanos) {
102100
//LOGD("ADPF Oboe %s(dur=%lld)", __func__, (long long)actualDurationNanos);
103101
std::lock_guard<std::mutex> lock(mLock);
104-
Trace::beginSection("reportActualDuration");
105-
Trace::setCounter("actualDurationNanos", actualDurationNanos);
106102
if (mHintSession != nullptr) {
103+
bool traceEnabled = Trace::getInstance().isEnabled();
104+
if (traceEnabled) {
105+
Trace::getInstance().beginSection("reportActualDuration");
106+
Trace::getInstance().setCounter("actualDurationNanos", actualDurationNanos);
107+
}
107108
gAPH_reportActualWorkDurationFn(mHintSession, actualDurationNanos);
109+
if (traceEnabled) {
110+
Trace::getInstance().endSection();
111+
}
108112
}
109-
Trace::endSection();
110113
}
111114

112115
void AdpfWrapper::close() {

src/common/StabilizedCallback.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ constexpr float kPercentageOfCallbackToUse = 0.8;
2323

2424
using namespace oboe;
2525

26-
StabilizedCallback::StabilizedCallback(AudioStreamCallback *callback) : mCallback(callback){
27-
Trace::initialize();
26+
StabilizedCallback::StabilizedCallback(AudioStreamCallback *callback) : mCallback(callback) {
2827
}
2928

3029
/**
@@ -64,16 +63,17 @@ StabilizedCallback::onAudioReady(AudioStream *oboeStream, void *audioData, int32
6463
int64_t targetDurationNanos = static_cast<int64_t>(
6564
(numFramesAsNanos * kPercentageOfCallbackToUse) - lateStartNanos);
6665

67-
Trace::beginSection("Actual load");
66+
bool traceEnabled = Trace::getInstance().isEnabled();
67+
if (traceEnabled) Trace::getInstance().beginSection("Actual load");
6868
DataCallbackResult result = mCallback->onAudioReady(oboeStream, audioData, numFrames);
69-
Trace::endSection();
69+
if (traceEnabled) Trace::getInstance().endSection();
7070

7171
int64_t executionDurationNanos = AudioClock::getNanoseconds() - startTimeNanos;
7272
int64_t stabilizingLoadDurationNanos = targetDurationNanos - executionDurationNanos;
7373

74-
Trace::beginSection("Stabilized load for %lldns", stabilizingLoadDurationNanos);
74+
if (traceEnabled) Trace::getInstance().beginSection("Stabilized load for %lldns", stabilizingLoadDurationNanos);
7575
generateLoad(stabilizingLoadDurationNanos);
76-
Trace::endSection();
76+
if (traceEnabled) Trace::getInstance().endSection();
7777

7878
// Wraparound: At 48000 frames per second mFrameCount wraparound will occur after 6m years,
7979
// significantly longer than the average lifetime of an Android phone.

src/common/Trace.cpp

Lines changed: 39 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,6 @@
2121

2222
using namespace oboe;
2323

24-
static char buffer[256];
25-
26-
// Tracing functions
27-
static void *(*ATrace_beginSection)(const char *sectionName);
28-
29-
static void *(*ATrace_endSection)();
30-
31-
static void *(*ATrace_setCounter)(const char *counterName, int64_t counterValue);
32-
33-
static bool *(*ATrace_isEnabled)(void);
34-
3524
typedef void *(*fp_ATrace_beginSection)(const char *sectionName);
3625

3726
typedef void *(*fp_ATrace_endSection)();
@@ -40,65 +29,65 @@ typedef void *(*fp_ATrace_setCounter)(const char *counterName, int64_t counterVa
4029

4130
typedef bool *(*fp_ATrace_isEnabled)(void);
4231

43-
bool Trace::mIsTracingEnabled = false;
44-
bool Trace::mIsSetCounterSupported = false;
45-
bool Trace::mHasErrorBeenShown = false;
32+
bool Trace::isEnabled() const {
33+
return ATrace_isEnabled != nullptr && ATrace_isEnabled();
34+
}
4635

47-
void Trace::beginSection(const char *format, ...){
48-
if (mIsTracingEnabled) {
49-
va_list va;
50-
va_start(va, format);
51-
vsprintf(buffer, format, va);
52-
ATrace_beginSection(buffer);
53-
va_end(va);
54-
} else if (!mHasErrorBeenShown) {
55-
LOGE("Tracing is either not initialized (call Trace::initialize()) "
56-
"or not supported on this device");
57-
mHasErrorBeenShown = true;
58-
}
36+
void Trace::beginSection(const char *format, ...) {
37+
char buffer[256];
38+
va_list va;
39+
va_start(va, format);
40+
vsprintf(buffer, format, va);
41+
ATrace_beginSection(buffer);
42+
va_end(va);
5943
}
6044

61-
void Trace::endSection() {
62-
if (mIsTracingEnabled) {
63-
ATrace_endSection();
64-
}
45+
void Trace::endSection() const {
46+
ATrace_endSection();
6547
}
6648

67-
void Trace::setCounter(const char *counterName, int64_t counterValue) {
68-
if (mIsSetCounterSupported) {
69-
ATrace_setCounter(counterName, counterValue);
70-
}
49+
void Trace::setCounter(const char *counterName, int64_t counterValue) const {
50+
ATrace_setCounter(counterName, counterValue);
7151
}
7252

73-
void Trace::initialize() {
74-
//LOGE("Trace::initialize");
53+
Trace::Trace() {
7554
// Using dlsym allows us to use tracing on API 21+ without needing android/trace.h which wasn't
7655
// published until API 23
7756
void *lib = dlopen("libandroid.so", RTLD_NOW | RTLD_LOCAL);
57+
LOGD("Trace(): dlopen(%s) returned %p", "libandroid.so", lib);
7858
if (lib == nullptr) {
79-
LOGE("Could not open libandroid.so to dynamically load tracing symbols");
59+
LOGE("Trace() could not open libandroid.so to dynamically load tracing symbols");
8060
} else {
8161
ATrace_beginSection =
82-
reinterpret_cast<fp_ATrace_beginSection >(
62+
reinterpret_cast<fp_ATrace_beginSection>(
8363
dlsym(lib, "ATrace_beginSection"));
64+
if (ATrace_beginSection == nullptr) {
65+
LOGE("Trace::beginSection() not supported");
66+
return;
67+
}
68+
8469
ATrace_endSection =
85-
reinterpret_cast<fp_ATrace_endSection >(
70+
reinterpret_cast<fp_ATrace_endSection>(
8671
dlsym(lib, "ATrace_endSection"));
72+
if (ATrace_endSection == nullptr) {
73+
LOGE("Trace::endSection() not supported");
74+
return;
75+
}
76+
8777
ATrace_setCounter =
88-
reinterpret_cast<fp_ATrace_setCounter >(
78+
reinterpret_cast<fp_ATrace_setCounter>(
8979
dlsym(lib, "ATrace_setCounter"));
80+
if (ATrace_setCounter == nullptr) {
81+
LOGE("Trace::setCounter() not supported");
82+
return;
83+
}
84+
85+
// If any of the previous functions are null then ATrace_isEnabled will be null.
9086
ATrace_isEnabled =
91-
reinterpret_cast<fp_ATrace_isEnabled >(
87+
reinterpret_cast<fp_ATrace_isEnabled>(
9288
dlsym(lib, "ATrace_isEnabled"));
93-
94-
if (ATrace_beginSection != nullptr && ATrace_endSection != nullptr
95-
&& ATrace_isEnabled != nullptr && ATrace_isEnabled()) {
96-
mIsTracingEnabled = true;
97-
if (ATrace_setCounter != nullptr) {
98-
mIsSetCounterSupported = true;
99-
} else {
100-
LOGE("setCounter not supported");
101-
}
89+
if (ATrace_isEnabled == nullptr) {
90+
LOGE("Trace::isEnabled() not supported");
10291
}
10392
}
10493
}

src/common/Trace.h

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,45 @@ namespace oboe {
2727
class Trace {
2828

2929
public:
30-
static void beginSection(const char *format, ...);
30+
static Trace &getInstance() {
31+
static Trace instance;
32+
return instance;
33+
}
3134

32-
static void endSection();
35+
/**
36+
* @return true if Perfetto tracing is enabled.
37+
*/
38+
bool isEnabled() const;
3339

34-
static void setCounter(const char *counterName, int64_t counterValue);
40+
/**
41+
* Only call this function if isEnabled() returns true.
42+
* @param format
43+
* @param ...
44+
*/
45+
void beginSection(const char *format, ...);
3546

36-
static void initialize();
47+
/**
48+
* Only call this function if isEnabled() returns true.
49+
*/
50+
void endSection() const;
51+
52+
/**
53+
* Only call this function if isEnabled() returns true.
54+
* @param counterName human readable name
55+
* @param counterValue value to log in trace
56+
*/
57+
void setCounter(const char *counterName, int64_t counterValue) const;
3758

3859
private:
39-
static bool mIsTracingEnabled;
40-
static bool mIsSetCounterSupported;
41-
static bool mHasErrorBeenShown;
60+
Trace();
61+
// Tracing functions
62+
void *(*ATrace_beginSection)(const char *sectionName) = nullptr;
63+
64+
void *(*ATrace_endSection)() = nullptr;
65+
66+
void *(*ATrace_setCounter)(const char *counterName, int64_t counterValue) = nullptr;
67+
68+
bool *(*ATrace_isEnabled)(void) = nullptr;
4269
};
4370

4471
}

0 commit comments

Comments
 (0)