nsprpub/pr/tests/ranfile.c

Fri, 16 Jan 2015 04:50:19 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 04:50:19 +0100
branch
TOR_BUG_9701
changeset 13
44a2da4a2ab2
permissions
-rw-r--r--

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 /***********************************************************************
michael@0 7 **
michael@0 8 ** Contact: AOF<freier@netscape.com>
michael@0 9 **
michael@0 10 ** Name: ranfile.c
michael@0 11 **
michael@0 12 ** Description: Test to hammer on various components of NSPR
michael@0 13 ** Modification History:
michael@0 14 ** 20-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
michael@0 15 ** The debug mode will print all of the printfs associated with this test.
michael@0 16 ** The regress mode will be the default mode. Since the regress tool limits
michael@0 17 ** the output to a one line status:PASS or FAIL,all of the printf statements
michael@0 18 ** have been handled with an if (debug_mode) statement.
michael@0 19 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
michael@0 20 ** recognize the return code from tha main program.
michael@0 21 ***********************************************************************/
michael@0 22
michael@0 23
michael@0 24 /***********************************************************************
michael@0 25 ** Includes
michael@0 26 ***********************************************************************/
michael@0 27 /* Used to get the command line option */
michael@0 28 #include "plgetopt.h"
michael@0 29
michael@0 30 #include "prinit.h"
michael@0 31 #include "prthread.h"
michael@0 32 #include "prlock.h"
michael@0 33 #include "prcvar.h"
michael@0 34 #include "prmem.h"
michael@0 35 #include "prinrval.h"
michael@0 36 #include "prio.h"
michael@0 37
michael@0 38 #include <string.h>
michael@0 39 #include <stdio.h>
michael@0 40
michael@0 41 static PRIntn debug_mode = 0;
michael@0 42 static PRIntn failed_already=0;
michael@0 43 static PRThreadScope thread_scope = PR_LOCAL_THREAD;
michael@0 44
michael@0 45 typedef enum {sg_go, sg_stop, sg_done} Action;
michael@0 46 typedef enum {sg_okay, sg_open, sg_close, sg_delete, sg_write, sg_seek} Problem;
michael@0 47
michael@0 48 typedef struct Hammer_s {
michael@0 49 PRLock *ml;
michael@0 50 PRCondVar *cv;
michael@0 51 PRUint32 id;
michael@0 52 PRUint32 limit;
michael@0 53 PRUint32 writes;
michael@0 54 PRThread *thread;
michael@0 55 PRIntervalTime timein;
michael@0 56 Action action;
michael@0 57 Problem problem;
michael@0 58 } Hammer_t;
michael@0 59
michael@0 60 #define DEFAULT_LIMIT 10
michael@0 61 #define DEFAULT_THREADS 2
michael@0 62 #define DEFAULT_LOOPS 1
michael@0 63
michael@0 64 static PRInt32 pageSize = 1024;
michael@0 65 static const char* baseName = "./";
michael@0 66 static const char *programName = "Random File";
michael@0 67
michael@0 68 /***********************************************************************
michael@0 69 ** PRIVATE FUNCTION: RandomNum
michael@0 70 ** DESCRIPTION:
michael@0 71 ** Generate a pseudo-random number
michael@0 72 ** INPUTS: None
michael@0 73 ** OUTPUTS: None
michael@0 74 ** RETURN: A pseudo-random unsigned number, 32-bits wide
michael@0 75 ** SIDE EFFECTS:
michael@0 76 ** Updates random seed (a static)
michael@0 77 ** RESTRICTIONS:
michael@0 78 ** None
michael@0 79 ** MEMORY: NA
michael@0 80 ** ALGORITHM:
michael@0 81 ** Uses the current interval timer value, promoted to a 64 bit
michael@0 82 ** float as a multiplier for a static residue (which begins
michael@0 83 ** as an uninitialized variable). The result is bits [16..48)
michael@0 84 ** of the product. Seed is then updated with the return value
michael@0 85 ** promoted to a float-64.
michael@0 86 ***********************************************************************/
michael@0 87 static PRUint32 RandomNum(void)
michael@0 88 {
michael@0 89 PRUint32 rv;
michael@0 90 PRUint64 shift;
michael@0 91 static PRFloat64 seed = 0x58a9382; /* Just make sure it isn't 0! */
michael@0 92 PRFloat64 random = seed * (PRFloat64)PR_IntervalNow();
michael@0 93 LL_USHR(shift, *((PRUint64*)&random), 16);
michael@0 94 LL_L2UI(rv, shift);
michael@0 95 seed = (PRFloat64)rv;
michael@0 96 return rv;
michael@0 97 } /* RandomNum */
michael@0 98
michael@0 99 /***********************************************************************
michael@0 100 ** PRIVATE FUNCTION: Thread
michael@0 101 ** DESCRIPTION:
michael@0 102 ** Hammer on the file I/O system
michael@0 103 ** INPUTS: A pointer to the thread's private data
michael@0 104 ** OUTPUTS: None
michael@0 105 ** RETURN: None
michael@0 106 ** SIDE EFFECTS:
michael@0 107 ** Creates, accesses and deletes a file
michael@0 108 ** RESTRICTIONS:
michael@0 109 ** (Currently) must have file create permission in "/usr/tmp".
michael@0 110 ** MEMORY: NA
michael@0 111 ** ALGORITHM:
michael@0 112 ** This function is a root of a thread
michael@0 113 ** 1) Creates a (hopefully) unique file in /usr/tmp/
michael@0 114 ** 2) Writes a zero to a random number of sequential pages
michael@0 115 ** 3) Closes the file
michael@0 116 ** 4) Reopens the file
michael@0 117 ** 5) Seeks to a random page within the file
michael@0 118 ** 6) Writes a one byte on that page
michael@0 119 ** 7) Repeat steps [5..6] for each page in the file
michael@0 120 ** 8) Close and delete the file
michael@0 121 ** 9) Repeat steps [1..8] until told to stop
michael@0 122 ** 10) Notify complete and return
michael@0 123 ***********************************************************************/
michael@0 124 static void PR_CALLBACK Thread(void *arg)
michael@0 125 {
michael@0 126 PRUint32 index;
michael@0 127 char filename[30];
michael@0 128 const char zero = 0;
michael@0 129 PRFileDesc *file = NULL;
michael@0 130 PRStatus rv = PR_SUCCESS;
michael@0 131 Hammer_t *cd = (Hammer_t*)arg;
michael@0 132
michael@0 133 (void)sprintf(filename, "%ssg%04ld.dat", baseName, cd->id);
michael@0 134
michael@0 135 if (debug_mode) printf("Starting work on %s\n", filename);
michael@0 136
michael@0 137 while (PR_TRUE)
michael@0 138 {
michael@0 139 PRUint32 bytes;
michael@0 140 PRUint32 minor = (RandomNum() % cd->limit) + 1;
michael@0 141 PRUint32 random = (RandomNum() % cd->limit) + 1;
michael@0 142 PRUint32 pages = (RandomNum() % cd->limit) + 10;
michael@0 143 while (minor-- > 0)
michael@0 144 {
michael@0 145 cd->problem = sg_okay;
michael@0 146 if (cd->action != sg_go) goto finished;
michael@0 147 cd->problem = sg_open;
michael@0 148 file = PR_Open(filename, PR_RDWR|PR_CREATE_FILE, 0666);
michael@0 149 if (file == NULL) goto finished;
michael@0 150 for (index = 0; index < pages; index++)
michael@0 151 {
michael@0 152 cd->problem = sg_okay;
michael@0 153 if (cd->action != sg_go) goto close;
michael@0 154 cd->problem = sg_seek;
michael@0 155 bytes = PR_Seek(file, pageSize * index, PR_SEEK_SET);
michael@0 156 if (bytes != pageSize * index) goto close;
michael@0 157 cd->problem = sg_write;
michael@0 158 bytes = PR_Write(file, &zero, sizeof(zero));
michael@0 159 if (bytes <= 0) goto close;
michael@0 160 cd->writes += 1;
michael@0 161 }
michael@0 162 cd->problem = sg_close;
michael@0 163 rv = PR_Close(file);
michael@0 164 if (rv != PR_SUCCESS) goto purge;
michael@0 165
michael@0 166 cd->problem = sg_okay;
michael@0 167 if (cd->action != sg_go) goto purge;
michael@0 168
michael@0 169 cd->problem = sg_open;
michael@0 170 file = PR_Open(filename, PR_RDWR, 0666);
michael@0 171 for (index = 0; index < pages; index++)
michael@0 172 {
michael@0 173 cd->problem = sg_okay;
michael@0 174 if (cd->action != sg_go) goto close;
michael@0 175 cd->problem = sg_seek;
michael@0 176 bytes = PR_Seek(file, pageSize * index, PR_SEEK_SET);
michael@0 177 if (bytes != pageSize * index) goto close;
michael@0 178 cd->problem = sg_write;
michael@0 179 bytes = PR_Write(file, &zero, sizeof(zero));
michael@0 180 if (bytes <= 0) goto close;
michael@0 181 cd->writes += 1;
michael@0 182 random = (random + 511) % pages;
michael@0 183 }
michael@0 184 cd->problem = sg_close;
michael@0 185 rv = PR_Close(file);
michael@0 186 if (rv != PR_SUCCESS) goto purge;
michael@0 187 cd->problem = sg_delete;
michael@0 188 rv = PR_Delete(filename);
michael@0 189 if (rv != PR_SUCCESS) goto finished;
michael@0 190 }
michael@0 191 }
michael@0 192
michael@0 193 close:
michael@0 194 (void)PR_Close(file);
michael@0 195 purge:
michael@0 196 (void)PR_Delete(filename);
michael@0 197 finished:
michael@0 198 PR_Lock(cd->ml);
michael@0 199 cd->action = sg_done;
michael@0 200 PR_NotifyCondVar(cd->cv);
michael@0 201 PR_Unlock(cd->ml);
michael@0 202
michael@0 203 if (debug_mode) printf("Ending work on %s\n", filename);
michael@0 204
michael@0 205 return;
michael@0 206 } /* Thread */
michael@0 207
michael@0 208 static Hammer_t hammer[100];
michael@0 209 static PRCondVar *cv;
michael@0 210 /***********************************************************************
michael@0 211 ** PRIVATE FUNCTION: main
michael@0 212 ** DESCRIPTION:
michael@0 213 ** Hammer on the file I/O system
michael@0 214 ** INPUTS: The usual argc and argv
michael@0 215 ** argv[0] - program name (not used)
michael@0 216 ** argv[1] - the number of times to execute the major loop
michael@0 217 ** argv[2] - the number of threads to toss into the batch
michael@0 218 ** argv[3] - the clipping number applied to randoms
michael@0 219 ** default values: loops = 2, threads = 10, limit = 57
michael@0 220 ** OUTPUTS: None
michael@0 221 ** RETURN: None
michael@0 222 ** SIDE EFFECTS:
michael@0 223 ** Creates, accesses and deletes lots of files
michael@0 224 ** RESTRICTIONS:
michael@0 225 ** (Currently) must have file create permission in "/usr/tmp".
michael@0 226 ** MEMORY: NA
michael@0 227 ** ALGORITHM:
michael@0 228 ** 1) Fork a "Thread()"
michael@0 229 ** 2) Wait for 'interleave' seconds
michael@0 230 ** 3) For [0..'threads') repeat [1..2]
michael@0 231 ** 4) Mark all objects to stop
michael@0 232 ** 5) Collect the threads, accumulating the results
michael@0 233 ** 6) For [0..'loops') repeat [1..5]
michael@0 234 ** 7) Print accumulated results and exit
michael@0 235 **
michael@0 236 ** Characteristic output (from IRIX)
michael@0 237 ** Random File: Using loops = 2, threads = 10, limit = 57
michael@0 238 ** Random File: [min [avg] max] writes/sec average
michael@0 239 ***********************************************************************/
michael@0 240 int main(int argc, char **argv)
michael@0 241 {
michael@0 242 PRLock *ml;
michael@0 243 PRUint32 id = 0;
michael@0 244 int active, poll;
michael@0 245 PRIntervalTime interleave;
michael@0 246 PRIntervalTime duration = 0;
michael@0 247 int limit = 0, loops = 0, threads = 0, times;
michael@0 248 PRUint32 writes, writesMin = 0x7fffffff, writesTot = 0, durationTot = 0, writesMax = 0;
michael@0 249
michael@0 250 const char *where[] = {"okay", "open", "close", "delete", "write", "seek"};
michael@0 251
michael@0 252 /* The command line argument: -d is used to determine if the test is being run
michael@0 253 in debug mode. The regress tool requires only one line output:PASS or FAIL.
michael@0 254 All of the printfs associated with this test has been handled with a if (debug_mode)
michael@0 255 test.
michael@0 256 Usage: test_name -d
michael@0 257 */
michael@0 258 PLOptStatus os;
michael@0 259 PLOptState *opt = PL_CreateOptState(argc, argv, "Gdl:t:i:");
michael@0 260 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
michael@0 261 {
michael@0 262 if (PL_OPT_BAD == os) continue;
michael@0 263 switch (opt->option)
michael@0 264 {
michael@0 265 case 'G': /* global threads */
michael@0 266 thread_scope = PR_GLOBAL_THREAD;
michael@0 267 break;
michael@0 268 case 'd': /* debug mode */
michael@0 269 debug_mode = 1;
michael@0 270 break;
michael@0 271 case 'l': /* limiting number */
michael@0 272 limit = atoi(opt->value);
michael@0 273 break;
michael@0 274 case 't': /* number of threads */
michael@0 275 threads = atoi(opt->value);
michael@0 276 break;
michael@0 277 case 'i': /* iteration counter */
michael@0 278 loops = atoi(opt->value);
michael@0 279 break;
michael@0 280 default:
michael@0 281 break;
michael@0 282 }
michael@0 283 }
michael@0 284 PL_DestroyOptState(opt);
michael@0 285
michael@0 286 /* main test */
michael@0 287
michael@0 288 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
michael@0 289 PR_STDIO_INIT();
michael@0 290
michael@0 291 interleave = PR_SecondsToInterval(10);
michael@0 292
michael@0 293 ml = PR_NewLock();
michael@0 294 cv = PR_NewCondVar(ml);
michael@0 295
michael@0 296 if (loops == 0) loops = DEFAULT_LOOPS;
michael@0 297 if (limit == 0) limit = DEFAULT_LIMIT;
michael@0 298 if (threads == 0) threads = DEFAULT_THREADS;
michael@0 299
michael@0 300 if (debug_mode) printf(
michael@0 301 "%s: Using loops = %d, threads = %d, limit = %d and %s threads\n",
michael@0 302 programName, loops, threads, limit,
michael@0 303 (thread_scope == PR_LOCAL_THREAD) ? "LOCAL" : "GLOBAL");
michael@0 304
michael@0 305 for (times = 0; times < loops; ++times)
michael@0 306 {
michael@0 307 if (debug_mode) printf("%s: Setting concurrency level to %d\n", programName, times + 1);
michael@0 308 PR_SetConcurrency(times + 1);
michael@0 309 for (active = 0; active < threads; active++)
michael@0 310 {
michael@0 311 hammer[active].ml = ml;
michael@0 312 hammer[active].cv = cv;
michael@0 313 hammer[active].id = id++;
michael@0 314 hammer[active].writes = 0;
michael@0 315 hammer[active].action = sg_go;
michael@0 316 hammer[active].problem = sg_okay;
michael@0 317 hammer[active].limit = (RandomNum() % limit) + 1;
michael@0 318 hammer[active].timein = PR_IntervalNow();
michael@0 319 hammer[active].thread = PR_CreateThread(
michael@0 320 PR_USER_THREAD, Thread, &hammer[active],
michael@0 321 PR_GetThreadPriority(PR_GetCurrentThread()),
michael@0 322 thread_scope, PR_JOINABLE_THREAD, 0);
michael@0 323
michael@0 324 PR_Lock(ml);
michael@0 325 PR_WaitCondVar(cv, interleave); /* start new ones slowly */
michael@0 326 PR_Unlock(ml);
michael@0 327 }
michael@0 328
michael@0 329 /*
michael@0 330 * The last thread started has had the opportunity to run for
michael@0 331 * 'interleave' seconds. Now gather them all back in.
michael@0 332 */
michael@0 333 PR_Lock(ml);
michael@0 334 for (poll = 0; poll < threads; poll++)
michael@0 335 {
michael@0 336 if (hammer[poll].action == sg_go) /* don't overwrite done */
michael@0 337 hammer[poll].action = sg_stop; /* ask him to stop */
michael@0 338 }
michael@0 339 PR_Unlock(ml);
michael@0 340
michael@0 341 while (active > 0)
michael@0 342 {
michael@0 343 for (poll = 0; poll < threads; poll++)
michael@0 344 {
michael@0 345 PR_Lock(ml);
michael@0 346 while (hammer[poll].action < sg_done)
michael@0 347 PR_WaitCondVar(cv, PR_INTERVAL_NO_TIMEOUT);
michael@0 348 PR_Unlock(ml);
michael@0 349
michael@0 350 active -= 1; /* this is another one down */
michael@0 351 (void)PR_JoinThread(hammer[poll].thread);
michael@0 352 hammer[poll].thread = NULL;
michael@0 353 if (hammer[poll].problem == sg_okay)
michael@0 354 {
michael@0 355 duration = PR_IntervalToMilliseconds(
michael@0 356 PR_IntervalNow() - hammer[poll].timein);
michael@0 357 writes = hammer[poll].writes * 1000 / duration;
michael@0 358 if (writes < writesMin)
michael@0 359 writesMin = writes;
michael@0 360 if (writes > writesMax)
michael@0 361 writesMax = writes;
michael@0 362 writesTot += hammer[poll].writes;
michael@0 363 durationTot += duration;
michael@0 364 }
michael@0 365 else
michael@0 366 if (debug_mode) printf(
michael@0 367 "%s: test failed %s after %ld seconds\n",
michael@0 368 programName, where[hammer[poll].problem], duration);
michael@0 369 else failed_already=1;
michael@0 370 }
michael@0 371 }
michael@0 372 }
michael@0 373 if (debug_mode) printf(
michael@0 374 "%s: [%ld [%ld] %ld] writes/sec average\n",
michael@0 375 programName, writesMin, writesTot * 1000 / durationTot, writesMax);
michael@0 376
michael@0 377 PR_DestroyCondVar(cv);
michael@0 378 PR_DestroyLock(ml);
michael@0 379
michael@0 380 if (failed_already)
michael@0 381 {
michael@0 382 printf("FAIL\n");
michael@0 383 return 1;
michael@0 384 }
michael@0 385 else
michael@0 386 {
michael@0 387 printf("PASS\n");
michael@0 388 return 0;
michael@0 389 }
michael@0 390 } /* main */

mercurial