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