7
7
// ===----------------------------------------------------------------------===//
8
8
9
9
#include " CtxInstrProfiling.h"
10
+ #include " RootAutoDetector.h"
10
11
#include " sanitizer_common/sanitizer_allocator_internal.h"
11
12
#include " sanitizer_common/sanitizer_atomic.h"
12
13
#include " sanitizer_common/sanitizer_atomic_clang.h"
@@ -43,6 +44,12 @@ Arena *FlatCtxArena = nullptr;
43
44
__thread bool IsUnderContext = false ;
44
45
__sanitizer::atomic_uint8_t ProfilingStarted = {};
45
46
47
+ __sanitizer::atomic_uintptr_t RootDetector = {};
48
+ RootAutoDetector *getRootDetector () {
49
+ return reinterpret_cast <RootAutoDetector *>(
50
+ __sanitizer::atomic_load_relaxed (&RootDetector));
51
+ }
52
+
46
53
// utility to taint a pointer by setting the LSB. There is an assumption
47
54
// throughout that the addresses of contexts are even (really, they should be
48
55
// align(8), but "even"-ness is the minimum assumption)
@@ -201,7 +208,7 @@ ContextNode *getCallsiteSlow(GUID Guid, ContextNode **InsertionPoint,
201
208
return Ret;
202
209
}
203
210
204
- ContextNode *getFlatProfile (FunctionData &Data, GUID Guid,
211
+ ContextNode *getFlatProfile (FunctionData &Data, void *Callee, GUID Guid,
205
212
uint32_t NumCounters) {
206
213
if (ContextNode *Existing = Data.FlatCtx )
207
214
return Existing;
@@ -232,6 +239,7 @@ ContextNode *getFlatProfile(FunctionData &Data, GUID Guid,
232
239
auto *Ret = allocContextNode (AllocBuff, Guid, NumCounters, 0 );
233
240
Data.FlatCtx = Ret;
234
241
242
+ Data.EntryAddress = Callee;
235
243
Data.Next = reinterpret_cast <FunctionData *>(
236
244
__sanitizer::atomic_load_relaxed (&AllFunctionsData));
237
245
while (!__sanitizer::atomic_compare_exchange_strong (
@@ -316,27 +324,32 @@ ContextNode *getUnhandledContext(FunctionData &Data, GUID Guid,
316
324
// entered once and never exit. They should be assumed to be entered before
317
325
// profiling starts - because profiling should start after the server is up
318
326
// and running (which is equivalent to "message pumps are set up").
319
- ContextRoot *R = __llvm_ctx_profile_current_context_root;
320
- if (!R) {
327
+ if (!CtxRoot) {
328
+ if (auto *RAD = getRootDetector ())
329
+ RAD->sample ();
330
+ else if (auto *CR = Data.CtxRoot )
331
+ return tryStartContextGivenRoot (CR, Guid, NumCounters, NumCallsites);
321
332
if (IsUnderContext || !__sanitizer::atomic_load_relaxed (&ProfilingStarted))
322
333
return TheScratchContext;
323
334
else
324
335
return markAsScratch (
325
- onContextEnter (*getFlatProfile (Data, Guid, NumCounters)));
336
+ onContextEnter (*getFlatProfile (Data, Callee, Guid, NumCounters)));
326
337
}
327
- auto [Iter, Ins] = R ->Unhandled .insert ({Guid, nullptr });
338
+ auto [Iter, Ins] = CtxRoot ->Unhandled .insert ({Guid, nullptr });
328
339
if (Ins)
329
- Iter->second =
330
- getCallsiteSlow (Guid, &R-> FirstUnhandledCalleeNode , NumCounters, 0 );
340
+ Iter->second = getCallsiteSlow (Guid, &CtxRoot-> FirstUnhandledCalleeNode ,
341
+ NumCounters, 0 );
331
342
return markAsScratch (onContextEnter (*Iter->second ));
332
343
}
333
344
334
345
ContextNode *__llvm_ctx_profile_get_context (FunctionData *Data, void *Callee,
335
346
GUID Guid, uint32_t NumCounters,
336
347
uint32_t NumCallsites) {
348
+ auto *CtxRoot = __llvm_ctx_profile_current_context_root;
337
349
// fast "out" if we're not even doing contextual collection.
338
- if (!__llvm_ctx_profile_current_context_root)
339
- return getUnhandledContext (*Data, Guid, NumCounters);
350
+ if (!CtxRoot)
351
+ return getUnhandledContext (*Data, Callee, Guid, NumCounters, NumCallsites,
352
+ nullptr );
340
353
341
354
// also fast "out" if the caller is scratch. We can see if it's scratch by
342
355
// looking at the interior pointer into the subcontexts vector that the caller
@@ -345,7 +358,8 @@ ContextNode *__llvm_ctx_profile_get_context(FunctionData *Data, void *Callee,
345
358
// precisely, aligned - 8 values)
346
359
auto **CallsiteContext = consume (__llvm_ctx_profile_callsite[0 ]);
347
360
if (!CallsiteContext || isScratch (CallsiteContext))
348
- return getUnhandledContext (*Data, Guid, NumCounters);
361
+ return getUnhandledContext (*Data, Callee, Guid, NumCounters, NumCallsites,
362
+ CtxRoot);
349
363
350
364
// if the callee isn't the expected one, return scratch.
351
365
// Signal handler(s) could have been invoked at any point in the execution.
@@ -363,7 +377,8 @@ ContextNode *__llvm_ctx_profile_get_context(FunctionData *Data, void *Callee,
363
377
// for that case.
364
378
auto *ExpectedCallee = consume (__llvm_ctx_profile_expected_callee[0 ]);
365
379
if (ExpectedCallee != Callee)
366
- return getUnhandledContext (*Data, Guid, NumCounters);
380
+ return getUnhandledContext (*Data, Callee, Guid, NumCounters, NumCallsites,
381
+ CtxRoot);
367
382
368
383
auto *Callsite = *CallsiteContext;
369
384
// in the case of indirect calls, we will have all seen targets forming a
@@ -385,24 +400,26 @@ ContextNode *__llvm_ctx_profile_get_context(FunctionData *Data, void *Callee,
385
400
return Ret;
386
401
}
387
402
388
- ContextNode *__llvm_ctx_profile_start_context (
389
- FunctionData *FData, GUID Guid, uint32_t Counters,
390
- uint32_t Callsites) SANITIZER_NO_THREAD_SAFETY_ANALYSIS {
403
+ ContextNode *__llvm_ctx_profile_start_context (FunctionData *FData, GUID Guid,
404
+ uint32_t Counters,
405
+ uint32_t Callsites) {
406
+
391
407
return tryStartContextGivenRoot (FData->getOrAllocateContextRoot (), Guid,
392
408
Counters, Callsites);
393
409
}
394
410
395
411
void __llvm_ctx_profile_release_context (FunctionData *FData)
396
412
SANITIZER_NO_THREAD_SAFETY_ANALYSIS {
413
+ const auto *CurrentRoot = __llvm_ctx_profile_current_context_root;
414
+ if (!CurrentRoot || FData->CtxRoot != CurrentRoot)
415
+ return ;
397
416
IsUnderContext = false ;
398
- if (__llvm_ctx_profile_current_context_root) {
399
- __llvm_ctx_profile_current_context_root = nullptr ;
400
- assert (FData->CtxRoot );
401
- FData->CtxRoot ->Taken .Unlock ();
402
- }
417
+ assert (FData->CtxRoot );
418
+ __llvm_ctx_profile_current_context_root = nullptr ;
419
+ FData->CtxRoot ->Taken .Unlock ();
403
420
}
404
421
405
- void __llvm_ctx_profile_start_collection () {
422
+ void __llvm_ctx_profile_start_collection (unsigned AutodetectDuration ) {
406
423
size_t NumMemUnits = 0 ;
407
424
__sanitizer::GenericScopedLock<__sanitizer::SpinMutex> Lock (
408
425
&AllContextsMutex);
@@ -418,12 +435,24 @@ void __llvm_ctx_profile_start_collection() {
418
435
resetContextNode (*Root->FirstUnhandledCalleeNode );
419
436
__sanitizer::atomic_store_relaxed (&Root->TotalEntries , 0 );
420
437
}
438
+ if (AutodetectDuration) {
439
+ auto *RD = new (__sanitizer::InternalAlloc (sizeof (RootAutoDetector)))
440
+ RootAutoDetector (AllFunctionsData, RootDetector, AutodetectDuration);
441
+ RD->start ();
442
+ } else {
443
+ __sanitizer::Printf (" [ctxprof] Initial NumMemUnits: %zu \n " , NumMemUnits);
444
+ }
421
445
__sanitizer::atomic_store_relaxed (&ProfilingStarted, true );
422
- __sanitizer::Printf (" [ctxprof] Initial NumMemUnits: %zu \n " , NumMemUnits);
423
446
}
424
447
425
448
bool __llvm_ctx_profile_fetch (ProfileWriter &Writer) {
426
449
__sanitizer::atomic_store_relaxed (&ProfilingStarted, false );
450
+ if (auto *RD = getRootDetector ()) {
451
+ __sanitizer::Printf (" [ctxprof] Expected the root autodetector to have "
452
+ " finished well before attempting to fetch a context" );
453
+ RD->join ();
454
+ }
455
+
427
456
__sanitizer::GenericScopedLock<__sanitizer::SpinMutex> Lock (
428
457
&AllContextsMutex);
429
458
@@ -448,8 +477,9 @@ bool __llvm_ctx_profile_fetch(ProfileWriter &Writer) {
448
477
const auto *Pos = reinterpret_cast <const FunctionData *>(
449
478
__sanitizer::atomic_load_relaxed (&AllFunctionsData));
450
479
for (; Pos; Pos = Pos->Next )
451
- Writer.writeFlat (Pos->FlatCtx ->guid (), Pos->FlatCtx ->counters (),
452
- Pos->FlatCtx ->counters_size ());
480
+ if (!Pos->CtxRoot )
481
+ Writer.writeFlat (Pos->FlatCtx ->guid (), Pos->FlatCtx ->counters (),
482
+ Pos->FlatCtx ->counters_size ());
453
483
Writer.endFlatSection ();
454
484
return true ;
455
485
}
0 commit comments