qappctrl.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. QGfaAppCtrl::QGfaAppCtrl(QObject *pParent) : QObject(pParent),
  17. m_bSysInfoRunning(false),
  18. m_hAC(NULL),
  19. m_nAppID(0),
  20. m_nTimerID(0),
  21. m_sysInfo(this),
  22. m_curPass(0),
  23. m_minPass(LLONG_MAX),
  24. m_maxPass(LLONG_MIN),
  25. m_avgPass(0),
  26. m_nEvtSrcs(0),
  27. m_nHeavyLoadUpdateIntervalUs(2500000)
  28. {
  29. for(int i = 0; i < _APP_CTRL_MAX_SLOTS; ++i)
  30. {
  31. QGfaAppInfo *pai = new QGfaAppInfo(i, this);
  32. connect(pai, SIGNAL(sendControlMessage(appid_t, ctrlmsg_t)), SLOT(onSendControlMessage(appid_t, ctrlmsg_t)));
  33. m_appInfo.append(pai);
  34. }
  35. memset(&m_tsLastHeavyLoadUpdate, 0, sizeof(m_tsLastHeavyLoadUpdate));
  36. setObjectName("QGfaAppCtrl");
  37. }
  38. QGfaAppCtrl::~QGfaAppCtrl(void)
  39. {
  40. Release();
  41. for(int i = 0; i < _APP_CTRL_MAX_SLOTS; ++i)
  42. {
  43. QGfaAppInfo *pai = m_appInfo.at(i);
  44. delete pai;
  45. }
  46. }
  47. /////////////////////////////////////////////////////////////////////////////
  48. bool QGfaAppCtrl::Create(appid_t nAppID, const char *pszDisplayName, int nTimerIntMs, clock64_t nHeavyLoadUpdateIntervalMs)
  49. {
  50. if(!m_hAC)
  51. {
  52. if(nTimerIntMs < _MIN_TIMER_INT)
  53. nTimerIntMs = _MIN_TIMER_INT;
  54. if(nHeavyLoadUpdateIntervalMs < (nTimerIntMs * 50))
  55. nHeavyLoadUpdateIntervalMs = (nTimerIntMs * 50);
  56. if((m_hAC = ::GfaIpcAppCtrlAcquire(nAppID, pszDisplayName, 0, (clock64_t)(nTimerIntMs * 1000 * 2))))
  57. {
  58. m_nAppID = nAppID;
  59. m_nHeavyLoadUpdateIntervalUs = nHeavyLoadUpdateIntervalMs * 1000;
  60. ::GfaIpcAppCtrlSubscribeSysEvents(m_hAC, GFA_APPCTRL_SYSEVENT_ALL_STG_DEV);
  61. m_nTimerID = startTimer(nTimerIntMs, Qt::CoarseTimer);
  62. }
  63. }
  64. return !!m_hAC;
  65. }
  66. /////////////////////////////////////////////////////////////////////////////
  67. void QGfaAppCtrl::Release(void)
  68. {
  69. if(m_hAC)
  70. {
  71. killTimer(m_nTimerID);
  72. ::GfaIpcAppCtrlRelease(m_hAC);
  73. m_hAC = NULL;
  74. }
  75. }
  76. /////////////////////////////////////////////////////////////////////////////
  77. void QGfaAppCtrl::RegisterQmlTypes(QQmlEngine &rEng, int nVerMajor, int nVerMinor)
  78. {
  79. qmlRegisterUncreatableType<QGfaStgDevList>("com.gfa.ipc.appctrl", nVerMajor, nVerMinor, "QGfaStgDevList", QStringLiteral("class not creatable in QML"));
  80. qmlRegisterUncreatableType<QGfaSysInfo>("com.gfa.ipc.appctrl", nVerMajor, nVerMinor, "QGfaSysInfo", QStringLiteral("class not creatable in QML"));
  81. qmlRegisterUncreatableType<QGfaAppInfo>("com.gfa.ipc.appctrl", nVerMajor, nVerMinor, "QGfaAppInfo", QStringLiteral("class not creatable in QML"));
  82. qmlRegisterUncreatableType<QGfaAppCtrl>("com.gfa.ipc.appctrl", nVerMajor, nVerMinor, "QGfaAppCtrl", QStringLiteral("class not creatable in QML"));
  83. rEng.rootContext()->setContextProperty(QStringLiteral("qGfaAppCtrl"), this);
  84. }
  85. /////////////////////////////////////////////////////////////////////////////
  86. bool QGfaAppCtrl::PresetDisplayName(appid_t nAppID, const char *pszName)
  87. {
  88. if(m_hAC)
  89. return ::GfaIpcAppCtrlPresetDisplayName(m_hAC, nAppID, pszName);
  90. return false;
  91. }
  92. /////////////////////////////////////////////////////////////////////////////
  93. bool QGfaAppCtrl::SubscribeStateEvents(appid_t nAppMask)
  94. {
  95. if(m_hAC)
  96. {
  97. nAppMask &= ~m_nAppID;
  98. m_nEvtSrcs |= nAppMask;
  99. return ::GfaIpcAppCtrlSubscribeStateEvents(m_hAC, nAppMask);
  100. }
  101. return false;
  102. }
  103. /////////////////////////////////////////////////////////////////////////////
  104. void QGfaAppCtrl::timerEvent(QTimerEvent *event)
  105. {
  106. if((event->timerId() == m_nTimerID) && m_hAC)
  107. {
  108. static clock64_t nCurPass = 0;
  109. struct timespec tsEnterUpdate, tsLeaveUpdate;
  110. ::clock_gettime(CLOCK_MONOTONIC, &tsEnterUpdate);
  111. GFA_APPCTRL_SYSMEM sm;
  112. HAPPINFO hAI;
  113. bool bDoHeavyLoadUpdate = (_TIMESPEC_DIFF_US(tsEnterUpdate, m_tsLastHeavyLoadUpdate) >= m_nHeavyLoadUpdateIntervalUs);
  114. if((hAI = ::GfaIpcAppCtrlInfoUpdate(m_hAC, nCurPass)))
  115. {
  116. int b;
  117. appid_t app, nAppIdSrc;
  118. sysevt_t nSysEvt;
  119. GFA_APPCTRL_APPTIMES at;
  120. GFA_APPCTRL_APPMEM am;
  121. GFA_SYSINFO_DISK disk;
  122. GFA_SYSINFO_PARTITION part;
  123. char szDispName[128];
  124. while((nAppIdSrc = ::GfaIpcAppCtrlGetNextStateEvtSrc(hAI)))
  125. {
  126. GfaIpcAppStates state = ::GfaIpcAppCtrlGetState(m_hAC, nAppIdSrc);
  127. int nIndex = appIndexFromAppID(nAppIdSrc);
  128. QGfaAppInfo &ai = *m_appInfo[nIndex];
  129. bool bStateChanged = ai.setState(state);
  130. if(bStateChanged)
  131. {
  132. if(state == GIAS_Running)
  133. memset(&m_tsLastHeavyLoadUpdate, 0, sizeof(m_tsLastHeavyLoadUpdate));
  134. else if(state != GIAS_Paused)
  135. {
  136. ai.setAppTimes(NULL, state);
  137. ai.setAppMemInfo(NULL, state);
  138. }
  139. if(nAppIdSrc == GFA_APPCTRL_APPID_SYSINFO)
  140. {
  141. m_bSysInfoRunning = (state == GIAS_Running);
  142. m_sysInfo.setSysInfoRunning(m_bSysInfoRunning);
  143. }
  144. }
  145. if((state >= GIAS_StateNotRunning) && (state <= GIAS_Paused))
  146. {
  147. if(::GfaIpcAppCtrlGetDisplayName(m_hAC, nAppIdSrc, szDispName, sizeof(szDispName)))
  148. {
  149. ai.setName(szDispName);
  150. }
  151. }
  152. }
  153. if(m_bSysInfoRunning)
  154. {
  155. while((nSysEvt = ::GfaIpcAppCtrlGetNextSysEvt(hAI)))
  156. {
  157. int nIndex;
  158. switch(nSysEvt)
  159. {
  160. case GFA_APPCTRL_SYSEVENT_DISK_EVT:
  161. while((nIndex = ::GfaIpcAppCtrlGetNextDiskRemoved(m_hAC, &disk)) >= 0)
  162. {
  163. m_sysInfo.diskRemoved(nIndex, disk);
  164. }
  165. while((nIndex = ::GfaIpcAppCtrlGetNextDiskAdded(m_hAC, &disk)) >= 0)
  166. {
  167. m_sysInfo.diskAdded(nIndex, disk);
  168. }
  169. break;
  170. case GFA_APPCTRL_SYSEVENT_PART_EVT:
  171. while((nIndex = ::GfaIpcAppCtrlGetNextPartitionRemoved(m_hAC, &part)) >= 0)
  172. {
  173. m_sysInfo.partitionRemoved(nIndex, part);
  174. }
  175. while((nIndex = ::GfaIpcAppCtrlGetNextPartitionAdded(m_hAC, &part)) >= 0)
  176. {
  177. m_sysInfo.partitionAdded(nIndex, part);
  178. }
  179. break;
  180. case GFA_APPCTRL_SYSEVENT_MOUNT_EVT:
  181. while((nIndex = ::GfaIpcAppCtrlGetNextMountRemoved(m_hAC, &part)) >= 0)
  182. {
  183. m_sysInfo.mountRemoved(nIndex, part);
  184. }
  185. while((nIndex = ::GfaIpcAppCtrlGetNextMountAdded(m_hAC, &part)) >= 0)
  186. {
  187. m_sysInfo.mountAdded(nIndex, part);
  188. }
  189. break;
  190. }
  191. }
  192. }
  193. nAppIdSrc = m_nEvtSrcs;
  194. bool bDontSaveLast = false;
  195. while(nAppIdSrc)
  196. {
  197. b = ffsll(nAppIdSrc) - 1;
  198. app = ((appid_t)0x1 << b);
  199. nAppIdSrc &= ~app;
  200. QGfaAppInfo &ai = *m_appInfo[b];
  201. GfaIpcAppStates state = ::GfaIpcAppCtrlGetState(m_hAC, app);
  202. if(::GfaIpcAppCtrlGetAppMem(m_hAC, app, &am))
  203. {
  204. ai.setAppMemInfo(&am, state, bDoHeavyLoadUpdate);
  205. }
  206. if(::GfaIpcAppCtrlGetAppTimes(m_hAC, app, &at) >= 0)
  207. {
  208. if(!at.nCycleLastUs)
  209. {
  210. bDontSaveLast = true;
  211. continue;
  212. }
  213. ai.setAppTimes(&at, state, bDoHeavyLoadUpdate);
  214. }
  215. size_t nSize = ::GfaIpcAppCtrlGetAppSize(m_hAC, app);
  216. ai.setAppSize((quint32)nSize, state);
  217. }
  218. if(bDoHeavyLoadUpdate && !bDontSaveLast)
  219. m_tsLastHeavyLoadUpdate = tsEnterUpdate;
  220. }
  221. if(::GfaIpcAppCtrlGetSysMem(m_hAC, &sm))
  222. {
  223. m_sysInfo.setSysMemInfo(&sm, bDoHeavyLoadUpdate);
  224. }
  225. ::clock_gettime(CLOCK_MONOTONIC, &tsLeaveUpdate);
  226. m_curPass = nCurPass = _TIMESPEC_DIFF_US(tsLeaveUpdate, tsEnterUpdate);
  227. if(m_minPass > m_curPass)
  228. {
  229. m_minPass = m_curPass;
  230. emit minPassChanged(m_minPass);
  231. }
  232. if(m_maxPass < m_curPass)
  233. {
  234. m_maxPass = m_curPass;
  235. emit maxPassChanged(m_maxPass);
  236. }
  237. if(!m_avgPass)
  238. {
  239. m_avgPass = m_curPass;
  240. emit avgPassChanged(m_avgPass);
  241. }
  242. else
  243. {
  244. static uint64_t nPasses = 0;
  245. ++nPasses;
  246. clock64_t avgPass = (m_avgPass + m_curPass) / 2;
  247. if(m_avgPass != avgPass)
  248. {
  249. m_avgPass = avgPass;
  250. if(!(nPasses % 50))
  251. emit avgPassChanged(m_avgPass);
  252. }
  253. }
  254. }
  255. }
  256. /////////////////////////////////////////////////////////////////////////////
  257. void QGfaAppCtrl::onSendControlMessage(appid_t nAppID, ctrlmsg_t msg)
  258. {
  259. if(m_hAC)
  260. ::GfaIpcAppCtrlSendCtrlMsg(m_hAC, nAppID, msg);
  261. }
  262. /////////////////////////////////////////////////////////////////////////////
  263. int QGfaAppCtrl::SetState(int nState)
  264. {
  265. if(m_hAC)
  266. ::GfaIpcAppCtrlSetState(m_hAC, (GfaIpcAppStates)nState);
  267. return GIAS_Invalid;
  268. }
  269. /////////////////////////////////////////////////////////////////////////////
  270. quint64 QGfaAppCtrl::minPass(void) const
  271. {
  272. return m_minPass;
  273. }
  274. quint64 QGfaAppCtrl::maxPass(void) const
  275. {
  276. return m_maxPass;
  277. }
  278. quint64 QGfaAppCtrl::avgPass(void) const
  279. {
  280. return m_avgPass;
  281. }
  282. QGfaSysInfo* QGfaAppCtrl::sysInfo(void)
  283. {
  284. return &m_sysInfo;
  285. }
  286. /////////////////////////////////////////////////////////////////////////////
  287. int QGfaAppCtrl::appIndexFromAppID(quint64 nAppID)
  288. {
  289. if(_IS_VALID_APP_ID(nAppID))
  290. return _APP_INDEX_FROM_APP_ID(nAppID);
  291. return _INVALID_SLOT_INDEX;
  292. }
  293. /////////////////////////////////////////////////////////////////////////////
  294. QQmlListProperty<QGfaAppInfo> QGfaAppCtrl::appInfo(void)
  295. {
  296. return QQmlListProperty<QGfaAppInfo>(this, m_appInfo);
  297. }