Blame IbaTools/usemem/usemem.c

Packit 857059
/* BEGIN_ICS_COPYRIGHT5 ****************************************
Packit 857059
Packit 857059
Copyright (c) 2015, Intel Corporation
Packit 857059
Packit 857059
Redistribution and use in source and binary forms, with or without
Packit 857059
modification, are permitted provided that the following conditions are met:
Packit 857059
Packit 857059
    * Redistributions of source code must retain the above copyright notice,
Packit 857059
      this list of conditions and the following disclaimer.
Packit 857059
    * Redistributions in binary form must reproduce the above copyright
Packit 857059
      notice, this list of conditions and the following disclaimer in the
Packit 857059
     documentation and/or other materials provided with the distribution.
Packit 857059
    * Neither the name of Intel Corporation nor the names of its contributors
Packit 857059
      may be used to endorse or promote products derived from this software
Packit 857059
      without specific prior written permission.
Packit 857059
Packit 857059
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit 857059
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 857059
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Packit 857059
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
Packit 857059
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 857059
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Packit 857059
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Packit 857059
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Packit 857059
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 857059
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 857059
Packit 857059
 * ** END_ICS_COPYRIGHT5   ****************************************/
Packit 857059
/* [ICS VERSION STRING: unknown] */
Packit 857059
#include <stdio.h>
Packit 857059
#include <stdlib.h>
Packit 857059
#include <stdint.h>
Packit 857059
#include <sys/types.h>
Packit 857059
#include <unistd.h>
Packit 857059
#include <malloc.h>
Packit 857059
#include <string.h>
Packit 857059
#include <sched.h>
Packit 857059
#include <errno.h>
Packit 857059
#include <sys/mman.h>
Packit 857059
Packit 857059
void a(int *f)
Packit 857059
{
Packit 857059
#ifdef USE__STACK
Packit 857059
	int z[16384];
Packit 857059
Packit 857059
	f[0] = 1;
Packit 857059
	f[16383] = 2;
Packit 857059
#else
Packit 857059
	int *z = sbrk(16384);
Packit 857059
	if(z == (int *)-1) {
Packit 857059
		perror("couldn't allocate more memory while recursing");
Packit 857059
		exit(1);
Packit 857059
	}
Packit 857059
Packit 857059
#endif
Packit 857059
Packit 857059
	printf("a(%p)\n", f);
Packit 857059
	sched_yield();
Packit 857059
	a(z);
Packit 857059
}
Packit 857059
Packit 857059
Packit 857059
int
Packit 857059
main(int cnt, char **args)
Packit 857059
{
Packit 857059
	unsigned long mem, i, pages, pgsz, passes;
Packit 857059
	uint64_t tot, totmem=0x1000000000; // 64GB
Packit 857059
	// volatile int val;
Packit 857059
	volatile char *p;
Packit 857059
	int zfirst[16384];
Packit 857059
	int verbose=0;
Packit 857059
Packit 857059
	if(cnt < 2 || (mem = strtoul(args[1], NULL, 0)) <= 0) {
Packit 857059
		printf("Usage: %s mem_incr [totmem]\n", args[0]);
Packit 857059
		return 1;
Packit 857059
	}
Packit 857059
	if(cnt == 3 && (totmem = strtoul(args[2], NULL, 0)) <= 0) {
Packit 857059
		printf("Usage: %s mem_incr [totmem]\n", args[0]);
Packit 857059
		return 1;
Packit 857059
	}
Packit 857059
Packit 857059
	if (mem > totmem) {
Packit 857059
		printf("mem_incr is larger than totmem.\n");
Packit 857059
		return 1;
Packit 857059
	} else if (totmem > 0x100000000000) { // 16 terabytes...
Packit 857059
		printf("totmem is too large.\n");
Packit 857059
		return 1;
Packit 857059
	}
Packit 857059
		
Packit 857059
	if(0 && mlockall(MCL_CURRENT|MCL_FUTURE))
Packit 857059
		perror("mlockall failed");
Packit 857059
Packit 857059
	pgsz = getpagesize();
Packit 857059
Packit 857059
	for(tot=passes=0; tot
Packit 857059
		if(!(p = calloc(mem, 1))) {
Packit 857059
			fprintf(stderr,
Packit 857059
				"couldn't calloc %lu, pass %lu, %lu allocated: %s\n",
Packit 857059
				mem, passes, tot, strerror(errno));
Packit 857059
				break;
Packit 857059
		}
Packit 857059
		else if(verbose)
Packit 857059
			printf("calloc'ed (%lx) bytes at %p, tot %lu\n", mem, p, tot);
Packit 857059
Packit 857059
		// calloc isn't good enough, since with mmap'ed based calloc,
Packit 857059
		// it "knows" the pages are zero-filled, so you have to actuall
Packit 857059
		// touch each page
Packit 857059
		pages = (mem+pgsz-1)/pgsz;
Packit 857059
		if((pgsz-1)&(ptrdiff_t)p)
Packit 857059
			pages++; // not likely
Packit 857059
		if((pgsz-1)&(ptrdiff_t)p)
Packit 857059
			pages--; // wasn't page aligned, so one less page
Packit 857059
		for(i=0; i
Packit 857059
			p[i*pgsz] = 1;
Packit 857059
			memset((void *)&p[i*pgsz], 0xa5, pgsz); // completely write the page
Packit 857059
			sched_yield(); // try to randomize somewhat by letting other stuff run
Packit 857059
		}
Packit 857059
	}
Packit 857059
	printf("alloc'ed %lu (%lx) bytes total\n", tot, tot);
Packit 857059
Packit 857059
	//printf("Pausing\n"); pause();
Packit 857059
Packit 857059
	return 0; // OLSON, stack is separate
Packit 857059
	printf("recursing to grow stack 16K at a time\n");
Packit 857059
	a(zfirst);
Packit 857059
Packit 857059
	return 0;
Packit 857059
}
Packit 857059
Packit 857059