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