qappctrl.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. #include <QTimerEvent>
  2. #include "qappctrl.h"
  3. #include "../../src/defines.h"
  4. /////////////////////////////////////////////////////////////////////////////
  5. #define _INVALID_SLOT_INDEX -1
  6. #define _APP_CTRL_MAX_SLOTS ((int)(sizeof(appid_t) * 8))
  7. #define _APP_INDEX_FROM_APP_ID(aid) (ffsll(aid) - 1)
  8. #define _IS_POWER_OF_2(x) (!!(x) && !((x) & ((x) - 1)))
  9. #define _IS_VALID_APP_ID(i) _IS_POWER_OF_2(i)
  10. #define _TIMESPEC_2_US(ts) (((clock64_t)(ts).tv_sec) * 1000000LL + ((clock64_t)(ts).tv_nsec) / 1000LL)
  11. #define _TIMESPEC_DIFF(ts1, ts2) (_TIMESPEC_2_US(ts1) - _TIMESPEC_2_US(ts2))
  12. #define _MIN_TIMER_INT 20
  13. /////////////////////////////////////////////////////////////////////////////
  14. /////////////////////////////////////////////////////////////////////////////
  15. /////////////////////////////////////////////////////////////////////////////
  16. QGfaAppInfo::QGfaAppInfo(int nIndex, QObject *pParent) : QObject(pParent),
  17. m_state(GIAS_StateNotRunning),
  18. m_nIndex(nIndex),
  19. m_cycCur(0),
  20. m_cycMin(0),
  21. m_cycMax(0),
  22. m_upTime(0),
  23. m_cpuTime(0.0),
  24. m_cpuPercCur(0.0),
  25. m_cpuPercAvg(0.0)
  26. {
  27. m_nAppID = 1ull << m_nIndex;
  28. setStateText(m_state);
  29. }
  30. QGfaAppInfo::~QGfaAppInfo(void)
  31. {
  32. this->disconnect();
  33. }
  34. /////////////////////////////////////////////////////////////////////////////
  35. int QGfaAppInfo::state(void) const
  36. {
  37. return m_state;
  38. }
  39. bool QGfaAppInfo::setState(int val)
  40. {
  41. if(m_state != val)
  42. {
  43. m_state = val;
  44. emit stateChanged(val);
  45. setStateText(val);
  46. return true;
  47. }
  48. return false;
  49. }
  50. /////////////////////////////////////////////////////////////////////////////
  51. QString QGfaAppInfo::stateText(void) const
  52. {
  53. return m_stateText;
  54. }
  55. void QGfaAppInfo::setStateText(int val)
  56. {
  57. QString sval = ::GfaIpcAppCtrlGetStateText((GfaIpcAppStates)val);
  58. if(m_stateText != sval)
  59. {
  60. m_stateText = sval;
  61. emit stateTextChanged(sval);
  62. }
  63. }
  64. /////////////////////////////////////////////////////////////////////////////
  65. QString QGfaAppInfo::name(void) const
  66. {
  67. return m_name;
  68. }
  69. void QGfaAppInfo::setName(const QString &val)
  70. {
  71. if(m_name != val)
  72. {
  73. m_name = val;
  74. emit nameChanged(val);
  75. }
  76. }
  77. /////////////////////////////////////////////////////////////////////////////
  78. quint64 QGfaAppInfo::cycCur(void) const
  79. {
  80. return m_cycCur;
  81. }
  82. quint64 QGfaAppInfo::cycMin(void) const
  83. {
  84. return m_cycMin;
  85. }
  86. quint64 QGfaAppInfo::cycMax(void) const
  87. {
  88. return m_cycMax;
  89. }
  90. quint64 QGfaAppInfo::upTime(void) const
  91. {
  92. return m_upTime;
  93. }
  94. double QGfaAppInfo::cpuTime(void) const
  95. {
  96. return m_cpuTime;
  97. }
  98. double QGfaAppInfo::cpuCur(void) const
  99. {
  100. return m_cpuPercCur;
  101. }
  102. double QGfaAppInfo::cpuAvg(void) const
  103. {
  104. return m_cpuPercAvg;
  105. }
  106. void QGfaAppInfo::setAppTimes(LPCGFA_APPCTRL_APPTIMES pat, GfaIpcAppStates state, bool bDoHeavyLoadUpdate)
  107. {
  108. if(pat)
  109. {
  110. if(state == GIAS_Running)
  111. {
  112. if(m_cycMin != (quint64)pat->nCycleMinUs)
  113. {
  114. m_cycMin = (quint64)pat->nCycleMinUs;
  115. emit cycMinChanged(m_cycMin);
  116. }
  117. if(m_cycMax != (quint64)pat->nCycleMaxUs)
  118. {
  119. m_cycMax = (quint64)pat->nCycleMaxUs;
  120. emit cycMaxChanged(m_cycMax);
  121. }
  122. }
  123. else
  124. {
  125. if(m_cycCur != 0)
  126. {
  127. m_cycCur = 0;
  128. emit cycCurChanged(m_cycCur);
  129. }
  130. }
  131. if(bDoHeavyLoadUpdate)
  132. {
  133. quint64 upTime = time(NULL) - pat->nTsStart;
  134. if(m_upTime != upTime)
  135. {
  136. m_upTime = upTime;
  137. emit upTimeChanged(m_upTime);
  138. }
  139. if(state == GIAS_Running)
  140. {
  141. if(m_cycCur != (quint64)pat->nCycleLastUs)
  142. {
  143. m_cycCur = (quint64)pat->nCycleLastUs;
  144. emit cycCurChanged(m_cycCur);
  145. }
  146. }
  147. if(state != GIAS_Hanging)
  148. {
  149. if(m_cpuTime != pat->fCpuTime)
  150. {
  151. m_cpuTime = pat->fCpuTime;
  152. emit cpuTimeChanged(m_cpuTime);
  153. }
  154. if(m_cpuPercCur != pat->fCpuCur)
  155. {
  156. m_cpuPercCur = pat->fCpuCur;
  157. emit cpuCurChanged(m_cpuPercCur);
  158. }
  159. if(m_cpuPercAvg != pat->fCpuAvg)
  160. {
  161. m_cpuPercAvg = pat->fCpuAvg;
  162. emit cpuAvgChanged(m_cpuPercAvg);
  163. }
  164. }
  165. }
  166. }
  167. else
  168. {
  169. if(m_cycCur != 0)
  170. {
  171. m_cycCur = 0;
  172. emit cycCurChanged(m_cycCur);
  173. }
  174. if(state == GIAS_Hanging)
  175. {
  176. if(m_cpuTime != -1)
  177. {
  178. m_cpuTime = -1;
  179. emit cpuTimeChanged(m_cpuTime);
  180. }
  181. if(m_cpuPercCur != -1)
  182. {
  183. m_cpuPercCur = -1;
  184. emit cpuCurChanged(m_cpuPercCur);
  185. }
  186. if(m_cpuPercAvg != -1)
  187. {
  188. m_cpuPercAvg = -1;
  189. emit cpuAvgChanged(m_cpuPercAvg);
  190. }
  191. }
  192. else
  193. {
  194. if(m_cycMin != 0)
  195. {
  196. m_cycMin = 0;
  197. emit cycMinChanged(m_cycMin);
  198. }
  199. if(m_cycMax != 0)
  200. {
  201. m_cycMax = 0;
  202. emit cycMaxChanged(m_cycMax);
  203. }
  204. if(m_upTime != 0)
  205. {
  206. m_upTime = 0;
  207. emit upTimeChanged(m_upTime);
  208. }
  209. if(m_cpuTime != 0)
  210. {
  211. m_cpuTime = 0;
  212. emit cpuTimeChanged(m_cpuTime);
  213. }
  214. if(m_cpuPercCur != 0)
  215. {
  216. m_cpuPercCur = 0;
  217. emit cpuCurChanged(m_cpuPercCur);
  218. }
  219. if(m_cpuPercAvg != 0)
  220. {
  221. m_cpuPercAvg = 0;
  222. emit cpuAvgChanged(m_cpuPercAvg);
  223. }
  224. }
  225. }
  226. }
  227. /////////////////////////////////////////////////////////////////////////////
  228. bool QGfaAppInfo::pause(void)
  229. {
  230. emit sendControlMessage(m_nAppID, GFA_APPCTRL_CTRLMSG_PAUSE);
  231. return true;
  232. }
  233. bool QGfaAppInfo::resume(void)
  234. {
  235. emit sendControlMessage(m_nAppID, GFA_APPCTRL_CTRLMSG_RESUME);
  236. return true;
  237. }
  238. bool QGfaAppInfo::stop(void)
  239. {
  240. emit sendControlMessage(m_nAppID, GFA_APPCTRL_CTRLMSG_STOP);
  241. return true;
  242. }
  243. /////////////////////////////////////////////////////////////////////////////
  244. /////////////////////////////////////////////////////////////////////////////
  245. /////////////////////////////////////////////////////////////////////////////
  246. QGfaAppCtrl::QGfaAppCtrl(QObject *pParent) : QObject(pParent),
  247. m_hAC(NULL),
  248. m_nAppID(0),
  249. m_nTimerID(0),
  250. m_curPass(0),
  251. m_minPass(LLONG_MAX),
  252. m_maxPass(LLONG_MIN),
  253. m_avgPass(0),
  254. m_nEvtSrcs(0),
  255. m_nHeavyLoadUpdateIntervalUs(2500000)
  256. {
  257. for(int i = 0; i < _APP_CTRL_MAX_SLOTS; ++i)
  258. {
  259. QGfaAppInfo *pai = new QGfaAppInfo(i, this);
  260. connect(pai, SIGNAL(sendControlMessage(appid_t, ctrlmsg_t)), SLOT(onSendControlMessage(appid_t, ctrlmsg_t)));
  261. m_appInfo.append(pai);
  262. }
  263. memset(&m_tsLastHeavyLoadUpdate, 0, sizeof(m_tsLastHeavyLoadUpdate));
  264. }
  265. QGfaAppCtrl::~QGfaAppCtrl(void)
  266. {
  267. Release();
  268. for(int i = 0; i < _APP_CTRL_MAX_SLOTS; ++i)
  269. {
  270. QGfaAppInfo *pai = m_appInfo.at(i);
  271. delete pai;
  272. }
  273. }
  274. /////////////////////////////////////////////////////////////////////////////
  275. bool QGfaAppCtrl::Create(appid_t nAppID, const char *pszDisplayName, int nTimerIntMs, clock64_t nHeavyLoadUpdateIntervalMs)
  276. {
  277. if(!m_hAC)
  278. {
  279. if(nTimerIntMs < _MIN_TIMER_INT)
  280. nTimerIntMs = _MIN_TIMER_INT;
  281. if(nHeavyLoadUpdateIntervalMs < (nTimerIntMs * 50))
  282. nHeavyLoadUpdateIntervalMs = (nTimerIntMs * 50);
  283. if((m_hAC = ::GfaIpcAppCtrlAcquire(nAppID, pszDisplayName, (clock64_t)(nTimerIntMs * 1000 * 2))))
  284. {
  285. m_nAppID = nAppID;
  286. m_nHeavyLoadUpdateIntervalUs = nHeavyLoadUpdateIntervalMs * 1000;
  287. m_nTimerID = startTimer(nTimerIntMs, Qt::CoarseTimer);
  288. }
  289. }
  290. return !!m_hAC;
  291. }
  292. /////////////////////////////////////////////////////////////////////////////
  293. void QGfaAppCtrl::Release(void)
  294. {
  295. if(m_hAC)
  296. {
  297. killTimer(m_nTimerID);
  298. ::GfaIpcAppCtrlRelease(m_hAC);
  299. m_hAC = NULL;
  300. }
  301. }
  302. /////////////////////////////////////////////////////////////////////////////
  303. void QGfaAppCtrl::RegisterQmlTypes(QQmlEngine &rEng, int nVerMajor, int nVerMinor)
  304. {
  305. qmlRegisterUncreatableType<QGfaAppInfo>("com.gfa.ipc.appctrl", nVerMajor, nVerMinor, "QGfaAppInfo", QStringLiteral("class not creatable in QML"));
  306. qmlRegisterUncreatableType<QGfaAppCtrl>("com.gfa.ipc.appctrl", nVerMajor, nVerMinor, "QGfaAppCtrl", QStringLiteral("class not creatable in QML"));
  307. rEng.rootContext()->setContextProperty(QStringLiteral("qGfaAppCtrl"), this);
  308. }
  309. /////////////////////////////////////////////////////////////////////////////
  310. bool QGfaAppCtrl::PresetDisplayName(appid_t nAppID, const char *pszName)
  311. {
  312. if(m_hAC)
  313. return ::GfaIpcAppCtrlPresetDisplayName(m_hAC, nAppID, pszName);
  314. return false;
  315. }
  316. /////////////////////////////////////////////////////////////////////////////
  317. bool QGfaAppCtrl::SubscribeStateEvents(appid_t nAppMask)
  318. {
  319. if(m_hAC)
  320. {
  321. nAppMask &= ~m_nAppID;
  322. m_nEvtSrcs |= nAppMask;
  323. return ::GfaIpcAppCtrlSubscribeStateEvents(m_hAC, nAppMask);
  324. }
  325. return false;
  326. }
  327. /////////////////////////////////////////////////////////////////////////////
  328. void QGfaAppCtrl::timerEvent(QTimerEvent *event)
  329. {
  330. struct timespec tsEnterUpdate, tsLeaveUpdate;
  331. ::clock_gettime(CLOCK_MONOTONIC, &tsEnterUpdate);
  332. if((event->timerId() == m_nTimerID) && m_hAC)
  333. {
  334. HAPPINFO hAI;
  335. if((hAI = ::GfaIpcAppCtrlInfoUpdate(m_hAC)))
  336. {
  337. int b;
  338. appid_t app, nAppIdSrc;
  339. GFA_APPCTRL_APPTIMES at;
  340. char szDispName[128];
  341. #if 0
  342. ctrlmsg_t nCtrlMsg;
  343. while((nCtrlMsg = ::GfaIpcAppCtrlGetNextCtrlMsg(hAI)))
  344. {
  345. switch(nCtrlMsg)
  346. {
  347. case GFA_APPCTRL_CTRLMSG_STOP:
  348. break;
  349. case GFA_APPCTRL_CTRLMSG_PAUSE:
  350. break;
  351. case GFA_APPCTRL_CTRLMSG_RESUME:
  352. break;
  353. default:
  354. break;
  355. }
  356. }
  357. #endif
  358. while((nAppIdSrc = ::GfaIpcAppCtrlGetNextStateEvtSrc(hAI)))
  359. {
  360. GfaIpcAppStates state = ::GfaIpcAppCtrlGetState(m_hAC, nAppIdSrc);
  361. int nIndex = appIndexFromAppID(nAppIdSrc);
  362. QGfaAppInfo &ai = *m_appInfo[nIndex];
  363. bool bStateChanged = ai.setState(state);
  364. if(bStateChanged)
  365. {
  366. if(state == GIAS_Running)
  367. memset(&m_tsLastHeavyLoadUpdate, 0, sizeof(m_tsLastHeavyLoadUpdate));
  368. else if(state != GIAS_Paused)
  369. ai.setAppTimes(NULL, state);
  370. }
  371. if((state >= GIAS_StateNotRunning) && (state <= GIAS_Paused))
  372. {
  373. if(::GfaIpcAppCtrlGetDisplayName(m_hAC, nAppIdSrc, szDispName, sizeof(szDispName)))
  374. {
  375. ai.setName(szDispName);
  376. }
  377. }
  378. }
  379. nAppIdSrc = m_nEvtSrcs;
  380. bool bDontSaveLast = false, bUpdate = (_TIMESPEC_DIFF(tsEnterUpdate, m_tsLastHeavyLoadUpdate) >= m_nHeavyLoadUpdateIntervalUs);
  381. while(nAppIdSrc)
  382. {
  383. b = ffsll(nAppIdSrc) - 1;
  384. app = ((appid_t)0x1 << b);
  385. nAppIdSrc &= ~app;
  386. GfaIpcAppStates state = ::GfaIpcAppCtrlGetState(m_hAC, app);
  387. if(::GfaIpcAppCtrlGetAppTimes(m_hAC, app, &at) >= 0)
  388. {
  389. if(!at.nCycleLastUs)
  390. {
  391. bDontSaveLast = true;
  392. continue;
  393. }
  394. QGfaAppInfo &ai = *m_appInfo[b];
  395. ai.setAppTimes(&at, state, bUpdate);
  396. }
  397. }
  398. if(bUpdate && !bDontSaveLast)
  399. m_tsLastHeavyLoadUpdate = tsEnterUpdate;
  400. }
  401. }
  402. ::clock_gettime(CLOCK_MONOTONIC, &tsLeaveUpdate);
  403. m_curPass = _TIMESPEC_DIFF(tsLeaveUpdate, tsEnterUpdate);
  404. if(m_minPass > m_curPass)
  405. {
  406. m_minPass = m_curPass;
  407. emit minPassChanged(m_minPass);
  408. }
  409. if(m_maxPass < m_curPass)
  410. {
  411. m_maxPass = m_curPass;
  412. emit maxPassChanged(m_maxPass);
  413. }
  414. if(!m_avgPass)
  415. {
  416. m_avgPass = m_curPass;
  417. emit avgPassChanged(m_avgPass);
  418. }
  419. else
  420. {
  421. static uint64_t nPasses = 0;
  422. ++nPasses;
  423. // clock64_t avgPass = (m_avgPass + m_curPass) / 2;
  424. clock64_t avgPass = (clock64_t)sqrt(double((m_avgPass * m_avgPass + m_curPass * m_curPass) / 2));
  425. if(m_avgPass != avgPass)
  426. {
  427. m_avgPass = avgPass;
  428. if(!(nPasses % 50))
  429. emit avgPassChanged(m_avgPass);
  430. }
  431. }
  432. }
  433. /////////////////////////////////////////////////////////////////////////////
  434. void QGfaAppCtrl::onSendControlMessage(appid_t nAppID, ctrlmsg_t msg)
  435. {
  436. if(m_hAC)
  437. ::GfaIpcAppCtrlSendCtrlMsg(m_hAC, nAppID, msg);
  438. }
  439. /////////////////////////////////////////////////////////////////////////////
  440. int QGfaAppCtrl::SetState(int nState)
  441. {
  442. if(m_hAC)
  443. ::GfaIpcAppCtrlSetState(m_hAC, (GfaIpcAppStates)nState);
  444. return GIAS_Invalid;
  445. }
  446. /////////////////////////////////////////////////////////////////////////////
  447. quint64 QGfaAppCtrl::minPass(void) const
  448. {
  449. return m_minPass;
  450. }
  451. quint64 QGfaAppCtrl::maxPass(void) const
  452. {
  453. return m_maxPass;
  454. }
  455. quint64 QGfaAppCtrl::avgPass(void) const
  456. {
  457. return m_avgPass;
  458. }
  459. /////////////////////////////////////////////////////////////////////////////
  460. int QGfaAppCtrl::appIndexFromAppID(quint64 nAppID)
  461. {
  462. if(_IS_VALID_APP_ID(nAppID))
  463. return _APP_INDEX_FROM_APP_ID(nAppID);
  464. return _INVALID_SLOT_INDEX;
  465. }
  466. /////////////////////////////////////////////////////////////////////////////
  467. QQmlListProperty<QGfaAppInfo> QGfaAppCtrl::appInfo(void)
  468. {
  469. return QQmlListProperty<QGfaAppInfo>(this, m_appInfo);
  470. }