qappctrl.cpp 14 KB

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