codecache_trace.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include "fixed_types.h"
  2. #include "codecache_trace.h"
  3. #include "timer.h"
  4. #include "hooks_manager.h"
  5. #include "subsecond_time.h"
  6. #include "simulator.h"
  7. #include "pin.H"
  8. #define __STDC_FORMAT_MACROS // PRIu64
  9. #include <inttypes.h>
  10. #include <stdio.h>
  11. #include <iostream>
  12. uint64_t cc_counters[8];
  13. static FILE* cctrace;
  14. static FILE* ccstats;
  15. SubsecondTime next_callback;
  16. #define atomic_inc_int64(a) __sync_fetch_and_add(&(a), 1)
  17. VOID cacheInit()
  18. {
  19. }
  20. VOID newCacheBlock(USIZE new_block_size)
  21. {
  22. atomic_inc_int64(cc_counters[0]);
  23. }
  24. VOID fullCache(USIZE trace_size, USIZE stub_size)
  25. {
  26. atomic_inc_int64(cc_counters[1]);
  27. }
  28. VOID cacheFlushed()
  29. {
  30. atomic_inc_int64(cc_counters[2]);
  31. }
  32. VOID codeCacheEntered(ADDRINT cache_pc)
  33. {
  34. atomic_inc_int64(cc_counters[3]);
  35. }
  36. VOID codeCacheExited(ADDRINT cache_pc)
  37. {
  38. }
  39. VOID traceLinked(ADDRINT branch_pc, ADDRINT target_pc)
  40. {
  41. atomic_inc_int64(cc_counters[4]);
  42. }
  43. VOID traceUnlinked(ADDRINT branch_pc, ADDRINT stub_pc)
  44. {
  45. atomic_inc_int64(cc_counters[5]);
  46. }
  47. VOID traceInvalidated(ADDRINT orig_pc, ADDRINT cache_pc, BOOL success)
  48. {
  49. atomic_inc_int64(cc_counters[6]);
  50. }
  51. VOID traceInserted(TRACE trace, VOID *v)
  52. {
  53. atomic_inc_int64(cc_counters[7]);
  54. }
  55. void printCodeCacheTrace(const SubsecondTime& time)
  56. {
  57. fprintf(cctrace, "%" PRIu64, time.getNS());
  58. for (uint32_t i = 0 ; i < 8 ; i++)
  59. {
  60. fprintf(cctrace, " %" PRIu64, cc_counters[i]);
  61. }
  62. fprintf(cctrace, "\n");
  63. }
  64. void printCodeCacheStats(const SubsecondTime& time)
  65. {
  66. fprintf(ccstats, "%" PRIu64, time.getNS());
  67. fprintf(ccstats, " %u", CODECACHE_CodeMemReserved());
  68. fprintf(ccstats, " %u", CODECACHE_DirectoryMemUsed());
  69. fprintf(ccstats, " %u", CODECACHE_CodeMemUsed());
  70. fprintf(ccstats, " %u", CODECACHE_ExitStubBytes());
  71. fprintf(ccstats, " %u", CODECACHE_LinkBytes());
  72. fprintf(ccstats, " %u", CODECACHE_CacheSizeLimit());
  73. fprintf(ccstats, " %u", CODECACHE_BlockSize());
  74. fprintf(ccstats, " %u", CODECACHE_NumTracesInCache());
  75. fprintf(ccstats, " %u", CODECACHE_NumExitStubsInCache());
  76. fprintf(ccstats, "\n");
  77. }
  78. void codeCachePeriodicCallback(void *self, subsecond_time_t _time)
  79. {
  80. SubsecondTime time(_time);
  81. if (time < next_callback)
  82. return;
  83. next_callback = time + SubsecondTime::NS(10000);
  84. printCodeCacheTrace(time);
  85. printCodeCacheStats(time);
  86. }
  87. void initCodeCacheTracing()
  88. {
  89. cctrace = fopen(Sim()->getConfig()->formatOutputFileName("cctrace.txt").c_str(), "w");
  90. LOG_ASSERT_ERROR(cctrace != NULL, "Expected to be able to create a file");
  91. ccstats = fopen(Sim()->getConfig()->formatOutputFileName("ccstats.txt").c_str(), "w");
  92. LOG_ASSERT_ERROR(cctrace != NULL, "Expected to be able to create a file");
  93. next_callback = SubsecondTime::Zero();
  94. Sim()->getHooksManager()->registerHook(HookType::HOOK_PERIODIC, (HooksManager::HookCallbackFunc)codeCachePeriodicCallback, (UInt64)NULL);
  95. //CODECACHE_AddCacheInitFunction(cacheInit, NULL);
  96. CODECACHE_AddCacheBlockFunction(newCacheBlock, NULL);
  97. CODECACHE_AddFullCacheFunction(fullCache, NULL);
  98. CODECACHE_AddCacheFlushedFunction(cacheFlushed, NULL);
  99. CODECACHE_AddCodeCacheEnteredFunction(codeCacheEntered, NULL);
  100. //CODECACHE_AddCodeCacheExitedFunction(codeCacheExited, NULL);
  101. CODECACHE_AddTraceLinkedFunction(traceLinked, NULL);
  102. CODECACHE_AddTraceUnlinkedFunction(traceUnlinked, NULL);
  103. CODECACHE_AddTraceInvalidatedFunction(traceInvalidated, NULL);
  104. CODECACHE_AddTraceInsertedFunction(traceInserted, NULL);
  105. }