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