StatReport.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #include "servant/StatReport.h"
  17. #include "util/tc_common.h"
  18. #include "util/tc_timeprovider.h"
  19. #include "servant/RemoteLogger.h"
  20. #include <iostream>
  21. #if TARGET_PLATFORM_LINUX || TARGET_PLATFORM_IOS
  22. #include <arpa/inet.h>
  23. #endif
  24. #include "servant/Application.h"
  25. #include "servant/Communicator.h"
  26. #include "servant/CommunicatorEpoll.h"
  27. namespace tars
  28. {
  29. //////////////////////////////////////////////////////////////////
  30. //
  31. StatReport::StatReport(Communicator* communicator)
  32. : _communicator(communicator)
  33. , _time(0)
  34. , _reportInterval(60000)
  35. , _reportTimeout(5000)
  36. , _maxReportSize(MAX_REPORT_SIZE)
  37. , _terminate(false)
  38. , _sampleRate(1)
  39. , _maxSampleCount(500)
  40. , _retValueNumLimit(10)
  41. {
  42. srand(time(NULL));
  43. }
  44. StatReport::~StatReport()
  45. {
  46. if (isAlive())
  47. {
  48. terminate();
  49. getThreadControl().join();
  50. }
  51. }
  52. void StatReport::terminate()
  53. {
  54. Lock lock(*this);
  55. _terminate = true;
  56. notifyAll();
  57. }
  58. void StatReport::setReportInfo(const StatFPrx& statPrx,
  59. const PropertyFPrx& propertyPrx,
  60. const string& strModuleName,
  61. const string& strModuleIp,
  62. const string& strSetDivision,
  63. int iReportInterval,
  64. int iSampleRate,
  65. unsigned int iMaxSampleCount,
  66. int iMaxReportSize,
  67. int iReportTimeout)
  68. {
  69. Lock lock(*this);
  70. _statPrx = statPrx;
  71. _propertyPrx = propertyPrx;
  72. //包头信息,trim&substr 防止超长导致udp包发送失败
  73. _moduleName = trimAndLimitStr(strModuleName, MAX_MASTER_NAME_LEN);
  74. _ip = trimAndLimitStr(strModuleIp, MAX_MASTER_IP_LEN);
  75. _time = TNOW;
  76. _reportInterval = iReportInterval < 5000 ? 5000 : iReportInterval;
  77. _reportTimeout = iReportTimeout < 5000 ? 5000 : iReportTimeout;
  78. _sampleRate = (iSampleRate < 1)?1: iSampleRate;
  79. _maxSampleCount = iMaxSampleCount>500?500:iMaxSampleCount;
  80. if ( iMaxReportSize < MIN_REPORT_SIZE || iMaxReportSize > MAX_REPORT_SIZE )
  81. {
  82. _maxReportSize = MAX_REPORT_SIZE;
  83. }
  84. else
  85. {
  86. _maxReportSize = iMaxReportSize;
  87. }
  88. vector<string> vtSetInfo = TC_Common::sepstr<string>(strSetDivision,".");
  89. if (vtSetInfo.size()!=3 ||(vtSetInfo[0]=="*"||vtSetInfo[1]=="*"))
  90. {
  91. _setArea= "";
  92. _setID = "";
  93. }
  94. else
  95. {
  96. _setName = vtSetInfo[0];
  97. _setArea = vtSetInfo[1];
  98. _setID = vtSetInfo[2];
  99. }
  100. //TLOGDEBUG("setReportInfo Division:" << strSetDivision << " " << _setName << " " << _setArea << " " << _setID << endl);
  101. resetStatInterv();
  102. if (!isAlive())
  103. {
  104. start();
  105. }
  106. }
  107. void StatReport::addStatInterv(int iInterv)
  108. {
  109. Lock lock(*this);
  110. _timePoint.push_back(iInterv);
  111. sort(_timePoint.begin(),_timePoint.end());
  112. auto it = unique(_timePoint.begin(),_timePoint.end());
  113. _timePoint.resize(std::distance(_timePoint.begin(),it));
  114. }
  115. void StatReport::getIntervCount(int time, StatMicMsgBody& body)
  116. {
  117. int iTimePoint = 0;
  118. bool bNeedInit = false;
  119. bool bGetIntev = false;
  120. if (body.intervalCount.size() == 0) //第一次需要将所有描点值初始化为0
  121. {
  122. bNeedInit = true;
  123. }
  124. for(int i =0;i<(int)_timePoint.size();i++)
  125. {
  126. iTimePoint = _timePoint[i];
  127. if(bGetIntev == false && time < iTimePoint)
  128. {
  129. bGetIntev = true;
  130. body.intervalCount[iTimePoint]++;
  131. if(bNeedInit == false)
  132. break;
  133. else
  134. continue;
  135. }
  136. if(bNeedInit == true)
  137. {
  138. body.intervalCount[iTimePoint] = 0;
  139. }
  140. }
  141. return;
  142. }
  143. void StatReport::resetStatInterv()
  144. {
  145. _timePoint.clear();
  146. _timePoint.push_back(5);
  147. _timePoint.push_back(10);
  148. _timePoint.push_back(50);
  149. _timePoint.push_back(100);
  150. _timePoint.push_back(200);
  151. _timePoint.push_back(500);
  152. _timePoint.push_back(1000);
  153. _timePoint.push_back(2000);
  154. _timePoint.push_back(3000);
  155. sort(_timePoint.begin(),_timePoint.end());
  156. unique(_timePoint.begin(),_timePoint.end());
  157. }
  158. string StatReport::trimAndLimitStr(const string& str, uint32_t limitlen)
  159. {
  160. static const string strTime = "\r\t";
  161. string ret = TC_Common::trim(str, strTime);
  162. if (ret.length() > limitlen)
  163. {
  164. ret.resize(limitlen);
  165. }
  166. return ret;
  167. }
  168. bool StatReport::divison2SetInfo(const string& str, vector<string>& vtSetInfo)
  169. {
  170. vtSetInfo = TC_Common::sepstr<string>(str,".");
  171. if (vtSetInfo.size() != 3 ||(vtSetInfo[0]=="*"||vtSetInfo[1]=="*"))
  172. {
  173. TLOGERROR(__FUNCTION__ << ":" << __LINE__ << "|bad set name [" << str << endl);
  174. return false;
  175. }
  176. return true;
  177. }
  178. /*
  179. tars.tarsstat to tarsstat
  180. */
  181. string StatReport::getServerName(string sModuleName)
  182. {
  183. string::size_type pos = sModuleName.find(".");
  184. if (pos != string::npos)
  185. {
  186. return sModuleName.substr(pos + 1); //+1:过滤.
  187. }
  188. return sModuleName;
  189. }
  190. //void StatReport::report(const string& strInterfaceName,
  191. // const string& strModuleIp,
  192. // uint16_t iPort,
  193. // StatResult eResult,
  194. // int iSptime,
  195. // int iReturnValue,
  196. // bool bFromClient)
  197. //{
  198. // report(this->_communicator->clientConfig().ModuleName, this->_communicator->clientConfig().SetDivision, strInterfaceName, strModuleIp, iPort, eResult, iSptime, iReturnValue, bFromClient);
  199. //}
  200. void StatReport::report(const string& strModuleName,
  201. const string& strInterfaceName,
  202. const string& strModuleIp,
  203. uint16_t iPort,
  204. StatResult eResult,
  205. int iSptime,
  206. int iReturnValue,
  207. bool bFromClient)
  208. {
  209. //包头信息,trim&substr 防止超长导致udp包发送失败
  210. //masterIp为空服务端自己获取。
  211. StatMicMsgHead head;
  212. StatMicMsgBody body;
  213. string sMaterServerName = "";
  214. string sSlaveServerName = "";
  215. string appName = ""; // 由setdivision生成
  216. if (bFromClient)
  217. {
  218. if (!_setName.empty())
  219. {
  220. head.masterName = _moduleName + "." + _setName + _setArea + _setID + "@" + this->_communicator->clientConfig().TarsVersion;
  221. }
  222. else
  223. {
  224. head.masterName = _moduleName + "@" + this->_communicator->clientConfig().TarsVersion;
  225. }
  226. const string& setdivision = this->_communicator->clientConfig().SetDivision;
  227. if (!setdivision.empty()) //被调没有启用set分组,slavename保持原样
  228. {
  229. vector <string> vtSetInfo;
  230. if(divison2SetInfo(setdivision, vtSetInfo))
  231. {
  232. head.slaveSetName = vtSetInfo[0];
  233. head.slaveSetArea = vtSetInfo[1];
  234. head.slaveSetID = vtSetInfo[2];
  235. }
  236. head.slaveName = trimAndLimitStr(strModuleName, MAX_MASTER_NAME_LEN) + "." + head.slaveSetName + head.slaveSetArea + head.slaveSetID;
  237. }
  238. else
  239. {
  240. head.slaveName = trimAndLimitStr(strModuleName, MAX_MASTER_NAME_LEN);
  241. }
  242. head.masterIp = "";
  243. head.slaveIp = trimAndLimitStr(strModuleIp, MAX_MASTER_IP_LEN);
  244. }
  245. else
  246. {
  247. //被调上报,masterName没有set信息
  248. head.masterName = trimAndLimitStr(strModuleName, MAX_MASTER_NAME_LEN);
  249. head.masterIp = trimAndLimitStr(strModuleIp, MAX_MASTER_IP_LEN);
  250. if(_setName.empty()) //被调上报,slave的set信息为空
  251. {
  252. head.slaveName = _moduleName;//服务端version不需要上报
  253. }
  254. else
  255. {
  256. head.slaveName = _moduleName + "." + _setName + _setArea + _setID;
  257. }
  258. head.slaveIp = "";
  259. head.slaveSetName = _setName;
  260. head.slaveSetArea = _setArea;
  261. head.slaveSetID = _setID;
  262. }
  263. head.interfaceName = trimAndLimitStr(strInterfaceName, MAX_MASTER_NAME_LEN);
  264. head.slavePort = iPort;
  265. head.returnValue = iReturnValue;
  266. //包体信息.
  267. if (eResult == STAT_SUCC)
  268. {
  269. body.count = 1;
  270. body.totalRspTime = body.minRspTime = body.maxRspTime = iSptime;
  271. }
  272. else if (eResult == STAT_TIMEOUT)
  273. {
  274. body.timeoutCount = 1;
  275. }
  276. else
  277. {
  278. body.execCount = 1;
  279. }
  280. submit(head, body, bFromClient);
  281. }
  282. void StatReport::report(const string& strMasterName,
  283. const string& strMasterIp,
  284. const string& strSlaveName,
  285. const string& strSlaveIp,
  286. uint16_t iSlavePort,
  287. const string& strInterfaceName,
  288. StatResult eResult,
  289. int iSptime,
  290. int iReturnValue)
  291. {
  292. //包头信息,trim&substr 防止超长导致udp包发送失败
  293. //masterIp为空服务端自己获取。
  294. StatMicMsgHead head;
  295. StatMicMsgBody body;
  296. head.masterName = trimAndLimitStr(strMasterName + "@" + this->_communicator->clientConfig().TarsVersion, MAX_MASTER_NAME_LEN);
  297. head.masterIp = trimAndLimitStr(strMasterIp, MAX_MASTER_IP_LEN);
  298. head.slaveName = trimAndLimitStr(strSlaveName, MAX_MASTER_NAME_LEN);
  299. head.slaveIp = trimAndLimitStr(strSlaveIp, MAX_MASTER_IP_LEN);
  300. head.interfaceName = trimAndLimitStr(strInterfaceName, MAX_MASTER_NAME_LEN);
  301. head.slavePort = iSlavePort;
  302. head.returnValue = iReturnValue;
  303. //包体信息.
  304. if (eResult == STAT_SUCC)
  305. {
  306. body.count = 1;
  307. body.totalRspTime = body.minRspTime = body.maxRspTime = iSptime;
  308. }
  309. else if (eResult == STAT_TIMEOUT)
  310. {
  311. body.timeoutCount = 1;
  312. }
  313. else
  314. {
  315. body.execCount = 1;
  316. }
  317. submit(head, body, true);
  318. }
  319. void StatReport::submit(StatMicMsgHead& head, StatMicMsgBody& body, bool bFromClient)
  320. {
  321. Lock lock(*this);
  322. MapStatMicMsg& msg = (bFromClient == true)?_statMicMsgClient:_statMicMsgServer;
  323. auto it = msg.find( head );
  324. if ( it != msg.end() )
  325. {
  326. StatMicMsgBody& stBody = it->second;
  327. stBody.count += body.count;
  328. stBody.timeoutCount += body.timeoutCount;
  329. stBody.execCount += body.execCount;
  330. stBody.totalRspTime += body.totalRspTime;
  331. if (stBody.maxRspTime < body.maxRspTime)
  332. {
  333. stBody.maxRspTime = body.maxRspTime;
  334. }
  335. //非0最小值
  336. if (stBody.minRspTime == 0 || (stBody.minRspTime > body.minRspTime && body.minRspTime != 0))
  337. {
  338. stBody.minRspTime = body.minRspTime;
  339. }
  340. getIntervCount(body.maxRspTime, stBody);
  341. }
  342. else
  343. {
  344. getIntervCount(body.maxRspTime, body);
  345. msg[head] = body;
  346. }
  347. }
  348. int StatReport::reportMicMsg(MapStatMicMsg& msg, bool bFromClient)
  349. {
  350. if (msg.empty()) return 0;
  351. try
  352. {
  353. int iLen = 0;
  354. MapStatMicMsg mTemp;
  355. MapStatMicMsg mStatMsg;
  356. mStatMsg.clear();
  357. mTemp.clear();
  358. {
  359. Lock lock(*this);
  360. msg.swap(mStatMsg);
  361. }
  362. TLOGTARS("[StatReport::reportMicMsg get size:" << mStatMsg.size()<<"]"<< endl);
  363. for(MapStatMicMsg::iterator it = mStatMsg.begin(); it != mStatMsg.end(); it++)
  364. {
  365. const StatMicMsgHead &head = it->first;
  366. int iTemLen = STAT_PROTOCOL_LEN +head.masterName.length() + head.slaveName.length() + head.interfaceName.length()
  367. + head.slaveSetName.length() + head.slaveSetArea.length() + head.slaveSetID.length();
  368. iLen = iLen + iTemLen;
  369. if(iLen > _maxReportSize) //不能超过udp 1472
  370. {
  371. if(_statPrx)
  372. {
  373. TLOGTARS("[StatReport::reportMicMsg send size:" << mTemp.size()<<"]"<< endl);
  374. _statPrx->tars_set_timeout(_reportTimeout)->async_reportMicMsg(NULL,mTemp,bFromClient, ServerConfig::Context);
  375. }
  376. iLen = iTemLen;
  377. mTemp.clear();
  378. }
  379. mTemp[head] = it->second;
  380. if(LOG->isNeedLog(LocalRollLogger::INFO_LOG))
  381. {
  382. ostringstream os;
  383. os.str("");
  384. head.displaySimple(os);
  385. os << " ";
  386. mTemp[head].displaySimple(os);
  387. TLOGTARS("[StatReport::reportMicMsg display:" << os.str() << "]" << endl);
  388. }
  389. }
  390. if(0 != (int)mTemp.size())
  391. {
  392. if(_statPrx)
  393. {
  394. TLOGTARS("[StatReport::reportMicMsg send size:" << mTemp.size()<<"]"<< endl);
  395. _statPrx->tars_set_timeout(_reportTimeout)->async_reportMicMsg(NULL,mTemp,bFromClient, ServerConfig::Context);
  396. }
  397. }
  398. return 0;
  399. }
  400. catch (exception& e)
  401. {
  402. TLOGERROR("StatReport::report catch exception:" << e.what() << endl);
  403. }
  404. catch ( ... )
  405. {
  406. TLOGERROR("StatReport::report catch unkown exception" << endl);
  407. }
  408. return -1;
  409. }
  410. int StatReport::reportPropMsg()
  411. {
  412. try
  413. {
  414. MapStatPropMsg mStatMsg;
  415. {
  416. Lock lock(*this);
  417. for(map<string, PropertyReportPtr>::iterator it = _statPropMsg.begin(); it != _statPropMsg.end(); ++it)
  418. {
  419. StatPropMsgHead head;
  420. StatPropMsgBody body;
  421. if (!it->second->getMasterName().empty())
  422. {
  423. if (!_setName.empty())
  424. {
  425. head.moduleName = it->second->getMasterName() + "." + _setName + _setArea + _setID;
  426. }
  427. else
  428. {
  429. head.moduleName = it->second->getMasterName();
  430. }
  431. }
  432. else
  433. {
  434. if (!_setName.empty())
  435. {
  436. head.moduleName = _moduleName + "." + _setName + _setArea + _setID;
  437. }
  438. else
  439. {
  440. head.moduleName = _moduleName;
  441. }
  442. }
  443. head.ip = "";
  444. head.propertyName = it->first;
  445. head.setName = _setName;
  446. head.setArea = _setArea;
  447. head.setID = _setID;
  448. head.iPropertyVer = 2;
  449. vector<pair<string, string> > v = it->second->get();
  450. for(size_t i = 0; i < v.size(); i++)
  451. {
  452. // bool bFlag = false;
  453. // if(v[i].first == "Sum")
  454. // {
  455. // if(v[i].second != "0")
  456. // bFlag = true;
  457. // }
  458. // else if(v[i].first == "Avg")
  459. // {
  460. // if(v[i].second != "0")
  461. // bFlag = true;
  462. // }
  463. // else if(v[i].first == "Distr")
  464. // {
  465. // if(v[i].second != "")
  466. // bFlag = true;
  467. // }
  468. // else if(v[i].first == "Max")
  469. // {
  470. // if(v[i].second != "-9999999")
  471. // bFlag = true;
  472. // }
  473. // else if(v[i].first == "Min")
  474. // {
  475. // if(v[i].second != "0")
  476. // bFlag = true;
  477. // }
  478. // else if(v[i].first == "Count")
  479. // {
  480. // if(v[i].second != "0")
  481. // bFlag = true;
  482. // }
  483. // else
  484. // {
  485. // bFlag = true;
  486. // }
  487. // if(bFlag)
  488. {
  489. StatPropInfo sp;
  490. sp.policy = v[i].first;
  491. sp.value = v[i].second;
  492. body.vInfo.push_back(sp);
  493. }
  494. }
  495. mStatMsg[head] = body;
  496. if(LOG->isNeedLog(LocalRollLogger::INFO_LOG))
  497. {
  498. ostringstream os;
  499. os.str("");
  500. head.displaySimple(os);
  501. os << " ";
  502. mStatMsg[head].displaySimple(os);
  503. TLOGTARS("[StatReport::reportPropMsg display:" << os.str() << "]" << endl);
  504. }
  505. }
  506. }
  507. TLOGTARS("[StatReport::reportPropMsg get size:" << mStatMsg.size() << "]" << endl);
  508. int iLen = 0;
  509. MapStatPropMsg mTemp;
  510. for (MapStatPropMsg::iterator it = mStatMsg.begin(); it != mStatMsg.end(); it++)
  511. {
  512. const StatPropMsgHead& head = it->first;
  513. const StatPropMsgBody& body = it->second;
  514. int iTemLen = head.moduleName.length() + head.ip.length() + head.propertyName.length() + head.setName.length() + head.setArea.length() + head.setID.length();
  515. for (size_t i = 0; i < body.vInfo.size(); i++)
  516. {
  517. iTemLen += body.vInfo[i].policy.length();
  518. iTemLen += body.vInfo[i].value.length();
  519. }
  520. iTemLen = PROPERTY_PROTOCOL_LEN + body.vInfo.size(); //
  521. iLen = iLen + iTemLen;
  522. if (iLen > _maxReportSize) //不能超过udp 1472
  523. {
  524. if (_propertyPrx)
  525. {
  526. TLOGTARS("[StatReport::reportPropMsg send size:" << mTemp.size() << "]" << endl);
  527. _propertyPrx->tars_set_timeout(_reportTimeout)->async_reportPropMsg(NULL, mTemp, ServerConfig::Context);
  528. }
  529. iLen = iTemLen;
  530. mTemp.clear();
  531. }
  532. mTemp[it->first] = it->second;
  533. }
  534. if (0 != (int)mTemp.size())
  535. {
  536. if (_propertyPrx)
  537. {
  538. TLOGTARS("[StatReport::reportPropMsg send size:" << mTemp.size() << "]" << endl);
  539. _propertyPrx->tars_set_timeout(_reportTimeout)->async_reportPropMsg(NULL, mTemp, ServerConfig::Context);
  540. }
  541. }
  542. return 0;
  543. }
  544. catch ( exception& e )
  545. {
  546. TLOGERROR("StatReport::reportPropMsg catch exception:" << e.what() << endl);
  547. }
  548. catch ( ... )
  549. {
  550. TLOGERROR("StatReport::reportPropMsg catch unkown exception" << endl);
  551. }
  552. return -1;
  553. }
  554. void StatReport::addMicMsg(MapStatMicMsg& old, MapStatMicMsg& add)
  555. {
  556. auto iter = add.begin();
  557. for (; iter != add.end(); ++iter)
  558. {
  559. auto iterOld = old.find(iter->first);
  560. if (iterOld == old.end())
  561. {
  562. //直接insert
  563. old.insert(make_pair(iter->first, iter->second));
  564. }
  565. else
  566. {
  567. //合并
  568. iterOld->second.count += iter->second.count;
  569. iterOld->second.timeoutCount += iter->second.timeoutCount;
  570. iterOld->second.execCount += iter->second.execCount;
  571. map<int, int>::iterator iterOldInt, iterInt;
  572. map<int, int>& mCount = iter->second.intervalCount;
  573. map<int, int>& mCountOld = iterOld->second.intervalCount;
  574. iterInt = mCount.begin();
  575. for (; iterInt != mCount.end(); ++iterInt)
  576. {
  577. iterOldInt = mCountOld.find(iterInt->first);
  578. if (iterOldInt == mCountOld.end())
  579. {
  580. mCountOld.insert(make_pair(iterInt->first, iterInt->second));
  581. }
  582. else
  583. {
  584. iterOldInt->second += iterInt->second;
  585. }
  586. }
  587. iterOld->second.totalRspTime += iter->second.totalRspTime;
  588. if(iterOld->second.maxRspTime < iter->second.maxRspTime)
  589. iterOld->second.maxRspTime = iter->second.maxRspTime;
  590. if(iterOld->second.minRspTime > iter->second.minRspTime)
  591. iterOld->second.minRspTime = iter->second.minRspTime;
  592. }
  593. }
  594. }
  595. void StatReport::run()
  596. {
  597. while(!_terminate)
  598. {
  599. {
  600. Lock lock(*this);
  601. timedWait(1000);
  602. }
  603. try
  604. {
  605. time_t tNow = TNOW;
  606. if(tNow - _time >= _reportInterval/1000)
  607. {
  608. reportMicMsg(_statMicMsgClient, true);
  609. reportMicMsg(_statMicMsgServer, false);
  610. MapStatMicMsg mStatMsg;
  611. auto communicatorEpolls = _communicator->getAllCommunicatorEpoll();
  612. for(auto ce : communicatorEpolls)
  613. {
  614. MapStatMicMsg * pStatMsg;
  615. while(ce->popStatMsg(pStatMsg))
  616. {
  617. addMicMsg(mStatMsg,*pStatMsg);
  618. delete pStatMsg;
  619. }
  620. }
  621. reportMicMsg(mStatMsg, true);
  622. reportPropMsg();
  623. // reportSampleMsg();
  624. _time = tNow;
  625. }
  626. }
  627. catch ( exception& e )
  628. {
  629. TLOGERROR("StatReport::run catch exception:" << e.what() << endl);
  630. }
  631. catch ( ... )
  632. {
  633. TLOGERROR("StatReport::run catch unkown exception" << endl);
  634. }
  635. }
  636. ServantProxyThreadData::g_sp.reset();
  637. }
  638. PropertyReportPtr StatReport::createPropertyReportWithFlag(const string& strProperty, int flag)
  639. {
  640. Lock lock(*this);
  641. if(_statPropMsg.find(strProperty) != _statPropMsg.end())
  642. {
  643. return _statPropMsg[strProperty];
  644. }
  645. vector<shared_ptr<PropertyReport::base>> properties;
  646. if(flag & PT_SUM)
  647. {
  648. properties.push_back(std::make_shared<PropertyReport::sumProperty>());
  649. }
  650. if(flag & PT_COUNT)
  651. {
  652. properties.push_back(std::make_shared<PropertyReport::countProperty>());
  653. }
  654. if(flag & PT_AVG)
  655. {
  656. properties.push_back(std::make_shared<PropertyReport::avgProperty>());
  657. }
  658. if(flag & PT_MAX)
  659. {
  660. properties.push_back(std::make_shared<PropertyReport::maxProperty>());
  661. }
  662. if(flag & PT_MIN)
  663. {
  664. properties.push_back(std::make_shared<PropertyReport::minProperty>());
  665. }
  666. if(flag & PT_DISTR)
  667. {
  668. properties.push_back(std::make_shared<PropertyReport::distrProperty>());
  669. }
  670. PropertyReportImp *p = new PropertyReportImp();
  671. p->setProperties(properties);
  672. PropertyReportPtr srPtr(p);
  673. _statPropMsg[strProperty] = srPtr;
  674. return srPtr;
  675. }
  676. ////////////////////////////////////////////////////////////////
  677. }