sys/platform/pc64/x86_64/mp_flame.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | /* * Copyright (c) 2020 The DragonFly Project. All rights reserved. * * This code is derived from software contributed to The DragonFly Project * by Matthew Dillon <dillon@backplane.com> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. Neither the name of The DragonFly Project nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific, prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include "opt_cpu.h" #include <sys/param.h> #include <sys/systm.h> #include <sys/kernel.h> #include <sys/sysctl.h> #include <sys/malloc.h> #include <sys/caps.h> #include <sys/flame_graph.h> #include <sys/thread2.h> #include <machine/atomic.h> #include <machine/cpufunc.h> #include <machine/cputypes.h> #include <machine/psl.h> #include <machine/segments.h> #include <machine/tss.h> #include <machine/specialreg.h> #include <machine/globaldata.h> #include <machine/md_var.h> /* setidt() */ __read_frequently static struct flame_graph_pcpu *flame_graph_array; __read_frequently static int flame_graph_enable; SYSCTL_INT(_debug, OID_AUTO, flame_graph_enable, CTLFLAG_RW, &flame_graph_enable, 0, "Collect data for flame graphs"); SYSCTL_LONG(_debug, OID_AUTO, flame_graph_array, CTLFLAG_RD, &flame_graph_array, 0, "Collect data for flame graphs"); static MALLOC_DEFINE(M_FLAME, "flame_graphs", "Flame Graphs"); static void hard_sniff_init(void *arg) { struct flame_graph_pcpu *fg; struct flame_graph_entry *fge; int n; flame_graph_array = kmalloc(sizeof(*fg) * ncpus, M_FLAME, M_WAITOK | M_CACHEALIGN | M_ZERO); for (n = 0; n < ncpus; ++n) { fge = kmalloc(sizeof(*fge) * FLAME_GRAPH_NENTRIES, M_FLAME, M_WAITOK | M_CACHEALIGN | M_ZERO); fg = &flame_graph_array[n]; fg->nentries = FLAME_GRAPH_NENTRIES; fg->fge = fge; } } SYSINIT(swi_vm_setup, SI_BOOT2_MACHDEP, SI_ORDER_ANY, hard_sniff_init, NULL); /* * Xsniff vector calls into here * * WARNING! This code ignores critical sections! The system can be * in any state. The only thing we safely have access to * is the gd. */ void hard_sniff(struct trapframe *tf) { globaldata_t gd = mycpu; thread_t td; char *top; char *bot; char *rbp; char *rip; struct flame_graph_pcpu *fg; struct flame_graph_entry *fge; int n; /* * systat -pv 1 sampling */ gd->gd_sample_pc = (void *)(intptr_t)tf->tf_rip; gd->gd_sample_sp = (void *)(intptr_t)tf->tf_rsp; /* * Flame graph sampling, require %rbp (frame pointer) chaining. * Attempt to follow the chain and record what we believe are * %rip addresses. */ if (flame_graph_enable == 0) return; td = gd->gd_curthread; if (td == NULL) return; bot = (char *)td->td_kstack + PAGE_SIZE; /* skip guard */ top = (char *)td->td_kstack + td->td_kstack_size; if (bot >= top) return; fg = &flame_graph_array[gd->gd_cpuid]; fge = &fg->fge[fg->windex % FLAME_GRAPH_NENTRIES]; rip = (char *)(intptr_t)tf->tf_rip; fge->rips[0] = (intptr_t)rip; rbp = (char *)(intptr_t)tf->tf_rbp; for (n = 1; n < FLAME_GRAPH_FRAMES - 1; ++n) { if (rbp < bot || rbp > top - 8 || ((intptr_t)rbp & 7)) break; fge->rips[n] = (intptr_t)*(char **)(rbp + 8); if (*(char **)rbp <= rbp) break; rbp = *(char **)rbp; } fge->rips[n] = 0; cpu_sfence(); ++fg->windex; } static int sysctl_flame_graph_data(SYSCTL_HANDLER_ARGS) { int error; int n; size_t ebytes; error = caps_priv_check_self(SYSCAP_RESTRICTEDROOT); if (error) return error; if (flame_graph_array == NULL) return EOPNOTSUPP; ebytes = sizeof(struct flame_graph_pcpu) + sizeof(struct flame_graph_entry); for (n = 0; n < ncpus && error == 0; ++n) { error = SYSCTL_OUT(req, &ebytes, sizeof(ebytes)); if (error == 0) error = SYSCTL_OUT(req, flame_graph_array + n, sizeof(*flame_graph_array)); if (error == 0) error = SYSCTL_OUT(req, flame_graph_array[n].fge, sizeof(*flame_graph_array->fge) * FLAME_GRAPH_NENTRIES); } return error; } SYSCTL_PROC(_debug, OID_AUTO, flame_graph_data, (CTLTYPE_OPAQUE|CTLFLAG_RD), 0, 0, sysctl_flame_graph_data, "S,flames", "Flame Graph Data"); static int sysctl_flame_graph_sniff(SYSCTL_HANDLER_ARGS) { int error; error = caps_priv_check_self(SYSCAP_RESTRICTEDROOT); if (error) return error; if (flame_graph_enable == 0) return EINVAL; if (req->newptr) smp_sniff(); return(SYSCTL_OUT(req, &error, sizeof(int))); } SYSCTL_PROC(_debug, OID_AUTO, flame_graph_sniff, (CTLTYPE_UINT|CTLFLAG_RW), 0, 0, sysctl_flame_graph_sniff, "IU", "Flame Graph Poll"); |