SelfCheckerFSM.h 6.8 KB

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