xref: /openbmc/qemu/util/qsp.c (revision ac7ff4cf)
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  */
59*ac7ff4cfSMarkus Armbruster 
60fe9959a2SEmilio G. Cota #include "qemu/osdep.h"
61*ac7ff4cfSMarkus Armbruster #include "qemu/qemu-print.h"
62fe9959a2SEmilio G. Cota #include "qemu/thread.h"
63fe9959a2SEmilio G. Cota #include "qemu/timer.h"
64fe9959a2SEmilio G. Cota #include "qemu/qht.h"
65996e8d9aSEmilio G. Cota #include "qemu/rcu.h"
66fe656e31SEmilio G. Cota #include "qemu/xxhash.h"
67fe9959a2SEmilio G. Cota 
68fe9959a2SEmilio G. Cota enum QSPType {
69fe9959a2SEmilio G. Cota     QSP_MUTEX,
70cb764d06SEmilio G. Cota     QSP_BQL_MUTEX,
71fe9959a2SEmilio G. Cota     QSP_REC_MUTEX,
72fe9959a2SEmilio G. Cota     QSP_CONDVAR,
73fe9959a2SEmilio G. Cota };
74fe9959a2SEmilio G. Cota 
75fe9959a2SEmilio G. Cota struct QSPCallSite {
76fe9959a2SEmilio G. Cota     const void *obj;
77fe9959a2SEmilio G. Cota     const char *file; /* i.e. __FILE__; shortened later */
78fe9959a2SEmilio G. Cota     int line;
79fe9959a2SEmilio G. Cota     enum QSPType type;
80fe9959a2SEmilio G. Cota };
81fe9959a2SEmilio G. Cota typedef struct QSPCallSite QSPCallSite;
82fe9959a2SEmilio G. Cota 
83fe9959a2SEmilio G. Cota struct QSPEntry {
84fe9959a2SEmilio G. Cota     void *thread_ptr;
85fe9959a2SEmilio G. Cota     const QSPCallSite *callsite;
86fe9959a2SEmilio G. Cota     uint64_t n_acqs;
87fe9959a2SEmilio G. Cota     uint64_t ns;
88d557de4aSEmilio G. Cota     unsigned int n_objs; /* count of coalesced objs; only used for reporting */
89fe9959a2SEmilio G. Cota };
90fe9959a2SEmilio G. Cota typedef struct QSPEntry QSPEntry;
91fe9959a2SEmilio G. Cota 
92996e8d9aSEmilio G. Cota struct QSPSnapshot {
93996e8d9aSEmilio G. Cota     struct rcu_head rcu;
94996e8d9aSEmilio G. Cota     struct qht ht;
95996e8d9aSEmilio G. Cota };
96996e8d9aSEmilio G. Cota typedef struct QSPSnapshot QSPSnapshot;
97996e8d9aSEmilio G. Cota 
98fe9959a2SEmilio G. Cota /* initial sizing for hash tables */
99fe9959a2SEmilio G. Cota #define QSP_INITIAL_SIZE 64
100fe9959a2SEmilio G. Cota 
101fe9959a2SEmilio G. Cota /* If this file is moved, QSP_REL_PATH should be updated accordingly */
102fe9959a2SEmilio G. Cota #define QSP_REL_PATH "util/qsp.c"
103fe9959a2SEmilio G. Cota 
104fe9959a2SEmilio G. Cota /* this file's full path. Used to present all call sites with relative paths */
105fe9959a2SEmilio G. Cota static size_t qsp_qemu_path_len;
106fe9959a2SEmilio G. Cota 
107fe9959a2SEmilio G. Cota /* the address of qsp_thread gives us a unique 'thread ID' */
108fe9959a2SEmilio G. Cota static __thread int qsp_thread;
109fe9959a2SEmilio G. Cota 
110fe9959a2SEmilio G. Cota /*
111fe9959a2SEmilio G. Cota  * Call sites are the same for all threads, so we track them in a separate hash
112fe9959a2SEmilio G. Cota  * table to save memory.
113fe9959a2SEmilio G. Cota  */
114fe9959a2SEmilio G. Cota static struct qht qsp_callsite_ht;
115fe9959a2SEmilio G. Cota 
116fe9959a2SEmilio G. Cota static struct qht qsp_ht;
117996e8d9aSEmilio G. Cota static QSPSnapshot *qsp_snapshot;
118fe9959a2SEmilio G. Cota static bool qsp_initialized, qsp_initializing;
119fe9959a2SEmilio G. Cota 
120fe9959a2SEmilio G. Cota static const char * const qsp_typenames[] = {
121fe9959a2SEmilio G. Cota     [QSP_MUTEX]     = "mutex",
122cb764d06SEmilio G. Cota     [QSP_BQL_MUTEX] = "BQL mutex",
123fe9959a2SEmilio G. Cota     [QSP_REC_MUTEX] = "rec_mutex",
124fe9959a2SEmilio G. Cota     [QSP_CONDVAR]   = "condvar",
125fe9959a2SEmilio G. Cota };
126fe9959a2SEmilio G. Cota 
127cb764d06SEmilio G. Cota QemuMutexLockFunc qemu_bql_mutex_lock_func = qemu_mutex_lock_impl;
128fe9959a2SEmilio G. Cota QemuMutexLockFunc qemu_mutex_lock_func = qemu_mutex_lock_impl;
129fe9959a2SEmilio G. Cota QemuMutexTrylockFunc qemu_mutex_trylock_func = qemu_mutex_trylock_impl;
130fe9959a2SEmilio G. Cota QemuRecMutexLockFunc qemu_rec_mutex_lock_func = qemu_rec_mutex_lock_impl;
131fe9959a2SEmilio G. Cota QemuRecMutexTrylockFunc qemu_rec_mutex_trylock_func =
132fe9959a2SEmilio G. Cota     qemu_rec_mutex_trylock_impl;
133fe9959a2SEmilio G. Cota QemuCondWaitFunc qemu_cond_wait_func = qemu_cond_wait_impl;
134fe9959a2SEmilio G. Cota 
135fe9959a2SEmilio G. Cota /*
136fe9959a2SEmilio G. Cota  * It pays off to _not_ hash callsite->file; hashing a string is slow, and
137fe9959a2SEmilio G. Cota  * without it we still get a pretty unique hash.
138fe9959a2SEmilio G. Cota  */
139fe9959a2SEmilio G. Cota static inline
140c971d8faSEmilio G. Cota uint32_t do_qsp_callsite_hash(const QSPCallSite *callsite, uint64_t ab)
141fe9959a2SEmilio G. Cota {
142c971d8faSEmilio G. Cota     uint64_t cd = (uint64_t)(uintptr_t)callsite->obj;
143fe9959a2SEmilio G. Cota     uint32_t e = callsite->line;
144fe9959a2SEmilio G. Cota     uint32_t f = callsite->type;
145fe9959a2SEmilio G. Cota 
146c971d8faSEmilio G. Cota     return qemu_xxhash6(ab, cd, e, f);
147fe9959a2SEmilio G. Cota }
148fe9959a2SEmilio G. Cota 
149fe9959a2SEmilio G. Cota static inline
150fe9959a2SEmilio G. Cota uint32_t qsp_callsite_hash(const QSPCallSite *callsite)
151fe9959a2SEmilio G. Cota {
152fe9959a2SEmilio G. Cota     return do_qsp_callsite_hash(callsite, 0);
153fe9959a2SEmilio G. Cota }
154fe9959a2SEmilio G. Cota 
155fe9959a2SEmilio G. Cota static inline uint32_t do_qsp_entry_hash(const QSPEntry *entry, uint64_t a)
156fe9959a2SEmilio G. Cota {
157fe9959a2SEmilio G. Cota     return do_qsp_callsite_hash(entry->callsite, a);
158fe9959a2SEmilio G. Cota }
159fe9959a2SEmilio G. Cota 
160fe9959a2SEmilio G. Cota static uint32_t qsp_entry_hash(const QSPEntry *entry)
161fe9959a2SEmilio G. Cota {
162fe9959a2SEmilio G. Cota     return do_qsp_entry_hash(entry, (uint64_t)(uintptr_t)entry->thread_ptr);
163fe9959a2SEmilio G. Cota }
164fe9959a2SEmilio G. Cota 
165fe9959a2SEmilio G. Cota static uint32_t qsp_entry_no_thread_hash(const QSPEntry *entry)
166fe9959a2SEmilio G. Cota {
167fe9959a2SEmilio G. Cota     return do_qsp_entry_hash(entry, 0);
168fe9959a2SEmilio G. Cota }
169fe9959a2SEmilio G. Cota 
170d557de4aSEmilio G. Cota /* without the objects we need to hash the file name to get a decent hash */
171d557de4aSEmilio G. Cota static uint32_t qsp_entry_no_thread_obj_hash(const QSPEntry *entry)
172d557de4aSEmilio G. Cota {
173d557de4aSEmilio G. Cota     const QSPCallSite *callsite = entry->callsite;
174c971d8faSEmilio G. Cota     uint64_t ab = g_str_hash(callsite->file);
175c971d8faSEmilio G. Cota     uint64_t cd = callsite->line;
176d557de4aSEmilio G. Cota     uint32_t e = callsite->type;
177d557de4aSEmilio G. Cota 
178c971d8faSEmilio G. Cota     return qemu_xxhash5(ab, cd, e);
179d557de4aSEmilio G. Cota }
180d557de4aSEmilio G. Cota 
181fe9959a2SEmilio G. Cota static bool qsp_callsite_cmp(const void *ap, const void *bp)
182fe9959a2SEmilio G. Cota {
183fe9959a2SEmilio G. Cota     const QSPCallSite *a = ap;
184fe9959a2SEmilio G. Cota     const QSPCallSite *b = bp;
185fe9959a2SEmilio G. Cota 
186fe9959a2SEmilio G. Cota     return a == b ||
187fe9959a2SEmilio G. Cota         (a->obj == b->obj &&
188fe9959a2SEmilio G. Cota          a->line == b->line &&
189fe9959a2SEmilio G. Cota          a->type == b->type &&
190fe9959a2SEmilio G. Cota          (a->file == b->file || !strcmp(a->file, b->file)));
191fe9959a2SEmilio G. Cota }
192fe9959a2SEmilio G. Cota 
193d557de4aSEmilio G. Cota static bool qsp_callsite_no_obj_cmp(const void *ap, const void *bp)
194d557de4aSEmilio G. Cota {
195d557de4aSEmilio G. Cota     const QSPCallSite *a = ap;
196d557de4aSEmilio G. Cota     const QSPCallSite *b = bp;
197d557de4aSEmilio G. Cota 
198d557de4aSEmilio G. Cota     return a == b ||
199d557de4aSEmilio G. Cota         (a->line == b->line &&
200d557de4aSEmilio G. Cota          a->type == b->type &&
201d557de4aSEmilio G. Cota          (a->file == b->file || !strcmp(a->file, b->file)));
202d557de4aSEmilio G. Cota }
203d557de4aSEmilio G. Cota 
204fe9959a2SEmilio G. Cota static bool qsp_entry_no_thread_cmp(const void *ap, const void *bp)
205fe9959a2SEmilio G. Cota {
206fe9959a2SEmilio G. Cota     const QSPEntry *a = ap;
207fe9959a2SEmilio G. Cota     const QSPEntry *b = bp;
208fe9959a2SEmilio G. Cota 
209fe9959a2SEmilio G. Cota     return qsp_callsite_cmp(a->callsite, b->callsite);
210fe9959a2SEmilio G. Cota }
211fe9959a2SEmilio G. Cota 
212d557de4aSEmilio G. Cota static bool qsp_entry_no_thread_obj_cmp(const void *ap, const void *bp)
213d557de4aSEmilio G. Cota {
214d557de4aSEmilio G. Cota     const QSPEntry *a = ap;
215d557de4aSEmilio G. Cota     const QSPEntry *b = bp;
216d557de4aSEmilio G. Cota 
217d557de4aSEmilio G. Cota     return qsp_callsite_no_obj_cmp(a->callsite, b->callsite);
218d557de4aSEmilio G. Cota }
219d557de4aSEmilio G. Cota 
220fe9959a2SEmilio G. Cota static bool qsp_entry_cmp(const void *ap, const void *bp)
221fe9959a2SEmilio G. Cota {
222fe9959a2SEmilio G. Cota     const QSPEntry *a = ap;
223fe9959a2SEmilio G. Cota     const QSPEntry *b = bp;
224fe9959a2SEmilio G. Cota 
225fe9959a2SEmilio G. Cota     return a->thread_ptr == b->thread_ptr &&
226fe9959a2SEmilio G. Cota         qsp_callsite_cmp(a->callsite, b->callsite);
227fe9959a2SEmilio G. Cota }
228fe9959a2SEmilio G. Cota 
229fe9959a2SEmilio G. Cota /*
230fe9959a2SEmilio G. Cota  * Normally we'd call this from a constructor function, but we want it to work
231fe9959a2SEmilio G. Cota  * via libutil as well.
232fe9959a2SEmilio G. Cota  */
233fe9959a2SEmilio G. Cota static void qsp_do_init(void)
234fe9959a2SEmilio G. Cota {
235fe9959a2SEmilio G. Cota     /* make sure this file's path in the tree is up to date with QSP_REL_PATH */
236fe9959a2SEmilio G. Cota     g_assert(strstr(__FILE__, QSP_REL_PATH));
237fe9959a2SEmilio G. Cota     qsp_qemu_path_len = strlen(__FILE__) - strlen(QSP_REL_PATH);
238fe9959a2SEmilio G. Cota 
239fe9959a2SEmilio G. Cota     qht_init(&qsp_ht, qsp_entry_cmp, QSP_INITIAL_SIZE,
240fe9959a2SEmilio G. Cota              QHT_MODE_AUTO_RESIZE | QHT_MODE_RAW_MUTEXES);
241fe9959a2SEmilio G. Cota     qht_init(&qsp_callsite_ht, qsp_callsite_cmp, QSP_INITIAL_SIZE,
242fe9959a2SEmilio G. Cota              QHT_MODE_AUTO_RESIZE | QHT_MODE_RAW_MUTEXES);
243fe9959a2SEmilio G. Cota }
244fe9959a2SEmilio G. Cota 
245fe9959a2SEmilio G. Cota static __attribute__((noinline)) void qsp_init__slowpath(void)
246fe9959a2SEmilio G. Cota {
247fe9959a2SEmilio G. Cota     if (atomic_cmpxchg(&qsp_initializing, false, true) == false) {
248fe9959a2SEmilio G. Cota         qsp_do_init();
249fe9959a2SEmilio G. Cota         atomic_set(&qsp_initialized, true);
250fe9959a2SEmilio G. Cota     } else {
251fe9959a2SEmilio G. Cota         while (!atomic_read(&qsp_initialized)) {
252fe9959a2SEmilio G. Cota             cpu_relax();
253fe9959a2SEmilio G. Cota         }
254fe9959a2SEmilio G. Cota     }
255fe9959a2SEmilio G. Cota }
256fe9959a2SEmilio G. Cota 
257fe9959a2SEmilio G. Cota /* qsp_init() must be called from _all_ exported functions */
258fe9959a2SEmilio G. Cota static inline void qsp_init(void)
259fe9959a2SEmilio G. Cota {
260fe9959a2SEmilio G. Cota     if (likely(atomic_read(&qsp_initialized))) {
261fe9959a2SEmilio G. Cota         return;
262fe9959a2SEmilio G. Cota     }
263fe9959a2SEmilio G. Cota     qsp_init__slowpath();
264fe9959a2SEmilio G. Cota }
265fe9959a2SEmilio G. Cota 
266fe9959a2SEmilio G. Cota static QSPCallSite *qsp_callsite_find(const QSPCallSite *orig)
267fe9959a2SEmilio G. Cota {
268fe9959a2SEmilio G. Cota     QSPCallSite *callsite;
269fe9959a2SEmilio G. Cota     uint32_t hash;
270fe9959a2SEmilio G. Cota 
271fe9959a2SEmilio G. Cota     hash = qsp_callsite_hash(orig);
272fe9959a2SEmilio G. Cota     callsite = qht_lookup(&qsp_callsite_ht, orig, hash);
273fe9959a2SEmilio G. Cota     if (callsite == NULL) {
274fe9959a2SEmilio G. Cota         void *existing = NULL;
275fe9959a2SEmilio G. Cota 
276fe9959a2SEmilio G. Cota         callsite = g_new(QSPCallSite, 1);
277fe9959a2SEmilio G. Cota         memcpy(callsite, orig, sizeof(*callsite));
278fe9959a2SEmilio G. Cota         qht_insert(&qsp_callsite_ht, callsite, hash, &existing);
279fe9959a2SEmilio G. Cota         if (unlikely(existing)) {
280fe9959a2SEmilio G. Cota             g_free(callsite);
281fe9959a2SEmilio G. Cota             callsite = existing;
282fe9959a2SEmilio G. Cota         }
283fe9959a2SEmilio G. Cota     }
284fe9959a2SEmilio G. Cota     return callsite;
285fe9959a2SEmilio G. Cota }
286fe9959a2SEmilio G. Cota 
287fe9959a2SEmilio G. Cota static QSPEntry *
288fe9959a2SEmilio G. Cota qsp_entry_create(struct qht *ht, const QSPEntry *entry, uint32_t hash)
289fe9959a2SEmilio G. Cota {
290fe9959a2SEmilio G. Cota     QSPEntry *e;
291fe9959a2SEmilio G. Cota     void *existing = NULL;
292fe9959a2SEmilio G. Cota 
293fe9959a2SEmilio G. Cota     e = g_new0(QSPEntry, 1);
294fe9959a2SEmilio G. Cota     e->thread_ptr = entry->thread_ptr;
295fe9959a2SEmilio G. Cota     e->callsite = qsp_callsite_find(entry->callsite);
296fe9959a2SEmilio G. Cota 
297fe9959a2SEmilio G. Cota     qht_insert(ht, e, hash, &existing);
298fe9959a2SEmilio G. Cota     if (unlikely(existing)) {
299fe9959a2SEmilio G. Cota         g_free(e);
300fe9959a2SEmilio G. Cota         e = existing;
301fe9959a2SEmilio G. Cota     }
302fe9959a2SEmilio G. Cota     return e;
303fe9959a2SEmilio G. Cota }
304fe9959a2SEmilio G. Cota 
305fe9959a2SEmilio G. Cota static QSPEntry *
306fe9959a2SEmilio G. Cota qsp_entry_find(struct qht *ht, const QSPEntry *entry, uint32_t hash)
307fe9959a2SEmilio G. Cota {
308fe9959a2SEmilio G. Cota     QSPEntry *e;
309fe9959a2SEmilio G. Cota 
310fe9959a2SEmilio G. Cota     e = qht_lookup(ht, entry, hash);
311fe9959a2SEmilio G. Cota     if (e == NULL) {
312fe9959a2SEmilio G. Cota         e = qsp_entry_create(ht, entry, hash);
313fe9959a2SEmilio G. Cota     }
314fe9959a2SEmilio G. Cota     return e;
315fe9959a2SEmilio G. Cota }
316fe9959a2SEmilio G. Cota 
317fe9959a2SEmilio G. Cota /*
318fe9959a2SEmilio G. Cota  * Note: Entries are never removed, so callers do not have to be in an RCU
319fe9959a2SEmilio G. Cota  * read-side critical section.
320fe9959a2SEmilio G. Cota  */
321fe9959a2SEmilio G. Cota static QSPEntry *qsp_entry_get(const void *obj, const char *file, int line,
322fe9959a2SEmilio G. Cota                                enum QSPType type)
323fe9959a2SEmilio G. Cota {
324fe9959a2SEmilio G. Cota     QSPCallSite callsite = {
325fe9959a2SEmilio G. Cota         .obj = obj,
326fe9959a2SEmilio G. Cota         .file = file,
327fe9959a2SEmilio G. Cota         .line = line,
328fe9959a2SEmilio G. Cota         .type = type,
329fe9959a2SEmilio G. Cota     };
330fe9959a2SEmilio G. Cota     QSPEntry orig;
331fe9959a2SEmilio G. Cota     uint32_t hash;
332fe9959a2SEmilio G. Cota 
333fe9959a2SEmilio G. Cota     qsp_init();
334fe9959a2SEmilio G. Cota 
335fe9959a2SEmilio G. Cota     orig.thread_ptr = &qsp_thread;
336fe9959a2SEmilio G. Cota     orig.callsite = &callsite;
337fe9959a2SEmilio G. Cota 
338fe9959a2SEmilio G. Cota     hash = qsp_entry_hash(&orig);
339fe9959a2SEmilio G. Cota     return qsp_entry_find(&qsp_ht, &orig, hash);
340fe9959a2SEmilio G. Cota }
341fe9959a2SEmilio G. Cota 
342fe9959a2SEmilio G. Cota /*
343fe9959a2SEmilio G. Cota  * @e is in the global hash table; it is only written to by the current thread,
344fe9959a2SEmilio G. Cota  * so we write to it atomically (as in "write once") to prevent torn reads.
345fe9959a2SEmilio G. Cota  */
346fe9959a2SEmilio G. Cota static inline void do_qsp_entry_record(QSPEntry *e, int64_t delta, bool acq)
347fe9959a2SEmilio G. Cota {
348ac8c7748SEmilio G. Cota     atomic_set_u64(&e->ns, e->ns + delta);
349fe9959a2SEmilio G. Cota     if (acq) {
350ac8c7748SEmilio G. Cota         atomic_set_u64(&e->n_acqs, e->n_acqs + 1);
351fe9959a2SEmilio G. Cota     }
352fe9959a2SEmilio G. Cota }
353fe9959a2SEmilio G. Cota 
354fe9959a2SEmilio G. Cota static inline void qsp_entry_record(QSPEntry *e, int64_t delta)
355fe9959a2SEmilio G. Cota {
356fe9959a2SEmilio G. Cota     do_qsp_entry_record(e, delta, true);
357fe9959a2SEmilio G. Cota }
358fe9959a2SEmilio G. Cota 
359fe9959a2SEmilio G. Cota #define QSP_GEN_VOID(type_, qsp_t_, func_, impl_)                       \
360fe9959a2SEmilio G. Cota     static void func_(type_ *obj, const char *file, int line)           \
361fe9959a2SEmilio G. Cota     {                                                                   \
362fe9959a2SEmilio G. Cota         QSPEntry *e;                                                    \
363fe9959a2SEmilio G. Cota         int64_t t0, t1;                                                 \
364fe9959a2SEmilio G. Cota                                                                         \
365fe9959a2SEmilio G. Cota         t0 = get_clock();                                               \
366fe9959a2SEmilio G. Cota         impl_(obj, file, line);                                         \
367fe9959a2SEmilio G. Cota         t1 = get_clock();                                               \
368fe9959a2SEmilio G. Cota                                                                         \
369fe9959a2SEmilio G. Cota         e = qsp_entry_get(obj, file, line, qsp_t_);                     \
370fe9959a2SEmilio G. Cota         qsp_entry_record(e, t1 - t0);                                   \
371fe9959a2SEmilio G. Cota     }
372fe9959a2SEmilio G. Cota 
373fe9959a2SEmilio G. Cota #define QSP_GEN_RET1(type_, qsp_t_, func_, impl_)                       \
374fe9959a2SEmilio G. Cota     static int func_(type_ *obj, const char *file, int line)            \
375fe9959a2SEmilio G. Cota     {                                                                   \
376fe9959a2SEmilio G. Cota         QSPEntry *e;                                                    \
377fe9959a2SEmilio G. Cota         int64_t t0, t1;                                                 \
378fe9959a2SEmilio G. Cota         int err;                                                        \
379fe9959a2SEmilio G. Cota                                                                         \
380fe9959a2SEmilio G. Cota         t0 = get_clock();                                               \
381fe9959a2SEmilio G. Cota         err = impl_(obj, file, line);                                   \
382fe9959a2SEmilio G. Cota         t1 = get_clock();                                               \
383fe9959a2SEmilio G. Cota                                                                         \
384fe9959a2SEmilio G. Cota         e = qsp_entry_get(obj, file, line, qsp_t_);                     \
385fe9959a2SEmilio G. Cota         do_qsp_entry_record(e, t1 - t0, !err);                          \
386fe9959a2SEmilio G. Cota         return err;                                                     \
387fe9959a2SEmilio G. Cota     }
388fe9959a2SEmilio G. Cota 
389cb764d06SEmilio G. Cota QSP_GEN_VOID(QemuMutex, QSP_BQL_MUTEX, qsp_bql_mutex_lock, qemu_mutex_lock_impl)
390fe9959a2SEmilio G. Cota QSP_GEN_VOID(QemuMutex, QSP_MUTEX, qsp_mutex_lock, qemu_mutex_lock_impl)
391fe9959a2SEmilio G. Cota QSP_GEN_RET1(QemuMutex, QSP_MUTEX, qsp_mutex_trylock, qemu_mutex_trylock_impl)
392fe9959a2SEmilio G. Cota 
393fe9959a2SEmilio G. Cota QSP_GEN_VOID(QemuRecMutex, QSP_REC_MUTEX, qsp_rec_mutex_lock,
394fe9959a2SEmilio G. Cota              qemu_rec_mutex_lock_impl)
395fe9959a2SEmilio G. Cota QSP_GEN_RET1(QemuRecMutex, QSP_REC_MUTEX, qsp_rec_mutex_trylock,
396fe9959a2SEmilio G. Cota              qemu_rec_mutex_trylock_impl)
397fe9959a2SEmilio G. Cota 
398fe9959a2SEmilio G. Cota #undef QSP_GEN_RET1
399fe9959a2SEmilio G. Cota #undef QSP_GEN_VOID
400fe9959a2SEmilio G. Cota 
401fe9959a2SEmilio G. Cota static void
402fe9959a2SEmilio G. Cota qsp_cond_wait(QemuCond *cond, QemuMutex *mutex, const char *file, int line)
403fe9959a2SEmilio G. Cota {
404fe9959a2SEmilio G. Cota     QSPEntry *e;
405fe9959a2SEmilio G. Cota     int64_t t0, t1;
406fe9959a2SEmilio G. Cota 
407fe9959a2SEmilio G. Cota     t0 = get_clock();
408fe9959a2SEmilio G. Cota     qemu_cond_wait_impl(cond, mutex, file, line);
409fe9959a2SEmilio G. Cota     t1 = get_clock();
410fe9959a2SEmilio G. Cota 
411fe9959a2SEmilio G. Cota     e = qsp_entry_get(cond, file, line, QSP_CONDVAR);
412fe9959a2SEmilio G. Cota     qsp_entry_record(e, t1 - t0);
413fe9959a2SEmilio G. Cota }
414fe9959a2SEmilio G. Cota 
415fe9959a2SEmilio G. Cota bool qsp_is_enabled(void)
416fe9959a2SEmilio G. Cota {
417fe9959a2SEmilio G. Cota     return atomic_read(&qemu_mutex_lock_func) == qsp_mutex_lock;
418fe9959a2SEmilio G. Cota }
419fe9959a2SEmilio G. Cota 
420fe9959a2SEmilio G. Cota void qsp_enable(void)
421fe9959a2SEmilio G. Cota {
422fe9959a2SEmilio G. Cota     atomic_set(&qemu_mutex_lock_func, qsp_mutex_lock);
423fe9959a2SEmilio G. Cota     atomic_set(&qemu_mutex_trylock_func, qsp_mutex_trylock);
424cb764d06SEmilio G. Cota     atomic_set(&qemu_bql_mutex_lock_func, qsp_bql_mutex_lock);
425fe9959a2SEmilio G. Cota     atomic_set(&qemu_rec_mutex_lock_func, qsp_rec_mutex_lock);
426fe9959a2SEmilio G. Cota     atomic_set(&qemu_rec_mutex_trylock_func, qsp_rec_mutex_trylock);
427fe9959a2SEmilio G. Cota     atomic_set(&qemu_cond_wait_func, qsp_cond_wait);
428fe9959a2SEmilio G. Cota }
429fe9959a2SEmilio G. Cota 
430fe9959a2SEmilio G. Cota void qsp_disable(void)
431fe9959a2SEmilio G. Cota {
432fe9959a2SEmilio G. Cota     atomic_set(&qemu_mutex_lock_func, qemu_mutex_lock_impl);
433fe9959a2SEmilio G. Cota     atomic_set(&qemu_mutex_trylock_func, qemu_mutex_trylock_impl);
434cb764d06SEmilio G. Cota     atomic_set(&qemu_bql_mutex_lock_func, qemu_mutex_lock_impl);
435fe9959a2SEmilio G. Cota     atomic_set(&qemu_rec_mutex_lock_func, qemu_rec_mutex_lock_impl);
436fe9959a2SEmilio G. Cota     atomic_set(&qemu_rec_mutex_trylock_func, qemu_rec_mutex_trylock_impl);
437fe9959a2SEmilio G. Cota     atomic_set(&qemu_cond_wait_func, qemu_cond_wait_impl);
438fe9959a2SEmilio G. Cota }
439fe9959a2SEmilio G. Cota 
440fe9959a2SEmilio G. Cota static gint qsp_tree_cmp(gconstpointer ap, gconstpointer bp, gpointer up)
441fe9959a2SEmilio G. Cota {
442fe9959a2SEmilio G. Cota     const QSPEntry *a = ap;
443fe9959a2SEmilio G. Cota     const QSPEntry *b = bp;
4440a22777cSEmilio G. Cota     enum QSPSortBy sort_by = *(enum QSPSortBy *)up;
445fe9959a2SEmilio G. Cota     const QSPCallSite *ca;
446fe9959a2SEmilio G. Cota     const QSPCallSite *cb;
447fe9959a2SEmilio G. Cota 
4480a22777cSEmilio G. Cota     switch (sort_by) {
4490a22777cSEmilio G. Cota     case QSP_SORT_BY_TOTAL_WAIT_TIME:
450fe9959a2SEmilio G. Cota         if (a->ns > b->ns) {
451fe9959a2SEmilio G. Cota             return -1;
452fe9959a2SEmilio G. Cota         } else if (a->ns < b->ns) {
453fe9959a2SEmilio G. Cota             return 1;
454fe9959a2SEmilio G. Cota         }
4550a22777cSEmilio G. Cota         break;
4560a22777cSEmilio G. Cota     case QSP_SORT_BY_AVG_WAIT_TIME:
4570a22777cSEmilio G. Cota     {
4580a22777cSEmilio G. Cota         double avg_a = a->n_acqs ? a->ns / a->n_acqs : 0;
4590a22777cSEmilio G. Cota         double avg_b = b->n_acqs ? b->ns / b->n_acqs : 0;
4600a22777cSEmilio G. Cota 
4610a22777cSEmilio G. Cota         if (avg_a > avg_b) {
4620a22777cSEmilio G. Cota             return -1;
4630a22777cSEmilio G. Cota         } else if (avg_a < avg_b) {
4640a22777cSEmilio G. Cota             return 1;
4650a22777cSEmilio G. Cota         }
4660a22777cSEmilio G. Cota         break;
4670a22777cSEmilio G. Cota     }
4680a22777cSEmilio G. Cota     default:
4690a22777cSEmilio G. Cota         g_assert_not_reached();
4700a22777cSEmilio G. Cota     }
4710a22777cSEmilio G. Cota 
472fe9959a2SEmilio G. Cota     ca = a->callsite;
473fe9959a2SEmilio G. Cota     cb = b->callsite;
474fe9959a2SEmilio G. Cota     /* Break the tie with the object's address */
475fe9959a2SEmilio G. Cota     if (ca->obj < cb->obj) {
476fe9959a2SEmilio G. Cota         return -1;
477fe9959a2SEmilio G. Cota     } else if (ca->obj > cb->obj) {
478fe9959a2SEmilio G. Cota         return 1;
479fe9959a2SEmilio G. Cota     } else {
480fe9959a2SEmilio G. Cota         int cmp;
481fe9959a2SEmilio G. Cota 
482fe9959a2SEmilio G. Cota         /* same obj. Break the tie with the callsite's file */
483fe9959a2SEmilio G. Cota         cmp = strcmp(ca->file, cb->file);
484fe9959a2SEmilio G. Cota         if (cmp) {
485fe9959a2SEmilio G. Cota             return cmp;
486fe9959a2SEmilio G. Cota         }
487fe9959a2SEmilio G. Cota         /* same callsite file. Break the tie with the callsite's line */
488fe9959a2SEmilio G. Cota         g_assert(ca->line != cb->line);
489fe9959a2SEmilio G. Cota         if (ca->line < cb->line) {
490fe9959a2SEmilio G. Cota             return -1;
491fe9959a2SEmilio G. Cota         } else if (ca->line > cb->line) {
492fe9959a2SEmilio G. Cota             return 1;
493fe9959a2SEmilio G. Cota         } else {
494fe9959a2SEmilio G. Cota             /* break the tie with the callsite's type */
495fe9959a2SEmilio G. Cota             return cb->type - ca->type;
496fe9959a2SEmilio G. Cota         }
497fe9959a2SEmilio G. Cota     }
498fe9959a2SEmilio G. Cota }
499fe9959a2SEmilio G. Cota 
50078255ba2SEmilio G. Cota static void qsp_sort(void *p, uint32_t h, void *userp)
501fe9959a2SEmilio G. Cota {
502fe9959a2SEmilio G. Cota     QSPEntry *e = p;
503fe9959a2SEmilio G. Cota     GTree *tree = userp;
504fe9959a2SEmilio G. Cota 
505fe9959a2SEmilio G. Cota     g_tree_insert(tree, e, NULL);
506fe9959a2SEmilio G. Cota }
507fe9959a2SEmilio G. Cota 
50878255ba2SEmilio G. Cota static void qsp_aggregate(void *p, uint32_t h, void *up)
509fe9959a2SEmilio G. Cota {
510fe9959a2SEmilio G. Cota     struct qht *ht = up;
511fe9959a2SEmilio G. Cota     const QSPEntry *e = p;
512fe9959a2SEmilio G. Cota     QSPEntry *agg;
513fe9959a2SEmilio G. Cota     uint32_t hash;
514fe9959a2SEmilio G. Cota 
515fe9959a2SEmilio G. Cota     hash = qsp_entry_no_thread_hash(e);
516fe9959a2SEmilio G. Cota     agg = qsp_entry_find(ht, e, hash);
517ac8c7748SEmilio G. Cota     /*
518ac8c7748SEmilio G. Cota      * The entry is in the global hash table; read from it atomically (as in
519ac8c7748SEmilio G. Cota      * "read once").
520ac8c7748SEmilio G. Cota      */
521ac8c7748SEmilio G. Cota     agg->ns += atomic_read_u64(&e->ns);
522ac8c7748SEmilio G. Cota     agg->n_acqs += atomic_read_u64(&e->n_acqs);
523fe9959a2SEmilio G. Cota }
524fe9959a2SEmilio G. Cota 
52578255ba2SEmilio G. Cota static void qsp_iter_diff(void *p, uint32_t hash, void *htp)
526996e8d9aSEmilio G. Cota {
527996e8d9aSEmilio G. Cota     struct qht *ht = htp;
528996e8d9aSEmilio G. Cota     QSPEntry *old = p;
529996e8d9aSEmilio G. Cota     QSPEntry *new;
530996e8d9aSEmilio G. Cota 
531996e8d9aSEmilio G. Cota     new = qht_lookup(ht, old, hash);
532996e8d9aSEmilio G. Cota     /* entries are never deleted, so we must have this one */
533996e8d9aSEmilio G. Cota     g_assert(new != NULL);
534996e8d9aSEmilio G. Cota     /* our reading of the stats happened after the snapshot was taken */
535996e8d9aSEmilio G. Cota     g_assert(new->n_acqs >= old->n_acqs);
536996e8d9aSEmilio G. Cota     g_assert(new->ns >= old->ns);
537996e8d9aSEmilio G. Cota 
538996e8d9aSEmilio G. Cota     new->n_acqs -= old->n_acqs;
539996e8d9aSEmilio G. Cota     new->ns -= old->ns;
540996e8d9aSEmilio G. Cota 
541996e8d9aSEmilio G. Cota     /* No point in reporting an empty entry */
542996e8d9aSEmilio G. Cota     if (new->n_acqs == 0 && new->ns == 0) {
543996e8d9aSEmilio G. Cota         bool removed = qht_remove(ht, new, hash);
544996e8d9aSEmilio G. Cota 
545996e8d9aSEmilio G. Cota         g_assert(removed);
546996e8d9aSEmilio G. Cota         g_free(new);
547996e8d9aSEmilio G. Cota     }
548996e8d9aSEmilio G. Cota }
549996e8d9aSEmilio G. Cota 
550996e8d9aSEmilio G. Cota static void qsp_diff(struct qht *orig, struct qht *new)
551996e8d9aSEmilio G. Cota {
552996e8d9aSEmilio G. Cota     qht_iter(orig, qsp_iter_diff, new);
553996e8d9aSEmilio G. Cota }
554996e8d9aSEmilio G. Cota 
55578255ba2SEmilio G. Cota static void qsp_iter_callsite_coalesce(void *p, uint32_t h, void *htp)
556d557de4aSEmilio G. Cota {
557d557de4aSEmilio G. Cota     struct qht *ht = htp;
558d557de4aSEmilio G. Cota     QSPEntry *old = p;
559d557de4aSEmilio G. Cota     QSPEntry *e;
560d557de4aSEmilio G. Cota     uint32_t hash;
561d557de4aSEmilio G. Cota 
562d557de4aSEmilio G. Cota     hash = qsp_entry_no_thread_obj_hash(old);
563d557de4aSEmilio G. Cota     e = qht_lookup(ht, old, hash);
564d557de4aSEmilio G. Cota     if (e == NULL) {
565d557de4aSEmilio G. Cota         e = qsp_entry_create(ht, old, hash);
566d557de4aSEmilio G. Cota         e->n_objs = 1;
567d557de4aSEmilio G. Cota     } else if (e->callsite->obj != old->callsite->obj) {
568d557de4aSEmilio G. Cota         e->n_objs++;
569d557de4aSEmilio G. Cota     }
570d557de4aSEmilio G. Cota     e->ns += old->ns;
571d557de4aSEmilio G. Cota     e->n_acqs += old->n_acqs;
572d557de4aSEmilio G. Cota }
573d557de4aSEmilio G. Cota 
57478255ba2SEmilio G. Cota static void qsp_ht_delete(void *p, uint32_t h, void *htp)
575996e8d9aSEmilio G. Cota {
576996e8d9aSEmilio G. Cota     g_free(p);
577996e8d9aSEmilio G. Cota }
578996e8d9aSEmilio G. Cota 
579d557de4aSEmilio G. Cota static void qsp_mktree(GTree *tree, bool callsite_coalesce)
580fe9959a2SEmilio G. Cota {
581996e8d9aSEmilio G. Cota     QSPSnapshot *snap;
582d557de4aSEmilio G. Cota     struct qht ht, coalesce_ht;
583d557de4aSEmilio G. Cota     struct qht *htp;
584fe9959a2SEmilio G. Cota 
585996e8d9aSEmilio G. Cota     /*
586996e8d9aSEmilio G. Cota      * First, see if there's a prior snapshot, so that we read the global hash
587996e8d9aSEmilio G. Cota      * table _after_ the snapshot has been created, which guarantees that
588996e8d9aSEmilio G. Cota      * the entries we'll read will be a superset of the snapshot's entries.
589996e8d9aSEmilio G. Cota      *
590996e8d9aSEmilio G. Cota      * We must remain in an RCU read-side critical section until we're done
591996e8d9aSEmilio G. Cota      * with the snapshot.
592996e8d9aSEmilio G. Cota      */
593996e8d9aSEmilio G. Cota     rcu_read_lock();
594996e8d9aSEmilio G. Cota     snap = atomic_rcu_read(&qsp_snapshot);
595996e8d9aSEmilio G. Cota 
596fe9959a2SEmilio G. Cota     /* Aggregate all results from the global hash table into a local one */
597fe9959a2SEmilio G. Cota     qht_init(&ht, qsp_entry_no_thread_cmp, QSP_INITIAL_SIZE,
598fe9959a2SEmilio G. Cota              QHT_MODE_AUTO_RESIZE | QHT_MODE_RAW_MUTEXES);
599fe9959a2SEmilio G. Cota     qht_iter(&qsp_ht, qsp_aggregate, &ht);
600fe9959a2SEmilio G. Cota 
601996e8d9aSEmilio G. Cota     /* compute the difference wrt the snapshot, if any */
602996e8d9aSEmilio G. Cota     if (snap) {
603996e8d9aSEmilio G. Cota         qsp_diff(&snap->ht, &ht);
604996e8d9aSEmilio G. Cota     }
605996e8d9aSEmilio G. Cota     /* done with the snapshot; RCU can reclaim it */
606996e8d9aSEmilio G. Cota     rcu_read_unlock();
607996e8d9aSEmilio G. Cota 
608d557de4aSEmilio G. Cota     htp = &ht;
609d557de4aSEmilio G. Cota     if (callsite_coalesce) {
610d557de4aSEmilio G. Cota         qht_init(&coalesce_ht, qsp_entry_no_thread_obj_cmp, QSP_INITIAL_SIZE,
611d557de4aSEmilio G. Cota                  QHT_MODE_AUTO_RESIZE | QHT_MODE_RAW_MUTEXES);
612d557de4aSEmilio G. Cota         qht_iter(&ht, qsp_iter_callsite_coalesce, &coalesce_ht);
613d557de4aSEmilio G. Cota 
614d557de4aSEmilio G. Cota         /* free the previous hash table, and point htp to coalesce_ht */
615d557de4aSEmilio G. Cota         qht_iter(&ht, qsp_ht_delete, NULL);
616d557de4aSEmilio G. Cota         qht_destroy(&ht);
617d557de4aSEmilio G. Cota         htp = &coalesce_ht;
618d557de4aSEmilio G. Cota     }
619d557de4aSEmilio G. Cota 
620fe9959a2SEmilio G. Cota     /* sort the hash table elements by using a tree */
621d557de4aSEmilio G. Cota     qht_iter(htp, qsp_sort, tree);
622fe9959a2SEmilio G. Cota 
623fe9959a2SEmilio G. Cota     /* free the hash table, but keep the elements (those are in the tree now) */
624d557de4aSEmilio G. Cota     qht_destroy(htp);
625fe9959a2SEmilio G. Cota }
626fe9959a2SEmilio G. Cota 
627fe9959a2SEmilio G. Cota /* free string with g_free */
628fe9959a2SEmilio G. Cota static char *qsp_at(const QSPCallSite *callsite)
629fe9959a2SEmilio G. Cota {
630fe9959a2SEmilio G. Cota     GString *s = g_string_new(NULL);
631fe9959a2SEmilio G. Cota     const char *shortened;
632fe9959a2SEmilio G. Cota 
633fe9959a2SEmilio G. Cota     /* remove the absolute path to qemu */
634fe9959a2SEmilio G. Cota     if (unlikely(strlen(callsite->file) < qsp_qemu_path_len)) {
635fe9959a2SEmilio G. Cota         shortened = callsite->file;
636fe9959a2SEmilio G. Cota     } else {
637fe9959a2SEmilio G. Cota         shortened = callsite->file + qsp_qemu_path_len;
638fe9959a2SEmilio G. Cota     }
639fe9959a2SEmilio G. Cota     g_string_append_printf(s, "%s:%u", shortened, callsite->line);
640fe9959a2SEmilio G. Cota     return g_string_free(s, FALSE);
641fe9959a2SEmilio G. Cota }
642fe9959a2SEmilio G. Cota 
643fe9959a2SEmilio G. Cota struct QSPReportEntry {
644fe9959a2SEmilio G. Cota     const void *obj;
645fe9959a2SEmilio G. Cota     char *callsite_at;
646fe9959a2SEmilio G. Cota     const char *typename;
647fe9959a2SEmilio G. Cota     double time_s;
648fe9959a2SEmilio G. Cota     double ns_avg;
649fe9959a2SEmilio G. Cota     uint64_t n_acqs;
650d557de4aSEmilio G. Cota     unsigned int n_objs;
651fe9959a2SEmilio G. Cota };
652fe9959a2SEmilio G. Cota typedef struct QSPReportEntry QSPReportEntry;
653fe9959a2SEmilio G. Cota 
654fe9959a2SEmilio G. Cota struct QSPReport {
655fe9959a2SEmilio G. Cota     QSPReportEntry *entries;
656fe9959a2SEmilio G. Cota     size_t n_entries;
657fe9959a2SEmilio G. Cota     size_t max_n_entries;
658fe9959a2SEmilio G. Cota };
659fe9959a2SEmilio G. Cota typedef struct QSPReport QSPReport;
660fe9959a2SEmilio G. Cota 
661fe9959a2SEmilio G. Cota static gboolean qsp_tree_report(gpointer key, gpointer value, gpointer udata)
662fe9959a2SEmilio G. Cota {
663fe9959a2SEmilio G. Cota     const QSPEntry *e = key;
664fe9959a2SEmilio G. Cota     QSPReport *report = udata;
665fe9959a2SEmilio G. Cota     QSPReportEntry *entry;
666fe9959a2SEmilio G. Cota 
667fe9959a2SEmilio G. Cota     if (report->n_entries == report->max_n_entries) {
668fe9959a2SEmilio G. Cota         return TRUE;
669fe9959a2SEmilio G. Cota     }
670fe9959a2SEmilio G. Cota     entry = &report->entries[report->n_entries];
671fe9959a2SEmilio G. Cota     report->n_entries++;
672fe9959a2SEmilio G. Cota 
673fe9959a2SEmilio G. Cota     entry->obj = e->callsite->obj;
674d557de4aSEmilio G. Cota     entry->n_objs = e->n_objs;
675fe9959a2SEmilio G. Cota     entry->callsite_at = qsp_at(e->callsite);
676fe9959a2SEmilio G. Cota     entry->typename = qsp_typenames[e->callsite->type];
677fe9959a2SEmilio G. Cota     entry->time_s = e->ns * 1e-9;
678fe9959a2SEmilio G. Cota     entry->n_acqs = e->n_acqs;
679fe9959a2SEmilio G. Cota     entry->ns_avg = e->n_acqs ? e->ns / e->n_acqs : 0;
680fe9959a2SEmilio G. Cota     return FALSE;
681fe9959a2SEmilio G. Cota }
682fe9959a2SEmilio G. Cota 
683*ac7ff4cfSMarkus Armbruster static void pr_report(const QSPReport *rep)
684fe9959a2SEmilio G. Cota {
685fe9959a2SEmilio G. Cota     char *dashes;
686fe9959a2SEmilio G. Cota     size_t max_len = 0;
687fe9959a2SEmilio G. Cota     int callsite_len = 0;
688fe9959a2SEmilio G. Cota     int callsite_rspace;
689fe9959a2SEmilio G. Cota     int n_dashes;
690fe9959a2SEmilio G. Cota     size_t i;
691fe9959a2SEmilio G. Cota 
692fe9959a2SEmilio G. Cota     /* find out the maximum length of all 'callsite' fields */
693fe9959a2SEmilio G. Cota     for (i = 0; i < rep->n_entries; i++) {
694fe9959a2SEmilio G. Cota         const QSPReportEntry *e = &rep->entries[i];
695fe9959a2SEmilio G. Cota         size_t len = strlen(e->callsite_at);
696fe9959a2SEmilio G. Cota 
697fe9959a2SEmilio G. Cota         if (len > max_len) {
698fe9959a2SEmilio G. Cota             max_len = len;
699fe9959a2SEmilio G. Cota         }
700fe9959a2SEmilio G. Cota     }
701fe9959a2SEmilio G. Cota 
702fe9959a2SEmilio G. Cota     callsite_len = MAX(max_len, strlen("Call site"));
703fe9959a2SEmilio G. Cota     /* white space to leave to the right of "Call site" */
704fe9959a2SEmilio G. Cota     callsite_rspace = callsite_len - strlen("Call site");
705fe9959a2SEmilio G. Cota 
706*ac7ff4cfSMarkus Armbruster     qemu_printf("Type               Object  Call site%*s  Wait Time (s)  "
707fe9959a2SEmilio G. Cota                 "       Count  Average (us)\n", callsite_rspace, "");
708fe9959a2SEmilio G. Cota 
709fe9959a2SEmilio G. Cota     /* build a horizontal rule with dashes */
710fe9959a2SEmilio G. Cota     n_dashes = 79 + callsite_rspace;
711fe9959a2SEmilio G. Cota     dashes = g_malloc(n_dashes + 1);
712fe9959a2SEmilio G. Cota     memset(dashes, '-', n_dashes);
713fe9959a2SEmilio G. Cota     dashes[n_dashes] = '\0';
714*ac7ff4cfSMarkus Armbruster     qemu_printf("%s\n", dashes);
715fe9959a2SEmilio G. Cota 
716fe9959a2SEmilio G. Cota     for (i = 0; i < rep->n_entries; i++) {
717fe9959a2SEmilio G. Cota         const QSPReportEntry *e = &rep->entries[i];
718d557de4aSEmilio G. Cota         GString *s = g_string_new(NULL);
719fe9959a2SEmilio G. Cota 
720d557de4aSEmilio G. Cota         g_string_append_printf(s, "%-9s  ", e->typename);
721d557de4aSEmilio G. Cota         if (e->n_objs > 1) {
722d557de4aSEmilio G. Cota             g_string_append_printf(s, "[%12u]", e->n_objs);
723d557de4aSEmilio G. Cota         } else {
724d557de4aSEmilio G. Cota             g_string_append_printf(s, "%14p", e->obj);
725d557de4aSEmilio G. Cota         }
726d557de4aSEmilio G. Cota         g_string_append_printf(s, "  %s%*s  %13.5f  %12" PRIu64 "  %12.2f\n",
727d557de4aSEmilio G. Cota                                e->callsite_at,
728d557de4aSEmilio G. Cota                                callsite_len - (int)strlen(e->callsite_at), "",
729d557de4aSEmilio G. Cota                                e->time_s, e->n_acqs, e->ns_avg * 1e-3);
730*ac7ff4cfSMarkus Armbruster         qemu_printf("%s", s->str);
731d557de4aSEmilio G. Cota         g_string_free(s, TRUE);
732fe9959a2SEmilio G. Cota     }
733fe9959a2SEmilio G. Cota 
734*ac7ff4cfSMarkus Armbruster     qemu_printf("%s\n", dashes);
735fe9959a2SEmilio G. Cota     g_free(dashes);
736fe9959a2SEmilio G. Cota }
737fe9959a2SEmilio G. Cota 
738fe9959a2SEmilio G. Cota static void report_destroy(QSPReport *rep)
739fe9959a2SEmilio G. Cota {
740fe9959a2SEmilio G. Cota     size_t i;
741fe9959a2SEmilio G. Cota 
742fe9959a2SEmilio G. Cota     for (i = 0; i < rep->n_entries; i++) {
743fe9959a2SEmilio G. Cota         QSPReportEntry *e = &rep->entries[i];
744fe9959a2SEmilio G. Cota 
745fe9959a2SEmilio G. Cota         g_free(e->callsite_at);
746fe9959a2SEmilio G. Cota     }
747fe9959a2SEmilio G. Cota     g_free(rep->entries);
748fe9959a2SEmilio G. Cota }
749fe9959a2SEmilio G. Cota 
750*ac7ff4cfSMarkus Armbruster void qsp_report(size_t max, enum QSPSortBy sort_by,
751*ac7ff4cfSMarkus Armbruster                 bool callsite_coalesce)
752fe9959a2SEmilio G. Cota {
7530a22777cSEmilio G. Cota     GTree *tree = g_tree_new_full(qsp_tree_cmp, &sort_by, g_free, NULL);
754fe9959a2SEmilio G. Cota     QSPReport rep;
755fe9959a2SEmilio G. Cota 
756fe9959a2SEmilio G. Cota     qsp_init();
757fe9959a2SEmilio G. Cota 
758fe9959a2SEmilio G. Cota     rep.entries = g_new0(QSPReportEntry, max);
759fe9959a2SEmilio G. Cota     rep.n_entries = 0;
760fe9959a2SEmilio G. Cota     rep.max_n_entries = max;
761fe9959a2SEmilio G. Cota 
762d557de4aSEmilio G. Cota     qsp_mktree(tree, callsite_coalesce);
763fe9959a2SEmilio G. Cota     g_tree_foreach(tree, qsp_tree_report, &rep);
764fe9959a2SEmilio G. Cota     g_tree_destroy(tree);
765fe9959a2SEmilio G. Cota 
766*ac7ff4cfSMarkus Armbruster     pr_report(&rep);
767fe9959a2SEmilio G. Cota     report_destroy(&rep);
768fe9959a2SEmilio G. Cota }
769996e8d9aSEmilio G. Cota 
770996e8d9aSEmilio G. Cota static void qsp_snapshot_destroy(QSPSnapshot *snap)
771996e8d9aSEmilio G. Cota {
772996e8d9aSEmilio G. Cota     qht_iter(&snap->ht, qsp_ht_delete, NULL);
773996e8d9aSEmilio G. Cota     qht_destroy(&snap->ht);
774996e8d9aSEmilio G. Cota     g_free(snap);
775996e8d9aSEmilio G. Cota }
776996e8d9aSEmilio G. Cota 
777996e8d9aSEmilio G. Cota void qsp_reset(void)
778996e8d9aSEmilio G. Cota {
779996e8d9aSEmilio G. Cota     QSPSnapshot *new = g_new(QSPSnapshot, 1);
780996e8d9aSEmilio G. Cota     QSPSnapshot *old;
781996e8d9aSEmilio G. Cota 
782996e8d9aSEmilio G. Cota     qsp_init();
783996e8d9aSEmilio G. Cota 
784996e8d9aSEmilio G. Cota     qht_init(&new->ht, qsp_entry_cmp, QSP_INITIAL_SIZE,
785996e8d9aSEmilio G. Cota              QHT_MODE_AUTO_RESIZE | QHT_MODE_RAW_MUTEXES);
786996e8d9aSEmilio G. Cota 
787996e8d9aSEmilio G. Cota     /* take a snapshot of the current state */
788996e8d9aSEmilio G. Cota     qht_iter(&qsp_ht, qsp_aggregate, &new->ht);
789996e8d9aSEmilio G. Cota 
790996e8d9aSEmilio G. Cota     /* replace the previous snapshot, if any */
791996e8d9aSEmilio G. Cota     old = atomic_xchg(&qsp_snapshot, new);
792996e8d9aSEmilio G. Cota     if (old) {
793996e8d9aSEmilio G. Cota         call_rcu(old, qsp_snapshot_destroy, rcu);
794996e8d9aSEmilio G. Cota     }
795996e8d9aSEmilio G. Cota }
796