首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >这是测量线程上下文切换开销的正确解决方案吗?

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

Stack Overflow用户
提问于 2017-02-15 06:59:36
回答 1查看 570关注 0票数 1

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

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

我的假设正确吗?

我的代码如下:

代码语言:javascript
复制
#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);

}

我得到的时间如下:

代码语言:javascript
复制
first context-switch time:144240
thread1 result: 3660
thread2 result: 3770
EN

回答 1

Stack Overflow用户

发布于 2017-04-07 15:39:08

你说:

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

这不是一个正确的假设。一旦互斥锁被释放,这就通知内核,内核必须唤醒另一个线程。它可能不会立即选择这样做,例如,如果有其他线程等待运行。正如它的名字所暗示的那样,这个互斥体可以保证什么时候会发生而不是。它不能保证他们什么时候会这样做。

您不能指望从进程中可靠地度量上下文切换,当然也不能使用Posix API,因为没有一个承诺会这样做。

  • 在Linux上,您可以使用/proc/[pid]/status计算进程或线程的上下文开关。
  • 在Windows上,此信息可从Performance获得。

我不知道这些是否能让你达到目标。我怀疑您真正想知道的是使用多线程系统对性能的影响有多大,但这需要您作为一个整体来度量应用程序的性能。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42242586

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档