Commit | Line | Data |
---|---|---|
a58c490f | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
a58c490f | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: LGPL-2.1-only |
a58c490f | 5 | * |
a58c490f JG |
6 | */ |
7 | ||
14ec7e87 JR |
8 | #include <assert.h> |
9 | #include <common/error.h> | |
a58c490f | 10 | #include <lttng/action/action-internal.h> |
2d57482c | 11 | #include <lttng/action/firing-policy-internal.h> |
0c51e8f3 | 12 | #include <lttng/action/group-internal.h> |
a58c490f | 13 | #include <lttng/action/notify-internal.h> |
bfb2ec6a | 14 | #include <lttng/action/rotate-session-internal.h> |
757c48a2 | 15 | #include <lttng/action/snapshot-session-internal.h> |
58397d0d | 16 | #include <lttng/action/start-session-internal.h> |
931bdbaa | 17 | #include <lttng/action/stop-session-internal.h> |
a58c490f | 18 | |
10615eee JR |
19 | LTTNG_HIDDEN |
20 | const char *lttng_action_type_string(enum lttng_action_type action_type) | |
2666d352 SM |
21 | { |
22 | switch (action_type) { | |
23 | case LTTNG_ACTION_TYPE_UNKNOWN: | |
24 | return "UNKNOWN"; | |
0c51e8f3 SM |
25 | case LTTNG_ACTION_TYPE_GROUP: |
26 | return "GROUP"; | |
2666d352 SM |
27 | case LTTNG_ACTION_TYPE_NOTIFY: |
28 | return "NOTIFY"; | |
bfb2ec6a SM |
29 | case LTTNG_ACTION_TYPE_ROTATE_SESSION: |
30 | return "ROTATE_SESSION"; | |
757c48a2 SM |
31 | case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION: |
32 | return "SNAPSHOT_SESSION"; | |
58397d0d SM |
33 | case LTTNG_ACTION_TYPE_START_SESSION: |
34 | return "START_SESSION"; | |
931bdbaa SM |
35 | case LTTNG_ACTION_TYPE_STOP_SESSION: |
36 | return "STOP_SESSION"; | |
2666d352 SM |
37 | default: |
38 | return "???"; | |
39 | } | |
40 | } | |
41 | ||
17182cfd | 42 | enum lttng_action_type lttng_action_get_type(const struct lttng_action *action) |
a58c490f JG |
43 | { |
44 | return action ? action->type : LTTNG_ACTION_TYPE_UNKNOWN; | |
45 | } | |
46 | ||
6acb3f46 | 47 | LTTNG_HIDDEN |
2d57482c | 48 | void lttng_action_init(struct lttng_action *action, |
6acb3f46 SM |
49 | enum lttng_action_type type, |
50 | action_validate_cb validate, | |
51 | action_serialize_cb serialize, | |
3dd04a6a | 52 | action_equal_cb equal, |
2d57482c JR |
53 | action_destroy_cb destroy, |
54 | action_get_firing_policy_cb get_firing_policy) | |
6acb3f46 | 55 | { |
c852ce4e | 56 | urcu_ref_init(&action->ref); |
6acb3f46 SM |
57 | action->type = type; |
58 | action->validate = validate; | |
59 | action->serialize = serialize; | |
3dd04a6a | 60 | action->equal = equal; |
6acb3f46 | 61 | action->destroy = destroy; |
2d57482c JR |
62 | action->get_firing_policy = get_firing_policy; |
63 | ||
64 | action->execution_request_counter = 0; | |
65 | action->execution_counter = 0; | |
66 | action->execution_failure_counter = 0; | |
6acb3f46 SM |
67 | } |
68 | ||
c852ce4e JG |
69 | static |
70 | void action_destroy_ref(struct urcu_ref *ref) | |
71 | { | |
72 | struct lttng_action *action = | |
73 | container_of(ref, struct lttng_action, ref); | |
74 | ||
75 | action->destroy(action); | |
76 | } | |
77 | ||
78 | LTTNG_HIDDEN | |
79 | void lttng_action_get(struct lttng_action *action) | |
80 | { | |
81 | urcu_ref_get(&action->ref); | |
82 | } | |
83 | ||
84 | LTTNG_HIDDEN | |
85 | void lttng_action_put(struct lttng_action *action) | |
a58c490f JG |
86 | { |
87 | if (!action) { | |
88 | return; | |
89 | } | |
90 | ||
91 | assert(action->destroy); | |
c852ce4e JG |
92 | urcu_ref_put(&action->ref, action_destroy_ref); |
93 | } | |
94 | ||
95 | void lttng_action_destroy(struct lttng_action *action) | |
96 | { | |
97 | lttng_action_put(action); | |
a58c490f JG |
98 | } |
99 | ||
100 | LTTNG_HIDDEN | |
101 | bool lttng_action_validate(struct lttng_action *action) | |
102 | { | |
103 | bool valid; | |
104 | ||
105 | if (!action) { | |
106 | valid = false; | |
107 | goto end; | |
108 | } | |
109 | ||
110 | if (!action->validate) { | |
111 | /* Sub-class guarantees that it can never be invalid. */ | |
112 | valid = true; | |
113 | goto end; | |
114 | } | |
115 | ||
116 | valid = action->validate(action); | |
117 | end: | |
118 | return valid; | |
119 | } | |
120 | ||
121 | LTTNG_HIDDEN | |
3647288f | 122 | int lttng_action_serialize(struct lttng_action *action, |
c0a66c84 | 123 | struct lttng_payload *payload) |
a58c490f | 124 | { |
3647288f JG |
125 | int ret; |
126 | struct lttng_action_comm action_comm = { | |
127 | .action_type = (int8_t) action->type, | |
128 | }; | |
129 | ||
c0a66c84 | 130 | ret = lttng_dynamic_buffer_append(&payload->buffer, &action_comm, |
3647288f JG |
131 | sizeof(action_comm)); |
132 | if (ret) { | |
a58c490f JG |
133 | goto end; |
134 | } | |
135 | ||
c0a66c84 | 136 | ret = action->serialize(action, payload); |
3647288f | 137 | if (ret) { |
a58c490f JG |
138 | goto end; |
139 | } | |
a58c490f JG |
140 | end: |
141 | return ret; | |
142 | } | |
143 | ||
144 | LTTNG_HIDDEN | |
c0a66c84 | 145 | ssize_t lttng_action_create_from_payload(struct lttng_payload_view *view, |
869a3c2d | 146 | struct lttng_action **action) |
a58c490f | 147 | { |
869a3c2d | 148 | ssize_t consumed_len, specific_action_consumed_len; |
c0a66c84 | 149 | action_create_from_payload_cb create_from_payload_cb; |
3e6e0df2 JG |
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)); | |
a58c490f | 154 | |
869a3c2d SM |
155 | if (!view || !action) { |
156 | consumed_len = -1; | |
a58c490f JG |
157 | goto end; |
158 | } | |
159 | ||
3e6e0df2 JG |
160 | if (!lttng_payload_view_is_valid(&action_comm_view)) { |
161 | /* Payload not large enough to contain the header. */ | |
162 | consumed_len = -1; | |
163 | goto end; | |
164 | } | |
165 | ||
166 | action_comm = (const struct lttng_action_comm *) action_comm_view.buffer.data; | |
869a3c2d | 167 | |
c0a66c84 | 168 | DBG("Create action from payload: action-type=%s", |
2666d352 SM |
169 | lttng_action_type_string(action_comm->action_type)); |
170 | ||
a58c490f JG |
171 | switch (action_comm->action_type) { |
172 | case LTTNG_ACTION_TYPE_NOTIFY: | |
c0a66c84 | 173 | create_from_payload_cb = lttng_action_notify_create_from_payload; |
a58c490f | 174 | break; |
bfb2ec6a | 175 | case LTTNG_ACTION_TYPE_ROTATE_SESSION: |
c0a66c84 JG |
176 | create_from_payload_cb = |
177 | lttng_action_rotate_session_create_from_payload; | |
bfb2ec6a | 178 | break; |
757c48a2 SM |
179 | case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION: |
180 | create_from_payload_cb = | |
181 | lttng_action_snapshot_session_create_from_payload; | |
182 | break; | |
58397d0d | 183 | case LTTNG_ACTION_TYPE_START_SESSION: |
c0a66c84 JG |
184 | create_from_payload_cb = |
185 | lttng_action_start_session_create_from_payload; | |
58397d0d | 186 | break; |
931bdbaa | 187 | case LTTNG_ACTION_TYPE_STOP_SESSION: |
c0a66c84 JG |
188 | create_from_payload_cb = |
189 | lttng_action_stop_session_create_from_payload; | |
931bdbaa | 190 | break; |
0c51e8f3 SM |
191 | case LTTNG_ACTION_TYPE_GROUP: |
192 | create_from_payload_cb = lttng_action_group_create_from_payload; | |
193 | break; | |
a58c490f | 194 | default: |
c0a66c84 | 195 | ERR("Failed to create action from payload, unhandled action type: action-type=%u (%s)", |
2666d352 SM |
196 | action_comm->action_type, |
197 | lttng_action_type_string( | |
198 | action_comm->action_type)); | |
869a3c2d | 199 | consumed_len = -1; |
a58c490f JG |
200 | goto end; |
201 | } | |
202 | ||
c0a66c84 JG |
203 | { |
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), | |
208 | -1); | |
869a3c2d | 209 | |
c0a66c84 JG |
210 | specific_action_consumed_len = create_from_payload_cb( |
211 | &specific_action_view, action); | |
212 | } | |
869a3c2d SM |
213 | if (specific_action_consumed_len < 0) { |
214 | ERR("Failed to create specific action from buffer."); | |
215 | consumed_len = -1; | |
a58c490f JG |
216 | goto end; |
217 | } | |
869a3c2d SM |
218 | |
219 | assert(*action); | |
220 | ||
221 | consumed_len = sizeof(struct lttng_action_comm) + | |
222 | specific_action_consumed_len; | |
223 | ||
a58c490f | 224 | end: |
869a3c2d | 225 | return consumed_len; |
a58c490f | 226 | } |
3dd04a6a JR |
227 | |
228 | LTTNG_HIDDEN | |
229 | bool lttng_action_is_equal(const struct lttng_action *a, | |
230 | const struct lttng_action *b) | |
231 | { | |
232 | bool is_equal = false; | |
233 | ||
234 | if (!a || !b) { | |
235 | goto end; | |
236 | } | |
237 | ||
238 | if (a->type != b->type) { | |
239 | goto end; | |
240 | } | |
241 | ||
242 | if (a == b) { | |
243 | is_equal = true; | |
244 | goto end; | |
245 | } | |
246 | ||
247 | assert(a->equal); | |
248 | is_equal = a->equal(a, b); | |
249 | end: | |
250 | return is_equal; | |
251 | } | |
2d57482c JR |
252 | |
253 | LTTNG_HIDDEN | |
254 | void lttng_action_increase_execution_request_count(struct lttng_action *action) | |
255 | { | |
256 | action->execution_request_counter++; | |
257 | } | |
258 | ||
259 | LTTNG_HIDDEN | |
260 | void lttng_action_increase_execution_count(struct lttng_action *action) | |
261 | { | |
262 | action->execution_counter++; | |
263 | } | |
264 | ||
265 | LTTNG_HIDDEN | |
266 | void lttng_action_increase_execution_failure_count(struct lttng_action *action) | |
267 | { | |
268 | action->execution_failure_counter++; | |
269 | } | |
270 | ||
271 | LTTNG_HIDDEN | |
272 | bool lttng_action_should_execute(const struct lttng_action *action) | |
273 | { | |
274 | const struct lttng_firing_policy *policy = NULL; | |
275 | bool execute = false; | |
276 | ||
277 | if (action->get_firing_policy == NULL) { | |
278 | execute = true; | |
279 | goto end; | |
280 | } | |
281 | ||
282 | policy = action->get_firing_policy(action); | |
283 | if (policy == NULL) { | |
284 | execute = true; | |
285 | goto end; | |
286 | } | |
287 | ||
288 | execute = lttng_firing_policy_should_execute( | |
289 | policy, action->execution_request_counter); | |
290 | end: | |
291 | return execute; | |
292 | } |