2 * Copyright (C) 2017 Julien Desfossez <jdesfossez@efficios.com>
3 * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
10 #include <lttng/trigger/trigger.h>
11 #include <common/error.h>
12 #include <common/config/session-config.h>
13 #include <common/defaults.h>
14 #include <common/utils.h>
15 #include <common/futex.h>
16 #include <common/align.h>
17 #include <common/time.h>
18 #include <common/hashtable/utils.h>
19 #include <common/kernel-ctl/kernel-ctl.h>
20 #include <common/credentials.h>
26 #include <lttng/notification/channel-internal.h>
27 #include <lttng/rotate-internal.h>
28 #include <lttng/condition/condition-internal.h>
29 #include <lttng/action/action-internal.h>
33 #include "rotation-thread.h"
34 #include "lttng-sessiond.h"
35 #include "health-sessiond.h"
38 #include "notification-thread-commands.h"
41 #include <urcu/list.h>
42 #include <urcu/rculfhash.h>
44 int subscribe_session_consumed_size_rotation(struct ltt_session
*session
, uint64_t size
,
45 struct notification_thread_handle
*notification_thread_handle
)
48 enum lttng_condition_status condition_status
;
49 enum lttng_notification_channel_status nc_status
;
50 const uint64_t eventfd_increment_value
= 1;
51 const struct lttng_credentials session_creds
= {
52 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session
->uid
),
53 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session
->gid
),
55 struct lttng_condition
*rotate_condition
;
56 struct lttng_action
*notify_action
= NULL
;
58 rotate_condition
= lttng_condition_session_consumed_size_create();
59 if (!rotate_condition
) {
60 ERR("Failed to create session consumed size condition object");
65 condition_status
= lttng_condition_session_consumed_size_set_threshold(
66 rotate_condition
, size
);
67 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
68 ERR("Could not set session consumed size condition threshold (size = %" PRIu64
")",
75 lttng_condition_session_consumed_size_set_session_name(
76 rotate_condition
, session
->name
);
77 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
78 ERR("Could not set session consumed size condition session name (name = %s)",
84 notify_action
= lttng_action_notify_create();
86 ERR("Could not create notify action");
91 assert(!session
->rotate_trigger
);
92 session
->rotate_trigger
= lttng_trigger_create(rotate_condition
,
94 if (!session
->rotate_trigger
) {
95 ERR("Could not create size-based rotation trigger");
100 /* Ensure this trigger is not visible to external users. */
101 lttng_trigger_set_hidden(session
->rotate_trigger
);
102 lttng_trigger_set_credentials(
103 session
->rotate_trigger
, &session_creds
);
105 nc_status
= lttng_notification_channel_subscribe(
106 rotate_notification_channel
, rotate_condition
);
107 if (nc_status
!= LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
) {
108 ERR("Could not subscribe to session consumed size notification");
113 ret
= lttng_write(rotate_notification_channel_subscription_change_eventfd
,
114 &eventfd_increment_value
,
115 sizeof(eventfd_increment_value
));
116 if (ret
!= sizeof(eventfd_increment_value
)) {
117 PERROR("Failed to wake up rotation thread as writing to the rotation thread notification channel subscription change eventfd failed");
122 ret
= notification_thread_command_register_trigger(
123 notification_thread_handle
, session
->rotate_trigger
,
125 if (ret
< 0 && ret
!= -LTTNG_ERR_TRIGGER_EXISTS
) {
126 ERR("Register trigger, %s", lttng_strerror(ret
));
134 lttng_condition_put(rotate_condition
);
135 lttng_action_put(notify_action
);
137 lttng_trigger_put(session
->rotate_trigger
);
142 int unsubscribe_session_consumed_size_rotation(struct ltt_session
*session
,
143 struct notification_thread_handle
*notification_thread_handle
)
146 enum lttng_notification_channel_status status
;
147 const uint64_t eventfd_increment_value
= 1;
149 assert(session
->rotate_trigger
);
150 status
= lttng_notification_channel_unsubscribe(
151 rotate_notification_channel
,
152 lttng_trigger_get_const_condition(session
->rotate_trigger
));
153 if (status
!= LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
) {
154 ERR("Session unsubscribe error: %d", (int) status
);
159 ret
= lttng_write(rotate_notification_channel_subscription_change_eventfd
,
160 &eventfd_increment_value
,
161 sizeof(eventfd_increment_value
));
162 if (ret
!= sizeof(eventfd_increment_value
)) {
163 PERROR("Failed to wake up rotation thread as writing to the rotation thread notification channel subscription change eventfd failed");
168 ret
= notification_thread_command_unregister_trigger(notification_thread_handle
,
169 session
->rotate_trigger
);
170 if (ret
!= LTTNG_OK
) {
171 ERR("Session unregister trigger error: %d", ret
);
175 lttng_trigger_put(session
->rotate_trigger
);
176 session
->rotate_trigger
= NULL
;