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 <common/mi-lttng.h>
11 #include <lttng/action/action-internal.h>
12 #include <lttng/action/list-internal.h>
13 #include <lttng/action/notify-internal.h>
14 #include <lttng/action/rate-policy-internal.h>
15 #include <lttng/action/rotate-session-internal.h>
16 #include <lttng/action/snapshot-session-internal.h>
17 #include <lttng/action/start-session-internal.h>
18 #include <lttng/action/stop-session-internal.h>
19 #include <lttng/error-query-internal.h>
22 const char *lttng_action_type_string(enum lttng_action_type action_type
)
24 switch (action_type
) {
25 case LTTNG_ACTION_TYPE_UNKNOWN
:
27 case LTTNG_ACTION_TYPE_LIST
:
29 case LTTNG_ACTION_TYPE_NOTIFY
:
31 case LTTNG_ACTION_TYPE_ROTATE_SESSION
:
32 return "ROTATE_SESSION";
33 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION
:
34 return "SNAPSHOT_SESSION";
35 case LTTNG_ACTION_TYPE_START_SESSION
:
36 return "START_SESSION";
37 case LTTNG_ACTION_TYPE_STOP_SESSION
:
38 return "STOP_SESSION";
44 enum lttng_action_type
lttng_action_get_type(const struct lttng_action
*action
)
46 return action
? action
->type
: LTTNG_ACTION_TYPE_UNKNOWN
;
50 void lttng_action_init(struct lttng_action
*action
,
51 enum lttng_action_type type
,
52 action_validate_cb validate
,
53 action_serialize_cb serialize
,
54 action_equal_cb equal
,
55 action_destroy_cb destroy
,
56 action_get_rate_policy_cb get_rate_policy
,
57 action_add_error_query_results_cb add_error_query_results
,
58 action_mi_serialize_cb mi
)
60 urcu_ref_init(&action
->ref
);
62 action
->validate
= validate
;
63 action
->serialize
= serialize
;
64 action
->equal
= equal
;
65 action
->destroy
= destroy
;
66 action
->get_rate_policy
= get_rate_policy
;
67 action
->add_error_query_results
= add_error_query_results
;
68 action
->mi_serialize
= mi
;
70 action
->execution_request_counter
= 0;
71 action
->execution_counter
= 0;
72 action
->execution_failure_counter
= 0;
76 void action_destroy_ref(struct urcu_ref
*ref
)
78 struct lttng_action
*action
=
79 container_of(ref
, struct lttng_action
, ref
);
81 action
->destroy(action
);
85 void lttng_action_get(struct lttng_action
*action
)
87 urcu_ref_get(&action
->ref
);
91 void lttng_action_put(struct lttng_action
*action
)
97 assert(action
->destroy
);
98 urcu_ref_put(&action
->ref
, action_destroy_ref
);
101 void lttng_action_destroy(struct lttng_action
*action
)
103 lttng_action_put(action
);
107 bool lttng_action_validate(struct lttng_action
*action
)
116 if (!action
->validate
) {
117 /* Sub-class guarantees that it can never be invalid. */
122 valid
= action
->validate(action
);
128 int lttng_action_serialize(struct lttng_action
*action
,
129 struct lttng_payload
*payload
)
132 struct lttng_action_comm action_comm
= {
133 .action_type
= (int8_t) action
->type
,
136 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &action_comm
,
137 sizeof(action_comm
));
142 ret
= action
->serialize(action
, payload
);
151 ssize_t
lttng_action_create_from_payload(struct lttng_payload_view
*view
,
152 struct lttng_action
**action
)
154 ssize_t consumed_len
, specific_action_consumed_len
;
155 action_create_from_payload_cb create_from_payload_cb
;
156 const struct lttng_action_comm
*action_comm
;
157 const struct lttng_payload_view action_comm_view
=
158 lttng_payload_view_from_view(
159 view
, 0, sizeof(*action_comm
));
161 if (!view
|| !action
) {
166 if (!lttng_payload_view_is_valid(&action_comm_view
)) {
167 /* Payload not large enough to contain the header. */
172 action_comm
= (const struct lttng_action_comm
*) action_comm_view
.buffer
.data
;
174 DBG("Create action from payload: action-type=%s",
175 lttng_action_type_string(action_comm
->action_type
));
177 switch (action_comm
->action_type
) {
178 case LTTNG_ACTION_TYPE_NOTIFY
:
179 create_from_payload_cb
= lttng_action_notify_create_from_payload
;
181 case LTTNG_ACTION_TYPE_ROTATE_SESSION
:
182 create_from_payload_cb
=
183 lttng_action_rotate_session_create_from_payload
;
185 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION
:
186 create_from_payload_cb
=
187 lttng_action_snapshot_session_create_from_payload
;
189 case LTTNG_ACTION_TYPE_START_SESSION
:
190 create_from_payload_cb
=
191 lttng_action_start_session_create_from_payload
;
193 case LTTNG_ACTION_TYPE_STOP_SESSION
:
194 create_from_payload_cb
=
195 lttng_action_stop_session_create_from_payload
;
197 case LTTNG_ACTION_TYPE_LIST
:
198 create_from_payload_cb
= lttng_action_list_create_from_payload
;
201 ERR("Failed to create action from payload, unhandled action type: action-type=%u (%s)",
202 action_comm
->action_type
,
203 lttng_action_type_string(
204 action_comm
->action_type
));
210 /* Create buffer view for the action-type-specific data. */
211 struct lttng_payload_view specific_action_view
=
212 lttng_payload_view_from_view(view
,
213 sizeof(struct lttng_action_comm
),
216 specific_action_consumed_len
= create_from_payload_cb(
217 &specific_action_view
, action
);
219 if (specific_action_consumed_len
< 0) {
220 ERR("Failed to create specific action from buffer.");
227 consumed_len
= sizeof(struct lttng_action_comm
) +
228 specific_action_consumed_len
;
235 bool lttng_action_is_equal(const struct lttng_action
*a
,
236 const struct lttng_action
*b
)
238 bool is_equal
= false;
244 if (a
->type
!= b
->type
) {
254 is_equal
= a
->equal(a
, b
);
260 void lttng_action_increase_execution_request_count(struct lttng_action
*action
)
262 action
->execution_request_counter
++;
266 void lttng_action_increase_execution_count(struct lttng_action
*action
)
268 action
->execution_counter
++;
272 void lttng_action_increase_execution_failure_count(struct lttng_action
*action
)
274 uatomic_inc(&action
->execution_failure_counter
);
278 bool lttng_action_should_execute(const struct lttng_action
*action
)
280 const struct lttng_rate_policy
*policy
= NULL
;
281 bool execute
= false;
283 if (action
->get_rate_policy
== NULL
) {
288 policy
= action
->get_rate_policy(action
);
289 if (policy
== NULL
) {
294 execute
= lttng_rate_policy_should_execute(
295 policy
, action
->execution_request_counter
);
301 enum lttng_action_status
lttng_action_add_error_query_results(
302 const struct lttng_action
*action
,
303 struct lttng_error_query_results
*results
)
305 return action
->add_error_query_results(action
, results
);
309 enum lttng_action_status
lttng_action_generic_add_error_query_results(
310 const struct lttng_action
*action
,
311 struct lttng_error_query_results
*results
)
313 enum lttng_action_status action_status
;
314 struct lttng_error_query_result
*error_counter
= NULL
;
315 const uint64_t execution_failure_counter
=
316 uatomic_read(&action
->execution_failure_counter
);
318 error_counter
= lttng_error_query_result_counter_create(
319 "total execution failures",
320 "Aggregated count of errors encountered when executing the action",
321 execution_failure_counter
);
322 if (!error_counter
) {
323 action_status
= LTTNG_ACTION_STATUS_ERROR
;
327 if (lttng_error_query_results_add_result(
328 results
, error_counter
)) {
329 action_status
= LTTNG_ACTION_STATUS_ERROR
;
333 /* Ownership transferred to the results. */
334 error_counter
= NULL
;
335 action_status
= LTTNG_ACTION_STATUS_OK
;
337 lttng_error_query_result_destroy(error_counter
);
338 return action_status
;
342 enum lttng_error_code
lttng_action_mi_serialize(const struct lttng_trigger
*trigger
,
343 const struct lttng_action
*action
,
344 struct mi_writer
*writer
,
345 const struct mi_lttng_error_query_callbacks
346 *error_query_callbacks
,
347 struct lttng_dynamic_array
*action_path_indexes
)
350 enum lttng_error_code ret_code
;
351 struct lttng_action_path
*action_path
= NULL
;
352 struct lttng_error_query_results
*error_query_results
= NULL
;
358 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_action
);
363 if (action
->type
== LTTNG_ACTION_TYPE_LIST
) {
365 * Recursion is safe since action lists can't be nested for
368 ret_code
= lttng_action_list_mi_serialize(trigger
, action
, writer
,
369 error_query_callbacks
, action_path_indexes
);
370 if (ret_code
!= LTTNG_OK
) {
374 /* Nothing else to do. */
375 goto close_action_element
;
378 assert(action
->mi_serialize
);
379 ret_code
= action
->mi_serialize(action
, writer
);
380 if (ret_code
!= LTTNG_OK
) {
384 /* Error query for the action. */
385 if (error_query_callbacks
&& error_query_callbacks
->action_cb
) {
386 const uint64_t *action_path_indexes_raw_pointer
= NULL
;
387 const size_t action_path_indexes_size
=
388 lttng_dynamic_array_get_count(
389 action_path_indexes
);
391 if (action_path_indexes_size
!= 0) {
392 action_path_indexes_raw_pointer
=
393 (const uint64_t *) action_path_indexes
397 action_path
= lttng_action_path_create(
398 action_path_indexes_raw_pointer
,
399 action_path_indexes_size
);
402 ret_code
= error_query_callbacks
->action_cb(
403 trigger
, action_path
, &error_query_results
);
404 if (ret_code
!= LTTNG_OK
) {
408 /* Serialize the error query results. */
409 ret_code
= lttng_error_query_results_mi_serialize(
410 error_query_results
, writer
);
411 if (ret_code
!= LTTNG_OK
) {
416 close_action_element
:
418 ret
= mi_lttng_writer_close_element(writer
);
427 ret_code
= LTTNG_ERR_MI_IO_FAIL
;
429 lttng_action_path_destroy(action_path
);
430 lttng_error_query_results_destroy(error_query_results
);