I am designing a utility to count the total number of context switches performed by the kernel in a given time period. For this purpose I was thinking of using getrusage() system call. Since I need total number of context switches I would use getrusage(RUSAGE_CHILDREN,rusage) via init process. Since init is the parent of all processes we should get total number of context switches. I want to add a signal handler to init so that whenever it receives SIGUSR1 it executes getrusage() and create a log file. But I do not know how to associate a signal handler with a process. So I want to know how to add a custom signal handlwer to init.
Asked
Active
Viewed 346 times
1
-
I think this calls for some actual code which then could be improved based on answers you get. – Jan 29 '14 at 08:02
-
You title is actually misleading are your final question is "how to add a custom signal handler to init". Please edit your question to correctly reflect what is your issue. – Christophe Augier Jan 29 '14 at 18:05
-
How is it different from adding a regular custom signal handler? – buc030 Jan 29 '14 at 20:10
-
@buc030 exactly my point. The question title is about context switches when the actual question is probably either "how to set a signal handler?" or "how to modify the process init?". – Christophe Augier Jan 30 '14 at 09:32
-
What's wrong with existing tools to trace these: http://stackoverflow.com/q/2914187/1401351. Especially see `perf`, which you probably already have on your system, and will easily do simple things like count context switches. – Peter Jan 30 '14 at 14:38