Guest User

Untitled

a guest
Jan 18th, 2020
1,232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.13 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <string>
  4. #include <chrono>
  5. #include <algorithm>
  6. #include <fstream>
  7. #include <thread>
  8. #include <mutex>
  9.  
  10. #define PROFILING 1
  11. #ifdef PROFILING
  12.     #define PROFILE_SCOPE(name) InstrumentationTimer timer##__LINE__(name)
  13.     #define PROFILE_FUNCTION()  PROFILE_SCOPE(__FUNCTION__)
  14. #else
  15.     #define PROFILE_SCOPE(name)
  16. #endif
  17.  
  18. struct ProfileResult
  19. {
  20.     const std::string name;
  21.     long long start, end;
  22.     uint32_t threadID;
  23. };
  24.  
  25. class Instrumentor
  26. {
  27.     std::string     m_sessionName   = "None";
  28.     std::ofstream   m_outputStream;
  29.     int             m_profileCount  = 0;
  30.     std::mutex      m_lock;
  31.     bool            m_activeSession = false;
  32.  
  33.     Instrumentor() { }
  34.  
  35. public:
  36.  
  37.     static Instrumentor& Instance()
  38.     {
  39.         static Instrumentor instance;
  40.         return instance;
  41.     }
  42.  
  43.     ~Instrumentor()
  44.     {
  45.         endSession();
  46.     }
  47.  
  48.     void beginSession(const std::string& name, const std::string& filepath = "results.json")
  49.     {
  50.         if (m_activeSession) { endSession(); }
  51.         m_activeSession = true;
  52.         m_outputStream.open(filepath);
  53.         writeHeader();
  54.         m_sessionName = name;
  55.     }
  56.  
  57.     void endSession()
  58.     {
  59.         if (!m_activeSession) { return; }
  60.         m_activeSession = false;
  61.         writeFooter();
  62.         m_outputStream.close();
  63.         m_profileCount = 0;
  64.     }
  65.  
  66.     void writeProfile(const ProfileResult& result)
  67.     {
  68.         std::lock_guard<std::mutex> lock(m_lock);
  69.  
  70.         if (m_profileCount++ > 0) { m_outputStream << ","; }
  71.  
  72.         std::string name = result.name;
  73.         std::replace(name.begin(), name.end(), '"', '\'');
  74.  
  75.         m_outputStream << "{";
  76.         m_outputStream << "\"cat\":\"function\",";
  77.         m_outputStream << "\"dur\":" << (result.end - result.start) << ',';
  78.         m_outputStream << "\"name\":\"" << name << "\",";
  79.         m_outputStream << "\"ph\":\"X\",";
  80.         m_outputStream << "\"pid\":0,";
  81.         m_outputStream << "\"tid\":" << result.threadID << ",";
  82.         m_outputStream << "\"ts\":" << result.start;
  83.         m_outputStream << "}";
  84.     }
  85.  
  86.     void writeHeader()
  87.     {
  88.         m_outputStream << "{\"otherData\": {},\"traceEvents\":[";
  89.     }
  90.  
  91.     void writeFooter()
  92.     {
  93.         m_outputStream << "]}";
  94.     }
  95. };
  96.  
  97. class InstrumentationTimer
  98. {
  99.     ProfileResult m_result;
  100.  
  101.     std::chrono::time_point<std::chrono::high_resolution_clock> m_startTimepoint;
  102.     bool m_stopped;
  103.  
  104. public:
  105.  
  106.     InstrumentationTimer(const std::string & name)
  107.         : m_result({ name, 0, 0, 0 })
  108.         , m_stopped(false)
  109.     {
  110.         m_startTimepoint = std::chrono::high_resolution_clock::now();
  111.     }
  112.  
  113.     ~InstrumentationTimer()
  114.     {
  115.         if (!m_stopped) { stop(); }
  116.     }
  117.  
  118.     void stop()
  119.     {
  120.         auto endTimepoint = std::chrono::high_resolution_clock::now();
  121.  
  122.         m_result.start = std::chrono::time_point_cast<std::chrono::microseconds>(m_startTimepoint).time_since_epoch().count();
  123.         m_result.end   = std::chrono::time_point_cast<std::chrono::microseconds>(endTimepoint).time_since_epoch().count();
  124.         m_result.threadID = std::hash<std::thread::id>{}(std::this_thread::get_id());
  125.         Instrumentor::Instance().writeProfile(m_result);
  126.  
  127.         m_stopped = true;
  128.     }
  129. };
Add Comment
Please, Sign In to add comment