I have a problem with schedule() and sys_perf_event_open.
Hello. I am hacking the kernel source for studying. I just modified schedule() in kernel/sched.c for profiling processes. I want to count the number of cache miss, so added the code using sys_perf_event_open. However, it seems to returns always error fd. I hope to know what should I do for getting the number of cache miss. Here is the diff code based at kernel version 2.6.37 Thanks for reading this e-mail. diff --git a/kernel/sched.c b/kernel/sched.c index 297d1a0..afe68bc 100644 --- a/kernel/sched.c +++ b/kernel/sched.c @@ -72,6 +72,7 @@ #include <linux/ctype.h> #include <linux/ftrace.h> #include <linux/slab.h> +#include <linux/syscalls.h> #include <asm/tlb.h> #include <asm/irq_regs.h> @@ -4061,6 +4062,30 @@ pick_next_task(struct rq *rq) BUG(); /* the idle class will always have a runnable task */ } +static void profile(struct task_struct *task) +{ + int fd; + size_t res; + u64 count; + struct perf_event_attr attr = + { + .type = PERF_TYPE_HARDWARE, + .config = PERF_COUNT_HW_CACHE_MISSES, + .size = sizeof(struct perf_event_attr) + }; + int res_close; + + BUG_ON(task == 0); + + fd = sys_perf_event_open(&attr, 0, -1, -1, 0); + BUG_ON(fd < 0); + res = sys_read(fd, (char *)&count, sizeof(u64)); + BUG_ON(res != sizeof(u64)); + res_close = sys_close(fd); + BUG_ON(res_close == -1); + pr_info("PERF: %d %u %llu %d", fd, res, count, res_close); +} + /* * schedule() is the main scheduler function. */ @@ -4080,6 +4105,7 @@ need_resched: release_kernel_lock(prev); need_resched_nonpreemptible: + profile(prev); schedule_debug(prev);
Hi Minwoo... I am not the best... just trying to give idea here... On Tue, Mar 15, 2011 at 00:03, Minwoo Lee <ermaker@gmail.com> wrote:
Hello. I am hacking the kernel source for studying. I just modified schedule() in kernel/sched.c for profiling processes.
basic question is: are you sure the cache misses you see during schedule is entirely attributed to the soon-to-be-switched-out process? have you consider that during that process run time, on going interrupts/exception etc could attribute into the cache misses? and about this line: BUG_ON(res != sizeof(u64)) not sure if I read it correctly, but previously I see "res" is declared as size_t, right? -- regards, Mulyadi Santosa Freelance Linux trainer and consultant blog: the-hydra.blogspot.com training: mulyaditraining.blogspot.com
Thanks for reply. 2011/3/15 Mulyadi Santosa <mulyadi.santosa@gmail.com>
Hi Minwoo...
I am not the best... just trying to give idea here...
On Tue, Mar 15, 2011 at 00:03, Minwoo Lee <ermaker@gmail.com> wrote:
Hello. I am hacking the kernel source for studying. I just modified schedule() in kernel/sched.c for profiling processes.
basic question is: are you sure the cache misses you see during schedule is entirely attributed to the soon-to-be-switched-out process? have you consider that during that process run time, on going interrupts/exception etc could attribute into the cache misses?
You're right. This code is wrong. I wrote the code just for checking that the perf library runs. The code should be changed to check cache misses while in a slice time(or an unit time?). The main problem is, sys_pref_event_open does not return the valid fd. I have no idea why sys_perf_event_open returns invalid fd.
and about this line: BUG_ON(res != sizeof(u64))
not sure if I read it correctly, but previously I see "res" is declared as size_t, right?
Yes, res is declared as size_t. Also type of sizeof() is size_t. Isn't it right? This line assumes that sys_read() read correctly. This line is from the code in line 227 at tools/perf/builtin-stat.c, 225 res = read(fd[cpu][counter][thread], 226 single_count, nv * sizeof(u64)); 227 assert(res == nv * sizeof(u64));
-- regards,
Mulyadi Santosa Freelance Linux trainer and consultant
blog: the-hydra.blogspot.com training: mulyaditraining.blogspot.com
participants (2)
-
Minwoo Lee -
Mulyadi Santosa