Hello Community,

I am trying to figure a simple way approximate cost of context switching for a given OS/hardware combination under different loads.

For example on my 2 GHz dual core MAC OSX, running single instance clocks at 0.5 usec where as three instance at 1.3 usec. Below is sample, appreciate feedback, is this a valid approximation? If not why? Code below is compiled/tested on MAC OSX.

#include <iostream>
#include <sched.h>
#include <sys/time.h>

class Timer
{
public:
	Timer()
	{
	}
	void start()
	{
                gettimeofday(&_start, NULL);
	}
	double elapsed()
	{
		timeval stop;
                gettimeofday(&stop, NULL);

   		const int64_t USEC = 1000000;
		int64_t t1=_start.tv_sec*USEC+_start.tv_usec;
		int64_t t2=stop.tv_sec*USEC+stop.tv_usec;
		
		return (double)t2-t1;
	}
private:
	timeval _start;
};

void yield(int64_t loop)
{
    Timer timer;

    timer.start(); 

   for(int64_t i=0; i < loop; i++)
   {
         sched_yield();
   }

   double elapsed = timer.elapsed();

   double avg = elapsed/loop;

   std::cout << "yield loop=[" << loop << "] avg=[" << avg << "]" << std::endl;
}

int main(int argc, char *argv[])
{
        int64_t loop = 100000000;

        if( argc > 1 ) loop = atoi(argv[1]);

        yield(loop);
}

Recommended Answers

All 5 Replies

> is this a valid approximation?

I don't think so.

> If not why?

Yielding CPU is not necessarily a context switch: control could be returned to the same context without remapping. I don't see anything forcing a switch.

The sample code gives a reliable lower bound of a system call/return though.

good day every one i am osagie by name, i've been trying to develope a biometric(fingerprint) software in win32 that will serialise and save all user fingerprint record to a binary mode cpp file(after due analysis).  Thereafter, d program is expected to send a copy of the serialied file to a php script in a server for deserialisation and save on a database. my problems are:   1.  how can i use socket to upload d file(serialised) to d remote php file without using a browse(i.e without any web/html interface)?                                                      2. how can i also read(download)from d server using only d same socket(recv()) without a web browser? i.e both upload nd download should be done by my application....                                    pls only reply me if u have an idea of what am talking about. a little snippet and/or tutorial link wld b highly appreciated

Yielding CPU is not necessarily a context switch: control could be returned to the same context without remapping. I don't see anything forcing a switch.

The sample code gives a reliable lower bound of a system call/return though.

True if a single instance is run. However, if two instances are run simultaneously on a single core, since yield would occur before a thread quantum has expired and CPU is 100%, isn’t an implicit context switch expected? (ignoring all the other background process on the machine for now)

As indicated in the original post, on dual core simultaneously execution of 1 or 2 instances give an average of 0.5 usec, where as running three instance shows 1.3 usec.

A) The first case is mostly likely mostly function call overhead, since each process would have its own core and context switch is not required.

B) The second case, context switch would most likely occur frequently, which may explain the jump in time when 3 processes are running.

Just curious why do you want this information?

Time critical application where context switching is a factor in building up latency as event goes through multiple transformations. Besides this is good information for all students, favorite Google interview question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.