profiler.c 27 KB

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