[<prev] [next>] [day] [month] [year] [list]
Message-ID:
<SJ2P223MB10261D3FCC3D949DA7DA4565F7B22@SJ2P223MB1026.NAMP223.PROD.OUTLOOK.COM>
Date: Thu, 1 Aug 2024 15:40:33 -0400
From: Steven Davis <goldside000@...look.com>
To: akpm@...ux-foundation.org
Cc: linux-kernel@...r.kernel.org,
Steven Davis <goldside000@...look.com>
Subject: [PATCH] Add memlogger utility and update MAINTAINERS
This adds memlogger, a module that can be both used in critical
situations and learned from. When making a patch, the kernel
can be confusing. Memlogger uses simpler components of the kernel
to provide an easy learning environment for new developers.
It can also be used to debug memory related issues.
For example, if there were an issue where something was gradually
consuming memory, memlogger would easily catch that.
MAINTAINERS was also updated to recognize the creation of the
memloggers directory.
Signed-off-by: Steven Davis <goldside000@...look.com>
---
MAINTAINERS | 6 +++
samples/memlogger/Makefile | 7 ++++
samples/memlogger/get_memory_info.h | 4 ++
samples/memlogger/memlogger.c | 64 +++++++++++++++++++++++++++++
4 files changed, 81 insertions(+)
create mode 100644 samples/memlogger/Makefile
create mode 100644 samples/memlogger/get_memory_info.h
create mode 100644 samples/memlogger/memlogger.c
diff --git a/MAINTAINERS b/MAINTAINERS
index 8766f3e5e87e..a140e2714b3d 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -14633,6 +14633,12 @@ F: mm/memblock.c
F: mm/mm_init.c
F: tools/testing/memblock/
+MEMLOGGER
+M: Steven Davis <goldside000@...look.com>
+L: linux-kernel@...r.kernel.org
+S: Maintained
+F: samples/memlogger/
+
MEMORY ALLOCATION PROFILING
M: Suren Baghdasaryan <surenb@...gle.com>
M: Kent Overstreet <kent.overstreet@...ux.dev>
diff --git a/samples/memlogger/Makefile b/samples/memlogger/Makefile
new file mode 100644
index 000000000000..bc130c1c8ede
--- /dev/null
+++ b/samples/memlogger/Makefile
@@ -0,0 +1,7 @@
+obj-m += memlogger.o
+
+all:
+ make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
+
+clean:
+ make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
diff --git a/samples/memlogger/get_memory_info.h b/samples/memlogger/get_memory_info.h
new file mode 100644
index 000000000000..5fdc47d58a75
--- /dev/null
+++ b/samples/memlogger/get_memory_info.h
@@ -0,0 +1,4 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#ifndef GET_MEMORY_INFO_H
+void get_memory_info(struct timer_list *t);
+#endif // GET_MEMORY_INFO_H
diff --git a/samples/memlogger/memlogger.c b/samples/memlogger/memlogger.c
new file mode 100644
index 000000000000..196bdf53fd91
--- /dev/null
+++ b/samples/memlogger/memlogger.c
@@ -0,0 +1,64 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * This module logs memory information to dmesg every 5 minutes.
+ * Useful for seeing how memory logging works and how timers work in the Linux kernel.
+ *
+ * Copyright (C) 2024 Steven Davis <goldside000@...look.com>
+ */
+
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/mm.h>
+#include <linux/timer.h>
+#include "get_memory_info.h"
+
+#define TIMER_INTERVAL (5 * 60 * HZ) // 5 minutes
+
+static struct timer_list mem_timer;
+static unsigned int interval = TIMER_INTERVAL / HZ; // Default interval in seconds
+
+module_param(interval, uint, 0644);
+MODULE_PARM_DESC(interval, "Timer interval in seconds for memory info logging");
+
+void get_memory_info(struct timer_list *t)
+{
+struct sysinfo si;
+
+si_meminfo(&si);
+
+pr_info("Memory Info:\n");
+pr_info("Total RAM: %lu kB\n", si.totalram * si.mem_unit / 1024);
+pr_info("Free RAM: %lu kB\n", si.freeram * si.mem_unit / 1024);
+pr_info("Shared RAM: %lu kB\n", si.sharedram * si.mem_unit / 1024);
+pr_info("Buffered RAM: %lu kB\n", si.bufferram * si.mem_unit / 1024);
+
+ // Re-schedule the timer
+mod_timer(&mem_timer, jiffies + interval * HZ);
+}
+
+static int __init mem_info_init(void)
+{
+pr_info("Memory Info Module Loaded\n");
+
+ // Initialize the timer
+timer_setup(&mem_timer, get_memory_info, 0);
+
+ // Schedule the timer for the first time
+mod_timer(&mem_timer, jiffies + interval * HZ);
+
+return 0;
+}
+
+static void __exit mem_info_exit(void)
+{
+del_timer(&mem_timer);
+pr_info("Memory Info Module Unloaded\n");
+}
+
+module_init(mem_info_init);
+module_exit(mem_info_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Steven Davis <goldside000@...look.com>");
+MODULE_DESCRIPTION("A module that outputs memory information periodically.");
--
2.46.0
Powered by blists - more mailing lists