hooks_manager.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #ifndef __HOOKS_MANAGER_H
  2. #define __HOOKS_MANAGER_H
  3. #include "fixed_types.h"
  4. #include "subsecond_time.h"
  5. #include "thread_manager.h"
  6. #include <vector>
  7. #include <unordered_map>
  8. class HookType
  9. {
  10. public:
  11. enum hook_type_t {
  12. // Hook name Parameter (cast from UInt64) Description
  13. HOOK_PERIODIC, // SubsecondTime current_time Barrier was reached
  14. HOOK_PERIODIC_INS, // UInt64 icount Instruction-based periodic callback
  15. HOOK_SIM_START, // none Simulation start
  16. HOOK_SIM_END, // none Simulation end
  17. HOOK_ROI_BEGIN, // none ROI begin
  18. HOOK_ROI_END, // none ROI end
  19. HOOK_CPUFREQ_CHANGE, // UInt64 coreid CPU frequency was changed
  20. HOOK_MAGIC_MARKER, // MagicServer::MagicMarkerType * Magic marker (SimMarker) in application
  21. HOOK_MAGIC_USER, // MagicServer::MagicMarkerType * Magic user function (SimUser) in application
  22. HOOK_INSTR_COUNT, // UInt64 coreid Core has executed a preset number of instructions
  23. HOOK_THREAD_CREATE, // HooksManager::ThreadCreate Thread creation
  24. HOOK_THREAD_START, // HooksManager::ThreadTime Thread start
  25. HOOK_THREAD_EXIT, // HooksManager::ThreadTime Thread end
  26. HOOK_THREAD_STALL, // HooksManager::ThreadStall Thread has entered stalled state
  27. HOOK_THREAD_RESUME, // HooksManager::ThreadResume Thread has entered running state
  28. HOOK_THREAD_MIGRATE, // HooksManager::ThreadMigrate Thread was moved to a different core
  29. HOOK_INSTRUMENT_MODE, // UInt64 Instrument Mode Simulation mode change (ex. detailed, ffwd)
  30. HOOK_PRE_STAT_WRITE, // const char * prefix Before statistics are written (update generated stats now!)
  31. HOOK_SYSCALL_ENTER, // SyscallMdl::HookSyscallEnter Thread enters a system call
  32. HOOK_SYSCALL_EXIT, // SyscallMdl::HookSyscallExit Thread exist from system call
  33. HOOK_APPLICATION_START, // app_id_t Application (re)start
  34. HOOK_APPLICATION_EXIT, // app_id_t Application exit
  35. HOOK_APPLICATION_ROI_BEGIN, // none ROI begin, always triggers
  36. HOOK_APPLICATION_ROI_END, // none ROI end, always triggers
  37. HOOK_SIGUSR1, // none Sniper process received SIGUSR1
  38. HOOK_TYPES_MAX
  39. };
  40. static const char* hook_type_names[];
  41. };
  42. namespace std
  43. {
  44. template <> struct hash<HookType::hook_type_t> {
  45. size_t operator()(const HookType::hook_type_t & type) const {
  46. //return std::hash<int>(type);
  47. return (int)type;
  48. }
  49. };
  50. }
  51. class HooksManager
  52. {
  53. public:
  54. enum HookCallbackOrder {
  55. ORDER_NOTIFY_PRE, // For callbacks that want to inspect state before any actions
  56. ORDER_ACTION, // For callbacks that want to change simulator state based on the event
  57. ORDER_NOTIFY_POST, // For callbacks that want to inspect state after any actions
  58. NUM_HOOK_ORDER,
  59. };
  60. typedef SInt64 (*HookCallbackFunc)(UInt64, UInt64);
  61. struct HookCallback {
  62. HookCallbackFunc func;
  63. UInt64 arg;
  64. HookCallbackOrder order;
  65. HookCallback(HookCallbackFunc _func, UInt64 _arg, HookCallbackOrder _order) : func(_func), arg(_arg), order(_order) {}
  66. };
  67. typedef struct {
  68. thread_id_t thread_id;
  69. thread_id_t creator_thread_id;
  70. } ThreadCreate;
  71. typedef struct {
  72. thread_id_t thread_id;
  73. subsecond_time_t time;
  74. } ThreadTime;
  75. typedef struct {
  76. thread_id_t thread_id; // Thread stalling
  77. ThreadManager::stall_type_t reason; // Reason for thread stall
  78. subsecond_time_t time; // Time at which the stall occurs (if known, else SubsecondTime::MaxTime())
  79. } ThreadStall;
  80. typedef struct {
  81. thread_id_t thread_id; // Thread being woken up
  82. thread_id_t thread_by; // Thread triggering the wakeup
  83. subsecond_time_t time; // Time at which the wakeup occurs (if known, else SubsecondTime::MaxTime())
  84. } ThreadResume;
  85. typedef struct {
  86. thread_id_t thread_id; // Thread being migrated
  87. core_id_t core_id; // Core the thread is now running (or INVALID_CORE_ID == -1 for unscheduled)
  88. subsecond_time_t time; // Current time
  89. } ThreadMigrate;
  90. HooksManager();
  91. void init();
  92. void fini();
  93. void registerHook(HookType::hook_type_t type, HookCallbackFunc func, UInt64 argument, HookCallbackOrder order = ORDER_NOTIFY_PRE);
  94. SInt64 callHooks(HookType::hook_type_t type, UInt64 argument, bool expect_return = false);
  95. private:
  96. std::unordered_map<HookType::hook_type_t, std::vector<HookCallback> > m_registry;
  97. };
  98. #endif /* __HOOKS_MANAGER_H */