1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
|
//
//********************************************************************
// Copyright (C) 2002-2005, International Business Machines
// Corporation and others. All Rights Reserved.
//********************************************************************
//
// File threadtest.cpp
//
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "unicode/utypes.h"
#include "unicode/uclean.h"
#include "umutex.h"
#include "threadtest.h"
//------------------------------------------------------------------------------
//
// Factory functions for creating different test types.
//
//------------------------------------------------------------------------------
extern AbstractThreadTest *createStringTest();
extern AbstractThreadTest *createConvertTest();
//------------------------------------------------------------------------------
//
// Windows specific code for starting threads
//
//------------------------------------------------------------------------------
#ifdef U_WINDOWS
#include "Windows.h"
#include "process.h"
typedef void (*ThreadFunc)(void *);
class ThreadFuncs // This class isolates OS dependent threading
{ // functions from the rest of ThreadTest program.
public:
static void Sleep(int millis) {::Sleep(millis);};
static void startThread(ThreadFunc, void *param);
static unsigned long getCurrentMillis();
static void yield() {::Sleep(0);};
};
void ThreadFuncs::startThread(ThreadFunc func, void *param)
{
unsigned long x;
x = _beginthread(func, 0x10000, param);
if (x == -1)
{
fprintf(stderr, "Error starting thread. Errno = %d\n", errno);
exit(-1);
}
}
unsigned long ThreadFuncs::getCurrentMillis()
{
return (unsigned long)::GetTickCount();
}
// #elif defined (POSIX)
#else
//------------------------------------------------------------------------------
//
// UNIX specific code for starting threads
//
//------------------------------------------------------------------------------
#include <pthread.h>
#include <unistd.h>
#include <errno.h>
#include <sched.h>
#include <sys/timeb.h>
extern "C" {
typedef void (*ThreadFunc)(void *);
typedef void *(*pthreadfunc)(void *);
class ThreadFuncs // This class isolates OS dependent threading
{ // functions from the rest of ThreadTest program.
public:
static void Sleep(int millis);
static void startThread(ThreadFunc, void *param);
static unsigned long getCurrentMillis();
static void yield() {sched_yield();};
};
void ThreadFuncs::Sleep(int millis)
{
int seconds = millis/1000;
if (seconds <= 0) seconds = 1;
::sleep(seconds);
}
void ThreadFuncs::startThread(ThreadFunc func, void *param)
{
unsigned long x;
pthread_t tId;
//thread_t tId;
#if defined(_HP_UX) && defined(XML_USE_DCE)
x = pthread_create( &tId, pthread_attr_default, (pthreadfunc)func, param);
#else
pthread_attr_t attr;
pthread_attr_init(&attr);
x = pthread_create( &tId, &attr, (pthreadfunc)func, param);
#endif
if (x == -1)
{
fprintf(stderr, "Error starting thread. Errno = %d\n", errno);
exit(-1);
}
}
unsigned long ThreadFuncs::getCurrentMillis() {
timeb aTime;
ftime(&aTime);
return (unsigned long)(aTime.time*1000 + aTime.millitm);
}
}
// #else
// #error This platform is not supported
#endif
//------------------------------------------------------------------------------
//
// struct runInfo Holds the info extracted from the command line and data
// that is shared by all threads.
// There is only one of these, and it is static.
// During the test, the threads will access this info without
// any synchronization.
//
//------------------------------------------------------------------------------
const int MAXINFILES = 25;
struct RunInfo
{
bool quiet;
bool verbose;
int numThreads;
int totalTime;
int checkTime;
AbstractThreadTest *fTest;
bool stopFlag;
bool exitFlag;
int32_t runningThreads;
};
//------------------------------------------------------------------------------
//
// struct threadInfo Holds information specific to an individual thread.
// One of these is set up for each thread in the test.
// The main program monitors the threads by looking
// at the status stored in these structs.
//
//------------------------------------------------------------------------------
struct ThreadInfo
{
bool fHeartBeat; // Set true by the thread each time it finishes
// a test.
unsigned int fCycles; // Number of cycles completed.
int fThreadNum; // Identifying number for this thread.
ThreadInfo() {
fHeartBeat = false;
fCycles = 0;
fThreadNum = -1;
}
};
//
//------------------------------------------------------------------------------
//
// Global Data
//
//------------------------------------------------------------------------------
RunInfo gRunInfo;
ThreadInfo *gThreadInfo;
UMTX gStopMutex; // Lets main thread suspend test threads.
UMTX gInfoMutex; // Synchronize access to data passed between
// worker threads and the main thread
//----------------------------------------------------------------------
//
// parseCommandLine Read through the command line, and save all
// of the options in the gRunInfo struct.
//
// Display the usage message if the command line
// is no good.
//
// Probably ought to be a member function of RunInfo.
//
//----------------------------------------------------------------------
void parseCommandLine(int argc, char **argv)
{
gRunInfo.quiet = false; // Set up defaults for run.
gRunInfo.verbose = false;
gRunInfo.numThreads = 2;
gRunInfo.totalTime = 0;
gRunInfo.checkTime = 10;
try // Use exceptions for command line syntax errors.
{
int argnum = 1;
while (argnum < argc)
{
if (strcmp(argv[argnum], "-quiet") == 0)
gRunInfo.quiet = true;
else if (strcmp(argv[argnum], "-verbose") == 0)
gRunInfo.verbose = true;
else if (strcmp(argv[argnum], "--help") == 0 ||
(strcmp(argv[argnum], "?") == 0)) {throw 1; }
else if (strcmp(argv[argnum], "-threads") == 0)
{
++argnum;
if (argnum >= argc)
throw 1;
gRunInfo.numThreads = atoi(argv[argnum]);
if (gRunInfo.numThreads < 0)
throw 1;
}
else if (strcmp(argv[argnum], "-time") == 0)
{
++argnum;
if (argnum >= argc)
throw 1;
gRunInfo.totalTime = atoi(argv[argnum]);
if (gRunInfo.totalTime < 1)
throw 1;
}
else if (strcmp(argv[argnum], "-ctime") == 0)
{
++argnum;
if (argnum >= argc)
throw 1;
gRunInfo.checkTime = atoi(argv[argnum]);
if (gRunInfo.checkTime < 1)
throw 1;
}
else if (strcmp(argv[argnum], "string") == 0)
{
gRunInfo.fTest = createStringTest();
}
else if (strcmp(argv[argnum], "convert") == 0)
{
gRunInfo.fTest = createConvertTest();
}
else
{
fprintf(stderr, "Unrecognized command line option. Scanning \"%s\"\n",
argv[argnum]);
throw 1;
}
argnum++;
}
// We've reached the end of the command line parameters.
// Fail if no test name was specified.
if (gRunInfo.fTest == NULL) {
fprintf(stderr, "No test specified.\n");
throw 1;
}
}
catch (int)
{
fprintf(stderr, "usage: threadtest [-threads nnn] [-time nnn] [-quiet] [-verbose] test-name\n"
" -quiet Suppress periodic status display. \n"
" -verbose Display extra messages. \n"
" -threads nnn Number of threads. Default is 2. \n"
" -time nnn Total time to run, in seconds. Default is forever.\n"
" -ctime nnn Time between extra consistency checks, in seconds. Default 10\n"
" testname string | convert\n"
);
exit(1);
}
}
//----------------------------------------------------------------------
//
// threadMain The main function for each of the swarm of test threads.
// Run in a loop, executing the runOnce() test function each time.
//
//
//----------------------------------------------------------------------
extern "C" {
void threadMain (void *param)
{
ThreadInfo *thInfo = (ThreadInfo *)param;
if (gRunInfo.verbose)
printf("Thread #%d: starting\n", thInfo->fThreadNum);
umtx_atomic_inc(&gRunInfo.runningThreads);
//
//
while (true)
{
if (gRunInfo.verbose )
printf("Thread #%d: starting loop\n", thInfo->fThreadNum);
//
// If the main thread is asking us to wait, do so by locking gStopMutex
// which will block us, since the main thread will be holding it already.
//
umtx_lock(&gInfoMutex);
UBool stop = gRunInfo.stopFlag; // Need mutex for processors with flakey memory models.
umtx_unlock(&gInfoMutex);
if (stop) {
if (gRunInfo.verbose) {
fprintf(stderr, "Thread #%d: suspending\n", thInfo->fThreadNum);
}
umtx_atomic_dec(&gRunInfo.runningThreads);
while (gRunInfo.stopFlag) {
umtx_lock(&gStopMutex);
umtx_unlock(&gStopMutex);
}
umtx_atomic_inc(&gRunInfo.runningThreads);
if (gRunInfo.verbose) {
fprintf(stderr, "Thread #%d: restarting\n", thInfo->fThreadNum);
}
}
//
// The real work of the test happens here.
//
gRunInfo.fTest->runOnce();
umtx_lock(&gInfoMutex);
thInfo->fHeartBeat = true;
thInfo->fCycles++;
UBool exitNow = gRunInfo.exitFlag;
umtx_unlock(&gInfoMutex);
//
// If main thread says it's time to exit, break out of the loop.
//
if (exitNow) {
break;
}
}
umtx_atomic_dec(&gRunInfo.runningThreads);
// Returning will kill the thread.
return;
}
}
//----------------------------------------------------------------------
//
// main
//
//----------------------------------------------------------------------
int main (int argc, char **argv)
{
//
// Parse the command line options, and create the specified kind of test.
//
parseCommandLine(argc, argv);
//
// Fire off the requested number of parallel threads
//
if (gRunInfo.numThreads == 0)
exit(0);
gRunInfo.exitFlag = FALSE;
gRunInfo.stopFlag = TRUE; // Will cause the new threads to block
umtx_lock(&gStopMutex);
gThreadInfo = new ThreadInfo[gRunInfo.numThreads];
int threadNum;
for (threadNum=0; threadNum < gRunInfo.numThreads; threadNum++)
{
gThreadInfo[threadNum].fThreadNum = threadNum;
ThreadFuncs::startThread(threadMain, &gThreadInfo[threadNum]);
}
unsigned long startTime = ThreadFuncs::getCurrentMillis();
int elapsedSeconds = 0;
int timeSinceCheck = 0;
//
// Unblock the threads.
//
gRunInfo.stopFlag = FALSE; // Unblocks the worker threads.
umtx_unlock(&gStopMutex);
//
// Loop, watching the heartbeat of the worker threads.
// Each second,
// display "+" if all threads have completed at least one loop
// display "." if some thread hasn't since previous "+"
// Each "ctime" seconds,
// Stop all the worker threads at the top of their loop, then
// call the test's check function.
//
while (gRunInfo.totalTime == 0 || gRunInfo.totalTime > elapsedSeconds)
{
ThreadFuncs::Sleep(1000); // We sleep while threads do their work ...
if (gRunInfo.quiet == false && gRunInfo.verbose == false)
{
char c = '+';
int threadNum;
umtx_lock(&gInfoMutex);
for (threadNum=0; threadNum < gRunInfo.numThreads; threadNum++)
{
if (gThreadInfo[threadNum].fHeartBeat == false)
{
c = '.';
break;
};
}
umtx_unlock(&gInfoMutex);
fputc(c, stdout);
fflush(stdout);
if (c == '+')
for (threadNum=0; threadNum < gRunInfo.numThreads; threadNum++)
gThreadInfo[threadNum].fHeartBeat = false;
}
//
// Update running times.
//
timeSinceCheck -= elapsedSeconds;
elapsedSeconds = (ThreadFuncs::getCurrentMillis() - startTime) / 1000;
timeSinceCheck += elapsedSeconds;
//
// Call back to the test to let it check its internal validity
//
if (timeSinceCheck >= gRunInfo.checkTime) {
if (gRunInfo.verbose) {
fprintf(stderr, "Main: suspending all threads\n");
}
umtx_lock(&gStopMutex); // Block the worker threads at the top of their loop
gRunInfo.stopFlag = TRUE;
for (;;) {
umtx_lock(&gInfoMutex);
UBool done = gRunInfo.runningThreads == 0;
umtx_unlock(&gInfoMutex);
if (done) { break;}
ThreadFuncs::yield();
}
gRunInfo.fTest->check();
if (gRunInfo.quiet == false && gRunInfo.verbose == false) {
fputc('C', stdout);
}
if (gRunInfo.verbose) {
fprintf(stderr, "Main: starting all threads.\n");
}
gRunInfo.stopFlag = FALSE; // Unblock the worker threads.
umtx_unlock(&gStopMutex);
timeSinceCheck = 0;
}
};
//
// Time's up, we are done. (We only get here if this was a timed run)
// Tell the threads to exit.
//
gRunInfo.exitFlag = true;
for (;;) {
umtx_lock(&gInfoMutex);
UBool done = gRunInfo.runningThreads == 0;
umtx_unlock(&gInfoMutex);
if (done) { break;}
ThreadFuncs::yield();
}
//
// Tally up the total number of cycles completed by each of the threads.
//
double totalCyclesCompleted = 0;
for (threadNum=0; threadNum < gRunInfo.numThreads; threadNum++) {
totalCyclesCompleted += gThreadInfo[threadNum].fCycles;
}
double cyclesPerMinute = totalCyclesCompleted / (double(gRunInfo.totalTime) / double(60));
printf("\n%8.1f cycles per minute.", cyclesPerMinute);
//
// Memory should be clean coming out
//
delete gRunInfo.fTest;
delete [] gThreadInfo;
umtx_destroy(&gInfoMutex);
umtx_destroy(&gStopMutex);
u_cleanup();
return 0;
}
|