Find the answer to your Linux question:
Results 1 to 5 of 5
I have done some searching around the internet and this site, but I haven't found a good way to count the context switches on a thread in a c++ program ...
  1. #1
    Just Joined!
    Join Date
    May 2010
    Posts
    28

    counting context switches?

    I have done some searching around the internet and this site, but I haven't found a good way to count the context switches on a thread in a c++ program I am running. I need to know if it get swapped off of a CPU for correct timing.

    Anyone have any idea?

  2. #2
    Linux Newbie
    Join Date
    Mar 2010
    Posts
    121
    Quote Originally Posted by blah32 View Post
    I need to know if it get swapped off of a CPU for correct timing.
    I don't think I get what you mean. Why is this?

    Does your program rely on context switches happening/not happening at a certain time? If so, there are much, much better ways of synchronising threads than counting context switches. If that is what you're trying to do, there's no way that plan will ever end well.

  3. #3
    Linux Newbie
    Join Date
    Mar 2010
    Posts
    121
    Oh yeah, I nearly forgot - if you really do need to find out how many context switches you've encountered, the proc(5) man page says /proc/[pid]/status has this information. Would be interesting to know why you need this, though.

  4. #4
    Just Joined!
    Join Date
    May 2010
    Posts
    28
    I am not sync-ing threads with it. I need to know if my thread ever gets switched off a core. I gave it HIGH_PRIORITY scheduling and FIFO policy on linux. So unless there is a realtime event that requires the core, it should get to keep it, right? But I need to make sure linux isn't taking it off that core....thus I need to see if its doing context switches.

    Quote Originally Posted by JohnGraham View Post
    Oh yeah, I nearly forgot - if you really do need to find out how many context switches you've encountered, the proc(5) man page says /proc/[pid]/status has this information. Would be interesting to know why you need this, though.
    How long does that take? I was certainly hoping for something less than an io call. A system call is bad enough, but do-able.

  5. #5
    Just Joined!
    Join Date
    May 2010
    Posts
    28
    Quote Originally Posted by blah32 View Post
    I am not sync-ing threads with it. I need to know if my thread ever gets switched off a core. I gave it HIGH_PRIORITY scheduling and FIFO policy on linux. So unless there is a realtime event that requires the core, it should get to keep it, right? But I need to make sure linux isn't taking it off that core....thus I need to see if its doing context switches.



    How long does that take? I was certainly hoping for something less than an io call. A system call is bad enough, but do-able.
    Also, does that only work per processor or will it work per thread if I use the tid?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...