Use the dynamic buffer to serialize notification objects
[lttng-tools.git] / src / common / session-consumed-size.c
1 /*
2 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License, version 2.1 only,
6 * as published by the Free Software Foundation.
7 *
8 * This library is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
11 * for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18 #include <lttng/condition/condition-internal.h>
19 #include <lttng/condition/session-consumed-size-internal.h>
20 #include <lttng/constant.h>
21 #include <common/macros.h>
22 #include <common/error.h>
23 #include <assert.h>
24 #include <math.h>
25 #include <float.h>
26 #include <time.h>
27
28 #define IS_CONSUMED_SIZE_CONDITION(condition) ( \
29 lttng_condition_get_type(condition) == LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE \
30 )
31
32 #define IS_CONSUMED_SIZE_EVALUATION(evaluation) ( \
33 lttng_evaluation_get_type(evaluation) == LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE \
34 )
35
36 static
37 void lttng_condition_session_consumed_size_destroy(struct lttng_condition *condition)
38 {
39 struct lttng_condition_session_consumed_size *consumed_size;
40
41 consumed_size = container_of(condition,
42 struct lttng_condition_session_consumed_size, parent);
43
44 free(consumed_size->session_name);
45 free(consumed_size);
46 }
47
48 static
49 bool lttng_condition_session_consumed_size_validate(
50 const struct lttng_condition *condition)
51 {
52 bool valid = false;
53 struct lttng_condition_session_consumed_size *consumed;
54
55 if (!condition) {
56 goto end;
57 }
58
59 consumed = container_of(condition, struct lttng_condition_session_consumed_size,
60 parent);
61 if (!consumed->session_name) {
62 ERR("Invalid buffer condition: a target session name must be set.");
63 goto end;
64 }
65 if (!consumed->consumed_threshold_bytes.set) {
66 ERR("Invalid session condition: a threshold must be set.");
67 goto end;
68 }
69
70 valid = true;
71 end:
72 return valid;
73 }
74
75 static
76 int lttng_condition_session_consumed_size_serialize(
77 const struct lttng_condition *condition,
78 struct lttng_dynamic_buffer *buf)
79 {
80 int ret;
81 size_t session_name_len;
82 struct lttng_condition_session_consumed_size *consumed;
83 struct lttng_condition_session_consumed_size_comm consumed_comm;
84
85 if (!condition || !IS_CONSUMED_SIZE_CONDITION(condition)) {
86 ret = -1;
87 goto end;
88 }
89
90 DBG("Serializing session consumed size condition");
91 consumed = container_of(condition,
92 struct lttng_condition_session_consumed_size,
93 parent);
94
95 session_name_len = strlen(consumed->session_name) + 1;
96 if (session_name_len > LTTNG_NAME_MAX) {
97 ret = -1;
98 goto end;
99 }
100
101 consumed_comm.consumed_threshold_bytes =
102 consumed->consumed_threshold_bytes.value;
103 consumed_comm.session_name_len = (uint32_t) session_name_len;
104
105 ret = lttng_dynamic_buffer_append(buf, &consumed_comm,
106 sizeof(consumed_comm));
107 if (ret) {
108 goto end;
109 }
110 ret = lttng_dynamic_buffer_append(buf, consumed->session_name,
111 session_name_len);
112 if (ret) {
113 goto end;
114 }
115 end:
116 return ret;
117 }
118
119 static
120 bool lttng_condition_session_consumed_size_is_equal(const struct lttng_condition *_a,
121 const struct lttng_condition *_b)
122 {
123 bool is_equal = false;
124 struct lttng_condition_session_consumed_size *a, *b;
125
126 a = container_of(_a, struct lttng_condition_session_consumed_size, parent);
127 b = container_of(_b, struct lttng_condition_session_consumed_size, parent);
128
129 if (a->consumed_threshold_bytes.set && b->consumed_threshold_bytes.set) {
130 uint64_t a_value, b_value;
131
132 a_value = a->consumed_threshold_bytes.value;
133 b_value = b->consumed_threshold_bytes.value;
134 if (a_value != b_value) {
135 goto end;
136 }
137 }
138
139 if ((a->session_name && !b->session_name) ||
140 (!a->session_name && b->session_name)) {
141 goto end;
142 }
143
144 is_equal = true;
145 end:
146 return is_equal;
147 }
148
149 struct lttng_condition *lttng_condition_session_consumed_size_create(void)
150 {
151 struct lttng_condition_session_consumed_size *condition;
152
153 condition = zmalloc(sizeof(struct lttng_condition_session_consumed_size));
154 if (!condition) {
155 return NULL;
156 }
157
158 lttng_condition_init(&condition->parent, LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE);
159 condition->parent.validate = lttng_condition_session_consumed_size_validate;
160 condition->parent.serialize = lttng_condition_session_consumed_size_serialize;
161 condition->parent.equal = lttng_condition_session_consumed_size_is_equal;
162 condition->parent.destroy = lttng_condition_session_consumed_size_destroy;
163 return &condition->parent;
164 }
165
166 static
167 ssize_t init_condition_from_buffer(struct lttng_condition *condition,
168 const struct lttng_buffer_view *src_view)
169 {
170 ssize_t ret, condition_size;
171 enum lttng_condition_status status;
172 const struct lttng_condition_session_consumed_size_comm *condition_comm;
173 const char *session_name;
174 struct lttng_buffer_view names_view;
175
176 if (src_view->size < sizeof(*condition_comm)) {
177 ERR("Failed to initialize from malformed condition buffer: buffer too short to contain header");
178 ret = -1;
179 goto end;
180 }
181
182 condition_comm = (const struct lttng_condition_session_consumed_size_comm *) src_view->data;
183 names_view = lttng_buffer_view_from_view(src_view,
184 sizeof(*condition_comm), -1);
185
186 if (condition_comm->session_name_len > LTTNG_NAME_MAX) {
187 ERR("Failed to initialize from malformed condition buffer: name exceeds LTTNG_MAX_NAME");
188 ret = -1;
189 goto end;
190 }
191
192 if (names_view.size < condition_comm->session_name_len) {
193 ERR("Failed to initialize from malformed condition buffer: buffer too short to contain element names");
194 ret = -1;
195 goto end;
196 }
197
198 status = lttng_condition_session_consumed_size_set_threshold(condition,
199 condition_comm->consumed_threshold_bytes);
200 if (status != LTTNG_CONDITION_STATUS_OK) {
201 ERR("Failed to initialize session consumed condition threshold");
202 ret = -1;
203 goto end;
204 }
205
206 session_name = names_view.data;
207 if (*(session_name + condition_comm->session_name_len - 1) != '\0') {
208 ERR("Malformed session name encountered in condition buffer");
209 ret = -1;
210 goto end;
211 }
212
213 status = lttng_condition_session_consumed_size_set_session_name(condition,
214 session_name);
215 if (status != LTTNG_CONDITION_STATUS_OK) {
216 ERR("Failed to set buffer consumed session name");
217 ret = -1;
218 goto end;
219 }
220
221 if (!lttng_condition_validate(condition)) {
222 ret = -1;
223 goto end;
224 }
225
226 condition_size = sizeof(*condition_comm) +
227 (ssize_t) condition_comm->session_name_len;
228 ret = condition_size;
229 end:
230 return ret;
231 }
232
233 LTTNG_HIDDEN
234 ssize_t lttng_condition_session_consumed_size_create_from_buffer(
235 const struct lttng_buffer_view *view,
236 struct lttng_condition **_condition)
237 {
238 ssize_t ret;
239 struct lttng_condition *condition =
240 lttng_condition_session_consumed_size_create();
241
242 if (!_condition || !condition) {
243 ret = -1;
244 goto error;
245 }
246
247 ret = init_condition_from_buffer(condition, view);
248 if (ret < 0) {
249 goto error;
250 }
251
252 *_condition = condition;
253 return ret;
254 error:
255 lttng_condition_destroy(condition);
256 return ret;
257 }
258
259 static
260 struct lttng_evaluation *create_evaluation_from_buffer(
261 enum lttng_condition_type type,
262 const struct lttng_buffer_view *view)
263 {
264 const struct lttng_evaluation_session_consumed_size_comm *comm =
265 (const struct lttng_evaluation_session_consumed_size_comm *) view->data;
266 struct lttng_evaluation *evaluation = NULL;
267
268 if (view->size < sizeof(*comm)) {
269 goto end;
270 }
271
272 evaluation = lttng_evaluation_session_consumed_size_create(type,
273 comm->session_consumed);
274 end:
275 return evaluation;
276 }
277
278 LTTNG_HIDDEN
279 ssize_t lttng_evaluation_session_consumed_size_create_from_buffer(
280 const struct lttng_buffer_view *view,
281 struct lttng_evaluation **_evaluation)
282 {
283 ssize_t ret;
284 struct lttng_evaluation *evaluation = NULL;
285
286 if (!_evaluation) {
287 ret = -1;
288 goto error;
289 }
290
291 evaluation = create_evaluation_from_buffer(
292 LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE, view);
293 if (!evaluation) {
294 ret = -1;
295 goto error;
296 }
297
298 *_evaluation = evaluation;
299 ret = sizeof(struct lttng_evaluation_session_consumed_size_comm);
300 return ret;
301 error:
302 lttng_evaluation_destroy(evaluation);
303 return ret;
304 }
305
306 enum lttng_condition_status
307 lttng_condition_session_consumed_size_get_threshold(
308 const struct lttng_condition *condition,
309 uint64_t *consumed_threshold_bytes)
310 {
311 struct lttng_condition_session_consumed_size *consumed;
312 enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK;
313
314 if (!condition || !IS_CONSUMED_SIZE_CONDITION(condition) || !consumed_threshold_bytes) {
315 status = LTTNG_CONDITION_STATUS_INVALID;
316 goto end;
317 }
318
319 consumed = container_of(condition, struct lttng_condition_session_consumed_size,
320 parent);
321 if (!consumed->consumed_threshold_bytes.set) {
322 status = LTTNG_CONDITION_STATUS_UNSET;
323 goto end;
324 }
325 *consumed_threshold_bytes = consumed->consumed_threshold_bytes.value;
326 end:
327 return status;
328 }
329
330 enum lttng_condition_status
331 lttng_condition_session_consumed_size_set_threshold(
332 struct lttng_condition *condition, uint64_t consumed_threshold_bytes)
333 {
334 struct lttng_condition_session_consumed_size *consumed;
335 enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK;
336
337 if (!condition || !IS_CONSUMED_SIZE_CONDITION(condition)) {
338 status = LTTNG_CONDITION_STATUS_INVALID;
339 goto end;
340 }
341
342 consumed = container_of(condition, struct lttng_condition_session_consumed_size,
343 parent);
344 consumed->consumed_threshold_bytes.set = true;
345 consumed->consumed_threshold_bytes.value = consumed_threshold_bytes;
346 end:
347 return status;
348 }
349
350 enum lttng_condition_status
351 lttng_condition_session_consumed_size_get_session_name(
352 const struct lttng_condition *condition,
353 const char **session_name)
354 {
355 struct lttng_condition_session_consumed_size *consumed;
356 enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK;
357
358 if (!condition || !IS_CONSUMED_SIZE_CONDITION(condition) || !session_name) {
359 status = LTTNG_CONDITION_STATUS_INVALID;
360 goto end;
361 }
362
363 consumed = container_of(condition, struct lttng_condition_session_consumed_size,
364 parent);
365 if (!consumed->session_name) {
366 status = LTTNG_CONDITION_STATUS_UNSET;
367 goto end;
368 }
369 *session_name = consumed->session_name;
370 end:
371 return status;
372 }
373
374 enum lttng_condition_status
375 lttng_condition_session_consumed_size_set_session_name(
376 struct lttng_condition *condition, const char *session_name)
377 {
378 char *session_name_copy;
379 struct lttng_condition_session_consumed_size *consumed;
380 enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK;
381
382 if (!condition || !IS_CONSUMED_SIZE_CONDITION(condition) ||
383 !session_name || strlen(session_name) == 0) {
384 status = LTTNG_CONDITION_STATUS_INVALID;
385 goto end;
386 }
387
388 consumed = container_of(condition, struct lttng_condition_session_consumed_size,
389 parent);
390 session_name_copy = strdup(session_name);
391 if (!session_name_copy) {
392 status = LTTNG_CONDITION_STATUS_ERROR;
393 goto end;
394 }
395
396 if (consumed->session_name) {
397 free(consumed->session_name);
398 }
399 consumed->session_name = session_name_copy;
400 end:
401 return status;
402 }
403
404 static
405 int lttng_evaluation_session_consumed_size_serialize(
406 struct lttng_evaluation *evaluation,
407 struct lttng_dynamic_buffer *buf)
408 {
409 struct lttng_evaluation_session_consumed_size *consumed;
410 struct lttng_evaluation_session_consumed_size_comm comm;
411
412 consumed = container_of(evaluation, struct lttng_evaluation_session_consumed_size,
413 parent);
414 comm.session_consumed = consumed->session_consumed;
415 return lttng_dynamic_buffer_append(buf, &comm, sizeof(comm));
416 }
417
418 static
419 void lttng_evaluation_session_consumed_size_destroy(
420 struct lttng_evaluation *evaluation)
421 {
422 struct lttng_evaluation_session_consumed_size *consumed;
423
424 consumed = container_of(evaluation, struct lttng_evaluation_session_consumed_size,
425 parent);
426 free(consumed);
427 }
428
429 LTTNG_HIDDEN
430 struct lttng_evaluation *lttng_evaluation_session_consumed_size_create(
431 enum lttng_condition_type type, uint64_t consumed)
432 {
433 struct lttng_evaluation_session_consumed_size *consumed_eval;
434
435 consumed_eval = zmalloc(sizeof(struct lttng_evaluation_session_consumed_size));
436 if (!consumed_eval) {
437 goto end;
438 }
439
440 consumed_eval->parent.type = type;
441 consumed_eval->session_consumed = consumed;
442 consumed_eval->parent.serialize = lttng_evaluation_session_consumed_size_serialize;
443 consumed_eval->parent.destroy = lttng_evaluation_session_consumed_size_destroy;
444 end:
445 return &consumed_eval->parent;
446 }
447
448 enum lttng_evaluation_status
449 lttng_evaluation_session_consumed_size_get_consumed_size(
450 const struct lttng_evaluation *evaluation,
451 uint64_t *session_consumed)
452 {
453 struct lttng_evaluation_session_consumed_size *consumed;
454 enum lttng_evaluation_status status = LTTNG_EVALUATION_STATUS_OK;
455
456 if (!evaluation || !IS_CONSUMED_SIZE_EVALUATION(evaluation) ||
457 !session_consumed) {
458 status = LTTNG_EVALUATION_STATUS_INVALID;
459 goto end;
460 }
461
462 consumed = container_of(evaluation, struct lttng_evaluation_session_consumed_size,
463 parent);
464 *session_consumed = consumed->session_consumed;
465 end:
466 return status;
467 }
This page took 0.061311 seconds and 5 git commands to generate.