main.cpp 4.8 KB

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