Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // avp 2015 strange getrusage() behavior
- // (from http://ru.stackoverflow.com/questions/85287 compare /usr/bin/time & /proc/[PID]/status RSS)
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/time.h>
- #include <sys/resource.h>
- #include <errno.h>
- #include <limits.h>
- #include <fcntl.h>
- void
- usage (int rc)
- {
- puts("Usage: ./a.out [-f] [-s] [-r ...] [-?] [-h]\n"
- "\t-?,-h\t this text and exit\n"
- "\t-f\t fork/exec and wait3() for get resources (as in /usr/bin/time)\n"
- "\t-s\t use system(grep Vm /proc/PID/status) other use FILE *...\n"
- "\t-r\t repeate FILE fgets /proc/PID/status (may be more than one)\n"
- "\t-R\t repeate using open/read /proc/PID/status (may be more than one)"
- );
- exit(rc);
- }
- int
- main (int ac, char *av[])
- {
- char cmd[100], str[LINE_MAX * 2];
- struct rusage r;
- pid_t p;
- int i, doit = 1, use_file = 1, grep = 0, repeate = 0;
- for (i = 1; av[i]; i++)
- if (strcmp(av[i], "-f") == 0)
- doit = 0;
- else if (strcmp(av[i], "-s") == 0)
- grep = 1;
- else if (strcmp(av[i], "-r") == 0) {
- use_file = 1;
- repeate++;
- } else if (strcmp(av[i], "-R") == 0) {
- use_file = 0;
- repeate++;
- } else if (strcmp(av[i], "-?") == 0 || strcmp(av[i], "-h") == 0)
- usage(0);
- else {
- printf("unknown option '%s'\n", av[i]);
- usage(1);
- }
- if (doit) {
- int fail = getrusage(RUSAGE_SELF, &r);
- printf("start ru_maxrss: %ld (%s)\n", r.ru_maxrss, fail ? strerror(errno) : "OK");
- if (grep) {
- puts("system grep");
- sprintf(cmd, "grep Vm /proc/%d/status", (int)getpid());
- system(cmd);
- } else {
- puts("file grep");
- sprintf(cmd, "/proc/%d/status", (int)getpid());
- FILE *f = fopen(cmd, "r");
- while (fgets(str, LINE_MAX, f))
- if (strncmp(str, "Vm", 2) == 0)
- fputs(str, stdout);
- fclose(f);
- }
- if (repeate) {
- sprintf(cmd, "/proc/%d/status", (int)getpid());
- printf("... and now repeate with file grep %d times\n", repeate);
- // why we need fopen() for update /proc/[PID]/status and getrusage() ?
- while (repeate--) {
- if (use_file) { // -r -r -s -f CHANGE /proc/[PID]/status and getrusage() !!!
- FILE *f = fopen(cmd, "r");
- while (fgets(str, LINE_MAX, f))
- if (strncmp(str, "VmRSS", 5) == 0)
- fputs(str, stdout);
- fclose(f);
- } else { // -R -R -s -f DON'T CHANGE /proc/[PID]/status and getrusage() ???
- int l, fd;
- printf ("read %d ... ", l = read(fd = open(cmd, O_RDONLY), str, LINE_MAX));
- close(fd);
- str[l > 0 ? l : 0] = 0;
- char *t = strstr(str, "VmRSS:");
- if (t) {
- *strchr(t, '\n') = 0;
- puts(t);
- } else
- puts("ERROR /proc");
- }
- fail = getrusage(RUSAGE_SELF, &r);
- printf("mid ru_maxrss: %ld (%s)\n", r.ru_maxrss, fail ? strerror(errno) : "OK");
- }
- }
- fail = getrusage(RUSAGE_SELF, &r);
- printf("fin ru_maxrss: %ld (%s)\n", r.ru_maxrss, fail ? strerror(errno) : "OK");
- } else if (p = fork()) {
- printf("run child %d\n", (int)p);
- p = wait3(0, 0, &r);
- printf("child %d wait3.ru_maxrss: %ld\n", (int)p, r.ru_maxrss);
- } else {
- const char *ar[repeate + 3]; // = {av[0]};
- i = 0;
- ar[i++] = av[0];
- if (grep)
- ar[i++] = "-s";
- while (repeate--)
- ar[i++] = use_file ? "-r" : "-R";
- ar[i] = 0;
- execv(ar[0], ar);
- perror(av[0]);
- }
- return puts("End") == EOF;
- }
Advertisement
Add Comment
Please, Sign In to add comment