2

I am needing to identify whether a user process was ever preempted somehow, I understand we have hooks in preempt.h and sched.c which allow us to define preempt_notifiers which can in turn call sched_in and sched_out functions whenever a process is rescheduled or preempted.

But I still can't find out how can I attach a notifier to a particular process or pid in user space and then somehow log if this particular process was ever pre-empted. I'm assuming I have to write a module to do so, but how would I go about attaching a pid to a particular notifier?

Mahesh M
  • 21
  • 2

2 Answers2

2

The notifier is inherently per-process. When you register it, you are registering it for the current process. See the code in preempt_notifier_register(), it attaches the notifer to current->preempt_notifiers.

mpe
  • 2,640
  • 1
  • 19
  • 17
  • Yes , that is correct , but preempt_notifier_register() can only be accessed by a kernel module , how can i determine that current is the user process that i want to have the notifier registered for. IS there a way to pin a notifier to a particular pid, i.e current.pid = my pid . – Mahesh M Aug 14 '12 at 15:25
  • The process you want to monitor needs to call your kernel module, then `current` will be the right process. – mpe Aug 14 '12 at 23:29
  • Sorry I am new at this , so should I make a system call from user process to kernel module , how do i go about doing that ? I need a really low latency call from user process which could access the module functions.Any ideas ? – Mahesh M Aug 15 '12 at 04:59
  • You could add a syscall, that would be one way to do it. But adding a syscall in a module is a little complicated - because the syscall may be called when the module is not loaded. Another option is to have your module create a char device and use the `ioctl()` callback as a pseduo-syscall, that is how KVM is implemented for example. – mpe Aug 15 '12 at 11:31
0

The pseudo-file /proc/<pid>/status contains a line nonvoluntary_ctxt_switches: which seems to be the information that you're after.

caf
  • 233,326
  • 40
  • 323
  • 462
  • I'm not sure it does. Looking at the code in __schedule() it looks like that is also incremented when a process runs to the end of its time slice. But I could be wrong. – mpe Aug 15 '12 at 11:30