profiler.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  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 =
  82. map->max_probe_count < val ? 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, uint64_t usec,
  86. 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, uint64_t usec,
  158. 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. static THREAD_LOCAL profile_call *thread_context = NULL;
  218. static THREAD_LOCAL bool thread_enabled = true;
  219. void profiler_start(void)
  220. {
  221. pthread_mutex_lock(&root_mutex);
  222. enabled = true;
  223. pthread_mutex_unlock(&root_mutex);
  224. }
  225. void profiler_stop(void)
  226. {
  227. pthread_mutex_lock(&root_mutex);
  228. enabled = false;
  229. pthread_mutex_unlock(&root_mutex);
  230. }
  231. void profile_reenable_thread(void)
  232. {
  233. if (thread_enabled)
  234. return;
  235. pthread_mutex_lock(&root_mutex);
  236. thread_enabled = enabled;
  237. pthread_mutex_unlock(&root_mutex);
  238. }
  239. static bool lock_root(void)
  240. {
  241. pthread_mutex_lock(&root_mutex);
  242. if (!enabled) {
  243. pthread_mutex_unlock(&root_mutex);
  244. thread_enabled = false;
  245. return false;
  246. }
  247. return true;
  248. }
  249. static profile_root_entry *get_root_entry(const char *name)
  250. {
  251. profile_root_entry *r_entry = NULL;
  252. for (size_t i = 0; i < root_entries.num; i++) {
  253. if (root_entries.array[i].name == name) {
  254. r_entry = &root_entries.array[i];
  255. break;
  256. }
  257. }
  258. if (!r_entry) {
  259. r_entry = da_push_back_new(root_entries);
  260. r_entry->mutex = bmalloc(sizeof(pthread_mutex_t));
  261. pthread_mutex_init(r_entry->mutex, NULL);
  262. r_entry->name = name;
  263. r_entry->entry = bzalloc(sizeof(profile_entry));
  264. init_entry(r_entry->entry, name);
  265. }
  266. return r_entry;
  267. }
  268. void profile_register_root(const char *name,
  269. uint64_t expected_time_between_calls)
  270. {
  271. if (!lock_root())
  272. return;
  273. get_root_entry(name)->entry->expected_time_between_calls =
  274. (expected_time_between_calls + 500) / 1000;
  275. pthread_mutex_unlock(&root_mutex);
  276. }
  277. static void free_call_context(profile_call *context);
  278. static void merge_context(profile_call *context)
  279. {
  280. pthread_mutex_t *mutex = NULL;
  281. profile_entry *entry = NULL;
  282. profile_call *prev_call = NULL;
  283. if (!lock_root()) {
  284. free_call_context(context);
  285. return;
  286. }
  287. profile_root_entry *r_entry = get_root_entry(context->name);
  288. mutex = r_entry->mutex;
  289. entry = r_entry->entry;
  290. prev_call = r_entry->prev_call;
  291. r_entry->prev_call = context;
  292. pthread_mutex_lock(mutex);
  293. pthread_mutex_unlock(&root_mutex);
  294. merge_call(entry, context, prev_call);
  295. pthread_mutex_unlock(mutex);
  296. free_call_context(prev_call);
  297. }
  298. void profile_start(const char *name)
  299. {
  300. if (!thread_enabled)
  301. return;
  302. profile_call new_call = {
  303. .name = name,
  304. #ifdef TRACK_OVERHEAD
  305. .overhead_start = os_gettime_ns(),
  306. #endif
  307. .parent = thread_context,
  308. };
  309. profile_call *call = NULL;
  310. if (new_call.parent) {
  311. size_t idx = da_push_back(new_call.parent->children, &new_call);
  312. call = &new_call.parent->children.array[idx];
  313. } else {
  314. call = bmalloc(sizeof(profile_call));
  315. memcpy(call, &new_call, sizeof(profile_call));
  316. }
  317. thread_context = call;
  318. call->start_time = os_gettime_ns();
  319. }
  320. void profile_end(const char *name)
  321. {
  322. uint64_t end = os_gettime_ns();
  323. if (!thread_enabled)
  324. return;
  325. profile_call *call = thread_context;
  326. if (!call) {
  327. blog(LOG_ERROR, "Called profile end with no active profile");
  328. return;
  329. }
  330. if (!call->name)
  331. call->name = name;
  332. if (call->name != name) {
  333. blog(LOG_ERROR,
  334. "Called profile end with mismatching name: "
  335. "start(\"%s\"[%p]) <-> end(\"%s\"[%p])",
  336. call->name, call->name, name, name);
  337. profile_call *parent = call->parent;
  338. while (parent && parent->parent && parent->name != name)
  339. parent = parent->parent;
  340. if (!parent || parent->name != name)
  341. return;
  342. while (call->name != name) {
  343. profile_end(call->name);
  344. call = call->parent;
  345. }
  346. }
  347. thread_context = call->parent;
  348. call->end_time = end;
  349. #ifdef TRACK_OVERHEAD
  350. call->overhead_end = os_gettime_ns();
  351. #endif
  352. if (call->parent)
  353. return;
  354. merge_context(call);
  355. }
  356. static int profiler_time_entry_compare(const void *first, const void *second)
  357. {
  358. int64_t diff = ((profiler_time_entry *)second)->time_delta -
  359. ((profiler_time_entry *)first)->time_delta;
  360. return diff < 0 ? -1 : (diff > 0 ? 1 : 0);
  361. }
  362. static uint64_t copy_map_to_array(profile_times_table *map,
  363. profiler_time_entries_t *entry_buffer,
  364. uint64_t *min_, uint64_t *max_)
  365. {
  366. migrate_old_entries(map, false);
  367. da_reserve((*entry_buffer), map->occupied);
  368. da_resize((*entry_buffer), 0);
  369. uint64_t min__ = ~(uint64_t)0;
  370. uint64_t max__ = 0;
  371. uint64_t calls = 0;
  372. for (size_t i = 0; i < map->size; i++) {
  373. if (!map->entries[i].probes)
  374. continue;
  375. profiler_time_entry *entry = &map->entries[i].entry;
  376. da_push_back((*entry_buffer), entry);
  377. calls += entry->count;
  378. min__ = (min__ < entry->time_delta) ? min__ : entry->time_delta;
  379. max__ = (max__ > entry->time_delta) ? max__ : entry->time_delta;
  380. }
  381. if (min_)
  382. *min_ = min__;
  383. if (max_)
  384. *max_ = max__;
  385. return calls;
  386. }
  387. typedef void (*profile_entry_print_func)(profiler_snapshot_entry_t *entry,
  388. struct dstr *indent_buffer,
  389. struct dstr *output_buffer,
  390. unsigned indent, uint64_t active,
  391. uint64_t parent_calls);
  392. /* UTF-8 characters */
  393. #define VPIPE_RIGHT " \xe2\x94\xa3"
  394. #define VPIPE " \xe2\x94\x83"
  395. #define DOWN_RIGHT " \xe2\x94\x97"
  396. static void make_indent_string(struct dstr *indent_buffer, unsigned indent,
  397. uint64_t active)
  398. {
  399. indent_buffer->len = 0;
  400. if (!indent) {
  401. dstr_cat_ch(indent_buffer, 0);
  402. return;
  403. }
  404. for (size_t i = 0; i < indent; i++) {
  405. const char *fragment = "";
  406. bool last = i + 1 == indent;
  407. if (active & ((uint64_t)1 << i))
  408. fragment = last ? VPIPE_RIGHT : VPIPE;
  409. else
  410. fragment = last ? DOWN_RIGHT : " ";
  411. dstr_cat(indent_buffer, fragment);
  412. }
  413. }
  414. static void gather_stats(uint64_t expected_time_between_calls,
  415. profiler_time_entries_t *entries, uint64_t calls,
  416. uint64_t *percentile99, uint64_t *median,
  417. double *percent_within_bounds)
  418. {
  419. if (!entries->num) {
  420. *percentile99 = 0;
  421. *median = 0;
  422. *percent_within_bounds = 0.;
  423. return;
  424. }
  425. /*if (entry_buffer->num > 2)
  426. blog(LOG_INFO, "buffer-size %lu, overall count %llu\n"
  427. "map-size %lu, occupied %lu, probes %lu",
  428. entry_buffer->num, calls,
  429. map->size, map->occupied,
  430. map->max_probe_count);*/
  431. uint64_t accu = 0;
  432. for (size_t i = 0; i < entries->num; i++) {
  433. uint64_t old_accu = accu;
  434. accu += entries->array[i].count;
  435. if (old_accu < calls * 0.01 && accu >= calls * 0.01)
  436. *percentile99 = entries->array[i].time_delta;
  437. else if (old_accu < calls * 0.5 && accu >= calls * 0.5) {
  438. *median = entries->array[i].time_delta;
  439. break;
  440. }
  441. }
  442. *percent_within_bounds = 0.;
  443. if (!expected_time_between_calls)
  444. return;
  445. accu = 0;
  446. for (size_t i = 0; i < entries->num; i++) {
  447. profiler_time_entry *entry = &entries->array[i];
  448. if (entry->time_delta < expected_time_between_calls)
  449. break;
  450. accu += entry->count;
  451. }
  452. *percent_within_bounds = (1. - (double)accu / calls) * 100;
  453. }
  454. #define G_MS "g\xC2\xA0ms"
  455. static void profile_print_entry(profiler_snapshot_entry_t *entry,
  456. struct dstr *indent_buffer,
  457. struct dstr *output_buffer, unsigned indent,
  458. 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, &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, indent_buffer->array,
  471. entry->name, min_ / 1000.);
  472. } else {
  473. dstr_printf(output_buffer,
  474. "%s%s: min=%" G_MS ", median=%" G_MS ", "
  475. "max=%" G_MS ", 99th percentile=%" G_MS,
  476. indent_buffer->array, entry->name, min_ / 1000.,
  477. median / 1000., max_ / 1000., percentile99 / 1000.);
  478. if (entry->expected_time_between_calls) {
  479. double expected_ms =
  480. entry->expected_time_between_calls / 1000.;
  481. dstr_catf(output_buffer, ", %g%% below %" G_MS,
  482. percent_within_bounds, expected_ms);
  483. }
  484. }
  485. if (parent_calls && calls != parent_calls) {
  486. double calls_per_parent = (double)calls / parent_calls;
  487. if (lround(calls_per_parent * 10) != 10)
  488. dstr_catf(output_buffer, ", %g calls per parent call",
  489. calls_per_parent);
  490. }
  491. blog(LOG_INFO, "%s", output_buffer->array);
  492. active |= (uint64_t)1 << indent;
  493. for (size_t i = 0; i < entry->children.num; i++) {
  494. if ((i + 1) == entry->children.num)
  495. active &= (1 << indent) - 1;
  496. profile_print_entry(&entry->children.array[i], indent_buffer,
  497. output_buffer, indent + 1, active, calls);
  498. }
  499. }
  500. static void gather_stats_between(profiler_time_entries_t *entries,
  501. uint64_t calls, uint64_t lower_bound,
  502. uint64_t upper_bound, uint64_t min_,
  503. uint64_t max_, uint64_t *median,
  504. double *percent, double *lower, double *higher)
  505. {
  506. *median = 0;
  507. *percent = 0.;
  508. *lower = 0.;
  509. *higher = 0.;
  510. if (!entries->num)
  511. return;
  512. uint64_t accu = 0;
  513. for (size_t i = 0; i < entries->num; i++) {
  514. accu += entries->array[i].count;
  515. if (accu < calls * 0.5)
  516. continue;
  517. *median = entries->array[i].time_delta;
  518. break;
  519. }
  520. bool found_upper_bound = max_ <= upper_bound;
  521. bool found_lower_bound = false;
  522. if (min_ >= upper_bound) {
  523. *higher = 100.;
  524. return;
  525. }
  526. if (found_upper_bound && min_ >= lower_bound) {
  527. *percent = 100.;
  528. return;
  529. }
  530. accu = 0;
  531. for (size_t i = 0; i < entries->num; i++) {
  532. uint64_t delta = entries->array[i].time_delta;
  533. if (!found_upper_bound && delta <= upper_bound) {
  534. *higher = (double)accu / calls * 100;
  535. accu = 0;
  536. found_upper_bound = true;
  537. }
  538. if (!found_lower_bound && delta < lower_bound) {
  539. *percent = (double)accu / calls * 100;
  540. accu = 0;
  541. found_lower_bound = true;
  542. }
  543. accu += entries->array[i].count;
  544. }
  545. if (!found_upper_bound) {
  546. *higher = 100.;
  547. } else if (!found_lower_bound) {
  548. *percent = (double)accu / calls * 100;
  549. } else {
  550. *lower = (double)accu / calls * 100;
  551. }
  552. }
  553. static void profile_print_entry_expected(profiler_snapshot_entry_t *entry,
  554. struct dstr *indent_buffer,
  555. struct dstr *output_buffer,
  556. unsigned indent, uint64_t active,
  557. 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), min_, max_,
  573. &median, &percent, &lower, &higher);
  574. make_indent_string(indent_buffer, indent, active);
  575. blog(LOG_INFO,
  576. "%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, min_ / 1000., median / 1000.,
  579. max_ / 1000., percent, expected_time / 1000., lower, higher);
  580. active |= (uint64_t)1 << indent;
  581. for (size_t i = 0; i < entry->children.num; i++) {
  582. if ((i + 1) == entry->children.num)
  583. active &= (1 << indent) - 1;
  584. profile_print_entry_expected(&entry->children.array[i],
  585. indent_buffer, output_buffer,
  586. indent + 1, active, 0);
  587. }
  588. }
  589. void profile_print_func(const char *intro, profile_entry_print_func print,
  590. profiler_snapshot_t *snap)
  591. {
  592. struct dstr indent_buffer = {0};
  593. struct dstr output_buffer = {0};
  594. bool free_snapshot = !snap;
  595. if (!snap)
  596. snap = profile_snapshot_create();
  597. blog(LOG_INFO, "%s", intro);
  598. for (size_t i = 0; i < snap->roots.num; i++) {
  599. print(&snap->roots.array[i], &indent_buffer, &output_buffer, 0,
  600. 0, 0);
  601. }
  602. blog(LOG_INFO, "=================================================");
  603. if (free_snapshot)
  604. profile_snapshot_free(snap);
  605. dstr_free(&output_buffer);
  606. dstr_free(&indent_buffer);
  607. }
  608. void profiler_print(profiler_snapshot_t *snap)
  609. {
  610. profile_print_func("== Profiler Results =============================",
  611. profile_print_entry, snap);
  612. }
  613. void profiler_print_time_between_calls(profiler_snapshot_t *snap)
  614. {
  615. profile_print_func("== Profiler Time Between Calls ==================",
  616. profile_print_entry_expected, snap);
  617. }
  618. static void free_call_children(profile_call *call)
  619. {
  620. if (!call)
  621. return;
  622. const size_t num = call->children.num;
  623. for (size_t i = 0; i < num; i++)
  624. free_call_children(&call->children.array[i]);
  625. da_free(call->children);
  626. }
  627. static void free_call_context(profile_call *context)
  628. {
  629. free_call_children(context);
  630. bfree(context);
  631. }
  632. static void free_hashmap(profile_times_table *map)
  633. {
  634. map->size = 0;
  635. bfree(map->entries);
  636. map->entries = NULL;
  637. bfree(map->old_entries);
  638. map->old_entries = NULL;
  639. }
  640. static void free_profile_entry(profile_entry *entry)
  641. {
  642. for (size_t i = 0; i < entry->children.num; i++)
  643. free_profile_entry(&entry->children.array[i]);
  644. free_hashmap(&entry->times);
  645. #ifdef TRACK_OVERHEAD
  646. free_hashmap(&entry->overhead);
  647. #endif
  648. free_hashmap(&entry->times_between_calls);
  649. da_free(entry->children);
  650. }
  651. void profiler_free(void)
  652. {
  653. DARRAY(profile_root_entry) old_root_entries = {0};
  654. pthread_mutex_lock(&root_mutex);
  655. enabled = false;
  656. da_move(old_root_entries, root_entries);
  657. pthread_mutex_unlock(&root_mutex);
  658. for (size_t i = 0; i < old_root_entries.num; i++) {
  659. profile_root_entry *entry = &old_root_entries.array[i];
  660. pthread_mutex_lock(entry->mutex);
  661. pthread_mutex_unlock(entry->mutex);
  662. pthread_mutex_destroy(entry->mutex);
  663. bfree(entry->mutex);
  664. entry->mutex = NULL;
  665. free_call_context(entry->prev_call);
  666. free_profile_entry(entry->entry);
  667. bfree(entry->entry);
  668. }
  669. da_free(old_root_entries);
  670. }
  671. /* ------------------------------------------------------------------------- */
  672. /* Profiler name storage */
  673. struct profiler_name_store {
  674. pthread_mutex_t mutex;
  675. DARRAY(char *) names;
  676. };
  677. profiler_name_store_t *profiler_name_store_create(void)
  678. {
  679. profiler_name_store_t *store = bzalloc(sizeof(profiler_name_store_t));
  680. if (pthread_mutex_init(&store->mutex, NULL))
  681. goto error;
  682. return store;
  683. error:
  684. bfree(store);
  685. return NULL;
  686. }
  687. void profiler_name_store_free(profiler_name_store_t *store)
  688. {
  689. if (!store)
  690. return;
  691. for (size_t i = 0; i < store->names.num; i++)
  692. bfree(store->names.array[i]);
  693. da_free(store->names);
  694. bfree(store);
  695. }
  696. const char *profile_store_name(profiler_name_store_t *store, const char *format,
  697. ...)
  698. {
  699. va_list args;
  700. va_start(args, format);
  701. struct dstr str = {0};
  702. dstr_vprintf(&str, format, args);
  703. va_end(args);
  704. const char *result = NULL;
  705. pthread_mutex_lock(&store->mutex);
  706. size_t idx = da_push_back(store->names, &str.array);
  707. result = store->names.array[idx];
  708. pthread_mutex_unlock(&store->mutex);
  709. return result;
  710. }
  711. /* ------------------------------------------------------------------------- */
  712. /* Profiler data access */
  713. static void add_entry_to_snapshot(profile_entry *entry,
  714. profiler_snapshot_entry_t *s_entry)
  715. {
  716. s_entry->name = entry->name;
  717. s_entry->overall_count =
  718. copy_map_to_array(&entry->times, &s_entry->times,
  719. &s_entry->min_time, &s_entry->max_time);
  720. if ((s_entry->expected_time_between_calls =
  721. entry->expected_time_between_calls))
  722. s_entry->overall_between_calls_count =
  723. copy_map_to_array(&entry->times_between_calls,
  724. &s_entry->times_between_calls,
  725. &s_entry->min_time_between_calls,
  726. &s_entry->max_time_between_calls);
  727. da_reserve(s_entry->children, entry->children.num);
  728. for (size_t i = 0; i < entry->children.num; i++)
  729. add_entry_to_snapshot(&entry->children.array[i],
  730. da_push_back_new(s_entry->children));
  731. }
  732. static void sort_snapshot_entry(profiler_snapshot_entry_t *entry)
  733. {
  734. qsort(entry->times.array, entry->times.num, sizeof(profiler_time_entry),
  735. profiler_time_entry_compare);
  736. if (entry->expected_time_between_calls)
  737. qsort(entry->times_between_calls.array,
  738. entry->times_between_calls.num,
  739. sizeof(profiler_time_entry), profiler_time_entry_compare);
  740. for (size_t i = 0; i < entry->children.num; i++)
  741. sort_snapshot_entry(&entry->children.array[i]);
  742. }
  743. profiler_snapshot_t *profile_snapshot_create(void)
  744. {
  745. profiler_snapshot_t *snap = bzalloc(sizeof(profiler_snapshot_t));
  746. pthread_mutex_lock(&root_mutex);
  747. da_reserve(snap->roots, root_entries.num);
  748. for (size_t i = 0; i < root_entries.num; i++) {
  749. pthread_mutex_lock(root_entries.array[i].mutex);
  750. add_entry_to_snapshot(root_entries.array[i].entry,
  751. da_push_back_new(snap->roots));
  752. pthread_mutex_unlock(root_entries.array[i].mutex);
  753. }
  754. pthread_mutex_unlock(&root_mutex);
  755. for (size_t i = 0; i < snap->roots.num; i++)
  756. sort_snapshot_entry(&snap->roots.array[i]);
  757. return snap;
  758. }
  759. static void free_snapshot_entry(profiler_snapshot_entry_t *entry)
  760. {
  761. for (size_t i = 0; i < entry->children.num; i++)
  762. free_snapshot_entry(&entry->children.array[i]);
  763. da_free(entry->children);
  764. da_free(entry->times_between_calls);
  765. da_free(entry->times);
  766. }
  767. void profile_snapshot_free(profiler_snapshot_t *snap)
  768. {
  769. if (!snap)
  770. return;
  771. for (size_t i = 0; i < snap->roots.num; i++)
  772. free_snapshot_entry(&snap->roots.array[i]);
  773. da_free(snap->roots);
  774. bfree(snap);
  775. }
  776. typedef void (*dump_csv_func)(void *data, struct dstr *buffer);
  777. static void entry_dump_csv(struct dstr *buffer,
  778. const profiler_snapshot_entry_t *parent,
  779. const profiler_snapshot_entry_t *entry,
  780. dump_csv_func func, void *data)
  781. {
  782. const char *parent_name = parent ? parent->name : NULL;
  783. for (size_t i = 0; i < entry->times.num; i++) {
  784. dstr_printf(buffer,
  785. "%p,%p,%p,%p,%s,0,"
  786. "%" PRIu64 ",%" PRIu64 "\n",
  787. entry, parent, entry->name, parent_name,
  788. entry->name, entry->times.array[i].time_delta,
  789. entry->times.array[i].count);
  790. func(data, buffer);
  791. }
  792. for (size_t i = 0; i < entry->times_between_calls.num; i++) {
  793. dstr_printf(buffer,
  794. "%p,%p,%p,%p,%s,"
  795. "%" PRIu64 ",%" PRIu64 ",%" PRIu64 "\n",
  796. entry, parent, entry->name, parent_name,
  797. entry->name, entry->expected_time_between_calls,
  798. entry->times_between_calls.array[i].time_delta,
  799. entry->times_between_calls.array[i].count);
  800. func(data, buffer);
  801. }
  802. for (size_t i = 0; i < entry->children.num; i++)
  803. entry_dump_csv(buffer, entry, &entry->children.array[i], func,
  804. data);
  805. }
  806. static void profiler_snapshot_dump(const profiler_snapshot_t *snap,
  807. dump_csv_func func, void *data)
  808. {
  809. struct dstr buffer = {0};
  810. dstr_init_copy(&buffer, "id,parent_id,name_id,parent_name_id,name,"
  811. "time_between_calls,time_delta_µs,count\n");
  812. func(data, &buffer);
  813. for (size_t i = 0; i < snap->roots.num; i++)
  814. entry_dump_csv(&buffer, NULL, &snap->roots.array[i], func,
  815. data);
  816. dstr_free(&buffer);
  817. }
  818. static void dump_csv_fwrite(void *data, struct dstr *buffer)
  819. {
  820. fwrite(buffer->array, 1, buffer->len, data);
  821. }
  822. bool profiler_snapshot_dump_csv(const profiler_snapshot_t *snap,
  823. const char *filename)
  824. {
  825. FILE *f = os_fopen(filename, "wb+");
  826. if (!f)
  827. return false;
  828. profiler_snapshot_dump(snap, dump_csv_fwrite, f);
  829. fclose(f);
  830. return true;
  831. }
  832. static void dump_csv_gzwrite(void *data, struct dstr *buffer)
  833. {
  834. gzwrite(data, buffer->array, (unsigned)buffer->len);
  835. }
  836. bool profiler_snapshot_dump_csv_gz(const profiler_snapshot_t *snap,
  837. const char *filename)
  838. {
  839. gzFile gz;
  840. #ifdef _WIN32
  841. wchar_t *filename_w = NULL;
  842. os_utf8_to_wcs_ptr(filename, 0, &filename_w);
  843. if (!filename_w)
  844. return false;
  845. gz = gzopen_w(filename_w, "wb");
  846. bfree(filename_w);
  847. #else
  848. gz = gzopen(filename, "wb");
  849. #endif
  850. if (!gz)
  851. return false;
  852. profiler_snapshot_dump(snap, dump_csv_gzwrite, gz);
  853. #ifdef _WIN32
  854. gzclose_w(gz);
  855. #else
  856. gzclose(gz);
  857. #endif
  858. return true;
  859. }
  860. size_t profiler_snapshot_num_roots(profiler_snapshot_t *snap)
  861. {
  862. return snap ? snap->roots.num : 0;
  863. }
  864. void profiler_snapshot_enumerate_roots(profiler_snapshot_t *snap,
  865. profiler_entry_enum_func func,
  866. void *context)
  867. {
  868. if (!snap)
  869. return;
  870. for (size_t i = 0; i < snap->roots.num; i++)
  871. if (!func(context, &snap->roots.array[i]))
  872. break;
  873. }
  874. void profiler_snapshot_filter_roots(profiler_snapshot_t *snap,
  875. profiler_name_filter_func func, void *data)
  876. {
  877. for (size_t i = 0; i < snap->roots.num;) {
  878. bool remove = false;
  879. bool res = func(data, snap->roots.array[i].name, &remove);
  880. if (remove) {
  881. free_snapshot_entry(&snap->roots.array[i]);
  882. da_erase(snap->roots, i);
  883. }
  884. if (!res)
  885. break;
  886. if (!remove)
  887. i += 1;
  888. }
  889. }
  890. size_t profiler_snapshot_num_children(profiler_snapshot_entry_t *entry)
  891. {
  892. return entry ? entry->children.num : 0;
  893. }
  894. void profiler_snapshot_enumerate_children(profiler_snapshot_entry_t *entry,
  895. profiler_entry_enum_func func,
  896. void *context)
  897. {
  898. if (!entry)
  899. return;
  900. for (size_t i = 0; i < entry->children.num; i++)
  901. if (!func(context, &entry->children.array[i]))
  902. break;
  903. }
  904. const char *profiler_snapshot_entry_name(profiler_snapshot_entry_t *entry)
  905. {
  906. return entry ? entry->name : NULL;
  907. }
  908. profiler_time_entries_t *
  909. profiler_snapshot_entry_times(profiler_snapshot_entry_t *entry)
  910. {
  911. return entry ? &entry->times : NULL;
  912. }
  913. uint64_t profiler_snapshot_entry_overall_count(profiler_snapshot_entry_t *entry)
  914. {
  915. return entry ? entry->overall_count : 0;
  916. }
  917. uint64_t profiler_snapshot_entry_min_time(profiler_snapshot_entry_t *entry)
  918. {
  919. return entry ? entry->min_time : 0;
  920. }
  921. uint64_t profiler_snapshot_entry_max_time(profiler_snapshot_entry_t *entry)
  922. {
  923. return entry ? entry->max_time : 0;
  924. }
  925. profiler_time_entries_t *
  926. profiler_snapshot_entry_times_between_calls(profiler_snapshot_entry_t *entry)
  927. {
  928. return entry ? &entry->times_between_calls : NULL;
  929. }
  930. uint64_t profiler_snapshot_entry_expected_time_between_calls(
  931. profiler_snapshot_entry_t *entry)
  932. {
  933. return entry ? entry->expected_time_between_calls : 0;
  934. }
  935. uint64_t
  936. profiler_snapshot_entry_min_time_between_calls(profiler_snapshot_entry_t *entry)
  937. {
  938. return entry ? entry->min_time_between_calls : 0;
  939. }
  940. uint64_t
  941. profiler_snapshot_entry_max_time_between_calls(profiler_snapshot_entry_t *entry)
  942. {
  943. return entry ? entry->max_time_between_calls : 0;
  944. }
  945. uint64_t profiler_snapshot_entry_overall_between_calls_count(
  946. profiler_snapshot_entry_t *entry)
  947. {
  948. return entry ? entry->overall_between_calls_count : 0;
  949. }