profiler.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #pragma once
  2. #include "base.h"
  3. #include "darray.h"
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. typedef struct profiler_snapshot profiler_snapshot_t;
  8. typedef struct profiler_snapshot_entry profiler_snapshot_entry_t;
  9. typedef struct profiler_time_entry profiler_time_entry_t;
  10. /* ------------------------------------------------------------------------- */
  11. /* Profiling */
  12. EXPORT void profile_register_root(const char *name,
  13. uint64_t expected_time_between_calls);
  14. EXPORT void profile_start(const char *name);
  15. EXPORT void profile_end(const char *name);
  16. EXPORT void profile_reenable_thread(void);
  17. /* ------------------------------------------------------------------------- */
  18. /* Profiler control */
  19. EXPORT void profiler_start(void);
  20. EXPORT void profiler_stop(void);
  21. EXPORT void profiler_print(profiler_snapshot_t *snap);
  22. EXPORT void profiler_print_time_between_calls(profiler_snapshot_t *snap);
  23. EXPORT void profiler_free(void);
  24. /* ------------------------------------------------------------------------- */
  25. /* Profiler name storage */
  26. typedef struct profiler_name_store profiler_name_store_t;
  27. EXPORT profiler_name_store_t *profiler_name_store_create(void);
  28. EXPORT void profiler_name_store_free(profiler_name_store_t *store);
  29. #ifndef _MSC_VER
  30. #define PRINTFATTR(f, a) __attribute__((__format__(__printf__, f, a)))
  31. #else
  32. #define PRINTFATTR(f, a)
  33. #endif
  34. PRINTFATTR(2, 3)
  35. EXPORT const char *profile_store_name(profiler_name_store_t *store,
  36. const char *format, ...);
  37. #undef PRINTFATTR
  38. /* ------------------------------------------------------------------------- */
  39. /* Profiler data access */
  40. struct profiler_time_entry {
  41. uint64_t time_delta;
  42. uint64_t count;
  43. };
  44. typedef DARRAY(profiler_time_entry_t) profiler_time_entries_t;
  45. typedef bool (*profiler_entry_enum_func)(void *context,
  46. profiler_snapshot_entry_t *entry);
  47. EXPORT profiler_snapshot_t *profile_snapshot_create(void);
  48. EXPORT void profile_snapshot_free(profiler_snapshot_t *snap);
  49. EXPORT bool profiler_snapshot_dump_csv(const profiler_snapshot_t *snap,
  50. const char *filename);
  51. EXPORT bool profiler_snapshot_dump_csv_gz(const profiler_snapshot_t *snap,
  52. const char *filename);
  53. EXPORT size_t profiler_snapshot_num_roots(profiler_snapshot_t *snap);
  54. EXPORT void profiler_snapshot_enumerate_roots(profiler_snapshot_t *snap,
  55. profiler_entry_enum_func func,
  56. void *context);
  57. typedef bool (*profiler_name_filter_func)(void *data, const char *name,
  58. bool *remove);
  59. EXPORT void profiler_snapshot_filter_roots(profiler_snapshot_t *snap,
  60. profiler_name_filter_func func,
  61. void *data);
  62. EXPORT size_t profiler_snapshot_num_children(profiler_snapshot_entry_t *entry);
  63. EXPORT void
  64. profiler_snapshot_enumerate_children(profiler_snapshot_entry_t *entry,
  65. profiler_entry_enum_func func,
  66. void *context);
  67. EXPORT const char *
  68. profiler_snapshot_entry_name(profiler_snapshot_entry_t *entry);
  69. EXPORT profiler_time_entries_t *
  70. profiler_snapshot_entry_times(profiler_snapshot_entry_t *entry);
  71. EXPORT uint64_t
  72. profiler_snapshot_entry_min_time(profiler_snapshot_entry_t *entry);
  73. EXPORT uint64_t
  74. profiler_snapshot_entry_max_time(profiler_snapshot_entry_t *entry);
  75. EXPORT uint64_t
  76. profiler_snapshot_entry_overall_count(profiler_snapshot_entry_t *entry);
  77. EXPORT profiler_time_entries_t *
  78. profiler_snapshot_entry_times_between_calls(profiler_snapshot_entry_t *entry);
  79. EXPORT uint64_t profiler_snapshot_entry_expected_time_between_calls(
  80. profiler_snapshot_entry_t *entry);
  81. EXPORT uint64_t profiler_snapshot_entry_min_time_between_calls(
  82. profiler_snapshot_entry_t *entry);
  83. EXPORT uint64_t profiler_snapshot_entry_max_time_between_calls(
  84. profiler_snapshot_entry_t *entry);
  85. EXPORT uint64_t profiler_snapshot_entry_overall_between_calls_count(
  86. profiler_snapshot_entry_t *entry);
  87. #ifdef __cplusplus
  88. }
  89. #endif