xref: /openbmc/qemu/util/qsp.c (revision 367189ef)
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  */
59ac7ff4cfSMarkus Armbruster 
60fe9959a2SEmilio G. Cota #include "qemu/osdep.h"
61ac7ff4cfSMarkus 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;
869ef0c6d6SRichard Henderson     aligned_uint64_t n_acqs;
879ef0c6d6SRichard Henderson     aligned_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;
1343dcc9c6eSYury Kotov QemuCondTimedWaitFunc qemu_cond_timedwait_func = qemu_cond_timedwait_impl;
135fe9959a2SEmilio G. Cota 
136fe9959a2SEmilio G. Cota /*
137fe9959a2SEmilio G. Cota  * It pays off to _not_ hash callsite->file; hashing a string is slow, and
138fe9959a2SEmilio G. Cota  * without it we still get a pretty unique hash.
139fe9959a2SEmilio G. Cota  */
140fe9959a2SEmilio G. Cota static inline
do_qsp_callsite_hash(const QSPCallSite * callsite,uint64_t ab)141c971d8faSEmilio G. Cota uint32_t do_qsp_callsite_hash(const QSPCallSite *callsite, uint64_t ab)
142fe9959a2SEmilio G. Cota {
143c971d8faSEmilio G. Cota     uint64_t cd = (uint64_t)(uintptr_t)callsite->obj;
144fe9959a2SEmilio G. Cota     uint32_t e = callsite->line;
145fe9959a2SEmilio G. Cota     uint32_t f = callsite->type;
146fe9959a2SEmilio G. Cota 
147*367189efSAlex Bennée     return qemu_xxhash8(ab, cd, 0, e, f);
148fe9959a2SEmilio G. Cota }
149fe9959a2SEmilio G. Cota 
150fe9959a2SEmilio G. Cota static inline
qsp_callsite_hash(const QSPCallSite * callsite)151fe9959a2SEmilio G. Cota uint32_t qsp_callsite_hash(const QSPCallSite *callsite)
152fe9959a2SEmilio G. Cota {
153fe9959a2SEmilio G. Cota     return do_qsp_callsite_hash(callsite, 0);
154fe9959a2SEmilio G. Cota }
155fe9959a2SEmilio G. Cota 
do_qsp_entry_hash(const QSPEntry * entry,uint64_t a)156fe9959a2SEmilio G. Cota static inline uint32_t do_qsp_entry_hash(const QSPEntry *entry, uint64_t a)
157fe9959a2SEmilio G. Cota {
158fe9959a2SEmilio G. Cota     return do_qsp_callsite_hash(entry->callsite, a);
159fe9959a2SEmilio G. Cota }
160fe9959a2SEmilio G. Cota 
qsp_entry_hash(const QSPEntry * entry)161fe9959a2SEmilio G. Cota static uint32_t qsp_entry_hash(const QSPEntry *entry)
162fe9959a2SEmilio G. Cota {
163fe9959a2SEmilio G. Cota     return do_qsp_entry_hash(entry, (uint64_t)(uintptr_t)entry->thread_ptr);
164fe9959a2SEmilio G. Cota }
165fe9959a2SEmilio G. Cota 
qsp_entry_no_thread_hash(const QSPEntry * entry)166fe9959a2SEmilio G. Cota static uint32_t qsp_entry_no_thread_hash(const QSPEntry *entry)
167fe9959a2SEmilio G. Cota {
168fe9959a2SEmilio G. Cota     return do_qsp_entry_hash(entry, 0);
169fe9959a2SEmilio G. Cota }
170fe9959a2SEmilio G. Cota 
171d557de4aSEmilio G. Cota /* without the objects we need to hash the file name to get a decent hash */
qsp_entry_no_thread_obj_hash(const QSPEntry * entry)172d557de4aSEmilio G. Cota static uint32_t qsp_entry_no_thread_obj_hash(const QSPEntry *entry)
173d557de4aSEmilio G. Cota {
174d557de4aSEmilio G. Cota     const QSPCallSite *callsite = entry->callsite;
175c971d8faSEmilio G. Cota     uint64_t ab = g_str_hash(callsite->file);
176c971d8faSEmilio G. Cota     uint64_t cd = callsite->line;
177d557de4aSEmilio G. Cota     uint32_t e = callsite->type;
178d557de4aSEmilio G. Cota 
179c971d8faSEmilio G. Cota     return qemu_xxhash5(ab, cd, e);
180d557de4aSEmilio G. Cota }
181d557de4aSEmilio G. Cota 
qsp_callsite_cmp(const void * ap,const void * bp)182fe9959a2SEmilio G. Cota static bool qsp_callsite_cmp(const void *ap, const void *bp)
183fe9959a2SEmilio G. Cota {
184fe9959a2SEmilio G. Cota     const QSPCallSite *a = ap;
185fe9959a2SEmilio G. Cota     const QSPCallSite *b = bp;
186fe9959a2SEmilio G. Cota 
187fe9959a2SEmilio G. Cota     return a == b ||
188fe9959a2SEmilio G. Cota         (a->obj == b->obj &&
189fe9959a2SEmilio G. Cota          a->line == b->line &&
190fe9959a2SEmilio G. Cota          a->type == b->type &&
191fe9959a2SEmilio G. Cota          (a->file == b->file || !strcmp(a->file, b->file)));
192fe9959a2SEmilio G. Cota }
193fe9959a2SEmilio G. Cota 
qsp_callsite_no_obj_cmp(const void * ap,const void * bp)194d557de4aSEmilio G. Cota static bool qsp_callsite_no_obj_cmp(const void *ap, const void *bp)
195d557de4aSEmilio G. Cota {
196d557de4aSEmilio G. Cota     const QSPCallSite *a = ap;
197d557de4aSEmilio G. Cota     const QSPCallSite *b = bp;
198d557de4aSEmilio G. Cota 
199d557de4aSEmilio G. Cota     return a == b ||
200d557de4aSEmilio G. Cota         (a->line == b->line &&
201d557de4aSEmilio G. Cota          a->type == b->type &&
202d557de4aSEmilio G. Cota          (a->file == b->file || !strcmp(a->file, b->file)));
203d557de4aSEmilio G. Cota }
204d557de4aSEmilio G. Cota 
qsp_entry_no_thread_cmp(const void * ap,const void * bp)205fe9959a2SEmilio G. Cota static bool qsp_entry_no_thread_cmp(const void *ap, const void *bp)
206fe9959a2SEmilio G. Cota {
207fe9959a2SEmilio G. Cota     const QSPEntry *a = ap;
208fe9959a2SEmilio G. Cota     const QSPEntry *b = bp;
209fe9959a2SEmilio G. Cota 
210fe9959a2SEmilio G. Cota     return qsp_callsite_cmp(a->callsite, b->callsite);
211fe9959a2SEmilio G. Cota }
212fe9959a2SEmilio G. Cota 
qsp_entry_no_thread_obj_cmp(const void * ap,const void * bp)213d557de4aSEmilio G. Cota static bool qsp_entry_no_thread_obj_cmp(const void *ap, const void *bp)
214d557de4aSEmilio G. Cota {
215d557de4aSEmilio G. Cota     const QSPEntry *a = ap;
216d557de4aSEmilio G. Cota     const QSPEntry *b = bp;
217d557de4aSEmilio G. Cota 
218d557de4aSEmilio G. Cota     return qsp_callsite_no_obj_cmp(a->callsite, b->callsite);
219d557de4aSEmilio G. Cota }
220d557de4aSEmilio G. Cota 
qsp_entry_cmp(const void * ap,const void * bp)221fe9959a2SEmilio G. Cota static bool qsp_entry_cmp(const void *ap, const void *bp)
222fe9959a2SEmilio G. Cota {
223fe9959a2SEmilio G. Cota     const QSPEntry *a = ap;
224fe9959a2SEmilio G. Cota     const QSPEntry *b = bp;
225fe9959a2SEmilio G. Cota 
226fe9959a2SEmilio G. Cota     return a->thread_ptr == b->thread_ptr &&
227fe9959a2SEmilio G. Cota         qsp_callsite_cmp(a->callsite, b->callsite);
228fe9959a2SEmilio G. Cota }
229fe9959a2SEmilio G. Cota 
230fe9959a2SEmilio G. Cota /*
231fe9959a2SEmilio G. Cota  * Normally we'd call this from a constructor function, but we want it to work
232fe9959a2SEmilio G. Cota  * via libutil as well.
233fe9959a2SEmilio G. Cota  */
qsp_do_init(void)234fe9959a2SEmilio G. Cota static void qsp_do_init(void)
235fe9959a2SEmilio G. Cota {
236fe9959a2SEmilio G. Cota     /* make sure this file's path in the tree is up to date with QSP_REL_PATH */
237fe9959a2SEmilio G. Cota     g_assert(strstr(__FILE__, QSP_REL_PATH));
238fe9959a2SEmilio G. Cota     qsp_qemu_path_len = strlen(__FILE__) - strlen(QSP_REL_PATH);
239fe9959a2SEmilio G. Cota 
240fe9959a2SEmilio G. Cota     qht_init(&qsp_ht, qsp_entry_cmp, QSP_INITIAL_SIZE,
241fe9959a2SEmilio G. Cota              QHT_MODE_AUTO_RESIZE | QHT_MODE_RAW_MUTEXES);
242fe9959a2SEmilio G. Cota     qht_init(&qsp_callsite_ht, qsp_callsite_cmp, QSP_INITIAL_SIZE,
243fe9959a2SEmilio G. Cota              QHT_MODE_AUTO_RESIZE | QHT_MODE_RAW_MUTEXES);
244fe9959a2SEmilio G. Cota }
245fe9959a2SEmilio G. Cota 
qsp_init__slowpath(void)246fe9959a2SEmilio G. Cota static __attribute__((noinline)) void qsp_init__slowpath(void)
247fe9959a2SEmilio G. Cota {
248d73415a3SStefan Hajnoczi     if (qatomic_cmpxchg(&qsp_initializing, false, true) == false) {
249fe9959a2SEmilio G. Cota         qsp_do_init();
250d73415a3SStefan Hajnoczi         qatomic_set(&qsp_initialized, true);
251fe9959a2SEmilio G. Cota     } else {
252d73415a3SStefan Hajnoczi         while (!qatomic_read(&qsp_initialized)) {
253fe9959a2SEmilio G. Cota             cpu_relax();
254fe9959a2SEmilio G. Cota         }
255fe9959a2SEmilio G. Cota     }
256fe9959a2SEmilio G. Cota }
257fe9959a2SEmilio G. Cota 
258fe9959a2SEmilio G. Cota /* qsp_init() must be called from _all_ exported functions */
qsp_init(void)259fe9959a2SEmilio G. Cota static inline void qsp_init(void)
260fe9959a2SEmilio G. Cota {
261d73415a3SStefan Hajnoczi     if (likely(qatomic_read(&qsp_initialized))) {
262fe9959a2SEmilio G. Cota         return;
263fe9959a2SEmilio G. Cota     }
264fe9959a2SEmilio G. Cota     qsp_init__slowpath();
265fe9959a2SEmilio G. Cota }
266fe9959a2SEmilio G. Cota 
qsp_callsite_find(const QSPCallSite * orig)267fe9959a2SEmilio G. Cota static QSPCallSite *qsp_callsite_find(const QSPCallSite *orig)
268fe9959a2SEmilio G. Cota {
269fe9959a2SEmilio G. Cota     QSPCallSite *callsite;
270fe9959a2SEmilio G. Cota     uint32_t hash;
271fe9959a2SEmilio G. Cota 
272fe9959a2SEmilio G. Cota     hash = qsp_callsite_hash(orig);
273fe9959a2SEmilio G. Cota     callsite = qht_lookup(&qsp_callsite_ht, orig, hash);
274fe9959a2SEmilio G. Cota     if (callsite == NULL) {
275fe9959a2SEmilio G. Cota         void *existing = NULL;
276fe9959a2SEmilio G. Cota 
277fe9959a2SEmilio G. Cota         callsite = g_new(QSPCallSite, 1);
278fe9959a2SEmilio G. Cota         memcpy(callsite, orig, sizeof(*callsite));
279fe9959a2SEmilio G. Cota         qht_insert(&qsp_callsite_ht, callsite, hash, &existing);
280fe9959a2SEmilio G. Cota         if (unlikely(existing)) {
281fe9959a2SEmilio G. Cota             g_free(callsite);
282fe9959a2SEmilio G. Cota             callsite = existing;
283fe9959a2SEmilio G. Cota         }
284fe9959a2SEmilio G. Cota     }
285fe9959a2SEmilio G. Cota     return callsite;
286fe9959a2SEmilio G. Cota }
287fe9959a2SEmilio G. Cota 
288fe9959a2SEmilio G. Cota static QSPEntry *
qsp_entry_create(struct qht * ht,const QSPEntry * entry,uint32_t hash)289fe9959a2SEmilio G. Cota qsp_entry_create(struct qht *ht, const QSPEntry *entry, uint32_t hash)
290fe9959a2SEmilio G. Cota {
291fe9959a2SEmilio G. Cota     QSPEntry *e;
292fe9959a2SEmilio G. Cota     void *existing = NULL;
293fe9959a2SEmilio G. Cota 
294fe9959a2SEmilio G. Cota     e = g_new0(QSPEntry, 1);
295fe9959a2SEmilio G. Cota     e->thread_ptr = entry->thread_ptr;
296fe9959a2SEmilio G. Cota     e->callsite = qsp_callsite_find(entry->callsite);
297fe9959a2SEmilio G. Cota 
298fe9959a2SEmilio G. Cota     qht_insert(ht, e, hash, &existing);
299fe9959a2SEmilio G. Cota     if (unlikely(existing)) {
300fe9959a2SEmilio G. Cota         g_free(e);
301fe9959a2SEmilio G. Cota         e = existing;
302fe9959a2SEmilio G. Cota     }
303fe9959a2SEmilio G. Cota     return e;
304fe9959a2SEmilio G. Cota }
305fe9959a2SEmilio G. Cota 
306fe9959a2SEmilio G. Cota static QSPEntry *
qsp_entry_find(struct qht * ht,const QSPEntry * entry,uint32_t hash)307fe9959a2SEmilio G. Cota qsp_entry_find(struct qht *ht, const QSPEntry *entry, uint32_t hash)
308fe9959a2SEmilio G. Cota {
309fe9959a2SEmilio G. Cota     QSPEntry *e;
310fe9959a2SEmilio G. Cota 
311fe9959a2SEmilio G. Cota     e = qht_lookup(ht, entry, hash);
312fe9959a2SEmilio G. Cota     if (e == NULL) {
313fe9959a2SEmilio G. Cota         e = qsp_entry_create(ht, entry, hash);
314fe9959a2SEmilio G. Cota     }
315fe9959a2SEmilio G. Cota     return e;
316fe9959a2SEmilio G. Cota }
317fe9959a2SEmilio G. Cota 
318fe9959a2SEmilio G. Cota /*
319fe9959a2SEmilio G. Cota  * Note: Entries are never removed, so callers do not have to be in an RCU
320fe9959a2SEmilio G. Cota  * read-side critical section.
321fe9959a2SEmilio G. Cota  */
qsp_entry_get(const void * obj,const char * file,int line,enum QSPType type)322fe9959a2SEmilio G. Cota static QSPEntry *qsp_entry_get(const void *obj, const char *file, int line,
323fe9959a2SEmilio G. Cota                                enum QSPType type)
324fe9959a2SEmilio G. Cota {
325fe9959a2SEmilio G. Cota     QSPCallSite callsite = {
326fe9959a2SEmilio G. Cota         .obj = obj,
327fe9959a2SEmilio G. Cota         .file = file,
328fe9959a2SEmilio G. Cota         .line = line,
329fe9959a2SEmilio G. Cota         .type = type,
330fe9959a2SEmilio G. Cota     };
331fe9959a2SEmilio G. Cota     QSPEntry orig;
332fe9959a2SEmilio G. Cota     uint32_t hash;
333fe9959a2SEmilio G. Cota 
334fe9959a2SEmilio G. Cota     qsp_init();
335fe9959a2SEmilio G. Cota 
336fe9959a2SEmilio G. Cota     orig.thread_ptr = &qsp_thread;
337fe9959a2SEmilio G. Cota     orig.callsite = &callsite;
338fe9959a2SEmilio G. Cota 
339fe9959a2SEmilio G. Cota     hash = qsp_entry_hash(&orig);
340fe9959a2SEmilio G. Cota     return qsp_entry_find(&qsp_ht, &orig, hash);
341fe9959a2SEmilio G. Cota }
342fe9959a2SEmilio G. Cota 
343fe9959a2SEmilio G. Cota /*
344fe9959a2SEmilio G. Cota  * @e is in the global hash table; it is only written to by the current thread,
345fe9959a2SEmilio G. Cota  * so we write to it atomically (as in "write once") to prevent torn reads.
346fe9959a2SEmilio G. Cota  */
do_qsp_entry_record(QSPEntry * e,int64_t delta,bool acq)347fe9959a2SEmilio G. Cota static inline void do_qsp_entry_record(QSPEntry *e, int64_t delta, bool acq)
348fe9959a2SEmilio G. Cota {
349d73415a3SStefan Hajnoczi     qatomic_set_u64(&e->ns, e->ns + delta);
350fe9959a2SEmilio G. Cota     if (acq) {
351d73415a3SStefan Hajnoczi         qatomic_set_u64(&e->n_acqs, e->n_acqs + 1);
352fe9959a2SEmilio G. Cota     }
353fe9959a2SEmilio G. Cota }
354fe9959a2SEmilio G. Cota 
qsp_entry_record(QSPEntry * e,int64_t delta)355fe9959a2SEmilio G. Cota static inline void qsp_entry_record(QSPEntry *e, int64_t delta)
356fe9959a2SEmilio G. Cota {
357fe9959a2SEmilio G. Cota     do_qsp_entry_record(e, delta, true);
358fe9959a2SEmilio G. Cota }
359fe9959a2SEmilio G. Cota 
360fe9959a2SEmilio G. Cota #define QSP_GEN_VOID(type_, qsp_t_, func_, impl_)                       \
361fe9959a2SEmilio G. Cota     static void func_(type_ *obj, const char *file, int line)           \
362fe9959a2SEmilio G. Cota     {                                                                   \
363fe9959a2SEmilio G. Cota         QSPEntry *e;                                                    \
364fe9959a2SEmilio G. Cota         int64_t t0, t1;                                                 \
365fe9959a2SEmilio G. Cota                                                                         \
366fe9959a2SEmilio G. Cota         t0 = get_clock();                                               \
367fe9959a2SEmilio G. Cota         impl_(obj, file, line);                                         \
368fe9959a2SEmilio G. Cota         t1 = get_clock();                                               \
369fe9959a2SEmilio G. Cota                                                                         \
370fe9959a2SEmilio G. Cota         e = qsp_entry_get(obj, file, line, qsp_t_);                     \
371fe9959a2SEmilio G. Cota         qsp_entry_record(e, t1 - t0);                                   \
372fe9959a2SEmilio G. Cota     }
373fe9959a2SEmilio G. Cota 
374fe9959a2SEmilio G. Cota #define QSP_GEN_RET1(type_, qsp_t_, func_, impl_)                       \
375fe9959a2SEmilio G. Cota     static int func_(type_ *obj, const char *file, int line)            \
376fe9959a2SEmilio G. Cota     {                                                                   \
377fe9959a2SEmilio G. Cota         QSPEntry *e;                                                    \
378fe9959a2SEmilio G. Cota         int64_t t0, t1;                                                 \
379fe9959a2SEmilio G. Cota         int err;                                                        \
380fe9959a2SEmilio G. Cota                                                                         \
381fe9959a2SEmilio G. Cota         t0 = get_clock();                                               \
382fe9959a2SEmilio G. Cota         err = impl_(obj, file, line);                                   \
383fe9959a2SEmilio G. Cota         t1 = get_clock();                                               \
384fe9959a2SEmilio G. Cota                                                                         \
385fe9959a2SEmilio G. Cota         e = qsp_entry_get(obj, file, line, qsp_t_);                     \
386fe9959a2SEmilio G. Cota         do_qsp_entry_record(e, t1 - t0, !err);                          \
387fe9959a2SEmilio G. Cota         return err;                                                     \
388fe9959a2SEmilio G. Cota     }
389fe9959a2SEmilio G. Cota 
QSP_GEN_VOID(QemuMutex,QSP_BQL_MUTEX,qsp_bql_mutex_lock,qemu_mutex_lock_impl)390cb764d06SEmilio G. Cota QSP_GEN_VOID(QemuMutex, QSP_BQL_MUTEX, qsp_bql_mutex_lock, qemu_mutex_lock_impl)
391fe9959a2SEmilio G. Cota QSP_GEN_VOID(QemuMutex, QSP_MUTEX, qsp_mutex_lock, qemu_mutex_lock_impl)
392fe9959a2SEmilio G. Cota QSP_GEN_RET1(QemuMutex, QSP_MUTEX, qsp_mutex_trylock, qemu_mutex_trylock_impl)
393fe9959a2SEmilio G. Cota 
394fe9959a2SEmilio G. Cota QSP_GEN_VOID(QemuRecMutex, QSP_REC_MUTEX, qsp_rec_mutex_lock,
395fe9959a2SEmilio G. Cota              qemu_rec_mutex_lock_impl)
396fe9959a2SEmilio G. Cota QSP_GEN_RET1(QemuRecMutex, QSP_REC_MUTEX, qsp_rec_mutex_trylock,
397fe9959a2SEmilio G. Cota              qemu_rec_mutex_trylock_impl)
398fe9959a2SEmilio G. Cota 
399fe9959a2SEmilio G. Cota #undef QSP_GEN_RET1
400fe9959a2SEmilio G. Cota #undef QSP_GEN_VOID
401fe9959a2SEmilio G. Cota 
402fe9959a2SEmilio G. Cota static void
403fe9959a2SEmilio G. Cota qsp_cond_wait(QemuCond *cond, QemuMutex *mutex, const char *file, int line)
404fe9959a2SEmilio G. Cota {
405fe9959a2SEmilio G. Cota     QSPEntry *e;
406fe9959a2SEmilio G. Cota     int64_t t0, t1;
407fe9959a2SEmilio G. Cota 
408fe9959a2SEmilio G. Cota     t0 = get_clock();
409fe9959a2SEmilio G. Cota     qemu_cond_wait_impl(cond, mutex, file, line);
410fe9959a2SEmilio G. Cota     t1 = get_clock();
411fe9959a2SEmilio G. Cota 
412fe9959a2SEmilio G. Cota     e = qsp_entry_get(cond, file, line, QSP_CONDVAR);
413fe9959a2SEmilio G. Cota     qsp_entry_record(e, t1 - t0);
414fe9959a2SEmilio G. Cota }
415fe9959a2SEmilio G. Cota 
4163dcc9c6eSYury Kotov static bool
qsp_cond_timedwait(QemuCond * cond,QemuMutex * mutex,int ms,const char * file,int line)4173dcc9c6eSYury Kotov qsp_cond_timedwait(QemuCond *cond, QemuMutex *mutex, int ms,
4183dcc9c6eSYury Kotov                    const char *file, int line)
4193dcc9c6eSYury Kotov {
4203dcc9c6eSYury Kotov     QSPEntry *e;
4213dcc9c6eSYury Kotov     int64_t t0, t1;
4223dcc9c6eSYury Kotov     bool ret;
4233dcc9c6eSYury Kotov 
4243dcc9c6eSYury Kotov     t0 = get_clock();
4253dcc9c6eSYury Kotov     ret = qemu_cond_timedwait_impl(cond, mutex, ms, file, line);
4263dcc9c6eSYury Kotov     t1 = get_clock();
4273dcc9c6eSYury Kotov 
4283dcc9c6eSYury Kotov     e = qsp_entry_get(cond, file, line, QSP_CONDVAR);
4293dcc9c6eSYury Kotov     qsp_entry_record(e, t1 - t0);
4303dcc9c6eSYury Kotov     return ret;
4313dcc9c6eSYury Kotov }
4323dcc9c6eSYury Kotov 
qsp_is_enabled(void)433fe9959a2SEmilio G. Cota bool qsp_is_enabled(void)
434fe9959a2SEmilio G. Cota {
435d73415a3SStefan Hajnoczi     return qatomic_read(&qemu_mutex_lock_func) == qsp_mutex_lock;
436fe9959a2SEmilio G. Cota }
437fe9959a2SEmilio G. Cota 
qsp_enable(void)438fe9959a2SEmilio G. Cota void qsp_enable(void)
439fe9959a2SEmilio G. Cota {
440d73415a3SStefan Hajnoczi     qatomic_set(&qemu_mutex_lock_func, qsp_mutex_lock);
441d73415a3SStefan Hajnoczi     qatomic_set(&qemu_mutex_trylock_func, qsp_mutex_trylock);
442d73415a3SStefan Hajnoczi     qatomic_set(&qemu_bql_mutex_lock_func, qsp_bql_mutex_lock);
443d73415a3SStefan Hajnoczi     qatomic_set(&qemu_rec_mutex_lock_func, qsp_rec_mutex_lock);
444d73415a3SStefan Hajnoczi     qatomic_set(&qemu_rec_mutex_trylock_func, qsp_rec_mutex_trylock);
445d73415a3SStefan Hajnoczi     qatomic_set(&qemu_cond_wait_func, qsp_cond_wait);
446d73415a3SStefan Hajnoczi     qatomic_set(&qemu_cond_timedwait_func, qsp_cond_timedwait);
447fe9959a2SEmilio G. Cota }
448fe9959a2SEmilio G. Cota 
qsp_disable(void)449fe9959a2SEmilio G. Cota void qsp_disable(void)
450fe9959a2SEmilio G. Cota {
451d73415a3SStefan Hajnoczi     qatomic_set(&qemu_mutex_lock_func, qemu_mutex_lock_impl);
452d73415a3SStefan Hajnoczi     qatomic_set(&qemu_mutex_trylock_func, qemu_mutex_trylock_impl);
453d73415a3SStefan Hajnoczi     qatomic_set(&qemu_bql_mutex_lock_func, qemu_mutex_lock_impl);
454d73415a3SStefan Hajnoczi     qatomic_set(&qemu_rec_mutex_lock_func, qemu_rec_mutex_lock_impl);
455d73415a3SStefan Hajnoczi     qatomic_set(&qemu_rec_mutex_trylock_func, qemu_rec_mutex_trylock_impl);
456d73415a3SStefan Hajnoczi     qatomic_set(&qemu_cond_wait_func, qemu_cond_wait_impl);
457d73415a3SStefan Hajnoczi     qatomic_set(&qemu_cond_timedwait_func, qemu_cond_timedwait_impl);
458fe9959a2SEmilio G. Cota }
459fe9959a2SEmilio G. Cota 
qsp_tree_cmp(gconstpointer ap,gconstpointer bp,gpointer up)460fe9959a2SEmilio G. Cota static gint qsp_tree_cmp(gconstpointer ap, gconstpointer bp, gpointer up)
461fe9959a2SEmilio G. Cota {
462fe9959a2SEmilio G. Cota     const QSPEntry *a = ap;
463fe9959a2SEmilio G. Cota     const QSPEntry *b = bp;
4640a22777cSEmilio G. Cota     enum QSPSortBy sort_by = *(enum QSPSortBy *)up;
465fe9959a2SEmilio G. Cota     const QSPCallSite *ca;
466fe9959a2SEmilio G. Cota     const QSPCallSite *cb;
467fe9959a2SEmilio G. Cota 
4680a22777cSEmilio G. Cota     switch (sort_by) {
4690a22777cSEmilio G. Cota     case QSP_SORT_BY_TOTAL_WAIT_TIME:
470fe9959a2SEmilio G. Cota         if (a->ns > b->ns) {
471fe9959a2SEmilio G. Cota             return -1;
472fe9959a2SEmilio G. Cota         } else if (a->ns < b->ns) {
473fe9959a2SEmilio G. Cota             return 1;
474fe9959a2SEmilio G. Cota         }
4750a22777cSEmilio G. Cota         break;
4760a22777cSEmilio G. Cota     case QSP_SORT_BY_AVG_WAIT_TIME:
4770a22777cSEmilio G. Cota     {
4780a22777cSEmilio G. Cota         double avg_a = a->n_acqs ? a->ns / a->n_acqs : 0;
4790a22777cSEmilio G. Cota         double avg_b = b->n_acqs ? b->ns / b->n_acqs : 0;
4800a22777cSEmilio G. Cota 
4810a22777cSEmilio G. Cota         if (avg_a > avg_b) {
4820a22777cSEmilio G. Cota             return -1;
4830a22777cSEmilio G. Cota         } else if (avg_a < avg_b) {
4840a22777cSEmilio G. Cota             return 1;
4850a22777cSEmilio G. Cota         }
4860a22777cSEmilio G. Cota         break;
4870a22777cSEmilio G. Cota     }
4880a22777cSEmilio G. Cota     default:
4890a22777cSEmilio G. Cota         g_assert_not_reached();
4900a22777cSEmilio G. Cota     }
4910a22777cSEmilio G. Cota 
492fe9959a2SEmilio G. Cota     ca = a->callsite;
493fe9959a2SEmilio G. Cota     cb = b->callsite;
494fe9959a2SEmilio G. Cota     /* Break the tie with the object's address */
495fe9959a2SEmilio G. Cota     if (ca->obj < cb->obj) {
496fe9959a2SEmilio G. Cota         return -1;
497fe9959a2SEmilio G. Cota     } else if (ca->obj > cb->obj) {
498fe9959a2SEmilio G. Cota         return 1;
499fe9959a2SEmilio G. Cota     } else {
500fe9959a2SEmilio G. Cota         int cmp;
501fe9959a2SEmilio G. Cota 
502fe9959a2SEmilio G. Cota         /* same obj. Break the tie with the callsite's file */
503fe9959a2SEmilio G. Cota         cmp = strcmp(ca->file, cb->file);
504fe9959a2SEmilio G. Cota         if (cmp) {
505fe9959a2SEmilio G. Cota             return cmp;
506fe9959a2SEmilio G. Cota         }
507fe9959a2SEmilio G. Cota         /* same callsite file. Break the tie with the callsite's line */
508fe9959a2SEmilio G. Cota         g_assert(ca->line != cb->line);
509fe9959a2SEmilio G. Cota         if (ca->line < cb->line) {
510fe9959a2SEmilio G. Cota             return -1;
511fe9959a2SEmilio G. Cota         } else if (ca->line > cb->line) {
512fe9959a2SEmilio G. Cota             return 1;
513fe9959a2SEmilio G. Cota         } else {
514fe9959a2SEmilio G. Cota             /* break the tie with the callsite's type */
515fe9959a2SEmilio G. Cota             return cb->type - ca->type;
516fe9959a2SEmilio G. Cota         }
517fe9959a2SEmilio G. Cota     }
518fe9959a2SEmilio G. Cota }
519fe9959a2SEmilio G. Cota 
qsp_sort(void * p,uint32_t h,void * userp)52078255ba2SEmilio G. Cota static void qsp_sort(void *p, uint32_t h, void *userp)
521fe9959a2SEmilio G. Cota {
522fe9959a2SEmilio G. Cota     QSPEntry *e = p;
523fe9959a2SEmilio G. Cota     GTree *tree = userp;
524fe9959a2SEmilio G. Cota 
525fe9959a2SEmilio G. Cota     g_tree_insert(tree, e, NULL);
526fe9959a2SEmilio G. Cota }
527fe9959a2SEmilio G. Cota 
qsp_aggregate(void * p,uint32_t h,void * up)52878255ba2SEmilio G. Cota static void qsp_aggregate(void *p, uint32_t h, void *up)
529fe9959a2SEmilio G. Cota {
530fe9959a2SEmilio G. Cota     struct qht *ht = up;
531fe9959a2SEmilio G. Cota     const QSPEntry *e = p;
532fe9959a2SEmilio G. Cota     QSPEntry *agg;
533fe9959a2SEmilio G. Cota     uint32_t hash;
534fe9959a2SEmilio G. Cota 
535fe9959a2SEmilio G. Cota     hash = qsp_entry_no_thread_hash(e);
536fe9959a2SEmilio G. Cota     agg = qsp_entry_find(ht, e, hash);
537ac8c7748SEmilio G. Cota     /*
538ac8c7748SEmilio G. Cota      * The entry is in the global hash table; read from it atomically (as in
539ac8c7748SEmilio G. Cota      * "read once").
540ac8c7748SEmilio G. Cota      */
541d73415a3SStefan Hajnoczi     agg->ns += qatomic_read_u64(&e->ns);
542d73415a3SStefan Hajnoczi     agg->n_acqs += qatomic_read_u64(&e->n_acqs);
543fe9959a2SEmilio G. Cota }
544fe9959a2SEmilio G. Cota 
qsp_iter_diff(void * p,uint32_t hash,void * htp)54578255ba2SEmilio G. Cota static void qsp_iter_diff(void *p, uint32_t hash, void *htp)
546996e8d9aSEmilio G. Cota {
547996e8d9aSEmilio G. Cota     struct qht *ht = htp;
548996e8d9aSEmilio G. Cota     QSPEntry *old = p;
549996e8d9aSEmilio G. Cota     QSPEntry *new;
550996e8d9aSEmilio G. Cota 
551996e8d9aSEmilio G. Cota     new = qht_lookup(ht, old, hash);
552996e8d9aSEmilio G. Cota     /* entries are never deleted, so we must have this one */
553996e8d9aSEmilio G. Cota     g_assert(new != NULL);
554996e8d9aSEmilio G. Cota     /* our reading of the stats happened after the snapshot was taken */
555996e8d9aSEmilio G. Cota     g_assert(new->n_acqs >= old->n_acqs);
556996e8d9aSEmilio G. Cota     g_assert(new->ns >= old->ns);
557996e8d9aSEmilio G. Cota 
558996e8d9aSEmilio G. Cota     new->n_acqs -= old->n_acqs;
559996e8d9aSEmilio G. Cota     new->ns -= old->ns;
560996e8d9aSEmilio G. Cota 
561996e8d9aSEmilio G. Cota     /* No point in reporting an empty entry */
562996e8d9aSEmilio G. Cota     if (new->n_acqs == 0 && new->ns == 0) {
563996e8d9aSEmilio G. Cota         bool removed = qht_remove(ht, new, hash);
564996e8d9aSEmilio G. Cota 
565996e8d9aSEmilio G. Cota         g_assert(removed);
566996e8d9aSEmilio G. Cota         g_free(new);
567996e8d9aSEmilio G. Cota     }
568996e8d9aSEmilio G. Cota }
569996e8d9aSEmilio G. Cota 
qsp_diff(struct qht * orig,struct qht * new)570996e8d9aSEmilio G. Cota static void qsp_diff(struct qht *orig, struct qht *new)
571996e8d9aSEmilio G. Cota {
572996e8d9aSEmilio G. Cota     qht_iter(orig, qsp_iter_diff, new);
573996e8d9aSEmilio G. Cota }
574996e8d9aSEmilio G. Cota 
qsp_iter_callsite_coalesce(void * p,uint32_t h,void * htp)57578255ba2SEmilio G. Cota static void qsp_iter_callsite_coalesce(void *p, uint32_t h, void *htp)
576d557de4aSEmilio G. Cota {
577d557de4aSEmilio G. Cota     struct qht *ht = htp;
578d557de4aSEmilio G. Cota     QSPEntry *old = p;
579d557de4aSEmilio G. Cota     QSPEntry *e;
580d557de4aSEmilio G. Cota     uint32_t hash;
581d557de4aSEmilio G. Cota 
582d557de4aSEmilio G. Cota     hash = qsp_entry_no_thread_obj_hash(old);
583d557de4aSEmilio G. Cota     e = qht_lookup(ht, old, hash);
584d557de4aSEmilio G. Cota     if (e == NULL) {
585d557de4aSEmilio G. Cota         e = qsp_entry_create(ht, old, hash);
586d557de4aSEmilio G. Cota         e->n_objs = 1;
587d557de4aSEmilio G. Cota     } else if (e->callsite->obj != old->callsite->obj) {
588d557de4aSEmilio G. Cota         e->n_objs++;
589d557de4aSEmilio G. Cota     }
590d557de4aSEmilio G. Cota     e->ns += old->ns;
591d557de4aSEmilio G. Cota     e->n_acqs += old->n_acqs;
592d557de4aSEmilio G. Cota }
593d557de4aSEmilio G. Cota 
qsp_ht_delete(void * p,uint32_t h,void * htp)59478255ba2SEmilio G. Cota static void qsp_ht_delete(void *p, uint32_t h, void *htp)
595996e8d9aSEmilio G. Cota {
596996e8d9aSEmilio G. Cota     g_free(p);
597996e8d9aSEmilio G. Cota }
598996e8d9aSEmilio G. Cota 
qsp_mktree(GTree * tree,bool callsite_coalesce)599d557de4aSEmilio G. Cota static void qsp_mktree(GTree *tree, bool callsite_coalesce)
600fe9959a2SEmilio G. Cota {
601d557de4aSEmilio G. Cota     struct qht ht, coalesce_ht;
602d557de4aSEmilio G. Cota     struct qht *htp;
603fe9959a2SEmilio G. Cota 
604996e8d9aSEmilio G. Cota     /*
605996e8d9aSEmilio G. Cota      * First, see if there's a prior snapshot, so that we read the global hash
606996e8d9aSEmilio G. Cota      * table _after_ the snapshot has been created, which guarantees that
607996e8d9aSEmilio G. Cota      * the entries we'll read will be a superset of the snapshot's entries.
608996e8d9aSEmilio G. Cota      *
609996e8d9aSEmilio G. Cota      * We must remain in an RCU read-side critical section until we're done
610996e8d9aSEmilio G. Cota      * with the snapshot.
611996e8d9aSEmilio G. Cota      */
6122a86be25SDr. David Alan Gilbert     WITH_RCU_READ_LOCK_GUARD() {
613d73415a3SStefan Hajnoczi         QSPSnapshot *snap = qatomic_rcu_read(&qsp_snapshot);
614996e8d9aSEmilio G. Cota 
615fe9959a2SEmilio G. Cota         /* Aggregate all results from the global hash table into a local one */
616fe9959a2SEmilio G. Cota         qht_init(&ht, qsp_entry_no_thread_cmp, QSP_INITIAL_SIZE,
617fe9959a2SEmilio G. Cota                  QHT_MODE_AUTO_RESIZE | QHT_MODE_RAW_MUTEXES);
618fe9959a2SEmilio G. Cota         qht_iter(&qsp_ht, qsp_aggregate, &ht);
619fe9959a2SEmilio G. Cota 
620996e8d9aSEmilio G. Cota         /* compute the difference wrt the snapshot, if any */
621996e8d9aSEmilio G. Cota         if (snap) {
622996e8d9aSEmilio G. Cota             qsp_diff(&snap->ht, &ht);
623996e8d9aSEmilio G. Cota         }
6242a86be25SDr. David Alan Gilbert     }
625996e8d9aSEmilio G. Cota 
626d557de4aSEmilio G. Cota     htp = &ht;
627d557de4aSEmilio G. Cota     if (callsite_coalesce) {
628d557de4aSEmilio G. Cota         qht_init(&coalesce_ht, qsp_entry_no_thread_obj_cmp, QSP_INITIAL_SIZE,
629d557de4aSEmilio G. Cota                  QHT_MODE_AUTO_RESIZE | QHT_MODE_RAW_MUTEXES);
630d557de4aSEmilio G. Cota         qht_iter(&ht, qsp_iter_callsite_coalesce, &coalesce_ht);
631d557de4aSEmilio G. Cota 
632d557de4aSEmilio G. Cota         /* free the previous hash table, and point htp to coalesce_ht */
633d557de4aSEmilio G. Cota         qht_iter(&ht, qsp_ht_delete, NULL);
634d557de4aSEmilio G. Cota         qht_destroy(&ht);
635d557de4aSEmilio G. Cota         htp = &coalesce_ht;
636d557de4aSEmilio G. Cota     }
637d557de4aSEmilio G. Cota 
638fe9959a2SEmilio G. Cota     /* sort the hash table elements by using a tree */
639d557de4aSEmilio G. Cota     qht_iter(htp, qsp_sort, tree);
640fe9959a2SEmilio G. Cota 
641fe9959a2SEmilio G. Cota     /* free the hash table, but keep the elements (those are in the tree now) */
642d557de4aSEmilio G. Cota     qht_destroy(htp);
643fe9959a2SEmilio G. Cota }
644fe9959a2SEmilio G. Cota 
645fe9959a2SEmilio G. Cota /* free string with g_free */
qsp_at(const QSPCallSite * callsite)646fe9959a2SEmilio G. Cota static char *qsp_at(const QSPCallSite *callsite)
647fe9959a2SEmilio G. Cota {
648fe9959a2SEmilio G. Cota     GString *s = g_string_new(NULL);
649fe9959a2SEmilio G. Cota     const char *shortened;
650fe9959a2SEmilio G. Cota 
651fe9959a2SEmilio G. Cota     /* remove the absolute path to qemu */
652fe9959a2SEmilio G. Cota     if (unlikely(strlen(callsite->file) < qsp_qemu_path_len)) {
653fe9959a2SEmilio G. Cota         shortened = callsite->file;
654fe9959a2SEmilio G. Cota     } else {
655fe9959a2SEmilio G. Cota         shortened = callsite->file + qsp_qemu_path_len;
656fe9959a2SEmilio G. Cota     }
657fe9959a2SEmilio G. Cota     g_string_append_printf(s, "%s:%u", shortened, callsite->line);
658fe9959a2SEmilio G. Cota     return g_string_free(s, FALSE);
659fe9959a2SEmilio G. Cota }
660fe9959a2SEmilio G. Cota 
661fe9959a2SEmilio G. Cota struct QSPReportEntry {
662fe9959a2SEmilio G. Cota     const void *obj;
663fe9959a2SEmilio G. Cota     char *callsite_at;
664fe9959a2SEmilio G. Cota     const char *typename;
665fe9959a2SEmilio G. Cota     double time_s;
666fe9959a2SEmilio G. Cota     double ns_avg;
667fe9959a2SEmilio G. Cota     uint64_t n_acqs;
668d557de4aSEmilio G. Cota     unsigned int n_objs;
669fe9959a2SEmilio G. Cota };
670fe9959a2SEmilio G. Cota typedef struct QSPReportEntry QSPReportEntry;
671fe9959a2SEmilio G. Cota 
672fe9959a2SEmilio G. Cota struct QSPReport {
673fe9959a2SEmilio G. Cota     QSPReportEntry *entries;
674fe9959a2SEmilio G. Cota     size_t n_entries;
675fe9959a2SEmilio G. Cota     size_t max_n_entries;
676fe9959a2SEmilio G. Cota };
677fe9959a2SEmilio G. Cota typedef struct QSPReport QSPReport;
678fe9959a2SEmilio G. Cota 
qsp_tree_report(gpointer key,gpointer value,gpointer udata)679fe9959a2SEmilio G. Cota static gboolean qsp_tree_report(gpointer key, gpointer value, gpointer udata)
680fe9959a2SEmilio G. Cota {
681fe9959a2SEmilio G. Cota     const QSPEntry *e = key;
682fe9959a2SEmilio G. Cota     QSPReport *report = udata;
683fe9959a2SEmilio G. Cota     QSPReportEntry *entry;
684fe9959a2SEmilio G. Cota 
685fe9959a2SEmilio G. Cota     if (report->n_entries == report->max_n_entries) {
686fe9959a2SEmilio G. Cota         return TRUE;
687fe9959a2SEmilio G. Cota     }
688fe9959a2SEmilio G. Cota     entry = &report->entries[report->n_entries];
689fe9959a2SEmilio G. Cota     report->n_entries++;
690fe9959a2SEmilio G. Cota 
691fe9959a2SEmilio G. Cota     entry->obj = e->callsite->obj;
692d557de4aSEmilio G. Cota     entry->n_objs = e->n_objs;
693fe9959a2SEmilio G. Cota     entry->callsite_at = qsp_at(e->callsite);
694fe9959a2SEmilio G. Cota     entry->typename = qsp_typenames[e->callsite->type];
695fe9959a2SEmilio G. Cota     entry->time_s = e->ns * 1e-9;
696fe9959a2SEmilio G. Cota     entry->n_acqs = e->n_acqs;
697fe9959a2SEmilio G. Cota     entry->ns_avg = e->n_acqs ? e->ns / e->n_acqs : 0;
698fe9959a2SEmilio G. Cota     return FALSE;
699fe9959a2SEmilio G. Cota }
700fe9959a2SEmilio G. Cota 
pr_report(const QSPReport * rep)701ac7ff4cfSMarkus Armbruster static void pr_report(const QSPReport *rep)
702fe9959a2SEmilio G. Cota {
703fe9959a2SEmilio G. Cota     char *dashes;
704fe9959a2SEmilio G. Cota     size_t max_len = 0;
705fe9959a2SEmilio G. Cota     int callsite_len = 0;
706fe9959a2SEmilio G. Cota     int callsite_rspace;
707fe9959a2SEmilio G. Cota     int n_dashes;
708fe9959a2SEmilio G. Cota     size_t i;
709fe9959a2SEmilio G. Cota 
710fe9959a2SEmilio G. Cota     /* find out the maximum length of all 'callsite' fields */
711fe9959a2SEmilio G. Cota     for (i = 0; i < rep->n_entries; i++) {
712fe9959a2SEmilio G. Cota         const QSPReportEntry *e = &rep->entries[i];
713fe9959a2SEmilio G. Cota         size_t len = strlen(e->callsite_at);
714fe9959a2SEmilio G. Cota 
715fe9959a2SEmilio G. Cota         if (len > max_len) {
716fe9959a2SEmilio G. Cota             max_len = len;
717fe9959a2SEmilio G. Cota         }
718fe9959a2SEmilio G. Cota     }
719fe9959a2SEmilio G. Cota 
720fe9959a2SEmilio G. Cota     callsite_len = MAX(max_len, strlen("Call site"));
721fe9959a2SEmilio G. Cota     /* white space to leave to the right of "Call site" */
722fe9959a2SEmilio G. Cota     callsite_rspace = callsite_len - strlen("Call site");
723fe9959a2SEmilio G. Cota 
724ac7ff4cfSMarkus Armbruster     qemu_printf("Type               Object  Call site%*s  Wait Time (s)  "
725fe9959a2SEmilio G. Cota                 "       Count  Average (us)\n", callsite_rspace, "");
726fe9959a2SEmilio G. Cota 
727fe9959a2SEmilio G. Cota     /* build a horizontal rule with dashes */
728fe9959a2SEmilio G. Cota     n_dashes = 79 + callsite_rspace;
729fe9959a2SEmilio G. Cota     dashes = g_malloc(n_dashes + 1);
730fe9959a2SEmilio G. Cota     memset(dashes, '-', n_dashes);
731fe9959a2SEmilio G. Cota     dashes[n_dashes] = '\0';
732ac7ff4cfSMarkus Armbruster     qemu_printf("%s\n", dashes);
733fe9959a2SEmilio G. Cota 
734fe9959a2SEmilio G. Cota     for (i = 0; i < rep->n_entries; i++) {
735fe9959a2SEmilio G. Cota         const QSPReportEntry *e = &rep->entries[i];
736d557de4aSEmilio G. Cota         GString *s = g_string_new(NULL);
737fe9959a2SEmilio G. Cota 
738d557de4aSEmilio G. Cota         g_string_append_printf(s, "%-9s  ", e->typename);
739d557de4aSEmilio G. Cota         if (e->n_objs > 1) {
740d557de4aSEmilio G. Cota             g_string_append_printf(s, "[%12u]", e->n_objs);
741d557de4aSEmilio G. Cota         } else {
742d557de4aSEmilio G. Cota             g_string_append_printf(s, "%14p", e->obj);
743d557de4aSEmilio G. Cota         }
744d557de4aSEmilio G. Cota         g_string_append_printf(s, "  %s%*s  %13.5f  %12" PRIu64 "  %12.2f\n",
745d557de4aSEmilio G. Cota                                e->callsite_at,
746d557de4aSEmilio G. Cota                                callsite_len - (int)strlen(e->callsite_at), "",
747d557de4aSEmilio G. Cota                                e->time_s, e->n_acqs, e->ns_avg * 1e-3);
748ac7ff4cfSMarkus Armbruster         qemu_printf("%s", s->str);
749d557de4aSEmilio G. Cota         g_string_free(s, TRUE);
750fe9959a2SEmilio G. Cota     }
751fe9959a2SEmilio G. Cota 
752ac7ff4cfSMarkus Armbruster     qemu_printf("%s\n", dashes);
753fe9959a2SEmilio G. Cota     g_free(dashes);
754fe9959a2SEmilio G. Cota }
755fe9959a2SEmilio G. Cota 
report_destroy(QSPReport * rep)756fe9959a2SEmilio G. Cota static void report_destroy(QSPReport *rep)
757fe9959a2SEmilio G. Cota {
758fe9959a2SEmilio G. Cota     size_t i;
759fe9959a2SEmilio G. Cota 
760fe9959a2SEmilio G. Cota     for (i = 0; i < rep->n_entries; i++) {
761fe9959a2SEmilio G. Cota         QSPReportEntry *e = &rep->entries[i];
762fe9959a2SEmilio G. Cota 
763fe9959a2SEmilio G. Cota         g_free(e->callsite_at);
764fe9959a2SEmilio G. Cota     }
765fe9959a2SEmilio G. Cota     g_free(rep->entries);
766fe9959a2SEmilio G. Cota }
767fe9959a2SEmilio G. Cota 
qsp_report(size_t max,enum QSPSortBy sort_by,bool callsite_coalesce)768ac7ff4cfSMarkus Armbruster void qsp_report(size_t max, enum QSPSortBy sort_by,
769ac7ff4cfSMarkus Armbruster                 bool callsite_coalesce)
770fe9959a2SEmilio G. Cota {
7710a22777cSEmilio G. Cota     GTree *tree = g_tree_new_full(qsp_tree_cmp, &sort_by, g_free, NULL);
772fe9959a2SEmilio G. Cota     QSPReport rep;
773fe9959a2SEmilio G. Cota 
774fe9959a2SEmilio G. Cota     qsp_init();
775fe9959a2SEmilio G. Cota 
776fe9959a2SEmilio G. Cota     rep.entries = g_new0(QSPReportEntry, max);
777fe9959a2SEmilio G. Cota     rep.n_entries = 0;
778fe9959a2SEmilio G. Cota     rep.max_n_entries = max;
779fe9959a2SEmilio G. Cota 
780d557de4aSEmilio G. Cota     qsp_mktree(tree, callsite_coalesce);
781fe9959a2SEmilio G. Cota     g_tree_foreach(tree, qsp_tree_report, &rep);
782fe9959a2SEmilio G. Cota     g_tree_destroy(tree);
783fe9959a2SEmilio G. Cota 
784ac7ff4cfSMarkus Armbruster     pr_report(&rep);
785fe9959a2SEmilio G. Cota     report_destroy(&rep);
786fe9959a2SEmilio G. Cota }
787996e8d9aSEmilio G. Cota 
qsp_snapshot_destroy(QSPSnapshot * snap)788996e8d9aSEmilio G. Cota static void qsp_snapshot_destroy(QSPSnapshot *snap)
789996e8d9aSEmilio G. Cota {
790996e8d9aSEmilio G. Cota     qht_iter(&snap->ht, qsp_ht_delete, NULL);
791996e8d9aSEmilio G. Cota     qht_destroy(&snap->ht);
792996e8d9aSEmilio G. Cota     g_free(snap);
793996e8d9aSEmilio G. Cota }
794996e8d9aSEmilio G. Cota 
qsp_reset(void)795996e8d9aSEmilio G. Cota void qsp_reset(void)
796996e8d9aSEmilio G. Cota {
797996e8d9aSEmilio G. Cota     QSPSnapshot *new = g_new(QSPSnapshot, 1);
798996e8d9aSEmilio G. Cota     QSPSnapshot *old;
799996e8d9aSEmilio G. Cota 
800996e8d9aSEmilio G. Cota     qsp_init();
801996e8d9aSEmilio G. Cota 
802996e8d9aSEmilio G. Cota     qht_init(&new->ht, qsp_entry_cmp, QSP_INITIAL_SIZE,
803996e8d9aSEmilio G. Cota              QHT_MODE_AUTO_RESIZE | QHT_MODE_RAW_MUTEXES);
804996e8d9aSEmilio G. Cota 
805996e8d9aSEmilio G. Cota     /* take a snapshot of the current state */
806996e8d9aSEmilio G. Cota     qht_iter(&qsp_ht, qsp_aggregate, &new->ht);
807996e8d9aSEmilio G. Cota 
808996e8d9aSEmilio G. Cota     /* replace the previous snapshot, if any */
809d73415a3SStefan Hajnoczi     old = qatomic_xchg(&qsp_snapshot, new);
810996e8d9aSEmilio G. Cota     if (old) {
811996e8d9aSEmilio G. Cota         call_rcu(old, qsp_snapshot_destroy, rcu);
812996e8d9aSEmilio G. Cota     }
813996e8d9aSEmilio G. Cota }
814