summaryrefslogtreecommitdiff
path: root/src/dtpm/dtpm.c
blob: 749f0f07ea67579b5206c9f396c27aee370c8c70 (plain)
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
#define _GNU_SOURCE
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/stat.h>
#include <sys/timerfd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <limits.h>
#include <unistd.h>
#include <dirent.h>
#include <mntent.h>
#include <pwd.h>

#include "log.h"
#include "conf.h"
#include "mainloop.h"
#include "display.h"
#include "thermal.h"

#define MAX_EVENTS 10
#define DTPM_CHILDREN_MAX 16
#define DTPM_CONSTRAINT_MAX 1

typedef unsigned long long u64;

struct dtpm_field {
	const char *filename;
	const char *fmt;
	union {
		u64 u64val;
		char name[MAXNAMLEN];
	};
	void *ptr;
};

struct dtpm_constraint {
	struct dtpm_field name;
	struct dtpm_field power_limit_uw;
	struct dtpm_field time_window_us;
	struct dtpm_field max_power_uw;
};

struct dtpm_node {
	int level;
	char *path;
	struct dtpm_node *parent;
	struct dtpm_node *children[DTPM_CHILDREN_MAX];
	struct dtpm_constraint constraints[DTPM_CONSTRAINT_MAX];
	struct dtpm_field max_power_range_uw;
	struct dtpm_field power_uw;
	struct dtpm_field name;
	struct thermal_zone *tz;
};

struct cb_param {
	struct thermal_handler *th;
	struct thermal_zone *tz;
	struct dtpm_config *cfg;
	struct dtpm_node *root;
	int line;
};

typedef int (*filter_t)(const char *);
typedef int (*dtpm_file_cb_t)(const char *, void *);
typedef int (*dtpm_node_cb_t)(struct dtpm_node *, void *);

static int dtpm_file_rw(const char *filename, int rw, const char *fmt, ...)
{
	const char *mode = rw == O_RDONLY ? "r" : "w";
 	FILE *f;
        va_list args;
	int ret;

	f = fopen(filename, mode);
	if (!f) {
		ERROR("Failed to open %s:%m\n", filename);
		return -1;
	}

	va_start (args, fmt);
	ret = rw == O_RDONLY ? vfscanf(f, fmt, args) : vfprintf(f, fmt, args);
	va_end(args);

	fclose(f);

	return ret == EOF;
}

int dtpm_node_add_child(struct dtpm_node *node, struct dtpm_node *child)
{
	int i;

	if (!node)
		return 0;
	
	for (i = 0; i < DTPM_CHILDREN_MAX; i++) {

		if (node->children[i])
			continue;

		node->children[i] = child;

		return 0;
	}

	return -1;
}

struct dtpm_node *dtpm_node_alloc(void)
{
	struct dtpm_node *node;
	int i;

	node = malloc(sizeof(*node));
	if (!node)
		return NULL;

	memset(node, 0, sizeof(*node));

	node->name.filename = "name";
	node->name.fmt = "%s";
	node->name.ptr = node->name.name;

	node->power_uw.filename = "power_uw";
	node->power_uw.fmt = "%llu";
	node->power_uw.ptr = &node->power_uw.u64val;

	node->max_power_range_uw.filename = "max_power_range_uw";
	node->max_power_range_uw.fmt = "%llu";
	node->max_power_range_uw.ptr = &node->max_power_range_uw.u64val;

	for (i = 0; i < DTPM_CONSTRAINT_MAX; i++) {
		node->constraints[i].name.filename = "constraint_0_name";
		node->constraints[i].name.fmt = "%s";
		node->constraints[i].name.ptr = node->constraints[i].name.name;

		node->constraints[i].power_limit_uw.filename = "constraint_0_power_limit_uw";
		node->constraints[i].power_limit_uw.fmt = "%llu";
		node->constraints[i].power_limit_uw.ptr = &node->constraints[i].power_limit_uw.u64val;

		node->constraints[i].time_window_us.filename = "constraint_0_time_window_us";
		node->constraints[i].time_window_us.fmt = "%llu";
		node->constraints[i].time_window_us.ptr = &node->constraints[i].time_window_us.u64val;

		node->constraints[i].max_power_uw.filename = "constraint_0_max_power_uw";
		node->constraints[i].max_power_uw.fmt = "%llu";
		node->constraints[i].max_power_uw.ptr = &node->constraints[i].max_power_uw.u64val;
	}
	
	return node;
}

void dtpm_node_free(struct dtpm_node *node)
{
	free(node);
}

#define HASHMASK 0x00FF
#if 0
static unsigned int hash(const char* key)
{
	unsigned int i;
	unsigned int h;

	h = 1315423911;
	for (i = 0; *key; key++, i++) {
		h ^= ((h << 5) + (*key) + (h >> 2));
	}
	h &= HASHMASK;

	return h;
}
#endif

int for_each_dtpm_node(struct dtpm_node *node, dtpm_node_cb_t cb, void *data)
{
	int i, ret;

	ret = cb(node, data);
	
	for (i = 0; i < DTPM_CHILDREN_MAX; i++) {

		if (!node->children[i])
			continue;

		ret += for_each_dtpm_node(node->children[i], cb, data);
	}

	return ret;
}

static int dtpm_read_field(struct dtpm_node *node, struct dtpm_field *field)
{
	char path[PATH_MAX];

	sprintf(path, "%s/%s", node->path, field->filename);
	
	return dtpm_file_rw(path, O_RDONLY, field->fmt, field->ptr);
}

int dtpm_read(struct dtpm_node *node)
{
	int i;
	int ret = 0;
	
	ret += dtpm_read_field(node, &node->max_power_range_uw);
	ret += dtpm_read_field(node, &node->power_uw);
	ret += dtpm_read_field(node, &node->name);

	for (i = 0; i < DTPM_CONSTRAINT_MAX; i++) {
		ret += dtpm_read_field(node, &node->constraints[i].name);
		ret += dtpm_read_field(node, &node->constraints[i].power_limit_uw);
		ret += dtpm_read_field(node, &node->constraints[i].max_power_uw);
	}

	return ret;
}

int dtpm_init(const char *dirname, struct dtpm_node *node)
{
	node->path = strdup(dirname);
	if (!node->path) {
		fprintf(stderr, "Failed to strdup '%s': %m\n", dirname);
		return -1;
	}

	if (dtpm_read(node) < 0) {
		fprintf(stderr, "Failed to read node information '%s'\n", dirname);
		return -1;
	}
	
	return 0;
}

struct dtpm_node *dtpm_build_tree(const char *dirname, struct dtpm_node *parent)
{
	DIR *dir;
	int ret = -1;
	struct dirent *direntp;
	struct dtpm_node *new_node;

	DEBUG("Building tree from '%s'\n", dirname);
	
        dir = opendir(dirname);
        if (!dir) {
		ERROR("Failed to open directory '%s': %m\n", dirname);
		return NULL;
	}

	new_node = dtpm_node_alloc();
	if (!new_node) {
		ERROR("Failed to allocated node\n");
		goto out_close_dir;
	}

	new_node->parent = parent;
	new_node->level = parent ? parent->level + 1 : 0;
	
	ret = dtpm_init(dirname, new_node);
	if (ret)
		goto out_dtpm_node_free;

	while ((direntp = readdir(dir)) != NULL) {

		char *subdir;
		struct dtpm_node *child;

		if (strncmp(direntp->d_name, "dtpm", strlen("dtpm")))
			continue;
		
		if (asprintf(&subdir, "%s/%s", dirname, direntp->d_name) == -1) {
			fprintf(stderr, "Failed to allocate string: %m\n");
			goto out_close_dir;
		}
			
		child = dtpm_build_tree(subdir, new_node);
	
		if (!child) {
			fprintf(stderr, "Failed to build '%s': %m\n", subdir);
			free(subdir);
			goto out_dtpm_node_free;
		}

		free(subdir);
			
		ret = dtpm_node_add_child(new_node, child);
		if (ret) {
			fprintf(stderr, "Failed to add new node\n");
			goto out_dtpm_node_free;
		}
	}

out_close_dir:
	closedir(dir);
out_dtpm_node_free:
	if (ret)
		dtpm_node_free(new_node);

	return ret ? NULL : new_node;
}

static int tz_create(const char *name, int tz_id, __maybe_unused void *arg)
{
	printf("Thermal zone '%s'/%d created\n", name, tz_id);

	return 0;
}

static int tz_delete(int tz_id, __maybe_unused void *arg)
{
	printf("Thermal zone %d deleted\n", tz_id);

	return 0;
}

static int tz_disable(int tz_id, __maybe_unused void *arg)
{
	printf("Thermal zone %d disabled\n", tz_id);

	return 0;
}

static int tz_enable(int tz_id, __maybe_unused void *arg)
{
	printf("Thermal zone %d enabled\n", tz_id);

	return 0;
}

static int trip_high(int tz_id, int trip_id, int temp, __maybe_unused void *arg)
{
	struct cb_param *cb_param = arg;
	struct thermal_zone *tz = cb_param->tz;
	
/*	printf("Thermal zone %d: trip point %d (%d mC°) crossed way up with %d °C\n",
	tz_id, trip_id, tz[tz_id].trip[trip_id].temp, temp); */

	return 0;
}

static int trip_low(int tz_id, int trip_id, int temp, __maybe_unused void *arg)
{
	struct cb_param *cb_param = arg;
	struct thermal_zone *tz = cb_param->tz;
	
	/* printf("Thermal zone %d: trip point %d (%d mC°) crossed way down with %d °C\n",
	   tz_id, trip_id, tz[tz_id].trip[trip_id].temp, temp); */

	return 0;
}

static int trip_add(int tz_id, int trip_id, int type, int temp, int hyst, __maybe_unused void *arg)
{
	printf("Trip point added %d: id=%d, type=%d, temp=%d, hyst=%d\n",
	       tz_id, trip_id, type, temp, hyst);

	return 0;
}

static int trip_delete(int tz_id, int trip_id, __maybe_unused void *arg)
{
	printf("Trip point deleted %d: id=%d\n", tz_id, trip_id);

	return 0;
}

static int trip_change(int tz_id, int trip_id, int type, int temp, int hyst, __maybe_unused void *arg)
{
	printf("Trip point changed %d: id=%d, type=%d, temp=%d, hyst=%d\n",
	       tz_id, trip_id, type, temp, hyst);

	return 0;
}

static int cdev_add(const char *name, int cdev_id, int max_state, __maybe_unused void *arg)
{
	printf("Cooling device '%s'/%d (max state=%d) added",
	       name, cdev_id, max_state);
	
	return 0;
}

static int cdev_delete(int cdev_id, __maybe_unused void *arg)
{
	printf("Cooling device %d deleted", cdev_id);

	return 0;
}

static int cdev_update(int cdev_id, int cur_state, __maybe_unused void *arg)
{
	struct cb_param *cb_param = arg;
	char buffer[4096];

	snprintf(buffer, sizeof(buffer) - 1, "cdev:%d state:%d",
		 cdev_id, cur_state);
	
	display_reset_cursor(15 + cdev_id);

	display_print_line(0, 0, buffer, 0, arg);
	
	display_refresh_pad(0);

	return 0;
}

static int gov_change(int tz_id, const char *name, __maybe_unused void *arg)
{
	printf("tz %d, governor=%s\n", tz_id, name);

	return 0;
}

static struct thermal_ops ops = {
	.events.tz_create	= tz_create,
	.events.tz_delete	= tz_delete,
	.events.tz_disable	= tz_disable,
	.events.tz_enable	= tz_enable,
	.events.trip_high	= trip_high,
	.events.trip_low	= trip_low,
	.events.trip_add	= trip_add,
	.events.trip_delete	= trip_delete,
	.events.trip_change	= trip_change,
	.events.cdev_add	= cdev_add,
	.events.cdev_delete	= cdev_delete,
	.events.cdev_update	= cdev_update,
	.events.gov_change	= gov_change
};

int show_power_uw(struct dtpm_node *node, void *arg)
{
	struct cb_param *cb_param = arg;
	struct thermal_handler *th = cb_param->th;
	char buffer[4096];
	size_t len;

	dtpm_read_field(node, &node->power_uw);

	len = snprintf(buffer, sizeof(buffer) - 1, "%*s%s: power=%llu uW", node->level, "",
		       node->name.name, node->power_uw.u64val);

	if (node->tz) {
		if (thermal_cmd_get_temp(th, node->tz) < 0)
			return -1;
		
		snprintf(buffer + len, sizeof(buffer) + len - 1, " / temp=%d mC°", node->tz->temp);
	}

	display_print_line(0, cb_param->line++, buffer, 0, arg);

	return 0;
}

int tz_get_temp(struct thermal_zone *tz, void *arg)
{
	struct cb_param *cb_param = arg;
	struct thermal_handler *th = cb_param->th;

	return thermal_cmd_get_temp(th, tz);
}

static int event_callback(__maybe_unused int fd, void *arg)
{
	struct cb_param *cb_param = arg;
	struct thermal_handler *th = cb_param->th;

	return thermal_events_handle(th, arg);
}

static int timer_callback(int fd, void *arg)
{
	struct cb_param *cb_param = arg;
	struct dtpm_node *root = cb_param->root;
	struct thermal_zone *tz = cb_param->tz;
	char buf[8];

	cb_param->line = 3;

	display_reset_cursor(0);

	for_each_thermal_zone(tz, tz_get_temp, arg);
	
	if (for_each_dtpm_node(root, show_power_uw, arg))
		return -1;

	read(fd, buf, sizeof(buf));

	display_refresh_pad(0);
	
	return 0;
}

static int dtpm_bind_thermal_zone(struct dtpm_node *node, void *arg)
{
	struct cb_param *cbp = arg;
	struct thermal_zone *tz;
	const char *name;

	name = dtpm_get_thermal_zone(cbp->cfg, node->name.ptr);
	if (!name)
		return 0;

	tz = thermal_zone_find_by_name(cbp->tz, name);
	if (!tz)
		return 0;

	node->tz = tz;
	
	DEBUG("'%s' belongs to the thermal zone '%s'\n",
	      (char *)node->name.ptr, name);

	return 0;
}

static struct timespec sec_to_timespec(float delay)
{
	struct timespec tv = {
		.tv_sec = (time_t)delay,
		.tv_nsec = (delay - tv.tv_sec) * 1000000000
	};

	return tv;
}

int main(void)
{
	struct cb_param cb_param;
	struct dtpm_node *root;
	struct thermal_zone *tz;
	struct thermal_handler *th;
	struct itimerspec timer_it = { 0 };
	struct dtpm_config *cfg;
	char *config_path = NULL;
	float delay = 0.5;
	int timerfd;
	
	/*
	if (getuid()) {
		CRITICAL("Needs root privileges\n");
		return -1;
	}
	*/

	cfg = dtpm_config(config_path);
	if (!cfg)
		WARN("No configuration file\n");

	root = dtpm_build_tree(dtpm_path(), NULL);
	if (!root)
		return -1;

	th = thermal_init(&ops);
	if (!th)
		return -1;

	tz = thermal_zone_discover(th);
	if (!tz)
		return -1;

	cb_param.cfg = cfg;
	cb_param.root = root;
	cb_param.th = th;
	cb_param.tz = tz;

	if (for_each_dtpm_node(root, dtpm_bind_thermal_zone, &cb_param))
		return -1;

	if (mainloop_init())
		return -1;
	
	timerfd = timerfd_create(CLOCK_MONOTONIC, 0);
	if (timerfd < 0)
		return -1;

	timer_it.it_interval = timer_it.it_value = sec_to_timespec(delay);

	if (timerfd_settime(timerfd, 0, &timer_it, NULL) < 0)
		return -1;
	
	if (mainloop_add(timerfd, timer_callback, &cb_param))
		return -1;

	if (mainloop_add(thermal_events_fd(th), event_callback, &cb_param))
		return -1;
	
	if (display_init())
		return -1;

	return mainloop(-1);
}