Blame SOURCES/0004-Prepare-to-handle-thermal-event.patch

9425f0
From b66647ab757c580b2532c117bd345d1bad3b2bd5 Mon Sep 17 00:00:00 2001
9425f0
From: "Chang S. Bae" <chang.seok.bae@intel.com>
9425f0
Date: Thu, 10 Feb 2022 14:46:01 -0800
9425f0
Subject: [PATCH 04/14] Prepare to handle thermal event
9425f0
9425f0
Intel's new hardware supports Hardware Feedback Interface to provide CPU
9425f0
performance and energy efficiency information. Every update on this is
9425f0
delivered via thermal event interrupt. The thermal framework in the Linux
9425f0
kernel relays these notifications to userspace via a Netlink interface.
9425f0
9425f0
When a CPU's performance and efficiency are zero, irqbalance needs to mask
9425f0
the CPU from interrupts. Introduce a new CPU mask to indicate banned CPUs
9425f0
for this.
9425f0
9425f0
Before supporting this event handling, define functions. Their
9425f0
implementation will be on the following patches.
9425f0
9425f0
This event is available only on x86-64 systems. And it can be subscribed
9425f0
with help of Netlink libraries. So check them before building it.
9425f0
9425f0
Also add a new build option so users may opt out this support. Setting this
9425f0
option on other systems will result in a build failure.
9425f0
9425f0
Signed-off-by: Chang S. Bae <chang.seok.bae@intel.com>
9425f0
---
9425f0
 Makefile.am  |  7 +++--
9425f0
 configure.ac | 22 ++++++++++++++
9425f0
 cputree.c    |  6 ++++
9425f0
 irqbalance.c |  4 +++
9425f0
 thermal.c    | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++
9425f0
 thermal.h    | 15 ++++++++++
9425f0
 6 files changed, 135 insertions(+), 2 deletions(-)
9425f0
 create mode 100644 thermal.c
9425f0
 create mode 100644 thermal.h
9425f0
9425f0
diff --git a/Makefile.am b/Makefile.am
9425f0
index 84e7d46..9181a34 100644
9425f0
--- a/Makefile.am
9425f0
+++ b/Makefile.am
9425f0
@@ -27,7 +27,7 @@ EXTRA_DIST = COPYING autogen.sh misc/irqbalance.service misc/irqbalance.env
9425f0
 SUBDIRS = tests
9425f0
 
9425f0
 UI_DIR = ui
9425f0
-AM_CFLAGS = $(LIBCAP_NG_CFLAGS) $(GLIB2_CFLAGS) $(NUMA_CFLAGS)
9425f0
+AM_CFLAGS = $(LIBCAP_NG_CFLAGS) $(GLIB2_CFLAGS) $(NUMA_CFLAGS) $(LIBNL3_CFLAGS)
9425f0
 AM_CPPFLAGS = -I${top_srcdir} -W -Wall -Wshadow -Wformat -Wundef -D_GNU_SOURCE
9425f0
 noinst_HEADERS = bitmap.h constants.h cpumask.h irqbalance.h non-atomic.h \
9425f0
 	types.h $(UI_DIR)/helpers.h $(UI_DIR)/irqbalance-ui.h $(UI_DIR)/ui.h
9425f0
@@ -39,7 +39,10 @@ endif
9425f0
 
9425f0
 irqbalance_SOURCES = activate.c bitmap.c classify.c cputree.c irqbalance.c \
9425f0
 	irqlist.c numa.c placement.c procinterrupts.c
9425f0
-irqbalance_LDADD = $(LIBCAP_NG_LIBS) $(GLIB2_LIBS) $(NUMA_LIBS)
9425f0
+if THERMAL
9425f0
+irqbalance_SOURCES += thermal.c
9425f0
+endif
9425f0
+irqbalance_LDADD = $(LIBCAP_NG_LIBS) $(GLIB2_LIBS) $(NUMA_LIBS) $(LIBNL3_LIBS)
9425f0
 if IRQBALANCEUI
9425f0
 irqbalance_ui_SOURCES = $(UI_DIR)/helpers.c $(UI_DIR)/irqbalance-ui.c \
9425f0
 	$(UI_DIR)/ui.c
9425f0
diff --git a/configure.ac b/configure.ac
9425f0
index 32082fc..15532c1 100644
9425f0
--- a/configure.ac
9425f0
+++ b/configure.ac
9425f0
@@ -36,6 +36,28 @@ AS_IF([test "x$has_ncursesw" = "xyes"], [
9425f0
   AC_SUBST([LIBS])
9425f0
 ])
9425f0
 
9425f0
+AC_CANONICAL_HOST
9425f0
+
9425f0
+AC_ARG_ENABLE(thermal,
9425f0
+  AS_HELP_STRING([--enable-thermal], [enable thermal event support [default=auto]]),,
9425f0
+  AS_IF([test x"$host_cpu" = x"x86_64"], [enable_thermal=yes], [enable_thermal=no])
9425f0
+)
9425f0
+
9425f0
+AS_IF([test x"$enable_thermal" = x"yes" && test x"$host_cpu" != x"x86_64"],
9425f0
+  AC_MSG_ERROR([no thermal events support on $host_cpu systems.]),
9425f0
+)
9425f0
+
9425f0
+AS_IF([test x"$enable_thermal" = x"yes"],
9425f0
+  [PKG_CHECK_MODULES([LIBNL3], [libnl-3.0 libnl-genl-3.0], [have_thermal=yes],
9425f0
+    AC_MSG_NOTICE([no thermal event support as libnl-3.0 is unavailable.])
9425f0
+  )]
9425f0
+)
9425f0
+
9425f0
+AS_IF([test "x$have_thermal" = xyes],
9425f0
+  AC_DEFINE([HAVE_THERMAL], 1, [Build irqbalance to support thermal events])
9425f0
+)
9425f0
+AM_CONDITIONAL([THERMAL], [test "x$have_thermal" = xyes])
9425f0
+
9425f0
 AC_C_CONST
9425f0
 AC_C_INLINE
9425f0
 AM_PROG_CC_C_O
9425f0
diff --git a/cputree.c b/cputree.c
9425f0
index b716a8f..c250977 100644
9425f0
--- a/cputree.c
9425f0
+++ b/cputree.c
9425f0
@@ -38,6 +38,7 @@
9425f0
 #include <glib.h>
9425f0
 
9425f0
 #include "irqbalance.h"
9425f0
+#include "thermal.h"
9425f0
 
9425f0
 #ifdef HAVE_IRQBALANCEUI
9425f0
 extern char *banned_cpumask_from_ui;
9425f0
@@ -162,6 +163,11 @@ static void setup_banned_cpus(void)
9425f0
 	cpumask_scnprintf(buffer, 4096, nohz_full);
9425f0
 	log(TO_CONSOLE, LOG_INFO, "Adaptive-ticks CPUs: %s\n", buffer);
9425f0
 out:
9425f0
+#ifdef HAVE_THERMAL
9425f0
+	cpus_or(banned_cpus, banned_cpus, thermal_banned_cpus);
9425f0
+	cpumask_scnprintf(buffer, 4096, thermal_banned_cpus);
9425f0
+	log(TO_CONSOLE, LOG_INFO, "Thermal-banned CPUs: %s\n", buffer);
9425f0
+#endif
9425f0
 	cpumask_scnprintf(buffer, 4096, banned_cpus);
9425f0
 	log(TO_CONSOLE, LOG_INFO, "Banned CPUs: %s\n", buffer);
9425f0
 }
9425f0
diff --git a/irqbalance.c b/irqbalance.c
9425f0
index e8d9ba9..c520c11 100644
9425f0
--- a/irqbalance.c
9425f0
+++ b/irqbalance.c
9425f0
@@ -44,6 +44,7 @@
9425f0
 #include <sys/socket.h>
9425f0
 #endif
9425f0
 #include "irqbalance.h"
9425f0
+#include "thermal.h"
9425f0
 
9425f0
 volatile int keep_going = 1;
9425f0
 int one_shot_mode;
9425f0
@@ -703,6 +704,8 @@ int main(int argc, char** argv)
9425f0
 		goto out;
9425f0
 	}
9425f0
 #endif
9425f0
+	if (init_thermal())
9425f0
+		log(TO_ALL, LOG_WARNING, "Failed to initialize thermal events.\n");
9425f0
 	main_loop = g_main_loop_new(NULL, FALSE);
9425f0
 	last_interval = sleep_interval;
9425f0
 	g_timeout_add_seconds(sleep_interval, scan, NULL);
9425f0
@@ -711,6 +714,7 @@ int main(int argc, char** argv)
9425f0
 	g_main_loop_quit(main_loop);
9425f0
 
9425f0
 out:
9425f0
+	deinit_thermal();
9425f0
 	free_object_tree();
9425f0
 	free_cl_opts();
9425f0
 	free(polscript);
9425f0
diff --git a/thermal.c b/thermal.c
9425f0
new file mode 100644
9425f0
index 0000000..308bc48
9425f0
--- /dev/null
9425f0
+++ b/thermal.c
9425f0
@@ -0,0 +1,83 @@
9425f0
+/*
9425f0
+ * This program is free software; you can redistribute it and/or modify
9425f0
+ * it under the terms of the GNU General Public License version 2 as
9425f0
+ * published by the Free Software Foundation.
9425f0
+ */
9425f0
+
9425f0
+#include <stdio.h>
9425f0
+
9425f0
+#include <netlink/genl/genl.h>
9425f0
+#include <netlink/genl/family.h>
9425f0
+#include <netlink/genl/ctrl.h>
9425f0
+
9425f0
+#include "irqbalance.h"
9425f0
+
9425f0
+cpumask_t thermal_banned_cpus;
9425f0
+
9425f0
+static gboolean prepare_netlink(void)
9425f0
+{
9425f0
+	gboolean error = TRUE;
9425f0
+
9425f0
+	log(TO_ALL, LOG_ERR, "thermal: not yet implement to alloc memory for netlink.\n");
9425f0
+	return error;
9425f0
+}
9425f0
+
9425f0
+#define NL_FAMILY_NAME	"nlctrl"
9425f0
+
9425f0
+static gboolean establish_netlink(void)
9425f0
+{
9425f0
+	gboolean error = TRUE;
9425f0
+
9425f0
+	log(TO_ALL, LOG_ERR, "thermal: not yet implemented to establish netlink.\n");
9425f0
+	return error;
9425f0
+}
9425f0
+
9425f0
+static gboolean register_netlink_handler(nl_recvmsg_msg_cb_t handler __attribute__((unused)))
9425f0
+{
9425f0
+	gboolean error = TRUE;
9425f0
+
9425f0
+	log(TO_ALL, LOG_ERR, "thermal: not yet implemented to register thermal handler.\n");
9425f0
+	return error;
9425f0
+}
9425f0
+
9425f0
+static gboolean set_netlink_nonblocking(void)
9425f0
+{
9425f0
+	gboolean error = TRUE;
9425f0
+
9425f0
+	log(TO_ALL, LOG_ERR, "thermal: not yet implemented to set nonblocking socket.\n");
9425f0
+	return error;
9425f0
+}
9425f0
+
9425f0
+void deinit_thermal(void)
9425f0
+{
9425f0
+	return;
9425f0
+}
9425f0
+
9425f0
+/*
9425f0
+ * return value: TRUE with an error; otherwise, FALSE
9425f0
+ */
9425f0
+gboolean init_thermal(void)
9425f0
+{
9425f0
+	gboolean error;
9425f0
+
9425f0
+	error = prepare_netlink();
9425f0
+	if (error)
9425f0
+		goto err_out;
9425f0
+
9425f0
+	error = establish_netlink();
9425f0
+	if (error)
9425f0
+		goto err_out;
9425f0
+
9425f0
+	error = register_netlink_handler(NULL);
9425f0
+	if (error)
9425f0
+		goto err_out;
9425f0
+
9425f0
+	error = set_netlink_nonblocking();
9425f0
+	if (error)
9425f0
+		goto err_out;
9425f0
+
9425f0
+	return FALSE;
9425f0
+err_out:
9425f0
+	deinit_thermal();
9425f0
+	return TRUE;
9425f0
+}
9425f0
diff --git a/thermal.h b/thermal.h
9425f0
new file mode 100644
9425f0
index 0000000..657d54e
9425f0
--- /dev/null
9425f0
+++ b/thermal.h
9425f0
@@ -0,0 +1,15 @@
9425f0
+#ifndef __LINUX_THERMAL_H_
9425f0
+#define __LINUX_THERMAL_H_
9425f0
+
9425f0
+#include <glib.h>
9425f0
+
9425f0
+#ifdef HAVE_THERMAL
9425f0
+gboolean init_thermal(void);
9425f0
+void deinit_thermal(void);
9425f0
+extern cpumask_t thermal_banned_cpus;
9425f0
+#else
9425f0
+static inline gboolean init_thermal(void) { return FALSE; }
9425f0
+#define deinit_thermal() do { } while (0)
9425f0
+#endif
9425f0
+
9425f0
+#endif /* __LINUX_THERMAL_H_ */
9425f0
-- 
9425f0
2.33.1
9425f0