PageRenderTime 304ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/cocos/platform/android/jni/ProcessCpuTracker.cpp

https://github.com/dumganhar/cocos2d-x
C++ | 355 lines | 206 code | 38 blank | 111 comment | 40 complexity | c7f39f9e3eb99dfb93b83112d22a7fce MD5 | raw file
  1. /****************************************************************************
  2. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "platform/android/jni/ProcessCpuTracker.h"
  21. #ifdef ANDROID
  22. #include <jni.h>
  23. #include <android/log.h>
  24. #include <unistd.h>
  25. #endif
  26. #include <stdlib.h>
  27. #include <vector>
  28. #include <time.h>
  29. #include <fcntl.h>
  30. #include <sstream>
  31. #ifdef ANDROID
  32. #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, "ProcessCpuTracker", __VA_ARGS__)
  33. #else
  34. #define LOGD printf
  35. #endif
  36. typedef struct _CpuInfo
  37. {
  38. long userTime; // Unit: jiffies
  39. long niceTime;
  40. long systemTime;
  41. long idleTime;
  42. long ioWaitTime;
  43. long irqTime;
  44. long softIrqTime;
  45. }CpuInfo;
  46. // Return 0 means the end of buffer
  47. static bool readLine(char* p, const char* end, char** next)
  48. {
  49. if (p == NULL)
  50. {
  51. *next = NULL;
  52. return false;
  53. }
  54. while (*p != '\n' && p < end)
  55. ++p;
  56. if (*p == '\n')
  57. {// line break
  58. *p = '\0'; // Set to \0 to make a sub-sequence string
  59. *next = ++p;
  60. return true;
  61. }
  62. else
  63. {// end of buffer
  64. *next = NULL;
  65. return true;
  66. }
  67. }
  68. static std::vector<CpuInfo> readProcStat()
  69. {
  70. std::vector<CpuInfo> cpuInfos;
  71. cpuInfos.reserve(13);
  72. char buffer[1024] = {0};
  73. #ifdef ANDROID
  74. int fd = open("/proc/stat", O_RDONLY);
  75. if (fd < 0)
  76. {
  77. return cpuInfos;
  78. }
  79. const int len = read(fd, buffer, sizeof(buffer)-1);
  80. close(fd);
  81. if (len < 0) {
  82. LOGD("Unable to open process fd=%d", fd);
  83. return cpuInfos;
  84. }
  85. buffer[len] = 0;
  86. #else
  87. FILE* fp = fopen("fonts/stat-huawei.txt", "rb");
  88. if (fp == NULL)
  89. return cpuInfos;
  90. fread(buffer, sizeof(buffer)-1, 1, fp);
  91. fclose(fp);
  92. size_t len = strlen(buffer);
  93. #endif
  94. char* p = buffer;
  95. const char* end = p + len;
  96. char* next = NULL;
  97. int cpuIndex;
  98. const int COLUMN = sizeof(CpuInfo) / sizeof(long);
  99. size_t i = 0;
  100. while (readLine(p, end, &next))
  101. {
  102. // break if the line with no cpu prefix
  103. if (p[0] != 'c' || p[1] != 'p' || p[2] != 'u')
  104. break;
  105. // LOGD("%s\n", p);
  106. // Removes 'cpu%d' prefix
  107. p = p + 3;
  108. if (*p == ' ')
  109. { // The first line means the average cpu usage
  110. cpuIndex = 0;
  111. }
  112. else
  113. {
  114. cpuIndex = strtol(p, &p, 10) + 1;
  115. }
  116. // LOGD("cpu index: %d\n", cpuIndex);
  117. cpuInfos.resize(cpuIndex + 1);
  118. for (i = 0; i < COLUMN; ++i)
  119. {
  120. long value = strtol(p, &p, 10);
  121. // LOGD("%ld ", value);
  122. CpuInfo& info = cpuInfos[cpuIndex];
  123. long* e = (long*)&info + i;
  124. *e = value;
  125. }
  126. // LOGD("%s", "\n");
  127. p = next;
  128. }
  129. return cpuInfos;
  130. }
  131. void ProcessCpuTracker::update()
  132. {
  133. static const int JIFFYMILLIS = 10;
  134. std::vector<CpuInfo> cpuInfos = readProcStat();
  135. if (!cpuInfos.empty())
  136. {
  137. if (_cpuTimeInfos.size() < cpuInfos.size())
  138. {
  139. _cpuTimeInfos.resize(cpuInfos.size());
  140. }
  141. // LOGD("cpuInfo size: %d", (int)cpuInfos.size());
  142. for (size_t i = 0, len = cpuInfos.size(); i < len; ++i)
  143. {
  144. CpuTimeInfo& cpuTimeInfo = _cpuTimeInfos[i];
  145. const CpuInfo& cpuInfo = cpuInfos[i];
  146. // Total user time is user + nice time.
  147. const long usertime = (cpuInfo.userTime + cpuInfo.niceTime) * JIFFYMILLIS;
  148. // Total system time is simply system time.
  149. const long systemtime = cpuInfo.systemTime * JIFFYMILLIS;
  150. // Total idle time is simply idle time.
  151. const long idletime = cpuInfo.idleTime * JIFFYMILLIS;
  152. // Total irq time is iowait + irq + softirq time.
  153. const long iowaittime = cpuInfo.ioWaitTime * JIFFYMILLIS;
  154. const long irqtime = cpuInfo.irqTime * JIFFYMILLIS;
  155. const long softirqtime = cpuInfo.softIrqTime * JIFFYMILLIS;
  156. // This code is trying to avoid issues with idle time going backwards,
  157. // but currently it gets into situations where it triggers most of the time. :(
  158. if (false || (usertime >= cpuTimeInfo.mBaseUserTime && systemtime >= cpuTimeInfo.mBaseSystemTime
  159. && iowaittime >= cpuTimeInfo.mBaseIoWaitTime && irqtime >= cpuTimeInfo.mBaseIrqTime
  160. && softirqtime >= cpuTimeInfo.mBaseSoftIrqTime && idletime >= cpuTimeInfo.mBaseIdleTime)) {
  161. cpuTimeInfo.mRelUserTime = usertime - cpuTimeInfo.mBaseUserTime;
  162. cpuTimeInfo.mRelSystemTime = systemtime - cpuTimeInfo.mBaseSystemTime;
  163. cpuTimeInfo.mRelIoWaitTime = iowaittime - cpuTimeInfo.mBaseIoWaitTime;
  164. cpuTimeInfo.mRelIrqTime = irqtime - cpuTimeInfo.mBaseIrqTime;
  165. cpuTimeInfo.mRelSoftIrqTime = softirqtime - cpuTimeInfo.mBaseSoftIrqTime;
  166. cpuTimeInfo.mRelIdleTime = idletime - cpuTimeInfo.mBaseIdleTime;
  167. // if (true) {
  168. // LOGD("CPU%d, Total U: %ld, N:%ld S:%ld I:%ld W:%ld Q:%ld O:%ld\n",
  169. // (int)i,
  170. // cpuInfo.userTime,
  171. // cpuInfo.niceTime,
  172. // cpuInfo.systemTime,
  173. // cpuInfo.idleTime,
  174. // cpuInfo.ioWaitTime,
  175. // cpuInfo.irqTime,
  176. // cpuInfo.softIrqTime
  177. // );
  178. // LOGD("CPU%d, Rel U:%ld, S:%ld, I:%ld, Q:%ld\n",
  179. // (int)i,
  180. // cpuTimeInfo.mRelUserTime,
  181. // cpuTimeInfo.mRelSystemTime,
  182. // cpuTimeInfo.mRelIdleTime,
  183. // cpuTimeInfo.mRelIrqTime
  184. // );
  185. // if (cpuTimeInfo.mRelUserTime < 0
  186. // || cpuTimeInfo.mRelSystemTime < 0
  187. // || cpuTimeInfo.mRelIdleTime < 0
  188. // || cpuTimeInfo.mRelIrqTime < 0)
  189. // {
  190. // LOGD("CPU%d,base U:%ld, S:%ld, I:%ld, Q:%ld\n",
  191. // (int)i,
  192. // cpuTimeInfo.mBaseUserTime,
  193. // cpuTimeInfo.mBaseSystemTime,
  194. // cpuTimeInfo.mBaseIdleTime,
  195. // cpuTimeInfo.mBaseIrqTime
  196. // );
  197. // }
  198. // }
  199. cpuTimeInfo.mBaseUserTime = usertime;
  200. cpuTimeInfo.mBaseSystemTime = systemtime;
  201. cpuTimeInfo.mBaseIoWaitTime = iowaittime;
  202. cpuTimeInfo.mBaseIrqTime = irqtime;
  203. cpuTimeInfo.mBaseSoftIrqTime = softirqtime;
  204. cpuTimeInfo.mBaseIdleTime = idletime;
  205. } else {
  206. // if (usertime < cpuTimeInfo.mBaseUserTime)
  207. // {
  208. // LOGD("ERROR: usertime: %ld, base: %ld", usertime, cpuTimeInfo.mBaseUserTime);
  209. // }
  210. //
  211. // if (systemtime < cpuTimeInfo.mBaseSystemTime)
  212. // {
  213. // LOGD("ERROR: systemtime: %ld, base: %ld", systemtime, cpuTimeInfo.mBaseSystemTime);
  214. // }
  215. //
  216. // if (iowaittime < cpuTimeInfo.mBaseIoWaitTime)
  217. // {
  218. // LOGD("ERROR: iowaittime: %ld, base: %ld", iowaittime, cpuTimeInfo.mBaseIoWaitTime);
  219. // }
  220. //
  221. // if (irqtime < cpuTimeInfo.mBaseIrqTime)
  222. // {
  223. // LOGD("ERROR: irqtime: %ld, base: %ld", irqtime, cpuTimeInfo.mBaseIrqTime);
  224. // }
  225. //
  226. // if (softirqtime < cpuTimeInfo.mBaseSoftIrqTime)
  227. // {
  228. // LOGD("ERROR: softirqtime: %ld, base: %ld", softirqtime, cpuTimeInfo.mBaseSoftIrqTime);
  229. // }
  230. //
  231. // if (idletime < cpuTimeInfo.mBaseIdleTime)
  232. // {
  233. // LOGD("ERROR: idletime: %ld, base: %ld", idletime, cpuTimeInfo.mBaseIdleTime);
  234. // }
  235. if (usertime > 0 || idletime > 0)
  236. {
  237. cpuTimeInfo.mBaseUserTime = usertime;
  238. cpuTimeInfo.mBaseSystemTime = systemtime;
  239. cpuTimeInfo.mBaseIoWaitTime = iowaittime;
  240. cpuTimeInfo.mBaseIrqTime = irqtime;
  241. cpuTimeInfo.mBaseSoftIrqTime = softirqtime;
  242. cpuTimeInfo.mBaseIdleTime = idletime;
  243. }
  244. cpuTimeInfo.mRelUserTime = 0;
  245. cpuTimeInfo.mRelSystemTime = 0;
  246. cpuTimeInfo.mRelIoWaitTime = 0;
  247. cpuTimeInfo.mRelIrqTime = 0;
  248. cpuTimeInfo.mRelSoftIrqTime = 0;
  249. cpuTimeInfo.mRelIdleTime = 0;
  250. LOGD("CPU: %d, /proc/stats has gone backwards; skipping CPU update\n", (int)i);
  251. }
  252. }
  253. }
  254. }
  255. static long printRatio(std::stringstream& ss, long numerator, long denominator) {
  256. long hundreds = 0;
  257. if (denominator > 0)
  258. {
  259. long thousands = (numerator*1000)/denominator;
  260. hundreds = thousands/10;
  261. ss << hundreds;
  262. if (hundreds < 10) {
  263. long remainder = thousands - (hundreds*10);
  264. if (remainder != 0) {
  265. ss << '.';
  266. ss << remainder;
  267. }
  268. }
  269. }
  270. else
  271. {
  272. ss << '0';
  273. }
  274. ss << " ";
  275. return hundreds;
  276. }
  277. static long printProcessCPU(std::stringstream& ss, long totalTime, long user)
  278. {
  279. return printRatio(ss, user, totalTime);
  280. }
  281. void ProcessCpuTracker::printCurrentState()
  282. {
  283. std::stringstream ss;
  284. long totalCpuUsage = 0;
  285. for (size_t i = 0, len = _cpuTimeInfos.size(); i < len; ++i)
  286. {
  287. CpuTimeInfo& cpuTimeInfo = _cpuTimeInfos[i];
  288. const long totalTime = cpuTimeInfo.mRelUserTime + cpuTimeInfo.mRelSystemTime + cpuTimeInfo.mRelIoWaitTime
  289. + cpuTimeInfo.mRelIrqTime + cpuTimeInfo.mRelSoftIrqTime + cpuTimeInfo.mRelIdleTime;
  290. // if (totalTime <= 0)
  291. // {
  292. // LOGD("cjh totalTime, i=%d: %ld mRelUserTime: %ld, mRelSystemTime: %ld, mRelIoWaitTime: %ld, mRelIrqTime: %ld, mRelSoftIrqTime: %ld, mRelIdleTime: %ld",
  293. // (int)i,
  294. // totalTime,
  295. // cpuTimeInfo.mRelUserTime,
  296. // cpuTimeInfo.mRelSystemTime,
  297. // cpuTimeInfo.mRelIoWaitTime,
  298. // cpuTimeInfo.mRelIrqTime,
  299. // cpuTimeInfo.mRelSoftIrqTime,
  300. // cpuTimeInfo.mRelIdleTime
  301. // );
  302. // }
  303. const long preCoreUsage = printProcessCPU(ss, totalTime, cpuTimeInfo.mRelUserTime);
  304. if (i > 0)
  305. {
  306. totalCpuUsage += preCoreUsage;
  307. }
  308. }
  309. ss << "T:";
  310. ss << totalCpuUsage;
  311. std::string str = ss.str();
  312. LOGD("CPU: %s", str.c_str());
  313. }