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

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