stats.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #pragma once
  2. #include "simulator.h"
  3. #include "itostr.h"
  4. #include <strings.h>
  5. #include <sqlite3.h>
  6. class StatsMetricBase
  7. {
  8. public:
  9. String objectName;
  10. UInt32 index;
  11. String metricName;
  12. StatsMetricBase(String _objectName, UInt32 _index, String _metricName) :
  13. objectName(_objectName), index(_index), metricName(_metricName)
  14. {}
  15. virtual ~StatsMetricBase() {}
  16. virtual UInt64 recordMetric() = 0;
  17. virtual bool isDefault() { return false; } // Return true when value hasn't changed from its initialization value
  18. };
  19. template <class T> UInt64 makeStatsValue(T t);
  20. template <class T> class StatsMetric : public StatsMetricBase
  21. {
  22. public:
  23. T *metric;
  24. StatsMetric(String _objectName, UInt32 _index, String _metricName, T *_metric) :
  25. StatsMetricBase(_objectName, _index, _metricName), metric(_metric)
  26. {}
  27. virtual UInt64 recordMetric()
  28. {
  29. return makeStatsValue<T>(*metric);
  30. }
  31. virtual bool isDefault()
  32. {
  33. return recordMetric() == 0;
  34. }
  35. };
  36. typedef UInt64 (*StatsCallback)(String objectName, UInt32 index, String metricName, UInt64 arg);
  37. class StatsMetricCallback : public StatsMetricBase
  38. {
  39. public:
  40. StatsCallback func;
  41. UInt64 arg;
  42. StatsMetricCallback(String _objectName, UInt32 _index, String _metricName, StatsCallback _func, UInt64 _arg) :
  43. StatsMetricBase(_objectName, _index, _metricName), func(_func), arg(_arg)
  44. {}
  45. virtual UInt64 recordMetric()
  46. {
  47. return func(objectName, index, metricName, arg);
  48. }
  49. };
  50. class StatsManager
  51. {
  52. public:
  53. // Event type core thread arg0 arg1 description
  54. typedef enum {
  55. EVENT_MARKER = 1, // calling core calling thread magic arg0 magic arg1 str (SimMarker/SimNamedMarker)
  56. EVENT_THREAD_NAME, // calling core calling thread 0 0 thread name (SimSetThreadName)
  57. EVENT_APP_START, // -1 -1 app id 0 ""
  58. EVENT_APP_EXIT, // -1 -1 app id 0 ""
  59. EVENT_THREAD_CREATE, // initial core created thread app id creator thread ""
  60. EVENT_THREAD_EXIT, // current core exiting thread 0 0 ""
  61. } event_type_t;
  62. StatsManager();
  63. ~StatsManager();
  64. void init();
  65. void recordStats(String prefix);
  66. void registerMetric(StatsMetricBase *metric);
  67. StatsMetricBase *getMetricObject(String objectName, UInt32 index, String metricName);
  68. void logTopology(String component, core_id_t core_id, core_id_t master_id);
  69. void logMarker(SubsecondTime time, core_id_t core_id, thread_id_t thread_id, UInt64 value0, UInt64 value1, const char * description)
  70. { logEvent(EVENT_MARKER, time, core_id, thread_id, value0, value1, description); }
  71. void logEvent(event_type_t event, SubsecondTime time, core_id_t core_id, thread_id_t thread_id, UInt64 value0, UInt64 value1, const char * description);
  72. private:
  73. UInt64 m_keyid;
  74. UInt64 m_prefixnum;
  75. sqlite3 *m_db;
  76. sqlite3_stmt *m_stmt_insert_name;
  77. sqlite3_stmt *m_stmt_insert_prefix;
  78. sqlite3_stmt *m_stmt_insert_value;
  79. // Use std::string here because String (__versa_string) does not provide a hash function for STL containers with gcc < 4.6
  80. typedef std::unordered_map<UInt64, StatsMetricBase *> StatsIndexList;
  81. typedef std::pair<UInt64, StatsIndexList> StatsMetricWithKey;
  82. typedef std::unordered_map<std::string, StatsMetricWithKey> StatsMetricList;
  83. typedef std::unordered_map<std::string, StatsMetricList> StatsObjectList;
  84. StatsObjectList m_objects;
  85. static int __busy_handler(void* self, int count) { return ((StatsManager*)self)->busy_handler(count); }
  86. int busy_handler(int count);
  87. void recordMetricName(UInt64 keyId, std::string objectName, std::string metricName);
  88. };
  89. template <class T> void registerStatsMetric(String objectName, UInt32 index, String metricName, T *metric)
  90. {
  91. Sim()->getStatsManager()->registerMetric(new StatsMetric<T>(objectName, index, metricName, metric));
  92. }
  93. class StatHist {
  94. private:
  95. static const int HIST_MAX = 20;
  96. unsigned long n, s, s2, min, max;
  97. unsigned long hist[HIST_MAX];
  98. char dummy[64];
  99. public:
  100. StatHist() : n(0), s(0), s2(0), min(0), max(0) { bzero(hist, sizeof(hist)); }
  101. StatHist & operator += (StatHist & stat);
  102. void update(unsigned long v);
  103. void print();
  104. };