xref: /openbsd-src/share/btrace/runqlat.bt (revision 679d3e5f993b00a49a532b1d4cde87917aa340fc)
1/*	$OpenBSD: runqlat.bt,v 1.3 2022/05/01 20:23:11 bluhm Exp $	*/
2
3/*
4 * runqlat.bt	Measure run queue latency (aka scheduler latency). OpenBSD.
5 *
6 * This measures the time from enqueue to on-cpu, for the same thread.
7 *
8 * WARNING: This traces scheduler functions, which can incur high overhead.
9 * This is not suitable for 24x7 monitoring.
10 *
11 * Copyright (c) 2014 Brendan Gregg. All rights reserved.
12 * Copyright (c) 2021 Martin Pieuchot. All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 *    notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 *    notice, this list of conditions and the following disclaimer in the
21 *    documentation and/or other materials provided with the distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * 19-Jun-2014	Brendan Gregg	Created this.
36 * 08-Sep-2021	Martin Pieuchot Ported this to OpenBSD's btrace(8)
37 */
38
39/*
40 * Work around multiple enqueue/dequeue cycles due to priority changes
41 * before executing by registering only the first time a thread is put
42 * on a scheduler queue.
43 */
44tracepoint:sched:enqueue
45/@ts[arg0] == 0/
46{
47	/*
48	 * For the enqueue and dequeue probes, arg0 is the thread ID.
49	 */
50	@ts[arg0] = nsecs
51}
52
53tracepoint:sched:on__cpu
54/@ts[tid]/
55{
56	$usec = (nsecs - @ts[tid]) / 1000;
57	@max_usec[0] = max($usec);
58	@dist_usec = hist($usec);
59	delete(@ts[tid]);
60}
61
62interval:hz:1
63{
64	time("%H:%M:%S  ");
65	printf("Run queue latency (us):\n");
66	print(@dist_usec);
67	printf("Max run queue latency: %d ms\n", @max_usec[0] / 1000);
68	clear(@dist_usec); clear(@max_usec);
69}
70
71END
72{
73	clear(@ts);
74	time("%H:%M:%S  ");
75	printf("Run queue latency (us):\n");
76	print(@dist_usec);
77	printf("Max run queue latency: %d ms\n", @max_usec[0] / 1000);
78	clear(@dist_usec); clear(@max_usec);
79}
80