/
thermalmanager.c
568 lines (462 loc) · 17.1 KB
/
thermalmanager.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/**
@file thermalmanager.c
This file implements part of the device thermal management policy
by providing the current thermal state for interested sw components.
<p>
Copyright (C) 2009-2010 Nokia Corporation
@author Semi Malinen <semi.malinen@nokia.com>
This file is part of Dsme.
Dsme is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License
version 2.1 as published by the Free Software Foundation.
Dsme is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with Dsme. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* An example command line to obtain thermal state over D-Bus:
* $ dbus-send --system --print-reply --dest=com.nokia.thermalmanager /com/nokia/thermalmanager com.nokia.thermalmanager.get_thermal_state
*
* TODO:
* - use a single timer for all thermal objects
* i.e. use the shortest interval of all thermal objects
*/
#define _GNU_SOURCE
#include "thermalmanager.h"
#include <iphbd/iphb_internal.h>
#include "dbusproxy.h"
#include "dsme_dbus.h"
#include "dsme/modules.h"
#include "dsme/modulebase.h"
#include "dsme/logging.h"
#include "heartbeat.h"
#include <dsme/state.h>
#include <dsme/thermalmanager_dbus_if.h>
#include <glib.h>
#include <stdlib.h>
#include <string.h>
static void receive_temperature_response(thermal_object_t* thermal_object,
int temperature);
static void thermal_object_polling_interval_expired(void* object);
#ifdef DSME_THERMAL_TUNING
static void thermal_object_try_to_read_config(thermal_object_t* thermal_object);
static thermal_object_t* thermal_object_copy(
const thermal_object_t* thermal_object);
#endif
#ifdef DSME_THERMAL_LOGGING
static void log_temperature(int temperature, const thermal_object_t* thermal_object);
#endif
static module_t* this_module = 0;
static GSList* thermal_objects = 0;
static const char* const service = thermalmanager_service;
static const char* const interface = thermalmanager_interface;
static const char* const path = thermalmanager_path;
static THERMAL_STATUS current_status = THERMAL_STATUS_NORMAL;
#ifdef DSME_THERMAL_TUNING
static bool is_in_ta_test = false;
#endif
static const char* const thermal_status_name[] = {
"low-temp-warning", "normal", "warning", "alert", "fatal"
};
static const char* current_status_name()
{
return thermal_status_name[current_status];
}
static THERMAL_STATUS worst_current_thermal_object_status(void)
{
THERMAL_STATUS overall_status = THERMAL_STATUS_NORMAL;
THERMAL_STATUS highest_status = THERMAL_STATUS_NORMAL;
THERMAL_STATUS lowest_status = THERMAL_STATUS_NORMAL;
GSList* node;
for (node = thermal_objects; node != 0; node = g_slist_next(node)) {
/* Find highest status */
if (((thermal_object_t*)(node->data))->status > highest_status) {
highest_status = ((thermal_object_t*)(node->data))->status;
}
/* Find lowest status */
if (((thermal_object_t*)(node->data))->status < lowest_status) {
lowest_status = ((thermal_object_t*)(node->data))->status;
}
}
/* Decide overall status */
/* If we have any ALERT of FATAL then that decides overall status */
/* During LOW, NORMAL or WARNING, any LOW wins */
/* Else status is highest */
if (highest_status >= THERMAL_STATUS_ALERT) {
overall_status = highest_status;
} else if (lowest_status == THERMAL_STATUS_LOW) {
overall_status = THERMAL_STATUS_LOW;
} else {
overall_status = highest_status;
}
return overall_status;
}
static void send_thermal_status(dsme_thermal_status_t status,
const char *sensor_name, int temperature)
{
DSM_MSGTYPE_SET_THERMAL_STATUS msg =
DSME_MSG_INIT(DSM_MSGTYPE_SET_THERMAL_STATUS);
msg.status = status;
msg.temperature = temperature;
strncpy(msg.sensor_name, sensor_name, DSM_TEMP_SENSOR_MAX_NAME_LEN);
msg.sensor_name[DSM_TEMP_SENSOR_MAX_NAME_LEN - 1] = 0;
broadcast_internally(&msg);
}
static void send_thermal_indication(const char *sensor_name, int temperature)
{
/* first send an indication to D-Bus */
{
DsmeDbusMessage* sig =
dsme_dbus_signal_new(path,
interface,
thermalmanager_state_change_ind);
dsme_dbus_message_append_string(sig, current_status_name());
dsme_dbus_signal_emit(sig);
dsme_log(LOG_NOTICE, "thermalmanager: Device (%s) thermal status: %s (%dC)", sensor_name, current_status_name(), temperature);
}
/* then broadcast an indication internally */
{
static bool temp_warning_sent = false;
if (current_status == THERMAL_STATUS_FATAL) {
send_thermal_status(DSM_THERMAL_STATUS_OVERHEATED, sensor_name, temperature);
temp_warning_sent = true;
dsme_log(LOG_CRIT, "thermalmanager: Device (%s) overheated (%dC)", sensor_name, temperature);
} else if (current_status == THERMAL_STATUS_LOW) {
send_thermal_status(DSM_THERMAL_STATUS_LOWTEMP, sensor_name, temperature);
temp_warning_sent = true;
dsme_log(LOG_WARNING, "thermalmanager: Device (%s) temperature low (%dC)", sensor_name, temperature);
} else if (temp_warning_sent) {
send_thermal_status(DSM_THERMAL_STATUS_NORMAL, sensor_name, temperature);
temp_warning_sent = false;
dsme_log(LOG_NOTICE, "thermalmanager: Device (%s) temperature back to normal (%dC)", sensor_name, temperature);
}
}
}
static void send_temperature_request(thermal_object_t* thermal_object)
{
if (!thermal_object->request_pending) {
dsme_log(LOG_DEBUG,
"thermalmanager: requesting %s temperature",
thermal_object->conf->name);
thermal_object->request_pending = true;
if (!thermal_object->conf->request_temperature(
thermal_object,
receive_temperature_response))
{
thermal_object->request_pending = false;
dsme_log(LOG_DEBUG,
"thermalmanager: error requesting %s temperature",
thermal_object->conf->name);
}
} else {
dsme_log(LOG_DEBUG,
"thermalmanager: still waiting for %s temperature",
thermal_object->conf->name);
}
}
static void receive_temperature_response(thermal_object_t* thermal_object,
int temperature)
{
thermal_object->request_pending = false;
if (temperature == INVALID_TEMPERATURE) {
dsme_log(LOG_DEBUG,
"thermalmanager: %s temperature request failed",
thermal_object->conf->name);
return;
}
THERMAL_STATUS previous_status = thermal_object->status;
THERMAL_STATUS new_status = thermal_object->status;
#ifdef DSME_THERMAL_TUNING
if (is_in_ta_test) {
thermal_object_try_to_read_config(thermal_object);
}
#endif
/* heuristics to convert to degrees C */
if (temperature > 1000) {
/* convert from millidegrees to degrees */
temperature = temperature / 1000;
}
if (temperature > 223) { /* 223 K ~ -50 degrees C */
/* convert from kelvin to degrees celsius */
temperature = temperature - 273;
}
/* figure out the new thermal object status based on the temperature */
if (temperature < thermal_object->conf->state[new_status].min) {
while (new_status > THERMAL_STATUS_LOW &&
temperature < thermal_object->conf->state[new_status].min)
{
--new_status;
}
} else if (temperature > thermal_object->conf->state[new_status].max) {
while (new_status < THERMAL_STATUS_FATAL &&
temperature > thermal_object->conf->state[new_status].max)
{
++new_status;
}
}
thermal_object->status = new_status;
#ifndef DSME_THERMAL_LOGGING
dsme_log(LOG_DEBUG,
"thermalmanager: %s temperature: %d %s",
thermal_object->conf->name,
temperature,
thermal_status_name[thermal_object->status]);
#endif
if (new_status != previous_status) {
/* thermal object status has changed*/
/* see if the new status affects global thermal status */
THERMAL_STATUS previously_indicated_status = current_status;
current_status = worst_current_thermal_object_status();
if (current_status != previously_indicated_status) {
/* global thermal status has changed; send indication */
send_thermal_indication(thermal_object->conf->name, temperature);
}
}
#ifdef DSME_THERMAL_LOGGING
log_temperature(temperature, thermal_object);
#endif
}
static void thermal_object_polling_interval_expired(void* object)
{
thermal_object_t* thermal_object = object;
send_temperature_request(thermal_object);
// set up heartbeat service to poll the temperature of the object
const thermal_status_configuration_t* conf =
&thermal_object->conf->state[thermal_object->status];
DSM_MSGTYPE_WAIT msg = DSME_MSG_INIT(DSM_MSGTYPE_WAIT);
msg.req.mintime = conf->mintime;
msg.req.maxtime = conf->maxtime;
msg.req.pid = 0;
msg.data = thermal_object;
broadcast_internally(&msg);
}
void dsme_register_thermal_object(thermal_object_t* thermal_object)
{
dsme_log(LOG_DEBUG,
"thermalmanager: %s (%s)", __FUNCTION__,
thermal_object->conf->name);
enter_module(this_module);
#ifdef DSME_THERMAL_TUNING
thermal_object = thermal_object_copy(thermal_object);
thermal_object_try_to_read_config(thermal_object);
#endif
// add the thermal object to the list of know thermal objects
thermal_objects = g_slist_append(thermal_objects, thermal_object);
thermal_object_polling_interval_expired(thermal_object);
leave_module();
}
void dsme_unregister_thermal_object(thermal_object_t* thermal_object)
{
dsme_log(LOG_DEBUG, "thermalmanager: %s(%s)", __FUNCTION__, thermal_object->conf->name);
// TODO
}
static void get_thermal_state(const DsmeDbusMessage* request,
DsmeDbusMessage** reply)
{
*reply = dsme_dbus_reply_new(request);
dsme_dbus_message_append_string(*reply, current_status_name());
}
static const dsme_dbus_binding_t methods[] = {
{ get_thermal_state, thermalmanager_get_thermal_state },
{ 0, 0 }
};
static bool bound = false;
DSME_HANDLER(DSM_MSGTYPE_WAKEUP, client, msg)
{
thermal_object_t* thermal_object = (thermal_object_t*)(msg->data);
dsme_log(LOG_DEBUG,
"thermalmanager: check thermal object '%s'",
thermal_object->conf->name);
thermal_object_polling_interval_expired(thermal_object);
}
DSME_HANDLER(DSM_MSGTYPE_DBUS_CONNECT, client, msg)
{
dsme_log(LOG_DEBUG, "thermalmanager: DBUS_CONNECT");
dsme_dbus_bind_methods(&bound, methods, service, interface);
}
DSME_HANDLER(DSM_MSGTYPE_DBUS_DISCONNECT, client, msg)
{
dsme_log(LOG_DEBUG, "thermalmanager: DBUS_DISCONNECT");
dsme_dbus_unbind_methods(&bound, methods, service, interface);
}
#ifdef DSME_THERMAL_TUNING
DSME_HANDLER(DSM_MSGTYPE_SET_TA_TEST_MODE, client, msg)
{
is_in_ta_test = true;
dsme_log(LOG_NOTICE, "thermalmanager: set TA test mode");
}
#endif
// TODO: rename module_fn_info_t to dsme_binding_t
module_fn_info_t message_handlers[] = {
DSME_HANDLER_BINDING(DSM_MSGTYPE_WAKEUP),
DSME_HANDLER_BINDING(DSM_MSGTYPE_DBUS_CONNECT),
DSME_HANDLER_BINDING(DSM_MSGTYPE_DBUS_DISCONNECT),
#ifdef DSME_THERMAL_TUNING
DSME_HANDLER_BINDING(DSM_MSGTYPE_SET_TA_TEST_MODE),
#endif
{ 0 }
};
void module_init(module_t* handle)
{
dsme_log(LOG_DEBUG, "thermalmanager.so loaded");
this_module = handle;
}
void module_fini(void)
{
g_slist_free(thermal_objects);
dsme_dbus_unbind_methods(&bound, methods, service, interface);
dsme_log(LOG_DEBUG, "thermalmanager.so unloaded");
}
#ifdef DSME_THERMAL_TUNING
#include <stdio.h>
/* Thermal values can be configured in file /etc/dsme/temp_<name>.conf */
#define DSME_THERMAL_TUNING_CONF_PATH "/etc/dsme/temp_"
static FILE* thermal_tuning_file(const char* thermal_object_name)
{
char name[1024];
snprintf(name,
sizeof(name),
"%s%s.conf",
DSME_THERMAL_TUNING_CONF_PATH,
thermal_object_name);
dsme_log(LOG_DEBUG, "thermalmanager: trying to open %s for thermal tuning values", name);
return fopen(name, "r");
}
static bool thermal_object_config_read(
thermal_object_configuration_t* config,
FILE* f)
{
bool success = true;
int i;
thermal_object_configuration_t new_config;
new_config = *config;
for (i = 0; i < THERMAL_STATUS_COUNT; ++i) {
if (fscanf(f,
"%d, %d, %d",
&new_config.state[i].min,
&new_config.state[i].max,
&new_config.state[i].maxtime) != 3) {
success = false;
}
if (success) {
/* Do some sanity checking for values
* Temp values should be between -40..+200, and in ascending order.
* Min must be < max
* Next min <= previous max
* Polling times should also make sense 10-1000s
*/
if (((i > THERMAL_STATUS_LOW) && (new_config.state[i].min < -40)) ||
(new_config.state[i].max < -40) ||
(new_config.state[i].min > 200) ||
((i > THERMAL_STATUS_FATAL) && (new_config.state[i].max > 200)) ||
(new_config.state[i].min >= new_config.state[i].max) ||
((i > THERMAL_STATUS_LOW) && (new_config.state[i].min <= new_config.state[i-1].min)) ||
((i > THERMAL_STATUS_LOW) && (new_config.state[i].max <= new_config.state[i-1].max)) ||
((i > THERMAL_STATUS_LOW) && (new_config.state[i].min > new_config.state[i-1].max)) ||
(new_config.state[i].maxtime < 10) ||
(new_config.state[i].maxtime > 1000)) {
success = false;
}
}
if (success) {
/* Note, it is important to give big enough window min..max
* then IPHB can freely choose best wake-up time
*/
new_config.state[i].mintime = new_config.state[i].maxtime/2;
} else {
dsme_log(LOG_ERR, "thermalmanager: syntax error in thermal tuning on line %d", i+1);
break;
}
}
if (success) {
*config = new_config;
}
return success;
}
static void thermal_object_try_to_read_config(thermal_object_t* thermal_object)
{
FILE* f;
if ((f = thermal_tuning_file(thermal_object->conf->name))) {
if (thermal_object_config_read(thermal_object->conf, f)) {
dsme_log(LOG_NOTICE,
"thermalmanager: Read thermal tuning file for %s",
thermal_object->conf->name);
} else {
dsme_log(LOG_NOTICE,
"thermalmanager: Thermal tuning file for %s discarded. Using default values",
thermal_object->conf->name);
}
fclose(f);
} else {
dsme_log(LOG_NOTICE,
"thermalmanager: No thermal tuning file for %s. Using default values",
thermal_object->conf->name);
}
}
static thermal_object_t* thermal_object_copy(
const thermal_object_t* thermal_object)
{
thermal_object_t* copy_object;
thermal_object_configuration_t* copy_config;
copy_object = malloc(sizeof(thermal_object_t));
copy_config = malloc(sizeof(thermal_object_configuration_t));
if (copy_object && copy_config) {
*copy_object = *thermal_object;
*copy_config = *(thermal_object->conf);
copy_object->conf = copy_config;
} else {
free(copy_object);
free(copy_config);
copy_object = 0;
}
return copy_object;
}
#endif
#ifdef DSME_THERMAL_LOGGING
#include <stdio.h>
#include <string.h>
#include <errno.h>
#define DSME_THERMAL_LOG_PATH "/var/lib/dsme/thermal.log"
static const char* status_string(THERMAL_STATUS status)
{
switch (status) {
case THERMAL_STATUS_LOW: return "LOW_WARNING";
case THERMAL_STATUS_NORMAL: return "NORMAL";
case THERMAL_STATUS_WARNING: return "WARNING";
case THERMAL_STATUS_ALERT: return "ALERT";
case THERMAL_STATUS_FATAL: return "FATAL";
default: return "UNKNOWN";
}
}
static void log_temperature(int temperature, const thermal_object_t* thermal_object)
{
static FILE* log_file = 0;
if (!log_file) {
if (!(log_file = fopen(DSME_THERMAL_LOG_PATH, "a"))) {
dsme_log(LOG_ERR,
"thermalmanager: Error opening thermal log " DSME_THERMAL_LOG_PATH ": %s",
strerror(errno));
return;
}
}
struct timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
int now = t.tv_sec;
static int start_time = 0;
if (!start_time) {
start_time = now;
}
fprintf(log_file,
"%d %s %d C %s\n",
now - start_time,
thermal_object->conf->name,
temperature,
status_string(thermal_object->status));
fflush(log_file);
dsme_log(LOG_DEBUG,"thermalmanager: %s %d C %s", thermal_object->conf->name,
temperature, status_string(thermal_object->status));
}
#endif