fsync.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "prio.h"
  6. #include "prmem.h"
  7. #include "prprf.h"
  8. #include "prinrval.h"
  9. #include "plerror.h"
  10. #include "plgetopt.h"
  11. static PRFileDesc *err = NULL;
  12. static void Help(void)
  13. {
  14. PR_fprintf(err, "Usage: [-S] [-K <n>] [-h] <filename>\n");
  15. PR_fprintf(err, "\t-c Nuber of iterations (default: 10)\n");
  16. PR_fprintf(err, "\t-S Sync the file (default: FALSE)\n");
  17. PR_fprintf(err, "\t-K Size of file (K bytes) (default: 10)\n");
  18. PR_fprintf(err, "\t Name of file to write (default: ./tmp-sync.dat)\n");
  19. PR_fprintf(err, "\t-h This message and nothing else\n");
  20. } /* Help */
  21. int main(int argc, char **argv)
  22. {
  23. PRStatus rv;
  24. PLOptStatus os;
  25. PRUint8 *buffer;
  26. PRFileDesc *file = NULL;
  27. const char *filename = "sync.dat";
  28. PRUint32 index, loops, iterations = 10, filesize = 10;
  29. PRIntn flags = PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE;
  30. PLOptState *opt = PL_CreateOptState(argc, argv, "hSK:c:");
  31. PRIntervalTime time, total = 0, shortest = 0x7fffffff, longest = 0;
  32. err = PR_GetSpecialFD(PR_StandardError);
  33. while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  34. {
  35. if (PL_OPT_BAD == os) {
  36. continue;
  37. }
  38. switch (opt->option)
  39. {
  40. case 0: /* Name of file to create */
  41. filename = opt->value;
  42. break;
  43. case 'S': /* Use sych option on file */
  44. flags |= PR_SYNC;
  45. break;
  46. case 'K': /* Size of file to write */
  47. filesize = atoi(opt->value);
  48. break;
  49. case 'c': /* Number of iterations */
  50. iterations = atoi(opt->value);
  51. break;
  52. case 'h': /* user wants some guidance */
  53. default: /* user needs some guidance */
  54. Help(); /* so give him an earful */
  55. return 2; /* but not a lot else */
  56. }
  57. }
  58. PL_DestroyOptState(opt);
  59. file = PR_Open(filename, flags, 0666);
  60. if (NULL == file)
  61. {
  62. PL_FPrintError(err, "Failed to open file");
  63. return 1;
  64. }
  65. buffer = (PRUint8*)PR_CALLOC(1024);
  66. if (NULL == buffer)
  67. {
  68. PL_FPrintError(err, "Cannot allocate buffer");
  69. return 1;
  70. }
  71. for (index = 0; index < sizeof(buffer); ++index) {
  72. buffer[index] = (PRUint8)index;
  73. }
  74. for (loops = 0; loops < iterations; ++loops)
  75. {
  76. time = PR_IntervalNow();
  77. for (index = 0; index < filesize; ++index)
  78. {
  79. PR_Write(file, buffer, 1024);
  80. }
  81. time = (PR_IntervalNow() - time);
  82. total += time;
  83. if (time < shortest) {
  84. shortest = time;
  85. }
  86. else if (time > longest) {
  87. longest = time;
  88. }
  89. if (0 != PR_Seek(file, 0, PR_SEEK_SET))
  90. {
  91. PL_FPrintError(err, "Rewinding file");
  92. return 1;
  93. }
  94. }
  95. total = total / iterations;
  96. PR_fprintf(
  97. err, "%u iterations over a %u kbyte %sfile: %u [%u] %u\n",
  98. iterations, filesize, ((flags & PR_SYNC) ? "SYNCH'd " : ""),
  99. PR_IntervalToMicroseconds(shortest),
  100. PR_IntervalToMicroseconds(total),
  101. PR_IntervalToMicroseconds(longest));
  102. PR_DELETE(buffer);
  103. rv = PR_Close(file);
  104. if (PR_SUCCESS != rv)
  105. {
  106. PL_FPrintError(err, "Closing file failed");
  107. return 1;
  108. }
  109. rv = PR_Delete(filename);
  110. if (PR_SUCCESS != rv)
  111. {
  112. PL_FPrintError(err, "Deleting file failed");
  113. return 1;
  114. }
  115. return 0;
  116. }