2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
9 #include <common/error.h>
10 #include <lttng/action/action-internal.h>
11 #include <lttng/action/firing-policy-internal.h>
12 #include <lttng/action/group-internal.h>
13 #include <lttng/action/notify-internal.h>
14 #include <lttng/action/rotate-session-internal.h>
15 #include <lttng/action/snapshot-session-internal.h>
16 #include <lttng/action/start-session-internal.h>
17 #include <lttng/action/stop-session-internal.h>
20 const char *lttng_action_type_string(enum lttng_action_type action_type
)
22 switch (action_type
) {
23 case LTTNG_ACTION_TYPE_UNKNOWN
:
25 case LTTNG_ACTION_TYPE_GROUP
:
27 case LTTNG_ACTION_TYPE_NOTIFY
:
29 case LTTNG_ACTION_TYPE_ROTATE_SESSION
:
30 return "ROTATE_SESSION";
31 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION
:
32 return "SNAPSHOT_SESSION";
33 case LTTNG_ACTION_TYPE_START_SESSION
:
34 return "START_SESSION";
35 case LTTNG_ACTION_TYPE_STOP_SESSION
:
36 return "STOP_SESSION";
42 enum lttng_action_type
lttng_action_get_type(const struct lttng_action
*action
)
44 return action
? action
->type
: LTTNG_ACTION_TYPE_UNKNOWN
;
48 void lttng_action_init(struct lttng_action
*action
,
49 enum lttng_action_type type
,
50 action_validate_cb validate
,
51 action_serialize_cb serialize
,
52 action_equal_cb equal
,
53 action_destroy_cb destroy
,
54 action_get_firing_policy_cb get_firing_policy
)
56 urcu_ref_init(&action
->ref
);
58 action
->validate
= validate
;
59 action
->serialize
= serialize
;
60 action
->equal
= equal
;
61 action
->destroy
= destroy
;
62 action
->get_firing_policy
= get_firing_policy
;
64 action
->execution_request_counter
= 0;
65 action
->execution_counter
= 0;
66 action
->execution_failure_counter
= 0;
70 void action_destroy_ref(struct urcu_ref
*ref
)
72 struct lttng_action
*action
=
73 container_of(ref
, struct lttng_action
, ref
);
75 action
->destroy(action
);
79 void lttng_action_get(struct lttng_action
*action
)
81 urcu_ref_get(&action
->ref
);
85 void lttng_action_put(struct lttng_action
*action
)
91 assert(action
->destroy
);
92 urcu_ref_put(&action
->ref
, action_destroy_ref
);
95 void lttng_action_destroy(struct lttng_action
*action
)
97 lttng_action_put(action
);
101 bool lttng_action_validate(struct lttng_action
*action
)
110 if (!action
->validate
) {
111 /* Sub-class guarantees that it can never be invalid. */
116 valid
= action
->validate(action
);
122 int lttng_action_serialize(struct lttng_action
*action
,
123 struct lttng_payload
*payload
)
126 struct lttng_action_comm action_comm
= {
127 .action_type
= (int8_t) action
->type
,
130 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &action_comm
,
131 sizeof(action_comm
));
136 ret
= action
->serialize(action
, payload
);
145 ssize_t
lttng_action_create_from_payload(struct lttng_payload_view
*view
,
146 struct lttng_action
**action
)
148 ssize_t consumed_len
, specific_action_consumed_len
;
149 action_create_from_payload_cb create_from_payload_cb
;
150 const struct lttng_action_comm
*action_comm
;
151 const struct lttng_payload_view action_comm_view
=
152 lttng_payload_view_from_view(
153 view
, 0, sizeof(*action_comm
));
155 if (!view
|| !action
) {
160 if (!lttng_payload_view_is_valid(&action_comm_view
)) {
161 /* Payload not large enough to contain the header. */
166 action_comm
= (const struct lttng_action_comm
*) action_comm_view
.buffer
.data
;
168 DBG("Create action from payload: action-type=%s",
169 lttng_action_type_string(action_comm
->action_type
));
171 switch (action_comm
->action_type
) {
172 case LTTNG_ACTION_TYPE_NOTIFY
:
173 create_from_payload_cb
= lttng_action_notify_create_from_payload
;
175 case LTTNG_ACTION_TYPE_ROTATE_SESSION
:
176 create_from_payload_cb
=
177 lttng_action_rotate_session_create_from_payload
;
179 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION
:
180 create_from_payload_cb
=
181 lttng_action_snapshot_session_create_from_payload
;
183 case LTTNG_ACTION_TYPE_START_SESSION
:
184 create_from_payload_cb
=
185 lttng_action_start_session_create_from_payload
;
187 case LTTNG_ACTION_TYPE_STOP_SESSION
:
188 create_from_payload_cb
=
189 lttng_action_stop_session_create_from_payload
;
191 case LTTNG_ACTION_TYPE_GROUP
:
192 create_from_payload_cb
= lttng_action_group_create_from_payload
;
195 ERR("Failed to create action from payload, unhandled action type: action-type=%u (%s)",
196 action_comm
->action_type
,
197 lttng_action_type_string(
198 action_comm
->action_type
));
204 /* Create buffer view for the action-type-specific data. */
205 struct lttng_payload_view specific_action_view
=
206 lttng_payload_view_from_view(view
,
207 sizeof(struct lttng_action_comm
),
210 specific_action_consumed_len
= create_from_payload_cb(
211 &specific_action_view
, action
);
213 if (specific_action_consumed_len
< 0) {
214 ERR("Failed to create specific action from buffer.");
221 consumed_len
= sizeof(struct lttng_action_comm
) +
222 specific_action_consumed_len
;
229 bool lttng_action_is_equal(const struct lttng_action
*a
,
230 const struct lttng_action
*b
)
232 bool is_equal
= false;
238 if (a
->type
!= b
->type
) {
248 is_equal
= a
->equal(a
, b
);
254 void lttng_action_increase_execution_request_count(struct lttng_action
*action
)
256 action
->execution_request_counter
++;
260 void lttng_action_increase_execution_count(struct lttng_action
*action
)
262 action
->execution_counter
++;
266 void lttng_action_increase_execution_failure_count(struct lttng_action
*action
)
268 action
->execution_failure_counter
++;
272 bool lttng_action_should_execute(const struct lttng_action
*action
)
274 const struct lttng_firing_policy
*policy
= NULL
;
275 bool execute
= false;
277 if (action
->get_firing_policy
== NULL
) {
282 policy
= action
->get_firing_policy(action
);
283 if (policy
== NULL
) {
288 execute
= lttng_firing_policy_should_execute(
289 policy
, action
->execution_request_counter
);