read.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*++
  2. Copyright (c) 2015 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. read.c
  5. Abstract:
  6. This module implements the performance benchmark tests for the read() C
  7. library routine.
  8. Author:
  9. Chris Stevens 6-May-2015
  10. Environment:
  11. User
  12. --*/
  13. //
  14. // ------------------------------------------------------------------- Includes
  15. //
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <errno.h>
  19. #include <fcntl.h>
  20. #include <unistd.h>
  21. #include "perftest.h"
  22. //
  23. // ---------------------------------------------------------------- Definitions
  24. //
  25. #define PT_READ_TEST_FILE_NAME_LENGTH 48
  26. #define PT_READ_TEST_FILE_SIZE (2 * 1024 * 1024)
  27. #define PT_READ_TEST_BUFFER_SIZE 4096
  28. //
  29. // ------------------------------------------------------ Data Type Definitions
  30. //
  31. //
  32. // ----------------------------------------------- Internal Function Prototypes
  33. //
  34. //
  35. // -------------------------------------------------------------------- Globals
  36. //
  37. //
  38. // ------------------------------------------------------------------ Functions
  39. //
  40. void
  41. ReadMain (
  42. PPT_TEST_INFORMATION Test,
  43. PPT_TEST_RESULT Result
  44. )
  45. /*++
  46. Routine Description:
  47. This routine performs the read performance benchmark test.
  48. Arguments:
  49. Test - Supplies a pointer to the performance test being executed.
  50. Result - Supplies a pointer to a performance test result structure that
  51. receives the tests results.
  52. Return Value:
  53. None.
  54. --*/
  55. {
  56. char *Buffer;
  57. ssize_t BytesRead;
  58. ssize_t BytesWritten;
  59. int FileCreated;
  60. int FileDescriptor;
  61. char FileName[PT_READ_TEST_FILE_NAME_LENGTH];
  62. int Index;
  63. pid_t ProcessId;
  64. int Status;
  65. unsigned long long TotalBytes;
  66. FileCreated = 0;
  67. FileDescriptor = -1;
  68. Result->Type = PtResultBytes;
  69. Result->Status = 0;
  70. TotalBytes = 0;
  71. //
  72. // Allocate a buffer for the reads.
  73. //
  74. Buffer = malloc(PT_READ_TEST_BUFFER_SIZE);
  75. if (Buffer == NULL) {
  76. Result->Status = ENOMEM;
  77. goto MainEnd;
  78. }
  79. //
  80. // Get the process ID and create a process safe file path.
  81. //
  82. ProcessId = getpid();
  83. Status = snprintf(FileName,
  84. PT_READ_TEST_FILE_NAME_LENGTH,
  85. "read_%d.txt",
  86. ProcessId);
  87. if (Status < 0) {
  88. Result->Status = errno;
  89. goto MainEnd;
  90. }
  91. //
  92. // Create and open the file with read/write permission so the size can be
  93. // extended.
  94. //
  95. FileDescriptor = open(FileName,
  96. O_RDWR | O_CREAT | O_TRUNC,
  97. S_IRUSR | S_IWUSR);
  98. if (FileDescriptor < 0) {
  99. Result->Status = errno;
  100. goto MainEnd;
  101. }
  102. FileCreated = 1;
  103. //
  104. // As this is really a test of reading from the system's cache, prime the
  105. // cache with junk data. ftruncate() could be used here but POSIX does not
  106. // require systems to support file extension and the goal is not to test
  107. // various implementations of fruncate() file extension.
  108. //
  109. for (Index = 0;
  110. Index < (PT_READ_TEST_FILE_SIZE / PT_READ_TEST_BUFFER_SIZE);
  111. Index += 1) {
  112. do {
  113. BytesWritten = write(FileDescriptor,
  114. Buffer,
  115. PT_READ_TEST_BUFFER_SIZE);
  116. } while ((BytesWritten < 0) && (errno == EINTR));
  117. if (BytesWritten < 0) {
  118. Result->Status = errno;
  119. goto MainEnd;
  120. }
  121. if (BytesWritten != PT_READ_TEST_BUFFER_SIZE) {
  122. Result->Status = EIO;
  123. goto MainEnd;
  124. }
  125. }
  126. //
  127. // Let the system process these writes before starting the reads.
  128. //
  129. Status = fsync(FileDescriptor);
  130. if (Status != 0) {
  131. Result->Status = errno;
  132. goto MainEnd;
  133. }
  134. //
  135. // Start the test. This snaps resource usage and starts the clock ticking.
  136. //
  137. Status = PtStartTimedTest(Test->Duration);
  138. if (Status != 0) {
  139. Result->Status = errno;
  140. goto MainEnd;
  141. }
  142. //
  143. // Measure the performance of the read() C library routine by counting the
  144. // number of bytes that can be read in.
  145. //
  146. while (PtIsTimedTestRunning() != 0) {
  147. do {
  148. BytesRead = read(FileDescriptor, Buffer, PT_READ_TEST_BUFFER_SIZE);
  149. } while ((BytesRead < 0) && (errno == EINTR));
  150. if (BytesRead < 0) {
  151. Result->Status = errno;
  152. break;
  153. }
  154. //
  155. // If the bytes read did not fill the entire buffer, then the end of
  156. // the file was likely reached. Seek back to the beginning.
  157. //
  158. if (BytesRead != PT_READ_TEST_BUFFER_SIZE) {
  159. Status = lseek(FileDescriptor, 0, SEEK_SET);
  160. if (Status != 0) {
  161. if (Status < 0) {
  162. Result->Status = errno;
  163. } else {
  164. Result->Status = EIO;
  165. }
  166. break;
  167. }
  168. }
  169. TotalBytes += (unsigned long long)BytesRead;
  170. }
  171. Status = PtFinishTimedTest(Result);
  172. if ((Status != 0) && (Result->Status == 0)) {
  173. Result->Status = errno;
  174. }
  175. MainEnd:
  176. if (FileCreated != 0) {
  177. close(FileDescriptor);
  178. remove(FileName);
  179. }
  180. if (Buffer != NULL) {
  181. free(Buffer);
  182. }
  183. Result->Data.Bytes = TotalBytes;
  184. return;
  185. }
  186. //
  187. // --------------------------------------------------------- Internal Functions
  188. //