Skip to content
Snippets Groups Projects
Commit b9cd13fe authored by Florian Fischer's avatar Florian Fischer
Browse files

implement alternative way to get memusage using wait4

run now forks the benchmark command instead of simply calling exec.
Ressource usage of the benchmark is retrieved with a call to wait4.
But the reported value ru_maxrss differs from values in /proc/<pid>/status.
parent 78167c5d
No related branches found
No related tags found
No related merge requests found
...@@ -15,7 +15,7 @@ MEMSIZE=$(shell echo $(MEMSIZE_KB)"* 1024" | bc) ...@@ -15,7 +15,7 @@ MEMSIZE=$(shell echo $(MEMSIZE_KB)"* 1024" | bc)
.PHONY: all clean .PHONY: all clean
all: $(OBJDIR)/print_status_on_exit.so $(OBJDIR)/allocators/bumpptr_alloc.so $(OBJDIR)/exec all: $(OBJDIR)/print_status_on_exit.so $(OBJDIR)/allocators/bumpptr_alloc.so $(OBJDIR)/run
$(OBJDIR)/allocators/bumpptr_alloc.so: bumpptr_alloc.c Makefile | $(OBJDIR)/allocators $(OBJDIR)/allocators/bumpptr_alloc.so: bumpptr_alloc.c Makefile | $(OBJDIR)/allocators
@echo "Compiling $@..."; @echo "Compiling $@...";
...@@ -26,7 +26,7 @@ $(OBJDIR)/print_status_on_exit.so: print_status_on_exit.c Makefile | $(OBJDIR) ...@@ -26,7 +26,7 @@ $(OBJDIR)/print_status_on_exit.so: print_status_on_exit.c Makefile | $(OBJDIR)
@echo "Compiling $@..."; @echo "Compiling $@...";
$(CC) $(LDFLAGS) -shared $(CFLAGS) -o $@ $< $(CC) $(LDFLAGS) -shared $(CFLAGS) -o $@ $<
$(OBJDIR)/exec: exec.c Makefile | $(OBJDIR) $(OBJDIR)/run: run.c Makefile | $(OBJDIR)
@echo "Compiling $@..."; @echo "Compiling $@...";
$(CC) $(LDFLAGS) $(CFLAGS) -o $@ $< $(CC) $(LDFLAGS) $(CFLAGS) -o $@ $<
......
...@@ -2,35 +2,84 @@ ...@@ -2,35 +2,84 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <unistd.h> #include <unistd.h>
static int child(int argc, char* argv[])
{
char **newargv = malloc(sizeof(char*) * argc);
if (newargv == NULL)
{
perror("malloc");
return 1;
}
for (int i = 0; i < argc; i++)
{
newargv[i] = argv[i];
}
newargv[argc] = NULL;
execvp(newargv[0], newargv);
perror("execvp");
return 1;
}
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
if (argc < 2) { if (argc < 2)
{
printf("Usage: %s [-p LD_PRELOAD] [-l LD_LIBRARY_PATH] <cmd> [cmd args]\n", argv[0]); printf("Usage: %s [-p LD_PRELOAD] [-l LD_LIBRARY_PATH] <cmd> [cmd args]\n", argv[0]);
printf("\tset LD_PRELOAD to ld_preload and call execvp <cmd> [cmd args]\n"); printf("\tset LD_PRELOAD to ld_preload and call execvp <cmd> [cmd args]\n");
return 1; return 1;
} }
int status, child_val;
struct rusage r_usage;
int i = 1; int i = 1;
for (; i < argc; i++) { for (; i < argc; i++)
{
// Overwrite LD_PRELOAD. // Overwrite LD_PRELOAD.
if (strncmp(argv[i], "-p", 2) == 0) { if (strncmp(argv[i], "-p", 2) == 0)
{
setenv("LD_PRELOAD", argv[i+1], 1); setenv("LD_PRELOAD", argv[i+1], 1);
i++; i++;
// Overwrite LD_LIBRARY_PATH. // Overwrite LD_LIBRARY_PATH.
} else if (strncmp(argv[i], "-l", 2) == 0) { } else if (strncmp(argv[i], "-l", 2) == 0)
{
setenv("LD_LIBRARY_PATH", argv[i+1], 1); setenv("LD_LIBRARY_PATH", argv[i+1], 1);
i++; i++;
} else { } else
{
break; break;
} }
} }
pid_t child_pid = fork();
// Run cmd.
execvp(argv[i], &argv[i]);
fprintf(stderr, "executing %s failed\n", argv[i]); if (child_pid == 0)
{
return child(argc - i, &argv[i]);
}
else if (child_pid > 0)
{
/* Collect child */
wait4(child_pid, &status, 0, &r_usage);
printf("child memory usage = %ld\n", r_usage.ru_maxrss);
return 1; /* Get child status value */
if (WIFEXITED(status))
{
child_val = WEXITSTATUS(status);
exit(child_val);
}
} else
{
perror("fork");
}
return 0;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment