jdebug.cpp 108 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798
  1. /*##############################################################################
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. ############################################################################## */
  12. #include "platform.h"
  13. #include "jdebug.hpp"
  14. #include "jstring.hpp"
  15. #include "jhash.hpp"
  16. #include "jmisc.hpp"
  17. #include "jexcept.hpp"
  18. #include "jmutex.hpp"
  19. #include "jtime.hpp"
  20. #include <stdio.h>
  21. #include <time.h>
  22. #include <atomic>
  23. #if defined(_DEBUG) && defined(_WIN32) && !defined(USING_MPATROL)
  24. #undef new
  25. #define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
  26. #endif
  27. #ifdef _WIN32
  28. #define DPSAPI_VERSION 1
  29. #include <psapi.h>
  30. #endif
  31. #ifdef __linux__
  32. #include <sys/time.h>
  33. #include <sys/resource.h>
  34. #include <unistd.h>
  35. #include <sys/vfs.h>
  36. #include <sys/types.h>
  37. #include <sys/stat.h>
  38. #include <sys/klog.h>
  39. #include <dirent.h>
  40. #endif
  41. #ifdef __APPLE__
  42. #include <sys/param.h>
  43. #include <sys/mount.h>
  44. #include <sys/sysctl.h>
  45. #include <mach/task.h>
  46. #include <mach/mach_init.h>
  47. #include <mach/mach_host.h>
  48. #include <mach/vm_statistics.h>
  49. #endif
  50. #include "build-config.h"
  51. //===========================================================================
  52. #ifdef _DEBUG
  53. // #define _USE_MALLOC_HOOK // Only enable if you need it - slow!
  54. #else
  55. #undef _USE_MALLOC_HOOK // don't define for release - not threadsafe
  56. #endif
  57. #define _USE_RDTSC true
  58. #ifdef _USE_MALLOC_HOOK
  59. #define REPORT_LARGER_BLOCK_THAN (10*0x100000)
  60. static __int64 totalMem = 0;
  61. static __int64 hwmTotalMem = 0;
  62. #ifdef __linux__
  63. static unsigned memArea[32];
  64. #endif
  65. #endif
  66. // FIXME: Make sure this is still relevant, and if not, delete
  67. #ifndef _WIN32
  68. #ifndef __64BIT__
  69. #define USE_OLD_PU
  70. #endif
  71. #endif
  72. /* LINUX SYS: log KEY
  73. ========================
  74. PU (%) is the percentage CPU in use (unchanged from previously).
  75. MU (%) is what percentage of total (all processes) memory is in use
  76. (ram + swap) or in 32 bit is the percentage of 3GB (address space)
  77. used (whichever larger).
  78. MAL is total memory in use (i.e malloced and not freed ) by this
  79. process (= MMP+SBK)
  80. MMP is the sum of memory mapped (large) blocks in use by this process
  81. (which will be returned to OS when freed).
  82. SBK is the sbrk'ed memory i.e. smaller blocks allocated from the
  83. arena. (note this memory is unlikely to be returned to OS while the
  84. process is still running).
  85. TOT (K) is an indication of the memory footprint of the process.
  86. This is the 'arena' size (which is how much reserved by sbrk) plus the
  87. mmap memory size (MMP).
  88. RAM (K) is how much real memory is in use by all processes - it is
  89. the same as what would be reported by the 'free' command after the
  90. caches/buffers have been subtracted.
  91. SWP (K) is the swap size in use for all processes.
  92. Extended stats
  93. DSK: disk statistics for each disk (e.g. [sda] and [sdb])
  94. r/s read i/o operations per sec (over last period)
  95. kr/s K bytes read per sec
  96. w/s write i/o operations per sec
  97. kw/s K bytes written per sec
  98. busy indication how busy the disk was during period (%)
  99. NIC: network (i.e. eth0) statistics
  100. rxp/s packets received per sec
  101. rxk/s K bytes received per sec
  102. txp/s packets transmitted per sec
  103. txk/s K bytes transmitted per sec
  104. CPU:
  105. usr % at user level
  106. sys % in kernel
  107. iow % waiting for i/o
  108. */
  109. inline void skipSp(const char *&s)
  110. {
  111. while (isspace(*s))
  112. s++;
  113. }
  114. inline offset_t readHexNum(const char *&s)
  115. {
  116. offset_t ret = 0;
  117. for (;;) {
  118. switch (*s) {
  119. case '0':
  120. case '1':
  121. case '2':
  122. case '3':
  123. case '4':
  124. case '5':
  125. case '6':
  126. case '7':
  127. case '8':
  128. case '9': ret = ret*16+(*s-'0');
  129. break;
  130. case 'A':
  131. case 'B':
  132. case 'C':
  133. case 'D':
  134. case 'E':
  135. case 'F': ret = ret*16+(*s-'A'+10);
  136. break;
  137. case 'a':
  138. case 'b':
  139. case 'c':
  140. case 'd':
  141. case 'e':
  142. case 'f': ret = ret*16+(*s-'a'+10);
  143. break;
  144. default:
  145. return ret;
  146. }
  147. s++;
  148. }
  149. return 0;
  150. }
  151. inline offset_t readDecNum(const char *&s)
  152. {
  153. offset_t ret = 0;
  154. for (;;) {
  155. switch (*s) {
  156. case '0':
  157. case '1':
  158. case '2':
  159. case '3':
  160. case '4':
  161. case '5':
  162. case '6':
  163. case '7':
  164. case '8':
  165. case '9': ret = ret*10+(*s-'0');
  166. break;
  167. default:
  168. return ret;
  169. }
  170. s++;
  171. }
  172. return 0;
  173. }
  174. #if defined(_WIN32)
  175. static __int64 numCyclesNTicks;
  176. static __int64 ticksPerSec;
  177. static __int64 numScaleTicks;
  178. static bool useRDTSC = _USE_RDTSC;
  179. static double cycleToNanoScale;
  180. static void calibrate_timing()
  181. {
  182. #ifndef _AMD64_
  183. if (useRDTSC)
  184. {
  185. unsigned long r;
  186. __asm {
  187. mov eax, 1 ;
  188. cpuid ;
  189. mov r, edx
  190. }
  191. if ((r&0x10)==0)
  192. useRDTSC = false;
  193. }
  194. #endif
  195. if (useRDTSC) {
  196. unsigned startu = usTick();
  197. cycle_t start = getTSC();
  198. unsigned s1 = msTick();
  199. unsigned s2;
  200. while (s1==(s2=msTick()));
  201. unsigned s3;
  202. while (s2==(s3=msTick()));
  203. unsigned elapsedu = usTick()-startu;
  204. if (elapsedu) {
  205. double numPerUS=(double)(getTSC()-start)/(double)elapsedu; // this probably could be more accurate
  206. if (numPerUS>0)
  207. {
  208. cycleToNanoScale = 1000.0 / numPerUS;
  209. return;
  210. }
  211. }
  212. ERRLOG("calibrate_timing failed using RDTSC");
  213. useRDTSC = false;
  214. }
  215. static LARGE_INTEGER temp;
  216. QueryPerformanceFrequency(&temp);
  217. ticksPerSec = temp.QuadPart;
  218. numScaleTicks = ticksPerSec/100;
  219. LARGE_INTEGER t1;
  220. LARGE_INTEGER t2;
  221. QueryPerformanceCounter(&t1);
  222. t2.QuadPart=t1.QuadPart;
  223. while (t1.QuadPart==t2.QuadPart) QueryPerformanceCounter(&t1);
  224. cycle_t a1 = getTSC();
  225. t2.QuadPart = t1.QuadPart;
  226. while (t2.QuadPart-t1.QuadPart<numScaleTicks) QueryPerformanceCounter(&t2);
  227. cycle_t a2 = getTSC();
  228. numCyclesNTicks = (a2 - a1);
  229. cycleToNanoScale = ((double)numScaleTicks * 1000000000.0) / ((double)numCyclesNTicks * ticksPerSec);
  230. }
  231. __int64 cycle_to_nanosec(cycle_t cycles)
  232. {
  233. return (__int64)((double)cycles * cycleToNanoScale);
  234. }
  235. __int64 jlib_decl cycle_to_microsec(cycle_t cycles)
  236. {
  237. return (__int64)((double)cycles * cycleToNanoScale) / 1000;
  238. }
  239. __int64 jlib_decl cycle_to_millisec(cycle_t cycles)
  240. {
  241. return (__int64)((double)cycles * cycleToNanoScale) / 1000000;
  242. }
  243. cycle_t nanosec_to_cycle(__int64 ns)
  244. {
  245. return (__int64)((double)ns / cycleToNanoScale);
  246. }
  247. #if !(defined(INLINE_GET_CYCLES_NOW) && defined(HAS_GOOD_CYCLE_COUNTER))
  248. cycle_t jlib_decl get_cycles_now()
  249. {
  250. if (useRDTSC)
  251. return getTSC();
  252. LARGE_INTEGER temp;
  253. QueryPerformanceCounter(&temp);
  254. return temp.QuadPart;
  255. }
  256. #endif
  257. double getCycleToNanoScale()
  258. {
  259. return cycleToNanoScale;
  260. }
  261. #else
  262. #if defined(HAS_GOOD_CYCLE_COUNTER)
  263. static bool useRDTSC = _USE_RDTSC;
  264. #endif
  265. static double cycleToNanoScale;
  266. static double cycleToMicroScale;
  267. static double cycleToMilliScale;
  268. void calibrate_timing()
  269. {
  270. #if defined(_ARCH_X86_) || defined(_ARCH_X86_64_)
  271. if (useRDTSC) {
  272. unsigned long eax;
  273. unsigned long ebx;
  274. unsigned long ecx;
  275. unsigned long edx;
  276. #if defined(_ARCH_X86_64_)
  277. __asm__ ("cpuid\n\t" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "0" (1));
  278. #else
  279. // NB PIC code and ebx usage don't mix well
  280. asm volatile("pushl %%ebx \n\t"
  281. "cpuid \n\t"
  282. "movl %%ebx, %1 \n\t"
  283. "popl %%ebx \n\t"
  284. : "=a"(eax), "=r"(ebx), "=c"(ecx), "=d"(edx)
  285. : "a"(1)
  286. : "cc");
  287. #endif
  288. if ((edx&0x10)==0)
  289. useRDTSC = false;
  290. }
  291. #endif
  292. #if defined(HAS_GOOD_CYCLE_COUNTER)
  293. if (useRDTSC) {
  294. unsigned startu = usTick();
  295. cycle_t start = getTSC();
  296. unsigned s1 = msTick();
  297. unsigned s2;
  298. while (s1==(s2=msTick()));
  299. unsigned s3;
  300. while (s2==(s3=msTick()));
  301. unsigned elapsedu = usTick()-startu;
  302. if (elapsedu) {
  303. double numPerUS=(double)(getTSC()-start)/(double)elapsedu; // this probably could be more accurate
  304. if (numPerUS>0)
  305. {
  306. cycleToNanoScale = 1000.0 / numPerUS;
  307. cycleToMicroScale = 1.0 / numPerUS;
  308. cycleToMilliScale = 0.001 / numPerUS;
  309. return;
  310. }
  311. }
  312. ERRLOG("calibrate_timing failed using RDTSC");
  313. useRDTSC = false;
  314. }
  315. #endif
  316. cycleToNanoScale = 1.0;
  317. cycleToMicroScale = 1.0;
  318. cycleToMilliScale = 1.0;
  319. }
  320. #if !defined(INLINE_GET_CYCLES_NOW) || !defined(HAS_GOOD_CYCLE_COUNTER)
  321. #if defined(CLOCK_MONOTONIC) && !defined(__APPLE__)
  322. static bool use_gettimeofday=false;
  323. #endif
  324. cycle_t jlib_decl get_cycles_now()
  325. {
  326. #if defined(HAS_GOOD_CYCLE_COUNTER)
  327. if (useRDTSC)
  328. return getTSC();
  329. #endif
  330. #ifdef __APPLE__
  331. return mach_absolute_time();
  332. #elif defined(CLOCK_MONOTONIC)
  333. if (!use_gettimeofday) {
  334. timespec tm;
  335. if (clock_gettime(CLOCK_MONOTONIC, &tm)>=0)
  336. return ((cycle_t)tm.tv_sec)*1000000000L+(tm.tv_nsec);
  337. use_gettimeofday = true;
  338. fprintf(stderr,"clock_gettime CLOCK_MONOTONIC returns %d",errno); // don't use PROGLOG
  339. }
  340. #endif
  341. struct timeval tm;
  342. gettimeofday(&tm,NULL);
  343. return ((cycle_t)tm.tv_sec)*1000000000L+(cycle_t)tm.tv_usec*1000L;
  344. }
  345. #endif
  346. __int64 jlib_decl cycle_to_nanosec(cycle_t cycles)
  347. {
  348. #if defined(HAS_GOOD_CYCLE_COUNTER)
  349. if (useRDTSC)
  350. return (__int64)((double)cycles * cycleToNanoScale);
  351. #endif
  352. #ifdef __APPLE__
  353. return cycles * (uint64_t) timebase_info.numer / (uint64_t)timebase_info.denom;
  354. #endif
  355. return cycles;
  356. }
  357. __int64 jlib_decl cycle_to_microsec(cycle_t cycles)
  358. {
  359. #if defined(HAS_GOOD_CYCLE_COUNTER)
  360. if (useRDTSC)
  361. return (__int64)((double)cycles * cycleToMicroScale);
  362. #endif
  363. return cycles / 1000;
  364. }
  365. __int64 jlib_decl cycle_to_millisec(cycle_t cycles)
  366. {
  367. #if defined(HAS_GOOD_CYCLE_COUNTER)
  368. if (useRDTSC)
  369. return (__int64)((double)cycles * cycleToMilliScale);
  370. #endif
  371. return cycles / 1000000;
  372. }
  373. cycle_t nanosec_to_cycle(__int64 ns)
  374. {
  375. #if defined(HAS_GOOD_CYCLE_COUNTER)
  376. if (useRDTSC)
  377. return (__int64)((double)ns / cycleToNanoScale);
  378. #endif
  379. return ns;
  380. }
  381. double getCycleToNanoScale()
  382. {
  383. return cycleToNanoScale;
  384. }
  385. #endif
  386. void display_time(const char *title, cycle_t diff)
  387. {
  388. DBGLOG("Time taken for %s: %" I64F "d cycles (%" I64F "dM) = %" I64F "d msec", title, diff, diff/1000000, cycle_to_nanosec(diff)/1000000);
  389. }
  390. TimeSection::TimeSection(const char * _title) : title(_title)
  391. {
  392. start_time = get_cycles_now();
  393. }
  394. TimeSection::~TimeSection()
  395. {
  396. cycle_t end_time = get_cycles_now();
  397. if (title)
  398. display_time(title, end_time-start_time);
  399. }
  400. MTimeSection::MTimeSection(ITimeReporter *_master, const char * _scope) : scope(_scope), master(_master)
  401. {
  402. start_time = get_cycles_now();
  403. }
  404. MTimeSection::~MTimeSection()
  405. {
  406. cycle_t end_time = get_cycles_now();
  407. if (master)
  408. master->addTiming(scope, end_time-start_time);
  409. else
  410. display_time(title, end_time-start_time);
  411. }
  412. class TimeSectionInfo : public MappingBase
  413. {
  414. public:
  415. TimeSectionInfo(const char * _scope, const char *_description, __int64 _cycles) : scope(_scope), description(_description), totalcycles(_cycles), maxcycles(_cycles), count(1) {};
  416. TimeSectionInfo(const char * _scope, const char *_description, __int64 _cycles, __int64 _maxcycles, unsigned _count)
  417. : scope(_scope), description(_description), totalcycles(_cycles), maxcycles(_maxcycles), count(_count) {};
  418. TimeSectionInfo(MemoryBuffer &mb)
  419. {
  420. mb.read(scope).read(description).read(totalcycles).read(maxcycles).read(count);
  421. }
  422. void serialize(MemoryBuffer &mb)
  423. {
  424. mb.read(scope).read(description).append(totalcycles).append(maxcycles).append(count);
  425. }
  426. virtual const void * getKey() const { return scope.get(); }
  427. StringAttr scope;
  428. StringAttr description;
  429. __int64 totalcycles;
  430. __int64 maxcycles;
  431. unsigned count;
  432. };
  433. class DefaultTimeReporter : implements ITimeReporter, public CInterface
  434. {
  435. StringMapOf<TimeSectionInfo> *sections;
  436. CriticalSection c;
  437. TimeSectionInfo &findSection(unsigned idx)
  438. {
  439. CriticalBlock b(c);
  440. HashIterator iter(*sections);
  441. for(iter.first(); iter.isValid(); iter.next())
  442. {
  443. if (!idx--)
  444. return (TimeSectionInfo &) iter.query();
  445. }
  446. throw MakeStringException(2, "Invalid index to DefaultTimeReporter");
  447. }
  448. public:
  449. IMPLEMENT_IINTERFACE
  450. DefaultTimeReporter()
  451. {
  452. sections = new StringMapOf<TimeSectionInfo>(true);
  453. }
  454. DefaultTimeReporter(MemoryBuffer &mb)
  455. {
  456. sections = new StringMapOf<TimeSectionInfo>(true);
  457. unsigned ns;
  458. mb.read(ns);
  459. while (ns--)
  460. {
  461. TimeSectionInfo &newinfo = * new TimeSectionInfo(mb);
  462. sections->replaceOwn(newinfo);
  463. }
  464. }
  465. ~DefaultTimeReporter()
  466. {
  467. // printTimings(); // Must explicitly call printTimings - no automatic print (too late here!)
  468. delete sections;
  469. }
  470. virtual void report(ITimeReportInfo &cb)
  471. {
  472. CriticalBlock b(c);
  473. HashIterator iter(*sections);
  474. for(iter.first(); iter.isValid(); iter.next())
  475. {
  476. TimeSectionInfo &ts = (TimeSectionInfo &)iter.query();
  477. cb.report(ts.scope, ts.description, cycle_to_nanosec(ts.totalcycles), cycle_to_nanosec(ts.maxcycles), ts.count);
  478. }
  479. }
  480. virtual void addTiming(const char * scope, cycle_t cycles)
  481. {
  482. CriticalBlock b(c);
  483. TimeSectionInfo *info = sections->find(scope);
  484. if (info)
  485. {
  486. info->totalcycles += cycles;
  487. if (cycles > info->maxcycles) info->maxcycles = cycles;
  488. info->count++;
  489. }
  490. else
  491. {
  492. TimeSectionInfo &newinfo = * new TimeSectionInfo(scope, NULL, cycles);
  493. sections->replaceOwn(newinfo);
  494. }
  495. }
  496. virtual unsigned numSections()
  497. {
  498. CriticalBlock b(c);
  499. return sections->count();
  500. }
  501. virtual StatisticKind getTimerType(unsigned idx __attribute__((unused)))
  502. {
  503. return StTimeElapsed;
  504. }
  505. virtual StatisticScopeType getScopeType(unsigned idx __attribute__((unused)))
  506. {
  507. return SSTsection;
  508. }
  509. virtual __int64 getTime(unsigned idx)
  510. {
  511. CriticalBlock b(c);
  512. return cycle_to_nanosec(findSection(idx).totalcycles);
  513. }
  514. virtual __int64 getMaxTime(unsigned idx)
  515. {
  516. CriticalBlock b(c);
  517. return cycle_to_nanosec(findSection(idx).maxcycles);
  518. }
  519. virtual unsigned getCount(unsigned idx)
  520. {
  521. CriticalBlock b(c);
  522. return findSection(idx).count;
  523. }
  524. virtual StringBuffer &getScope(unsigned idx, StringBuffer &s)
  525. {
  526. CriticalBlock b(c);
  527. return s.append(findSection(idx).scope);
  528. }
  529. virtual void reset()
  530. {
  531. CriticalBlock b(c);
  532. delete sections;
  533. sections = new StringMapOf<TimeSectionInfo>(true);
  534. }
  535. virtual StringBuffer &getTimings(StringBuffer &str)
  536. {
  537. CriticalBlock b(c);
  538. if (numSections())
  539. {
  540. for (unsigned i = 0; i < numSections(); i++)
  541. {
  542. getScope(i, str.append("Timing: ")).append(" total=")
  543. .append(getTime(i)/1000000)
  544. .append("ms max=")
  545. .append(getMaxTime(i)/1000)
  546. .append("us count=")
  547. .append(getCount(i))
  548. .append(" ave=")
  549. .append((getTime(i)/1000)/getCount(i))
  550. .append("us\n");
  551. }
  552. }
  553. return str;
  554. }
  555. virtual void printTimings()
  556. {
  557. CriticalBlock b(c);
  558. if (numSections())
  559. {
  560. StringBuffer str;
  561. PrintLog(getTimings(str).str());
  562. }
  563. }
  564. virtual void mergeTiming(const char * scope, cycle_t totalcycles, cycle_t maxcycles, const unsigned count)
  565. {
  566. CriticalBlock b(c);
  567. TimeSectionInfo *info = sections->find(scope);
  568. if (!info)
  569. {
  570. info = new TimeSectionInfo(scope, NULL, totalcycles, maxcycles, count);
  571. sections->replaceOwn(*info);
  572. }
  573. else
  574. {
  575. info->totalcycles += totalcycles;
  576. if (maxcycles > info->maxcycles) info->maxcycles = maxcycles;
  577. info->count += count;
  578. }
  579. }
  580. virtual void mergeInto(ITimeReporter &other)
  581. {
  582. CriticalBlock b(c);
  583. HashIterator iter(*sections);
  584. for(iter.first(); iter.isValid(); iter.next())
  585. {
  586. TimeSectionInfo &ts = (TimeSectionInfo &) iter.query();
  587. other.mergeTiming(ts.scope, ts.totalcycles, ts.maxcycles, ts.count);
  588. }
  589. }
  590. virtual void merge(ITimeReporter &other)
  591. {
  592. CriticalBlock b(c);
  593. other.mergeInto(*this);
  594. }
  595. virtual void serialize(MemoryBuffer &mb)
  596. {
  597. CriticalBlock b(c);
  598. mb.append(numSections());
  599. HashIterator iter(*sections);
  600. for(iter.first(); iter.isValid(); iter.next())
  601. {
  602. TimeSectionInfo &ts = (TimeSectionInfo &) iter.query();
  603. ts.serialize(mb);
  604. }
  605. }
  606. };
  607. static ITimeReporter * activeTimer = NULL;
  608. ITimeReporter * queryActiveTimer()
  609. {
  610. return activeTimer;
  611. }
  612. ITimeReporter *createStdTimeReporter() { return new DefaultTimeReporter(); }
  613. ITimeReporter *createStdTimeReporter(MemoryBuffer &mb) { return new DefaultTimeReporter(mb); }
  614. cycle_t oneSecInCycles;
  615. MODULE_INIT(INIT_PRIORITY_JDEBUG1)
  616. {
  617. // perform v. early in process startup, ideally this would grab process exclusively for the 2 100ths of a sec it performs calc.
  618. calibrate_timing();
  619. oneSecInCycles = nanosec_to_cycle(1000000000);
  620. return 1;
  621. }
  622. MODULE_INIT(INIT_PRIORITY_JDEBUG2)
  623. {
  624. activeTimer = new DefaultTimeReporter();
  625. return true;
  626. }
  627. MODULE_EXIT()
  628. {
  629. ::Release(activeTimer);
  630. activeTimer = NULL;
  631. }
  632. //===========================================================================
  633. // Performance Monitor
  634. #ifdef _WIN32
  635. #define SystemBasicInformation 0
  636. #define SystemPerformanceInformation 2
  637. #define SystemTimeInformation 3
  638. #define SystemProcessList 5
  639. typedef enum _PROCESSINFOCLASS {
  640. ProcessBasicInformation,
  641. ProcessQuotaLimits,
  642. ProcessIoCounters,
  643. ProcessVmCounters,
  644. ProcessTimes,
  645. ProcessBasePriority,
  646. ProcessRaisePriority,
  647. ProcessDebugPort,
  648. ProcessExceptionPort,
  649. ProcessAccessToken,
  650. ProcessLdtInformation,
  651. ProcessLdtSize,
  652. ProcessDefaultHardErrorMode,
  653. ProcessIoPortHandlers, // Note: this is kernel mode only
  654. ProcessPooledUsageAndLimits,
  655. ProcessWorkingSetWatch,
  656. ProcessUserModeIOPL,
  657. ProcessEnableAlignmentFaultFixup,
  658. ProcessPriorityClass,
  659. ProcessWx86Information,
  660. ProcessHandleCount,
  661. ProcessAffinityMask,
  662. ProcessPriorityBoost,
  663. ProcessDeviceMap,
  664. ProcessSessionInformation,
  665. ProcessForegroundInformation,
  666. ProcessWow64Information,
  667. MaxProcessInfoClass
  668. } PROCESSINFOCLASS;
  669. typedef LONG NTSTATUS;
  670. #define Li2Double(x) ((double)((x).HighPart) * 4.294967296E9 + (double)((x).LowPart))
  671. typedef struct
  672. {
  673. DWORD dwUnknown1;
  674. ULONG uKeMaximumIncrement;
  675. ULONG uPageSize;
  676. ULONG uMmNumberOfPhysicalPages;
  677. ULONG uMmLowestPhysicalPage;
  678. ULONG uMmHighestPhysicalPage;
  679. ULONG uAllocationGranularity;
  680. PVOID pLowestUserAddress;
  681. PVOID pMmHighestUserAddress;
  682. ULONG uKeActiveProcessors;
  683. BYTE bKeNumberProcessors;
  684. BYTE bUnknown2;
  685. WORD wUnknown3;
  686. } SYSTEM_BASIC_INFORMATION;
  687. typedef struct
  688. {
  689. LARGE_INTEGER liIdleTime;
  690. DWORD dwSpare[76];
  691. } SYSTEM_PERFORMANCE_INFORMATION;
  692. typedef struct
  693. {
  694. LARGE_INTEGER liKeBootTime;
  695. LARGE_INTEGER liKeSystemTime;
  696. LARGE_INTEGER liExpTimeZoneBias;
  697. ULONG uCurrentTimeZoneId;
  698. DWORD dwReserved;
  699. } SYSTEM_TIME_INFORMATION;
  700. struct PROCESS_BASIC_INFORMATION {
  701. long ExitStatus;
  702. void * PebBaseAddress;
  703. unsigned long AffinityMask;
  704. long BasePriority;
  705. unsigned long UniqueProcessId;
  706. unsigned long InheritedFromUniqueProcessId;
  707. };
  708. // QUOTA_LIMITS
  709. struct __IO_COUNTERS { // defined in SDK
  710. ULONGLONG ReadOperationCount;
  711. ULONGLONG WriteOperationCount;
  712. ULONGLONG OtherOperationCount;
  713. ULONGLONG ReadTransferCount;
  714. ULONGLONG WriteTransferCount;
  715. ULONGLONG OtherTransferCount;
  716. };
  717. struct VM_COUNTERS {
  718. unsigned long PeakVirtualSize;
  719. unsigned long VirtualSize;
  720. unsigned long PageFaultCount;
  721. unsigned long PeakWorkingSetSize;
  722. unsigned long WorkingSetSize;
  723. unsigned long QuotaPeakPagedPoolUsage;
  724. unsigned long QuotaPagedPoolUsage;
  725. unsigned long QuotaPeakNonPagedPoolUsage;
  726. unsigned long QuotaNonPagedPoolUsage;
  727. unsigned long PagefileUsage;
  728. unsigned long PeakPagefileUsage;
  729. };
  730. struct POOLED_USAGE_AND_LIMITS {
  731. unsigned long PeakPagedPoolUsage;
  732. unsigned long PagedPoolUsage;
  733. unsigned long PagedPoolLimit;
  734. unsigned long PeakNonPagedPoolUsage;
  735. unsigned long NonPagedPoolUsage;
  736. unsigned long NonPagedPoolLimit;
  737. unsigned long PeakPagefileUsage;
  738. unsigned long PagefileUsage;
  739. unsigned long PagefileLimit;
  740. };
  741. struct KERNEL_USER_TIMES {
  742. __int64 CreateTime;
  743. __int64 ExitTime;
  744. __int64 KernelTime;
  745. __int64 UserTime;
  746. //__int64 EllapsedTime;
  747. };
  748. // ntdll!NtQuerySystemInformation (NT specific!)
  749. //
  750. // The function copies the system information of the
  751. // specified type into a buffer
  752. //
  753. // NTSYSAPI
  754. // NTSTATUS
  755. // NTAPI
  756. // NtQuerySystemInformation(
  757. // IN UINT SystemInformationClass, // information type
  758. // OUT PVOID SystemInformation, // pointer to buffer
  759. // IN ULONG SystemInformationLength, // buffer size in bytes
  760. // OUT PULONG ReturnLength OPTIONAL // pointer to a 32-bit
  761. // // variable that receives
  762. // // the number of bytes
  763. // // written to the buffer
  764. // );
  765. //
  766. //NTSYSCALLAPI
  767. //NTSTATUS
  768. //NTAPI
  769. //NtQueryInformationProcess(
  770. // IN HANDLE ProcessHandle,
  771. // IN PROCESSINFOCLASS ProcessInformationClass,
  772. // OUT PVOID ProcessInformation,
  773. // IN ULONG ProcessInformationLength,
  774. // OUT PULONG ReturnLength OPTIONAL
  775. // );
  776. typedef LONG (WINAPI *PROCNTQSI)(UINT,PVOID,ULONG,PULONG);
  777. typedef LONG (WINAPI *PROCNTQIP)(HANDLE,UINT,PVOID,ULONG,PULONG);
  778. typedef LONG (WINAPI *PROCNTGST)(LARGE_INTEGER*, LARGE_INTEGER*, LARGE_INTEGER*);
  779. memsize_t getMapInfo(const char *type)
  780. {
  781. return 0; // TODO/UNKNOWN
  782. }
  783. memsize_t getVMInfo(const char *type)
  784. {
  785. return 0; // TODO/UNKNOWN
  786. }
  787. void getCpuInfo(unsigned &numCPUs, unsigned &CPUSpeed)
  788. {
  789. // MORE: Might be a better way to get CPU speed (actual) than the one stored in Registry
  790. LONG lRet;
  791. HKEY hKey;
  792. DWORD keyValue;
  793. DWORD valueLen = sizeof(keyValue);
  794. if ((lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0L, KEY_READ , &hKey)) != ERROR_SUCCESS)
  795. {
  796. DBGLOG("RegOpenKeyEx(HKEY_LOCAL_MACHINE, ...) failed to open CentralProcessor\\0 - SysErrorCode=%d", lRet);
  797. }
  798. else if ((lRet = RegQueryValueEx(hKey, TEXT("~MHz"), NULL, NULL, (LPBYTE) &keyValue, &valueLen)) != ERROR_SUCCESS)
  799. {
  800. DBGLOG("RegQueryValueEx() failed to get CPU speed - errorCode=%d", lRet);
  801. RegCloseKey(hKey);
  802. }
  803. else
  804. {
  805. CPUSpeed = keyValue;
  806. RegCloseKey(hKey);
  807. }
  808. SYSTEM_INFO sysInfo;
  809. GetSystemInfo(&sysInfo);
  810. numCPUs = sysInfo.dwNumberOfProcessors;
  811. }
  812. static unsigned evalAffinityCpus()
  813. {
  814. unsigned numCpus = 0;
  815. DWORD ProcessAffinityMask, SystemAffinityMask;
  816. if (GetProcessAffinityMask(GetCurrentProcess(), (PDWORD_PTR)&ProcessAffinityMask, (PDWORD_PTR)&SystemAffinityMask))
  817. {
  818. unsigned i = 0;
  819. while (ProcessAffinityMask)
  820. {
  821. if (ProcessAffinityMask & 1)
  822. ++numCpus;
  823. ProcessAffinityMask >>=1;
  824. }
  825. }
  826. else // fall back to legacy num system cpus
  827. {
  828. Owned<IException> e = makeOsException(GetLastError(), "Failed to get affinity");
  829. EXCLOG(e, NULL);
  830. unsigned cpuSpeed;
  831. getCpuInfo(numCpus, cpuSpeed);
  832. return numCpus;
  833. }
  834. return numCpus;
  835. }
  836. #else // linux
  837. memsize_t getMapInfo(const char *type)
  838. {
  839. // NOTE: 'total' heap value includes Roxiemem allocation, if present
  840. enum mapList { HEAP, STACK, SBRK, ANON };
  841. enum mapList mapType;
  842. if ( streq(type, "heap") )
  843. mapType = HEAP;
  844. else if ( streq(type, "stack") )
  845. mapType = STACK;
  846. else if ( streq(type, "sbrk") )
  847. mapType = SBRK;
  848. else if ( streq(type, "anon") )
  849. mapType = ANON;
  850. else
  851. return 0;
  852. memsize_t ret = 0;
  853. VStringBuffer procMaps("/proc/%d/maps", GetCurrentProcessId());
  854. FILE *diskfp = fopen(procMaps.str(), "r");
  855. if (!diskfp)
  856. return false;
  857. char ln[256];
  858. /*
  859. * exmaple /proc/<pid>/maps format:
  860. * addr_start -addr_end perms offset dev inode pathname
  861. * 01c3a000-01c5b000 rw-p 00000000 00:00 0 [heap]
  862. * 7f3f25217000-7f3f25a40000 rw-p 00000000 00:00 0 [stack:2362]
  863. * 7f4020a40000-7f4020a59000 rw-p 00000000 00:00 0
  864. * 7f4020a59000-7f4020a5a000 ---p 00000000 00:00 0
  865. * 7f4029bd4000-7f4029bf6000 r-xp 00000000 08:01 17576135 /lib/x86_64-linux-gnu/ld-2.15.so
  866. */
  867. while (fgets(ln, sizeof(ln), diskfp))
  868. {
  869. bool skipline = true;
  870. if ( mapType == HEAP || mapType == ANON ) // 'general' heap includes anon mmapped + sbrk
  871. {
  872. // skip file maps (beginning with /) and all other regions (except [heap if selected)
  873. if ( (mapType == HEAP && strstr(ln, "[heap")) || (!strstr(ln, " /") && !strstr(ln, " [")) )
  874. {
  875. // include only (r)ead + (w)rite and (p)rivate (not shared), skipping e(x)ecutable
  876. // and ---p guard regions
  877. if ( strstr(ln, " rw-p") )
  878. skipline = false;
  879. }
  880. }
  881. else if ( mapType == STACK )
  882. {
  883. if ( strstr(ln, "[stack") )
  884. skipline = false;
  885. }
  886. else if ( mapType == SBRK )
  887. {
  888. if ( strstr(ln, "[heap") )
  889. skipline = false;
  890. }
  891. if ( !skipline )
  892. {
  893. unsigned __int64 addrLow, addrHigh;
  894. if (2 == sscanf(ln, "%16" I64F "x-%16" I64F "x", &addrLow, &addrHigh))
  895. ret += (memsize_t)(addrHigh-addrLow);
  896. }
  897. }
  898. fclose(diskfp);
  899. return ret;
  900. }
  901. static bool matchExtract(const char * prefix, const char * line, memsize_t & value)
  902. {
  903. size32_t len = strlen(prefix);
  904. if (strncmp(prefix, line, len)==0)
  905. {
  906. char * tail = NULL;
  907. value = strtol(line+len, &tail, 10);
  908. while (isspace(*tail))
  909. tail++;
  910. if (strncmp(tail, "kB", 2) == 0)
  911. value *= 0x400;
  912. else if (strncmp(tail, "mB", 2) == 0)
  913. value *= 0x100000;
  914. else if (strncmp(tail, "gB", 2) == 0)
  915. value *= 0x40000000;
  916. return true;
  917. }
  918. return false;
  919. }
  920. memsize_t getVMInfo(const char *type)
  921. {
  922. memsize_t ret = 0;
  923. VStringBuffer name("%s:", type);
  924. VStringBuffer procMaps("/proc/self/status");
  925. FILE *diskfp = fopen(procMaps.str(), "r");
  926. if (!diskfp)
  927. return 0;
  928. char ln[256];
  929. while (fgets(ln, sizeof(ln), diskfp))
  930. {
  931. if (matchExtract(name.str(), ln, ret))
  932. break;
  933. }
  934. fclose(diskfp);
  935. return ret;
  936. }
  937. void getCpuInfo(unsigned &numCPUs, unsigned &CPUSpeed)
  938. {
  939. int cpufd = open("/proc/cpuinfo",O_RDONLY);
  940. if (cpufd==-1)
  941. return;
  942. MemoryAttr ma;
  943. char *buf = (char *)ma.allocate(0x10000);
  944. size32_t l=0;
  945. for (;;) {
  946. size32_t rd = read(cpufd, buf+l, 0x10000-1-l);
  947. if ((int)rd<=0)
  948. break;
  949. l += rd;
  950. }
  951. buf[l] = 0;
  952. const char *bufptr = buf;
  953. char * tail;
  954. numCPUs = CPUSpeed = 0;
  955. // MORE: It is a shame that the info in this file (/proc/cpuinfo) are formatted (ie tabs .. etc)
  956. const char *cpuNumTag = "processor\t:";
  957. const char *cpuSpeedTag = "cpu MHz\t\t:";
  958. while (bufptr) {
  959. if (*bufptr =='\n')
  960. bufptr++;
  961. if (strncmp(cpuNumTag, bufptr, strlen(cpuNumTag))==0)
  962. numCPUs++;
  963. else if (strncmp(cpuSpeedTag, bufptr, strlen(cpuSpeedTag))==0)
  964. CPUSpeed = (unsigned)strtol(bufptr+strlen(cpuSpeedTag), &tail, 10);
  965. bufptr = strchr(bufptr, '\n');
  966. }
  967. close(cpufd);
  968. }
  969. static unsigned evalAffinityCpus()
  970. {
  971. #ifdef __APPLE__
  972. // MORE - could do better
  973. #else
  974. cpu_set_t cpuset;
  975. int err = pthread_getaffinity_np(GetCurrentThreadId(), sizeof(cpu_set_t), &cpuset);
  976. if (0 == err)
  977. {
  978. #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 6)
  979. return CPU_COUNT(&cpuset);
  980. #else
  981. unsigned numCpus = 0;
  982. unsigned setSize = CPU_SETSIZE;
  983. while (setSize--)
  984. {
  985. if (CPU_ISSET(setSize, &cpuset))
  986. ++numCpus;
  987. }
  988. return numCpus;
  989. #endif /* GLIBC */
  990. }
  991. #endif
  992. return 1;
  993. }
  994. // Note - values are returned in Kb
  995. static void getMemUsage(unsigned &inuse,unsigned &active,unsigned &total,unsigned &swaptotal,unsigned &swapinuse)
  996. {
  997. #ifdef __APPLE__
  998. active = 0;
  999. inuse = 0;
  1000. total = 0;
  1001. swaptotal = 0;
  1002. swapinuse = 0;
  1003. vm_size_t pageSize;
  1004. if (KERN_SUCCESS != host_page_size(mach_host_self(), &pageSize))
  1005. return;
  1006. mach_msg_type_number_t count = HOST_VM_INFO64_COUNT;
  1007. vm_statistics64_data_t vmstat;
  1008. if (KERN_SUCCESS != host_statistics64(mach_host_self(), HOST_VM_INFO64, (host_info64_t)&vmstat, &count))
  1009. return;
  1010. uint64_t totalBytes = (vmstat.wire_count + vmstat.active_count + vmstat.inactive_count + vmstat.free_count + vmstat.compressor_page_count) * pageSize;
  1011. uint64_t inuseBytes = (vmstat.wire_count + vmstat.active_count + vmstat.inactive_count + vmstat.compressor_page_count) * pageSize;
  1012. uint64_t activeBytes = (vmstat.wire_count + vmstat.active_count) * pageSize;
  1013. active = activeBytes / 1024;
  1014. inuse = inuseBytes / 1024;
  1015. total = totalBytes / 1024;
  1016. // swaptotal and swapinuse TBD
  1017. #else
  1018. unsigned free=0;
  1019. unsigned swapfree=0;
  1020. active = 0;
  1021. inuse = 0;
  1022. static int memfd = -1;
  1023. if (memfd==-1)
  1024. memfd = open("/proc/meminfo",O_RDONLY);
  1025. if (memfd==-1)
  1026. return;
  1027. char buf[2048];
  1028. size32_t l = pread(memfd, buf, sizeof(buf)-1, 0L);
  1029. if ((int)l<=0)
  1030. return;
  1031. buf[l] = 0;
  1032. const char *bufptr = buf;
  1033. char * tail;
  1034. unsigned i = 7; // supposed to match max number of items extract below
  1035. total = swaptotal = free = active = swapfree = 0;
  1036. unsigned swapcached = 0;
  1037. unsigned cached = 0;
  1038. while (bufptr&&i) {
  1039. if (*bufptr =='\n')
  1040. bufptr++;
  1041. i--;
  1042. if (strncmp("MemTotal:", bufptr, 9)==0)
  1043. total = (unsigned)strtol(bufptr+9, &tail, 10);
  1044. else if (strncmp("SwapTotal:", bufptr, 10)==0)
  1045. swaptotal = (unsigned)strtol(bufptr+10, &tail, 10);
  1046. else if (strncmp("MemFree:", bufptr, 8)==0)
  1047. free = (unsigned)strtol(bufptr+8, &tail, 10);
  1048. else if (strncmp("Active:", bufptr, 7)==0)
  1049. active = (unsigned)strtol(bufptr+7, &tail, 10);
  1050. else if (strncmp("SwapFree:", bufptr, 9)==0)
  1051. swapfree = (unsigned)strtol(bufptr+9, &tail, 10);
  1052. else if (strncmp("Cached:", bufptr, 7)==0)
  1053. cached = (unsigned)strtol(bufptr+7, &tail, 10);
  1054. else if (strncmp("SwapCached:", bufptr, 11)==0)
  1055. swapcached = (unsigned)strtol(bufptr+11, &tail, 10);
  1056. else
  1057. i++;
  1058. bufptr = strchr(bufptr, '\n');
  1059. }
  1060. inuse = total-free-cached;
  1061. swapinuse = swaptotal-swapfree-swapcached;
  1062. #endif
  1063. }
  1064. class CInt64fix
  1065. {
  1066. __int64 val;
  1067. public:
  1068. CInt64fix()
  1069. {
  1070. val = 0;
  1071. }
  1072. void set(int v)
  1073. {
  1074. __int64 ret = (unsigned)v;
  1075. while (val-ret>0x80000000LL)
  1076. ret += 0x100000000LL;
  1077. val = ret;
  1078. }
  1079. __int64 get()
  1080. {
  1081. return val;
  1082. }
  1083. };
  1084. void getMemStats(StringBuffer &out, unsigned &memused, unsigned &memtot)
  1085. {
  1086. #ifdef __linux__
  1087. __int64 total = getMapInfo("heap");
  1088. __int64 sbrkmem = getMapInfo("sbrk");
  1089. __int64 mmapmem = total - sbrkmem;
  1090. __int64 virttot = getVMInfo("VmData");
  1091. unsigned mu;
  1092. unsigned ma;
  1093. unsigned mt;
  1094. unsigned st;
  1095. unsigned su;
  1096. getMemUsage(mu,ma,mt,st,su);
  1097. unsigned muval = (unsigned)(((__int64)mu+(__int64)su)*100/((__int64)mt+(__int64)st));
  1098. if (sizeof(memsize_t)==4) {
  1099. unsigned muval2 = (virttot*100)/(3*(__int64)0x40000000);
  1100. if (muval2>muval)
  1101. muval = muval2;
  1102. }
  1103. if (muval>100)
  1104. muval = 100; // !
  1105. out.appendf("MU=%3u%% MAL=%" I64F "d MMP=%" I64F "d SBK=%" I64F "d TOT=%uK RAM=%uK SWP=%uK",
  1106. muval, total, mmapmem, sbrkmem, (unsigned)(virttot/1024), mu, su);
  1107. #ifdef _USE_MALLOC_HOOK
  1108. if (totalMem)
  1109. out.appendf(" TM=%" I64F "d",totalMem);
  1110. #endif
  1111. memused = mu+su;
  1112. memtot = mt+st;
  1113. #elif defined (__APPLE__)
  1114. __uint64 bytes;
  1115. size_t len = sizeof(bytes);
  1116. sysctlbyname("hw.memsize", &bytes, &len, NULL, 0);
  1117. // See http://miknight.blogspot.com/2005/11/resident-set-size-in-mac-os-x.html
  1118. struct task_basic_info t_info;
  1119. mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
  1120. task_info(current_task(), TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count);
  1121. out.appendf("RES=%" I64F "uMiB VIRT=%" I64F "uMiB TOT=%" I64F "uMiB",
  1122. (__uint64) t_info.resident_size/(1024*1024),
  1123. (__uint64) t_info.virtual_size/(1024*1024),
  1124. bytes/(1024*1024));
  1125. memused = t_info.resident_size;
  1126. memtot = t_info.virtual_size;
  1127. #elif defined (__FreeBSD___)
  1128. UNIMPLEMENTED;
  1129. #endif
  1130. }
  1131. void getDiskUsage(char const * path, unsigned __int64 & total, unsigned __int64 & inUse)
  1132. {
  1133. #if defined(__linux__) || defined(__APPLE__)
  1134. struct statfs stfs;
  1135. if(statfs(path, &stfs) < 0)
  1136. {
  1137. //PrintLog("statfs error for filesystem '%s'", path);
  1138. total = inUse = 0;
  1139. }
  1140. else
  1141. {
  1142. struct stat st;
  1143. if(stat(path, &st) < 0)
  1144. {
  1145. //PrintLog("stat error for filesystem '%s'", path);
  1146. total = inUse = 0;
  1147. }
  1148. else
  1149. {
  1150. total = (unsigned __int64)stfs.f_blocks * st.st_blksize;
  1151. inUse = total - (unsigned __int64)stfs.f_bfree * st.st_blksize;
  1152. }
  1153. }
  1154. #else
  1155. total = inUse = 0;
  1156. #endif
  1157. }
  1158. #endif
  1159. static std::atomic<unsigned> cachedNumCpus;
  1160. unsigned getAffinityCpus()
  1161. {
  1162. if (cachedNumCpus.load(std::memory_order_acquire) == 0)
  1163. cachedNumCpus.store(evalAffinityCpus(), std::memory_order_release);
  1164. return cachedNumCpus.load(std::memory_order_acquire);
  1165. }
  1166. void clearAffinityCache()
  1167. {
  1168. cachedNumCpus.store(0, std::memory_order_release);
  1169. }
  1170. void getPeakMemUsage(memsize_t &peakVm,memsize_t &peakResident)
  1171. {
  1172. peakVm = 0;
  1173. peakResident = 0;
  1174. #ifdef _WIN32
  1175. PROCESS_MEMORY_COUNTERS pmc;
  1176. if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)))
  1177. {
  1178. peakVm = pmc.PeakWorkingSetSize;
  1179. peakResident = pmc.PeakWorkingSetSize;
  1180. }
  1181. #else
  1182. static int memfd = -1;
  1183. if (memfd==-1)
  1184. memfd = open("/proc/self/status",O_RDONLY);
  1185. if (memfd==-1)
  1186. return;
  1187. char buf[2048];
  1188. size32_t l = pread(memfd, buf, sizeof(buf)-1, 0L);
  1189. if ((int)l<=0)
  1190. return;
  1191. buf[l] = 0;
  1192. const char *bufptr = buf;
  1193. while (bufptr) {
  1194. if (*bufptr =='\n')
  1195. bufptr++;
  1196. if (!matchExtract("VmPeak:", bufptr, peakVm) &&
  1197. !matchExtract("VmHWM:", bufptr, peakResident))
  1198. {
  1199. //ignore this line
  1200. }
  1201. bufptr = strchr(bufptr, '\n');
  1202. }
  1203. #endif
  1204. }
  1205. #define RXMAX 1000000 // can be 10x bigger but this produces reasonable amounts
  1206. unsigned packetsLasttime;
  1207. __int64 packetsLastrx = -1;
  1208. __int64 packetsLasttx;
  1209. bool getPacketStats(unsigned & tx, unsigned & rx)
  1210. {
  1211. unsigned thistime = msTick();
  1212. JSocketStatistics jstats;
  1213. getSocketStatistics(jstats);
  1214. bool ret = true;
  1215. if(packetsLastrx!=-1)
  1216. {
  1217. tx = (unsigned) ((jstats.writesize-packetsLasttx)*100000/(((__int64)(packetsLasttime-thistime))*RXMAX));
  1218. rx = (unsigned) ((jstats.readsize-packetsLastrx)*100000/(((__int64)(packetsLasttime-thistime))*RXMAX));
  1219. }
  1220. else
  1221. ret = false;
  1222. packetsLastrx = jstats.readsize;
  1223. packetsLasttx = jstats.writesize;
  1224. packetsLasttime = thistime;
  1225. return ret;
  1226. }
  1227. #ifndef _WIN32
  1228. struct UserStatusInfo
  1229. {
  1230. public:
  1231. UserStatusInfo(pid_t _pid)
  1232. {
  1233. pid = _pid;
  1234. }
  1235. bool update()
  1236. {
  1237. StringBuffer fn;
  1238. fn.appendf("/proc/%d/stat", pid);
  1239. char buf[800]; /* about 40 fields, 64-bit decimal is about 20 chars */
  1240. int fd = open(fn.str(), O_RDONLY, 0);
  1241. if (fd==-1)
  1242. return false;
  1243. int rd = read(fd, buf, sizeof(buf)-1);
  1244. close(fd);
  1245. if (rd<80)
  1246. return false;
  1247. buf[rd] = 0;
  1248. char *s = strchr(buf,'(');
  1249. if (!s)
  1250. return false;
  1251. s++;
  1252. unsigned i = 0;
  1253. while (*s&&(*s!=')')&&(i<15))
  1254. cmd[i++] = *(s++);
  1255. if (*s != ')')
  1256. return false;
  1257. cmd[i] = 0;
  1258. s+=3; // Skip ") X" where X is the state
  1259. //The PID of the parent process
  1260. const char *num;
  1261. s = skipnumfld(s,num);
  1262. //int ppid = atoi(num);
  1263. // skip pgrp, session, tty_num, tpgid, flags, min_flt, cmin_flt, maj_flt, cmaj_flt
  1264. for (i=0;i<9;i++)
  1265. s = skipnumfld(s,num);
  1266. //utime - user mode time in clock ticks.
  1267. s = skipnumfld(s,num);
  1268. //printf("**N'%s'\n",num);
  1269. time.user = (unsigned)atoi64_l(num,strlen(num));
  1270. //stime - amount of time scheduled in kernel mode in clock ticks
  1271. s = skipnumfld(s,num);
  1272. //printf("**N'%s'\n",num);
  1273. time.system = (unsigned)atoi64_l(num,strlen(num));
  1274. return true;
  1275. }
  1276. public:
  1277. pid_t pid;
  1278. char cmd[16];
  1279. UserSystemTime_t time;
  1280. private:
  1281. char *skipnumfld(char *s, const char *&num)
  1282. {
  1283. while (*s && isspace(*s))
  1284. s++;
  1285. num = s;
  1286. if ((*s=='-')||(*s=='+'))
  1287. s++;
  1288. while (*s && isdigit(*s))
  1289. s++;
  1290. if (*s==' ')
  1291. *(s++) = 0; // terminate num
  1292. while (*s && isspace(*s))
  1293. s++;
  1294. return s;
  1295. }
  1296. };
  1297. struct CProcInfo: extends CInterface
  1298. {
  1299. UserStatusInfo info;
  1300. UserSystemTime_t delta;
  1301. bool active;
  1302. bool first;
  1303. CProcInfo(int _pid) : info(_pid)
  1304. {
  1305. active = false;
  1306. first = true;
  1307. }
  1308. inline pid_t pid() const { return info.pid; }
  1309. bool load()
  1310. {
  1311. UserSystemTime_t prev = info.time;
  1312. if (!info.update())
  1313. return false;
  1314. active = true;
  1315. if (first)
  1316. first = false;
  1317. else {
  1318. delta.system = info.time.system-prev.system;
  1319. delta.user = info.time.user-prev.user;
  1320. }
  1321. return true;
  1322. }
  1323. };
  1324. class CProcessMonitor
  1325. {
  1326. CIArrayOf<CProcInfo> processes;
  1327. unsigned tot_time;
  1328. bool busy;
  1329. CriticalSection sect;
  1330. static int compare(CInterface * const *i1, CInterface * const *i2)
  1331. {
  1332. CProcInfo *pi1 = QUERYINTERFACE(*i1,CProcInfo);
  1333. CProcInfo *pi2 = QUERYINTERFACE(*i2,CProcInfo);
  1334. return pi2->delta.system+pi2->delta.user-pi1->delta.system-pi1->delta.user;
  1335. }
  1336. public:
  1337. CProcessMonitor()
  1338. {
  1339. busy = false;
  1340. }
  1341. void scan()
  1342. {
  1343. #ifdef __linux__
  1344. ForEachItemIn(i1,processes)
  1345. processes.item(i1).active = false;
  1346. DIR *dir = opendir("/proc");
  1347. for (;;) {
  1348. CriticalBlock b(sect);
  1349. struct dirent *ent = readdir(dir);
  1350. if (!ent)
  1351. break;
  1352. if ((ent->d_name[0]>='0')&&(ent->d_name[0]<='9')) {
  1353. int pid = atoi(ent->d_name);
  1354. if (pid) {
  1355. CProcInfo *pi = NULL;
  1356. ForEachItemIn(i2,processes) {
  1357. if (processes.item(i2).pid() == pid) {
  1358. pi = &processes.item(i2);
  1359. break;
  1360. }
  1361. }
  1362. if (!pi) {
  1363. pi = new CProcInfo(pid);
  1364. processes.append(*pi);
  1365. }
  1366. pi->load();
  1367. }
  1368. }
  1369. }
  1370. closedir(dir);
  1371. tot_time = 0;
  1372. ForEachItemInRev(i3,processes) {
  1373. CProcInfo &pi = processes.item(i3);
  1374. if (pi.active)
  1375. tot_time += pi.delta.system+pi.delta.user;
  1376. else
  1377. processes.remove(i3);
  1378. }
  1379. #endif
  1380. #if defined (__FreeBSD__) || defined (__APPLE__)
  1381. UNIMPLEMENTED;
  1382. #endif
  1383. }
  1384. void print(unsigned n,StringBuffer &str)
  1385. {
  1386. if (!tot_time)
  1387. return;
  1388. assertex(n);
  1389. processes.sort(compare);
  1390. StringBuffer name;
  1391. ForEachItemIn(i1,processes) {
  1392. CProcInfo &pi = processes.item(i1);
  1393. if ((pi.delta.system==0)&&(pi.delta.user==0))
  1394. break;
  1395. getThreadName(pi.pid(),0,name.clear());
  1396. str.appendf("\n TT: PI=%d PN=%s PC=%d ST=%d UT=%d%s%s",
  1397. pi.pid(),pi.info.cmd,(pi.delta.system+pi.delta.user)*100/tot_time,pi.delta.system,pi.delta.user,name.length()?" TN=":"",name.str());
  1398. if (--n==0)
  1399. break;
  1400. }
  1401. }
  1402. void printBusy(unsigned pc,StringBuffer &str)
  1403. {
  1404. if (pc>90) {
  1405. scan();
  1406. if (busy)
  1407. print(3,str); // print top 3
  1408. else
  1409. busy = true;
  1410. }
  1411. else {
  1412. busy = false;
  1413. processes.kill();
  1414. }
  1415. }
  1416. };
  1417. #ifndef HZ
  1418. #define HZ 100
  1419. #endif
  1420. #define IDE0_MAJOR 3
  1421. #define SCSI_DISK0_MAJOR 8
  1422. #define SCSI_DISK1_MAJOR 65
  1423. #define SCSI_DISK7_MAJOR 71
  1424. #define SCSI_DISK10_MAJOR 128
  1425. #define SCSI_DISK17_MAJOR 135
  1426. #define IDE1_MAJOR 22
  1427. #define IDE2_MAJOR 33
  1428. #define IDE3_MAJOR 34
  1429. #define IDE4_MAJOR 56
  1430. #define IDE5_MAJOR 57
  1431. #define IDE6_MAJOR 88
  1432. #define IDE7_MAJOR 89
  1433. #define IDE8_MAJOR 90
  1434. #define IDE9_MAJOR 91
  1435. #define COMPAQ_SMART2_MAJOR 72
  1436. #define IDE_DISK_MAJOR(M) ((M) == IDE0_MAJOR || (M) == IDE1_MAJOR || \
  1437. (M) == IDE2_MAJOR || (M) == IDE3_MAJOR || \
  1438. (M) == IDE4_MAJOR || (M) == IDE5_MAJOR || \
  1439. (M) == IDE6_MAJOR || (M) == IDE7_MAJOR || \
  1440. (M) == IDE8_MAJOR || (M) == IDE9_MAJOR)
  1441. #define SCSI_DISK_MAJOR(M) ((M) == SCSI_DISK0_MAJOR || \
  1442. ((M) >= SCSI_DISK1_MAJOR && (M) <= SCSI_DISK7_MAJOR) || \
  1443. ((M) >= SCSI_DISK10_MAJOR && (M) <= SCSI_DISK17_MAJOR))
  1444. #define OTHER_DISK_MAJOR(M) ((M) == COMPAQ_SMART2_MAJOR) // by investigation!
  1445. class CExtendedStats // Disk network and cpu stats
  1446. {
  1447. struct blkio_info
  1448. {
  1449. unsigned rd_ios; // Read I/O operations
  1450. unsigned rd_merges; // Reads merged
  1451. __uint64 rd_sectors; // Sectors read
  1452. unsigned rd_ticks; // Time in queue + service for read
  1453. unsigned wr_ios; // Write I/O operations
  1454. unsigned wr_merges; // Writes merged
  1455. __uint64 wr_sectors; // Sectors written
  1456. unsigned wr_ticks; // Time in queue + service for write
  1457. unsigned ticks; // Time of requests in queue
  1458. unsigned aveq; // Average queue length
  1459. };
  1460. struct cpu_info
  1461. {
  1462. __uint64 user;
  1463. __uint64 system;
  1464. __uint64 idle;
  1465. __uint64 iowait;
  1466. };
  1467. struct net_info
  1468. {
  1469. __uint64 rxbytes;
  1470. __uint64 rxpackets;
  1471. __uint64 rxerrors;
  1472. __uint64 rxdrops;
  1473. __uint64 txbytes;
  1474. __uint64 txpackets;
  1475. __uint64 txerrors;
  1476. __uint64 txdrops;
  1477. };
  1478. struct part_info
  1479. {
  1480. unsigned int major;
  1481. unsigned int minor;
  1482. char name[32];
  1483. };
  1484. part_info *partition;
  1485. unsigned nparts;
  1486. blkio_info *newblkio;
  1487. blkio_info *oldblkio;
  1488. cpu_info newcpu;
  1489. unsigned numcpu;
  1490. cpu_info oldcpu;
  1491. cpu_info cpu;
  1492. net_info oldnet;
  1493. net_info newnet;
  1494. unsigned ncpu;
  1495. bool first;
  1496. char *kbuf;
  1497. size32_t kbufmax;
  1498. int kbadcnt;
  1499. unsigned short kbufcrc;
  1500. __uint64 totalcpu;
  1501. unsigned ndisks;
  1502. int isdisk(unsigned int major, unsigned int minor)
  1503. {
  1504. if (IDE_DISK_MAJOR(major))
  1505. return ((minor&0x3F)==0);
  1506. if (SCSI_DISK_MAJOR(major))
  1507. return ((minor&0x0F)==0);
  1508. if (OTHER_DISK_MAJOR(major))
  1509. return ((minor&0x0F)==0);
  1510. return 0;
  1511. }
  1512. bool getNextCPU()
  1513. {
  1514. oldcpu = newcpu;
  1515. if (!ncpu) {
  1516. unsigned speed;
  1517. getCpuInfo(ncpu, speed);
  1518. if (!ncpu)
  1519. ncpu = 1;
  1520. }
  1521. FILE* cpufp = fopen("/proc/stat", "r");
  1522. if (!cpufp) {
  1523. memset(&cpu,0,sizeof(cpu));
  1524. totalcpu = 0;
  1525. return false;
  1526. }
  1527. char ln[256];
  1528. while (fgets(ln, sizeof(ln), cpufp)) {
  1529. if (strncmp(ln, "cpu ", 4)==0) {
  1530. int items;
  1531. __uint64 nice, irq, softirq;
  1532. items = sscanf(ln,
  1533. "cpu %llu %llu %llu %llu %llu %llu %llu",
  1534. &newcpu.user, &nice,
  1535. &newcpu.system,
  1536. &newcpu.idle,
  1537. &newcpu.iowait,
  1538. &irq, &softirq);
  1539. newcpu.user += nice;
  1540. if (items == 4)
  1541. newcpu.iowait = 0;
  1542. if (items == 7)
  1543. newcpu.system += irq + softirq;
  1544. break;
  1545. }
  1546. }
  1547. fclose(cpufp);
  1548. cpu.user = newcpu.user - oldcpu.user;
  1549. cpu.system = newcpu.system - oldcpu.system;
  1550. cpu.idle = newcpu.idle - oldcpu.idle;
  1551. cpu.iowait = newcpu.iowait - oldcpu.iowait;
  1552. totalcpu = (cpu.user + cpu.system + cpu.idle + cpu.iowait);
  1553. return true;
  1554. }
  1555. bool getDiskInfo()
  1556. {
  1557. char ln[256];
  1558. part_info pi;
  1559. FILE* diskfp = fopen("/proc/diskstats", "r");
  1560. if (!diskfp)
  1561. return false;
  1562. if (!newblkio) {
  1563. nparts = 0;
  1564. while (fgets(ln, sizeof(ln), diskfp)) {
  1565. unsigned reads = 0;
  1566. if (sscanf(ln, "%4d %4d %31s %u", &pi.major, &pi.minor, pi.name, &reads) == 4) {
  1567. unsigned p = 0;
  1568. while ((p<nparts) && (partition[p].major != pi.major || partition[p].minor != pi.minor))
  1569. p++;
  1570. if ((p==nparts) && reads && isdisk(pi.major,pi.minor)) {
  1571. nparts++;
  1572. partition = (part_info *)realloc(partition,nparts*sizeof(part_info));
  1573. partition[p] = pi;
  1574. }
  1575. }
  1576. }
  1577. free(newblkio);
  1578. free(oldblkio);
  1579. newblkio = (blkio_info *)calloc(sizeof(blkio_info),nparts);
  1580. oldblkio = (blkio_info* )calloc(sizeof(blkio_info),nparts);
  1581. }
  1582. rewind(diskfp);
  1583. // could skip lines we know aren't significant here
  1584. while (fgets(ln, sizeof(ln), diskfp)) {
  1585. blkio_info blkio;
  1586. unsigned items = sscanf(ln, "%4d %4d %*s %u %u %llu %u %u %u %llu %u %*u %u %u",
  1587. &pi.major, &pi.minor,
  1588. &blkio.rd_ios, &blkio.rd_merges,
  1589. &blkio.rd_sectors, &blkio.rd_ticks,
  1590. &blkio.wr_ios, &blkio.wr_merges,
  1591. &blkio.wr_sectors, &blkio.wr_ticks,
  1592. &blkio.ticks, &blkio.aveq);
  1593. if (items == 6) {
  1594. // hopefully not this branch!
  1595. blkio.rd_sectors = blkio.rd_merges;
  1596. blkio.wr_sectors = blkio.rd_ticks;
  1597. blkio.rd_ios = 0;
  1598. blkio.rd_merges = 0;
  1599. blkio.rd_ticks = 0;
  1600. blkio.wr_ios = 0;
  1601. blkio.wr_merges = 0;
  1602. blkio.wr_ticks = 0;
  1603. blkio.ticks = 0;
  1604. blkio.aveq = 0;
  1605. items = 12;
  1606. }
  1607. if (items == 12) {
  1608. for (unsigned p = 0; p < nparts; p++) {
  1609. if (partition[p].major == pi.major && partition[p].minor == pi.minor) {
  1610. newblkio[p] = blkio;
  1611. break;
  1612. }
  1613. }
  1614. }
  1615. }
  1616. fclose(diskfp);
  1617. return true;
  1618. }
  1619. bool getNetInfo()
  1620. {
  1621. FILE *netfp = fopen("/proc/net/dev", "r");
  1622. if (!netfp)
  1623. return false;
  1624. char ln[512];
  1625. // Read two lines
  1626. if (!fgets(ln, sizeof(ln), netfp) || !fgets(ln, sizeof(ln), netfp)) {
  1627. fclose(netfp);
  1628. return false;
  1629. }
  1630. unsigned txskip = 2;
  1631. bool hasbyt = false;
  1632. if (strstr(ln,"compressed")) {
  1633. txskip = 4;
  1634. hasbyt = true;
  1635. }
  1636. else if (strstr(ln,"bytes"))
  1637. hasbyt = true;
  1638. while (fgets(ln, sizeof(ln), netfp)) {
  1639. const char *s = ln;
  1640. skipSp(s);
  1641. if (strncmp(s, "eth0:", 5)==0) { // may want eth1 at some point!
  1642. s+=5;
  1643. if (hasbyt) {
  1644. newnet.rxbytes = readDecNum(s);
  1645. skipSp(s);
  1646. }
  1647. else
  1648. newnet.rxbytes = 0;
  1649. newnet.rxpackets = readDecNum(s);
  1650. skipSp(s);
  1651. newnet.rxerrors = readDecNum(s);
  1652. skipSp(s);
  1653. newnet.rxdrops = readDecNum(s);
  1654. skipSp(s);
  1655. while (txskip--) {
  1656. readDecNum(s);
  1657. skipSp(s);
  1658. }
  1659. if (hasbyt) {
  1660. newnet.txbytes = readDecNum(s);
  1661. skipSp(s);
  1662. }
  1663. else
  1664. newnet.txbytes = 0;
  1665. newnet.txpackets = readDecNum(s);
  1666. skipSp(s);
  1667. newnet.txerrors = readDecNum(s);
  1668. skipSp(s);
  1669. newnet.txdrops = readDecNum(s);
  1670. break;
  1671. }
  1672. }
  1673. fclose(netfp);
  1674. return true;
  1675. }
  1676. size32_t getKLog(const char *&data)
  1677. {
  1678. #ifdef __linux__
  1679. if (kbufmax)
  1680. {
  1681. data = nullptr;
  1682. size32_t ksz = 0;
  1683. unsigned short lastCRC = 0;
  1684. // NOTE: allocated 2*kbufmax to work around kernel bug
  1685. // where klogctl() could sometimes return more than requested
  1686. size32_t sz = klogctl(3, kbuf, kbufmax);
  1687. if ((int)sz < 0)
  1688. {
  1689. if (kbadcnt < 5)
  1690. {
  1691. ERRLOG("klogctl SYSLOG_ACTION_READ_ALL error %d", errno);
  1692. kbadcnt++;
  1693. }
  1694. else
  1695. kbufmax = 0;
  1696. return 0;
  1697. }
  1698. #if 0
  1699. kbuf[sz] = '\0';
  1700. #endif
  1701. if (kbufcrc)
  1702. {
  1703. data = kbuf;
  1704. ksz = sz;
  1705. }
  1706. // determine where new info starts ...
  1707. StringBuffer ln;
  1708. const char *p = kbuf;
  1709. const char *e = p+sz;
  1710. while (p && p!=e)
  1711. {
  1712. if (*p=='<')
  1713. {
  1714. ln.clear();
  1715. while ((p && p!=e)&&(*p!='\n'))
  1716. {
  1717. ln.append(*p);
  1718. p++;
  1719. sz--;
  1720. }
  1721. lastCRC = chksum16(ln.str(), ln.length());
  1722. if (kbufcrc && kbufcrc == lastCRC)
  1723. {
  1724. ksz = sz - 1;
  1725. if (ksz && sz)
  1726. data = p + 1;
  1727. else
  1728. data = nullptr;
  1729. }
  1730. }
  1731. while ((p && p!=e)&&(*p!='\n'))
  1732. {
  1733. p++;
  1734. sz--;
  1735. }
  1736. if (p && p!=e)
  1737. {
  1738. p++;
  1739. sz--;
  1740. }
  1741. }
  1742. if (lastCRC)
  1743. kbufcrc = lastCRC;
  1744. if (!ksz)
  1745. data = nullptr;
  1746. return ksz;
  1747. }
  1748. #endif
  1749. data = nullptr;
  1750. return 0;
  1751. }
  1752. inline double perSec(double v,double deltams)
  1753. {
  1754. return 1000.0*v/deltams;
  1755. }
  1756. public:
  1757. unsigned getCPU()
  1758. {
  1759. if (!getNextCPU())
  1760. return (unsigned)-1;
  1761. if (totalcpu==0)
  1762. return 0;
  1763. unsigned ret = (unsigned)((totalcpu-cpu.idle)*100/totalcpu);
  1764. if (ret>100)
  1765. ret = 100;
  1766. return ret;
  1767. }
  1768. CExtendedStats(bool printklog)
  1769. {
  1770. partition = (part_info *)malloc(sizeof(part_info));
  1771. nparts = 0;
  1772. newblkio = NULL;
  1773. oldblkio = NULL;
  1774. first = true;
  1775. ncpu = 0;
  1776. kbuf = nullptr;
  1777. kbufcrc = 0;
  1778. memset(&oldcpu, 0, sizeof(oldcpu));
  1779. memset(&newcpu, 0, sizeof(newcpu));
  1780. memset(&cpu, 0, sizeof(cpu));
  1781. totalcpu = 0;
  1782. numcpu = 0;
  1783. memset(&oldnet, 0, sizeof(oldnet));
  1784. memset(&newnet, 0, sizeof(newnet));
  1785. ndisks = 0;
  1786. kbadcnt = 0;
  1787. if (printklog)
  1788. {
  1789. kbufmax = 0x1000;
  1790. kbuf = (char *)malloc(kbufmax*2);
  1791. if (!kbuf)
  1792. kbufmax = 0;
  1793. }
  1794. else
  1795. kbufmax = 0;
  1796. }
  1797. ~CExtendedStats()
  1798. {
  1799. free(partition);
  1800. free(newblkio);
  1801. free(oldblkio);
  1802. if (kbuf != nullptr)
  1803. free(kbuf);
  1804. }
  1805. bool getLine(StringBuffer &out)
  1806. {
  1807. blkio_info *t = oldblkio;
  1808. oldblkio = newblkio;
  1809. newblkio = t;
  1810. oldnet = newnet;
  1811. #ifdef USE_OLD_PU
  1812. if (!getNextCPU())
  1813. return false; // required
  1814. #endif
  1815. bool gotdisk = getDiskInfo()&&nparts;
  1816. bool gotnet = getNetInfo();
  1817. if (first) {
  1818. first = false;
  1819. return false;
  1820. }
  1821. double deltams = ((double)totalcpu*1000) / ncpu / HZ;
  1822. if (deltams<10)
  1823. return false;
  1824. if (gotdisk) {
  1825. if (out.length()&&(out.charAt(out.length()-1)!=' '))
  1826. out.append(' ');
  1827. out.append("DSK: ");
  1828. for (unsigned p = 0; p < nparts; p++) {
  1829. unsigned rd_ios = newblkio[p].rd_ios - oldblkio[p].rd_ios;
  1830. __uint64 rd_sectors = newblkio[p].rd_sectors - oldblkio[p].rd_sectors;
  1831. unsigned wr_ios = newblkio[p].wr_ios - oldblkio[p].wr_ios;
  1832. __uint64 wr_sectors = newblkio[p].wr_sectors - oldblkio[p].wr_sectors;
  1833. unsigned ticks = newblkio[p].ticks - oldblkio[p].ticks;
  1834. unsigned busy = (unsigned)(100*ticks/deltams);
  1835. if (busy>100)
  1836. busy = 100;
  1837. out.appendf("[%s] r/s=%0.1f kr/s=%0.1f w/s=%0.1f kw/s=%0.1f bsy=%d",
  1838. partition[p].name,
  1839. perSec(rd_ios,deltams),
  1840. perSec(rd_sectors,deltams)/2.0,
  1841. perSec(wr_ios,deltams),
  1842. perSec(wr_sectors,deltams)/2.0,
  1843. busy);
  1844. out.append(' ');
  1845. }
  1846. }
  1847. if (gotnet) {
  1848. out.append("NIC: ");
  1849. __uint64 rxbytes = newnet.rxbytes-oldnet.rxbytes;
  1850. __uint64 rxpackets = newnet.rxpackets-oldnet.rxpackets;
  1851. __uint64 txbytes = newnet.txbytes-oldnet.txbytes;
  1852. __uint64 txpackets = newnet.txpackets-oldnet.txpackets;
  1853. out.appendf("rxp/s=%0.1f rxk/s=%0.1f txp/s=%0.1f txk/s=%0.1f",
  1854. perSec(rxpackets,deltams),
  1855. perSec(rxbytes/1024.0,deltams),
  1856. perSec(txpackets,deltams),
  1857. perSec(txbytes/1024.0,deltams));
  1858. out.append(' ');
  1859. }
  1860. if (totalcpu)
  1861. out.appendf("CPU: usr=%d sys=%d iow=%d idle=%d", (unsigned)(cpu.user*100/totalcpu), (unsigned)(cpu.system*100/totalcpu), (unsigned)(cpu.iowait*100/totalcpu), (unsigned)(cpu.idle*100/totalcpu));
  1862. return true;
  1863. }
  1864. #define KERN_EMERG "<0>" // system is unusable
  1865. #define KERN_ALERT "<1>" // action must be taken immediately
  1866. #define KERN_CRIT "<2>" // critical conditions
  1867. #define KERN_ERR "<3>" // error conditions
  1868. #define KERN_WARNING "<4>" // warning conditions
  1869. #define KERN_NOTICE "<5>" // normal but significant condition
  1870. #define KERN_INFO "<6>" // informational
  1871. #define KERN_DEBUG "<7>" // debug-level messages
  1872. #define KMSGTEST(S) if (memcmp(p,S,3)==0) { ln.append(#S); level = p[1]-'0'; }
  1873. void printKLog(IPerfMonHook *hook)
  1874. {
  1875. const char *p = nullptr;
  1876. size32_t sz = getKLog(p);
  1877. #if 0
  1878. DBGLOG("getKLog() returns: %u <%s>", sz, p);
  1879. #endif
  1880. StringBuffer ln;
  1881. const char *e = p+sz;
  1882. while (p && (p!=e)) {
  1883. if (*p=='<') {
  1884. ln.clear();
  1885. int level = -1;
  1886. KMSGTEST(KERN_EMERG)
  1887. else KMSGTEST(KERN_ALERT)
  1888. else KMSGTEST(KERN_CRIT)
  1889. else KMSGTEST(KERN_ERR)
  1890. else KMSGTEST(KERN_WARNING)
  1891. else KMSGTEST(KERN_NOTICE)
  1892. else KMSGTEST(KERN_INFO)
  1893. else KMSGTEST(KERN_DEBUG)
  1894. else {
  1895. ln.append("KERN_UNKNOWN");
  1896. p -= 3;
  1897. }
  1898. p += 3;
  1899. ln.append(": ");
  1900. while ((p && p!=e)&&(*p!='\n'))
  1901. ln.append(*(p++));
  1902. if (hook)
  1903. hook->log(level, ln.str());
  1904. else
  1905. PROGLOG("%s",ln.str());
  1906. }
  1907. while ((p && p!=e)&&(*p!='\n'))
  1908. p++;
  1909. if (p && p!=e)
  1910. p++;
  1911. }
  1912. }
  1913. };
  1914. #endif
  1915. #ifdef _WIN32
  1916. static struct CNtKernelInformation
  1917. {
  1918. CNtKernelInformation()
  1919. {
  1920. NtQuerySystemInformation = (PROCNTQSI)GetProcAddress(
  1921. GetModuleHandle("ntdll"),
  1922. "NtQuerySystemInformation"
  1923. );
  1924. NtQueryInformationProcess = (PROCNTQIP)GetProcAddress(
  1925. GetModuleHandle("ntdll"),
  1926. "NtQueryInformationProcess"
  1927. );
  1928. // GetSystemTimes not available on earlier versions of Windows - NtQuerySystemInformation not consistent on later ones. So use GetSystemTimes if available
  1929. pGetSystemTimes = (PROCNTGST)GetProcAddress(
  1930. GetModuleHandle("kernel32"),
  1931. "GetSystemTimes"
  1932. );
  1933. NtQuerySystemInformation(SystemBasicInformation,&SysBaseInfo,sizeof(SysBaseInfo),NULL);
  1934. }
  1935. PROCNTQSI NtQuerySystemInformation;
  1936. PROCNTQIP NtQueryInformationProcess;
  1937. PROCNTGST pGetSystemTimes;
  1938. SYSTEM_BASIC_INFORMATION SysBaseInfo;
  1939. } NtKernelFunctions;
  1940. #endif
  1941. struct PortStats
  1942. {
  1943. unsigned port;
  1944. unsigned drops;
  1945. unsigned rx_queue;
  1946. };
  1947. typedef MapBetween<unsigned, unsigned, PortStats, PortStats> MapPortToPortStats;
  1948. class CUdpStatsReporter
  1949. {
  1950. public:
  1951. CUdpStatsReporter()
  1952. {
  1953. dropsCol = -1;
  1954. portCol = -1;
  1955. uidCol = -1;
  1956. queueCol = -1;
  1957. }
  1958. bool reportUdpInfo(unsigned traceLevel)
  1959. {
  1960. #ifdef _WIN32
  1961. return false;
  1962. #else
  1963. if (uidCol==-1 && columnNames.length())
  1964. return false;
  1965. FILE *netfp = fopen("/proc/net/udp", "r");
  1966. if (!netfp)
  1967. return false;
  1968. char ln[512];
  1969. // Read header
  1970. if (!fgets(ln, sizeof(ln), netfp)) {
  1971. fclose(netfp);
  1972. return false;
  1973. }
  1974. if (!columnNames.length())
  1975. {
  1976. columnNames.appendList(ln, " ");
  1977. ForEachItemInRev(idx, columnNames)
  1978. {
  1979. if (streq(columnNames.item(idx), "rem_address"))
  1980. columnNames.add("rem_port", idx+1);
  1981. else if (streq(columnNames.item(idx), "local_address"))
  1982. columnNames.add("local_port", idx+1);
  1983. }
  1984. ForEachItemIn(idx2, columnNames)
  1985. {
  1986. if (streq(columnNames.item(idx2), "drops"))
  1987. dropsCol = idx2;
  1988. else if (streq(columnNames.item(idx2), "local_port"))
  1989. portCol = idx2;
  1990. else if (streq(columnNames.item(idx2), "rx_queue"))
  1991. queueCol = idx2;
  1992. else if (streq(columnNames.item(idx2), "uid"))
  1993. uidCol = idx2;
  1994. }
  1995. if (portCol == -1 || queueCol == -1 || uidCol == -1)
  1996. {
  1997. uidCol = -1;
  1998. fclose(netfp);
  1999. return false;
  2000. }
  2001. }
  2002. int myUid = geteuid();
  2003. while (fgets(ln, sizeof(ln), netfp))
  2004. {
  2005. StringArray cols;
  2006. cols.appendList(ln, " :");
  2007. if (cols.length() >= columnNames.length() && atoi(cols.item(uidCol))==myUid)
  2008. {
  2009. unsigned queue = strtoul(cols.item(queueCol), NULL, 16);
  2010. unsigned drops = 0;
  2011. if (dropsCol >= 0)
  2012. drops = strtoul(cols.item(dropsCol), NULL, 10);
  2013. if (queue || drops)
  2014. {
  2015. unsigned port = strtoul(cols.item(portCol), NULL, 16);
  2016. if (traceLevel > 0)
  2017. DBGLOG("From /proc/net/udp: port %d rx_queue=%u drops=%u", port, queue, drops);
  2018. PortStats *ret = map.getValue(port);
  2019. if (!ret)
  2020. {
  2021. PortStats e = {port, 0, 0};
  2022. map.setValue(port, e);
  2023. ret = map.getValue(port);
  2024. assertex(ret);
  2025. }
  2026. if (queue > ret->rx_queue)
  2027. {
  2028. DBGLOG("UDP queue: new max rx_queue: port %d rx_queue=%u drops=%u", port, queue, drops);
  2029. ret->rx_queue = queue;
  2030. }
  2031. if (drops > ret->drops)
  2032. {
  2033. LOG(MCoperatorError, unknownJob, "DROPPED UDP PACKETS: port %d rx_queue=%u (peak %u) drops=%u (total %i)", port, queue, ret->rx_queue, drops-ret->drops, drops);
  2034. ret->drops = drops;
  2035. }
  2036. }
  2037. }
  2038. }
  2039. fclose(netfp);
  2040. return true;
  2041. #endif
  2042. }
  2043. private:
  2044. MapPortToPortStats map;
  2045. StringArray columnNames;
  2046. int dropsCol;
  2047. int portCol;
  2048. int uidCol;
  2049. int queueCol;
  2050. };
  2051. class CSnmpStatsReporter
  2052. {
  2053. public:
  2054. CSnmpStatsReporter()
  2055. {
  2056. inErrorsCol = -1;
  2057. prevErrors = 0;
  2058. }
  2059. bool reportSnmpInfo()
  2060. {
  2061. #ifdef _WIN32
  2062. return false;
  2063. #else
  2064. if (inErrorsCol==-1 && columnNames.length())
  2065. return false;
  2066. FILE *netfp = fopen("/proc/net/snmp", "r");
  2067. if (!netfp)
  2068. return false;
  2069. char ln[512];
  2070. bool ok = false;
  2071. while (fgets(ln, sizeof(ln), netfp))
  2072. {
  2073. if (strncmp(ln, "Udp:", 4)==0)
  2074. {
  2075. if (!columnNames.length())
  2076. {
  2077. columnNames.appendList(ln, " ");
  2078. ForEachItemIn(idx, columnNames)
  2079. {
  2080. if (streq(columnNames.item(idx), "InErrors"))
  2081. inErrorsCol = idx;
  2082. }
  2083. if (inErrorsCol == -1)
  2084. break;
  2085. }
  2086. if (fgets(ln, sizeof(ln), netfp))
  2087. {
  2088. StringArray cols;
  2089. cols.appendList(ln, " ");
  2090. if (cols.length() >= columnNames.length())
  2091. {
  2092. ok = true;
  2093. unsigned errors = strtoul(cols.item(inErrorsCol), NULL, 10);
  2094. if (errors > prevErrors)
  2095. LOG(MCoperatorError, unknownJob, "UDP InErrors: %u (total %u)", errors-prevErrors, errors);
  2096. prevErrors = errors;
  2097. }
  2098. }
  2099. break;
  2100. }
  2101. }
  2102. fclose(netfp);
  2103. return ok;
  2104. #endif
  2105. }
  2106. private:
  2107. StringArray columnNames;
  2108. int inErrorsCol;
  2109. unsigned prevErrors;
  2110. };
  2111. static class CMemoryUsageReporter: public Thread
  2112. {
  2113. bool term;
  2114. unsigned interval;
  2115. Semaphore sem;
  2116. PerfMonMode traceMode;
  2117. Linked<IPerfMonHook> hook;
  2118. unsigned latestCPU;
  2119. #if defined(USE_OLD_PU) || defined(_WIN32)
  2120. double dbIdleTime;
  2121. double dbSystemTime;
  2122. #endif
  2123. #ifdef _WIN32
  2124. LONG status;
  2125. LARGE_INTEGER liOldIdleTime;
  2126. LARGE_INTEGER liOldSystemTime;
  2127. #else
  2128. double OldIdleTime;
  2129. double OldSystemTime;
  2130. CProcessMonitor procmon;
  2131. CExtendedStats extstats;
  2132. #endif
  2133. StringBuffer primaryfs;
  2134. StringBuffer secondaryfs;
  2135. CriticalSection sect; // for getSystemTraceInfo
  2136. CSnmpStatsReporter snmpStats;
  2137. CUdpStatsReporter udpStats;
  2138. public:
  2139. CMemoryUsageReporter(unsigned _interval, PerfMonMode _traceMode, IPerfMonHook * _hook, bool printklog)
  2140. : Thread("CMemoryUsageReporter"), traceMode(_traceMode)
  2141. #ifndef _WIN32
  2142. , extstats(printklog)
  2143. #endif
  2144. {
  2145. interval = _interval;
  2146. hook.set(_hook);
  2147. term = false;
  2148. latestCPU = 0;
  2149. // UDP stats reported unless explicitly disabled
  2150. if (queryEnvironmentConf().getPropBool("udp_stats", true))
  2151. traceMode |= PerfMonUDP;
  2152. #ifdef _WIN32
  2153. memset(&liOldIdleTime,0,sizeof(liOldIdleTime));
  2154. memset(&liOldSystemTime,0,sizeof(liOldSystemTime));
  2155. dbIdleTime = 0;
  2156. primaryfs.append("C:");
  2157. #else
  2158. FILE* procfp;
  2159. procfp = fopen("/proc/uptime", "r");
  2160. int matched = 0;
  2161. if (procfp) {
  2162. matched = fscanf(procfp, "%lf %lf\n", &OldSystemTime, &OldIdleTime);
  2163. fclose(procfp);
  2164. }
  2165. if (!procfp || matched != 2)
  2166. {
  2167. OldSystemTime = 0;
  2168. OldIdleTime = 0;
  2169. }
  2170. primaryfs.append("/");
  2171. #endif
  2172. }
  2173. void setPrimaryFileSystem(char const * _primaryfs)
  2174. {
  2175. CriticalBlock block(sect);
  2176. primaryfs.clear();
  2177. if(_primaryfs)
  2178. primaryfs.append(_primaryfs);
  2179. }
  2180. void setSecondaryFileSystem(char const * _secondaryfs)
  2181. {
  2182. CriticalBlock block(sect);
  2183. secondaryfs.clear();
  2184. if(_secondaryfs)
  2185. secondaryfs.append(_secondaryfs);
  2186. }
  2187. void getSystemTraceInfo(StringBuffer &str, PerfMonMode mode)
  2188. {
  2189. CriticalBlock block(sect);
  2190. #ifdef _WIN32
  2191. if (NtKernelFunctions.pGetSystemTimes) {
  2192. LARGE_INTEGER idle, kernel, user;
  2193. NtKernelFunctions.pGetSystemTimes(&idle, &kernel, &user);
  2194. // note - kernel time seems to include idle time
  2195. if(liOldIdleTime.QuadPart != 0) {
  2196. // CurrentValue = NewValue - OldValue
  2197. dbIdleTime = Li2Double(idle) - Li2Double(liOldIdleTime);
  2198. dbSystemTime = (Li2Double(kernel) + Li2Double(user)) - Li2Double(liOldSystemTime);
  2199. // CurrentCpuIdle = IdleTime / SystemTime
  2200. dbIdleTime = dbIdleTime / dbSystemTime;
  2201. // CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors
  2202. latestCPU = (unsigned) (100.0 - dbIdleTime * 100.0 + 0.5);
  2203. }
  2204. liOldIdleTime = idle;
  2205. liOldSystemTime.QuadPart = user.QuadPart + kernel.QuadPart;
  2206. } else {
  2207. SYSTEM_PERFORMANCE_INFORMATION SysPerfInfo;
  2208. SYSTEM_TIME_INFORMATION SysTimeInfo;
  2209. NtKernelFunctions.NtQuerySystemInformation(SystemTimeInformation,&SysTimeInfo,sizeof(SysTimeInfo),0);
  2210. NtKernelFunctions.NtQuerySystemInformation(SystemPerformanceInformation,&SysPerfInfo,sizeof(SysPerfInfo),NULL);
  2211. if(liOldIdleTime.QuadPart != 0) {
  2212. // CurrentValue = NewValue - OldValue
  2213. dbIdleTime = Li2Double(SysPerfInfo.liIdleTime) - Li2Double(liOldIdleTime);
  2214. dbSystemTime = Li2Double(SysTimeInfo.liKeSystemTime) - Li2Double(liOldSystemTime);
  2215. // CurrentCpuIdle = IdleTime / SystemTime
  2216. dbIdleTime = dbIdleTime / dbSystemTime;
  2217. // CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors
  2218. latestCPU = (unsigned) (100.0 - dbIdleTime * 100.0 / (double)NtKernelFunctions.SysBaseInfo.bKeNumberProcessors + 0.5);
  2219. }
  2220. liOldIdleTime = SysPerfInfo.liIdleTime;
  2221. liOldSystemTime = SysTimeInfo.liKeSystemTime;
  2222. }
  2223. MEMORYSTATUSEX memstatus;
  2224. memstatus.dwLength = sizeof(memstatus);
  2225. GlobalMemoryStatusEx(&memstatus);
  2226. DWORDLONG vmTotal = memstatus.ullTotalVirtual;
  2227. DWORDLONG vmAvail = memstatus.ullAvailVirtual;
  2228. DWORDLONG vmInUse = vmTotal - vmAvail;
  2229. DWORDLONG physTotal = memstatus.ullAvailPhys;
  2230. DWORDLONG physAvail = memstatus.ullTotalPhys;
  2231. DWORDLONG physInUse = physTotal - physAvail;
  2232. ULARGE_INTEGER diskAvailStruct;
  2233. ULARGE_INTEGER diskTotalStruct;
  2234. unsigned __int64 firstDriveTotal = 0;
  2235. unsigned __int64 firstDriveInUse = 0;
  2236. unsigned __int64 secondDriveTotal = 0;
  2237. unsigned __int64 secondDriveInUse = 0;
  2238. if(primaryfs.length())
  2239. {
  2240. diskAvailStruct.QuadPart = 0;
  2241. diskTotalStruct.QuadPart = 0;
  2242. GetDiskFreeSpaceEx(primaryfs.str(), &diskAvailStruct, &diskTotalStruct, 0);
  2243. firstDriveTotal = diskTotalStruct.QuadPart;
  2244. firstDriveInUse = diskTotalStruct.QuadPart - diskAvailStruct.QuadPart;
  2245. }
  2246. if(secondaryfs.length())
  2247. {
  2248. diskAvailStruct.QuadPart = 0;
  2249. diskTotalStruct.QuadPart = 0;
  2250. GetDiskFreeSpaceEx(secondaryfs.str(), &diskAvailStruct, &diskTotalStruct, 0);
  2251. secondDriveTotal = diskTotalStruct.QuadPart;
  2252. secondDriveInUse = diskTotalStruct.QuadPart - diskAvailStruct.QuadPart;
  2253. }
  2254. if(hook)
  2255. hook->processPerfStats(latestCPU, (unsigned)(vmInUse/1024), (unsigned)(vmTotal/1024), firstDriveInUse, firstDriveTotal, secondDriveInUse, secondDriveTotal, getThreadCount());
  2256. if(!mode)
  2257. return;
  2258. if(mode & PerfMonProcMem)
  2259. {
  2260. str.appendf("PU=%3d%%",latestCPU);
  2261. #if 0
  2262. VM_COUNTERS vmc;
  2263. DWORD dwSize = 0;
  2264. NtKernelFunctions.NtQueryInformationProcess(GetCurrentProcess(), ProcessVmCounters, &vmc, sizeof(vmc), &dwSize);
  2265. str.appendf(" MU=%3u%%",(unsigned)((__int64)vmc.WorkingSetSize*100/(__int64)vmTotal));
  2266. #else
  2267. str.appendf(" MU=%3u%%",(unsigned)((__int64)vmInUse*100/(__int64)vmTotal));
  2268. str.appendf(" PY=%3u%%",(unsigned)((__int64)physInUse*100/(__int64)physTotal));
  2269. if (hook)
  2270. hook->extraLogging(str);
  2271. #ifdef _USE_MALLOC_HOOK
  2272. if (totalMem)
  2273. str.appendf(" TM=%" I64F "d",totalMem);
  2274. #endif
  2275. #endif
  2276. }
  2277. if(mode & PerfMonPackets)
  2278. {
  2279. unsigned tx, rx;
  2280. if(getPacketStats(tx, rx))
  2281. str.appendf(" TX=%3u%% RX=%3u%%", tx, rx);
  2282. else
  2283. str.appendf(" ");
  2284. }
  2285. if(mode & PerfMonDiskUsage)
  2286. {
  2287. if(firstDriveTotal) str.appendf(" D1=%3u%%", (unsigned)(firstDriveInUse*100/firstDriveTotal));
  2288. if(secondDriveTotal) str.appendf(" D2=%3u%%", (unsigned)(secondDriveInUse*100/secondDriveTotal));
  2289. }
  2290. if(mode & PerfMonExtended)
  2291. {
  2292. __IO_COUNTERS ioc;
  2293. KERNEL_USER_TIMES kut;
  2294. POOLED_USAGE_AND_LIMITS put;
  2295. VM_COUNTERS vmc;
  2296. DWORD dwSize;
  2297. DWORD dwHandles;
  2298. dwSize = 0;
  2299. NtKernelFunctions.NtQueryInformationProcess(GetCurrentProcess(), ProcessVmCounters, &vmc, sizeof(vmc), &dwSize);
  2300. dwHandles = 0;
  2301. dwSize = 0;
  2302. NtKernelFunctions.NtQueryInformationProcess(GetCurrentProcess(), ProcessHandleCount, &dwHandles, sizeof(dwHandles), &dwSize);
  2303. dwSize = 0;
  2304. NtKernelFunctions.NtQueryInformationProcess(GetCurrentProcess(), ProcessIoCounters, &ioc, sizeof(ioc), &dwSize);
  2305. dwSize = 0;
  2306. NtKernelFunctions.NtQueryInformationProcess(GetCurrentProcess(), ProcessTimes, &kut, sizeof(kut), &dwSize);
  2307. dwSize = 0;
  2308. NtKernelFunctions.NtQueryInformationProcess(GetCurrentProcess(), ProcessPooledUsageAndLimits, &put, sizeof(put), &dwSize);
  2309. str.appendf(" WS=%10u ",vmc.WorkingSetSize);
  2310. str.appendf("PP=%10u ",put.PagedPoolUsage);
  2311. str.appendf("NP=%10u ",put.NonPagedPoolUsage);
  2312. str.appendf("HC=%5u ",dwHandles);
  2313. str.appendf("TC=%5u ",getThreadCount());
  2314. str.appendf("IR=%10u ",(unsigned)(ioc.ReadTransferCount/1024));
  2315. str.appendf("IW=%10u ",(unsigned)(ioc.WriteTransferCount/1024));
  2316. str.appendf("IO=%10u ",(unsigned)(ioc.OtherTransferCount/1024));
  2317. str.appendf("KT=%16" I64F "u ",kut.KernelTime);
  2318. str.appendf("UT=%16" I64F "u ",kut.UserTime);
  2319. }
  2320. #else
  2321. bool outofhandles = false;
  2322. #ifdef USE_OLD_PU
  2323. FILE* procfp = fopen("/proc/uptime", "r");
  2324. int matched = 0;
  2325. OldSystemTime = 0;
  2326. if (procfp) {
  2327. matched = fscanf(procfp, "%lf %lf\n", &dbSystemTime, &dbIdleTime);
  2328. fclose(procfp);
  2329. outofhandles = false;
  2330. }
  2331. latestCPU = unsigned(100.0 - (dbIdleTime - OldIdleTime)*100.0/(dbSystemTime - OldSystemTime) + 0.5);
  2332. if (procfp && matched == 2)
  2333. {
  2334. OldSystemTime = dbSystemTime;
  2335. OldIdleTime = dbIdleTime;
  2336. }
  2337. #else
  2338. latestCPU = extstats.getCPU();
  2339. if (latestCPU==(unsigned)-1) {
  2340. outofhandles = true;
  2341. latestCPU = 0;
  2342. }
  2343. #endif
  2344. unsigned __int64 primaryfsTotal = 0;
  2345. unsigned __int64 primaryfsInUse = 0;
  2346. unsigned __int64 secondaryfsTotal = 0;
  2347. unsigned __int64 secondaryfsInUse = 0;
  2348. if(primaryfs.length())
  2349. getDiskUsage(primaryfs.str(), primaryfsTotal, primaryfsInUse);
  2350. if(secondaryfs.length())
  2351. getDiskUsage(secondaryfs.str(), secondaryfsTotal, secondaryfsInUse);
  2352. if(!mode) return;
  2353. unsigned memused=0;
  2354. unsigned memtot=0;
  2355. if(mode & PerfMonProcMem)
  2356. {
  2357. if (!outofhandles)
  2358. str.appendf("PU=%3d%% ", latestCPU);
  2359. else
  2360. str.appendf("PU=OOH ");
  2361. getMemStats(str,memused,memtot);
  2362. if (hook)
  2363. hook->extraLogging(str);
  2364. procmon.printBusy(latestCPU,str);
  2365. }
  2366. if (hook) {
  2367. if (!memtot) {
  2368. unsigned mu;
  2369. unsigned ma;
  2370. unsigned mt;
  2371. unsigned st;
  2372. unsigned su;
  2373. getMemUsage(mu,ma,mt,st,su);
  2374. memused = mu+su;
  2375. memtot = mt+st;
  2376. }
  2377. hook->processPerfStats(latestCPU, memused, memtot, primaryfsInUse, primaryfsTotal, secondaryfsInUse, secondaryfsTotal, getThreadCount());
  2378. }
  2379. if(mode & PerfMonPackets)
  2380. {
  2381. unsigned tx, rx;
  2382. if(getPacketStats(tx, rx))
  2383. str.appendf(" TX=%3u%% RX=%3u%%", tx, rx);
  2384. else
  2385. str.appendf(" ");
  2386. }
  2387. if(mode & PerfMonDiskUsage)
  2388. {
  2389. if(primaryfsTotal) str.appendf(" D1=%3u%%", (unsigned)(primaryfsInUse*100/primaryfsTotal));
  2390. if(secondaryfsTotal) str.appendf(" D2=%3u%%", (unsigned)(secondaryfsInUse*100/secondaryfsTotal));
  2391. }
  2392. if(mode & PerfMonExtended)
  2393. {
  2394. extstats.getLine(str);
  2395. }
  2396. #endif
  2397. }
  2398. #define NAMEDCOUNTPERIOD 60*30
  2399. int run()
  2400. {
  2401. StringBuffer str;
  2402. getSystemTraceInfo(str, traceMode&~PerfMonExtended); // initializes the values so that first one we print is meaningful rather than always saying PU=0%
  2403. if (traceMode&PerfMonUDP)
  2404. {
  2405. snmpStats.reportSnmpInfo();
  2406. udpStats.reportUdpInfo(0);
  2407. }
  2408. CTimeMon tm(NAMEDCOUNTPERIOD*1000);
  2409. while (!term) {
  2410. if (sem.wait(interval))
  2411. break;
  2412. str.clear();
  2413. getSystemTraceInfo(str, traceMode&~PerfMonExtended);
  2414. #ifdef NAMEDCOUNTS
  2415. if (tm.timedout())
  2416. {
  2417. dumpNamedCounts(str.newline());
  2418. tm.reset(NAMEDCOUNTPERIOD*1000);
  2419. }
  2420. #endif
  2421. if (traceMode&PerfMonUDP)
  2422. {
  2423. snmpStats.reportSnmpInfo();
  2424. udpStats.reportUdpInfo(0);
  2425. }
  2426. if(traceMode&&str.length()) {
  2427. LOG(MCdebugInfo, unknownJob, "SYS: %s", str.str());
  2428. #ifndef _WIN32
  2429. if (traceMode&PerfMonExtended) {
  2430. if (extstats.getLine(str.clear()))
  2431. LOG(MCdebugInfo, unknownJob, "%s", str.str());
  2432. {
  2433. CriticalBlock block(sect);
  2434. extstats.printKLog(hook);
  2435. }
  2436. }
  2437. #endif
  2438. }
  2439. }
  2440. return 0;
  2441. }
  2442. void stop()
  2443. {
  2444. term = true;
  2445. sem.signal();
  2446. join();
  2447. }
  2448. unsigned queryLatestCPU() const
  2449. {
  2450. return latestCPU;
  2451. }
  2452. void setHook(IPerfMonHook *_hook)
  2453. {
  2454. CriticalBlock block(sect);
  2455. hook.set(_hook);
  2456. }
  2457. } *MemoryUsageReporter=NULL;
  2458. #ifdef _WIN32
  2459. static inline unsigned scaleFileTimeToMilli(unsigned __int64 nano100)
  2460. {
  2461. return (unsigned)(nano100 / 10000);
  2462. }
  2463. void getProcessTime(UserSystemTime_t & result)
  2464. {
  2465. LARGE_INTEGER startTime, exitTime, kernelTime, userTime;
  2466. if (GetProcessTimes(GetCurrentProcess(), (FILETIME *)&startTime, (FILETIME *)&exitTime, (FILETIME *)&kernelTime, (FILETIME *)&userTime))
  2467. {
  2468. result.user = scaleFileTimeToMilli(userTime.QuadPart);
  2469. result.system = scaleFileTimeToMilli(kernelTime.QuadPart);
  2470. }
  2471. }
  2472. #else
  2473. void getProcessTime(UserSystemTime_t & result)
  2474. {
  2475. UserStatusInfo info(GetCurrentProcessId());
  2476. if (info.update())
  2477. result = info.time;
  2478. }
  2479. #endif
  2480. void getSystemTraceInfo(StringBuffer &str, PerfMonMode mode)
  2481. {
  2482. if (!MemoryUsageReporter)
  2483. MemoryUsageReporter = new CMemoryUsageReporter(1000, mode, 0, false);
  2484. MemoryUsageReporter->getSystemTraceInfo(str, mode);
  2485. }
  2486. void startPerformanceMonitor(unsigned interval, PerfMonMode traceMode, IPerfMonHook * hook)
  2487. {
  2488. stopPerformanceMonitor();
  2489. if (!MemoryUsageReporter) {
  2490. MemoryUsageReporter = new CMemoryUsageReporter(interval, traceMode, hook, (traceMode&PerfMonExtended)!=0);
  2491. MemoryUsageReporter->start();
  2492. }
  2493. }
  2494. void stopPerformanceMonitor()
  2495. {
  2496. if (MemoryUsageReporter) {
  2497. MemoryUsageReporter->stop();
  2498. delete MemoryUsageReporter;
  2499. MemoryUsageReporter = NULL;
  2500. }
  2501. }
  2502. void setPerformanceMonitorHook(IPerfMonHook *hook)
  2503. {
  2504. if (MemoryUsageReporter)
  2505. MemoryUsageReporter->setHook(hook);
  2506. }
  2507. void setPerformanceMonitorPrimaryFileSystem(char const * fs)
  2508. {
  2509. if(MemoryUsageReporter)
  2510. MemoryUsageReporter->setPrimaryFileSystem(fs);
  2511. }
  2512. void setPerformanceMonitorSecondaryFileSystem(char const * fs)
  2513. {
  2514. if(MemoryUsageReporter)
  2515. MemoryUsageReporter->setSecondaryFileSystem(fs);
  2516. }
  2517. unsigned getLatestCPUUsage()
  2518. {
  2519. if (MemoryUsageReporter)
  2520. return MemoryUsageReporter->queryLatestCPU();
  2521. else
  2522. return 0;
  2523. }
  2524. void getHardwareInfo(HardwareInfo &hdwInfo, const char *primDiskPath, const char *secDiskPath)
  2525. {
  2526. memset(&hdwInfo, 0, sizeof(HardwareInfo));
  2527. getCpuInfo(hdwInfo.numCPUs, hdwInfo.CPUSpeed);
  2528. #ifdef _WIN32
  2529. MEMORYSTATUS memstatus;
  2530. GlobalMemoryStatus(&memstatus);
  2531. hdwInfo.totalMemory = (unsigned)(memstatus.dwTotalPhys / (1024*1024)); // in MB
  2532. ULARGE_INTEGER diskAvailStruct;
  2533. ULARGE_INTEGER diskTotalStruct;
  2534. if (primDiskPath)
  2535. {
  2536. diskTotalStruct.QuadPart = 0;
  2537. GetDiskFreeSpaceEx(primDiskPath, &diskAvailStruct, &diskTotalStruct, 0);
  2538. hdwInfo.primDiskSize = (unsigned)(diskTotalStruct.QuadPart / (1024*1024*1024)); // in GB
  2539. hdwInfo.primFreeSize = (unsigned)(diskAvailStruct.QuadPart / (1024*1024*1024)); // in GB
  2540. }
  2541. if (secDiskPath)
  2542. {
  2543. diskTotalStruct.QuadPart = 0;
  2544. GetDiskFreeSpaceEx(secDiskPath, &diskAvailStruct, &diskTotalStruct, 0);
  2545. hdwInfo.secDiskSize = (unsigned)(diskTotalStruct.QuadPart / (1024*1024*1024)); // in GB
  2546. hdwInfo.secFreeSize = (unsigned)(diskAvailStruct.QuadPart / (1024*1024*1024)); // in GB
  2547. }
  2548. // MORE: Find win32 call for NIC speed
  2549. #else // linux
  2550. unsigned memUsed, memActive, memSwap, memSwapUsed;
  2551. getMemUsage(memUsed, memActive, hdwInfo.totalMemory, memSwap, memSwapUsed);
  2552. hdwInfo.totalMemory /= 1024; // in MB
  2553. unsigned __int64 diskSize;
  2554. unsigned __int64 diskUsed;
  2555. if (primDiskPath)
  2556. {
  2557. getDiskUsage(primDiskPath, diskSize, diskUsed);
  2558. hdwInfo.primDiskSize = diskSize / (1024*1024*1024); // in GB
  2559. hdwInfo.primFreeSize = (diskSize - diskUsed) / (1024*1024*1024); // in GB
  2560. }
  2561. if (secDiskPath)
  2562. {
  2563. getDiskUsage(secDiskPath, diskSize, diskUsed);
  2564. hdwInfo.secDiskSize = diskSize / (1024*1024*1024); // in GB
  2565. hdwInfo.secFreeSize = (diskSize - diskUsed) / (1024*1024*1024); // in GB
  2566. }
  2567. // MORE: Find linux call for NIC speed -- mii-tool does not seem to work on our roxie clusters?
  2568. #endif
  2569. }
  2570. //===========================================================================
  2571. enum SegTypes {
  2572. segtype_free, //
  2573. segtype_heap, // rw-p named [heap]
  2574. segtype_data, // rw-p unnamed
  2575. segtype_guard, // ---p unnamed/named
  2576. segtype_stack, // rwxp
  2577. segtype_qlibcode, // r-xp named */lib200
  2578. segtype_qlibdata, // rw-p named */lib200
  2579. segtype_libcode, // r-xp named *
  2580. segtype_libdata, // rw-p named *
  2581. segtype_pstack, // rwxp named [stack]
  2582. segtype_const, // r--p
  2583. segtype_null // must be last
  2584. };
  2585. struct SegTypeRec
  2586. {
  2587. offset_t total;
  2588. unsigned n;
  2589. offset_t largest;
  2590. };
  2591. const char *PROCMAPHEADER =
  2592. "FREE,NFREE,MAXFREE,HEAP,STACK,NSTACKS,DATA,NDATA,MAXDATA,LIBDATA,QUERYDATA,MAXQUERYDATA,LIBCODE,QUERYCODE,MAXQLIBCODE";
  2593. class CProcReader
  2594. {
  2595. // Cant use JFile for /proc filesystem as seek doesn't work
  2596. public:
  2597. char ln [512];
  2598. FILE *file;
  2599. const char *buf;
  2600. size32_t bufsize;
  2601. CProcReader(const char *filename,const void *_buf,size32_t _buflen)
  2602. {
  2603. buf = (const char *)_buf;
  2604. bufsize = buf?_buflen:0;
  2605. file = buf?NULL:fopen(filename,"r");
  2606. }
  2607. ~CProcReader()
  2608. {
  2609. if (file)
  2610. fclose(file);
  2611. }
  2612. bool nextln()
  2613. {
  2614. if (buf) {
  2615. if (bufsize&&*buf) {
  2616. unsigned i = 0;
  2617. while (bufsize&&(i<sizeof(ln)-1)&&*buf&&(*buf!=10)&&(*buf!=13)) {
  2618. ln[i++] = *(buf++);
  2619. bufsize--;
  2620. }
  2621. ln[i] = 0;
  2622. while (bufsize&&*buf&&((*buf==10)||(*buf==13))) {
  2623. buf++;
  2624. bufsize--;
  2625. }
  2626. return true;
  2627. }
  2628. }
  2629. else if (file&&fgets (ln, sizeof(ln), file)) {
  2630. size_t i = strlen(ln);
  2631. while (i&&((ln[i-1]==10)||(ln[i-1]==13)))
  2632. i--;
  2633. ln[i] = 0;
  2634. return true;
  2635. }
  2636. if (file) {
  2637. fclose(file);
  2638. file = NULL;
  2639. }
  2640. return false;
  2641. }
  2642. void dump(bool useprintf)
  2643. {
  2644. while (nextln()) {
  2645. if (useprintf)
  2646. printf("%s\n",ln);
  2647. else
  2648. PROGLOG("%s",ln);
  2649. }
  2650. }
  2651. };
  2652. void printProcMap(const char *fn, bool printbody, bool printsummary, StringBuffer *lnout, MemoryBuffer *mb, bool useprintf)
  2653. {
  2654. CProcReader reader(fn,mb?mb->toByteArray():NULL,mb?mb->length():0);
  2655. unsigned i;
  2656. SegTypeRec recs[segtype_null];
  2657. memset(&recs,0,sizeof(recs));
  2658. offset_t last=0;
  2659. if (printbody) {
  2660. if (useprintf)
  2661. printf("START,END,SIZE,OFFSET,PERMS,PATH\n");
  2662. else
  2663. PROGLOG("START,END,SIZE,OFFSET,PERMS,PATH");
  2664. }
  2665. while (reader.nextln()) {
  2666. const char *ln = reader.ln;
  2667. unsigned n=0;
  2668. if (*ln) {
  2669. offset_t start = readHexNum(ln);
  2670. if (last&&(last!=start)) {
  2671. recs[segtype_free].n++;
  2672. offset_t ssz = start-last;
  2673. recs[segtype_free].total += ssz;
  2674. if (ssz>recs[segtype_free].largest)
  2675. recs[segtype_free].largest = ssz;
  2676. }
  2677. if (*ln=='-') {
  2678. ln++;
  2679. offset_t end = readHexNum(ln);
  2680. char perms[5];
  2681. skipSp(ln);
  2682. for (i=0;i<4;)
  2683. if (*ln)
  2684. perms[i++] = *(ln++);
  2685. perms[i] = 0;
  2686. skipSp(ln);
  2687. offset_t offset = readHexNum(ln);
  2688. skipSp(ln);
  2689. char dev[6];
  2690. for (i=0;i<5;)
  2691. if (*ln)
  2692. dev[i++] = *(ln++);
  2693. dev[i] = 0;
  2694. skipSp(ln);
  2695. unsigned inode __attribute__((unused)) = (unsigned) readDecNum(ln);
  2696. skipSp(ln);
  2697. const char *path = ln;
  2698. if (printbody) {
  2699. if (useprintf)
  2700. printf("%08" I64F "x,%08" I64F "x,%" I64F "d,%08" I64F "x,%s,%s,%s\n",start,end,(offset_t)(end-start),offset,perms,dev,path);
  2701. else
  2702. PROGLOG("%08" I64F "x,%08" I64F "x,%" I64F "d,%08" I64F "x,%s,%s,%s",start,end,(offset_t)(end-start),offset,perms,dev,path);
  2703. }
  2704. SegTypes t = segtype_data;
  2705. if (strcmp(perms,"---p")==0)
  2706. t = segtype_guard;
  2707. else if (strcmp(perms,"rwxp")==0) {
  2708. if (memicmp(ln,"[stack]",7)==0)
  2709. t = segtype_pstack;
  2710. else
  2711. t = segtype_stack;
  2712. }
  2713. else if (strcmp(perms,"rw-p")==0) {
  2714. if (memicmp(ln,"[heap]",6)==0)
  2715. t = segtype_heap;
  2716. else if (strstr(ln,"/libW200"))
  2717. t = segtype_qlibdata;
  2718. else if (*ln)
  2719. t = segtype_libdata;
  2720. else
  2721. t = segtype_data;
  2722. }
  2723. else if (strcmp(perms,"r-xp")==0) {
  2724. if (strstr(ln,"/libW200"))
  2725. t = segtype_qlibcode;
  2726. else if (*ln)
  2727. t = segtype_libcode;
  2728. }
  2729. else if (strcmp(perms,"r--p")==0)
  2730. t = segtype_const;
  2731. else {
  2732. ERRLOG("%s - unknown perms",perms);
  2733. continue;
  2734. }
  2735. recs[t].n++;
  2736. offset_t ssz = end-start;
  2737. recs[t].total += ssz;
  2738. if (ssz>recs[t].largest)
  2739. recs[t].largest = ssz;
  2740. n++;
  2741. last = end;
  2742. #ifndef __64BIT__
  2743. if ((end<0xffffffff)&&(end>=0xc0000000)) // rest is OS (32-bit only)
  2744. break;
  2745. #endif
  2746. }
  2747. }
  2748. }
  2749. if (printsummary||lnout) {
  2750. StringBuffer tln;
  2751. if (lnout==NULL)
  2752. lnout = &tln;
  2753. lnout->appendf("%" I64F "u," // total
  2754. "%u," // n
  2755. "%" I64F "u," // largest
  2756. "%" I64F "u," // total
  2757. "%" I64F "u," // total
  2758. "%u," // n
  2759. "%" I64F "u," // total
  2760. "%u," // n
  2761. "%" I64F "u," // largest
  2762. "%" I64F "u," // total
  2763. "%" I64F "u," // total
  2764. "%" I64F "u," // largest
  2765. "%" I64F "u," // total
  2766. "%" I64F "u," // total
  2767. "%" I64F "u" // largest
  2768. ,
  2769. recs[segtype_free].total,
  2770. recs[segtype_free].n,
  2771. recs[segtype_free].largest,
  2772. recs[segtype_heap].total,
  2773. recs[segtype_stack].total,
  2774. recs[segtype_stack].n,
  2775. recs[segtype_data].total,
  2776. recs[segtype_data].n,
  2777. recs[segtype_data].largest,
  2778. recs[segtype_libdata].total,
  2779. recs[segtype_qlibdata].total,
  2780. recs[segtype_qlibdata].largest,
  2781. recs[segtype_libcode].total,
  2782. recs[segtype_qlibcode].total,
  2783. recs[segtype_qlibcode].largest
  2784. );
  2785. if (printsummary) {
  2786. if (useprintf)
  2787. printf("%s\n%s\n",PROCMAPHEADER,tln.str());
  2788. else {
  2789. PROGLOG("%s",PROCMAPHEADER);
  2790. PROGLOG("%s",tln.str());
  2791. }
  2792. }
  2793. }
  2794. }
  2795. #ifdef _WIN32
  2796. // stubs
  2797. void PrintMemoryReport(bool full)
  2798. {
  2799. StringBuffer s;
  2800. getSystemTraceInfo(s,PerfMonProcMem);
  2801. PROGLOG("%s",s.str());
  2802. }
  2803. #else
  2804. void PrintMemoryReport(bool full)
  2805. {
  2806. // may be very close to oom so protect against re-entry
  2807. static int recurse=0;
  2808. if (recurse++==0) {
  2809. try {
  2810. printProcMap("/proc/self/maps",full,true,NULL,NULL,false);
  2811. }
  2812. catch (IException *e) {
  2813. e->Release();
  2814. }
  2815. catch (...) {
  2816. }
  2817. try {
  2818. PROGLOG("/proc/meminfo:");
  2819. CProcReader reader("/proc/meminfo",NULL,0);
  2820. reader.dump(false);
  2821. }
  2822. catch (IException *e) {
  2823. e->Release();
  2824. }
  2825. catch (...) {
  2826. }
  2827. try {
  2828. PROGLOG("/proc/self/status:");
  2829. CProcReader reader("/proc/self/status",NULL,0);
  2830. reader.dump(false);
  2831. }
  2832. catch (IException *e) {
  2833. e->Release();
  2834. }
  2835. catch (...) {
  2836. }
  2837. try {
  2838. StringBuffer s;
  2839. getSystemTraceInfo(s,PerfMonProcMem);
  2840. PROGLOG("%s",s.str());
  2841. PROGLOG("===============================================================");
  2842. }
  2843. catch (IException *e) {
  2844. e->Release();
  2845. }
  2846. catch (...) {
  2847. }
  2848. }
  2849. recurse--;
  2850. }
  2851. #endif
  2852. bool areTransparentHugePagesEnabled()
  2853. {
  2854. #ifdef __linux__
  2855. StringBuffer contents;
  2856. try
  2857. {
  2858. contents.loadFile("/sys/kernel/mm/transparent_hugepage/enabled");
  2859. return !strstr(contents.str(), "[never]");
  2860. }
  2861. catch (IException * e)
  2862. {
  2863. e->Release();
  2864. }
  2865. #endif
  2866. return false;
  2867. }
  2868. memsize_t getHugePageSize()
  2869. {
  2870. #ifdef __linux__
  2871. StringBuffer contents;
  2872. try
  2873. {
  2874. //Search for an entry Hugepagesize: xxxx kB
  2875. const char * const tag = "Hugepagesize:";
  2876. contents.loadFile("/proc/meminfo");
  2877. const char * hugepage = strstr(contents.str(), tag);
  2878. if (hugepage)
  2879. {
  2880. const char * next = hugepage + strlen(tag);
  2881. char * end;
  2882. memsize_t size = strtoul(next, &end, 10);
  2883. if (strncmp(end, " kB", 3) == 0)
  2884. return size * 0x400;
  2885. }
  2886. }
  2887. catch (IException * e)
  2888. {
  2889. e->Release();
  2890. }
  2891. #endif
  2892. return 0x200000; // Default for an x86 system
  2893. }
  2894. //===========================================================================
  2895. #ifdef LEAK_CHECK
  2896. #ifdef _WIN32
  2897. LeakChecker::LeakChecker(const char * _title) : title(_title)
  2898. {
  2899. _CrtMemCheckpoint(&oldMemState);
  2900. }
  2901. LeakChecker::~LeakChecker()
  2902. {
  2903. _CrtMemState newMemState, diffMemState;
  2904. _CrtMemCheckpoint(&newMemState);
  2905. if(_CrtMemDifference(&diffMemState, &oldMemState, &newMemState))
  2906. {
  2907. _RPT1(_CRT_WARN, "----- Memory leaks in '%s' -----\n", title);
  2908. _CrtMemDumpStatistics(&diffMemState);
  2909. _CrtMemDumpAllObjectsSince(&oldMemState);
  2910. _RPT0(_CRT_WARN, "----- End of leaks -----\n");
  2911. }
  2912. }
  2913. static char _logFile[255]; // used to hold last file name of log file for memory leak logging
  2914. static FILE *_logHandle = NULL;
  2915. _CRT_REPORT_HOOK oldReport;
  2916. static int MemoryLeakReportHook(int nRptType,char *szMsg,int *retVal)
  2917. {
  2918. if (szMsg && *szMsg)
  2919. {
  2920. if (_logHandle)
  2921. fprintf(_logHandle, szMsg);
  2922. if (*_logFile)
  2923. {
  2924. #if 1
  2925. // this works better in VS 2008 libraries (which fault in fopen)
  2926. int handle = _open(_logFile, O_RDWR | O_CREAT, _S_IREAD | _S_IWRITE);
  2927. _lseek(handle,0,SEEK_END);
  2928. _write(handle,szMsg,(unsigned)strlen(szMsg));
  2929. _close(handle);
  2930. #else
  2931. FILE *handle = fopen(_logFile, "a");
  2932. fprintf(handle, szMsg);
  2933. fclose(handle);
  2934. #endif
  2935. }
  2936. }
  2937. if (oldReport)
  2938. return oldReport(nRptType,szMsg,retVal);
  2939. else
  2940. return false;
  2941. }
  2942. MODULE_INIT(INIT_PRIORITY_JDEBUG1)
  2943. {
  2944. oldReport = _CrtSetReportHook(MemoryLeakReportHook);
  2945. return 1;
  2946. }
  2947. void logLeaks (const char *logFile)
  2948. {
  2949. if (logFile)
  2950. strncpy(_logFile, logFile, sizeof(_logFile));
  2951. else
  2952. _logFile[0] = 0;
  2953. }
  2954. void logLeaks (FILE *logHandle)
  2955. {
  2956. _logHandle = logHandle;
  2957. }
  2958. #else
  2959. #endif
  2960. #endif
  2961. #if !defined(USING_MPATROL) && defined(WIN32) && defined(_DEBUG)
  2962. void jlib_decl enableMemLeakChecking(bool enable)
  2963. {
  2964. int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
  2965. if (enable)
  2966. tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
  2967. else
  2968. tmpFlag &= ~_CRTDBG_LEAK_CHECK_DF;
  2969. _CrtSetDbgFlag( tmpFlag );
  2970. }
  2971. #endif
  2972. #if defined(_WIN32) && defined(_DEBUG)
  2973. //#include <dbgint.h>
  2974. const unsigned maxUnique = 10000;
  2975. typedef struct _CrtMemBlockHeader
  2976. {
  2977. // Pointer to the block allocated just before this one:
  2978. struct _CrtMemBlockHeader *pBlockHeaderNext;
  2979. // Pointer to the block allocated just after this one:
  2980. struct _CrtMemBlockHeader *pBlockHeaderPrev;
  2981. char *szFileName; // File name
  2982. int nLine; // Line number
  2983. size_t nDataSize; // Size of user block
  2984. int nBlockUse; // Type of block
  2985. long lRequest; // Allocation number
  2986. } _CrtMemBlockHeader;
  2987. int compareFile(_CrtMemBlockHeader * left, _CrtMemBlockHeader * right)
  2988. {
  2989. int compare;
  2990. if (left->szFileName && right->szFileName)
  2991. compare = strcmp(left->szFileName, right->szFileName);
  2992. else if (left->szFileName)
  2993. compare = -1;
  2994. else if (right->szFileName)
  2995. compare = +1;
  2996. else
  2997. compare = 0;
  2998. return compare;
  2999. }
  3000. int compareLocation(_CrtMemBlockHeader * left, _CrtMemBlockHeader * right)
  3001. {
  3002. int compare = compareFile(left, right);
  3003. if (compare != 0)
  3004. return compare;
  3005. return left->nLine - right->nLine;
  3006. }
  3007. int compareBlocks(_CrtMemBlockHeader * left, _CrtMemBlockHeader * right)
  3008. {
  3009. int compare = compareLocation(left, right);
  3010. if (compare != 0)
  3011. return compare;
  3012. return (int)(right->nDataSize - left->nDataSize);
  3013. }
  3014. void addLocation(unsigned & numUnique, _CrtMemBlockHeader * * locations, unsigned * counts, _CrtMemBlockHeader * search)
  3015. {
  3016. int left = 0;
  3017. int right = numUnique;
  3018. while (left < right)
  3019. {
  3020. int mid = (left + right) >> 1;
  3021. int cmp = compareBlocks(search, locations[mid]);
  3022. if (cmp < 0)
  3023. right = mid;
  3024. else if (cmp > 0)
  3025. left = mid+1;
  3026. else
  3027. {
  3028. //Save the lowest allocation number (so quicker to set a subsequent breakpoint)
  3029. if (search->lRequest < locations[mid]->lRequest)
  3030. locations[mid] = search;
  3031. counts[mid]++;
  3032. return;
  3033. }
  3034. }
  3035. if (numUnique != maxUnique)
  3036. {
  3037. assertex(left == right);
  3038. memmove(locations + left+1, locations + left, (numUnique-left)*sizeof(*locations));
  3039. memmove(counts + left+1, counts + left, (numUnique-left)*sizeof(*counts));
  3040. locations[left] = search;
  3041. counts[left] = 1;
  3042. numUnique++;
  3043. }
  3044. else
  3045. counts[maxUnique]++;
  3046. }
  3047. unsigned dumpMemory(unsigned lenTarget, char * target, unsigned lenSrc, const void * ptr)
  3048. {
  3049. if (lenSrc > lenTarget)
  3050. lenSrc = lenTarget;
  3051. const char * src = (const char *)ptr;
  3052. for (unsigned i=0; i < lenSrc; i++)
  3053. {
  3054. byte next = src[i];
  3055. target[i] = (next >= 0x20 && next <= 0x7e) ? next : '.';
  3056. }
  3057. return lenSrc;
  3058. }
  3059. void printAllocationSummary()
  3060. {
  3061. _CrtMemState state;
  3062. _CrtMemCheckpoint(&state);
  3063. unsigned numUnique = 0;
  3064. _CrtMemBlockHeader * locations[maxUnique+1];
  3065. unsigned counts[maxUnique+1];
  3066. _clear(counts);
  3067. unsigned __int64 totalFree = 0;
  3068. unsigned __int64 totalAllocated = 0;
  3069. //Walk the heap, keeping a tally of (filename, line, size)->count
  3070. _CrtMemBlockHeader * cur;
  3071. for (cur = state.pBlockHeader; cur; cur=cur->pBlockHeaderNext)
  3072. {
  3073. switch (cur->nBlockUse)
  3074. {
  3075. case _NORMAL_BLOCK:
  3076. {
  3077. addLocation(numUnique, locations, counts, cur);
  3078. totalAllocated += cur->nDataSize;
  3079. break;
  3080. }
  3081. case _FREE_BLOCK:
  3082. totalFree += cur->nDataSize;
  3083. break;
  3084. }
  3085. }
  3086. PROGLOG("%d Unique allocations by <filename>(line)@size", numUnique);
  3087. for (unsigned i2 = 0; i2 < numUnique; i2++)
  3088. {
  3089. _CrtMemBlockHeader * display = locations[i2];
  3090. //char tempBuffer[16];
  3091. //unsigned len = dumpMemory(sizeof(tempBuffer), tempBuffer, display->nDataSize,
  3092. PROGLOG("%s(%d) %d:%d {%ld} = %d", display->szFileName ? display->szFileName : "<unknown>", display->nLine, display->nDataSize, counts[i2], display->lRequest, display->nDataSize * counts[i2]);
  3093. }
  3094. PROGLOG("Ungrouped: %d Total %" I64F "d", counts[maxUnique], totalAllocated);
  3095. PROGLOG("Summary by location");
  3096. for (unsigned iSummary2 = 0; iSummary2 < numUnique; )
  3097. {
  3098. _CrtMemBlockHeader * display = locations[iSummary2];
  3099. unsigned count = counts[iSummary2];
  3100. unsigned __int64 size = count * display->nDataSize;
  3101. for (iSummary2++; iSummary2 < numUnique; iSummary2++)
  3102. {
  3103. _CrtMemBlockHeader * next = locations[iSummary2];
  3104. if (compareLocation(display, next) != 0)
  3105. break;
  3106. count += counts[iSummary2];
  3107. size += (counts[iSummary2] * next->nDataSize);
  3108. }
  3109. PROGLOG("%s(%d) %d = %d", display->szFileName ? display->szFileName : "<unknown>", display->nLine, count, size);
  3110. }
  3111. PROGLOG("Summary by source");
  3112. for (unsigned iSummary2 = 0; iSummary2 < numUnique; )
  3113. {
  3114. _CrtMemBlockHeader * display = locations[iSummary2];
  3115. unsigned count = counts[iSummary2];
  3116. unsigned __int64 size = count * display->nDataSize;
  3117. for (iSummary2++; iSummary2 < numUnique; iSummary2++)
  3118. {
  3119. _CrtMemBlockHeader * next = locations[iSummary2];
  3120. if (compareFile(display, next) != 0)
  3121. break;
  3122. count += counts[iSummary2];
  3123. size += (counts[iSummary2] * next->nDataSize);
  3124. }
  3125. PROGLOG("%s %d = %d", display->szFileName ? display->szFileName : "<unknown>", count, size);
  3126. }
  3127. }
  3128. #else
  3129. void printAllocationSummary()
  3130. {
  3131. }
  3132. #endif
  3133. #ifdef _USE_MALLOC_HOOK
  3134. // Note memory hooks should not be enabled for release (as not re-entrant in linux)
  3135. static CriticalSection hookSect;
  3136. #ifdef __linux__
  3137. static void *(*old_malloc_hook)(size_t, const void *);
  3138. static void (*old_free_hook)(void *, const void *);
  3139. static void *(*old_realloc_hook)(void *, size_t, const void *);
  3140. static void * jlib_malloc_hook (size_t size, const void *caller) ;
  3141. static void jlib_free_hook (void *ptr, const void *caller);
  3142. static void *jlib_realloc_hook (void *ptr, size_t size, const void *caller);
  3143. static int jlib_hooknest = 0; // this *shouldn't* really be needed
  3144. inline void restore_malloc_hooks()
  3145. {
  3146. if (--jlib_hooknest==0) {
  3147. __malloc_hook = old_malloc_hook;
  3148. __realloc_hook = old_realloc_hook;
  3149. __free_hook = old_free_hook;
  3150. }
  3151. }
  3152. inline void set_malloc_hooks()
  3153. {
  3154. assertex(jlib_hooknest==0);
  3155. old_malloc_hook = __malloc_hook;
  3156. old_free_hook = __free_hook;
  3157. old_realloc_hook = __realloc_hook;
  3158. __malloc_hook = jlib_malloc_hook;
  3159. __free_hook = jlib_free_hook;
  3160. __realloc_hook = jlib_realloc_hook;
  3161. jlib_hooknest = 1;
  3162. }
  3163. inline void reset_malloc_hooks()
  3164. {
  3165. if (jlib_hooknest++==0) {
  3166. __malloc_hook = jlib_malloc_hook;
  3167. __free_hook = jlib_free_hook;
  3168. __realloc_hook = jlib_realloc_hook;
  3169. }
  3170. }
  3171. inline void incCount(unsigned sz,bool inc)
  3172. {
  3173. int i=0;
  3174. size32_t s=sz;
  3175. while (s) {
  3176. s /= 2;
  3177. i++;
  3178. }
  3179. if (inc)
  3180. memArea[i] += sz;
  3181. else
  3182. memArea[i] -= sz;
  3183. }
  3184. void * jlib_malloc_hook (size_t size, const void *caller)
  3185. {
  3186. CriticalBlock block(hookSect);
  3187. void *res;
  3188. restore_malloc_hooks();
  3189. res = malloc (size);
  3190. if (res) {
  3191. size = malloc_usable_size(res);
  3192. totalMem+=size;
  3193. if (totalMem>hwmTotalMem) {
  3194. if (hwmTotalMem/(100*0x100000)!=totalMem/(100*0x100000)) {
  3195. PrintStackReport();
  3196. PROGLOG("TOTALMEM(%" I64F "d): malloc %u",totalMem,(unsigned)size);
  3197. }
  3198. hwmTotalMem = totalMem;
  3199. }
  3200. }
  3201. else
  3202. size = 0;
  3203. incCount(size,true);
  3204. if (size>REPORT_LARGER_BLOCK_THAN) {
  3205. PrintStackReport();
  3206. PROGLOG("LARGEALLOC(%u): %p",(unsigned)size,res);
  3207. }
  3208. reset_malloc_hooks();
  3209. return res;
  3210. }
  3211. void jlib_free_hook (void *ptr, const void *caller)
  3212. {
  3213. if (!ptr)
  3214. return;
  3215. CriticalBlock block(hookSect);
  3216. restore_malloc_hooks();
  3217. size32_t sz = malloc_usable_size(ptr);
  3218. free (ptr);
  3219. totalMem -= sz;
  3220. incCount(sz,false);
  3221. if (sz>REPORT_LARGER_BLOCK_THAN) {
  3222. PROGLOG("LARGEFREE(%u): %p",(unsigned)sz,ptr);
  3223. }
  3224. reset_malloc_hooks();
  3225. }
  3226. void *jlib_realloc_hook (void *ptr, size_t size, const void *caller)
  3227. {
  3228. CriticalBlock block(hookSect);
  3229. restore_malloc_hooks();
  3230. size32_t oldsz = ptr?malloc_usable_size(ptr):0;
  3231. void *res = realloc (ptr,size);
  3232. if (res) {
  3233. size = malloc_usable_size(res);
  3234. totalMem += size;
  3235. }
  3236. else
  3237. size = 0;
  3238. totalMem -= oldsz;
  3239. if (totalMem>hwmTotalMem) {
  3240. if (hwmTotalMem/(100*0x100000)!=totalMem/(100*0x100000)) {
  3241. PrintStackReport();
  3242. PROGLOG("TOTALMEM(%" I64F "d): realloc %u %u",totalMem,(unsigned)oldsz,(unsigned)size);
  3243. }
  3244. hwmTotalMem = totalMem;
  3245. }
  3246. incCount(size,true);
  3247. incCount(oldsz,false);
  3248. if ((size>REPORT_LARGER_BLOCK_THAN)||(oldsz>REPORT_LARGER_BLOCK_THAN)) {
  3249. if (size>oldsz) {
  3250. PrintStackReport();
  3251. PROGLOG("LARGEREALLOC_UP(%u,%u): %p %p",(unsigned)oldsz,(unsigned)size,ptr,res);
  3252. }
  3253. else {
  3254. PROGLOG("LARGEREALLOC_DN(%u,%u): %p %p",(unsigned)oldsz,(unsigned)size,ptr,res);
  3255. }
  3256. }
  3257. reset_malloc_hooks();
  3258. return res;
  3259. }
  3260. void jlib_decl jlib_init_hook()
  3261. {
  3262. set_malloc_hooks();
  3263. }
  3264. __int64 jlib_decl setAllocHook(bool on,bool clear)
  3265. {
  3266. CriticalBlock block(hookSect);
  3267. __int64 ret = totalMem;
  3268. if (clear) {
  3269. totalMem = 0;
  3270. hwmTotalMem = 0;
  3271. }
  3272. if (on) {
  3273. if (jlib_hooknest==0)
  3274. set_malloc_hooks();
  3275. }
  3276. else {
  3277. while (jlib_hooknest) {
  3278. restore_malloc_hooks();
  3279. //printf("Total = %d bytes\n",totalMem);
  3280. }
  3281. }
  3282. return ret;
  3283. }
  3284. unsigned jlib_decl setAllocHook(bool on)
  3285. {
  3286. // bwd compatible - should use above version in preference
  3287. CriticalBlock block(hookSect);
  3288. if (on) {
  3289. if (jlib_hooknest==0) {
  3290. set_malloc_hooks();
  3291. totalMem = 0;
  3292. hwmTotalMem = 0;
  3293. }
  3294. }
  3295. else {
  3296. while (jlib_hooknest) {
  3297. restore_malloc_hooks();
  3298. //printf("Total = %d bytes\n",totalMem);
  3299. }
  3300. }
  3301. return (unsigned)totalMem;
  3302. }
  3303. #else // windows
  3304. static _CRT_ALLOC_HOOK oldHook = NULL;
  3305. static int allocHook( int allocType, void *userData, size_t size, int nBlockUse,
  3306. long requestNumber, const unsigned char *filename, int lineNumber)
  3307. {
  3308. CriticalBlock block(hookSect);
  3309. if ( nBlockUse == _CRT_BLOCK ) // Ignore internal C runtime library allocations
  3310. return TRUE;
  3311. static bool recurse = false;
  3312. if (recurse)
  3313. return TRUE;
  3314. recurse = true;
  3315. char *operation[] = { "", "allocating", "re-allocating", "freeing" };
  3316. char *blockType[] = { "Free", "Normal", "CRT", "Ignore", "Client" };
  3317. switch (allocType) {
  3318. case _HOOK_REALLOC:
  3319. if (userData==NULL)
  3320. printf("no data on realloc\n");
  3321. else
  3322. totalMem-=_msize(userData);
  3323. // fall through
  3324. case _HOOK_ALLOC:
  3325. totalMem+=size;
  3326. break;
  3327. case _HOOK_FREE:
  3328. if (userData)
  3329. totalMem-=_msize(userData);
  3330. break;
  3331. }
  3332. // printf( "Memory operation in %s, line %d: %s a %d-byte '%s' block (#%ld)\n",
  3333. // filename, lineNumber, operation[allocType], size,
  3334. // blockType[nBlockUse], requestNumber );
  3335. recurse = false;
  3336. return TRUE; // Allow the memory operation to proceed
  3337. }
  3338. unsigned jlib_decl setAllocHook(bool on)
  3339. {
  3340. CriticalBlock block(hookSect);
  3341. if (on) {
  3342. if (oldHook==NULL) {
  3343. oldHook = _CrtSetAllocHook( allocHook );
  3344. totalMem = 0;
  3345. }
  3346. }
  3347. else {
  3348. if (oldHook!=NULL)
  3349. _CrtSetAllocHook( oldHook );
  3350. oldHook = NULL;
  3351. //printf("Total = %d bytes\n",totalMem);
  3352. }
  3353. return (unsigned)totalMem; // return unsigned for bwd compat
  3354. }
  3355. #endif
  3356. __int64 getTotalMem()
  3357. {
  3358. return totalMem;
  3359. }
  3360. #else // release
  3361. unsigned jlib_decl setAllocHook(bool on __attribute__((unused)))
  3362. {
  3363. return 0;
  3364. }
  3365. __int64 jlib_decl setAllocHook(bool on __attribute__((unused)), bool clear __attribute__((unused)))
  3366. {
  3367. return 0;
  3368. }
  3369. __int64 jlib_decl getTotalMem()
  3370. {
  3371. return 0;
  3372. }
  3373. void jlib_decl jlib_init_hook()
  3374. {
  3375. }
  3376. #endif
  3377. class UserMetricMsgHandler : public CInterface, implements ILogMsgHandler, implements IUserMetric
  3378. {
  3379. mutable unsigned __int64 counter;
  3380. StringAttr metricName;
  3381. StringAttr regex;
  3382. Owned<ILogMsgFilter> regexFilter;
  3383. public:
  3384. virtual void Link(void) const { CInterface::Link(); }
  3385. virtual bool Release(void) const
  3386. {
  3387. if (CInterface::Release())
  3388. return true;
  3389. if (!IsShared())
  3390. {
  3391. queryLogMsgManager()->removeMonitor(const_cast<UserMetricMsgHandler *>(this)); // removeMonitor should take a const param really
  3392. }
  3393. return false;
  3394. }
  3395. UserMetricMsgHandler(const char *_name, const char *_regex) : metricName(_name), regex(_regex)
  3396. {
  3397. counter = 0;
  3398. regexFilter.setown(getRegexLogMsgFilter(regex, true));
  3399. queryLogMsgManager()->addMonitor(this, regexFilter);
  3400. }
  3401. // interface ILogMsgHandler
  3402. virtual void handleMessage(const LogMsg & msg __attribute__((unused))) const { counter++; }
  3403. virtual bool needsPrep() const { return false; }
  3404. virtual void prep() {}
  3405. virtual unsigned queryMessageFields() const { return MSGFIELD_detail; }
  3406. virtual void setMessageFields(unsigned _fields __attribute__((unused)) = MSGFIELD_all) {}
  3407. virtual void addToPTree(IPropertyTree * parent __attribute__((unused))) const {}
  3408. virtual int flush() { return 0; }
  3409. virtual char const *disable() { return 0; }
  3410. virtual void enable() {}
  3411. virtual bool getLogName(StringBuffer &name __attribute__((unused))) const { return false; }
  3412. virtual offset_t getLogPosition(StringBuffer &logFileName __attribute__((unused))) const { return 0; };
  3413. // interface IUserMetric
  3414. virtual unsigned __int64 queryCount() const { return counter; }
  3415. virtual const char *queryName() const { return metricName; }
  3416. virtual const char *queryMatchString() const { return regex; }
  3417. virtual void inc() { counter++; }
  3418. virtual void reset() { counter = 0; }
  3419. };
  3420. jlib_decl IUserMetric *createUserMetric(const char *name, const char *matchString)
  3421. {
  3422. return new UserMetricMsgHandler(name, matchString);
  3423. }