profiler.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  1. #include "profiler.h"
  2. #include "darray.h"
  3. #include "dstr.h"
  4. #include "platform.h"
  5. #include "threading.h"
  6. #include <math.h>
  7. #include <zlib.h>
  8. //#define TRACK_OVERHEAD
  9. struct profiler_snapshot {
  10. DARRAY(profiler_snapshot_entry_t) roots;
  11. };
  12. struct profiler_snapshot_entry {
  13. const char *name;
  14. profiler_time_entries_t times;
  15. uint64_t min_time;
  16. uint64_t max_time;
  17. uint64_t overall_count;
  18. profiler_time_entries_t times_between_calls;
  19. uint64_t expected_time_between_calls;
  20. uint64_t min_time_between_calls;
  21. uint64_t max_time_between_calls;
  22. uint64_t overall_between_calls_count;
  23. DARRAY(profiler_snapshot_entry_t) children;
  24. };
  25. typedef struct profiler_time_entry profiler_time_entry;
  26. typedef struct profile_call profile_call;
  27. struct profile_call {
  28. const char *name;
  29. #ifdef TRACK_OVERHEAD
  30. uint64_t overhead_start;
  31. #endif
  32. uint64_t start_time;
  33. uint64_t end_time;
  34. #ifdef TRACK_OVERHEAD
  35. uint64_t overhead_end;
  36. #endif
  37. uint64_t expected_time_between_calls;
  38. DARRAY(profile_call) children;
  39. profile_call *parent;
  40. };
  41. typedef struct profile_times_table_entry profile_times_table_entry;
  42. struct profile_times_table_entry {
  43. size_t probes;
  44. profiler_time_entry entry;
  45. };
  46. typedef struct profile_times_table profile_times_table;
  47. struct profile_times_table {
  48. size_t size;
  49. size_t occupied;
  50. size_t max_probe_count;
  51. profile_times_table_entry *entries;
  52. size_t old_start_index;
  53. size_t old_occupied;
  54. profile_times_table_entry *old_entries;
  55. };
  56. typedef struct profile_entry profile_entry;
  57. struct profile_entry {
  58. const char *name;
  59. profile_times_table times;
  60. #ifdef TRACK_OVERHEAD
  61. profile_times_table overhead;
  62. #endif
  63. uint64_t expected_time_between_calls;
  64. profile_times_table times_between_calls;
  65. DARRAY(profile_entry) children;
  66. };
  67. typedef struct profile_root_entry profile_root_entry;
  68. struct profile_root_entry {
  69. pthread_mutex_t *mutex;
  70. const char *name;
  71. profile_entry *entry;
  72. profile_call *prev_call;
  73. };
  74. static inline uint64_t diff_ns_to_usec(uint64_t prev, uint64_t next)
  75. {
  76. return (next - prev + 500) / 1000;
  77. }
  78. static inline void update_max_probes(profile_times_table *map, size_t val)
  79. {
  80. map->max_probe_count = map->max_probe_count < val ?
  81. val : map->max_probe_count;
  82. }
  83. static void migrate_old_entries(profile_times_table *map, bool limit_items);
  84. static void grow_hashmap(profile_times_table *map,
  85. uint64_t usec, uint64_t count);
  86. static void add_hashmap_entry(profile_times_table *map, uint64_t usec,
  87. uint64_t count)
  88. {
  89. size_t probes = 1;
  90. size_t start = usec % map->size;
  91. for (;; probes += 1) {
  92. size_t idx = (start + probes) % map->size;
  93. profile_times_table_entry *entry = &map->entries[idx];
  94. if (!entry->probes) {
  95. entry->probes = probes;
  96. entry->entry.time_delta = usec;
  97. entry->entry.count = count;
  98. map->occupied += 1;
  99. update_max_probes(map, probes);
  100. return;
  101. }
  102. if (entry->entry.time_delta == usec) {
  103. entry->entry.count += count;
  104. return;
  105. }
  106. if (entry->probes >= probes)
  107. continue;
  108. if (map->occupied/(double)map->size > 0.7) {
  109. grow_hashmap(map, usec, count);
  110. return;
  111. }
  112. size_t old_probes = entry->probes;
  113. uint64_t old_count = entry->entry.count;
  114. uint64_t old_usec = entry->entry.time_delta;
  115. entry->probes = probes;
  116. entry->entry.count = count;
  117. entry->entry.time_delta = usec;
  118. update_max_probes(map, probes);
  119. probes = old_probes;
  120. count = old_count;
  121. usec = old_usec;
  122. start = usec % map->size;
  123. }
  124. }
  125. static void init_hashmap(profile_times_table *map, size_t size)
  126. {
  127. map->size = size;
  128. map->occupied = 0;
  129. map->max_probe_count = 0;
  130. map->entries = bzalloc(sizeof(profile_times_table_entry) * size);
  131. map->old_start_index = 0;
  132. map->old_occupied = 0;
  133. map->old_entries = NULL;
  134. }
  135. static void migrate_old_entries(profile_times_table *map, bool limit_items)
  136. {
  137. if (!map->old_entries)
  138. return;
  139. if (!map->old_occupied) {
  140. bfree(map->old_entries);
  141. map->old_entries = NULL;
  142. return;
  143. }
  144. for (size_t i = 0; !limit_items || i < 8; i++, map->old_start_index++) {
  145. if (!map->old_occupied)
  146. return;
  147. profile_times_table_entry *entry =
  148. &map->old_entries[map->old_start_index];
  149. if (!entry->probes)
  150. continue;
  151. add_hashmap_entry(map, entry->entry.time_delta,
  152. entry->entry.count);
  153. map->old_occupied -= 1;
  154. }
  155. }
  156. static void grow_hashmap(profile_times_table *map,
  157. uint64_t usec, uint64_t count)
  158. {
  159. migrate_old_entries(map, false);
  160. size_t old_size = map->size;
  161. size_t old_occupied = map->occupied;
  162. profile_times_table_entry *entries = map->entries;
  163. init_hashmap(map, (old_size * 2 < 16) ? 16 : (old_size * 2));
  164. map->old_occupied = old_occupied;
  165. map->old_entries = entries;
  166. add_hashmap_entry(map, usec, count);
  167. }
  168. static profile_entry *init_entry(profile_entry *entry, const char *name)
  169. {
  170. entry->name = name;
  171. init_hashmap(&entry->times, 1);
  172. #ifdef TRACK_OVERHEAD
  173. init_hashmap(&entry->overhead, 1);
  174. #endif
  175. entry->expected_time_between_calls = 0;
  176. init_hashmap(&entry->times_between_calls, 1);
  177. return entry;
  178. }
  179. static profile_entry *get_child(profile_entry *parent, const char *name)
  180. {
  181. const size_t num = parent->children.num;
  182. for (size_t i = 0; i < num; i++) {
  183. profile_entry *child = &parent->children.array[i];
  184. if (child->name == name)
  185. return child;
  186. }
  187. return init_entry(da_push_back_new(parent->children), name);
  188. }
  189. static void merge_call(profile_entry *entry, profile_call *call,
  190. profile_call *prev_call)
  191. {
  192. const size_t num = call->children.num;
  193. for (size_t i = 0; i < num; i++) {
  194. profile_call *child = &call->children.array[i];
  195. merge_call(get_child(entry, child->name), child, NULL);
  196. }
  197. if (entry->expected_time_between_calls != 0 && prev_call) {
  198. migrate_old_entries(&entry->times_between_calls, true);
  199. uint64_t usec = diff_ns_to_usec(prev_call->start_time,
  200. call->start_time);
  201. add_hashmap_entry(&entry->times_between_calls, usec, 1);
  202. }
  203. migrate_old_entries(&entry->times, true);
  204. uint64_t usec = diff_ns_to_usec(call->start_time, call->end_time);
  205. add_hashmap_entry(&entry->times, usec, 1);
  206. #ifdef TRACK_OVERHEAD
  207. migrate_old_entries(&entry->overhead, true);
  208. usec = diff_ns_to_usec(call->overhead_start, call->start_time);
  209. usec += diff_ns_to_usec(call->end_time, call->overhead_end);
  210. add_hashmap_entry(&entry->overhead, usec, 1);
  211. #endif
  212. }
  213. static bool enabled = false;
  214. static pthread_mutex_t root_mutex = PTHREAD_MUTEX_INITIALIZER;
  215. static DARRAY(profile_root_entry) root_entries;
  216. #ifdef _MSC_VER
  217. static __declspec(thread) profile_call *thread_context = NULL;
  218. static __declspec(thread) bool thread_enabled = true;
  219. #else
  220. static __thread profile_call *thread_context = NULL;
  221. static __thread bool thread_enabled = true;
  222. #endif
  223. void profiler_start(void)
  224. {
  225. pthread_mutex_lock(&root_mutex);
  226. enabled = true;
  227. pthread_mutex_unlock(&root_mutex);
  228. }
  229. void profiler_stop(void)
  230. {
  231. pthread_mutex_lock(&root_mutex);
  232. enabled = false;
  233. pthread_mutex_unlock(&root_mutex);
  234. }
  235. void profile_reenable_thread(void)
  236. {
  237. if (thread_enabled)
  238. return;
  239. pthread_mutex_lock(&root_mutex);
  240. thread_enabled = enabled;
  241. pthread_mutex_unlock(&root_mutex);
  242. }
  243. static bool lock_root(void)
  244. {
  245. pthread_mutex_lock(&root_mutex);
  246. if (!enabled) {
  247. pthread_mutex_unlock(&root_mutex);
  248. thread_enabled = false;
  249. return false;
  250. }
  251. return true;
  252. }
  253. static profile_root_entry *get_root_entry(const char *name)
  254. {
  255. profile_root_entry *r_entry = NULL;
  256. for (size_t i = 0; i < root_entries.num; i++) {
  257. if (root_entries.array[i].name == name) {
  258. r_entry = &root_entries.array[i];
  259. break;
  260. }
  261. }
  262. if (!r_entry) {
  263. r_entry = da_push_back_new(root_entries);
  264. r_entry->mutex = bmalloc(sizeof(pthread_mutex_t));
  265. pthread_mutex_init(r_entry->mutex, NULL);
  266. r_entry->name = name;
  267. r_entry->entry = bzalloc(sizeof(profile_entry));
  268. init_entry(r_entry->entry, name);
  269. }
  270. return r_entry;
  271. }
  272. void profile_register_root(const char *name,
  273. uint64_t expected_time_between_calls)
  274. {
  275. if (!lock_root())
  276. return;
  277. get_root_entry(name)->entry->expected_time_between_calls =
  278. (expected_time_between_calls + 500) / 1000;
  279. pthread_mutex_unlock(&root_mutex);
  280. }
  281. static void free_call_context(profile_call *context);
  282. static void merge_context(profile_call *context)
  283. {
  284. pthread_mutex_t *mutex = NULL;
  285. profile_entry *entry = NULL;
  286. profile_call *prev_call = NULL;
  287. if (!lock_root()) {
  288. free_call_context(context);
  289. return;
  290. }
  291. profile_root_entry *r_entry = get_root_entry(context->name);
  292. mutex = r_entry->mutex;
  293. entry = r_entry->entry;
  294. prev_call = r_entry->prev_call;
  295. r_entry->prev_call = context;
  296. pthread_mutex_lock(mutex);
  297. pthread_mutex_unlock(&root_mutex);
  298. merge_call(entry, context, prev_call);
  299. pthread_mutex_unlock(mutex);
  300. free_call_context(prev_call);
  301. }
  302. void profile_start(const char *name)
  303. {
  304. if (!thread_enabled)
  305. return;
  306. profile_call new_call = {
  307. .name = name,
  308. #ifdef TRACK_OVERHEAD
  309. .overhead_start = os_gettime_ns(),
  310. #endif
  311. .parent = thread_context,
  312. };
  313. profile_call *call = NULL;
  314. if (new_call.parent) {
  315. size_t idx = da_push_back(new_call.parent->children, &new_call);
  316. call = &new_call.parent->children.array[idx];
  317. } else {
  318. call = bmalloc(sizeof(profile_call));
  319. memcpy(call, &new_call, sizeof(profile_call));
  320. }
  321. thread_context = call;
  322. call->start_time = os_gettime_ns();
  323. }
  324. void profile_end(const char *name)
  325. {
  326. uint64_t end = os_gettime_ns();
  327. if (!thread_enabled)
  328. return;
  329. profile_call *call = thread_context;
  330. if (!call) {
  331. blog(LOG_ERROR, "Called profile end with no active profile");
  332. return;
  333. }
  334. if (!call->name)
  335. call->name = name;
  336. if (call->name != name) {
  337. blog(LOG_ERROR, "Called profile end with mismatching name: "
  338. "start(\"%s\"[%p]) <-> end(\"%s\"[%p])",
  339. call->name, call->name, name, name);
  340. profile_call *parent = call->parent;
  341. while (parent && parent->parent && parent->name != name)
  342. parent = parent->parent;
  343. if (!parent || parent->name != name)
  344. return;
  345. while (call->name != name) {
  346. profile_end(call->name);
  347. call = call->parent;
  348. }
  349. }
  350. thread_context = call->parent;
  351. call->end_time = end;
  352. #ifdef TRACK_OVERHEAD
  353. call->overhead_end = os_gettime_ns();
  354. #endif
  355. if (call->parent)
  356. return;
  357. merge_context(call);
  358. }
  359. static int profiler_time_entry_compare(const void *first, const void *second)
  360. {
  361. int64_t diff = ((profiler_time_entry*)second)->time_delta -
  362. ((profiler_time_entry*)first)->time_delta;
  363. return diff < 0 ? -1 : (diff > 0 ? 1 : 0);
  364. }
  365. static uint64_t copy_map_to_array(profile_times_table *map,
  366. profiler_time_entries_t *entry_buffer,
  367. uint64_t *min_, uint64_t *max_)
  368. {
  369. migrate_old_entries(map, false);
  370. da_reserve((*entry_buffer), map->occupied);
  371. da_resize((*entry_buffer), 0);
  372. uint64_t min__ = ~(uint64_t)0;
  373. uint64_t max__ = 0;
  374. uint64_t calls = 0;
  375. for (size_t i = 0; i < map->size; i++) {
  376. if (!map->entries[i].probes)
  377. continue;
  378. profiler_time_entry *entry = &map->entries[i].entry;
  379. da_push_back((*entry_buffer), entry);
  380. calls += entry->count;
  381. min__ = (min__ < entry->time_delta) ? min__ : entry->time_delta;
  382. max__ = (max__ > entry->time_delta) ? max__ : entry->time_delta;
  383. }
  384. if (min_)
  385. *min_ = min__;
  386. if (max_)
  387. *max_ = max__;
  388. return calls;
  389. }
  390. typedef void (*profile_entry_print_func)(profiler_snapshot_entry_t *entry,
  391. struct dstr *indent_buffer, struct dstr *output_buffer,
  392. unsigned indent, uint64_t active, uint64_t parent_calls);
  393. /* UTF-8 characters */
  394. #define VPIPE_RIGHT " \xe2\x94\xa3"
  395. #define VPIPE " \xe2\x95\x83"
  396. #define DOWN_RIGHT " \xe2\x94\x97"
  397. static void make_indent_string(struct dstr *indent_buffer, unsigned indent,
  398. uint64_t active)
  399. {
  400. indent_buffer->len = 0;
  401. if (!indent) {
  402. dstr_cat_ch(indent_buffer, 0);
  403. return;
  404. }
  405. for (size_t i = 0; i < indent; i++) {
  406. const char *fragment = "";
  407. bool last = i + 1 == indent;
  408. if (active & ((uint64_t)1 << i))
  409. fragment = last ? VPIPE_RIGHT : VPIPE;
  410. else
  411. fragment = last ? DOWN_RIGHT : " ";
  412. dstr_cat(indent_buffer, fragment);
  413. }
  414. }
  415. static void gather_stats(uint64_t expected_time_between_calls,
  416. profiler_time_entries_t *entries,
  417. uint64_t calls, uint64_t *percentile99, uint64_t *median,
  418. double *percent_within_bounds)
  419. {
  420. if (!entries->num) {
  421. *percentile99 = 0;
  422. *median = 0;
  423. *percent_within_bounds = 0.;
  424. return;
  425. }
  426. /*if (entry_buffer->num > 2)
  427. blog(LOG_INFO, "buffer-size %lu, overall count %llu\n"
  428. "map-size %lu, occupied %lu, probes %lu",
  429. entry_buffer->num, calls,
  430. map->size, map->occupied,
  431. map->max_probe_count);*/
  432. uint64_t accu = 0;
  433. for (size_t i = 0; i < entries->num; i++) {
  434. uint64_t old_accu = accu;
  435. accu += entries->array[i].count;
  436. if (old_accu < calls * 0.01 && accu >= calls * 0.01)
  437. *percentile99 = entries->array[i].time_delta;
  438. else if (old_accu < calls * 0.5 && accu >= calls * 0.5) {
  439. *median = entries->array[i].time_delta;
  440. break;
  441. }
  442. }
  443. *percent_within_bounds = 0.;
  444. if (!expected_time_between_calls)
  445. return;
  446. accu = 0;
  447. for (size_t i = 0; i < entries->num; i++) {
  448. profiler_time_entry *entry = &entries->array[i];
  449. if (entry->time_delta < expected_time_between_calls)
  450. break;
  451. accu += entry->count;
  452. }
  453. *percent_within_bounds = (1. - (double)accu / calls) * 100;
  454. }
  455. static void profile_print_entry(profiler_snapshot_entry_t *entry,
  456. struct dstr *indent_buffer, struct dstr *output_buffer,
  457. unsigned indent, uint64_t active, uint64_t parent_calls)
  458. {
  459. uint64_t calls = entry->overall_count;
  460. uint64_t min_ = entry->min_time;
  461. uint64_t max_ = entry->max_time;
  462. uint64_t percentile99 = 0;
  463. uint64_t median = 0;
  464. double percent_within_bounds = 0.;
  465. gather_stats(entry->expected_time_between_calls,
  466. &entry->times, calls,
  467. &percentile99, &median, &percent_within_bounds);
  468. make_indent_string(indent_buffer, indent, active);
  469. if (min_ == max_) {
  470. dstr_printf(output_buffer, "%s%s: %g ms",
  471. indent_buffer->array, entry->name,
  472. min_ / 1000.);
  473. } else {
  474. dstr_printf(output_buffer, "%s%s: min=%g ms, median=%g ms, "
  475. "max=%g ms, 99th percentile=%g ms",
  476. indent_buffer->array, entry->name,
  477. min_ / 1000., median / 1000., max_ / 1000.,
  478. percentile99 / 1000.);
  479. if (entry->expected_time_between_calls) {
  480. double expected_ms =
  481. entry->expected_time_between_calls / 1000.;
  482. dstr_catf(output_buffer, ", %g%% below %g ms",
  483. percent_within_bounds, expected_ms);
  484. }
  485. }
  486. if (parent_calls && calls != parent_calls) {
  487. double calls_per_parent = (double)calls / parent_calls;
  488. if (lround(calls_per_parent * 10) != 10)
  489. dstr_catf(output_buffer, ", %g calls per parent call",
  490. calls_per_parent);
  491. }
  492. blog(LOG_INFO, "%s", output_buffer->array);
  493. active |= (uint64_t)1 << indent;
  494. for (size_t i = 0; i < entry->children.num; i++) {
  495. if ((i + 1) == entry->children.num)
  496. active &= (1 << indent) - 1;
  497. profile_print_entry(&entry->children.array[i],
  498. indent_buffer, output_buffer,
  499. indent + 1, active, calls);
  500. }
  501. }
  502. static void gather_stats_between(profiler_time_entries_t *entries,
  503. uint64_t calls, uint64_t lower_bound, uint64_t upper_bound,
  504. uint64_t min_, uint64_t max_, uint64_t *median,
  505. double *percent, double *lower, double *higher)
  506. {
  507. *median = 0;
  508. *percent = 0.;
  509. *lower = 0.;
  510. *higher = 0.;
  511. if (!entries->num)
  512. return;
  513. uint64_t accu = 0;
  514. for (size_t i = 0; i < entries->num; i++) {
  515. accu += entries->array[i].count;
  516. if (accu < calls * 0.5)
  517. continue;
  518. *median = entries->array[i].time_delta;
  519. break;
  520. }
  521. bool found_upper_bound = max_ <= upper_bound;
  522. bool found_lower_bound = false;
  523. if (min_ >= upper_bound) {
  524. *higher = 100.;
  525. return;
  526. }
  527. if (found_upper_bound && min_ >= lower_bound) {
  528. *percent = 100.;
  529. return;
  530. }
  531. accu = 0;
  532. for (size_t i = 0; i < entries->num; i++) {
  533. uint64_t delta = entries->array[i].time_delta;
  534. if (!found_upper_bound && delta <= upper_bound) {
  535. *higher = (double)accu / calls * 100;
  536. accu = 0;
  537. found_upper_bound = true;
  538. }
  539. if (!found_lower_bound && delta < lower_bound) {
  540. *percent = (double)accu / calls * 100;
  541. accu = 0;
  542. found_lower_bound = true;
  543. }
  544. accu += entries->array[i].count;
  545. }
  546. if (!found_upper_bound) {
  547. *higher = 100.;
  548. } else if (!found_lower_bound) {
  549. *percent = (double)accu / calls * 100;
  550. } else {
  551. *lower = (double)accu / calls * 100;
  552. }
  553. }
  554. static void profile_print_entry_expected(profiler_snapshot_entry_t *entry,
  555. struct dstr *indent_buffer, struct dstr *output_buffer,
  556. unsigned indent, uint64_t active, uint64_t parent_calls)
  557. {
  558. UNUSED_PARAMETER(parent_calls);
  559. if (!entry->expected_time_between_calls)
  560. return;
  561. uint64_t expected_time = entry->expected_time_between_calls;
  562. uint64_t min_ = entry->min_time_between_calls;
  563. uint64_t max_ = entry->max_time_between_calls;
  564. uint64_t median = 0;
  565. double percent = 0.;
  566. double lower = 0.;
  567. double higher = 0.;
  568. gather_stats_between(&entry->times_between_calls,
  569. entry->overall_between_calls_count,
  570. (uint64_t)(expected_time * 0.98),
  571. (uint64_t)(expected_time * 1.02 + 0.5),
  572. min_, max_,
  573. &median, &percent, &lower, &higher);
  574. make_indent_string(indent_buffer, indent, active);
  575. blog(LOG_INFO, "%s%s: min=%g ms, median=%g ms, max=%g ms, %g%% "
  576. "within ±2%% of %g ms (%g%% lower, %g%% higher)",
  577. indent_buffer->array, entry->name,
  578. min_ / 1000., median / 1000., max_ / 1000., percent,
  579. expected_time / 1000.,
  580. lower, higher);
  581. active |= (uint64_t)1 << indent;
  582. for (size_t i = 0; i < entry->children.num; i++) {
  583. if ((i + 1) == entry->children.num)
  584. active &= (1 << indent) - 1;
  585. profile_print_entry_expected(&entry->children.array[i],
  586. indent_buffer, output_buffer,
  587. indent + 1, active, 0);
  588. }
  589. }
  590. void profile_print_func(const char *intro, profile_entry_print_func print,
  591. profiler_snapshot_t *snap)
  592. {
  593. struct dstr indent_buffer = {0};
  594. struct dstr output_buffer = {0};
  595. bool free_snapshot = !snap;
  596. if (!snap)
  597. snap = profile_snapshot_create();
  598. blog(LOG_INFO, "%s", intro);
  599. for (size_t i = 0; i < snap->roots.num; i++) {
  600. print(&snap->roots.array[i],
  601. &indent_buffer, &output_buffer, 0, 0, 0);
  602. }
  603. blog(LOG_INFO, "=================================================");
  604. if (free_snapshot)
  605. profile_snapshot_free(snap);
  606. dstr_free(&output_buffer);
  607. dstr_free(&indent_buffer);
  608. }
  609. void profiler_print(profiler_snapshot_t *snap)
  610. {
  611. profile_print_func("== Profiler Results =============================",
  612. profile_print_entry, snap);
  613. }
  614. void profiler_print_time_between_calls(profiler_snapshot_t *snap)
  615. {
  616. profile_print_func("== Profiler Time Between Calls ==================",
  617. profile_print_entry_expected, snap);
  618. }
  619. static void free_call_children(profile_call *call)
  620. {
  621. if (!call)
  622. return;
  623. const size_t num = call->children.num;
  624. for (size_t i = 0; i < num; i++)
  625. free_call_children(&call->children.array[i]);
  626. da_free(call->children);
  627. }
  628. static void free_call_context(profile_call *context)
  629. {
  630. free_call_children(context);
  631. bfree(context);
  632. }
  633. static void free_hashmap(profile_times_table *map)
  634. {
  635. map->size = 0;
  636. bfree(map->entries);
  637. map->entries = NULL;
  638. bfree(map->old_entries);
  639. map->old_entries = NULL;
  640. }
  641. static void free_profile_entry(profile_entry *entry)
  642. {
  643. for (size_t i = 0; i < entry->children.num; i++)
  644. free_profile_entry(&entry->children.array[i]);
  645. free_hashmap(&entry->times);
  646. #ifdef TRACK_OVERHEAD
  647. free_hashmap(&entry->overhead);
  648. #endif
  649. free_hashmap(&entry->times_between_calls);
  650. da_free(entry->children);
  651. }
  652. void profiler_free(void)
  653. {
  654. DARRAY(profile_root_entry) old_root_entries = {0};
  655. pthread_mutex_lock(&root_mutex);
  656. enabled = false;
  657. da_move(old_root_entries, root_entries);
  658. pthread_mutex_unlock(&root_mutex);
  659. for (size_t i = 0; i < old_root_entries.num; i++) {
  660. profile_root_entry *entry = &old_root_entries.array[i];
  661. pthread_mutex_lock(entry->mutex);
  662. pthread_mutex_unlock(entry->mutex);
  663. pthread_mutex_destroy(entry->mutex);
  664. bfree(entry->mutex);
  665. entry->mutex = NULL;
  666. free_call_context(entry->prev_call);
  667. free_profile_entry(entry->entry);
  668. bfree(entry->entry);
  669. }
  670. da_free(old_root_entries);
  671. }
  672. /* ------------------------------------------------------------------------- */
  673. /* Profiler name storage */
  674. struct profiler_name_store {
  675. pthread_mutex_t mutex;
  676. DARRAY(char*) names;
  677. };
  678. profiler_name_store_t *profiler_name_store_create(void)
  679. {
  680. profiler_name_store_t *store = bzalloc(sizeof(profiler_name_store_t));
  681. if (pthread_mutex_init(&store->mutex, NULL))
  682. goto error;
  683. return store;
  684. error:
  685. bfree(store);
  686. return NULL;
  687. }
  688. void profiler_name_store_free(profiler_name_store_t *store)
  689. {
  690. if (!store)
  691. return;
  692. for (size_t i = 0; i < store->names.num; i++)
  693. bfree(store->names.array[i]);
  694. da_free(store->names);
  695. bfree(store);
  696. }
  697. const char *profile_store_name(profiler_name_store_t *store,
  698. const char *format, ...)
  699. {
  700. va_list args;
  701. va_start(args, format);
  702. struct dstr str = {0};
  703. dstr_vprintf(&str, format, args);
  704. va_end(args);
  705. const char *result = NULL;
  706. pthread_mutex_lock(&store->mutex);
  707. size_t idx = da_push_back(store->names, &str.array);
  708. result = store->names.array[idx];
  709. pthread_mutex_unlock(&store->mutex);
  710. return result;
  711. }
  712. /* ------------------------------------------------------------------------- */
  713. /* Profiler data access */
  714. static void add_entry_to_snapshot(profile_entry *entry,
  715. profiler_snapshot_entry_t *s_entry)
  716. {
  717. s_entry->name = entry->name;
  718. s_entry->overall_count = copy_map_to_array(&entry->times,
  719. &s_entry->times,
  720. &s_entry->min_time, &s_entry->max_time);
  721. if ((s_entry->expected_time_between_calls =
  722. entry->expected_time_between_calls))
  723. s_entry->overall_between_calls_count =
  724. copy_map_to_array(&entry->times_between_calls,
  725. &s_entry->times_between_calls,
  726. &s_entry->min_time_between_calls,
  727. &s_entry->max_time_between_calls);
  728. da_reserve(s_entry->children, entry->children.num);
  729. for (size_t i = 0; i < entry->children.num; i++)
  730. add_entry_to_snapshot(&entry->children.array[i],
  731. da_push_back_new(s_entry->children));
  732. }
  733. static void sort_snapshot_entry(profiler_snapshot_entry_t *entry)
  734. {
  735. qsort(entry->times.array, entry->times.num,
  736. sizeof(profiler_time_entry),
  737. profiler_time_entry_compare);
  738. if (entry->expected_time_between_calls)
  739. qsort(entry->times_between_calls.array,
  740. entry->times_between_calls.num,
  741. sizeof(profiler_time_entry),
  742. profiler_time_entry_compare);
  743. for (size_t i = 0; i < entry->children.num; i++)
  744. sort_snapshot_entry(&entry->children.array[i]);
  745. }
  746. profiler_snapshot_t *profile_snapshot_create(void)
  747. {
  748. profiler_snapshot_t *snap = bzalloc(sizeof(profiler_snapshot_t));
  749. pthread_mutex_lock(&root_mutex);
  750. da_reserve(snap->roots, root_entries.num);
  751. for (size_t i = 0; i < root_entries.num; i++) {
  752. pthread_mutex_lock(root_entries.array[i].mutex);
  753. add_entry_to_snapshot(root_entries.array[i].entry,
  754. da_push_back_new(snap->roots));
  755. pthread_mutex_unlock(root_entries.array[i].mutex);
  756. }
  757. pthread_mutex_unlock(&root_mutex);
  758. for (size_t i = 0; i < snap->roots.num; i++)
  759. sort_snapshot_entry(&snap->roots.array[i]);
  760. return snap;
  761. }
  762. static void free_snapshot_entry(profiler_snapshot_entry_t *entry)
  763. {
  764. for (size_t i = 0; i < entry->children.num; i++)
  765. free_snapshot_entry(&entry->children.array[i]);
  766. da_free(entry->children);
  767. da_free(entry->times_between_calls);
  768. da_free(entry->times);
  769. }
  770. void profile_snapshot_free(profiler_snapshot_t *snap)
  771. {
  772. if (!snap)
  773. return;
  774. for (size_t i = 0; i < snap->roots.num; i++)
  775. free_snapshot_entry(&snap->roots.array[i]);
  776. da_free(snap->roots);
  777. bfree(snap);
  778. }
  779. typedef void (*dump_csv_func)(void *data, struct dstr *buffer);
  780. static void entry_dump_csv(struct dstr *buffer,
  781. const profiler_snapshot_entry_t *parent,
  782. const profiler_snapshot_entry_t *entry,
  783. dump_csv_func func, void *data)
  784. {
  785. const char *parent_name = parent ? parent->name : NULL;
  786. for (size_t i = 0; i < entry->times.num; i++) {
  787. dstr_printf(buffer, "%p,%p,%p,%p,%s,0,%llu,%llu\n", entry,
  788. parent, entry->name, parent_name, entry->name,
  789. entry->times.array[i].time_delta,
  790. entry->times.array[i].count);
  791. func(data, buffer);
  792. }
  793. for (size_t i = 0; i < entry->times_between_calls.num; i++) {
  794. dstr_printf(buffer, "%p,%p,%p,%p,%s,%llu,%llu,%llu\n", entry,
  795. parent, entry->name, parent_name, entry->name,
  796. entry->expected_time_between_calls,
  797. entry->times_between_calls.array[i].time_delta,
  798. entry->times_between_calls.array[i].count);
  799. func(data, buffer);
  800. }
  801. for (size_t i = 0; i < entry->children.num; i++)
  802. entry_dump_csv(buffer, entry, &entry->children.array[i],
  803. func, data);
  804. }
  805. static void profiler_snapshot_dump(const profiler_snapshot_t *snap,
  806. dump_csv_func func, void *data)
  807. {
  808. struct dstr buffer = {0};
  809. dstr_init_copy(&buffer, "id,parent_id,name_id,parent_name_id,name,"
  810. "time_between_calls,time_delta_µs,count\n");
  811. func(data, &buffer);
  812. for (size_t i = 0; i < snap->roots.num; i++)
  813. entry_dump_csv(&buffer, NULL,
  814. &snap->roots.array[i], func, data);
  815. dstr_free(&buffer);
  816. }
  817. static void dump_csv_fwrite(void *data, struct dstr *buffer)
  818. {
  819. fwrite(buffer->array, 1, buffer->len, data);
  820. }
  821. bool profiler_snapshot_dump_csv(const profiler_snapshot_t *snap,
  822. const char *filename)
  823. {
  824. FILE *f = os_fopen(filename, "wb+");
  825. if (!f)
  826. return false;
  827. profiler_snapshot_dump(snap, dump_csv_fwrite, f);
  828. fclose(f);
  829. return true;
  830. }
  831. static void dump_csv_gzwrite(void *data, struct dstr *buffer)
  832. {
  833. gzwrite(data, buffer->array, (unsigned)buffer->len);
  834. }
  835. bool profiler_snapshot_dump_csv_gz(const profiler_snapshot_t *snap,
  836. const char *filename)
  837. {
  838. FILE *f = os_fopen(filename, "wb");
  839. if (!f)
  840. return false;
  841. gzFile gz = gzdopen(fileno(f), "wb");
  842. if (!gz)
  843. return false;
  844. profiler_snapshot_dump(snap, dump_csv_gzwrite, gz);
  845. gzclose_w(gz);
  846. return true;
  847. }
  848. size_t profiler_snapshot_num_roots(profiler_snapshot_t *snap)
  849. {
  850. return snap ? snap->roots.num : 0;
  851. }
  852. void profiler_snapshot_enumerate_roots(profiler_snapshot_t *snap,
  853. profiler_entry_enum_func func, void *context)
  854. {
  855. if (!snap)
  856. return;
  857. for (size_t i = 0; i < snap->roots.num; i++)
  858. if (!func(context, &snap->roots.array[i]))
  859. break;
  860. }
  861. void profiler_snapshot_filter_roots(profiler_snapshot_t *snap,
  862. profiler_name_filter_func func, void *data)
  863. {
  864. for (size_t i = 0; i < snap->roots.num;) {
  865. bool remove = false;
  866. bool res = func(data, snap->roots.array[i].name, &remove);
  867. if (remove) {
  868. free_snapshot_entry(&snap->roots.array[i]);
  869. da_erase(snap->roots, i);
  870. }
  871. if (!res)
  872. break;
  873. if (!remove)
  874. i += 1;
  875. }
  876. }
  877. size_t profiler_snapshot_num_children(profiler_snapshot_entry_t *entry)
  878. {
  879. return entry ? entry->children.num : 0;
  880. }
  881. void profiler_snapshot_enumerate_children(profiler_snapshot_entry_t *entry,
  882. profiler_entry_enum_func func, void *context)
  883. {
  884. if (!entry)
  885. return;
  886. for (size_t i = 0; i < entry->children.num; i++)
  887. if (!func(context, &entry->children.array[i]))
  888. break;
  889. }
  890. const char *profiler_snapshot_entry_name(profiler_snapshot_entry_t *entry)
  891. {
  892. return entry ? entry->name : NULL;
  893. }
  894. profiler_time_entries_t *profiler_snapshot_entry_times(
  895. profiler_snapshot_entry_t *entry)
  896. {
  897. return entry ? &entry->times : NULL;
  898. }
  899. uint64_t profiler_snapshot_entry_overall_count(
  900. profiler_snapshot_entry_t *entry)
  901. {
  902. return entry ? entry->overall_count : 0;
  903. }
  904. uint64_t profiler_snapshot_entry_min_time(profiler_snapshot_entry_t *entry)
  905. {
  906. return entry ? entry->min_time : 0;
  907. }
  908. uint64_t profiler_snapshot_entry_max_time(profiler_snapshot_entry_t *entry)
  909. {
  910. return entry ? entry->max_time : 0;
  911. }
  912. profiler_time_entries_t *profiler_snapshot_entry_times_between_calls(
  913. profiler_snapshot_entry_t *entry)
  914. {
  915. return entry ? &entry->times_between_calls : NULL;
  916. }
  917. uint64_t profiler_snapshot_entry_expected_time_between_calls(
  918. profiler_snapshot_entry_t *entry)
  919. {
  920. return entry ? entry->expected_time_between_calls : 0;
  921. }
  922. uint64_t profiler_snapshot_entry_min_time_between_calls(
  923. profiler_snapshot_entry_t *entry)
  924. {
  925. return entry ? entry->min_time_between_calls : 0;
  926. }
  927. uint64_t profiler_snapshot_entry_max_time_between_calls(
  928. profiler_snapshot_entry_t *entry)
  929. {
  930. return entry ? entry->max_time_between_calls : 0;
  931. }
  932. uint64_t profiler_snapshot_entry_overall_between_calls_count(
  933. profiler_snapshot_entry_t *entry)
  934. {
  935. return entry ? entry->overall_between_calls_count : 0;
  936. }