Debian Codegraph @

src/libsysprof-capture/sysprof-collector.c

raw
1 /* sysprof-collector.c
2 *
3 * Copyright 2020 Christian Hergert <[email protected]>
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * Subject to the terms and conditions of this license, each copyright holder
17 * and contributor hereby grants to those receiving rights under this license
18 * a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
19 * irrevocable (except for failure to satisfy the conditions of this license)
20 * patent license to make, have made, use, offer to sell, sell, import, and
21 * otherwise transfer this software, where such license applies only to those
22 * patent claims, already acquired or hereafter acquired, licensable by such
23 * copyright holder or contributor that are necessarily infringed by:
24 *
25 * (a) their Contribution(s) (the licensed copyrights of copyright holders
26 * and non-copyrightable additions of contributors, in source or binary
27 * form) alone; or
28 *
29 * (b) combination of their Contribution(s) with the work of authorship to
30 * which such Contribution(s) was added by such copyright holder or
31 * contributor, if, at the time the Contribution is added, such addition
32 * causes such combination to be necessarily infringed. The patent license
33 * shall not apply to any other combinations which include the
34 * Contribution.
35 *
36 * Except as expressly stated above, no rights or licenses from any copyright
37 * holder or contributor is granted under this license, whether expressly, by
38 * implication, estoppel or otherwise.
39 *
40 * DISCLAIMER
41 *
42 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
43 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
44 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
45 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
46 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
47 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
48 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
49 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
50 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
51 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
52 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53 *
54 * SPDX-License-Identifier: BSD-2-Clause-Patent
55 */
56
57 #include "config.h"
58
59 #include <assert.h>
60 #include <errno.h>
61 #include <fcntl.h>
62 #include <poll.h>
63 #include <pthread.h>
64 #ifdef __linux__
65 # include <sched.h>
66 #endif
67 #include <stdarg.h>
68 #include <stdbool.h>
69 #include <stdio.h>
70 #include <stdlib.h>
71 #include <string.h>
72 #include <sys/socket.h>
73 #include <sys/syscall.h>
74 #include <sys/types.h>
75 #include <unistd.h>
76
77 #include "mapped-ring-buffer.h"
78
79 #include "sysprof-capture-util-private.h"
80 #include "sysprof-collector.h"
81 #include "sysprof-macros-internal.h"
82
83 #define MAX_UNWIND_DEPTH 128
84 #define CREATRING "CreatRing\0"
85 #define CREATRING_LEN 10
86
87 #ifndef MSG_NOSIGNAL
88 #define MSG_NOSIGNAL 0
89 #endif
90
91 #ifndef MSG_CMSG_CLOEXEC
92 #define MSG_CMSG_CLOEXEC 0
93 #endif
94
95 typedef struct
96 {
97 MappedRingBuffer *buffer;
98 bool is_shared;
99 int tid;
100 int pid;
101 int next_counter_id;
102 } SysprofCollector;
103
104 #define COLLECTOR_INVALID ((void *)&invalid)
105
106 static MappedRingBuffer *request_writer (void);
107 static void sysprof_collector_free (void *data);
108 static const SysprofCollector *sysprof_collector_get (void);
109
110 static pthread_mutex_t control_fd_lock = PTHREAD_MUTEX_INITIALIZER;
111 static pthread_key_t collector_key; /* initialised in sysprof_collector_init() */
112 static pthread_key_t single_trace_key; /* initialised in sysprof_collector_init() */
113 static pthread_once_t collector_init = PTHREAD_ONCE_INIT;
114 static SysprofCollector *shared_collector;
115 static SysprofCollector invalid;
116
117 static inline bool
118 use_single_trace (void)
119 {
120 return (bool) pthread_getspecific (single_trace_key);
121 }
122
123 static inline int
124 _do_getcpu (void)
125 {
126 #ifdef __linux__
127 return sched_getcpu ();
128 #else
129 return -1;
130 #endif
131 }
132
133 static inline size_t
134 realign (size_t size)
135 {
136 return (size + SYSPROF_CAPTURE_ALIGN - 1) & ~(SYSPROF_CAPTURE_ALIGN - 1);
137 }
138
139 static bool
140 set_fd_blocking (int fd)
141 {
142 #ifdef F_GETFL
143 long fcntl_flags;
144 fcntl_flags = fcntl (fd, F_GETFL);
145
146 if (fcntl_flags == -1)
147 return false;
148
149 #ifdef O_NONBLOCK
150 fcntl_flags &= ~O_NONBLOCK;
151 #else
152 fcntl_flags &= ~O_NDELAY;
153 #endif
154
155 if (fcntl (fd, F_SETFL, fcntl_flags) == -1)
156 return false;
157
158 return true;
159 #else
160 return false;
161 #endif
162 }
163
164 static bool
165 block_on_poll (int fd,
166 int condition)
167 {
168 struct pollfd poll_fd;
169
170 poll_fd.fd = fd;
171 poll_fd.events = condition;
172
173 return (TEMP_FAILURE_RETRY (poll (&poll_fd, 1, -1)) == 1);
174 }
175
176 static ssize_t
177 send_blocking (int fd,
178 const uint8_t *buffer,
179 size_t buffer_len)
180 {
181 ssize_t res;
182
183 while ((res = TEMP_FAILURE_RETRY (send (fd, buffer, buffer_len, MSG_NOSIGNAL))) < 0)
184 {
185 int errsv = errno;
186
187 if (errsv == EWOULDBLOCK ||
188 errsv == EAGAIN)
189 {
190 if (!block_on_poll (fd, POLLOUT))
191 return -1;
192 }
193 else
194 {
195 return -1;
196 }
197 }
198
199 return res;
200 }
201
202 static bool
203 send_all_blocking (int fd,
204 const uint8_t *buffer,
205 size_t buffer_len,
206 size_t *bytes_written)
207 {
208 size_t _bytes_written;
209
210 _bytes_written = 0;
211 while (_bytes_written < buffer_len)
212 {
213 ssize_t res = send_blocking (fd, buffer + _bytes_written, buffer_len - _bytes_written);
214 if (res == -1)
215 {
216 if (bytes_written != NULL)
217 *bytes_written = _bytes_written;
218 return false;
219 }
220 assert (res > 0);
221
222 _bytes_written += res;
223 }
224
225 if (bytes_written != NULL)
226 *bytes_written = _bytes_written;
227 return true;
228 }
229
230 static int
231 receive_fd_blocking (int peer_fd)
232 {
233 ssize_t res;
234 struct msghdr msg;
235 struct iovec one_vector;
236 char one_byte;
237 uint8_t cmsg_buffer[CMSG_SPACE (sizeof (int))];
238 struct cmsghdr *cmsg;
239 const int *fds = NULL;
240 size_t n_fds = 0;
241
242 one_vector.iov_base = &one_byte;
243 one_vector.iov_len = 1;
244
245 msg.msg_name = NULL;
246 msg.msg_namelen = 0;
247 msg.msg_iov = &one_vector;
248 msg.msg_iovlen = 1;
249 msg.msg_flags = MSG_CMSG_CLOEXEC;
250 msg.msg_control = &cmsg_buffer;
251 msg.msg_controllen = sizeof (cmsg_buffer);
252
253 while ((res = TEMP_FAILURE_RETRY (recvmsg (peer_fd, &msg, msg.msg_flags))) < 0)
254 {
255 int errsv = errno;
256
257 if (errsv == EWOULDBLOCK ||
258 errsv == EAGAIN)
259 {
260 if (!block_on_poll (peer_fd, POLLIN))
261 return -1;
262 }
263 else
264 {
265 return -1;
266 }
267 }
268
269 /* Decode the returned control message */
270 cmsg = CMSG_FIRSTHDR (&msg);
271 if (cmsg == NULL)
272 return -1;
273
274 if (cmsg->cmsg_level != SOL_SOCKET ||
275 cmsg->cmsg_type != SCM_RIGHTS)
276 return -1;
277
278 /* non-integer number of FDs */
279 if ((cmsg->cmsg_len - ((char *)CMSG_DATA (cmsg) - (char *)cmsg)) % 4 != 0)
280 return -1;
281
282 fds = (const int *)(void *)CMSG_DATA (cmsg);
283 n_fds = (cmsg->cmsg_len - ((char *)CMSG_DATA (cmsg) - (char *)cmsg)) / sizeof (*fds);
284
285 /* only expecting one FD */
286 if (n_fds != 1)
287 goto close_fds_err;
288
289 for (size_t i = 0; i < n_fds; i++)
290 {
291 if (fds[i] < 0)
292 goto close_fds_err;
293 }
294
295 /* only expecting one control message */
296 cmsg = CMSG_NXTHDR (&msg, cmsg);
297 if (cmsg != NULL)
298 goto close_fds_err;
299
300 return fds[0];
301
302 close_fds_err:
303 for (size_t i = 0; i < n_fds; i++)
304 close (fds[i]);
305
306 return -1;
307 }
308
309 /* Called with @control_fd_lock held. */
310 static MappedRingBuffer *
311 request_writer (void)
312 {
313 static int peer_fd = -1;
314 MappedRingBuffer *buffer = NULL;
315
316 if (peer_fd == -1)
317 {
318 const char *fdstr = getenv ("SYSPROF_CONTROL_FD");
319
320 if (fdstr != NULL)
321 peer_fd = atoi (fdstr);
322
323 if (peer_fd > 0)
324 {
325 (void) set_fd_blocking (peer_fd);
326
327 #ifdef SO_NOSIGPIPE
328 {
329 int opt_value = 1;
330 (void) setsockopt (peer_fd, SOL_SOCKET, SO_NOSIGPIPE, &opt_value, sizeof (opt_value));
331 }
332 #endif
333 }
334 }
335
336 if (peer_fd >= 0)
337 {
338 if (send_all_blocking (peer_fd, (const uint8_t *) CREATRING, CREATRING_LEN, NULL))
339 {
340 int ring_fd = receive_fd_blocking (peer_fd);
341
342 if (ring_fd >= 0)
343 {
344 buffer = mapped_ring_buffer_new_writer (ring_fd);
345 close (ring_fd);
346 }
347 }
348 }
349
350 return sysprof_steal_pointer (&buffer);
351 }
352
353 static void
354 write_final_frame (MappedRingBuffer *ring)
355 {
356 SysprofCaptureFrame *fr;
357
358 assert (ring != NULL);
359
360 if ((fr = mapped_ring_buffer_allocate (ring, sizeof *fr)))
361 {
362 fr->len = sizeof *fr; /* aligned */
363 fr->type = 0xFF; /* Invalid */
364 fr->cpu = -1;
365 fr->pid = -1;
366 fr->time = SYSPROF_CAPTURE_CURRENT_TIME;
367 mapped_ring_buffer_advance (ring, fr->len);
368 }
369 }
370
371 static void
372 sysprof_collector_free (void *data)
373 {
374 SysprofCollector *collector = data;
375
376 if (collector != NULL && collector != COLLECTOR_INVALID)
377 {
378 MappedRingBuffer *buffer = sysprof_steal_pointer (&collector->buffer);
379
380 if (buffer != NULL)
381 {
382 write_final_frame (buffer);
383 mapped_ring_buffer_unref (buffer);
384 }
385
386 free (collector);
387 }
388 }
389
390 static const SysprofCollector *
391 sysprof_collector_get (void)
392 {
393 const SysprofCollector *collector;
394
395 sysprof_collector_init ();
396 collector = pthread_getspecific (collector_key);
397
398 /* We might have gotten here recursively */
399 if SYSPROF_UNLIKELY (collector == COLLECTOR_INVALID)
400 return COLLECTOR_INVALID;
401
402 if SYSPROF_LIKELY (collector != NULL)
403 return collector;
404
405 if (use_single_trace () && shared_collector != COLLECTOR_INVALID)
406 return shared_collector;
407
408 {
409 SysprofCollector *self, *old_collector;
410
411 /* First set the collector to invalid so anything recursive
412 * here fails instead of becoming re-entrant.
413 */
414 pthread_setspecific (collector_key, COLLECTOR_INVALID);
415
416 /* Now we can malloc without ending up here again */
417 self = sysprof_malloc0 (sizeof (SysprofCollector));
418 if (self == NULL)
419 return COLLECTOR_INVALID;
420
421 self->pid = getpid ();
422 #ifdef __linux__
423 self->tid = syscall (__NR_gettid, 0);
424 #else
425 self->tid = self->pid;
426 #endif
427 self->next_counter_id = (((unsigned)self->pid << 16) >> 8);
428
429 pthread_mutex_lock (&control_fd_lock);
430
431 if (getenv ("SYSPROF_CONTROL_FD") != NULL)
432 self->buffer = request_writer ();
433
434 /* Update the stored collector */
435 old_collector = pthread_getspecific (collector_key);
436
437 if (self->is_shared)
438 {
439 if (pthread_setspecific (collector_key, COLLECTOR_INVALID) != 0)
440 goto fail;
441 sysprof_collector_free (old_collector);
442 shared_collector = self;
443 }
444 else
445 {
446 if (pthread_setspecific (collector_key, self) != 0)
447 goto fail;
448 sysprof_collector_free (old_collector);
449 }
450
451 pthread_mutex_unlock (&control_fd_lock);
452
453 return self;
454
455 fail:
456 pthread_mutex_unlock (&control_fd_lock);
457 sysprof_collector_free (self);
458
459 return COLLECTOR_INVALID;
460 }
461 }
462
463 static void
464 collector_init_cb (void)
465 {
466 if (SYSPROF_UNLIKELY (pthread_key_create (&collector_key, sysprof_collector_free) != 0))
467 abort ();
468 if (SYSPROF_UNLIKELY (pthread_key_create (&single_trace_key, NULL) != 0))
469 abort ();
470
471 sysprof_clock_init ();
472 }
473
474 void
475 sysprof_collector_init (void)
476 {
477 if SYSPROF_UNLIKELY (pthread_once (&collector_init, collector_init_cb) != 0)
478 abort ();
479 }
480
481 #define COLLECTOR_BEGIN \
482 do { \
483 const SysprofCollector *collector = sysprof_collector_get (); \
484 if SYSPROF_LIKELY (collector->buffer) \
485 { \
486 if SYSPROF_UNLIKELY (collector->is_shared) \
487 pthread_mutex_lock (&control_fd_lock); \
488 \
489 {
490
491 #define COLLECTOR_END \
492 } \
493 \
494 if SYSPROF_UNLIKELY (collector->is_shared) \
495 pthread_mutex_unlock (&control_fd_lock); \
496 } \
497 } while (0)
498
499 void
500 sysprof_collector_allocate (SysprofCaptureAddress alloc_addr,
501 int64_t alloc_size,
502 SysprofBacktraceFunc backtrace_func,
503 void *backtrace_data)
504 {
505 COLLECTOR_BEGIN {
506 SysprofCaptureAllocation *ev;
507 size_t len;
508
509 len = sizeof *ev + (sizeof (SysprofCaptureAllocation) * MAX_UNWIND_DEPTH);
510
511 if ((ev = mapped_ring_buffer_allocate (collector->buffer, len)))
512 {
513 int n_addrs;
514
515 /* First take a backtrace, so that backtrace_func() can overwrite
516 * a little bit of data *BEFORE* ev->addrs as stratch space. This
517 * is useful to allow using unw_backtrace() or backtrace() to skip
518 * a small number of frames.
519 *
520 * We fill in all the other data afterwards which overwrites that
521 * scratch space anyway.
522 */
523 if (backtrace_func)
524 n_addrs = backtrace_func (ev->addrs, MAX_UNWIND_DEPTH, backtrace_data);
525 else
526 n_addrs = 0;
527
528 if (n_addrs < 0)
529 n_addrs = 0;
530
531 ev->n_addrs = ((n_addrs < 0) ? 0 : (n_addrs > MAX_UNWIND_DEPTH) ? MAX_UNWIND_DEPTH : n_addrs);
532 ev->frame.len = sizeof *ev + sizeof (SysprofCaptureAddress) * ev->n_addrs;
533 ev->frame.type = SYSPROF_CAPTURE_FRAME_ALLOCATION;
534 ev->frame.cpu = _do_getcpu ();
535 ev->frame.pid = collector->pid;
536 ev->frame.time = SYSPROF_CAPTURE_CURRENT_TIME;
537 ev->tid = collector->tid;
538 ev->alloc_addr = alloc_addr;
539 ev->alloc_size = alloc_size;
540 ev->padding1 = 0;
541
542 mapped_ring_buffer_advance (collector->buffer, ev->frame.len);
543 }
544
545 } COLLECTOR_END;
546 }
547
548 void
549 sysprof_collector_sample (SysprofBacktraceFunc backtrace_func,
550 void *backtrace_data)
551 {
552 COLLECTOR_BEGIN {
553 SysprofCaptureSample *ev;
554 size_t len;
555
556 len = sizeof *ev + (sizeof (SysprofCaptureSample) * MAX_UNWIND_DEPTH);
557
558 if ((ev = mapped_ring_buffer_allocate (collector->buffer, len)))
559 {
560 int n_addrs;
561
562 /* See comment from sysprof_collector_allocate(). */
563 if (backtrace_func)
564 n_addrs = backtrace_func (ev->addrs, MAX_UNWIND_DEPTH, backtrace_data);
565 else
566 n_addrs = 0;
567
568 ev->n_addrs = ((n_addrs < 0) ? 0 : (n_addrs > MAX_UNWIND_DEPTH) ? MAX_UNWIND_DEPTH : n_addrs);
569 ev->frame.len = sizeof *ev + sizeof (SysprofCaptureAddress) * ev->n_addrs;
570 ev->frame.type = SYSPROF_CAPTURE_FRAME_SAMPLE;
571 ev->frame.cpu = _do_getcpu ();
572 ev->frame.pid = collector->pid;
573 ev->frame.time = SYSPROF_CAPTURE_CURRENT_TIME;
574 ev->tid = collector->tid;
575 ev->padding1 = 0;
576
577 mapped_ring_buffer_advance (collector->buffer, ev->frame.len);
578 }
579
580 } COLLECTOR_END;
581 }
582
583 void
584 sysprof_collector_trace (SysprofBacktraceFunc backtrace_func,
585 void *backtrace_data,
586 bool entering)
587 {
588 COLLECTOR_BEGIN {
589 SysprofCaptureTrace *ev;
590 size_t len;
591
592 len = sizeof *ev + (sizeof (SysprofCaptureTrace) * MAX_UNWIND_DEPTH);
593
594 if ((ev = mapped_ring_buffer_allocate (collector->buffer, len)))
595 {
596 int n_addrs;
597
598 /* See comment from sysprof_collector_allocate(). */
599 if (backtrace_func)
600 n_addrs = backtrace_func (ev->addrs, MAX_UNWIND_DEPTH, backtrace_data);
601 else
602 n_addrs = 0;
603
604 ev->n_addrs = ((n_addrs < 0) ? 0 : (n_addrs > MAX_UNWIND_DEPTH) ? MAX_UNWIND_DEPTH : n_addrs);
605 ev->frame.len = sizeof *ev + sizeof (SysprofCaptureAddress) * ev->n_addrs;
606 ev->frame.type = SYSPROF_CAPTURE_FRAME_TRACE;
607 ev->frame.cpu = _do_getcpu ();
608 ev->frame.pid = collector->pid;
609 ev->frame.time = SYSPROF_CAPTURE_CURRENT_TIME;
610 ev->tid = collector->tid;
611 ev->entering = !!entering;
612 ev->padding1 = 0;
613
614 mapped_ring_buffer_advance (collector->buffer, ev->frame.len);
615 }
616
617 } COLLECTOR_END;
618 }
619
620 void
621 sysprof_collector_mark (int64_t time,
622 int64_t duration,
623 const char *group,
624 const char *mark,
625 const char *message)
626 {
627 COLLECTOR_BEGIN {
628 SysprofCaptureMark *ev;
629 size_t len;
630 size_t sl;
631
632 if (group == NULL)
633 group = "";
634
635 if (mark == NULL)
636 mark = "";
637
638 if (message == NULL)
639 message = "";
640
641 sl = strlen (message);
642 len = realign (sizeof *ev + sl + 1);
643
644 if ((ev = mapped_ring_buffer_allocate (collector->buffer, len)))
645 {
646 ev->frame.len = len;
647 ev->frame.type = SYSPROF_CAPTURE_FRAME_MARK;
648 ev->frame.cpu = _do_getcpu ();
649 ev->frame.pid = collector->pid;
650 ev->frame.time = time;
651 ev->duration = duration;
652 _sysprof_strlcpy (ev->group, group, sizeof ev->group);
653 _sysprof_strlcpy (ev->name, mark, sizeof ev->name);
654 memcpy (ev->message, message, sl);
655 ev->message[sl] = 0;
656
657 mapped_ring_buffer_advance (collector->buffer, ev->frame.len);
658 }
659
660 } COLLECTOR_END;
661 }
662
663 void
664 sysprof_collector_mark_printf (int64_t time,
665 int64_t duration,
666 const char *group,
667 const char *mark,
668 const char *message_format,
669 ...)
670 {
671 va_list args;
672
673 va_start (args, message_format);
674 sysprof_collector_mark_vprintf (time, duration, group, mark, message_format, args);
675 va_end (args);
676 }
677
678 void
679 sysprof_collector_mark_vprintf (int64_t time,
680 int64_t duration,
681 const char *group,
682 const char *mark,
683 const char *message_format,
684 va_list args)
685 {
686 COLLECTOR_BEGIN {
687 SysprofCaptureMark *ev;
688 size_t len;
689 size_t sl;
690 va_list args2;
691
692 /* Need to take a copy of @args since we iterate through it twice, once to
693 * work out the formatted string length, and once to format it. */
694 va_copy (args2, args);
695
696 if (group == NULL)
697 group = "";
698
699 if (mark == NULL)
700 mark = "";
701
702 if (message_format == NULL)
703 message_format = "";
704
705 /* Work out the formatted message length */
706 sl = vsnprintf (NULL, 0, message_format, args);
707
708 len = realign (sizeof *ev + sl + 1);
709
710 if ((ev = mapped_ring_buffer_allocate (collector->buffer, len)))
711 {
712 ev->frame.len = len;
713 ev->frame.type = SYSPROF_CAPTURE_FRAME_MARK;
714 ev->frame.cpu = _do_getcpu ();
715 ev->frame.pid = collector->pid;
716 ev->frame.time = time;
717 ev->duration = duration;
718 _sysprof_strlcpy (ev->group, group, sizeof ev->group);
719 _sysprof_strlcpy (ev->name, mark, sizeof ev->name);
720 vsnprintf (ev->message, sl + 1, message_format, args2);
721 ev->message[sl] = 0;
722
723 mapped_ring_buffer_advance (collector->buffer, ev->frame.len);
724 }
725
726 va_end (args2);
727 } COLLECTOR_END;
728 }
729
730 void
731 sysprof_collector_log (int severity,
732 const char *domain,
733 const char *message)
734 {
735 COLLECTOR_BEGIN {
736 SysprofCaptureLog *ev;
737 size_t len;
738 size_t sl;
739
740 if (domain == NULL)
741 domain = "";
742
743 if (message == NULL)
744 message = "";
745
746 sl = strlen (message);
747 len = realign (sizeof *ev + sl + 1);
748
749 if ((ev = mapped_ring_buffer_allocate (collector->buffer, len)))
750 {
751 ev->frame.len = len;
752 ev->frame.type = SYSPROF_CAPTURE_FRAME_LOG;
753 ev->frame.cpu = _do_getcpu ();
754 ev->frame.pid = collector->pid;
755 ev->frame.time = SYSPROF_CAPTURE_CURRENT_TIME;
756 ev->severity = severity & 0xFFFF;
757 ev->padding1 = 0;
758 ev->padding2 = 0;
759 _sysprof_strlcpy (ev->domain, domain, sizeof ev->domain);
760 memcpy (ev->message, message, sl);
761 ev->message[sl] = 0;
762
763 mapped_ring_buffer_advance (collector->buffer, ev->frame.len);
764 }
765
766 } COLLECTOR_END;
767 }
768
769 void
770 sysprof_collector_log_printf (int severity,
771 const char *domain,
772 const char *format,
773 ...)
774 {
775 COLLECTOR_BEGIN {
776 char formatted[2048];
777 SysprofCaptureLog *ev;
778 va_list args;
779 size_t len;
780 size_t sl;
781
782 va_start (args, format);
783 vsnprintf (formatted, sizeof (formatted), format, args);
784 va_end (args);
785
786 if (domain == NULL)
787 domain = "";
788
789 sl = strlen (formatted);
790 len = realign (sizeof *ev + sl + 1);
791
792 if ((ev = mapped_ring_buffer_allocate (collector->buffer, len)))
793 {
794 ev->frame.len = len;
795 ev->frame.type = SYSPROF_CAPTURE_FRAME_LOG;
796 ev->frame.cpu = _do_getcpu ();
797 ev->frame.pid = collector->pid;
798 ev->frame.time = SYSPROF_CAPTURE_CURRENT_TIME;
799 ev->severity = severity & 0xFFFF;
800 ev->padding1 = 0;
801 ev->padding2 = 0;
802 _sysprof_strlcpy (ev->domain, domain, sizeof ev->domain);
803 memcpy (ev->message, formatted, sl);
804 ev->message[sl] = 0;
805
806 mapped_ring_buffer_advance (collector->buffer, ev->frame.len);
807 }
808 } COLLECTOR_END;
809 }
810
811 void
812 sysprof_collector_define_counters (const SysprofCaptureCounter *counters,
813 unsigned int n_counters)
814 {
815 if (counters == NULL || n_counters == 0)
816 return;
817
818 COLLECTOR_BEGIN {
819 SysprofCaptureCounterDefine *def;
820 size_t len;
821
822 len = realign (sizeof *def + (sizeof *counters * n_counters));
823
824 if ((def = mapped_ring_buffer_allocate (collector->buffer, len)))
825 {
826 def->frame.len = len;
827 def->frame.type = SYSPROF_CAPTURE_FRAME_CTRDEF;
828 def->frame.cpu = _do_getcpu ();
829 def->frame.pid = collector->pid;
830 def->frame.time = SYSPROF_CAPTURE_CURRENT_TIME;
831 def->padding1 = 0;
832 def->padding2 = 0;
833 def->n_counters = n_counters;
834 memcpy (def->counters, counters, sizeof *counters * n_counters);
835
836 mapped_ring_buffer_advance (collector->buffer, def->frame.len);
837 }
838 } COLLECTOR_END;
839 }
840
841 void
842 sysprof_collector_set_counters (const unsigned int *counters_ids,
843 const SysprofCaptureCounterValue *values,
844 unsigned int n_counters)
845 {
846 if (n_counters == 0)
847 return;
848
849 COLLECTOR_BEGIN {
850 SysprofCaptureCounterSet *set;
851 size_t len;
852 unsigned int n_groups;
853 unsigned int group;
854 unsigned int field;
855 unsigned int i;
856
857 /* Determine how many value groups we need */
858 n_groups = n_counters / SYSPROF_N_ELEMENTS (set->values[0].values);
859 if ((n_groups * SYSPROF_N_ELEMENTS (set->values[0].values)) != n_counters)
860 n_groups++;
861
862 len = realign (sizeof *set + (n_groups * sizeof (SysprofCaptureCounterValues)));
863
864 if ((set = mapped_ring_buffer_allocate (collector->buffer, len)))
865 {
866 set->frame.len = len;
867 set->frame.type = SYSPROF_CAPTURE_FRAME_CTRSET;
868 set->frame.cpu = _do_getcpu ();
869 set->frame.pid = collector->pid;
870 set->frame.time = SYSPROF_CAPTURE_CURRENT_TIME;
871 set->padding1 = 0;
872 set->padding2 = 0;
873 set->n_values = n_groups;
874
875 /* Zero all groups upfront as we might not populate them fully, and a
876 * single aligned memset is faster than zeroing the leftover values.
877 */
878 memset (set->values, 0, n_groups * sizeof (SysprofCaptureCounterValues));
879
880 for (i = 0, group = 0, field = 0; i < n_counters; i++)
881 {
882 set->values[group].ids[field] = counters_ids[i];
883 set->values[group].values[field] = values[i];
884
885 field++;
886
887 if (field == SYSPROF_N_ELEMENTS (set->values[0].values))
888 {
889 field = 0;
890 group++;
891 }
892 }
893
894 mapped_ring_buffer_advance (collector->buffer, set->frame.len);
895 }
896 } COLLECTOR_END;
897 }
898
899 unsigned int
900 sysprof_collector_request_counters (unsigned int n_counters)
901 {
902 unsigned int ret = 0;
903
904 if (n_counters == 0)
905 return 0;
906
907 COLLECTOR_BEGIN {
908 ret = collector->next_counter_id;
909 ((SysprofCollector *)collector)->next_counter_id += n_counters;
910 } COLLECTOR_END;
911
912 return ret;
913 }
914
915 bool
916 sysprof_collector_is_active (void)
917 {
918 bool ret = false;
919
920 COLLECTOR_BEGIN {
921 ret = true;
922 } COLLECTOR_END;
923
924 return ret;
925 }