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