| 1 | /* |
| 2 | * Copyright (C) 2021 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: LGPL-2.1-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <common/buffer-view.hpp> |
| 9 | #include <common/dynamic-buffer.hpp> |
| 10 | #include <common/error.hpp> |
| 11 | #include <common/macros.hpp> |
| 12 | #include <common/mi-lttng.hpp> |
| 13 | #include <common/payload-view.hpp> |
| 14 | #include <common/payload.hpp> |
| 15 | #include <limits.h> |
| 16 | #include <lttng/action/rate-policy-internal.hpp> |
| 17 | #include <lttng/action/rate-policy.h> |
| 18 | #include <stdbool.h> |
| 19 | #include <sys/types.h> |
| 20 | |
| 21 | #define IS_EVERY_N_RATE_POLICY(policy) \ |
| 22 | (lttng_rate_policy_get_type(policy) == LTTNG_RATE_POLICY_TYPE_EVERY_N) |
| 23 | |
| 24 | #define IS_ONCE_AFTER_N_RATE_POLICY(policy) \ |
| 25 | (lttng_rate_policy_get_type(policy) == \ |
| 26 | LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N) |
| 27 | |
| 28 | typedef void (*rate_policy_destroy_cb)(struct lttng_rate_policy *rate_policy); |
| 29 | typedef int (*rate_policy_serialize_cb)(struct lttng_rate_policy *rate_policy, |
| 30 | struct lttng_payload *payload); |
| 31 | typedef bool (*rate_policy_equal_cb)(const struct lttng_rate_policy *a, |
| 32 | const struct lttng_rate_policy *b); |
| 33 | typedef ssize_t (*rate_policy_create_from_payload_cb)( |
| 34 | struct lttng_payload_view *view, |
| 35 | struct lttng_rate_policy **rate_policy); |
| 36 | typedef struct lttng_rate_policy *(*rate_policy_copy_cb)( |
| 37 | const struct lttng_rate_policy *source); |
| 38 | typedef enum lttng_error_code (*rate_policy_mi_serialize_cb)( |
| 39 | const struct lttng_rate_policy *rate_policy, |
| 40 | struct mi_writer *writer); |
| 41 | |
| 42 | struct lttng_rate_policy { |
| 43 | enum lttng_rate_policy_type type; |
| 44 | rate_policy_serialize_cb serialize; |
| 45 | rate_policy_equal_cb equal; |
| 46 | rate_policy_destroy_cb destroy; |
| 47 | rate_policy_copy_cb copy; |
| 48 | rate_policy_mi_serialize_cb mi_serialize; |
| 49 | }; |
| 50 | |
| 51 | namespace { |
| 52 | struct lttng_rate_policy_every_n { |
| 53 | struct lttng_rate_policy parent; |
| 54 | uint64_t interval; |
| 55 | }; |
| 56 | |
| 57 | struct lttng_rate_policy_once_after_n { |
| 58 | struct lttng_rate_policy parent; |
| 59 | uint64_t threshold; |
| 60 | }; |
| 61 | |
| 62 | struct lttng_rate_policy_comm { |
| 63 | /* enum lttng_rate_policy_type */ |
| 64 | int8_t rate_policy_type; |
| 65 | } LTTNG_PACKED; |
| 66 | |
| 67 | struct lttng_rate_policy_once_after_n_comm { |
| 68 | uint64_t threshold; |
| 69 | } LTTNG_PACKED; |
| 70 | |
| 71 | struct lttng_rate_policy_every_n_comm { |
| 72 | uint64_t interval; |
| 73 | } LTTNG_PACKED; |
| 74 | } /* namespace */ |
| 75 | |
| 76 | /* Forward declaration. */ |
| 77 | static void lttng_rate_policy_init(struct lttng_rate_policy *rate_policy, |
| 78 | enum lttng_rate_policy_type type, |
| 79 | rate_policy_serialize_cb serialize, |
| 80 | rate_policy_equal_cb equal, |
| 81 | rate_policy_destroy_cb destroy, |
| 82 | rate_policy_copy_cb copy, |
| 83 | rate_policy_mi_serialize_cb mi); |
| 84 | |
| 85 | /* Forward declaration. Every n */ |
| 86 | static bool lttng_rate_policy_every_n_should_execute( |
| 87 | const struct lttng_rate_policy *policy, uint64_t counter); |
| 88 | |
| 89 | /* Forward declaration. Once after N */ |
| 90 | static bool lttng_rate_policy_once_after_n_should_execute( |
| 91 | const struct lttng_rate_policy *policy, uint64_t counter); |
| 92 | |
| 93 | const char *lttng_rate_policy_type_string( |
| 94 | enum lttng_rate_policy_type rate_policy_type) |
| 95 | { |
| 96 | switch (rate_policy_type) { |
| 97 | case LTTNG_RATE_POLICY_TYPE_EVERY_N: |
| 98 | return "EVERY-N"; |
| 99 | case LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N: |
| 100 | return "ONCE-AFTER-N"; |
| 101 | default: |
| 102 | return "???"; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | enum lttng_rate_policy_type lttng_rate_policy_get_type( |
| 107 | const struct lttng_rate_policy *policy) |
| 108 | { |
| 109 | return policy ? policy->type : LTTNG_RATE_POLICY_TYPE_UNKNOWN; |
| 110 | } |
| 111 | |
| 112 | void lttng_rate_policy_init(struct lttng_rate_policy *rate_policy, |
| 113 | enum lttng_rate_policy_type type, |
| 114 | rate_policy_serialize_cb serialize, |
| 115 | rate_policy_equal_cb equal, |
| 116 | rate_policy_destroy_cb destroy, |
| 117 | rate_policy_copy_cb copy, |
| 118 | rate_policy_mi_serialize_cb mi) |
| 119 | { |
| 120 | rate_policy->type = type; |
| 121 | rate_policy->serialize = serialize; |
| 122 | rate_policy->equal = equal; |
| 123 | rate_policy->destroy = destroy; |
| 124 | rate_policy->copy = copy; |
| 125 | rate_policy->mi_serialize = mi; |
| 126 | } |
| 127 | |
| 128 | void lttng_rate_policy_destroy(struct lttng_rate_policy *rate_policy) |
| 129 | { |
| 130 | if (!rate_policy) { |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | rate_policy->destroy(rate_policy); |
| 135 | } |
| 136 | |
| 137 | int lttng_rate_policy_serialize(struct lttng_rate_policy *rate_policy, |
| 138 | struct lttng_payload *payload) |
| 139 | { |
| 140 | int ret; |
| 141 | struct lttng_rate_policy_comm rate_policy_comm = { |
| 142 | .rate_policy_type = (int8_t) rate_policy->type, |
| 143 | }; |
| 144 | |
| 145 | ret = lttng_dynamic_buffer_append(&payload->buffer, &rate_policy_comm, |
| 146 | sizeof(rate_policy_comm)); |
| 147 | if (ret) { |
| 148 | goto end; |
| 149 | } |
| 150 | |
| 151 | ret = rate_policy->serialize(rate_policy, payload); |
| 152 | if (ret) { |
| 153 | goto end; |
| 154 | } |
| 155 | end: |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | static ssize_t lttng_rate_policy_once_after_n_create_from_payload( |
| 160 | struct lttng_payload_view *view, |
| 161 | struct lttng_rate_policy **rate_policy) |
| 162 | { |
| 163 | ssize_t consumed_len = -1; |
| 164 | struct lttng_rate_policy *policy = NULL; |
| 165 | const struct lttng_rate_policy_once_after_n_comm *comm; |
| 166 | const struct lttng_payload_view comm_view = |
| 167 | lttng_payload_view_from_view(view, 0, sizeof(*comm)); |
| 168 | |
| 169 | if (!view || !rate_policy) { |
| 170 | consumed_len = -1; |
| 171 | goto end; |
| 172 | } |
| 173 | |
| 174 | if (!lttng_payload_view_is_valid(&comm_view)) { |
| 175 | /* Payload not large enough to contain the header. */ |
| 176 | consumed_len = -1; |
| 177 | goto end; |
| 178 | } |
| 179 | |
| 180 | comm = (const struct lttng_rate_policy_once_after_n_comm *) |
| 181 | comm_view.buffer.data; |
| 182 | |
| 183 | policy = lttng_rate_policy_once_after_n_create(comm->threshold); |
| 184 | if (policy == NULL) { |
| 185 | consumed_len = -1; |
| 186 | goto end; |
| 187 | } |
| 188 | |
| 189 | *rate_policy = policy; |
| 190 | consumed_len = sizeof(*comm); |
| 191 | |
| 192 | end: |
| 193 | return consumed_len; |
| 194 | } |
| 195 | |
| 196 | static ssize_t lttng_rate_policy_every_n_create_from_payload( |
| 197 | struct lttng_payload_view *view, |
| 198 | struct lttng_rate_policy **rate_policy) |
| 199 | { |
| 200 | ssize_t consumed_len = -1; |
| 201 | struct lttng_rate_policy *policy = NULL; |
| 202 | const struct lttng_rate_policy_every_n_comm *comm; |
| 203 | const struct lttng_payload_view comm_view = |
| 204 | lttng_payload_view_from_view(view, 0, sizeof(*comm)); |
| 205 | |
| 206 | if (!view || !rate_policy) { |
| 207 | consumed_len = -1; |
| 208 | goto end; |
| 209 | } |
| 210 | |
| 211 | if (!lttng_payload_view_is_valid(&comm_view)) { |
| 212 | /* Payload not large enough to contain the header. */ |
| 213 | consumed_len = -1; |
| 214 | goto end; |
| 215 | } |
| 216 | |
| 217 | comm = (const struct lttng_rate_policy_every_n_comm *) |
| 218 | comm_view.buffer.data; |
| 219 | |
| 220 | policy = lttng_rate_policy_every_n_create(comm->interval); |
| 221 | if (policy == NULL) { |
| 222 | consumed_len = -1; |
| 223 | goto end; |
| 224 | } |
| 225 | |
| 226 | *rate_policy = policy; |
| 227 | consumed_len = sizeof(*comm); |
| 228 | |
| 229 | end: |
| 230 | return consumed_len; |
| 231 | } |
| 232 | |
| 233 | ssize_t lttng_rate_policy_create_from_payload(struct lttng_payload_view *view, |
| 234 | struct lttng_rate_policy **rate_policy) |
| 235 | { |
| 236 | ssize_t consumed_len, specific_rate_policy_consumed_len; |
| 237 | rate_policy_create_from_payload_cb create_from_payload_cb; |
| 238 | const struct lttng_rate_policy_comm *rate_policy_comm; |
| 239 | const struct lttng_payload_view rate_policy_comm_view = |
| 240 | lttng_payload_view_from_view( |
| 241 | view, 0, sizeof(*rate_policy_comm)); |
| 242 | |
| 243 | if (!view || !rate_policy) { |
| 244 | consumed_len = -1; |
| 245 | goto end; |
| 246 | } |
| 247 | |
| 248 | if (!lttng_payload_view_is_valid(&rate_policy_comm_view)) { |
| 249 | /* Payload not large enough to contain the header. */ |
| 250 | consumed_len = -1; |
| 251 | goto end; |
| 252 | } |
| 253 | |
| 254 | rate_policy_comm = (const struct lttng_rate_policy_comm *) |
| 255 | rate_policy_comm_view.buffer.data; |
| 256 | |
| 257 | DBG("Create rate_policy from payload: rate-policy-type=%s", |
| 258 | lttng_rate_policy_type_string( |
| 259 | (lttng_rate_policy_type) rate_policy_comm->rate_policy_type)); |
| 260 | |
| 261 | switch (rate_policy_comm->rate_policy_type) { |
| 262 | case LTTNG_RATE_POLICY_TYPE_EVERY_N: |
| 263 | create_from_payload_cb = |
| 264 | lttng_rate_policy_every_n_create_from_payload; |
| 265 | break; |
| 266 | case LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N: |
| 267 | create_from_payload_cb = |
| 268 | lttng_rate_policy_once_after_n_create_from_payload; |
| 269 | break; |
| 270 | default: |
| 271 | ERR("Failed to create rate-policy from payload, unhandled rate-policy type: rate-policy-type=%u (%s)", |
| 272 | rate_policy_comm->rate_policy_type, |
| 273 | lttng_rate_policy_type_string( |
| 274 | (lttng_rate_policy_type) rate_policy_comm->rate_policy_type)); |
| 275 | consumed_len = -1; |
| 276 | goto end; |
| 277 | } |
| 278 | |
| 279 | { |
| 280 | /* Create buffer view for the rate_policy-type-specific data. |
| 281 | */ |
| 282 | struct lttng_payload_view specific_rate_policy_view = |
| 283 | lttng_payload_view_from_view(view, |
| 284 | sizeof(struct lttng_rate_policy_comm), |
| 285 | -1); |
| 286 | |
| 287 | specific_rate_policy_consumed_len = create_from_payload_cb( |
| 288 | &specific_rate_policy_view, rate_policy); |
| 289 | } |
| 290 | if (specific_rate_policy_consumed_len < 0) { |
| 291 | ERR("Failed to create specific rate_policy from buffer."); |
| 292 | consumed_len = -1; |
| 293 | goto end; |
| 294 | } |
| 295 | |
| 296 | LTTNG_ASSERT(*rate_policy); |
| 297 | |
| 298 | consumed_len = sizeof(struct lttng_rate_policy_comm) + |
| 299 | specific_rate_policy_consumed_len; |
| 300 | |
| 301 | end: |
| 302 | return consumed_len; |
| 303 | } |
| 304 | |
| 305 | bool lttng_rate_policy_is_equal(const struct lttng_rate_policy *a, |
| 306 | const struct lttng_rate_policy *b) |
| 307 | { |
| 308 | bool is_equal = false; |
| 309 | |
| 310 | if (!a || !b) { |
| 311 | goto end; |
| 312 | } |
| 313 | |
| 314 | if (a->type != b->type) { |
| 315 | goto end; |
| 316 | } |
| 317 | |
| 318 | if (a == b) { |
| 319 | is_equal = true; |
| 320 | goto end; |
| 321 | } |
| 322 | |
| 323 | LTTNG_ASSERT(a->equal); |
| 324 | is_equal = a->equal(a, b); |
| 325 | end: |
| 326 | return is_equal; |
| 327 | } |
| 328 | |
| 329 | bool lttng_rate_policy_should_execute( |
| 330 | const struct lttng_rate_policy *policy, uint64_t counter) |
| 331 | { |
| 332 | switch (policy->type) { |
| 333 | case LTTNG_RATE_POLICY_TYPE_EVERY_N: |
| 334 | return lttng_rate_policy_every_n_should_execute( |
| 335 | policy, counter); |
| 336 | case LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N: |
| 337 | return lttng_rate_policy_once_after_n_should_execute( |
| 338 | policy, counter); |
| 339 | default: |
| 340 | abort(); |
| 341 | break; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | /* Every N */ |
| 346 | static struct lttng_rate_policy_every_n *rate_policy_every_n_from_rate_policy( |
| 347 | struct lttng_rate_policy *policy) |
| 348 | { |
| 349 | LTTNG_ASSERT(policy); |
| 350 | |
| 351 | return container_of(policy, struct lttng_rate_policy_every_n, parent); |
| 352 | } |
| 353 | |
| 354 | static const struct lttng_rate_policy_every_n * |
| 355 | rate_policy_every_n_from_rate_policy_const( |
| 356 | const struct lttng_rate_policy *policy) |
| 357 | { |
| 358 | LTTNG_ASSERT(policy); |
| 359 | |
| 360 | return container_of(policy, struct lttng_rate_policy_every_n, parent); |
| 361 | } |
| 362 | |
| 363 | static int lttng_rate_policy_every_n_serialize( |
| 364 | struct lttng_rate_policy *policy, struct lttng_payload *payload) |
| 365 | { |
| 366 | int ret; |
| 367 | |
| 368 | struct lttng_rate_policy_every_n *every_n_policy; |
| 369 | struct lttng_rate_policy_every_n_comm comm = {}; |
| 370 | |
| 371 | LTTNG_ASSERT(policy); |
| 372 | LTTNG_ASSERT(payload); |
| 373 | |
| 374 | every_n_policy = rate_policy_every_n_from_rate_policy(policy); |
| 375 | comm.interval = every_n_policy->interval; |
| 376 | |
| 377 | ret = lttng_dynamic_buffer_append( |
| 378 | &payload->buffer, &comm, sizeof(comm)); |
| 379 | return ret; |
| 380 | } |
| 381 | |
| 382 | static bool lttng_rate_policy_every_n_is_equal( |
| 383 | const struct lttng_rate_policy *_a, |
| 384 | const struct lttng_rate_policy *_b) |
| 385 | { |
| 386 | bool is_equal = false; |
| 387 | const struct lttng_rate_policy_every_n *a, *b; |
| 388 | |
| 389 | a = rate_policy_every_n_from_rate_policy_const(_a); |
| 390 | b = rate_policy_every_n_from_rate_policy_const(_b); |
| 391 | |
| 392 | if (a->interval != b->interval) { |
| 393 | goto end; |
| 394 | } |
| 395 | |
| 396 | is_equal = true; |
| 397 | |
| 398 | end: |
| 399 | return is_equal; |
| 400 | } |
| 401 | |
| 402 | static void lttng_rate_policy_every_n_destroy(struct lttng_rate_policy *policy) |
| 403 | { |
| 404 | struct lttng_rate_policy_every_n *every_n_policy; |
| 405 | |
| 406 | if (!policy) { |
| 407 | goto end; |
| 408 | } |
| 409 | |
| 410 | every_n_policy = rate_policy_every_n_from_rate_policy(policy); |
| 411 | |
| 412 | free(every_n_policy); |
| 413 | |
| 414 | end: |
| 415 | return; |
| 416 | } |
| 417 | |
| 418 | static struct lttng_rate_policy *lttng_rate_policy_every_n_copy( |
| 419 | const struct lttng_rate_policy *source) |
| 420 | { |
| 421 | struct lttng_rate_policy *copy = NULL; |
| 422 | const struct lttng_rate_policy_every_n *every_n_policy; |
| 423 | |
| 424 | if (!source) { |
| 425 | goto end; |
| 426 | } |
| 427 | |
| 428 | every_n_policy = rate_policy_every_n_from_rate_policy_const(source); |
| 429 | copy = lttng_rate_policy_every_n_create(every_n_policy->interval); |
| 430 | |
| 431 | end: |
| 432 | return copy; |
| 433 | } |
| 434 | |
| 435 | static enum lttng_error_code lttng_rate_policy_every_n_mi_serialize( |
| 436 | const struct lttng_rate_policy *rate_policy, |
| 437 | struct mi_writer *writer) |
| 438 | { |
| 439 | int ret; |
| 440 | enum lttng_error_code ret_code; |
| 441 | const struct lttng_rate_policy_every_n *every_n_policy = NULL; |
| 442 | |
| 443 | LTTNG_ASSERT(rate_policy); |
| 444 | LTTNG_ASSERT(IS_EVERY_N_RATE_POLICY(rate_policy)); |
| 445 | LTTNG_ASSERT(writer); |
| 446 | |
| 447 | every_n_policy = rate_policy_every_n_from_rate_policy_const( |
| 448 | rate_policy); |
| 449 | |
| 450 | /* Open rate_policy_every_n element. */ |
| 451 | ret = mi_lttng_writer_open_element( |
| 452 | writer, mi_lttng_element_rate_policy_every_n); |
| 453 | if (ret) { |
| 454 | goto mi_error; |
| 455 | } |
| 456 | |
| 457 | /* Interval. */ |
| 458 | ret = mi_lttng_writer_write_element_unsigned_int(writer, |
| 459 | mi_lttng_element_rate_policy_every_n_interval, |
| 460 | every_n_policy->interval); |
| 461 | if (ret) { |
| 462 | goto mi_error; |
| 463 | } |
| 464 | |
| 465 | /* Close rate_policy_every_n element. */ |
| 466 | ret = mi_lttng_writer_close_element(writer); |
| 467 | if (ret) { |
| 468 | goto mi_error; |
| 469 | } |
| 470 | |
| 471 | ret_code = LTTNG_OK; |
| 472 | goto end; |
| 473 | |
| 474 | mi_error: |
| 475 | ret_code = LTTNG_ERR_MI_IO_FAIL; |
| 476 | end: |
| 477 | return ret_code; |
| 478 | } |
| 479 | |
| 480 | struct lttng_rate_policy *lttng_rate_policy_every_n_create(uint64_t interval) |
| 481 | { |
| 482 | struct lttng_rate_policy_every_n *policy = NULL; |
| 483 | struct lttng_rate_policy *_policy = NULL; |
| 484 | |
| 485 | if (interval == 0) { |
| 486 | /* |
| 487 | * An interval of 0 is invalid since it would never be fired. |
| 488 | */ |
| 489 | goto end; |
| 490 | } |
| 491 | |
| 492 | policy = zmalloc<lttng_rate_policy_every_n>(); |
| 493 | if (!policy) { |
| 494 | goto end; |
| 495 | } |
| 496 | |
| 497 | lttng_rate_policy_init(&policy->parent, LTTNG_RATE_POLICY_TYPE_EVERY_N, |
| 498 | lttng_rate_policy_every_n_serialize, |
| 499 | lttng_rate_policy_every_n_is_equal, |
| 500 | lttng_rate_policy_every_n_destroy, |
| 501 | lttng_rate_policy_every_n_copy, |
| 502 | lttng_rate_policy_every_n_mi_serialize); |
| 503 | |
| 504 | policy->interval = interval; |
| 505 | |
| 506 | _policy = &policy->parent; |
| 507 | policy = NULL; |
| 508 | |
| 509 | end: |
| 510 | free(policy); |
| 511 | return _policy; |
| 512 | } |
| 513 | |
| 514 | enum lttng_rate_policy_status lttng_rate_policy_every_n_get_interval( |
| 515 | const struct lttng_rate_policy *policy, uint64_t *interval) |
| 516 | { |
| 517 | const struct lttng_rate_policy_every_n *every_n_policy; |
| 518 | enum lttng_rate_policy_status status; |
| 519 | |
| 520 | if (!policy || !IS_EVERY_N_RATE_POLICY(policy) || !interval) { |
| 521 | status = LTTNG_RATE_POLICY_STATUS_INVALID; |
| 522 | goto end; |
| 523 | } |
| 524 | |
| 525 | every_n_policy = rate_policy_every_n_from_rate_policy_const(policy); |
| 526 | *interval = every_n_policy->interval; |
| 527 | status = LTTNG_RATE_POLICY_STATUS_OK; |
| 528 | end: |
| 529 | |
| 530 | return status; |
| 531 | } |
| 532 | |
| 533 | static bool lttng_rate_policy_every_n_should_execute( |
| 534 | const struct lttng_rate_policy *policy, uint64_t counter) |
| 535 | { |
| 536 | const struct lttng_rate_policy_every_n *every_n_policy; |
| 537 | LTTNG_ASSERT(policy); |
| 538 | bool execute = false; |
| 539 | |
| 540 | every_n_policy = rate_policy_every_n_from_rate_policy_const(policy); |
| 541 | |
| 542 | if (every_n_policy->interval == 0) { |
| 543 | abort(); |
| 544 | } |
| 545 | |
| 546 | execute = (counter % every_n_policy->interval) == 0; |
| 547 | |
| 548 | DBG("Policy every N = %" PRIu64 |
| 549 | ": execution %s. Execution count: %" PRIu64, |
| 550 | every_n_policy->interval, |
| 551 | execute ? "accepted" : "denied", counter); |
| 552 | |
| 553 | return execute; |
| 554 | } |
| 555 | |
| 556 | /* Once after N */ |
| 557 | |
| 558 | static struct lttng_rate_policy_once_after_n * |
| 559 | rate_policy_once_after_n_from_rate_policy(struct lttng_rate_policy *policy) |
| 560 | { |
| 561 | LTTNG_ASSERT(policy); |
| 562 | |
| 563 | return container_of( |
| 564 | policy, struct lttng_rate_policy_once_after_n, parent); |
| 565 | } |
| 566 | |
| 567 | static const struct lttng_rate_policy_once_after_n * |
| 568 | rate_policy_once_after_n_from_rate_policy_const( |
| 569 | const struct lttng_rate_policy *policy) |
| 570 | { |
| 571 | LTTNG_ASSERT(policy); |
| 572 | |
| 573 | return container_of( |
| 574 | policy, struct lttng_rate_policy_once_after_n, parent); |
| 575 | } |
| 576 | static int lttng_rate_policy_once_after_n_serialize( |
| 577 | struct lttng_rate_policy *policy, struct lttng_payload *payload) |
| 578 | { |
| 579 | int ret; |
| 580 | |
| 581 | struct lttng_rate_policy_once_after_n *once_after_n_policy; |
| 582 | struct lttng_rate_policy_once_after_n_comm comm = {}; |
| 583 | |
| 584 | LTTNG_ASSERT(policy); |
| 585 | LTTNG_ASSERT(payload); |
| 586 | |
| 587 | once_after_n_policy = rate_policy_once_after_n_from_rate_policy(policy); |
| 588 | comm.threshold = once_after_n_policy->threshold; |
| 589 | |
| 590 | ret = lttng_dynamic_buffer_append( |
| 591 | &payload->buffer, &comm, sizeof(comm)); |
| 592 | return ret; |
| 593 | } |
| 594 | |
| 595 | static bool lttng_rate_policy_once_after_n_is_equal( |
| 596 | const struct lttng_rate_policy *_a, |
| 597 | const struct lttng_rate_policy *_b) |
| 598 | { |
| 599 | bool is_equal = false; |
| 600 | const struct lttng_rate_policy_once_after_n *a, *b; |
| 601 | |
| 602 | a = rate_policy_once_after_n_from_rate_policy_const(_a); |
| 603 | b = rate_policy_once_after_n_from_rate_policy_const(_b); |
| 604 | |
| 605 | if (a->threshold != b->threshold) { |
| 606 | goto end; |
| 607 | } |
| 608 | |
| 609 | is_equal = true; |
| 610 | |
| 611 | end: |
| 612 | return is_equal; |
| 613 | } |
| 614 | |
| 615 | static void lttng_rate_policy_once_after_n_destroy( |
| 616 | struct lttng_rate_policy *policy) |
| 617 | { |
| 618 | struct lttng_rate_policy_once_after_n *once_after_n_policy; |
| 619 | |
| 620 | if (!policy) { |
| 621 | goto end; |
| 622 | } |
| 623 | |
| 624 | once_after_n_policy = rate_policy_once_after_n_from_rate_policy(policy); |
| 625 | |
| 626 | free(once_after_n_policy); |
| 627 | |
| 628 | end: |
| 629 | return; |
| 630 | } |
| 631 | |
| 632 | static struct lttng_rate_policy *lttng_rate_policy_once_after_n_copy( |
| 633 | const struct lttng_rate_policy *source) |
| 634 | { |
| 635 | struct lttng_rate_policy *copy = NULL; |
| 636 | const struct lttng_rate_policy_once_after_n *once_after_n_policy; |
| 637 | |
| 638 | if (!source) { |
| 639 | goto end; |
| 640 | } |
| 641 | |
| 642 | once_after_n_policy = |
| 643 | rate_policy_once_after_n_from_rate_policy_const(source); |
| 644 | copy = lttng_rate_policy_once_after_n_create( |
| 645 | once_after_n_policy->threshold); |
| 646 | |
| 647 | end: |
| 648 | return copy; |
| 649 | } |
| 650 | |
| 651 | static enum lttng_error_code lttng_rate_policy_once_after_n_mi_serialize( |
| 652 | const struct lttng_rate_policy *rate_policy, |
| 653 | struct mi_writer *writer) |
| 654 | { |
| 655 | int ret; |
| 656 | enum lttng_error_code ret_code; |
| 657 | const struct lttng_rate_policy_once_after_n *once_after_n_policy = NULL; |
| 658 | |
| 659 | LTTNG_ASSERT(rate_policy); |
| 660 | LTTNG_ASSERT(IS_ONCE_AFTER_N_RATE_POLICY(rate_policy)); |
| 661 | LTTNG_ASSERT(writer); |
| 662 | |
| 663 | once_after_n_policy = rate_policy_once_after_n_from_rate_policy_const( |
| 664 | rate_policy); |
| 665 | |
| 666 | /* Open rate_policy_once_after_n. */ |
| 667 | ret = mi_lttng_writer_open_element( |
| 668 | writer, mi_lttng_element_rate_policy_once_after_n); |
| 669 | if (ret) { |
| 670 | goto mi_error; |
| 671 | } |
| 672 | |
| 673 | /* Threshold. */ |
| 674 | ret = mi_lttng_writer_write_element_unsigned_int(writer, |
| 675 | mi_lttng_element_rate_policy_once_after_n_threshold, |
| 676 | once_after_n_policy->threshold); |
| 677 | if (ret) { |
| 678 | goto mi_error; |
| 679 | } |
| 680 | |
| 681 | /* Close rate_policy_once_after_n element. */ |
| 682 | ret = mi_lttng_writer_close_element(writer); |
| 683 | if (ret) { |
| 684 | goto mi_error; |
| 685 | } |
| 686 | |
| 687 | ret_code = LTTNG_OK; |
| 688 | goto end; |
| 689 | |
| 690 | mi_error: |
| 691 | ret_code = LTTNG_ERR_MI_IO_FAIL; |
| 692 | end: |
| 693 | return ret_code; |
| 694 | } |
| 695 | |
| 696 | struct lttng_rate_policy *lttng_rate_policy_once_after_n_create( |
| 697 | uint64_t threshold) |
| 698 | { |
| 699 | struct lttng_rate_policy_once_after_n *policy = NULL; |
| 700 | struct lttng_rate_policy *_policy = NULL; |
| 701 | |
| 702 | if (threshold == 0) { |
| 703 | /* threshold is expected to be > 0 */ |
| 704 | goto end; |
| 705 | } |
| 706 | |
| 707 | policy = zmalloc<lttng_rate_policy_once_after_n>(); |
| 708 | if (!policy) { |
| 709 | goto end; |
| 710 | } |
| 711 | |
| 712 | lttng_rate_policy_init(&policy->parent, |
| 713 | LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N, |
| 714 | lttng_rate_policy_once_after_n_serialize, |
| 715 | lttng_rate_policy_once_after_n_is_equal, |
| 716 | lttng_rate_policy_once_after_n_destroy, |
| 717 | lttng_rate_policy_once_after_n_copy, |
| 718 | lttng_rate_policy_once_after_n_mi_serialize); |
| 719 | |
| 720 | policy->threshold = threshold; |
| 721 | |
| 722 | _policy = &policy->parent; |
| 723 | policy = NULL; |
| 724 | |
| 725 | end: |
| 726 | free(policy); |
| 727 | return _policy; |
| 728 | } |
| 729 | |
| 730 | enum lttng_rate_policy_status lttng_rate_policy_once_after_n_get_threshold( |
| 731 | const struct lttng_rate_policy *policy, uint64_t *threshold) |
| 732 | { |
| 733 | const struct lttng_rate_policy_once_after_n *once_after_n_policy; |
| 734 | enum lttng_rate_policy_status status; |
| 735 | |
| 736 | if (!policy || !IS_ONCE_AFTER_N_RATE_POLICY(policy) || !threshold) { |
| 737 | status = LTTNG_RATE_POLICY_STATUS_INVALID; |
| 738 | goto end; |
| 739 | } |
| 740 | |
| 741 | once_after_n_policy = |
| 742 | rate_policy_once_after_n_from_rate_policy_const(policy); |
| 743 | *threshold = once_after_n_policy->threshold; |
| 744 | status = LTTNG_RATE_POLICY_STATUS_OK; |
| 745 | end: |
| 746 | |
| 747 | return status; |
| 748 | } |
| 749 | |
| 750 | struct lttng_rate_policy *lttng_rate_policy_copy( |
| 751 | const struct lttng_rate_policy *source) |
| 752 | { |
| 753 | LTTNG_ASSERT(source->copy); |
| 754 | return source->copy(source); |
| 755 | } |
| 756 | |
| 757 | static bool lttng_rate_policy_once_after_n_should_execute( |
| 758 | const struct lttng_rate_policy *policy, uint64_t counter) |
| 759 | { |
| 760 | const struct lttng_rate_policy_once_after_n *once_after_n_policy; |
| 761 | bool execute = false; |
| 762 | LTTNG_ASSERT(policy); |
| 763 | |
| 764 | once_after_n_policy = |
| 765 | rate_policy_once_after_n_from_rate_policy_const(policy); |
| 766 | |
| 767 | execute = counter == once_after_n_policy->threshold; |
| 768 | |
| 769 | DBG("Policy once after N = %" PRIu64 |
| 770 | ": execution %s. Execution count: %" PRIu64, |
| 771 | once_after_n_policy->threshold, |
| 772 | execute ? "accepted" : "denied", counter); |
| 773 | |
| 774 | return counter == once_after_n_policy->threshold; |
| 775 | } |
| 776 | |
| 777 | enum lttng_error_code lttng_rate_policy_mi_serialize( |
| 778 | const struct lttng_rate_policy *rate_policy, |
| 779 | struct mi_writer *writer) |
| 780 | { |
| 781 | int ret; |
| 782 | enum lttng_error_code ret_code; |
| 783 | |
| 784 | LTTNG_ASSERT(rate_policy); |
| 785 | LTTNG_ASSERT(writer); |
| 786 | LTTNG_ASSERT(rate_policy->mi_serialize); |
| 787 | |
| 788 | /* Open rate policy element. */ |
| 789 | ret = mi_lttng_writer_open_element( |
| 790 | writer, mi_lttng_element_rate_policy); |
| 791 | if (ret) { |
| 792 | goto mi_error; |
| 793 | } |
| 794 | |
| 795 | /* Serialize underlying rate policy. */ |
| 796 | ret_code = rate_policy->mi_serialize(rate_policy, writer); |
| 797 | if (ret_code != LTTNG_OK) { |
| 798 | goto end; |
| 799 | } |
| 800 | |
| 801 | /* Close rate policy element. */ |
| 802 | ret = mi_lttng_writer_close_element(writer); |
| 803 | if (ret) { |
| 804 | goto mi_error; |
| 805 | } |
| 806 | |
| 807 | ret_code = LTTNG_OK; |
| 808 | goto end; |
| 809 | |
| 810 | mi_error: |
| 811 | ret_code = LTTNG_ERR_MI_IO_FAIL; |
| 812 | end: |
| 813 | return ret_code; |
| 814 | } |