| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #define PULSE_INTERNALS 1 |
| 4 | #include "pulse-ml.h" |
| 5 | |
| 6 | static struct ml_statistics { |
| 7 | PAD64(uint64_t) ml_models_consulted; |
| 8 | PAD64(uint64_t) ml_models_received; |
| 9 | PAD64(uint64_t) ml_models_ignored; |
| 10 | PAD64(uint64_t) ml_models_sent; |
| 11 | PAD64(uint64_t) ml_models_deserialization_failures; |
| 12 | PAD64(uint64_t) ml_memory_consumption; |
| 13 | PAD64(uint64_t) ml_memory_new; |
| 14 | PAD64(uint64_t) ml_memory_delete; |
| 15 | } ml_statistics = { 0 }; |
| 16 | |
| 17 | void pulse_ml_models_received() |
| 18 | { |
| 19 | __atomic_fetch_add(&ml_statistics.ml_models_received, 1, __ATOMIC_RELAXED); |
| 20 | } |
| 21 | |
| 22 | void pulse_ml_models_ignored() |
| 23 | { |
| 24 | __atomic_fetch_add(&ml_statistics.ml_models_ignored, 1, __ATOMIC_RELAXED); |
| 25 | } |
| 26 | |
| 27 | void pulse_ml_models_sent() |
| 28 | { |
| 29 | __atomic_fetch_add(&ml_statistics.ml_models_sent, 1, __ATOMIC_RELAXED); |
| 30 | } |
| 31 | |
| 32 | void global_statistics_ml_models_deserialization_failures() |
| 33 | { |
| 34 | __atomic_fetch_add(&ml_statistics.ml_models_deserialization_failures, 1, __ATOMIC_RELAXED); |
| 35 | } |
| 36 | |
| 37 | void pulse_ml_models_consulted(size_t models_consulted) |
| 38 | { |
| 39 | __atomic_fetch_add(&ml_statistics.ml_models_consulted, models_consulted, __ATOMIC_RELAXED); |
| 40 | } |
| 41 | |
| 42 | void pulse_ml_memory_allocated(size_t n) |
| 43 | { |
| 44 | __atomic_fetch_add(&ml_statistics.ml_memory_consumption, n, __ATOMIC_RELAXED); |
| 45 | __atomic_fetch_add(&ml_statistics.ml_memory_new, 1, __ATOMIC_RELAXED); |
| 46 | } |
| 47 | |
| 48 | void pulse_ml_memory_freed(size_t n) |
| 49 | { |
| 50 | __atomic_fetch_sub(&ml_statistics.ml_memory_consumption, n, __ATOMIC_RELAXED); |
| 51 | __atomic_fetch_add(&ml_statistics.ml_memory_delete, 1, __ATOMIC_RELAXED); |
| 52 | } |
| 53 | |
| 54 | uint64_t pulse_ml_get_current_memory_usage(void) { |
| 55 | return __atomic_load_n(&ml_statistics.ml_memory_consumption, __ATOMIC_RELAXED); |
| 56 | } |
| 57 | |
| 58 | static inline void ml_statistics_copy(struct ml_statistics *gs) |
| 59 | { |
| 60 | gs->ml_models_consulted = __atomic_load_n(&ml_statistics.ml_models_consulted, __ATOMIC_RELAXED); |
| 61 | gs->ml_models_received = __atomic_load_n(&ml_statistics.ml_models_received, __ATOMIC_RELAXED); |
| 62 | gs->ml_models_sent = __atomic_load_n(&ml_statistics.ml_models_sent, __ATOMIC_RELAXED); |
| 63 | gs->ml_models_ignored = __atomic_load_n(&ml_statistics.ml_models_ignored, __ATOMIC_RELAXED); |
| 64 | gs->ml_models_deserialization_failures = |
| 65 | __atomic_load_n(&ml_statistics.ml_models_deserialization_failures, __ATOMIC_RELAXED); |
| 66 | |
| 67 | gs->ml_memory_consumption = __atomic_load_n(&ml_statistics.ml_memory_consumption, __ATOMIC_RELAXED); |
| 68 | gs->ml_memory_new = __atomic_load_n(&ml_statistics.ml_memory_new, __ATOMIC_RELAXED); |
| 69 | gs->ml_memory_delete = __atomic_load_n(&ml_statistics.ml_memory_delete, __ATOMIC_RELAXED); |
| 70 | } |
| 71 | |
| 72 | void pulse_ml_do(bool extended) |
| 73 | { |
| 74 | if (!extended) |
| 75 | return; |
| 76 | |
| 77 | struct ml_statistics gs; |
| 78 | ml_statistics_copy(&gs); |
| 79 | |
| 80 | ml_update_global_statistics_charts( |
| 81 | gs.ml_models_consulted, |
| 82 | gs.ml_models_received, |
| 83 | gs.ml_models_sent, |
| 84 | gs.ml_models_ignored, |
| 85 | gs.ml_models_deserialization_failures, |
| 86 | gs.ml_memory_consumption, |
| 87 | gs.ml_memory_new, |
| 88 | gs.ml_memory_delete); |
| 89 | } |