nsprpub/pr/tests/fsync.c

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

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

mercurial