From 0ac85a2895f7112b67663020a6bc188637218f95 Mon Sep 17 00:00:00 2001 From: Konrad Rzeszutek Wilk Date: Wed, 12 Jan 2011 15:37:51 -0500 Subject: [PATCH] eatmem.c --- root_image/tools/eatmem/Makefile | 25 ++++++ root_image/tools/eatmem/eatmem.c | 133 +++++++++++++++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 root_image/tools/eatmem/Makefile create mode 100644 root_image/tools/eatmem/eatmem.c diff --git a/root_image/tools/eatmem/Makefile b/root_image/tools/eatmem/Makefile new file mode 100644 index 0000000..3897c70 --- /dev/null +++ b/root_image/tools/eatmem/Makefile @@ -0,0 +1,25 @@ +TARGETS-y := eatmem +TARGETS := $(TARGETS-y) + +INSTALL_BIN-y := eatmem +INSTALL_BIN := $(INSTALL_BIN-y) + + +.PHONY: all +all: build + +.PHONY: build +build: $(TARGETS) + +.PHONY: install +install: build + $(INSTALL_DIR) $(DESTDIR)$(BINDIR) + $(INSTALL_DIR) $(DESTDIR)$(SBINDIR) + +.PHONY: clean +clean: + $(RM) *.o $(TARGETS) *~ $(DEPS) + +%.o: %.c $(HDRS) Makefile + $(CC) -c $(CFLAGS) -o $@ $< + diff --git a/root_image/tools/eatmem/eatmem.c b/root_image/tools/eatmem/eatmem.c new file mode 100644 index 0000000..d7087fc --- /dev/null +++ b/root_image/tools/eatmem/eatmem.c @@ -0,0 +1,133 @@ +/* +* eatmem.c -- allocate a junk of memory and make it dirty +* Copyright Harald Koenig 1994 +* +* Permission is hereby granted to copy, modify and redistribute this code +* in terms of the GNU Library General Public License, Version 2 or later, +* at your option. +* +* to compile use +* cc -O2 -Wall -fomit-frame-pointer -s -o eatmem eatmem.c +*/ + +/* + Modified by Frank Sorenson 2005 +*/ + +#include +#include +#include +#include +#include +#include + +int signal_caught = 0; + +void signal_handler(int sig) +{ + pid_t pgrp; + + if (signal_caught > 0) return; + signal_caught ++; + pgrp = getpgrp(); + killpg(pgrp, sig); +} + +long int get_mem_size(char *size) +{ + long int mem_size = (-1); + char *ptr; + + mem_size = strtoul(size, &ptr, 10); + switch (ptr[0]) { + case 'M' : + case 'm' : + mem_size *= 1024; + break; + case 'G' : + case 'g' : + mem_size *= 1024*1024; + break; + case 'K' : + case 'k' : + default : + break; + } + return mem_size; +} + +void setup_handlers() +{ + /* setup the signal handler */ + signal(SIGINT, signal_handler); + signal(SIGTERM, signal_handler); + signal(SIGCHLD, signal_handler); +} + +void dirty_mem(char *mem, long int mem_size) +{ + int i; + char *pmem = mem; + + for (i = 0; i < mem_size; i++) { + (*pmem)++; + pmem += 1024; + } +} + +int main(int argc, char *argv[]) +{ + char *mem=NULL,*pb; + long int mem_size = (-1); + int err = 0; + int num_threads = 1; + pid_t pid; + pid_t ppid; + int thread_id; + + if (argc >= 2) { + mem_size = get_mem_size(argv[1]); + if (argc == 3) + num_threads = strtoul(argv[2], &pb, 10); + else if (argc > 3) + err=1; + } + + if (mem_size <= 0 || err) { + fprintf(stderr,"usage: %s size[K|M|G] [num_of_threads]\n", argv[0]); + exit(1); + } + + for (thread_id = 0; thread_id < (num_threads - 1) ; thread_id ++) { + pid = fork(); + if (pid == 0) break; + } + ppid = getppid(); + + mem = malloc(mem_size*1024); + if (mem == NULL) { + fprintf(stderr,"malloc failed.\n"); + exit(1); + } + + if (num_threads == 1) + signal_caught ++; + else + setup_handlers(); + + do { + dirty_mem(mem, mem_size); + + /* if parent has died... */ + /* there's got to be a better way, though */ + if (thread_id != 0 && getppid() != ppid) + killpg(getpgrp(), SIGTERM); + } while (signal_caught == 0); + + if (num_threads == 1) + kill(0, SIGSTOP); + + free(mem); + exit(0); +} + -- 2.39.5