Skip to content

Instantly share code, notes, and snippets.

@mahmoudimus
Created May 18, 2010 21:04
Show Gist options
  • Select an option

  • Save mahmoudimus/405547 to your computer and use it in GitHub Desktop.

Select an option

Save mahmoudimus/405547 to your computer and use it in GitHub Desktop.
Speed test for context switch timers
#include <sched.h>
#include <sys/time.h>
#include <signal.h>
#include <pthread.h>
#include <cstdint>
#include <cstdlib>
#include <iostream>
#define NUM_THREADS 20
namespace {
pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
volatile long switches = 0L;
long msec = 0L;
/**
* Current GMT time in ms since the Epoch
*/
uint64_t epochmsec() {
timeval tv = timeval();
if (gettimeofday(&tv, 0) == 0) {
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
else {
return 0L;
}
}
/**
* ALRM signal handler, logs context switching statistics
*/
void alarm_handler(int signum) {
alarm(1);
uint64_t tm = epochmsec();
uint64_t delta = tm - msec;
msec = tm;
std::cout << (delta / (double) switches * 1000) << "ns" << std::endl;
switches = 0;
}
/**
* Thread function
*/
void* thread_func(void* arg) {
for(;;) {
pthread_mutex_lock(&mtx);
++switches;
pthread_mutex_unlock(&mtx);
sched_yield();
}
}
} // namespace
int main() {
msec = epochmsec();
pthread_t threads[NUM_THREADS];
for (size_t i = 0; i < sizeof threads; ++i) {
pthread_create(&threads[i], 0, &thread_func, 0);
}
// setup an alarm to print log message every second
signal(SIGALRM, &alarm_handler);
alarm(1);
for (;;) {
sleep(1000);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment