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