Process.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #include "il2cpp-config.h"
  2. #if IL2CPP_TARGET_LUMIN
  3. #include <sys/sysinfo.h>
  4. #include <sys/time.h>
  5. #include <sys/types.h>
  6. #include <unistd.h>
  7. #include <stdio.h>
  8. #include "os/Process.h"
  9. #include "utils/Logging.h"
  10. struct ProcessHandle
  11. {
  12. pid_t pid;
  13. };
  14. namespace il2cpp
  15. {
  16. namespace os
  17. {
  18. const int _sc_clk_tck = sysconf(_SC_CLK_TCK);
  19. typedef struct
  20. {
  21. char name[256];
  22. unsigned long utime;
  23. unsigned long stime;
  24. unsigned long long starttime;
  25. } ProcessStatInfo;
  26. typedef struct
  27. {
  28. unsigned vmpeak;
  29. unsigned vmsize;
  30. unsigned vmhwm;
  31. unsigned vmrss;
  32. unsigned vmdata;
  33. unsigned vmstk;
  34. unsigned vmswap;
  35. } ProcessStatusInfo;
  36. static bool GetProcessStatInfo(int pid, ProcessStatInfo *result)
  37. {
  38. char buffer[4096], *open_paren, *close_paren;
  39. FILE *fp;
  40. sprintf(buffer, "/proc/%d/stat", pid);
  41. fp = fopen(buffer, "r");
  42. if (fp == NULL)
  43. {
  44. return false;
  45. }
  46. fgets(buffer, sizeof(buffer) - 1, fp);
  47. fclose(fp);
  48. buffer[sizeof(buffer) - 1] = 0;
  49. open_paren = strchr(buffer, '(');
  50. if (open_paren == NULL)
  51. {
  52. return false;
  53. }
  54. close_paren = strrchr(open_paren + 1, ')');
  55. if (close_paren == NULL)
  56. {
  57. return false;
  58. }
  59. *close_paren = 0;
  60. strncpy(result->name, open_paren + 1, sizeof(result->name) - 1);
  61. result->name[sizeof(result->name) - 1] = 0;
  62. sscanf(close_paren + 1, " %*c %*d %*d %*d %*d %*d %*u %*lu %*lu %*lu %*lu %lu %lu %*ld %*ld %*ld %*ld %*ld %*ld %llu",
  63. &result->utime,
  64. &result->stime,
  65. &result->starttime);
  66. return true;
  67. }
  68. #define GPSI_STARTSWITH(s) (strncasecmp(buffer, s, sizeof(s) - 1) == 0)
  69. static bool GetProcessStatusInfo(int pid, ProcessStatusInfo *result)
  70. {
  71. char buffer[1024], *p;
  72. FILE *fp;
  73. sprintf(buffer, "/proc/%d/status", pid);
  74. fp = fopen(buffer, "r");
  75. if (fp == NULL)
  76. {
  77. return false;
  78. }
  79. memset(result, 0, sizeof(*result));
  80. while (fgets(buffer, sizeof(buffer) - 1, fp))
  81. {
  82. buffer[sizeof(buffer) - 1] = 0;
  83. if (GPSI_STARTSWITH("VmPeak:"))
  84. {
  85. sscanf(buffer + sizeof("VmPeak:") - 1, "%u", &result->vmpeak);
  86. }
  87. else if (GPSI_STARTSWITH("VmSize:"))
  88. {
  89. sscanf(buffer + sizeof("VmSize:") - 1, "%u", &result->vmsize);
  90. }
  91. else if (GPSI_STARTSWITH("VmHWM:"))
  92. {
  93. sscanf(buffer + sizeof("VmHWM:") - 1, "%u", &result->vmhwm);
  94. }
  95. else if (GPSI_STARTSWITH("VmRSS:"))
  96. {
  97. sscanf(buffer + sizeof("VmRSS:") - 1, "%u", &result->vmrss);
  98. }
  99. else if (GPSI_STARTSWITH("VmData:"))
  100. {
  101. sscanf(buffer + sizeof("VmData:") - 1, "%u", &result->vmdata);
  102. }
  103. else if (GPSI_STARTSWITH("VmStk:"))
  104. {
  105. sscanf(buffer + sizeof("VmStk:") - 1, "%u", &result->vmstk);
  106. }
  107. else if (GPSI_STARTSWITH("VmSwap:"))
  108. {
  109. sscanf(buffer + sizeof("VmSwap:") - 1, "%u", &result->vmswap);
  110. }
  111. }
  112. fclose(fp);
  113. return true;
  114. }
  115. #undef GPSI_STARTSWITH
  116. int Process::GetCurrentProcessId()
  117. {
  118. return getpid();
  119. }
  120. utils::Expected<ProcessHandle*> Process::GetProcess(int processId)
  121. {
  122. // If/when we implement the CreateProcess_internal icall we will likely
  123. // need to so something smarter here to find the process if we did
  124. // not create it and return a known pseudo-handle. For now this
  125. // is sufficient though.
  126. return (ProcessHandle*)(intptr_t)processId;
  127. }
  128. void Process::FreeProcess(ProcessHandle* handle)
  129. {
  130. // We have nothing to do here.
  131. }
  132. utils::Expected<std::string> Process::GetProcessName(ProcessHandle* handle)
  133. {
  134. intptr_t pid = (intptr_t)handle;
  135. ProcessStatInfo psi;
  136. if (GetProcessStatInfo(pid, &psi))
  137. {
  138. return std::string(psi.name);
  139. }
  140. else
  141. {
  142. return std::string();
  143. }
  144. }
  145. int64_t Process::Times(ProcessHandle* handle, int32_t type)
  146. {
  147. int pid = (intptr_t)handle;
  148. ProcessStatInfo psi;
  149. int64_t result = 0;
  150. if (GetProcessStatInfo(pid, &psi))
  151. {
  152. result = (int64_t)psi.utime * (10000000 / _sc_clk_tck);
  153. if (type == 2) // Total process time
  154. {
  155. result += (int64_t)psi.stime * (10000000 / _sc_clk_tck);
  156. }
  157. }
  158. return result;
  159. }
  160. int64_t Process::StartTime(ProcessHandle* handle)
  161. {
  162. int pid = (intptr_t)handle;
  163. ProcessStatInfo psi;
  164. if (GetProcessStatInfo(pid, &psi))
  165. {
  166. struct sysinfo si;
  167. if (sysinfo(&si) == 0)
  168. {
  169. const int64_t unix_time_to_filetime = 11644473600ll;
  170. int64_t boot_time = (int64_t)time(NULL) - si.uptime + unix_time_to_filetime;
  171. return (boot_time + psi.starttime / _sc_clk_tck) * 10000000ll;
  172. }
  173. }
  174. return 0;
  175. }
  176. int64_t Process::GetProcessData(int32_t pid, int32_t data_type, int32_t* error)
  177. {
  178. ProcessStatusInfo psi;
  179. if (GetProcessStatusInfo(pid, &psi))
  180. {
  181. switch (data_type)
  182. {
  183. case 4: // WorkingSet64
  184. return (int64_t)psi.vmrss * 1024;
  185. case 5: // PeakWorkingSet64
  186. return (int64_t)psi.vmhwm * 1024;
  187. case 6: // PrivateMemorySize64
  188. return (int64_t)(psi.vmdata + psi.vmstk) * 1024;
  189. case 7: // VirtualMemorySize64
  190. return (int64_t)psi.vmsize * 1024;
  191. case 8: // PeakVirtualMemorySize64
  192. return (int64_t)psi.vmpeak * 1024;
  193. default:
  194. {
  195. char buf[256];
  196. sprintf(buf, "Process::GetProcessData: Unknown data_type %d", data_type);
  197. utils::Logging::Write(buf);
  198. break;
  199. }
  200. }
  201. }
  202. return 0;
  203. }
  204. intptr_t Process::GetMainWindowHandle(int32_t pid)
  205. {
  206. return 0;
  207. }
  208. }
  209. }
  210. #endif