LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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-05-2012, 11:18 PM   #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, 12:53 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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, 03: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, 05:05 PM   #4
markseger
Member
 
Registered: Jul 2003
Posts: 244

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 07:34 AM.
 
Old 02-10-2012, 06:58 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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, 07:36 AM   #6
markseger
Member
 
Registered: Jul 2003
Posts: 244

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, 10: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



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 11:40 AM
Lots of context switches - sorry best I could do! jrcs Linux - Kernel 6 01-26-2011 08:18 AM
context switches user777 Solaris / OpenSolaris 3 12-05-2008 06:59 PM
Number of context switches linuxdoniv Programming 1 05-08-2008 07:50 AM
Context switches balteo Linux - General 0 09-06-2005 07:09 AM

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

All times are GMT -5. The time now is 04:05 AM.

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
Open Source Consulting | Domain Registration