sys/kern/kern_collect.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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | /* * Copyright (c) 2017 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. * * 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. */ /* * Collects general statistics on a 10-second interval. */ #include <sys/param.h> #include <sys/systm.h> #include <sys/kernel.h> #include <sys/proc.h> #include <sys/kinfo.h> #include <sys/queue.h> #include <sys/sysctl.h> #include <sys/kthread.h> #include <machine/cpu.h> #include <sys/lock.h> #include <sys/spinlock.h> #include <sys/kcollect.h> #include <sys/malloc.h> #include <sys/thread2.h> #include <sys/spinlock2.h> #include <vm/vm.h> #include <vm/vm_param.h> #include <vm/vm_kern.h> #include <vm/vm_object.h> #include <vm/vm_page.h> #include <vm/vm_map.h> #include <vm/vm_pager.h> #include <vm/vm_extern.h> #include <machine/stdarg.h> #include <machine/smp.h> #include <machine/clock.h> static uint32_t kcollect_samples = -1; /* 0 to disable */ TUNABLE_INT("kern.collect_samples", &kcollect_samples); SYSCTL_UINT(_kern, OID_AUTO, collect_samples, CTLFLAG_RD, &kcollect_samples, 0, "number of 10-second samples"); static uint64_t kcollect_index; static const char *kcollect_slots[KCOLLECT_ENTRIES]; static kcallback_t kcollect_callback[KCOLLECT_ENTRIES]; static kcollect_t kcollect_scale; static kcollect_t *kcollect_ary; static struct lock kcollect_lock = LOCK_INITIALIZER("kcolk", 0, 0); MALLOC_DEFINE(M_KCOLLECT, "kcollect", "kcollect array"); int kcollect_register(int which, const char *id, kcallback_t func, uint64_t scale) { int n; lockmgr(&kcollect_lock, LK_EXCLUSIVE); if (which >= 0) { n = which; } else { for (n = KCOLLECT_DYNAMIC_START; n < KCOLLECT_ENTRIES; ++n) { if (kcollect_slots[n] == NULL) break; } } if (n < 0 || n >= KCOLLECT_ENTRIES) { n = -1; } else { kcollect_slots[n] = id; kcollect_callback[n] = func; kcollect_scale.data[n] = scale; } lockmgr(&kcollect_lock, LK_RELEASE); return n; } void kcollect_unregister(int n) { lockmgr(&kcollect_lock, LK_EXCLUSIVE); if (n >= 0 && n < KCOLLECT_ENTRIES) { kcollect_slots[n] = NULL; kcollect_callback[n] = NULL; } lockmgr(&kcollect_lock, LK_RELEASE); } /* * Typically called by a rollup function in the callback from the * collection thread. Not usually called ad-hoc. This allows a * subsystem to register several collection ids but only one callback * which populates all of them. */ void kcollect_setvalue(int n, uint64_t value) { uint32_t i; if (n >= 0 && n < KCOLLECT_ENTRIES) { i = kcollect_index % kcollect_samples; kcollect_ary[i].data[n] = value; } } /* * Callback to change scale adjustment, if necessary. Certain statistics * have scale info available (such as KCOLLECT_SWAPANO and SWAPCAC). */ void kcollect_setscale(int n, uint64_t value) { if (n >= 0 && n < KCOLLECT_ENTRIES) { kcollect_scale.data[n] = value; } } static void kcollect_thread(void *dummy) { uint32_t i; int n; for (;;) { lockmgr(&kcollect_lock, LK_EXCLUSIVE); i = kcollect_index % kcollect_samples; bzero(&kcollect_ary[i], sizeof(kcollect_ary[i])); crit_enter(); kcollect_ary[i].ticks = ticks; getmicrotime(&kcollect_ary[i].realtime); crit_exit(); for (n = 0; n < KCOLLECT_ENTRIES; ++n) { if (kcollect_callback[n]) { kcollect_ary[i].data[n] = kcollect_callback[n](n); } } cpu_sfence(); ++kcollect_index; lockmgr(&kcollect_lock, LK_RELEASE); tsleep(&dummy, 0, "sleep", hz * KCOLLECT_INTERVAL); } } /* * No requirements. */ static int sysctl_kcollect_data(SYSCTL_HANDLER_ARGS) { int error; uint32_t i; uint32_t start; uint64_t count; kcollect_t scale; kcollect_t id; if (kcollect_samples == (uint32_t)-1 || kcollect_samples == 0) { return EINVAL; } error = 0; count = kcollect_index; start = count % kcollect_samples; if (count >= kcollect_samples) count = kcollect_samples - 1; /* * Sizing request */ if (req->oldptr == NULL) { error = SYSCTL_OUT(req, 0, sizeof(kcollect_t) * (count + 2)); return error; } /* * Output request. We output a scale record, a string record, and * N collection records. The strings in the string record can be * up to 8 characters long, and if a string is 8 characters long it * will not be zero-terminated. * * The low byte of the scale record specifies the format. To get * the scale value shift right by 8. */ if (kcollect_ary == NULL) return ENOTSUP; lockmgr(&kcollect_lock, LK_EXCLUSIVE); scale = kcollect_scale; scale.ticks = ticks; scale.hz = hz; bzero(&id, sizeof(id)); for (i = 0; i < KCOLLECT_ENTRIES; ++i) { if (kcollect_slots[i]) { char *ptr = (char *)&id.data[i]; size_t len = strlen(kcollect_slots[i]); if (len > sizeof(id.data[0])) len = sizeof(id.data[0]); bcopy(kcollect_slots[i], ptr, len); } } lockmgr(&kcollect_lock, LK_RELEASE); error = SYSCTL_OUT(req, &scale, sizeof(scale)); if (error == 0) error = SYSCTL_OUT(req, &id, sizeof(id)); /* * Start at the current entry (not yet populated) and work * backwards. This allows callers of the sysctl to acquire * a lesser amount of data aligned to the most recent side of * the array. */ i = start; while (count) { if (req->oldlen - req->oldidx < sizeof(kcollect_t)) break; if (i == 0) i = kcollect_samples - 1; else --i; error = SYSCTL_OUT(req, &kcollect_ary[i], sizeof(kcollect_t)); if (error) break; --count; } return error; } SYSCTL_PROC(_kern, OID_AUTO, collect_data, CTLFLAG_RD | CTLTYPE_STRUCT, 0, 0, sysctl_kcollect_data, "S,kcollect", "Dump collected statistics"); static void kcollect_thread_init(void) { thread_t td = NULL; /* * Autosize sample retention (10 second interval) */ if ((int)kcollect_samples < 0) { if (kmem_lim_size() < 1024) kcollect_samples = 1024; else kcollect_samples = 8192; } if (kcollect_samples) { kcollect_ary = kmalloc(kcollect_samples * sizeof(kcollect_t), M_KCOLLECT, M_WAITOK | M_ZERO); lwkt_create(kcollect_thread, NULL, &td, NULL, 0, 0, "kcollect"); } } SYSINIT(kcol, SI_SUB_HELPER_THREADS, SI_ORDER_ANY, kcollect_thread_init, 0); |