stress/stress.c

126 lines
2.3 KiB
C
Raw Normal View History

2019-02-11 13:06:53 +01:00
#include "stress.h"
void usage(void)
{
printf("Usage: stress [-t n] [-m n]\n");
printf("\t-t n\tRun n processes parallel\n");
printf("\t-m n\tTest n MiB of Memory\n");
printf("\n");
printf("\t\tCan be combined\n");
exit(0);
}
void testMemory(unsigned long long size)
{
char *mem=malloc(size*sizeof(char));
unsigned long long i;
unsigned char run=0;
while(1==1)
{
for(i=0; i<size; i++)
{
mem[i]=(mem[i]+i+run)%256;
}
run++;
}
}
void testCPU(void)
{
long double f=0;
long double res=0;
while(1==1)
{
for(f=0; f<LDBL_MAX; f+=LDBL_EPSILON)
{
res=sqrtl(f);
}
}
}
int main(int argc, char **argv)
{
const char *optargs = "t:m:h";
char optc;
unsigned long long convtemp;
unsigned char errflags=0;
unsigned char optflags=0;
unsigned char threads = 1;
unsigned long long memory = 1048576LL;
int i, pid, wstatus;
while((optc=getopt(argc,argv,optargs))!=-1)
{
switch(optc)
{
case 't':
if((convtemp = strtoll(optarg, NULL, 10))==0)
{
errflags|=THREADS;
perror("Error with -t: ");
}
else
{
optflags|=THREADS;
threads=convtemp;
}
break;
case 'm':
if((convtemp = strtoll(optarg, NULL, 10))==0)
{
errflags|=MEMSIZE;
perror("Error with -m: ");
}
else
{
optflags|=MEMSIZE;
memory=convtemp*1024*1024;
}
break;
case 'h':
case '?':
default:
usage();
}
}
if(errflags>0)
{
usage();
}
if(optflags & THREADS)
{
printf("Threads: %d\n", threads);
}
if(optflags & MEMSIZE)
{
printf("Memsize: %llu\n", memory);
}
for(i=0; i<threads; i++)
{
switch(pid=fork())
{
case -1:
perror("fork()");
break;
case 0:
if(optflags&MEMSIZE)
testMemory(memory/threads);
else
testCPU();
break;
}
}
for(i=0; i<threads; i++)
{
wait(&wstatus);
}
return 0;
}