xref: /aosp_15_r20/external/compiler-rt/lib/esan/esan_sideline.h (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot //===-- esan_sideline.h -----------------------------------------*- C++ -*-===//
2*7c3d14c8STreehugger Robot //
3*7c3d14c8STreehugger Robot //                     The LLVM Compiler Infrastructure
4*7c3d14c8STreehugger Robot //
5*7c3d14c8STreehugger Robot // This file is distributed under the University of Illinois Open Source
6*7c3d14c8STreehugger Robot // License. See LICENSE.TXT for details.
7*7c3d14c8STreehugger Robot //
8*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
9*7c3d14c8STreehugger Robot //
10*7c3d14c8STreehugger Robot // This file is a part of EfficiencySanitizer, a family of performance tuners.
11*7c3d14c8STreehugger Robot //
12*7c3d14c8STreehugger Robot // Esan sideline thread support.
13*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
14*7c3d14c8STreehugger Robot 
15*7c3d14c8STreehugger Robot #ifndef ESAN_SIDELINE_H
16*7c3d14c8STreehugger Robot #define ESAN_SIDELINE_H
17*7c3d14c8STreehugger Robot 
18*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_atomic.h"
19*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_internal_defs.h"
20*7c3d14c8STreehugger Robot 
21*7c3d14c8STreehugger Robot namespace __esan {
22*7c3d14c8STreehugger Robot 
23*7c3d14c8STreehugger Robot typedef void (*SidelineFunc)(void *Arg);
24*7c3d14c8STreehugger Robot 
25*7c3d14c8STreehugger Robot // Currently only one sideline thread is supported.
26*7c3d14c8STreehugger Robot // It calls the SidelineFunc passed to launchThread once on each sample at the
27*7c3d14c8STreehugger Robot // given frequency in real time (i.e., wall clock time).
28*7c3d14c8STreehugger Robot class SidelineThread {
29*7c3d14c8STreehugger Robot public:
30*7c3d14c8STreehugger Robot   // We cannot initialize any fields in the constructor as it will be called
31*7c3d14c8STreehugger Robot   // *after* launchThread for a static instance, as esan.module_ctor is called
32*7c3d14c8STreehugger Robot   // before static initializers.
SidelineThread()33*7c3d14c8STreehugger Robot   SidelineThread() {}
~SidelineThread()34*7c3d14c8STreehugger Robot   ~SidelineThread() {}
35*7c3d14c8STreehugger Robot 
36*7c3d14c8STreehugger Robot   // To simplify declaration in sanitizer code where we want to avoid
37*7c3d14c8STreehugger Robot   // heap allocations, the constructor and destructor do nothing and
38*7c3d14c8STreehugger Robot   // launchThread and joinThread do the real work.
39*7c3d14c8STreehugger Robot   // They should each be called just once.
40*7c3d14c8STreehugger Robot   bool launchThread(SidelineFunc takeSample, void *Arg, u32 FreqMilliSec);
41*7c3d14c8STreehugger Robot   bool joinThread();
42*7c3d14c8STreehugger Robot 
43*7c3d14c8STreehugger Robot   // Must be called from the sideline thread itself.
44*7c3d14c8STreehugger Robot   bool adjustTimer(u32 FreqMilliSec);
45*7c3d14c8STreehugger Robot 
46*7c3d14c8STreehugger Robot private:
47*7c3d14c8STreehugger Robot   static int runSideline(void *Arg);
48*7c3d14c8STreehugger Robot   static void registerSignal(int SigNum);
49*7c3d14c8STreehugger Robot   static void handleSidelineSignal(int SigNum, void *SigInfo, void *Ctx);
50*7c3d14c8STreehugger Robot 
51*7c3d14c8STreehugger Robot   char *Stack;
52*7c3d14c8STreehugger Robot   SidelineFunc sampleFunc;
53*7c3d14c8STreehugger Robot   void *FuncArg;
54*7c3d14c8STreehugger Robot   u32 Freq;
55*7c3d14c8STreehugger Robot   uptr SidelineId;
56*7c3d14c8STreehugger Robot   atomic_uintptr_t SidelineExit;
57*7c3d14c8STreehugger Robot };
58*7c3d14c8STreehugger Robot 
59*7c3d14c8STreehugger Robot } // namespace __esan
60*7c3d14c8STreehugger Robot 
61*7c3d14c8STreehugger Robot #endif  // ESAN_SIDELINE_H
62