Remove unneeded forward declaration in condition headers
[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 ssize_t lttng_condition_session_consumed_size_serialize(
77 const struct lttng_condition *condition, char *buf)
78 {
79 struct lttng_condition_session_consumed_size *consumed;
80 ssize_t ret, size;
81 size_t session_name_len;
82
83 if (!condition || !IS_CONSUMED_SIZE_CONDITION(condition)) {
84 ret = -1;
85 goto end;
86 }
87
88 DBG("Serializing session consumed condition");
89 consumed = container_of(condition, struct lttng_condition_session_consumed_size,
90 parent);
91 size = sizeof(struct lttng_condition_session_consumed_size_comm);
92 session_name_len = strlen(consumed->session_name) + 1;
93 if (session_name_len > LTTNG_NAME_MAX) {
94 ret = -1;
95 goto end;
96 }
97 size += session_name_len;
98 if (buf) {
99 struct lttng_condition_session_consumed_size_comm consumed_comm = {
100 .consumed_threshold_bytes = consumed->consumed_threshold_bytes.value,
101 .session_name_len = session_name_len,
102 };
103
104 memcpy(buf, &consumed_comm, sizeof(consumed_comm));
105 buf += sizeof(consumed_comm);
106 memcpy(buf, consumed->session_name, session_name_len);
107 buf += session_name_len;
108 }
109 ret = size;
110 end:
111 return ret;
112 }
113
114 static
115 bool lttng_condition_session_consumed_size_is_equal(const struct lttng_condition *_a,
116 const struct lttng_condition *_b)
117 {
118 bool is_equal = false;
119 struct lttng_condition_session_consumed_size *a, *b;
120
121 a = container_of(_a, struct lttng_condition_session_consumed_size, parent);
122 b = container_of(_b, struct lttng_condition_session_consumed_size, parent);
123
124 if (a->consumed_threshold_bytes.set && b->consumed_threshold_bytes.set) {
125 uint64_t a_value, b_value;
126
127 a_value = a->consumed_threshold_bytes.value;
128 b_value = b->consumed_threshold_bytes.value;
129 if (a_value != b_value) {
130 goto end;
131 }
132 }
133
134 if ((a->session_name && !b->session_name) ||
135 (!a->session_name && b->session_name)) {
136 goto end;
137 }
138
139 is_equal = true;
140 end:
141 return is_equal;
142 }
143
144 struct lttng_condition *lttng_condition_session_consumed_size_create(void)
145 {
146 struct lttng_condition_session_consumed_size *condition;
147
148 condition = zmalloc(sizeof(struct lttng_condition_session_consumed_size));
149 if (!condition) {
150 return NULL;
151 }
152
153 lttng_condition_init(&condition->parent, LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE);
154 condition->parent.validate = lttng_condition_session_consumed_size_validate;
155 condition->parent.serialize = lttng_condition_session_consumed_size_serialize;
156 condition->parent.equal = lttng_condition_session_consumed_size_is_equal;
157 condition->parent.destroy = lttng_condition_session_consumed_size_destroy;
158 return &condition->parent;
159 }
160
161 static
162 ssize_t init_condition_from_buffer(struct lttng_condition *condition,
163 const struct lttng_buffer_view *src_view)
164 {
165 ssize_t ret, condition_size;
166 enum lttng_condition_status status;
167 const struct lttng_condition_session_consumed_size_comm *condition_comm;
168 const char *session_name;
169 struct lttng_buffer_view names_view;
170
171 if (src_view->size < sizeof(*condition_comm)) {
172 ERR("Failed to initialize from malformed condition buffer: buffer too short to contain header");
173 ret = -1;
174 goto end;
175 }
176
177 condition_comm = (const struct lttng_condition_session_consumed_size_comm *) src_view->data;
178 names_view = lttng_buffer_view_from_view(src_view,
179 sizeof(*condition_comm), -1);
180
181 if (condition_comm->session_name_len > LTTNG_NAME_MAX) {
182 ERR("Failed to initialize from malformed condition buffer: name exceeds LTTNG_MAX_NAME");
183 ret = -1;
184 goto end;
185 }
186
187 if (names_view.size < condition_comm->session_name_len) {
188 ERR("Failed to initialize from malformed condition buffer: buffer too short to contain element names");
189 ret = -1;
190 goto end;
191 }
192
193 status = lttng_condition_session_consumed_size_set_threshold(condition,
194 condition_comm->consumed_threshold_bytes);
195 if (status != LTTNG_CONDITION_STATUS_OK) {
196 ERR("Failed to initialize session consumed condition threshold");
197 ret = -1;
198 goto end;
199 }
200
201 session_name = names_view.data;
202 if (*(session_name + condition_comm->session_name_len - 1) != '\0') {
203 ERR("Malformed session name encountered in condition buffer");
204 ret = -1;
205 goto end;
206 }
207
208 status = lttng_condition_session_consumed_size_set_session_name(condition,
209 session_name);
210 if (status != LTTNG_CONDITION_STATUS_OK) {
211 ERR("Failed to set buffer consumed session name");
212 ret = -1;
213 goto end;
214 }
215
216 if (!lttng_condition_validate(condition)) {
217 ret = -1;
218 goto end;
219 }
220
221 condition_size = sizeof(*condition_comm) +
222 (ssize_t) condition_comm->session_name_len;
223 ret = condition_size;
224 end:
225 return ret;
226 }
227
228 LTTNG_HIDDEN
229 ssize_t lttng_condition_session_consumed_size_create_from_buffer(
230 const struct lttng_buffer_view *view,
231 struct lttng_condition **_condition)
232 {
233 ssize_t ret;
234 struct lttng_condition *condition =
235 lttng_condition_session_consumed_size_create();
236
237 if (!_condition || !condition) {
238 ret = -1;
239 goto error;
240 }
241
242 ret = init_condition_from_buffer(condition, view);
243 if (ret < 0) {
244 goto error;
245 }
246
247 *_condition = condition;
248 return ret;
249 error:
250 lttng_condition_destroy(condition);
251 return ret;
252 }
253
254 static
255 struct lttng_evaluation *create_evaluation_from_buffer(
256 enum lttng_condition_type type,
257 const struct lttng_buffer_view *view)
258 {
259 const struct lttng_evaluation_session_consumed_size_comm *comm =
260 (const struct lttng_evaluation_session_consumed_size_comm *) view->data;
261 struct lttng_evaluation *evaluation = NULL;
262
263 if (view->size < sizeof(*comm)) {
264 goto end;
265 }
266
267 evaluation = lttng_evaluation_session_consumed_size_create(type,
268 comm->session_consumed);
269 end:
270 return evaluation;
271 }
272
273 LTTNG_HIDDEN
274 ssize_t lttng_evaluation_session_consumed_size_create_from_buffer(
275 const struct lttng_buffer_view *view,
276 struct lttng_evaluation **_evaluation)
277 {
278 ssize_t ret;
279 struct lttng_evaluation *evaluation = NULL;
280
281 if (!_evaluation) {
282 ret = -1;
283 goto error;
284 }
285
286 evaluation = create_evaluation_from_buffer(
287 LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE, view);
288 if (!evaluation) {
289 ret = -1;
290 goto error;
291 }
292
293 *_evaluation = evaluation;
294 ret = sizeof(struct lttng_evaluation_session_consumed_size_comm);
295 return ret;
296 error:
297 lttng_evaluation_destroy(evaluation);
298 return ret;
299 }
300
301 enum lttng_condition_status
302 lttng_condition_session_consumed_size_get_threshold(
303 const struct lttng_condition *condition,
304 uint64_t *consumed_threshold_bytes)
305 {
306 struct lttng_condition_session_consumed_size *consumed;
307 enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK;
308
309 if (!condition || !IS_CONSUMED_SIZE_CONDITION(condition) || !consumed_threshold_bytes) {
310 status = LTTNG_CONDITION_STATUS_INVALID;
311 goto end;
312 }
313
314 consumed = container_of(condition, struct lttng_condition_session_consumed_size,
315 parent);
316 if (!consumed->consumed_threshold_bytes.set) {
317 status = LTTNG_CONDITION_STATUS_UNSET;
318 goto end;
319 }
320 *consumed_threshold_bytes = consumed->consumed_threshold_bytes.value;
321 end:
322 return status;
323 }
324
325 enum lttng_condition_status
326 lttng_condition_session_consumed_size_set_threshold(
327 struct lttng_condition *condition, uint64_t consumed_threshold_bytes)
328 {
329 struct lttng_condition_session_consumed_size *consumed;
330 enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK;
331
332 if (!condition || !IS_CONSUMED_SIZE_CONDITION(condition)) {
333 status = LTTNG_CONDITION_STATUS_INVALID;
334 goto end;
335 }
336
337 consumed = container_of(condition, struct lttng_condition_session_consumed_size,
338 parent);
339 consumed->consumed_threshold_bytes.set = true;
340 consumed->consumed_threshold_bytes.value = consumed_threshold_bytes;
341 end:
342 return status;
343 }
344
345 enum lttng_condition_status
346 lttng_condition_session_consumed_size_get_session_name(
347 const struct lttng_condition *condition,
348 const char **session_name)
349 {
350 struct lttng_condition_session_consumed_size *consumed;
351 enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK;
352
353 if (!condition || !IS_CONSUMED_SIZE_CONDITION(condition) || !session_name) {
354 status = LTTNG_CONDITION_STATUS_INVALID;
355 goto end;
356 }
357
358 consumed = container_of(condition, struct lttng_condition_session_consumed_size,
359 parent);
360 if (!consumed->session_name) {
361 status = LTTNG_CONDITION_STATUS_UNSET;
362 goto end;
363 }
364 *session_name = consumed->session_name;
365 end:
366 return status;
367 }
368
369 enum lttng_condition_status
370 lttng_condition_session_consumed_size_set_session_name(
371 struct lttng_condition *condition, const char *session_name)
372 {
373 char *session_name_copy;
374 struct lttng_condition_session_consumed_size *consumed;
375 enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK;
376
377 if (!condition || !IS_CONSUMED_SIZE_CONDITION(condition) ||
378 !session_name || strlen(session_name) == 0) {
379 status = LTTNG_CONDITION_STATUS_INVALID;
380 goto end;
381 }
382
383 consumed = container_of(condition, struct lttng_condition_session_consumed_size,
384 parent);
385 session_name_copy = strdup(session_name);
386 if (!session_name_copy) {
387 status = LTTNG_CONDITION_STATUS_ERROR;
388 goto end;
389 }
390
391 if (consumed->session_name) {
392 free(consumed->session_name);
393 }
394 consumed->session_name = session_name_copy;
395 end:
396 return status;
397 }
398
399 static
400 ssize_t lttng_evaluation_session_consumed_size_serialize(
401 struct lttng_evaluation *evaluation, char *buf)
402 {
403 ssize_t ret;
404 struct lttng_evaluation_session_consumed_size *consumed;
405
406 consumed = container_of(evaluation, struct lttng_evaluation_session_consumed_size,
407 parent);
408 if (buf) {
409 struct lttng_evaluation_session_consumed_size_comm comm = {
410 .session_consumed = consumed->session_consumed,
411 };
412
413 memcpy(buf, &comm, sizeof(comm));
414 }
415
416 ret = sizeof(struct lttng_evaluation_session_consumed_size_comm);
417 return ret;
418 }
419
420 static
421 void lttng_evaluation_session_consumed_size_destroy(
422 struct lttng_evaluation *evaluation)
423 {
424 struct lttng_evaluation_session_consumed_size *consumed;
425
426 consumed = container_of(evaluation, struct lttng_evaluation_session_consumed_size,
427 parent);
428 free(consumed);
429 }
430
431 LTTNG_HIDDEN
432 struct lttng_evaluation *lttng_evaluation_session_consumed_size_create(
433 enum lttng_condition_type type, uint64_t consumed)
434 {
435 struct lttng_evaluation_session_consumed_size *consumed_eval;
436
437 consumed_eval = zmalloc(sizeof(struct lttng_evaluation_session_consumed_size));
438 if (!consumed_eval) {
439 goto end;
440 }
441
442 consumed_eval->parent.type = type;
443 consumed_eval->session_consumed = consumed;
444 consumed_eval->parent.serialize = lttng_evaluation_session_consumed_size_serialize;
445 consumed_eval->parent.destroy = lttng_evaluation_session_consumed_size_destroy;
446 end:
447 return &consumed_eval->parent;
448 }
449
450 enum lttng_evaluation_status
451 lttng_evaluation_session_consumed_size_get_consumed_size(
452 const struct lttng_evaluation *evaluation,
453 uint64_t *session_consumed)
454 {
455 struct lttng_evaluation_session_consumed_size *consumed;
456 enum lttng_evaluation_status status = LTTNG_EVALUATION_STATUS_OK;
457
458 if (!evaluation || !IS_CONSUMED_SIZE_EVALUATION(evaluation) ||
459 !session_consumed) {
460 status = LTTNG_EVALUATION_STATUS_INVALID;
461 goto end;
462 }
463
464 consumed = container_of(evaluation, struct lttng_evaluation_session_consumed_size,
465 parent);
466 *session_consumed = consumed->session_consumed;
467 end:
468 return status;
469 }
This page took 0.062413 seconds and 6 git commands to generate.