Debian Codegraph @

nsprpub/pr/tests/fsync.c

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