main.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*##############################################################################
  2. Copyright (C) 2011 HPCC Systems.
  3. All rights reserved. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ############################################################################## */
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #if defined(_WIN32) || defined(_WIN64)
  17. #include <windows.h>
  18. #define Li2Double(x) ((double)((x).HighPart) * 4.294967296E9 + (double)((x).LowPart))
  19. typedef union
  20. {
  21. LONGLONG li;
  22. FILETIME ft;
  23. } BIGTIME;
  24. class TSpan
  25. {
  26. public:
  27. int s, m, h, d;
  28. TSpan(int secs)
  29. {
  30. s = secs % 60;
  31. m = (secs % 3600) / 60;
  32. h = (secs % 86400) / 3600;
  33. d = secs / 86400;
  34. }
  35. };
  36. #endif
  37. int main(int argc, char** argv)
  38. {
  39. #if !defined(_WIN32) && !defined(_WIN64)
  40. printf("Only Windows OS is supported.\n");
  41. #else
  42. if(argc < 3)
  43. {
  44. printf("usage: %s <dir> <command>\n", argv[0]);
  45. return -1;
  46. }
  47. char path[512];
  48. sprintf_s(path, 511, "%s\\%s.pid", argv[1], argv[2]);
  49. DWORD pid = 0;
  50. FILE* f = NULL;
  51. fopen_s(&f, path, "r");
  52. if(!f)
  53. {
  54. fprintf(stderr, "Can't open file %s\n", path);
  55. }
  56. else
  57. {
  58. char* pidbuf[32];
  59. int numread = fread(pidbuf, sizeof(char), 31, f);
  60. if(numread > 0)
  61. {
  62. pidbuf[numread] = '\0';
  63. pid = atoi((const char*)pidbuf);
  64. }
  65. }
  66. if(pid > 0)
  67. {
  68. printf("ProcessID: %d\n", pid);
  69. HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION, false, pid);
  70. if(h <= 0)
  71. {
  72. fprintf(stderr, "Process %d can't be opened.\n", pid);
  73. printf("ProcessUpTime: \n");
  74. }
  75. else
  76. {
  77. //Process elapsed time.
  78. BIGTIME CreateTime, ExitTime, ElapsedTime, Now;
  79. FILETIME KernelTime, UserTime;
  80. GetProcessTimes(h, &CreateTime.ft, &ExitTime.ft, &KernelTime, &UserTime);
  81. if(ExitTime.li > CreateTime.li)
  82. ElapsedTime.li = ExitTime.li - CreateTime.li;
  83. else
  84. {
  85. GetSystemTimeAsFileTime(&Now.ft);
  86. ElapsedTime.li = Now.li - CreateTime.li;
  87. }
  88. unsigned elapsedsecs = (unsigned)(ElapsedTime.li/10000000);
  89. TSpan span(elapsedsecs);
  90. printf("ProcessUpTime: %d-%02d:%02d:%02d\n", span.d, span.h, span.m, span.s);
  91. }
  92. }
  93. else
  94. {
  95. printf("ProcessID: \nProcessUpTime: \n");
  96. }
  97. //CPU usage
  98. BIGTIME idle1, kernel1, user1, idle2, kernel2, user2;
  99. GetSystemTimes(&idle1.ft, &kernel1.ft, &user1.ft);
  100. Sleep(1000);
  101. GetSystemTimes(&idle2.ft, &kernel2.ft, &user2.ft);
  102. int IdleTime = (int)(idle2.li - idle1.li);
  103. int TotalTime = (int)((kernel2.li + user2.li) - (kernel1.li + user1.li));
  104. int idleRate = (int)(100.0 * IdleTime / TotalTime);
  105. printf("CPU-Idle: %d%%\n", idleRate);
  106. //Computer uptime
  107. LARGE_INTEGER ticks, unit;
  108. QueryPerformanceCounter(&ticks);
  109. QueryPerformanceFrequency(&unit);
  110. int secs = (int)(ticks.QuadPart/unit.QuadPart);
  111. TSpan u((int)secs);
  112. printf("ComputerUpTime: %d days, %d:%d\n", u.d, u.h, u.m);
  113. printf("---SpaceUsedAndFree---\n");
  114. //Physical and virtual memory usage.
  115. MEMORYSTATUS memstatus;
  116. GlobalMemoryStatus(&memstatus);
  117. printf("Physical Memory: %d %d\nVirtual Memory: %d %d\n",
  118. (memstatus.dwTotalPhys - memstatus.dwAvailPhys)/1024, memstatus.dwAvailPhys/1024,
  119. (memstatus.dwTotalVirtual - memstatus.dwAvailVirtual)/1024, memstatus.dwAvailVirtual/1024);
  120. // Disk Usage
  121. char drivePath[] = "?:\\";
  122. char driveName;
  123. for( driveName = 'A'; driveName <= 'Z'; driveName++ )
  124. {
  125. drivePath[0] = driveName;
  126. int dtype = GetDriveTypeA(drivePath);
  127. if(dtype == DRIVE_FIXED || dtype == DRIVE_RAMDISK || dtype == DRIVE_REMOVABLE || dtype == DRIVE_CDROM)
  128. {
  129. ULARGE_INTEGER diskAvailStruct;
  130. ULARGE_INTEGER diskTotalStruct;
  131. diskAvailStruct.QuadPart = 0;
  132. diskTotalStruct.QuadPart = 0;
  133. GetDiskFreeSpaceExA(drivePath, &diskAvailStruct, &diskTotalStruct, 0);
  134. double DiskSize = diskTotalStruct.QuadPart / 1024.0;
  135. double FreeSize = diskAvailStruct.QuadPart / 1024.0;
  136. printf("%s: %.0f %.0f\n", drivePath, DiskSize - FreeSize, FreeSize);
  137. }
  138. }
  139. #endif
  140. return 0;
  141. }