2 * Copyright (c) 2020 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 static void flame_disable(int sig
);
38 static void flame_collect(int ncpus
);
39 static void flame_doentry(int cpuid
, uint32_t idx
,
40 struct flame_graph_entry
*fge
);
42 static uint32_t *savewindex
;
43 static struct save_ctx symctx
;
46 flame_collect_loop(void)
52 size_t ncpus_size
= sizeof(ncpus
);
54 if (sysctlbyname("hw.ncpu", &ncpus
, &ncpus_size
, NULL
, 0) < 0) {
58 savewindex
= calloc(ncpus
, sizeof(*savewindex
));
60 read_symbols("/boot/kernel/kernel");
63 signal(SIGINT
, flame_disable
);
64 if (sysctlbyname("debug.flame_graph_enable",
65 NULL
, NULL
, &enable
, sizeof(enable
)) < 0) {
66 perror("debug.flame_graph_enable");
72 sysctlbyname("debug.flame_graph_sniff", NULL
, NULL
,
73 &sniff
, sizeof(sniff
));
83 flame_collect(int ncpus
)
85 struct flame_graph_pcpu
*fg
;
86 struct flame_graph_entry
*fge
;
87 struct flame_graph_entry
*scan
;
96 bytes
= sizeof(size_t) +
97 sizeof(struct flame_graph_pcpu
) +
98 sizeof(struct flame_graph_entry
) * FLAME_GRAPH_NENTRIES
;
102 if (sysctlbyname("debug.flame_graph_data", buf
, &bytes
, NULL
, 0) < 0) {
103 perror("debug.flame_graph_data");
108 for (n
= 0; n
< ncpus
; ++n
) {
109 if (bytes
< sizeof(size_t) + sizeof(*fg
))
111 fg
= (void *)((char *)ptr
+ sizeof(size_t));
112 bytes
-= sizeof(size_t) + sizeof(*fg
);
113 ptr
= (char *)ptr
+ sizeof(size_t) + sizeof(*fg
);
115 if (bytes
< sizeof(*fge
) * fg
->nentries
)
117 fge
= (void *)(fg
+ 1);
118 ptr
= (char *)ptr
+ sizeof(*fge
) * fg
->nentries
;
119 bytes
-= sizeof(*fge
) * fg
->nentries
;
121 rindex
= savewindex
[n
];
127 delta
= windex
- rindex
;
128 if (delta
>= fg
->nentries
)
129 rindex
= windex
- fg
->nentries
+ 1;
134 while (rindex
!= windex
) {
135 scan
= &fge
[rindex
% fg
->nentries
];
137 flame_doentry(n
, rindex
, scan
);
140 savewindex
[n
] = windex
;
146 flame_doentry(int cpuid
, uint32_t idx
, struct flame_graph_entry
*fge
)
150 for (i
= 0; i
< FLAME_GRAPH_FRAMES
; ++i
) {
151 if (fge
->rips
[i
] == 0)
155 printf("%03d/%03d ", cpuid
, idx
% 1000);
160 address_to_symbol((void *)fge
->rips
[i
], &symctx
));
169 flame_disable(int sig __unused
)
174 if (sysctlbyname("debug.flame_graph_enable",
175 NULL
, NULL
, &enable
, sizeof(enable
)) < 0) {
176 perror("debug.flame_graph_enable");