jdebug.cpp 92 KB

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