Skip to contentSkip to conversation

BloodTest: kernel status

RFC In review Hui Zhu 2 participants 6 messages Oct 13, 2017 – Oct 23, 2017 Proc filesystem

lore
Share & tools
lore

Hui Zhu

author
#0 lore raw

cover BloodTest: kernel status

BloodTest: an interface to call other analysing tools

Linux kernel has a lot of analysing tools, perf, ftrace, systemtap, KGTP and so on.
And kernel also supplies a lot of internal value from procfs and sysfs to analyse the performance.

Sometime, user need get performance infomation quickly, low overhead and full coverage.
BloodTest is for it.
It is a interface can acess function of other analysing tools and records to internal buffer that user or application can access very quickly (mmap).

Now, BloodTest just support record cpu, perf and task infomation in one seconds.

Hui Zhu (2):
BloodTest: kernel status
BloodTest: perf
Module: add /proc/modules_update_version
BloodTest: task

 fs/proc/stat.c                 |    8 
 include/linux/bloodtest.h      |   10 
 include/linux/kernel_stat.h    |    3 
 init/Kconfig                   |    3 
 kernel/Makefile                |    2 
 kernel/bloodtest/Makefile      |    3 
 kernel/bloodtest/core.c        |  132 +++++++++
 kernel/bloodtest/internal.h    |   61 ++++
 kernel/bloodtest/kernel_stat.c |   62 ++++
 kernel/bloodtest/pages.c       |  266 ++++++++++++++++++
 kernel/bloodtest/perf.c        |  576 +++++++++++++++++++++++++++++++++++++++++
 kernel/bloodtest/task.c        |  447 +++++++++++++++++++++++++++++++
 kernel/exit.c                  |    4 
 kernel/module.c                |   19 +
 14 files changed, 1592 insertions(+), 4 deletions(-)
author
#1 lore raw

RFC 1/4 BloodTest: kernel status

This patch include the base framework of BloodTest and get the kernel status function.

The interface is in "/sys/kernel/debug/bloodtest".
Access "test" will call bt_insert that will call all start record function. And register a hrtimer to call bt_pullout to stop record.

bt_insert and bt_pullout will call analysing tools.

---
 fs/proc/stat.c                 |   8 +--
 include/linux/kernel_stat.h    |   3 ++
 init/Kconfig                   |   3 ++
 kernel/Makefile                |   2 +
 kernel/bloodtest/Makefile      |   1 +
 kernel/bloodtest/core.c        | 117 +++++++++++++++++++++++++++++++++++++++++
 kernel/bloodtest/internal.h    |  19 +++++++
 kernel/bloodtest/kernel_stat.c |  62 ++++++++++++++++++++++
 8 files changed, 211 insertions(+), 4 deletions(-)
 create mode 100644 kernel/bloodtest/Makefile
 create mode 100644 kernel/bloodtest/core.c
 create mode 100644 kernel/bloodtest/internal.h
 create mode 100644 kernel/bloodtest/kernel_stat.c
fs/proc/stat.c +4 -4
index bd4e55f..c6f4fd4 100644
@@ -22,7 +22,7 @@
 
 #ifdef arch_idle_time
 
-static u64 get_idle_time(int cpu)
+u64 get_idle_time(int cpu)
 {
 	u64 idle;
 
@@ -32,7 +32,7 @@ static u64 get_idle_time(int cpu)
 	return idle;
 }
 
-static u64 get_iowait_time(int cpu)
+u64 get_iowait_time(int cpu)
 {
 	u64 iowait;
 
@@ -44,7 +44,7 @@ static u64 get_iowait_time(int cpu)
 
 #else
 
-static u64 get_idle_time(int cpu)
+u64 get_idle_time(int cpu)
 {
 	u64 idle, idle_usecs = -1ULL;
 
@@ -60,7 +60,7 @@ static u64 get_idle_time(int cpu)
 	return idle;
 }
 
-static u64 get_iowait_time(int cpu)
+u64 get_iowait_time(int cpu)
 {
 	u64 iowait, iowait_usecs = -1ULL;
 
include/linux/kernel_stat.h +3 -0
index 66be8b6..bf8d3f0 100644
@@ -96,4 +96,7 @@ static inline void account_process_tick(struct task_struct *tsk, int user)
 
 extern void account_idle_ticks(unsigned long ticks);
 
+extern u64 get_idle_time(int cpu);
+extern u64 get_iowait_time(int cpu);
+
 #endif /* _LINUX_KERNEL_STAT_H */
init/Kconfig +3 -0
index 78cb246..f63550c 100644
@@ -1909,3 +1909,6 @@ config ASN1
 	  functions to call on what tags.
 
 source "kernel/Kconfig.locks"
+
+config BLOODTEST
+	bool "Blood test"
kernel/Makefile +2 -0
index ed470aa..2a04e42 100644
@@ -103,6 +103,8 @@ obj-$(CONFIG_BPF) += bpf/
 
 obj-$(CONFIG_PERF_EVENTS) += events/
 
+obj-$(CONFIG_BLOODTEST) += bloodtest/
+
 obj-$(CONFIG_USER_RETURN_NOTIFIER) += user-return-notifier.o
 obj-$(CONFIG_PADATA) += padata.o
 obj-$(CONFIG_CRASH_DUMP) += crash_dump.o
kernel/bloodtest/Makefile new file +1 -0
new file mode 100644
index 0000000..7f289af
@@ -0,0 +1 @@
+obj-y	= core.o kernel_stat.o
kernel/bloodtest/core.c new file +117 -0

117 lines not on this page · raw message

kernel/bloodtest/internal.h new file +19 -0
new file mode 100644
index 0000000..48faf4d
@@ -0,0 +1,19 @@
+#ifndef _KERNEL_BLOODTEST_INTERNAL_H
+#define _KERNEL_BLOODTEST_INTERNAL_H
+
+#include <linux/seq_file.h>
+
+enum bt_stat_enum {
+	bt_empty = 0,
+	bt_done,
+	bt_running,
+};
+
+extern enum bt_stat_enum bt_stat;
+extern spinlock_t bt_lock;
+
+extern void bt_insert_kernel_stat(void);
+extern void bt_pullout_kernel_stat(void);
+extern void bt_report_kernel_stat(struct seq_file *p);
+
+#endif /* _KERNEL_BLOODTEST_INTERNAL_H */
kernel/bloodtest/kernel_stat.c new file +62 -0

62 lines not on this page · raw message

Signature · 1 line
1.9.1
author
#2 lore raw

RFC 2/4 BloodTest: perf

In reply to Hui Zhu

This patch add the function that call perf function and bt_pages that can record the data that get from perf.

The interface is in "/sys/kernel/debug/bloodtest/perf".
"on" is the switch. When it set to 1, access "test" will call perf. There are "perf_config", "perf_freq", "perf_period", "perf_type" can set the options of perf.
After record, access "str" will get the record data in string. Access "cpu0/page" will get the record data in binary that is format is in "bin_format".

---
 kernel/bloodtest/Makefile   |   4 +-
 kernel/bloodtest/core.c     |  76 +++---
 kernel/bloodtest/internal.h |  43 +++-
 kernel/bloodtest/pages.c    | 266 ++++++++++++++++++++
 kernel/bloodtest/perf.c     | 591 ++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 931 insertions(+), 49 deletions(-)
 create mode 100644 kernel/bloodtest/pages.c
 create mode 100644 kernel/bloodtest/perf.c
kernel/bloodtest/Makefile +3 -1
index 7f289af..79b7ea0 100644
@@ -1 +1,3 @@
-obj-y	= core.o kernel_stat.o
+obj-y	= core.o pages.o kernel_stat.o
+
+obj-$(CONFIG_PERF_EVENTS) += perf.o
kernel/bloodtest/core.c +35 -41

132 lines not on this page · raw message

kernel/bloodtest/internal.h +36 -7

53 lines not on this page · raw message

kernel/bloodtest/pages.c new file +266 -0

266 lines not on this page · raw message

kernel/bloodtest/perf.c new file +591 -0

591 lines not on this page · raw message

Signature · 1 line
1.9.1
author
#3 lore raw

RFC 3/4 module: add /proc/modules_update_version

In reply to Hui Zhu

With "BloodTest: perf", we can get the address of kernel from "cpu0/page" without symbol.
The application that call BloodTest need translate the address to symbol with itself. For normal address, just vmlinux is OK to get the right symbol. But for the address of kernel module, it also need the address of modules from /proc/modules.

Add /proc/modules_update_version will help the application to get if the kernel modules address is changed or not.

---
 kernel/module.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
kernel/module.c +19 -0
index de66ec8..ed6f370 100644
@@ -317,6 +317,8 @@ struct load_info {
 	} index;
 };
 
+static atomic_t modules_update_version = ATOMIC_INIT(0);
+
 /*
  * We require a truly strong try_module_get(): 0 means success.
  * Otherwise an error is returned due to ongoing or failed
@@ -1020,6 +1022,9 @@ int module_refcount(struct module *mod)
 	strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
 
 	free_module(mod);
+
+	atomic_inc(&modules_update_version);
+
 	return 0;
 out:
 	mutex_unlock(&module_mutex);
@@ -3183,6 +3188,8 @@ static int move_module(struct module *mod, struct load_info *info)
 			 (long)shdr->sh_addr, info->secstrings + shdr->sh_name);
 	}
 
+	atomic_inc(&modules_update_version);
+
 	return 0;
 }
 
@@ -4196,9 +4203,21 @@ static int modules_open(struct inode *inode, struct file *file)
 	.release	= seq_release,
 };
 
+static int modules_update_version_get(void *data, u64 *val)
+{
+	*val = (u64)atomic_read(&modules_update_version);
+
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(proc_modules_update_version_operations,
+			modules_update_version_get, NULL, "%llu\n");
+
 static int __init proc_modules_init(void)
 {
 	proc_create("modules", 0, NULL, &proc_modules_operations);
+	proc_create("modules_update_version", 0, NULL,
+		    &proc_modules_update_version_operations);
 	return 0;
 }
 module_init(proc_modules_init);
Signature · 1 line
1.9.1
author
#4 lore raw

RFC 4/4 BloodTest: task

In reply to Hui Zhu

This patch add the function that get the infomation that task use the resource of system for example cpu time, read_bytes, write_bytes. The interface is in "/sys/kernel/debug/bloodtest/task". "on" is the switch. When it set to 1, access "test" will record task infomation.
After record, access "str" will get the record data in string. Access "page" will get the record data in binary that is format is in "bin_format".

---
 include/linux/bloodtest.h   |  10 +
 kernel/bloodtest/Makefile   |   2 +-
 kernel/bloodtest/core.c     |  21 +++
 kernel/bloodtest/internal.h |  13 ++
 kernel/bloodtest/perf.c     |  33 +---
 kernel/bloodtest/task.c     | 447 ++++++++++++++++++++++++++++++++++++++++++++
 kernel/exit.c               |   4 +
 7 files changed, 505 insertions(+), 25 deletions(-)
 create mode 100644 include/linux/bloodtest.h
 create mode 100644 kernel/bloodtest/task.c
include/linux/bloodtest.h new file +10 -0
new file mode 100644
index 0000000..55f4ebc
@@ -0,0 +1,10 @@
+#ifndef __LINUX_BLOODTEST_H
+#define __LINUX_BLOODTEST_H
+
+#ifdef CONFIG_BLOODTEST
+extern void bt_task_exit_record(struct task_struct *p);
+#else
+static inline void bt_task_exit_record(struct task_struct *p)	{ }
+#endif
+
+#endif /* __LINUX_BLOODTEST_H */
kernel/bloodtest/Makefile +1 -1
index 79b7ea0..a6f1a7a 100644
@@ -1,3 +1,3 @@
-obj-y	= core.o pages.o kernel_stat.o
+obj-y	= core.o pages.o kernel_stat.o task.o
 
 obj-$(CONFIG_PERF_EVENTS) += perf.o
kernel/bloodtest/core.c +21 -0
index 5ba800c..6cfcdf2 100644
@@ -16,6 +16,7 @@
 /* This function must be called under the protection of bt_lock.  */
 static void bt_insert(void)
 {
+	bt_insert_task();
 	bt_insert_perf();
 	bt_insert_kernel_stat();
 }
@@ -25,6 +26,7 @@ static void bt_pullout(void)
 {
 	bt_pullout_kernel_stat();
 	bt_pullout_perf();
+	bt_pullout_task();
 }
 
 /* This function must be called under the protection of bt_lock.  */
@@ -99,13 +101,32 @@ static int __init bt_init(void)
 	bt_ktime = ktime_set(1, 0);
 
 	ret = bt_perf_init(d);
+	if (ret < 0)
+		goto out;
+
+	ret = bt_task_init(d);
 
 out:
 	if (ret != 0) {
 		debugfs_remove(t);
 		debugfs_remove(d);
+		pr_err("bloodtest: init get error %d\n", ret);
 	}
 	return ret;
 }
 
 core_initcall(bt_init);
+
+int bt_number_get(void *data, u64 *val)
+{
+	unsigned int *number_point = data;
+
+	down_read(&bt_lock);
+
+	*val = (u64)*number_point;
+
+	up_read(&bt_lock);
+
+	return 0;
+}
+
kernel/bloodtest/internal.h +13 -0
index f6befc4..5aacf37 100644
@@ -3,6 +3,13 @@
 
 #include <linux/seq_file.h>
 
+#define SHOW_FORMAT_1(p, s, entry, type, sign, size) \
+	seq_printf(p, "%s format:%s %s offset:%lu size:%lu\n", \
+		   #entry, #type, sign, offsetof(s, entry), \
+		   (unsigned long)size)
+#define SHOW_FORMAT(p, s, entry, type, sign) \
+	SHOW_FORMAT_1(p, s, entry, type, sign, sizeof(type))
+
 extern struct rw_semaphore bt_lock;
 
 struct bt_pages {
@@ -45,4 +52,10 @@ static inline void bt_task_pullout_perf(void)		{ }
 static inline int bt_perf_init(struct dentry *d)	{ return 0; }
 #endif
 
+extern void bt_insert_task(void);
+extern void bt_pullout_task(void);
+extern int bt_task_init(struct dentry *d);
+
+extern int bt_number_get(void *data, u64 *val);
+
 #endif /* _KERNEL_BLOODTEST_INTERNAL_H */
kernel/bloodtest/perf.c +9 -24
index cf23844..d495258 100644
@@ -40,20 +40,7 @@ struct perf_rec {
 struct dentry *perf_dir;
 struct dentry *perf_str_dir;
 
-static int perf_number_get(void *data, u64 *val)
-{
-	unsigned int *number_point = data;
-
-	down_read(&bt_lock);
-
-	*val = (u64)*number_point;
-
-	up_read(&bt_lock);
-
-	return 0;
-}
-
-DEFINE_SIMPLE_ATTRIBUTE(perf_number_fops, perf_number_get, NULL, "%llu\n");
+DEFINE_SIMPLE_ATTRIBUTE(perf_number_fops, bt_number_get, NULL, "%llu\n");
 
 static void perf_overflow_handler(struct perf_event *event,
 		struct perf_sample_data *data,
@@ -402,7 +389,7 @@ static int perf_event_set(void *data, u64 val)
 }
 
 DEFINE_SIMPLE_ATTRIBUTE(perf_event_fops,
-			perf_number_get,
+			bt_number_get,
 			perf_event_set, "%llu\n");
 
 static int perf_bin_format_show(struct seq_file *p, void *unused)
@@ -412,16 +399,14 @@ static int perf_bin_format_show(struct seq_file *p, void *unused)
 #else
 	seq_puts(p, "little-endian\n");
 #endif
+	seq_printf(p, "page_size:%lu\n", PAGE_SIZE);
 	seq_printf(p, "size:%lu\n", sizeof(struct perf_entry));
 
-	seq_printf(p, "pc format:u64 unsigned offset:%lu size:%lu\n",
-		   offsetof(struct perf_entry, pc), sizeof(u64));
-	seq_printf(p, "is_user format:u8 unsigned offset:%lu size:%lu\n",
-		   offsetof(struct perf_entry, is_user), sizeof(u8));
-	seq_printf(p, "oom_score_adj format:s16 signed offset:%lu size:%lu\n",
-		   offsetof(struct perf_entry, oom_score_adj), sizeof(s16));
-	seq_printf(p, "comm format:char[] signed offset:%lu size:%d\n",
-		   offsetof(struct perf_entry, comm), TASK_COMM_LEN);
+	SHOW_FORMAT(p, struct perf_entry, pc, u64, "unsigned");
+	SHOW_FORMAT(p, struct perf_entry, is_user, u8, "unsigned");
+	SHOW_FORMAT(p, struct perf_entry, oom_score_adj, s16, "signed");
+	SHOW_FORMAT_1(p, struct perf_entry, comm, char[], "signed",
+		      TASK_COMM_LEN);
 
 	return 0;
 }
@@ -465,7 +450,7 @@ static int rec_max_set(void *data, u64 val)
 }
 
 DEFINE_SIMPLE_ATTRIBUTE(rec_max_fops,
-			perf_number_get,
+			bt_number_get,
 			rec_max_set, "%llu\n");
 
 void
kernel/bloodtest/task.c new file +447 -0

447 lines not on this page · raw message

kernel/exit.c +4 -0
index f2cd53e..513de91 100644
@@ -62,6 +62,7 @@
 #include <linux/random.h>
 #include <linux/rcuwait.h>
 #include <linux/compat.h>
+#include <linux/bloodtest.h>
 
 #include <linux/uaccess.h>
 #include <asm/unistd.h>
@@ -855,6 +856,9 @@ void __noreturn do_exit(long code)
 		acct_process();
 	trace_sched_process_exit(tsk);
 
+	if (group_dead)
+		bt_task_exit_record(tsk);
+
 	exit_sem(tsk);
 	exit_shm(tsk);
 	exit_files(tsk);
Signature · 1 line
1.9.1
#5 lore raw
In reply to Hui Zhu

Hi!

Quoted 7 lines

BloodTest: an interface to call other analysing tools

Linux kernel has a lot of analysing tools, perf, ftrace, systemtap, KGTP and so on.
And kernel also supplies a lot of internal value from procfs and sysfs to analyse the performance.

Quoted 15 lines
 fs/proc/stat.c                 |    8 
 include/linux/bloodtest.h      |   10 
 include/linux/kernel_stat.h    |    3 
 init/Kconfig                   |    3 
 kernel/Makefile                |    2 
 kernel/bloodtest/Makefile      |    3 
 kernel/bloodtest/core.c        |  132 +++++++++
 kernel/bloodtest/internal.h    |   61 ++++
 kernel/bloodtest/kernel_stat.c |   62 ++++
 kernel/bloodtest/pages.c       |  266 ++++++++++++++++++
 kernel/bloodtest/perf.c        |  576 +++++++++++++++++++++++++++++++++++++++++
 kernel/bloodtest/task.c        |  447 +++++++++++++++++++++++++++++++
 kernel/exit.c                  |    4 
 kernel/module.c                |   19 +
 14 files changed, 1592 insertions(+), 4 deletions(-)

No documentation?
Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html