2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include <common/error.h>
9 #include <common/mi-lttng.h>
10 #include <lttng/action/action-internal.h>
11 #include <lttng/action/list-internal.h>
12 #include <lttng/action/notify-internal.h>
13 #include <lttng/action/rate-policy-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>
18 #include <lttng/error-query-internal.h>
21 const char *lttng_action_type_string(enum lttng_action_type action_type
)
23 switch (action_type
) {
24 case LTTNG_ACTION_TYPE_UNKNOWN
:
26 case LTTNG_ACTION_TYPE_LIST
:
28 case LTTNG_ACTION_TYPE_NOTIFY
:
30 case LTTNG_ACTION_TYPE_ROTATE_SESSION
:
31 return "ROTATE_SESSION";
32 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION
:
33 return "SNAPSHOT_SESSION";
34 case LTTNG_ACTION_TYPE_START_SESSION
:
35 return "START_SESSION";
36 case LTTNG_ACTION_TYPE_STOP_SESSION
:
37 return "STOP_SESSION";
43 enum lttng_action_type
lttng_action_get_type(const struct lttng_action
*action
)
45 return action
? action
->type
: LTTNG_ACTION_TYPE_UNKNOWN
;
49 void lttng_action_init(struct lttng_action
*action
,
50 enum lttng_action_type type
,
51 action_validate_cb validate
,
52 action_serialize_cb serialize
,
53 action_equal_cb equal
,
54 action_destroy_cb destroy
,
55 action_get_rate_policy_cb get_rate_policy
,
56 action_add_error_query_results_cb add_error_query_results
,
57 action_mi_serialize_cb mi
)
59 urcu_ref_init(&action
->ref
);
61 action
->validate
= validate
;
62 action
->serialize
= serialize
;
63 action
->equal
= equal
;
64 action
->destroy
= destroy
;
65 action
->get_rate_policy
= get_rate_policy
;
66 action
->add_error_query_results
= add_error_query_results
;
67 action
->mi_serialize
= mi
;
69 action
->execution_request_counter
= 0;
70 action
->execution_counter
= 0;
71 action
->execution_failure_counter
= 0;
75 void action_destroy_ref(struct urcu_ref
*ref
)
77 struct lttng_action
*action
=
78 container_of(ref
, struct lttng_action
, ref
);
80 action
->destroy(action
);
84 void lttng_action_get(struct lttng_action
*action
)
86 urcu_ref_get(&action
->ref
);
90 void lttng_action_put(struct lttng_action
*action
)
96 LTTNG_ASSERT(action
->destroy
);
97 urcu_ref_put(&action
->ref
, action_destroy_ref
);
100 void lttng_action_destroy(struct lttng_action
*action
)
102 lttng_action_put(action
);
106 bool lttng_action_validate(struct lttng_action
*action
)
115 if (!action
->validate
) {
116 /* Sub-class guarantees that it can never be invalid. */
121 valid
= action
->validate(action
);
127 int lttng_action_serialize(struct lttng_action
*action
,
128 struct lttng_payload
*payload
)
131 struct lttng_action_comm action_comm
= {
132 .action_type
= (int8_t) action
->type
,
135 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &action_comm
,
136 sizeof(action_comm
));
141 ret
= action
->serialize(action
, payload
);
150 ssize_t
lttng_action_create_from_payload(struct lttng_payload_view
*view
,
151 struct lttng_action
**action
)
153 ssize_t consumed_len
, specific_action_consumed_len
;
154 action_create_from_payload_cb create_from_payload_cb
;
155 const struct lttng_action_comm
*action_comm
;
156 const struct lttng_payload_view action_comm_view
=
157 lttng_payload_view_from_view(
158 view
, 0, sizeof(*action_comm
));
160 if (!view
|| !action
) {
165 if (!lttng_payload_view_is_valid(&action_comm_view
)) {
166 /* Payload not large enough to contain the header. */
171 action_comm
= (const struct lttng_action_comm
*) action_comm_view
.buffer
.data
;
173 DBG("Create action from payload: action-type=%s",
174 lttng_action_type_string(action_comm
->action_type
));
176 switch (action_comm
->action_type
) {
177 case LTTNG_ACTION_TYPE_NOTIFY
:
178 create_from_payload_cb
= lttng_action_notify_create_from_payload
;
180 case LTTNG_ACTION_TYPE_ROTATE_SESSION
:
181 create_from_payload_cb
=
182 lttng_action_rotate_session_create_from_payload
;
184 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION
:
185 create_from_payload_cb
=
186 lttng_action_snapshot_session_create_from_payload
;
188 case LTTNG_ACTION_TYPE_START_SESSION
:
189 create_from_payload_cb
=
190 lttng_action_start_session_create_from_payload
;
192 case LTTNG_ACTION_TYPE_STOP_SESSION
:
193 create_from_payload_cb
=
194 lttng_action_stop_session_create_from_payload
;
196 case LTTNG_ACTION_TYPE_LIST
:
197 create_from_payload_cb
= lttng_action_list_create_from_payload
;
200 ERR("Failed to create action from payload, unhandled action type: action-type=%u (%s)",
201 action_comm
->action_type
,
202 lttng_action_type_string(
203 action_comm
->action_type
));
209 /* Create buffer view for the action-type-specific data. */
210 struct lttng_payload_view specific_action_view
=
211 lttng_payload_view_from_view(view
,
212 sizeof(struct lttng_action_comm
),
215 specific_action_consumed_len
= create_from_payload_cb(
216 &specific_action_view
, action
);
218 if (specific_action_consumed_len
< 0) {
219 ERR("Failed to create specific action from buffer.");
224 LTTNG_ASSERT(*action
);
226 consumed_len
= sizeof(struct lttng_action_comm
) +
227 specific_action_consumed_len
;
234 bool lttng_action_is_equal(const struct lttng_action
*a
,
235 const struct lttng_action
*b
)
237 bool is_equal
= false;
243 if (a
->type
!= b
->type
) {
252 LTTNG_ASSERT(a
->equal
);
253 is_equal
= a
->equal(a
, b
);
259 void lttng_action_increase_execution_request_count(struct lttng_action
*action
)
261 action
->execution_request_counter
++;
265 void lttng_action_increase_execution_count(struct lttng_action
*action
)
267 action
->execution_counter
++;
271 void lttng_action_increase_execution_failure_count(struct lttng_action
*action
)
273 uatomic_inc(&action
->execution_failure_counter
);
277 bool lttng_action_should_execute(const struct lttng_action
*action
)
279 const struct lttng_rate_policy
*policy
= NULL
;
280 bool execute
= false;
282 if (action
->get_rate_policy
== NULL
) {
287 policy
= action
->get_rate_policy(action
);
288 if (policy
== NULL
) {
293 execute
= lttng_rate_policy_should_execute(
294 policy
, action
->execution_request_counter
);
300 enum lttng_action_status
lttng_action_add_error_query_results(
301 const struct lttng_action
*action
,
302 struct lttng_error_query_results
*results
)
304 return action
->add_error_query_results(action
, results
);
308 enum lttng_action_status
lttng_action_generic_add_error_query_results(
309 const struct lttng_action
*action
,
310 struct lttng_error_query_results
*results
)
312 enum lttng_action_status action_status
;
313 struct lttng_error_query_result
*error_counter
= NULL
;
314 const uint64_t execution_failure_counter
=
315 uatomic_read(&action
->execution_failure_counter
);
317 error_counter
= lttng_error_query_result_counter_create(
318 "total execution failures",
319 "Aggregated count of errors encountered when executing the action",
320 execution_failure_counter
);
321 if (!error_counter
) {
322 action_status
= LTTNG_ACTION_STATUS_ERROR
;
326 if (lttng_error_query_results_add_result(
327 results
, error_counter
)) {
328 action_status
= LTTNG_ACTION_STATUS_ERROR
;
332 /* Ownership transferred to the results. */
333 error_counter
= NULL
;
334 action_status
= LTTNG_ACTION_STATUS_OK
;
336 lttng_error_query_result_destroy(error_counter
);
337 return action_status
;
341 enum lttng_error_code
lttng_action_mi_serialize(const struct lttng_trigger
*trigger
,
342 const struct lttng_action
*action
,
343 struct mi_writer
*writer
,
344 const struct mi_lttng_error_query_callbacks
345 *error_query_callbacks
,
346 struct lttng_dynamic_array
*action_path_indexes
)
349 enum lttng_error_code ret_code
;
350 struct lttng_action_path
*action_path
= NULL
;
351 struct lttng_error_query_results
*error_query_results
= NULL
;
353 LTTNG_ASSERT(action
);
354 LTTNG_ASSERT(writer
);
357 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_action
);
362 if (action
->type
== LTTNG_ACTION_TYPE_LIST
) {
364 * Recursion is safe since action lists can't be nested for
367 ret_code
= lttng_action_list_mi_serialize(trigger
, action
, writer
,
368 error_query_callbacks
, action_path_indexes
);
369 if (ret_code
!= LTTNG_OK
) {
373 /* Nothing else to do. */
374 goto close_action_element
;
377 LTTNG_ASSERT(action
->mi_serialize
);
378 ret_code
= action
->mi_serialize(action
, writer
);
379 if (ret_code
!= LTTNG_OK
) {
383 /* Error query for the action. */
384 if (error_query_callbacks
&& error_query_callbacks
->action_cb
) {
385 const uint64_t *action_path_indexes_raw_pointer
= NULL
;
386 const size_t action_path_indexes_size
=
387 lttng_dynamic_array_get_count(
388 action_path_indexes
);
390 if (action_path_indexes_size
!= 0) {
391 action_path_indexes_raw_pointer
=
392 (const uint64_t *) action_path_indexes
396 action_path
= lttng_action_path_create(
397 action_path_indexes_raw_pointer
,
398 action_path_indexes_size
);
399 LTTNG_ASSERT(action_path
);
401 ret_code
= error_query_callbacks
->action_cb(
402 trigger
, action_path
, &error_query_results
);
403 if (ret_code
!= LTTNG_OK
) {
407 /* Serialize the error query results. */
408 ret_code
= lttng_error_query_results_mi_serialize(
409 error_query_results
, writer
);
410 if (ret_code
!= LTTNG_OK
) {
415 close_action_element
:
417 ret
= mi_lttng_writer_close_element(writer
);
426 ret_code
= LTTNG_ERR_MI_IO_FAIL
;
428 lttng_action_path_destroy(action_path
);
429 lttng_error_query_results_destroy(error_query_results
);