Want more features on Pastebin? Sign Up, it's FREE!
Guest

Linux kernel schedule latency test

By: a guest on Aug 24th, 2012  |  syntax: C++  |  size: 2.59 KB  |  views: 1,269  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <iostream>
  2. #include <vector>
  3. #include <pthread.h>
  4. #include <sys/time.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. double CpuFrequency=3601.0; // CPU frequency in MHz
  9.  
  10. pthread_cond_t cv;
  11. pthread_mutex_t m;
  12. pthread_t thread2;
  13.  
  14. struct timeval before, after;
  15.  
  16. typedef unsigned long long ticks;
  17. unsigned long long beforeTicks, afterTicks;
  18.  
  19. static __inline__ ticks getrdtsc()
  20. {
  21.      unsigned a, d;
  22. //     asm("cpuid"); // We don't need to cause a pipeline stall for this test
  23.      asm volatile("rdtsc" : "=a" (a), "=d" (d));
  24.  
  25.      return (((ticks)a) | (((ticks)d) << 32));
  26. }
  27.  
  28. void *beginthread2(void *v)
  29. {
  30.    for (;;)
  31.    {
  32.       // Wait for a signal from thread 1
  33.       pthread_mutex_lock(&m);
  34.       pthread_cond_wait(&cv, &m);
  35.  
  36.       // Some dequeue op would normally be performed here after a spurious wake
  37.       // up test
  38.  
  39.       // Get the ending ticks
  40.       afterTicks=getrdtsc();
  41.       pthread_mutex_unlock(&m);
  42.  
  43.       // Display the time elapsed
  44.       std::cout << "Ticks elapsed: " << afterTicks-beforeTicks << " ("
  45.                 << (afterTicks-beforeTicks)/CpuFrequency << " us)\n";
  46.    }
  47.  
  48.    return NULL;
  49. }
  50.  
  51. int main(int argc, char *argv[])
  52. {
  53.    int core1=0, core2=0;
  54.  
  55.    if (argc < 3)
  56.    {
  57.       std::cout << "Usage: " << argv[0] << " producer_corenum consumer_corenum" << std::endl;
  58.       return 1;
  59.    }
  60.  
  61.    // Get core numbers on which to perform the test
  62.    core1 = atoi(argv[1]);
  63.    core2 = atoi(argv[2]);
  64.  
  65.    std::cout << "Core 1: " << core1 << std::endl;
  66.    std::cout << "Core 2: " << core2 << std::endl;
  67.  
  68.    pthread_mutex_init(&m, NULL);
  69.    pthread_cond_init(&cv, NULL);
  70.  
  71.    cpu_set_t cpuset;
  72.  
  73.    CPU_ZERO(&cpuset);
  74.    CPU_SET(core1, &cpuset);
  75.  
  76.    // Set affinity of the first (current) thread to core1
  77.    pthread_t self=pthread_self();
  78.    if (pthread_setaffinity_np(self, sizeof(cpu_set_t), &cpuset)!=0)
  79.    {
  80.       perror("pthread_setaffinity_np");
  81.       return 1;
  82.    }
  83.  
  84.    CPU_ZERO(&cpuset);
  85.    CPU_SET(core2, &cpuset);
  86.  
  87.    // Create second thread
  88.    pthread_create(&thread2, NULL, beginthread2, NULL);
  89.    // Set affinity of the second thread to core2
  90.    if (pthread_setaffinity_np(thread2, sizeof(cpu_set_t), &cpuset)!=0)
  91.    {
  92.       perror("pthread_setaffinity_np");
  93.       return 1;
  94.    }
  95.  
  96.    // Run the test
  97.    for (;;)
  98.    {
  99.       // Sleep for one second
  100.       sleep(1);
  101.       // Get the starting ticks
  102.       beforeTicks=getrdtsc();
  103.  
  104.       // Signal thread 2
  105.       pthread_mutex_lock(&m);
  106.       // Some enqueue op would normally be performed here
  107.       pthread_cond_signal(&cv);
  108.       pthread_mutex_unlock(&m);
  109.    }
  110. }
clone this paste RAW Paste Data