avp210159

getrusage-t.c strange getrusage() & /proc/PID/status behavio

Aug 10th, 2015
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.44 KB | None | 0 0
  1. // avp 2015 strange getrusage() behavior
  2. //   (from http://ru.stackoverflow.com/questions/85287 compare /usr/bin/time & /proc/[PID]/status RSS)
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <sys/time.h>
  8. #include <sys/resource.h>
  9. #include <errno.h>
  10. #include <limits.h>
  11. #include <fcntl.h>
  12.  
  13. void
  14. usage (int rc)
  15. {
  16.   puts("Usage: ./a.out [-f] [-s] [-r ...] [-?] [-h]\n"
  17.        "\t-?,-h\t this text and exit\n"
  18.        "\t-f\t fork/exec and  wait3() for get resources (as in /usr/bin/time)\n"
  19.        "\t-s\t use system(grep Vm /proc/PID/status) other use FILE *...\n"
  20.        "\t-r\t repeate FILE fgets /proc/PID/status (may be more than one)\n"
  21.        "\t-R\t repeate using open/read /proc/PID/status (may be more than one)"
  22.        );
  23.   exit(rc);
  24. }
  25.  
  26. int
  27. main (int ac, char *av[])
  28. {
  29.   char cmd[100], str[LINE_MAX * 2];
  30.   struct rusage r;
  31.   pid_t p;
  32.   int i, doit = 1, use_file = 1, grep = 0, repeate = 0;
  33.  
  34.   for (i = 1; av[i]; i++)
  35.     if (strcmp(av[i], "-f") == 0)
  36.       doit = 0;
  37.     else if (strcmp(av[i], "-s") == 0)
  38.       grep = 1;
  39.     else if (strcmp(av[i], "-r") == 0) {
  40.       use_file = 1;
  41.       repeate++;
  42.     } else if (strcmp(av[i], "-R") == 0) {
  43.       use_file = 0;
  44.       repeate++;
  45.     } else if (strcmp(av[i], "-?") == 0 || strcmp(av[i], "-h") == 0)
  46.       usage(0);
  47.     else {
  48.       printf("unknown option '%s'\n", av[i]);
  49.       usage(1);
  50.     }
  51.  
  52.  
  53.   if (doit) {
  54.     int fail = getrusage(RUSAGE_SELF, &r);
  55.     printf("start ru_maxrss: %ld (%s)\n", r.ru_maxrss, fail ? strerror(errno) : "OK");
  56.     if (grep) {
  57.       puts("system grep");
  58.       sprintf(cmd, "grep Vm /proc/%d/status", (int)getpid());
  59.       system(cmd);
  60.     } else {
  61.       puts("file grep");
  62.       sprintf(cmd, "/proc/%d/status", (int)getpid());
  63.       FILE *f = fopen(cmd, "r");
  64.       while (fgets(str, LINE_MAX, f))
  65.     if (strncmp(str, "Vm", 2) == 0)
  66.       fputs(str, stdout);
  67.       fclose(f);
  68.     }
  69.     if (repeate) {
  70.       sprintf(cmd, "/proc/%d/status", (int)getpid());
  71.       printf("... and now repeate with file grep %d times\n", repeate);
  72.       // why we need fopen() for update /proc/[PID]/status and getrusage() ?
  73.       while (repeate--) {
  74.     if (use_file) { // -r -r  -s -f CHANGE /proc/[PID]/status and getrusage() !!!
  75.       FILE *f = fopen(cmd, "r");
  76.       while (fgets(str, LINE_MAX, f))
  77.         if (strncmp(str, "VmRSS", 5) == 0)
  78.           fputs(str, stdout);
  79.       fclose(f);
  80.     } else { // -R -R  -s -f DON'T CHANGE /proc/[PID]/status and getrusage() ???
  81.       int l, fd;
  82.       printf ("read %d ... ", l = read(fd = open(cmd, O_RDONLY), str, LINE_MAX));
  83.       close(fd);
  84.       str[l > 0 ? l : 0] = 0;
  85.       char *t = strstr(str, "VmRSS:");
  86.       if (t) {
  87.         *strchr(t, '\n') = 0;
  88.         puts(t);
  89.       } else
  90.         puts("ERROR /proc");
  91.     }
  92.     fail = getrusage(RUSAGE_SELF, &r);
  93.     printf("mid ru_maxrss: %ld (%s)\n", r.ru_maxrss, fail ? strerror(errno) : "OK");
  94.       }
  95.     }
  96.     fail = getrusage(RUSAGE_SELF, &r);
  97.     printf("fin ru_maxrss: %ld (%s)\n", r.ru_maxrss, fail ? strerror(errno) : "OK");
  98.   } else if (p = fork()) {
  99.     printf("run child %d\n", (int)p);
  100.     p = wait3(0, 0, &r);
  101.     printf("child %d wait3.ru_maxrss: %ld\n", (int)p, r.ru_maxrss);
  102.   } else {
  103.     const char *ar[repeate + 3]; // = {av[0]};
  104.     i = 0;
  105.     ar[i++] = av[0];
  106.     if (grep)
  107.       ar[i++] = "-s";
  108.     while (repeate--)
  109.       ar[i++] = use_file ? "-r" : "-R";
  110.     ar[i] = 0;
  111.     execv(ar[0], ar);
  112.     perror(av[0]);
  113.   }
  114.    
  115.   return puts("End") == EOF;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment