Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7509085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:48:08+00:00 2026-05-29T22:48:08+00:00

I have to do a system call to count the voluntary & involuntary context

  • 0

I have to do a system call to count the voluntary & involuntary context switches of a process. I already know the steps to add a new system call to a linux kernel but i have no clue of where i should start for the context-switch function. Any idea?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:48:11+00:00Added an answer on May 29, 2026 at 10:48 pm

    If your syscall should only report statistics, you can use context switch counting code that is already in the kernel.

    wait3 syscall or getrusage syscall already reports context switch count in struct rusage fields:

    struct rusage {
     ...
        long   ru_nvcsw;         /* voluntary context switches */
        long   ru_nivcsw;        /* involuntary context switches */
    };
    

    You can try it by running:

    $ /usr/bin/time -v /bin/ls -R
    ....
        Voluntary context switches: 1669
        Involuntary context switches: 207
    

    where “/bin/ls -R” is any program.

    By searching an “struct rusage” in kernel sources, you can find this accumulate_thread_rusage in kernel/sys.c, which updates rusage struct. It reads from struct task_struct *t; the fields t->nvcsw; and t->nivcsw;:

    1477  static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
    1478  {
    1479        r->ru_nvcsw += t->nvcsw;    // <<=== here
    1480        r->ru_nivcsw += t->nivcsw;
    1481        r->ru_minflt += t->min_flt;
    1482        r->ru_majflt += t->maj_flt;
    

    Then you should search nvcsw and nivcsw in kernel folder to find how they are updated by kernel.

    asmlinkage void __sched schedule(void):

    4124     if (likely(prev != next)) {         // <= if we are switching between different tasks
    4125            sched_info_switch(prev, next);
    4126            perf_event_task_sched_out(prev, next);
    4127
    4128            rq->nr_switches++;          
    4129            rq->curr = next;
    4130            ++*switch_count;     // <= increment nvcsw or nivcsw via pointer
    4131
    4132            context_switch(rq, prev, next); /* unlocks the rq */
    

    Pointer switch_count is from line 4091 or line 4111 of the same file.

    PS: Link from perreal is great: http://oreilly.com/catalog/linuxkernel/chapter/ch10.html (search context_swtch)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have system here that uses Java JNI to call a function in a
I have a program which does a system call: latex somefile.latex This runs ok,
I have a doubt regarding the backlog value in listen system call. From man
I have a form MyForm : System.Windows.Forms.Form {} and I need to call Site.GetService(..)
I have a library that interacts with our phone system, ie, Hey phone, call
I have a ASP.NET MVC page, which call WCF logic. The system is single-signon
I have a process, which creates a dynamic list of timers(System.Threading.Timer) and continues to
I have two Slackware Linux systems on which the POSIX semaphore sem_open() call fails
I have System.Collections.Generic.Dictionary<A, B> dict where A and B are classes, and an instance
Let's say we have system A comprising a MySQL database, with several tables. After

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.