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

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