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