Skip to content

Commit 0f08586

Browse files
committed
util/async: add a human-readable name to BHs for debugging
It can be difficult to debug issues with BHs in production environments. Although BHs can usually be identified by looking up their ->cb() function pointer, this requires debug information for the program. It is also not possible to print human-readable diagnostics about BHs because they have no identifier. This patch adds a name to each BH. The name is not unique per instance but differentiates between cb() functions, which is usually enough. It's done by changing aio_bh_new() and friends to macros that stringify cb. The next patch will use the name field when reporting leaked BHs. Signed-off-by: Stefan Hajnoczi <[email protected]> Reviewed-by: Philippe Mathieu-Daudé <[email protected]> Message-Id: <[email protected]>
1 parent 711c041 commit 0f08586

File tree

5 files changed

+41
-9
lines changed

5 files changed

+41
-9
lines changed

include/block/aio.h

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,20 +291,45 @@ void aio_context_acquire(AioContext *ctx);
291291
/* Relinquish ownership of the AioContext. */
292292
void aio_context_release(AioContext *ctx);
293293

294+
/**
295+
* aio_bh_schedule_oneshot_full: Allocate a new bottom half structure that will
296+
* run only once and as soon as possible.
297+
*
298+
* @name: A human-readable identifier for debugging purposes.
299+
*/
300+
void aio_bh_schedule_oneshot_full(AioContext *ctx, QEMUBHFunc *cb, void *opaque,
301+
const char *name);
302+
294303
/**
295304
* aio_bh_schedule_oneshot: Allocate a new bottom half structure that will run
296305
* only once and as soon as possible.
306+
*
307+
* A convenience wrapper for aio_bh_schedule_oneshot_full() that uses cb as the
308+
* name string.
297309
*/
298-
void aio_bh_schedule_oneshot(AioContext *ctx, QEMUBHFunc *cb, void *opaque);
310+
#define aio_bh_schedule_oneshot(ctx, cb, opaque) \
311+
aio_bh_schedule_oneshot_full((ctx), (cb), (opaque), (stringify(cb)))
299312

300313
/**
301-
* aio_bh_new: Allocate a new bottom half structure.
314+
* aio_bh_new_full: Allocate a new bottom half structure.
302315
*
303316
* Bottom halves are lightweight callbacks whose invocation is guaranteed
304317
* to be wait-free, thread-safe and signal-safe. The #QEMUBH structure
305318
* is opaque and must be allocated prior to its use.
319+
*
320+
* @name: A human-readable identifier for debugging purposes.
321+
*/
322+
QEMUBH *aio_bh_new_full(AioContext *ctx, QEMUBHFunc *cb, void *opaque,
323+
const char *name);
324+
325+
/**
326+
* aio_bh_new: Allocate a new bottom half structure
327+
*
328+
* A convenience wrapper for aio_bh_new_full() that uses the cb as the name
329+
* string.
306330
*/
307-
QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque);
331+
#define aio_bh_new(ctx, cb, opaque) \
332+
aio_bh_new_full((ctx), (cb), (opaque), (stringify(cb)))
308333

309334
/**
310335
* aio_notify: Force processing of pending events.

include/qemu/main-loop.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,9 @@ void qemu_cond_timedwait_iothread(QemuCond *cond, int ms);
294294

295295
void qemu_fd_register(int fd);
296296

297-
QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque);
297+
#define qemu_bh_new(cb, opaque) \
298+
qemu_bh_new_full((cb), (opaque), (stringify(cb)))
299+
QEMUBH *qemu_bh_new_full(QEMUBHFunc *cb, void *opaque, const char *name);
298300
void qemu_bh_schedule_idle(QEMUBH *bh);
299301

300302
enum {

tests/unit/ptimer-test-stubs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ int64_t qemu_clock_deadline_ns_all(QEMUClockType type, int attr_mask)
108108
return deadline;
109109
}
110110

111-
QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
111+
QEMUBH *qemu_bh_new_full(QEMUBHFunc *cb, void *opaque, const char *name)
112112
{
113113
QEMUBH *bh = g_new(QEMUBH, 1);
114114

util/async.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ enum {
5757

5858
struct QEMUBH {
5959
AioContext *ctx;
60+
const char *name;
6061
QEMUBHFunc *cb;
6162
void *opaque;
6263
QSLIST_ENTRY(QEMUBH) next;
@@ -107,26 +108,30 @@ static QEMUBH *aio_bh_dequeue(BHList *head, unsigned *flags)
107108
return bh;
108109
}
109110

110-
void aio_bh_schedule_oneshot(AioContext *ctx, QEMUBHFunc *cb, void *opaque)
111+
void aio_bh_schedule_oneshot_full(AioContext *ctx, QEMUBHFunc *cb,
112+
void *opaque, const char *name)
111113
{
112114
QEMUBH *bh;
113115
bh = g_new(QEMUBH, 1);
114116
*bh = (QEMUBH){
115117
.ctx = ctx,
116118
.cb = cb,
117119
.opaque = opaque,
120+
.name = name,
118121
};
119122
aio_bh_enqueue(bh, BH_SCHEDULED | BH_ONESHOT);
120123
}
121124

122-
QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque)
125+
QEMUBH *aio_bh_new_full(AioContext *ctx, QEMUBHFunc *cb, void *opaque,
126+
const char *name)
123127
{
124128
QEMUBH *bh;
125129
bh = g_new(QEMUBH, 1);
126130
*bh = (QEMUBH){
127131
.ctx = ctx,
128132
.cb = cb,
129133
.opaque = opaque,
134+
.name = name,
130135
};
131136
return bh;
132137
}

util/main-loop.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,9 +544,9 @@ void main_loop_wait(int nonblocking)
544544

545545
/* Functions to operate on the main QEMU AioContext. */
546546

547-
QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
547+
QEMUBH *qemu_bh_new_full(QEMUBHFunc *cb, void *opaque, const char *name)
548548
{
549-
return aio_bh_new(qemu_aio_context, cb, opaque);
549+
return aio_bh_new_full(qemu_aio_context, cb, opaque, name);
550550
}
551551

552552
/*

0 commit comments

Comments
 (0)