Back to index

lightning-sunbird  0.9+nobinonly
fsync.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "prio.h"
00039 #include "prmem.h"
00040 #include "prprf.h"
00041 #include "prinrval.h"
00042 
00043 #include "plerror.h"
00044 #include "plgetopt.h"
00045 
00046 static PRFileDesc *err = NULL;
00047 
00048 static void Help(void)
00049 {
00050     PR_fprintf(err, "Usage: [-S] [-K <n>] [-h] <filename>\n");
00051     PR_fprintf(err, "\t-c   Nuber of iterations     (default: 10)\n");
00052     PR_fprintf(err, "\t-S   Sync the file           (default: FALSE)\n");
00053     PR_fprintf(err, "\t-K   Size of file (K bytes)  (default: 10)\n");
00054     PR_fprintf(err, "\t     Name of file to write   (default: /usr/tmp/sync.dat)\n");
00055     PR_fprintf(err, "\t-h   This message and nothing else\n");
00056 }  /* Help */
00057 
00058 PRIntn main(PRIntn argc, char **argv)
00059 {
00060     PRStatus rv;
00061     PLOptStatus os;
00062     PRUint8 *buffer;
00063     PRFileDesc *file = NULL;
00064     const char *filename = "sync.dat";
00065     PRUint32 index, loops, iterations = 10, filesize = 10;
00066     PRIntn flags = PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE;
00067     PLOptState *opt = PL_CreateOptState(argc, argv, "hSK:c:");
00068     PRIntervalTime time, total = 0, shortest = 0x7fffffff, longest = 0;
00069 
00070     err = PR_GetSpecialFD(PR_StandardError);
00071 
00072     while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
00073     {
00074         if (PL_OPT_BAD == os) continue;
00075         switch (opt->option)
00076         {
00077         case 0:       /* Name of file to create */
00078             filename = opt->value;
00079             break;
00080         case 'S':       /* Use sych option on file */
00081             flags |= PR_SYNC;
00082             break;
00083         case 'K':       /* Size of file to write */
00084             filesize = atoi(opt->value);
00085             break;
00086         case 'c':       /* Number of iterations */
00087             iterations = atoi(opt->value);
00088             break;
00089         case 'h':       /* user wants some guidance */
00090         default:        /* user needs some guidance */
00091             Help();     /* so give him an earful */
00092             return 2;   /* but not a lot else */
00093         }
00094     }
00095     PL_DestroyOptState(opt);
00096 
00097     file = PR_Open(filename, flags, 0666);
00098     if (NULL == file)
00099     {
00100         PL_FPrintError(err, "Failed to open file");
00101         return 1;
00102     }
00103 
00104     buffer = (PRUint8*)PR_CALLOC(1024);
00105     if (NULL == buffer)
00106     {
00107         PL_FPrintError(err, "Cannot allocate buffer");
00108         return 1;
00109     }
00110 
00111     for (index = 0; index < sizeof(buffer); ++index)
00112         buffer[index] = (PRUint8)index;
00113 
00114     for (loops = 0; loops < iterations; ++loops)
00115     {
00116         time = PR_IntervalNow();
00117         for (index = 0; index < filesize; ++index)
00118         {
00119             PR_Write(file, buffer, 1024);
00120         }
00121         time = (PR_IntervalNow() - time);
00122 
00123         total += time;
00124         if (time < shortest) shortest = time;
00125         else if (time > longest) longest = time;
00126         if (0 != PR_Seek(file, 0, PR_SEEK_SET))
00127         {
00128            PL_FPrintError(err, "Rewinding file");
00129            return 1;
00130         }
00131     }
00132 
00133     total = total / iterations;
00134     PR_fprintf(
00135         err, "%u iterations over a %u kbyte %sfile: %u [%u] %u\n",
00136         iterations, filesize, ((flags & PR_SYNC) ? "SYNCH'd " : ""),
00137         PR_IntervalToMicroseconds(shortest),
00138         PR_IntervalToMicroseconds(total),
00139         PR_IntervalToMicroseconds(longest));
00140 
00141     PR_DELETE(buffer);
00142     rv = PR_Close(file);
00143     if (PR_SUCCESS != rv)
00144     {
00145         PL_FPrintError(err, "Closing file failed");
00146         return 1;
00147     }
00148     rv = PR_Delete(filename);
00149     if (PR_SUCCESS != rv)
00150     {
00151         PL_FPrintError(err, "Deleting file failed");
00152         return 1;
00153     }
00154     return 0;
00155 }