Line data Source code
1 : /*
2 : * Copyright (C) 2025 Andrea Mazzoleni
3 : *
4 : * This program is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 3 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * This program is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "portable.h"
19 :
20 : #include "thermal.h"
21 : #include "state.h"
22 : #include "io.h"
23 :
24 : #include <math.h>
25 :
26 30 : struct snapraid_thermal* thermal_alloc(uint64_t dev, const char* name)
27 : {
28 30 : struct snapraid_thermal* thermal = malloc_nofail(sizeof(struct snapraid_thermal));
29 :
30 30 : thermal->device = dev;
31 30 : thermal->latest_temperature = 0;
32 30 : thermal->count = 0;
33 30 : pathcpy(thermal->name, sizeof(thermal->name), name);
34 :
35 30 : return thermal;
36 : }
37 :
38 30 : void thermal_free(struct snapraid_thermal* thermal)
39 : {
40 30 : free(thermal);
41 30 : }
42 :
43 : /*
44 : * Fit exponential heating model to data using least squares
45 : */
46 30 : struct snapraid_thermal_params fit_thermal_model(const struct snapraid_thermal_point* points, int n_points, double t_ambient)
47 : {
48 : struct snapraid_thermal_params model;
49 : double t_steady_try;
50 : double k_try;
51 :
52 30 : memset(&model, 0, sizeof(model));
53 :
54 30 : model.t_ambient = t_ambient;
55 :
56 : /* at least four points to have a result */
57 30 : if (n_points < 4)
58 0 : return model;
59 :
60 30 : double last_temp = points[n_points - 1].temperature;
61 :
62 : /* iterative refinement to find best k_heat and t_steady */
63 30 : double best_error = 1e10;
64 30 : double best_k = 0;
65 30 : double best_t_steady = 0;
66 :
67 : /* grid search for parameters */
68 1440 : for (t_steady_try = last_temp + 2.0; t_steady_try <= last_temp + 25.0; t_steady_try += 0.5) {
69 38070 : for (k_try = 0.00001; k_try <= 0.001; k_try *= 1.2) {
70 36660 : double error = 0.0;
71 : int i;
72 :
73 : /* calculate error for this parameter set */
74 219960 : for (i = 0; i < n_points; i++) {
75 183300 : double t = points[i].time;
76 183300 : double t_predicted = t_steady_try - (t_steady_try - points[0].temperature) * exp(-k_try * t);
77 183300 : double diff = points[i].temperature - t_predicted;
78 183300 : error += diff * diff;
79 : }
80 :
81 36660 : if (error < best_error) {
82 780 : best_error = error;
83 780 : best_k = k_try;
84 780 : best_t_steady = t_steady_try;
85 : }
86 : }
87 : }
88 :
89 30 : model.k_heat = best_k;
90 30 : model.t_steady = best_t_steady;
91 :
92 : /* calculate quality metrics */
93 30 : double sum_squared_residuals = 0.0;
94 30 : double sum_total = 0.0;
95 30 : double mean_temp = 0.0;
96 30 : model.max_error = 0.0;
97 : int i;
98 :
99 : /* calculate mean temperature */
100 180 : for (i = 0; i < n_points; i++)
101 150 : mean_temp += points[i].temperature;
102 30 : mean_temp /= n_points;
103 :
104 : /* calculate R-squared and errors */
105 180 : for (i = 0; i < n_points; i++) {
106 150 : double t = points[i].time;
107 150 : double t_predicted = model.t_steady - (model.t_steady - points[0].temperature) * exp(-model.k_heat * t);
108 150 : double residual = points[i].temperature - t_predicted;
109 :
110 150 : sum_squared_residuals += residual * residual;
111 150 : sum_total += (points[i].temperature - mean_temp) * (points[i].temperature - mean_temp);
112 :
113 150 : double abs_error = fabs(residual);
114 150 : if (abs_error > model.max_error) {
115 90 : model.max_error = abs_error;
116 : }
117 : }
118 :
119 30 : model.rmse = sqrt(sum_squared_residuals / n_points);
120 30 : model.r_squared = 1.0 - (sum_squared_residuals / sum_total);
121 :
122 30 : return model;
123 : }
124 :
125 2620 : void state_thermal(struct snapraid_state* state, time_t now)
126 : {
127 : tommy_node* i;
128 : unsigned j;
129 : tommy_list high;
130 : tommy_list low;
131 : int ret;
132 :
133 2620 : if (state->thermal_temperature_limit == 0)
134 204 : return;
135 :
136 2416 : tommy_list_init(&high);
137 2416 : tommy_list_init(&low);
138 :
139 : /* for all disks */
140 16912 : for (i = state->disklist; i != 0; i = i->next) {
141 14496 : struct snapraid_disk* disk = i->data;
142 : devinfo_t* entry;
143 :
144 14496 : entry = calloc_nofail(1, sizeof(devinfo_t));
145 :
146 14496 : entry->device = disk->device;
147 14496 : device_name_set(entry, disk->name, 0);
148 14496 : pathcpy(entry->mount, sizeof(entry->mount), disk->dir);
149 14496 : pathcpy(entry->smartctl, sizeof(entry->smartctl), disk->smartctl);
150 14496 : memcpy(entry->smartignore, disk->smartignore, sizeof(entry->smartignore));
151 :
152 14496 : tommy_list_insert_tail(&high, &entry->node, entry);
153 : }
154 :
155 : /* for all parities */
156 16912 : for (j = 0; j < state->level; ++j) {
157 : devinfo_t* entry;
158 : unsigned s;
159 :
160 72480 : for (s = 0; s < state->parity[j].split_mac; ++s) {
161 57984 : entry = calloc_nofail(1, sizeof(devinfo_t));
162 :
163 57984 : entry->device = state->parity[j].split_map[s].device;
164 57984 : device_name_set(entry, lev_config_name(j), s);
165 57984 : pathcpy(entry->mount, sizeof(entry->mount), state->parity[j].split_map[s].path);
166 57984 : pathcpy(entry->smartctl, sizeof(entry->smartctl), state->parity[j].smartctl);
167 57984 : memcpy(entry->smartignore, state->parity[j].smartignore, sizeof(entry->smartignore));
168 57984 : pathcut(entry->mount); /* remove the parity file */
169 :
170 57984 : tommy_list_insert_tail(&high, &entry->node, entry);
171 : }
172 : }
173 :
174 : /* with a GUI always gives time reference */
175 2416 : if (state->opt.gui)
176 0 : log_tag("unixtime:%" PRIi64 "\n", (int64_t)now);
177 :
178 2416 : if (state->opt.fake_device) {
179 2416 : ret = devtest(&high, &low, DEVICE_SMART);
180 : } else {
181 0 : ret = devquery(&high, &low, DEVICE_SMART);
182 : }
183 :
184 : /* on error, just disable thermal gathering */
185 2416 : if (ret != 0)
186 0 : return;
187 :
188 : /* if the list is empty, it's not supported in this platform */
189 2416 : if (tommy_list_empty(&low))
190 0 : return;
191 :
192 : /* report new attribute */
193 2416 : state_attr(state, &low);
194 :
195 : /* if ambient temperature is not set, set it now with the lowest HD temperature */
196 2416 : if (state->thermal_ambient_temperature == 0) {
197 1 : state->thermal_ambient_temperature = ambient_temperature();
198 :
199 31 : for (i = tommy_list_head(&low); i != 0; i = i->next) {
200 30 : devinfo_t* devinfo = i->data;
201 :
202 30 : int temp = smart_temp(devinfo);
203 30 : if (temp < 0)
204 0 : continue;
205 :
206 30 : log_tag("thermal:system:candidate:%d\n", temp);
207 :
208 30 : if (state->thermal_ambient_temperature == 0 || state->thermal_ambient_temperature > temp)
209 1 : state->thermal_ambient_temperature = temp;
210 : }
211 :
212 1 : log_tag("thermal:system:final:%d\n", state->thermal_ambient_temperature);
213 : }
214 :
215 2416 : int highest_temperature = 0;
216 74896 : for (i = tommy_list_head(&low); i != 0; i = i->next) {
217 : tommy_node* t;
218 : struct snapraid_thermal* found;
219 72480 : devinfo_t* devinfo = i->data;
220 : unsigned k;
221 :
222 72480 : int temperature = smart_temp(devinfo);
223 72480 : if (temperature < 0)
224 0 : continue;
225 :
226 : /* search of the entry */
227 72480 : found = 0;
228 1123440 : for (t = tommy_list_head(&state->thermallist); t != 0; t = t->next) {
229 1123410 : struct snapraid_thermal* thermal = t->data;
230 1123410 : if (thermal->device == devinfo->device) {
231 72450 : found = thermal;
232 72450 : break;
233 : }
234 : }
235 :
236 : /* if not found, create it */
237 72480 : if (found == 0) {
238 30 : found = thermal_alloc(devinfo->device, devinfo->name);
239 30 : tommy_list_insert_tail(&state->thermallist, &found->node, found);
240 : }
241 :
242 72480 : found->latest_temperature = temperature;
243 :
244 72480 : if (highest_temperature < temperature)
245 2416 : highest_temperature = temperature;
246 :
247 72480 : log_tag("thermal:current:%s:%" PRIu64 ":%d\n", devinfo->name, devinfo->device, temperature);
248 :
249 72480 : if (state->thermal_stop_gathering)
250 0 : continue;
251 :
252 72480 : if (found->count + 1 >= THERMAL_MAX) /* keep one extra space at the end */
253 0 : continue;
254 :
255 : /* only monotone increasing temperature */
256 72480 : if (found->count > 0 && found->data[found->count - 1].temperature >= temperature)
257 72450 : continue;
258 :
259 : /* insert the new data point */
260 30 : found->data[found->count].temperature = temperature;
261 30 : found->data[found->count].time = now - state->thermal_first;
262 30 : ++found->count;
263 :
264 30 : if (state->opt.fake_device) {
265 : /* fill with fake data */
266 30 : found->data[0].time = 0;
267 30 : found->data[0].temperature = 27;
268 30 : found->data[1].time = 100;
269 30 : found->data[1].temperature = 28;
270 30 : found->data[2].time = 300;
271 30 : found->data[2].temperature = 29;
272 30 : found->data[3].time = 700;
273 30 : found->data[3].temperature = 30;
274 30 : found->data[4].time = 1500;
275 30 : found->data[4].temperature = 31;
276 30 : found->count = 5;
277 : }
278 :
279 : /* log the new data */
280 30 : log_tag("thermal:heat:%s:%" PRIu64 ":%u:", devinfo->name, devinfo->device, found->count);
281 180 : for (k = 0; k < found->count; ++k)
282 150 : log_tag("%s%d/%d", k > 0 ? "," : "", (int)found->data[k].temperature, (int)found->data[k].time);
283 30 : log_tag("\n");
284 :
285 : /* estimate parameters */
286 30 : found->params = fit_thermal_model(found->data, found->count, state->thermal_ambient_temperature);
287 :
288 30 : log_tag("thermal:params:%s:%" PRIu64 ":%g:%g:%g:%g:%g:%g\n", devinfo->name, devinfo->device,
289 : found->params.k_heat, found->params.t_ambient, found->params.t_steady,
290 : found->params.rmse, found->params.r_squared, found->params.max_error);
291 : }
292 :
293 : /* always update the highest temperature */
294 2416 : state->thermal_highest_temperature = highest_temperature;
295 :
296 2416 : log_tag("thermal:highest:%d\n", highest_temperature);
297 2416 : log_flush();
298 :
299 2416 : tommy_list_foreach(&high, free);
300 2416 : tommy_list_foreach(&low, free);
301 : }
302 :
303 1103806 : int state_thermal_alarm(struct snapraid_state* state)
304 : {
305 : /* if no limit, there is no thermal support */
306 1103806 : if (state->thermal_temperature_limit == 0)
307 1101390 : return 0;
308 :
309 2416 : if (state->thermal_highest_temperature <= state->thermal_temperature_limit)
310 2416 : return 0;
311 :
312 0 : return 1;
313 : }
314 :
315 0 : void state_thermal_cooldown(struct snapraid_state* state)
316 : {
317 0 : int sleep_time = state->thermal_cooldown_time;
318 :
319 0 : if (sleep_time == 0)
320 0 : sleep_time = 5 * 60; /* default sleep time */
321 0 : if (sleep_time < 3 * 60)
322 0 : sleep_time = 3 * 60; /* minimum sleep time */
323 :
324 : /* from now on, stop any further data gathering as the heating is interrupted */
325 0 : state->thermal_stop_gathering = 1;
326 :
327 0 : log_tag("thermal:spindown\n");
328 0 : state_device(state, DEVICE_DOWN, 0);
329 :
330 0 : msg_progress("Cooldown...\n");
331 :
332 0 : log_tag("thermal:cooldown:%d\n", sleep_time);
333 0 : printf("Waiting for %d minutes...\n", sleep_time / 60);
334 :
335 0 : log_flush();
336 :
337 : /* every 30 seconds spin down any disk that was spunup */
338 0 : while (sleep_time > 0) {
339 0 : state_device(state, DEVICE_DOWNIFUP, 0);
340 :
341 0 : sleep(30);
342 0 : sleep_time -= 30;
343 : }
344 :
345 0 : if (!global_interrupt) { /* don't wake-up if we are interrupting */
346 0 : log_tag("thermal:spinup\n");
347 :
348 : /* spinup */
349 0 : state_device(state, DEVICE_UP, 0);
350 :
351 : /* log new thermal info */
352 0 : state_thermal(state, 0);
353 : }
354 0 : }
355 :
356 235 : int state_thermal_begin(struct snapraid_state* state, time_t now)
357 : {
358 235 : if (state->thermal_temperature_limit == 0)
359 234 : return 1;
360 :
361 : /* initial thermal measure */
362 1 : state->thermal_first = now;
363 1 : state->thermal_latest = now;
364 1 : state_thermal(state, now);
365 :
366 1 : if (state->thermal_ambient_temperature != 0) {
367 1 : printf("System temperature is %u degrees\n", state->thermal_ambient_temperature);
368 :
369 1 : if (state->thermal_temperature_limit != 0 && state->thermal_temperature_limit <= state->thermal_ambient_temperature) {
370 : /* LCOV_EXCL_START */
371 : log_fatal(EENVIRONMENT, "DANGER! System temperature of %d degrees is higher than the temperature limit of %d degrees. Unable to proceed!\n", state->thermal_ambient_temperature, state->thermal_temperature_limit);
372 : log_flush();
373 : return 0;
374 : /* LCOV_EXCL_STOP */
375 : }
376 : }
377 :
378 1 : if (state_thermal_alarm(state)) {
379 : /* LCOV_EXCL_START */
380 : log_fatal(EENVIRONMENT, "DANGER! Hard disk temperature of %d degrees is already outside the operating range. Unable to proceed!\n", state->thermal_highest_temperature);
381 : log_flush();
382 : return 0;
383 : /* LCOV_EXCL_STOP */
384 : }
385 :
386 1 : return 1;
387 : }
388 :
|