jdebug.cpp 113 KB

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