Advertisement
teknoraver

O_PATH overhead

Jun 30th, 2024
723
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.47 KB | None | 0 0
  1. /*
  2.  * # perf stat --all-kernel ./open open.c p |&grep -e cycles:khH -e instructions:khH
  3.  *     5,412,222,342      cycles:khH                       #    2.715 GHz
  4.  *     5,878,446,552      instructions:khH                 #    1.09  insn per cycle
  5.  * # perf stat --all-kernel ./open open.c r |&grep -e cycles:khH -e instructions:khH
  6.  *     8,654,592,565      cycles:khH                       #    3.328 GHz
  7.  *     8,223,833,831      instructions:khH                 #    0.95  insn per cycle
  8.  * # perf stat --all-kernel ./open open.c w |&grep -e cycles:khH -e instructions:khH
  9.  *     9,799,845,626      cycles:khH                       #    3.417 GHz
  10.  *     9,059,955,513      instructions:khH                 #    0.92  insn per cycle
  11.  * # perf stat --all-kernel ./open open.c rw |&grep -e cycles:khH -e instructions:khH
  12.  *    10,163,667,588      cycles:khH                       #    3.348 GHz
  13.  *     9,351,841,216      instructions:khH                 #    0.92  insn per cycle
  14.  */
  15.  
  16. #define _GNU_SOURCE
  17.  
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include <fcntl.h>
  21.  
  22. int main(int argc, char *argv[])
  23. {
  24.     char *filename, *mode;
  25.     int fd, flags = O_PATH;
  26.  
  27.     if (argc < 3)
  28.         return 1;
  29.  
  30.     filename = argv[1];
  31.     mode = argv[2];
  32.  
  33.     if (!strcmp(mode, "r"))
  34.         flags = O_RDONLY;
  35.     else if (!strcmp(mode, "w"))
  36.         flags = O_WRONLY;
  37.     else if (!strcmp(mode, "rw"))
  38.         flags = O_RDWR;
  39.  
  40.     for (int i = 0; i < 1000000; i++) {
  41.         fd = open(filename, flags);
  42.         close(fd);
  43.     }
  44.  
  45.     return 0;
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement