jstats.cpp 109 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742
  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. #include "jerror.hpp"
  21. #include <math.h>
  22. #include <sstream>
  23. #include <iomanip>
  24. #include <ctype.h>
  25. #include <cmath>
  26. #ifdef _WIN32
  27. #include <sys/timeb.h>
  28. #endif
  29. static CriticalSection statsNameCs;
  30. static StringBuffer statisticsComponentName;
  31. static StatisticCreatorType statisticsComponentType = SCTunknown;
  32. const static unsigned currentStatisticsVersion = 1;
  33. StatisticCreatorType queryStatisticsComponentType()
  34. {
  35. return statisticsComponentType;
  36. }
  37. const char * queryStatisticsComponentName()
  38. {
  39. CriticalBlock c(statsNameCs);
  40. if (statisticsComponentName.length() == 0)
  41. {
  42. statisticsComponentName.append("unknown").append(GetCachedHostName());
  43. DBGLOG("getProcessUniqueName hasn't been configured correctly");
  44. }
  45. return statisticsComponentName.str();
  46. }
  47. void setStatisticsComponentName(StatisticCreatorType processType, const char * processName, bool appendIP)
  48. {
  49. if (!processName)
  50. return;
  51. CriticalBlock c(statsNameCs);
  52. statisticsComponentType = processType;
  53. statisticsComponentName.clear().append(processName);
  54. if (appendIP)
  55. statisticsComponentName.append("@").append(GetCachedHostName()); // should I use _ instead?
  56. }
  57. //--------------------------------------------------------------------------------------------------------------------
  58. // Textual forms of the different enumerations, first items are for none and all.
  59. static constexpr const char * const measureNames[] = { "", "all", "ns", "ts", "cnt", "sz", "cpu", "skw", "node", "ppm", "ip", "cy", "en", "txt", "bool", "id", "fname", "cost", NULL };
  60. static constexpr const char * const creatorTypeNames[]= { "", "all", "unknown", "hthor", "roxie", "roxie:s", "thor", "thor:m", "thor:s", "eclcc", "esp", "summary", NULL };
  61. static constexpr const char * const scopeTypeNames[] = { "", "all", "global", "graph", "subgraph", "activity", "allocator", "section", "compile", "dfu", "edge", "function", "workflow", "child", "file", "unknown", nullptr };
  62. static unsigned matchString(const char * const * names, const char * search, unsigned dft)
  63. {
  64. if (!search)
  65. return dft;
  66. if (streq(search, "*"))
  67. search = "all";
  68. unsigned i=0;
  69. for (;;)
  70. {
  71. const char * next = names[i];
  72. if (!next)
  73. return dft;
  74. if (strieq(next, search))
  75. return i;
  76. i++;
  77. }
  78. }
  79. //--------------------------------------------------------------------------------------------------------------------
  80. static const StatisticScopeType scoreOrder[] = {
  81. SSTedge,
  82. SSTactivity,
  83. SSTnone,
  84. SSTall,
  85. SSTglobal,
  86. SSTgraph,
  87. SSTsubgraph,
  88. SSTallocator,
  89. SSTsection,
  90. SSTcompilestage,
  91. SSTdfuworkunit,
  92. SSTfunction,
  93. SSTworkflow,
  94. SSTchildgraph,
  95. SSTfile,
  96. SSTunknown
  97. };
  98. static int scopePriority[SSTmax];
  99. MODULE_INIT(INIT_PRIORITY_STANDARD)
  100. {
  101. static_assert(_elements_in(scoreOrder) == SSTmax, "Elements missing from scoreOrder[]");
  102. for (unsigned i=0; i < _elements_in(scoreOrder); i++)
  103. scopePriority[scoreOrder[i]] = i;
  104. return true;
  105. }
  106. extern jlib_decl int compareScopeName(const char * left, const char * right)
  107. {
  108. if (!left || !*left)
  109. {
  110. if (!right || !*right)
  111. return 0;
  112. else
  113. return -1;
  114. }
  115. else
  116. {
  117. if (!right || !*right)
  118. return +1;
  119. }
  120. StatsScopeId leftId;
  121. StatsScopeId rightId;
  122. for(;;)
  123. {
  124. leftId.extractScopeText(left, &left);
  125. rightId.extractScopeText(right, &right);
  126. int result = leftId.compare(rightId);
  127. if (result != 0)
  128. return result;
  129. left = strchr(left, ':');
  130. right = strchr(right, ':');
  131. if (!left || !right)
  132. {
  133. if (left)
  134. return +1;
  135. if (right)
  136. return -1;
  137. return 0;
  138. }
  139. left++;
  140. right++;
  141. }
  142. }
  143. //--------------------------------------------------------------------------------------------------------------------
  144. extern jlib_decl unsigned __int64 getTimeStampNowValue()
  145. {
  146. #ifdef _WIN32
  147. struct _timeb now;
  148. _ftime(&now);
  149. return (unsigned __int64)now.time * I64C(1000000) + now.millitm * 1000;
  150. #else
  151. struct timeval tm;
  152. gettimeofday(&tm,NULL);
  153. return (unsigned __int64)tm.tv_sec * I64C(1000000) + tm.tv_usec;
  154. #endif
  155. }
  156. unsigned __int64 getIPV4StatsValue(const IpAddress & ip)
  157. {
  158. unsigned ipValue;
  159. if (ip.getNetAddress(sizeof(ipValue),&ipValue))
  160. return ipValue;
  161. return 0;
  162. }
  163. //--------------------------------------------------------------------------------------------------------------------
  164. const static unsigned __int64 oneMicroSecond = I64C(1000);
  165. const static unsigned __int64 oneMilliSecond = I64C(1000000);
  166. const static unsigned __int64 oneSecond = I64C(1000000000);
  167. const static unsigned __int64 oneMinute = I64C(60000000000);
  168. const static unsigned __int64 oneHour = I64C(3600000000000);
  169. const static unsigned __int64 oneDay = 24 * I64C(3600000000000);
  170. static void formatTime(StringBuffer & out, unsigned __int64 value)
  171. {
  172. //Aim to display at least 3 significant digits in the result string
  173. if (value < oneMicroSecond)
  174. out.appendf("%uns", (unsigned)value);
  175. else if (value < oneMilliSecond)
  176. {
  177. unsigned uvalue = (unsigned)value;
  178. out.appendf("%u.%03uus", uvalue / 1000, uvalue % 1000);
  179. }
  180. else if (value < oneSecond)
  181. {
  182. unsigned uvalue = (unsigned)value;
  183. out.appendf("%u.%03ums", uvalue / 1000000, (uvalue / 1000) % 1000);
  184. }
  185. else
  186. {
  187. unsigned days = (unsigned)(value / oneDay);
  188. value = value % oneDay;
  189. unsigned hours = (unsigned)(value / oneHour);
  190. value = value % oneHour;
  191. unsigned mins = (unsigned)(value / oneMinute);
  192. value = value % oneMinute;
  193. unsigned secs = (unsigned)(value / oneSecond);
  194. unsigned ns = (unsigned)(value % oneSecond);
  195. if (days > 0)
  196. out.appendf("%u days ", days);
  197. if (hours > 0 || days)
  198. out.appendf("%uh%02um%02us", hours, mins, secs);
  199. else if (mins >= 10)
  200. out.appendf("%um%02us", mins, secs);
  201. else if (mins >= 1)
  202. out.appendf("%um%02u.%03us", mins, secs, ns / 1000000);
  203. else
  204. out.appendf("%u.%03us", secs, ns / 1000000);
  205. }
  206. }
  207. extern void formatTimeCollatable(StringBuffer & out, unsigned __int64 value, bool nano)
  208. {
  209. unsigned days = (unsigned)(value / oneDay);
  210. value = value % oneDay;
  211. unsigned hours = (unsigned)(value / oneHour);
  212. value = value % oneHour;
  213. unsigned mins = (unsigned)(value / oneMinute);
  214. value = value % oneMinute;
  215. unsigned secs = (unsigned)(value / oneSecond);
  216. unsigned ns = (unsigned)(value % oneSecond);
  217. if (days)
  218. out.appendf(" %3ud ", days); // Two leading spaces helps the cassandra driver force to a single partition
  219. else
  220. out.appendf(" ");
  221. if (nano)
  222. out.appendf("%2u:%02u:%02u.%09u", hours, mins, secs, ns);
  223. else
  224. out.appendf("%2u:%02u:%02u.%03u", hours, mins, secs, ns/1000000);
  225. // More than 999 days, I don't care that it goes wrong.
  226. }
  227. extern unsigned __int64 extractTimestamp(const char *s, const char * * end)
  228. {
  229. if (!s)
  230. return 0;
  231. const char * next = nullptr;
  232. CDateTime timestamp;
  233. try
  234. {
  235. timestamp.setString(s, &next, false); // Sets to date and time given as yyyy-mm-ddThh:mm:ss[.nnnnnnnnn]
  236. }
  237. catch(IException * e)
  238. {
  239. e->Release();
  240. return 0;
  241. }
  242. //Skip a trailing Z. The setString function should really process it, but that may break other code in CScmDateTime.
  243. if (*next == 'Z')
  244. next++;
  245. if (end)
  246. *end = next;
  247. return timestamp.getTimeStamp();
  248. }
  249. extern unsigned __int64 extractTimeCollatable(const char *s, const char * * end)
  250. {
  251. if (!s)
  252. return 0;
  253. unsigned days,hours,mins;
  254. double secs;
  255. unsigned numRead = 0;
  256. if (sscanf(s, " %ud %u:%u:%lf%n", &days, &hours, &mins, &secs, &numRead)!=4)
  257. {
  258. days = 0;
  259. if (sscanf(s, " %u:%u:%lf%n", &hours, &mins, &secs, &numRead) != 3)
  260. return 0;
  261. }
  262. if (end)
  263. *end = s + numRead;
  264. return days*oneDay + hours*oneHour + mins*oneMinute + secs*oneSecond;
  265. }
  266. static void formatTimeStamp(StringBuffer & out, unsigned __int64 value)
  267. {
  268. time_t seconds = value / 1000000;
  269. unsigned us = value % 1000000;
  270. char timeStamp[64];
  271. time_t tNow = seconds;
  272. #ifdef _WIN32
  273. struct tm *gmtNow;
  274. gmtNow = gmtime(&tNow);
  275. strftime(timeStamp, 64, "%Y-%m-%dT%H:%M:%S", gmtNow);
  276. #else
  277. struct tm gmtNow;
  278. gmtime_r(&tNow, &gmtNow);
  279. strftime(timeStamp, 64, "%Y-%m-%dT%H:%M:%S", &gmtNow);
  280. #endif //_WIN32
  281. out.append(timeStamp).appendf(".%03uZ", us / 1000);
  282. }
  283. void formatTimeStampAsLocalTime(StringBuffer & out, unsigned __int64 value)
  284. {
  285. time_t seconds = value / 1000000;
  286. unsigned us = value % 1000000;
  287. char timeStamp[64];
  288. time_t tNow = seconds;
  289. #ifdef _WIN32
  290. struct tm *gmtNow;
  291. gmtNow = localtime(&tNow);
  292. strftime(timeStamp, 64, "%H:%M:%S", gmtNow);
  293. #else
  294. struct tm gmtNow;
  295. localtime_r(&tNow, &gmtNow);
  296. strftime(timeStamp, 64, "%H:%M:%S", &gmtNow);
  297. #endif //_WIN32
  298. out.append(timeStamp).appendf(".%03u", us / 1000);
  299. }
  300. static const unsigned oneKb = 1024;
  301. static const unsigned oneMb = 1024 * 1024;
  302. static const unsigned oneGb = 1024 * 1024 * 1024;
  303. static unsigned toPermille(unsigned x) { return (x * 1000) / 1024; }
  304. static StringBuffer & formatSize(StringBuffer & out, unsigned __int64 value)
  305. {
  306. unsigned Gb = (unsigned)(value / oneGb);
  307. unsigned Mb = (unsigned)((value % oneGb) / oneMb);
  308. unsigned Kb = (unsigned)((value % oneMb) / oneKb);
  309. unsigned b = (unsigned)(value % oneKb);
  310. if (Gb)
  311. return out.appendf("%u.%03uGb", Gb, toPermille(Mb));
  312. else if (Mb)
  313. return out.appendf("%u.%03uMb", Mb, toPermille(Kb));
  314. else if (Kb)
  315. return out.appendf("%u.%03uKb", Kb, toPermille(b));
  316. else
  317. return out.appendf("%ub", b);
  318. }
  319. static StringBuffer & formatLoad(StringBuffer & out, unsigned __int64 value)
  320. {
  321. //Stored as millionth of a core. Display as a percentage => scale by 10,000
  322. return out.appendf("%u.%03u%%", (unsigned)(value / 10000), (unsigned)(value % 10000) / 10);
  323. }
  324. static StringBuffer & formatSkew(StringBuffer & out, unsigned __int64 value)
  325. {
  326. //Skew stored as 10000 = perfect, display as percentage
  327. const __int64 sval = (__int64) value;
  328. const double percent = ((double)sval) / 100.0;
  329. if (sval >= 10000 || sval <= -10000) // For values >= 100%, whole numbers
  330. return out.appendf("%.0f%%", percent);
  331. else if (sval >= 1000 || sval <= -1000) // For values >= 10%, 1 decimal point
  332. return out.appendf("%.1f%%", percent);
  333. else // Anything < 10%, 2 decimal points
  334. return out.appendf("%.2f%%", percent);
  335. }
  336. static StringBuffer & formatIPV4(StringBuffer & out, unsigned __int64 value)
  337. {
  338. byte ip1 = (value & 255);
  339. byte ip2 = ((value >> 8) & 255);
  340. byte ip3 = ((value >> 16) & 255);
  341. byte ip4 = ((value >> 24) & 255);
  342. return out.appendf("%d.%d.%d.%d", ip1, ip2, ip3, ip4);
  343. }
  344. class MoneyLocale
  345. {
  346. public:
  347. ~MoneyLocale()
  348. {
  349. delete locale.load(std::memory_order_relaxed);
  350. }
  351. std::locale * createMoneyLocale() const
  352. {
  353. StringBuffer localestr;
  354. getGlobalConfigSP()->getProp("cost/@moneyLocale", localestr);
  355. std::locale * loc = nullptr;
  356. try
  357. {
  358. loc = new std::locale(localestr.str());
  359. }
  360. catch (std::exception const& e)
  361. {
  362. // Use default locale if the specified moneyLocale is invalid
  363. // (avoids difficult to track down crashes)
  364. OERRLOG("Locale '%s' is not installed [%s]", localestr.str(), e.what());
  365. loc = new std::locale("");
  366. }
  367. return loc;
  368. }
  369. std::locale & queryMoneyLocale()
  370. {
  371. return *querySingleton(locale, cslock, [this]{ return this->createMoneyLocale(); });
  372. }
  373. private:
  374. static CriticalSection cslock;
  375. std::atomic<std::locale *> locale {nullptr};
  376. };
  377. static MoneyLocale moneyLocale;
  378. CriticalSection MoneyLocale::cslock;
  379. StringBuffer & formatMoney(StringBuffer &out, unsigned __int64 value)
  380. {
  381. std::stringstream ss;
  382. std::locale & loc = moneyLocale.queryMoneyLocale();
  383. ss.imbue(loc);
  384. unsigned decplaces = std::use_facet<std::moneypunct<char>>(loc).frac_digits();
  385. long double mvalue = cost_type2money(value)*std::pow(10, decplaces);
  386. ss << std::showbase << std::put_money(mvalue);
  387. return out.append(ss.str().c_str());
  388. }
  389. StringBuffer & formatStatistic(StringBuffer & out, unsigned __int64 value, StatisticMeasure measure)
  390. {
  391. switch (measure)
  392. {
  393. case SMeasureNone: // Unknown stat - e.g, on old esp accessing a new workunit
  394. return out.append(value);
  395. case SMeasureTimeNs:
  396. formatTime(out, value);
  397. return out;
  398. case SMeasureTimestampUs:
  399. formatTimeStamp(out, value);
  400. return out;
  401. case SMeasureCount:
  402. return out.append(value);
  403. case SMeasureSize:
  404. return formatSize(out, value);
  405. case SMeasureLoad:
  406. return formatLoad(out, value);
  407. case SMeasureSkew:
  408. return formatSkew(out, value);
  409. case SMeasureNode:
  410. return out.append(value);
  411. case SMeasurePercent:
  412. return out.appendf("%.2f%%", (double)value / 10000.0); // stored as ppm
  413. case SMeasureIPV4:
  414. return formatIPV4(out, value);
  415. case SMeasureCycle:
  416. return out.append(value);
  417. case SMeasureBool:
  418. return out.append(boolToStr(value != 0));
  419. case SMeasureText:
  420. case SMeasureId:
  421. case SMeasureFilename:
  422. return out.append(value);
  423. case SMeasureEnum:
  424. return out.append("Enum{").append(value).append("}"); // JCS->GH for now, should map to known enum text somehow
  425. case SMeasureCost:
  426. return formatMoney(out, value);
  427. default:
  428. return out.append(value).append('?');
  429. }
  430. }
  431. StringBuffer & formatStatistic(StringBuffer & out, unsigned __int64 value, StatisticKind kind)
  432. {
  433. return formatStatistic(out, value, queryMeasure(kind));
  434. }
  435. //--------------------------------------------------------------------------------------------------------------------
  436. stat_type readStatisticValue(const char * cur, const char * * end, StatisticMeasure measure)
  437. {
  438. char * next = nullptr;
  439. stat_type value = strtoll(cur, &next, 10);
  440. switch (measure)
  441. {
  442. case SMeasureTimestampUs:
  443. {
  444. //ignore value and check if it is a formatted timestamp
  445. stat_type timestamp = extractTimestamp(cur, end);
  446. if (timestamp)
  447. return timestamp;
  448. //Assume that it is a single number
  449. break;
  450. }
  451. case SMeasureTimeNs:
  452. //Allow s, ms and us as scaling suffixes
  453. if (next[0] == 's')
  454. {
  455. value *= 1000000000;
  456. next++;
  457. }
  458. else if ((next[0] == 'm') && (next[1] == 's'))
  459. {
  460. value *= 1000000;
  461. next += 2;
  462. }
  463. else if ((next[0] == 'u') && (next[1] == 's'))
  464. {
  465. value *= 1000;
  466. next += 2;
  467. }
  468. else if ((next[0] == 'n') && (next[1] == 's'))
  469. {
  470. next += 2;
  471. }
  472. else
  473. {
  474. stat_type timestamp = extractTimeCollatable(cur, end);
  475. if (timestamp)
  476. return timestamp;
  477. //Assume that it is a single number
  478. }
  479. break;
  480. case SMeasureCount:
  481. case SMeasureSize:
  482. //Allow K, M, G as scaling suffixes
  483. if (next[0] == 'K')
  484. {
  485. value *= 0x400;
  486. next++;
  487. }
  488. else if (next[0] == 'M')
  489. {
  490. value *= 0x100000;
  491. next++;
  492. }
  493. else if (next[0] == 'G')
  494. {
  495. value *= 0x40000000;
  496. next++;
  497. }
  498. //Skip bytes marker
  499. if ((*next == 'b') || (*next == 'B'))
  500. next++;
  501. break;
  502. case SMeasurePercent:
  503. //MORE: Extend to allow fractional percentages
  504. //Allow % to mean a percentage - instead of ppm
  505. if (next[0] == '%')
  506. {
  507. value *= 10000;
  508. next++;
  509. }
  510. break;
  511. }
  512. if (end)
  513. *end = next;
  514. return value;
  515. }
  516. void validateScopeId(const char * idText)
  517. {
  518. StatsScopeId id;
  519. if (!id.setScopeText(idText))
  520. throw makeStringExceptionV(JLIBERR_UnexpectedValue, "'%s' does not appear to be a valid scope id", idText);
  521. }
  522. void validateScope(const char * scopeText)
  523. {
  524. StatsScopeId id;
  525. const char * cur = scopeText;
  526. for(;;)
  527. {
  528. if (!id.setScopeText(cur, &cur))
  529. throw makeStringExceptionV(JLIBERR_UnexpectedValue, "'%s' does not appear to be a valid scope id", cur);
  530. cur = strchr(cur, ':');
  531. if (!cur)
  532. return;
  533. cur++;
  534. }
  535. }
  536. //--------------------------------------------------------------------------------------------------------------------
  537. unsigned queryScopeDepth(const char * text)
  538. {
  539. if (!*text)
  540. return 0;
  541. unsigned depth = 1;
  542. for (;;)
  543. {
  544. switch (*text)
  545. {
  546. case 0:
  547. return depth;
  548. case ':':
  549. depth++;
  550. break;
  551. }
  552. text++;
  553. }
  554. }
  555. const char * queryScopeTail(const char * scope)
  556. {
  557. const char * colon = strrchr(scope, ':');
  558. if (colon)
  559. return colon+1;
  560. else
  561. return scope;
  562. }
  563. bool getParentScope(StringBuffer & parent, const char * scope)
  564. {
  565. const char * colon = strrchr(scope, ':');
  566. if (colon)
  567. {
  568. parent.append(colon-scope, scope);
  569. return true;
  570. }
  571. else
  572. return false;
  573. }
  574. void describeScope(StringBuffer & description, const char * scope)
  575. {
  576. if (!*scope)
  577. return;
  578. StatsScopeId id;
  579. for(;;)
  580. {
  581. id.extractScopeText(scope, &scope);
  582. id.describe(description);
  583. if (!*scope)
  584. return;
  585. description.append(": ");
  586. scope++;
  587. }
  588. }
  589. bool isParentScope(const char *parent, const char *scope)
  590. {
  591. const char *p = parent;
  592. const char *q = scope;
  593. while(*p && (*p==*q))
  594. {
  595. ++p;
  596. ++q;
  597. }
  598. if ((*p==0) && (*q==':' || *q==0))
  599. return true;
  600. return false;
  601. }
  602. const char * queryMeasurePrefix(StatisticMeasure measure)
  603. {
  604. switch (measure)
  605. {
  606. case SMeasureAll: return nullptr;
  607. case SMeasureTimeNs: return "Time";
  608. case SMeasureTimestampUs: return "When";
  609. case SMeasureCount: return "Num";
  610. case SMeasureSize: return "Size";
  611. case SMeasureLoad: return "Load";
  612. case SMeasureSkew: return "Skew";
  613. case SMeasureNode: return "Node";
  614. case SMeasurePercent: return "Per";
  615. case SMeasureIPV4: return "Ip";
  616. case SMeasureCycle: return "Cycle";
  617. case SMeasureEnum: return "";
  618. case SMeasureText: return "";
  619. case SMeasureBool: return "Is";
  620. case SMeasureId: return "Id";
  621. case SMeasureFilename: return "";
  622. case SMeasureCost: return "Cost";
  623. case SMeasureNone: return nullptr;
  624. default:
  625. return "Unknown";
  626. }
  627. }
  628. const char * queryMeasureName(StatisticMeasure measure)
  629. {
  630. return measureNames[measure];
  631. }
  632. StatisticMeasure queryMeasure(const char * measure, StatisticMeasure dft)
  633. {
  634. //MORE: Use a hash table??
  635. StatisticMeasure ret = (StatisticMeasure)matchString(measureNames, measure, SMeasureMax);
  636. if (ret != SMeasureMax)
  637. return ret;
  638. //Legacy support for an unusual statistic - pretend the sizes are in bytes instead of kb.
  639. if (streq(measure, "kb"))
  640. return SMeasureSize;
  641. for (unsigned i1=SMeasureAll+1; i1 < SMeasureMax; i1++)
  642. {
  643. const char * prefix = queryMeasurePrefix((StatisticMeasure)i1);
  644. if (strieq(measure, prefix))
  645. return (StatisticMeasure)i1;
  646. }
  647. return dft;
  648. }
  649. StatsMergeAction queryMergeMode(StatisticMeasure measure)
  650. {
  651. switch (measure)
  652. {
  653. case SMeasureTimeNs: return StatsMergeSum;
  654. case SMeasureTimestampUs: return StatsMergeKeepNonZero;
  655. case SMeasureCount: return StatsMergeSum;
  656. case SMeasureSize: return StatsMergeSum;
  657. case SMeasureLoad: return StatsMergeMax;
  658. case SMeasureSkew: return StatsMergeMax;
  659. case SMeasureNode: return StatsMergeKeepNonZero;
  660. case SMeasurePercent: return StatsMergeReplace;
  661. case SMeasureIPV4: return StatsMergeKeepNonZero;
  662. case SMeasureCycle: return StatsMergeSum;
  663. case SMeasureEnum: return StatsMergeKeepNonZero;
  664. case SMeasureText: return StatsMergeKeepNonZero;
  665. case SMeasureBool: return StatsMergeKeepNonZero;
  666. case SMeasureId: return StatsMergeKeepNonZero;
  667. case SMeasureFilename: return StatsMergeKeepNonZero;
  668. case SMeasureCost: return StatsMergeSum;
  669. default:
  670. #ifdef _DEBUG
  671. throwUnexpected();
  672. #else
  673. return StatsMergeSum;
  674. #endif
  675. }
  676. }
  677. extern jlib_decl StatsMergeAction queryMergeMode(StatisticKind kind)
  678. {
  679. //MORE: Optimize by looking up in the meta
  680. return queryMergeMode(queryMeasure(kind));
  681. }
  682. //--------------------------------------------------------------------------------------------------------------------
  683. #define BASE_NAMES(x, y) \
  684. #x #y, \
  685. #x "Min" # y, \
  686. #x "Max" # y, \
  687. #x "Avg" # y, \
  688. "Skew" # y, \
  689. "SkewMin" # y, \
  690. "SkewMax" # y, \
  691. "NodeMin" # y, \
  692. "NodeMax" # y,
  693. #define NAMES(x, y) \
  694. BASE_NAMES(x, y) \
  695. #x "Delta" # y, \
  696. #x "StdDev" #y,
  697. #define WHENNAMES(x, y) \
  698. BASE_NAMES(x, y) \
  699. "TimeDelta" # y, \
  700. "TimeStdDev" # y,
  701. #define BASE_TAGS(x, y) \
  702. "@" #x "Min" # y, \
  703. "@" #x "Max" # y, \
  704. "@" #x "Avg" # y, \
  705. "@Skew" # y, \
  706. "@SkewMin" # y, \
  707. "@SkewMax" # y, \
  708. "@NodeMin" # y, \
  709. "@NodeMax" # y,
  710. //Default tags nothing special overriden
  711. #define TAGS(x, y) \
  712. "@" #x #y, \
  713. BASE_TAGS(x, y) \
  714. "@" #x "Delta" # y, \
  715. "@" #x "StdDev" # y,
  716. //Define the tags for time items.
  717. #define WHENTAGS(x, y) \
  718. "@" #x #y, \
  719. BASE_TAGS(x, y) \
  720. "@TimeDelta" # y, \
  721. "@TimeStdDev" # y,
  722. #define CORESTAT(x, y, m) St##x##y, m, St##x##y, St##x##y, { NAMES(x, y) }, { TAGS(x, y) }
  723. #define STAT(x, y, m) CORESTAT(x, y, m)
  724. //--------------------------------------------------------------------------------------------------------------------
  725. //These are the macros to use to define the different entries in the stats meta table
  726. //#define TIMESTAT(y) STAT(Time, y, SMeasureTimeNs)
  727. #define TIMESTAT(y) St##Time##y, SMeasureTimeNs, St##Time##y, St##Cycle##y##Cycles, { NAMES(Time, y) }, { TAGS(Time, y) }
  728. #define WHENSTAT(y) St##When##y, SMeasureTimestampUs, St##When##y, St##When##y, { WHENNAMES(When, y) }, { WHENTAGS(When, y) }
  729. #define NUMSTAT(y) STAT(Num, y, SMeasureCount)
  730. #define SIZESTAT(y) STAT(Size, y, SMeasureSize)
  731. #define LOADSTAT(y) STAT(Load, y, SMeasureLoad)
  732. #define SKEWSTAT(y) STAT(Skew, y, SMeasureSkew)
  733. #define NODESTAT(y) STAT(Node, y, SMeasureNode)
  734. #define PERSTAT(y) STAT(Per, y, SMeasurePercent)
  735. #define IPV4STAT(y) STAT(IPV4, y, SMeasureIPV4)
  736. #define CYCLESTAT(y) St##Cycle##y##Cycles, SMeasureCycle, St##Time##y, St##Cycle##y##Cycles, { NAMES(Cycle, y##Cycles) }, { TAGS(Cycle, y##Cycles) }
  737. #define ENUMSTAT(y) STAT(Enum, y, SMeasureEnum)
  738. //--------------------------------------------------------------------------------------------------------------------
  739. class StatisticMeta
  740. {
  741. public:
  742. StatisticKind kind;
  743. StatisticMeasure measure;
  744. StatisticKind serializeKind;
  745. StatisticKind rawKind;
  746. const char * names[StNextModifier/StVariantScale];
  747. const char * tags[StNextModifier/StVariantScale];
  748. };
  749. //The order of entries in this table must match the order in the enumeration
  750. static const StatisticMeta statsMetaData[StMax] = {
  751. { StKindNone, SMeasureNone, StKindNone, StKindNone, { "none" }, { "@none" } },
  752. { StKindAll, SMeasureAll, StKindAll, StKindAll, { "all" }, { "@all" } },
  753. { WHENSTAT(GraphStarted) }, // Deprecated - use WhenStart
  754. { WHENSTAT(GraphFinished) }, // Deprecated - use WhenFinished
  755. { WHENSTAT(FirstRow) },
  756. { WHENSTAT(QueryStarted) }, // Deprecated - use WhenStart
  757. { WHENSTAT(QueryFinished) }, // Deprecated - use WhenFinished
  758. { WHENSTAT(Created) },
  759. { WHENSTAT(Compiled) },
  760. { WHENSTAT(WorkunitModified) },
  761. { TIMESTAT(Elapsed) },
  762. { TIMESTAT(LocalExecute) },
  763. { TIMESTAT(TotalExecute) },
  764. { TIMESTAT(Remaining) },
  765. { SIZESTAT(GeneratedCpp) },
  766. { SIZESTAT(PeakMemory) },
  767. { SIZESTAT(MaxRowSize) },
  768. { NUMSTAT(RowsProcessed) },
  769. { NUMSTAT(Slaves) },
  770. { NUMSTAT(Starts) },
  771. { NUMSTAT(Stops) },
  772. { NUMSTAT(IndexSeeks) },
  773. { NUMSTAT(IndexScans) },
  774. { NUMSTAT(IndexWildSeeks) },
  775. { NUMSTAT(IndexSkips) },
  776. { NUMSTAT(IndexNullSkips) },
  777. { NUMSTAT(IndexMerges) },
  778. { NUMSTAT(IndexMergeCompares) },
  779. { NUMSTAT(PreFiltered) },
  780. { NUMSTAT(PostFiltered) },
  781. { NUMSTAT(BlobCacheHits) },
  782. { NUMSTAT(LeafCacheHits) },
  783. { NUMSTAT(NodeCacheHits) },
  784. { NUMSTAT(BlobCacheAdds) },
  785. { NUMSTAT(LeafCacheAdds) },
  786. { NUMSTAT(NodeCacheAdds) },
  787. { NUMSTAT(PreloadCacheHits) },
  788. { NUMSTAT(PreloadCacheAdds) },
  789. { NUMSTAT(ServerCacheHits) },
  790. { NUMSTAT(IndexAccepted) },
  791. { NUMSTAT(IndexRejected) },
  792. { NUMSTAT(AtmostTriggered) },
  793. { NUMSTAT(DiskSeeks) },
  794. { NUMSTAT(Iterations) },
  795. { LOADSTAT(WhileSorting) },
  796. { NUMSTAT(LeftRows) },
  797. { NUMSTAT(RightRows) },
  798. { PERSTAT(Replicated) },
  799. { NUMSTAT(DiskRowsRead) },
  800. { NUMSTAT(IndexRowsRead) },
  801. { NUMSTAT(DiskAccepted) },
  802. { NUMSTAT(DiskRejected) },
  803. { TIMESTAT(Soapcall) },
  804. { TIMESTAT(FirstExecute) },
  805. { TIMESTAT(DiskReadIO) },
  806. { TIMESTAT(DiskWriteIO) },
  807. { SIZESTAT(DiskRead) },
  808. { SIZESTAT(DiskWrite) },
  809. { CYCLESTAT(DiskReadIO) },
  810. { CYCLESTAT(DiskWriteIO) },
  811. { NUMSTAT(DiskReads) },
  812. { NUMSTAT(DiskWrites) },
  813. { NUMSTAT(Spills) },
  814. { TIMESTAT(SpillElapsed) },
  815. { TIMESTAT(SortElapsed) },
  816. { NUMSTAT(Groups) },
  817. { NUMSTAT(GroupMax) },
  818. { SIZESTAT(SpillFile) },
  819. { CYCLESTAT(SpillElapsed) },
  820. { CYCLESTAT(SortElapsed) },
  821. { NUMSTAT(Strands) },
  822. { CYCLESTAT(TotalExecute) },
  823. { NUMSTAT(Executions) },
  824. { TIMESTAT(TotalNested) },
  825. { CYCLESTAT(LocalExecute) },
  826. { NUMSTAT(Compares) },
  827. { NUMSTAT(ScansPerRow) },
  828. { NUMSTAT(Allocations) },
  829. { NUMSTAT(AllocationScans) },
  830. { NUMSTAT(DiskRetries) },
  831. { CYCLESTAT(Elapsed) },
  832. { CYCLESTAT(Remaining) },
  833. { CYCLESTAT(Soapcall) },
  834. { CYCLESTAT(FirstExecute) },
  835. { CYCLESTAT(TotalNested) },
  836. { TIMESTAT(Generate) },
  837. { CYCLESTAT(Generate) },
  838. { WHENSTAT(Started) },
  839. { WHENSTAT(Finished) },
  840. { NUMSTAT(AnalyseExprs) },
  841. { NUMSTAT(TransformExprs) },
  842. { NUMSTAT(UniqueAnalyseExprs) },
  843. { NUMSTAT(UniqueTransformExprs) },
  844. { NUMSTAT(DuplicateKeys) },
  845. { NUMSTAT(AttribsProcessed) },
  846. { NUMSTAT(AttribsSimplified) },
  847. { NUMSTAT(AttribsFromCache) },
  848. { NUMSTAT(SmartJoinDegradedToLocal) },
  849. { NUMSTAT(SmartJoinSlavesDegradedToStd) },
  850. { NUMSTAT(AttribsSimplifiedTooComplex) },
  851. { NUMSTAT(SysContextSwitches) },
  852. { TIMESTAT(OsUser) },
  853. { TIMESTAT(OsSystem) },
  854. { TIMESTAT(OsTotal) },
  855. { CYCLESTAT(OsUser) },
  856. { CYCLESTAT(OsSystem) },
  857. { CYCLESTAT(OsTotal) },
  858. { NUMSTAT(ContextSwitches) },
  859. { TIMESTAT(User) },
  860. { TIMESTAT(System) },
  861. { TIMESTAT(Total) },
  862. { CYCLESTAT(User) },
  863. { CYCLESTAT(System) },
  864. { CYCLESTAT(Total) },
  865. { SIZESTAT(OsDiskRead) },
  866. { SIZESTAT(OsDiskWrite) },
  867. { TIMESTAT(Blocked) },
  868. { CYCLESTAT(Blocked) },
  869. { STAT(Cost, Execute, SMeasureCost) },
  870. { SIZESTAT(AgentReply) },
  871. { TIMESTAT(AgentWait) },
  872. { CYCLESTAT(AgentWait) },
  873. };
  874. //Is a 0 value likely, and useful to be reported if it does happen to be zero?
  875. bool includeStatisticIfZero(StatisticKind kind)
  876. {
  877. switch (kind)
  878. {
  879. case StNumRowsProcessed:
  880. case StNumIterations:
  881. case StNumIndexSeeks:
  882. case StNumDuplicateKeys:
  883. return true;
  884. }
  885. return false;
  886. }
  887. //--------------------------------------------------------------------------------------------------------------------
  888. StatisticMeasure queryMeasure(StatisticKind kind)
  889. {
  890. unsigned variant = queryStatsVariant(kind);
  891. switch (variant)
  892. {
  893. case StSkew:
  894. case StSkewMin:
  895. case StSkewMax:
  896. return SMeasureSkew;
  897. case StNodeMin:
  898. case StNodeMax:
  899. return SMeasureNode;
  900. case StDeltaX:
  901. case StStdDevX:
  902. {
  903. StatisticMeasure measure = queryMeasure((StatisticKind)(kind & StKindMask));
  904. switch (measure)
  905. {
  906. case SMeasureTimestampUs:
  907. return SMeasureTimeNs;
  908. default:
  909. return measure;
  910. }
  911. break;
  912. }
  913. }
  914. StatisticKind rawkind = (StatisticKind)(kind & StKindMask);
  915. if (rawkind >= StKindNone && rawkind < StMax)
  916. return statsMetaData[rawkind].measure;
  917. return SMeasureNone;
  918. }
  919. const char * queryStatisticName(StatisticKind kind)
  920. {
  921. StatisticKind rawkind = (StatisticKind)(kind & StKindMask);
  922. unsigned variant = (kind / StVariantScale);
  923. dbgassertex(variant < (StNextModifier/StVariantScale));
  924. if (rawkind >= StKindNone && rawkind < StMax)
  925. return statsMetaData[rawkind].names[variant];
  926. return "Unknown";
  927. }
  928. unsigned __int64 convertMeasure(StatisticMeasure from, StatisticMeasure to, unsigned __int64 value)
  929. {
  930. if (from == to)
  931. return value;
  932. if ((from == SMeasureCycle) && (to == SMeasureTimeNs))
  933. return cycle_to_nanosec(value);
  934. if ((from == SMeasureTimeNs) && (to == SMeasureCycle))
  935. return nanosec_to_cycle(value);
  936. if ((from == SMeasureTimestampUs) && (to == SMeasureTimeNs))
  937. return value * 1000;
  938. if ((from == SMeasureTimeNs) && (to == SMeasureTimestampUs))
  939. return value / 1000;
  940. #ifdef _DEBUG
  941. throwUnexpected();
  942. #else
  943. return value;
  944. #endif
  945. }
  946. unsigned __int64 convertMeasure(StatisticKind from, StatisticKind to, unsigned __int64 value)
  947. {
  948. if (from == to)
  949. return value;
  950. return convertMeasure(queryMeasure(from), queryMeasure(to), value);
  951. }
  952. static unsigned __int64 convertSumMeasure(StatisticKind from, StatisticKind to, double value)
  953. {
  954. if (from == to)
  955. return value;
  956. return convertMeasure(queryMeasure(from), queryMeasure(to), value);
  957. }
  958. static double convertSquareMeasure(StatisticMeasure from, StatisticMeasure to, double value)
  959. {
  960. if (from == to)
  961. return value;
  962. //Coded to a avoid overflow of unsigned __int64 in cycle_to_nanosec etc.
  963. const unsigned __int64 largeValue = 1000000000;
  964. double scale;
  965. if ((from == SMeasureCycle) && (to == SMeasureTimeNs))
  966. scale = (double)cycle_to_nanosec(largeValue) / (double)largeValue;
  967. else if ((from == SMeasureTimeNs) && (to == SMeasureCycle))
  968. scale = (double)nanosec_to_cycle(largeValue) / (double)largeValue;
  969. else
  970. {
  971. #ifdef _DEBUG
  972. throwUnexpected();
  973. #else
  974. scale = 1.0;
  975. #endif
  976. }
  977. return value * scale * scale;
  978. }
  979. static double convertSquareMeasure(StatisticKind from, StatisticKind to, double value)
  980. {
  981. return convertSquareMeasure(queryMeasure(from), queryMeasure(to), value);
  982. }
  983. static StatisticKind querySerializedKind(StatisticKind kind)
  984. {
  985. StatisticKind rawkind = (StatisticKind)(kind & StKindMask);
  986. if (rawkind >= StMax)
  987. return kind;
  988. StatisticKind serialKind = statsMetaData[rawkind].serializeKind;
  989. return (StatisticKind)(serialKind | (kind & ~StKindMask));
  990. }
  991. static StatisticKind queryRawKind(StatisticKind kind)
  992. {
  993. StatisticKind basekind = (StatisticKind)(kind & StKindMask);
  994. if (basekind >= StMax)
  995. return kind;
  996. StatisticKind rawKind = statsMetaData[basekind].rawKind;
  997. return (StatisticKind)(rawKind | (kind & ~StKindMask));
  998. }
  999. //--------------------------------------------------------------------------------------------------------------------
  1000. void queryLongStatisticName(StringBuffer & out, StatisticKind kind)
  1001. {
  1002. out.append(queryStatisticName(kind));
  1003. }
  1004. //--------------------------------------------------------------------------------------------------------------------
  1005. const char * queryTreeTag(StatisticKind kind)
  1006. {
  1007. StatisticKind rawkind = (StatisticKind)(kind & StKindMask);
  1008. unsigned variant = (kind / StVariantScale);
  1009. dbgassertex(variant < (StNextModifier/StVariantScale));
  1010. if (rawkind >= StKindNone && rawkind < StMax)
  1011. return statsMetaData[rawkind].tags[variant];
  1012. return "@Unknown";
  1013. }
  1014. //--------------------------------------------------------------------------------------------------------------------
  1015. StatisticKind queryStatisticKind(const char * search, StatisticKind dft)
  1016. {
  1017. if (!search)
  1018. return dft;
  1019. if (streq(search, "*"))
  1020. return StKindAll;
  1021. //Slow - should use a hash table....
  1022. for (unsigned variant=0; variant < StNextModifier; variant += StVariantScale)
  1023. {
  1024. for (unsigned i=0; i < StMax; i++)
  1025. {
  1026. StatisticKind kind = (StatisticKind)(i+variant);
  1027. const char * shortName = queryStatisticName(kind);
  1028. if (shortName && strieq(shortName, search))
  1029. return kind;
  1030. }
  1031. }
  1032. return dft;
  1033. }
  1034. //--------------------------------------------------------------------------------------------------------------------
  1035. const char * queryCreatorTypeName(StatisticCreatorType sct)
  1036. {
  1037. return creatorTypeNames[sct];
  1038. }
  1039. StatisticCreatorType queryCreatorType(const char * sct, StatisticCreatorType dft)
  1040. {
  1041. //MORE: Use a hash table??
  1042. return (StatisticCreatorType)matchString(creatorTypeNames, sct, dft);
  1043. }
  1044. //--------------------------------------------------------------------------------------------------------------------
  1045. const char * queryScopeTypeName(StatisticScopeType sst)
  1046. {
  1047. return scopeTypeNames[sst];
  1048. }
  1049. extern jlib_decl StatisticScopeType queryScopeType(const char * sst, StatisticScopeType dft)
  1050. {
  1051. //MORE: Use a hash table??
  1052. return (StatisticScopeType)matchString(scopeTypeNames, sst, dft);
  1053. }
  1054. //--------------------------------------------------------------------------------------------------------------------
  1055. inline void mergeUpdate(StatisticMeasure measure, unsigned __int64 & value, const unsigned __int64 otherValue)
  1056. {
  1057. switch (measure)
  1058. {
  1059. case SMeasureTimeNs:
  1060. case SMeasureCount:
  1061. case SMeasureSize:
  1062. case SMeasureLoad:
  1063. case SMeasureSkew:
  1064. case SMeasureCycle:
  1065. value += otherValue;
  1066. break;
  1067. case SMeasureTimestampUs:
  1068. if (otherValue && otherValue < value)
  1069. value = otherValue;
  1070. break;
  1071. }
  1072. }
  1073. unsigned __int64 mergeStatistic(StatisticMeasure measure, unsigned __int64 value, unsigned __int64 otherValue)
  1074. {
  1075. mergeUpdate(measure, value, otherValue);
  1076. return value;
  1077. }
  1078. unsigned __int64 mergeStatisticValue(unsigned __int64 prevValue, unsigned __int64 newValue, StatsMergeAction mergeAction)
  1079. {
  1080. switch (mergeAction)
  1081. {
  1082. case StatsMergeKeepNonZero:
  1083. if (prevValue)
  1084. return prevValue;
  1085. return newValue;
  1086. case StatsMergeAppend:
  1087. case StatsMergeReplace:
  1088. return newValue;
  1089. case StatsMergeSum:
  1090. return prevValue + newValue;
  1091. case StatsMergeMin:
  1092. if (prevValue > newValue)
  1093. return newValue;
  1094. else
  1095. return prevValue;
  1096. case StatsMergeMax:
  1097. if (prevValue < newValue)
  1098. return newValue;
  1099. else
  1100. return prevValue;
  1101. default:
  1102. #ifdef _DEBUG
  1103. throwUnexpected();
  1104. #else
  1105. return newValue;
  1106. #endif
  1107. }
  1108. }
  1109. //--------------------------------------------------------------------------------------------------------------------
  1110. class CComponentStatistics
  1111. {
  1112. protected:
  1113. StringAttr creator;
  1114. byte creatorDepth;
  1115. byte scopeDepth;
  1116. // StatisticArray stats;
  1117. };
  1118. //--------------------------------------------------------------------------------------------------------------------
  1119. static int compareUnsigned(unsigned const * left, unsigned const * right)
  1120. {
  1121. return (*left < *right) ? -1 : (*left > *right) ? +1 : 0;
  1122. }
  1123. void StatisticsMapping::createMappings()
  1124. {
  1125. //Possibly not needed, but sort the kinds, so that it is easy to merge/stream the results out in the correct order.
  1126. indexToKind.sort(compareUnsigned);
  1127. //Provide mappings to all statistics to map them to the "unknown" bin by default
  1128. for (unsigned i=0; i < StMax; i++)
  1129. kindToIndex.append(numStatistics());
  1130. ForEachItemIn(i2, indexToKind)
  1131. {
  1132. unsigned kind = indexToKind.item(i2);
  1133. kindToIndex.replace(i2, kind);
  1134. }
  1135. }
  1136. const StatisticsMapping allStatistics(StKindAll);
  1137. const StatisticsMapping heapStatistics({StNumAllocations, StNumAllocationScans});
  1138. const StatisticsMapping diskLocalStatistics({StCycleDiskReadIOCycles, StSizeDiskRead, StNumDiskReads, StCycleDiskWriteIOCycles, StSizeDiskWrite, StNumDiskWrites, StNumDiskRetries});
  1139. const StatisticsMapping diskRemoteStatistics({StTimeDiskReadIO, StSizeDiskRead, StNumDiskReads, StTimeDiskWriteIO, StSizeDiskWrite, StNumDiskWrites, StNumDiskRetries});
  1140. const StatisticsMapping diskReadRemoteStatistics({StTimeDiskReadIO, StSizeDiskRead, StNumDiskReads, StNumDiskRetries, StCycleDiskReadIOCycles});
  1141. const StatisticsMapping diskWriteRemoteStatistics({StTimeDiskWriteIO, StSizeDiskWrite, StNumDiskWrites, StNumDiskRetries, StCycleDiskWriteIOCycles});
  1142. //--------------------------------------------------------------------------------------------------------------------
  1143. StringBuffer & StatisticValueFilter::describe(StringBuffer & out) const
  1144. {
  1145. out.append(queryStatisticName(kind));
  1146. if (minValue == maxValue)
  1147. out.append("=").append(minValue);
  1148. else
  1149. out.append("=").append(minValue).append("..").append(maxValue);
  1150. return out;
  1151. }
  1152. //--------------------------------------------------------------------------------------------------------------------
  1153. class Statistic
  1154. {
  1155. public:
  1156. Statistic(StatisticKind _kind, unsigned __int64 _value) : kind(_kind), value(_value)
  1157. {
  1158. }
  1159. Statistic(MemoryBuffer & in, unsigned version)
  1160. {
  1161. unsigned _kind;
  1162. in.read(_kind);
  1163. kind = (StatisticKind)_kind;
  1164. in.read(value);
  1165. }
  1166. StatisticKind queryKind() const
  1167. {
  1168. return kind;
  1169. }
  1170. unsigned __int64 queryValue() const
  1171. {
  1172. return value;
  1173. }
  1174. void merge(unsigned __int64 otherValue)
  1175. {
  1176. mergeUpdate(queryMeasure(kind), value, otherValue);
  1177. }
  1178. void serialize(MemoryBuffer & out) const
  1179. {
  1180. //MORE: Could compress - e.g., store as a packed integers
  1181. out.append((unsigned)kind);
  1182. out.append(value);
  1183. }
  1184. void mergeInto(IStatisticGatherer & target) const
  1185. {
  1186. StatsMergeAction mergeAction = queryMergeMode(kind);
  1187. target.updateStatistic(kind, value, mergeAction);
  1188. }
  1189. StringBuffer & toXML(StringBuffer &out) const
  1190. {
  1191. return out.append(" <Stat name=\"").append(queryStatisticName(kind)).append("\" value=\"").append(value).append("\"/>\n");
  1192. }
  1193. public:
  1194. StatisticKind kind;
  1195. unsigned __int64 value;
  1196. };
  1197. //--------------------------------------------------------------------------------------------------------------------
  1198. StringBuffer & StatsScopeId::getScopeText(StringBuffer & out) const
  1199. {
  1200. switch (scopeType)
  1201. {
  1202. case SSTgraph:
  1203. return out.append(GraphScopePrefix).append(id);
  1204. case SSTsubgraph:
  1205. return out.append(SubGraphScopePrefix).append(id);
  1206. case SSTactivity:
  1207. return out.append(ActivityScopePrefix).append(id);
  1208. case SSTedge:
  1209. return out.append(EdgeScopePrefix).append(id).append("_").append(extra);
  1210. case SSTfunction:
  1211. return out.append(FunctionScopePrefix).append(name);
  1212. case SSTworkflow:
  1213. return out.append(WorkflowScopePrefix).append(id);
  1214. case SSTchildgraph:
  1215. return out.append(ChildGraphScopePrefix).append(id);
  1216. case SSTunknown:
  1217. return out.append(name);
  1218. default:
  1219. #ifdef _DEBUG
  1220. throwUnexpected();
  1221. #endif
  1222. return out.append("????").append(id);
  1223. }
  1224. }
  1225. unsigned StatsScopeId::getHash() const
  1226. {
  1227. switch (scopeType)
  1228. {
  1229. case SSTfunction:
  1230. case SSTunknown:
  1231. return hashc((const byte *)name.get(), strlen(name), (unsigned)scopeType);
  1232. default:
  1233. return hashc((const byte *)&id, sizeof(id), (unsigned)scopeType);
  1234. }
  1235. }
  1236. bool StatsScopeId::isWildcard() const
  1237. {
  1238. return (id == 0) && (extra == 0) && !name;
  1239. }
  1240. int StatsScopeId::compare(const StatsScopeId & other) const
  1241. {
  1242. if (scopeType != other.scopeType)
  1243. return scopePriority[scopeType] - scopePriority[other.scopeType];
  1244. if (id != other.id)
  1245. return (int)(id - other.id);
  1246. if (extra != other.extra)
  1247. return (int)(extra - other.extra);
  1248. if (name && other.name)
  1249. return strcmp(name, other.name);
  1250. if (name)
  1251. return +1;
  1252. if (other.name)
  1253. return -1;
  1254. return 0;
  1255. }
  1256. void StatsScopeId::describe(StringBuffer & description) const
  1257. {
  1258. const char * name = queryScopeTypeName(scopeType);
  1259. description.append((char)toupper(*name)).append(name+1);
  1260. switch (scopeType)
  1261. {
  1262. case SSTgraph:
  1263. description.append(" graph").append(id);
  1264. break;
  1265. case SSTsubgraph:
  1266. case SSTactivity:
  1267. case SSTworkflow:
  1268. case SSTchildgraph:
  1269. description.append(' ').append(id);
  1270. break;
  1271. case SSTedge:
  1272. description.append(' ').append(id).append(',').append(extra);
  1273. break;
  1274. case SSTfile:
  1275. case SSTfunction:
  1276. description.append(' ').append(name);
  1277. break;
  1278. default:
  1279. throwUnexpected();
  1280. break;
  1281. }
  1282. }
  1283. bool StatsScopeId::matches(const StatsScopeId & other) const
  1284. {
  1285. return (scopeType == other.scopeType) && (id == other.id) && (extra == other.extra) && strsame(name, other.name);
  1286. }
  1287. unsigned StatsScopeId::queryActivity() const
  1288. {
  1289. switch (scopeType)
  1290. {
  1291. case SSTactivity:
  1292. case SSTedge:
  1293. return id;
  1294. default:
  1295. return 0;
  1296. }
  1297. }
  1298. void StatsScopeId::deserialize(MemoryBuffer & in, unsigned version)
  1299. {
  1300. byte scopeTypeByte;
  1301. in.read(scopeTypeByte);
  1302. scopeType = (StatisticScopeType)scopeTypeByte;
  1303. switch (scopeType)
  1304. {
  1305. case SSTgraph:
  1306. case SSTsubgraph:
  1307. case SSTactivity:
  1308. case SSTworkflow:
  1309. case SSTchildgraph:
  1310. in.read(id);
  1311. break;
  1312. case SSTedge:
  1313. in.read(id);
  1314. in.read(extra);
  1315. break;
  1316. case SSTfunction:
  1317. in.read(name);
  1318. break;
  1319. default:
  1320. throwUnexpected();
  1321. break;
  1322. }
  1323. }
  1324. void StatsScopeId::serialize(MemoryBuffer & out) const
  1325. {
  1326. out.append((byte)scopeType);
  1327. switch (scopeType)
  1328. {
  1329. case SSTgraph:
  1330. case SSTsubgraph:
  1331. case SSTactivity:
  1332. case SSTworkflow:
  1333. case SSTchildgraph:
  1334. out.append(id);
  1335. break;
  1336. case SSTedge:
  1337. out.append(id);
  1338. out.append(extra);
  1339. break;
  1340. case SSTfile:
  1341. case SSTfunction:
  1342. out.append(name);
  1343. break;
  1344. default:
  1345. throwUnexpected();
  1346. break;
  1347. }
  1348. }
  1349. void StatsScopeId::setId(StatisticScopeType _scopeType, unsigned _id, unsigned _extra)
  1350. {
  1351. scopeType = _scopeType;
  1352. id = _id;
  1353. extra = _extra;
  1354. }
  1355. bool StatsScopeId::setScopeText(const char * text, const char * * _next)
  1356. {
  1357. char * * next = (char * *)_next;
  1358. switch (*text)
  1359. {
  1360. case ActivityScopePrefix[0]:
  1361. if (MATCHES_CONST_PREFIX(text, ActivityScopePrefix))
  1362. {
  1363. if (isdigit(text[strlen(ActivityScopePrefix)]))
  1364. {
  1365. unsigned id = strtoul(text + strlen(ActivityScopePrefix), next, 10);
  1366. setActivityId(id);
  1367. return true;
  1368. }
  1369. }
  1370. break;
  1371. case GraphScopePrefix[0]:
  1372. if (MATCHES_CONST_PREFIX(text, GraphScopePrefix))
  1373. {
  1374. if (isdigit(text[strlen(GraphScopePrefix)]))
  1375. {
  1376. unsigned id = strtoul(text + strlen(GraphScopePrefix), next, 10);
  1377. setId(SSTgraph, id);
  1378. return true;
  1379. }
  1380. }
  1381. break;
  1382. case SubGraphScopePrefix[0]:
  1383. if (MATCHES_CONST_PREFIX(text, SubGraphScopePrefix))
  1384. {
  1385. if (isdigit(text[strlen(SubGraphScopePrefix)]))
  1386. {
  1387. unsigned id = strtoul(text + strlen(SubGraphScopePrefix), next, 10);
  1388. setSubgraphId(id);
  1389. return true;
  1390. }
  1391. }
  1392. break;
  1393. case EdgeScopePrefix[0]:
  1394. if (MATCHES_CONST_PREFIX(text, EdgeScopePrefix))
  1395. {
  1396. const char * underscore = strchr(text, '_');
  1397. if (!underscore || !isdigit(underscore[1]))
  1398. return false;
  1399. unsigned id1 = atoi(text + strlen(EdgeScopePrefix));
  1400. unsigned id2 = strtoul(underscore+1, next, 10);
  1401. setEdgeId(id1, id2);
  1402. return true;
  1403. }
  1404. break;
  1405. case FunctionScopePrefix[0]:
  1406. if (MATCHES_CONST_PREFIX(text, FunctionScopePrefix))
  1407. {
  1408. setFunctionId(text+ strlen(FunctionScopePrefix));
  1409. if (_next)
  1410. *_next = text + strlen(text);
  1411. return true;
  1412. }
  1413. break;
  1414. case FileScopePrefix[0]:
  1415. if (MATCHES_CONST_PREFIX(text, FileScopePrefix))
  1416. {
  1417. setFileId(text+strlen(FileScopePrefix));
  1418. if (_next)
  1419. *_next = text + strlen(text);
  1420. return true;
  1421. }
  1422. break;
  1423. case WorkflowScopePrefix[0]:
  1424. if (MATCHES_CONST_PREFIX(text, WorkflowScopePrefix) && isdigit(text[strlen(WorkflowScopePrefix)]))
  1425. {
  1426. setWorkflowId(strtoul(text+ strlen(WorkflowScopePrefix), next, 10));
  1427. return true;
  1428. }
  1429. break;
  1430. case ChildGraphScopePrefix[0]:
  1431. if (MATCHES_CONST_PREFIX(text, ChildGraphScopePrefix))
  1432. {
  1433. setChildGraphId(strtoul(text+ strlen(ChildGraphScopePrefix), next, 10));
  1434. return true;
  1435. }
  1436. break;
  1437. case '\0':
  1438. setId(SSTglobal, 0);
  1439. return true;
  1440. }
  1441. return false;
  1442. }
  1443. bool StatsScopeId::extractScopeText(const char * text, const char * * next)
  1444. {
  1445. if (setScopeText(text, next))
  1446. return true;
  1447. scopeType = SSTunknown;
  1448. const char * end = strchr(text, ':');
  1449. if (end)
  1450. {
  1451. name.set(text, end-text);
  1452. if (next)
  1453. *next = end;
  1454. }
  1455. else
  1456. {
  1457. name.set(text);
  1458. if (next)
  1459. *next = text + strlen(text);
  1460. }
  1461. return false;
  1462. }
  1463. void StatsScopeId::setActivityId(unsigned _id)
  1464. {
  1465. setId(SSTactivity, _id);
  1466. }
  1467. void StatsScopeId::setEdgeId(unsigned _id, unsigned _output)
  1468. {
  1469. setId(SSTedge, _id, _output);
  1470. }
  1471. void StatsScopeId::setSubgraphId(unsigned _id)
  1472. {
  1473. setId(SSTsubgraph, _id);
  1474. }
  1475. void StatsScopeId::setFunctionId(const char * _name)
  1476. {
  1477. scopeType = SSTfunction;
  1478. name.set(_name);
  1479. }
  1480. void StatsScopeId::setFileId(const char * _name)
  1481. {
  1482. scopeType = SSTfile;
  1483. name.set(_name);
  1484. }
  1485. void StatsScopeId::setWorkflowId(unsigned _id)
  1486. {
  1487. setId(SSTworkflow, _id);
  1488. }
  1489. void StatsScopeId::setChildGraphId(unsigned _id)
  1490. {
  1491. setId(SSTchildgraph, _id);
  1492. }
  1493. //--------------------------------------------------------------------------------------------------------------------
  1494. enum
  1495. {
  1496. SCroot,
  1497. SCintermediate,
  1498. SCleaf,
  1499. };
  1500. class CStatisticCollection;
  1501. static CStatisticCollection * deserializeCollection(CStatisticCollection * parent, MemoryBuffer & in, unsigned version);
  1502. //MORE: Create an implementation with no children
  1503. typedef StructArrayOf<Statistic> StatsArray;
  1504. class CollectionHashTable : public SuperHashTableOf<CStatisticCollection, StatsScopeId>
  1505. {
  1506. public:
  1507. ~CollectionHashTable() { _releaseAll(); }
  1508. virtual void onAdd(void *et);
  1509. virtual void onRemove(void *et);
  1510. virtual unsigned getHashFromElement(const void *et) const;
  1511. virtual unsigned getHashFromFindParam(const void *fp) const;
  1512. virtual const void * getFindParam(const void *et) const;
  1513. virtual bool matchesFindParam(const void *et, const void *key, unsigned fphash) const;
  1514. virtual bool matchesElement(const void *et, const void *searchET) const;
  1515. };
  1516. typedef IArrayOf<CStatisticCollection> CollectionArray;
  1517. static int compareCollection(IInterface * const * pl, IInterface * const *pr);
  1518. class SortedCollectionIterator : public ArrayIIteratorOf<IArrayOf<IStatisticCollection>, IStatisticCollection, IStatisticCollectionIterator>
  1519. {
  1520. IArrayOf<IStatisticCollection> elems;
  1521. public:
  1522. SortedCollectionIterator(IStatisticCollectionIterator &iter) : ArrayIIteratorOf<IArrayOf<IStatisticCollection>, IStatisticCollection, IStatisticCollectionIterator>(elems)
  1523. {
  1524. ForEach(iter)
  1525. elems.append(iter.get());
  1526. elems.sort(compareCollection);
  1527. }
  1528. };
  1529. class CStatisticCollection : public CInterfaceOf<IStatisticCollection>
  1530. {
  1531. friend class CollectionHashTable;
  1532. public:
  1533. CStatisticCollection(CStatisticCollection * _parent, const StatsScopeId & _id) : id(_id), parent(_parent)
  1534. {
  1535. }
  1536. CStatisticCollection(CStatisticCollection * _parent, MemoryBuffer & in, unsigned version) : parent(_parent)
  1537. {
  1538. id.deserialize(in, version);
  1539. unsigned numStats;
  1540. in.read(numStats);
  1541. stats.ensureCapacity(numStats);
  1542. while (numStats-- > 0)
  1543. {
  1544. Statistic next (in, version);
  1545. stats.append(next);
  1546. }
  1547. unsigned numChildren;
  1548. in.read(numChildren);
  1549. children.ensure(numChildren);
  1550. while (numChildren-- > 0)
  1551. {
  1552. CStatisticCollection * next = deserializeCollection(this, in, version);
  1553. children.add(*next);
  1554. }
  1555. }
  1556. virtual byte getCollectionType() const { return SCintermediate; }
  1557. StringBuffer &toXML(StringBuffer &out) const;
  1558. //interface IStatisticCollection:
  1559. virtual StatisticScopeType queryScopeType() const override
  1560. {
  1561. return id.queryScopeType();
  1562. }
  1563. virtual unsigned __int64 queryWhenCreated() const override
  1564. {
  1565. if (parent)
  1566. return parent->queryWhenCreated();
  1567. return 0;
  1568. }
  1569. virtual StringBuffer & getScope(StringBuffer & str) const override
  1570. {
  1571. return id.getScopeText(str);
  1572. }
  1573. virtual StringBuffer & getFullScope(StringBuffer & str) const override
  1574. {
  1575. if (parent)
  1576. {
  1577. parent->getFullScope(str);
  1578. str.append(':');
  1579. }
  1580. id.getScopeText(str);
  1581. return str;
  1582. }
  1583. virtual unsigned __int64 queryStatistic(StatisticKind kind) const override
  1584. {
  1585. ForEachItemIn(i, stats)
  1586. {
  1587. const Statistic & cur = stats.item(i);
  1588. if (cur.kind == kind)
  1589. return cur.value;
  1590. }
  1591. return 0;
  1592. }
  1593. virtual bool getStatistic(StatisticKind kind, unsigned __int64 & value) const override
  1594. {
  1595. ForEachItemIn(i, stats)
  1596. {
  1597. const Statistic & cur = stats.item(i);
  1598. if (cur.kind == kind)
  1599. {
  1600. value = cur.value;
  1601. return true;
  1602. }
  1603. }
  1604. return false;
  1605. }
  1606. virtual unsigned getNumStatistics() const override
  1607. {
  1608. return stats.ordinality();
  1609. }
  1610. virtual void getStatistic(StatisticKind & kind, unsigned __int64 & value, unsigned idx) const override
  1611. {
  1612. const Statistic & cur = stats.item(idx);
  1613. kind = cur.kind;
  1614. value = cur.value;
  1615. }
  1616. virtual IStatisticCollectionIterator & getScopes(const char * filter, bool sorted) override
  1617. {
  1618. assertex(!filter);
  1619. Owned<IStatisticCollectionIterator> hashIter = new SuperHashIIteratorOf<IStatisticCollection, IStatisticCollectionIterator, false>(children);
  1620. if (!sorted)
  1621. return *hashIter.getClear();
  1622. return * new SortedCollectionIterator(*hashIter);
  1623. }
  1624. virtual void getMinMaxScope(IStringVal & minValue, IStringVal & maxValue, StatisticScopeType searchScopeType) const override
  1625. {
  1626. if (id.queryScopeType() == searchScopeType)
  1627. {
  1628. const char * curMin = minValue.str();
  1629. const char * curMax = maxValue.str();
  1630. StringBuffer name;
  1631. id.getScopeText(name);
  1632. if (!curMin || !*curMin || strcmp(name.str(), curMin) < 0)
  1633. minValue.set(name.str());
  1634. if (!curMax || strcmp(name.str(), curMax) > 0)
  1635. maxValue.set(name.str());
  1636. }
  1637. for (auto & curChild : children)
  1638. curChild.getMinMaxScope(minValue, maxValue, searchScopeType);
  1639. }
  1640. virtual void getMinMaxActivity(unsigned & minValue, unsigned & maxValue) const override
  1641. {
  1642. unsigned activityId = id.queryActivity();
  1643. if (activityId)
  1644. {
  1645. if ((minValue == 0) || (activityId < minValue))
  1646. minValue = activityId;
  1647. if (activityId > maxValue)
  1648. maxValue = activityId;
  1649. }
  1650. SuperHashIteratorOf<CStatisticCollection> iter(children, false);
  1651. for (iter.first(); iter.isValid(); iter.next())
  1652. iter.query().getMinMaxActivity(minValue, maxValue);
  1653. }
  1654. //other public interface functions
  1655. void addStatistic(StatisticKind kind, unsigned __int64 value)
  1656. {
  1657. Statistic s(kind, value);
  1658. stats.append(s);
  1659. }
  1660. void updateStatistic(StatisticKind kind, unsigned __int64 value, StatsMergeAction mergeAction)
  1661. {
  1662. if (mergeAction != StatsMergeAppend)
  1663. {
  1664. ForEachItemIn(i, stats)
  1665. {
  1666. Statistic & cur = stats.element(i);
  1667. if (cur.kind == kind)
  1668. {
  1669. cur.value = mergeStatisticValue(cur.value, value, mergeAction);
  1670. return;
  1671. }
  1672. }
  1673. }
  1674. Statistic s(kind, value);
  1675. stats.append(s);
  1676. }
  1677. CStatisticCollection * ensureSubScope(const StatsScopeId & search, bool hasChildren)
  1678. {
  1679. //Once the CStatisicCollection is created it should not be replaced - so that returned pointers remain valid.
  1680. CStatisticCollection * match = children.find(&search);
  1681. if (match)
  1682. return match;
  1683. CStatisticCollection * ret = new CStatisticCollection(this, search);
  1684. children.add(*ret);
  1685. return ret;
  1686. }
  1687. virtual void serialize(MemoryBuffer & out) const
  1688. {
  1689. out.append(getCollectionType());
  1690. id.serialize(out);
  1691. out.append(stats.ordinality());
  1692. ForEachItemIn(iStat, stats)
  1693. stats.item(iStat).serialize(out);
  1694. out.append(children.ordinality());
  1695. SuperHashIteratorOf<CStatisticCollection> iter(children, false);
  1696. for (iter.first(); iter.isValid(); iter.next())
  1697. iter.query().serialize(out);
  1698. }
  1699. inline const StatsScopeId & queryScopeId() const { return id; }
  1700. virtual void mergeInto(IStatisticGatherer & target) const
  1701. {
  1702. StatsOptScope block(target, id);
  1703. ForEachItemIn(iStat, stats)
  1704. stats.item(iStat).mergeInto(target);
  1705. for (auto const & cur : children)
  1706. cur.mergeInto(target);
  1707. }
  1708. private:
  1709. StatsScopeId id;
  1710. CStatisticCollection * parent;
  1711. CollectionHashTable children;
  1712. StatsArray stats;
  1713. };
  1714. StringBuffer &CStatisticCollection::toXML(StringBuffer &out) const
  1715. {
  1716. out.append("<Scope id=\"");
  1717. id.getScopeText(out).append("\">\n");
  1718. if (stats.ordinality())
  1719. {
  1720. out.append(" <Stats>");
  1721. ForEachItemIn(i, stats)
  1722. stats.item(i).toXML(out);
  1723. out.append(" </Stats>\n");
  1724. }
  1725. SuperHashIteratorOf<CStatisticCollection> iter(children, false);
  1726. for (iter.first(); iter.isValid(); iter.next())
  1727. iter.query().toXML(out);
  1728. out.append("</Scope>\n");
  1729. return out;
  1730. }
  1731. static int compareCollection(IInterface * const * pl, IInterface * const *pr)
  1732. {
  1733. CStatisticCollection * l = static_cast<CStatisticCollection *>(static_cast<IStatisticCollection *>(*pl));
  1734. CStatisticCollection * r = static_cast<CStatisticCollection *>(static_cast<IStatisticCollection *>(*pr));
  1735. return l->queryScopeId().compare(r->queryScopeId());
  1736. }
  1737. //---------------------------------------------------------------------------------------------------------------------
  1738. void CollectionHashTable::onAdd(void *et)
  1739. {
  1740. }
  1741. void CollectionHashTable::onRemove(void *et)
  1742. {
  1743. CStatisticCollection * elem = reinterpret_cast<CStatisticCollection *>(et);
  1744. elem->Release();
  1745. }
  1746. unsigned CollectionHashTable::getHashFromElement(const void *et) const
  1747. {
  1748. const CStatisticCollection * elem = reinterpret_cast<const CStatisticCollection *>(et);
  1749. return elem->id.getHash();
  1750. }
  1751. unsigned CollectionHashTable::getHashFromFindParam(const void *fp) const
  1752. {
  1753. const StatsScopeId * search = reinterpret_cast<const StatsScopeId *>(fp);
  1754. return search->getHash();
  1755. }
  1756. const void * CollectionHashTable::getFindParam(const void *et) const
  1757. {
  1758. const CStatisticCollection * elem = reinterpret_cast<const CStatisticCollection *>(et);
  1759. return &elem->id;
  1760. }
  1761. bool CollectionHashTable::matchesFindParam(const void *et, const void *key, unsigned fphash) const
  1762. {
  1763. const CStatisticCollection * elem = reinterpret_cast<const CStatisticCollection *>(et);
  1764. const StatsScopeId * search = reinterpret_cast<const StatsScopeId *>(key);
  1765. return elem->id.matches(*search);
  1766. }
  1767. bool CollectionHashTable::matchesElement(const void *et, const void *searchET) const
  1768. {
  1769. const CStatisticCollection * elem = reinterpret_cast<const CStatisticCollection *>(et);
  1770. const CStatisticCollection * searchElem = reinterpret_cast<const CStatisticCollection *>(searchET);
  1771. return elem->id.matches(searchElem->id);
  1772. }
  1773. //---------------------------------------------------------------------------------------------------------------------
  1774. class CRootStatisticCollection : public CStatisticCollection
  1775. {
  1776. public:
  1777. CRootStatisticCollection(StatisticCreatorType _creatorType, const char * _creator, const StatsScopeId & _id)
  1778. : CStatisticCollection(NULL, _id), creatorType(_creatorType), creator(_creator)
  1779. {
  1780. whenCreated = getTimeStampNowValue();
  1781. }
  1782. CRootStatisticCollection(MemoryBuffer & in, unsigned version) : CStatisticCollection(NULL, in, version)
  1783. {
  1784. byte creatorTypeByte;
  1785. in.read(creatorTypeByte);
  1786. creatorType = (StatisticCreatorType)creatorTypeByte;
  1787. in.read(creator);
  1788. in.read(whenCreated);
  1789. }
  1790. virtual byte getCollectionType() const { return SCroot; }
  1791. virtual unsigned __int64 queryWhenCreated() const
  1792. {
  1793. return whenCreated;
  1794. }
  1795. virtual void serialize(MemoryBuffer & out) const
  1796. {
  1797. CStatisticCollection::serialize(out);
  1798. out.append((byte)creatorType);
  1799. out.append(creator);
  1800. out.append(whenCreated);
  1801. }
  1802. public:
  1803. StatisticCreatorType creatorType;
  1804. StringAttr creator;
  1805. unsigned __int64 whenCreated;
  1806. };
  1807. //---------------------------------------------------------------------------------------------------------------------
  1808. void serializeStatisticCollection(MemoryBuffer & out, IStatisticCollection * collection)
  1809. {
  1810. out.append(currentStatisticsVersion);
  1811. collection->serialize(out);
  1812. }
  1813. static CStatisticCollection * deserializeCollection(CStatisticCollection * parent, MemoryBuffer & in, unsigned version)
  1814. {
  1815. byte kind;
  1816. in.read(kind);
  1817. switch (kind)
  1818. {
  1819. case SCroot:
  1820. assertex(!parent);
  1821. return new CRootStatisticCollection(in, version);
  1822. case SCintermediate:
  1823. return new CStatisticCollection(parent, in, version);
  1824. default:
  1825. UNIMPLEMENTED;
  1826. }
  1827. }
  1828. IStatisticCollection * createStatisticCollection(MemoryBuffer & in)
  1829. {
  1830. unsigned version;
  1831. in.read(version);
  1832. return deserializeCollection(NULL, in, version);
  1833. }
  1834. //--------------------------------------------------------------------------------------------------------------------
  1835. class StatisticGatherer : implements CInterfaceOf<IStatisticGatherer>
  1836. {
  1837. public:
  1838. StatisticGatherer(CStatisticCollection * scope) : rootScope(scope)
  1839. {
  1840. scopes.append(*scope);
  1841. }
  1842. virtual void beginScope(const StatsScopeId & id) override
  1843. {
  1844. CStatisticCollection & tos = scopes.tos();
  1845. scopes.append(*tos.ensureSubScope(id, true));
  1846. }
  1847. virtual void beginActivityScope(unsigned id) override
  1848. {
  1849. StatsScopeId scopeId(SSTactivity, id);
  1850. CStatisticCollection & tos = scopes.tos();
  1851. scopes.append(*tos.ensureSubScope(scopeId, false));
  1852. }
  1853. virtual void beginSubGraphScope(unsigned id) override
  1854. {
  1855. StatsScopeId scopeId(SSTsubgraph, id);
  1856. CStatisticCollection & tos = scopes.tos();
  1857. scopes.append(*tos.ensureSubScope(scopeId, true));
  1858. }
  1859. virtual void beginEdgeScope(unsigned id, unsigned oid) override
  1860. {
  1861. StatsScopeId scopeId(SSTedge, id, oid);
  1862. CStatisticCollection & tos = scopes.tos();
  1863. scopes.append(*tos.ensureSubScope(scopeId, false));
  1864. }
  1865. virtual void beginChildGraphScope(unsigned id) override
  1866. {
  1867. StatsScopeId scopeId(SSTchildgraph, id);
  1868. CStatisticCollection & tos = scopes.tos();
  1869. scopes.append(*tos.ensureSubScope(scopeId, true));
  1870. }
  1871. virtual void endScope() override
  1872. {
  1873. scopes.pop();
  1874. }
  1875. virtual void addStatistic(StatisticKind kind, unsigned __int64 value) override
  1876. {
  1877. CStatisticCollection & tos = scopes.tos();
  1878. tos.addStatistic(kind, value);
  1879. }
  1880. virtual void updateStatistic(StatisticKind kind, unsigned __int64 value, StatsMergeAction mergeAction) override
  1881. {
  1882. CStatisticCollection & tos = scopes.tos();
  1883. tos.updateStatistic(kind, value, mergeAction);
  1884. }
  1885. virtual IStatisticCollection * getResult() override
  1886. {
  1887. return LINK(rootScope);
  1888. }
  1889. protected:
  1890. ICopyArrayOf<CStatisticCollection> scopes;
  1891. Linked<CStatisticCollection> rootScope;
  1892. };
  1893. extern IStatisticGatherer * createStatisticsGatherer(StatisticCreatorType creatorType, const char * creator, const StatsScopeId & rootScope)
  1894. {
  1895. //creator unused at the moment.
  1896. Owned<CStatisticCollection> rootCollection = new CRootStatisticCollection(creatorType, creator, rootScope);
  1897. return new StatisticGatherer(rootCollection);
  1898. }
  1899. //--------------------------------------------------------------------------------------------------------------------
  1900. extern IPropertyTree * selectTreeStat(IPropertyTree *node, const char *statName, const char *statType)
  1901. {
  1902. StringBuffer xpath;
  1903. xpath.appendf("att[@name='%s']", statName);
  1904. IPropertyTree *att = node->queryPropTree(xpath.str());
  1905. if (!att)
  1906. {
  1907. att = node->addPropTree("att", createPTree());
  1908. att->setProp("@name", statName);
  1909. att->setProp("@type", statType);
  1910. }
  1911. return att;
  1912. }
  1913. extern void putStatsTreeValue(IPropertyTree *node, const char *statName, const char *statType, unsigned __int64 val)
  1914. {
  1915. if (val)
  1916. selectTreeStat(node, statName, statType)->setPropInt64("@value", val);
  1917. }
  1918. class TreeNodeStatisticGatherer : public CInterfaceOf<IStatisticGatherer>
  1919. {
  1920. public:
  1921. TreeNodeStatisticGatherer(IPropertyTree & root) : node(&root) {}
  1922. virtual void beginScope(const StatsScopeId & id)
  1923. {
  1924. StringBuffer temp;
  1925. id.getScopeText(temp);
  1926. beginScope(temp.str());
  1927. }
  1928. virtual void beginSubGraphScope(unsigned id) { throwUnexpected(); }
  1929. virtual void beginChildGraphScope(unsigned id) { throwUnexpected(); }
  1930. virtual void beginActivityScope(unsigned id) { throwUnexpected(); }
  1931. virtual void beginEdgeScope(unsigned id, unsigned oid) { throwUnexpected(); }
  1932. virtual void endScope()
  1933. {
  1934. node = &stack.popGet();
  1935. }
  1936. virtual void addStatistic(StatisticKind kind, unsigned __int64 value)
  1937. {
  1938. putStatsTreeValue(node, queryStatisticName(kind), "sum", value);
  1939. }
  1940. virtual void updateStatistic(StatisticKind kind, unsigned __int64 value, StatsMergeAction mergeAction)
  1941. {
  1942. if (value)
  1943. {
  1944. IPropertyTree * stat = selectTreeStat(node, queryStatisticName(kind), "sum");
  1945. unsigned __int64 newValue = mergeStatisticValue(stat->getPropInt64("@value"), value, mergeAction);
  1946. stat->setPropInt64("@value", newValue);
  1947. }
  1948. }
  1949. virtual IStatisticCollection * getResult() { throwUnexpected(); }
  1950. protected:
  1951. void beginScope(const char * id)
  1952. {
  1953. stack.append(*node);
  1954. StringBuffer xpath;
  1955. xpath.appendf("scope[@name='%s']", id);
  1956. IPropertyTree *att = node->queryPropTree(xpath.str());
  1957. if (!att)
  1958. {
  1959. att = node->addPropTree("scope", createPTree());
  1960. att->setProp("@name", id);
  1961. }
  1962. node = att;
  1963. }
  1964. protected:
  1965. IPropertyTree * node;
  1966. ICopyArrayOf<IPropertyTree> stack;
  1967. };
  1968. //--------------------------------------------------------------------------------------------------------------------
  1969. void CRuntimeStatistic::merge(unsigned __int64 otherValue, StatsMergeAction mergeAction)
  1970. {
  1971. switch (mergeAction)
  1972. {
  1973. case StatsMergeKeepNonZero:
  1974. if (otherValue && !value)
  1975. {
  1976. unsigned __int64 zero = 0;
  1977. value.compare_exchange_strong(zero, otherValue);
  1978. }
  1979. break;
  1980. case StatsMergeAppend:
  1981. case StatsMergeReplace:
  1982. value = otherValue;
  1983. break;
  1984. case StatsMergeSum:
  1985. if (otherValue)
  1986. addAtomic(otherValue);
  1987. break;
  1988. case StatsMergeMin:
  1989. value.store_min(otherValue);
  1990. break;
  1991. case StatsMergeMax:
  1992. value.store_max(otherValue);
  1993. break;
  1994. default:
  1995. #ifdef _DEBUG
  1996. throwUnexpected();
  1997. #else
  1998. value = otherValue;
  1999. #endif
  2000. }
  2001. }
  2002. //--------------------------------------------------------------------------------------------------------------------
  2003. CRuntimeStatisticCollection::~CRuntimeStatisticCollection()
  2004. {
  2005. delete [] values;
  2006. delete queryNested();
  2007. }
  2008. CNestedRuntimeStatisticMap * CRuntimeStatisticCollection::queryNested() const
  2009. {
  2010. return nested.load(std::memory_order_relaxed);
  2011. }
  2012. CNestedRuntimeStatisticMap * CRuntimeStatisticCollection::createNested() const
  2013. {
  2014. return new CNestedRuntimeStatisticMap;
  2015. }
  2016. CNestedRuntimeStatisticMap & CRuntimeStatisticCollection::ensureNested()
  2017. {
  2018. return *querySingleton(nested, nestlock, [this]{ return this->createNested(); });
  2019. }
  2020. CriticalSection CRuntimeStatisticCollection::nestlock;
  2021. unsigned __int64 CRuntimeStatisticCollection::getSerialStatisticValue(StatisticKind kind) const
  2022. {
  2023. unsigned __int64 value = getStatisticValue(kind);
  2024. StatisticKind rawKind= queryRawKind(kind);
  2025. if (kind == rawKind)
  2026. return value;
  2027. unsigned __int64 rawValue = getStatisticValue(rawKind);
  2028. return value + convertMeasure(rawKind, kind, rawValue);
  2029. }
  2030. void CRuntimeStatisticCollection::merge(const CRuntimeStatisticCollection & other, unsigned node)
  2031. {
  2032. ForEachItemIn(i, other)
  2033. {
  2034. StatisticKind kind = other.getKind(i);
  2035. unsigned __int64 value = other.getStatisticValue(kind);
  2036. if (value)
  2037. mergeStatistic(kind, value, node);
  2038. }
  2039. CNestedRuntimeStatisticMap *otherNested = other.queryNested();
  2040. if (otherNested)
  2041. {
  2042. ensureNested().merge(*otherNested, node);
  2043. }
  2044. }
  2045. void CRuntimeStatisticCollection::updateDelta(CRuntimeStatisticCollection & target, const CRuntimeStatisticCollection & source)
  2046. {
  2047. ForEachItemIn(i, source)
  2048. {
  2049. StatisticKind kind = source.getKind(i);
  2050. unsigned __int64 sourceValue = source.getStatisticValue(kind);
  2051. if (queryMergeMode(kind) == StatsMergeSum)
  2052. {
  2053. unsigned __int64 prevValue = getStatisticValue(kind);
  2054. if (sourceValue != prevValue)
  2055. {
  2056. target.mergeStatistic(kind, sourceValue - prevValue);
  2057. setStatistic(kind, sourceValue);
  2058. }
  2059. }
  2060. else
  2061. {
  2062. if (sourceValue)
  2063. target.mergeStatistic(kind, sourceValue);
  2064. }
  2065. }
  2066. CNestedRuntimeStatisticMap *sourceNested = source.queryNested();
  2067. if (sourceNested)
  2068. {
  2069. ensureNested().updateDelta(target.ensureNested(), *sourceNested);
  2070. }
  2071. }
  2072. void CRuntimeStatisticCollection::mergeStatistic(StatisticKind kind, unsigned __int64 value)
  2073. {
  2074. queryStatistic(kind).merge(value, queryMergeMode(kind));
  2075. }
  2076. void CRuntimeStatisticCollection::sumStatistic(StatisticKind kind, unsigned __int64 value)
  2077. {
  2078. queryStatistic(kind).sum(value);
  2079. }
  2080. void CRuntimeStatisticCollection::mergeStatistic(StatisticKind kind, unsigned __int64 value, unsigned node)
  2081. {
  2082. mergeStatistic(kind, value);
  2083. }
  2084. void CRuntimeStatisticCollection::reset()
  2085. {
  2086. unsigned num = mapping.numStatistics();
  2087. for (unsigned i = 0; i <= num; i++)
  2088. values[i].clear();
  2089. }
  2090. void CRuntimeStatisticCollection::reset(const StatisticsMapping & toClear)
  2091. {
  2092. unsigned num = toClear.numStatistics();
  2093. for (unsigned i = 0; i < num; i++)
  2094. queryStatistic(toClear.getKind(i)).clear();
  2095. }
  2096. CRuntimeStatisticCollection & CRuntimeStatisticCollection::registerNested(const StatsScopeId & scope, const StatisticsMapping & mapping)
  2097. {
  2098. return ensureNested().addNested(scope, mapping).queryStats();
  2099. }
  2100. void CRuntimeStatisticCollection::rollupStatistics(unsigned numTargets, IContextLogger * const * targets) const
  2101. {
  2102. ForEachItem(iStat)
  2103. {
  2104. unsigned __int64 value = values[iStat].getClear();
  2105. if (value)
  2106. {
  2107. StatisticKind kind = getKind(iStat);
  2108. for (unsigned iTarget = 0; iTarget < numTargets; iTarget++)
  2109. targets[iTarget]->noteStatistic(kind, value);
  2110. }
  2111. }
  2112. reportIgnoredStats();
  2113. }
  2114. void CRuntimeStatisticCollection::recordStatistics(IStatisticGatherer & target) const
  2115. {
  2116. ForEachItem(i)
  2117. {
  2118. StatisticKind kind = getKind(i);
  2119. unsigned __int64 value = values[i].get();
  2120. if (value || includeStatisticIfZero(kind))
  2121. {
  2122. StatisticKind serialKind= querySerializedKind(kind);
  2123. value = convertMeasure(kind, serialKind, value);
  2124. StatsMergeAction mergeAction = queryMergeMode(serialKind);
  2125. target.updateStatistic(serialKind, value, mergeAction);
  2126. }
  2127. }
  2128. reportIgnoredStats();
  2129. CNestedRuntimeStatisticMap *qn = queryNested();
  2130. if (qn)
  2131. qn->recordStatistics(target);
  2132. }
  2133. void CRuntimeStatisticCollection::reportIgnoredStats() const
  2134. {
  2135. if (values[mapping.numStatistics()].getClear())
  2136. DBGLOG("Some statistics were added but thrown away");
  2137. }
  2138. StringBuffer & CRuntimeStatisticCollection::toXML(StringBuffer &str) const
  2139. {
  2140. ForEachItem(iStat)
  2141. {
  2142. unsigned __int64 value = values[iStat].get();
  2143. if (value)
  2144. {
  2145. StatisticKind kind = getKind(iStat);
  2146. const char * name = queryStatisticName(kind);
  2147. str.appendf("<%s>%" I64F "d</%s>", name, value, name);
  2148. }
  2149. }
  2150. CNestedRuntimeStatisticMap *qn = queryNested();
  2151. if (qn)
  2152. qn->toXML(str);
  2153. return str;
  2154. }
  2155. StringBuffer & CRuntimeStatisticCollection::toStr(StringBuffer &str) const
  2156. {
  2157. ForEachItem(iStat)
  2158. {
  2159. unsigned __int64 value = values[iStat].get();
  2160. if (value)
  2161. {
  2162. StatisticKind kind = getKind(iStat);
  2163. StatisticKind serialKind = querySerializedKind(kind);
  2164. if (kind != serialKind)
  2165. value = convertMeasure(kind, serialKind, value);
  2166. const char * name = queryStatisticName(serialKind);
  2167. str.append(' ').append(name).append("=");
  2168. formatStatistic(str, value, serialKind);
  2169. }
  2170. }
  2171. CNestedRuntimeStatisticMap *qn = queryNested();
  2172. if (qn)
  2173. qn->toStr(str);
  2174. return str;
  2175. }
  2176. void CRuntimeStatisticCollection::deserialize(MemoryBuffer& in)
  2177. {
  2178. unsigned numValid;
  2179. in.readPacked(numValid);
  2180. for (unsigned i=0; i < numValid; i++)
  2181. {
  2182. unsigned kindVal;
  2183. unsigned __int64 value;
  2184. in.readPacked(kindVal).readPacked(value);
  2185. StatisticKind kind = (StatisticKind)kindVal;
  2186. setStatistic(kind, value);
  2187. }
  2188. bool hasNested;
  2189. in.read(hasNested);
  2190. if (hasNested)
  2191. {
  2192. ensureNested().deserializeMerge(in);
  2193. }
  2194. }
  2195. void CRuntimeStatisticCollection::deserializeMerge(MemoryBuffer& in)
  2196. {
  2197. unsigned numValid;
  2198. in.readPacked(numValid);
  2199. for (unsigned i=0; i < numValid; i++)
  2200. {
  2201. unsigned kindVal;
  2202. unsigned __int64 value;
  2203. in.readPacked(kindVal).readPacked(value);
  2204. StatisticKind kind = (StatisticKind)kindVal;
  2205. StatsMergeAction mergeAction = queryMergeMode(kind);
  2206. mergeStatistic(kind, value, mergeAction);
  2207. }
  2208. bool hasNested;
  2209. in.read(hasNested);
  2210. if (hasNested)
  2211. {
  2212. ensureNested().deserializeMerge(in);
  2213. }
  2214. }
  2215. void CRuntimeStatisticCollection::getNodeProgressInfo(IPropertyTree &node) const
  2216. {
  2217. TreeNodeStatisticGatherer gatherer(node);
  2218. recordStatistics(gatherer);
  2219. }
  2220. bool CRuntimeStatisticCollection::serialize(MemoryBuffer& out) const
  2221. {
  2222. unsigned numValid = 0;
  2223. ForEachItem(i1)
  2224. {
  2225. if (values[i1].get())
  2226. numValid++;
  2227. }
  2228. out.appendPacked(numValid);
  2229. ForEachItem(i2)
  2230. {
  2231. unsigned __int64 value = values[i2].get();
  2232. if (value)
  2233. {
  2234. StatisticKind kind = mapping.getKind(i2);
  2235. StatisticKind serialKind= querySerializedKind(kind);
  2236. if (kind != serialKind)
  2237. value = convertMeasure(kind, serialKind, value);
  2238. out.appendPacked((unsigned)serialKind);
  2239. out.appendPacked(value);
  2240. }
  2241. }
  2242. bool nonEmpty = (numValid != 0);
  2243. CNestedRuntimeStatisticMap *qn = queryNested();
  2244. out.append(qn != nullptr);
  2245. if (qn)
  2246. {
  2247. if (qn->serialize(out))
  2248. nonEmpty = true;
  2249. }
  2250. return nonEmpty;
  2251. }
  2252. //---------------------------------------------------------------------------------------------------------------------
  2253. void CRuntimeSummaryStatisticCollection::DerivedStats::mergeStatistic(unsigned __int64 value, unsigned node)
  2254. {
  2255. if (count == 0)
  2256. {
  2257. min = value;
  2258. max = value;
  2259. minNode = node;
  2260. maxNode = node;
  2261. }
  2262. else
  2263. {
  2264. if (value < min)
  2265. {
  2266. min = value;
  2267. minNode = node;
  2268. }
  2269. if (value > max)
  2270. {
  2271. max = value;
  2272. maxNode = node;
  2273. }
  2274. }
  2275. count++;
  2276. sum += value;
  2277. double dvalue = (double)value;
  2278. sumSquares += dvalue * dvalue;
  2279. }
  2280. CRuntimeSummaryStatisticCollection::CRuntimeSummaryStatisticCollection(const StatisticsMapping & _mapping) : CRuntimeStatisticCollection(_mapping)
  2281. {
  2282. derived = new DerivedStats[ordinality()+1];
  2283. }
  2284. CRuntimeSummaryStatisticCollection::~CRuntimeSummaryStatisticCollection()
  2285. {
  2286. delete[] derived;
  2287. }
  2288. CNestedRuntimeStatisticMap * CRuntimeSummaryStatisticCollection::createNested() const
  2289. {
  2290. return new CNestedSummaryRuntimeStatisticMap;
  2291. }
  2292. void CRuntimeSummaryStatisticCollection::mergeStatistic(StatisticKind kind, unsigned __int64 value, unsigned node)
  2293. {
  2294. CRuntimeStatisticCollection::mergeStatistic(kind, value);
  2295. unsigned index = queryMapping().getIndex(kind);
  2296. derived[index].mergeStatistic(value, node);
  2297. }
  2298. static bool skewHasMeaning(StatisticKind kind)
  2299. {
  2300. //Check that skew makes any sense for the type of measurement
  2301. switch (queryMeasure(kind))
  2302. {
  2303. case SMeasureTimeNs:
  2304. case SMeasureCount:
  2305. case SMeasureSize:
  2306. return true;
  2307. default:
  2308. return false;
  2309. }
  2310. }
  2311. static bool isSignificantRange(StatisticKind kind, unsigned __int64 range, unsigned __int64 mean)
  2312. {
  2313. //Ignore tiny differences (often occur with counts of single rows on 1 slave node)
  2314. unsigned insignificantDiff = 1;
  2315. switch (queryMeasure(kind))
  2316. {
  2317. case SMeasureTimestampUs:
  2318. insignificantDiff = 1000; // Ignore 1ms timestamp difference between nodes
  2319. break;
  2320. case SMeasureTimeNs:
  2321. insignificantDiff = 1000; // Ignore 1us timing difference between nodes
  2322. break;
  2323. case SMeasureSize:
  2324. insignificantDiff = 1024;
  2325. break;
  2326. }
  2327. if (range <= insignificantDiff)
  2328. return false;
  2329. if (queryMergeMode(kind) == StatsMergeSum)
  2330. {
  2331. //if the range is < 0.01% of the mean, then it is unlikely to be interesting
  2332. if (range * 10000 < mean)
  2333. return false;
  2334. }
  2335. return true;
  2336. }
  2337. static bool isWorthReportingMergedValue(StatisticKind kind)
  2338. {
  2339. switch (queryMergeMode(kind))
  2340. {
  2341. //Does the merged value have a meaning?
  2342. case StatsMergeSum:
  2343. case StatsMergeMin:
  2344. case StatsMergeMax:
  2345. break;
  2346. default:
  2347. return false;
  2348. }
  2349. switch (queryMeasure(kind))
  2350. {
  2351. case SMeasureTimeNs:
  2352. //Not generally worth reporting the total time across all slaves
  2353. return false;
  2354. }
  2355. return true;
  2356. }
  2357. void CRuntimeSummaryStatisticCollection::recordStatistics(IStatisticGatherer & target) const
  2358. {
  2359. for (unsigned i = 0; i < ordinality(); i++)
  2360. {
  2361. DerivedStats & cur = derived[i];
  2362. StatisticKind kind = getKind(i);
  2363. StatisticKind serialKind = querySerializedKind(kind);
  2364. if (cur.count)
  2365. {
  2366. //Thor should always publish the average value for a stat, and the merged value if it makes sense.
  2367. //So that it is easy to analyse graphs independent of the number of slave nodes it is executed on.
  2368. unsigned __int64 mergedValue = convertMeasure(kind, serialKind, values[i].get());
  2369. if (isWorthReportingMergedValue(serialKind))
  2370. {
  2371. if (mergedValue || includeStatisticIfZero(serialKind))
  2372. target.addStatistic(serialKind, mergedValue);
  2373. }
  2374. unsigned __int64 minValue = convertMeasure(kind, serialKind, cur.min);
  2375. unsigned __int64 maxValue = convertMeasure(kind, serialKind, cur.max);
  2376. if (minValue != maxValue)
  2377. {
  2378. //Avoid rounding errors summing values as doubles - if they were also summed as integers. Probably overkill!
  2379. //There may still be noticeable rounding errors with timestamps... revisit if it is an issue with any measurement
  2380. double sum = (queryMergeMode(kind) == StatsMergeSum) ? (double)mergedValue : convertSumMeasure(kind, serialKind, cur.sum);
  2381. double mean = (double)(sum / cur.count);
  2382. unsigned __int64 range = maxValue - minValue;
  2383. target.addStatistic((StatisticKind)(serialKind|StAvgX), (unsigned __int64)mean);
  2384. target.addStatistic((StatisticKind)(serialKind|StMinX), minValue);
  2385. target.addStatistic((StatisticKind)(serialKind|StMaxX), maxValue);
  2386. //Exclude delta and std dev if a single node was the only one that provided a value
  2387. if ((minValue != 0) || (maxValue != mergedValue))
  2388. {
  2389. //The delta/std dev may have a different unit from the original values e.g., timestamps->times, so needs scaling
  2390. unsigned __int64 scaledRange = convertMeasure(serialKind, serialKind|StDeltaX, range);
  2391. target.addStatistic(serialKind|StDeltaX, scaledRange);
  2392. if (skewHasMeaning(serialKind))
  2393. {
  2394. //Sum of squares needs to be translated twice
  2395. double sumSquares = convertSquareMeasure(kind, serialKind, cur.sumSquares);
  2396. double variance = (sumSquares - sum * mean) / cur.count;
  2397. double stdDev = sqrt(variance);
  2398. unsigned __int64 scaledStdDev = convertMeasure(serialKind, serialKind|StStdDevX, stdDev);
  2399. target.addStatistic(serialKind|StStdDevX, scaledStdDev);
  2400. }
  2401. }
  2402. //First test is redundant - but protects against minValue != maxValue test above changing.
  2403. if ((cur.minNode != cur.maxNode) && isSignificantRange(serialKind, range, mean))
  2404. {
  2405. target.addStatistic((StatisticKind)(serialKind|StNodeMin), cur.minNode);
  2406. target.addStatistic((StatisticKind)(serialKind|StNodeMax), cur.maxNode);
  2407. if (skewHasMeaning(serialKind))
  2408. {
  2409. double maxSkew = (10000.0 * ((maxValue-mean)/mean));
  2410. double minSkew = (10000.0 * ((mean-minValue)/mean));
  2411. target.addStatistic((StatisticKind)(serialKind|StSkewMin), (unsigned __int64)minSkew);
  2412. target.addStatistic((StatisticKind)(serialKind|StSkewMax), (unsigned __int64)maxSkew);
  2413. }
  2414. }
  2415. }
  2416. else
  2417. {
  2418. if (minValue || includeStatisticIfZero(serialKind))
  2419. target.addStatistic((StatisticKind)(serialKind|StAvgX), minValue);
  2420. }
  2421. }
  2422. else
  2423. {
  2424. //No results received from any of the slave yet... so do not report any stats
  2425. }
  2426. }
  2427. reportIgnoredStats();
  2428. CNestedRuntimeStatisticMap *qn = queryNested();
  2429. if (qn)
  2430. qn->recordStatistics(target);
  2431. }
  2432. bool CRuntimeSummaryStatisticCollection::serialize(MemoryBuffer & out) const
  2433. {
  2434. UNIMPLEMENTED; // NB: Need to convert sum squares twice.
  2435. }
  2436. void CRuntimeSummaryStatisticCollection::deserialize(MemoryBuffer & in)
  2437. {
  2438. UNIMPLEMENTED;
  2439. }
  2440. void CRuntimeSummaryStatisticCollection::deserializeMerge(MemoryBuffer& in)
  2441. {
  2442. UNIMPLEMENTED;
  2443. }
  2444. //---------------------------------------------------
  2445. bool CNestedRuntimeStatisticCollection::matches(const StatsScopeId & otherScope) const
  2446. {
  2447. return scope.matches(otherScope);
  2448. }
  2449. //NOTE: When deserializing, the scope is deserialized by the caller, and the correct target selected
  2450. //which is why there is no corresponding deserialize() ofthe scope at this point
  2451. void CNestedRuntimeStatisticCollection::deserialize(MemoryBuffer & in)
  2452. {
  2453. stats->deserialize(in);
  2454. }
  2455. void CNestedRuntimeStatisticCollection::deserializeMerge(MemoryBuffer& in)
  2456. {
  2457. stats->deserializeMerge(in);
  2458. }
  2459. void CNestedRuntimeStatisticCollection::merge(const CNestedRuntimeStatisticCollection & other, unsigned node)
  2460. {
  2461. stats->merge(other.queryStats(), node);
  2462. }
  2463. bool CNestedRuntimeStatisticCollection::serialize(MemoryBuffer& out) const
  2464. {
  2465. scope.serialize(out);
  2466. return stats->serialize(out);
  2467. }
  2468. void CNestedRuntimeStatisticCollection::recordStatistics(IStatisticGatherer & target) const
  2469. {
  2470. target.beginScope(scope);
  2471. stats->recordStatistics(target);
  2472. target.endScope();
  2473. }
  2474. StringBuffer & CNestedRuntimeStatisticCollection::toStr(StringBuffer &str) const
  2475. {
  2476. str.append(' ');
  2477. scope.getScopeText(str).append("={");
  2478. stats->toStr(str);
  2479. return str.append(" }");
  2480. }
  2481. StringBuffer & CNestedRuntimeStatisticCollection::toXML(StringBuffer &str) const
  2482. {
  2483. str.append("<Scope id=\"");
  2484. scope.getScopeText(str).append("\">");
  2485. stats->toXML(str);
  2486. return str.append("</Scope>");
  2487. }
  2488. void CNestedRuntimeStatisticCollection::updateDelta(CNestedRuntimeStatisticCollection & target, const CNestedRuntimeStatisticCollection & source)
  2489. {
  2490. stats->updateDelta(*target.stats, *source.stats);
  2491. }
  2492. //---------------------------------------------------
  2493. CNestedRuntimeStatisticCollection & CNestedRuntimeStatisticMap::addNested(const StatsScopeId & scope, const StatisticsMapping & mapping)
  2494. {
  2495. unsigned mapSize;
  2496. unsigned entry;
  2497. {
  2498. ReadLockBlock b(lock);
  2499. mapSize = map.length();
  2500. for (entry = 0; entry < mapSize; entry++)
  2501. {
  2502. CNestedRuntimeStatisticCollection & cur = map.item(entry);
  2503. if (cur.matches(scope))
  2504. return cur;
  2505. }
  2506. }
  2507. {
  2508. WriteLockBlock b(lock);
  2509. // Check no-one added anything between the read and write locks
  2510. mapSize = map.length();
  2511. for (; entry < mapSize; entry++)
  2512. {
  2513. CNestedRuntimeStatisticCollection & cur = map.item(entry);
  2514. if (cur.matches(scope))
  2515. return cur;
  2516. }
  2517. CNestedRuntimeStatisticCollection * stats = new CNestedRuntimeStatisticCollection(scope, createStats(mapping));
  2518. map.append(*stats);
  2519. return *stats;
  2520. }
  2521. }
  2522. void CNestedRuntimeStatisticMap::deserialize(MemoryBuffer& in)
  2523. {
  2524. unsigned numItems;
  2525. in.readPacked(numItems);
  2526. for (unsigned i=0; i < numItems; i++)
  2527. {
  2528. StatsScopeId scope;
  2529. scope.deserialize(in, currentStatisticsVersion);
  2530. //Use allStatistics as the default mapping if it hasn't already been added.
  2531. CNestedRuntimeStatisticCollection & child = addNested(scope, allStatistics);
  2532. child.deserialize(in);
  2533. }
  2534. }
  2535. void CNestedRuntimeStatisticMap::deserializeMerge(MemoryBuffer& in)
  2536. {
  2537. unsigned numItems;
  2538. in.readPacked(numItems);
  2539. for (unsigned i=0; i < numItems; i++)
  2540. {
  2541. StatsScopeId scope;
  2542. scope.deserialize(in, currentStatisticsVersion);
  2543. //Use allStatistics as the default mapping if it hasn't already been added.
  2544. CNestedRuntimeStatisticCollection & child = addNested(scope, allStatistics);
  2545. child.deserializeMerge(in);
  2546. }
  2547. }
  2548. void CNestedRuntimeStatisticMap::merge(const CNestedRuntimeStatisticMap & other, unsigned node)
  2549. {
  2550. ReadLockBlock b(other.lock);
  2551. ForEachItemIn(i, other.map)
  2552. {
  2553. CNestedRuntimeStatisticCollection & cur = other.map.item(i);
  2554. CNestedRuntimeStatisticCollection & target = addNested(cur.scope, cur.queryMapping());
  2555. target.merge(cur, node);
  2556. }
  2557. }
  2558. void CNestedRuntimeStatisticMap::updateDelta(CNestedRuntimeStatisticMap & target, const CNestedRuntimeStatisticMap & source)
  2559. {
  2560. ReadLockBlock b(source.lock);
  2561. ForEachItemIn(i, source.map)
  2562. {
  2563. CNestedRuntimeStatisticCollection & curSource = source.map.item(i);
  2564. CNestedRuntimeStatisticCollection & curTarget = target.addNested(curSource.scope, curSource.queryMapping());
  2565. CNestedRuntimeStatisticCollection & curDelta = addNested(curSource.scope, curSource.queryMapping());
  2566. curDelta.updateDelta(curTarget, curSource);
  2567. }
  2568. }
  2569. bool CNestedRuntimeStatisticMap::serialize(MemoryBuffer& out) const
  2570. {
  2571. ReadLockBlock b(lock);
  2572. out.appendPacked(map.ordinality());
  2573. bool nonEmpty = false;
  2574. ForEachItemIn(i, map)
  2575. {
  2576. if (map.item(i).serialize(out))
  2577. nonEmpty = true;
  2578. }
  2579. return nonEmpty;
  2580. }
  2581. void CNestedRuntimeStatisticMap::recordStatistics(IStatisticGatherer & target) const
  2582. {
  2583. ReadLockBlock b(lock);
  2584. ForEachItemIn(i, map)
  2585. map.item(i).recordStatistics(target);
  2586. }
  2587. StringBuffer & CNestedRuntimeStatisticMap::toStr(StringBuffer &str) const
  2588. {
  2589. ReadLockBlock b(lock);
  2590. ForEachItemIn(i, map)
  2591. map.item(i).toStr(str);
  2592. return str;
  2593. }
  2594. StringBuffer & CNestedRuntimeStatisticMap::toXML(StringBuffer &str) const
  2595. {
  2596. ReadLockBlock b(lock);
  2597. ForEachItemIn(i, map)
  2598. map.item(i).toXML(str);
  2599. return str;
  2600. }
  2601. CRuntimeStatisticCollection * CNestedRuntimeStatisticMap::createStats(const StatisticsMapping & mapping)
  2602. {
  2603. return new CRuntimeStatisticCollection(mapping);
  2604. }
  2605. CRuntimeStatisticCollection * CNestedSummaryRuntimeStatisticMap::createStats(const StatisticsMapping & mapping)
  2606. {
  2607. return new CRuntimeSummaryStatisticCollection(mapping);
  2608. }
  2609. //---------------------------------------------------
  2610. void StatsAggregation::noteValue(stat_type value)
  2611. {
  2612. if (count == 0)
  2613. {
  2614. minValue = value;
  2615. maxValue = value;
  2616. }
  2617. else
  2618. {
  2619. if (value < minValue)
  2620. minValue = value;
  2621. else if (value > maxValue)
  2622. maxValue = value;
  2623. }
  2624. count++;
  2625. sumValue += value;
  2626. }
  2627. stat_type StatsAggregation::getAve() const
  2628. {
  2629. return (sumValue / count);
  2630. }
  2631. //---------------------------------------------------
  2632. ScopeCompare compareScopes(const char * scope, const char * key)
  2633. {
  2634. byte left = *scope;
  2635. byte right = *key;
  2636. //Check for root scope "" compared with anything
  2637. if (!left)
  2638. {
  2639. if (!right)
  2640. return SCequal;
  2641. return SCparent;
  2642. }
  2643. else if (!right)
  2644. {
  2645. return SCchild;
  2646. }
  2647. bool hadCommonScope = false;
  2648. for (;;)
  2649. {
  2650. if (left != right)
  2651. {
  2652. //FUTURE: Extend this function to support skipping numbers to allow wildcard matching
  2653. if (!left)
  2654. {
  2655. if (right == ':')
  2656. return SCparent; // scope is a parent (prefix) of the key
  2657. }
  2658. if (!right)
  2659. {
  2660. if (left == ':')
  2661. return SCchild; // scope is a child (superset) of the key
  2662. }
  2663. return hadCommonScope ? SCrelated : SCunrelated;
  2664. }
  2665. if (!left)
  2666. return SCequal;
  2667. if (left == ':')
  2668. hadCommonScope = true;
  2669. left = *++scope;
  2670. right =*++key;
  2671. }
  2672. }
  2673. ScopeFilter::ScopeFilter(const char * scopeList)
  2674. {
  2675. //MORE: This currently expands a list of scopes - it should probably be improved
  2676. scopes.appendList(scopeList, ",");
  2677. }
  2678. void ScopeFilter::addScope(const char * scope)
  2679. {
  2680. if (!scope)
  2681. return;
  2682. if (streq(scope, "*"))
  2683. {
  2684. scopes.kill();
  2685. minDepth = 0;
  2686. maxDepth = UINT_MAX;
  2687. return;
  2688. }
  2689. if (ids)
  2690. throw makeStringExceptionV(0, "Cannot filter by id and scope in the same request");
  2691. unsigned depth = queryScopeDepth(scope);
  2692. if ((scopes.ordinality() == 0) || (depth < minDepth))
  2693. minDepth = depth;
  2694. if ((scopes.ordinality() == 0) || (depth > maxDepth))
  2695. maxDepth = depth;
  2696. scopes.append(scope);
  2697. }
  2698. void ScopeFilter::addScopes(const char * scope)
  2699. {
  2700. StringArray list;
  2701. list.appendList(scope, ",");
  2702. ForEachItemIn(i, list)
  2703. addScope(list.item(i));
  2704. }
  2705. void ScopeFilter::addScopeType(StatisticScopeType scopeType)
  2706. {
  2707. if (scopeType == SSTall)
  2708. return;
  2709. scopeTypes.append(scopeType);
  2710. }
  2711. void ScopeFilter::addId(const char * id)
  2712. {
  2713. if (scopes)
  2714. throw makeStringExceptionV(0, "Cannot filter by id and scope in the same request");
  2715. ids.append(id);
  2716. }
  2717. void ScopeFilter::setDepth(unsigned low, unsigned high)
  2718. {
  2719. if (low > high)
  2720. throw makeStringExceptionV(0, "Depth parameters in wrong order %u..%u", low, high);
  2721. minDepth = low;
  2722. maxDepth = high;
  2723. }
  2724. void ScopeFilter::intersectDepth(const unsigned low, const unsigned high)
  2725. {
  2726. if (low > high)
  2727. throw makeStringExceptionV(0, "Depth parameters in wrong order %u..%u", low, high);
  2728. if (minDepth < low)
  2729. minDepth = low;
  2730. if (maxDepth > high)
  2731. maxDepth = high;
  2732. }
  2733. ScopeCompare ScopeFilter::compare(const char * scope) const
  2734. {
  2735. ScopeCompare result = SCunknown;
  2736. if (scopes)
  2737. {
  2738. //If scopes have been provided, then we are searching for an exact match against that scope
  2739. ForEachItemIn(i, scopes)
  2740. result |= compareScopes(scope, scopes.item(i));
  2741. }
  2742. else
  2743. {
  2744. //How does the depth of the scope compare with the range we are expecting?
  2745. unsigned depth = queryScopeDepth(scope);
  2746. if (depth < minDepth)
  2747. return SCparent;
  2748. if (depth > maxDepth)
  2749. return SCchild;
  2750. //Assume it is a match until proven otherwise
  2751. result |= SCequal;
  2752. // Could be the child of a match
  2753. if (depth > minDepth)
  2754. result |= SCchild;
  2755. //Could be the parent of a match
  2756. if (depth < maxDepth)
  2757. result |= SCparent;
  2758. //Check if the type of the current object matches the type
  2759. const char * tail = queryScopeTail(scope);
  2760. if (scopeTypes.ordinality())
  2761. {
  2762. StatsScopeId id(tail);
  2763. if (!scopeTypes.contains(id.queryScopeType()))
  2764. result &= ~SCequal;
  2765. }
  2766. if (ids)
  2767. {
  2768. if (!ids.contains(tail))
  2769. result &= ~SCequal;
  2770. }
  2771. }
  2772. if (!(result & SCequal))
  2773. return result;
  2774. //Have a match - now check that the attributes match as required
  2775. //MORE:
  2776. if (false)
  2777. {
  2778. result &= ~SCequal;
  2779. }
  2780. return result;
  2781. }
  2782. bool ScopeFilter::canAlwaysPreFilter() const
  2783. {
  2784. //If the only filter being applied is a restriction on the minimum depth, then you can always apply it as a pre-filter
  2785. return (!ids && !scopeTypes && !scopes && maxDepth == UINT_MAX);
  2786. }
  2787. int ScopeFilter::compareDepth(unsigned depth) const
  2788. {
  2789. if (depth < minDepth)
  2790. return -1;
  2791. if (depth > maxDepth)
  2792. return +1;
  2793. return 0;
  2794. }
  2795. StringBuffer & ScopeFilter::describe(StringBuffer & out) const
  2796. {
  2797. if ((minDepth != 0) || (maxDepth != UINT_MAX))
  2798. {
  2799. if (minDepth == maxDepth)
  2800. out.appendf(",depth(%u)", minDepth);
  2801. else
  2802. out.appendf(",depth(%u,%u)", minDepth, maxDepth);
  2803. }
  2804. if (scopeTypes)
  2805. {
  2806. out.append(",stype[");
  2807. ForEachItemIn(i, scopeTypes)
  2808. {
  2809. if (i)
  2810. out.append(",");
  2811. out.append(queryScopeTypeName((StatisticScopeType)scopeTypes.item(i)));
  2812. }
  2813. out.append("]");
  2814. }
  2815. if (scopes)
  2816. {
  2817. out.append(",scope[");
  2818. ForEachItemIn(i, scopes)
  2819. {
  2820. if (i)
  2821. out.append(",");
  2822. out.append(scopes.item(i));
  2823. }
  2824. out.append("]");
  2825. }
  2826. if (ids)
  2827. {
  2828. out.append(",id[");
  2829. ForEachItemIn(i, ids)
  2830. {
  2831. if (i)
  2832. out.append(",");
  2833. out.append(ids.item(i));
  2834. }
  2835. out.append("]");
  2836. }
  2837. return out;
  2838. }
  2839. void ScopeFilter::finishedFilter()
  2840. {
  2841. //If scopeTypes are provided, then this code ensure that any scopes and ids match them
  2842. //but that would have little benefit, and would cause complications if the only id was removed
  2843. //Some scope types can only exist at a single level.
  2844. if (scopeTypes.ordinality() == 1)
  2845. {
  2846. switch (scopeTypes.item(0))
  2847. {
  2848. case SSTglobal:
  2849. intersectDepth(0, 0);
  2850. break;
  2851. case SSTworkflow:
  2852. intersectDepth(1, 1);
  2853. break;
  2854. case SSTgraph:
  2855. //This should really be intersectDepth(2,2) but workunits prior to 7.4 did not have graph ids prefixed by the wfid
  2856. //Remove once 7.2 is a distant memory (see HPCC-22887)
  2857. intersectDepth(1, 2);
  2858. break;
  2859. case SSTsubgraph:
  2860. intersectDepth(3, UINT_MAX);
  2861. break;
  2862. case SSTactivity:
  2863. intersectDepth(4, UINT_MAX);
  2864. break;
  2865. }
  2866. }
  2867. }
  2868. bool ScopeFilter::hasSingleMatch() const
  2869. {
  2870. return scopes.ordinality() == 1 || ids.ordinality() == 1;
  2871. }
  2872. bool ScopeFilter::matchOnly(StatisticScopeType scopeType) const
  2873. {
  2874. if ((scopeTypes.ordinality() == 1) && (scopeTypes.item(0) == scopeType))
  2875. return true;
  2876. //Check the types of the scopes that are being searched
  2877. if (scopes.ordinality())
  2878. {
  2879. ForEachItemIn(i, scopes)
  2880. {
  2881. const char * scopeId = queryScopeTail(scopes.item(i));
  2882. StatsScopeId id(scopeId);
  2883. if (id.queryScopeType() != scopeType)
  2884. return false;
  2885. }
  2886. return true;
  2887. }
  2888. if (ids.ordinality())
  2889. {
  2890. ForEachItemIn(i, ids)
  2891. {
  2892. const char * scopeId = ids.item(i);
  2893. StatsScopeId id(scopeId);
  2894. if (id.queryScopeType() != scopeType)
  2895. return false;
  2896. }
  2897. return true;
  2898. }
  2899. return false;
  2900. }
  2901. //---------------------------------------------------
  2902. bool ScopedItemFilter::matchDepth(unsigned low, unsigned high) const
  2903. {
  2904. if (maxDepth && low && maxDepth < low)
  2905. return false;
  2906. if (minDepth && high && minDepth > high)
  2907. return false;
  2908. return true;
  2909. }
  2910. bool ScopedItemFilter::match(const char * search) const
  2911. {
  2912. if (search)
  2913. {
  2914. if (value)
  2915. {
  2916. if (hasWildcard)
  2917. {
  2918. //MORE: If wildcarding ends up being used a lot then this should be replaced with something that creates a DFA
  2919. if (!WildMatch(search, value, false))
  2920. return false;
  2921. }
  2922. else
  2923. {
  2924. return streq(search, value);
  2925. }
  2926. }
  2927. if (minDepth || maxDepth)
  2928. {
  2929. unsigned searchDepth = queryScopeDepth(search);
  2930. if (searchDepth < minDepth)
  2931. return false;
  2932. if (maxDepth && searchDepth > maxDepth)
  2933. return false;
  2934. }
  2935. }
  2936. return true;
  2937. }
  2938. bool ScopedItemFilter::recurseChildScopes(const char * curScope) const
  2939. {
  2940. if (maxDepth == 0 || !curScope)
  2941. return true;
  2942. if (queryScopeDepth(curScope) >= maxDepth)
  2943. return false;
  2944. return true;
  2945. }
  2946. void ScopedItemFilter::set(const char * _value)
  2947. {
  2948. if (_value && !streq(_value, "*") )
  2949. {
  2950. value.set(_value);
  2951. minDepth = queryScopeDepth(_value);
  2952. if (!strchr(_value, '*'))
  2953. {
  2954. maxDepth = minDepth;
  2955. hasWildcard = strchr(_value, '?') != NULL;
  2956. }
  2957. else
  2958. hasWildcard = true;
  2959. }
  2960. else
  2961. value.clear();
  2962. }
  2963. void ScopedItemFilter::setDepth(unsigned _depth)
  2964. {
  2965. minDepth = _depth;
  2966. maxDepth = _depth;
  2967. }
  2968. void ScopedItemFilter::setDepth(unsigned _minDepth, unsigned _maxDepth)
  2969. {
  2970. minDepth = _minDepth;
  2971. maxDepth = _maxDepth;
  2972. }
  2973. StatisticsFilter::StatisticsFilter()
  2974. {
  2975. init();
  2976. }
  2977. StatisticsFilter::StatisticsFilter(const char * filter)
  2978. {
  2979. init();
  2980. setFilter(filter);
  2981. }
  2982. StatisticsFilter::StatisticsFilter(StatisticCreatorType _creatorType, StatisticScopeType _scopeType, StatisticMeasure _measure, StatisticKind _kind)
  2983. {
  2984. init();
  2985. creatorType = _creatorType;
  2986. scopeType = _scopeType;
  2987. measure = _measure;
  2988. kind = _kind;
  2989. }
  2990. StatisticsFilter::StatisticsFilter(const char * _creatorType, const char * _scopeType, const char * _kind)
  2991. {
  2992. init();
  2993. set(_creatorType, _scopeType, _kind);
  2994. }
  2995. StatisticsFilter::StatisticsFilter(const char * _creatorTypeText, const char * _creator, const char * _scopeTypeText, const char * _scope, const char * _measureText, const char * _kindText)
  2996. {
  2997. init();
  2998. set(_creatorTypeText, _creator, _scopeTypeText, _scope, _measureText, _kindText);
  2999. }
  3000. StatisticsFilter::StatisticsFilter(StatisticCreatorType _creatorType, const char * _creator, StatisticScopeType _scopeType, const char * _scope, StatisticMeasure _measure, StatisticKind _kind)
  3001. {
  3002. init();
  3003. creatorType = _creatorType;
  3004. setCreator(_creator);
  3005. scopeType = _scopeType;
  3006. setScope(_scope);
  3007. measure = _measure;
  3008. kind = _kind;
  3009. }
  3010. void StatisticsFilter::init()
  3011. {
  3012. creatorType = SCTall;
  3013. scopeType = SSTall;
  3014. measure = SMeasureAll;
  3015. kind = StKindAll;
  3016. }
  3017. bool StatisticsFilter::matches(StatisticCreatorType curCreatorType, const char * curCreator, StatisticScopeType curScopeType, const char * curScope, StatisticMeasure curMeasure, StatisticKind curKind, unsigned __int64 value) const
  3018. {
  3019. if ((curCreatorType != SCTall) && (creatorType != SCTall) && (creatorType != curCreatorType))
  3020. return false;
  3021. if ((curScopeType != SSTall) && (scopeType != SSTall) && (scopeType != curScopeType))
  3022. return false;
  3023. if ((curMeasure != SMeasureAll) && (measure != SMeasureAll) && (measure != curMeasure))
  3024. return false;
  3025. if ((curKind!= StKindAll) && (kind != StKindAll) && (kind != curKind))
  3026. return false;
  3027. if (!creatorFilter.match(curCreator))
  3028. return false;
  3029. if (!scopeFilter.match(curScope))
  3030. return false;
  3031. if (value != MaxStatisticValue)
  3032. {
  3033. if ((value < minValue) || (value > maxValue))
  3034. return false;
  3035. }
  3036. return true;
  3037. }
  3038. bool StatisticsFilter::recurseChildScopes(StatisticScopeType curScopeType, const char * curScope) const
  3039. {
  3040. switch (curScopeType)
  3041. {
  3042. case SSTgraph:
  3043. // A child of a graph will have depth 2 or more
  3044. if (!scopeFilter.matchDepth(2, (unsigned)-1))
  3045. return false;
  3046. break;
  3047. case SSTsubgraph:
  3048. // A child of a subgraph will have depth 3 or more
  3049. if (!scopeFilter.matchDepth(3, (unsigned)-1))
  3050. return false;
  3051. break;
  3052. }
  3053. if (!curScope)
  3054. return true;
  3055. return scopeFilter.recurseChildScopes(curScope);
  3056. }
  3057. void StatisticsFilter::set(const char * creatorTypeText, const char * scopeTypeText, const char * kindText)
  3058. {
  3059. StatisticCreatorType creatorType = queryCreatorType(creatorTypeText, SCTnone);
  3060. StatisticScopeType scopeType = queryScopeType(scopeTypeText, SSTnone);
  3061. if (creatorType != SCTnone)
  3062. setCreatorType(creatorType);
  3063. if (scopeType != SSTnone)
  3064. setScopeType(scopeType);
  3065. setKind(kindText);
  3066. }
  3067. void StatisticsFilter::set(const char * _creatorTypeText, const char * _creator, const char * _scopeTypeText, const char * _scope, const char * _measureText, const char * _kindText)
  3068. {
  3069. StatisticMeasure newMeasure = queryMeasure(_measureText, SMeasureNone);
  3070. if (newMeasure != SMeasureNone)
  3071. setMeasure(newMeasure);
  3072. set(_creatorTypeText, _scopeTypeText, _kindText);
  3073. setCreator(_creator);
  3074. setScope(_scope);
  3075. }
  3076. void StatisticsFilter::setCreatorDepth(unsigned _minCreatorDepth, unsigned _maxCreatorDepth)
  3077. {
  3078. creatorFilter.setDepth(_minCreatorDepth, _maxCreatorDepth);
  3079. }
  3080. void StatisticsFilter::setCreator(const char * _creator)
  3081. {
  3082. creatorFilter.set(_creator);
  3083. }
  3084. void StatisticsFilter::setCreatorType(StatisticCreatorType _creatorType)
  3085. {
  3086. creatorType = _creatorType;
  3087. }
  3088. void StatisticsFilter::addFilter(const char * filter)
  3089. {
  3090. //Match a filter of the form category[value] (use square brackets to avoid bash grief)
  3091. const char * openBra = strchr(filter, '[');
  3092. if (!openBra)
  3093. return;
  3094. const char * closeBra = strchr(openBra, ']');
  3095. if (!closeBra)
  3096. return;
  3097. const char * start = openBra + 1;
  3098. StringBuffer value(closeBra - start, start);
  3099. if (hasPrefix(filter, "creator[", false))
  3100. setCreator(value);
  3101. else if (hasPrefix(filter, "creatortype[", false))
  3102. setCreatorType(queryCreatorType(value, SCTall));
  3103. else if (hasPrefix(filter, "depth[", false))
  3104. {
  3105. const char * comma = strchr(value, ',');
  3106. if (comma)
  3107. setScopeDepth(atoi(value), atoi(comma+1));
  3108. else
  3109. setScopeDepth(atoi(value));
  3110. }
  3111. else if (hasPrefix(filter, "kind[", false))
  3112. setKind(value);
  3113. else if (hasPrefix(filter, "measure[", false))
  3114. setMeasure(queryMeasure(value, SMeasureAll));
  3115. else if (hasPrefix(filter, "scope[", false))
  3116. setScope(value);
  3117. else if (hasPrefix(filter, "scopetype[", false))
  3118. setScopeType(queryScopeType(value, SSTall));
  3119. else if (hasPrefix(filter, "value[", false))
  3120. {
  3121. //value[exact|low..high] where low and high are optional
  3122. unsigned __int64 lowValue = 0;
  3123. unsigned __int64 highValue = MaxStatisticValue;
  3124. if (isdigit(*value))
  3125. lowValue = (unsigned __int64)atoi64(value);
  3126. const char * dotdot = strstr(value, "..");
  3127. if (dotdot)
  3128. {
  3129. unsigned __int64 maxValue = (unsigned __int64)atoi64(dotdot + 2);
  3130. if (maxValue != 0)
  3131. highValue = maxValue;
  3132. }
  3133. else
  3134. highValue = lowValue;
  3135. setValueRange(lowValue, highValue);
  3136. }
  3137. else
  3138. throw MakeStringException(1, "Unknown stats filter '%s' - expected creator,creatortype,depth,kind,measure,scope,scopetype", filter);
  3139. }
  3140. void StatisticsFilter::setFilter(const char * filter)
  3141. {
  3142. if (isEmptyString(filter))
  3143. return;
  3144. for (;;)
  3145. {
  3146. const char * closeBra = strchr(filter, ']');
  3147. if (!closeBra)
  3148. throw MakeStringException(1, "Missing close bracket ']' in '%s' ", filter);
  3149. const char * comma = strchr(closeBra, ',');
  3150. if (comma)
  3151. {
  3152. //Take a copy - simplicity rather than efficiency
  3153. StringBuffer temp(comma - filter, filter);
  3154. addFilter(temp);
  3155. filter = comma + 1;
  3156. }
  3157. else
  3158. {
  3159. addFilter(filter);
  3160. return;
  3161. }
  3162. }
  3163. }
  3164. void StatisticsFilter::setScopeDepth(unsigned _scopeDepth)
  3165. {
  3166. scopeFilter.setDepth(_scopeDepth);
  3167. }
  3168. void StatisticsFilter::setScopeDepth(unsigned _minScopeDepth, unsigned _maxScopeDepth)
  3169. {
  3170. scopeFilter.setDepth(_minScopeDepth, _maxScopeDepth);
  3171. }
  3172. void StatisticsFilter::setScope(const char * _scope)
  3173. {
  3174. scopeFilter.set(_scope);
  3175. }
  3176. void StatisticsFilter::setScopeType(StatisticScopeType _scopeType)
  3177. {
  3178. scopeType = _scopeType;
  3179. switch (scopeType)
  3180. {
  3181. case SSTglobal:
  3182. case SSTgraph:
  3183. scopeFilter.setDepth(1);
  3184. break;
  3185. case SSTsubgraph:
  3186. scopeFilter.setDepth(2);
  3187. break;
  3188. case SSTactivity:
  3189. scopeFilter.setDepth(3);
  3190. break;
  3191. }
  3192. }
  3193. void StatisticsFilter::setMeasure(StatisticMeasure _measure)
  3194. {
  3195. measure = _measure;
  3196. }
  3197. void StatisticsFilter::setValueRange(unsigned __int64 _minValue, unsigned __int64 _maxValue)
  3198. {
  3199. minValue = _minValue;
  3200. maxValue = _maxValue;
  3201. }
  3202. void StatisticsFilter::setKind(StatisticKind _kind)
  3203. {
  3204. kind = _kind;
  3205. if (measure == SMeasureAll)
  3206. measure = queryMeasure(kind);
  3207. }
  3208. void StatisticsFilter::setKind(const char * _kind)
  3209. {
  3210. if (!_kind || !*_kind || streq(_kind, "*"))
  3211. {
  3212. if (measure == SMeasureNone)
  3213. measure = SMeasureAll;
  3214. kind = StKindAll;
  3215. return;
  3216. }
  3217. //Convert a kind wildcard to a measure
  3218. for (unsigned i1=SMeasureAll+1; i1 < SMeasureMax; i1++)
  3219. {
  3220. const char * prefix = queryMeasurePrefix((StatisticMeasure)i1);
  3221. size_t len = strlen(prefix);
  3222. if (len && strnicmp(_kind, prefix, len) == 0)
  3223. {
  3224. setMeasure((StatisticMeasure)i1);
  3225. //Treat When* and When as filters on times.
  3226. if (streq(_kind + len, "*") || !_kind[len])
  3227. return;
  3228. }
  3229. }
  3230. //Other wildcards not currently supported
  3231. kind = queryStatisticKind(_kind, StKindAll);
  3232. }
  3233. //---------------------------------------------------
  3234. class CStatsCategory : public CInterface
  3235. {
  3236. public:
  3237. StringAttr longName;
  3238. StringAttr shortName;
  3239. CStatsCategory(const char *_longName, const char *_shortName)
  3240. : longName(_longName), shortName(_shortName)
  3241. {
  3242. }
  3243. bool match(const char *_longName, const char *_shortName)
  3244. {
  3245. bool lm = stricmp(_longName, longName)==0;
  3246. bool sm = stricmp(_shortName, shortName)==0;
  3247. if (lm || sm)
  3248. {
  3249. if (lm && sm)
  3250. return true;
  3251. throw MakeStringException(0, "A stats category %s (%s) is already registered", shortName.get(), longName.get());
  3252. }
  3253. return false;
  3254. }
  3255. };
  3256. static CIArrayOf<CStatsCategory> statsCategories;
  3257. static CriticalSection statsCategoriesCrit;
  3258. extern int registerStatsCategory(const char *longName, const char *shortName)
  3259. {
  3260. CriticalBlock b(statsCategoriesCrit);
  3261. ForEachItemIn(idx, statsCategories)
  3262. {
  3263. if (statsCategories.item(idx).match(longName, shortName))
  3264. return idx;
  3265. }
  3266. statsCategories.append(*new CStatsCategory(longName, shortName));
  3267. return statsCategories.ordinality()-1;
  3268. }
  3269. static void checkKind(StatisticKind kind)
  3270. {
  3271. if (kind < StMax)
  3272. {
  3273. const StatisticMeta & meta = statsMetaData[kind];
  3274. if (meta.kind != kind)
  3275. throw makeStringExceptionV(0, "Statistic %u in the wrong order", kind);
  3276. }
  3277. StatisticKind serialKind = querySerializedKind(kind);
  3278. StatisticKind rawKind = queryRawKind(kind);
  3279. if (kind != serialKind)
  3280. assertex(queryRawKind(serialKind) == kind);
  3281. if (kind != rawKind)
  3282. assertex(querySerializedKind(rawKind) == kind);
  3283. StatisticMeasure measure = queryMeasure(kind);
  3284. const char * shortName = queryStatisticName(kind);
  3285. StringBuffer longName;
  3286. queryLongStatisticName(longName, kind);
  3287. const char * tagName __attribute__ ((unused)) = queryTreeTag(kind);
  3288. const char * prefix = queryMeasurePrefix(measure);
  3289. //Check short names are all correctly prefixed.
  3290. assertex(strncmp(shortName, prefix, strlen(prefix)) == 0);
  3291. }
  3292. static void checkDistributedKind(StatisticKind kind)
  3293. {
  3294. checkKind(kind);
  3295. checkKind((StatisticKind)(kind|StMinX));
  3296. checkKind((StatisticKind)(kind|StMaxX));
  3297. checkKind((StatisticKind)(kind|StAvgX));
  3298. checkKind((StatisticKind)(kind|StSkew));
  3299. checkKind((StatisticKind)(kind|StSkewMin));
  3300. checkKind((StatisticKind)(kind|StSkewMax));
  3301. checkKind((StatisticKind)(kind|StNodeMin));
  3302. checkKind((StatisticKind)(kind|StNodeMax));
  3303. checkKind((StatisticKind)(kind|StDeltaX));
  3304. }
  3305. void verifyStatisticFunctions()
  3306. {
  3307. static_assert(_elements_in(measureNames) == SMeasureMax+1 && !measureNames[SMeasureMax], "measureNames needs updating");
  3308. static_assert(_elements_in(creatorTypeNames) == SCTmax+1 && !creatorTypeNames[SCTmax], "creatorTypeNames needs updating");
  3309. static_assert(_elements_in(scopeTypeNames) == SSTmax+1 && !scopeTypeNames[SSTmax], "scopeTypeNames needs updating");
  3310. //Check the various functions return values for all possible values.
  3311. for (unsigned i1=SMeasureAll; i1 < SMeasureMax; i1++)
  3312. {
  3313. const char * prefix __attribute__((unused)) = queryMeasurePrefix((StatisticMeasure)i1);
  3314. const char * name = queryMeasureName((StatisticMeasure)i1);
  3315. assertex(queryMeasure(name, SMeasureMax) == i1);
  3316. }
  3317. for (StatisticScopeType sst = SSTnone; sst < SSTmax; sst = (StatisticScopeType)(sst+1))
  3318. {
  3319. const char * name = queryScopeTypeName(sst);
  3320. assertex(queryScopeType(name, SSTmax) == sst);
  3321. }
  3322. for (StatisticCreatorType sct = SCTnone; sct < SCTmax; sct = (StatisticCreatorType)(sct+1))
  3323. {
  3324. const char * name = queryCreatorTypeName(sct);
  3325. assertex(queryCreatorType(name, SCTmax) == sct);
  3326. }
  3327. for (unsigned i2=StKindAll+1; i2 < StMax; i2++)
  3328. {
  3329. checkDistributedKind((StatisticKind)i2);
  3330. }
  3331. }
  3332. #ifdef _DEBUG
  3333. MODULE_INIT(INIT_PRIORITY_STANDARD)
  3334. {
  3335. verifyStatisticFunctions();
  3336. return true;
  3337. }
  3338. #endif