5 void TSysProc::Sleep(
const uint& MSecs){
9 TStr TSysProc::GetExeFNm(){
11 LPTSTR FNmCStr=
new char[MxFNmLen];
12 DWORD FNmLen=GetModuleFileName(NULL, FNmCStr, MxFNmLen);
20 void TSysProc::SetLowPriority(){
21 SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);
24 bool TSysProc::ExeProc(
const TStr& ExeFNm,
TStr& ParamStr){
26 PROCESS_INFORMATION pi;
27 ZeroMemory(&si,
sizeof(si));
29 ZeroMemory(&pi,
sizeof(pi));
32 BOOL Ok=CreateProcess(
45 WaitForSingleObject( pi.hProcess, INFINITE );
47 CloseHandle( pi.hProcess );
48 CloseHandle( pi.hThread );
57 TStr TSysMemStat::GetLoadStr(){
58 static TStr MemUsageStr=
"Mem Load: ";
66 TStr TSysMemStat::GetUsageStr(){
67 static TStr MemUsageStr=
"Mem Usage: ";
68 uint64 GlobalUsage=GetTotalPageFile()-GetAvailPageFile();
78 TStr TSysMemStat::GetInfoStr(){
82 ChA+=
"Total Physical:";
84 ChA+=
"Available Physical:";
86 ChA+=
"Total Page File:";
88 ChA+=
"Available Page File:";
90 ChA+=
"Total Virtual:";
92 ChA+=
"Available Virtual:";
97 TStr TSysMemStat::GetStr(){
111 TSysConsole::TSysConsole(){
112 Ok=(AllocConsole()!=0);
114 hStdOut=GetStdHandle(STD_OUTPUT_HANDLE);
115 IAssert(hStdOut!=INVALID_HANDLE_VALUE);
118 TSysConsole::~TSysConsole(){
123 void TSysConsole::Put(
const TStr& Str){
125 WriteConsole(hStdOut, Str.
CStr(), Str.
Len(), &ChsWritten, NULL);
131 void TSysConsoleNotify::OnNotify(
const TNotifyType& Type,
const TStr& MsgStr){
140 void TSysConsoleNotify::OnStatus(
const TStr& MsgStr){
141 SysConsole->Put(MsgStr.
CStr());
144 SysConsole->PutLn(
""); }
159 TTm TSysTm::GetCurUniTm(){
161 GetSystemTime(&SysTm);
162 return TTm(SysTm.wYear, SysTm.wMonth, SysTm.wDay, SysTm.wDayOfWeek,
163 SysTm.wHour, SysTm.wMinute, SysTm.wSecond, SysTm.wMilliseconds);
166 TTm TSysTm::GetCurLocTm(){
168 GetLocalTime(&SysTm);
169 return TTm(SysTm.wYear, SysTm.wMonth, SysTm.wDay, SysTm.wDayOfWeek,
170 SysTm.wHour, SysTm.wMinute, SysTm.wSecond, SysTm.wMilliseconds);
173 uint64 TSysTm::GetCurUniMSecs(){
174 SYSTEMTIME SysTm; FILETIME FileTm;
175 GetSystemTime(&SysTm);
176 IAssert(SystemTimeToFileTime(&SysTm, &FileTm));
177 TUInt64 UInt64(
uint(FileTm.dwHighDateTime),
uint(FileTm.dwLowDateTime));
178 return UInt64.Val/
uint64(10000);
181 uint64 TSysTm::GetCurLocMSecs(){
182 SYSTEMTIME SysTm; FILETIME FileTm;
183 GetLocalTime(&SysTm);
184 IAssert(SystemTimeToFileTime(&SysTm, &FileTm));
185 TUInt64 UInt64(
uint(FileTm.dwHighDateTime),
uint(FileTm.dwLowDateTime));
186 return UInt64.Val/
uint64(10000);
189 uint64 TSysTm::GetMSecsFromTm(
const TTm& Tm){
190 SYSTEMTIME SysTm; FILETIME FileTm;
191 SysTm.wYear=WORD(Tm.
GetYear());
194 SysTm.wDay=WORD(Tm.
GetDay());
195 SysTm.wHour=WORD(Tm.
GetHour());
196 SysTm.wMinute=WORD(Tm.
GetMin());
197 SysTm.wSecond=WORD(Tm.
GetSec());
198 SysTm.wMilliseconds=WORD(Tm.
GetMSec());
199 ESAssert(SystemTimeToFileTime(&SysTm, &FileTm));
200 TUInt64 UInt64(
uint(FileTm.dwHighDateTime),
uint(FileTm.dwLowDateTime));
201 return UInt64.Val/
uint64(10000);
204 TTm TSysTm::GetTmFromMSecs(
const uint64& MSecs){
206 SYSTEMTIME SysTm; FILETIME FileTm;
207 FileTm.dwHighDateTime=FileTmUnits.GetMsVal();
208 FileTm.dwLowDateTime=FileTmUnits.GetLsVal();
209 SAssert(FileTimeToSystemTime(&FileTm, &SysTm));
210 return TTm(SysTm.wYear, SysTm.wMonth, SysTm.wDay, SysTm.wDayOfWeek,
211 SysTm.wHour, SysTm.wMinute, SysTm.wSecond, SysTm.wMilliseconds);
214 uint TSysTm::GetMSecsFromOsStart(){
215 return uint(GetTickCount());
218 TTm TSysTm::GetLocTmFromUniTm(
const TTm& Tm){
220 TIME_ZONE_INFORMATION TzInf;
221 GetTimeZoneInformation(&TzInf);
224 UniSysTm.wYear=WORD(Tm.
GetYear());
225 UniSysTm.wMonth=WORD(Tm.
GetMonth());
227 UniSysTm.wDay=WORD(Tm.
GetDay());
228 UniSysTm.wHour=WORD(Tm.
GetHour());
229 UniSysTm.wMinute=WORD(Tm.
GetMin());
230 UniSysTm.wSecond=WORD(Tm.
GetSec());
231 UniSysTm.wMilliseconds=WORD(Tm.
GetMSec());
234 SystemTimeToTzSpecificLocalTime(&TzInf, &UniSysTm, &LocSysTm);
236 return TTm(LocSysTm.wYear, LocSysTm.wMonth, LocSysTm.wDay, LocSysTm.wDayOfWeek,
237 LocSysTm.wHour, LocSysTm.wMinute, LocSysTm.wSecond, LocSysTm.wMilliseconds);
240 TTm TSysTm::GetUniTmFromLocTm(
const TTm& Tm){
242 TIME_ZONE_INFORMATION TzInf;
243 GetTimeZoneInformation(&TzInf);
246 LocSysTm.wYear=WORD(Tm.
GetYear());
247 LocSysTm.wMonth=WORD(Tm.
GetMonth());
249 LocSysTm.wDay=WORD(Tm.
GetDay());
250 LocSysTm.wHour=WORD(Tm.
GetHour());
251 LocSysTm.wMinute=WORD(Tm.
GetMin());
252 LocSysTm.wSecond=WORD(Tm.
GetSec());
253 LocSysTm.wMilliseconds=WORD(Tm.
GetMSec());
255 SYSTEMTIME UniSysTm=LocSysTm;
259 return TTm(UniSysTm.wYear, UniSysTm.wMonth, UniSysTm.wDay, UniSysTm.wDayOfWeek,
260 UniSysTm.wHour, UniSysTm.wMinute, UniSysTm.wSecond, UniSysTm.wMilliseconds);
263 uint64 TSysTm::GetProcessMSecs(){
264 FILETIME CreationTime, ExitTime, KernelTime, UserTime;
265 IAssert(GetProcessTimes(GetCurrentProcess(),
266 &CreationTime, &ExitTime, &KernelTime, &UserTime));
267 TUInt64 KernelMSecs(
uint(KernelTime.dwHighDateTime),
uint(KernelTime.dwLowDateTime));
268 TUInt64 UserMSecs(
uint(UserTime.dwHighDateTime),
uint(UserTime.dwLowDateTime));
269 uint64 ProcessMSecs=KernelMSecs+UserMSecs;
273 uint64 TSysTm::GetThreadMSecs(){
274 FILETIME CreationTime, ExitTime, KernelTime, UserTime;
275 IAssert(GetProcessTimes(GetCurrentProcess(),
276 &CreationTime, &ExitTime, &KernelTime, &UserTime));
277 TUInt64 KernelMSecs(
uint(KernelTime.dwHighDateTime),
uint(KernelTime.dwLowDateTime));
278 TUInt64 UserMSecs(
uint(UserTime.dwHighDateTime),
uint(UserTime.dwLowDateTime));
279 uint64 ThreadMSecs=KernelMSecs+UserMSecs;
283 uint64 TSysTm::GetPerfTimerFq(){
285 LARGE_INTEGER LargeInt;
286 if (QueryPerformanceFrequency(&LargeInt)){
287 MsFq=LargeInt.u.HighPart;
288 LsFq=LargeInt.u.LowPart;
297 uint64 TSysTm::GetPerfTimerTicks(){
299 LARGE_INTEGER LargeInt;
300 if (QueryPerformanceCounter(&LargeInt)){
301 MsVal=LargeInt.u.HighPart;
302 LsVal=LargeInt.u.LowPart;
305 LsVal=int(time(NULL));
313 TStr TSysStr::GetCmLn(){
314 return TStr((
char*)GetCommandLine());
317 TStr TSysStr::GetMsgStr(
const DWORD& MsgCd){
321 FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
324 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
329 TStr MsgStr((
char*)lpMsgBuf);
335 char* TSysStr::GetLastMsgCStr(){
336 TStr MsgStr=GetLastMsgStr();
337 static char* MsgCStr=NULL;
338 if (MsgCStr==NULL){MsgCStr=
new char[1000];}
339 strcpy(MsgCStr, MsgStr.
CStr());
345 PRegKey TRegKey::GetKey(
const PRegKey& BaseKey,
const TStr& SubKeyNm){
347 DWORD RetCd=RegOpenKeyEx(
348 BaseKey->GetHandle(), SubKeyNm.
CStr(), 0, KEY_ALL_ACCESS, &hKey);
349 bool Ok=RetCd==ERROR_SUCCESS;
350 return new TRegKey(Ok, hKey);
353 TStr TRegKey::GetVal(
const PRegKey& Key,
const TStr& SubKeyNm,
const TStr& ValNm){
354 PRegKey RegKey=TRegKey::GetKey(Key, SubKeyNm);
356 TStrKdV ValNmStrKdV; RegKey->GetValV(ValNmStrKdV);
359 return ValNmStrKdV[ValN].Dat;
368 void TRegKey::GetKeyNmV(
TStrV& KeyNmV)
const {
374 DWORD RetCd=RegQueryInfoKey(
387 if (RetCd!=ERROR_SUCCESS){
return;}
391 KeyNmV.
Gen(SubKeys, 0);
392 char* SubKeyNmCStr=
new char[MxSubKeyNmLen+1];
395 DWORD SubKeyNmCStrLen=MxSubKeyNmLen+1;
396 DWORD RetCd=RegEnumKeyEx(
405 if (RetCd==ERROR_SUCCESS){
406 TStr KeyNm(SubKeyNmCStr);
413 delete[] SubKeyNmCStr;
417 void TRegKey::GetValV(
TStrKdV& ValNmStrKdV)
const {
424 DWORD RetCd=RegQueryInfoKey(
437 if (RetCd!=ERROR_SUCCESS){
return;}
441 ValNmStrKdV.
Gen(Vals, 0);
442 char* ValNmCStr=
new char[MxValNmLen+1];
443 char* ValCStr=
new char[MxValStrLen+1];
446 DWORD ValNmCStrLen=MxValNmLen+1;
447 DWORD ValCStrLen=MxValStrLen+1;
449 DWORD RetCd=RegEnumValue(
456 (
unsigned char*) ValCStr,
458 if (RetCd==ERROR_SUCCESS){
459 if (ValType==REG_SZ){
460 TStr ValNm(ValNmCStr);
461 TStr ValStr(ValCStr);
476 void TStdIOPipe::CreateProc(
const TStr& Cmd) {
477 PROCESS_INFORMATION piProcInfo;
478 STARTUPINFO siStartInfo;
479 ZeroMemory( &piProcInfo,
sizeof(PROCESS_INFORMATION));
480 ZeroMemory( &siStartInfo,
sizeof(STARTUPINFO));
481 siStartInfo.cb =
sizeof(STARTUPINFO);
482 siStartInfo.hStdInput = ChildStdinRd;
483 siStartInfo.hStdOutput = ChildStdoutWr;
484 siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
486 const BOOL FuncRetn = CreateProcess(NULL,
497 CloseHandle(piProcInfo.hProcess);
498 CloseHandle(piProcInfo.hThread);
501 TStdIOPipe::TStdIOPipe(
const TStr& CmdToExe) : ChildStdinRd(NULL), ChildStdinWrDup(NULL),
502 ChildStdoutWr(NULL), ChildStdoutRdDup(NULL) {
503 HANDLE ChildStdinWr, ChildStdoutRd;
504 SECURITY_ATTRIBUTES saAttr;
506 saAttr.nLength =
sizeof(SECURITY_ATTRIBUTES);
507 saAttr.bInheritHandle = TRUE;
508 saAttr.lpSecurityDescriptor = NULL;
510 EAssert(CreatePipe(&ChildStdoutRd, &ChildStdoutWr, &saAttr, 0));
512 EAssert(DuplicateHandle(GetCurrentProcess(), ChildStdoutRd,
513 GetCurrentProcess(), &ChildStdoutRdDup, 0, FALSE, DUPLICATE_SAME_ACCESS));
514 CloseHandle(ChildStdoutRd);
516 EAssert(CreatePipe(&ChildStdinRd, &ChildStdinWr, &saAttr, 0));
518 EAssert(DuplicateHandle(GetCurrentProcess(), ChildStdinWr,
519 GetCurrentProcess(), &ChildStdinWrDup, 0, FALSE, DUPLICATE_SAME_ACCESS));
520 CloseHandle(ChildStdinWr);
522 CreateProc(CmdToExe);
525 TStdIOPipe::~TStdIOPipe() {
526 if (ChildStdinRd != NULL) CloseHandle(ChildStdinRd);
527 if (ChildStdinWrDup != NULL) CloseHandle(ChildStdinWrDup);
528 if (ChildStdoutWr != NULL) CloseHandle(ChildStdoutWr);
529 if (ChildStdoutRdDup != NULL) CloseHandle(ChildStdoutRdDup);
532 int TStdIOPipe::Write(
const char* Bf,
const int& BfLen) {
534 EAssert(WriteFile(ChildStdinWrDup, Bf, BfLen, &Written, NULL));
538 int TStdIOPipe::Read(
char *Bf,
const int& BfMxLen) {
540 EAssert(ReadFile(ChildStdoutRdDup, Bf, BfMxLen, &Read, NULL));
544 #elif defined(GLib_UNIX)
548 int GetModuleFileName(
void *hModule,
char *Bf,
int MxBfL) {
549 int retlen = (int) readlink(
"/proc/self/exe", Bf, MxBfL);
551 if (MxBfL > 0) Bf[0] =
'\0';
554 if (retlen == MxBfL) --retlen;
559 int GetCurrentDirectory(
const int MxBfL,
char *Bf) {
561 return (
int) strlen(Bf);
564 int CreateDirectory(
const char *FNm,
void *useless) {
565 return mkdir(FNm, 0777)==0;
568 int RemoveDirectory(
const char *FNm) {
569 return unlink(FNm)==0;
572 #define TICKS_PER_SECOND 10000000
573 #define EPOCH_DIFFERENCE 11644473600LL
576 uint64 Epoch2Ft(time_t Epoch){
578 Ft = Epoch + EPOCH_DIFFERENCE;
579 Ft *= TICKS_PER_SECOND;
584 time_t Ft2Epoch(
uint64 Ft){
586 Epoch = Ft / TICKS_PER_SECOND;
587 Epoch -= EPOCH_DIFFERENCE;
588 return (time_t) Epoch;
593 TTm TSysTm::GetCurUniTm(){
599 int ErrCd = gettimeofday(&tv, NULL);
601 Assert((ErrCd==0)&&(t!=-1));
605 return TTm(1900+tms.tm_year, tms.tm_mon, tms.tm_mday, tms.tm_wday,
606 tms.tm_hour, tms.tm_min, tms.tm_sec, tv.tv_usec/1000);
609 TTm TSysTm::GetCurLocTm(){
615 int ErrCd = gettimeofday(&tv, NULL);
617 Assert((ErrCd==0)&&(t!=-1));
619 localtime_r(&t, &tms);
621 return TTm(1900+tms.tm_year, tms.tm_mon, tms.tm_mday, tms.tm_wday,
622 tms.tm_hour, tms.tm_min, tms.tm_sec, tv.tv_usec/1000);
625 uint64 TSysTm::GetCurUniMSecs(){
629 uint64 TSysTm::GetCurLocMSecs(){
633 uint64 TSysTm::GetMSecsFromTm(
const TTm& Tm){
636 tms.tm_year = Tm.
GetYear() - 1900;
638 tms.tm_mday = Tm.
GetDay();
647 TTm TSysTm::GetTmFromMSecs(
const uint64& TmNum){
648 const int MSec = int(TmNum % 1000);
649 time_t Sec = Ft2Epoch(TmNum*10000);
652 gmtime_r(&Sec, &tms);
654 return TTm(1900+tms.tm_year, tms.tm_mon, tms.tm_mday, tms.tm_wday,
655 tms.tm_hour, tms.tm_min, tms.tm_sec, MSec);
658 TTm TSysTm::GetLocTmFromUniTm(
const TTm& Tm) {
661 tms.tm_year = Tm.
GetYear() - 1900;
663 tms.tm_mday = Tm.
GetDay();
669 time_t Sec = timegm(&tms);
670 localtime_r(&Sec, &tmr);
672 return TTm(1900+tmr.tm_year, tmr.tm_mon, tmr.tm_mday, tmr.tm_wday,
673 tmr.tm_hour, tmr.tm_min, tmr.tm_sec, MSec);
676 TTm TSysTm::GetUniTmFromLocTm(
const TTm& Tm) {
679 tms.tm_year = Tm.
GetYear() - 1900;
681 tms.tm_mday = Tm.
GetDay();
688 time_t Sec = mktime(&tms);
689 gmtime_r(&Sec, &tmr);
691 return TTm(1900+tmr.tm_year, tmr.tm_mon, tmr.tm_mday, tmr.tm_wday,
692 tmr.tm_hour, tmr.tm_min, tmr.tm_sec, MSec);
695 uint TSysTm::GetMSecsFromOsStart(){
696 #if defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK != -1)
698 int ErrCd=clock_gettime(CLOCK_MONOTONIC, &ts);
702 return (ts.tv_sec*1000) + (ts.tv_nsec/1000000);
706 f = fopen(
"/proc/uptime",
"r");
707 if (!f)
return 0xffffffff;
708 fscanf(f,
"%u.%u", &sec, &csec);
710 return (
uint) (sec * 1000) + (csec * 10);
714 uint64 TSysTm::GetProcessMSecs() {
715 #if defined(_POSIX_CPUTIME) && (_POSIX_CPUTIME != -1)
717 int ErrCd=clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
721 return (ts.tv_sec*1000) + (ts.tv_nsec / 1000000);
725 int ErrCd = getrusage(RUSAGE_SELF, &ru);
729 return ((ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000) +
730 ((ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) * 1000);
734 uint64 TSysTm::GetThreadMSecs() {
735 #if defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME != -1)
737 int ErrCd=clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
741 return (ts.tv_sec*1000) + (ts.tv_nsec / 1000000);
744 return GetProcessMSecs();
748 uint64 TSysTm::GetPerfTimerFq(){
749 #if defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK != -1)
756 uint64 TSysTm::GetPerfTimerTicks(){
757 #if defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK != -1)
759 int ErrCd=clock_gettime(CLOCK_MONOTONIC, &ts);
762 return (
uint64)ts.tv_sec*1000000000ll + (
uint64)ts.tv_nsec; }
765 gettimeofday(&tv, NULL);
766 return (
uint64)tv.tv_usec + ((
uint64)tv.tv_sec)*1000000;
771 gettimeofday(&tv, NULL);
772 return (
uint64)tv.tv_usec + ((
uint64)tv.tv_sec)*1000000;
778 int TSysProc::Sleep(
const uint& MSecs) {
780 struct timespec tsp, trem;
781 tsp.tv_sec = MSecs / 1000;
782 tsp.tv_nsec = (MSecs % 1000) * 1000000;
785 ret = nanosleep(&tsp, &trem);
786 if ((ret != -1) || (errno != EINTR)) {
795 TStr TSysProc::GetExeFNm() {
797 GetModuleFileName(NULL, Bf, 1023);
801 void TSysProc::SetLowPriority() {
805 bool TSysProc::ExeProc(
const TStr& ExeFNm,
TStr& ParamStr) {
810 if (pid == -1)
return false;
811 if (pid > 0)
return true;
814 argv =
new char*[SArgV.
Len()+2];
815 argv[0] = strdup(ExeFNm.
CStr());
816 for (
int i=0;i<SArgV.
Len();i++) argv[i+1] = strdup(SArgV[i].CStr());
817 argv[SArgV.
Len()+1] = NULL;
819 execvp(argv[0], argv);
823 kill(getpid(), SIGINT);
848 TStdIOPipe::TStdIOPipe(
const TStr& CmdToExe) {
849 FailR(
"Not intended for use under Linux!");
852 TStdIOPipe::~TStdIOPipe() {
853 FailR(
"Not intended for use under Linux!");
856 int TStdIOPipe::Write(
const char* Bf,
const int& BfLen) {
857 FailR(
"Not intended for use under Linux!");
861 int TStdIOPipe::Read(
char *Bf,
const int& BfMxLen) {
862 FailR(
"Not intended for use under Linux!");
static TStr GetTypeStr(const TNotifyType &Type, const bool &Brief=true)
bool IsIn(const TVal &Val) const
Checks whether element Val is a member of the vector.
TSizeTy Len() const
Returns the number of elements in the vector.
static TStr GetMegaStr(const uint64 &Val)
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
unsigned long long uint64
enum TNotifyType_ TNotifyType
static TStr Fmt(const char *FmtStr,...)
#define EAssertR(Cond, MsgStr)
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
void SplitOnWs(TStrV &StrV) const
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
static uint64 GetMSecsFromTm(const TTm &Tm)
TKeyDat< TStr, TStr > TStrKd