Created
May 23, 2012 09:05
-
-
Save ytoshima/2774052 to your computer and use it in GitHub Desktop.
Simple stress program
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* A small cpu and memory stress program | |
* compile: CC cpumem.cpp -o cpumem -lpthread | |
* or: make LDFLAGS=-lpthread cpumem | |
*/ | |
#include <pthread.h> | |
#include <iostream> | |
#include <vector> | |
#include <sys/types.h> | |
#include <assert.h> | |
#include <unistd.h> | |
#include <stdlib.h> | |
#include <errno.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <sys/mman.h> | |
using namespace std; | |
#if !defined(ulong) | |
typedef unsigned long ulong; | |
#endif | |
vector<ulong>* SieveOfEratosthenes(int lim) | |
{ | |
assert(lim < 1073741824); | |
ulong* p; | |
vector<ulong>* vp = new vector<ulong>(); | |
p = new ulong[lim]; | |
p[0]=p[1]=0; | |
for (int i=2;i<lim;i++) p[i]=i; | |
for (int i=0;i<lim;i++) | |
if (p[i]) | |
for (int j=2;(j*i)<lim;j++) | |
p[j*i]=0; | |
for (int i=0;i<lim;i++) | |
if (p[i]) | |
vp->push_back(p[i]); | |
return vp; | |
} | |
long fibonacci(long n) | |
{ | |
if (n == 0) return 0; | |
if (n > 1) return fibonacci(n-1) + fibonacci(n-2); | |
else return 1; | |
} | |
static int ZERO = 0; | |
static int MONE = -1; | |
void* cpuFunc(void *vp) | |
{ | |
vector<ulong>* ans = SieveOfEratosthenes(10000000); | |
vector<ulong>::iterator p; | |
fibonacci(40); | |
return (void*)&ZERO; | |
} | |
void doCpuWork(int nthreads) | |
{ | |
void *rp; | |
pthread_t* tids = new pthread_t[nthreads]; | |
memset(tids, 0, sizeof(pthread_t)*nthreads); | |
for (int i = 0; i < nthreads; i++) { | |
if (pthread_create(&tids[i], NULL, cpuFunc, NULL)) { | |
cerr << "E: pthread_create " << strerror(errno) << endl; | |
} | |
} | |
for (int i = 0; i < nthreads; i++) { | |
if (tids[i]) { | |
if (pthread_join(tids[i], &rp)) { | |
cerr << "E: pthread_join " << strerror(errno) << endl; | |
} | |
} | |
} | |
} | |
void memOp1() | |
{ | |
const int npages = (16*1024*1024)/4096; | |
void *vp = mmap(NULL, npages*0x1000, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0); | |
if (vp == MAP_FAILED) { | |
cerr << "E: mmap " << strerror(errno) << endl; | |
} | |
int *ip = (int*)vp; | |
int nints = (npages*4096)/sizeof(int); | |
for (int i = 0; i < nints; i++) ip[i] = rand(); | |
for (int i = 0; i < nints; i++) { | |
int nv = rand(); | |
if (nv > ip[i]) ip[i] = nv; | |
} | |
int *fip = ip; | |
int *lip = ip + nints - 1; | |
for (; fip < lip; fip++, lip--) { | |
int x = *fip; | |
*fip = *lip; | |
*lip = x; | |
} | |
} | |
void* | |
memFunc(void* va) | |
{ | |
memOp1(); | |
return (void*)&ZERO; | |
} | |
void doMemWork(int nthreads) | |
{ | |
void *rp; | |
pthread_t* tids = new pthread_t[nthreads]; | |
memset(tids, 0, sizeof(pthread_t)*nthreads); | |
for (int i = 0; i < nthreads; i++) { | |
if (pthread_create(&tids[i], NULL, memFunc, NULL)) { | |
cerr << "E: pthread_create " << strerror(errno) << endl; | |
} | |
} | |
for (int i = 0; i < nthreads; i++) { | |
if (tids[i]) { | |
if (pthread_join(tids[i], &rp)) { | |
cerr << "E: pthread_join " << strerror(errno) << endl; | |
} | |
} | |
} | |
} | |
int cpuCount() | |
{ | |
return sysconf(_SC_NPROCESSORS_CONF); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
int nthreads = cpuCount(); | |
if (argc > 1) { | |
int tnt = atoi(argv[1]); | |
if (tnt > 0) { | |
nthreads = tnt; | |
cout << "nthreads was set to " << nthreads << endl; | |
} | |
} | |
srand(getpid()); | |
doCpuWork(nthreads); | |
doMemWork(nthreads); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment