perf.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright (C) 2007 Nokia Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. *
  18. * Author: Adrian Hunter
  19. */
  20. #include <unistd.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <stdint.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <fcntl.h>
  28. #include <errno.h>
  29. #include <sys/time.h>
  30. #include <time.h>
  31. #include <limits.h>
  32. #include "tests.h"
  33. #define BLOCK_SIZE 32 * 1024
  34. struct timeval tv_start;
  35. struct timeval tv_stop;
  36. static inline void start_timer(void)
  37. {
  38. CHECK(gettimeofday(&tv_start, NULL) != -1);
  39. }
  40. static inline long long stop_timer(void)
  41. {
  42. long long usecs;
  43. CHECK(gettimeofday(&tv_stop, NULL) != -1);
  44. usecs = (tv_stop.tv_sec - tv_start.tv_sec);
  45. usecs *= 1000000;
  46. usecs += tv_stop.tv_usec;
  47. usecs -= tv_start.tv_usec;
  48. return usecs;
  49. }
  50. static unsigned speed(size_t bytes, long long usecs)
  51. {
  52. unsigned long long k;
  53. k = bytes * 1000000ULL;
  54. k /= usecs;
  55. k /= 1024;
  56. CHECK(k <= UINT_MAX);
  57. return (unsigned) k;
  58. }
  59. static void perf(void)
  60. {
  61. pid_t pid;
  62. int fd, i;
  63. ssize_t written, readsz;
  64. size_t remains, block, actual_size;
  65. char file_name[256];
  66. unsigned char *buf;
  67. long long write_time, unmount_time, mount_time, read_time;
  68. /* Sync all file systems */
  69. sync();
  70. /* Make random data to write */
  71. buf = malloc(BLOCK_SIZE);
  72. CHECK(buf != NULL);
  73. pid = getpid();
  74. srand(pid);
  75. for (i = 0; i < BLOCK_SIZE; i++)
  76. buf[i] = rand();
  77. /* Open file */
  78. tests_cat_pid(file_name, "perf_test_file_", pid);
  79. start_timer();
  80. fd = open(file_name, O_CREAT | O_RDWR | O_TRUNC,
  81. S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
  82. CHECK(fd != -1);
  83. CHECK(tests_size_parameter > 0);
  84. CHECK(tests_size_parameter <= SIZE_MAX);
  85. /* Write to file */
  86. actual_size = 0;
  87. remains = tests_size_parameter;
  88. while (remains > 0) {
  89. if (remains > BLOCK_SIZE)
  90. block = BLOCK_SIZE;
  91. else
  92. block = remains;
  93. written = write(fd, buf, block);
  94. if (written <= 0) {
  95. CHECK(errno == ENOSPC); /* File system full */
  96. errno = 0;
  97. break;
  98. }
  99. remains -= written;
  100. actual_size += written;
  101. }
  102. CHECK(fsync(fd) != -1);
  103. CHECK(close(fd) != -1);
  104. write_time = stop_timer();
  105. /* Unmount */
  106. start_timer();
  107. tests_unmount();
  108. unmount_time = stop_timer();
  109. /* Mount */
  110. start_timer();
  111. tests_mount();
  112. mount_time = stop_timer();
  113. /* Open file, read it, and close it */
  114. start_timer();
  115. fd = open(file_name, O_RDONLY);
  116. CHECK(fd != -1);
  117. remains = actual_size;
  118. while (remains > 0) {
  119. if (remains > BLOCK_SIZE)
  120. block = BLOCK_SIZE;
  121. else
  122. block = remains;
  123. readsz = read(fd, buf, block);
  124. CHECK(readsz == block);
  125. remains -= readsz;
  126. }
  127. CHECK(close(fd) != -1);
  128. read_time = stop_timer();
  129. CHECK(unlink(file_name) != -1);
  130. /* Display timings */
  131. printf("File system read and write speed\n");
  132. printf("================================\n");
  133. printf("Specfied file size: %lld\n",
  134. (unsigned long long)tests_size_parameter);
  135. printf("Actual file size: %zu\n", actual_size);
  136. printf("Write time (us): %lld\n", write_time);
  137. printf("Unmount time (us): %lld\n", unmount_time);
  138. printf("Mount time (us): %lld\n", mount_time);
  139. printf("Read time (us): %lld\n", read_time);
  140. printf("Write speed (KiB/s): %u\n", speed(actual_size, write_time));
  141. printf("Read speed (KiB/s): %u\n", speed(actual_size, read_time));
  142. printf("Test completed\n");
  143. free(buf);
  144. }
  145. /* Title of this test */
  146. static const char *perf_get_title(void)
  147. {
  148. return "Measure file system read and write speed";
  149. }
  150. /* Description of this test */
  151. static const char *perf_get_description(void)
  152. {
  153. return
  154. "Syncs the file system (a newly created empty file system is " \
  155. "preferable). Creates a file named perf_test_file_pid, where " \
  156. "pid is the process id. The file is filled with random data. " \
  157. "The size of the file is given by the -z or --size option, " \
  158. "otherwise it defaults to 10MiB. Unmounts the file system. " \
  159. "Mounts the file system. Reads the entire file in 32KiB size " \
  160. "blocks. Displays the time taken for each activity. Deletes " \
  161. "the file. Note that the file is synced after writing and " \
  162. "that time is included in the write time and speed.";
  163. }
  164. int main(int argc, char *argv[])
  165. {
  166. int run_test;
  167. /* Set default test file size */
  168. tests_size_parameter = 10 * 1024 * 1024;
  169. /* Handle common arguments */
  170. run_test = tests_get_args(argc, argv, perf_get_title(),
  171. perf_get_description(), "z");
  172. if (!run_test)
  173. return 1;
  174. /* Change directory to the file system and check it is ok for testing */
  175. tests_check_test_file_system();
  176. /* Do the actual test */
  177. perf();
  178. return 0;
  179. }