2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Copyright (C) 2011-2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License only.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <lttng/ust-config.h>
25 #include <lttng/ust-ctl.h>
26 #include <lttng/ust-abi.h>
27 #include <lttng/ust-events.h>
28 #include <lttng/ust-endian.h>
29 #include <usterr-signal-safe.h>
33 #include "../libringbuffer/backend.h"
34 #include "../libringbuffer/frontend.h"
35 #include "../liblttng-ust/wait.h"
36 #include "../liblttng-ust/lttng-rb-clients.h"
37 #include "../liblttng-ust/clock.h"
38 #include "../liblttng-ust/getenv.h"
40 #include "../libcounter/shm.h"
41 #include "../libcounter/smp.h"
42 #include "../libcounter/counter.h"
45 * Number of milliseconds to retry before failing metadata writes on
46 * buffer full condition. (10 seconds)
48 #define LTTNG_METADATA_TIMEOUT_MSEC 10000
51 * Channel representation within consumer.
53 struct ustctl_consumer_channel
{
54 struct lttng_channel
*chan
; /* lttng channel buffers */
56 /* initial attributes */
57 struct ustctl_consumer_channel_attr attr
;
58 int wait_fd
; /* monitor close() */
59 int wakeup_fd
; /* monitor close() */
63 * Stream representation within consumer.
65 struct ustctl_consumer_stream
{
66 struct lttng_ust_shm_handle
*handle
; /* shared-memory handle */
67 struct lttng_ust_lib_ring_buffer
*buf
;
68 struct ustctl_consumer_channel
*chan
;
69 int shm_fd
, wait_fd
, wakeup_fd
;
71 uint64_t memory_map_size
;
74 #define USTCTL_COUNTER_ATTR_DIMENSION_MAX 8
75 struct ustctl_counter_attr
{
76 enum ustctl_counter_arithmetic arithmetic
;
77 enum ustctl_counter_bitness bitness
;
78 uint32_t nr_dimensions
;
79 int64_t global_sum_step
;
80 struct ustctl_counter_dimension dimensions
[USTCTL_COUNTER_ATTR_DIMENSION_MAX
];
84 * Counter representation within daemon.
86 struct ustctl_daemon_counter
{
87 struct lib_counter
*counter
;
88 const struct lttng_counter_ops
*ops
;
89 struct ustctl_counter_attr
*attr
; /* initial attributes */
92 extern void lttng_ring_buffer_client_overwrite_init(void);
93 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
94 extern void lttng_ring_buffer_client_discard_init(void);
95 extern void lttng_ring_buffer_client_discard_rt_init(void);
96 extern void lttng_ring_buffer_metadata_client_init(void);
97 extern void lttng_ring_buffer_client_overwrite_exit(void);
98 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
99 extern void lttng_ring_buffer_client_discard_exit(void);
100 extern void lttng_ring_buffer_client_discard_rt_exit(void);
101 extern void lttng_ring_buffer_metadata_client_exit(void);
102 extern void lttng_counter_client_percpu_32_modular_init(void);
103 extern void lttng_counter_client_percpu_32_modular_exit(void);
104 extern void lttng_counter_client_percpu_64_modular_init(void);
105 extern void lttng_counter_client_percpu_64_modular_exit(void);
107 int ustctl_release_handle(int sock
, int handle
)
109 struct ustcomm_ust_msg lum
;
110 struct ustcomm_ust_reply lur
;
112 if (sock
< 0 || handle
< 0)
114 memset(&lum
, 0, sizeof(lum
));
116 lum
.cmd
= LTTNG_UST_RELEASE
;
117 return ustcomm_send_app_cmd(sock
, &lum
, &lur
);
121 * If sock is negative, it means we don't have to notify the other side
122 * (e.g. application has already vanished).
124 int ustctl_release_object(int sock
, struct lttng_ust_object_data
*data
)
131 switch (data
->type
) {
132 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
133 if (data
->u
.channel
.wakeup_fd
>= 0) {
134 ret
= close(data
->u
.channel
.wakeup_fd
);
139 data
->u
.channel
.wakeup_fd
= -1;
141 free(data
->u
.channel
.data
);
142 data
->u
.channel
.data
= NULL
;
144 case LTTNG_UST_OBJECT_TYPE_STREAM
:
145 if (data
->u
.stream
.shm_fd
>= 0) {
146 ret
= close(data
->u
.stream
.shm_fd
);
151 data
->u
.stream
.shm_fd
= -1;
153 if (data
->u
.stream
.wakeup_fd
>= 0) {
154 ret
= close(data
->u
.stream
.wakeup_fd
);
159 data
->u
.stream
.wakeup_fd
= -1;
162 case LTTNG_UST_OBJECT_TYPE_EVENT
:
163 case LTTNG_UST_OBJECT_TYPE_CONTEXT
:
164 case LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER_GROUP
:
165 case LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER
:
167 case LTTNG_UST_OBJECT_TYPE_COUNTER
:
168 free(data
->u
.counter
.data
);
169 data
->u
.counter
.data
= NULL
;
171 case LTTNG_UST_OBJECT_TYPE_COUNTER_GLOBAL
:
172 if (data
->u
.counter_global
.shm_fd
>= 0) {
173 ret
= close(data
->u
.counter_global
.shm_fd
);
178 data
->u
.counter_global
.shm_fd
= -1;
181 case LTTNG_UST_OBJECT_TYPE_COUNTER_CPU
:
182 if (data
->u
.counter_cpu
.shm_fd
>= 0) {
183 ret
= close(data
->u
.counter_cpu
.shm_fd
);
188 data
->u
.counter_cpu
.shm_fd
= -1;
194 return ustctl_release_handle(sock
, data
->handle
);
198 * Send registration done packet to the application.
200 int ustctl_register_done(int sock
)
202 struct ustcomm_ust_msg lum
;
203 struct ustcomm_ust_reply lur
;
206 DBG("Sending register done command to %d", sock
);
207 memset(&lum
, 0, sizeof(lum
));
208 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
209 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
210 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
217 * returns session handle.
219 int ustctl_create_session(int sock
)
221 struct ustcomm_ust_msg lum
;
222 struct ustcomm_ust_reply lur
;
223 int ret
, session_handle
;
226 memset(&lum
, 0, sizeof(lum
));
227 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
228 lum
.cmd
= LTTNG_UST_SESSION
;
229 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
232 session_handle
= lur
.ret_val
;
233 DBG("received session handle %u", session_handle
);
234 return session_handle
;
237 int ustctl_create_event(int sock
, struct lttng_ust_event
*ev
,
238 struct lttng_ust_object_data
*channel_data
,
239 struct lttng_ust_object_data
**_event_data
)
241 struct ustcomm_ust_msg lum
;
242 struct ustcomm_ust_reply lur
;
243 struct lttng_ust_object_data
*event_data
;
246 if (!channel_data
|| !_event_data
)
249 event_data
= zmalloc(sizeof(*event_data
));
252 event_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT
;
253 memset(&lum
, 0, sizeof(lum
));
254 lum
.handle
= channel_data
->handle
;
255 lum
.cmd
= LTTNG_UST_EVENT
;
256 strncpy(lum
.u
.event
.name
, ev
->name
,
257 LTTNG_UST_SYM_NAME_LEN
);
258 lum
.u
.event
.instrumentation
= ev
->instrumentation
;
259 lum
.u
.event
.loglevel_type
= ev
->loglevel_type
;
260 lum
.u
.event
.loglevel
= ev
->loglevel
;
261 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
266 event_data
->handle
= lur
.ret_val
;
267 DBG("received event handle %u", event_data
->handle
);
268 *_event_data
= event_data
;
272 int ustctl_add_context(int sock
, struct lttng_ust_context_attr
*ctx
,
273 struct lttng_ust_object_data
*obj_data
,
274 struct lttng_ust_object_data
**_context_data
)
276 struct ustcomm_ust_msg lum
;
277 struct ustcomm_ust_reply lur
;
278 struct lttng_ust_object_data
*context_data
= NULL
;
283 if (!obj_data
|| !_context_data
) {
288 context_data
= zmalloc(sizeof(*context_data
));
293 context_data
->type
= LTTNG_UST_OBJECT_TYPE_CONTEXT
;
294 memset(&lum
, 0, sizeof(lum
));
295 lum
.handle
= obj_data
->handle
;
296 lum
.cmd
= LTTNG_UST_CONTEXT
;
298 lum
.u
.context
.ctx
= ctx
->ctx
;
300 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
301 lum
.u
.context
.u
.perf_counter
= ctx
->u
.perf_counter
;
303 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
305 size_t provider_name_len
= strlen(
306 ctx
->u
.app_ctx
.provider_name
) + 1;
307 size_t ctx_name_len
= strlen(ctx
->u
.app_ctx
.ctx_name
) + 1;
309 lum
.u
.context
.u
.app_ctx
.provider_name_len
= provider_name_len
;
310 lum
.u
.context
.u
.app_ctx
.ctx_name_len
= ctx_name_len
;
312 len
= provider_name_len
+ ctx_name_len
;
318 memcpy(buf
, ctx
->u
.app_ctx
.provider_name
,
320 memcpy(buf
+ provider_name_len
, ctx
->u
.app_ctx
.ctx_name
,
327 ret
= ustcomm_send_app_msg(sock
, &lum
);
331 /* send var len ctx_name */
332 ret
= ustcomm_send_unix_sock(sock
, buf
, len
);
341 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
345 context_data
->handle
= -1;
346 DBG("Context created successfully");
347 *_context_data
= context_data
;
355 int ustctl_set_filter(int sock
, struct lttng_ust_filter_bytecode
*bytecode
,
356 struct lttng_ust_object_data
*obj_data
)
358 struct ustcomm_ust_msg lum
;
359 struct ustcomm_ust_reply lur
;
365 memset(&lum
, 0, sizeof(lum
));
366 lum
.handle
= obj_data
->handle
;
367 lum
.cmd
= LTTNG_UST_FILTER
;
368 lum
.u
.filter
.data_size
= bytecode
->len
;
369 lum
.u
.filter
.reloc_offset
= bytecode
->reloc_offset
;
370 lum
.u
.filter
.seqnum
= bytecode
->seqnum
;
372 ret
= ustcomm_send_app_msg(sock
, &lum
);
375 /* send var len bytecode */
376 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
381 if (ret
!= bytecode
->len
)
383 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
386 int ustctl_set_capture(int sock
, struct lttng_ust_capture_bytecode
*bytecode
,
387 struct lttng_ust_object_data
*obj_data
)
389 struct ustcomm_ust_msg lum
;
390 struct ustcomm_ust_reply lur
;
396 memset(&lum
, 0, sizeof(lum
));
397 lum
.handle
= obj_data
->handle
;
398 lum
.cmd
= LTTNG_UST_CAPTURE
;
399 lum
.u
.capture
.data_size
= bytecode
->len
;
400 lum
.u
.capture
.reloc_offset
= bytecode
->reloc_offset
;
401 lum
.u
.capture
.seqnum
= bytecode
->seqnum
;
403 ret
= ustcomm_send_app_msg(sock
, &lum
);
406 /* send var len bytecode */
407 ret
= ustcomm_send_unix_sock(sock
, bytecode
->data
,
412 if (ret
!= bytecode
->len
)
414 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
417 int ustctl_set_exclusion(int sock
, struct lttng_ust_event_exclusion
*exclusion
,
418 struct lttng_ust_object_data
*obj_data
)
420 struct ustcomm_ust_msg lum
;
421 struct ustcomm_ust_reply lur
;
428 memset(&lum
, 0, sizeof(lum
));
429 lum
.handle
= obj_data
->handle
;
430 lum
.cmd
= LTTNG_UST_EXCLUSION
;
431 lum
.u
.exclusion
.count
= exclusion
->count
;
433 ret
= ustcomm_send_app_msg(sock
, &lum
);
438 /* send var len exclusion names */
439 ret
= ustcomm_send_unix_sock(sock
,
441 exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
);
445 if (ret
!= exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) {
448 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
451 /* Enable event, channel and session ioctl */
452 int ustctl_enable(int sock
, struct lttng_ust_object_data
*object
)
454 struct ustcomm_ust_msg lum
;
455 struct ustcomm_ust_reply lur
;
461 memset(&lum
, 0, sizeof(lum
));
462 lum
.handle
= object
->handle
;
463 lum
.cmd
= LTTNG_UST_ENABLE
;
464 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
467 DBG("enabled handle %u", object
->handle
);
471 /* Disable event, channel and session ioctl */
472 int ustctl_disable(int sock
, struct lttng_ust_object_data
*object
)
474 struct ustcomm_ust_msg lum
;
475 struct ustcomm_ust_reply lur
;
481 memset(&lum
, 0, sizeof(lum
));
482 lum
.handle
= object
->handle
;
483 lum
.cmd
= LTTNG_UST_DISABLE
;
484 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
487 DBG("disable handle %u", object
->handle
);
491 int ustctl_start_session(int sock
, int handle
)
493 struct lttng_ust_object_data obj
;
496 return ustctl_enable(sock
, &obj
);
499 int ustctl_stop_session(int sock
, int handle
)
501 struct lttng_ust_object_data obj
;
504 return ustctl_disable(sock
, &obj
);
507 int ustctl_create_event_notifier_group(int sock
, int pipe_fd
,
508 struct lttng_ust_object_data
**_event_notifier_group_data
)
510 struct lttng_ust_object_data
*event_notifier_group_data
;
511 struct ustcomm_ust_msg lum
;
512 struct ustcomm_ust_reply lur
;
516 if (!_event_notifier_group_data
)
519 event_notifier_group_data
= zmalloc(sizeof(*event_notifier_group_data
));
520 if (!event_notifier_group_data
)
523 event_notifier_group_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER_GROUP
;
525 memset(&lum
, 0, sizeof(lum
));
526 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
527 lum
.cmd
= LTTNG_UST_EVENT_NOTIFIER_GROUP_CREATE
;
529 ret
= ustcomm_send_app_msg(sock
, &lum
);
533 /* Send event_notifier notification pipe. */
534 len
= ustcomm_send_fds_unix_sock(sock
, &pipe_fd
, 1);
540 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
544 event_notifier_group_data
->handle
= lur
.ret_val
;
545 DBG("received event_notifier group handle %d", event_notifier_group_data
->handle
);
547 *_event_notifier_group_data
= event_notifier_group_data
;
552 free(event_notifier_group_data
);
558 int ustctl_create_event_notifier(int sock
, struct lttng_ust_event_notifier
*event_notifier
,
559 struct lttng_ust_object_data
*event_notifier_group
,
560 struct lttng_ust_object_data
**_event_notifier_data
)
562 struct ustcomm_ust_msg lum
;
563 struct ustcomm_ust_reply lur
;
564 struct lttng_ust_object_data
*event_notifier_data
;
568 if (!event_notifier_group
|| !_event_notifier_data
)
571 event_notifier_data
= zmalloc(sizeof(*event_notifier_data
));
572 if (!event_notifier_data
)
575 event_notifier_data
->type
= LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER
;
577 memset(&lum
, 0, sizeof(lum
));
578 lum
.handle
= event_notifier_group
->handle
;
579 lum
.cmd
= LTTNG_UST_EVENT_NOTIFIER_CREATE
;
580 lum
.u
.event_notifier
.len
= sizeof(*event_notifier
);
582 ret
= ustcomm_send_app_msg(sock
, &lum
);
584 free(event_notifier_data
);
587 /* Send struct lttng_ust_event_notifier */
588 len
= ustcomm_send_unix_sock(sock
, event_notifier
, sizeof(*event_notifier
));
589 if (len
!= sizeof(*event_notifier
)) {
595 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
597 free(event_notifier_data
);
600 event_notifier_data
->handle
= lur
.ret_val
;
601 DBG("received event_notifier handle %u", event_notifier_data
->handle
);
602 *_event_notifier_data
= event_notifier_data
;
607 int ustctl_tracepoint_list(int sock
)
609 struct ustcomm_ust_msg lum
;
610 struct ustcomm_ust_reply lur
;
611 int ret
, tp_list_handle
;
613 memset(&lum
, 0, sizeof(lum
));
614 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
615 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST
;
616 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
619 tp_list_handle
= lur
.ret_val
;
620 DBG("received tracepoint list handle %u", tp_list_handle
);
621 return tp_list_handle
;
624 int ustctl_tracepoint_list_get(int sock
, int tp_list_handle
,
625 struct lttng_ust_tracepoint_iter
*iter
)
627 struct ustcomm_ust_msg lum
;
628 struct ustcomm_ust_reply lur
;
634 memset(&lum
, 0, sizeof(lum
));
635 lum
.handle
= tp_list_handle
;
636 lum
.cmd
= LTTNG_UST_TRACEPOINT_LIST_GET
;
637 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
640 DBG("received tracepoint list entry name %s loglevel %d",
641 lur
.u
.tracepoint
.name
,
642 lur
.u
.tracepoint
.loglevel
);
643 memcpy(iter
, &lur
.u
.tracepoint
, sizeof(*iter
));
647 int ustctl_tracepoint_field_list(int sock
)
649 struct ustcomm_ust_msg lum
;
650 struct ustcomm_ust_reply lur
;
651 int ret
, tp_field_list_handle
;
653 memset(&lum
, 0, sizeof(lum
));
654 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
655 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST
;
656 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
659 tp_field_list_handle
= lur
.ret_val
;
660 DBG("received tracepoint field list handle %u", tp_field_list_handle
);
661 return tp_field_list_handle
;
664 int ustctl_tracepoint_field_list_get(int sock
, int tp_field_list_handle
,
665 struct lttng_ust_field_iter
*iter
)
667 struct ustcomm_ust_msg lum
;
668 struct ustcomm_ust_reply lur
;
675 memset(&lum
, 0, sizeof(lum
));
676 lum
.handle
= tp_field_list_handle
;
677 lum
.cmd
= LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
;
678 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
681 len
= ustcomm_recv_unix_sock(sock
, iter
, sizeof(*iter
));
682 if (len
!= sizeof(*iter
)) {
685 DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d",
693 int ustctl_tracer_version(int sock
, struct lttng_ust_tracer_version
*v
)
695 struct ustcomm_ust_msg lum
;
696 struct ustcomm_ust_reply lur
;
702 memset(&lum
, 0, sizeof(lum
));
703 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
704 lum
.cmd
= LTTNG_UST_TRACER_VERSION
;
705 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
708 memcpy(v
, &lur
.u
.version
, sizeof(*v
));
709 DBG("received tracer version");
713 int ustctl_wait_quiescent(int sock
)
715 struct ustcomm_ust_msg lum
;
716 struct ustcomm_ust_reply lur
;
719 memset(&lum
, 0, sizeof(lum
));
720 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
721 lum
.cmd
= LTTNG_UST_WAIT_QUIESCENT
;
722 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
725 DBG("waited for quiescent state");
729 int ustctl_calibrate(int sock
, struct lttng_ust_calibrate
*calibrate
)
737 int ustctl_sock_flush_buffer(int sock
, struct lttng_ust_object_data
*object
)
739 struct ustcomm_ust_msg lum
;
740 struct ustcomm_ust_reply lur
;
746 memset(&lum
, 0, sizeof(lum
));
747 lum
.handle
= object
->handle
;
748 lum
.cmd
= LTTNG_UST_FLUSH_BUFFER
;
749 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
752 DBG("flushed buffer handle %u", object
->handle
);
757 int ustctl_send_channel(int sock
,
758 enum lttng_ust_chan_type type
,
768 len
= ustcomm_send_unix_sock(sock
, &size
, sizeof(size
));
769 if (len
!= sizeof(size
)) {
776 /* Send channel type */
777 len
= ustcomm_send_unix_sock(sock
, &type
, sizeof(type
));
778 if (len
!= sizeof(type
)) {
786 /* Send channel data */
787 len
= ustcomm_send_unix_sock(sock
, data
, size
);
796 len
= ustcomm_send_fds_unix_sock(sock
, &wakeup_fd
, 1);
807 int ustctl_send_stream(int sock
,
809 uint64_t memory_map_size
,
810 int shm_fd
, int wakeup_fd
,
818 /* finish iteration */
821 len
= ustcomm_send_unix_sock(sock
, &v
, sizeof(v
));
822 if (len
!= sizeof(v
)) {
832 len
= ustcomm_send_unix_sock(sock
, &memory_map_size
,
833 sizeof(memory_map_size
));
834 if (len
!= sizeof(memory_map_size
)) {
842 len
= ustcomm_send_unix_sock(sock
, &stream_nr
,
844 if (len
!= sizeof(stream_nr
)) {
852 /* Send shm fd and wakeup fd */
855 len
= ustcomm_send_fds_unix_sock(sock
, fds
, 2);
865 int ustctl_recv_channel_from_consumer(int sock
,
866 struct lttng_ust_object_data
**_channel_data
)
868 struct lttng_ust_object_data
*channel_data
;
873 channel_data
= zmalloc(sizeof(*channel_data
));
878 channel_data
->type
= LTTNG_UST_OBJECT_TYPE_CHANNEL
;
879 channel_data
->handle
= -1;
882 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->size
,
883 sizeof(channel_data
->size
));
884 if (len
!= sizeof(channel_data
->size
)) {
892 /* recv channel type */
893 len
= ustcomm_recv_unix_sock(sock
, &channel_data
->u
.channel
.type
,
894 sizeof(channel_data
->u
.channel
.type
));
895 if (len
!= sizeof(channel_data
->u
.channel
.type
)) {
903 /* recv channel data */
904 channel_data
->u
.channel
.data
= zmalloc(channel_data
->size
);
905 if (!channel_data
->u
.channel
.data
) {
909 len
= ustcomm_recv_unix_sock(sock
, channel_data
->u
.channel
.data
,
911 if (len
!= channel_data
->size
) {
916 goto error_recv_data
;
919 len
= ustcomm_recv_fds_unix_sock(sock
, &wakeup_fd
, 1);
923 goto error_recv_data
;
926 goto error_recv_data
;
929 channel_data
->u
.channel
.wakeup_fd
= wakeup_fd
;
930 *_channel_data
= channel_data
;
934 free(channel_data
->u
.channel
.data
);
941 int ustctl_recv_stream_from_consumer(int sock
,
942 struct lttng_ust_object_data
**_stream_data
)
944 struct lttng_ust_object_data
*stream_data
;
949 stream_data
= zmalloc(sizeof(*stream_data
));
955 stream_data
->type
= LTTNG_UST_OBJECT_TYPE_STREAM
;
956 stream_data
->handle
= -1;
959 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->size
,
960 sizeof(stream_data
->size
));
961 if (len
!= sizeof(stream_data
->size
)) {
968 if (stream_data
->size
== -1) {
969 ret
= -LTTNG_UST_ERR_NOENT
;
974 len
= ustcomm_recv_unix_sock(sock
, &stream_data
->u
.stream
.stream_nr
,
975 sizeof(stream_data
->u
.stream
.stream_nr
));
976 if (len
!= sizeof(stream_data
->u
.stream
.stream_nr
)) {
984 /* recv shm fd and wakeup fd */
985 len
= ustcomm_recv_fds_unix_sock(sock
, fds
, 2);
995 stream_data
->u
.stream
.shm_fd
= fds
[0];
996 stream_data
->u
.stream
.wakeup_fd
= fds
[1];
997 *_stream_data
= stream_data
;
1006 int ustctl_send_channel_to_ust(int sock
, int session_handle
,
1007 struct lttng_ust_object_data
*channel_data
)
1009 struct ustcomm_ust_msg lum
;
1010 struct ustcomm_ust_reply lur
;
1016 memset(&lum
, 0, sizeof(lum
));
1017 lum
.handle
= session_handle
;
1018 lum
.cmd
= LTTNG_UST_CHANNEL
;
1019 lum
.u
.channel
.len
= channel_data
->size
;
1020 lum
.u
.channel
.type
= channel_data
->u
.channel
.type
;
1021 ret
= ustcomm_send_app_msg(sock
, &lum
);
1025 ret
= ustctl_send_channel(sock
,
1026 channel_data
->u
.channel
.type
,
1027 channel_data
->u
.channel
.data
,
1029 channel_data
->u
.channel
.wakeup_fd
,
1033 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
1035 channel_data
->handle
= lur
.ret_val
;
1040 int ustctl_send_stream_to_ust(int sock
,
1041 struct lttng_ust_object_data
*channel_data
,
1042 struct lttng_ust_object_data
*stream_data
)
1044 struct ustcomm_ust_msg lum
;
1045 struct ustcomm_ust_reply lur
;
1048 memset(&lum
, 0, sizeof(lum
));
1049 lum
.handle
= channel_data
->handle
;
1050 lum
.cmd
= LTTNG_UST_STREAM
;
1051 lum
.u
.stream
.len
= stream_data
->size
;
1052 lum
.u
.stream
.stream_nr
= stream_data
->u
.stream
.stream_nr
;
1053 ret
= ustcomm_send_app_msg(sock
, &lum
);
1057 assert(stream_data
);
1058 assert(stream_data
->type
== LTTNG_UST_OBJECT_TYPE_STREAM
);
1060 ret
= ustctl_send_stream(sock
,
1061 stream_data
->u
.stream
.stream_nr
,
1063 stream_data
->u
.stream
.shm_fd
,
1064 stream_data
->u
.stream
.wakeup_fd
, 1);
1067 return ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
1070 int ustctl_duplicate_ust_object_data(struct lttng_ust_object_data
**dest
,
1071 struct lttng_ust_object_data
*src
)
1073 struct lttng_ust_object_data
*obj
;
1076 if (src
->handle
!= -1) {
1081 obj
= zmalloc(sizeof(*obj
));
1087 obj
->type
= src
->type
;
1088 obj
->handle
= src
->handle
;
1089 obj
->size
= src
->size
;
1091 switch (obj
->type
) {
1092 case LTTNG_UST_OBJECT_TYPE_CHANNEL
:
1094 obj
->u
.channel
.type
= src
->u
.channel
.type
;
1095 if (src
->u
.channel
.wakeup_fd
>= 0) {
1096 obj
->u
.channel
.wakeup_fd
=
1097 dup(src
->u
.channel
.wakeup_fd
);
1098 if (obj
->u
.channel
.wakeup_fd
< 0) {
1100 goto chan_error_wakeup_fd
;
1103 obj
->u
.channel
.wakeup_fd
=
1104 src
->u
.channel
.wakeup_fd
;
1106 obj
->u
.channel
.data
= zmalloc(obj
->size
);
1107 if (!obj
->u
.channel
.data
) {
1109 goto chan_error_alloc
;
1111 memcpy(obj
->u
.channel
.data
, src
->u
.channel
.data
, obj
->size
);
1115 if (src
->u
.channel
.wakeup_fd
>= 0) {
1118 closeret
= close(obj
->u
.channel
.wakeup_fd
);
1123 chan_error_wakeup_fd
:
1128 case LTTNG_UST_OBJECT_TYPE_STREAM
:
1130 obj
->u
.stream
.stream_nr
= src
->u
.stream
.stream_nr
;
1131 if (src
->u
.stream
.wakeup_fd
>= 0) {
1132 obj
->u
.stream
.wakeup_fd
=
1133 dup(src
->u
.stream
.wakeup_fd
);
1134 if (obj
->u
.stream
.wakeup_fd
< 0) {
1136 goto stream_error_wakeup_fd
;
1139 obj
->u
.stream
.wakeup_fd
=
1140 src
->u
.stream
.wakeup_fd
;
1143 if (src
->u
.stream
.shm_fd
>= 0) {
1144 obj
->u
.stream
.shm_fd
=
1145 dup(src
->u
.stream
.shm_fd
);
1146 if (obj
->u
.stream
.shm_fd
< 0) {
1148 goto stream_error_shm_fd
;
1151 obj
->u
.stream
.shm_fd
=
1152 src
->u
.stream
.shm_fd
;
1156 stream_error_shm_fd
:
1157 if (src
->u
.stream
.wakeup_fd
>= 0) {
1160 closeret
= close(obj
->u
.stream
.wakeup_fd
);
1165 stream_error_wakeup_fd
:
1169 case LTTNG_UST_OBJECT_TYPE_COUNTER
:
1171 obj
->u
.counter
.data
= zmalloc(obj
->size
);
1172 if (!obj
->u
.counter
.data
) {
1176 memcpy(obj
->u
.counter
.data
, src
->u
.counter
.data
, obj
->size
);
1180 case LTTNG_UST_OBJECT_TYPE_COUNTER_GLOBAL
:
1182 if (src
->u
.counter_global
.shm_fd
>= 0) {
1183 obj
->u
.counter_global
.shm_fd
=
1184 dup(src
->u
.counter_global
.shm_fd
);
1185 if (obj
->u
.counter_global
.shm_fd
< 0) {
1193 case LTTNG_UST_OBJECT_TYPE_COUNTER_CPU
:
1195 obj
->u
.counter_cpu
.cpu_nr
= src
->u
.counter_cpu
.cpu_nr
;
1196 if (src
->u
.counter_cpu
.shm_fd
>= 0) {
1197 obj
->u
.counter_cpu
.shm_fd
=
1198 dup(src
->u
.counter_cpu
.shm_fd
);
1199 if (obj
->u
.counter_cpu
.shm_fd
< 0) {
1222 /* Buffer operations */
1224 int ustctl_get_nr_stream_per_channel(void)
1226 return num_possible_cpus();
1229 struct ustctl_consumer_channel
*
1230 ustctl_create_channel(struct ustctl_consumer_channel_attr
*attr
,
1231 const int *stream_fds
, int nr_stream_fds
)
1233 struct ustctl_consumer_channel
*chan
;
1234 const char *transport_name
;
1235 struct lttng_transport
*transport
;
1237 switch (attr
->type
) {
1238 case LTTNG_UST_CHAN_PER_CPU
:
1239 if (attr
->output
== LTTNG_UST_MMAP
) {
1240 if (attr
->overwrite
) {
1241 if (attr
->read_timer_interval
== 0) {
1242 transport_name
= "relay-overwrite-mmap";
1244 transport_name
= "relay-overwrite-rt-mmap";
1247 if (attr
->read_timer_interval
== 0) {
1248 transport_name
= "relay-discard-mmap";
1250 transport_name
= "relay-discard-rt-mmap";
1257 case LTTNG_UST_CHAN_METADATA
:
1258 if (attr
->output
== LTTNG_UST_MMAP
)
1259 transport_name
= "relay-metadata-mmap";
1264 transport_name
= "<unknown>";
1268 transport
= lttng_transport_find(transport_name
);
1270 DBG("LTTng transport %s not found\n",
1275 chan
= zmalloc(sizeof(*chan
));
1279 chan
->chan
= transport
->ops
.channel_create(transport_name
, NULL
,
1280 attr
->subbuf_size
, attr
->num_subbuf
,
1281 attr
->switch_timer_interval
,
1282 attr
->read_timer_interval
,
1283 attr
->uuid
, attr
->chan_id
,
1284 stream_fds
, nr_stream_fds
,
1285 attr
->blocking_timeout
);
1289 chan
->chan
->ops
= &transport
->ops
;
1290 memcpy(&chan
->attr
, attr
, sizeof(chan
->attr
));
1291 chan
->wait_fd
= ustctl_channel_get_wait_fd(chan
);
1292 chan
->wakeup_fd
= ustctl_channel_get_wakeup_fd(chan
);
1300 void ustctl_destroy_channel(struct ustctl_consumer_channel
*chan
)
1302 (void) ustctl_channel_close_wait_fd(chan
);
1303 (void) ustctl_channel_close_wakeup_fd(chan
);
1304 chan
->chan
->ops
->channel_destroy(chan
->chan
);
1308 int ustctl_send_channel_to_sessiond(int sock
,
1309 struct ustctl_consumer_channel
*channel
)
1311 struct shm_object_table
*table
;
1313 table
= channel
->chan
->handle
->table
;
1314 if (table
->size
<= 0)
1316 return ustctl_send_channel(sock
,
1318 table
->objects
[0].memory_map
,
1319 table
->objects
[0].memory_map_size
,
1324 int ustctl_send_stream_to_sessiond(int sock
,
1325 struct ustctl_consumer_stream
*stream
)
1328 return ustctl_send_stream(sock
, -1U, -1U, -1, -1, 0);
1330 return ustctl_send_stream(sock
,
1332 stream
->memory_map_size
,
1333 stream
->shm_fd
, stream
->wakeup_fd
,
1337 int ustctl_write_metadata_to_channel(
1338 struct ustctl_consumer_channel
*channel
,
1339 const char *metadata_str
, /* NOT null-terminated */
1340 size_t len
) /* metadata length */
1342 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1343 struct lttng_channel
*chan
= channel
->chan
;
1344 const char *str
= metadata_str
;
1345 int ret
= 0, waitret
;
1346 size_t reserve_len
, pos
;
1348 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
1349 reserve_len
= min_t(size_t,
1350 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1352 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1353 sizeof(char), -1, chan
->handle
, NULL
);
1355 * We don't care about metadata buffer's records lost
1356 * count, because we always retry here. Report error if
1357 * we need to bail out after timeout or being
1360 waitret
= wait_cond_interruptible_timeout(
1362 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1363 ret
!= -ENOBUFS
|| !ret
;
1365 LTTNG_METADATA_TIMEOUT_MSEC
);
1366 if (waitret
== -ETIMEDOUT
|| waitret
== -EINTR
|| ret
) {
1367 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
1368 waitret
== -EINTR
? "interrupted" :
1369 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
1370 if (waitret
== -EINTR
)
1374 chan
->ops
->event_write(&ctx
, &str
[pos
], reserve_len
);
1375 chan
->ops
->event_commit(&ctx
);
1382 * Write at most one packet in the channel.
1383 * Returns the number of bytes written on success, < 0 on error.
1385 ssize_t
ustctl_write_one_packet_to_channel(
1386 struct ustctl_consumer_channel
*channel
,
1387 const char *metadata_str
, /* NOT null-terminated */
1388 size_t len
) /* metadata length */
1390 struct lttng_ust_lib_ring_buffer_ctx ctx
;
1391 struct lttng_channel
*chan
= channel
->chan
;
1392 const char *str
= metadata_str
;
1393 ssize_t reserve_len
;
1396 reserve_len
= min_t(ssize_t
,
1397 chan
->ops
->packet_avail_size(chan
->chan
, chan
->handle
),
1399 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
1400 sizeof(char), -1, chan
->handle
, NULL
);
1401 ret
= chan
->ops
->event_reserve(&ctx
, 0);
1403 DBG("LTTng: event reservation failed");
1408 chan
->ops
->event_write(&ctx
, str
, reserve_len
);
1409 chan
->ops
->event_commit(&ctx
);
1415 int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel
*consumer_chan
)
1417 struct channel
*chan
;
1420 chan
= consumer_chan
->chan
->chan
;
1421 ret
= ring_buffer_channel_close_wait_fd(&chan
->backend
.config
,
1422 chan
, chan
->handle
);
1424 consumer_chan
->wait_fd
= -1;
1428 int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel
*consumer_chan
)
1430 struct channel
*chan
;
1433 chan
= consumer_chan
->chan
->chan
;
1434 ret
= ring_buffer_channel_close_wakeup_fd(&chan
->backend
.config
,
1435 chan
, chan
->handle
);
1437 consumer_chan
->wakeup_fd
= -1;
1441 int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream
*stream
)
1443 struct channel
*chan
;
1445 chan
= stream
->chan
->chan
->chan
;
1446 return ring_buffer_stream_close_wait_fd(&chan
->backend
.config
,
1447 chan
, stream
->handle
, stream
->cpu
);
1450 int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1452 struct channel
*chan
;
1454 chan
= stream
->chan
->chan
->chan
;
1455 return ring_buffer_stream_close_wakeup_fd(&chan
->backend
.config
,
1456 chan
, stream
->handle
, stream
->cpu
);
1459 struct ustctl_consumer_stream
*
1460 ustctl_create_stream(struct ustctl_consumer_channel
*channel
,
1463 struct ustctl_consumer_stream
*stream
;
1464 struct lttng_ust_shm_handle
*handle
;
1465 struct channel
*chan
;
1466 int shm_fd
, wait_fd
, wakeup_fd
;
1467 uint64_t memory_map_size
;
1468 struct lttng_ust_lib_ring_buffer
*buf
;
1473 handle
= channel
->chan
->handle
;
1477 chan
= channel
->chan
->chan
;
1478 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
1479 chan
, cpu
, handle
, &shm_fd
, &wait_fd
,
1480 &wakeup_fd
, &memory_map_size
);
1483 ret
= lib_ring_buffer_open_read(buf
, handle
);
1487 stream
= zmalloc(sizeof(*stream
));
1490 stream
->handle
= handle
;
1492 stream
->chan
= channel
;
1493 stream
->shm_fd
= shm_fd
;
1494 stream
->wait_fd
= wait_fd
;
1495 stream
->wakeup_fd
= wakeup_fd
;
1496 stream
->memory_map_size
= memory_map_size
;
1504 void ustctl_destroy_stream(struct ustctl_consumer_stream
*stream
)
1506 struct lttng_ust_lib_ring_buffer
*buf
;
1507 struct ustctl_consumer_channel
*consumer_chan
;
1511 consumer_chan
= stream
->chan
;
1512 (void) ustctl_stream_close_wait_fd(stream
);
1513 (void) ustctl_stream_close_wakeup_fd(stream
);
1514 lib_ring_buffer_release_read(buf
, consumer_chan
->chan
->handle
);
1518 int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel
*chan
)
1522 return shm_get_wait_fd(chan
->chan
->handle
,
1523 &chan
->chan
->handle
->chan
._ref
);
1526 int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel
*chan
)
1530 return shm_get_wakeup_fd(chan
->chan
->handle
,
1531 &chan
->chan
->handle
->chan
._ref
);
1534 int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream
*stream
)
1536 struct lttng_ust_lib_ring_buffer
*buf
;
1537 struct ustctl_consumer_channel
*consumer_chan
;
1542 consumer_chan
= stream
->chan
;
1543 return shm_get_wait_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1546 int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream
*stream
)
1548 struct lttng_ust_lib_ring_buffer
*buf
;
1549 struct ustctl_consumer_channel
*consumer_chan
;
1554 consumer_chan
= stream
->chan
;
1555 return shm_get_wakeup_fd(consumer_chan
->chan
->handle
, &buf
->self
._ref
);
1558 /* For mmap mode, readable without "get" operation */
1560 void *ustctl_get_mmap_base(struct ustctl_consumer_stream
*stream
)
1562 struct lttng_ust_lib_ring_buffer
*buf
;
1563 struct ustctl_consumer_channel
*consumer_chan
;
1568 consumer_chan
= stream
->chan
;
1569 return shmp(consumer_chan
->chan
->handle
, buf
->backend
.memory_map
);
1572 /* returns the length to mmap. */
1573 int ustctl_get_mmap_len(struct ustctl_consumer_stream
*stream
,
1576 struct ustctl_consumer_channel
*consumer_chan
;
1577 unsigned long mmap_buf_len
;
1578 struct channel
*chan
;
1582 consumer_chan
= stream
->chan
;
1583 chan
= consumer_chan
->chan
->chan
;
1584 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1586 mmap_buf_len
= chan
->backend
.buf_size
;
1587 if (chan
->backend
.extra_reader_sb
)
1588 mmap_buf_len
+= chan
->backend
.subbuf_size
;
1589 if (mmap_buf_len
> INT_MAX
)
1591 *len
= mmap_buf_len
;
1595 /* returns the maximum size for sub-buffers. */
1596 int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream
*stream
,
1599 struct ustctl_consumer_channel
*consumer_chan
;
1600 struct channel
*chan
;
1604 consumer_chan
= stream
->chan
;
1605 chan
= consumer_chan
->chan
->chan
;
1606 *len
= chan
->backend
.subbuf_size
;
1611 * For mmap mode, operate on the current packet (between get/put or
1612 * get_next/put_next).
1615 /* returns the offset of the subbuffer belonging to the mmap reader. */
1616 int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream
*stream
,
1619 struct channel
*chan
;
1620 unsigned long sb_bindex
;
1621 struct lttng_ust_lib_ring_buffer
*buf
;
1622 struct ustctl_consumer_channel
*consumer_chan
;
1623 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*barray_idx
;
1624 struct lttng_ust_lib_ring_buffer_backend_pages
*pages
;
1629 consumer_chan
= stream
->chan
;
1630 chan
= consumer_chan
->chan
->chan
;
1631 if (chan
->backend
.config
.output
!= RING_BUFFER_MMAP
)
1633 sb_bindex
= subbuffer_id_get_index(&chan
->backend
.config
,
1634 buf
->backend
.buf_rsb
.id
);
1635 barray_idx
= shmp_index(consumer_chan
->chan
->handle
, buf
->backend
.array
,
1639 pages
= shmp(consumer_chan
->chan
->handle
, barray_idx
->shmp
);
1642 *off
= pages
->mmap_offset
;
1646 /* returns the size of the current sub-buffer, without padding (for mmap). */
1647 int ustctl_get_subbuf_size(struct ustctl_consumer_stream
*stream
,
1650 struct ustctl_consumer_channel
*consumer_chan
;
1651 struct channel
*chan
;
1652 struct lttng_ust_lib_ring_buffer
*buf
;
1658 consumer_chan
= stream
->chan
;
1659 chan
= consumer_chan
->chan
->chan
;
1660 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1661 consumer_chan
->chan
->handle
);
1665 /* returns the size of the current sub-buffer, without padding (for mmap). */
1666 int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream
*stream
,
1669 struct ustctl_consumer_channel
*consumer_chan
;
1670 struct channel
*chan
;
1671 struct lttng_ust_lib_ring_buffer
*buf
;
1676 consumer_chan
= stream
->chan
;
1677 chan
= consumer_chan
->chan
->chan
;
1678 *len
= lib_ring_buffer_get_read_data_size(&chan
->backend
.config
, buf
,
1679 consumer_chan
->chan
->handle
);
1680 *len
= LTTNG_UST_PAGE_ALIGN(*len
);
1684 /* Get exclusive read access to the next sub-buffer that can be read. */
1685 int ustctl_get_next_subbuf(struct ustctl_consumer_stream
*stream
)
1687 struct lttng_ust_lib_ring_buffer
*buf
;
1688 struct ustctl_consumer_channel
*consumer_chan
;
1693 consumer_chan
= stream
->chan
;
1694 return lib_ring_buffer_get_next_subbuf(buf
,
1695 consumer_chan
->chan
->handle
);
1699 /* Release exclusive sub-buffer access, move consumer forward. */
1700 int ustctl_put_next_subbuf(struct ustctl_consumer_stream
*stream
)
1702 struct lttng_ust_lib_ring_buffer
*buf
;
1703 struct ustctl_consumer_channel
*consumer_chan
;
1708 consumer_chan
= stream
->chan
;
1709 lib_ring_buffer_put_next_subbuf(buf
, consumer_chan
->chan
->handle
);
1715 /* Get a snapshot of the current ring buffer producer and consumer positions */
1716 int ustctl_snapshot(struct ustctl_consumer_stream
*stream
)
1718 struct lttng_ust_lib_ring_buffer
*buf
;
1719 struct ustctl_consumer_channel
*consumer_chan
;
1724 consumer_chan
= stream
->chan
;
1725 return lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
1726 &buf
->prod_snapshot
, consumer_chan
->chan
->handle
);
1730 * Get a snapshot of the current ring buffer producer and consumer positions
1731 * even if the consumed and produced positions are contained within the same
1734 int ustctl_snapshot_sample_positions(struct ustctl_consumer_stream
*stream
)
1736 struct lttng_ust_lib_ring_buffer
*buf
;
1737 struct ustctl_consumer_channel
*consumer_chan
;
1742 consumer_chan
= stream
->chan
;
1743 return lib_ring_buffer_snapshot_sample_positions(buf
,
1744 &buf
->cons_snapshot
, &buf
->prod_snapshot
,
1745 consumer_chan
->chan
->handle
);
1748 /* Get the consumer position (iteration start) */
1749 int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream
*stream
,
1752 struct lttng_ust_lib_ring_buffer
*buf
;
1757 *pos
= buf
->cons_snapshot
;
1761 /* Get the producer position (iteration end) */
1762 int ustctl_snapshot_get_produced(struct ustctl_consumer_stream
*stream
,
1765 struct lttng_ust_lib_ring_buffer
*buf
;
1770 *pos
= buf
->prod_snapshot
;
1774 /* Get exclusive read access to the specified sub-buffer position */
1775 int ustctl_get_subbuf(struct ustctl_consumer_stream
*stream
,
1778 struct lttng_ust_lib_ring_buffer
*buf
;
1779 struct ustctl_consumer_channel
*consumer_chan
;
1784 consumer_chan
= stream
->chan
;
1785 return lib_ring_buffer_get_subbuf(buf
, *pos
,
1786 consumer_chan
->chan
->handle
);
1789 /* Release exclusive sub-buffer access */
1790 int ustctl_put_subbuf(struct ustctl_consumer_stream
*stream
)
1792 struct lttng_ust_lib_ring_buffer
*buf
;
1793 struct ustctl_consumer_channel
*consumer_chan
;
1798 consumer_chan
= stream
->chan
;
1799 lib_ring_buffer_put_subbuf(buf
, consumer_chan
->chan
->handle
);
1803 void ustctl_flush_buffer(struct ustctl_consumer_stream
*stream
,
1804 int producer_active
)
1806 struct lttng_ust_lib_ring_buffer
*buf
;
1807 struct ustctl_consumer_channel
*consumer_chan
;
1811 consumer_chan
= stream
->chan
;
1812 lib_ring_buffer_switch_slow(buf
,
1813 producer_active
? SWITCH_ACTIVE
: SWITCH_FLUSH
,
1814 consumer_chan
->chan
->handle
);
1817 void ustctl_clear_buffer(struct ustctl_consumer_stream
*stream
)
1819 struct lttng_ust_lib_ring_buffer
*buf
;
1820 struct ustctl_consumer_channel
*consumer_chan
;
1824 consumer_chan
= stream
->chan
;
1825 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
1826 consumer_chan
->chan
->handle
);
1827 lib_ring_buffer_clear_reader(buf
, consumer_chan
->chan
->handle
);
1831 struct lttng_ust_client_lib_ring_buffer_client_cb
*get_client_cb(
1832 struct lttng_ust_lib_ring_buffer
*buf
,
1833 struct lttng_ust_shm_handle
*handle
)
1835 struct channel
*chan
;
1836 const struct lttng_ust_lib_ring_buffer_config
*config
;
1837 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1839 chan
= shmp(handle
, buf
->backend
.chan
);
1842 config
= &chan
->backend
.config
;
1843 if (!config
->cb_ptr
)
1845 client_cb
= caa_container_of(config
->cb_ptr
,
1846 struct lttng_ust_client_lib_ring_buffer_client_cb
,
1851 int ustctl_get_timestamp_begin(struct ustctl_consumer_stream
*stream
,
1852 uint64_t *timestamp_begin
)
1854 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1855 struct lttng_ust_lib_ring_buffer
*buf
;
1856 struct lttng_ust_shm_handle
*handle
;
1858 if (!stream
|| !timestamp_begin
)
1861 handle
= stream
->chan
->chan
->handle
;
1862 client_cb
= get_client_cb(buf
, handle
);
1865 return client_cb
->timestamp_begin(buf
, handle
, timestamp_begin
);
1868 int ustctl_get_timestamp_end(struct ustctl_consumer_stream
*stream
,
1869 uint64_t *timestamp_end
)
1871 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1872 struct lttng_ust_lib_ring_buffer
*buf
;
1873 struct lttng_ust_shm_handle
*handle
;
1875 if (!stream
|| !timestamp_end
)
1878 handle
= stream
->chan
->chan
->handle
;
1879 client_cb
= get_client_cb(buf
, handle
);
1882 return client_cb
->timestamp_end(buf
, handle
, timestamp_end
);
1885 int ustctl_get_events_discarded(struct ustctl_consumer_stream
*stream
,
1886 uint64_t *events_discarded
)
1888 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1889 struct lttng_ust_lib_ring_buffer
*buf
;
1890 struct lttng_ust_shm_handle
*handle
;
1892 if (!stream
|| !events_discarded
)
1895 handle
= stream
->chan
->chan
->handle
;
1896 client_cb
= get_client_cb(buf
, handle
);
1899 return client_cb
->events_discarded(buf
, handle
, events_discarded
);
1902 int ustctl_get_content_size(struct ustctl_consumer_stream
*stream
,
1903 uint64_t *content_size
)
1905 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1906 struct lttng_ust_lib_ring_buffer
*buf
;
1907 struct lttng_ust_shm_handle
*handle
;
1909 if (!stream
|| !content_size
)
1912 handle
= stream
->chan
->chan
->handle
;
1913 client_cb
= get_client_cb(buf
, handle
);
1916 return client_cb
->content_size(buf
, handle
, content_size
);
1919 int ustctl_get_packet_size(struct ustctl_consumer_stream
*stream
,
1920 uint64_t *packet_size
)
1922 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1923 struct lttng_ust_lib_ring_buffer
*buf
;
1924 struct lttng_ust_shm_handle
*handle
;
1926 if (!stream
|| !packet_size
)
1929 handle
= stream
->chan
->chan
->handle
;
1930 client_cb
= get_client_cb(buf
, handle
);
1933 return client_cb
->packet_size(buf
, handle
, packet_size
);
1936 int ustctl_get_stream_id(struct ustctl_consumer_stream
*stream
,
1937 uint64_t *stream_id
)
1939 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1940 struct lttng_ust_lib_ring_buffer
*buf
;
1941 struct lttng_ust_shm_handle
*handle
;
1943 if (!stream
|| !stream_id
)
1946 handle
= stream
->chan
->chan
->handle
;
1947 client_cb
= get_client_cb(buf
, handle
);
1950 return client_cb
->stream_id(buf
, handle
, stream_id
);
1953 int ustctl_get_current_timestamp(struct ustctl_consumer_stream
*stream
,
1956 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1957 struct lttng_ust_lib_ring_buffer
*buf
;
1958 struct lttng_ust_shm_handle
*handle
;
1963 handle
= stream
->chan
->chan
->handle
;
1964 client_cb
= get_client_cb(buf
, handle
);
1965 if (!client_cb
|| !client_cb
->current_timestamp
)
1967 return client_cb
->current_timestamp(buf
, handle
, ts
);
1970 int ustctl_get_sequence_number(struct ustctl_consumer_stream
*stream
,
1973 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1974 struct lttng_ust_lib_ring_buffer
*buf
;
1975 struct lttng_ust_shm_handle
*handle
;
1977 if (!stream
|| !seq
)
1980 handle
= stream
->chan
->chan
->handle
;
1981 client_cb
= get_client_cb(buf
, handle
);
1982 if (!client_cb
|| !client_cb
->sequence_number
)
1984 return client_cb
->sequence_number(buf
, handle
, seq
);
1987 int ustctl_get_instance_id(struct ustctl_consumer_stream
*stream
,
1990 struct lttng_ust_client_lib_ring_buffer_client_cb
*client_cb
;
1991 struct lttng_ust_lib_ring_buffer
*buf
;
1992 struct lttng_ust_shm_handle
*handle
;
1997 handle
= stream
->chan
->chan
->handle
;
1998 client_cb
= get_client_cb(buf
, handle
);
2001 return client_cb
->instance_id(buf
, handle
, id
);
2004 #ifdef LTTNG_UST_HAVE_PERF_EVENT
2006 int ustctl_has_perf_counters(void)
2013 int ustctl_has_perf_counters(void)
2021 * Returns 0 on success, negative error value on error.
2023 int ustctl_recv_reg_msg(int sock
,
2024 enum ustctl_socket_type
*type
,
2031 uint32_t *bits_per_long
,
2032 uint32_t *uint8_t_alignment
,
2033 uint32_t *uint16_t_alignment
,
2034 uint32_t *uint32_t_alignment
,
2035 uint32_t *uint64_t_alignment
,
2036 uint32_t *long_alignment
,
2041 struct ustctl_reg_msg reg_msg
;
2043 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
2044 if (len
> 0 && len
!= sizeof(reg_msg
))
2051 if (reg_msg
.magic
== LTTNG_UST_COMM_MAGIC
) {
2052 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
2053 BIG_ENDIAN
: LITTLE_ENDIAN
;
2054 } else if (reg_msg
.magic
== bswap_32(LTTNG_UST_COMM_MAGIC
)) {
2055 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
2056 LITTLE_ENDIAN
: BIG_ENDIAN
;
2058 return -LTTNG_UST_ERR_INVAL_MAGIC
;
2060 switch (reg_msg
.socket_type
) {
2061 case 0: *type
= USTCTL_SOCKET_CMD
;
2063 case 1: *type
= USTCTL_SOCKET_NOTIFY
;
2066 return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE
;
2068 *major
= reg_msg
.major
;
2069 *minor
= reg_msg
.minor
;
2071 *ppid
= reg_msg
.ppid
;
2074 *bits_per_long
= reg_msg
.bits_per_long
;
2075 *uint8_t_alignment
= reg_msg
.uint8_t_alignment
;
2076 *uint16_t_alignment
= reg_msg
.uint16_t_alignment
;
2077 *uint32_t_alignment
= reg_msg
.uint32_t_alignment
;
2078 *uint64_t_alignment
= reg_msg
.uint64_t_alignment
;
2079 *long_alignment
= reg_msg
.long_alignment
;
2080 memcpy(name
, reg_msg
.name
, LTTNG_UST_ABI_PROCNAME_LEN
);
2081 if (reg_msg
.major
< LTTNG_UST_ABI_MAJOR_VERSION_OLDEST_COMPATIBLE
||
2082 reg_msg
.major
> LTTNG_UST_ABI_MAJOR_VERSION
) {
2083 return -LTTNG_UST_ERR_UNSUP_MAJOR
;
2089 int ustctl_recv_notify(int sock
, enum ustctl_notify_cmd
*notify_cmd
)
2091 struct ustcomm_notify_hdr header
;
2094 len
= ustcomm_recv_unix_sock(sock
, &header
, sizeof(header
));
2095 if (len
> 0 && len
!= sizeof(header
))
2101 switch (header
.notify_cmd
) {
2103 *notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
2106 *notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
2109 *notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
2118 * Returns 0 on success, negative error value on error.
2120 int ustctl_recv_register_event(int sock
,
2127 struct ustctl_field
**fields
,
2128 char **model_emf_uri
)
2131 struct ustcomm_notify_event_msg msg
;
2132 size_t signature_len
, fields_len
, model_emf_uri_len
;
2133 char *a_sign
= NULL
, *a_model_emf_uri
= NULL
;
2134 struct ustctl_field
*a_fields
= NULL
;
2136 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2137 if (len
> 0 && len
!= sizeof(msg
))
2144 *session_objd
= msg
.session_objd
;
2145 *channel_objd
= msg
.channel_objd
;
2146 strncpy(event_name
, msg
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
2147 event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
2148 *loglevel
= msg
.loglevel
;
2149 signature_len
= msg
.signature_len
;
2150 fields_len
= msg
.fields_len
;
2152 if (fields_len
% sizeof(*a_fields
) != 0) {
2156 model_emf_uri_len
= msg
.model_emf_uri_len
;
2158 /* recv signature. contains at least \0. */
2159 a_sign
= zmalloc(signature_len
);
2162 len
= ustcomm_recv_unix_sock(sock
, a_sign
, signature_len
);
2163 if (len
> 0 && len
!= signature_len
) {
2165 goto signature_error
;
2169 goto signature_error
;
2172 goto signature_error
;
2174 /* Enforce end of string */
2175 a_sign
[signature_len
- 1] = '\0';
2179 a_fields
= zmalloc(fields_len
);
2182 goto signature_error
;
2184 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2185 if (len
> 0 && len
!= fields_len
) {
2198 if (model_emf_uri_len
) {
2199 /* recv model_emf_uri_len */
2200 a_model_emf_uri
= zmalloc(model_emf_uri_len
);
2201 if (!a_model_emf_uri
) {
2205 len
= ustcomm_recv_unix_sock(sock
, a_model_emf_uri
,
2207 if (len
> 0 && len
!= model_emf_uri_len
) {
2218 /* Enforce end of string */
2219 a_model_emf_uri
[model_emf_uri_len
- 1] = '\0';
2222 *signature
= a_sign
;
2223 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2225 *model_emf_uri
= a_model_emf_uri
;
2230 free(a_model_emf_uri
);
2239 * Returns 0 on success, negative error value on error.
2241 int ustctl_reply_register_event(int sock
,
2247 struct ustcomm_notify_hdr header
;
2248 struct ustcomm_notify_event_reply r
;
2251 memset(&reply
, 0, sizeof(reply
));
2252 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_EVENT
;
2253 reply
.r
.ret_code
= ret_code
;
2254 reply
.r
.event_id
= id
;
2255 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2256 if (len
> 0 && len
!= sizeof(reply
))
2264 * Returns 0 on success, negative UST or system error value on error.
2266 int ustctl_recv_register_enum(int sock
,
2269 struct ustctl_enum_entry
**entries
,
2273 struct ustcomm_notify_enum_msg msg
;
2275 struct ustctl_enum_entry
*a_entries
= NULL
;
2277 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2278 if (len
> 0 && len
!= sizeof(msg
))
2285 *session_objd
= msg
.session_objd
;
2286 strncpy(enum_name
, msg
.enum_name
, LTTNG_UST_SYM_NAME_LEN
);
2287 enum_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
2288 entries_len
= msg
.entries_len
;
2290 if (entries_len
% sizeof(*a_entries
) != 0) {
2296 a_entries
= zmalloc(entries_len
);
2299 len
= ustcomm_recv_unix_sock(sock
, a_entries
, entries_len
);
2300 if (len
> 0 && len
!= entries_len
) {
2312 *nr_entries
= entries_len
/ sizeof(*a_entries
);
2313 *entries
= a_entries
;
2323 * Returns 0 on success, negative error value on error.
2325 int ustctl_reply_register_enum(int sock
,
2331 struct ustcomm_notify_hdr header
;
2332 struct ustcomm_notify_enum_reply r
;
2335 memset(&reply
, 0, sizeof(reply
));
2336 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_ENUM
;
2337 reply
.r
.ret_code
= ret_code
;
2338 reply
.r
.enum_id
= id
;
2339 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2340 if (len
> 0 && len
!= sizeof(reply
))
2348 * Returns 0 on success, negative UST or system error value on error.
2350 int ustctl_recv_register_channel(int sock
,
2351 int *session_objd
, /* session descriptor (output) */
2352 int *channel_objd
, /* channel descriptor (output) */
2354 struct ustctl_field
**fields
)
2357 struct ustcomm_notify_channel_msg msg
;
2359 struct ustctl_field
*a_fields
;
2361 len
= ustcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
2362 if (len
> 0 && len
!= sizeof(msg
))
2369 *session_objd
= msg
.session_objd
;
2370 *channel_objd
= msg
.channel_objd
;
2371 fields_len
= msg
.ctx_fields_len
;
2373 if (fields_len
% sizeof(*a_fields
) != 0) {
2379 a_fields
= zmalloc(fields_len
);
2384 len
= ustcomm_recv_unix_sock(sock
, a_fields
, fields_len
);
2385 if (len
> 0 && len
!= fields_len
) {
2400 *nr_fields
= fields_len
/ sizeof(*a_fields
);
2410 * Returns 0 on success, negative error value on error.
2412 int ustctl_reply_register_channel(int sock
,
2414 enum ustctl_channel_header header_type
,
2419 struct ustcomm_notify_hdr header
;
2420 struct ustcomm_notify_channel_reply r
;
2423 memset(&reply
, 0, sizeof(reply
));
2424 reply
.header
.notify_cmd
= USTCTL_NOTIFY_CMD_CHANNEL
;
2425 reply
.r
.ret_code
= ret_code
;
2426 reply
.r
.chan_id
= chan_id
;
2427 switch (header_type
) {
2428 case USTCTL_CHANNEL_HEADER_COMPACT
:
2429 reply
.r
.header_type
= 1;
2431 case USTCTL_CHANNEL_HEADER_LARGE
:
2432 reply
.r
.header_type
= 2;
2435 reply
.r
.header_type
= 0;
2438 len
= ustcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2439 if (len
> 0 && len
!= sizeof(reply
))
2446 /* Regenerate the statedump. */
2447 int ustctl_regenerate_statedump(int sock
, int handle
)
2449 struct ustcomm_ust_msg lum
;
2450 struct ustcomm_ust_reply lur
;
2453 memset(&lum
, 0, sizeof(lum
));
2454 lum
.handle
= handle
;
2455 lum
.cmd
= LTTNG_UST_SESSION_STATEDUMP
;
2456 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
2459 DBG("Regenerated statedump for handle %u", handle
);
2463 /* counter operations */
2465 int ustctl_get_nr_cpu_per_counter(void)
2467 return lttng_counter_num_possible_cpus();
2470 struct ustctl_daemon_counter
*
2471 ustctl_create_counter(size_t nr_dimensions
,
2472 const struct ustctl_counter_dimension
*dimensions
,
2473 int64_t global_sum_step
,
2474 int global_counter_fd
,
2475 int nr_counter_cpu_fds
,
2476 const int *counter_cpu_fds
,
2477 enum ustctl_counter_bitness bitness
,
2478 enum ustctl_counter_arithmetic arithmetic
,
2479 uint32_t alloc_flags
)
2481 const char *transport_name
;
2482 struct ustctl_daemon_counter
*counter
;
2483 struct lttng_counter_transport
*transport
;
2484 struct lttng_counter_dimension ust_dim
[LTTNG_COUNTER_DIMENSION_MAX
];
2487 if (nr_dimensions
> LTTNG_COUNTER_DIMENSION_MAX
)
2489 /* Currently, only per-cpu allocation is supported. */
2490 switch (alloc_flags
) {
2491 case USTCTL_COUNTER_ALLOC_PER_CPU
:
2494 case USTCTL_COUNTER_ALLOC_PER_CPU
| USTCTL_COUNTER_ALLOC_GLOBAL
:
2495 case USTCTL_COUNTER_ALLOC_GLOBAL
:
2500 case USTCTL_COUNTER_BITNESS_32
:
2501 switch (arithmetic
) {
2502 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2503 transport_name
= "counter-per-cpu-32-modular";
2505 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2506 transport_name
= "counter-per-cpu-32-saturation";
2512 case USTCTL_COUNTER_BITNESS_64
:
2513 switch (arithmetic
) {
2514 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2515 transport_name
= "counter-per-cpu-64-modular";
2517 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2518 transport_name
= "counter-per-cpu-64-saturation";
2528 transport
= lttng_counter_transport_find(transport_name
);
2530 DBG("LTTng transport %s not found\n",
2535 counter
= zmalloc(sizeof(*counter
));
2538 counter
->attr
= zmalloc(sizeof(*counter
->attr
));
2541 counter
->attr
->bitness
= bitness
;
2542 counter
->attr
->arithmetic
= arithmetic
;
2543 counter
->attr
->nr_dimensions
= nr_dimensions
;
2544 counter
->attr
->global_sum_step
= global_sum_step
;
2545 for (i
= 0; i
< nr_dimensions
; i
++)
2546 counter
->attr
->dimensions
[i
] = dimensions
[i
];
2548 for (i
= 0; i
< nr_dimensions
; i
++) {
2549 ust_dim
[i
].size
= dimensions
[i
].size
;
2550 ust_dim
[i
].underflow_index
= dimensions
[i
].underflow_index
;
2551 ust_dim
[i
].overflow_index
= dimensions
[i
].overflow_index
;
2552 ust_dim
[i
].has_underflow
= dimensions
[i
].has_underflow
;
2553 ust_dim
[i
].has_overflow
= dimensions
[i
].has_overflow
;
2555 counter
->counter
= transport
->ops
.counter_create(nr_dimensions
,
2556 ust_dim
, global_sum_step
, global_counter_fd
,
2557 nr_counter_cpu_fds
, counter_cpu_fds
, true);
2558 if (!counter
->counter
)
2560 counter
->ops
= &transport
->ops
;
2564 free(counter
->attr
);
2570 int ustctl_create_counter_data(struct ustctl_daemon_counter
*counter
,
2571 struct lttng_ust_object_data
**_counter_data
)
2573 struct lttng_ust_object_data
*counter_data
;
2574 struct lttng_ust_counter_conf counter_conf
= {0};
2578 switch (counter
->attr
->arithmetic
) {
2579 case USTCTL_COUNTER_ARITHMETIC_MODULAR
:
2580 counter_conf
.arithmetic
= LTTNG_UST_COUNTER_ARITHMETIC_MODULAR
;
2582 case USTCTL_COUNTER_ARITHMETIC_SATURATION
:
2583 counter_conf
.arithmetic
= LTTNG_UST_COUNTER_ARITHMETIC_SATURATION
;
2588 switch (counter
->attr
->bitness
) {
2589 case USTCTL_COUNTER_BITNESS_32
:
2590 counter_conf
.bitness
= LTTNG_UST_COUNTER_BITNESS_32
;
2592 case USTCTL_COUNTER_BITNESS_64
:
2593 counter_conf
.bitness
= LTTNG_UST_COUNTER_BITNESS_64
;
2598 counter_conf
.number_dimensions
= counter
->attr
->nr_dimensions
;
2599 counter_conf
.global_sum_step
= counter
->attr
->global_sum_step
;
2600 for (i
= 0; i
< counter
->attr
->nr_dimensions
; i
++) {
2601 counter_conf
.dimensions
[i
].size
= counter
->attr
->dimensions
[i
].size
;
2602 counter_conf
.dimensions
[i
].underflow_index
= counter
->attr
->dimensions
[i
].underflow_index
;
2603 counter_conf
.dimensions
[i
].overflow_index
= counter
->attr
->dimensions
[i
].overflow_index
;
2604 counter_conf
.dimensions
[i
].has_underflow
= counter
->attr
->dimensions
[i
].has_underflow
;
2605 counter_conf
.dimensions
[i
].has_overflow
= counter
->attr
->dimensions
[i
].has_overflow
;
2608 counter_data
= zmalloc(sizeof(*counter_data
));
2609 if (!counter_data
) {
2613 counter_data
->type
= LTTNG_UST_OBJECT_TYPE_COUNTER
;
2614 counter_data
->handle
= -1;
2616 counter_data
->size
= sizeof(counter_conf
);
2617 counter_data
->u
.counter
.data
= zmalloc(sizeof(counter_conf
));
2618 if (!counter_data
->u
.counter
.data
) {
2620 goto error_alloc_data
;
2623 memcpy(counter_data
->u
.counter
.data
, &counter_conf
, sizeof(counter_conf
));
2624 *_counter_data
= counter_data
;
2634 int ustctl_create_counter_global_data(struct ustctl_daemon_counter
*counter
,
2635 struct lttng_ust_object_data
**_counter_global_data
)
2637 struct lttng_ust_object_data
*counter_global_data
;
2641 if (lttng_counter_get_global_shm(counter
->counter
, &fd
, &len
))
2643 counter_global_data
= zmalloc(sizeof(*counter_global_data
));
2644 if (!counter_global_data
) {
2648 counter_global_data
->type
= LTTNG_UST_OBJECT_TYPE_COUNTER_GLOBAL
;
2649 counter_global_data
->handle
= -1;
2650 counter_global_data
->size
= len
;
2651 counter_global_data
->u
.counter_global
.shm_fd
= fd
;
2652 *_counter_global_data
= counter_global_data
;
2659 int ustctl_create_counter_cpu_data(struct ustctl_daemon_counter
*counter
, int cpu
,
2660 struct lttng_ust_object_data
**_counter_cpu_data
)
2662 struct lttng_ust_object_data
*counter_cpu_data
;
2666 if (lttng_counter_get_cpu_shm(counter
->counter
, cpu
, &fd
, &len
))
2668 counter_cpu_data
= zmalloc(sizeof(*counter_cpu_data
));
2669 if (!counter_cpu_data
) {
2673 counter_cpu_data
->type
= LTTNG_UST_OBJECT_TYPE_COUNTER_CPU
;
2674 counter_cpu_data
->handle
= -1;
2675 counter_cpu_data
->size
= len
;
2676 counter_cpu_data
->u
.counter_cpu
.shm_fd
= fd
;
2677 counter_cpu_data
->u
.counter_cpu
.cpu_nr
= cpu
;
2678 *_counter_cpu_data
= counter_cpu_data
;
2685 void ustctl_destroy_counter(struct ustctl_daemon_counter
*counter
)
2687 counter
->ops
->counter_destroy(counter
->counter
);
2688 free(counter
->attr
);
2692 int ustctl_send_counter_data_to_ust(int sock
, int parent_handle
,
2693 struct lttng_ust_object_data
*counter_data
)
2695 struct ustcomm_ust_msg lum
;
2696 struct ustcomm_ust_reply lur
;
2704 size
= counter_data
->size
;
2705 memset(&lum
, 0, sizeof(lum
));
2706 lum
.handle
= parent_handle
;
2707 lum
.cmd
= LTTNG_UST_COUNTER
;
2708 lum
.u
.counter
.len
= size
;
2709 ret
= ustcomm_send_app_msg(sock
, &lum
);
2713 /* Send counter data */
2714 len
= ustcomm_send_unix_sock(sock
, counter_data
->u
.counter
.data
, size
);
2722 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2724 counter_data
->handle
= lur
.ret_val
;
2729 int ustctl_send_counter_global_data_to_ust(int sock
,
2730 struct lttng_ust_object_data
*counter_data
,
2731 struct lttng_ust_object_data
*counter_global_data
)
2733 struct ustcomm_ust_msg lum
;
2734 struct ustcomm_ust_reply lur
;
2739 if (!counter_data
|| !counter_global_data
)
2742 size
= counter_global_data
->size
;
2743 memset(&lum
, 0, sizeof(lum
));
2744 lum
.handle
= counter_data
->handle
; /* parent handle */
2745 lum
.cmd
= LTTNG_UST_COUNTER_GLOBAL
;
2746 lum
.u
.counter_global
.len
= size
;
2747 ret
= ustcomm_send_app_msg(sock
, &lum
);
2751 shm_fd
[0] = counter_global_data
->u
.counter_global
.shm_fd
;
2752 len
= ustcomm_send_fds_unix_sock(sock
, shm_fd
, 1);
2760 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2762 counter_global_data
->handle
= lur
.ret_val
;
2767 int ustctl_send_counter_cpu_data_to_ust(int sock
,
2768 struct lttng_ust_object_data
*counter_data
,
2769 struct lttng_ust_object_data
*counter_cpu_data
)
2771 struct ustcomm_ust_msg lum
;
2772 struct ustcomm_ust_reply lur
;
2777 if (!counter_data
|| !counter_cpu_data
)
2780 size
= counter_cpu_data
->size
;
2781 memset(&lum
, 0, sizeof(lum
));
2782 lum
.handle
= counter_data
->handle
; /* parent handle */
2783 lum
.cmd
= LTTNG_UST_COUNTER_CPU
;
2784 lum
.u
.counter_cpu
.len
= size
;
2785 lum
.u
.counter_cpu
.cpu_nr
= counter_cpu_data
->u
.counter_cpu
.cpu_nr
;
2786 ret
= ustcomm_send_app_msg(sock
, &lum
);
2790 shm_fd
[0] = counter_cpu_data
->u
.counter_global
.shm_fd
;
2791 len
= ustcomm_send_fds_unix_sock(sock
, shm_fd
, 1);
2799 ret
= ustcomm_recv_app_reply(sock
, &lur
, lum
.handle
, lum
.cmd
);
2801 counter_cpu_data
->handle
= lur
.ret_val
;
2806 int ustctl_counter_read(struct ustctl_daemon_counter
*counter
,
2807 const size_t *dimension_indexes
,
2808 int cpu
, int64_t *value
,
2809 bool *overflow
, bool *underflow
)
2811 return counter
->ops
->counter_read(counter
->counter
, dimension_indexes
, cpu
,
2812 value
, overflow
, underflow
);
2815 int ustctl_counter_aggregate(struct ustctl_daemon_counter
*counter
,
2816 const size_t *dimension_indexes
,
2818 bool *overflow
, bool *underflow
)
2820 return counter
->ops
->counter_aggregate(counter
->counter
, dimension_indexes
,
2821 value
, overflow
, underflow
);
2824 int ustctl_counter_clear(struct ustctl_daemon_counter
*counter
,
2825 const size_t *dimension_indexes
)
2827 return counter
->ops
->counter_clear(counter
->counter
, dimension_indexes
);
2830 static __attribute__((constructor
))
2831 void ustctl_init(void)
2834 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
2835 lttng_ust_clock_init();
2836 lttng_ring_buffer_metadata_client_init();
2837 lttng_ring_buffer_client_overwrite_init();
2838 lttng_ring_buffer_client_overwrite_rt_init();
2839 lttng_ring_buffer_client_discard_init();
2840 lttng_ring_buffer_client_discard_rt_init();
2841 lttng_counter_client_percpu_32_modular_init();
2842 lttng_counter_client_percpu_64_modular_init();
2843 lib_ringbuffer_signal_init();
2846 static __attribute__((destructor
))
2847 void ustctl_exit(void)
2849 lttng_ring_buffer_client_discard_rt_exit();
2850 lttng_ring_buffer_client_discard_exit();
2851 lttng_ring_buffer_client_overwrite_rt_exit();
2852 lttng_ring_buffer_client_overwrite_exit();
2853 lttng_ring_buffer_metadata_client_exit();
2854 lttng_counter_client_percpu_32_modular_exit();
2855 lttng_counter_client_percpu_64_modular_exit();