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