jstats.cpp 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "jiface.hpp"
  14. #include "jstats.h"
  15. #include "jexcept.hpp"
  16. #include "jiter.ipp"
  17. #include "jlog.hpp"
  18. #include "jregexp.hpp"
  19. #include "jfile.hpp"
  20. #ifdef _WIN32
  21. #include <sys/timeb.h>
  22. #endif
  23. static CriticalSection statsNameCs;
  24. static StringBuffer statisticsComponentName;
  25. static StatisticCreatorType statisticsComponentType = SCTunknown;
  26. StatisticCreatorType queryStatisticsComponentType()
  27. {
  28. return statisticsComponentType;
  29. }
  30. const char * queryStatisticsComponentName()
  31. {
  32. CriticalBlock c(statsNameCs);
  33. if (statisticsComponentName.length() == 0)
  34. {
  35. statisticsComponentName.append("unknown").append(GetCachedHostName());
  36. DBGLOG("getProcessUniqueName hasn't been configured correctly");
  37. }
  38. return statisticsComponentName.str();
  39. }
  40. void setStatisticsComponentName(StatisticCreatorType processType, const char * processName, bool appendIP)
  41. {
  42. if (!processName)
  43. return;
  44. CriticalBlock c(statsNameCs);
  45. statisticsComponentType = processType;
  46. statisticsComponentName.clear().append(processName);
  47. if (appendIP)
  48. statisticsComponentName.append("@").append(GetCachedHostName()); // should I use _ instead?
  49. }
  50. //--------------------------------------------------------------------------------------------------------------------
  51. // Textual forms of the different enumerations, first items are for none and all.
  52. static const char * const measureNames[] = { "", "all", "ns", "ts", "cnt", "sz", "cpu", "skw", "node", "ppm", "ip", "cy", NULL };
  53. static const char * const creatorTypeNames[]= { "", "all", "unknown", "hthor", "roxie", "roxie:s", "thor", "thor:m", "thor:s", "eclcc", "esp", "summary", NULL };
  54. static const char * const scopeTypeNames[] = { "", "all", "global", "graph", "subgraph", "activity", "allocator", "section", "compile", "dfu", "edge", NULL };
  55. static unsigned matchString(const char * const * names, const char * search)
  56. {
  57. if (!search)
  58. return 0;
  59. if (streq(search, "*"))
  60. search = "all";
  61. unsigned i=0;
  62. loop
  63. {
  64. const char * next = names[i];
  65. if (!next)
  66. return 0;
  67. if (streq(next, search))
  68. return i;
  69. i++;
  70. }
  71. }
  72. //--------------------------------------------------------------------------------------------------------------------
  73. extern jlib_decl unsigned __int64 getTimeStampNowValue()
  74. {
  75. #ifdef _WIN32
  76. struct _timeb now;
  77. _ftime(&now);
  78. return (unsigned __int64)now.time * I64C(1000000) + now.millitm * 1000;
  79. #else
  80. struct timeval tm;
  81. gettimeofday(&tm,NULL);
  82. return (unsigned __int64)tm.tv_sec * I64C(1000000) + tm.tv_usec;
  83. #endif
  84. }
  85. const static unsigned __int64 msUntilResync = 1000; // resync every second ~= 1ms accuracy
  86. static cycle_t cyclesUntilResync;
  87. MODULE_INIT(INIT_PRIORITY_STANDARD)
  88. {
  89. cyclesUntilResync = nanosec_to_cycle(msUntilResync * 1000000);
  90. return true;
  91. }
  92. OptimizedTimestamp::OptimizedTimestamp()
  93. {
  94. lastCycles = get_cycles_now();
  95. lastTimestamp = ::getTimeStampNowValue();
  96. }
  97. #if 0
  98. //This version almost certainly has problems if the computer is suspended and cycles->nanoseconds is only accurate to
  99. //about 0.1% - so should only be used for relatively short periods
  100. unsigned __int64 OptimizedTimestamp::getTimeStampNowValue()
  101. {
  102. cycle_t nowCycles = get_cycles_now();
  103. return lastTimestamp + cycle_to_microsec(nowCycles - lastCycles);
  104. }
  105. #else
  106. //This version will resync every minute, but is not thread safe. Adding a critical section makes it less efficient than recalculating
  107. unsigned __int64 OptimizedTimestamp::getTimeStampNowValue()
  108. {
  109. cycle_t nowCycles = get_cycles_now();
  110. if (nowCycles - lastCycles > cyclesUntilResync)
  111. {
  112. lastCycles = nowCycles;
  113. lastTimestamp = ::getTimeStampNowValue();
  114. }
  115. return lastTimestamp + cycle_to_microsec(nowCycles - lastCycles);
  116. }
  117. #endif
  118. unsigned __int64 getIPV4StatsValue(const IpAddress & ip)
  119. {
  120. unsigned ipValue;
  121. if (ip.getNetAddress(sizeof(ipValue),&ipValue))
  122. return ipValue;
  123. return 0;
  124. }
  125. //--------------------------------------------------------------------------------------------------------------------
  126. const static unsigned __int64 oneMilliSecond = I64C(1000000);
  127. const static unsigned __int64 oneSecond = I64C(1000000000);
  128. const static unsigned __int64 oneMinute = I64C(60000000000);
  129. const static unsigned __int64 oneHour = I64C(3600000000000);
  130. const static unsigned __int64 oneDay = 24 * I64C(3600000000000);
  131. static void formatTime(StringBuffer & out, unsigned __int64 value)
  132. {
  133. //Aim to display at least 3 significant digits in the result string
  134. if (value < oneMilliSecond)
  135. out.appendf("%uns", (unsigned)value);
  136. else if (value < oneSecond)
  137. {
  138. unsigned uvalue = (unsigned)value;
  139. out.appendf("%u.%03ums", uvalue / 1000000, (uvalue / 1000) % 1000);
  140. }
  141. else
  142. {
  143. unsigned days = (unsigned)(value / oneDay);
  144. value = value % oneDay;
  145. unsigned hours = (unsigned)(value / oneHour);
  146. value = value % oneHour;
  147. unsigned mins = (unsigned)(value / oneMinute);
  148. value = value % oneMinute;
  149. unsigned secs = (unsigned)(value / oneSecond);
  150. unsigned ns = (unsigned)(value % oneSecond);
  151. if (days > 0)
  152. out.appendf("%u days ", days);
  153. if (hours > 0 || days)
  154. out.appendf("%u:%02u:%02u", hours, mins, secs);
  155. else if (mins >= 10)
  156. out.appendf("%u:%02u", mins, secs);
  157. else if (mins >= 1)
  158. out.appendf("%u:%02u.%03u", mins, secs, ns / 1000000);
  159. else
  160. out.appendf("%u.%03us", secs, ns / 1000000);
  161. }
  162. }
  163. extern void formatTimeCollatable(StringBuffer & out, unsigned __int64 value, bool nano)
  164. {
  165. unsigned days = (unsigned)(value / oneDay);
  166. value = value % oneDay;
  167. unsigned hours = (unsigned)(value / oneHour);
  168. value = value % oneHour;
  169. unsigned mins = (unsigned)(value / oneMinute);
  170. value = value % oneMinute;
  171. unsigned secs = (unsigned)(value / oneSecond);
  172. unsigned ns = (unsigned)(value % oneSecond);
  173. if (days)
  174. out.appendf(" %3ud ", days); // Two leading spaces helps the cassandra driver force to a single partition
  175. else
  176. out.appendf(" ");
  177. if (nano)
  178. out.appendf("%2u:%02u:%02u.%09u", hours, mins, secs, ns);
  179. else
  180. out.appendf("%2u:%02u:%02u.%03u", hours, mins, secs, ns/1000000);
  181. // More than 999 days, I don't care that it goes wrong.
  182. }
  183. extern unsigned __int64 extractTimeCollatable(const char *s, bool nano)
  184. {
  185. if (!s)
  186. return 0;
  187. unsigned days,hours,mins,secs,fracs;
  188. if (sscanf(s, " %ud %u:%u:%u.%u", &days, &hours, &mins, &secs, &fracs)!=5)
  189. {
  190. days = 0;
  191. if (sscanf(s, " %u:%u:%u.%u", &hours, &mins, &secs, &fracs) != 4)
  192. return 0;
  193. }
  194. unsigned __int64 ret = days*oneDay + hours*oneHour + mins*oneMinute + secs*oneSecond;
  195. if (nano)
  196. ret += fracs;
  197. else
  198. ret += milliToNano(fracs);
  199. return ret;
  200. }
  201. static void formatTimeStamp(StringBuffer & out, unsigned __int64 value)
  202. {
  203. time_t seconds = value / 1000000;
  204. unsigned us = value % 1000000;
  205. char timeStamp[64];
  206. time_t tNow = seconds;
  207. #ifdef _WIN32
  208. struct tm *gmtNow;
  209. gmtNow = gmtime(&tNow);
  210. strftime(timeStamp, 64, "%Y-%m-%dT%H:%M:%S", gmtNow);
  211. #else
  212. struct tm gmtNow;
  213. gmtime_r(&tNow, &gmtNow);
  214. strftime(timeStamp, 64, "%Y-%m-%dT%H:%M:%S", &gmtNow);
  215. #endif //_WIN32
  216. out.append(timeStamp).appendf(".%03uZ", us / 1000);
  217. }
  218. void formatTimeStampAsLocalTime(StringBuffer & out, unsigned __int64 value)
  219. {
  220. time_t seconds = value / 1000000;
  221. unsigned us = value % 1000000;
  222. char timeStamp[64];
  223. time_t tNow = seconds;
  224. #ifdef _WIN32
  225. struct tm *gmtNow;
  226. gmtNow = localtime(&tNow);
  227. strftime(timeStamp, 64, "%H:%M:%S", gmtNow);
  228. #else
  229. struct tm gmtNow;
  230. localtime_r(&tNow, &gmtNow);
  231. strftime(timeStamp, 64, "%H:%M:%S", &gmtNow);
  232. #endif //_WIN32
  233. out.append(timeStamp).appendf(".%03u", us / 1000);
  234. }
  235. static const unsigned oneKb = 1024;
  236. static const unsigned oneMb = 1024 * 1024;
  237. static const unsigned oneGb = 1024 * 1024 * 1024;
  238. static unsigned toPermille(unsigned x) { return (x * 1000) / 1024; }
  239. static void formatSize(StringBuffer & out, unsigned __int64 value)
  240. {
  241. unsigned Gb = (unsigned)(value / oneGb);
  242. unsigned Mb = (unsigned)((value % oneGb) / oneMb);
  243. unsigned Kb = (unsigned)((value % oneMb) / oneKb);
  244. unsigned b = (unsigned)(value % oneKb);
  245. if (Gb)
  246. out.appendf("%u.%03uGb", Gb, toPermille(Mb));
  247. else if (Mb)
  248. out.appendf("%u.%03uMb", Mb, toPermille(Kb));
  249. else if (Kb)
  250. out.appendf("%u.%03uKb", Kb, toPermille(b));
  251. else
  252. out.appendf("%ub", b);
  253. }
  254. static void formatLoad(StringBuffer & out, unsigned __int64 value)
  255. {
  256. //Stored as millionth of a core. Display as a percentage => scale by 10,000
  257. out.appendf("%u.%03u%%", (unsigned)(value / 10000), (unsigned)(value % 10000) / 10);
  258. }
  259. static void formatSkew(StringBuffer & out, unsigned __int64 value)
  260. {
  261. //Skew stored as 10000 = perfect, display as percentage
  262. out.appendf("%.2f%%", ((double)(__int64)value) / 100.0);
  263. }
  264. static void formatIPV4(StringBuffer & out, unsigned __int64 value)
  265. {
  266. byte ip1 = (value & 255);
  267. byte ip2 = ((value >> 8) & 255);
  268. byte ip3 = ((value >> 16) & 255);
  269. byte ip4 = ((value >> 24) & 255);
  270. out.appendf("%d.%d.%d.%d", ip1, ip2, ip3, ip4);
  271. }
  272. void formatStatistic(StringBuffer & out, unsigned __int64 value, StatisticMeasure measure)
  273. {
  274. switch (measure)
  275. {
  276. case SMeasureNone: // Unknown stat - e.g, on old esp accessing a new workunit
  277. out.append(value);
  278. break;
  279. case SMeasureTimeNs:
  280. formatTime(out, value);
  281. break;
  282. case SMeasureTimestampUs:
  283. formatTimeStamp(out, value);
  284. break;
  285. case SMeasureCount:
  286. out.append(value);
  287. break;
  288. case SMeasureSize:
  289. formatSize(out, value);
  290. break;
  291. case SMeasureLoad:
  292. formatLoad(out, value);
  293. break;
  294. case SMeasureSkew:
  295. formatSkew(out, value);
  296. break;
  297. case SMeasureNode:
  298. out.append(value);
  299. break;
  300. case SMeasurePercent:
  301. out.appendf("%.2f%%", (double)value / 10000.0); // stored as ppm
  302. break;
  303. case SMeasureIPV4:
  304. formatIPV4(out, value);
  305. break;
  306. case SMeasureCycle:
  307. out.append(value);
  308. break;
  309. default:
  310. throwUnexpected();
  311. }
  312. }
  313. void formatStatistic(StringBuffer & out, unsigned __int64 value, StatisticKind kind)
  314. {
  315. formatStatistic(out, value, queryMeasure(kind));
  316. }
  317. //--------------------------------------------------------------------------------------------------------------------
  318. unsigned queryStatisticsDepth(const char * text)
  319. {
  320. unsigned depth = 1;
  321. loop
  322. {
  323. switch (*text)
  324. {
  325. case 0:
  326. return depth;
  327. case ':':
  328. depth++;
  329. break;
  330. }
  331. text++;
  332. }
  333. }
  334. const char * queryMeasurePrefix(StatisticMeasure measure)
  335. {
  336. switch (measure)
  337. {
  338. case SMeasureAll: return NULL;
  339. case SMeasureTimeNs: return "Time";
  340. case SMeasureTimestampUs: return "When";
  341. case SMeasureCount: return "Num";
  342. case SMeasureSize: return "Size";
  343. case SMeasureLoad: return "Load";
  344. case SMeasureSkew: return "Skew";
  345. case SMeasureNode: return "Node";
  346. case SMeasurePercent: return "Per";
  347. case SMeasureIPV4: return "Ip";
  348. case SMeasureCycle: return "Cycle";
  349. default:
  350. throwUnexpected();
  351. }
  352. }
  353. const char * queryMeasureName(StatisticMeasure measure)
  354. {
  355. return measureNames[measure];
  356. }
  357. StatisticMeasure queryMeasure(const char * measure)
  358. {
  359. //MORE: Use a hash table??
  360. StatisticMeasure ret = (StatisticMeasure)matchString(measureNames, measure);
  361. //Legacy support for an unusual statistic - pretend the sizes are in bytes instead of kb.
  362. if ((ret == SMeasureNone) && measure && streq(measure, "kb"))
  363. ret = SMeasureSize;
  364. return ret;
  365. }
  366. StatsMergeAction queryMergeMode(StatisticMeasure measure)
  367. {
  368. switch (measure)
  369. {
  370. case SMeasureTimeNs: return StatsMergeSum;
  371. case SMeasureTimestampUs: return StatsMergeKeepNonZero;
  372. case SMeasureCount: return StatsMergeSum;
  373. case SMeasureSize: return StatsMergeSum;
  374. case SMeasureLoad: return StatsMergeMax;
  375. case SMeasureSkew: return StatsMergeMax;
  376. case SMeasureNode: return StatsMergeKeepNonZero;
  377. case SMeasurePercent: return StatsMergeReplace;
  378. case SMeasureIPV4: return StatsMergeKeepNonZero;
  379. case SMeasureCycle: return StatsMergeSum;
  380. default:
  381. throwUnexpected();
  382. }
  383. }
  384. extern jlib_decl StatsMergeAction queryMergeMode(StatisticKind kind)
  385. {
  386. //MORE: Optimize by looking up in the meta
  387. return queryMergeMode(queryMeasure(kind));
  388. }
  389. //--------------------------------------------------------------------------------------------------------------------
  390. #define BASE_NAMES(x, y) \
  391. #x #y, \
  392. #x "Min" # y, \
  393. #x "Max" # y, \
  394. #x "Avg" # y, \
  395. "Skew" # y, \
  396. "SkewMin" # y, \
  397. "SkewMax" # y, \
  398. "NodeMin" # y, \
  399. "NodeMax" # y,
  400. #define NAMES(x, y) \
  401. BASE_NAMES(x, y) \
  402. #x "Delta" # y
  403. #define TIMENAMES(x, y) \
  404. BASE_NAMES(x, y) \
  405. "TimeDelta" # y
  406. #define BASE_TAGS(x, y) \
  407. "@" #x "Min" # y, \
  408. "@" #x "Max" # y, \
  409. "@" #x "Avg" # y, \
  410. "@Skew" # y, \
  411. "@SkewMin" # y, \
  412. "@SkewMax" # y, \
  413. "@NodeMin" # y, \
  414. "@NodeMax" # y,
  415. //Default tags nothing special overriden
  416. #define TAGS(x, y) \
  417. "@" #x #y, \
  418. BASE_TAGS(x, y) \
  419. "@" #x "Delta" # y
  420. //Define the tags for time items.
  421. #define TIMETAGS(x, y) \
  422. "@" #x #y, \
  423. BASE_TAGS(x, y) \
  424. "@TimeDelta" # y
  425. #define CORESTAT(x, y, m) St##x##y, m, { NAMES(x, y) }, { TAGS(x, y) }
  426. #define STAT(x, y, m) CORESTAT(x, y, m)
  427. //--------------------------------------------------------------------------------------------------------------------
  428. //These are the macros to use to define the different entries in the stats meta table
  429. #define TIMESTAT(y) STAT(Time, y, SMeasureTimeNs)
  430. #define WHENSTAT(y) St##When##y, SMeasureTimestampUs, { TIMENAMES(When, y) }, { TIMETAGS(When, y) }
  431. #define NUMSTAT(y) STAT(Num, y, SMeasureCount)
  432. #define SIZESTAT(y) STAT(Size, y, SMeasureSize)
  433. #define LOADSTAT(y) STAT(Load, y, SMeasureLoad)
  434. #define SKEWSTAT(y) STAT(Skew, y, SMeasureSkew)
  435. #define NODESTAT(y) STAT(Node, y, SMeasureNode)
  436. #define PERSTAT(y) STAT(Per, y, SMeasurePercent)
  437. #define IPV4STAT(y) STAT(IPV4, y, SMeasureIPV4)
  438. #define CYCLESTAT(y) STAT(Cycle, y, SMeasureCycle)
  439. //--------------------------------------------------------------------------------------------------------------------
  440. class StatisticMeta
  441. {
  442. public:
  443. StatisticKind kind;
  444. StatisticMeasure measure;
  445. const char * names[StNextModifier/StVariantScale];
  446. const char * tags[StNextModifier/StVariantScale];
  447. };
  448. //The order of entries in this table must match the order in the enumeration
  449. static const StatisticMeta statsMetaData[StMax] = {
  450. { StKindNone, SMeasureNone, { "none" }, { "@none" } },
  451. { StKindAll, SMeasureAll, { "all" }, { "@all" } },
  452. { WHENSTAT(GraphStarted) },
  453. { WHENSTAT(GraphFinished) },
  454. { WHENSTAT(FirstRow) },
  455. { WHENSTAT(QueryStarted) },
  456. { WHENSTAT(QueryFinished) },
  457. { WHENSTAT(Created) },
  458. { WHENSTAT(Compiled) },
  459. { WHENSTAT(WorkunitModified) },
  460. { TIMESTAT(Elapsed) },
  461. { TIMESTAT(LocalExecute) },
  462. { TIMESTAT(TotalExecute) },
  463. { TIMESTAT(Remaining) },
  464. { SIZESTAT(GeneratedCpp) },
  465. { SIZESTAT(PeakMemory) },
  466. { SIZESTAT(MaxRowSize) },
  467. { NUMSTAT(RowsProcessed) },
  468. { NUMSTAT(Slaves) },
  469. { NUMSTAT(Started) },
  470. { NUMSTAT(Stopped) },
  471. { NUMSTAT(IndexSeeks) },
  472. { NUMSTAT(IndexScans) },
  473. { NUMSTAT(IndexWildSeeks) },
  474. { NUMSTAT(IndexSkips) },
  475. { NUMSTAT(IndexNullSkips) },
  476. { NUMSTAT(IndexMerges) },
  477. { NUMSTAT(IndexMergeCompares) },
  478. { NUMSTAT(PreFiltered) },
  479. { NUMSTAT(PostFiltered) },
  480. { NUMSTAT(BlobCacheHits) },
  481. { NUMSTAT(LeafCacheHits) },
  482. { NUMSTAT(NodeCacheHits) },
  483. { NUMSTAT(BlobCacheAdds) },
  484. { NUMSTAT(LeafCacheAdds) },
  485. { NUMSTAT(NodeCacheAdds) },
  486. { NUMSTAT(PreloadCacheHits) },
  487. { NUMSTAT(PreloadCacheAdds) },
  488. { NUMSTAT(ServerCacheHits) },
  489. { NUMSTAT(IndexAccepted) },
  490. { NUMSTAT(IndexRejected) },
  491. { NUMSTAT(AtmostTriggered) },
  492. { NUMSTAT(DiskSeeks) },
  493. { NUMSTAT(Iterations) },
  494. { LOADSTAT(WhileSorting) },
  495. { NUMSTAT(LeftRows) },
  496. { NUMSTAT(RightRows) },
  497. { PERSTAT(Replicated) },
  498. { NUMSTAT(DiskRowsRead) },
  499. { NUMSTAT(IndexRowsRead) },
  500. { NUMSTAT(DiskAccepted) },
  501. { NUMSTAT(DiskRejected) },
  502. { TIMESTAT(Soapcall) },
  503. { TIMESTAT(FirstExecute) },
  504. { TIMESTAT(DiskReadIO) },
  505. { TIMESTAT(DiskWriteIO) },
  506. { SIZESTAT(DiskRead) },
  507. { SIZESTAT(DiskWrite) },
  508. { CYCLESTAT(DiskReadIOCycles) },
  509. { CYCLESTAT(DiskWriteIOCycles) },
  510. { NUMSTAT(DiskReads) },
  511. { NUMSTAT(DiskWrites) },
  512. { NUMSTAT(Spills) },
  513. { TIMESTAT(SpillElapsed) },
  514. { TIMESTAT(SortElapsed) },
  515. { NUMSTAT(Groups) },
  516. { NUMSTAT(GroupMax) },
  517. { SIZESTAT(SpillFile) },
  518. { CYCLESTAT(SpillElapsedCycles) },
  519. { CYCLESTAT(SortElapsedCycles) },
  520. { NUMSTAT(Strands) },
  521. { CYCLESTAT(TotalExecuteCycles) },
  522. { NUMSTAT(Executions) },
  523. { TIMESTAT(TotalNested) },
  524. { CYCLESTAT(LocalExecuteCycles) },
  525. { NUMSTAT(Compares) },
  526. };
  527. //--------------------------------------------------------------------------------------------------------------------
  528. StatisticMeasure queryMeasure(StatisticKind kind)
  529. {
  530. unsigned variant = queryStatsVariant(kind);
  531. switch (variant)
  532. {
  533. case StSkew:
  534. case StSkewMin:
  535. case StSkewMax:
  536. return SMeasureSkew;
  537. case StNodeMin:
  538. case StNodeMax:
  539. return SMeasureNode;
  540. case StDeltaX:
  541. {
  542. StatisticMeasure measure = queryMeasure((StatisticKind)(kind & StKindMask));
  543. switch (measure)
  544. {
  545. case SMeasureTimestampUs:
  546. return SMeasureTimeNs;
  547. default:
  548. return measure;
  549. }
  550. break;
  551. }
  552. }
  553. StatisticKind rawkind = (StatisticKind)(kind & StKindMask);
  554. if (rawkind >= StKindNone && rawkind < StMax)
  555. return statsMetaData[rawkind].measure;
  556. return SMeasureNone;
  557. }
  558. const char * queryStatisticName(StatisticKind kind)
  559. {
  560. StatisticKind rawkind = (StatisticKind)(kind & StKindMask);
  561. unsigned variant = (kind / StVariantScale);
  562. dbgassertex(variant < (StNextModifier/StVariantScale));
  563. if (rawkind >= StKindNone && rawkind < StMax)
  564. return statsMetaData[rawkind].names[variant];
  565. return "Unknown";
  566. }
  567. unsigned __int64 convertMeasure(StatisticMeasure from, StatisticMeasure to, unsigned __int64 value)
  568. {
  569. if (from == to)
  570. return value;
  571. if ((from == SMeasureCycle) && (to == SMeasureTimeNs))
  572. return cycle_to_nanosec(value);
  573. if ((from == SMeasureTimeNs) && (to == SMeasureCycle))
  574. return nanosec_to_cycle(value);
  575. throwUnexpected();
  576. }
  577. unsigned __int64 convertMeasure(StatisticKind from, StatisticKind to, unsigned __int64 value)
  578. {
  579. return convertMeasure(queryMeasure(from), queryMeasure(to), value);
  580. }
  581. //--------------------------------------------------------------------------------------------------------------------
  582. void queryLongStatisticName(StringBuffer & out, StatisticKind kind)
  583. {
  584. out.append(queryStatisticName(kind));
  585. }
  586. //--------------------------------------------------------------------------------------------------------------------
  587. const char * queryTreeTag(StatisticKind kind)
  588. {
  589. StatisticKind rawkind = (StatisticKind)(kind & StKindMask);
  590. unsigned variant = (kind / StVariantScale);
  591. dbgassertex(variant < (StNextModifier/StVariantScale));
  592. if (rawkind >= StKindNone && rawkind < StMax)
  593. return statsMetaData[rawkind].tags[variant];
  594. return "@Unknown";
  595. }
  596. //--------------------------------------------------------------------------------------------------------------------
  597. StatisticKind queryStatisticKind(const char * search)
  598. {
  599. if (!search)
  600. return StKindNone;
  601. if (streq(search, "*"))
  602. return StKindAll;
  603. //Slow - should use a hash table....
  604. for (unsigned variant=0; variant < StNextModifier; variant += StVariantScale)
  605. {
  606. for (unsigned i=0; i < StMax; i++)
  607. {
  608. StatisticKind kind = (StatisticKind)(i+variant);
  609. const char * shortName = queryStatisticName(kind);
  610. if (shortName && strieq(shortName, search))
  611. return kind;
  612. }
  613. }
  614. return StKindNone;
  615. }
  616. //--------------------------------------------------------------------------------------------------------------------
  617. const char * queryCreatorTypeName(StatisticCreatorType sct)
  618. {
  619. return creatorTypeNames[sct];
  620. }
  621. StatisticCreatorType queryCreatorType(const char * sct)
  622. {
  623. //MORE: Use a hash table??
  624. return (StatisticCreatorType)matchString(creatorTypeNames, sct);
  625. }
  626. //--------------------------------------------------------------------------------------------------------------------
  627. const char * queryScopeTypeName(StatisticScopeType sst)
  628. {
  629. return scopeTypeNames[sst];
  630. }
  631. extern jlib_decl StatisticScopeType queryScopeType(const char * sst)
  632. {
  633. //MORE: Use a hash table??
  634. return (StatisticScopeType)matchString(scopeTypeNames, sst);
  635. }
  636. //--------------------------------------------------------------------------------------------------------------------
  637. inline void mergeUpdate(StatisticMeasure measure, unsigned __int64 & value, const unsigned __int64 otherValue)
  638. {
  639. switch (measure)
  640. {
  641. case SMeasureTimeNs:
  642. case SMeasureCount:
  643. case SMeasureSize:
  644. case SMeasureLoad:
  645. case SMeasureSkew:
  646. case SMeasureCycle:
  647. value += otherValue;
  648. break;
  649. case SMeasureTimestampUs:
  650. if (otherValue && otherValue < value)
  651. value = otherValue;
  652. break;
  653. }
  654. }
  655. unsigned __int64 mergeStatistic(StatisticMeasure measure, unsigned __int64 value, unsigned __int64 otherValue)
  656. {
  657. mergeUpdate(measure, value, otherValue);
  658. return value;
  659. }
  660. unsigned __int64 mergeStatisticValue(unsigned __int64 prevValue, unsigned __int64 newValue, StatsMergeAction mergeAction)
  661. {
  662. switch (mergeAction)
  663. {
  664. case StatsMergeKeepNonZero:
  665. if (prevValue)
  666. return prevValue;
  667. return newValue;
  668. case StatsMergeAppend:
  669. case StatsMergeReplace:
  670. return newValue;
  671. case StatsMergeSum:
  672. return prevValue + newValue;
  673. case StatsMergeMin:
  674. if (prevValue > newValue)
  675. return newValue;
  676. else
  677. return prevValue;
  678. case StatsMergeMax:
  679. if (prevValue < newValue)
  680. return newValue;
  681. else
  682. return prevValue;
  683. default:
  684. throwUnexpected();
  685. }
  686. }
  687. //--------------------------------------------------------------------------------------------------------------------
  688. class CComponentStatistics
  689. {
  690. protected:
  691. StringAttr creator;
  692. byte creatorDepth;
  693. byte scopeDepth;
  694. // StatisticArray stats;
  695. };
  696. //--------------------------------------------------------------------------------------------------------------------
  697. static int compareUnsigned(unsigned const * left, unsigned const * right)
  698. {
  699. return (*left < *right) ? -1 : (*left > *right) ? +1 : 0;
  700. }
  701. StatisticsMapping::StatisticsMapping(StatisticKind kind, ...)
  702. {
  703. indexToKind.append(kind);
  704. va_list args;
  705. va_start(args, kind);
  706. for (;;)
  707. {
  708. unsigned next = va_arg(args, unsigned);
  709. if (!next)
  710. break;
  711. indexToKind.appendUniq(next);
  712. }
  713. va_end(args);
  714. createMappings();
  715. }
  716. StatisticsMapping::StatisticsMapping(const StatisticsMapping * from, ...)
  717. {
  718. ForEachItemIn(idx, from->indexToKind)
  719. indexToKind.append(from->indexToKind.item(idx));
  720. va_list args;
  721. va_start(args, from);
  722. for (;;)
  723. {
  724. unsigned next = va_arg(args, unsigned);
  725. if (!next)
  726. break;
  727. indexToKind.appendUniq(next);
  728. }
  729. va_end(args);
  730. createMappings();
  731. }
  732. StatisticsMapping::StatisticsMapping()
  733. {
  734. for (int i = StKindAll+1; i < StMax; i++)
  735. indexToKind.append(i);
  736. createMappings();
  737. }
  738. void StatisticsMapping::createMappings()
  739. {
  740. //Possibly not needed, but sort the kinds, so that it is easy to merge/stream the results out in the correct order.
  741. indexToKind.sort(compareUnsigned);
  742. //Provide mappings to all statistics to map them to the "unknown" bin by default
  743. for (unsigned i=0; i < StMax; i++)
  744. kindToIndex.append(numStatistics());
  745. ForEachItemIn(i2, indexToKind)
  746. {
  747. unsigned kind = indexToKind.item(i2);
  748. kindToIndex.replace(i2, kind);
  749. }
  750. }
  751. const StatisticsMapping allStatistics;
  752. const StatisticsMapping diskLocalStatistics(StCycleDiskReadIOCycles, StSizeDiskRead, StNumDiskReads, StCycleDiskWriteIOCycles, StSizeDiskWrite, StNumDiskWrites, StKindNone);
  753. const StatisticsMapping diskRemoteStatistics(StTimeDiskReadIO, StSizeDiskRead, StNumDiskReads, StTimeDiskWriteIO, StSizeDiskWrite, StNumDiskWrites, StKindNone);
  754. const StatisticsMapping diskReadRemoteStatistics(StTimeDiskReadIO, StSizeDiskRead, StNumDiskReads, StKindNone);
  755. const StatisticsMapping diskWriteRemoteStatistics(StTimeDiskWriteIO, StSizeDiskWrite, StNumDiskWrites, StKindNone);
  756. //--------------------------------------------------------------------------------------------------------------------
  757. class Statistic
  758. {
  759. public:
  760. Statistic(StatisticKind _kind, unsigned __int64 _value) : kind(_kind), value(_value)
  761. {
  762. }
  763. Statistic(MemoryBuffer & in, unsigned version)
  764. {
  765. unsigned _kind;
  766. in.read(_kind);
  767. kind = (StatisticKind)_kind;
  768. in.read(value);
  769. }
  770. virtual StatisticKind queryKind() const
  771. {
  772. return kind;
  773. }
  774. virtual unsigned __int64 queryValue() const
  775. {
  776. return value;
  777. }
  778. void merge(unsigned __int64 otherValue)
  779. {
  780. mergeUpdate(queryMeasure(kind), value, otherValue);
  781. }
  782. void serialize(MemoryBuffer & out) const
  783. {
  784. //MORE: Could compress - e.g., store as a packed integers
  785. out.append((unsigned)kind);
  786. out.append(value);
  787. }
  788. public:
  789. StatisticKind kind;
  790. unsigned __int64 value;
  791. };
  792. //--------------------------------------------------------------------------------------------------------------------
  793. StringBuffer & StatsScopeId::getScopeText(StringBuffer & out) const
  794. {
  795. switch (scopeType)
  796. {
  797. case SSTgraph:
  798. return out.append(GraphScopePrefix).append(id);
  799. case SSTsubgraph:
  800. return out.append(SubGraphScopePrefix).append(id);
  801. case SSTactivity:
  802. return out.append(ActivityScopePrefix).append(id);
  803. case SSTedge:
  804. return out.append(EdgeScopePrefix).append(id).append("_").append(extra);
  805. default:
  806. throwUnexpected();
  807. }
  808. }
  809. unsigned StatsScopeId::getHash() const
  810. {
  811. return hashc((const byte *)&id, sizeof(id), (unsigned)scopeType);
  812. }
  813. bool StatsScopeId::matches(const StatsScopeId & other) const
  814. {
  815. return (scopeType == other.scopeType) && (id == other.id) && (extra == other.extra);
  816. }
  817. unsigned StatsScopeId::queryActivity() const
  818. {
  819. switch (scopeType)
  820. {
  821. case SSTactivity:
  822. case SSTedge:
  823. return id;
  824. default:
  825. return 0;
  826. }
  827. }
  828. void StatsScopeId::deserialize(MemoryBuffer & in, unsigned version)
  829. {
  830. byte scopeTypeByte;
  831. in.read(scopeTypeByte);
  832. scopeType = (StatisticScopeType)scopeTypeByte;
  833. switch (scopeType)
  834. {
  835. case SSTgraph:
  836. case SSTsubgraph:
  837. case SSTactivity:
  838. in.read(id);
  839. break;
  840. case SSTedge:
  841. in.read(id);
  842. in.read(extra);
  843. break;
  844. default:
  845. throwUnexpected();
  846. }
  847. }
  848. void StatsScopeId::serialize(MemoryBuffer & out) const
  849. {
  850. out.append((byte)scopeType);
  851. switch (scopeType)
  852. {
  853. case SSTgraph:
  854. case SSTsubgraph:
  855. case SSTactivity:
  856. out.append(id);
  857. break;
  858. case SSTedge:
  859. out.append(id);
  860. out.append(extra);
  861. break;
  862. default:
  863. throwUnexpected();
  864. }
  865. }
  866. void StatsScopeId::setId(StatisticScopeType _scopeType, unsigned _id, unsigned _extra)
  867. {
  868. scopeType = _scopeType;
  869. id = _id;
  870. extra = _extra;
  871. }
  872. bool StatsScopeId::setScopeText(const char * text)
  873. {
  874. if (MATCHES_CONST_PREFIX(text, ActivityScopePrefix))
  875. setActivityId(atoi(text + CONST_STRLEN(ActivityScopePrefix)));
  876. else if (MATCHES_CONST_PREFIX(text, GraphScopePrefix))
  877. setId(SSTgraph, atoi(text + CONST_STRLEN(GraphScopePrefix)));
  878. else if (MATCHES_CONST_PREFIX(text, SubGraphScopePrefix))
  879. setSubgraphId(atoi(text + CONST_STRLEN(SubGraphScopePrefix)));
  880. else if (MATCHES_CONST_PREFIX(text, EdgeScopePrefix))
  881. {
  882. const char * underscore = strchr(text, '_');
  883. if (!underscore)
  884. return false;
  885. setEdgeId(atoi(text + CONST_STRLEN(EdgeScopePrefix)), atoi(underscore+1));
  886. }
  887. else
  888. return false;
  889. return true;
  890. }
  891. void StatsScopeId::setActivityId(unsigned _id)
  892. {
  893. setId(SSTactivity, _id);
  894. }
  895. void StatsScopeId::setEdgeId(unsigned _id, unsigned _output)
  896. {
  897. setId(SSTedge, _id, _output);
  898. }
  899. void StatsScopeId::setSubgraphId(unsigned _id)
  900. {
  901. setId(SSTsubgraph, _id);
  902. }
  903. //--------------------------------------------------------------------------------------------------------------------
  904. enum
  905. {
  906. SCroot,
  907. SCintermediate,
  908. SCleaf,
  909. };
  910. class CStatisticCollection;
  911. static CStatisticCollection * deserializeCollection(CStatisticCollection * parent, MemoryBuffer & in, unsigned version);
  912. //MORE: Create an implementation with no children
  913. typedef StructArrayOf<Statistic> StatsArray;
  914. class CollectionHashTable : public SuperHashTableOf<CStatisticCollection, StatsScopeId>
  915. {
  916. public:
  917. ~CollectionHashTable() { _releaseAll(); }
  918. virtual void onAdd(void *et);
  919. virtual void onRemove(void *et);
  920. virtual unsigned getHashFromElement(const void *et) const;
  921. virtual unsigned getHashFromFindParam(const void *fp) const;
  922. virtual const void * getFindParam(const void *et) const;
  923. virtual bool matchesFindParam(const void *et, const void *key, unsigned fphash) const;
  924. virtual bool matchesElement(const void *et, const void *searchET) const;
  925. };
  926. typedef IArrayOf<CStatisticCollection> CollectionArray;
  927. class CStatisticCollection : public CInterfaceOf<IStatisticCollection>
  928. {
  929. friend class CollectionHashTable;
  930. public:
  931. CStatisticCollection(CStatisticCollection * _parent, const StatsScopeId & _id) : id(_id), parent(_parent)
  932. {
  933. }
  934. CStatisticCollection(CStatisticCollection * _parent, MemoryBuffer & in, unsigned version) : parent(_parent)
  935. {
  936. id.deserialize(in, version);
  937. unsigned numStats;
  938. in.read(numStats);
  939. stats.ensure(numStats);
  940. while (numStats-- > 0)
  941. {
  942. Statistic next (in, version);
  943. stats.append(next);
  944. }
  945. unsigned numChildren;
  946. in.read(numChildren);
  947. children.ensure(numChildren);
  948. while (numChildren-- > 0)
  949. {
  950. CStatisticCollection * next = deserializeCollection(this, in, version);
  951. children.add(*next);
  952. }
  953. }
  954. virtual byte getCollectionType() const { return SCintermediate; }
  955. //interface IStatisticCollection:
  956. virtual StatisticScopeType queryScopeType() const
  957. {
  958. return id.queryScopeType();
  959. }
  960. virtual unsigned __int64 queryWhenCreated() const
  961. {
  962. if (parent)
  963. return parent->queryWhenCreated();
  964. return 0;
  965. }
  966. virtual StringBuffer & getScope(StringBuffer & str) const
  967. {
  968. return id.getScopeText(str);
  969. }
  970. virtual StringBuffer & getFullScope(StringBuffer & str) const
  971. {
  972. if (parent)
  973. {
  974. parent->getFullScope(str);
  975. str.append(':');
  976. }
  977. id.getScopeText(str);
  978. return str;
  979. }
  980. virtual unsigned __int64 queryStatistic(StatisticKind kind) const
  981. {
  982. ForEachItemIn(i, stats)
  983. {
  984. const Statistic & cur = stats.item(i);
  985. if (cur.kind == kind)
  986. return cur.value;
  987. }
  988. return 0;
  989. }
  990. virtual unsigned getNumStatistics() const
  991. {
  992. return stats.ordinality();
  993. }
  994. virtual void getStatistic(StatisticKind & kind, unsigned __int64 & value, unsigned idx) const
  995. {
  996. const Statistic & cur = stats.item(idx);
  997. kind = cur.kind;
  998. value = cur.value;
  999. }
  1000. virtual IStatisticCollectionIterator & getScopes(const char * filter)
  1001. {
  1002. assertex(!filter);
  1003. return * new SuperHashIIteratorOf<IStatisticCollection, IStatisticCollectionIterator, false>(children);
  1004. }
  1005. virtual void getMinMaxScope(IStringVal & minValue, IStringVal & maxValue, StatisticScopeType searchScopeType) const
  1006. {
  1007. if (id.queryScopeType() == searchScopeType)
  1008. {
  1009. const char * curMin = minValue.str();
  1010. const char * curMax = maxValue.str();
  1011. StringBuffer name;
  1012. id.getScopeText(name);
  1013. if (!curMin || !*curMin || strcmp(name.str(), curMin) < 0)
  1014. minValue.set(name.str());
  1015. if (!curMax || strcmp(name.str(), curMax) > 0)
  1016. maxValue.set(name.str());
  1017. }
  1018. SuperHashIteratorOf<CStatisticCollection> iter(children, false);
  1019. for (iter.first(); iter.isValid(); iter.next())
  1020. iter.query().getMinMaxScope(minValue, maxValue, searchScopeType);
  1021. }
  1022. virtual void getMinMaxActivity(unsigned & minValue, unsigned & maxValue) const
  1023. {
  1024. unsigned activityId = id.queryActivity();
  1025. if (activityId)
  1026. {
  1027. if ((minValue == 0) || (activityId < minValue))
  1028. minValue = activityId;
  1029. if (activityId > maxValue)
  1030. maxValue = activityId;
  1031. }
  1032. SuperHashIteratorOf<CStatisticCollection> iter(children, false);
  1033. for (iter.first(); iter.isValid(); iter.next())
  1034. iter.query().getMinMaxActivity(minValue, maxValue);
  1035. }
  1036. //other public interface functions
  1037. void addStatistic(StatisticKind kind, unsigned __int64 value)
  1038. {
  1039. Statistic s(kind, value);
  1040. stats.append(s);
  1041. }
  1042. void updateStatistic(StatisticKind kind, unsigned __int64 value, StatsMergeAction mergeAction)
  1043. {
  1044. if (mergeAction != StatsMergeAppend)
  1045. {
  1046. ForEachItemIn(i, stats)
  1047. {
  1048. Statistic & cur = stats.element(i);
  1049. if (cur.kind == kind)
  1050. {
  1051. cur.value = mergeStatisticValue(cur.value, value, mergeAction);
  1052. return;
  1053. }
  1054. }
  1055. }
  1056. Statistic s(kind, value);
  1057. stats.append(s);
  1058. }
  1059. CStatisticCollection * ensureSubScope(const StatsScopeId & search, bool hasChildren)
  1060. {
  1061. //MORE: Implement hasChildren
  1062. return resolveSubScope(search, true, false);
  1063. }
  1064. CStatisticCollection * resolveSubScope(const StatsScopeId & search, bool create, bool replace)
  1065. {
  1066. if (!replace)
  1067. {
  1068. CStatisticCollection * match = children.find(&search);
  1069. if (match)
  1070. return LINK(match);
  1071. }
  1072. if (create)
  1073. {
  1074. CStatisticCollection * ret = new CStatisticCollection(this, search);
  1075. children.add(*ret);
  1076. return LINK(ret);
  1077. }
  1078. return NULL;
  1079. }
  1080. virtual void serialize(MemoryBuffer & out) const
  1081. {
  1082. out.append(getCollectionType());
  1083. id.serialize(out);
  1084. out.append(stats.ordinality());
  1085. ForEachItemIn(iStat, stats)
  1086. stats.item(iStat).serialize(out);
  1087. out.append(children.ordinality());
  1088. SuperHashIteratorOf<CStatisticCollection> iter(children, false);
  1089. for (iter.first(); iter.isValid(); iter.next())
  1090. iter.query().serialize(out);
  1091. }
  1092. private:
  1093. StatsScopeId id;
  1094. CStatisticCollection * parent;
  1095. CollectionHashTable children;
  1096. StatsArray stats;
  1097. };
  1098. //---------------------------------------------------------------------------------------------------------------------
  1099. void CollectionHashTable::onAdd(void *et)
  1100. {
  1101. }
  1102. void CollectionHashTable::onRemove(void *et)
  1103. {
  1104. CStatisticCollection * elem = reinterpret_cast<CStatisticCollection *>(et);
  1105. elem->Release();
  1106. }
  1107. unsigned CollectionHashTable::getHashFromElement(const void *et) const
  1108. {
  1109. const CStatisticCollection * elem = reinterpret_cast<const CStatisticCollection *>(et);
  1110. return elem->id.getHash();
  1111. }
  1112. unsigned CollectionHashTable::getHashFromFindParam(const void *fp) const
  1113. {
  1114. const StatsScopeId * search = reinterpret_cast<const StatsScopeId *>(fp);
  1115. return search->getHash();
  1116. }
  1117. const void * CollectionHashTable::getFindParam(const void *et) const
  1118. {
  1119. const CStatisticCollection * elem = reinterpret_cast<const CStatisticCollection *>(et);
  1120. return &elem->id;
  1121. }
  1122. bool CollectionHashTable::matchesFindParam(const void *et, const void *key, unsigned fphash) const
  1123. {
  1124. const CStatisticCollection * elem = reinterpret_cast<const CStatisticCollection *>(et);
  1125. const StatsScopeId * search = reinterpret_cast<const StatsScopeId *>(key);
  1126. return elem->id.matches(*search);
  1127. }
  1128. bool CollectionHashTable::matchesElement(const void *et, const void *searchET) const
  1129. {
  1130. const CStatisticCollection * elem = reinterpret_cast<const CStatisticCollection *>(et);
  1131. const CStatisticCollection * searchElem = reinterpret_cast<const CStatisticCollection *>(searchET);
  1132. return elem->id.matches(searchElem->id);
  1133. }
  1134. //---------------------------------------------------------------------------------------------------------------------
  1135. class CRootStatisticCollection : public CStatisticCollection
  1136. {
  1137. public:
  1138. CRootStatisticCollection(StatisticCreatorType _creatorType, const char * _creator, const StatsScopeId & _id)
  1139. : CStatisticCollection(NULL, _id), creatorType(_creatorType), creator(_creator)
  1140. {
  1141. whenCreated = getTimeStampNowValue();
  1142. }
  1143. CRootStatisticCollection(MemoryBuffer & in, unsigned version) : CStatisticCollection(NULL, in, version)
  1144. {
  1145. byte creatorTypeByte;
  1146. in.read(creatorTypeByte);
  1147. creatorType = (StatisticCreatorType)creatorTypeByte;
  1148. in.read(creator);
  1149. in.read(whenCreated);
  1150. }
  1151. virtual byte getCollectionType() const { return SCroot; }
  1152. virtual unsigned __int64 queryWhenCreated() const
  1153. {
  1154. return whenCreated;
  1155. }
  1156. virtual void serialize(MemoryBuffer & out) const
  1157. {
  1158. CStatisticCollection::serialize(out);
  1159. out.append((byte)creatorType);
  1160. out.append(creator);
  1161. out.append(whenCreated);
  1162. }
  1163. public:
  1164. StatisticCreatorType creatorType;
  1165. StringAttr creator;
  1166. unsigned __int64 whenCreated;
  1167. };
  1168. //---------------------------------------------------------------------------------------------------------------------
  1169. void serializeStatisticCollection(MemoryBuffer & out, IStatisticCollection * collection)
  1170. {
  1171. unsigned currentStatisticsVersion = 1;
  1172. out.append(currentStatisticsVersion);
  1173. collection->serialize(out);
  1174. }
  1175. static CStatisticCollection * deserializeCollection(CStatisticCollection * parent, MemoryBuffer & in, unsigned version)
  1176. {
  1177. byte kind;
  1178. in.read(kind);
  1179. switch (kind)
  1180. {
  1181. case SCroot:
  1182. assertex(!parent);
  1183. return new CRootStatisticCollection(in, version);
  1184. case SCintermediate:
  1185. return new CStatisticCollection(parent, in, version);
  1186. default:
  1187. UNIMPLEMENTED;
  1188. }
  1189. }
  1190. IStatisticCollection * createStatisticCollection(MemoryBuffer & in)
  1191. {
  1192. unsigned version;
  1193. in.read(version);
  1194. return deserializeCollection(NULL, in, version);
  1195. }
  1196. //--------------------------------------------------------------------------------------------------------------------
  1197. class StatisticGatherer : implements CInterfaceOf<IStatisticGatherer>
  1198. {
  1199. public:
  1200. StatisticGatherer(CStatisticCollection * scope) : rootScope(scope)
  1201. {
  1202. scopes.append(*scope);
  1203. }
  1204. virtual void beginScope(const StatsScopeId & id)
  1205. {
  1206. CStatisticCollection & tos = scopes.tos();
  1207. scopes.append(*tos.ensureSubScope(id, true));
  1208. }
  1209. virtual void beginActivityScope(unsigned id)
  1210. {
  1211. StatsScopeId scopeId(SSTactivity, id);
  1212. CStatisticCollection & tos = scopes.tos();
  1213. scopes.append(*tos.ensureSubScope(scopeId, false));
  1214. }
  1215. virtual void beginSubGraphScope(unsigned id)
  1216. {
  1217. StatsScopeId scopeId(SSTsubgraph, id);
  1218. CStatisticCollection & tos = scopes.tos();
  1219. scopes.append(*tos.ensureSubScope(scopeId, true));
  1220. }
  1221. virtual void beginEdgeScope(unsigned id, unsigned oid)
  1222. {
  1223. StatsScopeId scopeId(SSTedge, id, oid);
  1224. CStatisticCollection & tos = scopes.tos();
  1225. scopes.append(*tos.ensureSubScope(scopeId, false));
  1226. }
  1227. virtual void endScope()
  1228. {
  1229. scopes.tos().Release();
  1230. scopes.pop();
  1231. }
  1232. virtual void addStatistic(StatisticKind kind, unsigned __int64 value)
  1233. {
  1234. CStatisticCollection & tos = scopes.tos();
  1235. tos.addStatistic(kind, value);
  1236. }
  1237. virtual void updateStatistic(StatisticKind kind, unsigned __int64 value, StatsMergeAction mergeAction)
  1238. {
  1239. CStatisticCollection & tos = scopes.tos();
  1240. tos.updateStatistic(kind, value, mergeAction);
  1241. }
  1242. virtual IStatisticCollection * getResult()
  1243. {
  1244. return LINK(rootScope);
  1245. }
  1246. protected:
  1247. ICopyArrayOf<CStatisticCollection> scopes;
  1248. Linked<CStatisticCollection> rootScope;
  1249. };
  1250. extern IStatisticGatherer * createStatisticsGatherer(StatisticCreatorType creatorType, const char * creator, const StatsScopeId & rootScope)
  1251. {
  1252. //creator unused at the moment.
  1253. Owned<CStatisticCollection> rootCollection = new CRootStatisticCollection(creatorType, creator, rootScope);
  1254. return new StatisticGatherer(rootCollection);
  1255. }
  1256. //--------------------------------------------------------------------------------------------------------------------
  1257. void CRuntimeStatistic::merge(unsigned __int64 otherValue, StatsMergeAction mergeAction)
  1258. {
  1259. value = mergeStatisticValue(value, otherValue, mergeAction);
  1260. }
  1261. void CRuntimeStatisticCollection::merge(const CRuntimeStatisticCollection & other)
  1262. {
  1263. ForEachItemIn(i, other)
  1264. {
  1265. StatisticKind kind = other.getKind(i);
  1266. unsigned __int64 value = other.getStatisticValue(kind);
  1267. if (value)
  1268. mergeStatistic(kind, other.getStatisticValue(kind));
  1269. }
  1270. }
  1271. void CRuntimeStatisticCollection::mergeStatistic(StatisticKind kind, unsigned __int64 value)
  1272. {
  1273. queryStatistic(kind).merge(value, queryMergeMode(kind));
  1274. }
  1275. void CRuntimeStatisticCollection::rollupStatistics(unsigned numTargets, IContextLogger * const * targets) const
  1276. {
  1277. ForEachItem(iStat)
  1278. {
  1279. unsigned __int64 value = values[iStat].getClear();
  1280. if (value)
  1281. {
  1282. StatisticKind kind = getKind(iStat);
  1283. for (unsigned iTarget = 0; iTarget < numTargets; iTarget++)
  1284. targets[iTarget]->noteStatistic(kind, value);
  1285. }
  1286. }
  1287. reportIgnoredStats();
  1288. }
  1289. void CRuntimeStatisticCollection::recordStatistics(IStatisticGatherer & target) const
  1290. {
  1291. ForEachItem(i)
  1292. {
  1293. unsigned __int64 value = values[i].get();
  1294. if (value)
  1295. {
  1296. StatisticKind kind = getKind(i);
  1297. StatsMergeAction mergeAction = queryMergeMode(kind);
  1298. target.updateStatistic(kind, values[i].get(), mergeAction);
  1299. }
  1300. }
  1301. reportIgnoredStats();
  1302. }
  1303. void CRuntimeStatisticCollection::reportIgnoredStats() const
  1304. {
  1305. if (values[mapping.numStatistics()].getClear())
  1306. DBGLOG("Some statistics were added but thrown away");
  1307. }
  1308. StringBuffer & CRuntimeStatisticCollection::toXML(StringBuffer &str) const
  1309. {
  1310. ForEachItem(iStat)
  1311. {
  1312. unsigned __int64 value = values[iStat].get();
  1313. if (value)
  1314. {
  1315. StatisticKind kind = getKind(iStat);
  1316. const char * name = queryStatisticName(kind);
  1317. str.appendf("<%s>%" I64F "d</%s>", name, value, name);
  1318. }
  1319. }
  1320. return str;
  1321. }
  1322. StringBuffer & CRuntimeStatisticCollection::toStr(StringBuffer &str) const
  1323. {
  1324. ForEachItem(iStat)
  1325. {
  1326. unsigned __int64 value = values[iStat].get();
  1327. if (value)
  1328. {
  1329. StatisticKind kind = getKind(iStat);
  1330. const char * name = queryStatisticName(kind);
  1331. str.append(' ').append(name).append("=");
  1332. formatStatistic(str, value, kind);
  1333. }
  1334. }
  1335. return str;
  1336. }
  1337. void CRuntimeStatisticCollection::deserialize(MemoryBuffer& in)
  1338. {
  1339. unsigned numValid;
  1340. in.readPacked(numValid);
  1341. for (unsigned i=0; i < numValid; i++)
  1342. {
  1343. unsigned kindVal;
  1344. unsigned __int64 value;
  1345. in.readPacked(kindVal).readPacked(value);
  1346. StatisticKind kind = (StatisticKind)kindVal;
  1347. setStatistic(kind, value);
  1348. }
  1349. }
  1350. void CRuntimeStatisticCollection::deserializeMerge(MemoryBuffer& in)
  1351. {
  1352. unsigned numValid;
  1353. in.readPacked(numValid);
  1354. for (unsigned i=0; i < numValid; i++)
  1355. {
  1356. unsigned kindVal;
  1357. unsigned __int64 value;
  1358. in.readPacked(kindVal).readPacked(value);
  1359. StatisticKind kind = (StatisticKind)kindVal;
  1360. StatsMergeAction mergeAction = queryMergeMode(kind);
  1361. mergeStatistic(kind, value, mergeAction);
  1362. }
  1363. }
  1364. bool CRuntimeStatisticCollection::serialize(MemoryBuffer& out) const
  1365. {
  1366. unsigned numValid = 0;
  1367. ForEachItem(i1)
  1368. {
  1369. if (values[i1].get())
  1370. numValid++;
  1371. }
  1372. out.appendPacked(numValid);
  1373. ForEachItem(i2)
  1374. {
  1375. unsigned __int64 value = values[i2].get();
  1376. if (value)
  1377. {
  1378. out.appendPacked((unsigned)mapping.getKind(i2));
  1379. out.appendPacked(value);
  1380. }
  1381. }
  1382. return numValid != 0;
  1383. }
  1384. //---------------------------------------------------
  1385. bool ScopedItemFilter::matchDepth(unsigned low, unsigned high) const
  1386. {
  1387. if (maxDepth && low && maxDepth < low)
  1388. return false;
  1389. if (minDepth && high && minDepth > high)
  1390. return false;
  1391. return true;
  1392. }
  1393. bool ScopedItemFilter::match(const char * search) const
  1394. {
  1395. if (search)
  1396. {
  1397. if (value)
  1398. {
  1399. if (hasWildcard)
  1400. {
  1401. //MORE: If wildcarding ends up being used a lot then this should be replaced with something that creates a DFA
  1402. if (!WildMatch(search, value, false))
  1403. return false;
  1404. }
  1405. else
  1406. {
  1407. return streq(search, value);
  1408. }
  1409. }
  1410. if (minDepth || maxDepth)
  1411. {
  1412. unsigned searchDepth = queryStatisticsDepth(search);
  1413. if (searchDepth < minDepth)
  1414. return false;
  1415. if (maxDepth && searchDepth > maxDepth)
  1416. return false;
  1417. }
  1418. }
  1419. return true;
  1420. }
  1421. bool ScopedItemFilter::recurseChildScopes(const char * curScope) const
  1422. {
  1423. if (maxDepth == 0 || !curScope)
  1424. return true;
  1425. if (queryStatisticsDepth(curScope) >= maxDepth)
  1426. return false;
  1427. return true;
  1428. }
  1429. void ScopedItemFilter::set(const char * _value)
  1430. {
  1431. if (_value && *_value && !streq(_value, "*") )
  1432. {
  1433. value.set(_value);
  1434. minDepth = queryStatisticsDepth(_value);
  1435. if (!strchr(_value, '*'))
  1436. {
  1437. maxDepth = minDepth;
  1438. hasWildcard = strchr(_value, '?') != NULL;
  1439. }
  1440. else
  1441. hasWildcard = true;
  1442. }
  1443. else
  1444. value.clear();
  1445. }
  1446. void ScopedItemFilter::setDepth(unsigned _depth)
  1447. {
  1448. minDepth = _depth;
  1449. maxDepth = _depth;
  1450. }
  1451. void ScopedItemFilter::setDepth(unsigned _minDepth, unsigned _maxDepth)
  1452. {
  1453. minDepth = _minDepth;
  1454. maxDepth = _maxDepth;
  1455. }
  1456. StatisticsFilter::StatisticsFilter()
  1457. {
  1458. init();
  1459. }
  1460. StatisticsFilter::StatisticsFilter(const char * filter)
  1461. {
  1462. init();
  1463. setFilter(filter);
  1464. }
  1465. StatisticsFilter::StatisticsFilter(StatisticCreatorType _creatorType, StatisticScopeType _scopeType, StatisticMeasure _measure, StatisticKind _kind)
  1466. {
  1467. init();
  1468. creatorType = _creatorType;
  1469. scopeType = _scopeType;
  1470. measure = _measure;
  1471. kind = _kind;
  1472. }
  1473. StatisticsFilter::StatisticsFilter(const char * _creatorType, const char * _scopeType, const char * _kind)
  1474. {
  1475. init();
  1476. set(_creatorType, _scopeType, _kind);
  1477. }
  1478. StatisticsFilter::StatisticsFilter(const char * _creatorTypeText, const char * _creator, const char * _scopeTypeText, const char * _scope, const char * _measureText, const char * _kindText)
  1479. {
  1480. init();
  1481. set(_creatorTypeText, _creator, _scopeTypeText, _scope, _measureText, _kindText);
  1482. }
  1483. StatisticsFilter::StatisticsFilter(StatisticCreatorType _creatorType, const char * _creator, StatisticScopeType _scopeType, const char * _scope, StatisticMeasure _measure, StatisticKind _kind)
  1484. {
  1485. init();
  1486. creatorType = _creatorType;
  1487. setCreator(_creator);
  1488. scopeType = _scopeType;
  1489. setScope(_scope);
  1490. measure = _measure;
  1491. kind = _kind;
  1492. }
  1493. void StatisticsFilter::init()
  1494. {
  1495. creatorType = SCTall;
  1496. scopeType = SSTall;
  1497. measure = SMeasureAll;
  1498. kind = StKindAll;
  1499. }
  1500. bool StatisticsFilter::matches(StatisticCreatorType curCreatorType, const char * curCreator, StatisticScopeType curScopeType, const char * curScope, StatisticMeasure curMeasure, StatisticKind curKind) const
  1501. {
  1502. if ((curCreatorType != SCTall) && (creatorType != SCTall) && (creatorType != curCreatorType))
  1503. return false;
  1504. if ((curScopeType != SSTall) && (scopeType != SSTall) && (scopeType != curScopeType))
  1505. return false;
  1506. if ((curMeasure != SMeasureAll) && (measure != SMeasureAll) && (measure != curMeasure))
  1507. return false;
  1508. if ((curKind!= StKindAll) && (kind != StKindAll) && (kind != curKind))
  1509. return false;
  1510. if (!creatorFilter.match(curCreator))
  1511. return false;
  1512. if (!scopeFilter.match(curScope))
  1513. return false;
  1514. return true;
  1515. }
  1516. bool StatisticsFilter::recurseChildScopes(StatisticScopeType curScopeType, const char * curScope) const
  1517. {
  1518. return scopeFilter.recurseChildScopes(curScope);
  1519. }
  1520. void StatisticsFilter::set(const char * creatorTypeText, const char * scopeTypeText, const char * kindText)
  1521. {
  1522. StatisticCreatorType creatorType = queryCreatorType(creatorTypeText);
  1523. StatisticScopeType scopeType = queryScopeType(scopeTypeText);
  1524. if (creatorType != SCTnone)
  1525. setCreatorType(creatorType);
  1526. if (scopeType != SSTnone)
  1527. setScopeType(scopeType);
  1528. setKind(kindText);
  1529. }
  1530. void StatisticsFilter::set(const char * _creatorTypeText, const char * _creator, const char * _scopeTypeText, const char * _scope, const char * _measureText, const char * _kindText)
  1531. {
  1532. StatisticMeasure newMeasure = queryMeasure(_measureText);
  1533. if (newMeasure != SMeasureNone)
  1534. setMeasure(newMeasure);
  1535. set(_creatorTypeText, _scopeTypeText, _kindText);
  1536. setCreator(_creator);
  1537. setScope(_scope);
  1538. }
  1539. void StatisticsFilter::setCreatorDepth(unsigned _minCreatorDepth, unsigned _maxCreatorDepth)
  1540. {
  1541. creatorFilter.setDepth(_minCreatorDepth, _maxCreatorDepth);
  1542. }
  1543. void StatisticsFilter::setCreator(const char * _creator)
  1544. {
  1545. creatorFilter.set(_creator);
  1546. }
  1547. void StatisticsFilter::setCreatorType(StatisticCreatorType _creatorType)
  1548. {
  1549. creatorType = _creatorType;
  1550. }
  1551. void StatisticsFilter::setFilter(const char * filter)
  1552. {
  1553. //MORE: Process the filter from a text representation
  1554. }
  1555. void StatisticsFilter::setScopeDepth(unsigned _scopeDepth)
  1556. {
  1557. scopeFilter.setDepth(_scopeDepth);
  1558. }
  1559. void StatisticsFilter::setScopeDepth(unsigned _minScopeDepth, unsigned _maxScopeDepth)
  1560. {
  1561. scopeFilter.setDepth(_minScopeDepth, _maxScopeDepth);
  1562. }
  1563. void StatisticsFilter::setScope(const char * _scope)
  1564. {
  1565. scopeFilter.set(_scope);
  1566. }
  1567. void StatisticsFilter::setScopeType(StatisticScopeType _scopeType)
  1568. {
  1569. scopeType = _scopeType;
  1570. }
  1571. void StatisticsFilter::setMeasure(StatisticMeasure _measure)
  1572. {
  1573. measure = _measure;
  1574. }
  1575. void StatisticsFilter::setKind(StatisticKind _kind)
  1576. {
  1577. kind = _kind;
  1578. if (measure == SMeasureAll)
  1579. measure = queryMeasure(kind);
  1580. }
  1581. void StatisticsFilter::setKind(const char * _kind)
  1582. {
  1583. if (!_kind || !*_kind || streq(_kind, "*"))
  1584. {
  1585. if (measure == SMeasureNone)
  1586. measure = SMeasureAll;
  1587. kind = StKindAll;
  1588. return;
  1589. }
  1590. //Convert a kind wildcard to a measure
  1591. for (unsigned i1=SMeasureAll+1; i1 < SMeasureMax; i1++)
  1592. {
  1593. const char * prefix = queryMeasurePrefix((StatisticMeasure)i1);
  1594. size_t len = strlen(prefix);
  1595. if (strnicmp(_kind, prefix, len) == 0)
  1596. {
  1597. setMeasure((StatisticMeasure)i1);
  1598. //Treat When* and When as filters on times.
  1599. if (streq(_kind + len, "*") || !_kind[len])
  1600. return;
  1601. }
  1602. }
  1603. //Other wildcards not currently supported
  1604. kind = queryStatisticKind(_kind);
  1605. }
  1606. //---------------------------------------------------
  1607. class CStatsCategory : public CInterface
  1608. {
  1609. public:
  1610. StringAttr longName;
  1611. StringAttr shortName;
  1612. CStatsCategory(const char *_longName, const char *_shortName)
  1613. : longName(_longName), shortName(_shortName)
  1614. {
  1615. }
  1616. bool match(const char *_longName, const char *_shortName)
  1617. {
  1618. bool lm = stricmp(_longName, longName)==0;
  1619. bool sm = stricmp(_shortName, shortName)==0;
  1620. if (lm || sm)
  1621. {
  1622. if (lm && sm)
  1623. return true;
  1624. throw MakeStringException(0, "A stats category %s (%s) is already registered", shortName.get(), longName.get());
  1625. }
  1626. return false;
  1627. }
  1628. };
  1629. static CIArrayOf<CStatsCategory> statsCategories;
  1630. static CriticalSection statsCategoriesCrit;
  1631. extern int registerStatsCategory(const char *longName, const char *shortName)
  1632. {
  1633. CriticalBlock b(statsCategoriesCrit);
  1634. ForEachItemIn(idx, statsCategories)
  1635. {
  1636. if (statsCategories.item(idx).match(longName, shortName))
  1637. return idx;
  1638. }
  1639. statsCategories.append(*new CStatsCategory(longName, shortName));
  1640. return statsCategories.ordinality()-1;
  1641. }
  1642. static void checkKind(StatisticKind kind)
  1643. {
  1644. if (kind < StMax)
  1645. {
  1646. const StatisticMeta & meta = statsMetaData[kind];
  1647. if (meta.kind != kind)
  1648. throw makeStringExceptionV(0, "Statistic %u in the wrong order", kind);
  1649. }
  1650. StatisticMeasure measure = queryMeasure(kind);
  1651. const char * shortName = queryStatisticName(kind);
  1652. StringBuffer longName;
  1653. queryLongStatisticName(longName, kind);
  1654. const char * tagName __attribute__ ((unused)) = queryTreeTag(kind);
  1655. const char * prefix = queryMeasurePrefix(measure);
  1656. //Check short names are all correctly prefixed.
  1657. assertex(strncmp(shortName, prefix, strlen(prefix)) == 0);
  1658. }
  1659. static void checkDistributedKind(StatisticKind kind)
  1660. {
  1661. checkKind(kind);
  1662. checkKind((StatisticKind)(kind|StMinX));
  1663. checkKind((StatisticKind)(kind|StMaxX));
  1664. checkKind((StatisticKind)(kind|StAvgX));
  1665. checkKind((StatisticKind)(kind|StSkew));
  1666. checkKind((StatisticKind)(kind|StSkewMin));
  1667. checkKind((StatisticKind)(kind|StSkewMax));
  1668. checkKind((StatisticKind)(kind|StNodeMin));
  1669. checkKind((StatisticKind)(kind|StNodeMax));
  1670. checkKind((StatisticKind)(kind|StDeltaX));
  1671. }
  1672. void verifyStatisticFunctions()
  1673. {
  1674. assertex(_elements_in(measureNames) == SMeasureMax+1 && !measureNames[SMeasureMax]);
  1675. assertex(_elements_in(creatorTypeNames) == SCTmax+1 && !creatorTypeNames[SCTmax]);
  1676. assertex(_elements_in(scopeTypeNames) == SSTmax+1 && !scopeTypeNames[SSTmax]);
  1677. //Check the various functions return values for all possible values.
  1678. for (unsigned i1=SMeasureAll; i1 < SMeasureMax; i1++)
  1679. {
  1680. const char * prefix __attribute__((unused)) = queryMeasurePrefix((StatisticMeasure)i1);
  1681. const char * name = queryMeasureName((StatisticMeasure)i1);
  1682. assertex(queryMeasure(name) == i1);
  1683. }
  1684. for (StatisticScopeType sst = SSTnone; sst < SSTmax; sst = (StatisticScopeType)(sst+1))
  1685. {
  1686. const char * name = queryScopeTypeName(sst);
  1687. assertex(queryScopeType(name) == sst);
  1688. }
  1689. for (StatisticCreatorType sct = SCTnone; sct < SCTmax; sct = (StatisticCreatorType)(sct+1))
  1690. {
  1691. const char * name = queryCreatorTypeName(sct);
  1692. assertex(queryCreatorType(name) == sct);
  1693. }
  1694. for (unsigned i2=StKindAll+1; i2 < StMax; i2++)
  1695. {
  1696. checkDistributedKind((StatisticKind)i2);
  1697. }
  1698. }
  1699. #if 0
  1700. MODULE_INIT(INIT_PRIORITY_STANDARD)
  1701. {
  1702. verifyStatisticFunctions();
  1703. return true;
  1704. }
  1705. #endif