PerformanceCounterUtils.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #pragma once
  2. #include "il2cpp-class-internals.h"
  3. namespace il2cpp
  4. {
  5. namespace icalls
  6. {
  7. namespace System
  8. {
  9. namespace System
  10. {
  11. namespace Diagnostics
  12. {
  13. struct CategoryDesc
  14. {
  15. const char *name;
  16. const char *help;
  17. unsigned char id;
  18. signed int type : 2;
  19. unsigned int instance_type : 6;
  20. short first_counter;
  21. };
  22. #define PERFCTR_CAT(id, name, help, type, inst, first_counter) CATEGORY_ ## id,
  23. #define PERFCTR_COUNTER(id, name, help, type, field)
  24. enum
  25. {
  26. #include "perfcounters-def.h"
  27. NUM_CATEGORIES
  28. };
  29. #undef PERFCTR_CAT
  30. #undef PERFCTR_COUNTER
  31. #define PERFCTR_CAT(id, name, help, type, inst, first_counter) CATEGORY_START_ ## id = -1,
  32. #define PERFCTR_COUNTER(id, name, help, type, field) COUNTER_ ## id,
  33. /* each counter is assigned an id starting from 0 inside the category */
  34. enum
  35. {
  36. #include "perfcounters-def.h"
  37. END_COUNTERS
  38. };
  39. #undef PERFCTR_CAT
  40. #undef PERFCTR_COUNTER
  41. #define PERFCTR_CAT(id, name, help, type, inst, first_counter)
  42. #define PERFCTR_COUNTER(id, name, help, type, field) CCOUNTER_ ## id,
  43. /* this is used just to count the number of counters */
  44. enum
  45. {
  46. #include "perfcounters-def.h"
  47. NUM_COUNTERS
  48. };
  49. struct CounterDesc
  50. {
  51. const char *name;
  52. const char *help;
  53. short id;
  54. unsigned short offset; // offset inside Il2CppPerfCounters
  55. int type;
  56. };
  57. enum
  58. {
  59. FTYPE_CATEGORY = 'C',
  60. FTYPE_DELETED = 'D',
  61. FTYPE_PREDEF_INSTANCE = 'P', // an instance of a predef counter
  62. FTYPE_INSTANCE = 'I',
  63. FTYPE_DIRTY = 'd',
  64. FTYPE_END = 0
  65. };
  66. struct SharedHeader
  67. {
  68. unsigned char ftype;
  69. unsigned char extra;
  70. unsigned short size;
  71. };
  72. struct SharedCategory
  73. {
  74. SharedHeader header;
  75. unsigned short num_counters;
  76. unsigned short counters_data_size;
  77. int num_instances;
  78. /* variable length data follows */
  79. char name[1];
  80. // string name
  81. // string help
  82. // SharedCounter counters_info [num_counters]
  83. };
  84. struct SharedInstance
  85. {
  86. SharedHeader header;
  87. size_t category_offset;
  88. /* variable length data follows */
  89. char instance_name[1];
  90. // string name
  91. };
  92. struct SharedCounter
  93. {
  94. unsigned char type;
  95. uint8_t seq_num;
  96. /* variable length data follows */
  97. char name[1];
  98. // string name
  99. // string help
  100. };
  101. struct CatSearch
  102. {
  103. Il2CppString *name;
  104. SharedCategory *cat;
  105. };
  106. /* map of PerformanceCounterType.cs */
  107. enum
  108. {
  109. NumberOfItemsHEX32 = 0x00000000,
  110. NumberOfItemsHEX64 = 0x00000100,
  111. NumberOfItems32 = 0x00010000,
  112. NumberOfItems64 = 0x00010100,
  113. CounterDelta32 = 0x00400400,
  114. CounterDelta64 = 0x00400500,
  115. SampleCounter = 0x00410400,
  116. CountPerTimeInterval32 = 0x00450400,
  117. CountPerTimeInterval64 = 0x00450500,
  118. RateOfCountsPerSecond32 = 0x10410400,
  119. RateOfCountsPerSecond64 = 0x10410500,
  120. RawFraction = 0x20020400,
  121. CounterTimer = 0x20410500,
  122. Timer100Ns = 0x20510500,
  123. SampleFraction = 0x20C20400,
  124. CounterTimerInverse = 0x21410500,
  125. Timer100NsInverse = 0x21510500,
  126. CounterMultiTimer = 0x22410500,
  127. CounterMultiTimer100Ns = 0x22510500,
  128. CounterMultiTimerInverse = 0x23410500,
  129. CounterMultiTimer100NsInverse = 0x23510500,
  130. AverageTimer32 = 0x30020400,
  131. ElapsedTime = 0x30240500,
  132. AverageCount64 = 0x40020500,
  133. SampleBase = 0x40030401,
  134. AverageBase = 0x40030402,
  135. RawBase = 0x40030403,
  136. CounterMultiBase = 0x42030500
  137. };
  138. /* maps a small integer type to the counter types above */
  139. static const int simple_type_to_type[] =
  140. {
  141. NumberOfItemsHEX32, NumberOfItemsHEX64,
  142. NumberOfItems32, NumberOfItems64,
  143. CounterDelta32, CounterDelta64,
  144. SampleCounter, CountPerTimeInterval32,
  145. CountPerTimeInterval64, RateOfCountsPerSecond32,
  146. RateOfCountsPerSecond64, RawFraction,
  147. CounterTimer, Timer100Ns,
  148. SampleFraction, CounterTimerInverse,
  149. Timer100NsInverse, CounterMultiTimer,
  150. CounterMultiTimer100Ns, CounterMultiTimerInverse,
  151. CounterMultiTimer100NsInverse, AverageTimer32,
  152. ElapsedTime, AverageCount64,
  153. SampleBase, AverageBase,
  154. RawBase, CounterMultiBase
  155. };
  156. enum
  157. {
  158. SingleInstance,
  159. MultiInstance,
  160. CatTypeUnknown = -1
  161. };
  162. enum
  163. {
  164. ProcessInstance,
  165. ThreadInstance,
  166. CPUInstance,
  167. MonoInstance,
  168. NetworkInterfaceInstance,
  169. CustomInstance
  170. };
  171. /* map of CounterSample.cs */
  172. struct Il2CppCounterSample
  173. {
  174. int64_t rawValue;
  175. int64_t baseValue;
  176. int64_t counterFrequency;
  177. int64_t systemFrequency;
  178. int64_t timeStamp;
  179. int64_t timeStamp100nSec;
  180. int64_t counterTimeStamp;
  181. int counterType;
  182. };
  183. #undef PERFCTR_CAT
  184. #undef PERFCTR_COUNTER
  185. #define PERFCTR_CAT(id, name, help, type, inst, first_counter) {name, help, CATEGORY_ ## id, type, inst ## Instance, CCOUNTER_ ## first_counter},
  186. #define PERFCTR_COUNTER(id, name, help, type, field)
  187. static const CategoryDesc
  188. predef_categories[] =
  189. {
  190. /* sample runtime counter */
  191. #include "perfcounters-def.h"
  192. { NULL, NULL, NUM_CATEGORIES, -1, 0, NUM_COUNTERS }
  193. };
  194. #undef PERFCTR_CAT
  195. #undef PERFCTR_COUNTER
  196. #define PERFCTR_CAT(id, name, help, type, inst, first_counter)
  197. #define PERFCTR_COUNTER(id, name, help, type, field) {name, help, COUNTER_ ## id, offsetof (Il2CppPerfCounters, field), type},
  198. static const CounterDesc
  199. predef_counters[] =
  200. {
  201. #include "perfcounters-def.h"
  202. { NULL, NULL, -1, 0, 0 }
  203. };
  204. /*
  205. * We have several different classes of counters:
  206. * *) system counters
  207. * *) runtime counters
  208. * *) remote counters
  209. * *) user-defined counters
  210. * *) windows counters (the implementation on windows will use this)
  211. *
  212. * To easily handle the differences we create a vtable for each class that contains the
  213. * function pointers with the actual implementation to access the counters.
  214. */
  215. typedef struct _ImplVtable ImplVtable;
  216. typedef bool(*SampleFunc) (ImplVtable *vtable, bool only_value, Il2CppCounterSample* sample);
  217. typedef uint64_t(*UpdateFunc) (ImplVtable *vtable, bool do_incr, int64_t value);
  218. typedef void(*CleanupFunc) (ImplVtable *vtable);
  219. struct _ImplVtable
  220. {
  221. void *arg;
  222. SampleFunc sample;
  223. UpdateFunc update;
  224. CleanupFunc cleanup;
  225. };
  226. struct CustomVTable
  227. {
  228. ImplVtable vtable;
  229. SharedInstance *instance_desc;
  230. SharedCounter *counter_desc;
  231. };
  232. const CategoryDesc* find_category(Il2CppString *category);
  233. const CounterDesc* get_counter_in_category(const CategoryDesc *desc, Il2CppString *counter);
  234. ImplVtable* create_vtable(void *arg, SampleFunc sample, UpdateFunc update);
  235. } /* namespace Diagnostics */
  236. } /* namespace System */
  237. } /* namespace System */
  238. } /* namespace icalls */
  239. } /* namespace il2cpp */