这是衡量线程上下文切换开销的正确解决方案吗?

时间:2017-02-15 06:59:36

标签: c multithreading pthreads context-switch

我试图测量线程切换开销时间。 我有两个线程,一个共享变量,一个互斥锁和两个条件变量。两个线程将来回切换以向共享变量写入1或0。

我假设pthread_cond_wait(& cond,& mutex)等待时间大约等于2 x线程上下文切换时间。因为如果thread1必须等待条件变量,它必须放弃对thread2-> thread2上下文切换 - >的互斥锁。 thread2执行其任务并发出条件变量信号以唤醒第一个线程 - > 上下文切换回thread1 - > thread1重新获取锁定。

我的假设是否正确?

我的代码如下:

#include <sys/types.h>
#include <wait.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>
#include <dirent.h>
#include <ctype.h>
#include<signal.h>
#include <stdio.h>
#include <stdint.h>
#include <time.h>
#include <pthread.h>


int var = 0;

int setToZero = 1;

int count = 5000;

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

pthread_cond_t isZero = PTHREAD_COND_INITIALIZER;

pthread_cond_t isOne = PTHREAD_COND_INITIALIZER;


struct timespec firstStart; 

unsigned long long timespecDiff(struct timespec *timeA_p, struct timespec *timeB_p)
{
  return ((timeA_p->tv_sec * 1000000000) + timeA_p->tv_nsec) - 
           ((timeB_p->tv_sec * 1000000000) + timeB_p->tv_nsec);
}

void* thread1(void* param)
{ 

  int rc;
  struct timespec previousStart;
  struct timespec start; //start timestamp
  struct timespec stop; //stop timestamp
  unsigned long long result;
  int idx = 0;
  int measurements[count];
   clock_gettime(CLOCK_MONOTONIC, &stop);

   result = timespecDiff(&stop,&firstStart);

   printf("first context-switch time:%llu\n", result);

  clock_gettime(CLOCK_MONOTONIC, &previousStart);

  while(count > 0){

  //acquire lock
  rc = pthread_mutex_lock(&mutex);

  clock_gettime(CLOCK_MONOTONIC,&start);

  while(setToZero){
    pthread_cond_wait(&isOne,&mutex); // use condition variables so the threads don't busy wait inside local cache
  }

  clock_gettime(CLOCK_MONOTONIC,&stop);


   var = 0;

   count--;

   setToZero = 1;

   //printf("in thread1\n");

   pthread_cond_signal(&isZero);
    //end of critical section
   rc = pthread_mutex_unlock(&mutex); //release lock

    result = timespecDiff(&stop,&start);

    measurements[idx] = result;

    idx++;
 }

 result = 0;

 int i = 0;
while(i < idx)
 {
   result += measurements[i++];
 }

 result = result /(2*idx);

 printf("thread1 result: %llu\n",result);
}


void* thread2(void* param)
{
  int rc;
  struct timespec previousStart;
  struct timespec start; //start timestamp
  struct timespec stop; //stop timestamp
  unsigned long long result;
  int idx = 0;
  int measurements[count];

  while(count > 0){

  //acquire lock
  rc = pthread_mutex_lock(&mutex);

  clock_gettime(CLOCK_MONOTONIC,&start);

  while(!setToZero){
    pthread_cond_wait(&isZero,&mutex);
  }

  clock_gettime(CLOCK_MONOTONIC,&stop);

   var = 1;

   count--;

   setToZero = 0;

   //printf("in thread2\n");

   pthread_cond_signal(&isOne);
    //end of critical section
   rc = pthread_mutex_unlock(&mutex); //release lock

   result = timespecDiff(&stop,&start);

   measurements[idx] = result;

   idx++;
  }

 result = 0;

 int i = 0;
while(i < idx)
 {
   result += measurements[i++];
 }

 result = result /(2*idx);

 printf("thread2 result: %llu\n",result);
}

int main(){
  pthread_t threads[2];

  pthread_attr_t attr;

  pthread_attr_init(&attr);

  clock_gettime(CLOCK_MONOTONIC,&firstStart);

  pthread_create(&threads[0],&attr,thread1,NULL);

  pthread_create(&threads[1],&attr,thread2,NULL);

  printf("waiting...\n");

  pthread_join(threads[0],NULL);

  pthread_join(threads[1],NULL);

  pthread_cond_destroy(&isOne);

  pthread_cond_destroy(&isZero);

}

我得到以下时间:

first context-switch time:144240
thread1 result: 3660
thread2 result: 3770

1 个答案:

答案 0 :(得分:0)

你说:

  

我假设pthread_cond_wait(&amp; cond,&amp; mutex)等待时间大约等于2 x线程上下文切换时间。

这不是一个有效的假设。释放互斥锁后,会通知内核,然后内核必须唤醒其他线程。它可能不会选择立即执行此操作,例如,如果有其他线程等待运行。互斥 - 顾名思义 - 保证什么时候发生。它不能保证什么时候会。

您不能期望从流程中可靠地测量上下文切换,当然也不会使用Posix API,因为没有人会承诺这样做。

  • 在Linux上,您可以使用/proc/[pid]/status 计算进程或线程的上下文切换。

  • 在Windows上,此信息可从Performance Monitor API获得。

这些中的任何一个是否会让你朝着你的目标前进,我不知道。我怀疑你真正想知道的是多线程系统对性能有多大影响,但这需要你测量整个应用程序的性能。