Last active
October 21, 2022 17:08
-
-
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)
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
*.o | |
show-maxrss | |
bloat-fe |
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
#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(); | |
} |
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
FROM debian:buster-slim | |
RUN apt-get update && apt-get install -y build-essential | |
COPY . /app | |
WORKDIR /app | |
RUN make clean |
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
.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 $< |
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
#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); | |
} | |
} |
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
void showmaxrss(); |
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
#!/bin/bash | |
set -ex | |
docker build --rm --force-rm -t ru-maxrss . | |
docker run -it --rm ru-maxrss |
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
#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); | |
} | |
} |
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
void show(int who, const char *whos); |
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
#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