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 641417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:03:38+00:00 2026-05-13T21:03:38+00:00

Can we write a c program to find out time spent in context switch

  • 0

Can we write a c program to find out time spent in context switch in Linux? Could you please share code if you have one?
Thanks

  • 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-13T21:03:38+00:00Added an answer on May 13, 2026 at 9:03 pm

    Profiling the switching time is very difficult, but the in-kernel latency profiling tools, as well as oprofile (which can profile the kernel itself) will help you there.

    For benchmarking the interactive application performance, I have written a small tool called latencybench that measures unexpected latency spikes:

    // Compile with g++ latencybench.cc -o latencybench -lboost_thread-mt
    // Should also work on MSVC and other platforms supported by Boost.
    
    #include <boost/format.hpp>
    #include <boost/thread/thread.hpp>
    #include <boost/date_time.hpp>
    #include <algorithm>
    #include <cstdlib>
    #include <csignal>
    
    volatile bool m_quit = false;
    
    extern "C" void sighandler(int) {
        m_quit = true;
    }
    
    std::string num(unsigned val) {
        if (val == 1) return "one occurrence";
        return boost::lexical_cast<std::string>(val) + " occurrences";
    }
    
    int main(int argc, char** argv) {
        using namespace boost::posix_time;
        std::signal(SIGINT, sighandler);
        std::signal(SIGTERM, sighandler);
        time_duration duration = milliseconds(10);
        if (argc > 1) {
            try {
                if (argc != 2) throw 1;
                unsigned ms = boost::lexical_cast<unsigned>(argv[1]);
                if (ms > 1000) throw 2;
                duration = milliseconds(ms);
            } catch (...) {
                std::cerr << "Usage: " << argv[0] << " milliseconds" << std::endl;
                return EXIT_FAILURE;
            }
        }
        typedef std::map<long, unsigned> Durations;
        Durations durations;
        unsigned samples = 0, wrongsamples = 0;
        unsigned max = 0;
        long last = -1;
        std::cout << "Measuring actual sleep delays when requesting " << duration.total_milliseconds() << " ms: (Ctrl+C when done)" << std::endl;
        ptime begin = boost::get_system_time();
        while (!m_quit) {
            ptime start = boost::get_system_time();
            boost::this_thread::sleep(start + duration);
            long actual = (boost::get_system_time() - start).total_milliseconds();
            ++samples;
            unsigned num = ++durations[actual];
            if (actual != last) {
                std::cout << "\r  " << actual << " ms " << std::flush;
                last = actual;
            }
            if (actual != duration.total_milliseconds()) {
                ++wrongsamples;
                if (num > max) max = num;
                std::cout << "spike at " << start - begin << std::endl;
                last = -1;
            }
        }
        if (samples == 0) return 0;
        std::cout << "\rTotal measurement duration:  " << boost::get_system_time() - begin << "\n";
        std::cout << "Number of samples collected: " << samples << "\n";
        std::cout << "Incorrect delay count:       " << wrongsamples << boost::format(" (%.2f %%)") % (100.0 * wrongsamples / samples) << "\n\n";
        std::cout << "Histogram of actual delays:\n\n";
        unsigned correctsamples = samples - wrongsamples;
        const unsigned line = 60;
        double scale = 1.0;
        char ch = '+';
        if (max > line) {
            scale = double(line) / max;
            ch = '*';
        }
        double correctscale = 1.0;
        if (correctsamples > line) correctscale = double(line) / correctsamples;
        for (Durations::const_iterator it = durations.begin(); it != durations.end(); ++it) {
            std::string bar;
            if (it->first == duration.total_milliseconds()) bar = std::string(correctscale * it->second, '>');
            else bar = std::string(scale * it->second, ch);
            std::cout << boost::format("%5d ms | %s %d") % it->first % bar % it->second << std::endl;
        }
        std::cout << "\n";
        std::string indent(30, ' ');
        std::cout << indent << "+-- Legend ----------------------------------\n";
        std::cout << indent << "|  >  " << num(1.0 / correctscale) << " (of " << duration.total_milliseconds() << " ms delay)\n";
        if (wrongsamples > 0) std::cout << indent << "|  " << ch << "  " << num(1.0 / scale) << " (of any other delay)\n";
    }
    

    Results on Ubuntu 2.6.32-14-generic kernel. While measuring, I was compiling C++ code with four cores and playing a game with OpenGL graphics at the same time (to make it more interesting):

    Total measurement duration:  00:01:45.191465
    Number of samples collected: 10383
    Incorrect delay count:       196 (1.89 %)
    
    Histogram of actual delays:
    
       10 ms | >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 10187
       11 ms | *************************************************** 70
       12 ms | ************************************************************ 82
       13 ms | ********* 13
       14 ms | ********* 13
       15 ms | ** 4
       17 ms | *** 5
       18 ms | * 2
       19 ms | **** 6
       20 ms |  1
    
                                  +-- Legend ----------------------------------
                                  |  >  169 occurrences (of 10 ms delay)
                                  |  *  one occurrence (of any other delay)
    

    With rt-patched kernels I get much better results, pretty much 10-12 ms only.

    The legend in the printout appears to be suffering of a rounding error or something (and the source code pasted is not the exact same version). I never really polished this application for a release…

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

Sidebar

Ask A Question

Stats

  • Questions 432k
  • Answers 432k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It is doable with a central repo with access (push/pull)… May 15, 2026 at 2:41 pm
  • Editorial Team
    Editorial Team added an answer I use this - http://css3please.com/. It allows you to try… May 15, 2026 at 2:41 pm
  • Editorial Team
    Editorial Team added an answer According to the documentation page for the Closing event: If… May 15, 2026 at 2:41 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.