00001 /***************************** License ********************************** 00002 00003 Copyright (C) 2008 by Communicative Machines 00004 http://www.cmlabs.com All rights reserved. 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Lesser General Public 00008 License as published by the Free Software Foundation; either 00009 version 2.1 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Lesser General Public License for more details. 00015 00016 You should have received a copy of the GNU Lesser General Public 00017 License along with this library; if not, write to the Free Software 00018 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 00020 ************************************************************************/ 00021 00022 00023 // JThread.h: interface for the JThread class. 00024 // 00025 ////////////////////////////////////////////////////////////////////// 00026 00027 #if !defined(AFX_JTHREAD_H__7D7254A3_4145_4D83_94DE_1343EB810C61__INCLUDED_) 00028 #define AFX_JTHREAD_H__7D7254A3_4145_4D83_94DE_1343EB810C61__INCLUDED_ 00029 00030 #if _MSC_VER > 1000 00031 #pragma once 00032 #endif // _MSC_VER > 1000 00033 00034 #include "JSemaphore.h" 00035 #include "ObjectDictionary.h" 00036 #include "JFile.h" 00037 #include "Debug.h" 00038 #include "XMLParser.h" 00039 #include <math.h> 00040 00041 #ifdef WIN32 00042 #include <windows.h> 00043 #ifdef _WIN32_WCE 00044 #include <winsock2.h> 00045 #else 00046 #include <process.h> 00047 #endif 00048 #if defined(CYGWIN) 00049 #include <sys/time.h> 00050 #endif 00051 #if !defined(PROCESSOR_ARCHITECTURE_AMD64) 00052 #define PROCESSOR_ARCHITECTURE_AMD64 9 00053 #endif 00054 #if !defined(PROCESSOR_ARCHITECTURE_IA32_ON_WIN64) 00055 #define PROCESSOR_ARCHITECTURE_IA32_ON_WIN64 10 00056 #endif 00057 #include "psapi.h" 00058 #else 00059 #ifndef Darwin 00060 #include <execinfo.h> 00061 #endif // Darwin 00062 #include <errno.h> 00063 #include <pthread.h> 00064 #include <signal.h> 00065 #include <sys/times.h> 00066 #include <limits.h> 00067 #include <sys/resource.h> 00068 #include <unistd.h> 00069 #undef HANDLE 00070 #define HANDLE pthread_t* 00071 #endif 00072 00073 #include "Object.h" 00074 #include "JString.h" 00075 #include "JTime.h" 00076 // #include <thread.h> 00077 00078 00079 namespace cmlabs { 00080 00081 class SysInfo : public Object { 00082 public: 00083 SysInfo(); 00084 virtual ~SysInfo(); 00085 00086 virtual Object* clone() const; 00087 virtual bool equals(const Object* o2) const; 00088 virtual JString print(); 00089 virtual JString toHTML(); 00090 virtual JString toXML(); 00091 virtual JString toXML(JString tag, JString content); 00092 virtual bool fromXML(XMLNode* node); 00093 00094 virtual bool clear(); 00095 virtual bool update(); 00096 00097 int getOSVersionMajor(); 00098 int getOSVersionMinor(); 00099 int getOSVersionSub(); 00100 int getOSVersionBuild(); 00101 JString getOSVersionText(); 00102 00103 JString osName; 00104 JString osType; 00105 JString osVersionString; 00106 double osTotalMemory; 00107 double cpuSpeed; 00108 int cpuCount; 00109 JString cpuArchitecture; 00110 JString hostname; 00111 00112 // double procMemoryUsage; 00113 double procAverageCPUUsageKernel; 00114 double procAverageCPUUsageUser; 00115 double procAverageCPUUsage; 00116 double procCurrentCPUUsageKernel; 00117 double procCurrentCPUUsageUser; 00118 double procCurrentCPUUsage; 00119 }; 00120 00121 class ThreadStat : public Object { 00122 public: 00123 ThreadStat(); 00124 Object* clone() const; 00125 00126 struct timeval timeLast; 00127 longlong userLast; 00128 longlong kernelLast; 00129 00130 longlong realTimeElapse; 00131 longlong cpuUserUsage; 00132 longlong cpuKernelUsage; 00133 longlong cpuUsage; 00134 double percentKernelCPU; 00135 double percentUserCPU; 00136 double percentCPU; 00137 00138 bool isEmpty(); 00139 JString print(); 00140 }; 00141 00142 class JMutex; 00143 class Message; 00144 class PerfStat : public Object 00145 { 00146 public: 00147 PerfStat(); 00148 PerfStat(JString xml); 00149 PerfStat(XMLNode* node); 00150 PerfStat(const PerfStat &p); 00151 virtual ~PerfStat(); 00152 PerfStat& operator=(const PerfStat &p); 00153 Object* clone() const; 00154 bool fromXML(XMLNode* node); 00155 JString toXML(); 00156 JString print(); 00157 char* toBinary(int* len); 00158 bool fromBinary(char* data, int len); 00159 00160 bool isOlderThan(long ms); 00161 00162 JString name; 00163 JTime created; 00164 JTime lastUpdated; 00165 00166 long runCount; 00167 long msgWoken; 00168 long msgStored; 00169 long msgPosted; 00170 long msgReceived; 00171 long msgSent; 00172 long msgProcessed; 00173 long msgMax; 00174 00175 long bytesReceived; 00176 long bytesSent; 00177 00178 double totalRunSeconds; 00179 double totalCPUSeconds; 00180 double deltaSeconds; 00181 double deltaRunSeconds; 00182 double deltaCPUSeconds; 00183 double currentCPUUsage; 00184 double currentSystemUsage; 00185 double averageSystemUsage; 00186 double totalSystemUsage; 00187 00188 long streamsCount; 00189 long streamsTotalSize; 00190 double streamsTotalSizeEver; 00191 double streamTotalInRate; 00192 double streamTotalOutRate; 00193 00194 Message* lastMessageTrigger; 00195 Message* lastMessagePosted; 00196 00197 // Will clone object 00198 bool setLastMessageTrigger(const Message* msg); 00199 // Will clone object 00200 bool setLastMessagePost(const Message* msg); 00201 00202 protected: 00203 JMutex mutex; 00204 00205 00206 }; 00207 00208 00209 class JThread 00210 { 00211 public: 00212 JThread(bool start = false, int pri = 0); 00213 virtual ~JThread(); 00214 00215 virtual void run(); 00216 bool start(); 00217 00218 bool join(); 00219 bool terminate(); 00220 bool terminateWait(int timeout); 00221 bool suspend(); 00222 bool resume(); 00223 00224 bool waitThread(long ms); 00225 00226 bool isRunning(); 00227 bool isThread(); 00228 00229 JString printThreadStatistics(ThreadStat newStat); 00230 00231 //! get stat on local thread 00232 ThreadStat getLocalThreadStatistics(); 00233 //! reset stat on local thread 00234 bool resetLocalThreadStatistics(); 00235 //! print stat on local thread 00236 JString printLocalThreadStatistics(); 00237 00238 //! get stat on own (calling) thread 00239 ThreadStat getCallingThreadStatistics(); 00240 //! reset stat on own (calling) thread 00241 bool resetCallingThreadStatistics(); 00242 //! print stat on own (calling) thread 00243 JString printCallingThreadStatistics(); 00244 00245 ThreadStat getCurrentCPUUsage(); 00246 ThreadStat getAverageCPUUsage(); 00247 00248 ThreadStat getCPUUsage(HANDLE aThread); 00249 00250 int getPriority(); 00251 bool setPriority(int pri); 00252 00253 bool signalThread(int signo); 00254 bool waitSignal(int signo); 00255 void yield(); 00256 00257 // bool setTimer(unsigned long ms); 00258 // unsigned long getTimer(); 00259 // bool endTimer(); 00260 00261 bool unitTest(); 00262 JString unitTestString; 00263 bool priorityTest(); 00264 00265 JString name; 00266 JSemaphore* publicSemaphore; 00267 JSemaphore* testSemaphore; 00268 JMutex* testMutex; 00269 00270 JMutex resetMutex; 00271 00272 bool linuxThreadHasFinished; 00273 00274 // Public OS Information 00275 JString osGetNameVersion(); 00276 JString osGetName(); 00277 JString osGetVersion(); 00278 JString osGetMachineArch(); 00279 JString osGetMachineName(); 00280 JString osGetMachineAddress(); 00281 JString osGetType(); 00282 double osGetTotalMemory(); 00283 double osGetCPUSpeed(); 00284 int osGetCPUCount(); 00285 00286 JTime getStartTime(); 00287 JTime getEndTime(); 00288 00289 ThreadStat procAverageCPUUsage(); 00290 ThreadStat procCurrentCPUUsage(); 00291 JString getComputerName(); 00292 int procMemUsage(); 00293 00294 bool addToAllThreadsStats(HANDLE aThread); 00295 bool addToAllThreadsStats(HANDLE aThread, ThreadStat* stat); 00296 ThreadStat getAllThreadsStats(); 00297 bool removeFromAllThreadsStats(HANDLE aThread); 00298 00299 JString runOSTextCommand(JString cmd); 00300 00301 static Collection getProgramTrace(); 00302 00303 JString localhostName; 00304 JTime threadStartTime, threadEndTime; 00305 HANDLE hThread; 00306 00307 00308 protected: 00309 00310 #ifdef WIN32 00311 // LARGE_INTEGER usecFreq; 00312 double usecFreq; 00313 #else 00314 double usecFreq; 00315 #endif // WIN32 00316 00317 00318 int priority; 00319 ThreadStat stat; 00320 ThreadStat procLastStat; 00321 ThreadStat procStartStat; 00322 00323 int convPriorityToOS(int pri); 00324 int convPriorityToInternal(int pri); 00325 00326 ObjectDictionary statDict; 00327 ThreadStat getCPUUsageOnThread(HANDLE aThread); 00328 bool resetCPUUsageOnThread(HANDLE aThread); 00329 ThreadStat getStoredCPUUsageOnThread(HANDLE aThread); 00330 00331 HANDLE getCallingThreadID(); 00332 00333 // virtual void OnTimer(); 00334 // virtual void OnException(); 00335 // virtual void OnSignal(int signo); 00336 00337 private: 00338 }; 00339 00340 #ifdef WIN32 00341 static DWORD WINAPI staticRunThread(LPVOID lpParam); 00342 #else 00343 void* staticRunThread(void* arg); 00344 #endif 00345 00346 } // namespace cmlabs 00347 00348 #include "JSemaphore.h" 00349 00350 namespace cmlabs { 00351 00352 class JThreadTester : public JThread 00353 { 00354 public: 00355 JThreadTester(int pri); 00356 ~JThreadTester(); 00357 00358 void run(); 00359 int ver; 00360 }; 00361 00362 } // namespace cmlabs 00363 00364 #endif // !defined(AFX_JTHREAD_H__7D7254A3_4145_4D83_94DE_1343EB810C61__INCLUDED_)