core.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #ifndef CORE_H
  2. #define CORE_H
  3. // some forward declarations for cross includes
  4. class Thread;
  5. class Network;
  6. class MemoryManagerBase;
  7. class MemoryManagerFast;
  8. class PerformanceModel;
  9. class ClockSkewMinimizationClient;
  10. class ShmemPerfModel;
  11. class TopologyInfo;
  12. class CheetahManager;
  13. #include "mem_component.h"
  14. #include "fixed_types.h"
  15. #include "lock.h"
  16. #include "packet_type.h"
  17. #include "subsecond_time.h"
  18. #include "bbv_count.h"
  19. #include "cpuid.h"
  20. #include "hit_where.h"
  21. struct MemoryResult {
  22. HitWhere::where_t hit_where;
  23. subsecond_time_t latency;
  24. };
  25. MemoryResult makeMemoryResult(HitWhere::where_t _hit_where, SubsecondTime _latency);
  26. void applicationMemCopy(void *dest, const void *src, size_t n);
  27. class Core
  28. {
  29. public:
  30. enum State
  31. {
  32. RUNNING = 0,
  33. INITIALIZING,
  34. STALLED,
  35. SLEEPING,
  36. WAKING_UP,
  37. IDLE,
  38. BROKEN,
  39. NUM_STATES
  40. };
  41. enum lock_signal_t
  42. {
  43. INVALID_LOCK_SIGNAL = 0,
  44. MIN_LOCK_SIGNAL,
  45. NONE = MIN_LOCK_SIGNAL,
  46. LOCK,
  47. UNLOCK,
  48. MAX_LOCK_SIGNAL = UNLOCK,
  49. NUM_LOCK_SIGNAL_TYPES = MAX_LOCK_SIGNAL - MIN_LOCK_SIGNAL + 1
  50. };
  51. enum mem_op_t
  52. {
  53. INVALID_MEM_OP = 0,
  54. MIN_MEM_OP,
  55. READ = MIN_MEM_OP,
  56. READ_EX,
  57. WRITE,
  58. MAX_MEM_OP = WRITE,
  59. NUM_MEM_OP_TYPES = MAX_MEM_OP - MIN_MEM_OP + 1
  60. };
  61. /* To what extend to make a memory access visible to the simulated instruction */
  62. enum MemModeled
  63. {
  64. MEM_MODELED_NONE, /* Not at all (pure backdoor access) */
  65. MEM_MODELED_COUNT, /* Count in #accesses/#misses */
  66. MEM_MODELED_COUNT_TLBTIME, /* Count in #accesses/#misses, queue TLBMissInstruction on TLB miss */
  67. MEM_MODELED_TIME, /* Count + account for access latency (using MemAccessInstruction) */
  68. MEM_MODELED_FENCED, /* Count + account for access latency as memory fence (using MemAccessInstruction) */
  69. MEM_MODELED_RETURN, /* Count + time + return data to construct DynamicInstruction */
  70. };
  71. static const char * CoreStateString(State state);
  72. Core(SInt32 id);
  73. ~Core();
  74. // Query and update branch predictor, return true on mispredict
  75. bool accessBranchPredictor(IntPtr eip, bool taken, IntPtr target);
  76. MemoryResult readInstructionMemory(IntPtr address,
  77. UInt32 instruction_size);
  78. MemoryResult accessMemory(lock_signal_t lock_signal, mem_op_t mem_op_type, IntPtr d_addr, char* data_buffer, UInt32 data_size, MemModeled modeled = MEM_MODELED_NONE, IntPtr eip = 0, SubsecondTime now = SubsecondTime::MaxTime(), bool is_fault_mask = false);
  79. MemoryResult nativeMemOp(lock_signal_t lock_signal, mem_op_t mem_op_type, IntPtr d_addr, char* data_buffer, UInt32 data_size);
  80. void accessMemoryFast(bool icache, mem_op_t mem_op_type, IntPtr address);
  81. void logMemoryHit(bool icache, mem_op_t mem_op_type, IntPtr address, MemModeled modeled = MEM_MODELED_NONE, IntPtr eip = 0);
  82. bool countInstructions(IntPtr address, UInt32 count);
  83. void emulateCpuid(UInt32 eax, UInt32 ecx, cpuid_result_t &res) const;
  84. // network accessor since network is private
  85. int getId() const { return m_core_id; }
  86. Thread *getThread() const { return m_thread; }
  87. void setThread(Thread *thread) { m_thread = thread; }
  88. Network *getNetwork() { return m_network; }
  89. PerformanceModel *getPerformanceModel() { return m_performance_model; }
  90. ClockSkewMinimizationClient* getClockSkewMinimizationClient() const { return m_clock_skew_minimization_client; }
  91. MemoryManagerBase *getMemoryManager() { return m_memory_manager; }
  92. const MemoryManagerBase *getMemoryManager() const { return m_memory_manager; }
  93. ShmemPerfModel* getShmemPerfModel() { return m_shmem_perf_model; }
  94. const ComponentPeriod* getDvfsDomain() const { return m_dvfs_domain; }
  95. TopologyInfo* getTopologyInfo() { return m_topology_info; }
  96. const TopologyInfo* getTopologyInfo() const { return m_topology_info; }
  97. const CheetahManager* getCheetahManager() const { return m_cheetah_manager; }
  98. State getState() const { return m_core_state; }
  99. void setState(State core_state) { m_core_state = core_state; }
  100. UInt64 getInstructionCount() { return m_instructions; }
  101. BbvCount *getBbvCount() { return &m_bbv; }
  102. UInt64 getInstructionsCallback() { return m_instructions_callback; }
  103. bool isEnabledInstructionsCallback() { return m_instructions_callback != UINT64_MAX; }
  104. void setInstructionsCallback(UInt64 instructions) { m_instructions_callback = m_instructions + instructions; }
  105. void disableInstructionsCallback() { m_instructions_callback = UINT64_MAX; }
  106. void enablePerformanceModels();
  107. void disablePerformanceModels();
  108. void updateSpinCount(UInt64 instructions, SubsecondTime elapsed_time)
  109. {
  110. m_spin_loops++;
  111. m_spin_instructions += instructions;
  112. m_spin_elapsed_time += elapsed_time;
  113. }
  114. private:
  115. core_id_t m_core_id;
  116. const ComponentPeriod* m_dvfs_domain;
  117. MemoryManagerBase *m_memory_manager;
  118. Thread *m_thread;
  119. Network *m_network;
  120. PerformanceModel *m_performance_model;
  121. ClockSkewMinimizationClient *m_clock_skew_minimization_client;
  122. Lock m_mem_lock;
  123. ShmemPerfModel* m_shmem_perf_model;
  124. BbvCount m_bbv;
  125. TopologyInfo *m_topology_info;
  126. CheetahManager *m_cheetah_manager;
  127. State m_core_state;
  128. static Lock m_global_core_lock;
  129. MemoryResult initiateMemoryAccess(
  130. MemComponent::component_t mem_component,
  131. lock_signal_t lock_signal,
  132. mem_op_t mem_op_type,
  133. IntPtr address,
  134. Byte* data_buf, UInt32 data_size,
  135. MemModeled modeled,
  136. IntPtr eip,
  137. SubsecondTime now);
  138. void hookPeriodicInsCheck();
  139. void hookPeriodicInsCall();
  140. IntPtr m_icache_last_block;
  141. UInt64 m_spin_loops;
  142. UInt64 m_spin_instructions;
  143. SubsecondTime m_spin_elapsed_time;
  144. protected:
  145. // Optimized version of countInstruction has direct access to m_instructions and m_instructions_callback
  146. friend class InstructionModeling;
  147. // In contrast to core->m_performance_model->m_instructions, this one always increments,
  148. // also when performance modeling is disabled or when instrumenation mode is CACHE_ONLY or FAST_FORWARD
  149. UInt64 m_instructions;
  150. UInt64 m_instructions_callback;
  151. // HOOK_PERIODIC_INS implementation
  152. UInt64 m_instructions_hpi_callback;
  153. UInt64 m_instructions_hpi_last;
  154. static UInt64 g_instructions_hpi_global;
  155. static UInt64 g_instructions_hpi_global_callback;
  156. };
  157. #endif