lat_syscall.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * lat_syscall.c - time simple system calls
  3. *
  4. * Copyright (c) 1996 Larry McVoy. Distributed under the FSF GPL with
  5. * additional restriction that results may published only if
  6. * (1) the benchmark is unmodified, and
  7. * (2) the version in the sccsid below is included in the report.
  8. */
  9. char *id = "$Id$\n";
  10. #include "bench.h"
  11. #define FNAME "/usr/include/x86_64-linux-gnu/sys/types.h"
  12. void
  13. do_write(int fd, char *s)
  14. {
  15. char c;
  16. if (write(fd, &c, 1) != 1) {
  17. perror(s);
  18. return;
  19. }
  20. }
  21. void
  22. do_read(int fd, char *s)
  23. {
  24. char c;
  25. if (read(fd, &c, 1) != 1) {
  26. perror(s);
  27. return;
  28. }
  29. }
  30. void
  31. do_read_size(int fd, void * buf, size_t size, char *s)
  32. {
  33. if (read(fd, buf, size) == -1) {
  34. perror(s);
  35. return;
  36. }
  37. }
  38. void
  39. do_stat(char *s)
  40. {
  41. struct stat sbuf;
  42. if (stat(s, &sbuf) == -1) {
  43. perror(s);
  44. return;
  45. }
  46. }
  47. void
  48. do_fstat(int fd)
  49. {
  50. struct stat sbuf;
  51. if (fstat(fd, &sbuf) == -1) {
  52. perror("fstat");
  53. return;
  54. }
  55. }
  56. void
  57. do_openclose(char *s)
  58. {
  59. int fd;
  60. fd = open(s, 0);
  61. if (fd == -1) {
  62. perror(s);
  63. return;
  64. }
  65. close(fd);
  66. }
  67. int
  68. main(int ac, char **av)
  69. {
  70. int fd;
  71. char *file;
  72. if (ac < 2) goto usage;
  73. file = av[2] ? av[2] : FNAME;
  74. if (!strcmp("null", av[1])) {
  75. BENCH(getppid(), 0);
  76. micro("Simple syscall", get_n());
  77. } else if (!strcmp("write", av[1])) {
  78. file = av[2] ? av[2] : "/dev/null";
  79. fd = open(file, 1);
  80. if (fd == -1) {
  81. fprintf(stderr, "Write from %s: %s\n", file, strerror(errno));
  82. return(1);
  83. }
  84. BENCH(do_write(fd, file), 0);;
  85. micro("Simple write", get_n());
  86. close(fd);
  87. } else if (!strcmp("read", av[1])) {
  88. file = av[2] ? av[2] : "/dev/zero";
  89. fd = open(file, 0);
  90. if (fd == -1) {
  91. fprintf(stderr, "Read from %s: %s\n", file, strerror(errno));
  92. return(1);
  93. }
  94. BENCH(do_read(fd, file), 0);
  95. micro("Simple read", get_n());
  96. close(fd);
  97. } else if (!strcmp("read-size", av[1])) {
  98. size_t size = bytes(av[2]);
  99. void * buf = malloc(size);
  100. file = av[3] ? av[3] : "/dev/zero";
  101. fd = open(file, 0);
  102. if (fd == -1) {
  103. fprintf(stderr, "Read from %s: %s\n", file, strerror(errno));
  104. return(1);
  105. }
  106. BENCH(do_read_size(fd, buf, size, file), 0);
  107. micro("Simple read", get_n());
  108. close(fd);
  109. } else if (!strcmp("stat", av[1])) {
  110. BENCH(do_stat(file), 0);
  111. micro("Simple stat", get_n());
  112. } else if (!strcmp("fstat", av[1])) {
  113. fd = open(file, 0);
  114. BENCH(do_fstat(fd), 0);
  115. micro("Simple fstat", get_n());
  116. } else if (!strcmp("open", av[1])) {
  117. BENCH(do_openclose(file), 0);
  118. micro("Simple open/close", get_n());
  119. } else {
  120. usage: printf("Usage: %s null|read|write|stat|open\n", av[0]);
  121. }
  122. return(0);
  123. }