Initial version

This commit is contained in:
Peter 2019-02-11 13:06:53 +01:00
commit 959a848097
Signed by: pludi
GPG Key ID: CFBA360E696EDC99
4 changed files with 165 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
stress
.*.swp

11
LICENSE Normal file
View File

@ -0,0 +1,11 @@
Copyright 2019 Peter Ludikovsky
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

125
stress.c Normal file
View File

@ -0,0 +1,125 @@
#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;
}

27
stress.h Normal file
View File

@ -0,0 +1,27 @@
#ifndef _STRESS_H
#define _STRESS_H
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <unistd.h>
#include <math.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
extern int errno;
#ifndef LDBL_MAX
#define LDBL_MAX __LDBL_MAX__
#endif
#ifndef LDBL_EPSILON
#define LDBL_EPSILON __LDBL_EPSILON__
#endif
#define THREADS 0x1
#define MEMSIZE 0x2
void testCPU(void);
void testMemory(unsigned long long);
void usage(void);
#endif