LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-06-2012, 12:18 AM   #1
Joohi
LQ Newbie
 
Registered: Feb 2012
Posts: 4

Rep: Reputation: Disabled
How to calculate the context switches and disk reads that my C program makes


I need to count the total number of context switches and disk reads that a given C program makes. The nearest thing that I could find was vmstat command that monitors the system performance. But it gives out details of all the process that have been executed since boot time, and doesn't give a PID. How do I calculate the required parameters of a program that has already completed execution?
 
Old 02-06-2012, 01:53 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,999

Rep: Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219
It looks like Mark is planning on adding this (context switch data) to collectl - I'm sure he'll comment shortly.
If you want to track the process yourself, the data is in /proc/<pid>/status (presuming you have the appropriate kernel options selected) - but that data will be lost at process termination unless you do something to save it.
 
Old 02-08-2012, 04:35 AM   #3
Joohi
LQ Newbie
 
Registered: Feb 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
The function - getrusage() can be used, it provides all the required parameters.
 
Old 02-10-2012, 06:05 PM   #4
markseger
Member
 
Registered: Jul 2003
Posts: 242

Rep: Reputation: 26
Indeed mark IS planning on providing context switches by process. The process output has gotten so wide I've decided to make this optional by requesting 'extended' process stats like this, noting that extended stats apply to both top, interactive and playback. This is an example of top, sorted by voluntary context switches:

collectl --top vctx --procopt x

Code:
# TOP PROCESSES sorted by vctx (counters are /sec) 23:04:17
# PID  User     PR  PPID THRD S   VSZ   RSS CP  SysT  UsrT Pct  AccuTime  RKB  WKB VCtx NCtx MajF MinF Command
21171  1333     20 21170    0 S   42M    2M 12  0.01  0.00   1  00:03.83    0    0  309   21    0    0 ssh
21172  1333     20 21170    0 S    4M  584K 12  0.00  0.02   2  00:05.08    0    0  309    0    0    0 gzip
21198  1333     20 21195    0 S    4M  588K  8  0.00  0.02   2  00:04.19    0    0  263    1    0    0 gzip
21047  1333     20 21045    0 S   42M    2M  7  0.01  0.01   2  00:03.26    0    0  262    8    0    0 ssh
21197  1333     20 21195    0 S   42M    2M 20  0.01  0.00   1  00:03.33    0    0  262    2    0    0 ssh
21048  1333     20 21045    0 S    4M  584K  1  0.00  0.02   2  00:04.46    0    0  261    1    0    0 gzip
21071  1333     20 21070    0 S   42M    2M  0  0.01  0.00   1  00:03.30    0    0  261    0    0    0 ssh
I'm planning on pushing a new release next week, just waiting for some feedback from a few folks to make sure I didn't break anything they need.

-mark

Last edited by markseger; 02-11-2012 at 08:34 AM.
 
Old 02-10-2012, 07:58 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,999

Rep: Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219
Code:
# TOP PROCESSES sorted by vctx (counters are /sec) 23:04:17
# PID  User     PR  PPID THRD S   VSZ   RSS CP  SysT  UsrT Pct  AccuTime  RKB  WKB VCtx NCtx MajF MinF Command
21171  1333     20 21170    0 S   42M    2M 12  0.01  0.00   1  00:03.83    0    0  309   21    0    0 ssh
21172  1333     20 21170    0 S    4M  584K 12  0.00  0.02   2  00:05.08    0    0  309    0    0    0 gzip
21198  1333     20 21195    0 S    4M  588K  8  0.00  0.02   2  00:04.19    0    0  263    1    0    0 gzip
21047  1333     20 21045    0 S   42M    2M  7  0.01  0.01   2  00:03.26    0    0  262    8    0    0 ssh
21197  1333     20 21195    0 S   42M    2M 20  0.01  0.00   1  00:03.33    0    0  262    2    0    0 ssh
21048  1333     20 21045    0 S    4M  584K  1  0.00  0.02   2  00:04.46    0    0  261    1    0    0 gzip
21071  1333     20 21070    0 S   42M    2M  0  0.01  0.00   1  00:03.30    0    0  261    0    0    0 ssh
 
Old 02-11-2012, 08:36 AM   #6
markseger
Member
 
Registered: Jul 2003
Posts: 242

Rep: Reputation: 26
thanks syg00 - I went back and fixed it. I know some websites have an icon to click on to insert the right formatting tags but the only one I found was for QUOTE, so I used that and s/QUOTE/CODE/

-mark
 
Old 02-11-2012, 11:41 AM   #7
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
The icon for "CODE" should be in the quick reply box, but isn't ... this has been requested but I think the plan is not to customise the board further before updating to a new version of vbulletin.

If you "Go Advanced" and get the full reply editor, the code icon is the one with a # symbol.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between buffered disk reads and cached reads? pinga123 Linux - Newbie 1 07-13-2011 12:40 PM
Lots of context switches - sorry best I could do! jrcs Linux - Kernel 6 01-26-2011 09:18 AM
context switches user777 Solaris / OpenSolaris 3 12-05-2008 07:59 PM
Number of context switches linuxdoniv Programming 1 05-08-2008 08:50 AM
Context switches balteo Linux - General 0 09-06-2005 08:09 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration