summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzy <[email protected]>2023-12-13 03:17:52 -0500
committerzy <[email protected]>2023-12-13 03:17:52 -0500
commitcdde5566bfaa99a74ac561f4617cb630bb6dcc9f (patch)
treedea2c795ea6d46af75d2d91b3f21e8c3d215a50f
parent69bf4c8c69c3f0d2cbd43a73c4f14a4e44ee869a (diff)
add delay
-rw-r--r--source/module/monitor_kernel.c10
-rw-r--r--source/module/monitor_kernel.h1
-rw-r--r--source/module/monitor_kernel_lib.c58
-rw-r--r--source/module/monitor_perf.c102
-rw-r--r--source/module/monitor_perf.h8
-rw-r--r--source/module/monitor_timer.h3
-rw-r--r--source/module/monitor_trace.h2
-rw-r--r--source/ucli/ucli.cc2
-rw-r--r--source/ucli/ucli.h2
9 files changed, 151 insertions, 37 deletions
diff --git a/source/module/monitor_kernel.c b/source/module/monitor_kernel.c
index 644da98..2ee7838 100644
--- a/source/module/monitor_kernel.c
+++ b/source/module/monitor_kernel.c
@@ -99,7 +99,13 @@ static long device_ioctl(struct file *file, unsigned int ioctl_num,
case IOCTL_PID:
printk(KERN_INFO "variable_monitor PID\n");
ret = copy_from_user(&wid, (ioctl_id *)ioctl_param, sizeof(ioctl_id));
- diag_pid(wid.id);
+ if (ret) {
+ printk(KERN_INFO "copy_from_user failed\n");
+ }
+ ret = diag_pid(wid.id);
+ if (ret) {
+ printk(KERN_INFO "diag_pid failed\n");
+ }
break;
case IOCTL_TGID:
printk(KERN_INFO "variable_monitor TGID\n");
@@ -187,6 +193,8 @@ void cleanup_module(void) {
class_destroy(watch_class);
cdev_del(watch_cdev);
unregister_chrdev_region(dev_num, 1);
+
+ cleanup_perf_event(); // just for perf test
}
MODULE_LICENSE("GPL");
diff --git a/source/module/monitor_kernel.h b/source/module/monitor_kernel.h
index 759d1ad..e879c7f 100644
--- a/source/module/monitor_kernel.h
+++ b/source/module/monitor_kernel.h
@@ -3,6 +3,7 @@
#include "monitor_proc.h"
#include "monitor_timer.h"
#include "monitor_trace.h"
+#include "monitor_perf.h"
#include <linux/ioctl.h> // for ioctl
diff --git a/source/module/monitor_kernel_lib.c b/source/module/monitor_kernel_lib.c
index 0bba2c0..78451d0 100644
--- a/source/module/monitor_kernel_lib.c
+++ b/source/module/monitor_kernel_lib.c
@@ -213,7 +213,7 @@ void diag_task_info_work(struct work_struct *work) {
kernel_watch_timer *k_watch_timer =
container_of(work, kernel_watch_timer, wk);
- if (k_watch_timer->threshold_num <= 0) // if no threshold reached
+ if (k_watch_timer->threshold_over_count <= 0) // if no threshold reached
return;
// printk(KERN_INFO "diag_task_info_work\n");
@@ -228,28 +228,14 @@ void diag_task_info_work(struct work_struct *work) {
vm_record.id = event_id;
vm_record.et_type = VARIABLE_MONITOR_RECORD_TYPE;
- vm_record.tv = ktime_get_real();
- vm_record.threshold_num = k_watch_timer->threshold_num;
+ vm_record.tv = k_watch_timer->tv;
+ vm_record.threshold_over_count = k_watch_timer->threshold_over_count;
int i;
- for (i = 0; i < vm_record.threshold_num; i++) {
+ for (i = 0; i < vm_record.threshold_over_count; i++) {
kwarg = &k_watch_timer->k_watch_args[k_watch_timer->threshold_buffer[i]];
k_w_arg2threshold(kwarg, &vm_record.threshold_record[i]);
}
- // printk(KERN_INFO "-------------------------------------\n");
- // printk(KERN_INFO "-----------variable monitor----------\n");
- // printk(KERN_INFO "threshold exceeded, Timestamp %lld:\n", vm_record.tv);
-
- // for (i = 0; i < vm_record.threshold_num; i++) {
- // printk(KERN_INFO "\t: pid: %d, name: %s, ptr: %p, threshold:%lld,
- // true_value:%lld\n",
- // vm_record.threshold_record[i].task_id,
- // vm_record.threshold_record[i]
- // .name, // Assuming name is a null-terminated string
- // vm_record.threshold_record[i].ptr,
- // vm_record.threshold_record[i].threshold,
- // vm_record.threshold_record[i].true_value);
- // }
rcu_read_lock();
diag_variant_buffer_spin_lock(&load_monitor_variant_buffer, flags);
@@ -284,9 +270,10 @@ void diag_task_info_work(struct work_struct *work) {
rcu_read_unlock();
printk(KERN_INFO "-----------variable monitor----------\n");
- printk(KERN_INFO "threshold exceeded, Timestamp %lld:\n", vm_record.tv);
+ printk(KERN_INFO "threshold exceeded, Timestamp %lld, Delay %lld:\n",
+ vm_record.tv, ktime_get_real() - vm_record.tv);
- for (i = 0; i < vm_record.threshold_num; i++) {
+ for (i = 0; i < vm_record.threshold_over_count; i++) {
printk(KERN_INFO
"\t: pid: %d, name: %s, ptr: %p, threshold:%lld, true_value:%lld\n",
vm_record.threshold_record[i].task_id,
@@ -437,7 +424,8 @@ enum hrtimer_restart check_variable_cb(struct hrtimer *timer) {
}
if (j > 0) // if any threshold reached
{
- k_watch_timer->threshold_num = j;
+ k_watch_timer->threshold_over_count = j;
+ k_watch_timer->tv = ktime_get_real();
// highpri_wq
queue_work(system_highpri_wq, &k_watch_timer->wk);
// restart timer after 5s
@@ -457,13 +445,14 @@ enum hrtimer_restart check_variable_cb(struct hrtimer *timer) {
* @return int
*/
int diag_pid(int id) {
+ pr_info("diag_pid\n");
struct task_struct *tsk;
int ret;
- unsigned long flags;
- unsigned long event_id = get_cycles();
+ // unsigned long flags;
+ // unsigned long event_id = get_cycles();
- static variable_monitor_task tsk_info = {0};
- static variable_monitor_record vm_record = {0};
+ // static variable_monitor_task tsk_info = {0};
+ // static variable_monitor_record vm_record = {0};
pid_t pid = (pid_t)id;
@@ -478,15 +467,20 @@ int diag_pid(int id) {
}
rcu_read_unlock();
- get_task_struct(tsk); // count +1
+ pr_info("diag_pid: %d\n", tsk->pid);
+
+ // get_task_struct(tsk); // count +1
+
+ // tsk_info.et_type = VARIABLE_MONITOR_TASK_TYPE;
+ // tsk_info.id = event_id;
+ // tsk_info.tv = vm_record.tv;
+ // diag_tsk(tsk, &tsk_info);
+ // printk(KERN_INFO "pid: %d, name: %s\n", tsk->pid, tsk->comm);
+ setup_perf_event_for_task(tsk); // setup perf event for task
- tsk_info.et_type = VARIABLE_MONITOR_TASK_TYPE;
- tsk_info.id = event_id;
- tsk_info.tv = vm_record.tv;
- diag_tsk(tsk, &tsk_info);
- put_task_struct(tsk); // count -1
+ // put_task_struct(tsk); // count -1
- push_tskinfo_2_sa_buffer(&tsk_info, &flags); // push to buffer
+ // push_tskinfo_2_sa_buffer(&tsk_info, &flags); // push to buffer
return 0;
}
diff --git a/source/module/monitor_perf.c b/source/module/monitor_perf.c
new file mode 100644
index 0000000..549d7ac
--- /dev/null
+++ b/source/module/monitor_perf.c
@@ -0,0 +1,102 @@
+#include "monitor_perf.h"
+
+static struct perf_event *pe;
+
+void vm_perf_overflow_callback(struct perf_event *event,
+ struct perf_sample_data *data,
+ struct pt_regs *regs) {
+ // handle perf event data
+ // struct perf_callchain_entry *callchain;
+ // int nr, i;
+
+ pr_info("perf event callback\n");
+
+ // 如果 perf_sample_data 有调用堆栈信息
+ // if (data->callchain) {
+ // callchain = data->callchain;
+ // nr = callchain->nr; // 调用堆栈的长度
+
+ // // 遍历堆栈条目并处理它们
+ // for (i = 0; i < nr; i++) {
+ // // callchain->ip[i] 包含了堆栈的每个条目
+ // // 在这里可以调用 to_buff 将堆栈信息写入缓冲区
+ // // to_buff(&callchain->ip[i], sizeof(callchain->ip[i]));
+ // pr_info("callchain->ip[%d] = %llx\n", i, callchain->ip[i]);
+ // }
+ // }
+}
+
+// static struct perf_event_attr pea = {
+// .type = PERF_TYPE_SOFTWARE, // software event
+// .size = sizeof(struct perf_event_attr), // size of attr
+// .config = PERF_COUNT_SW_CPU_CLOCK, // no care
+// PERF_COUNT_SW_DUMMY PERF_COUNT_SW_CPU_CLOCK .sample_period = 1, // sample
+// every 1 event .sample_type =
+// PERF_SAMPLE_CALLCHAIN, // sample callchain | means include stacktrace
+// // .exclude_kernel = 1, // no kernel stacktrace | may need to change
+// after test
+// // .exclude_hv = 1, // no hypervisor stacktrace | may need to change
+// after test .disabled = 0, // disabled at first
+// };
+
+struct perf_event_attr pea = {
+ .type = PERF_TYPE_SOFTWARE,
+ .size = sizeof(struct perf_event_attr),
+ .config = PERF_COUNT_SW_CPU_CLOCK,
+ .sample_period = 1,
+ .sample_type = PERF_SAMPLE_CALLCHAIN,
+ // .disabled = 1,
+};
+
+#include <linux/cpumask.h>
+#include <linux/smp.h>
+/**
+ * @brief Set the up perf event for task object
+ *
+ * @param tsk
+ */
+void setup_perf_event_for_task(struct task_struct *tsk) {
+ pr_info("setup_perf_event_for_task: cpu = %d\n", tsk->on_cpu);
+ if (pe) {
+ pr_info("Perf event already created\n");
+ return;
+ }
+ int cpu;
+ struct perf_event **events;
+ for_each_possible_cpu(cpu) {
+ struct perf_event **event = per_cpu_ptr(events, cpu);
+ if (cpu_is_offline(cpu)) {
+ *event = NULL;
+ continue;
+ }
+ *event = perf_event_create_kernel_counter(&pea, cpu, tsk,
+ vm_perf_overflow_callback, NULL);
+ if (IS_ERR(*event)) {
+ printk(KERN_INFO "create perf event failure\n");
+ // return -1;
+ }
+ }
+
+ // pe = perf_event_create_kernel_counter(&pea, tsk->on_cpu, tsk,
+ // vm_perf_callback, NULL);
+ pe = perf_event_create_kernel_counter(&pea, tsk->on_cpu, tsk,
+ vm_perf_overflow_callback, NULL);
+ if (IS_ERR(pe)) {
+ pr_info("Error in perf_event_create_kernel_counter\n");
+ return;
+ }
+
+ // perf_event_enable(pe); // enable perf event
+}
+
+/**
+ * @brief Disable perf event
+ *
+ */
+void cleanup_perf_event(void) {
+ if (pe) {
+ perf_event_disable(pe);
+ perf_event_release_kernel(pe);
+ pe = NULL;
+ }
+} \ No newline at end of file
diff --git a/source/module/monitor_perf.h b/source/module/monitor_perf.h
new file mode 100644
index 0000000..714e3a4
--- /dev/null
+++ b/source/module/monitor_perf.h
@@ -0,0 +1,8 @@
+#include <linux/hw_breakpoint.h>
+#include <linux/perf_event.h>
+#include <linux/sched.h>
+#include <linux/uaccess.h>
+// #include <sys/types.h>
+
+void setup_perf_event_for_task(struct task_struct *tsk);
+void cleanup_perf_event(void);
diff --git a/source/module/monitor_timer.h b/source/module/monitor_timer.h
index 4109038..c62c463 100644
--- a/source/module/monitor_timer.h
+++ b/source/module/monitor_timer.h
@@ -35,10 +35,11 @@ typedef struct {
unsigned sentinel; // sentinel
kernel_watch_arg
k_watch_args[TIMER_MAX_WATCH_NUM]; // all watched kernel_watch_arg
- int threshold_num; // all over threshold number,
+ int threshold_over_count; // all over threshold number,
// 0 means no handle needed
int threshold_buffer[TIMER_MAX_WATCH_NUM]; //
struct work_struct wk; // for handle
+ unsigned long long tv; // time
} kernel_watch_timer;
// Global variable
diff --git a/source/module/monitor_trace.h b/source/module/monitor_trace.h
index 4cdd24c..4098f1a 100644
--- a/source/module/monitor_trace.h
+++ b/source/module/monitor_trace.h
@@ -36,7 +36,7 @@ typedef struct {
int et_type;
unsigned long id;
unsigned long long tv;
- int threshold_num;
+ int threshold_over_count;
threshold threshold_record[TIMER_MAX_WATCH_NUM];
} variable_monitor_record;
diff --git a/source/ucli/ucli.cc b/source/ucli/ucli.cc
index 8b8460f..fa26448 100644
--- a/source/ucli/ucli.cc
+++ b/source/ucli/ucli.cc
@@ -30,7 +30,7 @@ static int task_info_extract(void *buf, unsigned int len, void *) {
printf("threshold exceeded, Timestamp %lld :\n", vm_record->tv);
- for (int i = 0; i < vm_record->threshold_num; i++) {
+ for (int i = 0; i < vm_record->threshold_over_count; i++) {
printf("\t: pid: %d, name: %s, ptr: %p, threshold:%d, true_value:%d\n",
vm_record->threshold_record[i].task_id,
vm_record->threshold_record[i]
diff --git a/source/ucli/ucli.h b/source/ucli/ucli.h
index af62e91..db12376 100644
--- a/source/ucli/ucli.h
+++ b/source/ucli/ucli.h
@@ -54,7 +54,7 @@ typedef struct {
int et_type;
unsigned long id;
unsigned long long tv;
- int threshold_num;
+ int threshold_over_count;
threshold threshold_record[TIMER_MAX_WATCH_NUM];
} variable_monitor_record;