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