xref: /openbmc/qemu/util/qsp.c (revision fe656e31)
1fe9959a2SEmilio G. Cota /*
2fe9959a2SEmilio G. Cota  * qsp.c - QEMU Synchronization Profiler
3fe9959a2SEmilio G. Cota  *
4fe9959a2SEmilio G. Cota  * Copyright (C) 2018, Emilio G. Cota <cota@braap.org>
5fe9959a2SEmilio G. Cota  *
6fe9959a2SEmilio G. Cota  * License: GNU GPL, version 2 or later.
7fe9959a2SEmilio G. Cota  *   See the COPYING file in the top-level directory.
8fe9959a2SEmilio G. Cota  *
9fe9959a2SEmilio G. Cota  * QSP profiles the time spent in synchronization primitives, which can
10fe9959a2SEmilio G. Cota  * help diagnose performance problems, e.g. scalability issues when
11fe9959a2SEmilio G. Cota  * contention is high.
12fe9959a2SEmilio G. Cota  *
13fe9959a2SEmilio G. Cota  * The primitives currently supported are mutexes, recursive mutexes and
14fe9959a2SEmilio G. Cota  * condition variables. Note that not all related functions are intercepted;
15fe9959a2SEmilio G. Cota  * instead we profile only those functions that can have a performance impact,
16fe9959a2SEmilio G. Cota  * either due to blocking (e.g. cond_wait, mutex_lock) or cache line
17fe9959a2SEmilio G. Cota  * contention (e.g. mutex_lock, mutex_trylock).
18fe9959a2SEmilio G. Cota  *
19fe9959a2SEmilio G. Cota  * QSP's design focuses on speed and scalability. This is achieved
20fe9959a2SEmilio G. Cota  * by having threads do their profiling entirely on thread-local data.
21fe9959a2SEmilio G. Cota  * The appropriate thread-local data is found via a QHT, i.e. a concurrent hash
22fe9959a2SEmilio G. Cota  * table. To aggregate data in order to generate a report, we iterate over
23fe9959a2SEmilio G. Cota  * all entries in the hash table. Depending on the number of threads and
24fe9959a2SEmilio G. Cota  * synchronization objects this might be expensive, but note that it is
25fe9959a2SEmilio G. Cota  * very rarely called -- reports are generated only when requested by users.
26fe9959a2SEmilio G. Cota  *
27fe9959a2SEmilio G. Cota  * Reports are generated as a table where each row represents a call site. A
28fe9959a2SEmilio G. Cota  * call site is the triplet formed by the __file__ and __LINE__ of the caller
29fe9959a2SEmilio G. Cota  * as well as the address of the "object" (i.e. mutex, rec. mutex or condvar)
30d557de4aSEmilio G. Cota  * being operated on. Optionally, call sites that operate on different objects
31d557de4aSEmilio G. Cota  * of the same type can be coalesced, which can be particularly useful when
32d557de4aSEmilio G. Cota  * profiling dynamically-allocated objects.
33fe9959a2SEmilio G. Cota  *
34fe9959a2SEmilio G. Cota  * Alternative designs considered:
35fe9959a2SEmilio G. Cota  *
36fe9959a2SEmilio G. Cota  * - Use an off-the-shelf profiler such as mutrace. This is not a viable option
37fe9959a2SEmilio G. Cota  *   for us because QEMU has __malloc_hook set (by one of the libraries it
38fe9959a2SEmilio G. Cota  *   uses); leaving this hook unset is required to avoid deadlock in mutrace.
39fe9959a2SEmilio G. Cota  *
40fe9959a2SEmilio G. Cota  * - Use a glib HT for each thread, protecting each HT with its own lock.
41fe9959a2SEmilio G. Cota  *   This isn't simpler than the current design, and is 10% slower in the
42fe9959a2SEmilio G. Cota  *   atomic_add-bench microbenchmark (-m option).
43fe9959a2SEmilio G. Cota  *
44fe9959a2SEmilio G. Cota  * - For reports, just use a binary tree as we aggregate data, instead of having
45fe9959a2SEmilio G. Cota  *   an intermediate hash table. This would simplify the code only slightly, but
46fe9959a2SEmilio G. Cota  *   would perform badly if there were many threads and objects to track.
47fe9959a2SEmilio G. Cota  *
48996e8d9aSEmilio G. Cota  * - Wrap operations on qsp entries with RCU read-side critical sections, so
49996e8d9aSEmilio G. Cota  *   that qsp_reset() can delete entries. Unfortunately, the overhead of calling
50996e8d9aSEmilio G. Cota  *   rcu_read_lock/unlock slows down atomic_add-bench -m by 24%. Having
51996e8d9aSEmilio G. Cota  *   a snapshot that is updated on qsp_reset() avoids this overhead.
52996e8d9aSEmilio G. Cota  *
53fe9959a2SEmilio G. Cota  * Related Work:
54fe9959a2SEmilio G. Cota  * - Lennart Poettering's mutrace: http://0pointer.de/blog/projects/mutrace.html
55fe9959a2SEmilio G. Cota  * - Lozi, David, Thomas, Lawall and Muller. "Remote Core Locking: Migrating
56fe9959a2SEmilio G. Cota  *   Critical-Section Execution to Improve the Performance of Multithreaded
57fe9959a2SEmilio G. Cota  *   Applications", USENIX ATC'12.
58fe9959a2SEmilio G. Cota  */
59fe9959a2SEmilio G. Cota #include "qemu/osdep.h"
60fe9959a2SEmilio G. Cota #include "qemu/thread.h"
61fe9959a2SEmilio G. Cota #include "qemu/timer.h"
62fe9959a2SEmilio G. Cota #include "qemu/qht.h"
63996e8d9aSEmilio G. Cota #include "qemu/rcu.h"
64*fe656e31SEmilio G. Cota #include "qemu/xxhash.h"
65fe9959a2SEmilio G. Cota 
66fe9959a2SEmilio G. Cota enum QSPType {
67fe9959a2SEmilio G. Cota     QSP_MUTEX,
68cb764d06SEmilio G. Cota     QSP_BQL_MUTEX,
69fe9959a2SEmilio G. Cota     QSP_REC_MUTEX,
70fe9959a2SEmilio G. Cota     QSP_CONDVAR,
71fe9959a2SEmilio G. Cota };
72fe9959a2SEmilio G. Cota 
73fe9959a2SEmilio G. Cota struct QSPCallSite {
74fe9959a2SEmilio G. Cota     const void *obj;
75fe9959a2SEmilio G. Cota     const char *file; /* i.e. __FILE__; shortened later */
76fe9959a2SEmilio G. Cota     int line;
77fe9959a2SEmilio G. Cota     enum QSPType type;
78fe9959a2SEmilio G. Cota };
79fe9959a2SEmilio G. Cota typedef struct QSPCallSite QSPCallSite;
80fe9959a2SEmilio G. Cota 
81fe9959a2SEmilio G. Cota struct QSPEntry {
82fe9959a2SEmilio G. Cota     void *thread_ptr;
83fe9959a2SEmilio G. Cota     const QSPCallSite *callsite;
84fe9959a2SEmilio G. Cota     uint64_t n_acqs;
85fe9959a2SEmilio G. Cota     uint64_t ns;
86d557de4aSEmilio G. Cota     unsigned int n_objs; /* count of coalesced objs; only used for reporting */
87fe9959a2SEmilio G. Cota };
88fe9959a2SEmilio G. Cota typedef struct QSPEntry QSPEntry;
89fe9959a2SEmilio G. Cota 
90996e8d9aSEmilio G. Cota struct QSPSnapshot {
91996e8d9aSEmilio G. Cota     struct rcu_head rcu;
92996e8d9aSEmilio G. Cota     struct qht ht;
93996e8d9aSEmilio G. Cota };
94996e8d9aSEmilio G. Cota typedef struct QSPSnapshot QSPSnapshot;
95996e8d9aSEmilio G. Cota 
96fe9959a2SEmilio G. Cota /* initial sizing for hash tables */
97fe9959a2SEmilio G. Cota #define QSP_INITIAL_SIZE 64
98fe9959a2SEmilio G. Cota 
99fe9959a2SEmilio G. Cota /* If this file is moved, QSP_REL_PATH should be updated accordingly */
100fe9959a2SEmilio G. Cota #define QSP_REL_PATH "util/qsp.c"
101fe9959a2SEmilio G. Cota 
102fe9959a2SEmilio G. Cota /* this file's full path. Used to present all call sites with relative paths */
103fe9959a2SEmilio G. Cota static size_t qsp_qemu_path_len;
104fe9959a2SEmilio G. Cota 
105fe9959a2SEmilio G. Cota /* the address of qsp_thread gives us a unique 'thread ID' */
106fe9959a2SEmilio G. Cota static __thread int qsp_thread;
107fe9959a2SEmilio G. Cota 
108fe9959a2SEmilio G. Cota /*
109fe9959a2SEmilio G. Cota  * Call sites are the same for all threads, so we track them in a separate hash
110fe9959a2SEmilio G. Cota  * table to save memory.
111fe9959a2SEmilio G. Cota  */
112fe9959a2SEmilio G. Cota static struct qht qsp_callsite_ht;
113fe9959a2SEmilio G. Cota 
114fe9959a2SEmilio G. Cota static struct qht qsp_ht;
115996e8d9aSEmilio G. Cota static QSPSnapshot *qsp_snapshot;
116fe9959a2SEmilio G. Cota static bool qsp_initialized, qsp_initializing;
117fe9959a2SEmilio G. Cota 
118fe9959a2SEmilio G. Cota static const char * const qsp_typenames[] = {
119fe9959a2SEmilio G. Cota     [QSP_MUTEX]     = "mutex",
120cb764d06SEmilio G. Cota     [QSP_BQL_MUTEX] = "BQL mutex",
121fe9959a2SEmilio G. Cota     [QSP_REC_MUTEX] = "rec_mutex",
122fe9959a2SEmilio G. Cota     [QSP_CONDVAR]   = "condvar",
123fe9959a2SEmilio G. Cota };
124fe9959a2SEmilio G. Cota 
125cb764d06SEmilio G. Cota QemuMutexLockFunc qemu_bql_mutex_lock_func = qemu_mutex_lock_impl;
126fe9959a2SEmilio G. Cota QemuMutexLockFunc qemu_mutex_lock_func = qemu_mutex_lock_impl;
127fe9959a2SEmilio G. Cota QemuMutexTrylockFunc qemu_mutex_trylock_func = qemu_mutex_trylock_impl;
128fe9959a2SEmilio G. Cota QemuRecMutexLockFunc qemu_rec_mutex_lock_func = qemu_rec_mutex_lock_impl;
129fe9959a2SEmilio G. Cota QemuRecMutexTrylockFunc qemu_rec_mutex_trylock_func =
130fe9959a2SEmilio G. Cota     qemu_rec_mutex_trylock_impl;
131fe9959a2SEmilio G. Cota QemuCondWaitFunc qemu_cond_wait_func = qemu_cond_wait_impl;
132fe9959a2SEmilio G. Cota 
133fe9959a2SEmilio G. Cota /*
134fe9959a2SEmilio G. Cota  * It pays off to _not_ hash callsite->file; hashing a string is slow, and
135fe9959a2SEmilio G. Cota  * without it we still get a pretty unique hash.
136fe9959a2SEmilio G. Cota  */
137fe9959a2SEmilio G. Cota static inline
138c971d8faSEmilio G. Cota uint32_t do_qsp_callsite_hash(const QSPCallSite *callsite, uint64_t ab)
139fe9959a2SEmilio G. Cota {
140c971d8faSEmilio G. Cota     uint64_t cd = (uint64_t)(uintptr_t)callsite->obj;
141fe9959a2SEmilio G. Cota     uint32_t e = callsite->line;
142fe9959a2SEmilio G. Cota     uint32_t f = callsite->type;
143fe9959a2SEmilio G. Cota 
144c971d8faSEmilio G. Cota     return qemu_xxhash6(ab, cd, e, f);
145fe9959a2SEmilio G. Cota }
146fe9959a2SEmilio G. Cota 
147fe9959a2SEmilio G. Cota static inline
148fe9959a2SEmilio G. Cota uint32_t qsp_callsite_hash(const QSPCallSite *callsite)
149fe9959a2SEmilio G. Cota {
150fe9959a2SEmilio G. Cota     return do_qsp_callsite_hash(callsite, 0);
151fe9959a2SEmilio G. Cota }
152fe9959a2SEmilio G. Cota 
153fe9959a2SEmilio G. Cota static inline uint32_t do_qsp_entry_hash(const QSPEntry *entry, uint64_t a)
154fe9959a2SEmilio G. Cota {
155fe9959a2SEmilio G. Cota     return do_qsp_callsite_hash(entry->callsite, a);
156fe9959a2SEmilio G. Cota }
157fe9959a2SEmilio G. Cota 
158fe9959a2SEmilio G. Cota static uint32_t qsp_entry_hash(const QSPEntry *entry)
159fe9959a2SEmilio G. Cota {
160fe9959a2SEmilio G. Cota     return do_qsp_entry_hash(entry, (uint64_t)(uintptr_t)entry->thread_ptr);
161fe9959a2SEmilio G. Cota }
162fe9959a2SEmilio G. Cota 
163fe9959a2SEmilio G. Cota static uint32_t qsp_entry_no_thread_hash(const QSPEntry *entry)
164fe9959a2SEmilio G. Cota {
165fe9959a2SEmilio G. Cota     return do_qsp_entry_hash(entry, 0);
166fe9959a2SEmilio G. Cota }
167fe9959a2SEmilio G. Cota 
168d557de4aSEmilio G. Cota /* without the objects we need to hash the file name to get a decent hash */
169d557de4aSEmilio G. Cota static uint32_t qsp_entry_no_thread_obj_hash(const QSPEntry *entry)
170d557de4aSEmilio G. Cota {
171d557de4aSEmilio G. Cota     const QSPCallSite *callsite = entry->callsite;
172c971d8faSEmilio G. Cota     uint64_t ab = g_str_hash(callsite->file);
173c971d8faSEmilio G. Cota     uint64_t cd = callsite->line;
174d557de4aSEmilio G. Cota     uint32_t e = callsite->type;
175d557de4aSEmilio G. Cota 
176c971d8faSEmilio G. Cota     return qemu_xxhash5(ab, cd, e);
177d557de4aSEmilio G. Cota }
178d557de4aSEmilio G. Cota 
179fe9959a2SEmilio G. Cota static bool qsp_callsite_cmp(const void *ap, const void *bp)
180fe9959a2SEmilio G. Cota {
181fe9959a2SEmilio G. Cota     const QSPCallSite *a = ap;
182fe9959a2SEmilio G. Cota     const QSPCallSite *b = bp;
183fe9959a2SEmilio G. Cota 
184fe9959a2SEmilio G. Cota     return a == b ||
185fe9959a2SEmilio G. Cota         (a->obj == b->obj &&
186fe9959a2SEmilio G. Cota          a->line == b->line &&
187fe9959a2SEmilio G. Cota          a->type == b->type &&
188fe9959a2SEmilio G. Cota          (a->file == b->file || !strcmp(a->file, b->file)));
189fe9959a2SEmilio G. Cota }
190fe9959a2SEmilio G. Cota 
191d557de4aSEmilio G. Cota static bool qsp_callsite_no_obj_cmp(const void *ap, const void *bp)
192d557de4aSEmilio G. Cota {
193d557de4aSEmilio G. Cota     const QSPCallSite *a = ap;
194d557de4aSEmilio G. Cota     const QSPCallSite *b = bp;
195d557de4aSEmilio G. Cota 
196d557de4aSEmilio G. Cota     return a == b ||
197d557de4aSEmilio G. Cota         (a->line == b->line &&
198d557de4aSEmilio G. Cota          a->type == b->type &&
199d557de4aSEmilio G. Cota          (a->file == b->file || !strcmp(a->file, b->file)));
200d557de4aSEmilio G. Cota }
201d557de4aSEmilio G. Cota 
202fe9959a2SEmilio G. Cota static bool qsp_entry_no_thread_cmp(const void *ap, const void *bp)
203fe9959a2SEmilio G. Cota {
204fe9959a2SEmilio G. Cota     const QSPEntry *a = ap;
205fe9959a2SEmilio G. Cota     const QSPEntry *b = bp;
206fe9959a2SEmilio G. Cota 
207fe9959a2SEmilio G. Cota     return qsp_callsite_cmp(a->callsite, b->callsite);
208fe9959a2SEmilio G. Cota }
209fe9959a2SEmilio G. Cota 
210d557de4aSEmilio G. Cota static bool qsp_entry_no_thread_obj_cmp(const void *ap, const void *bp)
211d557de4aSEmilio G. Cota {
212d557de4aSEmilio G. Cota     const QSPEntry *a = ap;
213d557de4aSEmilio G. Cota     const QSPEntry *b = bp;
214d557de4aSEmilio G. Cota 
215d557de4aSEmilio G. Cota     return qsp_callsite_no_obj_cmp(a->callsite, b->callsite);
216d557de4aSEmilio G. Cota }
217d557de4aSEmilio G. Cota 
218fe9959a2SEmilio G. Cota static bool qsp_entry_cmp(const void *ap, const void *bp)
219fe9959a2SEmilio G. Cota {
220fe9959a2SEmilio G. Cota     const QSPEntry *a = ap;
221fe9959a2SEmilio G. Cota     const QSPEntry *b = bp;
222fe9959a2SEmilio G. Cota 
223fe9959a2SEmilio G. Cota     return a->thread_ptr == b->thread_ptr &&
224fe9959a2SEmilio G. Cota         qsp_callsite_cmp(a->callsite, b->callsite);
225fe9959a2SEmilio G. Cota }
226fe9959a2SEmilio G. Cota 
227fe9959a2SEmilio G. Cota /*
228fe9959a2SEmilio G. Cota  * Normally we'd call this from a constructor function, but we want it to work
229fe9959a2SEmilio G. Cota  * via libutil as well.
230fe9959a2SEmilio G. Cota  */
231fe9959a2SEmilio G. Cota static void qsp_do_init(void)
232fe9959a2SEmilio G. Cota {
233fe9959a2SEmilio G. Cota     /* make sure this file's path in the tree is up to date with QSP_REL_PATH */
234fe9959a2SEmilio G. Cota     g_assert(strstr(__FILE__, QSP_REL_PATH));
235fe9959a2SEmilio G. Cota     qsp_qemu_path_len = strlen(__FILE__) - strlen(QSP_REL_PATH);
236fe9959a2SEmilio G. Cota 
237fe9959a2SEmilio G. Cota     qht_init(&qsp_ht, qsp_entry_cmp, QSP_INITIAL_SIZE,
238fe9959a2SEmilio G. Cota              QHT_MODE_AUTO_RESIZE | QHT_MODE_RAW_MUTEXES);
239fe9959a2SEmilio G. Cota     qht_init(&qsp_callsite_ht, qsp_callsite_cmp, QSP_INITIAL_SIZE,
240fe9959a2SEmilio G. Cota              QHT_MODE_AUTO_RESIZE | QHT_MODE_RAW_MUTEXES);
241fe9959a2SEmilio G. Cota }
242fe9959a2SEmilio G. Cota 
243fe9959a2SEmilio G. Cota static __attribute__((noinline)) void qsp_init__slowpath(void)
244fe9959a2SEmilio G. Cota {
245fe9959a2SEmilio G. Cota     if (atomic_cmpxchg(&qsp_initializing, false, true) == false) {
246fe9959a2SEmilio G. Cota         qsp_do_init();
247fe9959a2SEmilio G. Cota         atomic_set(&qsp_initialized, true);
248fe9959a2SEmilio G. Cota     } else {
249fe9959a2SEmilio G. Cota         while (!atomic_read(&qsp_initialized)) {
250fe9959a2SEmilio G. Cota             cpu_relax();
251fe9959a2SEmilio G. Cota         }
252fe9959a2SEmilio G. Cota     }
253fe9959a2SEmilio G. Cota }
254fe9959a2SEmilio G. Cota 
255fe9959a2SEmilio G. Cota /* qsp_init() must be called from _all_ exported functions */
256fe9959a2SEmilio G. Cota static inline void qsp_init(void)
257fe9959a2SEmilio G. Cota {
258fe9959a2SEmilio G. Cota     if (likely(atomic_read(&qsp_initialized))) {
259fe9959a2SEmilio G. Cota         return;
260fe9959a2SEmilio G. Cota     }
261fe9959a2SEmilio G. Cota     qsp_init__slowpath();
262fe9959a2SEmilio G. Cota }
263fe9959a2SEmilio G. Cota 
264fe9959a2SEmilio G. Cota static QSPCallSite *qsp_callsite_find(const QSPCallSite *orig)
265fe9959a2SEmilio G. Cota {
266fe9959a2SEmilio G. Cota     QSPCallSite *callsite;
267fe9959a2SEmilio G. Cota     uint32_t hash;
268fe9959a2SEmilio G. Cota 
269fe9959a2SEmilio G. Cota     hash = qsp_callsite_hash(orig);
270fe9959a2SEmilio G. Cota     callsite = qht_lookup(&qsp_callsite_ht, orig, hash);
271fe9959a2SEmilio G. Cota     if (callsite == NULL) {
272fe9959a2SEmilio G. Cota         void *existing = NULL;
273fe9959a2SEmilio G. Cota 
274fe9959a2SEmilio G. Cota         callsite = g_new(QSPCallSite, 1);
275fe9959a2SEmilio G. Cota         memcpy(callsite, orig, sizeof(*callsite));
276fe9959a2SEmilio G. Cota         qht_insert(&qsp_callsite_ht, callsite, hash, &existing);
277fe9959a2SEmilio G. Cota         if (unlikely(existing)) {
278fe9959a2SEmilio G. Cota             g_free(callsite);
279fe9959a2SEmilio G. Cota             callsite = existing;
280fe9959a2SEmilio G. Cota         }
281fe9959a2SEmilio G. Cota     }
282fe9959a2SEmilio G. Cota     return callsite;
283fe9959a2SEmilio G. Cota }
284fe9959a2SEmilio G. Cota 
285fe9959a2SEmilio G. Cota static QSPEntry *
286fe9959a2SEmilio G. Cota qsp_entry_create(struct qht *ht, const QSPEntry *entry, uint32_t hash)
287fe9959a2SEmilio G. Cota {
288fe9959a2SEmilio G. Cota     QSPEntry *e;
289fe9959a2SEmilio G. Cota     void *existing = NULL;
290fe9959a2SEmilio G. Cota 
291fe9959a2SEmilio G. Cota     e = g_new0(QSPEntry, 1);
292fe9959a2SEmilio G. Cota     e->thread_ptr = entry->thread_ptr;
293fe9959a2SEmilio G. Cota     e->callsite = qsp_callsite_find(entry->callsite);
294fe9959a2SEmilio G. Cota 
295fe9959a2SEmilio G. Cota     qht_insert(ht, e, hash, &existing);
296fe9959a2SEmilio G. Cota     if (unlikely(existing)) {
297fe9959a2SEmilio G. Cota         g_free(e);
298fe9959a2SEmilio G. Cota         e = existing;
299fe9959a2SEmilio G. Cota     }
300fe9959a2SEmilio G. Cota     return e;
301fe9959a2SEmilio G. Cota }
302fe9959a2SEmilio G. Cota 
303fe9959a2SEmilio G. Cota static QSPEntry *
304fe9959a2SEmilio G. Cota qsp_entry_find(struct qht *ht, const QSPEntry *entry, uint32_t hash)
305fe9959a2SEmilio G. Cota {
306fe9959a2SEmilio G. Cota     QSPEntry *e;
307fe9959a2SEmilio G. Cota 
308fe9959a2SEmilio G. Cota     e = qht_lookup(ht, entry, hash);
309fe9959a2SEmilio G. Cota     if (e == NULL) {
310fe9959a2SEmilio G. Cota         e = qsp_entry_create(ht, entry, hash);
311fe9959a2SEmilio G. Cota     }
312fe9959a2SEmilio G. Cota     return e;
313fe9959a2SEmilio G. Cota }
314fe9959a2SEmilio G. Cota 
315fe9959a2SEmilio G. Cota /*
316fe9959a2SEmilio G. Cota  * Note: Entries are never removed, so callers do not have to be in an RCU
317fe9959a2SEmilio G. Cota  * read-side critical section.
318fe9959a2SEmilio G. Cota  */
319fe9959a2SEmilio G. Cota static QSPEntry *qsp_entry_get(const void *obj, const char *file, int line,
320fe9959a2SEmilio G. Cota                                enum QSPType type)
321fe9959a2SEmilio G. Cota {
322fe9959a2SEmilio G. Cota     QSPCallSite callsite = {
323fe9959a2SEmilio G. Cota         .obj = obj,
324fe9959a2SEmilio G. Cota         .file = file,
325fe9959a2SEmilio G. Cota         .line = line,
326fe9959a2SEmilio G. Cota         .type = type,
327fe9959a2SEmilio G. Cota     };
328fe9959a2SEmilio G. Cota     QSPEntry orig;
329fe9959a2SEmilio G. Cota     uint32_t hash;
330fe9959a2SEmilio G. Cota 
331fe9959a2SEmilio G. Cota     qsp_init();
332fe9959a2SEmilio G. Cota 
333fe9959a2SEmilio G. Cota     orig.thread_ptr = &qsp_thread;
334fe9959a2SEmilio G. Cota     orig.callsite = &callsite;
335fe9959a2SEmilio G. Cota 
336fe9959a2SEmilio G. Cota     hash = qsp_entry_hash(&orig);
337fe9959a2SEmilio G. Cota     return qsp_entry_find(&qsp_ht, &orig, hash);
338fe9959a2SEmilio G. Cota }
339fe9959a2SEmilio G. Cota 
340fe9959a2SEmilio G. Cota /*
341fe9959a2SEmilio G. Cota  * @e is in the global hash table; it is only written to by the current thread,
342fe9959a2SEmilio G. Cota  * so we write to it atomically (as in "write once") to prevent torn reads.
343fe9959a2SEmilio G. Cota  */
344fe9959a2SEmilio G. Cota static inline void do_qsp_entry_record(QSPEntry *e, int64_t delta, bool acq)
345fe9959a2SEmilio G. Cota {
346ac8c7748SEmilio G. Cota     atomic_set_u64(&e->ns, e->ns + delta);
347fe9959a2SEmilio G. Cota     if (acq) {
348ac8c7748SEmilio G. Cota         atomic_set_u64(&e->n_acqs, e->n_acqs + 1);
349fe9959a2SEmilio G. Cota     }
350fe9959a2SEmilio G. Cota }
351fe9959a2SEmilio G. Cota 
352fe9959a2SEmilio G. Cota static inline void qsp_entry_record(QSPEntry *e, int64_t delta)
353fe9959a2SEmilio G. Cota {
354fe9959a2SEmilio G. Cota     do_qsp_entry_record(e, delta, true);
355fe9959a2SEmilio G. Cota }
356fe9959a2SEmilio G. Cota 
357fe9959a2SEmilio G. Cota #define QSP_GEN_VOID(type_, qsp_t_, func_, impl_)                       \
358fe9959a2SEmilio G. Cota     static void func_(type_ *obj, const char *file, int line)           \
359fe9959a2SEmilio G. Cota     {                                                                   \
360fe9959a2SEmilio G. Cota         QSPEntry *e;                                                    \
361fe9959a2SEmilio G. Cota         int64_t t0, t1;                                                 \
362fe9959a2SEmilio G. Cota                                                                         \
363fe9959a2SEmilio G. Cota         t0 = get_clock();                                               \
364fe9959a2SEmilio G. Cota         impl_(obj, file, line);                                         \
365fe9959a2SEmilio G. Cota         t1 = get_clock();                                               \
366fe9959a2SEmilio G. Cota                                                                         \
367fe9959a2SEmilio G. Cota         e = qsp_entry_get(obj, file, line, qsp_t_);                     \
368fe9959a2SEmilio G. Cota         qsp_entry_record(e, t1 - t0);                                   \
369fe9959a2SEmilio G. Cota     }
370fe9959a2SEmilio G. Cota 
371fe9959a2SEmilio G. Cota #define QSP_GEN_RET1(type_, qsp_t_, func_, impl_)                       \
372fe9959a2SEmilio G. Cota     static int func_(type_ *obj, const char *file, int line)            \
373fe9959a2SEmilio G. Cota     {                                                                   \
374fe9959a2SEmilio G. Cota         QSPEntry *e;                                                    \
375fe9959a2SEmilio G. Cota         int64_t t0, t1;                                                 \
376fe9959a2SEmilio G. Cota         int err;                                                        \
377fe9959a2SEmilio G. Cota                                                                         \
378fe9959a2SEmilio G. Cota         t0 = get_clock();                                               \
379fe9959a2SEmilio G. Cota         err = impl_(obj, file, line);                                   \
380fe9959a2SEmilio G. Cota         t1 = get_clock();                                               \
381fe9959a2SEmilio G. Cota                                                                         \
382fe9959a2SEmilio G. Cota         e = qsp_entry_get(obj, file, line, qsp_t_);                     \
383fe9959a2SEmilio G. Cota         do_qsp_entry_record(e, t1 - t0, !err);                          \
384fe9959a2SEmilio G. Cota         return err;                                                     \
385fe9959a2SEmilio G. Cota     }
386fe9959a2SEmilio G. Cota 
387cb764d06SEmilio G. Cota QSP_GEN_VOID(QemuMutex, QSP_BQL_MUTEX, qsp_bql_mutex_lock, qemu_mutex_lock_impl)
388fe9959a2SEmilio G. Cota QSP_GEN_VOID(QemuMutex, QSP_MUTEX, qsp_mutex_lock, qemu_mutex_lock_impl)
389fe9959a2SEmilio G. Cota QSP_GEN_RET1(QemuMutex, QSP_MUTEX, qsp_mutex_trylock, qemu_mutex_trylock_impl)
390fe9959a2SEmilio G. Cota 
391fe9959a2SEmilio G. Cota QSP_GEN_VOID(QemuRecMutex, QSP_REC_MUTEX, qsp_rec_mutex_lock,
392fe9959a2SEmilio G. Cota              qemu_rec_mutex_lock_impl)
393fe9959a2SEmilio G. Cota QSP_GEN_RET1(QemuRecMutex, QSP_REC_MUTEX, qsp_rec_mutex_trylock,
394fe9959a2SEmilio G. Cota              qemu_rec_mutex_trylock_impl)
395fe9959a2SEmilio G. Cota 
396fe9959a2SEmilio G. Cota #undef QSP_GEN_RET1
397fe9959a2SEmilio G. Cota #undef QSP_GEN_VOID
398fe9959a2SEmilio G. Cota 
399fe9959a2SEmilio G. Cota static void
400fe9959a2SEmilio G. Cota qsp_cond_wait(QemuCond *cond, QemuMutex *mutex, const char *file, int line)
401fe9959a2SEmilio G. Cota {
402fe9959a2SEmilio G. Cota     QSPEntry *e;
403fe9959a2SEmilio G. Cota     int64_t t0, t1;
404fe9959a2SEmilio G. Cota 
405fe9959a2SEmilio G. Cota     t0 = get_clock();
406fe9959a2SEmilio G. Cota     qemu_cond_wait_impl(cond, mutex, file, line);
407fe9959a2SEmilio G. Cota     t1 = get_clock();
408fe9959a2SEmilio G. Cota 
409fe9959a2SEmilio G. Cota     e = qsp_entry_get(cond, file, line, QSP_CONDVAR);
410fe9959a2SEmilio G. Cota     qsp_entry_record(e, t1 - t0);
411fe9959a2SEmilio G. Cota }
412fe9959a2SEmilio G. Cota 
413fe9959a2SEmilio G. Cota bool qsp_is_enabled(void)
414fe9959a2SEmilio G. Cota {
415fe9959a2SEmilio G. Cota     return atomic_read(&qemu_mutex_lock_func) == qsp_mutex_lock;
416fe9959a2SEmilio G. Cota }
417fe9959a2SEmilio G. Cota 
418fe9959a2SEmilio G. Cota void qsp_enable(void)
419fe9959a2SEmilio G. Cota {
420fe9959a2SEmilio G. Cota     atomic_set(&qemu_mutex_lock_func, qsp_mutex_lock);
421fe9959a2SEmilio G. Cota     atomic_set(&qemu_mutex_trylock_func, qsp_mutex_trylock);
422cb764d06SEmilio G. Cota     atomic_set(&qemu_bql_mutex_lock_func, qsp_bql_mutex_lock);
423fe9959a2SEmilio G. Cota     atomic_set(&qemu_rec_mutex_lock_func, qsp_rec_mutex_lock);
424fe9959a2SEmilio G. Cota     atomic_set(&qemu_rec_mutex_trylock_func, qsp_rec_mutex_trylock);
425fe9959a2SEmilio G. Cota     atomic_set(&qemu_cond_wait_func, qsp_cond_wait);
426fe9959a2SEmilio G. Cota }
427fe9959a2SEmilio G. Cota 
428fe9959a2SEmilio G. Cota void qsp_disable(void)
429fe9959a2SEmilio G. Cota {
430fe9959a2SEmilio G. Cota     atomic_set(&qemu_mutex_lock_func, qemu_mutex_lock_impl);
431fe9959a2SEmilio G. Cota     atomic_set(&qemu_mutex_trylock_func, qemu_mutex_trylock_impl);
432cb764d06SEmilio G. Cota     atomic_set(&qemu_bql_mutex_lock_func, qemu_mutex_lock_impl);
433fe9959a2SEmilio G. Cota     atomic_set(&qemu_rec_mutex_lock_func, qemu_rec_mutex_lock_impl);
434fe9959a2SEmilio G. Cota     atomic_set(&qemu_rec_mutex_trylock_func, qemu_rec_mutex_trylock_impl);
435fe9959a2SEmilio G. Cota     atomic_set(&qemu_cond_wait_func, qemu_cond_wait_impl);
436fe9959a2SEmilio G. Cota }
437fe9959a2SEmilio G. Cota 
438fe9959a2SEmilio G. Cota static gint qsp_tree_cmp(gconstpointer ap, gconstpointer bp, gpointer up)
439fe9959a2SEmilio G. Cota {
440fe9959a2SEmilio G. Cota     const QSPEntry *a = ap;
441fe9959a2SEmilio G. Cota     const QSPEntry *b = bp;
4420a22777cSEmilio G. Cota     enum QSPSortBy sort_by = *(enum QSPSortBy *)up;
443fe9959a2SEmilio G. Cota     const QSPCallSite *ca;
444fe9959a2SEmilio G. Cota     const QSPCallSite *cb;
445fe9959a2SEmilio G. Cota 
4460a22777cSEmilio G. Cota     switch (sort_by) {
4470a22777cSEmilio G. Cota     case QSP_SORT_BY_TOTAL_WAIT_TIME:
448fe9959a2SEmilio G. Cota         if (a->ns > b->ns) {
449fe9959a2SEmilio G. Cota             return -1;
450fe9959a2SEmilio G. Cota         } else if (a->ns < b->ns) {
451fe9959a2SEmilio G. Cota             return 1;
452fe9959a2SEmilio G. Cota         }
4530a22777cSEmilio G. Cota         break;
4540a22777cSEmilio G. Cota     case QSP_SORT_BY_AVG_WAIT_TIME:
4550a22777cSEmilio G. Cota     {
4560a22777cSEmilio G. Cota         double avg_a = a->n_acqs ? a->ns / a->n_acqs : 0;
4570a22777cSEmilio G. Cota         double avg_b = b->n_acqs ? b->ns / b->n_acqs : 0;
4580a22777cSEmilio G. Cota 
4590a22777cSEmilio G. Cota         if (avg_a > avg_b) {
4600a22777cSEmilio G. Cota             return -1;
4610a22777cSEmilio G. Cota         } else if (avg_a < avg_b) {
4620a22777cSEmilio G. Cota             return 1;
4630a22777cSEmilio G. Cota         }
4640a22777cSEmilio G. Cota         break;
4650a22777cSEmilio G. Cota     }
4660a22777cSEmilio G. Cota     default:
4670a22777cSEmilio G. Cota         g_assert_not_reached();
4680a22777cSEmilio G. Cota     }
4690a22777cSEmilio G. Cota 
470fe9959a2SEmilio G. Cota     ca = a->callsite;
471fe9959a2SEmilio G. Cota     cb = b->callsite;
472fe9959a2SEmilio G. Cota     /* Break the tie with the object's address */
473fe9959a2SEmilio G. Cota     if (ca->obj < cb->obj) {
474fe9959a2SEmilio G. Cota         return -1;
475fe9959a2SEmilio G. Cota     } else if (ca->obj > cb->obj) {
476fe9959a2SEmilio G. Cota         return 1;
477fe9959a2SEmilio G. Cota     } else {
478fe9959a2SEmilio G. Cota         int cmp;
479fe9959a2SEmilio G. Cota 
480fe9959a2SEmilio G. Cota         /* same obj. Break the tie with the callsite's file */
481fe9959a2SEmilio G. Cota         cmp = strcmp(ca->file, cb->file);
482fe9959a2SEmilio G. Cota         if (cmp) {
483fe9959a2SEmilio G. Cota             return cmp;
484fe9959a2SEmilio G. Cota         }
485fe9959a2SEmilio G. Cota         /* same callsite file. Break the tie with the callsite's line */
486fe9959a2SEmilio G. Cota         g_assert(ca->line != cb->line);
487fe9959a2SEmilio G. Cota         if (ca->line < cb->line) {
488fe9959a2SEmilio G. Cota             return -1;
489fe9959a2SEmilio G. Cota         } else if (ca->line > cb->line) {
490fe9959a2SEmilio G. Cota             return 1;
491fe9959a2SEmilio G. Cota         } else {
492fe9959a2SEmilio G. Cota             /* break the tie with the callsite's type */
493fe9959a2SEmilio G. Cota             return cb->type - ca->type;
494fe9959a2SEmilio G. Cota         }
495fe9959a2SEmilio G. Cota     }
496fe9959a2SEmilio G. Cota }
497fe9959a2SEmilio G. Cota 
49878255ba2SEmilio G. Cota static void qsp_sort(void *p, uint32_t h, void *userp)
499fe9959a2SEmilio G. Cota {
500fe9959a2SEmilio G. Cota     QSPEntry *e = p;
501fe9959a2SEmilio G. Cota     GTree *tree = userp;
502fe9959a2SEmilio G. Cota 
503fe9959a2SEmilio G. Cota     g_tree_insert(tree, e, NULL);
504fe9959a2SEmilio G. Cota }
505fe9959a2SEmilio G. Cota 
50678255ba2SEmilio G. Cota static void qsp_aggregate(void *p, uint32_t h, void *up)
507fe9959a2SEmilio G. Cota {
508fe9959a2SEmilio G. Cota     struct qht *ht = up;
509fe9959a2SEmilio G. Cota     const QSPEntry *e = p;
510fe9959a2SEmilio G. Cota     QSPEntry *agg;
511fe9959a2SEmilio G. Cota     uint32_t hash;
512fe9959a2SEmilio G. Cota 
513fe9959a2SEmilio G. Cota     hash = qsp_entry_no_thread_hash(e);
514fe9959a2SEmilio G. Cota     agg = qsp_entry_find(ht, e, hash);
515ac8c7748SEmilio G. Cota     /*
516ac8c7748SEmilio G. Cota      * The entry is in the global hash table; read from it atomically (as in
517ac8c7748SEmilio G. Cota      * "read once").
518ac8c7748SEmilio G. Cota      */
519ac8c7748SEmilio G. Cota     agg->ns += atomic_read_u64(&e->ns);
520ac8c7748SEmilio G. Cota     agg->n_acqs += atomic_read_u64(&e->n_acqs);
521fe9959a2SEmilio G. Cota }
522fe9959a2SEmilio G. Cota 
52378255ba2SEmilio G. Cota static void qsp_iter_diff(void *p, uint32_t hash, void *htp)
524996e8d9aSEmilio G. Cota {
525996e8d9aSEmilio G. Cota     struct qht *ht = htp;
526996e8d9aSEmilio G. Cota     QSPEntry *old = p;
527996e8d9aSEmilio G. Cota     QSPEntry *new;
528996e8d9aSEmilio G. Cota 
529996e8d9aSEmilio G. Cota     new = qht_lookup(ht, old, hash);
530996e8d9aSEmilio G. Cota     /* entries are never deleted, so we must have this one */
531996e8d9aSEmilio G. Cota     g_assert(new != NULL);
532996e8d9aSEmilio G. Cota     /* our reading of the stats happened after the snapshot was taken */
533996e8d9aSEmilio G. Cota     g_assert(new->n_acqs >= old->n_acqs);
534996e8d9aSEmilio G. Cota     g_assert(new->ns >= old->ns);
535996e8d9aSEmilio G. Cota 
536996e8d9aSEmilio G. Cota     new->n_acqs -= old->n_acqs;
537996e8d9aSEmilio G. Cota     new->ns -= old->ns;
538996e8d9aSEmilio G. Cota 
539996e8d9aSEmilio G. Cota     /* No point in reporting an empty entry */
540996e8d9aSEmilio G. Cota     if (new->n_acqs == 0 && new->ns == 0) {
541996e8d9aSEmilio G. Cota         bool removed = qht_remove(ht, new, hash);
542996e8d9aSEmilio G. Cota 
543996e8d9aSEmilio G. Cota         g_assert(removed);
544996e8d9aSEmilio G. Cota         g_free(new);
545996e8d9aSEmilio G. Cota     }
546996e8d9aSEmilio G. Cota }
547996e8d9aSEmilio G. Cota 
548996e8d9aSEmilio G. Cota static void qsp_diff(struct qht *orig, struct qht *new)
549996e8d9aSEmilio G. Cota {
550996e8d9aSEmilio G. Cota     qht_iter(orig, qsp_iter_diff, new);
551996e8d9aSEmilio G. Cota }
552996e8d9aSEmilio G. Cota 
55378255ba2SEmilio G. Cota static void qsp_iter_callsite_coalesce(void *p, uint32_t h, void *htp)
554d557de4aSEmilio G. Cota {
555d557de4aSEmilio G. Cota     struct qht *ht = htp;
556d557de4aSEmilio G. Cota     QSPEntry *old = p;
557d557de4aSEmilio G. Cota     QSPEntry *e;
558d557de4aSEmilio G. Cota     uint32_t hash;
559d557de4aSEmilio G. Cota 
560d557de4aSEmilio G. Cota     hash = qsp_entry_no_thread_obj_hash(old);
561d557de4aSEmilio G. Cota     e = qht_lookup(ht, old, hash);
562d557de4aSEmilio G. Cota     if (e == NULL) {
563d557de4aSEmilio G. Cota         e = qsp_entry_create(ht, old, hash);
564d557de4aSEmilio G. Cota         e->n_objs = 1;
565d557de4aSEmilio G. Cota     } else if (e->callsite->obj != old->callsite->obj) {
566d557de4aSEmilio G. Cota         e->n_objs++;
567d557de4aSEmilio G. Cota     }
568d557de4aSEmilio G. Cota     e->ns += old->ns;
569d557de4aSEmilio G. Cota     e->n_acqs += old->n_acqs;
570d557de4aSEmilio G. Cota }
571d557de4aSEmilio G. Cota 
57278255ba2SEmilio G. Cota static void qsp_ht_delete(void *p, uint32_t h, void *htp)
573996e8d9aSEmilio G. Cota {
574996e8d9aSEmilio G. Cota     g_free(p);
575996e8d9aSEmilio G. Cota }
576996e8d9aSEmilio G. Cota 
577d557de4aSEmilio G. Cota static void qsp_mktree(GTree *tree, bool callsite_coalesce)
578fe9959a2SEmilio G. Cota {
579996e8d9aSEmilio G. Cota     QSPSnapshot *snap;
580d557de4aSEmilio G. Cota     struct qht ht, coalesce_ht;
581d557de4aSEmilio G. Cota     struct qht *htp;
582fe9959a2SEmilio G. Cota 
583996e8d9aSEmilio G. Cota     /*
584996e8d9aSEmilio G. Cota      * First, see if there's a prior snapshot, so that we read the global hash
585996e8d9aSEmilio G. Cota      * table _after_ the snapshot has been created, which guarantees that
586996e8d9aSEmilio G. Cota      * the entries we'll read will be a superset of the snapshot's entries.
587996e8d9aSEmilio G. Cota      *
588996e8d9aSEmilio G. Cota      * We must remain in an RCU read-side critical section until we're done
589996e8d9aSEmilio G. Cota      * with the snapshot.
590996e8d9aSEmilio G. Cota      */
591996e8d9aSEmilio G. Cota     rcu_read_lock();
592996e8d9aSEmilio G. Cota     snap = atomic_rcu_read(&qsp_snapshot);
593996e8d9aSEmilio G. Cota 
594fe9959a2SEmilio G. Cota     /* Aggregate all results from the global hash table into a local one */
595fe9959a2SEmilio G. Cota     qht_init(&ht, qsp_entry_no_thread_cmp, QSP_INITIAL_SIZE,
596fe9959a2SEmilio G. Cota              QHT_MODE_AUTO_RESIZE | QHT_MODE_RAW_MUTEXES);
597fe9959a2SEmilio G. Cota     qht_iter(&qsp_ht, qsp_aggregate, &ht);
598fe9959a2SEmilio G. Cota 
599996e8d9aSEmilio G. Cota     /* compute the difference wrt the snapshot, if any */
600996e8d9aSEmilio G. Cota     if (snap) {
601996e8d9aSEmilio G. Cota         qsp_diff(&snap->ht, &ht);
602996e8d9aSEmilio G. Cota     }
603996e8d9aSEmilio G. Cota     /* done with the snapshot; RCU can reclaim it */
604996e8d9aSEmilio G. Cota     rcu_read_unlock();
605996e8d9aSEmilio G. Cota 
606d557de4aSEmilio G. Cota     htp = &ht;
607d557de4aSEmilio G. Cota     if (callsite_coalesce) {
608d557de4aSEmilio G. Cota         qht_init(&coalesce_ht, qsp_entry_no_thread_obj_cmp, QSP_INITIAL_SIZE,
609d557de4aSEmilio G. Cota                  QHT_MODE_AUTO_RESIZE | QHT_MODE_RAW_MUTEXES);
610d557de4aSEmilio G. Cota         qht_iter(&ht, qsp_iter_callsite_coalesce, &coalesce_ht);
611d557de4aSEmilio G. Cota 
612d557de4aSEmilio G. Cota         /* free the previous hash table, and point htp to coalesce_ht */
613d557de4aSEmilio G. Cota         qht_iter(&ht, qsp_ht_delete, NULL);
614d557de4aSEmilio G. Cota         qht_destroy(&ht);
615d557de4aSEmilio G. Cota         htp = &coalesce_ht;
616d557de4aSEmilio G. Cota     }
617d557de4aSEmilio G. Cota 
618fe9959a2SEmilio G. Cota     /* sort the hash table elements by using a tree */
619d557de4aSEmilio G. Cota     qht_iter(htp, qsp_sort, tree);
620fe9959a2SEmilio G. Cota 
621fe9959a2SEmilio G. Cota     /* free the hash table, but keep the elements (those are in the tree now) */
622d557de4aSEmilio G. Cota     qht_destroy(htp);
623fe9959a2SEmilio G. Cota }
624fe9959a2SEmilio G. Cota 
625fe9959a2SEmilio G. Cota /* free string with g_free */
626fe9959a2SEmilio G. Cota static char *qsp_at(const QSPCallSite *callsite)
627fe9959a2SEmilio G. Cota {
628fe9959a2SEmilio G. Cota     GString *s = g_string_new(NULL);
629fe9959a2SEmilio G. Cota     const char *shortened;
630fe9959a2SEmilio G. Cota 
631fe9959a2SEmilio G. Cota     /* remove the absolute path to qemu */
632fe9959a2SEmilio G. Cota     if (unlikely(strlen(callsite->file) < qsp_qemu_path_len)) {
633fe9959a2SEmilio G. Cota         shortened = callsite->file;
634fe9959a2SEmilio G. Cota     } else {
635fe9959a2SEmilio G. Cota         shortened = callsite->file + qsp_qemu_path_len;
636fe9959a2SEmilio G. Cota     }
637fe9959a2SEmilio G. Cota     g_string_append_printf(s, "%s:%u", shortened, callsite->line);
638fe9959a2SEmilio G. Cota     return g_string_free(s, FALSE);
639fe9959a2SEmilio G. Cota }
640fe9959a2SEmilio G. Cota 
641fe9959a2SEmilio G. Cota struct QSPReportEntry {
642fe9959a2SEmilio G. Cota     const void *obj;
643fe9959a2SEmilio G. Cota     char *callsite_at;
644fe9959a2SEmilio G. Cota     const char *typename;
645fe9959a2SEmilio G. Cota     double time_s;
646fe9959a2SEmilio G. Cota     double ns_avg;
647fe9959a2SEmilio G. Cota     uint64_t n_acqs;
648d557de4aSEmilio G. Cota     unsigned int n_objs;
649fe9959a2SEmilio G. Cota };
650fe9959a2SEmilio G. Cota typedef struct QSPReportEntry QSPReportEntry;
651fe9959a2SEmilio G. Cota 
652fe9959a2SEmilio G. Cota struct QSPReport {
653fe9959a2SEmilio G. Cota     QSPReportEntry *entries;
654fe9959a2SEmilio G. Cota     size_t n_entries;
655fe9959a2SEmilio G. Cota     size_t max_n_entries;
656fe9959a2SEmilio G. Cota };
657fe9959a2SEmilio G. Cota typedef struct QSPReport QSPReport;
658fe9959a2SEmilio G. Cota 
659fe9959a2SEmilio G. Cota static gboolean qsp_tree_report(gpointer key, gpointer value, gpointer udata)
660fe9959a2SEmilio G. Cota {
661fe9959a2SEmilio G. Cota     const QSPEntry *e = key;
662fe9959a2SEmilio G. Cota     QSPReport *report = udata;
663fe9959a2SEmilio G. Cota     QSPReportEntry *entry;
664fe9959a2SEmilio G. Cota 
665fe9959a2SEmilio G. Cota     if (report->n_entries == report->max_n_entries) {
666fe9959a2SEmilio G. Cota         return TRUE;
667fe9959a2SEmilio G. Cota     }
668fe9959a2SEmilio G. Cota     entry = &report->entries[report->n_entries];
669fe9959a2SEmilio G. Cota     report->n_entries++;
670fe9959a2SEmilio G. Cota 
671fe9959a2SEmilio G. Cota     entry->obj = e->callsite->obj;
672d557de4aSEmilio G. Cota     entry->n_objs = e->n_objs;
673fe9959a2SEmilio G. Cota     entry->callsite_at = qsp_at(e->callsite);
674fe9959a2SEmilio G. Cota     entry->typename = qsp_typenames[e->callsite->type];
675fe9959a2SEmilio G. Cota     entry->time_s = e->ns * 1e-9;
676fe9959a2SEmilio G. Cota     entry->n_acqs = e->n_acqs;
677fe9959a2SEmilio G. Cota     entry->ns_avg = e->n_acqs ? e->ns / e->n_acqs : 0;
678fe9959a2SEmilio G. Cota     return FALSE;
679fe9959a2SEmilio G. Cota }
680fe9959a2SEmilio G. Cota 
681fe9959a2SEmilio G. Cota static void
682fe9959a2SEmilio G. Cota pr_report(const QSPReport *rep, FILE *f, fprintf_function pr)
683fe9959a2SEmilio G. Cota {
684fe9959a2SEmilio G. Cota     char *dashes;
685fe9959a2SEmilio G. Cota     size_t max_len = 0;
686fe9959a2SEmilio G. Cota     int callsite_len = 0;
687fe9959a2SEmilio G. Cota     int callsite_rspace;
688fe9959a2SEmilio G. Cota     int n_dashes;
689fe9959a2SEmilio G. Cota     size_t i;
690fe9959a2SEmilio G. Cota 
691fe9959a2SEmilio G. Cota     /* find out the maximum length of all 'callsite' fields */
692fe9959a2SEmilio G. Cota     for (i = 0; i < rep->n_entries; i++) {
693fe9959a2SEmilio G. Cota         const QSPReportEntry *e = &rep->entries[i];
694fe9959a2SEmilio G. Cota         size_t len = strlen(e->callsite_at);
695fe9959a2SEmilio G. Cota 
696fe9959a2SEmilio G. Cota         if (len > max_len) {
697fe9959a2SEmilio G. Cota             max_len = len;
698fe9959a2SEmilio G. Cota         }
699fe9959a2SEmilio G. Cota     }
700fe9959a2SEmilio G. Cota 
701fe9959a2SEmilio G. Cota     callsite_len = MAX(max_len, strlen("Call site"));
702fe9959a2SEmilio G. Cota     /* white space to leave to the right of "Call site" */
703fe9959a2SEmilio G. Cota     callsite_rspace = callsite_len - strlen("Call site");
704fe9959a2SEmilio G. Cota 
705fe9959a2SEmilio G. Cota     pr(f, "Type               Object  Call site%*s  Wait Time (s)  "
706fe9959a2SEmilio G. Cota        "       Count  Average (us)\n", callsite_rspace, "");
707fe9959a2SEmilio G. Cota 
708fe9959a2SEmilio G. Cota     /* build a horizontal rule with dashes */
709fe9959a2SEmilio G. Cota     n_dashes = 79 + callsite_rspace;
710fe9959a2SEmilio G. Cota     dashes = g_malloc(n_dashes + 1);
711fe9959a2SEmilio G. Cota     memset(dashes, '-', n_dashes);
712fe9959a2SEmilio G. Cota     dashes[n_dashes] = '\0';
713fe9959a2SEmilio G. Cota     pr(f, "%s\n", dashes);
714fe9959a2SEmilio G. Cota 
715fe9959a2SEmilio G. Cota     for (i = 0; i < rep->n_entries; i++) {
716fe9959a2SEmilio G. Cota         const QSPReportEntry *e = &rep->entries[i];
717d557de4aSEmilio G. Cota         GString *s = g_string_new(NULL);
718fe9959a2SEmilio G. Cota 
719d557de4aSEmilio G. Cota         g_string_append_printf(s, "%-9s  ", e->typename);
720d557de4aSEmilio G. Cota         if (e->n_objs > 1) {
721d557de4aSEmilio G. Cota             g_string_append_printf(s, "[%12u]", e->n_objs);
722d557de4aSEmilio G. Cota         } else {
723d557de4aSEmilio G. Cota             g_string_append_printf(s, "%14p", e->obj);
724d557de4aSEmilio G. Cota         }
725d557de4aSEmilio G. Cota         g_string_append_printf(s, "  %s%*s  %13.5f  %12" PRIu64 "  %12.2f\n",
726d557de4aSEmilio G. Cota                                e->callsite_at,
727d557de4aSEmilio G. Cota                                callsite_len - (int)strlen(e->callsite_at), "",
728d557de4aSEmilio G. Cota                                e->time_s, e->n_acqs, e->ns_avg * 1e-3);
729d557de4aSEmilio G. Cota         pr(f, "%s", s->str);
730d557de4aSEmilio G. Cota         g_string_free(s, TRUE);
731fe9959a2SEmilio G. Cota     }
732fe9959a2SEmilio G. Cota 
733fe9959a2SEmilio G. Cota     pr(f, "%s\n", dashes);
734fe9959a2SEmilio G. Cota     g_free(dashes);
735fe9959a2SEmilio G. Cota }
736fe9959a2SEmilio G. Cota 
737fe9959a2SEmilio G. Cota static void report_destroy(QSPReport *rep)
738fe9959a2SEmilio G. Cota {
739fe9959a2SEmilio G. Cota     size_t i;
740fe9959a2SEmilio G. Cota 
741fe9959a2SEmilio G. Cota     for (i = 0; i < rep->n_entries; i++) {
742fe9959a2SEmilio G. Cota         QSPReportEntry *e = &rep->entries[i];
743fe9959a2SEmilio G. Cota 
744fe9959a2SEmilio G. Cota         g_free(e->callsite_at);
745fe9959a2SEmilio G. Cota     }
746fe9959a2SEmilio G. Cota     g_free(rep->entries);
747fe9959a2SEmilio G. Cota }
748fe9959a2SEmilio G. Cota 
7490a22777cSEmilio G. Cota void qsp_report(FILE *f, fprintf_function cpu_fprintf, size_t max,
750d557de4aSEmilio G. Cota                 enum QSPSortBy sort_by, bool callsite_coalesce)
751fe9959a2SEmilio G. Cota {
7520a22777cSEmilio G. Cota     GTree *tree = g_tree_new_full(qsp_tree_cmp, &sort_by, g_free, NULL);
753fe9959a2SEmilio G. Cota     QSPReport rep;
754fe9959a2SEmilio G. Cota 
755fe9959a2SEmilio G. Cota     qsp_init();
756fe9959a2SEmilio G. Cota 
757fe9959a2SEmilio G. Cota     rep.entries = g_new0(QSPReportEntry, max);
758fe9959a2SEmilio G. Cota     rep.n_entries = 0;
759fe9959a2SEmilio G. Cota     rep.max_n_entries = max;
760fe9959a2SEmilio G. Cota 
761d557de4aSEmilio G. Cota     qsp_mktree(tree, callsite_coalesce);
762fe9959a2SEmilio G. Cota     g_tree_foreach(tree, qsp_tree_report, &rep);
763fe9959a2SEmilio G. Cota     g_tree_destroy(tree);
764fe9959a2SEmilio G. Cota 
765fe9959a2SEmilio G. Cota     pr_report(&rep, f, cpu_fprintf);
766fe9959a2SEmilio G. Cota     report_destroy(&rep);
767fe9959a2SEmilio G. Cota }
768996e8d9aSEmilio G. Cota 
769996e8d9aSEmilio G. Cota static void qsp_snapshot_destroy(QSPSnapshot *snap)
770996e8d9aSEmilio G. Cota {
771996e8d9aSEmilio G. Cota     qht_iter(&snap->ht, qsp_ht_delete, NULL);
772996e8d9aSEmilio G. Cota     qht_destroy(&snap->ht);
773996e8d9aSEmilio G. Cota     g_free(snap);
774996e8d9aSEmilio G. Cota }
775996e8d9aSEmilio G. Cota 
776996e8d9aSEmilio G. Cota void qsp_reset(void)
777996e8d9aSEmilio G. Cota {
778996e8d9aSEmilio G. Cota     QSPSnapshot *new = g_new(QSPSnapshot, 1);
779996e8d9aSEmilio G. Cota     QSPSnapshot *old;
780996e8d9aSEmilio G. Cota 
781996e8d9aSEmilio G. Cota     qsp_init();
782996e8d9aSEmilio G. Cota 
783996e8d9aSEmilio G. Cota     qht_init(&new->ht, qsp_entry_cmp, QSP_INITIAL_SIZE,
784996e8d9aSEmilio G. Cota              QHT_MODE_AUTO_RESIZE | QHT_MODE_RAW_MUTEXES);
785996e8d9aSEmilio G. Cota 
786996e8d9aSEmilio G. Cota     /* take a snapshot of the current state */
787996e8d9aSEmilio G. Cota     qht_iter(&qsp_ht, qsp_aggregate, &new->ht);
788996e8d9aSEmilio G. Cota 
789996e8d9aSEmilio G. Cota     /* replace the previous snapshot, if any */
790996e8d9aSEmilio G. Cota     old = atomic_xchg(&qsp_snapshot, new);
791996e8d9aSEmilio G. Cota     if (old) {
792996e8d9aSEmilio G. Cota         call_rcu(old, qsp_snapshot_destroy, rcu);
793996e8d9aSEmilio G. Cota     }
794996e8d9aSEmilio G. Cota }
795