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 | ||
8 | #include <lttng/condition/condition-internal.h> | |
9 | #include <lttng/condition/buffer-usage-internal.h> | |
10 | #include <common/macros.h> | |
11 | #include <common/error.h> | |
12 | #include <assert.h> | |
13 | #include <math.h> | |
14 | #include <float.h> | |
15 | #include <time.h> | |
16 | ||
17 | #define IS_USAGE_CONDITION(condition) ( \ | |
18 | lttng_condition_get_type(condition) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW || \ | |
19 | lttng_condition_get_type(condition) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH \ | |
20 | ) | |
21 | ||
22 | static | |
23 | double fixed_to_double(uint32_t val) | |
24 | { | |
25 | return (double) val / (double) UINT32_MAX; | |
26 | } | |
27 | ||
28 | static | |
29 | uint64_t double_to_fixed(double val) | |
30 | { | |
31 | return (val * (double) UINT32_MAX); | |
32 | } | |
33 | ||
34 | static | |
35 | bool is_usage_evaluation(const struct lttng_evaluation *evaluation) | |
36 | { | |
37 | enum lttng_condition_type type = lttng_evaluation_get_type(evaluation); | |
38 | ||
39 | return type == LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW || | |
40 | type == LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH; | |
41 | } | |
42 | ||
43 | static | |
44 | void lttng_condition_buffer_usage_destroy(struct lttng_condition *condition) | |
45 | { | |
46 | struct lttng_condition_buffer_usage *usage; | |
47 | ||
48 | usage = container_of(condition, struct lttng_condition_buffer_usage, | |
49 | parent); | |
50 | ||
51 | free(usage->session_name); | |
52 | free(usage->channel_name); | |
53 | free(usage); | |
54 | } | |
55 | ||
56 | static | |
57 | bool lttng_condition_buffer_usage_validate( | |
58 | const struct lttng_condition *condition) | |
59 | { | |
60 | bool valid = false; | |
61 | struct lttng_condition_buffer_usage *usage; | |
62 | ||
63 | if (!condition) { | |
64 | goto end; | |
65 | } | |
66 | ||
67 | usage = container_of(condition, struct lttng_condition_buffer_usage, | |
68 | parent); | |
69 | if (!usage->session_name) { | |
70 | ERR("Invalid buffer condition: a target session name must be set."); | |
71 | goto end; | |
72 | } | |
73 | if (!usage->channel_name) { | |
74 | ERR("Invalid buffer condition: a target channel name must be set."); | |
75 | goto end; | |
76 | } | |
77 | if (!usage->threshold_ratio.set && !usage->threshold_bytes.set) { | |
78 | ERR("Invalid buffer condition: a threshold must be set."); | |
79 | goto end; | |
80 | } | |
821d5e92 JG |
81 | if (!usage->domain.set) { |
82 | ERR("Invalid buffer usage condition: a domain must be set."); | |
83 | goto end; | |
84 | } | |
a58c490f JG |
85 | |
86 | valid = true; | |
87 | end: | |
88 | return valid; | |
89 | } | |
90 | ||
91 | static | |
3647288f JG |
92 | int lttng_condition_buffer_usage_serialize( |
93 | const struct lttng_condition *condition, | |
5024c2ac JR |
94 | struct lttng_dynamic_buffer *buf, |
95 | int *fd_to_send) | |
a58c490f | 96 | { |
3647288f | 97 | int ret; |
a58c490f | 98 | struct lttng_condition_buffer_usage *usage; |
a58c490f | 99 | size_t session_name_len, channel_name_len; |
3647288f | 100 | struct lttng_condition_buffer_usage_comm usage_comm; |
a58c490f JG |
101 | |
102 | if (!condition || !IS_USAGE_CONDITION(condition)) { | |
103 | ret = -1; | |
104 | goto end; | |
105 | } | |
106 | ||
107 | DBG("Serializing buffer usage condition"); | |
108 | usage = container_of(condition, struct lttng_condition_buffer_usage, | |
109 | parent); | |
3647288f | 110 | |
a58c490f JG |
111 | session_name_len = strlen(usage->session_name) + 1; |
112 | channel_name_len = strlen(usage->channel_name) + 1; | |
113 | if (session_name_len > LTTNG_NAME_MAX || | |
114 | channel_name_len > LTTNG_NAME_MAX) { | |
115 | ret = -1; | |
116 | goto end; | |
117 | } | |
3647288f JG |
118 | |
119 | usage_comm.threshold_set_in_bytes = !!usage->threshold_bytes.set; | |
120 | usage_comm.session_name_len = session_name_len; | |
121 | usage_comm.channel_name_len = channel_name_len; | |
122 | usage_comm.domain_type = (int8_t) usage->domain.type; | |
123 | ||
124 | if (usage->threshold_bytes.set) { | |
125 | usage_comm.threshold = usage->threshold_bytes.value; | |
126 | } else { | |
127 | uint64_t val = double_to_fixed( | |
128 | usage->threshold_ratio.value); | |
129 | ||
130 | if (val > UINT32_MAX) { | |
131 | /* overflow. */ | |
132 | ret = -1; | |
133 | goto end; | |
a58c490f | 134 | } |
3647288f JG |
135 | usage_comm.threshold = val; |
136 | } | |
a58c490f | 137 | |
3647288f JG |
138 | ret = lttng_dynamic_buffer_append(buf, &usage_comm, |
139 | sizeof(usage_comm)); | |
140 | if (ret) { | |
141 | goto end; | |
142 | } | |
143 | ret = lttng_dynamic_buffer_append(buf, usage->session_name, | |
144 | session_name_len); | |
145 | if (ret) { | |
146 | goto end; | |
147 | } | |
148 | ret = lttng_dynamic_buffer_append(buf, usage->channel_name, | |
149 | channel_name_len); | |
150 | if (ret) { | |
151 | goto end; | |
a58c490f | 152 | } |
5024c2ac JR |
153 | |
154 | if (fd_to_send) { | |
155 | /* No fd to send */ | |
156 | *fd_to_send = -1; | |
157 | } | |
a58c490f JG |
158 | end: |
159 | return ret; | |
160 | } | |
161 | ||
162 | static | |
163 | bool lttng_condition_buffer_usage_is_equal(const struct lttng_condition *_a, | |
164 | const struct lttng_condition *_b) | |
165 | { | |
166 | bool is_equal = false; | |
167 | struct lttng_condition_buffer_usage *a, *b; | |
168 | ||
169 | a = container_of(_a, struct lttng_condition_buffer_usage, parent); | |
170 | b = container_of(_b, struct lttng_condition_buffer_usage, parent); | |
171 | ||
172 | if ((a->threshold_ratio.set && !b->threshold_ratio.set) || | |
173 | (a->threshold_bytes.set && !b->threshold_bytes.set)) { | |
174 | goto end; | |
175 | } | |
176 | ||
177 | if (a->threshold_ratio.set && b->threshold_ratio.set) { | |
178 | double a_value, b_value, diff; | |
179 | ||
180 | a_value = a->threshold_ratio.value; | |
181 | b_value = b->threshold_ratio.value; | |
182 | diff = fabs(a_value - b_value); | |
183 | ||
184 | if (diff > DBL_EPSILON) { | |
185 | goto end; | |
186 | } | |
187 | } else if (a->threshold_bytes.set && b->threshold_bytes.set) { | |
188 | uint64_t a_value, b_value; | |
189 | ||
190 | a_value = a->threshold_bytes.value; | |
191 | b_value = b->threshold_bytes.value; | |
192 | if (a_value != b_value) { | |
193 | goto end; | |
194 | } | |
195 | } | |
196 | ||
821d5e92 JG |
197 | /* Condition is not valid if this is not true. */ |
198 | assert(a->session_name); | |
199 | assert(b->session_name); | |
200 | if (strcmp(a->session_name, b->session_name)) { | |
a58c490f JG |
201 | goto end; |
202 | } | |
203 | ||
821d5e92 JG |
204 | assert(a->channel_name); |
205 | assert(b->channel_name); | |
206 | if (strcmp(a->channel_name, b->channel_name)) { | |
a58c490f JG |
207 | goto end; |
208 | } | |
209 | ||
821d5e92 JG |
210 | assert(a->domain.set); |
211 | assert(b->domain.set); | |
212 | if (a->domain.type != b->domain.type) { | |
a58c490f JG |
213 | goto end; |
214 | } | |
a58c490f JG |
215 | is_equal = true; |
216 | end: | |
217 | return is_equal; | |
218 | } | |
219 | ||
220 | static | |
221 | struct lttng_condition *lttng_condition_buffer_usage_create( | |
222 | enum lttng_condition_type type) | |
223 | { | |
224 | struct lttng_condition_buffer_usage *condition; | |
225 | ||
226 | condition = zmalloc(sizeof(struct lttng_condition_buffer_usage)); | |
227 | if (!condition) { | |
9a09eef5 | 228 | return NULL; |
a58c490f JG |
229 | } |
230 | ||
231 | lttng_condition_init(&condition->parent, type); | |
232 | condition->parent.validate = lttng_condition_buffer_usage_validate; | |
233 | condition->parent.serialize = lttng_condition_buffer_usage_serialize; | |
234 | condition->parent.equal = lttng_condition_buffer_usage_is_equal; | |
235 | condition->parent.destroy = lttng_condition_buffer_usage_destroy; | |
a58c490f JG |
236 | return &condition->parent; |
237 | } | |
238 | ||
239 | struct lttng_condition *lttng_condition_buffer_usage_low_create(void) | |
240 | { | |
241 | return lttng_condition_buffer_usage_create( | |
242 | LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW); | |
243 | } | |
244 | ||
245 | struct lttng_condition *lttng_condition_buffer_usage_high_create(void) | |
246 | { | |
247 | return lttng_condition_buffer_usage_create( | |
248 | LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH); | |
249 | } | |
250 | ||
251 | static | |
252 | ssize_t init_condition_from_buffer(struct lttng_condition *condition, | |
253 | const struct lttng_buffer_view *src_view) | |
254 | { | |
255 | ssize_t ret, condition_size; | |
256 | enum lttng_condition_status status; | |
257 | enum lttng_domain_type domain_type; | |
258 | const struct lttng_condition_buffer_usage_comm *condition_comm; | |
259 | const char *session_name, *channel_name; | |
260 | struct lttng_buffer_view names_view; | |
261 | ||
262 | if (src_view->size < sizeof(*condition_comm)) { | |
263 | ERR("Failed to initialize from malformed condition buffer: buffer too short to contain header"); | |
264 | ret = -1; | |
265 | goto end; | |
266 | } | |
267 | ||
268 | condition_comm = (const struct lttng_condition_buffer_usage_comm *) src_view->data; | |
269 | names_view = lttng_buffer_view_from_view(src_view, | |
270 | sizeof(*condition_comm), -1); | |
271 | ||
272 | if (condition_comm->session_name_len > LTTNG_NAME_MAX || | |
273 | condition_comm->channel_name_len > LTTNG_NAME_MAX) { | |
274 | ERR("Failed to initialize from malformed condition buffer: name exceeds LTTNG_MAX_NAME"); | |
275 | ret = -1; | |
276 | goto end; | |
277 | } | |
278 | ||
279 | if (names_view.size < | |
280 | (condition_comm->session_name_len + | |
281 | condition_comm->channel_name_len)) { | |
282 | ERR("Failed to initialize from malformed condition buffer: buffer too short to contain element names"); | |
283 | ret = -1; | |
284 | goto end; | |
285 | } | |
286 | ||
287 | if (condition_comm->threshold_set_in_bytes) { | |
288 | status = lttng_condition_buffer_usage_set_threshold(condition, | |
289 | condition_comm->threshold); | |
290 | } else { | |
291 | status = lttng_condition_buffer_usage_set_threshold_ratio( | |
292 | condition, | |
293 | fixed_to_double(condition_comm->threshold)); | |
294 | } | |
295 | if (status != LTTNG_CONDITION_STATUS_OK) { | |
296 | ERR("Failed to initialize buffer usage condition threshold"); | |
297 | ret = -1; | |
298 | goto end; | |
299 | } | |
300 | ||
301 | if (condition_comm->domain_type <= LTTNG_DOMAIN_NONE || | |
302 | condition_comm->domain_type > LTTNG_DOMAIN_PYTHON) { | |
303 | /* Invalid domain value. */ | |
304 | ERR("Invalid domain type value (%i) found in condition buffer", | |
305 | (int) condition_comm->domain_type); | |
306 | ret = -1; | |
307 | goto end; | |
308 | } | |
309 | ||
310 | domain_type = (enum lttng_domain_type) condition_comm->domain_type; | |
311 | status = lttng_condition_buffer_usage_set_domain_type(condition, | |
312 | domain_type); | |
313 | if (status != LTTNG_CONDITION_STATUS_OK) { | |
314 | ERR("Failed to set buffer usage condition domain"); | |
315 | ret = -1; | |
316 | goto end; | |
317 | } | |
318 | ||
319 | session_name = names_view.data; | |
320 | if (*(session_name + condition_comm->session_name_len - 1) != '\0') { | |
321 | ERR("Malformed session name encountered in condition buffer"); | |
322 | ret = -1; | |
323 | goto end; | |
324 | } | |
325 | ||
326 | channel_name = session_name + condition_comm->session_name_len; | |
327 | if (*(channel_name + condition_comm->channel_name_len - 1) != '\0') { | |
328 | ERR("Malformed channel name encountered in condition buffer"); | |
329 | ret = -1; | |
330 | goto end; | |
331 | } | |
332 | ||
333 | status = lttng_condition_buffer_usage_set_session_name(condition, | |
334 | session_name); | |
335 | if (status != LTTNG_CONDITION_STATUS_OK) { | |
336 | ERR("Failed to set buffer usage session name"); | |
337 | ret = -1; | |
338 | goto end; | |
339 | } | |
340 | ||
341 | status = lttng_condition_buffer_usage_set_channel_name(condition, | |
342 | channel_name); | |
343 | if (status != LTTNG_CONDITION_STATUS_OK) { | |
344 | ERR("Failed to set buffer usage channel name"); | |
345 | ret = -1; | |
346 | goto end; | |
347 | } | |
348 | ||
349 | if (!lttng_condition_validate(condition)) { | |
350 | ret = -1; | |
351 | goto end; | |
352 | } | |
353 | ||
354 | condition_size = sizeof(*condition_comm) + | |
355 | (ssize_t) condition_comm->session_name_len + | |
356 | (ssize_t) condition_comm->channel_name_len; | |
357 | ret = condition_size; | |
358 | end: | |
359 | return ret; | |
360 | } | |
361 | ||
362 | LTTNG_HIDDEN | |
363 | ssize_t lttng_condition_buffer_usage_low_create_from_buffer( | |
364 | const struct lttng_buffer_view *view, | |
365 | struct lttng_condition **_condition) | |
366 | { | |
367 | ssize_t ret; | |
368 | struct lttng_condition *condition = | |
369 | lttng_condition_buffer_usage_low_create(); | |
370 | ||
371 | if (!_condition || !condition) { | |
372 | ret = -1; | |
373 | goto error; | |
374 | } | |
375 | ||
376 | ret = init_condition_from_buffer(condition, view); | |
377 | if (ret < 0) { | |
378 | goto error; | |
379 | } | |
380 | ||
381 | *_condition = condition; | |
382 | return ret; | |
383 | error: | |
384 | lttng_condition_destroy(condition); | |
385 | return ret; | |
386 | } | |
387 | ||
388 | LTTNG_HIDDEN | |
389 | ssize_t lttng_condition_buffer_usage_high_create_from_buffer( | |
390 | const struct lttng_buffer_view *view, | |
391 | struct lttng_condition **_condition) | |
392 | { | |
393 | ssize_t ret; | |
394 | struct lttng_condition *condition = | |
395 | lttng_condition_buffer_usage_high_create(); | |
396 | ||
397 | if (!_condition || !condition) { | |
398 | ret = -1; | |
399 | goto error; | |
400 | } | |
401 | ||
402 | ret = init_condition_from_buffer(condition, view); | |
403 | if (ret < 0) { | |
404 | goto error; | |
405 | } | |
406 | ||
407 | *_condition = condition; | |
408 | return ret; | |
409 | error: | |
410 | lttng_condition_destroy(condition); | |
411 | return ret; | |
412 | } | |
413 | ||
414 | static | |
415 | struct lttng_evaluation *create_evaluation_from_buffer( | |
416 | enum lttng_condition_type type, | |
417 | const struct lttng_buffer_view *view) | |
418 | { | |
419 | const struct lttng_evaluation_buffer_usage_comm *comm = | |
420 | (const struct lttng_evaluation_buffer_usage_comm *) view->data; | |
421 | struct lttng_evaluation *evaluation = NULL; | |
422 | ||
423 | if (view->size < sizeof(*comm)) { | |
424 | goto end; | |
425 | } | |
426 | ||
427 | evaluation = lttng_evaluation_buffer_usage_create(type, | |
428 | comm->buffer_use, comm->buffer_capacity); | |
429 | end: | |
430 | return evaluation; | |
431 | } | |
432 | ||
433 | LTTNG_HIDDEN | |
434 | ssize_t lttng_evaluation_buffer_usage_low_create_from_buffer( | |
435 | const struct lttng_buffer_view *view, | |
436 | struct lttng_evaluation **_evaluation) | |
437 | { | |
438 | ssize_t ret; | |
439 | struct lttng_evaluation *evaluation = NULL; | |
440 | ||
441 | if (!_evaluation) { | |
442 | ret = -1; | |
443 | goto error; | |
444 | } | |
445 | ||
446 | evaluation = create_evaluation_from_buffer( | |
447 | LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW, view); | |
448 | if (!evaluation) { | |
449 | ret = -1; | |
450 | goto error; | |
451 | } | |
452 | ||
453 | *_evaluation = evaluation; | |
454 | ret = sizeof(struct lttng_evaluation_buffer_usage_comm); | |
455 | return ret; | |
456 | error: | |
457 | lttng_evaluation_destroy(evaluation); | |
458 | return ret; | |
459 | } | |
460 | ||
461 | LTTNG_HIDDEN | |
462 | ssize_t lttng_evaluation_buffer_usage_high_create_from_buffer( | |
463 | const struct lttng_buffer_view *view, | |
464 | struct lttng_evaluation **_evaluation) | |
465 | { | |
466 | ssize_t ret; | |
467 | struct lttng_evaluation *evaluation = NULL; | |
468 | ||
469 | if (!_evaluation) { | |
470 | ret = -1; | |
471 | goto error; | |
472 | } | |
473 | ||
474 | evaluation = create_evaluation_from_buffer( | |
475 | LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH, view); | |
476 | if (!evaluation) { | |
477 | ret = -1; | |
478 | goto error; | |
479 | } | |
480 | ||
481 | *_evaluation = evaluation; | |
482 | ret = sizeof(struct lttng_evaluation_buffer_usage_comm); | |
483 | return ret; | |
484 | error: | |
485 | lttng_evaluation_destroy(evaluation); | |
486 | return ret; | |
487 | } | |
488 | ||
489 | enum lttng_condition_status | |
490 | lttng_condition_buffer_usage_get_threshold_ratio( | |
491 | const struct lttng_condition *condition, | |
492 | double *threshold_ratio) | |
493 | { | |
494 | struct lttng_condition_buffer_usage *usage; | |
495 | enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK; | |
496 | ||
497 | if (!condition || !IS_USAGE_CONDITION(condition) || | |
498 | !threshold_ratio) { | |
499 | status = LTTNG_CONDITION_STATUS_INVALID; | |
500 | goto end; | |
501 | } | |
502 | ||
503 | usage = container_of(condition, struct lttng_condition_buffer_usage, | |
504 | parent); | |
505 | if (!usage->threshold_ratio.set) { | |
506 | status = LTTNG_CONDITION_STATUS_UNSET; | |
507 | goto end; | |
508 | } | |
509 | *threshold_ratio = usage->threshold_ratio.value; | |
510 | end: | |
511 | return status; | |
512 | } | |
513 | ||
514 | /* threshold_ratio expressed as [0.0, 1.0]. */ | |
515 | enum lttng_condition_status | |
516 | lttng_condition_buffer_usage_set_threshold_ratio( | |
517 | struct lttng_condition *condition, double threshold_ratio) | |
518 | { | |
519 | struct lttng_condition_buffer_usage *usage; | |
520 | enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK; | |
521 | ||
522 | if (!condition || !IS_USAGE_CONDITION(condition) || | |
523 | threshold_ratio < 0.0 || | |
524 | threshold_ratio > 1.0) { | |
525 | status = LTTNG_CONDITION_STATUS_INVALID; | |
526 | goto end; | |
527 | } | |
528 | ||
529 | usage = container_of(condition, struct lttng_condition_buffer_usage, | |
530 | parent); | |
531 | usage->threshold_ratio.set = true; | |
532 | usage->threshold_bytes.set = false; | |
533 | usage->threshold_ratio.value = threshold_ratio; | |
534 | end: | |
535 | return status; | |
536 | } | |
537 | ||
538 | enum lttng_condition_status | |
539 | lttng_condition_buffer_usage_get_threshold( | |
540 | const struct lttng_condition *condition, | |
541 | uint64_t *threshold_bytes) | |
542 | { | |
543 | struct lttng_condition_buffer_usage *usage; | |
544 | enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK; | |
545 | ||
546 | if (!condition || !IS_USAGE_CONDITION(condition) || !threshold_bytes) { | |
547 | status = LTTNG_CONDITION_STATUS_INVALID; | |
548 | goto end; | |
549 | } | |
550 | ||
551 | usage = container_of(condition, struct lttng_condition_buffer_usage, | |
552 | parent); | |
553 | if (!usage->threshold_bytes.set) { | |
554 | status = LTTNG_CONDITION_STATUS_UNSET; | |
555 | goto end; | |
556 | } | |
557 | *threshold_bytes = usage->threshold_bytes.value; | |
558 | end: | |
559 | return status; | |
560 | } | |
561 | ||
562 | enum lttng_condition_status | |
563 | lttng_condition_buffer_usage_set_threshold( | |
564 | struct lttng_condition *condition, uint64_t threshold_bytes) | |
565 | { | |
566 | struct lttng_condition_buffer_usage *usage; | |
567 | enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK; | |
568 | ||
569 | if (!condition || !IS_USAGE_CONDITION(condition)) { | |
570 | status = LTTNG_CONDITION_STATUS_INVALID; | |
571 | goto end; | |
572 | } | |
573 | ||
574 | usage = container_of(condition, struct lttng_condition_buffer_usage, | |
575 | parent); | |
576 | usage->threshold_ratio.set = false; | |
577 | usage->threshold_bytes.set = true; | |
578 | usage->threshold_bytes.value = threshold_bytes; | |
579 | end: | |
580 | return status; | |
581 | } | |
582 | ||
583 | enum lttng_condition_status | |
584 | lttng_condition_buffer_usage_get_session_name( | |
585 | const struct lttng_condition *condition, | |
586 | const char **session_name) | |
587 | { | |
588 | struct lttng_condition_buffer_usage *usage; | |
589 | enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK; | |
590 | ||
591 | if (!condition || !IS_USAGE_CONDITION(condition) || !session_name) { | |
592 | status = LTTNG_CONDITION_STATUS_INVALID; | |
593 | goto end; | |
594 | } | |
595 | ||
596 | usage = container_of(condition, struct lttng_condition_buffer_usage, | |
597 | parent); | |
598 | if (!usage->session_name) { | |
599 | status = LTTNG_CONDITION_STATUS_UNSET; | |
600 | goto end; | |
601 | } | |
602 | *session_name = usage->session_name; | |
603 | end: | |
604 | return status; | |
605 | } | |
606 | ||
607 | enum lttng_condition_status | |
608 | lttng_condition_buffer_usage_set_session_name( | |
609 | struct lttng_condition *condition, const char *session_name) | |
610 | { | |
611 | char *session_name_copy; | |
612 | struct lttng_condition_buffer_usage *usage; | |
613 | enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK; | |
614 | ||
615 | if (!condition || !IS_USAGE_CONDITION(condition) || !session_name || | |
616 | strlen(session_name) == 0) { | |
617 | status = LTTNG_CONDITION_STATUS_INVALID; | |
618 | goto end; | |
619 | } | |
620 | ||
621 | usage = container_of(condition, struct lttng_condition_buffer_usage, | |
622 | parent); | |
623 | session_name_copy = strdup(session_name); | |
624 | if (!session_name_copy) { | |
625 | status = LTTNG_CONDITION_STATUS_ERROR; | |
626 | goto end; | |
627 | } | |
628 | ||
629 | if (usage->session_name) { | |
630 | free(usage->session_name); | |
631 | } | |
632 | usage->session_name = session_name_copy; | |
633 | end: | |
634 | return status; | |
635 | } | |
636 | ||
637 | enum lttng_condition_status | |
638 | lttng_condition_buffer_usage_get_channel_name( | |
639 | const struct lttng_condition *condition, | |
640 | const char **channel_name) | |
641 | { | |
642 | struct lttng_condition_buffer_usage *usage; | |
643 | enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK; | |
644 | ||
645 | if (!condition || !IS_USAGE_CONDITION(condition) || !channel_name) { | |
646 | status = LTTNG_CONDITION_STATUS_INVALID; | |
647 | goto end; | |
648 | } | |
649 | ||
650 | usage = container_of(condition, struct lttng_condition_buffer_usage, | |
651 | parent); | |
652 | if (!usage->channel_name) { | |
653 | status = LTTNG_CONDITION_STATUS_UNSET; | |
654 | goto end; | |
655 | } | |
656 | *channel_name = usage->channel_name; | |
657 | end: | |
658 | return status; | |
659 | } | |
660 | ||
661 | enum lttng_condition_status | |
662 | lttng_condition_buffer_usage_set_channel_name( | |
663 | struct lttng_condition *condition, const char *channel_name) | |
664 | { | |
665 | char *channel_name_copy; | |
666 | struct lttng_condition_buffer_usage *usage; | |
667 | enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK; | |
668 | ||
669 | if (!condition || !IS_USAGE_CONDITION(condition) || !channel_name || | |
670 | strlen(channel_name) == 0) { | |
671 | status = LTTNG_CONDITION_STATUS_INVALID; | |
672 | goto end; | |
673 | } | |
674 | ||
675 | usage = container_of(condition, struct lttng_condition_buffer_usage, | |
676 | parent); | |
677 | channel_name_copy = strdup(channel_name); | |
678 | if (!channel_name_copy) { | |
679 | status = LTTNG_CONDITION_STATUS_ERROR; | |
680 | goto end; | |
681 | } | |
682 | ||
683 | if (usage->channel_name) { | |
684 | free(usage->channel_name); | |
685 | } | |
686 | usage->channel_name = channel_name_copy; | |
687 | end: | |
688 | return status; | |
689 | } | |
690 | ||
691 | enum lttng_condition_status | |
692 | lttng_condition_buffer_usage_get_domain_type( | |
693 | const struct lttng_condition *condition, | |
694 | enum lttng_domain_type *type) | |
695 | { | |
696 | struct lttng_condition_buffer_usage *usage; | |
697 | enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK; | |
698 | ||
699 | if (!condition || !IS_USAGE_CONDITION(condition) || !type) { | |
700 | status = LTTNG_CONDITION_STATUS_INVALID; | |
701 | goto end; | |
702 | } | |
703 | ||
704 | usage = container_of(condition, struct lttng_condition_buffer_usage, | |
705 | parent); | |
706 | if (!usage->domain.set) { | |
707 | status = LTTNG_CONDITION_STATUS_UNSET; | |
708 | goto end; | |
709 | } | |
710 | *type = usage->domain.type; | |
711 | end: | |
712 | return status; | |
713 | } | |
714 | ||
715 | enum lttng_condition_status | |
716 | lttng_condition_buffer_usage_set_domain_type( | |
717 | struct lttng_condition *condition, enum lttng_domain_type type) | |
718 | { | |
719 | struct lttng_condition_buffer_usage *usage; | |
720 | enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK; | |
721 | ||
722 | if (!condition || !IS_USAGE_CONDITION(condition) || | |
723 | type == LTTNG_DOMAIN_NONE) { | |
724 | status = LTTNG_CONDITION_STATUS_INVALID; | |
725 | goto end; | |
726 | } | |
727 | ||
728 | usage = container_of(condition, struct lttng_condition_buffer_usage, | |
729 | parent); | |
730 | usage->domain.set = true; | |
731 | usage->domain.type = type; | |
732 | end: | |
733 | return status; | |
734 | } | |
735 | ||
736 | static | |
3647288f | 737 | int lttng_evaluation_buffer_usage_serialize( |
9b63a4aa | 738 | const struct lttng_evaluation *evaluation, |
3647288f | 739 | struct lttng_dynamic_buffer *buf) |
a58c490f | 740 | { |
a58c490f | 741 | struct lttng_evaluation_buffer_usage *usage; |
3647288f | 742 | struct lttng_evaluation_buffer_usage_comm comm; |
a58c490f JG |
743 | |
744 | usage = container_of(evaluation, struct lttng_evaluation_buffer_usage, | |
745 | parent); | |
3647288f JG |
746 | comm.buffer_use = usage->buffer_use; |
747 | comm.buffer_capacity = usage->buffer_capacity; | |
a58c490f | 748 | |
3647288f | 749 | return lttng_dynamic_buffer_append(buf, &comm, sizeof(comm)); |
a58c490f JG |
750 | } |
751 | ||
752 | static | |
753 | void lttng_evaluation_buffer_usage_destroy( | |
754 | struct lttng_evaluation *evaluation) | |
755 | { | |
756 | struct lttng_evaluation_buffer_usage *usage; | |
757 | ||
758 | usage = container_of(evaluation, struct lttng_evaluation_buffer_usage, | |
759 | parent); | |
760 | free(usage); | |
761 | } | |
762 | ||
763 | LTTNG_HIDDEN | |
764 | struct lttng_evaluation *lttng_evaluation_buffer_usage_create( | |
765 | enum lttng_condition_type type, uint64_t use, uint64_t capacity) | |
766 | { | |
767 | struct lttng_evaluation_buffer_usage *usage; | |
768 | ||
769 | usage = zmalloc(sizeof(struct lttng_evaluation_buffer_usage)); | |
770 | if (!usage) { | |
771 | goto end; | |
772 | } | |
773 | ||
774 | usage->parent.type = type; | |
775 | usage->buffer_use = use; | |
776 | usage->buffer_capacity = capacity; | |
777 | usage->parent.serialize = lttng_evaluation_buffer_usage_serialize; | |
778 | usage->parent.destroy = lttng_evaluation_buffer_usage_destroy; | |
779 | end: | |
780 | return &usage->parent; | |
781 | } | |
782 | ||
783 | /* | |
784 | * Get the sampled buffer usage which caused the associated condition to | |
785 | * evaluate to "true". | |
786 | */ | |
787 | enum lttng_evaluation_status | |
788 | lttng_evaluation_buffer_usage_get_usage_ratio( | |
789 | const struct lttng_evaluation *evaluation, double *usage_ratio) | |
790 | { | |
791 | struct lttng_evaluation_buffer_usage *usage; | |
792 | enum lttng_evaluation_status status = LTTNG_EVALUATION_STATUS_OK; | |
793 | ||
794 | if (!evaluation || !is_usage_evaluation(evaluation) || !usage_ratio) { | |
795 | status = LTTNG_EVALUATION_STATUS_INVALID; | |
796 | goto end; | |
797 | } | |
798 | ||
799 | usage = container_of(evaluation, struct lttng_evaluation_buffer_usage, | |
800 | parent); | |
801 | *usage_ratio = (double) usage->buffer_use / | |
802 | (double) usage->buffer_capacity; | |
803 | end: | |
804 | return status; | |
805 | } | |
806 | ||
807 | enum lttng_evaluation_status | |
808 | lttng_evaluation_buffer_usage_get_usage( | |
809 | const struct lttng_evaluation *evaluation, | |
810 | uint64_t *usage_bytes) | |
811 | { | |
812 | struct lttng_evaluation_buffer_usage *usage; | |
813 | enum lttng_evaluation_status status = LTTNG_EVALUATION_STATUS_OK; | |
814 | ||
815 | if (!evaluation || !is_usage_evaluation(evaluation) || !usage_bytes) { | |
816 | status = LTTNG_EVALUATION_STATUS_INVALID; | |
817 | goto end; | |
818 | } | |
819 | ||
820 | usage = container_of(evaluation, struct lttng_evaluation_buffer_usage, | |
821 | parent); | |
822 | *usage_bytes = usage->buffer_use; | |
823 | end: | |
824 | return status; | |
825 | } |