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/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
)
58 urcu_ref_init(&action
->ref
);
60 action
->validate
= validate
;
61 action
->serialize
= serialize
;
62 action
->equal
= equal
;
63 action
->destroy
= destroy
;
64 action
->get_rate_policy
= get_rate_policy
;
65 action
->add_error_query_results
= add_error_query_results
;
67 action
->execution_request_counter
= 0;
68 action
->execution_counter
= 0;
69 action
->execution_failure_counter
= 0;
73 void action_destroy_ref(struct urcu_ref
*ref
)
75 struct lttng_action
*action
=
76 container_of(ref
, struct lttng_action
, ref
);
78 action
->destroy(action
);
82 void lttng_action_get(struct lttng_action
*action
)
84 urcu_ref_get(&action
->ref
);
88 void lttng_action_put(struct lttng_action
*action
)
94 assert(action
->destroy
);
95 urcu_ref_put(&action
->ref
, action_destroy_ref
);
98 void lttng_action_destroy(struct lttng_action
*action
)
100 lttng_action_put(action
);
104 bool lttng_action_validate(struct lttng_action
*action
)
113 if (!action
->validate
) {
114 /* Sub-class guarantees that it can never be invalid. */
119 valid
= action
->validate(action
);
125 int lttng_action_serialize(struct lttng_action
*action
,
126 struct lttng_payload
*payload
)
129 struct lttng_action_comm action_comm
= {
130 .action_type
= (int8_t) action
->type
,
133 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &action_comm
,
134 sizeof(action_comm
));
139 ret
= action
->serialize(action
, payload
);
148 ssize_t
lttng_action_create_from_payload(struct lttng_payload_view
*view
,
149 struct lttng_action
**action
)
151 ssize_t consumed_len
, specific_action_consumed_len
;
152 action_create_from_payload_cb create_from_payload_cb
;
153 const struct lttng_action_comm
*action_comm
;
154 const struct lttng_payload_view action_comm_view
=
155 lttng_payload_view_from_view(
156 view
, 0, sizeof(*action_comm
));
158 if (!view
|| !action
) {
163 if (!lttng_payload_view_is_valid(&action_comm_view
)) {
164 /* Payload not large enough to contain the header. */
169 action_comm
= (const struct lttng_action_comm
*) action_comm_view
.buffer
.data
;
171 DBG("Create action from payload: action-type=%s",
172 lttng_action_type_string(action_comm
->action_type
));
174 switch (action_comm
->action_type
) {
175 case LTTNG_ACTION_TYPE_NOTIFY
:
176 create_from_payload_cb
= lttng_action_notify_create_from_payload
;
178 case LTTNG_ACTION_TYPE_ROTATE_SESSION
:
179 create_from_payload_cb
=
180 lttng_action_rotate_session_create_from_payload
;
182 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION
:
183 create_from_payload_cb
=
184 lttng_action_snapshot_session_create_from_payload
;
186 case LTTNG_ACTION_TYPE_START_SESSION
:
187 create_from_payload_cb
=
188 lttng_action_start_session_create_from_payload
;
190 case LTTNG_ACTION_TYPE_STOP_SESSION
:
191 create_from_payload_cb
=
192 lttng_action_stop_session_create_from_payload
;
194 case LTTNG_ACTION_TYPE_LIST
:
195 create_from_payload_cb
= lttng_action_list_create_from_payload
;
198 ERR("Failed to create action from payload, unhandled action type: action-type=%u (%s)",
199 action_comm
->action_type
,
200 lttng_action_type_string(
201 action_comm
->action_type
));
207 /* Create buffer view for the action-type-specific data. */
208 struct lttng_payload_view specific_action_view
=
209 lttng_payload_view_from_view(view
,
210 sizeof(struct lttng_action_comm
),
213 specific_action_consumed_len
= create_from_payload_cb(
214 &specific_action_view
, action
);
216 if (specific_action_consumed_len
< 0) {
217 ERR("Failed to create specific action from buffer.");
224 consumed_len
= sizeof(struct lttng_action_comm
) +
225 specific_action_consumed_len
;
232 bool lttng_action_is_equal(const struct lttng_action
*a
,
233 const struct lttng_action
*b
)
235 bool is_equal
= false;
241 if (a
->type
!= b
->type
) {
251 is_equal
= a
->equal(a
, b
);
257 void lttng_action_increase_execution_request_count(struct lttng_action
*action
)
259 action
->execution_request_counter
++;
263 void lttng_action_increase_execution_count(struct lttng_action
*action
)
265 action
->execution_counter
++;
269 void lttng_action_increase_execution_failure_count(struct lttng_action
*action
)
271 uatomic_inc(&action
->execution_failure_counter
);
275 bool lttng_action_should_execute(const struct lttng_action
*action
)
277 const struct lttng_rate_policy
*policy
= NULL
;
278 bool execute
= false;
280 if (action
->get_rate_policy
== NULL
) {
285 policy
= action
->get_rate_policy(action
);
286 if (policy
== NULL
) {
291 execute
= lttng_rate_policy_should_execute(
292 policy
, action
->execution_request_counter
);
298 enum lttng_action_status
lttng_action_add_error_query_results(
299 const struct lttng_action
*action
,
300 struct lttng_error_query_results
*results
)
302 return action
->add_error_query_results(action
, results
);
306 enum lttng_action_status
lttng_action_generic_add_error_query_results(
307 const struct lttng_action
*action
,
308 struct lttng_error_query_results
*results
)
310 enum lttng_action_status action_status
;
311 struct lttng_error_query_result
*error_counter
= NULL
;
312 const uint64_t execution_failure_counter
=
313 uatomic_read(&action
->execution_failure_counter
);
315 error_counter
= lttng_error_query_result_counter_create(
316 "total execution failures",
317 "Aggregated count of errors encountered when executing the action",
318 execution_failure_counter
);
319 if (!error_counter
) {
320 action_status
= LTTNG_ACTION_STATUS_ERROR
;
324 if (lttng_error_query_results_add_result(
325 results
, error_counter
)) {
326 action_status
= LTTNG_ACTION_STATUS_ERROR
;
330 /* Ownership transferred to the results. */
331 error_counter
= NULL
;
332 action_status
= LTTNG_ACTION_STATUS_OK
;
334 lttng_error_query_result_destroy(error_counter
);
335 return action_status
;