00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "JThread.h"
00026
00027 #ifdef WIN32
00028 #include "win32/JThread_Win32.cpp"
00029 #else
00030 #include "linux/JThread_Linux.cpp"
00031 #endif
00032
00033 #include "JSocket.h"
00034 #include "Message.h"
00035
00036 namespace cmlabs {
00037
00038 #ifdef WIN32
00039
00040 ObjectDictionary allThreadsStats;
00041
00042
00043
00044
00045
00046 bool JThread::addToAllThreadsStats(HANDLE aThread, ThreadStat* stat) {
00047 return allThreadsStats.put(JString(aThread), stat);
00048 }
00049
00050 bool JThread::addToAllThreadsStats(HANDLE aThread) {
00051 return allThreadsStats.put(JString(aThread), NULL);
00052 }
00053
00054 bool JThread::removeFromAllThreadsStats(HANDLE aThread) {
00055 bool res = allThreadsStats.remove(JString(aThread));
00056 return res;
00057 }
00058
00059 ThreadStat JThread::getAllThreadsStats() {
00060 ThreadStat totalStat;
00061 HANDLE aThread;
00062 ThreadStat stat;
00063 for (int n=0; n<allThreadsStats.getCount(); n++) {
00064 aThread = (HANDLE)(allThreadsStats.getKey(n).toPointer());
00065 if (aThread != NULL) {
00066 stat = getCPUUsage(aThread);
00067 totalStat.userLast += stat.userLast;
00068 totalStat.kernelLast += stat.kernelLast;
00069 }
00070 }
00071 return totalStat;
00072 }
00073 #else // WIN32
00074 ObjectDictionary* allThreadsStats = NULL;
00075
00076
00077
00078
00079
00080 bool JThread::addToAllThreadsStats(HANDLE aThread, ThreadStat* stat) {
00081 if (allThreadsStats == NULL)
00082 allThreadsStats = new ObjectDictionary();
00083 return allThreadsStats->put(JString((long)aThread), stat);
00084 }
00085
00086 bool JThread::addToAllThreadsStats(HANDLE aThread) {
00087 if (allThreadsStats == NULL)
00088 allThreadsStats = new ObjectDictionary();
00089 return allThreadsStats->put(JString((long)aThread), NULL);
00090 }
00091
00092 bool JThread::removeFromAllThreadsStats(HANDLE aThread) {
00093 if (allThreadsStats == NULL)
00094 return true;
00095 bool res = allThreadsStats->remove(JString((long)aThread));
00096 if (allThreadsStats == NULL)
00097 return true;
00098 if (allThreadsStats->getCount() == 0) {
00099 delete(allThreadsStats);
00100 allThreadsStats = NULL;
00101 }
00102 return res;
00103 }
00104
00105 ThreadStat JThread::getAllThreadsStats() {
00106
00107 ThreadStat totalStat;
00108
00109 if (allThreadsStats == NULL)
00110 return totalStat;
00111
00112 HANDLE aThread;
00113 ThreadStat stat;
00114 for (int n=0; n<allThreadsStats->getCount(); n++) {
00115 aThread = (HANDLE)(allThreadsStats->getKey(n).toLong());
00116 if (aThread != NULL) {
00117 stat = getCPUUsage(aThread);
00118 totalStat.userLast += stat.userLast;
00119 totalStat.kernelLast += stat.kernelLast;
00120 }
00121 }
00122 return totalStat;
00123 }
00124
00125 #endif // WIN32
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136 SysInfo::SysInfo() {
00137 clear();
00138 }
00139
00140 SysInfo::~SysInfo() {
00141 }
00142
00143 bool SysInfo::clear() {
00144 osName = "";
00145 osVersionString = "";
00146 cpuArchitecture = "";
00147 hostname = "";
00148 osType = "";
00149
00150 osTotalMemory = 0;
00151 cpuSpeed = 0;
00152 cpuCount = 0;
00153
00154 procAverageCPUUsageKernel = 0;
00155 procAverageCPUUsageUser = 0;
00156 procAverageCPUUsage = 0;
00157 procCurrentCPUUsageKernel = 0;
00158 procCurrentCPUUsageUser = 0;
00159 procCurrentCPUUsage = 0;
00160 return true;
00161 }
00162
00163 bool SysInfo::update() {
00164
00165
00166
00167
00168 JThread* thread = new JThread();
00169
00170
00171 ThreadStat av = thread->procAverageCPUUsage();
00172 ThreadStat cu = thread->procCurrentCPUUsage();
00173
00174
00175 osName = thread->osGetName();
00176
00177 osType = thread->osGetType();
00178
00179 osVersionString = thread->osGetVersion();
00180
00181
00182 osTotalMemory = thread->osGetTotalMemory();
00183
00184 cpuSpeed = thread->osGetCPUSpeed();
00185
00186 cpuCount = thread->osGetCPUCount();
00187
00188 cpuArchitecture = thread->osGetMachineArch();
00189
00190 hostname = thread->osGetMachineName();
00191
00192
00193
00194
00195 procAverageCPUUsageKernel = av.percentKernelCPU;
00196 procAverageCPUUsageUser = av.percentUserCPU;
00197 procAverageCPUUsage = av.percentCPU;
00198 procCurrentCPUUsageKernel = cu.percentKernelCPU;
00199 procCurrentCPUUsageUser = cu.percentUserCPU;
00200 procCurrentCPUUsage = cu.percentCPU;
00201
00202
00203
00204
00205 delete(thread);
00206 return true;
00207 }
00208
00209 int SysInfo::getOSVersionMajor() {
00210 Collection col = osVersionString.split(".");
00211 if (col.getCount() > 0)
00212 return col.get(0).toInt();
00213 else
00214 return 0;
00215 }
00216
00217 int SysInfo::getOSVersionMinor() {
00218 Collection col = osVersionString.split(".");
00219 if (col.getCount() > 1)
00220 return col.get(1).toInt();
00221 else
00222 return 0;
00223 }
00224
00225 int SysInfo::getOSVersionSub() {
00226 Collection col = osVersionString.split(".");
00227 if (col.getCount() > 2)
00228 return col.get(2).toInt();
00229 else
00230 return 0;
00231 }
00232
00233 int SysInfo::getOSVersionBuild() {
00234 Collection col = osVersionString.split(".");
00235 if (col.getCount() > 3)
00236 return col.get(3).toInt();
00237 else
00238 return 0;
00239 }
00240
00241 JString SysInfo::getOSVersionText() {
00242 Collection col = osVersionString.splitOnWhiteSpaces();
00243 if (col.getCount() > 1)
00244 return col.getLast();
00245 else
00246 return "";
00247 }
00248
00249 Object* SysInfo::clone() const {
00250 SysInfo* sysInfo = new SysInfo();
00251 *sysInfo = *this;
00252 return sysInfo;
00253 }
00254
00255 bool SysInfo::equals(const Object* o2) const {
00256 if ((o2 == NULL) || (!isSameClass(o2)))
00257 return 0;
00258
00259
00260
00261 SysInfo* si = (SysInfo*) o2;
00262
00263 if ( (osName.length() != 0) && (si->osName.length() != 0) && (!osName.matches(si->osName)) )
00264 return false;
00265 if ( (osType.length() != 0) && (si->osType.length() != 0) && (!osType.matches(si->osType)) )
00266 return false;
00267 if ( (osVersionString.length() != 0) && (si->osVersionString.length() != 0) && (!osVersionString.matches(si->osVersionString)) )
00268 return false;
00269 if ( (cpuArchitecture.length() != 0) && (si->cpuArchitecture.length() != 0) && (!cpuArchitecture.matches(si->cpuArchitecture)) )
00270 return false;
00271 if ( (hostname.length() != 0) && (si->hostname.length() != 0) && (!hostname.matches(si->hostname)) )
00272 return false;
00273 if ( (osTotalMemory != 0) && (si->osTotalMemory != 0) && (osTotalMemory <= si->osTotalMemory) )
00274 return false;
00275 if ( (cpuSpeed != 0) && (si->cpuSpeed != 0) && (cpuSpeed <= si->cpuSpeed) )
00276 return false;
00277 if ( (cpuCount != 0) && (si->cpuCount != 0) && (cpuCount <= si->cpuCount) )
00278 return false;
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294 return true;
00295 }
00296
00297 JString SysInfo::print() {
00298 return JString::format("\
00299 System Info\n osname=\"%s\"\n ostype=\"%s\"\n osversionstring=\"%s\"\n arch=\"%s\"\n\
00300 hostname=\"%s\"\n osmem=\"%f\"\n cpuspeed=\"%f\"\n\
00301 cpucount=\"%d\"\n procavgkernel=\"%f\"\n procavguser=\"%f\"\n\
00302 procavg=\"%f\"\n proccurkernel=\"%f\"\n proccuruser=\"%f\"\n proccur=\"%f\"\n",
00303 (char*) osName,
00304 (char*) osType,
00305 (char*) osVersionString,
00306 (char*) cpuArchitecture,
00307 (char*) hostname,
00308 osTotalMemory,
00309 cpuSpeed,
00310 cpuCount,
00311
00312 procAverageCPUUsageKernel,
00313 procAverageCPUUsageUser,
00314 procAverageCPUUsage,
00315 procCurrentCPUUsageKernel,
00316 procCurrentCPUUsageUser,
00317 procCurrentCPUUsage);
00318 }
00319
00320 JString SysInfo::toHTML() {
00321 return JString::format("\
00322 <table><tr><td colspan=\"2\">System Info</td></tr><tr><td>osname</td><td>%s</td></tr><tr><td>ostype</td><td>%s</td></tr><tr><td>osversionstring</td><td>%s</td></tr><tr><td>arch</td><td>%s</td></tr><tr><td>\
00323 hostname</td><td>%s</td></tr><tr><td>osmem</td><td>%f</td></tr><tr><td>cpuspeed</td><td>%f</td></tr><tr><td>\
00324 cpucount</td><td>%d</td></tr><tr><td>procavgkernel</td><td>%f</td></tr><tr><td>procavguser</td><td>%f</td></tr><tr><td>\
00325 procavg</td><td>%f</td></tr><tr><td>proccurkernel</td><td>%f</td></tr><tr><td>proccuruser</td><td>%f</td></tr><tr><td>proccur</td><td>%f</td></tr></table>",
00326 (char*) osName,
00327 (char*) osType,
00328 (char*) osVersionString,
00329 (char*) cpuArchitecture,
00330 (char*) hostname,
00331 osTotalMemory,
00332 cpuSpeed,
00333 cpuCount,
00334
00335 procAverageCPUUsageKernel,
00336 procAverageCPUUsageUser,
00337 procAverageCPUUsage,
00338 procCurrentCPUUsageKernel,
00339 procCurrentCPUUsageUser,
00340 procCurrentCPUUsage);
00341 }
00342
00343 JString SysInfo::toXML(JString tag, JString content) {
00344
00345 JString ending;
00346 if (content.length() == 0)
00347 ending = JString::format(" />");
00348 else
00349 ending = JString::format(">%s</%s>", (char*) content.xmlStringEncode(), (char*) tag);
00350
00351 return JString::format("\
00352 <%s osname=\"%s\" ostype=\"%s\" osversionstring=\"%s\" arch=\"%s\"\
00353 hostname=\"%s\" osmem=\"%f\" cpuspeed=\"%f\"\
00354 cpucount=\"%d\" procavgkernel=\"%f\" procavguser=\"%f\"\
00355 procavg=\"%f\" proccurkernel=\"%f\" proccuruser=\"%f\" proccur=\"%f\"%s",
00356 (char*) tag,
00357 (char*) osName,
00358 (char*) osType,
00359 (char*) osVersionString,
00360 (char*) cpuArchitecture,
00361 (char*) hostname,
00362 osTotalMemory,
00363 cpuSpeed,
00364 cpuCount,
00365
00366 procAverageCPUUsageKernel,
00367 procAverageCPUUsageUser,
00368 procAverageCPUUsage,
00369 procCurrentCPUUsageKernel,
00370 procCurrentCPUUsageUser,
00371 procCurrentCPUUsage,
00372 (char*) ending);
00373 }
00374
00375 JString SysInfo::toXML() {
00376
00377 return toXML("sysinfo", "");
00378 }
00379
00380 bool SysInfo::fromXML(XMLNode* node) {
00381
00382 if (node == NULL)
00383 return false;
00384
00385 osName = node->getAttribute("osname");
00386 osType = node->getAttribute("ostype");
00387 osVersionString = node->getAttribute("osversionstring");
00388 cpuArchitecture = node->getAttribute("arch");
00389 hostname = node->getAttribute("hostname");
00390 if (node->hasAttribute("osmem"))
00391 osTotalMemory = node->getAttribute("osmem").toDouble();
00392 if (node->hasAttribute("cpuspeed"))
00393 cpuSpeed = node->getAttribute("cpuspeed").toDouble();
00394 if (node->hasAttribute("cpucount"))
00395 cpuCount = node->getAttribute("cpucount").toInt();
00396
00397 if (node->hasAttribute("procavgkernel"))
00398 procAverageCPUUsageKernel = node->getAttribute("procavgkernel").toDouble();
00399 if (node->hasAttribute("procavguser"))
00400 procAverageCPUUsageUser = node->getAttribute("procavguser").toDouble();
00401 if (node->hasAttribute("procavg"))
00402 procAverageCPUUsage = node->getAttribute("procavg").toDouble();
00403 if (node->hasAttribute("proccurkernel"))
00404 procCurrentCPUUsageKernel = node->getAttribute("proccurkernel").toDouble();
00405 if (node->hasAttribute("proccuruser"))
00406 procCurrentCPUUsageUser = node->getAttribute("proccuruser").toDouble();
00407 if (node->hasAttribute("proccur"))
00408 procCurrentCPUUsage = node->getAttribute("proccur").toDouble();
00409
00410 return true;
00411 }
00412
00413
00414
00415
00416
00417
00418
00419
00420 ThreadStat::ThreadStat() {
00421 timeLast.tv_sec = timeLast.tv_usec = 0;
00422 userLast = kernelLast = realTimeElapse = cpuUserUsage = cpuKernelUsage = cpuUsage = (longlong) 0;
00423 percentKernelCPU = percentUserCPU = percentCPU = 0;
00424 }
00425
00426 Object* ThreadStat::clone() const {
00427 ThreadStat* stat = new ThreadStat();
00428 *stat = *this;
00429 return stat;
00430 }
00431
00432 bool ThreadStat::isEmpty() {
00433 return ( (timeLast.tv_sec == 0) && (timeLast.tv_usec == 0) && (userLast == 0) && (kernelLast == 0) );
00434 }
00435
00436 JString ThreadStat::print() {
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448 JString str = JString::format("userLast: %.f\nkernelLast: %.f\nrealTimeElapse: %.f (%.2f sec)\ncpuUserUsage: %.f\ncpuKernelUsage: %.f\ncpuUsage: %.f\ncpuUserUsage: %.2f %% \ncpuKernelUsage: %.2f %% \ncpuUsage: %.2f %% \n",
00449 (double)userLast, (double)kernelLast, (double)realTimeElapse,
00450 ((double)realTimeElapse/(longlong)1000000),
00451 (double)cpuUserUsage, (double)cpuKernelUsage, (double)cpuUsage,
00452 percentUserCPU, percentKernelCPU, percentCPU);
00453 return str;
00454 }
00455
00456
00457
00458
00459
00460 PerfStat::PerfStat() {
00461 runCount = 0;
00462 msgMax = 0;
00463 msgStored = 0;
00464 msgReceived = 0;
00465 msgPosted = 0;
00466 msgSent = 0;
00467 msgWoken = 0;
00468 msgProcessed = 0;
00469 bytesReceived = 0;
00470 bytesSent = 0;
00471 currentCPUUsage = 0;
00472 totalRunSeconds = 0;
00473 totalCPUSeconds = 0;
00474 deltaSeconds = 0;
00475 deltaRunSeconds = 0;
00476 deltaCPUSeconds = 0;
00477 currentSystemUsage = 0;
00478 averageSystemUsage = 0;
00479 totalSystemUsage = 0;
00480 streamsCount = 0;
00481 streamsTotalSize = 0;
00482 streamTotalInRate = 0;
00483 streamTotalOutRate = 0;
00484 streamsTotalSizeEver = 0;
00485 lastMessageTrigger = NULL;
00486 lastMessagePosted = NULL;
00487
00488
00489 }
00490
00491 PerfStat::PerfStat(JString xml) {
00492 runCount = 0;
00493 msgMax = 0;
00494 msgStored = 0;
00495 msgReceived = 0;
00496 msgPosted = 0;
00497 msgSent = 0;
00498 msgWoken = 0;
00499 msgProcessed = 0;
00500 bytesReceived = 0;
00501 bytesSent = 0;
00502 currentCPUUsage = 0;
00503 totalRunSeconds = 0;
00504 totalCPUSeconds = 0;
00505 deltaSeconds = 0;
00506 deltaRunSeconds = 0;
00507 deltaCPUSeconds = 0;
00508 currentSystemUsage = 0;
00509 averageSystemUsage = 0;
00510 totalSystemUsage = 0;
00511 streamsCount = 0;
00512 streamsTotalSize = 0;
00513 streamTotalInRate = 0;
00514 streamTotalOutRate = 0;
00515 streamsTotalSizeEver = 0;
00516 lastMessageTrigger = NULL;
00517 lastMessagePosted = NULL;
00518 Object::fromXML(xml);
00519 }
00520
00521 PerfStat::PerfStat(XMLNode* node) {
00522 runCount = 0;
00523 msgMax = 0;
00524 msgStored = 0;
00525 msgReceived = 0;
00526 msgPosted = 0;
00527 msgSent = 0;
00528 msgWoken = 0;
00529 msgProcessed = 0;
00530 bytesReceived = 0;
00531 bytesSent = 0;
00532 currentCPUUsage = 0;
00533 totalRunSeconds = 0;
00534 totalCPUSeconds = 0;
00535 deltaSeconds = 0;
00536 deltaRunSeconds = 0;
00537 deltaCPUSeconds = 0;
00538 currentSystemUsage = 0;
00539 averageSystemUsage = 0;
00540 totalSystemUsage = 0;
00541 streamsCount = 0;
00542 streamsTotalSize = 0;
00543 streamTotalInRate = 0;
00544 streamTotalOutRate = 0;
00545 streamsTotalSizeEver = 0;
00546 lastMessagePosted = NULL;
00547 lastMessageTrigger = NULL;
00548 fromXML(node);
00549 }
00550
00551 PerfStat::PerfStat(const PerfStat &p) {
00552 runCount = p.runCount;
00553 msgMax = p.msgMax;
00554 msgStored = p.msgStored;
00555 msgReceived = p.msgReceived;
00556 msgPosted = p.msgPosted;
00557 msgSent = p.msgSent;
00558 msgWoken = p.msgWoken;
00559 msgProcessed = p.msgProcessed;
00560 bytesReceived = p.bytesReceived;
00561 bytesSent = p.bytesSent;
00562 currentCPUUsage = p.currentCPUUsage;
00563 totalRunSeconds = p.totalRunSeconds;
00564 totalCPUSeconds = p.totalCPUSeconds;
00565 deltaSeconds = p.deltaSeconds;
00566 deltaRunSeconds = p.deltaRunSeconds;
00567 deltaCPUSeconds = p.deltaCPUSeconds;
00568 currentSystemUsage = p.currentSystemUsage;
00569 averageSystemUsage = p.averageSystemUsage;
00570 totalSystemUsage = p.totalSystemUsage;
00571 streamsCount = p.streamsCount;
00572 streamsTotalSize = p.streamsTotalSize;
00573 streamTotalInRate = p.streamTotalInRate;
00574 streamTotalOutRate = p.streamTotalOutRate;
00575 streamsTotalSizeEver = p.streamsTotalSizeEver;
00576 lastMessageTrigger = NULL;
00577 lastMessagePosted = NULL;
00578 if (p.mutex.EnterMutex(500)) {
00579 this->setLastMessageTrigger(p.lastMessageTrigger);
00580 this->setLastMessagePost(p.lastMessagePosted);
00581 p.mutex.LeaveMutex();
00582 }
00583 }
00584
00585 PerfStat& PerfStat::operator=(const PerfStat &p) {
00586 name = p.name;
00587 runCount = p.runCount;
00588 msgMax = p.msgMax;
00589 msgStored = p.msgStored;
00590 msgReceived = p.msgReceived;
00591 msgPosted = p.msgPosted;
00592 msgSent = p.msgSent;
00593 msgWoken = p.msgWoken;
00594 msgProcessed = p.msgProcessed;
00595 bytesReceived = p.bytesReceived;
00596 bytesSent = p.bytesSent;
00597 currentCPUUsage = p.currentCPUUsage;
00598 totalRunSeconds = p.totalRunSeconds;
00599 totalCPUSeconds = p.totalCPUSeconds;
00600 deltaSeconds = p.deltaSeconds;
00601 deltaRunSeconds = p.deltaRunSeconds;
00602 deltaCPUSeconds = p.deltaCPUSeconds;
00603 currentSystemUsage = p.currentSystemUsage;
00604 averageSystemUsage = p.averageSystemUsage;
00605 totalSystemUsage = p.totalSystemUsage;
00606 streamsCount = p.streamsCount;
00607 streamsTotalSize = p.streamsTotalSize;
00608 streamTotalInRate = p.streamTotalInRate;
00609 streamTotalOutRate = p.streamTotalOutRate;
00610 streamsTotalSizeEver = p.streamsTotalSizeEver;
00611 if (p.mutex.EnterMutex(500)) {
00612 this->setLastMessageTrigger(p.lastMessageTrigger);
00613 this->setLastMessagePost(p.lastMessagePosted);
00614 p.mutex.LeaveMutex();
00615 }
00616 else {
00617 if (mutex.EnterMutex(500)) {
00618 if (lastMessageTrigger != NULL)
00619 delete(lastMessageTrigger);
00620 if (lastMessagePosted != NULL)
00621 delete(lastMessagePosted);
00622 mutex.LeaveMutex();
00623 }
00624 lastMessageTrigger = NULL;
00625 lastMessagePosted = NULL;
00626 }
00627 return *this;
00628 }
00629
00630 PerfStat::~PerfStat() {
00631 if (mutex.EnterMutex(500)) {
00632 if (lastMessageTrigger != NULL)
00633 delete(lastMessageTrigger);
00634 lastMessageTrigger = NULL;
00635 if (lastMessagePosted != NULL)
00636 delete(lastMessagePosted);
00637 lastMessagePosted = NULL;
00638 mutex.LeaveMutex();
00639 }
00640 else {
00641 printf("******* PerfStat ignoring Mutex *******");
00642
00643 if (lastMessageTrigger != NULL)
00644 delete(lastMessageTrigger);
00645 lastMessageTrigger = NULL;
00646 if (lastMessagePosted != NULL)
00647 delete(lastMessagePosted);
00648 lastMessagePosted = NULL;
00649 }
00650 }
00651
00652
00653 Object* PerfStat::clone() const {
00654 PerfStat* stat = new PerfStat();
00655 *stat = *this;
00656 return stat;
00657 }
00658
00659 bool PerfStat::setLastMessageTrigger(const Message* msg) {
00660 if (mutex.EnterMutex(500)) {
00661 delete(lastMessageTrigger);
00662 lastMessageTrigger = NULL;
00663 if (msg != NULL) {
00664 lastMessageTrigger = msg->shallowClone();
00665
00666
00667 }
00668 mutex.LeaveMutex();
00669 return true;
00670 }
00671 else
00672 return false;
00673 }
00674
00675 bool PerfStat::setLastMessagePost(const Message* msg) {
00676 if (mutex.EnterMutex(500)) {
00677 delete(lastMessagePosted);
00678 lastMessagePosted = NULL;
00679 if (msg != NULL) {
00680 lastMessagePosted = msg->shallowClone();
00681
00682
00683 }
00684 mutex.LeaveMutex();
00685 return true;
00686 }
00687 else
00688 return false;
00689 }
00690
00691 bool PerfStat::fromXML(XMLNode* node) {
00692 if (node == NULL)
00693 return false;
00694
00695 if (!node->getTag().equalsIgnoreCase("perfstat"))
00696 return false;
00697
00698 ObjectCollection* col = node->getChildTags();
00699 if ( (col == NULL) || (col->getCount() < 2))
00700 return false;
00701
00702 name = node->getAttribute("name");
00703 runCount = node->getAttribute("count").toInt();
00704 msgReceived = node->getAttribute("received").toInt();
00705 msgWoken = node->getAttribute("woken").toInt();
00706 msgPosted = node->getAttribute("posted").toInt();
00707 msgSent = node->getAttribute("sent").toInt();
00708 msgProcessed = node->getAttribute("processed").toInt();
00709 msgMax = node->getAttribute("max").toLong();
00710 msgStored = node->getAttribute("stored").toLong();
00711 bytesReceived = node->getAttribute("bytesreceived").toLong();
00712 bytesSent = node->getAttribute("bytessent").toLong();
00713 currentCPUUsage = node->getAttribute("currentcpu").toDouble();
00714 totalRunSeconds = node->getAttribute("totalrun").toDouble();
00715 totalCPUSeconds = node->getAttribute("totalcpu").toDouble();
00716 deltaSeconds = node->getAttribute("delta").toDouble();
00717 deltaRunSeconds = node->getAttribute("deltarun").toDouble();
00718 deltaCPUSeconds = node->getAttribute("deltacpu").toDouble();
00719 currentSystemUsage = node->getAttribute("currentsystem").toDouble();
00720 averageSystemUsage = node->getAttribute("averagesystem").toDouble();
00721 totalSystemUsage = node->getAttribute("totalsystem").toDouble();
00722 streamsCount = node->getAttribute("streamscount").toLong();
00723 streamsTotalSize = node->getAttribute("streamssize").toLong();
00724 streamTotalInRate = node->getAttribute("streamsratein").toDouble();
00725 streamTotalOutRate = node->getAttribute("streamsrateout").toDouble();
00726 streamsTotalSizeEver = node->getAttribute("streamsever").toDouble();
00727
00728 created = JTime((XMLNode*)col->get(0));
00729 lastUpdated = JTime((XMLNode*)col->get(1));
00730
00731 if (col->getCount() > 2) {
00732 if (mutex.EnterMutex(500)) {
00733 delete(lastMessageTrigger);
00734
00735 lastMessageTrigger = new Message((XMLNode*)col->get(2));
00736 mutex.LeaveMutex();
00737 }
00738 }
00739 if (col->getCount() > 3) {
00740 if (mutex.EnterMutex(500)) {
00741 delete(lastMessagePosted);
00742
00743 lastMessagePosted = new Message((XMLNode*)col->get(3));
00744 mutex.LeaveMutex();
00745 }
00746 }
00747
00748 return true;
00749 }
00750
00751 JString PerfStat::toXML() {
00752 JString xml;
00753 if (lastMessageTrigger != NULL) {
00754 if (mutex.EnterMutex(500)) {
00755 xml = lastMessageTrigger->toXML().indentXML();
00756 mutex.LeaveMutex();
00757 }
00758 }
00759 if (lastMessagePosted != NULL) {
00760 if (xml.length() == 0) {
00761 Message* msg = new Message();
00762 xml = msg->toXML().indentXML();
00763 delete(msg);
00764 }
00765 if (mutex.EnterMutex(500)) {
00766 xml += lastMessagePosted->toXML().indentXML();
00767 mutex.LeaveMutex();
00768 }
00769 }
00770 return JString::format("<perfstat name=\"%s\" count=\"%d\" received=\"%d\" woken=\"%d\" posted=\"%d\" sent=\"%d\" processed=\"%d\" \
00771 max=\"%d\" stored=\"%d\" bytesreceived=\"%d\" bytessent=\"%d\" streamscount=\"%d\" streamssize=\"%d\" streamsratein=\"%.10f\" streamsrateout=\"%.10f\" streamsever=\"%.10f\" \
00772 currentcpu=\"%.10f\" totalrun=\"%.10f\" totalcpu=\"%.10f\" delta=\"%.10f\" deltarun=\"%.10f\" deltacpu=\"%.10f\" \
00773 currentsystem=\"%.10f\" averagesystem=\"%.10f\" totalsystem=\"%.10f\">\n%s%s%s</perfstat>",
00774 (char*) name.xmlStringEncode(), runCount, msgReceived, msgWoken, msgPosted, msgSent, msgProcessed,
00775 msgMax, msgStored, bytesReceived, bytesSent,
00776 streamsCount, streamsTotalSize, streamTotalInRate, streamTotalOutRate, streamsTotalSizeEver,
00777 currentCPUUsage, totalRunSeconds, totalCPUSeconds, deltaSeconds, deltaRunSeconds, deltaCPUSeconds,
00778 currentSystemUsage, averageSystemUsage, totalSystemUsage, (char*) created.toXML().indentXML(), (char*) lastUpdated.toXML().indentXML(), (char*) xml);
00779 }
00780
00781 char* PerfStat::toBinary(int* len) {
00782 JString xml = toXML();
00783 int l;
00784 char* buffer = xml.getCharCopy(l);
00785 *len = l+1;
00786 return buffer;
00787 }
00788
00789 bool PerfStat::fromBinary(char* data, int len) {
00790 JString xml = data;
00791 return Object::fromXML(xml);
00792 }
00793
00794
00795 JString PerfStat::print() {
00796 JString msgs;
00797 if (mutex.EnterMutex(500)) {
00798 if (lastMessageTrigger != NULL)
00799 msgs = JString::format("Last Trigger: %s\n", (char*) lastMessageTrigger->printShort());
00800 if (lastMessagePosted != NULL)
00801 msgs += JString::format("Last Posted: %s\n", (char*) lastMessagePosted->printShort());
00802 mutex.LeaveMutex();
00803 }
00804 JTime now;
00805 return JString::format("perfstat[%s] count: %d woken: %d posted: %d sent: %d bytesrec: %d bytessent: %d proc: %d currentcpu: %.2f current: %.4f average: %.4f total: %.4f\n%s",
00806 (char*) name, runCount, msgWoken, msgPosted, msgSent, bytesReceived, bytesSent, msgProcessed, currentCPUUsage, currentSystemUsage, averageSystemUsage, totalSystemUsage, (char*) msgs);
00807 }
00808
00809 bool PerfStat::isOlderThan(long ms) {
00810 JTime now;
00811 return (now - lastUpdated > ms);
00812 }
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824 ThreadStat JThread::getLocalThreadStatistics() {
00825 return getCPUUsageOnThread(hThread);
00826 }
00827
00828 bool JThread::resetLocalThreadStatistics() {
00829 return resetCPUUsageOnThread(hThread);
00830 }
00831
00832 JString JThread::printLocalThreadStatistics() {
00833 return printThreadStatistics(getLocalThreadStatistics());
00834 }
00835
00836
00837
00838
00839
00840
00841 ThreadStat JThread::getCallingThreadStatistics() {
00842 #ifdef WIN32
00843 return getCPUUsageOnThread(getCallingThreadID());
00844 #else // WIN32
00845 pthread_t* p = getCallingThreadID();
00846 ThreadStat stat = getCPUUsageOnThread(p);
00847 delete(p);
00848 return stat;
00849 #endif // WIN32
00850 }
00851
00852 bool JThread::resetCallingThreadStatistics() {
00853 #ifdef WIN32
00854 return resetCPUUsageOnThread(getCallingThreadID());
00855 #else // WIN32
00856 pthread_t* p = getCallingThreadID();
00857 bool res = resetCPUUsageOnThread(p);
00858 delete(p);
00859 return res;
00860 #endif // WIN32
00861 }
00862
00863 JString JThread::printCallingThreadStatistics() {
00864 return printThreadStatistics(getCallingThreadStatistics());
00865 }
00866
00867
00868
00869
00870
00871
00872 ThreadStat JThread::getCurrentCPUUsage() {
00873
00874
00875 ThreadStat newStat = getCPUUsage(NULL);
00876
00877 newStat.realTimeElapse = (newStat.timeLast.tv_sec - procLastStat.timeLast.tv_sec) * 1000000
00878 + (newStat.timeLast.tv_usec - procLastStat.timeLast.tv_usec);
00879 newStat.cpuUserUsage = newStat.userLast - procLastStat.userLast;
00880 newStat.cpuKernelUsage = newStat.kernelLast - procLastStat.kernelLast;
00881 newStat.cpuUsage = newStat.cpuUserUsage + newStat.cpuKernelUsage;
00882
00883 if (newStat.cpuUsage > newStat.realTimeElapse) {
00884 double factor = (double) newStat.realTimeElapse / (double) newStat.cpuUsage;
00885 newStat.cpuUserUsage = (longlong)(factor * newStat.cpuUserUsage);
00886 newStat.cpuKernelUsage = (longlong)(factor * newStat.cpuKernelUsage);
00887 newStat.cpuUsage = (longlong)(factor * newStat.cpuUsage);
00888 }
00889
00890 newStat.percentUserCPU = (newStat.cpuUserUsage / (double) newStat.realTimeElapse) * 100.0;
00891 newStat.percentKernelCPU = (newStat.cpuKernelUsage / (double) newStat.realTimeElapse) * 100.0;
00892 newStat.percentCPU = (newStat.cpuUsage / (double) newStat.realTimeElapse) * 100.0;
00893
00894
00895
00896 procLastStat = getCPUUsage(NULL);
00897
00898 return newStat;
00899 }
00900
00901 ThreadStat JThread::getAverageCPUUsage() {
00902
00903
00904 ThreadStat newStat = getCPUUsage(NULL);
00905
00906 newStat.realTimeElapse = (newStat.timeLast.tv_sec - procStartStat.timeLast.tv_sec) * 1000000
00907 + (newStat.timeLast.tv_usec - procStartStat.timeLast.tv_usec);
00908 newStat.cpuUserUsage = newStat.userLast - procStartStat.userLast;
00909 newStat.cpuKernelUsage = newStat.kernelLast - procStartStat.kernelLast;
00910 newStat.cpuUsage = newStat.cpuUserUsage + newStat.cpuKernelUsage;
00911
00912 if (newStat.cpuUsage > newStat.realTimeElapse) {
00913 double factor = (double) newStat.realTimeElapse / (double) newStat.cpuUsage;
00914 newStat.cpuUserUsage = (longlong)(factor * newStat.cpuUserUsage);
00915 newStat.cpuKernelUsage = (longlong)(factor * newStat.cpuKernelUsage);
00916 newStat.cpuUsage = (longlong)(factor * newStat.cpuUsage);
00917 }
00918
00919 newStat.percentUserCPU = (newStat.cpuUserUsage / (double) newStat.realTimeElapse) * 100.0;
00920 newStat.percentKernelCPU = (newStat.cpuKernelUsage / (double) newStat.realTimeElapse) * 100.0;
00921 newStat.percentCPU = (newStat.cpuUsage / (double) newStat.realTimeElapse) * 100.0;
00922
00923
00924
00925 return newStat;
00926 }
00927
00928 ThreadStat JThread::getCPUUsageOnThread(HANDLE aThread) {
00929
00930 ThreadStat newStat;
00931
00932 if (aThread == NULL)
00933 return newStat;
00934
00935 ThreadStat oldStat = getStoredCPUUsageOnThread(aThread);
00936 if (oldStat.isEmpty())
00937 return ThreadStat();
00938 newStat = getCPUUsage(aThread);
00939 if (newStat.isEmpty())
00940 return ThreadStat();
00941
00942 newStat.realTimeElapse = (newStat.timeLast.tv_sec - oldStat.timeLast.tv_sec) * 1000000
00943 + (newStat.timeLast.tv_usec - oldStat.timeLast.tv_usec);
00944 newStat.cpuUserUsage = newStat.userLast - oldStat.userLast;
00945 newStat.cpuKernelUsage = newStat.kernelLast - oldStat.kernelLast;
00946 newStat.cpuUsage = newStat.cpuUserUsage + newStat.cpuKernelUsage;
00947
00948 double factor;
00949 if ((newStat.cpuUsage > newStat.realTimeElapse) && (newStat.cpuUsage > 0)) {
00950 factor = (double) newStat.realTimeElapse / (double) newStat.cpuUsage;
00951 newStat.cpuUserUsage = (longlong)(factor * newStat.cpuUserUsage);
00952 newStat.cpuKernelUsage = (longlong)(factor * newStat.cpuKernelUsage);
00953 newStat.cpuUsage = (longlong)(factor * newStat.cpuUsage);
00954 }
00955
00956 newStat.percentUserCPU = (newStat.cpuUserUsage / (double) newStat.realTimeElapse) * 100.0;
00957 newStat.percentKernelCPU = (newStat.cpuKernelUsage / (double) newStat.realTimeElapse) * 100.0;
00958 newStat.percentCPU = (newStat.cpuUsage / (double) newStat.realTimeElapse) * 100.0;
00959
00960 return newStat;
00961
00962 }
00963
00964 bool JThread::resetCPUUsageOnThread(HANDLE aThread) {
00965
00966 if (aThread == NULL)
00967 return false;
00968
00969 if (!resetMutex.EnterMutex(1000)) {
00970 printf("resetCPUUsageOnThread - Couldn't get Mutex\n");
00971 return false;
00972 }
00973
00974
00975 statDict.put(JString(aThread), getCPUUsage(aThread).clone());
00976
00977
00978 resetMutex.LeaveMutex();
00979
00980
00981 return true;
00982 }
00983
00984 ThreadStat JThread::getStoredCPUUsageOnThread(HANDLE aThread) {
00985
00986 ThreadStat ts;
00987 if (aThread == NULL)
00988 return ts;
00989
00990 if (!resetMutex.EnterMutex(1000)) {
00991 printf("getStoredCPUUsageOnThread - Couldn't get Mutex\n");
00992 return ts;
00993 }
00994
00995
00996 ThreadStat* pts = (ThreadStat*) statDict.get(JString(aThread));
00997 if (pts == NULL) {
00998
00999
01000 statDict.put(JString(aThread), getCPUUsage(aThread).clone());
01001 pts = (ThreadStat*) statDict.get(JString(aThread));
01002 if (pts == NULL) {
01003 if (DEBUGLEVEL(KITCHENSINK)) {
01004 printf("*** Error getStoredCPUUsageOnThread ***\n");
01005 }
01006 }
01007 else
01008 ts = *pts;
01009 }
01010 else
01011 ts = *pts;
01012
01013 resetMutex.LeaveMutex();
01014
01015 return ts;
01016 }
01017
01018 JString JThread::osGetMachineName() {
01019 if (localhostName.length() > 0)
01020 return localhostName;
01021
01022 #ifdef WIN32
01023
01024 char* hname = new char[1000];
01025 if (gethostname(hname, 1000) == 0) {
01026 localhostName = hname;
01027 delete [] hname;
01028 return localhostName;
01029 }
01030 else {
01031 int er = WSAGetLastError();
01032 if (er == WSANOTINITIALISED) {
01033 WSADATA info;
01034 if (WSAStartup(MAKEWORD(1,1), &info) == 0) {
01035
01036 if (gethostname(hname, 1000) == 0) {
01037 localhostName = hname;
01038 delete [] hname;
01039 return localhostName;
01040 }
01041 }
01042 }
01043 }
01044
01045 JSocket* socket = new JSocket("", 80);
01046 localhostName = socket->getLocalHostname();
01047 delete(socket);
01048
01049 #elif defined(Darwin)
01050 localhostName = runOSTextCommand("uname -n");
01051 #else
01052 localhostName = runOSTextCommand("uname -n");
01053 #endif // WIN32
01054
01055 return localhostName;
01056 }
01057
01058 JString JThread::osGetMachineAddress() {
01059 JSocket* socket = new JSocket("", 80);
01060 JString address = socket->getLocalIPAddress();
01061 delete(socket);
01062 return address;
01063 }
01064
01065 JString JThread::osGetType() {
01066 #ifdef WIN32
01067 #ifdef _WIN32_WCE
01068 return "PocketPC";
01069 #else
01070 #ifdef CYGWIN
01071 return "CygWin";
01072 #else
01073 return "Win32";
01074 #endif
01075 #endif
01076 #else
01077 #if defined(Darwin)
01078 return "OSX";
01079 #else
01080 return "Linux";
01081 #endif // Darwin
01082 #endif
01083 }
01084
01085
01086
01087
01088
01089
01090 JString JThread::printThreadStatistics(ThreadStat newStat) {
01091 char str[400];
01092
01093
01094
01095
01096
01097 if (newStat.realTimeElapse > 0) {
01098 newStat.percentKernelCPU = ( (double) newStat.cpuKernelUsage / (double) newStat.realTimeElapse);
01099 newStat.percentUserCPU = ( (double) newStat.cpuUserUsage / (double) newStat.realTimeElapse);
01100 newStat.percentCPU = ( (double) newStat.cpuUsage / (double) newStat.realTimeElapse);
01101
01102 sprintf(str, "Time elapse: %ld usec | User: %3.1f%% | Kernel: %3.1f%% | CPU: %3.1f%%",
01103 newStat.realTimeElapse, (double)newStat.percentUserCPU * 100.0, (double)newStat.percentKernelCPU * 100.0, (double)newStat.percentCPU * 100.0);
01104 }
01105 else {
01106 sprintf(str, "Time elapsed: 0 usec");
01107 }
01108
01109 return JString(str);
01110 }
01111
01112 JTime JThread::getStartTime() {
01113 return threadStartTime;
01114 }
01115 JTime JThread::getEndTime() {
01116 return threadEndTime;
01117 }
01118
01119 bool JThread::terminateWait(int timeout) {
01120 if (!terminate())
01121 return false;
01122
01123 JTime t;
01124 while (t.getAge() < timeout) {
01125 if (!isRunning())
01126 break;
01127 waitThread(10);
01128 }
01129 return isRunning();
01130 }
01131
01132
01133
01134
01135
01136
01137 bool JThread::unitTest() {
01138
01139 JTime t;
01140 JMutex mutex1;
01141 JMutex mutex2;
01142 JMutex mutex3;
01143
01144 t = JTime();
01145 if (!mutex1.EnterMutex(500)) {
01146 unitTestString += "No access by Mutex 1!\n";
01147 return false;
01148 }
01149 if (t.getAge() > 50) {
01150 unitTestString += JString::format("Mutex 1 waited too long: %.3f ms >> 0 ms!\n", t.getMicroAge()/1000.0);
01151 return false;
01152 }
01153
01154 t = JTime();
01155 if (!mutex2.EnterMutex(500)) {
01156 unitTestString += "No access by Mutex 2!\n";
01157 return false;
01158 }
01159 if (t.getAge() > 50) {
01160 unitTestString += JString::format("Mutex 2 waited too long: %.3f ms >> 0 ms!\n", t.getMicroAge()/1000.0);
01161 return false;
01162 }
01163
01164 t = JTime();
01165 if (!mutex3.EnterMutex(500)) {
01166 unitTestString += "No access by Mutex 3!\n";
01167 return false;
01168 }
01169 if (t.getAge() > 50) {
01170 unitTestString += JString::format("Mutex 3 waited too long: %.3f ms >> 0 ms!\n", t.getMicroAge()/1000.0);
01171 return false;
01172 }
01173
01174 unitTestString = "";
01175 JSemaphore* sem = new JSemaphore();
01176 JSemaphore* sem2 = new JSemaphore();
01177
01178 t = JTime();
01179 if (sem->wait(500)) {
01180 unitTestString += "First semaphore should not have triggered!\n";
01181 return false;
01182 }
01183 if (t.getAge() < 490) {
01184 unitTestString += JString::format("First semaphore triggered too fast: %.3f ms < 500 ms!\n", t.getMicroAge()/1000.0);
01185 return false;
01186 }
01187
01188 sem->post();
01189
01190 t = JTime();
01191 if (!sem->wait(500)) {
01192 unitTestString += "First semaphore should have triggered!\n";
01193 return false;
01194 }
01195 if (t.getAge() > 50) {
01196 unitTestString += JString::format("First semaphore triggered too slow: %.3f ms << 500 ms!\n", t.getMicroAge()/1000.0);
01197 return false;
01198 }
01199
01200 t = JTime();
01201 if (sem->wait(500)) {
01202 unitTestString += "First semaphore should not have triggered again!\n";
01203 return false;
01204 }
01205 if (t.getAge() < 490) {
01206 unitTestString += JString::format("First semaphore triggered again too fast: %.3f ms < 500 ms!\n", t.getMicroAge()/1000.0);
01207 return false;
01208 }
01209
01210 JThread* thr = new JThread(false);
01211 thr->publicSemaphore = sem;
01212 thr->testSemaphore = sem2;
01213 thr->testMutex = &mutex1;
01214
01215
01216 thr->start();
01217 thr->waitThread(100);
01218
01219 sem->post();
01220 sem2->post();
01221
01222 unitTestString += "Waiting for first semaphore...\n";
01223 thr->waitThread(100);
01224 if (!sem->wait(5000)) {
01225 unitTestString += "Waiting for first semaphore failed!\n";
01226 return false;
01227 }
01228
01229 if (!sem2->wait(5000)) {
01230 unitTestString += "Waiting for test semaphore failed!\n";
01231 return false;
01232 }
01233
01234 sem->post();
01235
01236 unitTestString += "Waiting for second semaphore...\n";
01237 thr->waitThread(100);
01238 if (!sem->wait(5000)) {
01239 unitTestString += "Waiting for second semaphore failed!\n";
01240 return false;
01241 }
01242
01243 sem->post();
01244
01245 unitTestString += "Waiting for third semaphore...\n";
01246 thr->waitThread(100);
01247 if (!sem->wait(5000)) {
01248 unitTestString += "Waiting for third semaphore failed!\n";
01249 return false;
01250 }
01251
01252 sem->post();
01253
01254 unitTestString += "Waiting for forth semaphore...\n";
01255 thr->waitThread(100);
01256 if (!sem->wait(5000)) {
01257 unitTestString += "Waiting for forth semaphore failed!\n";
01258
01259
01260
01261 }
01262
01263
01264 thr->waitThread(100);
01265
01266 unitTestString += "Exiting...\n";
01267
01268 mutex1.LeaveMutex();
01269 mutex2.LeaveMutex();
01270 mutex3.LeaveMutex();
01271
01272 delete(sem);
01273 delete(sem2);
01274 delete(thr);
01275
01276 return true;
01277 }
01278
01279
01280
01281
01282
01283 void JThread::run()
01284 {
01285 JTime t = JTime();
01286 if (this->testMutex->EnterMutex(500)) {
01287 printf("Access to Mutex 1 when no access should be given!\n");
01288 return;
01289 }
01290 if (t.getAge() < 490) {
01291 printf("Mutex 1 waited too short: %.3f ms < 500 ms!\n", t.getMicroAge()/1000.0);
01292 return;
01293 }
01294
01295
01296
01297 if (publicSemaphore == NULL) {
01298 if (DEBUGLEVEL(KITCHENSINK)) {
01299 printf("JThread: PublicSemaphore NULL!!!\n");
01300 }
01301 return;
01302 }
01303
01304 if (!publicSemaphore->wait(5000)) {
01305 if (DEBUGLEVEL(KITCHENSINK)) {
01306 printf("JThread: RUN 1 WAIT FAILED!!!\n");
01307 }
01308 return;
01309 }
01310
01311 if (!testSemaphore->wait(5000)) {
01312 if (DEBUGLEVEL(KITCHENSINK)) {
01313 printf("JThread: RUN 1 1/2 WAIT FAILED!!!\n");
01314 }
01315 return;
01316 }
01317 testSemaphore->post();
01318
01319
01320 publicSemaphore->post();
01321
01322 this->waitThread(100);
01323 if (!publicSemaphore->wait(5000)) {
01324 if (DEBUGLEVEL(KITCHENSINK)) {
01325 printf("JThread: RUN 2 WAIT FAILED!!!\n");
01326 }
01327 return;
01328 }
01329
01330
01331 publicSemaphore->post();
01332
01333 this->waitThread(100);
01334 if (!publicSemaphore->wait(5000)) {
01335 if (DEBUGLEVEL(KITCHENSINK)) {
01336 printf("JThread: RUN 3 WAIT FAILED!!!\n");
01337 }
01338 return;
01339 }
01340
01341
01342 publicSemaphore->post();
01343
01344 this->waitThread(100);
01345 if (!publicSemaphore->wait(5000)) {
01346 if (DEBUGLEVEL(KITCHENSINK)) {
01347 printf("JThread: RUN 4 WAIT FAILED!!!\n");
01348 }
01349 return;
01350 }
01351
01352 this->waitThread(100);
01353 publicSemaphore->post();
01354
01355 }
01356
01357
01358
01359
01360
01361
01362
01363
01364 ThreadStat JThread::procAverageCPUUsage() {
01365 ThreadStat stat = getAverageCPUUsage();
01366 return stat;
01367 }
01368
01369 ThreadStat JThread::procCurrentCPUUsage() {
01370 ThreadStat stat = getCurrentCPUUsage();
01371 return stat;
01372 }
01373
01374
01375
01376
01377
01378
01379
01380
01381
01382
01383 bool JThread::priorityTest() {
01384
01385 printf("Creating threads with priorities 5, 10, 15, ..., 95, 100\n");
01386
01387
01388 JThreadTester* test100 = new JThreadTester(100);
01389 JThreadTester* test95 = new JThreadTester(95);
01390 JThreadTester* test90 = new JThreadTester(90);
01391 JThreadTester* test85 = new JThreadTester(85);
01392 JThreadTester* test80 = new JThreadTester(80);
01393 JThreadTester* test75 = new JThreadTester(75);
01394 JThreadTester* test70 = new JThreadTester(70);
01395 JThreadTester* test65 = new JThreadTester(65);
01396 JThreadTester* test60 = new JThreadTester(60);
01397 JThreadTester* test55 = new JThreadTester(55);
01398 JThreadTester* test50 = new JThreadTester(50);
01399 JThreadTester* test45 = new JThreadTester(45);
01400 JThreadTester* test40 = new JThreadTester(40);
01401 JThreadTester* test35 = new JThreadTester(35);
01402 JThreadTester* test30 = new JThreadTester(30);
01403 JThreadTester* test25 = new JThreadTester(25);
01404 JThreadTester* test20 = new JThreadTester(20);
01405 JThreadTester* test15 = new JThreadTester(15);
01406 JThreadTester* test10 = new JThreadTester(10);
01407 JThreadTester* test05 = new JThreadTester(05);
01408
01409 printf("Starting all threads...\n");
01410
01411 bool t100 = true;
01412 bool t95 = true;
01413 bool t90 = true;
01414 bool t85 = true;
01415 bool t80 = true;
01416 bool t75 = true;
01417 bool t70 = true;
01418 bool t65 = true;
01419 bool t60 = true;
01420 bool t55 = true;
01421 bool t50 = true;
01422 bool t45 = true;
01423 bool t40 = true;
01424 bool t35 = true;
01425 bool t30 = true;
01426 bool t25 = true;
01427 bool t20 = true;
01428 bool t15 = true;
01429 bool t10 = true;
01430 bool t05 = true;
01431
01432 test100->start();
01433 test95->start();
01434 test90->start();
01435 test85->start();
01436 test80->start();
01437 test75->start();
01438 test70->start();
01439 test65->start();
01440 test60->start();
01441 test55->start();
01442 test50->start();
01443 test45->start();
01444 test40->start();
01445 test35->start();
01446 test30->start();
01447 test25->start();
01448 test20->start();
01449 test15->start();
01450 test10->start();
01451 test05->start();
01452
01453 if (!test100->setPriority(test100->ver)) {
01454 printf("Could not SetPriority(%d), not super user...\n", test100->ver);
01455 }
01456 if (!test95->setPriority(test95->ver)) {
01457 printf("Could not SetPriority(%d), not super user...\n", test95->ver);
01458 }
01459 if (!test90->setPriority(test90->ver)) {
01460 printf("Could not SetPriority(%d), not super user...\n", test90->ver);
01461 }
01462 if (!test85->setPriority(test85->ver)) {
01463 printf("Could not SetPriority(%d), not super user...\n", test85->ver);
01464 }
01465 if (!test80->setPriority(test80->ver)) {
01466 printf("Could not SetPriority(%d), not super user...\n", test80->ver);
01467 }
01468 if (!test75->setPriority(test75->ver)) {
01469 printf("Could not SetPriority(%d), not super user...\n", test75->ver);
01470 }
01471 if (!test70->setPriority(test70->ver)) {
01472 printf("Could not SetPriority(%d), not super user...\n", test70->ver);
01473 }
01474 if (!test65->setPriority(test65->ver)) {
01475 printf("Could not SetPriority(%d), not super user...\n", test65->ver);
01476 }
01477 if (!test60->setPriority(test60->ver)) {
01478 printf("Could not SetPriority(%d), not super user...\n", test60->ver);
01479 }
01480 if (!test55->setPriority(test55->ver)) {
01481 printf("Could not SetPriority(%d), not super user...\n", test55->ver);
01482 }
01483 if (!test50->setPriority(test50->ver)) {
01484 printf("Could not SetPriority(%d), not super user...\n", test50->ver);
01485 }
01486 if (!test45->setPriority(test45->ver)) {
01487 printf("Could not SetPriority(%d), not super user...\n", test45->ver);
01488 }
01489 if (!test40->setPriority(test40->ver)) {
01490 printf("Could not SetPriority(%d), not super user...\n", test40->ver);
01491 }
01492 if (!test35->setPriority(test35->ver)) {
01493 printf("Could not SetPriority(%d), not super user...\n", test35->ver);
01494 }
01495 if (!test30->setPriority(test30->ver)) {
01496 printf("Could not SetPriority(%d), not super user...\n", test30->ver);
01497 }
01498 if (!test25->setPriority(test25->ver)) {
01499 printf("Could not SetPriority(%d), not super user...\n", test25->ver);
01500 }
01501 if (!test20->setPriority(test20->ver)) {
01502 printf("Could not SetPriority(%d), not super user...\n", test20->ver);
01503 }
01504 if (!test15->setPriority(test15->ver)) {
01505 printf("Could not SetPriority(%d), not super user...\n", test15->ver);
01506 }
01507 if (!test10->setPriority(test10->ver)) {
01508 printf("Could not SetPriority(%d), not super user...\n", test10->ver);
01509 }
01510 if (!test05->setPriority(test05->ver)) {
01511 printf("Could not SetPriority(%d), not super user...\n", test05->ver);
01512 }
01513
01514 printf("Please wait while testing priorities...\n\n");
01515 ObjectDictionary times;
01516 while(t100 || t95 || t90 || t85 || t80 || t75 || t70 || t65 || t60 || t55 || t50 || t45 || t40 || t35 || t30 || t25 || t20 || t15 || t10 || t05) {
01517
01518 if (t100 && (!test100->isRunning())) {
01519 times.put("100", test100->getEndTime().clone());
01520 t100 = false;
01521 }
01522 if (t95 && (!test95->isRunning())) {
01523 times.put("95", test95->getEndTime().clone());
01524 t95 = false;
01525 }
01526 if (t90 && (!test90->isRunning())) {
01527 times.put("90", test90->getEndTime().clone());
01528 t90 = false;
01529 }
01530 if (t85 && (!test85->isRunning())) {
01531 times.put("85", test85->getEndTime().clone());
01532 t85 = false;
01533 }
01534 if (t80 && (!test80->isRunning())) {
01535 times.put("80", test80->getEndTime().clone());
01536 t80 = false;
01537 }
01538 if (t75 && (!test75->isRunning())) {
01539 times.put("75", test75->getEndTime().clone());
01540 t75 = false;
01541 }
01542 if (t70 && (!test70->isRunning())) {
01543 times.put("70", test70->getEndTime().clone());
01544 t70 = false;
01545 }
01546 if (t65 && (!test65->isRunning())) {
01547 times.put("65", test65->getEndTime().clone());
01548 t65 = false;
01549 }
01550 if (t60 && (!test60->isRunning())) {
01551 times.put("60", test60->getEndTime().clone());
01552 t60 = false;
01553 }
01554 if (t55 && (!test55->isRunning())) {
01555 times.put("55", test55->getEndTime().clone());
01556 t55 = false;
01557 }
01558 if (t50 && (!test50->isRunning())) {
01559 times.put("50", test50->getEndTime().clone());
01560 t50 = false;
01561 }
01562 if (t45 && (!test45->isRunning())) {
01563 times.put("45", test45->getEndTime().clone());
01564 t45 = false;
01565 }
01566 if (t40 && (!test40->isRunning())) {
01567 times.put("40", test40->getEndTime().clone());
01568 t40 = false;
01569 }
01570 if (t35 && (!test35->isRunning())) {
01571 times.put("35", test35->getEndTime().clone());
01572 t35 = false;
01573 }
01574 if (t30 && (!test30->isRunning())) {
01575 times.put("30", test30->getEndTime().clone());
01576 t30 = false;
01577 }
01578 if (t25 && (!test25->isRunning())) {
01579 times.put("25", test25->getEndTime().clone());
01580 t25 = false;
01581 }
01582 if (t20 && (!test20->isRunning())) {
01583 times.put("20", test20->getEndTime().clone());
01584 t20 = false;
01585 }
01586 if (t15 && (!test15->isRunning())) {
01587 times.put("15", test15->getEndTime().clone());
01588 t15 = false;
01589 }
01590 if (t10 && (!test10->isRunning())) {
01591 times.put("10", test10->getEndTime().clone());
01592 t10 = false;
01593 }
01594 if (t05 && (!test05->isRunning())) {
01595 times.put("05", test05->getEndTime().clone());
01596 t05 = false;
01597 }
01598 this->waitThread(100);
01599 }
01600
01601 if (times.getCount() < 10) {
01602 printf("Test failed - no results\n");
01603 return false;
01604 }
01605
01606 double dist = ((JTime*)times.getLast())->microDifference(*(JTime*)times.getFirst()) / 10.0;
01607 if (dist <= 1) {
01608 printf("Test failed - results too fast...\n");
01609 return false;
01610 }
01611
01612 double t;
01613
01614 JTime* t1, *t2;
01615 for (int n=1; n<times.getCount(); n++) {
01616 t2 = (JTime*)times.get(n);
01617 if (n > 0) {
01618 t1 = (JTime*)times.get(n-1);
01619 t = t2->microDifference(*t1);
01620 printf("%3s %s (%.3f) \n", (char*)times.getKey(n), (char*)t2->printTimeMS(), t/1000.0);
01621 }
01622 else
01623 printf("%3s %s\n", (char*)times.getKey(n), (char*)t2->printTimeMS());
01624
01625
01626
01627
01628 }
01629 printf("\n\nTypical test time: %.3f ms\n\n", dist/1000.0);
01630
01631 delete(test100);
01632 delete(test95);
01633 delete(test90);
01634 delete(test85);
01635 delete(test80);
01636 delete(test75);
01637 delete(test70);
01638 delete(test65);
01639 delete(test60);
01640 delete(test55);
01641 delete(test50);
01642 delete(test45);
01643 delete(test40);
01644 delete(test35);
01645 delete(test30);
01646 delete(test25);
01647 delete(test20);
01648 delete(test15);
01649 delete(test10);
01650 delete(test05);
01651
01652 printf("Test success\n");
01653 return true;
01654 }
01655
01656
01657
01658 JThreadTester::JThreadTester(int pri) : JThread(false) {
01659 ver = pri;
01660 }
01661
01662 JThreadTester::~JThreadTester() {
01663 }
01664
01665 void JThreadTester::run() {
01666
01667 int louter = 10;
01668 int linner = 100000;
01669 double test;
01670
01671 this->waitThread(1000);
01672
01673 for (int n=0; n<louter; n++) {
01674 for (int m=0; m<linner; m++) {
01675 test = pow((double)sqrt((double)10000) - 1, 2.55);
01676 }
01677
01678 fflush(stdout);
01679 this->waitThread(0);
01680 }
01681 JTime now;
01682 printf("Priority: %3d %s\n", ver, (char*)now.printTimeMS());
01683
01684 }
01685
01686
01687
01688
01689
01690
01691
01692
01693
01694
01695
01696
01697
01698 }