1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2005-2007 Joseph Koshy 5 * Copyright (c) 2007 The FreeBSD Foundation 6 * Copyright (c) 2018 Matthew Macy 7 * All rights reserved. 8 * 9 * Portions of this software were developed by A. Joseph Koshy under 10 * sponsorship from the FreeBSD Foundation and Google, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 */ 34 35 /* 36 * Logging code for hwpmc(4) 37 */ 38 39 #include <sys/param.h> 40 #include <sys/capsicum.h> 41 #include <sys/domainset.h> 42 #include <sys/file.h> 43 #include <sys/kernel.h> 44 #include <sys/kthread.h> 45 #include <sys/lock.h> 46 #include <sys/module.h> 47 #include <sys/mutex.h> 48 #include <sys/pmc.h> 49 #include <sys/pmckern.h> 50 #include <sys/pmclog.h> 51 #include <sys/proc.h> 52 #include <sys/sched.h> 53 #include <sys/signalvar.h> 54 #include <sys/smp.h> 55 #include <sys/syscallsubr.h> 56 #include <sys/sysctl.h> 57 #include <sys/systm.h> 58 #include <sys/uio.h> 59 #include <sys/unistd.h> 60 #include <sys/vnode.h> 61 #include <sys/syslog.h> 62 63 #if defined(__i386__) || defined(__amd64__) 64 #include <machine/clock.h> 65 #endif 66 67 #define curdomain PCPU_GET(domain) 68 69 /* 70 * Sysctl tunables 71 */ 72 73 SYSCTL_DECL(_kern_hwpmc); 74 75 /* 76 * kern.hwpmc.logbuffersize -- size of the per-cpu owner buffers. 77 */ 78 79 static int pmclog_buffer_size = PMC_LOG_BUFFER_SIZE; 80 SYSCTL_INT(_kern_hwpmc, OID_AUTO, logbuffersize, CTLFLAG_RDTUN, 81 &pmclog_buffer_size, 0, "size of log buffers in kilobytes"); 82 83 /* 84 * kern.hwpmc.nbuffers_pcpu -- number of global log buffers 85 */ 86 87 static int pmc_nlogbuffers_pcpu = PMC_NLOGBUFFERS_PCPU; 88 SYSCTL_INT(_kern_hwpmc, OID_AUTO, nbuffers_pcpu, CTLFLAG_RDTUN, 89 &pmc_nlogbuffers_pcpu, 0, "number of log buffers per cpu"); 90 91 /* 92 * Global log buffer list and associated spin lock. 93 */ 94 95 static struct mtx pmc_kthread_mtx; /* sleep lock */ 96 97 #define PMCLOG_INIT_BUFFER_DESCRIPTOR(D, buf, domain) do { \ 98 (D)->plb_fence = ((char *)(buf)) + 1024 * pmclog_buffer_size; \ 99 (D)->plb_base = (D)->plb_ptr = ((char *)(buf)); \ 100 (D)->plb_domain = domain; \ 101 } while (0) 102 103 #define PMCLOG_RESET_BUFFER_DESCRIPTOR(D) do { \ 104 (D)->plb_ptr = (D)->plb_base; \ 105 } while (0) 106 107 /* 108 * Log file record constructors. 109 */ 110 #define _PMCLOG_TO_HEADER(T, L) \ 111 ((PMCLOG_HEADER_MAGIC << 24) | (T << 16) | ((L) & 0xFFFF)) 112 113 /* reserve LEN bytes of space and initialize the entry header */ 114 #define _PMCLOG_RESERVE_SAFE(PO, TYPE, LEN, ACTION, TSC) do { \ 115 uint32_t *_le; \ 116 int _len = roundup((LEN), sizeof(uint32_t)); \ 117 struct pmclog_header *ph; \ 118 \ 119 if ((_le = pmclog_reserve((PO), _len)) == NULL) { \ 120 ACTION; \ 121 } \ 122 ph = (struct pmclog_header *)_le; \ 123 ph->pl_header =_PMCLOG_TO_HEADER(TYPE,_len); \ 124 ph->pl_tsc = (TSC); \ 125 _le += sizeof(*ph) / 4 /* skip over timestamp */ 126 127 /* reserve LEN bytes of space and initialize the entry header */ 128 #define _PMCLOG_RESERVE(PO, TYPE, LEN, ACTION) do { \ 129 uint32_t *_le; \ 130 int _len = roundup((LEN), sizeof(uint32_t)); \ 131 uint64_t tsc; \ 132 struct pmclog_header *ph; \ 133 \ 134 tsc = pmc_rdtsc(); \ 135 spinlock_enter(); \ 136 if ((_le = pmclog_reserve((PO), _len)) == NULL) { \ 137 spinlock_exit(); \ 138 ACTION; \ 139 } \ 140 ph = (struct pmclog_header *)_le; \ 141 ph->pl_header =_PMCLOG_TO_HEADER(TYPE,_len); \ 142 ph->pl_tsc = tsc; \ 143 _le += sizeof(*ph) / 4 /* skip over timestamp */ 144 145 #define PMCLOG_RESERVE_SAFE(P, T, L, TSC) \ 146 _PMCLOG_RESERVE_SAFE(P, T, L, return, TSC) 147 #define PMCLOG_RESERVE(P,T,L) \ 148 _PMCLOG_RESERVE(P, T, L, return) 149 #define PMCLOG_RESERVE_WITH_ERROR(P, T, L) \ 150 _PMCLOG_RESERVE(P, T, L, error = ENOMEM; goto error) 151 152 #define PMCLOG_EMIT32(V) do { *_le++ = (V); } while (0) 153 #define PMCLOG_EMIT64(V) do { \ 154 *_le++ = (uint32_t) ((V) & 0xFFFFFFFF); \ 155 *_le++ = (uint32_t) (((V) >> 32) & 0xFFFFFFFF); \ 156 } while (0) 157 158 159 /* Emit a string. Caution: does NOT update _le, so needs to be last */ 160 #define PMCLOG_EMITSTRING(S,L) do { \ 161 bcopy((S), _le, (L)); \ 162 } while (0) 163 #define PMCLOG_EMITNULLSTRING(L) do { \ 164 bzero(_le, (L)); \ 165 } while (0) 166 167 #define PMCLOG_DESPATCH_SAFE(PO) \ 168 pmclog_release((PO)); \ 169 } while (0) 170 171 #define PMCLOG_DESPATCH_SCHED_LOCK(PO) \ 172 pmclog_release_flags((PO), 0); \ 173 } while (0) 174 175 #define PMCLOG_DESPATCH(PO) \ 176 pmclog_release((PO)); \ 177 spinlock_exit(); \ 178 } while (0) 179 180 #define PMCLOG_DESPATCH_SYNC(PO) \ 181 pmclog_schedule_io((PO), 1); \ 182 spinlock_exit(); \ 183 } while (0) 184 185 #define TSDELTA 4 186 /* 187 * Assertions about the log file format. 188 */ 189 CTASSERT(sizeof(struct pmclog_callchain) == 7*4 + TSDELTA + 190 PMC_CALLCHAIN_DEPTH_MAX*sizeof(uintfptr_t)); 191 CTASSERT(sizeof(struct pmclog_closelog) == 3*4 + TSDELTA); 192 CTASSERT(sizeof(struct pmclog_dropnotify) == 3*4 + TSDELTA); 193 CTASSERT(sizeof(struct pmclog_map_in) == PATH_MAX + TSDELTA + 194 5*4 + sizeof(uintfptr_t)); 195 CTASSERT(offsetof(struct pmclog_map_in,pl_pathname) == 196 5*4 + TSDELTA + sizeof(uintfptr_t)); 197 CTASSERT(sizeof(struct pmclog_map_out) == 5*4 + 2*sizeof(uintfptr_t) + TSDELTA); 198 CTASSERT(sizeof(struct pmclog_pmcallocate) == 9*4 + TSDELTA); 199 CTASSERT(sizeof(struct pmclog_pmcattach) == 5*4 + PATH_MAX + TSDELTA); 200 CTASSERT(offsetof(struct pmclog_pmcattach,pl_pathname) == 5*4 + TSDELTA); 201 CTASSERT(sizeof(struct pmclog_pmcdetach) == 5*4 + TSDELTA); 202 CTASSERT(sizeof(struct pmclog_proccsw) == 7*4 + 8 + TSDELTA); 203 CTASSERT(sizeof(struct pmclog_procexec) == 5*4 + PATH_MAX + 204 2*sizeof(uintptr_t) + TSDELTA); 205 CTASSERT(offsetof(struct pmclog_procexec,pl_pathname) == 5*4 + TSDELTA + 206 2*sizeof(uintptr_t)); 207 CTASSERT(sizeof(struct pmclog_procexit) == 5*4 + 8 + TSDELTA); 208 CTASSERT(sizeof(struct pmclog_procfork) == 5*4 + TSDELTA); 209 CTASSERT(sizeof(struct pmclog_sysexit) == 6*4); 210 CTASSERT(sizeof(struct pmclog_userdata) == 6*4); 211 212 /* 213 * Log buffer structure 214 */ 215 216 struct pmclog_buffer { 217 TAILQ_ENTRY(pmclog_buffer) plb_next; 218 char *plb_base; 219 char *plb_ptr; 220 char *plb_fence; 221 uint16_t plb_domain; 222 } __aligned(CACHE_LINE_SIZE); 223 224 /* 225 * Prototypes 226 */ 227 228 static int pmclog_get_buffer(struct pmc_owner *po); 229 static void pmclog_loop(void *arg); 230 static void pmclog_release(struct pmc_owner *po); 231 static uint32_t *pmclog_reserve(struct pmc_owner *po, int length); 232 static void pmclog_schedule_io(struct pmc_owner *po, int wakeup); 233 static void pmclog_schedule_all(struct pmc_owner *po); 234 static void pmclog_stop_kthread(struct pmc_owner *po); 235 236 /* 237 * Helper functions 238 */ 239 240 static inline void 241 pmc_plb_rele_unlocked(struct pmclog_buffer *plb) 242 { 243 TAILQ_INSERT_HEAD(&pmc_dom_hdrs[plb->plb_domain]->pdbh_head, plb, plb_next); 244 } 245 246 static inline void 247 pmc_plb_rele(struct pmclog_buffer *plb) 248 { 249 mtx_lock_spin(&pmc_dom_hdrs[plb->plb_domain]->pdbh_mtx); 250 pmc_plb_rele_unlocked(plb); 251 mtx_unlock_spin(&pmc_dom_hdrs[plb->plb_domain]->pdbh_mtx); 252 } 253 254 /* 255 * Get a log buffer 256 */ 257 static int 258 pmclog_get_buffer(struct pmc_owner *po) 259 { 260 struct pmclog_buffer *plb; 261 int domain; 262 263 KASSERT(po->po_curbuf[curcpu] == NULL, 264 ("[pmclog,%d] po=%p current buffer still valid", __LINE__, po)); 265 266 domain = curdomain; 267 MPASS(pmc_dom_hdrs[domain]); 268 mtx_lock_spin(&pmc_dom_hdrs[domain]->pdbh_mtx); 269 if ((plb = TAILQ_FIRST(&pmc_dom_hdrs[domain]->pdbh_head)) != NULL) 270 TAILQ_REMOVE(&pmc_dom_hdrs[domain]->pdbh_head, plb, plb_next); 271 mtx_unlock_spin(&pmc_dom_hdrs[domain]->pdbh_mtx); 272 273 PMCDBG2(LOG,GTB,1, "po=%p plb=%p", po, plb); 274 275 #ifdef HWPMC_DEBUG 276 if (plb) 277 KASSERT(plb->plb_ptr == plb->plb_base && 278 plb->plb_base < plb->plb_fence, 279 ("[pmclog,%d] po=%p buffer invariants: ptr=%p " 280 "base=%p fence=%p", __LINE__, po, plb->plb_ptr, 281 plb->plb_base, plb->plb_fence)); 282 #endif 283 284 po->po_curbuf[curcpu] = plb; 285 286 /* update stats */ 287 counter_u64_add(pmc_stats.pm_buffer_requests, 1); 288 if (plb == NULL) 289 counter_u64_add(pmc_stats.pm_buffer_requests_failed, 1); 290 291 return (plb ? 0 : ENOMEM); 292 } 293 294 struct pmclog_proc_init_args { 295 struct proc *kthr; 296 struct pmc_owner *po; 297 bool exit; 298 bool acted; 299 }; 300 301 int 302 pmclog_proc_create(struct thread *td, void **handlep) 303 { 304 struct pmclog_proc_init_args *ia; 305 int error; 306 307 ia = malloc(sizeof(*ia), M_TEMP, M_WAITOK | M_ZERO); 308 error = kproc_create(pmclog_loop, ia, &ia->kthr, 309 RFHIGHPID, 0, "hwpmc: proc(%d)", td->td_proc->p_pid); 310 if (error == 0) 311 *handlep = ia; 312 return (error); 313 } 314 315 void 316 pmclog_proc_ignite(void *handle, struct pmc_owner *po) 317 { 318 struct pmclog_proc_init_args *ia; 319 320 ia = handle; 321 mtx_lock(&pmc_kthread_mtx); 322 MPASS(!ia->acted); 323 MPASS(ia->po == NULL); 324 MPASS(!ia->exit); 325 MPASS(ia->kthr != NULL); 326 if (po == NULL) { 327 ia->exit = true; 328 } else { 329 ia->po = po; 330 KASSERT(po->po_kthread == NULL, 331 ("[pmclog,%d] po=%p kthread (%p) already present", 332 __LINE__, po, po->po_kthread)); 333 po->po_kthread = ia->kthr; 334 } 335 wakeup(ia); 336 while (!ia->acted) 337 msleep(ia, &pmc_kthread_mtx, PWAIT, "pmclogw", 0); 338 mtx_unlock(&pmc_kthread_mtx); 339 free(ia, M_TEMP); 340 } 341 342 /* 343 * Log handler loop. 344 * 345 * This function is executed by each pmc owner's helper thread. 346 */ 347 static void 348 pmclog_loop(void *arg) 349 { 350 struct pmclog_proc_init_args *ia; 351 struct pmc_owner *po; 352 struct pmclog_buffer *lb; 353 struct proc *p; 354 struct ucred *ownercred; 355 struct ucred *mycred; 356 struct thread *td; 357 sigset_t unb; 358 struct uio auio; 359 struct iovec aiov; 360 size_t nbytes; 361 int error; 362 363 td = curthread; 364 365 SIGEMPTYSET(unb); 366 SIGADDSET(unb, SIGHUP); 367 (void)kern_sigprocmask(td, SIG_UNBLOCK, &unb, NULL, 0); 368 369 ia = arg; 370 MPASS(ia->kthr == curproc); 371 MPASS(!ia->acted); 372 mtx_lock(&pmc_kthread_mtx); 373 while (ia->po == NULL && !ia->exit) 374 msleep(ia, &pmc_kthread_mtx, PWAIT, "pmclogi", 0); 375 if (ia->exit) { 376 ia->acted = true; 377 wakeup(ia); 378 mtx_unlock(&pmc_kthread_mtx); 379 kproc_exit(0); 380 } 381 MPASS(ia->po != NULL); 382 po = ia->po; 383 ia->acted = true; 384 wakeup(ia); 385 mtx_unlock(&pmc_kthread_mtx); 386 ia = NULL; 387 388 p = po->po_owner; 389 mycred = td->td_ucred; 390 391 PROC_LOCK(p); 392 ownercred = crhold(p->p_ucred); 393 PROC_UNLOCK(p); 394 395 PMCDBG2(LOG,INI,1, "po=%p kt=%p", po, po->po_kthread); 396 KASSERT(po->po_kthread == curthread->td_proc, 397 ("[pmclog,%d] proc mismatch po=%p po/kt=%p curproc=%p", __LINE__, 398 po, po->po_kthread, curthread->td_proc)); 399 400 lb = NULL; 401 402 403 /* 404 * Loop waiting for I/O requests to be added to the owner 405 * struct's queue. The loop is exited when the log file 406 * is deconfigured. 407 */ 408 409 mtx_lock(&pmc_kthread_mtx); 410 411 for (;;) { 412 413 /* check if we've been asked to exit */ 414 if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0) 415 break; 416 417 if (lb == NULL) { /* look for a fresh buffer to write */ 418 mtx_lock_spin(&po->po_mtx); 419 if ((lb = TAILQ_FIRST(&po->po_logbuffers)) == NULL) { 420 mtx_unlock_spin(&po->po_mtx); 421 422 /* No more buffers and shutdown required. */ 423 if (po->po_flags & PMC_PO_SHUTDOWN) 424 break; 425 426 (void) msleep(po, &pmc_kthread_mtx, PWAIT, 427 "pmcloop", 250); 428 continue; 429 } 430 431 TAILQ_REMOVE(&po->po_logbuffers, lb, plb_next); 432 mtx_unlock_spin(&po->po_mtx); 433 } 434 435 mtx_unlock(&pmc_kthread_mtx); 436 437 /* process the request */ 438 PMCDBG3(LOG,WRI,2, "po=%p base=%p ptr=%p", po, 439 lb->plb_base, lb->plb_ptr); 440 /* change our thread's credentials before issuing the I/O */ 441 442 aiov.iov_base = lb->plb_base; 443 aiov.iov_len = nbytes = lb->plb_ptr - lb->plb_base; 444 445 auio.uio_iov = &aiov; 446 auio.uio_iovcnt = 1; 447 auio.uio_offset = -1; 448 auio.uio_resid = nbytes; 449 auio.uio_rw = UIO_WRITE; 450 auio.uio_segflg = UIO_SYSSPACE; 451 auio.uio_td = td; 452 453 /* switch thread credentials -- see kern_ktrace.c */ 454 td->td_ucred = ownercred; 455 error = fo_write(po->po_file, &auio, ownercred, 0, td); 456 td->td_ucred = mycred; 457 458 if (error) { 459 /* XXX some errors are recoverable */ 460 /* send a SIGIO to the owner and exit */ 461 PROC_LOCK(p); 462 kern_psignal(p, SIGIO); 463 PROC_UNLOCK(p); 464 465 mtx_lock(&pmc_kthread_mtx); 466 467 po->po_error = error; /* save for flush log */ 468 469 PMCDBG2(LOG,WRI,2, "po=%p error=%d", po, error); 470 471 break; 472 } 473 474 mtx_lock(&pmc_kthread_mtx); 475 476 /* put the used buffer back into the global pool */ 477 PMCLOG_RESET_BUFFER_DESCRIPTOR(lb); 478 479 pmc_plb_rele(lb); 480 lb = NULL; 481 } 482 483 wakeup_one(po->po_kthread); 484 po->po_kthread = NULL; 485 486 mtx_unlock(&pmc_kthread_mtx); 487 488 /* return the current I/O buffer to the global pool */ 489 if (lb) { 490 PMCLOG_RESET_BUFFER_DESCRIPTOR(lb); 491 492 pmc_plb_rele(lb); 493 } 494 495 /* 496 * Exit this thread, signalling the waiter 497 */ 498 499 crfree(ownercred); 500 501 kproc_exit(0); 502 } 503 504 /* 505 * Release and log entry and schedule an I/O if needed. 506 */ 507 508 static void 509 pmclog_release_flags(struct pmc_owner *po, int wakeup) 510 { 511 struct pmclog_buffer *plb; 512 513 plb = po->po_curbuf[curcpu]; 514 KASSERT(plb->plb_ptr >= plb->plb_base, 515 ("[pmclog,%d] buffer invariants po=%p ptr=%p base=%p", __LINE__, 516 po, plb->plb_ptr, plb->plb_base)); 517 KASSERT(plb->plb_ptr <= plb->plb_fence, 518 ("[pmclog,%d] buffer invariants po=%p ptr=%p fenc=%p", __LINE__, 519 po, plb->plb_ptr, plb->plb_fence)); 520 521 /* schedule an I/O if we've filled a buffer */ 522 if (plb->plb_ptr >= plb->plb_fence) 523 pmclog_schedule_io(po, wakeup); 524 525 PMCDBG1(LOG,REL,1, "po=%p", po); 526 } 527 528 static void 529 pmclog_release(struct pmc_owner *po) 530 { 531 532 pmclog_release_flags(po, 1); 533 } 534 535 536 /* 537 * Attempt to reserve 'length' bytes of space in an owner's log 538 * buffer. The function returns a pointer to 'length' bytes of space 539 * if there was enough space or returns NULL if no space was 540 * available. Non-null returns do so with the po mutex locked. The 541 * caller must invoke pmclog_release() on the pmc owner structure 542 * when done. 543 */ 544 545 static uint32_t * 546 pmclog_reserve(struct pmc_owner *po, int length) 547 { 548 uintptr_t newptr, oldptr __diagused; 549 struct pmclog_buffer *plb, **pplb; 550 551 PMCDBG2(LOG,ALL,1, "po=%p len=%d", po, length); 552 553 KASSERT(length % sizeof(uint32_t) == 0, 554 ("[pmclog,%d] length not a multiple of word size", __LINE__)); 555 556 /* No more data when shutdown in progress. */ 557 if (po->po_flags & PMC_PO_SHUTDOWN) 558 return (NULL); 559 560 pplb = &po->po_curbuf[curcpu]; 561 if (*pplb == NULL && pmclog_get_buffer(po) != 0) 562 goto fail; 563 564 KASSERT(*pplb != NULL, 565 ("[pmclog,%d] po=%p no current buffer", __LINE__, po)); 566 567 plb = *pplb; 568 KASSERT(plb->plb_ptr >= plb->plb_base && 569 plb->plb_ptr <= plb->plb_fence, 570 ("[pmclog,%d] po=%p buffer invariants: ptr=%p base=%p fence=%p", 571 __LINE__, po, plb->plb_ptr, plb->plb_base, 572 plb->plb_fence)); 573 574 oldptr = (uintptr_t) plb->plb_ptr; 575 newptr = oldptr + length; 576 577 KASSERT(oldptr != (uintptr_t) NULL, 578 ("[pmclog,%d] po=%p Null log buffer pointer", __LINE__, po)); 579 580 /* 581 * If we have space in the current buffer, return a pointer to 582 * available space with the PO structure locked. 583 */ 584 if (newptr <= (uintptr_t) plb->plb_fence) { 585 plb->plb_ptr = (char *) newptr; 586 goto done; 587 } 588 589 /* 590 * Otherwise, schedule the current buffer for output and get a 591 * fresh buffer. 592 */ 593 pmclog_schedule_io(po, 0); 594 595 if (pmclog_get_buffer(po) != 0) 596 goto fail; 597 598 plb = *pplb; 599 KASSERT(plb != NULL, 600 ("[pmclog,%d] po=%p no current buffer", __LINE__, po)); 601 602 KASSERT(plb->plb_ptr != NULL, 603 ("[pmclog,%d] null return from pmc_get_log_buffer", __LINE__)); 604 605 KASSERT(plb->plb_ptr == plb->plb_base && 606 plb->plb_ptr <= plb->plb_fence, 607 ("[pmclog,%d] po=%p buffer invariants: ptr=%p base=%p fence=%p", 608 __LINE__, po, plb->plb_ptr, plb->plb_base, 609 plb->plb_fence)); 610 611 oldptr = (uintptr_t) plb->plb_ptr; 612 613 done: 614 return ((uint32_t *) oldptr); 615 fail: 616 return (NULL); 617 } 618 619 /* 620 * Schedule an I/O. 621 * 622 * Transfer the current buffer to the helper kthread. 623 */ 624 625 static void 626 pmclog_schedule_io(struct pmc_owner *po, int wakeup) 627 { 628 struct pmclog_buffer *plb; 629 630 plb = po->po_curbuf[curcpu]; 631 po->po_curbuf[curcpu] = NULL; 632 KASSERT(plb != NULL, 633 ("[pmclog,%d] schedule_io with null buffer po=%p", __LINE__, po)); 634 KASSERT(plb->plb_ptr >= plb->plb_base, 635 ("[pmclog,%d] buffer invariants po=%p ptr=%p base=%p", __LINE__, 636 po, plb->plb_ptr, plb->plb_base)); 637 KASSERT(plb->plb_ptr <= plb->plb_fence, 638 ("[pmclog,%d] buffer invariants po=%p ptr=%p fenc=%p", __LINE__, 639 po, plb->plb_ptr, plb->plb_fence)); 640 641 PMCDBG1(LOG,SIO, 1, "po=%p", po); 642 643 /* 644 * Add the current buffer to the tail of the buffer list and 645 * wakeup the helper. 646 */ 647 mtx_lock_spin(&po->po_mtx); 648 TAILQ_INSERT_TAIL(&po->po_logbuffers, plb, plb_next); 649 mtx_unlock_spin(&po->po_mtx); 650 if (wakeup) 651 wakeup_one(po); 652 } 653 654 /* 655 * Stop the helper kthread. 656 */ 657 658 static void 659 pmclog_stop_kthread(struct pmc_owner *po) 660 { 661 662 mtx_lock(&pmc_kthread_mtx); 663 po->po_flags &= ~PMC_PO_OWNS_LOGFILE; 664 if (po->po_kthread != NULL) { 665 PROC_LOCK(po->po_kthread); 666 kern_psignal(po->po_kthread, SIGHUP); 667 PROC_UNLOCK(po->po_kthread); 668 } 669 wakeup_one(po); 670 while (po->po_kthread) 671 msleep(po->po_kthread, &pmc_kthread_mtx, PPAUSE, "pmckstp", 0); 672 mtx_unlock(&pmc_kthread_mtx); 673 } 674 675 /* 676 * Public functions 677 */ 678 679 /* 680 * Configure a log file for pmc owner 'po'. 681 * 682 * Parameter 'logfd' is a file handle referencing an open file in the 683 * owner process. This file needs to have been opened for writing. 684 */ 685 686 int 687 pmclog_configure_log(struct pmc_mdep *md, struct pmc_owner *po, int logfd) 688 { 689 struct proc *p; 690 struct timespec ts; 691 int error; 692 693 sx_assert(&pmc_sx, SA_XLOCKED); 694 PMCDBG2(LOG,CFG,1, "config po=%p logfd=%d", po, logfd); 695 696 p = po->po_owner; 697 698 /* return EBUSY if a log file was already present */ 699 if (po->po_flags & PMC_PO_OWNS_LOGFILE) 700 return (EBUSY); 701 702 KASSERT(po->po_file == NULL, 703 ("[pmclog,%d] po=%p file (%p) already present", __LINE__, po, 704 po->po_file)); 705 706 /* get a reference to the file state */ 707 error = fget_write(curthread, logfd, &cap_write_rights, &po->po_file); 708 if (error) 709 goto error; 710 711 /* mark process as owning a log file */ 712 po->po_flags |= PMC_PO_OWNS_LOGFILE; 713 714 /* mark process as using HWPMCs */ 715 PROC_LOCK(p); 716 p->p_flag |= P_HWPMC; 717 PROC_UNLOCK(p); 718 nanotime(&ts); 719 /* create a log initialization entry */ 720 PMCLOG_RESERVE_WITH_ERROR(po, PMCLOG_TYPE_INITIALIZE, 721 sizeof(struct pmclog_initialize)); 722 PMCLOG_EMIT32(PMC_VERSION); 723 PMCLOG_EMIT32(md->pmd_cputype); 724 #if defined(__i386__) || defined(__amd64__) 725 PMCLOG_EMIT64(tsc_freq); 726 #else 727 /* other architectures will need to fill this in */ 728 PMCLOG_EMIT32(0); 729 PMCLOG_EMIT32(0); 730 #endif 731 memcpy(_le, &ts, sizeof(ts)); 732 _le += sizeof(ts)/4; 733 PMCLOG_EMITSTRING(pmc_cpuid, PMC_CPUID_LEN); 734 PMCLOG_DESPATCH_SYNC(po); 735 736 return (0); 737 738 error: 739 KASSERT(po->po_kthread == NULL, ("[pmclog,%d] po=%p kthread not " 740 "stopped", __LINE__, po)); 741 742 if (po->po_file) 743 (void) fdrop(po->po_file, curthread); 744 po->po_file = NULL; /* clear file and error state */ 745 po->po_error = 0; 746 po->po_flags &= ~PMC_PO_OWNS_LOGFILE; 747 748 return (error); 749 } 750 751 752 /* 753 * De-configure a log file. This will throw away any buffers queued 754 * for this owner process. 755 */ 756 757 int 758 pmclog_deconfigure_log(struct pmc_owner *po) 759 { 760 int error; 761 struct pmclog_buffer *lb; 762 struct pmc_binding pb; 763 764 PMCDBG1(LOG,CFG,1, "de-config po=%p", po); 765 766 if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0) 767 return (EINVAL); 768 769 KASSERT(po->po_sscount == 0, 770 ("[pmclog,%d] po=%p still owning SS PMCs", __LINE__, po)); 771 KASSERT(po->po_file != NULL, 772 ("[pmclog,%d] po=%p no log file", __LINE__, po)); 773 774 /* stop the kthread, this will reset the 'OWNS_LOGFILE' flag */ 775 pmclog_stop_kthread(po); 776 777 KASSERT(po->po_kthread == NULL, 778 ("[pmclog,%d] po=%p kthread not stopped", __LINE__, po)); 779 780 /* return all queued log buffers to the global pool */ 781 while ((lb = TAILQ_FIRST(&po->po_logbuffers)) != NULL) { 782 TAILQ_REMOVE(&po->po_logbuffers, lb, plb_next); 783 PMCLOG_RESET_BUFFER_DESCRIPTOR(lb); 784 pmc_plb_rele(lb); 785 } 786 pmc_save_cpu_binding(&pb); 787 for (int i = 0; i < mp_ncpus; i++) { 788 pmc_select_cpu(i); 789 /* return the 'current' buffer to the global pool */ 790 if ((lb = po->po_curbuf[curcpu]) != NULL) { 791 PMCLOG_RESET_BUFFER_DESCRIPTOR(lb); 792 pmc_plb_rele(lb); 793 } 794 } 795 pmc_restore_cpu_binding(&pb); 796 797 /* drop a reference to the fd */ 798 if (po->po_file != NULL) { 799 error = fdrop(po->po_file, curthread); 800 po->po_file = NULL; 801 } else 802 error = 0; 803 po->po_error = 0; 804 805 return (error); 806 } 807 808 /* 809 * Flush a process' log buffer. 810 */ 811 812 int 813 pmclog_flush(struct pmc_owner *po, int force) 814 { 815 int error; 816 817 PMCDBG1(LOG,FLS,1, "po=%p", po); 818 819 /* 820 * If there is a pending error recorded by the logger thread, 821 * return that. 822 */ 823 if (po->po_error) 824 return (po->po_error); 825 826 error = 0; 827 828 /* 829 * Check that we do have an active log file. 830 */ 831 mtx_lock(&pmc_kthread_mtx); 832 if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0) { 833 error = EINVAL; 834 goto error; 835 } 836 837 pmclog_schedule_all(po); 838 error: 839 mtx_unlock(&pmc_kthread_mtx); 840 841 return (error); 842 } 843 844 static void 845 pmclog_schedule_one_cond(struct pmc_owner *po) 846 { 847 struct pmclog_buffer *plb; 848 int cpu; 849 850 spinlock_enter(); 851 cpu = curcpu; 852 /* tell hardclock not to run again */ 853 if (PMC_CPU_HAS_SAMPLES(cpu)) 854 PMC_CALL_HOOK_UNLOCKED(curthread, PMC_FN_DO_SAMPLES, NULL); 855 856 plb = po->po_curbuf[cpu]; 857 if (plb && plb->plb_ptr != plb->plb_base) 858 pmclog_schedule_io(po, 1); 859 spinlock_exit(); 860 } 861 862 static void 863 pmclog_schedule_all(struct pmc_owner *po) 864 { 865 struct pmc_binding pb; 866 867 /* 868 * Schedule the current buffer if any and not empty. 869 */ 870 pmc_save_cpu_binding(&pb); 871 for (int i = 0; i < mp_ncpus; i++) { 872 pmc_select_cpu(i); 873 pmclog_schedule_one_cond(po); 874 } 875 pmc_restore_cpu_binding(&pb); 876 } 877 878 int 879 pmclog_close(struct pmc_owner *po) 880 { 881 882 PMCDBG1(LOG,CLO,1, "po=%p", po); 883 884 pmclog_process_closelog(po); 885 886 mtx_lock(&pmc_kthread_mtx); 887 /* 888 * Initiate shutdown: no new data queued, 889 * thread will close file on last block. 890 */ 891 po->po_flags |= PMC_PO_SHUTDOWN; 892 /* give time for all to see */ 893 DELAY(50); 894 895 /* 896 * Schedule the current buffer. 897 */ 898 pmclog_schedule_all(po); 899 wakeup_one(po); 900 901 mtx_unlock(&pmc_kthread_mtx); 902 903 return (0); 904 } 905 906 void 907 pmclog_process_callchain(struct pmc *pm, struct pmc_sample *ps) 908 { 909 int n, recordlen; 910 uint32_t flags; 911 struct pmc_owner *po; 912 913 PMCDBG3(LOG,SAM,1,"pm=%p pid=%d n=%d", pm, ps->ps_pid, 914 ps->ps_nsamples); 915 916 recordlen = offsetof(struct pmclog_callchain, pl_pc) + 917 ps->ps_nsamples * sizeof(uintfptr_t); 918 po = pm->pm_owner; 919 flags = PMC_CALLCHAIN_TO_CPUFLAGS(ps->ps_cpu,ps->ps_flags); 920 PMCLOG_RESERVE_SAFE(po, PMCLOG_TYPE_CALLCHAIN, recordlen, ps->ps_tsc); 921 PMCLOG_EMIT32(ps->ps_pid); 922 PMCLOG_EMIT32(ps->ps_tid); 923 PMCLOG_EMIT32(pm->pm_id); 924 PMCLOG_EMIT32(flags); 925 for (n = 0; n < ps->ps_nsamples; n++) 926 PMCLOG_EMITADDR(ps->ps_pc[n]); 927 PMCLOG_DESPATCH_SAFE(po); 928 } 929 930 void 931 pmclog_process_closelog(struct pmc_owner *po) 932 { 933 PMCLOG_RESERVE(po, PMCLOG_TYPE_CLOSELOG, 934 sizeof(struct pmclog_closelog)); 935 PMCLOG_DESPATCH_SYNC(po); 936 } 937 938 void 939 pmclog_process_dropnotify(struct pmc_owner *po) 940 { 941 PMCLOG_RESERVE(po, PMCLOG_TYPE_DROPNOTIFY, 942 sizeof(struct pmclog_dropnotify)); 943 PMCLOG_DESPATCH(po); 944 } 945 946 void 947 pmclog_process_map_in(struct pmc_owner *po, pid_t pid, uintfptr_t start, 948 const char *path) 949 { 950 int pathlen, recordlen; 951 952 KASSERT(path != NULL, ("[pmclog,%d] map-in, null path", __LINE__)); 953 954 pathlen = strlen(path) + 1; /* #bytes for path name */ 955 recordlen = offsetof(struct pmclog_map_in, pl_pathname) + 956 pathlen; 957 958 PMCLOG_RESERVE(po, PMCLOG_TYPE_MAP_IN, recordlen); 959 PMCLOG_EMIT32(pid); 960 PMCLOG_EMIT32(0); 961 PMCLOG_EMITADDR(start); 962 PMCLOG_EMITSTRING(path,pathlen); 963 PMCLOG_DESPATCH_SYNC(po); 964 } 965 966 void 967 pmclog_process_map_out(struct pmc_owner *po, pid_t pid, uintfptr_t start, 968 uintfptr_t end) 969 { 970 KASSERT(start <= end, ("[pmclog,%d] start > end", __LINE__)); 971 972 PMCLOG_RESERVE(po, PMCLOG_TYPE_MAP_OUT, sizeof(struct pmclog_map_out)); 973 PMCLOG_EMIT32(pid); 974 PMCLOG_EMIT32(0); 975 PMCLOG_EMITADDR(start); 976 PMCLOG_EMITADDR(end); 977 PMCLOG_DESPATCH(po); 978 } 979 980 void 981 pmclog_process_pmcallocate(struct pmc *pm) 982 { 983 struct pmc_owner *po; 984 struct pmc_soft *ps; 985 986 po = pm->pm_owner; 987 988 PMCDBG1(LOG,ALL,1, "pm=%p", pm); 989 990 if (PMC_TO_CLASS(pm) == PMC_CLASS_SOFT) { 991 PMCLOG_RESERVE(po, PMCLOG_TYPE_PMCALLOCATEDYN, 992 sizeof(struct pmclog_pmcallocatedyn)); 993 PMCLOG_EMIT32(pm->pm_id); 994 PMCLOG_EMIT32(pm->pm_event); 995 PMCLOG_EMIT32(pm->pm_flags); 996 PMCLOG_EMIT32(0); 997 PMCLOG_EMIT64(pm->pm_sc.pm_reloadcount); 998 ps = pmc_soft_ev_acquire(pm->pm_event); 999 if (ps != NULL) 1000 PMCLOG_EMITSTRING(ps->ps_ev.pm_ev_name,PMC_NAME_MAX); 1001 else 1002 PMCLOG_EMITNULLSTRING(PMC_NAME_MAX); 1003 pmc_soft_ev_release(ps); 1004 PMCLOG_DESPATCH_SYNC(po); 1005 } else { 1006 PMCLOG_RESERVE(po, PMCLOG_TYPE_PMCALLOCATE, 1007 sizeof(struct pmclog_pmcallocate)); 1008 PMCLOG_EMIT32(pm->pm_id); 1009 PMCLOG_EMIT32(pm->pm_event); 1010 PMCLOG_EMIT32(pm->pm_flags); 1011 PMCLOG_EMIT32(0); 1012 PMCLOG_EMIT64(pm->pm_sc.pm_reloadcount); 1013 PMCLOG_DESPATCH_SYNC(po); 1014 } 1015 } 1016 1017 void 1018 pmclog_process_pmcattach(struct pmc *pm, pid_t pid, char *path) 1019 { 1020 int pathlen, recordlen; 1021 struct pmc_owner *po; 1022 1023 PMCDBG2(LOG,ATT,1,"pm=%p pid=%d", pm, pid); 1024 1025 po = pm->pm_owner; 1026 1027 pathlen = strlen(path) + 1; /* #bytes for the string */ 1028 recordlen = offsetof(struct pmclog_pmcattach, pl_pathname) + pathlen; 1029 1030 PMCLOG_RESERVE(po, PMCLOG_TYPE_PMCATTACH, recordlen); 1031 PMCLOG_EMIT32(pm->pm_id); 1032 PMCLOG_EMIT32(pid); 1033 PMCLOG_EMITSTRING(path, pathlen); 1034 PMCLOG_DESPATCH_SYNC(po); 1035 } 1036 1037 void 1038 pmclog_process_pmcdetach(struct pmc *pm, pid_t pid) 1039 { 1040 struct pmc_owner *po; 1041 1042 PMCDBG2(LOG,ATT,1,"!pm=%p pid=%d", pm, pid); 1043 1044 po = pm->pm_owner; 1045 1046 PMCLOG_RESERVE(po, PMCLOG_TYPE_PMCDETACH, 1047 sizeof(struct pmclog_pmcdetach)); 1048 PMCLOG_EMIT32(pm->pm_id); 1049 PMCLOG_EMIT32(pid); 1050 PMCLOG_DESPATCH_SYNC(po); 1051 } 1052 1053 void 1054 pmclog_process_proccreate(struct pmc_owner *po, struct proc *p, int sync) 1055 { 1056 if (sync) { 1057 PMCLOG_RESERVE(po, PMCLOG_TYPE_PROC_CREATE, 1058 sizeof(struct pmclog_proccreate)); 1059 PMCLOG_EMIT32(p->p_pid); 1060 PMCLOG_EMIT32(p->p_flag); 1061 PMCLOG_EMITSTRING(p->p_comm, MAXCOMLEN+1); 1062 PMCLOG_DESPATCH_SYNC(po); 1063 } else { 1064 PMCLOG_RESERVE(po, PMCLOG_TYPE_PROC_CREATE, 1065 sizeof(struct pmclog_proccreate)); 1066 PMCLOG_EMIT32(p->p_pid); 1067 PMCLOG_EMIT32(p->p_flag); 1068 PMCLOG_EMITSTRING(p->p_comm, MAXCOMLEN+1); 1069 PMCLOG_DESPATCH(po); 1070 } 1071 } 1072 1073 /* 1074 * Log a context switch event to the log file. 1075 */ 1076 1077 void 1078 pmclog_process_proccsw(struct pmc *pm, struct pmc_process *pp, pmc_value_t v, struct thread *td) 1079 { 1080 struct pmc_owner *po; 1081 1082 KASSERT(pm->pm_flags & PMC_F_LOG_PROCCSW, 1083 ("[pmclog,%d] log-process-csw called gratuitously", __LINE__)); 1084 1085 PMCDBG3(LOG,SWO,1,"pm=%p pid=%d v=%jx", pm, pp->pp_proc->p_pid, 1086 v); 1087 1088 po = pm->pm_owner; 1089 1090 PMCLOG_RESERVE_SAFE(po, PMCLOG_TYPE_PROCCSW, 1091 sizeof(struct pmclog_proccsw), pmc_rdtsc()); 1092 PMCLOG_EMIT64(v); 1093 PMCLOG_EMIT32(pm->pm_id); 1094 PMCLOG_EMIT32(pp->pp_proc->p_pid); 1095 PMCLOG_EMIT32(td->td_tid); 1096 PMCLOG_EMIT32(0); 1097 PMCLOG_DESPATCH_SCHED_LOCK(po); 1098 } 1099 1100 void 1101 pmclog_process_procexec(struct pmc_owner *po, pmc_id_t pmid, pid_t pid, 1102 uintptr_t baseaddr, uintptr_t dynaddr, char *path) 1103 { 1104 int pathlen, recordlen; 1105 1106 PMCDBG3(LOG,EXC,1,"po=%p pid=%d path=\"%s\"", po, pid, path); 1107 1108 pathlen = strlen(path) + 1; /* #bytes for the path */ 1109 recordlen = offsetof(struct pmclog_procexec, pl_pathname) + pathlen; 1110 PMCLOG_RESERVE(po, PMCLOG_TYPE_PROCEXEC, recordlen); 1111 PMCLOG_EMIT32(pid); 1112 PMCLOG_EMIT32(pmid); 1113 PMCLOG_EMITADDR(baseaddr); 1114 PMCLOG_EMITADDR(dynaddr); 1115 PMCLOG_EMITSTRING(path,pathlen); 1116 PMCLOG_DESPATCH_SYNC(po); 1117 } 1118 1119 /* 1120 * Log a process exit event (and accumulated pmc value) to the log file. 1121 */ 1122 1123 void 1124 pmclog_process_procexit(struct pmc *pm, struct pmc_process *pp) 1125 { 1126 int ri; 1127 struct pmc_owner *po; 1128 1129 ri = PMC_TO_ROWINDEX(pm); 1130 PMCDBG3(LOG,EXT,1,"pm=%p pid=%d v=%jx", pm, pp->pp_proc->p_pid, 1131 pp->pp_pmcs[ri].pp_pmcval); 1132 1133 po = pm->pm_owner; 1134 1135 PMCLOG_RESERVE(po, PMCLOG_TYPE_PROCEXIT, 1136 sizeof(struct pmclog_procexit)); 1137 PMCLOG_EMIT32(pm->pm_id); 1138 PMCLOG_EMIT32(pp->pp_proc->p_pid); 1139 PMCLOG_EMIT64(pp->pp_pmcs[ri].pp_pmcval); 1140 PMCLOG_DESPATCH(po); 1141 } 1142 1143 /* 1144 * Log a fork event. 1145 */ 1146 1147 void 1148 pmclog_process_procfork(struct pmc_owner *po, pid_t oldpid, pid_t newpid) 1149 { 1150 PMCLOG_RESERVE(po, PMCLOG_TYPE_PROCFORK, 1151 sizeof(struct pmclog_procfork)); 1152 PMCLOG_EMIT32(oldpid); 1153 PMCLOG_EMIT32(newpid); 1154 PMCLOG_DESPATCH(po); 1155 } 1156 1157 /* 1158 * Log a process exit event of the form suitable for system-wide PMCs. 1159 */ 1160 1161 void 1162 pmclog_process_sysexit(struct pmc_owner *po, pid_t pid) 1163 { 1164 PMCLOG_RESERVE(po, PMCLOG_TYPE_SYSEXIT, sizeof(struct pmclog_sysexit)); 1165 PMCLOG_EMIT32(pid); 1166 PMCLOG_DESPATCH(po); 1167 } 1168 1169 void 1170 pmclog_process_threadcreate(struct pmc_owner *po, struct thread *td, int sync) 1171 { 1172 struct proc *p; 1173 1174 p = td->td_proc; 1175 if (sync) { 1176 PMCLOG_RESERVE(po, PMCLOG_TYPE_THR_CREATE, 1177 sizeof(struct pmclog_threadcreate)); 1178 PMCLOG_EMIT32(td->td_tid); 1179 PMCLOG_EMIT32(p->p_pid); 1180 PMCLOG_EMIT32(p->p_flag); 1181 PMCLOG_EMIT32(0); 1182 PMCLOG_EMITSTRING(td->td_name, MAXCOMLEN+1); 1183 PMCLOG_DESPATCH_SYNC(po); 1184 } else { 1185 PMCLOG_RESERVE(po, PMCLOG_TYPE_THR_CREATE, 1186 sizeof(struct pmclog_threadcreate)); 1187 PMCLOG_EMIT32(td->td_tid); 1188 PMCLOG_EMIT32(p->p_pid); 1189 PMCLOG_EMIT32(p->p_flag); 1190 PMCLOG_EMIT32(0); 1191 PMCLOG_EMITSTRING(td->td_name, MAXCOMLEN+1); 1192 PMCLOG_DESPATCH(po); 1193 } 1194 } 1195 1196 void 1197 pmclog_process_threadexit(struct pmc_owner *po, struct thread *td) 1198 { 1199 1200 PMCLOG_RESERVE(po, PMCLOG_TYPE_THR_EXIT, 1201 sizeof(struct pmclog_threadexit)); 1202 PMCLOG_EMIT32(td->td_tid); 1203 PMCLOG_DESPATCH(po); 1204 } 1205 1206 /* 1207 * Write a user log entry. 1208 */ 1209 1210 int 1211 pmclog_process_userlog(struct pmc_owner *po, struct pmc_op_writelog *wl) 1212 { 1213 int error; 1214 1215 PMCDBG2(LOG,WRI,1, "writelog po=%p ud=0x%x", po, wl->pm_userdata); 1216 1217 error = 0; 1218 1219 PMCLOG_RESERVE_WITH_ERROR(po, PMCLOG_TYPE_USERDATA, 1220 sizeof(struct pmclog_userdata)); 1221 PMCLOG_EMIT32(wl->pm_userdata); 1222 PMCLOG_DESPATCH(po); 1223 1224 error: 1225 return (error); 1226 } 1227 1228 /* 1229 * Initialization. 1230 * 1231 * Create a pool of log buffers and initialize mutexes. 1232 */ 1233 1234 void 1235 pmclog_initialize(void) 1236 { 1237 struct pmclog_buffer *plb; 1238 int domain, ncpus, total; 1239 1240 if (pmclog_buffer_size <= 0 || 1241 pmclog_buffer_size > PMC_LOG_BUFFER_SIZE_MAX) { 1242 log(LOG_WARNING, 1243 "hwpmc: logbuffersize=%d must be greater than zero " 1244 "and less than or equal to %d, resetting to %d\n", 1245 pmclog_buffer_size, PMC_LOG_BUFFER_SIZE_MAX, 1246 PMC_LOG_BUFFER_SIZE); 1247 1248 pmclog_buffer_size = PMC_LOG_BUFFER_SIZE; 1249 } 1250 1251 if (pmc_nlogbuffers_pcpu <= 0) { 1252 log(LOG_WARNING, 1253 "hwpmc: nbuffers_pcpu=%d must be greater than zero, " 1254 "resetting to %d\n", 1255 pmc_nlogbuffers_pcpu, PMC_NLOGBUFFERS_PCPU); 1256 1257 pmc_nlogbuffers_pcpu = PMC_NLOGBUFFERS_PCPU; 1258 } 1259 1260 if (pmc_nlogbuffers_pcpu * pmclog_buffer_size > 1261 PMC_NLOGBUFFERS_PCPU_MEM_MAX) { 1262 log(LOG_WARNING, 1263 "hwpmc: nbuffers_pcpu=%d * logbuffersize=%d exceeds " 1264 "%dMB per CPU limit, resetting to defaults (%d * %d)\n", 1265 pmc_nlogbuffers_pcpu, pmclog_buffer_size, 1266 PMC_NLOGBUFFERS_PCPU_MEM_MAX / 1024, 1267 PMC_NLOGBUFFERS_PCPU, PMC_LOG_BUFFER_SIZE); 1268 1269 pmc_nlogbuffers_pcpu = PMC_NLOGBUFFERS_PCPU; 1270 pmclog_buffer_size = PMC_LOG_BUFFER_SIZE; 1271 } 1272 1273 for (domain = 0; domain < vm_ndomains; domain++) { 1274 ncpus = pmc_dom_hdrs[domain]->pdbh_ncpus; 1275 total = ncpus * pmc_nlogbuffers_pcpu; 1276 1277 plb = malloc_domainset(sizeof(struct pmclog_buffer) * total, 1278 M_PMC, DOMAINSET_PREF(domain), M_WAITOK | M_ZERO); 1279 pmc_dom_hdrs[domain]->pdbh_plbs = plb; 1280 for (; total > 0; total--, plb++) { 1281 void *buf; 1282 1283 buf = malloc_domainset(1024 * pmclog_buffer_size, M_PMC, 1284 DOMAINSET_PREF(domain), M_WAITOK | M_ZERO); 1285 PMCLOG_INIT_BUFFER_DESCRIPTOR(plb, buf, domain); 1286 pmc_plb_rele_unlocked(plb); 1287 } 1288 } 1289 1290 mtx_init(&pmc_kthread_mtx, "pmc-kthread", "pmc-sleep", MTX_DEF); 1291 } 1292 1293 /* 1294 * Shutdown logging. 1295 * 1296 * Destroy mutexes and release memory back the to free pool. 1297 */ 1298 1299 void 1300 pmclog_shutdown(void) 1301 { 1302 struct pmclog_buffer *plb; 1303 int domain; 1304 1305 mtx_destroy(&pmc_kthread_mtx); 1306 1307 for (domain = 0; domain < vm_ndomains; domain++) { 1308 while ((plb = TAILQ_FIRST(&pmc_dom_hdrs[domain]->pdbh_head)) != NULL) { 1309 TAILQ_REMOVE(&pmc_dom_hdrs[domain]->pdbh_head, plb, plb_next); 1310 free(plb->plb_base, M_PMC); 1311 } 1312 free(pmc_dom_hdrs[domain]->pdbh_plbs, M_PMC); 1313 } 1314 } 1315