Skip to content

Instantly share code, notes, and snippets.

@spraints
Last active October 21, 2022 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spraints/0e8247ca27a84fa5db784c4e580675b1 to your computer and use it in GitHub Desktop.
Save spraints/0e8247ca27a84fa5db784c4e580675b1 to your computer and use it in GitHub Desktop.
getrusage quirks (on Linux, ru_maxrss is the max for the process, regardless of the exec after a fork)
*.o
show-maxrss
bloat-fe
#include <stdlib.h>
#include <sys/resource.h>
#include "runmaxrss.h"
#include "show-maxrss-lib.h"
#define CHONK (1024*1024*1024) // 1 GB
int main() {
showmaxrss();
show(RUSAGE_SELF, "bloat-fe after malloc");
char *m = (char*)malloc(CHONK);
for (int i = 0; i < CHONK; i += 1024*1024) {
m[i] = 0;
}
show(RUSAGE_SELF, "bloat-fe after malloc");
showmaxrss();
}
FROM debian:buster-slim
RUN apt-get update && apt-get install -y build-essential
COPY . /app
WORKDIR /app
RUN make clean
.PHONY: all
all: show-maxrss bloat-fe
clean:
rm -f show-maxrss bloat-fe *.o
HS = show-maxrss-lib.h runmaxrss.h
show-maxrss: show-maxrss.c $(HS) show-maxrss-lib.o
$(CC) -o show-maxrss show-maxrss.c show-maxrss-lib.o
bloat-fe: bloat-fe.c $(HS) runmaxrss.o show-maxrss-lib.o
$(CC) -o bloat-fe bloat-fe.c runmaxrss.o show-maxrss-lib.o
.c.o:
$(CC) -c $<
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <sys/resource.h>
void showmaxrss() {
pid_t pid = fork();
printf("f=>%d\n", pid); // For some reason, this makes execl succeed. ??
if (pid < 0) {
printf("error running fork: errno=%d\n", errno);
exit(1);
}
if (pid == 0) {
int res = execl("./show-maxrss", "show-maxrss");
printf("error running exec: errno=%d\n", errno);
exit(1);
} else {
int stat_loc;
struct rusage usage;
wait4(pid, &stat_loc, 0, &usage);
printf("child ru_maxrss: %ld\n", usage.ru_maxrss);
}
}
void showmaxrss();
#!/bin/bash
set -ex
docker build --rm --force-rm -t ru-maxrss .
docker run -it --rm ru-maxrss
#include <errno.h>
#include <sys/resource.h>
#include <stdio.h>
void show(int who, const char *whos) {
int res;
struct rusage usage;
res = getrusage(who, &usage);
if (res != 0) {
printf("%s: error: errno=%d\n", whos, errno);
} else {
printf("%s: ru_maxrss = %ld\n", whos, usage.ru_maxrss);
}
}
void show(int who, const char *whos);
#include <sys/resource.h>
#include "show-maxrss-lib.h"
#define _show(who) show(who, #who)
int main() {
_show(RUSAGE_SELF);
_show(RUSAGE_CHILDREN);
#ifdef RUSAGE_THREAD
_show(RUSAGE_THREAD);
#endif
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment