| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************/ | ||
| 2 | /* Copyright 2009-2026 Barcelona Supercomputing Center */ | ||
| 3 | /* */ | ||
| 4 | /* This file is part of the DLB library. */ | ||
| 5 | /* */ | ||
| 6 | /* DLB is free software: you can redistribute it and/or modify */ | ||
| 7 | /* it under the terms of the GNU Lesser General Public License as published by */ | ||
| 8 | /* the Free Software Foundation, either version 3 of the License, or */ | ||
| 9 | /* (at your option) any later version. */ | ||
| 10 | /* */ | ||
| 11 | /* DLB is distributed in the hope that it will be useful, */ | ||
| 12 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ | ||
| 13 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ | ||
| 14 | /* GNU Lesser General Public License for more details. */ | ||
| 15 | /* */ | ||
| 16 | /* You should have received a copy of the GNU Lesser General Public License */ | ||
| 17 | /* along with DLB. If not, see <https://www.gnu.org/licenses/>. */ | ||
| 18 | /*********************************************************************************/ | ||
| 19 | |||
| 20 | |||
| 21 | #include "talp/talp_energy.h" | ||
| 22 | |||
| 23 | #include "apis/dlb_errors.h" | ||
| 24 | #include "LB_core/spd.h" | ||
| 25 | #include "support/debug.h" | ||
| 26 | #include "support/dlb_common.h" | ||
| 27 | #include "support/mytime.h" | ||
| 28 | #include "talp/backend.h" | ||
| 29 | #include "talp/backend_manager.h" | ||
| 30 | |||
| 31 | // Minimum interval between real backend queries, to keep readings meaningful | ||
| 32 | enum { ENERGY_MIN_COLLECT_INTERVAL_NS = 1000000000 }; // 1 second | ||
| 33 | |||
| 34 | typedef struct { | ||
| 35 | bool active; // whether the energy backend is loaded and active | ||
| 36 | bool have_last; // whether last_raw holds a real reading yet | ||
| 37 | energy_readings_t last_raw; // last raw cumulative readings (nJ) from backend | ||
| 38 | int64_t last_query_ts; // timestamp (ns) of the last actual backend query | ||
| 39 | } energy_ctx_t; | ||
| 40 | |||
| 41 | static energy_ctx_t ctx = {0}; | ||
| 42 | |||
| 43 | static const backend_api_t *energy_backend_api = NULL; | ||
| 44 | |||
| 45 | 21 | int talp_energy_init(const subprocess_descriptor_t *spd) | |
| 46 | { | ||
| 47 | 21 | energy_backend_api = talp_backend_manager_load_energy_backend(spd->options.talp_energy_backend); | |
| 48 |
1/2✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
|
21 | if (energy_backend_api == NULL) { |
| 49 | 21 | debug_warning("Energy backend could not be loaded"); | |
| 50 | 21 | return DLB_ERR_UNKNOWN; | |
| 51 | } | ||
| 52 | |||
| 53 | int error; | ||
| 54 | ✗ | energy_readings_t first_collect = {0}; | |
| 55 | |||
| 56 | // If energy component is not explicitly set, probe plugin first | ||
| 57 | ✗ | if (!(spd->options.talp & TALP_COMPONENT_ENERGY)) { | |
| 58 | ✗ | error = energy_backend_api->probe(); | |
| 59 | ✗ | if (error == DLB_BACKEND_ERROR) { | |
| 60 | ✗ | debug_warning("Energy backend probe failed"); | |
| 61 | ✗ | talp_backend_manager_unload_energy_backend(); | |
| 62 | ✗ | energy_backend_api = NULL; | |
| 63 | ✗ | return DLB_ERR_UNKNOWN; | |
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | ✗ | error = energy_backend_api->init(&core_api); | |
| 68 | ✗ | if (error == DLB_BACKEND_ERROR) { | |
| 69 | ✗ | debug_warning("Energy backend could not be initialized"); | |
| 70 | ✗ | talp_backend_manager_unload_energy_backend(); | |
| 71 | ✗ | energy_backend_api = NULL; | |
| 72 | ✗ | return DLB_ERR_UNKNOWN; | |
| 73 | } | ||
| 74 | |||
| 75 | ✗ | error = energy_backend_api->start(); | |
| 76 | ✗ | if (error == DLB_BACKEND_ERROR) { | |
| 77 | ✗ | debug_warning("Energy backend could not be started"); | |
| 78 | ✗ | energy_backend_api->finalize(); | |
| 79 | ✗ | talp_backend_manager_unload_energy_backend(); | |
| 80 | ✗ | energy_backend_api = NULL; | |
| 81 | ✗ | return DLB_ERR_UNKNOWN; | |
| 82 | } | ||
| 83 | |||
| 84 | ✗ | ctx.active = true; | |
| 85 | ✗ | bool collected = talp_energy_collect(&first_collect, NULL); | |
| 86 | ✗ | if (!collected) { | |
| 87 | ✗ | debug_warning("Energy collection initialization failed and will be done at a later time"); | |
| 88 | } | ||
| 89 | |||
| 90 | ✗ | return DLB_SUCCESS; | |
| 91 | } | ||
| 92 | |||
| 93 | ✗ | void talp_energy_finalize(void) | |
| 94 | { | ||
| 95 | ✗ | if (energy_backend_api != NULL) { | |
| 96 | ✗ | energy_backend_api->stop(); | |
| 97 | ✗ | energy_backend_api->finalize(); | |
| 98 | |||
| 99 | ✗ | talp_backend_manager_unload_energy_backend(); | |
| 100 | ✗ | energy_backend_api = NULL; | |
| 101 | } | ||
| 102 | |||
| 103 | ✗ | ctx.active = false; | |
| 104 | ✗ | ctx.have_last = false; | |
| 105 | ✗ | ctx.last_raw = (const energy_readings_t){0}; | |
| 106 | ✗ | ctx.last_query_ts = 0; | |
| 107 | } | ||
| 108 | |||
| 109 | ✗ | static int64_t delta_guarded(int64_t raw, int64_t last_raw) | |
| 110 | { | ||
| 111 | // Guard against counter reset/wrap | ||
| 112 | ✗ | return raw >= last_raw ? raw - last_raw : 0; | |
| 113 | } | ||
| 114 | |||
| 115 | ✗ | bool talp_energy_collect(energy_readings_t *out, int64_t *interval_ns) | |
| 116 | { | ||
| 117 | ✗ | if (out == NULL) return false; | |
| 118 | |||
| 119 | ✗ | *out = (energy_readings_t){0}; | |
| 120 | ✗ | if (interval_ns != NULL) *interval_ns = 0; | |
| 121 | |||
| 122 | // No backend loaded or not started yet | ||
| 123 | ✗ | if (!ctx.active || energy_backend_api == NULL) { | |
| 124 | ✗ | return false; | |
| 125 | } | ||
| 126 | |||
| 127 | // Too soon since the last real query; skip this call entirely | ||
| 128 | ✗ | int64_t now = get_time_in_ns(); | |
| 129 | ✗ | if (ctx.have_last && now - ctx.last_query_ts < ENERGY_MIN_COLLECT_INTERVAL_NS) { | |
| 130 | ✗ | return false; | |
| 131 | } | ||
| 132 | |||
| 133 | // Collect energy reading | ||
| 134 | energy_readings_t raw; | ||
| 135 | ✗ | if (energy_backend_api->energy.collect(&raw) != DLB_BACKEND_SUCCESS) { | |
| 136 | ✗ | return false; | |
| 137 | } | ||
| 138 | |||
| 139 | // Real interval since the last query, computed before last_query_ts moves | ||
| 140 | ✗ | int64_t elapsed = now - ctx.last_query_ts; | |
| 141 | ✗ | ctx.last_query_ts = now; | |
| 142 | |||
| 143 | // First reading only sets the baseline | ||
| 144 | ✗ | if (unlikely(!ctx.have_last)) { | |
| 145 | ✗ | ctx.have_last = true; | |
| 146 | ✗ | ctx.last_raw = raw; | |
| 147 | ✗ | return false; | |
| 148 | } | ||
| 149 | |||
| 150 | // Same delta treatment for all four fields, regardless of backend | ||
| 151 | ✗ | out->cpu = delta_guarded(raw.cpu, ctx.last_raw.cpu); | |
| 152 | ✗ | out->dram = delta_guarded(raw.dram, ctx.last_raw.dram); | |
| 153 | ✗ | out->node = delta_guarded(raw.node, ctx.last_raw.node); | |
| 154 | ✗ | out->gpu = delta_guarded(raw.gpu, ctx.last_raw.gpu); | |
| 155 | |||
| 156 | // Becomes the baseline for the next call's delta | ||
| 157 | ✗ | ctx.last_raw = raw; | |
| 158 | |||
| 159 | ✗ | if (interval_ns != NULL) *interval_ns = elapsed; | |
| 160 | |||
| 161 | ✗ | return true; | |
| 162 | } | ||
| 163 |