当前位置: 技术问答>linux和unix
请问linux中文件读写放在进程中和线程中性能为什么差别大。修改线程优先级也不行.
来源: 互联网 发布时间:2015-09-07
本文导语: 同样的程序,如果在main中直接调用,可以达到50M(和hdparm测试的结果基本一样)。如果在线程中,只有50M/5。 还有有什么工具可以很好的测试linux下硬盘I/O的性能。 谢谢。 线程设置: pthread_t pid; pthread_attr_t* attr...
同样的程序,如果在main中直接调用,可以达到50M(和hdparm测试的结果基本一样)。如果在线程中,只有50M/5。
还有有什么工具可以很好的测试linux下硬盘I/O的性能。
谢谢。
线程设置:
pthread_t pid;
pthread_attr_t* attr;
struct sched_param* param;
param=(struct sched_param*)malloc(sizeof(struct sched_param));
attr=(pthread_attr_t*)malloc(sizeof(pthread_attr_t));
param->sched_priority=99;
pthread_attr_init(attr);
pthread_attr_setscope(attr,PTHREAD_SCOPE_SYSTEM);
pthread_attr_setdetachstate(attr,PTHREAD_CREATE_DETACHED);
pthread_attr_setschedpolicy(attr,SCHED_RR);
pthread_attr_setschedparam(attr,param);
pthread_create(&pid,attr,myProcess,NULL );
//setpriority(PRIO_PROCESS,pid,-20);
//*myProcess(NULL);
全部代码:
// burst.cpp : Defines the entry point for the console application.
//
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
#define BUFFERSIZE 1024*1024*10
#define WIBLOCK 30
long startSeconds;
long startMilli;
long finishSeconds;
long finishMilli;
long rate=0;
struct timeb* timeBuffer;
char FName[51][8]={"1.rar","2.rar","3.rar",
"4.rar","5.rar","6.rar","7.rar",
"8.rar","9.rar","10.rar","11.rar",
"12.rar","13.rar","14.rar",
"15.rar","16.rar","17.rar",
"18.rar","19.rar","20.rar",
"21.rar","22.rar","23.rar",
"24.rar","25.rar","26.rar",
"27.rar","28.rar","29.rar",
"30.rar","31.rar","32.rar",
"33.rar","34.rar","35.rar",
"36.rar","37.rar","38.rar",
"39.rar","40.rar","41.rar",
"42.rar","43.rar","44.rar",
"45.rar","46.rar","47.rar",
"48.rar","49.rar","50.rar",
"51.rar"};
char fileInBuffer[1024*500];
int randSelect[1024*1024];
long randNum=0;
static void *myProcess(void *arg);
int consol();
int threadNum;
int totalTimes=0;
int endFlag;
int g_iThreadNum;
int g_iFileNum;
int g_iBlockSize;
int g_iTotalBlocks;
int g_iThreadReadTimes;
int main()
{
long buffer[32767];
int times;
long k;
long f;
char fBuffer[200];
while(!consol()){};
endFlag=g_iThreadNum*g_iThreadReadTimes;
threadNum=g_iThreadNum;
sprintf(fBuffer,"***********************************************n Read test in linux! n %dM/PER FILE n***********************************************n",g_iTotalBlocks*g_iBlockSize);
strcat(fileInBuffer,fBuffer);
srand(time((time_t*)NULL));
for(f=0;fmillitm;
for(times=0;timessched_priority=99;
pthread_attr_init(attr);
pthread_attr_setscope(attr,PTHREAD_SCOPE_SYSTEM);
pthread_attr_setdetachstate(attr,PTHREAD_CREATE_DETACHED);
pthread_attr_setschedpolicy(attr,SCHED_RR);
pthread_attr_setschedparam(attr,param);
pthread_create(&pid,attr,myProcess,NULL );
//setpriority(PRIO_PROCESS,pid,-20);
//*myProcess(NULL);
}
while(1){};
}
static void *myProcess(void *arg){
int l_iFileSize;
int handle;
int readTimes;
int currentID;
int currentTimes;
int localEndFlag;
FILE *hfile;
char *Bp;
const char* p;
char fBuffer[WIBLOCK];
//long start, finish;
long ownTimeStartSeconds,ownTimeFinishSeconds,ownTimeStartMilli,ownTimeFinishMilli;
long perFileCost=0;
threadNum--;
currentID=threadNum;
for(readTimes=0;readTimestime;
ownTimeStartMilli=timeBuffer->millitm;
for(l_iFileSize=0;l_iFileSizetime;
ownTimeFinishMilli=timeBuffer->millitm;
ownTimeFinishMilli+=(ownTimeFinishSeconds-ownTimeStartSeconds)*1000;
perFileCost=ownTimeFinishMilli-ownTimeStartMilli;
endFlag--;
localEndFlag=endFlag;
//sprintf(fBuffer,"n Thread %d Filename: %s Readtimes: %d Cost: %ld",currentID,p,currentTimes,perFileCost);
sprintf(fBuffer,"n Thread %d ",currentID);
//myStrCat(fileInBuffer,fBuffer,sizeof(fileInBuffer),sizeof(fBuffer));
strcat(fileInBuffer,fBuffer);
sprintf(fBuffer,"Filename: %s ",p);
strcat(fileInBuffer,fBuffer);
sprintf(fBuffer,"Readtimes: %d ",currentTimes);
strcat(fileInBuffer,fBuffer);
sprintf(fBuffer,"Cost: %ldms ",perFileCost);
strcat(fileInBuffer,fBuffer);
rate+=(g_iTotalBlocks*g_iBlockSize*1024*1000)/(perFileCost);
printf("nThread %d Filename: %s Readtimes: %d Cost: %ld,Average Speed is %ld K/S",currentID,p,currentTimes,perFileCost,((g_iTotalBlocks*g_iBlockSize*1000*1024)/(perFileCost)));
sprintf(fBuffer,"Rate: %ldK/Sn",((g_iTotalBlocks*g_iBlockSize*1000*1024)/(perFileCost)));
strcat(fileInBuffer,fBuffer);
free(Bp);
fclose(hfile);
}
if(!localEndFlag) {
ftime(timeBuffer);
finishSeconds = timeBuffer->time;
finishMilli=timeBuffer->millitm;
finishMilli+=(finishSeconds-startSeconds)*1000;
randNum+=finishMilli-startMilli;
printf("nThread number: %d File number: %d Block size: %dM Total times:%d Cost:%8ld n",g_iThreadNum,g_iFileNum,g_iBlockSize,g_iThreadNum*g_iThreadReadTimes,randNum);
sprintf(fBuffer,"nThread number: %d File number: %d ",g_iThreadNum,g_iFileNum);
strcat(fileInBuffer,fBuffer);
sprintf(fBuffer,"Block size: %dM Total times: %d Cost: %ldms ",g_iBlockSize,g_iThreadNum*g_iThreadReadTimes,randNum);
strcat(fileInBuffer,fBuffer);
sprintf(fBuffer,"Avrage Rate: %ldK/S n",(rate/((g_iThreadNum)*g_iThreadReadTimes)));
strcat(fileInBuffer,fBuffer);
handle = creat("history.txt", S_IREAD | S_IWRITE);
write(handle, fileInBuffer, strlen(fileInBuffer));
close(handle);
}
return NULL;
}
int consol()
{
printf("nPlease input Thread number(integer): ");
scanf("%d",&g_iThreadNum);
printf("nPlease input fileNum(integer): ");
scanf("%d",&g_iFileNum);
if ((g_iFileNum)>50){ printf("n File number must
还有有什么工具可以很好的测试linux下硬盘I/O的性能。
谢谢。
线程设置:
pthread_t pid;
pthread_attr_t* attr;
struct sched_param* param;
param=(struct sched_param*)malloc(sizeof(struct sched_param));
attr=(pthread_attr_t*)malloc(sizeof(pthread_attr_t));
param->sched_priority=99;
pthread_attr_init(attr);
pthread_attr_setscope(attr,PTHREAD_SCOPE_SYSTEM);
pthread_attr_setdetachstate(attr,PTHREAD_CREATE_DETACHED);
pthread_attr_setschedpolicy(attr,SCHED_RR);
pthread_attr_setschedparam(attr,param);
pthread_create(&pid,attr,myProcess,NULL );
//setpriority(PRIO_PROCESS,pid,-20);
//*myProcess(NULL);
全部代码:
// burst.cpp : Defines the entry point for the console application.
//
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
#define BUFFERSIZE 1024*1024*10
#define WIBLOCK 30
long startSeconds;
long startMilli;
long finishSeconds;
long finishMilli;
long rate=0;
struct timeb* timeBuffer;
char FName[51][8]={"1.rar","2.rar","3.rar",
"4.rar","5.rar","6.rar","7.rar",
"8.rar","9.rar","10.rar","11.rar",
"12.rar","13.rar","14.rar",
"15.rar","16.rar","17.rar",
"18.rar","19.rar","20.rar",
"21.rar","22.rar","23.rar",
"24.rar","25.rar","26.rar",
"27.rar","28.rar","29.rar",
"30.rar","31.rar","32.rar",
"33.rar","34.rar","35.rar",
"36.rar","37.rar","38.rar",
"39.rar","40.rar","41.rar",
"42.rar","43.rar","44.rar",
"45.rar","46.rar","47.rar",
"48.rar","49.rar","50.rar",
"51.rar"};
char fileInBuffer[1024*500];
int randSelect[1024*1024];
long randNum=0;
static void *myProcess(void *arg);
int consol();
int threadNum;
int totalTimes=0;
int endFlag;
int g_iThreadNum;
int g_iFileNum;
int g_iBlockSize;
int g_iTotalBlocks;
int g_iThreadReadTimes;
int main()
{
long buffer[32767];
int times;
long k;
long f;
char fBuffer[200];
while(!consol()){};
endFlag=g_iThreadNum*g_iThreadReadTimes;
threadNum=g_iThreadNum;
sprintf(fBuffer,"***********************************************n Read test in linux! n %dM/PER FILE n***********************************************n",g_iTotalBlocks*g_iBlockSize);
strcat(fileInBuffer,fBuffer);
srand(time((time_t*)NULL));
for(f=0;fmillitm;
for(times=0;timessched_priority=99;
pthread_attr_init(attr);
pthread_attr_setscope(attr,PTHREAD_SCOPE_SYSTEM);
pthread_attr_setdetachstate(attr,PTHREAD_CREATE_DETACHED);
pthread_attr_setschedpolicy(attr,SCHED_RR);
pthread_attr_setschedparam(attr,param);
pthread_create(&pid,attr,myProcess,NULL );
//setpriority(PRIO_PROCESS,pid,-20);
//*myProcess(NULL);
}
while(1){};
}
static void *myProcess(void *arg){
int l_iFileSize;
int handle;
int readTimes;
int currentID;
int currentTimes;
int localEndFlag;
FILE *hfile;
char *Bp;
const char* p;
char fBuffer[WIBLOCK];
//long start, finish;
long ownTimeStartSeconds,ownTimeFinishSeconds,ownTimeStartMilli,ownTimeFinishMilli;
long perFileCost=0;
threadNum--;
currentID=threadNum;
for(readTimes=0;readTimestime;
ownTimeStartMilli=timeBuffer->millitm;
for(l_iFileSize=0;l_iFileSizetime;
ownTimeFinishMilli=timeBuffer->millitm;
ownTimeFinishMilli+=(ownTimeFinishSeconds-ownTimeStartSeconds)*1000;
perFileCost=ownTimeFinishMilli-ownTimeStartMilli;
endFlag--;
localEndFlag=endFlag;
//sprintf(fBuffer,"n Thread %d Filename: %s Readtimes: %d Cost: %ld",currentID,p,currentTimes,perFileCost);
sprintf(fBuffer,"n Thread %d ",currentID);
//myStrCat(fileInBuffer,fBuffer,sizeof(fileInBuffer),sizeof(fBuffer));
strcat(fileInBuffer,fBuffer);
sprintf(fBuffer,"Filename: %s ",p);
strcat(fileInBuffer,fBuffer);
sprintf(fBuffer,"Readtimes: %d ",currentTimes);
strcat(fileInBuffer,fBuffer);
sprintf(fBuffer,"Cost: %ldms ",perFileCost);
strcat(fileInBuffer,fBuffer);
rate+=(g_iTotalBlocks*g_iBlockSize*1024*1000)/(perFileCost);
printf("nThread %d Filename: %s Readtimes: %d Cost: %ld,Average Speed is %ld K/S",currentID,p,currentTimes,perFileCost,((g_iTotalBlocks*g_iBlockSize*1000*1024)/(perFileCost)));
sprintf(fBuffer,"Rate: %ldK/Sn",((g_iTotalBlocks*g_iBlockSize*1000*1024)/(perFileCost)));
strcat(fileInBuffer,fBuffer);
free(Bp);
fclose(hfile);
}
if(!localEndFlag) {
ftime(timeBuffer);
finishSeconds = timeBuffer->time;
finishMilli=timeBuffer->millitm;
finishMilli+=(finishSeconds-startSeconds)*1000;
randNum+=finishMilli-startMilli;
printf("nThread number: %d File number: %d Block size: %dM Total times:%d Cost:%8ld n",g_iThreadNum,g_iFileNum,g_iBlockSize,g_iThreadNum*g_iThreadReadTimes,randNum);
sprintf(fBuffer,"nThread number: %d File number: %d ",g_iThreadNum,g_iFileNum);
strcat(fileInBuffer,fBuffer);
sprintf(fBuffer,"Block size: %dM Total times: %d Cost: %ldms ",g_iBlockSize,g_iThreadNum*g_iThreadReadTimes,randNum);
strcat(fileInBuffer,fBuffer);
sprintf(fBuffer,"Avrage Rate: %ldK/S n",(rate/((g_iThreadNum)*g_iThreadReadTimes)));
strcat(fileInBuffer,fBuffer);
handle = creat("history.txt", S_IREAD | S_IWRITE);
write(handle, fileInBuffer, strlen(fileInBuffer));
close(handle);
}
return NULL;
}
int consol()
{
printf("nPlease input Thread number(integer): ");
scanf("%d",&g_iThreadNum);
printf("nPlease input fileNum(integer): ");
scanf("%d",&g_iFileNum);
if ((g_iFileNum)>50){ printf("n File number must