Skip to contentSkip to conversation

tools/lib/thermal: Add a thermal library

RFC PATCH In review Daniel Lezcano 2 participants 10 messages Feb 14, 2022 – Feb 16, 2022 Thermal

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/4 tools/lib/thermal: Add a thermal library

The thermal framework implements a netlink notification mechanism to be used by the userspace to have a thermal configuration discovery, trip point changes or violation, cooling device changes notifications, etc...

This library provides a level of abstraction for the thermal netlink notification allowing the userspace to connect to the notification mechanism more easily. The library is callback oriented.

---
 tools/Makefile                           |  14 +-
 tools/lib/thermal/.gitignore             |   2 +
 tools/lib/thermal/Build                  |   5 +
 tools/lib/thermal/Makefile               | 165 +++++++++++
 tools/lib/thermal/commands.c             | 339 +++++++++++++++++++++++
 tools/lib/thermal/events.c               | 153 ++++++++++
 tools/lib/thermal/include/thermal.h      | 129 +++++++++
 tools/lib/thermal/libthermal.map         |  25 ++
 tools/lib/thermal/libthermal.pc.template |  12 +
 tools/lib/thermal/sampling.c             |  64 +++++
 tools/lib/thermal/thermal.c              | 117 ++++++++
 tools/lib/thermal/thermal_nl.c           | 202 ++++++++++++++
 tools/lib/thermal/thermal_nl.h           |  43 +++
 13 files changed, 1268 insertions(+), 2 deletions(-)
 create mode 100644 tools/lib/thermal/.gitignore
 create mode 100644 tools/lib/thermal/Build
 create mode 100644 tools/lib/thermal/Makefile
 create mode 100644 tools/lib/thermal/commands.c
 create mode 100644 tools/lib/thermal/events.c
 create mode 100644 tools/lib/thermal/include/thermal.h
 create mode 100644 tools/lib/thermal/libthermal.map
 create mode 100644 tools/lib/thermal/libthermal.pc.template
 create mode 100644 tools/lib/thermal/sampling.c
 create mode 100644 tools/lib/thermal/thermal.c
 create mode 100644 tools/lib/thermal/thermal_nl.c
 create mode 100644 tools/lib/thermal/thermal_nl.h
tools/Makefile +12 -2
index db2f7b8ebed5..c253cbd27c06 100644
@@ -31,6 +31,7 @@ help:
 	@echo '  bootconfig             - boot config tool'
 	@echo '  spi                    - spi tools'
 	@echo '  tmon                   - thermal monitoring and tuning tool'
+	@echo '  thermal                - thermal library'
 	@echo '  tracing                - misc tracing tools'
 	@echo '  turbostat              - Intel CPU idle stats and freq reporting tool'
 	@echo '  usb                    - USB testing tools'
@@ -85,6 +86,9 @@ perf: FORCE
 selftests: FORCE
 	$(call descend,testing/$@)
 
+thermal: FORCE
+	$(call descend,lib/$@)
+
 turbostat x86_energy_perf_policy intel-speed-select: FORCE
 	$(call descend,power/x86/$@)
 
@@ -101,7 +105,7 @@ all: acpi cgroup counter cpupower gpio hv firewire \
 		perf selftests bootconfig spi turbostat usb \
 		virtio vm bpf x86_energy_perf_policy \
 		tmon freefall iio objtool kvm_stat wmi \
-		pci debugging tracing
+		pci debugging tracing thermal
 
 acpi_install:
 	$(call descend,power/$(@:_install=),install)
@@ -115,6 +119,9 @@ cgroup_install counter_install firewire_install gpio_install hv_install iio_inst
 selftests_install:
 	$(call descend,testing/$(@:_install=),install)
 
+thermal_install:
+	$(call descend,lib/$(@:_install=),install)
+
 turbostat_install x86_energy_perf_policy_install intel-speed-select_install:
 	$(call descend,power/x86/$(@:_install=),install)
 
@@ -160,6 +167,9 @@ perf_clean:
 selftests_clean:
 	$(call descend,testing/$(@:_clean=),clean)
 
+thermal_clean:
+	$(call descend,lib/thermal,clean)
+
 turbostat_clean x86_energy_perf_policy_clean intel-speed-select_clean:
 	$(call descend,power/x86/$(@:_clean=),clean)
 
@@ -177,6 +187,6 @@ clean: acpi_clean cgroup_clean counter_clean cpupower_clean hv_clean firewire_cl
 		vm_clean bpf_clean iio_clean x86_energy_perf_policy_clean tmon_clean \
 		freefall_clean build_clean libbpf_clean libsubcmd_clean \
 		gpio_clean objtool_clean leds_clean wmi_clean pci_clean firmware_clean debugging_clean \
-		intel-speed-select_clean tracing_clean
+		intel-speed-select_clean tracing_clean thermal_clean
 
 .PHONY: FORCE
tools/lib/thermal/.gitignore new file +2 -0
new file mode 100644
index 000000000000..5d2aeda80fea
@@ -0,0 +1,2 @@
+libthermal.so*
+libthermal.pc
tools/lib/thermal/Build new file +5 -0
new file mode 100644
index 000000000000..4a892d9e24f9
@@ -0,0 +1,5 @@
+libthermal-y += commands.o
+libthermal-y += events.o
+libthermal-y += thermal_nl.o
+libthermal-y += sampling.o
+libthermal-y += thermal.o
tools/lib/thermal/Makefile new file +165 -0

165 lines not on this page · raw message

tools/lib/thermal/commands.c new file +339 -0

339 lines not on this page · raw message

tools/lib/thermal/events.c new file +153 -0

153 lines not on this page · raw message

tools/lib/thermal/include/thermal.h new file +129 -0

129 lines not on this page · raw message

tools/lib/thermal/libthermal.map new file +25 -0
new file mode 100644
index 000000000000..d5e77738c7a4
@@ -0,0 +1,25 @@
+LIBTHERMAL_0.0.1 {
+	global:
+		thermal_init;
+		for_each_thermal_zone;
+		for_each_thermal_trip;
+		for_each_thermal_cdev;
+		thermal_zone_find_by_name;
+		thermal_zone_find_by_id;
+		thermal_zone_discover;
+		thermal_init;
+		thermal_events_init;
+		thermal_events_handle;
+		thermal_events_fd;
+		thermal_cmd_init;
+		thermal_cmd_get_tz;
+		thermal_cmd_get_cdev;
+		thermal_cmd_get_trip;
+		thermal_cmd_get_governor;
+		thermal_cmd_get_temp;
+		thermal_sampling_init;
+		thermal_sampling_handle;
+		thermal_sampling_fd;
+local:
+		*;
+};
tools/lib/thermal/libthermal.pc.template new file +12 -0
new file mode 100644
index 000000000000..6f3769731b59
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
+
+prefix=@PREFIX@
+libdir=@LIBDIR@
+includedir=${prefix}/include
+
+Name: libthermal
+Description: thermal library
+Requires: libnl-3.0 libnl-genl-3.0
+Version: @VERSION@
+Libs: -L${libdir} -lnl-genl-3 -lnl-3
+Cflags: -I${includedir} -I{include}/libnl3
tools/lib/thermal/sampling.c new file +64 -0

64 lines not on this page · raw message

tools/lib/thermal/thermal.c new file +117 -0

117 lines not on this page · raw message

tools/lib/thermal/thermal_nl.c new file +202 -0

202 lines not on this page · raw message

tools/lib/thermal/thermal_nl.h new file +43 -0

43 lines not on this page · raw message

Signature · 1 line
2.25.1
#1 lore raw

RFC PATCH 2/4 tools/thermal: Add util library

The next changes will provide a couple of tools using some common functions provided by this library.

It provides basic wrappers for:

- mainloop
- logging
- timestamp

---
 tools/thermal/lib/Build                       |   3 +
 tools/thermal/lib/Makefile                    | 158 ++++++++++++++++++
 .../thermal/lib/libthermal_tools.pc.template  |  12 ++
 tools/thermal/lib/log.c                       |  77 +++++++++
 tools/thermal/lib/log.h                       |  31 ++++
 tools/thermal/lib/mainloop.c                  | 135 +++++++++++++++
 tools/thermal/lib/mainloop.h                  |  14 ++
 tools/thermal/lib/thermal-tools.h             |  10 ++
 tools/thermal/lib/uptimeofday.c               |  40 +++++
 tools/thermal/lib/uptimeofday.h               |  12 ++
 10 files changed, 492 insertions(+)
 create mode 100644 tools/thermal/lib/Build
 create mode 100644 tools/thermal/lib/Makefile
 create mode 100644 tools/thermal/lib/libthermal_tools.pc.template
 create mode 100644 tools/thermal/lib/log.c
 create mode 100644 tools/thermal/lib/log.h
 create mode 100644 tools/thermal/lib/mainloop.c
 create mode 100644 tools/thermal/lib/mainloop.h
 create mode 100644 tools/thermal/lib/thermal-tools.h
 create mode 100644 tools/thermal/lib/uptimeofday.c
 create mode 100644 tools/thermal/lib/uptimeofday.h
tools/thermal/lib/Build new file +3 -0
new file mode 100644
index 000000000000..06f22760a272
@@ -0,0 +1,3 @@
+libthermal_tools-y += mainloop.o
+libthermal_tools-y += log.o
+libthermal_tools-y += uptimeofday.o
tools/thermal/lib/Makefile new file +158 -0

158 lines not on this page · raw message

tools/thermal/lib/libthermal_tools.pc.template new file +12 -0
new file mode 100644
index 000000000000..6f3769731b59
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
+
+prefix=@PREFIX@
+libdir=@LIBDIR@
+includedir=${prefix}/include
+
+Name: libthermal
+Description: thermal library
+Requires: libnl-3.0 libnl-genl-3.0
+Version: @VERSION@
+Libs: -L${libdir} -lnl-genl-3 -lnl-3
+Cflags: -I${includedir} -I{include}/libnl3
tools/thermal/lib/log.c new file +77 -0

77 lines not on this page · raw message

tools/thermal/lib/log.h new file +31 -0
new file mode 100644
index 000000000000..9ee945e0af57
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
+/* Copyright (C) 2022, Linaro Ltd - Daniel Lezcano <[email protected]> */
+#ifndef __THERMAL_TOOLS_LOG_H
+#define __THERMAL_TOOLS_LOG_H
+
+#include <syslog.h>
+
+#ifndef __maybe_unused
+#define __maybe_unused		__attribute__((__unused__))
+#endif
+
+#define TO_SYSLOG 0x1
+#define TO_STDOUT 0x2
+#define TO_STDERR 0x4
+
+extern void logit(int level, const char *format, ...);
+
+#define DEBUG(fmt, ...)		logit(LOG_DEBUG, "%s(%d): " fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__)
+#define INFO(fmt, ...)		logit(LOG_INFO, fmt, ##__VA_ARGS__)
+#define NOTICE(fmt, ...)	logit(LOG_NOTICE, fmt, ##__VA_ARGS__)
+#define WARN(fmt, ...)		logit(LOG_WARNING, fmt, ##__VA_ARGS__)
+#define ERROR(fmt, ...)		logit(LOG_ERR, fmt, ##__VA_ARGS__)
+#define CRITICAL(fmt, ...)	logit(LOG_CRIT, fmt, ##__VA_ARGS__)
+#define ALERT(fmt, ...)		logit(LOG_ALERT, fmt, ##__VA_ARGS__)
+#define EMERG(fmt, ...)		logit(LOG_EMERG, fmt, ##__VA_ARGS__)
+
+int log_init(int level, const char *ident, int options);
+int log_str2level(const char *lvl);
+void log_exit(void);
+
+#endif
tools/thermal/lib/mainloop.c new file +135 -0

135 lines not on this page · raw message

tools/thermal/lib/mainloop.h new file +14 -0
new file mode 100644
index 000000000000..eb37977a83e7
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
+/* Copyright (C) 2022, Linaro Ltd - Daniel Lezcano <[email protected]> */
+#ifndef __THERMAL_TOOLS_MAINLOOP_H
+#define __THERMAL_TOOLS_MAINLOOP_H
+
+typedef int (*mainloop_callback_t)(int fd, void *data);
+
+extern int mainloop(unsigned int timeout);
+extern int mainloop_add(int fd, mainloop_callback_t cb, void *data);
+extern int mainloop_del(int fd);
+extern int mainloop_init(void);
+extern void mainloop_fini(void);
+
+#endif
tools/thermal/lib/thermal-tools.h new file +10 -0
new file mode 100644
index 000000000000..f43939a468a3
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
+/* Copyright (C) 2022, Linaro Ltd - Daniel Lezcano <[email protected]> */
+#ifndef __THERMAL_TOOLS
+#define __THERMAL_TOOLS
+
+#include "log.h"
+#include "mainloop.h"
+#include "uptimeofday.h"
+
+#endif
tools/thermal/lib/uptimeofday.c new file +40 -0
new file mode 100644
index 000000000000..7ccbb272debd
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: LGPL-2.1+
+// Copyright (C) 2022, Linaro Ltd - Daniel Lezcano <[email protected]>
+#include <stdio.h>
+#include <sys/time.h>
+#include <linux/sysinfo.h>
+#include "thermal-tools.h"
+
+static unsigned long __offset;
+static struct timeval __tv;
+
+int uptimeofday_init(void)
+{
+	struct sysinfo info;
+
+	if (sysinfo(&info))
+		return -1;
+
+	gettimeofday(&__tv, NULL);
+
+	__offset = __tv.tv_sec - info.uptime;
+
+	return 0;
+}
+
+unsigned long getuptimeofday_ms(void)
+{
+	gettimeofday(&__tv, NULL);
+
+	return ((__tv.tv_sec - __offset) * 1000) + (__tv.tv_usec / 1000);
+}
+
+struct timespec msec_to_timespec(int msec)
+{
+        struct timespec tv = {
+                .tv_sec = (time_t)(msec / 1000),
+                .tv_nsec = (msec - tv.tv_sec) * 1000000,
+        };
+
+        return tv;
+}
tools/thermal/lib/uptimeofday.h new file +12 -0
new file mode 100644
index 000000000000..c0da5de41325
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
+/* Copyright (C) 2022, Linaro Ltd - Daniel Lezcano <[email protected]> */
+#ifndef __THERMAL_TOOLS_UPTIMEOFDAY_H
+#define __THERMAL_TOOLS_UPTIMEOFDAY_H
+#include <sys/sysinfo.h>
+#include <sys/time.h>
+
+int uptimeofday_init(void);
+unsigned long getuptimeofday_ms(void);
+struct timespec msec_to_timespec(int msec);
+
+#endif
Signature · 1 line
2.25.1
#2 lore raw

RFC PATCH 3/4 tools/thermal: A temperature capture tool

In reply to Daniel Lezcano

The 'thermometer' tool allows to capture the temperature of a set of thermal zones defined in a configuration file at a specified rate.

It is designed to have the lowest possible overhead. It will write the captured temperature per thermal zone per file so making easier to write a gnuplot script.

---
 tools/Makefile                             |  16 +-
 tools/thermal/thermometer/Build            |   2 +
 tools/thermal/thermometer/Makefile         |  23 ++
 tools/thermal/thermometer/thermometer.c    | 384 +++++++++++++++++++++
 tools/thermal/thermometer/thermometer.conf |   5 +
 5 files changed, 427 insertions(+), 3 deletions(-)
 create mode 100644 tools/thermal/thermometer/Build
 create mode 100644 tools/thermal/thermometer/Makefile
 create mode 100644 tools/thermal/thermometer/thermometer.c
 create mode 100644 tools/thermal/thermometer/thermometer.conf
tools/Makefile +13 -3
index c253cbd27c06..78615f8cb463 100644
@@ -31,6 +31,7 @@ help:
 	@echo '  bootconfig             - boot config tool'
 	@echo '  spi                    - spi tools'
 	@echo '  tmon                   - thermal monitoring and tuning tool'
+	@echo '  thermometer            - temperature capture tool'
 	@echo '  thermal                - thermal library'
 	@echo '  tracing                - misc tracing tools'
 	@echo '  turbostat              - Intel CPU idle stats and freq reporting tool'
@@ -95,6 +96,9 @@ turbostat x86_energy_perf_policy intel-speed-select: FORCE
 tmon: FORCE
 	$(call descend,thermal/$@)
 
+thermometer: FORCE
+	$(call descend,thermal/$@)
+
 freefall: FORCE
 	$(call descend,laptop/$@)
 
@@ -105,7 +109,7 @@ all: acpi cgroup counter cpupower gpio hv firewire \
 		perf selftests bootconfig spi turbostat usb \
 		virtio vm bpf x86_energy_perf_policy \
 		tmon freefall iio objtool kvm_stat wmi \
-		pci debugging tracing thermal
+		pci debugging tracing thermal thermometer
 
 acpi_install:
 	$(call descend,power/$(@:_install=),install)
@@ -128,6 +132,9 @@ turbostat_install x86_energy_perf_policy_install intel-speed-select_install:
 tmon_install:
 	$(call descend,thermal/$(@:_install=),install)
 
+thermometer_install:
+	$(call descend,thermal/$(@:_install=),install)
+
 freefall_install:
 	$(call descend,laptop/$(@:_install=),install)
 
@@ -140,7 +147,7 @@ install: acpi_install cgroup_install counter_install cpupower_install gpio_insta
 		virtio_install vm_install bpf_install x86_energy_perf_policy_install \
 		tmon_install freefall_install objtool_install kvm_stat_install \
 		wmi_install pci_install debugging_install intel-speed-select_install \
-		tracing_install
+		tracing_install thermometer_install
 
 acpi_clean:
 	$(call descend,power/acpi,clean)
@@ -173,6 +180,9 @@ thermal_clean:
 turbostat_clean x86_energy_perf_policy_clean intel-speed-select_clean:
 	$(call descend,power/x86/$(@:_clean=),clean)
 
+thermometer_clean:
+	$(call descend,thermal/thermometer,clean)
+
 tmon_clean:
 	$(call descend,thermal/tmon,clean)
 
@@ -187,6 +197,6 @@ clean: acpi_clean cgroup_clean counter_clean cpupower_clean hv_clean firewire_cl
 		vm_clean bpf_clean iio_clean x86_energy_perf_policy_clean tmon_clean \
 		freefall_clean build_clean libbpf_clean libsubcmd_clean \
 		gpio_clean objtool_clean leds_clean wmi_clean pci_clean firmware_clean debugging_clean \
-		intel-speed-select_clean tracing_clean thermal_clean
+		intel-speed-select_clean tracing_clean thermal_clean thermometer_clean
 
 .PHONY: FORCE
tools/thermal/thermometer/Build new file +2 -0
new file mode 100644
index 000000000000..2531dda4acdd
@@ -0,0 +1,2 @@
+thermometer-y += thermometer.o
+
tools/thermal/thermometer/Makefile new file +23 -0
new file mode 100644
index 000000000000..7d08661b3472
@@ -0,0 +1,23 @@
+# SPDX-License-Identifier: GPL-2.0
+# Makefile for cgroup tools
+
+ifeq ($(srctree),)
+srctree := $(patsubst %/,%,$(dir $(CURDIR)))
+srctree := $(patsubst %/,%,$(dir $(srctree)))
+srctree := $(patsubst %/,%,$(dir $(srctree)))
+# $(info Determined 'srctree' to be $(srctree))
+endif
+
+CFLAGS = -Wall -Wextra
+CFLAGS += -I$(srctree)/tools/thermal/lib
+
+LDFLAGS = -L$(srctree)/tools/thermal/lib
+LDFLAGS += -lthermal_tools
+LDFLAGS += -lconfig
+
+all: thermometer
+%: %.c
+	$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
+
+clean:
+	$(RM) thermometer
tools/thermal/thermometer/thermometer.c new file +384 -0

384 lines not on this page · raw message

tools/thermal/thermometer/thermometer.conf new file +5 -0
new file mode 100644
index 000000000000..350907accb3e
@@ -0,0 +1,5 @@
+
+thermal-zones = (
+	      {	name = "cpu[0-7]-thermal";
+	      	polling = 100; }
+      )
Signature · 1 line
2.25.1
#3 lore raw

RFC PATCH 4/4 tools/thermal: Add thermal daemon skeleton

In reply to Daniel Lezcano

This change provides a simple daemon skeleton. It provides an example of how to use the thermal library.

The goal of this skeleton is to give a base brick for anyone interested in writing its own thermal engine.

In the future, it will evolve with more features.

---
 tools/Makefile                    |  16 +-
 tools/thermal/thermald/Build      |   2 +
 tools/thermal/thermald/Makefile   |  27 +++
 tools/thermal/thermald/thermald.c | 275 ++++++++++++++++++++++++++++++
 4 files changed, 317 insertions(+), 3 deletions(-)
 create mode 100644 tools/thermal/thermald/Build
 create mode 100644 tools/thermal/thermald/Makefile
 create mode 100644 tools/thermal/thermald/thermald.c
tools/Makefile +13 -3
index 78615f8cb463..a8c4a65ac953 100644
@@ -32,6 +32,7 @@ help:
 	@echo '  spi                    - spi tools'
 	@echo '  tmon                   - thermal monitoring and tuning tool'
 	@echo '  thermometer            - temperature capture tool'
+	@echo '  thermald               - thermal monitoring tool'
 	@echo '  thermal                - thermal library'
 	@echo '  tracing                - misc tracing tools'
 	@echo '  turbostat              - Intel CPU idle stats and freq reporting tool'
@@ -99,6 +100,9 @@ tmon: FORCE
 thermometer: FORCE
 	$(call descend,thermal/$@)
 
+thermald: FORCE thermal
+	$(call descend,thermal/$@)
+
 freefall: FORCE
 	$(call descend,laptop/$@)
 
@@ -109,7 +113,7 @@ all: acpi cgroup counter cpupower gpio hv firewire \
 		perf selftests bootconfig spi turbostat usb \
 		virtio vm bpf x86_energy_perf_policy \
 		tmon freefall iio objtool kvm_stat wmi \
-		pci debugging tracing thermal thermometer
+		pci debugging tracing thermal thermometer thermald
 
 acpi_install:
 	$(call descend,power/$(@:_install=),install)
@@ -135,6 +139,9 @@ tmon_install:
 thermometer_install:
 	$(call descend,thermal/$(@:_install=),install)
 
+thermald_install:
+	$(call descend,thermal/$(@:_install=),install)
+
 freefall_install:
 	$(call descend,laptop/$(@:_install=),install)
 
@@ -147,7 +154,7 @@ install: acpi_install cgroup_install counter_install cpupower_install gpio_insta
 		virtio_install vm_install bpf_install x86_energy_perf_policy_install \
 		tmon_install freefall_install objtool_install kvm_stat_install \
 		wmi_install pci_install debugging_install intel-speed-select_install \
-		tracing_install thermometer_install
+		tracing_install thermometer_install thermald_install
 
 acpi_clean:
 	$(call descend,power/acpi,clean)
@@ -183,6 +190,9 @@ turbostat_clean x86_energy_perf_policy_clean intel-speed-select_clean:
 thermometer_clean:
 	$(call descend,thermal/thermometer,clean)
 
+thermald_clean:
+	$(call descend,thermal/thermald,clean)
+
 tmon_clean:
 	$(call descend,thermal/tmon,clean)
 
@@ -197,6 +207,6 @@ clean: acpi_clean cgroup_clean counter_clean cpupower_clean hv_clean firewire_cl
 		vm_clean bpf_clean iio_clean x86_energy_perf_policy_clean tmon_clean \
 		freefall_clean build_clean libbpf_clean libsubcmd_clean \
 		gpio_clean objtool_clean leds_clean wmi_clean pci_clean firmware_clean debugging_clean \
-		intel-speed-select_clean tracing_clean thermal_clean thermometer_clean
+		intel-speed-select_clean tracing_clean thermal_clean thermometer_clean thermald_clean
 
 .PHONY: FORCE
tools/thermal/thermald/Build new file +2 -0
new file mode 100644
index 000000000000..23a8ff56d290
@@ -0,0 +1,2 @@
+thermald-y += thermald.o
+
tools/thermal/thermald/Makefile new file +27 -0
new file mode 100644
index 000000000000..c74e87c2b233
@@ -0,0 +1,27 @@
+# SPDX-License-Identifier: GPL-2.0
+# Makefile for cgroup tools
+
+ifeq ($(srctree),)
+srctree := $(patsubst %/,%,$(dir $(CURDIR)))
+srctree := $(patsubst %/,%,$(dir $(srctree)))
+srctree := $(patsubst %/,%,$(dir $(srctree)))
+# $(info Determined 'srctree' to be $(srctree))
+endif
+
+CFLAGS = -Wall -Wextra
+CFLAGS += -I$(srctree)/tools/thermal/lib
+CFLAGS += -I$(srctree)/tools/lib/thermal/include
+
+LDFLAGS = -L$(srctree)/tools/thermal/lib
+LDFLAGS += -L$(srctree)/tools/lib/thermal
+LDFLAGS += -lthermal_tools
+LDFLAGS += -lthermal
+LDFLAGS += -lconfig
+LDFLAGS += -lnl-genl-3 -lnl-3
+
+all: thermald
+%: %.c
+	$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
+
+clean:
+	$(RM) thermald
tools/thermal/thermald/thermald.c new file +275 -0

275 lines not on this page · raw message

Signature · 1 line
2.25.1
#4 lore raw

Re: [RFC PATCH 4/4] tools/thermal: Add thermal daemon skeleton

Quoted 8 lines · Daniel Lezcano

This change provides a simple daemon skeleton. It provides an example of how to use the thermal library.

The goal of this skeleton is to give a base brick for anyone interested in writing its own thermal engine.

In the future, it will evolve with more features.

From the code, doesn't seam to have a function to daemonize. So may be not call a daemon if that is true. mainloop() is just an infinite loop polling netlink messages.

Also thermald will confuse with the standard thermald distributed with every distro. May be thermal_control_sample as this a sample for future development.

Thanks,
Srinivas

Quoted 446 lines

---
 tools/Makefile                    |  16 +-
 tools/thermal/thermald/Build      |   2 +
 tools/thermal/thermald/Makefile   |  27 +++
 tools/thermal/thermald/thermald.c | 275
++++++++++++++++++++++++++++++

 4 files changed, 317 insertions(+), 3 deletions(-)

 create mode 100644 tools/thermal/thermald/Build create mode 100644 tools/thermal/thermald/Makefile create mode 100644 tools/thermal/thermald/thermald.c

tools/Makefile +0 -0
index 78615f8cb463..a8c4a65ac953 100644
@@ -32,6 +32,7 @@ help:

        @echo '  spi                    - spi tools' @echo '  tmon                   - thermal monitoring and tuning tool'
        @echo '  thermometer            - temperature capture tool' +       @echo '  thermald               - thermal monitoring tool' @echo '  thermal                - thermal library' @echo '  tracing                - misc tracing tools' @echo '  turbostat              - Intel CPU idle stats and freq reporting tool'

tools/Makefile +0 -0
@@ -99,6 +100,9 @@ tmon: FORCE

 thermometer: FORCE
        $(call descend,thermal/$@)

tools/Makefile +3 -0
+thermald: FORCE thermal
+       $(call descend,thermal/$@)
+

 freefall: FORCE
        $(call descend,laptop/$@)

tools/Makefile +1 -1
@@ -109,7 +113,7 @@ all: acpi cgroup counter cpupower gpio hv
 firewire \
                 perf selftests bootconfig spi turbostat usb \
                 virtio vm bpf x86_energy_perf_policy \
                 tmon freefall iio objtool kvm_stat wmi \
-               pci debugging tracing thermal thermometer
+               pci debugging tracing thermal thermometer thermald


 acpi_install:
        $(call descend,power/$(@:_install=),install)

tools/Makefile +0 -0
@@ -135,6 +139,9 @@ tmon_install:

 thermometer_install:
        $(call descend,thermal/$(@:_install=),install)

tools/Makefile +3 -0
+thermald_install:
+       $(call descend,thermal/$(@:_install=),install)
+

 freefall_install:
        $(call descend,laptop/$(@:_install=),install)

tools/Makefile +0 -0
@@ -147,7 +154,7 @@ install: acpi_install cgroup_install

counter_install cpupower_install gpio_insta virtio_install vm_install bpf_install x86_energy_perf_policy_install \
                tmon_install freefall_install objtool_install kvm_stat_install \
                wmi_install pci_install debugging_install intel- speed-select_install \
-               tracing_install thermometer_install
+               tracing_install thermometer_install thermald_install
 acpi_clean:
        $(call descend,power/acpi,clean)

tools/Makefile +0 -0
@@ -183,6 +190,9 @@ turbostat_clean x86_energy_perf_policy_clean

intel-speed-select_clean:
 thermometer_clean:
        $(call descend,thermal/thermometer,clean)

tools/Makefile +3 -0
+thermald_clean:
+       $(call descend,thermal/thermald,clean)
+

 tmon_clean:
        $(call descend,thermal/tmon,clean)

tools/Makefile +0 -0
@@ -197,6 +207,6 @@ clean: acpi_clean cgroup_clean counter_clean

cpupower_clean hv_clean firewire_cl
                vm_clean bpf_clean iio_clean x86_energy_perf_policy_clean tmon_clean \
                freefall_clean build_clean libbpf_clean libsubcmd_clean \
                gpio_clean objtool_clean leds_clean wmi_clean pci_clean firmware_clean debugging_clean \
-               intel-speed-select_clean tracing_clean thermal_clean thermometer_clean
+               intel-speed-select_clean tracing_clean thermal_clean thermometer_clean thermald_clean

 .PHONY: FORCE

tools/thermal/thermald/Build new file +2 -0
new file mode 100644
index 000000000000..23a8ff56d290
@@ -0,0 +1,2 @@
+thermald-y += thermald.o
+
tools/thermal/thermald/Makefile new file +27 -0
new file mode 100644
index 000000000000..c74e87c2b233
@@ -0,0 +1,27 @@
+# SPDX-License-Identifier: GPL-2.0
+# Makefile for cgroup tools
+
+ifeq ($(srctree),)
+srctree := $(patsubst %/,%,$(dir $(CURDIR)))
+srctree := $(patsubst %/,%,$(dir $(srctree)))
+srctree := $(patsubst %/,%,$(dir $(srctree)))
+# $(info Determined 'srctree' to be $(srctree))
+endif
+
+CFLAGS = -Wall -Wextra
+CFLAGS += -I$(srctree)/tools/thermal/lib
+CFLAGS += -I$(srctree)/tools/lib/thermal/include
+
+LDFLAGS = -L$(srctree)/tools/thermal/lib
+LDFLAGS += -L$(srctree)/tools/lib/thermal
+LDFLAGS += -lthermal_tools
+LDFLAGS += -lthermal
+LDFLAGS += -lconfig
+LDFLAGS += -lnl-genl-3 -lnl-3
+
+all: thermald
+%: %.c
+       $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
+
+clean:
+       $(RM) thermald
tools/thermal/thermald/thermald.c new file +39 -0
new file mode 100644
index 000000000000..92ced613c06e
@@ -0,0 +1,275 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Thermal monitoring tool based on the thermal netlink events.
+ *
+ * Copyright (C) 2022 Linaro Ltd.
+ *
+ * Author: Daniel Lezcano <[email protected]>
+ */
+#include <errno.h>
+#include <fcntl.h>
+#include <libgen.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <signal.h>
+#include <unistd.h>
+
+#include <syslog.h>
+
+#include <sys/epoll.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include <thermal.h>
+#include "thermal-tools.h"
+
+struct options {
+       int loglevel;
+       int logopt;
+       int interactive;
+};
+
+struct thermal_data {
+       struct thermal_zone *tz;
+       struct thermal_handler *th;
+};
+
+static int show_trip(struct thermal_trip *tt, __maybe_unused void

*arg)

tools/thermal/thermald/thermald.c new file +8 -0
+{
+       INFO("trip id=%d, type=%d, temp=%d, hyst=%d\n",
+            tt->id, tt->type, tt->temp, tt->hyst);
+
+       return 0;
+}              
+
+static int show_temp(struct thermal_zone *tz, __maybe_unused void

*arg)

tools/thermal/thermald/thermald.c new file +9 -0
+{
+       thermal_cmd_get_temp(arg, tz);
+
+       INFO("temperature: %d\n", tz->temp);
+
+       return 0;
+}
+
+static int show_governor(struct thermal_zone *tz, __maybe_unused

void *arg)

tools/thermal/thermald/thermald.c new file +9 -0
+{
+       thermal_cmd_get_governor(arg, tz);
+
+       INFO("governor: '%s'\n", tz->governor);
+       
+       return 0;
+}
+
+static int show_tz(struct thermal_zone *tz, __maybe_unused void

*arg)

tools/thermal/thermald/thermald.c new file +13 -0
+{
+       INFO("thermal zone '%s', id=%d\n", tz->name, tz->id);
+
+       for_each_thermal_trip(tz->trip, show_trip, NULL);
+
+       show_temp(tz, arg);
+       
+       show_governor(tz, arg);
+       
+       return 0;
+}
+
+static int tz_create(const char *name, int tz_id, __maybe_unused

void *arg)

tools/thermal/thermald/thermald.c new file +17 -0
+{
+       INFO("Thermal zone '%s'/%d created\n", name, tz_id);
+
+       return 0;
+}
+
+static int tz_delete(int tz_id, __maybe_unused void *arg)
+{
+       INFO("Thermal zone %d deleted\n", tz_id);
+
+       return 0;
+}
+
+static int tz_disable(int tz_id, void *arg)
+{
+       struct thermal_data *td = arg;
+       struct thermal_zone *tz = thermal_zone_find_by_id(td->tz,

tz_id);

tools/thermal/thermald/thermald.c new file +10 -0
+
+       INFO("Thermal zone %d ('%s') disabled\n", tz_id, tz->name);
+
+       return 0;
+}
+
+static int tz_enable(int tz_id, void *arg)
+{
+       struct thermal_data *td = arg;
+       struct thermal_zone *tz = thermal_zone_find_by_id(td->tz,

tz_id);

tools/thermal/thermald/thermald.c new file +10 -0
+
+       INFO("Thermal zone %d ('%s') enabled\n", tz_id, tz->name);
+
+       return 0;
+}
+
+static int trip_high(int tz_id, int trip_id, int temp, void *arg)
+{
+       struct thermal_data *td = arg;
+       struct thermal_zone *tz = thermal_zone_find_by_id(td->tz,

tz_id);
+
+       INFO("Thermal zone %d ('%s'): trip point %d crossed way up with %d °C\n",
+            tz_id, tz->name, trip_id, temp);

tools/thermal/thermald/thermald.c new file +8 -0
+
+       return 0;
+}
+
+static int trip_low(int tz_id, int trip_id, int temp, void *arg)
+{
+       struct thermal_data *td = arg;
+       struct thermal_zone *tz = thermal_zone_find_by_id(td->tz,

tz_id);
+
+       INFO("Thermal zone %d ('%s'): trip point %d crossed way down with %d °C\n",
+            tz_id, tz->name, trip_id, temp);

tools/thermal/thermald/thermald.c new file +5 -0
+
+       return 0;
+}
+
+static int trip_add(int tz_id, int trip_id, int type, int temp, int

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

tools/thermal/thermald/thermald.c new file +5 -0
+
+       return 0;
+}
+
+static int trip_delete(int tz_id, int trip_id, __maybe_unused void

*arg)

tools/thermal/thermald/thermald.c new file +7 -0
+{
+       INFO("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)
+{
+       struct thermal_data *td = arg;
+       struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);
+
+       INFO("Trip point changed %d: id=%d, type=%d, temp=%d, hyst=%d\n",
+            tz_id, trip_id, type, temp, hyst);

tools/thermal/thermald/thermald.c new file +9 -0
+
+       tz->trip[trip_id].type = type;
+       tz->trip[trip_id].temp = temp;
+       tz->trip[trip_id].hyst = hyst;
+
+       return 0;
+}
+
+static int cdev_add(const char *name, int cdev_id, int max_state,

__maybe_unused void *arg)
+{
+       INFO("Cooling device '%s'/%d (max state=%d) added\n", name, cdev_id, max_state);

tools/thermal/thermald/thermald.c new file +12 -0
+
+       return 0;
+}
+
+static int cdev_delete(int cdev_id, __maybe_unused void *arg)
+{
+       INFO("Cooling device %d deleted", cdev_id);
+
+       return 0;
+}
+
+static int cdev_update(int cdev_id, int cur_state, __maybe_unused

void *arg)

tools/thermal/thermald/thermald.c new file +7 -0
+{
+       INFO("cdev:%d state:%d\n", cdev_id, cur_state);
+
+       return 0;
+}
+
+static int gov_change(int tz_id, const char *name, __maybe_unused

void *arg)
+{
+       struct thermal_data *td = arg;
+       struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);
+
+       INFO("%s: governor changed %s -> %s\n", tz->name, tz-

Quoted 1 line

governor, name);

tools/thermal/thermald/thermald.c new file +23 -0
+
+       strcpy(tz->governor, 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
+};
+
+static int thermal_event(__maybe_unused int fd, __maybe_unused void

*arg)

tools/thermal/thermald/thermald.c new file +7 -0
+{
+       struct thermal_data *td = arg;
+
+       return thermal_events_handle(td->th, td);
+}
+
+static int options_init(int argc, char *argv[], struct options

*options)

tools/thermal/thermald/thermald.c new file +35 -0
+{
+       int opt;
+       
+       while ((opt = getopt(argc, argv, "sl:")) != -1) {
+               switch (opt) {
+              case 'l':
+                      options->loglevel = log_str2level(optarg);
+                      break;
+              case 's':
+                      options->logopt |= TO_STDOUT;
+                      break;
+               default: /* '?' */
+                      ERROR("Usage: %s \n", argv[0]);
+                      return -1;
+               }
+       }
+
+       return 0;
+}
+
+int main(int argc, char *argv[])
+{
+       struct thermal_data td;
+       struct options options = {
+               .loglevel = LOG_DEBUG,
+               .logopt = LOG_SYSLOG
+       };
+
+       if (options_init(argc, argv, &options))
+               return 1;
+
+       if (!(options.logopt & TO_STDOUT) && daemon(0, 0))
+               return 1;
+       
+       if (log_init(options.loglevel, basename(argv[0]),

options.logopt))
+               return 1;

tools/thermal/thermald/thermald.c new file +15 -0
+
+       td.th = thermal_init(&ops);
+       if (!td.th)
+               return 1;
+
+       td.tz = thermal_zone_discover(td.th);
+       if (!td.tz)
+               return 1;
+
+       for_each_thermal_zone(td.tz, show_tz, td.th);
+       
+       if (mainloop_init())
+               return 1;
+
+       if (mainloop_add(thermal_events_fd(td.th), thermal_event,

&td))
+               return 1;

tools/thermal/thermald/thermald.c new file +3 -0
+
+       return mainloop(-1);
+}
#5 lore raw

Re: [RFC PATCH 4/4] tools/thermal: Add thermal daemon skeleton

Hi Srinivas,

Quoted 10 lines · srinivas pandruvada
Quoted 8 lines · Daniel Lezcano

This change provides a simple daemon skeleton. It provides an example of how to use the thermal library.

The goal of this skeleton is to give a base brick for anyone interested in writing its own thermal engine.

In the future, it will evolve with more features.

From the code, doesn't seam to have a function to daemonize.

Yes, it does, see below. If the stdout output is set, then it won't daemonize.

Quoted 6 lines

So may be not call a daemon if that is true. mainloop() is just an infinite loop polling netlink messages.

Also thermald will confuse with the standard thermald distributed with every distro. May be thermal_control_sample as this a sample for future development.

Does thermal-engine fine ?

Quoted 1 line
Quoted 1 line

[ ... ]

Quoted 1 line
Quoted 1 line
  tools/thermal/thermald/thermald.c | 275

[ ... ]

Quoted 2 lines
Quoted 2 lines

+       if (!(options.logopt & TO_STDOUT) && daemon(0, 0)) +               return 1;

[ ... ]

Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
#6 lore raw

Re: [RFC PATCH 4/4] tools/thermal: Add thermal daemon skeleton

Quoted 12 lines · Daniel Lezcano

Hi Srinivas,

Quoted 8 lines · srinivas pandruvada
Quoted 7 lines · Daniel Lezcano

This change provides a simple daemon skeleton. It provides an example
of how to use the thermal library.

The goal of this skeleton is to give a base brick for anyone interested in writing its own thermal engine.

Quoted 6 lines
Quoted 3 lines
Quoted 2 lines

In the future, it will evolve with more features.

 From the code, doesn't seam to have a function to daemonize.

Yes, it does, see below. If the stdout output is set, then it won't daemonize.

OK.

Quoted 5 lines
Quoted 5 lines

So may be not call a daemon if that is true. mainloop() is just an infinite loop polling netlink messages.

Quoted 7 lines
Quoted 5 lines

Also thermald will confuse with the standard thermald distributed with
every distro. May be thermal_control_sample as this a sample for future
development.

Does thermal-engine fine ?

fine.

Thanks,
Srinivas

Quoted 14 lines
Quoted 2 lines
Quoted 2 lines

[ ... ]

Quoted 1 line
Quoted 1 line

  tools/thermal/thermald/thermald.c | 275

[ ... ]

Quoted 2 lines
Quoted 2 lines

+       if (!(options.logopt & TO_STDOUT) && daemon(0, 0)) +               return 1;

[ ... ]

#7 lore raw
In reply to Daniel Lezcano
Quoted 10 lines · Daniel Lezcano

The thermal framework implements a netlink notification mechanism to be used by the userspace to have a thermal configuration discovery, trip point changes or violation, cooling device changes notifications,
etc...

This library provides a level of abstraction for the thermal netlink notification allowing the userspace to connect to the notification mechanism more easily. The library is callback oriented.

Some minor comments.
May be better use some defines instead of returning "-1" for error conditions.

Quoted 1 line

[...]

tools/lib/thermal/thermal.c line 47

Quoted 11 lines

+struct thermal_zone *thermal_zone_find_by_name(struct thermal_zone *tz,
+                                              const char *name)

tools/lib/thermal/thermal.c new file +8 -0
+{
+       int i;
+
+       if (!name)
+               return NULL;
+       
+       for (i = 0; tz[i].id != -1; i++) {
+               if (!strcmp(tz[i].name, name))

strncmp

Quoted 7 lines

+                       return &tz[i];
+       }

tools/lib/thermal/thermal.c new file +4 -0
+
+       return NULL;
+}
+

[...]

tools/lib/thermal/thermal_nl.h line 4

Quoted 12 lines
tools/lib/thermal/thermal_nl.h new file +2 -0
new file mode 100644
index 000000000000..54dbbad58349
@@ -0,0 +1,43 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
+/* Copyright (C) 2022, Linaro Ltd - Daniel Lezcano

<[email protected]> */

tools/lib/thermal/thermal_nl.h new file +2 -0
+#ifndef __THERMAL_H
+#define __THERMAL_H

__THERMAL_NL_H

Quoted 1 line

[...]

Quoted 7 lines
tools/lib/thermal/thermal_nl.h new file +4 -0
+/*
+ * Low level netlink
+ */
+extern int nl_subscribe_thermal(struct nl_sock *nl_sock, struct

nl_cb *nl_cb,
+                               const char *group); +

To complete API, don't we need nl_unsubscribe which calls nl_socket_drop_memberships()?

Thanks,
Srinivas

#8 lore raw

Hi Srinivas,

thanks for the review

Quoted 7 lines · srinivas pandruvada
Quoted 6 lines · Daniel Lezcano

The thermal framework implements a netlink notification mechanism to be used by the userspace to have a thermal configuration discovery, trip point changes or violation, cooling device changes notifications,
etc...

Quoted 7 lines
Quoted 4 lines

This library provides a level of abstraction for the thermal netlink notification allowing the userspace to connect to the notification mechanism more easily. The library is callback oriented.

Some minor comments.
May be better use some defines instead of returning "-1" for error conditions.

Do you suggest like an enum with different errors or just having -1 replaced by eg. THERMAL_ERROR ?

[ ... ]

Quoted 8 lines
Quoted 6 lines

+ * Low level netlink
+ */
+extern int nl_subscribe_thermal(struct nl_sock *nl_sock, struct nl_cb *nl_cb,
+                               const char *group);
+

To complete API, don't we need nl_unsubscribe which calls nl_socket_drop_memberships()?

Ah, yes. I'll add it.

Thanks

Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
#9 lore raw

Hi Daniel,

Quoted 21 lines · Daniel Lezcano

Hi Srinivas,

thanks for the review

Quoted 15 lines · srinivas pandruvada
Quoted 14 lines · Daniel Lezcano

The thermal framework implements a netlink notification mechanism to
be used by the userspace to have a thermal configuration discovery,
trip point changes or violation, cooling device changes notifications,
etc...

This library provides a level of abstraction for the thermal netlink
notification allowing the userspace to connect to the notification
mechanism more easily. The library is callback oriented.

Quoted 6 lines
Quoted 3 lines

Some minor comments.
May be better use some defines instead of returning "-1" for error conditions.

Do you suggest like an enum with different errors or just having -1 replaced by eg. THERMAL_ERROR ?

This will be fine.

Thanks,
Srinivas

Quoted 18 lines

[ ... ]

Quoted 8 lines
Quoted 6 lines

+ * Low level netlink
+ */
+extern int nl_subscribe_thermal(struct nl_sock *nl_sock, struct nl_cb *nl_cb,
+                               const char *group);
+

To complete API, don't we need nl_unsubscribe which calls nl_socket_drop_memberships()?

Ah, yes. I'll add it.

Thanks