18b756dce4c489e2a88f8ef318c366c03c41ce2e
[lttng-tools.git] / src / common / conditions / condition.cpp
1 /*
2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #include <common/buffer-view.h>
9 #include <common/dynamic-buffer.h>
10 #include <common/error.h>
11 #include <common/macros.h>
12 #include <common/mi-lttng.h>
13 #include <lttng/condition/buffer-usage-internal.h>
14 #include <lttng/condition/condition-internal.h>
15 #include <lttng/condition/event-rule-matches-internal.h>
16 #include <lttng/condition/session-consumed-size-internal.h>
17 #include <lttng/condition/session-rotation-internal.h>
18 #include <lttng/error-query-internal.h>
19 #include <stdbool.h>
20
21 enum lttng_condition_type lttng_condition_get_type(
22 const struct lttng_condition *condition)
23 {
24 return condition ? condition->type : LTTNG_CONDITION_TYPE_UNKNOWN;
25 }
26
27 void lttng_condition_destroy(struct lttng_condition *condition)
28 {
29 lttng_condition_put(condition);
30 }
31
32 static void condition_destroy_ref(struct urcu_ref *ref)
33 {
34 struct lttng_condition *condition =
35 container_of(ref, struct lttng_condition, ref);
36
37 condition->destroy(condition);
38 }
39
40 void lttng_condition_get(struct lttng_condition *condition)
41 {
42 urcu_ref_get(&condition->ref);
43 }
44
45 void lttng_condition_put(struct lttng_condition *condition)
46 {
47 if (!condition) {
48 return;
49 }
50
51 LTTNG_ASSERT(condition->destroy);
52 urcu_ref_put(&condition->ref, condition_destroy_ref);
53 }
54
55
56 bool lttng_condition_validate(const struct lttng_condition *condition)
57 {
58 bool valid;
59
60 if (!condition) {
61 valid = false;
62 goto end;
63 }
64
65 if (!condition->validate) {
66 /* Sub-class guarantees that it can never be invalid. */
67 valid = true;
68 goto end;
69 }
70
71 valid = condition->validate(condition);
72 end:
73 return valid;
74 }
75
76 int lttng_condition_serialize(const struct lttng_condition *condition,
77 struct lttng_payload *payload)
78 {
79 int ret;
80 struct lttng_condition_comm condition_comm = {};
81
82 if (!condition) {
83 ret = -1;
84 goto end;
85 }
86
87 condition_comm.condition_type = (int8_t) condition->type;
88
89 ret = lttng_dynamic_buffer_append(&payload->buffer, &condition_comm,
90 sizeof(condition_comm));
91 if (ret) {
92 goto end;
93 }
94
95 ret = condition->serialize(condition, payload);
96 if (ret) {
97 goto end;
98 }
99 end:
100 return ret;
101 }
102
103 bool lttng_condition_is_equal(const struct lttng_condition *a,
104 const struct lttng_condition *b)
105 {
106 bool is_equal = false;
107
108 if (!a || !b) {
109 goto end;
110 }
111
112 if (a->type != b->type) {
113 goto end;
114 }
115
116 if (a == b) {
117 is_equal = true;
118 goto end;
119 }
120
121 is_equal = a->equal ? a->equal(a, b) : true;
122 end:
123 return is_equal;
124 }
125
126 ssize_t lttng_condition_create_from_payload(
127 struct lttng_payload_view *view,
128 struct lttng_condition **condition)
129 {
130 ssize_t ret, condition_size = 0;
131 condition_create_from_payload_cb create_from_payload = NULL;
132 const struct lttng_condition_comm *condition_comm;
133 const struct lttng_payload_view condition_comm_view =
134 lttng_payload_view_from_view(
135 view, 0, sizeof(*condition_comm));
136
137 if (!view || !condition) {
138 ret = -1;
139 goto end;
140 }
141
142 if (!lttng_payload_view_is_valid(&condition_comm_view)) {
143 /* Payload not large enough to contain the header. */
144 ret = -1;
145 goto end;
146 }
147
148 DBG("Deserializing condition from buffer");
149 condition_comm = (typeof(condition_comm)) condition_comm_view.buffer.data;
150 condition_size += sizeof(*condition_comm);
151
152 switch ((enum lttng_condition_type) condition_comm->condition_type) {
153 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
154 create_from_payload = lttng_condition_buffer_usage_low_create_from_payload;
155 break;
156 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
157 create_from_payload = lttng_condition_buffer_usage_high_create_from_payload;
158 break;
159 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
160 create_from_payload = lttng_condition_session_consumed_size_create_from_payload;
161 break;
162 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
163 create_from_payload = lttng_condition_session_rotation_ongoing_create_from_payload;
164 break;
165 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
166 create_from_payload = lttng_condition_session_rotation_completed_create_from_payload;
167 break;
168 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES:
169 create_from_payload =
170 lttng_condition_event_rule_matches_create_from_payload;
171 break;
172 default:
173 ERR("Attempted to create condition of unknown type (%i)",
174 (int) condition_comm->condition_type);
175 ret = -1;
176 goto end;
177 }
178
179 if (create_from_payload) {
180 struct lttng_payload_view condition_view =
181 lttng_payload_view_from_view(view,
182 sizeof(*condition_comm), -1);
183
184 ret = create_from_payload(&condition_view, condition);
185 if (ret < 0) {
186 goto end;
187 }
188 condition_size += ret;
189
190 } else {
191 abort();
192 }
193
194 ret = condition_size;
195 end:
196 return ret;
197 }
198
199 void lttng_condition_init(struct lttng_condition *condition,
200 enum lttng_condition_type type)
201 {
202 condition->type = type;
203 urcu_ref_init(&condition->ref);
204 }
205
206 const char *lttng_condition_type_str(enum lttng_condition_type type)
207 {
208 switch (type) {
209 case LTTNG_CONDITION_TYPE_UNKNOWN:
210 return "unknown";
211
212 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
213 return "session consumed size";
214
215 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
216 return "buffer usage high";
217
218 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
219 return "buffer usage low";
220
221 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
222 return "session rotation ongoing";
223
224 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
225 return "session rotation completed";
226
227 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES:
228 return "event rule matches";
229
230 default:
231 return "???";
232 }
233 }
234
235 enum lttng_error_code lttng_condition_mi_serialize(
236 const struct lttng_trigger *trigger,
237 const struct lttng_condition *condition,
238 struct mi_writer *writer,
239 const struct mi_lttng_error_query_callbacks *error_query_callbacks)
240 {
241 int ret;
242 enum lttng_error_code ret_code;
243 struct lttng_error_query_results *error_query_results = NULL;
244
245 LTTNG_ASSERT(condition);
246 LTTNG_ASSERT(writer);
247 LTTNG_ASSERT(condition->mi_serialize);
248
249 /* Open condition element. */
250 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_condition);
251 if (ret) {
252 goto mi_error;
253 }
254
255 /* Serialize underlying condition. */
256 ret_code = condition->mi_serialize(condition, writer);
257 if (ret_code != LTTNG_OK) {
258 goto end;
259 }
260
261 /* Serialize error query results for the action. */
262 if (error_query_callbacks && error_query_callbacks->action_cb) {
263 ret_code = error_query_callbacks->condition_cb(
264 trigger, &error_query_results);
265 if (ret_code != LTTNG_OK) {
266 goto end;
267 }
268
269 ret_code = lttng_error_query_results_mi_serialize(
270 error_query_results, writer);
271 if (ret_code != LTTNG_OK) {
272 goto end;
273 }
274 }
275
276 /* Close condition element. */
277 ret = mi_lttng_writer_close_element(writer);
278 if (ret) {
279 goto mi_error;
280 }
281
282 ret_code = LTTNG_OK;
283 goto end;
284
285 mi_error:
286 ret_code = LTTNG_ERR_MI_IO_FAIL;
287 end:
288 lttng_error_query_results_destroy(error_query_results);
289 return ret_code;
290 }
This page took 0.04796 seconds and 3 git commands to generate.