Skip to content

Commit bebeee3

Browse files
committed
Tracer improvements
1 parent 8e4b39f commit bebeee3

10 files changed

Lines changed: 228 additions & 64 deletions

File tree

include/aspl/Tracer.hpp

Lines changed: 77 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,26 @@
1111
#include <pthread.h>
1212
#include <unistd.h>
1313

14+
#include <string>
15+
1416
namespace aspl {
1517

1618
//! Operation tracer.
1719
//!
18-
//! All objects use tracer to report operations issues by HAL. By default,
19-
//! tracers sends messages to syslog with "[aspl]" prefix.
20+
//! All objects use tracer to report operations issued by HAL. By default,
21+
//! tracer sends messages to syslog with "[aspl]" prefix.
2022
//!
2123
//! Tracer maintains per-thread operations depth counter. Nested operations
22-
//! are indented in output messages.
24+
//! are automatically indented.
25+
//!
26+
//! If you want to change formatting, you can use one of the predefined
27+
//! styles or override FormatXXX() methods.
2328
//!
2429
//! If you want to change tracer output, you can use one of the predefined
25-
//! modes or override Print() method. All other methods use it.
30+
//! modes or override Print() method.
2631
//!
27-
//! If you want to change formatting of the operations, you can override
28-
//! other public methods.
32+
//! If you want to exclude some operations from trace, you can override
33+
//! ShouldIgnore() method.
2934
class Tracer
3035
{
3136
public:
@@ -43,6 +48,32 @@ class Tracer
4348
//! System log.
4449
//! Send all messages to syslog().
4550
Syslog,
51+
52+
//! Custom mode.
53+
//! Use if derived class does something different.
54+
Custom,
55+
};
56+
57+
//! Tracing style.
58+
enum class Style
59+
{
60+
//! Indent nested operations.
61+
Hierarchical,
62+
63+
//! No indentation.
64+
Flat,
65+
};
66+
67+
//! Operation flags.
68+
struct Flags
69+
{
70+
//! This operation is read-only, i.e. doesn't change object state.
71+
static constexpr UInt32 Readonly = (1 << 0);
72+
73+
//! This operation is intended to be called on real-time thread on hot path.
74+
//! Such operations are traced only if the user enabled the option
75+
//! DeviceParameters::EnableRealtimeTracing
76+
static constexpr UInt32 Realtime = (1 << 1);
4677
};
4778

4879
//! Operation info.
@@ -51,6 +82,9 @@ class Tracer
5182
//! Operation name.
5283
const char* Name = nullptr;
5384

85+
//! Operation flags.
86+
UInt32 Flags = 0;
87+
5488
//! ID of object for which operation was initiated.
5589
AudioObjectID ObjectID = 0;
5690

@@ -88,7 +122,8 @@ class Tracer
88122

89123
//! Initialize tracer.
90124
//! Mode defines where to send messages.
91-
explicit Tracer(Mode mode = Mode::Syslog);
125+
//! Style defines how to format messages.
126+
explicit Tracer(Mode mode = Mode::Syslog, Style style = Style::Hierarchical);
92127

93128
Tracer(const Tracer&) = delete;
94129
Tracer& operator=(const Tracer&) = delete;
@@ -109,17 +144,49 @@ class Tracer
109144
virtual void OperationEnd(const Operation& operation, OSStatus status);
110145

111146
protected:
147+
//! Format operation begin message into string.
148+
//! Called by default implementation of OperationBegin().
149+
virtual std::string FormatOperationBegin(const Operation& operation, UInt32 depth);
150+
151+
//! Format message into string.
152+
//! Called by default implementation of Message().
153+
virtual std::string FormatMessage(const char* message, UInt32 depth);
154+
155+
//! Format operation end message into string.
156+
//! Called by default implementation of OperationEnd().
157+
virtual std::string FormatOperationEnd(const Operation& operation,
158+
OSStatus status,
159+
UInt32 depth);
160+
112161
//! Print message somewhere.
113162
//! Default implementation sends message to syslog if mode is Mode::Syslog,
114163
//! or does nothing if mode is Mode::Noop.
115164
virtual void Print(const char* message);
116165

166+
//! Check whethe the operation should be excluded from tracing.
167+
//! If this method returns true, the operation itself, as well as
168+
//! all nested operations, are not printed.
169+
//! Default implementation always returns false.
170+
virtual bool ShouldIgnore(const Operation& operation);
171+
117172
private:
118-
// Returns thread-local operation depth counter
119-
int& GetDepthCounter();
173+
struct ThreadLocalState
174+
{
175+
UInt32 DepthCounter = 0;
176+
UInt32 IgnoreCounter = 0;
177+
};
178+
179+
static void* CreateThreadLocalState();
180+
static void DestroyThreadLocalState(void*);
181+
182+
ThreadLocalState& GetThreadLocalState();
183+
184+
static constexpr size_t MaxMessageLen = 1024;
120185

121186
const Mode mode_;
122-
pthread_key_t depthCounter_;
187+
const Style style_;
188+
189+
pthread_key_t threadKey_;
123190
};
124191

125192
} // namespace aspl

script/generate-accessors.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@
223223
224224
Tracer::Operation op;
225225
op.Name = "{{ class }}::HasProperty()";
226+
op.Flags = Tracer::Flags::Readonly;
226227
op.ObjectID = objectID;
227228
op.ClientPID = clientPID;
228229
op.PropertyAddress = address;
@@ -290,6 +291,7 @@
290291
291292
Tracer::Operation op;
292293
op.Name = "{{ class }}::IsPropertySettable()";
294+
op.Flags = Tracer::Flags::Readonly;
293295
op.ObjectID = objectID;
294296
op.ClientPID = clientPID;
295297
op.PropertyAddress = address;
@@ -344,6 +346,7 @@
344346
345347
Tracer::Operation op;
346348
op.Name = "{{ class }}::GetPropertyDataSize()";
349+
op.Flags = Tracer::Flags::Readonly;
347350
op.ObjectID = objectID;
348351
op.ClientPID = clientPID;
349352
op.PropertyAddress = address;
@@ -418,6 +421,7 @@
418421
419422
Tracer::Operation op;
420423
op.Name = "{{ class }}::GetPropertyData()";
424+
op.Flags = Tracer::Flags::Readonly;
421425
op.ObjectID = objectID;
422426
op.ClientPID = clientPID;
423427
op.PropertyAddress = address;

src/Device.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,7 @@ OSStatus Device::GetZeroTimeStamp(AudioObjectID objectID,
11721172

11731173
Tracer::Operation op;
11741174
op.Name = "Device::GetZeroTimeStamp()";
1175+
op.Flags = (Tracer::Flags::Realtime | Tracer::Flags::Readonly);
11751176
op.ObjectID = GetID();
11761177

11771178
if (params_.EnableRealtimeTracing) {
@@ -1252,6 +1253,7 @@ OSStatus Device::WillDoIOOperation(AudioObjectID objectID,
12521253

12531254
Tracer::Operation op;
12541255
op.Name = "Device::WillDoIOOperation()";
1256+
op.Flags = (Tracer::Flags::Realtime | Tracer::Flags::Readonly);
12551257
op.ObjectID = GetID();
12561258

12571259
if (params_.EnableRealtimeTracing) {
@@ -1346,6 +1348,7 @@ OSStatus Device::DoIOOperation(AudioObjectID objectID,
13461348

13471349
Tracer::Operation op;
13481350
op.Name = "Device::DoIOOperation()";
1351+
op.Flags = Tracer::Flags::Realtime;
13491352
op.ObjectID = GetID();
13501353

13511354
if (params_.EnableRealtimeTracing) {

src/Device.g.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,7 @@ Boolean Device::HasProperty(AudioObjectID objectID,
635635

636636
Tracer::Operation op;
637637
op.Name = "Device::HasProperty()";
638+
op.Flags = Tracer::Flags::Readonly;
638639
op.ObjectID = objectID;
639640
op.ClientPID = clientPID;
640641
op.PropertyAddress = address;
@@ -920,6 +921,7 @@ OSStatus Device::IsPropertySettable(AudioObjectID objectID,
920921

921922
Tracer::Operation op;
922923
op.Name = "Device::IsPropertySettable()";
924+
op.Flags = Tracer::Flags::Readonly;
923925
op.ObjectID = objectID;
924926
op.ClientPID = clientPID;
925927
op.PropertyAddress = address;
@@ -1235,6 +1237,7 @@ OSStatus Device::GetPropertyDataSize(AudioObjectID objectID,
12351237

12361238
Tracer::Operation op;
12371239
op.Name = "Device::GetPropertyDataSize()";
1240+
op.Flags = Tracer::Flags::Readonly;
12381241
op.ObjectID = objectID;
12391242
op.ClientPID = clientPID;
12401243
op.PropertyAddress = address;
@@ -1596,6 +1599,7 @@ OSStatus Device::GetPropertyData(AudioObjectID objectID,
15961599

15971600
Tracer::Operation op;
15981601
op.Name = "Device::GetPropertyData()";
1602+
op.Flags = Tracer::Flags::Readonly;
15991603
op.ObjectID = objectID;
16001604
op.ClientPID = clientPID;
16011605
op.PropertyAddress = address;

src/MuteControl.g.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ Boolean MuteControl::HasProperty(AudioObjectID objectID,
8181

8282
Tracer::Operation op;
8383
op.Name = "MuteControl::HasProperty()";
84+
op.Flags = Tracer::Flags::Readonly;
8485
op.ObjectID = objectID;
8586
op.ClientPID = clientPID;
8687
op.PropertyAddress = address;
@@ -132,6 +133,7 @@ OSStatus MuteControl::IsPropertySettable(AudioObjectID objectID,
132133

133134
Tracer::Operation op;
134135
op.Name = "MuteControl::IsPropertySettable()";
136+
op.Flags = Tracer::Flags::Readonly;
135137
op.ObjectID = objectID;
136138
op.ClientPID = clientPID;
137139
op.PropertyAddress = address;
@@ -197,6 +199,7 @@ OSStatus MuteControl::GetPropertyDataSize(AudioObjectID objectID,
197199

198200
Tracer::Operation op;
199201
op.Name = "MuteControl::GetPropertyDataSize()";
202+
op.Flags = Tracer::Flags::Readonly;
200203
op.ObjectID = objectID;
201204
op.ClientPID = clientPID;
202205
op.PropertyAddress = address;
@@ -271,6 +274,7 @@ OSStatus MuteControl::GetPropertyData(AudioObjectID objectID,
271274

272275
Tracer::Operation op;
273276
op.Name = "MuteControl::GetPropertyData()";
277+
op.Flags = Tracer::Flags::Readonly;
274278
op.ObjectID = objectID;
275279
op.ClientPID = clientPID;
276280
op.PropertyAddress = address;

src/Object.g.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Boolean Object::HasProperty(AudioObjectID objectID,
4444

4545
Tracer::Operation op;
4646
op.Name = "Object::HasProperty()";
47+
op.Flags = Tracer::Flags::Readonly;
4748
op.ObjectID = objectID;
4849
op.ClientPID = clientPID;
4950
op.PropertyAddress = address;
@@ -107,6 +108,7 @@ OSStatus Object::IsPropertySettable(AudioObjectID objectID,
107108

108109
Tracer::Operation op;
109110
op.Name = "Object::IsPropertySettable()";
111+
op.Flags = Tracer::Flags::Readonly;
110112
op.ObjectID = objectID;
111113
op.ClientPID = clientPID;
112114
op.PropertyAddress = address;
@@ -192,6 +194,7 @@ OSStatus Object::GetPropertyDataSize(AudioObjectID objectID,
192194

193195
Tracer::Operation op;
194196
op.Name = "Object::GetPropertyDataSize()";
197+
op.Flags = Tracer::Flags::Readonly;
195198
op.ObjectID = objectID;
196199
op.ClientPID = clientPID;
197200
op.PropertyAddress = address;
@@ -292,6 +295,7 @@ OSStatus Object::GetPropertyData(AudioObjectID objectID,
292295

293296
Tracer::Operation op;
294297
op.Name = "Object::GetPropertyData()";
298+
op.Flags = Tracer::Flags::Readonly;
295299
op.ObjectID = objectID;
296300
op.ClientPID = clientPID;
297301
op.PropertyAddress = address;

src/Plugin.g.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Boolean Plugin::HasProperty(AudioObjectID objectID,
4545

4646
Tracer::Operation op;
4747
op.Name = "Plugin::HasProperty()";
48+
op.Flags = Tracer::Flags::Readonly;
4849
op.ObjectID = objectID;
4950
op.ClientPID = clientPID;
5051
op.PropertyAddress = address;
@@ -102,6 +103,7 @@ OSStatus Plugin::IsPropertySettable(AudioObjectID objectID,
102103

103104
Tracer::Operation op;
104105
op.Name = "Plugin::IsPropertySettable()";
106+
op.Flags = Tracer::Flags::Readonly;
105107
op.ObjectID = objectID;
106108
op.ClientPID = clientPID;
107109
op.PropertyAddress = address;
@@ -177,6 +179,7 @@ OSStatus Plugin::GetPropertyDataSize(AudioObjectID objectID,
177179

178180
Tracer::Operation op;
179181
op.Name = "Plugin::GetPropertyDataSize()";
182+
op.Flags = Tracer::Flags::Readonly;
180183
op.ObjectID = objectID;
181184
op.ClientPID = clientPID;
182185
op.PropertyAddress = address;
@@ -264,6 +267,7 @@ OSStatus Plugin::GetPropertyData(AudioObjectID objectID,
264267

265268
Tracer::Operation op;
266269
op.Name = "Plugin::GetPropertyData()";
270+
op.Flags = Tracer::Flags::Readonly;
267271
op.ObjectID = objectID;
268272
op.ClientPID = clientPID;
269273
op.PropertyAddress = address;

src/Stream.g.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ Boolean Stream::HasProperty(AudioObjectID objectID,
321321

322322
Tracer::Operation op;
323323
op.Name = "Stream::HasProperty()";
324+
op.Flags = Tracer::Flags::Readonly;
324325
op.ObjectID = objectID;
325326
op.ClientPID = clientPID;
326327
op.PropertyAddress = address;
@@ -408,6 +409,7 @@ OSStatus Stream::IsPropertySettable(AudioObjectID objectID,
408409

409410
Tracer::Operation op;
410411
op.Name = "Stream::IsPropertySettable()";
412+
op.Flags = Tracer::Flags::Readonly;
411413
op.ObjectID = objectID;
412414
op.ClientPID = clientPID;
413415
op.PropertyAddress = address;
@@ -533,6 +535,7 @@ OSStatus Stream::GetPropertyDataSize(AudioObjectID objectID,
533535

534536
Tracer::Operation op;
535537
op.Name = "Stream::GetPropertyDataSize()";
538+
op.Flags = Tracer::Flags::Readonly;
536539
op.ObjectID = objectID;
537540
op.ClientPID = clientPID;
538541
op.PropertyAddress = address;
@@ -677,6 +680,7 @@ OSStatus Stream::GetPropertyData(AudioObjectID objectID,
677680

678681
Tracer::Operation op;
679682
op.Name = "Stream::GetPropertyData()";
683+
op.Flags = Tracer::Flags::Readonly;
680684
op.ObjectID = objectID;
681685
op.ClientPID = clientPID;
682686
op.PropertyAddress = address;

0 commit comments

Comments
 (0)