SelfCheckerFSM.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #ifndef __SELFCHECKER_FSM_H
  2. #define __SELFCHECKER_FSM_H
  3. #pragma once
  4. #include "stdafx.h"
  5. #define WIN32_LEAN_AND_MEAN
  6. #include "tinyxml2.h"
  7. #include "CommEntityUtil.hpp"
  8. #include <cerrno>
  9. #include <cstdlib>
  10. #include <cstring>
  11. #include <ctime>
  12. #ifdef RVC_OS_WIN
  13. #include <windows.h>
  14. #include <Psapi.h>
  15. #else
  16. #endif //RVC_OS_WIN
  17. #include <fstream>
  18. #include <string>
  19. #include <iostream>
  20. #include <map>
  21. #include <vector>
  22. #include "SpFSM.h"
  23. #include "SpHelper.h"
  24. using namespace tinyxml2;
  25. using namespace std;
  26. const int MAX_KEY_VALUE_SIZE = 1024;
  27. class CSelfCheckerEntity;
  28. class CSelfCheckerFSM;
  29. #define SelfChecker_UserErrorCode_Start 0x50500200
  30. #define SelfChecker_UserErrorCode_EntityLost_Start 0x50500300
  31. enum EvtType
  32. {
  33. USER_EVT_TEST = EVT_USER+1,
  34. USER_EVT_QUIT,
  35. USER_EVT_INIT,
  36. };
  37. enum CheckPattern
  38. {
  39. CHECK_UNKNOWN,
  40. CHECK_HANDSHAKE,
  41. CHECK_EXAMINE,
  42. CHECK_RESET,
  43. CHECK_RESTART,
  44. };
  45. enum TestActionEnum
  46. {
  47. ACTION_NOTHINGTODO,
  48. ACTION_HANDSHAKE,
  49. ACTION_EXAMINE,
  50. ACTION_RESET,
  51. ACTION_CLOSE,
  52. ACTION_ENTITY_RESTART,
  53. ACTION_OS_RESTART,
  54. ACTION_POWER_RESTART,
  55. };
  56. struct callback_entry : public IReleasable
  57. {
  58. virtual ~callback_entry() {}
  59. CSimpleStringA EntityName;
  60. union {
  61. void *pRawData;
  62. int state;
  63. };
  64. int op;
  65. ErrorCodeEnum ErrorResult;
  66. };
  67. struct ErrorCfgInfo
  68. {
  69. //CheckPattern eType;
  70. ErrorCodeEnum eErrCode;
  71. TestActionEnum eAction;
  72. };
  73. struct EntityCfgInfo
  74. {
  75. //int resetCount;
  76. int entityRestartCount;
  77. int osRestartCount;
  78. int powerRestartCount;
  79. bool bWaitRestart;
  80. map<ErrorCodeEnum,TestActionEnum> hsInfo;//handshake
  81. map<ErrorCodeEnum,TestActionEnum> examInfo;//examine
  82. map<ErrorCodeEnum,TestActionEnum> resetInfo;//reset
  83. map<ErrorCodeEnum,TestActionEnum> restartInfo;//restart
  84. };
  85. struct EntityRunInfo
  86. {
  87. bool bGetLoadOpt;
  88. bool bRestarting;
  89. int eState;
  90. int eTest;
  91. int loadOpt;
  92. int memoryHighCount;//´Ë±äÁ¿·ÏÆúʹÓÃ
  93. int cpuRatio;
  94. FILETIME prevSysKernel, prevSysUser;
  95. FILETIME prevProcKernel, prevProcUser;
  96. };
  97. struct ProcItem
  98. {
  99. DWORD code;
  100. DWORD action;
  101. int upgradecount;
  102. DWORD upgradeaction;
  103. int upgradetime;
  104. vector<UINT64>proctime;
  105. };
  106. struct EntityCfg
  107. {
  108. vector<ProcItem> vShake;
  109. vector<ProcItem> vWarn;
  110. };
  111. struct EntityNameCompare
  112. {
  113. public:
  114. bool operator()(const std::string x, const std::string y)
  115. {
  116. return x.compare(y) < 0;
  117. }
  118. };
  119. enum ProcType
  120. {
  121. ProcType_None = 0,
  122. ProcType_Shake,
  123. ProcType_Warn,
  124. };
  125. class CSelfCheckerFSM : public FSMImpl<CSelfCheckerFSM>,public ICallbackListener
  126. {
  127. public:
  128. enum {s0,s1,s2,s3};
  129. BEGIN_FSM_STATE(CSelfCheckerFSM)
  130. FSM_STATE_ENTRY(s0, "Init", s0_on_entry, s0_on_exit, s0_on_event)
  131. FSM_STATE_ENTRY(s1, "Normal", s1_on_entry, s1_on_exit, s1_on_event)
  132. FSM_STATE_ENTRY(s2, "Check", s2_on_entry, s2_on_exit, s2_on_event)
  133. FSM_STATE_ENTRY(s3, "SingleCheck", s3_on_entry, s3_on_exit, s3_on_event)
  134. END_FSM_STATE()
  135. BEGIN_FSM_RULE(CSelfCheckerFSM, s0)
  136. FSM_RULE_ENTRY(s0, s1, USER_EVT_INIT, 0)
  137. END_FSM_RULE()
  138. CSelfCheckerFSM() :m_warmLevel(0), m_bFirstCalcCpu(true), m_diskLastWarnHour(1)
  139. , m_bEverInMainPage(false), m_bHaveGetEntityList(false), m_bFWBEntityAdd(false){}
  140. ~CSelfCheckerFSM(){}
  141. virtual ErrorCodeEnum OnInit();
  142. virtual ErrorCodeEnum OnExit();
  143. void s0_on_entry();
  144. void s0_on_exit();
  145. unsigned int s0_on_event(FSMEvent* event);
  146. void s1_on_entry();
  147. void s1_on_exit();
  148. unsigned int s1_on_event(FSMEvent* event);
  149. void s2_on_entry();
  150. void s2_on_exit();
  151. unsigned int s2_on_event(FSMEvent* event);
  152. void s3_on_entry();
  153. void s3_on_exit();
  154. unsigned int s3_on_event(FSMEvent* event);
  155. //ErrorCodeEnum AddEntity(const char *pszEntityName);
  156. //ErrorCodeEnum RemoveEntity(const char *pszEntityName);
  157. ErrorCodeEnum ExceptionErrorProcess(const char *pszEntityName,ErrorCodeEnum eErrCode);
  158. ErrorCodeEnum ExceptionErrorProcessXml(ProcType eType, const char *pszEntityName, DWORD dwAction, bool bDefault = false, const char *pszMessage="");
  159. void DoOnCreated(const char *pszEntityName,ErrorCodeEnum eOnStartErrorCode,const char *pszCallerEntity);
  160. void DoOnClosed(const char *pszEntityName,EntityCloseCauseEnum eCloseCause,ErrorCodeEnum eOnCloseErrorCode,const char *pszCallerEntity);
  161. void DoOnException(const char *pszEntityName,const char *pszFunctionName,EntityStateEnum eState,EntityStateEnum eLastState,ErrorCodeEnum eErrorCode);
  162. ErrorCodeEnum CheckEntity(const char* pszEntityName, EntityTestEnum eTestType, bool bNormalCheck = true);
  163. int AddEntityState(const char *pszEntityName,EntityStateEnum eState);
  164. ErrorCodeEnum GetEntityErrorList(int &warmLevel,CSimpleStringA &strList);
  165. void SetEntityCfgInfo(const char *pszEntityName,int loadOpt){m_entRunInfo[pszEntityName].loadOpt = loadOpt;};
  166. void SetEntityRestarting(const char* pszEntityName,bool flag) { m_entRunInfo[pszEntityName].bRestarting = flag;}
  167. int Proc(string entity, ProcType eType, DWORD dwCode,const char *pszMessage="");
  168. void SetEverEnterMainPageFlag(bool bValue = true);
  169. void AddFWBEntityToList();
  170. void GetEntityList(CSimpleStringA csList);
  171. private:
  172. ErrorCodeEnum Initial();
  173. void OnNormalWorkTimerout(void *pData);
  174. virtual void OnAnswer(CSmartPointer<IAsynWaitSp> pAsynWaitSp);
  175. void LogErrInfo(const char* msgHead,const char* msgBody,const int errCode);
  176. void LogActionProcess(const char *pszEntityName,ErrorCodeEnum errCode,TestActionEnum eAct);
  177. void UpgradeActionProcess(const char *pszEntityName, const char *pAction);
  178. void CheckEntityResouce(const char *pszEntityName,CEntityRunInfo &Info);
  179. //bool IsKeyEntity(const char *pszEntityName);
  180. bool ReadXmlFile(const char *szFileName);
  181. private:
  182. map<CSimpleStringA,EntityCfgInfo> m_entCfgInfo;
  183. map<CSimpleStringA,EntityRunInfo> m_entRunInfo;
  184. map<string, EntityCfg, EntityNameCompare> m_mapEntity;
  185. vector<CSimpleStringA> m_activeEntity,m_allEntity,m_vKeyEntity,m_vQueryStateEntity;
  186. int m_restartNormal,m_restartSpecial,m_maxOsRestart,m_maxPowerRestart
  187. ,m_warmLevel,m_diskLastWarnHour;
  188. DWORD m_dwIssueCount,m_dwCaptureCount;
  189. bool m_bInIssue,m_bFirstCalcCpu, m_bEverInMainPage,m_bHaveGetEntityList,m_bFWBEntityAdd;
  190. //CSimpleStringA m_csKeyEntity;
  191. __int64 m_xIdlePre,m_xKernelPre,m_xUserPre;
  192. CSystemStaticInfo m_sysInfo;
  193. };
  194. template<class T>
  195. class TimerOutHelper : public ITimerListener
  196. {
  197. public:
  198. typedef void (T::*FuncTimer)(void *pUserdata);
  199. TimerOutHelper(T *p, FuncTimer pTimerFunc,void *pData,bool bDeleteSelf = false)
  200. : m_pObject(p),m_pUserData(pData), m_pTimer(pTimerFunc), m_bDeleteSelf(bDeleteSelf) {}
  201. virtual void OnTimeout(DWORD dwTimerID)
  202. {
  203. (m_pObject->*m_pTimer)(m_pUserData);
  204. if (m_bDeleteSelf)
  205. delete this;
  206. }
  207. private:
  208. void *m_pUserData;
  209. T *m_pObject;
  210. FuncTimer m_pTimer;
  211. bool m_bDeleteSelf;
  212. };
  213. #endif // __SELFCHECKER_FSM_H