SystemInformation.cxx 130 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732
  1. /*============================================================================
  2. KWSys - Kitware System Library
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #if defined(_WIN32)
  11. # define NOMINMAX // use our min,max
  12. # if !defined(_WIN32_WINNT) && !(defined(_MSC_VER) && _MSC_VER < 1300)
  13. # define _WIN32_WINNT 0x0501
  14. # endif
  15. # include <winsock.h> // WSADATA, include before sys/types.h
  16. #endif
  17. // TODO:
  18. // We need an alternative implementation for many functions in this file
  19. // when USE_ASM_INSTRUCTIONS gets defined as 0.
  20. //
  21. // Consider using these on Win32/Win64 for some of them:
  22. //
  23. // IsProcessorFeaturePresent
  24. // http://msdn.microsoft.com/en-us/library/ms724482(VS.85).aspx
  25. //
  26. // GetProcessMemoryInfo
  27. // http://msdn.microsoft.com/en-us/library/ms683219(VS.85).aspx
  28. #include "kwsysPrivate.h"
  29. #include KWSYS_HEADER(stl/string)
  30. #include KWSYS_HEADER(stl/vector)
  31. #include KWSYS_HEADER(ios/iosfwd)
  32. #include KWSYS_HEADER(SystemInformation.hxx)
  33. #include KWSYS_HEADER(Process.h)
  34. #include KWSYS_HEADER(ios/iostream)
  35. #include KWSYS_HEADER(ios/sstream)
  36. #include KWSYS_HEADER(ios/fstream)
  37. // Work-around CMake dependency scanning limitation. This must
  38. // duplicate the above list of headers.
  39. #if 0
  40. # include "SystemInformation.hxx.in"
  41. # include "Process.h.in"
  42. # include "Configure.hxx.in"
  43. # include "kwsys_stl.hxx.in"
  44. # include "kwsys_stl_vector.in"
  45. # include "kwsys_stl_iosfwd.in"
  46. # include "kwsys_ios_sstream.h.in"
  47. # include "kwsys_ios_iostream.h.in"
  48. # include "kwsys_ios_fstream.h.in"
  49. #endif
  50. #if defined(_WIN32)
  51. # include <windows.h>
  52. # include <errno.h>
  53. # if defined(KWSYS_SYS_HAS_PSAPI)
  54. # include <psapi.h>
  55. # endif
  56. # if !defined(siginfo_t)
  57. typedef int siginfo_t;
  58. # endif
  59. #else
  60. # include <sys/types.h>
  61. # include <sys/time.h>
  62. # include <sys/utsname.h> // int uname(struct utsname *buf);
  63. # include <sys/resource.h> // getrlimit
  64. # include <unistd.h>
  65. # include <signal.h>
  66. # include <fcntl.h>
  67. # include <errno.h> // extern int errno;
  68. #endif
  69. #ifdef __FreeBSD__
  70. # include <sys/sysctl.h>
  71. # include <fenv.h>
  72. # include <sys/socket.h>
  73. # include <netdb.h>
  74. # include <netinet/in.h>
  75. # if defined(KWSYS_SYS_HAS_IFADDRS_H)
  76. # include <ifaddrs.h>
  77. # define KWSYS_SYSTEMINFORMATION_IMPLEMENT_FQDN
  78. # endif
  79. #endif
  80. #ifdef __APPLE__
  81. # include <sys/sysctl.h>
  82. # include <mach/vm_statistics.h>
  83. # include <mach/host_info.h>
  84. # include <mach/mach.h>
  85. # include <mach/mach_types.h>
  86. # include <fenv.h>
  87. # include <sys/socket.h>
  88. # include <netdb.h>
  89. # include <netinet/in.h>
  90. # if defined(KWSYS_SYS_HAS_IFADDRS_H)
  91. # include <ifaddrs.h>
  92. # define KWSYS_SYSTEMINFORMATION_IMPLEMENT_FQDN
  93. # endif
  94. # if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__-0 >= 1050
  95. # include <execinfo.h>
  96. # define KWSYS_SYSTEMINFORMATION_HAVE_BACKTRACE
  97. # endif
  98. #endif
  99. #ifdef __linux
  100. # include <fenv.h>
  101. # include <sys/socket.h>
  102. # include <netdb.h>
  103. # include <netinet/in.h>
  104. # if defined(KWSYS_SYS_HAS_IFADDRS_H)
  105. # include <ifaddrs.h>
  106. # if !defined(__LSB_VERSION__) /* LSB has no getifaddrs */
  107. # define KWSYS_SYSTEMINFORMATION_IMPLEMENT_FQDN
  108. # endif
  109. # endif
  110. # if defined(__GNUG__)
  111. # include <execinfo.h>
  112. # if !(defined(__LSB_VERSION__) && __LSB_VERSION__ < 41)
  113. # define KWSYS_SYSTEMINFORMATION_HAVE_BACKTRACE
  114. # endif
  115. # endif
  116. # if defined(KWSYS_CXX_HAS_RLIMIT64)
  117. typedef struct rlimit64 ResourceLimitType;
  118. # define GetResourceLimit getrlimit64
  119. # else
  120. typedef struct rlimit ResourceLimitType;
  121. # define GetResourceLimit getrlimit
  122. # endif
  123. #elif defined( __hpux )
  124. # include <sys/param.h>
  125. # include <sys/pstat.h>
  126. #endif
  127. #ifdef __HAIKU__
  128. # include <OS.h>
  129. #endif
  130. #include <memory.h>
  131. #include <stdlib.h>
  132. #include <stdio.h>
  133. #include <string.h>
  134. #include <ctype.h> // int isdigit(int c);
  135. #if defined(KWSYS_USE_LONG_LONG)
  136. # if defined(KWSYS_IOS_HAS_OSTREAM_LONG_LONG)
  137. # define iostreamLongLong(x) (x)
  138. # else
  139. # define iostreamLongLong(x) ((long)x)
  140. # endif
  141. #elif defined(KWSYS_USE___INT64)
  142. # if defined(KWSYS_IOS_HAS_OSTREAM___INT64)
  143. # define iostreamLongLong(x) (x)
  144. # else
  145. # define iostreamLongLong(x) ((long)x)
  146. # endif
  147. #else
  148. # error "No Long Long"
  149. #endif
  150. #if defined(KWSYS_CXX_HAS_ATOLL)
  151. # define atoLongLong atoll
  152. #else
  153. # if defined(KWSYS_CXX_HAS__ATOI64)
  154. # define atoLongLong _atoi64
  155. # elif defined(KWSYS_CXX_HAS_ATOL)
  156. # define atoLongLong atol
  157. # else
  158. # define atoLongLong atoi
  159. # endif
  160. #endif
  161. namespace KWSYS_NAMESPACE
  162. {
  163. template<typename T>
  164. T min(T a, T b){ return a<b ? a : b; }
  165. extern "C" { typedef void (*SigAction)(int,siginfo_t*,void*); }
  166. // Define SystemInformationImplementation class
  167. typedef void (*DELAY_FUNC)(unsigned int uiMS);
  168. class SystemInformationImplementation
  169. {
  170. public:
  171. typedef SystemInformation::LongLong LongLong;
  172. SystemInformationImplementation ();
  173. ~SystemInformationImplementation ();
  174. const char * GetVendorString();
  175. const char * GetVendorID();
  176. kwsys_stl::string GetTypeID();
  177. kwsys_stl::string GetFamilyID();
  178. kwsys_stl::string GetModelID();
  179. kwsys_stl::string GetModelName();
  180. kwsys_stl::string GetSteppingCode();
  181. const char * GetExtendedProcessorName();
  182. const char * GetProcessorSerialNumber();
  183. int GetProcessorCacheSize();
  184. unsigned int GetLogicalProcessorsPerPhysical();
  185. float GetProcessorClockFrequency();
  186. int GetProcessorAPICID();
  187. int GetProcessorCacheXSize(long int);
  188. bool DoesCPUSupportFeature(long int);
  189. const char * GetOSName();
  190. const char * GetHostname();
  191. int GetFullyQualifiedDomainName(kwsys_stl::string &fqdn);
  192. const char * GetOSRelease();
  193. const char * GetOSVersion();
  194. const char * GetOSPlatform();
  195. bool Is64Bits();
  196. unsigned int GetNumberOfLogicalCPU(); // per physical cpu
  197. unsigned int GetNumberOfPhysicalCPU();
  198. bool DoesCPUSupportCPUID();
  199. // Retrieve memory information in megabyte.
  200. size_t GetTotalVirtualMemory();
  201. size_t GetAvailableVirtualMemory();
  202. size_t GetTotalPhysicalMemory();
  203. size_t GetAvailablePhysicalMemory();
  204. LongLong GetProcessId();
  205. // Retrieve memory information in kib
  206. LongLong GetHostMemoryTotal();
  207. LongLong GetHostMemoryAvailable(const char *envVarName);
  208. LongLong GetHostMemoryUsed();
  209. LongLong GetProcMemoryAvailable(
  210. const char *hostLimitEnvVarName,
  211. const char *procLimitEnvVarName);
  212. LongLong GetProcMemoryUsed();
  213. // enable/disable stack trace signal handler.
  214. static
  215. void SetStackTraceOnError(int enable);
  216. /** Run the different checks */
  217. void RunCPUCheck();
  218. void RunOSCheck();
  219. void RunMemoryCheck();
  220. public:
  221. typedef struct tagID
  222. {
  223. int Type;
  224. int Family;
  225. int Model;
  226. int Revision;
  227. int ExtendedFamily;
  228. int ExtendedModel;
  229. kwsys_stl::string ProcessorName;
  230. kwsys_stl::string Vendor;
  231. kwsys_stl::string SerialNumber;
  232. kwsys_stl::string ModelName;
  233. } ID;
  234. typedef struct tagCPUPowerManagement
  235. {
  236. bool HasVoltageID;
  237. bool HasFrequencyID;
  238. bool HasTempSenseDiode;
  239. } CPUPowerManagement;
  240. typedef struct tagCPUExtendedFeatures
  241. {
  242. bool Has3DNow;
  243. bool Has3DNowPlus;
  244. bool SupportsMP;
  245. bool HasMMXPlus;
  246. bool HasSSEMMX;
  247. bool SupportsHyperthreading;
  248. unsigned int LogicalProcessorsPerPhysical;
  249. int APIC_ID;
  250. CPUPowerManagement PowerManagement;
  251. } CPUExtendedFeatures;
  252. typedef struct CPUtagFeatures
  253. {
  254. bool HasFPU;
  255. bool HasTSC;
  256. bool HasMMX;
  257. bool HasSSE;
  258. bool HasSSEFP;
  259. bool HasSSE2;
  260. bool HasIA64;
  261. bool HasAPIC;
  262. bool HasCMOV;
  263. bool HasMTRR;
  264. bool HasACPI;
  265. bool HasSerial;
  266. bool HasThermal;
  267. int CPUSpeed;
  268. int L1CacheSize;
  269. int L2CacheSize;
  270. int L3CacheSize;
  271. CPUExtendedFeatures ExtendedFeatures;
  272. } CPUFeatures;
  273. enum Manufacturer
  274. {
  275. AMD, Intel, NSC, UMC, Cyrix, NexGen, IDT, Rise, Transmeta, Sun, IBM,
  276. Motorola, UnknownManufacturer
  277. };
  278. protected:
  279. // Functions.
  280. bool RetrieveCPUFeatures();
  281. bool RetrieveCPUIdentity();
  282. bool RetrieveCPUCacheDetails();
  283. bool RetrieveClassicalCPUCacheDetails();
  284. bool RetrieveCPUClockSpeed();
  285. bool RetrieveClassicalCPUClockSpeed();
  286. bool RetrieveCPUExtendedLevelSupport(int);
  287. bool RetrieveExtendedCPUFeatures();
  288. bool RetrieveProcessorSerialNumber();
  289. bool RetrieveCPUPowerManagement();
  290. bool RetrieveClassicalCPUIdentity();
  291. bool RetrieveExtendedCPUIdentity();
  292. Manufacturer ChipManufacturer;
  293. CPUFeatures Features;
  294. ID ChipID;
  295. float CPUSpeedInMHz;
  296. unsigned int NumberOfLogicalCPU;
  297. unsigned int NumberOfPhysicalCPU;
  298. int CPUCount();
  299. unsigned char LogicalCPUPerPhysicalCPU();
  300. unsigned char GetAPICId();
  301. unsigned int IsHyperThreadingSupported();
  302. LongLong GetCyclesDifference(DELAY_FUNC, unsigned int);
  303. // For Linux and Cygwin, /proc/cpuinfo formats are slightly different
  304. int RetreiveInformationFromCpuInfoFile();
  305. kwsys_stl::string ExtractValueFromCpuInfoFile(kwsys_stl::string buffer,
  306. const char* word, size_t init=0);
  307. static void Delay (unsigned int);
  308. static void DelayOverhead (unsigned int);
  309. void FindManufacturer();
  310. // For Mac
  311. bool ParseSysCtl();
  312. int CallSwVers(const char *arg, kwsys_stl::string &ver);
  313. void TrimNewline(kwsys_stl::string&);
  314. kwsys_stl::string ExtractValueFromSysCtl(const char* word);
  315. kwsys_stl::string SysCtlBuffer;
  316. // For Solaris
  317. bool QuerySolarisInfo();
  318. kwsys_stl::string ParseValueFromKStat(const char* arguments);
  319. kwsys_stl::string RunProcess(kwsys_stl::vector<const char*> args);
  320. //For Haiku OS
  321. bool QueryHaikuInfo();
  322. //For QNX
  323. bool QueryQNXMemory();
  324. bool QueryQNXProcessor();
  325. // Evaluate the memory information.
  326. int QueryMemory();
  327. size_t TotalVirtualMemory;
  328. size_t AvailableVirtualMemory;
  329. size_t TotalPhysicalMemory;
  330. size_t AvailablePhysicalMemory;
  331. size_t CurrentPositionInFile;
  332. // Operating System information
  333. bool QueryOSInformation();
  334. kwsys_stl::string OSName;
  335. kwsys_stl::string Hostname;
  336. kwsys_stl::string OSRelease;
  337. kwsys_stl::string OSVersion;
  338. kwsys_stl::string OSPlatform;
  339. };
  340. SystemInformation::SystemInformation()
  341. {
  342. this->Implementation = new SystemInformationImplementation;
  343. }
  344. SystemInformation::~SystemInformation()
  345. {
  346. delete this->Implementation;
  347. }
  348. const char * SystemInformation::GetVendorString()
  349. {
  350. return this->Implementation->GetVendorString();
  351. }
  352. const char * SystemInformation::GetVendorID()
  353. {
  354. return this->Implementation->GetVendorID();
  355. }
  356. kwsys_stl::string SystemInformation::GetTypeID()
  357. {
  358. return this->Implementation->GetTypeID();
  359. }
  360. kwsys_stl::string SystemInformation::GetFamilyID()
  361. {
  362. return this->Implementation->GetFamilyID();
  363. }
  364. kwsys_stl::string SystemInformation::GetModelID()
  365. {
  366. return this->Implementation->GetModelID();
  367. }
  368. kwsys_stl::string SystemInformation::GetModelName()
  369. {
  370. return this->Implementation->GetModelName();
  371. }
  372. kwsys_stl::string SystemInformation::GetSteppingCode()
  373. {
  374. return this->Implementation->GetSteppingCode();
  375. }
  376. const char * SystemInformation::GetExtendedProcessorName()
  377. {
  378. return this->Implementation->GetExtendedProcessorName();
  379. }
  380. const char * SystemInformation::GetProcessorSerialNumber()
  381. {
  382. return this->Implementation->GetProcessorSerialNumber();
  383. }
  384. int SystemInformation::GetProcessorCacheSize()
  385. {
  386. return this->Implementation->GetProcessorCacheSize();
  387. }
  388. unsigned int SystemInformation::GetLogicalProcessorsPerPhysical()
  389. {
  390. return this->Implementation->GetLogicalProcessorsPerPhysical();
  391. }
  392. float SystemInformation::GetProcessorClockFrequency()
  393. {
  394. return this->Implementation->GetProcessorClockFrequency();
  395. }
  396. int SystemInformation::GetProcessorAPICID()
  397. {
  398. return this->Implementation->GetProcessorAPICID();
  399. }
  400. int SystemInformation::GetProcessorCacheXSize(long int l)
  401. {
  402. return this->Implementation->GetProcessorCacheXSize(l);
  403. }
  404. bool SystemInformation::DoesCPUSupportFeature(long int i)
  405. {
  406. return this->Implementation->DoesCPUSupportFeature(i);
  407. }
  408. kwsys_stl::string SystemInformation::GetCPUDescription()
  409. {
  410. kwsys_ios::ostringstream oss;
  411. oss
  412. << this->GetNumberOfPhysicalCPU()
  413. << " core ";
  414. if (this->GetModelName().empty())
  415. {
  416. oss
  417. << this->GetProcessorClockFrequency()
  418. << " MHz "
  419. << this->GetVendorString()
  420. << " "
  421. << this->GetExtendedProcessorName();
  422. }
  423. else
  424. {
  425. oss << this->GetModelName();
  426. }
  427. // remove extra spaces
  428. kwsys_stl::string tmp=oss.str();
  429. size_t pos;
  430. while( (pos=tmp.find(" "))!=kwsys_stl::string::npos)
  431. {
  432. tmp.replace(pos,2," ");
  433. }
  434. return tmp;
  435. }
  436. const char * SystemInformation::GetOSName()
  437. {
  438. return this->Implementation->GetOSName();
  439. }
  440. const char * SystemInformation::GetHostname()
  441. {
  442. return this->Implementation->GetHostname();
  443. }
  444. kwsys_stl::string SystemInformation::GetFullyQualifiedDomainName()
  445. {
  446. kwsys_stl::string fqdn;
  447. this->Implementation->GetFullyQualifiedDomainName(fqdn);
  448. return fqdn;
  449. }
  450. const char * SystemInformation::GetOSRelease()
  451. {
  452. return this->Implementation->GetOSRelease();
  453. }
  454. const char * SystemInformation::GetOSVersion()
  455. {
  456. return this->Implementation->GetOSVersion();
  457. }
  458. const char * SystemInformation::GetOSPlatform()
  459. {
  460. return this->Implementation->GetOSPlatform();
  461. }
  462. int SystemInformation::GetOSIsWindows()
  463. {
  464. #if defined(_WIN32)
  465. return 1;
  466. #else
  467. return 0;
  468. #endif
  469. }
  470. int SystemInformation::GetOSIsLinux()
  471. {
  472. #if defined(__linux)
  473. return 1;
  474. #else
  475. return 0;
  476. #endif
  477. }
  478. int SystemInformation::GetOSIsApple()
  479. {
  480. #if defined(__APPLE__)
  481. return 1;
  482. #else
  483. return 0;
  484. #endif
  485. }
  486. kwsys_stl::string SystemInformation::GetOSDescription()
  487. {
  488. kwsys_ios::ostringstream oss;
  489. oss
  490. << this->GetOSName()
  491. << " "
  492. << this->GetOSRelease()
  493. << " "
  494. << this->GetOSVersion();
  495. return oss.str();
  496. }
  497. bool SystemInformation::Is64Bits()
  498. {
  499. return this->Implementation->Is64Bits();
  500. }
  501. unsigned int SystemInformation::GetNumberOfLogicalCPU() // per physical cpu
  502. {
  503. return this->Implementation->GetNumberOfLogicalCPU();
  504. }
  505. unsigned int SystemInformation::GetNumberOfPhysicalCPU()
  506. {
  507. return this->Implementation->GetNumberOfPhysicalCPU();
  508. }
  509. bool SystemInformation::DoesCPUSupportCPUID()
  510. {
  511. return this->Implementation->DoesCPUSupportCPUID();
  512. }
  513. // Retrieve memory information in megabyte.
  514. size_t SystemInformation::GetTotalVirtualMemory()
  515. {
  516. return this->Implementation->GetTotalVirtualMemory();
  517. }
  518. size_t SystemInformation::GetAvailableVirtualMemory()
  519. {
  520. return this->Implementation->GetAvailableVirtualMemory();
  521. }
  522. size_t SystemInformation::GetTotalPhysicalMemory()
  523. {
  524. return this->Implementation->GetTotalPhysicalMemory();
  525. }
  526. size_t SystemInformation::GetAvailablePhysicalMemory()
  527. {
  528. return this->Implementation->GetAvailablePhysicalMemory();
  529. }
  530. kwsys_stl::string SystemInformation::GetMemoryDescription(
  531. const char *hostLimitEnvVarName,
  532. const char *procLimitEnvVarName)
  533. {
  534. kwsys_ios::ostringstream oss;
  535. oss
  536. << "Host Total: "
  537. << iostreamLongLong(this->GetHostMemoryTotal())
  538. << " KiB, Host Available: "
  539. << iostreamLongLong(this->GetHostMemoryAvailable(hostLimitEnvVarName))
  540. << " KiB, Process Available: "
  541. << iostreamLongLong(
  542. this->GetProcMemoryAvailable(hostLimitEnvVarName,procLimitEnvVarName))
  543. << " KiB";
  544. return oss.str();
  545. }
  546. // host memory info in units of KiB.
  547. SystemInformation::LongLong SystemInformation::GetHostMemoryTotal()
  548. {
  549. return this->Implementation->GetHostMemoryTotal();
  550. }
  551. SystemInformation::LongLong
  552. SystemInformation::GetHostMemoryAvailable(const char *hostLimitEnvVarName)
  553. {
  554. return this->Implementation->GetHostMemoryAvailable(hostLimitEnvVarName);
  555. }
  556. SystemInformation::LongLong SystemInformation::GetHostMemoryUsed()
  557. {
  558. return this->Implementation->GetHostMemoryUsed();
  559. }
  560. // process memory info in units of KiB.
  561. SystemInformation::LongLong
  562. SystemInformation::GetProcMemoryAvailable(
  563. const char *hostLimitEnvVarName,
  564. const char *procLimitEnvVarName)
  565. {
  566. return this->Implementation->GetProcMemoryAvailable(
  567. hostLimitEnvVarName,
  568. procLimitEnvVarName);
  569. }
  570. SystemInformation::LongLong SystemInformation::GetProcMemoryUsed()
  571. {
  572. return this->Implementation->GetProcMemoryUsed();
  573. }
  574. SystemInformation::LongLong SystemInformation::GetProcessId()
  575. {
  576. return this->Implementation->GetProcessId();
  577. }
  578. void SystemInformation::SetStackTraceOnError(int enable)
  579. {
  580. SystemInformationImplementation::SetStackTraceOnError(enable);
  581. }
  582. /** Run the different checks */
  583. void SystemInformation::RunCPUCheck()
  584. {
  585. this->Implementation->RunCPUCheck();
  586. }
  587. void SystemInformation::RunOSCheck()
  588. {
  589. this->Implementation->RunOSCheck();
  590. }
  591. void SystemInformation::RunMemoryCheck()
  592. {
  593. this->Implementation->RunMemoryCheck();
  594. }
  595. // --------------------------------------------------------------
  596. // SystemInformationImplementation starts here
  597. #if defined(_MSC_VER) && (_MSC_VER >= 1300) && !defined(_WIN64)
  598. #define USE_ASM_INSTRUCTIONS 1
  599. #else
  600. #define USE_ASM_INSTRUCTIONS 0
  601. #endif
  602. #define STORE_TLBCACHE_INFO(x,y) x = (x < y) ? y : x
  603. #define TLBCACHE_INFO_UNITS (15)
  604. #define CLASSICAL_CPU_FREQ_LOOP 10000000
  605. #define RDTSC_INSTRUCTION _asm _emit 0x0f _asm _emit 0x31
  606. #define CPUID_AWARE_COMPILER
  607. #ifdef CPUID_AWARE_COMPILER
  608. #define CPUID_INSTRUCTION cpuid
  609. #else
  610. #define CPUID_INSTRUCTION _asm _emit 0x0f _asm _emit 0xa2
  611. #endif
  612. #define MMX_FEATURE 0x00000001
  613. #define MMX_PLUS_FEATURE 0x00000002
  614. #define SSE_FEATURE 0x00000004
  615. #define SSE2_FEATURE 0x00000008
  616. #define AMD_3DNOW_FEATURE 0x00000010
  617. #define AMD_3DNOW_PLUS_FEATURE 0x00000020
  618. #define IA64_FEATURE 0x00000040
  619. #define MP_CAPABLE 0x00000080
  620. #define HYPERTHREAD_FEATURE 0x00000100
  621. #define SERIALNUMBER_FEATURE 0x00000200
  622. #define APIC_FEATURE 0x00000400
  623. #define SSE_FP_FEATURE 0x00000800
  624. #define SSE_MMX_FEATURE 0x00001000
  625. #define CMOV_FEATURE 0x00002000
  626. #define MTRR_FEATURE 0x00004000
  627. #define L1CACHE_FEATURE 0x00008000
  628. #define L2CACHE_FEATURE 0x00010000
  629. #define L3CACHE_FEATURE 0x00020000
  630. #define ACPI_FEATURE 0x00040000
  631. #define THERMALMONITOR_FEATURE 0x00080000
  632. #define TEMPSENSEDIODE_FEATURE 0x00100000
  633. #define FREQUENCYID_FEATURE 0x00200000
  634. #define VOLTAGEID_FREQUENCY 0x00400000
  635. // Status Flag
  636. #define HT_NOT_CAPABLE 0
  637. #define HT_ENABLED 1
  638. #define HT_DISABLED 2
  639. #define HT_SUPPORTED_NOT_ENABLED 3
  640. #define HT_CANNOT_DETECT 4
  641. // EDX[28] Bit 28 is set if HT is supported
  642. #define HT_BIT 0x10000000
  643. // EAX[11:8] Bit 8-11 contains family processor ID.
  644. #define FAMILY_ID 0x0F00
  645. #define PENTIUM4_ID 0x0F00
  646. // EAX[23:20] Bit 20-23 contains extended family processor ID
  647. #define EXT_FAMILY_ID 0x0F00000
  648. // EBX[23:16] Bit 16-23 in ebx contains the number of logical
  649. #define NUM_LOGICAL_BITS 0x00FF0000
  650. // processors per physical processor when execute cpuid with
  651. // eax set to 1
  652. // EBX[31:24] Bits 24-31 (8 bits) return the 8-bit unique
  653. #define INITIAL_APIC_ID_BITS 0xFF000000
  654. // initial APIC ID for the processor this code is running on.
  655. // Default value = 0xff if HT is not supported
  656. // Hide implementation details in an anonymous namespace.
  657. namespace {
  658. // *****************************************************************************
  659. #if defined(__linux) || defined(__APPLE__)
  660. int LoadLines(
  661. FILE *file,
  662. kwsys_stl::vector<kwsys_stl::string> &lines)
  663. {
  664. // Load each line in the given file into a the vector.
  665. int nRead=0;
  666. const int bufSize=1024;
  667. char buf[bufSize]={'\0'};
  668. while (!feof(file) && !ferror(file))
  669. {
  670. errno=0;
  671. if (fgets(buf,bufSize,file) == 0)
  672. {
  673. if (ferror(file) && (errno==EINTR))
  674. {
  675. clearerr(file);
  676. }
  677. continue;
  678. }
  679. lines.push_back(buf);
  680. ++nRead;
  681. }
  682. if (ferror(file))
  683. {
  684. return 0;
  685. }
  686. return nRead;
  687. }
  688. # if defined(__linux)
  689. // *****************************************************************************
  690. int LoadLines(
  691. const char *fileName,
  692. kwsys_stl::vector<kwsys_stl::string> &lines)
  693. {
  694. FILE *file=fopen(fileName,"r");
  695. if (file==0)
  696. {
  697. return 0;
  698. }
  699. int nRead=LoadLines(file,lines);
  700. fclose(file);
  701. return nRead;
  702. }
  703. # endif
  704. // ****************************************************************************
  705. template<typename T>
  706. int NameValue(
  707. kwsys_stl::vector<kwsys_stl::string> &lines,
  708. kwsys_stl::string name, T &value)
  709. {
  710. size_t nLines=lines.size();
  711. for (size_t i=0; i<nLines; ++i)
  712. {
  713. size_t at=lines[i].find(name);
  714. if (at==kwsys_stl::string::npos)
  715. {
  716. continue;
  717. }
  718. kwsys_ios::istringstream is(lines[i].substr(at+name.size()));
  719. is >> value;
  720. return 0;
  721. }
  722. return -1;
  723. }
  724. #endif
  725. #if defined(__linux)
  726. // ****************************************************************************
  727. template<typename T>
  728. int GetFieldsFromFile(
  729. const char *fileName,
  730. const char **fieldNames,
  731. T *values)
  732. {
  733. kwsys_stl::vector<kwsys_stl::string> fields;
  734. if (!LoadLines(fileName,fields))
  735. {
  736. return -1;
  737. }
  738. int i=0;
  739. while (fieldNames[i]!=NULL)
  740. {
  741. int ierr=NameValue(fields,fieldNames[i],values[i]);
  742. if (ierr)
  743. {
  744. return -(i+2);
  745. }
  746. i+=1;
  747. }
  748. return 0;
  749. }
  750. // ****************************************************************************
  751. template<typename T>
  752. int GetFieldFromFile(
  753. const char *fileName,
  754. const char *fieldName,
  755. T &value)
  756. {
  757. const char *fieldNames[2]={fieldName,NULL};
  758. T values[1]={T(0)};
  759. int ierr=GetFieldsFromFile(fileName,fieldNames,values);
  760. if (ierr)
  761. {
  762. return ierr;
  763. }
  764. value=values[0];
  765. return 0;
  766. }
  767. #endif
  768. // ****************************************************************************
  769. #if defined(__APPLE__)
  770. template<typename T>
  771. int GetFieldsFromCommand(
  772. const char *command,
  773. const char **fieldNames,
  774. T *values)
  775. {
  776. FILE *file=popen(command,"r");
  777. if (file==0)
  778. {
  779. return -1;
  780. }
  781. kwsys_stl::vector<kwsys_stl::string> fields;
  782. int nl=LoadLines(file,fields);
  783. pclose(file);
  784. if (nl==0)
  785. {
  786. return -1;
  787. }
  788. int i=0;
  789. while (fieldNames[i]!=NULL)
  790. {
  791. int ierr=NameValue(fields,fieldNames[i],values[i]);
  792. if (ierr)
  793. {
  794. return -(i+2);
  795. }
  796. i+=1;
  797. }
  798. return 0;
  799. }
  800. #endif
  801. // ****************************************************************************
  802. #if !defined(_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
  803. void StacktraceSignalHandler(
  804. int sigNo,
  805. siginfo_t *sigInfo,
  806. void * /*sigContext*/)
  807. {
  808. #if defined(__linux) || defined(__APPLE__)
  809. kwsys_ios::ostringstream oss;
  810. oss
  811. << "=========================================================" << kwsys_ios::endl
  812. << "Process id " << getpid() << " ";
  813. switch (sigNo)
  814. {
  815. case SIGFPE:
  816. oss << "Caught SIGFPE ";
  817. switch (sigInfo->si_code)
  818. {
  819. # if defined(FPE_INTDIV)
  820. case FPE_INTDIV:
  821. oss << "integer division by zero";
  822. break;
  823. # endif
  824. # if defined(FPE_INTOVF)
  825. case FPE_INTOVF:
  826. oss << "integer overflow";
  827. break;
  828. # endif
  829. case FPE_FLTDIV:
  830. oss << "floating point divide by zero";
  831. break;
  832. case FPE_FLTOVF:
  833. oss << "floating point overflow";
  834. break;
  835. case FPE_FLTUND:
  836. oss << "floating point underflow";
  837. break;
  838. case FPE_FLTRES:
  839. oss << "floating point inexact result";
  840. break;
  841. case FPE_FLTINV:
  842. oss << "floating point invalid operation";
  843. break;
  844. #if defined(FPE_FLTSUB)
  845. case FPE_FLTSUB:
  846. oss << "floating point subscript out of range";
  847. break;
  848. #endif
  849. default:
  850. oss << "code " << sigInfo->si_code;
  851. break;
  852. }
  853. break;
  854. case SIGSEGV:
  855. oss << "Caught SIGSEGV ";
  856. switch (sigInfo->si_code)
  857. {
  858. case SEGV_MAPERR:
  859. oss << "address not mapped to object";
  860. break;
  861. case SEGV_ACCERR:
  862. oss << "invalid permission for mapped object";
  863. break;
  864. default:
  865. oss << "code " << sigInfo->si_code;
  866. break;
  867. }
  868. break;
  869. case SIGINT:
  870. oss << "Caught SIGTERM";
  871. break;
  872. case SIGTERM:
  873. oss << "Caught SIGTERM";
  874. break;
  875. case SIGBUS:
  876. oss << "Caught SIGBUS type ";
  877. switch (sigInfo->si_code)
  878. {
  879. case BUS_ADRALN:
  880. oss << "invalid address alignment";
  881. break;
  882. # if defined(BUS_ADRERR)
  883. case BUS_ADRERR:
  884. oss << "non-exestent physical address";
  885. break;
  886. # endif
  887. # if defined(BUS_OBJERR)
  888. case BUS_OBJERR:
  889. oss << "object specific hardware error";
  890. break;
  891. # endif
  892. default:
  893. oss << "code " << sigInfo->si_code;
  894. break;
  895. }
  896. break;
  897. case SIGILL:
  898. oss << "Caught SIGILL ";
  899. switch (sigInfo->si_code)
  900. {
  901. case ILL_ILLOPC:
  902. oss << "illegal opcode";
  903. break;
  904. # if defined(ILL_ILLOPN)
  905. case ILL_ILLOPN:
  906. oss << "illegal operand";
  907. break;
  908. # endif
  909. # if defined(ILL_ILLADR)
  910. case ILL_ILLADR:
  911. oss << "illegal addressing mode.";
  912. break;
  913. # endif
  914. case ILL_ILLTRP:
  915. oss << "illegal trap";
  916. case ILL_PRVOPC:
  917. oss << "privileged opcode";
  918. break;
  919. # if defined(ILL_PRVREG)
  920. case ILL_PRVREG:
  921. oss << "privileged register";
  922. break;
  923. # endif
  924. # if defined(ILL_COPROC)
  925. case ILL_COPROC:
  926. oss << "co-processor error";
  927. break;
  928. # endif
  929. # if defined(ILL_BADSTK)
  930. case ILL_BADSTK:
  931. oss << "internal stack error";
  932. break;
  933. # endif
  934. default:
  935. oss << "code " << sigInfo->si_code;
  936. break;
  937. }
  938. break;
  939. default:
  940. oss << "Caught " << sigNo << " code " << sigInfo->si_code;
  941. break;
  942. }
  943. oss << kwsys_ios::endl;
  944. #if defined(KWSYS_SYSTEMINFORMATION_HAVE_BACKTRACE)
  945. oss << "Program Stack:" << kwsys_ios::endl;
  946. void *stackSymbols[128];
  947. int n=backtrace(stackSymbols,128);
  948. char **stackText=backtrace_symbols(stackSymbols,n);
  949. for (int i=0; i<n; ++i)
  950. {
  951. oss << " " << stackText[i] << kwsys_ios::endl;
  952. }
  953. #endif
  954. oss
  955. << "=========================================================" << kwsys_ios::endl;
  956. kwsys_ios::cerr << oss.str() << kwsys_ios::endl;
  957. abort();
  958. #else
  959. // avoid warning C4100
  960. (void)sigNo;
  961. (void)sigInfo;
  962. #endif
  963. }
  964. #endif
  965. } // anonymous namespace
  966. SystemInformationImplementation::SystemInformationImplementation()
  967. {
  968. this->TotalVirtualMemory = 0;
  969. this->AvailableVirtualMemory = 0;
  970. this->TotalPhysicalMemory = 0;
  971. this->AvailablePhysicalMemory = 0;
  972. this->CurrentPositionInFile = 0;
  973. this->ChipManufacturer = UnknownManufacturer;
  974. memset(&this->Features, 0, sizeof(CPUFeatures));
  975. this->ChipID.Type = 0;
  976. this->ChipID.Family = 0;
  977. this->ChipID.Model = 0;
  978. this->ChipID.Revision = 0;
  979. this->ChipID.ExtendedFamily = 0;
  980. this->ChipID.ExtendedModel = 0;
  981. this->CPUSpeedInMHz = 0;
  982. this->NumberOfLogicalCPU = 0;
  983. this->NumberOfPhysicalCPU = 0;
  984. this->OSName = "";
  985. this->Hostname = "";
  986. this->OSRelease = "";
  987. this->OSVersion = "";
  988. this->OSPlatform = "";
  989. }
  990. SystemInformationImplementation::~SystemInformationImplementation()
  991. {
  992. }
  993. void SystemInformationImplementation::RunCPUCheck()
  994. {
  995. #ifdef WIN32
  996. // Check to see if this processor supports CPUID.
  997. bool supportsCPUID = DoesCPUSupportCPUID();
  998. if (supportsCPUID)
  999. {
  1000. // Retrieve the CPU details.
  1001. RetrieveCPUIdentity();
  1002. RetrieveCPUFeatures();
  1003. }
  1004. // These two may be called without support for the CPUID instruction.
  1005. // (But if the instruction is there, they should be called *after*
  1006. // the above call to RetrieveCPUIdentity... that's why the two if
  1007. // blocks exist with the same "if (supportsCPUID)" logic...
  1008. //
  1009. if (!RetrieveCPUClockSpeed())
  1010. {
  1011. RetrieveClassicalCPUClockSpeed();
  1012. }
  1013. if (supportsCPUID)
  1014. {
  1015. // Retrieve cache information.
  1016. if (!RetrieveCPUCacheDetails())
  1017. {
  1018. RetrieveClassicalCPUCacheDetails();
  1019. }
  1020. // Retrieve the extended CPU details.
  1021. if (!RetrieveExtendedCPUIdentity())
  1022. {
  1023. RetrieveClassicalCPUIdentity();
  1024. }
  1025. RetrieveExtendedCPUFeatures();
  1026. RetrieveCPUPowerManagement();
  1027. // Now attempt to retrieve the serial number (if possible).
  1028. RetrieveProcessorSerialNumber();
  1029. }
  1030. this->CPUCount();
  1031. #elif defined(__APPLE__)
  1032. this->ParseSysCtl();
  1033. #elif defined (__SVR4) && defined (__sun)
  1034. this->QuerySolarisInfo();
  1035. #elif defined(__HAIKU__)
  1036. this->QueryHaikuInfo();
  1037. #elif defined(__QNX__)
  1038. this->QueryQNXProcessor();
  1039. #else
  1040. this->RetreiveInformationFromCpuInfoFile();
  1041. #endif
  1042. }
  1043. void SystemInformationImplementation::RunOSCheck()
  1044. {
  1045. this->QueryOSInformation();
  1046. }
  1047. void SystemInformationImplementation::RunMemoryCheck()
  1048. {
  1049. #if defined(__APPLE__)
  1050. this->ParseSysCtl();
  1051. #elif defined (__SVR4) && defined (__sun)
  1052. this->QuerySolarisInfo();
  1053. #elif defined(__HAIKU__)
  1054. this->QueryHaikuInfo();
  1055. #elif defined(__QNX__)
  1056. this->QueryQNXMemory();
  1057. #else
  1058. this->QueryMemory();
  1059. #endif
  1060. }
  1061. /** Get the vendor string */
  1062. const char * SystemInformationImplementation::GetVendorString()
  1063. {
  1064. return this->ChipID.Vendor.c_str();
  1065. }
  1066. /** Get the OS Name */
  1067. const char * SystemInformationImplementation::GetOSName()
  1068. {
  1069. return this->OSName.c_str();
  1070. }
  1071. /** Get the hostname */
  1072. const char* SystemInformationImplementation::GetHostname()
  1073. {
  1074. if (this->Hostname.empty())
  1075. {
  1076. this->Hostname="localhost";
  1077. #if defined(_WIN32)
  1078. WORD wVersionRequested;
  1079. WSADATA wsaData;
  1080. char name[255];
  1081. wVersionRequested = MAKEWORD(2,0);
  1082. if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
  1083. {
  1084. gethostname(name,sizeof(name));
  1085. WSACleanup( );
  1086. }
  1087. this->Hostname = name;
  1088. #else
  1089. struct utsname unameInfo;
  1090. int errorFlag = uname(&unameInfo);
  1091. if(errorFlag == 0)
  1092. {
  1093. this->Hostname = unameInfo.nodename;
  1094. }
  1095. #endif
  1096. }
  1097. return this->Hostname.c_str();
  1098. }
  1099. /** Get the FQDN */
  1100. int SystemInformationImplementation::GetFullyQualifiedDomainName(
  1101. kwsys_stl::string &fqdn)
  1102. {
  1103. // in the event of absolute failure return localhost.
  1104. fqdn="localhost";
  1105. #if defined(_WIN32)
  1106. int ierr;
  1107. // TODO - a more robust implementation for windows, see comments
  1108. // in unix implementation.
  1109. WSADATA wsaData;
  1110. WORD ver=MAKEWORD(2,0);
  1111. ierr=WSAStartup(ver,&wsaData);
  1112. if (ierr)
  1113. {
  1114. return -1;
  1115. }
  1116. char base[256]={'\0'};
  1117. ierr=gethostname(base,256);
  1118. if (ierr)
  1119. {
  1120. WSACleanup();
  1121. return -2;
  1122. }
  1123. fqdn=base;
  1124. HOSTENT *hent=gethostbyname(base);
  1125. if (hent)
  1126. {
  1127. fqdn=hent->h_name;
  1128. }
  1129. WSACleanup();
  1130. return 0;
  1131. #elif defined(KWSYS_SYSTEMINFORMATION_IMPLEMENT_FQDN)
  1132. // gethostname typical returns an alias for loopback interface
  1133. // we want the fully qualified domain name. Because there are
  1134. // any number of interfaces on this system we look for the
  1135. // first of these that contains the name returned by gethostname
  1136. // and is longer. failing that we return gethostname and indicate
  1137. // with a failure code. Return of a failure code is not necessarilly
  1138. // an indication of an error. for instance gethostname may return
  1139. // the fully qualified domain name, or there may not be one if the
  1140. // system lives on a private network such as in the case of a cluster
  1141. // node.
  1142. int ierr=0;
  1143. char base[NI_MAXHOST];
  1144. ierr=gethostname(base,NI_MAXHOST);
  1145. if (ierr)
  1146. {
  1147. return -1;
  1148. }
  1149. size_t baseSize=strlen(base);
  1150. fqdn=base;
  1151. struct ifaddrs *ifas;
  1152. struct ifaddrs *ifa;
  1153. ierr=getifaddrs(&ifas);
  1154. if (ierr)
  1155. {
  1156. return -2;
  1157. }
  1158. for (ifa=ifas; ifa!=NULL; ifa=ifa->ifa_next)
  1159. {
  1160. int fam = ifa->ifa_addr? ifa->ifa_addr->sa_family : -1;
  1161. if ((fam==AF_INET) || (fam==AF_INET6))
  1162. {
  1163. char host[NI_MAXHOST]={'\0'};
  1164. int addrlen
  1165. = (fam==AF_INET?sizeof(struct sockaddr_in):sizeof(struct sockaddr_in6));
  1166. ierr=getnameinfo(
  1167. ifa->ifa_addr,
  1168. addrlen,
  1169. host,
  1170. NI_MAXHOST,
  1171. NULL,
  1172. 0,
  1173. NI_NAMEREQD);
  1174. if (ierr)
  1175. {
  1176. // don't report the failure now since we may succeed on another
  1177. // interface. If all attempts fail then return the failure code.
  1178. ierr=-3;
  1179. continue;
  1180. }
  1181. kwsys_stl::string candidate=host;
  1182. if ((candidate.find(base)!=kwsys_stl::string::npos) && baseSize<candidate.size())
  1183. {
  1184. // success, stop now.
  1185. ierr=0;
  1186. fqdn=candidate;
  1187. break;
  1188. }
  1189. }
  1190. }
  1191. freeifaddrs(ifas);
  1192. return ierr;
  1193. #else
  1194. /* TODO: Implement on more platforms. */
  1195. fqdn=this->GetHostname();
  1196. return -1;
  1197. #endif
  1198. }
  1199. /** Get the OS release */
  1200. const char* SystemInformationImplementation::GetOSRelease()
  1201. {
  1202. return this->OSRelease.c_str();
  1203. }
  1204. /** Get the OS version */
  1205. const char* SystemInformationImplementation::GetOSVersion()
  1206. {
  1207. return this->OSVersion.c_str();
  1208. }
  1209. /** Get the OS platform */
  1210. const char* SystemInformationImplementation::GetOSPlatform()
  1211. {
  1212. return this->OSPlatform.c_str();
  1213. }
  1214. /** Get the vendor ID */
  1215. const char * SystemInformationImplementation::GetVendorID()
  1216. {
  1217. // Return the vendor ID.
  1218. switch (this->ChipManufacturer)
  1219. {
  1220. case Intel:
  1221. return "Intel Corporation";
  1222. case AMD:
  1223. return "Advanced Micro Devices";
  1224. case NSC:
  1225. return "National Semiconductor";
  1226. case Cyrix:
  1227. return "Cyrix Corp., VIA Inc.";
  1228. case NexGen:
  1229. return "NexGen Inc., Advanced Micro Devices";
  1230. case IDT:
  1231. return "IDT\\Centaur, Via Inc.";
  1232. case UMC:
  1233. return "United Microelectronics Corp.";
  1234. case Rise:
  1235. return "Rise";
  1236. case Transmeta:
  1237. return "Transmeta";
  1238. case Sun:
  1239. return "Sun Microelectronics";
  1240. case IBM:
  1241. return "IBM";
  1242. case Motorola:
  1243. return "Motorola";
  1244. default:
  1245. return "Unknown Manufacturer";
  1246. }
  1247. }
  1248. /** Return the type ID of the CPU */
  1249. kwsys_stl::string SystemInformationImplementation::GetTypeID()
  1250. {
  1251. kwsys_ios::ostringstream str;
  1252. str << this->ChipID.Type;
  1253. return str.str();
  1254. }
  1255. /** Return the family of the CPU present */
  1256. kwsys_stl::string SystemInformationImplementation::GetFamilyID()
  1257. {
  1258. kwsys_ios::ostringstream str;
  1259. str << this->ChipID.Family;
  1260. return str.str();
  1261. }
  1262. // Return the model of CPU present */
  1263. kwsys_stl::string SystemInformationImplementation::GetModelID()
  1264. {
  1265. kwsys_ios::ostringstream str;
  1266. str << this->ChipID.Model;
  1267. return str.str();
  1268. }
  1269. // Return the model name of CPU present */
  1270. kwsys_stl::string SystemInformationImplementation::GetModelName()
  1271. {
  1272. return this->ChipID.ModelName;
  1273. }
  1274. /** Return the stepping code of the CPU present. */
  1275. kwsys_stl::string SystemInformationImplementation::GetSteppingCode()
  1276. {
  1277. kwsys_ios::ostringstream str;
  1278. str << this->ChipID.Revision;
  1279. return str.str();
  1280. }
  1281. /** Return the stepping code of the CPU present. */
  1282. const char * SystemInformationImplementation::GetExtendedProcessorName()
  1283. {
  1284. return this->ChipID.ProcessorName.c_str();
  1285. }
  1286. /** Return the serial number of the processor
  1287. * in hexadecimal: xxxx-xxxx-xxxx-xxxx-xxxx-xxxx. */
  1288. const char * SystemInformationImplementation::GetProcessorSerialNumber()
  1289. {
  1290. return this->ChipID.SerialNumber.c_str();
  1291. }
  1292. /** Return the logical processors per physical */
  1293. unsigned int SystemInformationImplementation::GetLogicalProcessorsPerPhysical()
  1294. {
  1295. return this->Features.ExtendedFeatures.LogicalProcessorsPerPhysical;
  1296. }
  1297. /** Return the processor clock frequency. */
  1298. float SystemInformationImplementation::GetProcessorClockFrequency()
  1299. {
  1300. return this->CPUSpeedInMHz;
  1301. }
  1302. /** Return the APIC ID. */
  1303. int SystemInformationImplementation::GetProcessorAPICID()
  1304. {
  1305. return this->Features.ExtendedFeatures.APIC_ID;
  1306. }
  1307. /** Return the L1 cache size. */
  1308. int SystemInformationImplementation::GetProcessorCacheSize()
  1309. {
  1310. return this->Features.L1CacheSize;
  1311. }
  1312. /** Return the chosen cache size. */
  1313. int SystemInformationImplementation::GetProcessorCacheXSize(long int dwCacheID)
  1314. {
  1315. switch (dwCacheID)
  1316. {
  1317. case L1CACHE_FEATURE:
  1318. return this->Features.L1CacheSize;
  1319. case L2CACHE_FEATURE:
  1320. return this->Features.L2CacheSize;
  1321. case L3CACHE_FEATURE:
  1322. return this->Features.L3CacheSize;
  1323. }
  1324. return -1;
  1325. }
  1326. bool SystemInformationImplementation::DoesCPUSupportFeature(long int dwFeature)
  1327. {
  1328. bool bHasFeature = false;
  1329. // Check for MMX instructions.
  1330. if (((dwFeature & MMX_FEATURE) != 0) && this->Features.HasMMX) bHasFeature = true;
  1331. // Check for MMX+ instructions.
  1332. if (((dwFeature & MMX_PLUS_FEATURE) != 0) && this->Features.ExtendedFeatures.HasMMXPlus) bHasFeature = true;
  1333. // Check for SSE FP instructions.
  1334. if (((dwFeature & SSE_FEATURE) != 0) && this->Features.HasSSE) bHasFeature = true;
  1335. // Check for SSE FP instructions.
  1336. if (((dwFeature & SSE_FP_FEATURE) != 0) && this->Features.HasSSEFP) bHasFeature = true;
  1337. // Check for SSE MMX instructions.
  1338. if (((dwFeature & SSE_MMX_FEATURE) != 0) && this->Features.ExtendedFeatures.HasSSEMMX) bHasFeature = true;
  1339. // Check for SSE2 instructions.
  1340. if (((dwFeature & SSE2_FEATURE) != 0) && this->Features.HasSSE2) bHasFeature = true;
  1341. // Check for 3DNow! instructions.
  1342. if (((dwFeature & AMD_3DNOW_FEATURE) != 0) && this->Features.ExtendedFeatures.Has3DNow) bHasFeature = true;
  1343. // Check for 3DNow+ instructions.
  1344. if (((dwFeature & AMD_3DNOW_PLUS_FEATURE) != 0) && this->Features.ExtendedFeatures.Has3DNowPlus) bHasFeature = true;
  1345. // Check for IA64 instructions.
  1346. if (((dwFeature & IA64_FEATURE) != 0) && this->Features.HasIA64) bHasFeature = true;
  1347. // Check for MP capable.
  1348. if (((dwFeature & MP_CAPABLE) != 0) && this->Features.ExtendedFeatures.SupportsMP) bHasFeature = true;
  1349. // Check for a serial number for the processor.
  1350. if (((dwFeature & SERIALNUMBER_FEATURE) != 0) && this->Features.HasSerial) bHasFeature = true;
  1351. // Check for a local APIC in the processor.
  1352. if (((dwFeature & APIC_FEATURE) != 0) && this->Features.HasAPIC) bHasFeature = true;
  1353. // Check for CMOV instructions.
  1354. if (((dwFeature & CMOV_FEATURE) != 0) && this->Features.HasCMOV) bHasFeature = true;
  1355. // Check for MTRR instructions.
  1356. if (((dwFeature & MTRR_FEATURE) != 0) && this->Features.HasMTRR) bHasFeature = true;
  1357. // Check for L1 cache size.
  1358. if (((dwFeature & L1CACHE_FEATURE) != 0) && (this->Features.L1CacheSize != -1)) bHasFeature = true;
  1359. // Check for L2 cache size.
  1360. if (((dwFeature & L2CACHE_FEATURE) != 0) && (this->Features.L2CacheSize != -1)) bHasFeature = true;
  1361. // Check for L3 cache size.
  1362. if (((dwFeature & L3CACHE_FEATURE) != 0) && (this->Features.L3CacheSize != -1)) bHasFeature = true;
  1363. // Check for ACPI capability.
  1364. if (((dwFeature & ACPI_FEATURE) != 0) && this->Features.HasACPI) bHasFeature = true;
  1365. // Check for thermal monitor support.
  1366. if (((dwFeature & THERMALMONITOR_FEATURE) != 0) && this->Features.HasThermal) bHasFeature = true;
  1367. // Check for temperature sensing diode support.
  1368. if (((dwFeature & TEMPSENSEDIODE_FEATURE) != 0) && this->Features.ExtendedFeatures.PowerManagement.HasTempSenseDiode) bHasFeature = true;
  1369. // Check for frequency ID support.
  1370. if (((dwFeature & FREQUENCYID_FEATURE) != 0) && this->Features.ExtendedFeatures.PowerManagement.HasFrequencyID) bHasFeature = true;
  1371. // Check for voltage ID support.
  1372. if (((dwFeature & VOLTAGEID_FREQUENCY) != 0) && this->Features.ExtendedFeatures.PowerManagement.HasVoltageID) bHasFeature = true;
  1373. return bHasFeature;
  1374. }
  1375. void SystemInformationImplementation::Delay(unsigned int uiMS)
  1376. {
  1377. #ifdef WIN32
  1378. LARGE_INTEGER Frequency, StartCounter, EndCounter;
  1379. __int64 x;
  1380. // Get the frequency of the high performance counter.
  1381. if (!QueryPerformanceFrequency (&Frequency)) return;
  1382. x = Frequency.QuadPart / 1000 * uiMS;
  1383. // Get the starting position of the counter.
  1384. QueryPerformanceCounter (&StartCounter);
  1385. do {
  1386. // Get the ending position of the counter.
  1387. QueryPerformanceCounter (&EndCounter);
  1388. } while (EndCounter.QuadPart - StartCounter.QuadPart < x);
  1389. #endif
  1390. (void)uiMS;
  1391. }
  1392. bool SystemInformationImplementation::DoesCPUSupportCPUID()
  1393. {
  1394. #if USE_ASM_INSTRUCTIONS
  1395. // Use SEH to determine CPUID presence
  1396. __try {
  1397. _asm {
  1398. #ifdef CPUID_AWARE_COMPILER
  1399. ; we must push/pop the registers <<CPUID>> writes to, as the
  1400. ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
  1401. ; these registers to change.
  1402. push eax
  1403. push ebx
  1404. push ecx
  1405. push edx
  1406. #endif
  1407. ; <<CPUID>>
  1408. mov eax, 0
  1409. CPUID_INSTRUCTION
  1410. #ifdef CPUID_AWARE_COMPILER
  1411. pop edx
  1412. pop ecx
  1413. pop ebx
  1414. pop eax
  1415. #endif
  1416. }
  1417. }
  1418. __except(1)
  1419. {
  1420. // Stop the class from trying to use CPUID again!
  1421. return false;
  1422. }
  1423. // The cpuid instruction succeeded.
  1424. return true;
  1425. #else
  1426. // Assume no cpuid instruction.
  1427. return false;
  1428. #endif
  1429. }
  1430. bool SystemInformationImplementation::RetrieveCPUFeatures()
  1431. {
  1432. #if USE_ASM_INSTRUCTIONS
  1433. int localCPUFeatures = 0;
  1434. int localCPUAdvanced = 0;
  1435. // Use assembly to detect CPUID information...
  1436. __try {
  1437. _asm {
  1438. #ifdef CPUID_AWARE_COMPILER
  1439. ; we must push/pop the registers <<CPUID>> writes to, as the
  1440. ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
  1441. ; these registers to change.
  1442. push eax
  1443. push ebx
  1444. push ecx
  1445. push edx
  1446. #endif
  1447. ; <<CPUID>>
  1448. ; eax = 1 --> eax: CPU ID - bits 31..16 - unused, bits 15..12 - type, bits 11..8 - family, bits 7..4 - model, bits 3..0 - mask revision
  1449. ; ebx: 31..24 - default APIC ID, 23..16 - logical processsor ID, 15..8 - CFLUSH chunk size , 7..0 - brand ID
  1450. ; edx: CPU feature flags
  1451. mov eax,1
  1452. CPUID_INSTRUCTION
  1453. mov localCPUFeatures, edx
  1454. mov localCPUAdvanced, ebx
  1455. #ifdef CPUID_AWARE_COMPILER
  1456. pop edx
  1457. pop ecx
  1458. pop ebx
  1459. pop eax
  1460. #endif
  1461. }
  1462. }
  1463. __except(1)
  1464. {
  1465. return false;
  1466. }
  1467. // Retrieve the features of CPU present.
  1468. this->Features.HasFPU = ((localCPUFeatures & 0x00000001) != 0); // FPU Present --> Bit 0
  1469. this->Features.HasTSC = ((localCPUFeatures & 0x00000010) != 0); // TSC Present --> Bit 4
  1470. this->Features.HasAPIC = ((localCPUFeatures & 0x00000200) != 0); // APIC Present --> Bit 9
  1471. this->Features.HasMTRR = ((localCPUFeatures & 0x00001000) != 0); // MTRR Present --> Bit 12
  1472. this->Features.HasCMOV = ((localCPUFeatures & 0x00008000) != 0); // CMOV Present --> Bit 15
  1473. this->Features.HasSerial = ((localCPUFeatures & 0x00040000) != 0); // Serial Present --> Bit 18
  1474. this->Features.HasACPI = ((localCPUFeatures & 0x00400000) != 0); // ACPI Capable --> Bit 22
  1475. this->Features.HasMMX = ((localCPUFeatures & 0x00800000) != 0); // MMX Present --> Bit 23
  1476. this->Features.HasSSE = ((localCPUFeatures & 0x02000000) != 0); // SSE Present --> Bit 25
  1477. this->Features.HasSSE2 = ((localCPUFeatures & 0x04000000) != 0); // SSE2 Present --> Bit 26
  1478. this->Features.HasThermal = ((localCPUFeatures & 0x20000000) != 0); // Thermal Monitor Present --> Bit 29
  1479. this->Features.HasIA64 = ((localCPUFeatures & 0x40000000) != 0); // IA64 Present --> Bit 30
  1480. // Retrieve extended SSE capabilities if SSE is available.
  1481. if (this->Features.HasSSE) {
  1482. // Attempt to __try some SSE FP instructions.
  1483. __try
  1484. {
  1485. // Perform: orps xmm0, xmm0
  1486. _asm
  1487. {
  1488. _emit 0x0f
  1489. _emit 0x56
  1490. _emit 0xc0
  1491. }
  1492. // SSE FP capable processor.
  1493. this->Features.HasSSEFP = true;
  1494. }
  1495. __except(1)
  1496. {
  1497. // bad instruction - processor or OS cannot handle SSE FP.
  1498. this->Features.HasSSEFP = false;
  1499. }
  1500. }
  1501. else
  1502. {
  1503. // Set the advanced SSE capabilities to not available.
  1504. this->Features.HasSSEFP = false;
  1505. }
  1506. // Retrieve Intel specific extended features.
  1507. if (this->ChipManufacturer == Intel)
  1508. {
  1509. this->Features.ExtendedFeatures.SupportsHyperthreading = ((localCPUFeatures & 0x10000000) != 0); // Intel specific: Hyperthreading --> Bit 28
  1510. this->Features.ExtendedFeatures.LogicalProcessorsPerPhysical = (this->Features.ExtendedFeatures.SupportsHyperthreading) ? ((localCPUAdvanced & 0x00FF0000) >> 16) : 1;
  1511. if ((this->Features.ExtendedFeatures.SupportsHyperthreading) && (this->Features.HasAPIC))
  1512. {
  1513. // Retrieve APIC information if there is one present.
  1514. this->Features.ExtendedFeatures.APIC_ID = ((localCPUAdvanced & 0xFF000000) >> 24);
  1515. }
  1516. }
  1517. return true;
  1518. #else
  1519. return false;
  1520. #endif
  1521. }
  1522. /** Find the manufacturer given the vendor id */
  1523. void SystemInformationImplementation::FindManufacturer()
  1524. {
  1525. if (this->ChipID.Vendor == "GenuineIntel") this->ChipManufacturer = Intel; // Intel Corp.
  1526. else if (this->ChipID.Vendor == "UMC UMC UMC ") this->ChipManufacturer = UMC; // United Microelectronics Corp.
  1527. else if (this->ChipID.Vendor == "AuthenticAMD") this->ChipManufacturer = AMD; // Advanced Micro Devices
  1528. else if (this->ChipID.Vendor == "AMD ISBETTER") this->ChipManufacturer = AMD; // Advanced Micro Devices (1994)
  1529. else if (this->ChipID.Vendor == "CyrixInstead") this->ChipManufacturer = Cyrix; // Cyrix Corp., VIA Inc.
  1530. else if (this->ChipID.Vendor == "NexGenDriven") this->ChipManufacturer = NexGen; // NexGen Inc. (now AMD)
  1531. else if (this->ChipID.Vendor == "CentaurHauls") this->ChipManufacturer = IDT; // IDT/Centaur (now VIA)
  1532. else if (this->ChipID.Vendor == "RiseRiseRise") this->ChipManufacturer = Rise; // Rise
  1533. else if (this->ChipID.Vendor == "GenuineTMx86") this->ChipManufacturer = Transmeta; // Transmeta
  1534. else if (this->ChipID.Vendor == "TransmetaCPU") this->ChipManufacturer = Transmeta; // Transmeta
  1535. else if (this->ChipID.Vendor == "Geode By NSC") this->ChipManufacturer = NSC; // National Semiconductor
  1536. else if (this->ChipID.Vendor == "Sun") this->ChipManufacturer = Sun; // Sun Microelectronics
  1537. else if (this->ChipID.Vendor == "IBM") this->ChipManufacturer = IBM; // IBM Microelectronics
  1538. else if (this->ChipID.Vendor == "Motorola") this->ChipManufacturer = Motorola; // Motorola Microelectronics
  1539. else this->ChipManufacturer = UnknownManufacturer; // Unknown manufacturer
  1540. }
  1541. /** */
  1542. bool SystemInformationImplementation::RetrieveCPUIdentity()
  1543. {
  1544. #if USE_ASM_INSTRUCTIONS
  1545. int localCPUVendor[3];
  1546. int localCPUSignature;
  1547. // Use assembly to detect CPUID information...
  1548. __try
  1549. {
  1550. _asm
  1551. {
  1552. #ifdef CPUID_AWARE_COMPILER
  1553. ; we must push/pop the registers <<CPUID>> writes to, as the
  1554. ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
  1555. ; these registers to change.
  1556. push eax
  1557. push ebx
  1558. push ecx
  1559. push edx
  1560. #endif
  1561. ; <<CPUID>>
  1562. ; eax = 0 --> eax: maximum value of CPUID instruction.
  1563. ; ebx: part 1 of 3; CPU signature.
  1564. ; edx: part 2 of 3; CPU signature.
  1565. ; ecx: part 3 of 3; CPU signature.
  1566. mov eax, 0
  1567. CPUID_INSTRUCTION
  1568. mov localCPUVendor[0 * TYPE int], ebx
  1569. mov localCPUVendor[1 * TYPE int], edx
  1570. mov localCPUVendor[2 * TYPE int], ecx
  1571. ; <<CPUID>>
  1572. ; eax = 1 --> eax: CPU ID - bits 31..16 - unused, bits 15..12 - type, bits 11..8 - family, bits 7..4 - model, bits 3..0 - mask revision
  1573. ; ebx: 31..24 - default APIC ID, 23..16 - logical processsor ID, 15..8 - CFLUSH chunk size , 7..0 - brand ID
  1574. ; edx: CPU feature flags
  1575. mov eax,1
  1576. CPUID_INSTRUCTION
  1577. mov localCPUSignature, eax
  1578. #ifdef CPUID_AWARE_COMPILER
  1579. pop edx
  1580. pop ecx
  1581. pop ebx
  1582. pop eax
  1583. #endif
  1584. }
  1585. }
  1586. __except(1)
  1587. {
  1588. return false;
  1589. }
  1590. // Process the returned information.
  1591. char vbuf[13];
  1592. memcpy (&(vbuf[0]), &(localCPUVendor[0]), sizeof (int));
  1593. memcpy (&(vbuf[4]), &(localCPUVendor[1]), sizeof (int));
  1594. memcpy (&(vbuf[8]), &(localCPUVendor[2]), sizeof (int));
  1595. vbuf[12] = '\0';
  1596. this->ChipID.Vendor = vbuf;
  1597. this->FindManufacturer();
  1598. // Retrieve the family of CPU present.
  1599. this->ChipID.ExtendedFamily = ((localCPUSignature & 0x0FF00000) >> 20); // Bits 27..20 Used
  1600. this->ChipID.ExtendedModel = ((localCPUSignature & 0x000F0000) >> 16); // Bits 19..16 Used
  1601. this->ChipID.Type = ((localCPUSignature & 0x0000F000) >> 12); // Bits 15..12 Used
  1602. this->ChipID.Family = ((localCPUSignature & 0x00000F00) >> 8); // Bits 11..8 Used
  1603. this->ChipID.Model = ((localCPUSignature & 0x000000F0) >> 4); // Bits 7..4 Used
  1604. this->ChipID.Revision = ((localCPUSignature & 0x0000000F) >> 0); // Bits 3..0 Used
  1605. return true;
  1606. #else
  1607. return false;
  1608. #endif
  1609. }
  1610. /** */
  1611. bool SystemInformationImplementation::RetrieveCPUCacheDetails()
  1612. {
  1613. #if USE_ASM_INSTRUCTIONS
  1614. int L1Cache[4] = { 0, 0, 0, 0 };
  1615. int L2Cache[4] = { 0, 0, 0, 0 };
  1616. // Check to see if what we are about to do is supported...
  1617. if (RetrieveCPUExtendedLevelSupport (0x80000005))
  1618. {
  1619. // Use assembly to retrieve the L1 cache information ...
  1620. __try
  1621. {
  1622. _asm
  1623. {
  1624. #ifdef CPUID_AWARE_COMPILER
  1625. ; we must push/pop the registers <<CPUID>> writes to, as the
  1626. ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
  1627. ; these registers to change.
  1628. push eax
  1629. push ebx
  1630. push ecx
  1631. push edx
  1632. #endif
  1633. ; <<CPUID>>
  1634. ; eax = 0x80000005 --> eax: L1 cache information - Part 1 of 4.
  1635. ; ebx: L1 cache information - Part 2 of 4.
  1636. ; edx: L1 cache information - Part 3 of 4.
  1637. ; ecx: L1 cache information - Part 4 of 4.
  1638. mov eax, 0x80000005
  1639. CPUID_INSTRUCTION
  1640. mov L1Cache[0 * TYPE int], eax
  1641. mov L1Cache[1 * TYPE int], ebx
  1642. mov L1Cache[2 * TYPE int], ecx
  1643. mov L1Cache[3 * TYPE int], edx
  1644. #ifdef CPUID_AWARE_COMPILER
  1645. pop edx
  1646. pop ecx
  1647. pop ebx
  1648. pop eax
  1649. #endif
  1650. }
  1651. }
  1652. __except(1)
  1653. {
  1654. return false;
  1655. }
  1656. // Save the L1 data cache size (in KB) from ecx: bits 31..24 as well as data cache size from edx: bits 31..24.
  1657. this->Features.L1CacheSize = ((L1Cache[2] & 0xFF000000) >> 24);
  1658. this->Features.L1CacheSize += ((L1Cache[3] & 0xFF000000) >> 24);
  1659. }
  1660. else
  1661. {
  1662. // Store -1 to indicate the cache could not be queried.
  1663. this->Features.L1CacheSize = -1;
  1664. }
  1665. // Check to see if what we are about to do is supported...
  1666. if (RetrieveCPUExtendedLevelSupport (0x80000006))
  1667. {
  1668. // Use assembly to retrieve the L2 cache information ...
  1669. __try
  1670. {
  1671. _asm
  1672. {
  1673. #ifdef CPUID_AWARE_COMPILER
  1674. ; we must push/pop the registers <<CPUID>> writes to, as the
  1675. ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
  1676. ; these registers to change.
  1677. push eax
  1678. push ebx
  1679. push ecx
  1680. push edx
  1681. #endif
  1682. ; <<CPUID>>
  1683. ; eax = 0x80000006 --> eax: L2 cache information - Part 1 of 4.
  1684. ; ebx: L2 cache information - Part 2 of 4.
  1685. ; edx: L2 cache information - Part 3 of 4.
  1686. ; ecx: L2 cache information - Part 4 of 4.
  1687. mov eax, 0x80000006
  1688. CPUID_INSTRUCTION
  1689. mov L2Cache[0 * TYPE int], eax
  1690. mov L2Cache[1 * TYPE int], ebx
  1691. mov L2Cache[2 * TYPE int], ecx
  1692. mov L2Cache[3 * TYPE int], edx
  1693. #ifdef CPUID_AWARE_COMPILER
  1694. pop edx
  1695. pop ecx
  1696. pop ebx
  1697. pop eax
  1698. #endif
  1699. }
  1700. }
  1701. __except(1)
  1702. {
  1703. return false;
  1704. }
  1705. // Save the L2 unified cache size (in KB) from ecx: bits 31..16.
  1706. this->Features.L2CacheSize = ((L2Cache[2] & 0xFFFF0000) >> 16);
  1707. }
  1708. else
  1709. {
  1710. // Store -1 to indicate the cache could not be queried.
  1711. this->Features.L2CacheSize = -1;
  1712. }
  1713. // Define L3 as being not present as we cannot test for it.
  1714. this->Features.L3CacheSize = -1;
  1715. #endif
  1716. // Return failure if we cannot detect either cache with this method.
  1717. return ((this->Features.L1CacheSize == -1) && (this->Features.L2CacheSize == -1)) ? false : true;
  1718. }
  1719. /** */
  1720. bool SystemInformationImplementation::RetrieveClassicalCPUCacheDetails()
  1721. {
  1722. #if USE_ASM_INSTRUCTIONS
  1723. int TLBCode = -1, TLBData = -1, L1Code = -1, L1Data = -1, L1Trace = -1, L2Unified = -1, L3Unified = -1;
  1724. int TLBCacheData[4] = { 0, 0, 0, 0 };
  1725. int TLBPassCounter = 0;
  1726. int TLBCacheUnit = 0;
  1727. do {
  1728. // Use assembly to retrieve the L2 cache information ...
  1729. __try {
  1730. _asm {
  1731. #ifdef CPUID_AWARE_COMPILER
  1732. ; we must push/pop the registers <<CPUID>> writes to, as the
  1733. ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
  1734. ; these registers to change.
  1735. push eax
  1736. push ebx
  1737. push ecx
  1738. push edx
  1739. #endif
  1740. ; <<CPUID>>
  1741. ; eax = 2 --> eax: TLB and cache information - Part 1 of 4.
  1742. ; ebx: TLB and cache information - Part 2 of 4.
  1743. ; ecx: TLB and cache information - Part 3 of 4.
  1744. ; edx: TLB and cache information - Part 4 of 4.
  1745. mov eax, 2
  1746. CPUID_INSTRUCTION
  1747. mov TLBCacheData[0 * TYPE int], eax
  1748. mov TLBCacheData[1 * TYPE int], ebx
  1749. mov TLBCacheData[2 * TYPE int], ecx
  1750. mov TLBCacheData[3 * TYPE int], edx
  1751. #ifdef CPUID_AWARE_COMPILER
  1752. pop edx
  1753. pop ecx
  1754. pop ebx
  1755. pop eax
  1756. #endif
  1757. }
  1758. }
  1759. __except(1)
  1760. {
  1761. return false;
  1762. }
  1763. int bob = ((TLBCacheData[0] & 0x00FF0000) >> 16);
  1764. (void)bob;
  1765. // Process the returned TLB and cache information.
  1766. for (int nCounter = 0; nCounter < TLBCACHE_INFO_UNITS; nCounter ++)
  1767. {
  1768. // First of all - decide which unit we are dealing with.
  1769. switch (nCounter)
  1770. {
  1771. // eax: bits 8..15 : bits 16..23 : bits 24..31
  1772. case 0: TLBCacheUnit = ((TLBCacheData[0] & 0x0000FF00) >> 8); break;
  1773. case 1: TLBCacheUnit = ((TLBCacheData[0] & 0x00FF0000) >> 16); break;
  1774. case 2: TLBCacheUnit = ((TLBCacheData[0] & 0xFF000000) >> 24); break;
  1775. // ebx: bits 0..7 : bits 8..15 : bits 16..23 : bits 24..31
  1776. case 3: TLBCacheUnit = ((TLBCacheData[1] & 0x000000FF) >> 0); break;
  1777. case 4: TLBCacheUnit = ((TLBCacheData[1] & 0x0000FF00) >> 8); break;
  1778. case 5: TLBCacheUnit = ((TLBCacheData[1] & 0x00FF0000) >> 16); break;
  1779. case 6: TLBCacheUnit = ((TLBCacheData[1] & 0xFF000000) >> 24); break;
  1780. // ecx: bits 0..7 : bits 8..15 : bits 16..23 : bits 24..31
  1781. case 7: TLBCacheUnit = ((TLBCacheData[2] & 0x000000FF) >> 0); break;
  1782. case 8: TLBCacheUnit = ((TLBCacheData[2] & 0x0000FF00) >> 8); break;
  1783. case 9: TLBCacheUnit = ((TLBCacheData[2] & 0x00FF0000) >> 16); break;
  1784. case 10: TLBCacheUnit = ((TLBCacheData[2] & 0xFF000000) >> 24); break;
  1785. // edx: bits 0..7 : bits 8..15 : bits 16..23 : bits 24..31
  1786. case 11: TLBCacheUnit = ((TLBCacheData[3] & 0x000000FF) >> 0); break;
  1787. case 12: TLBCacheUnit = ((TLBCacheData[3] & 0x0000FF00) >> 8); break;
  1788. case 13: TLBCacheUnit = ((TLBCacheData[3] & 0x00FF0000) >> 16); break;
  1789. case 14: TLBCacheUnit = ((TLBCacheData[3] & 0xFF000000) >> 24); break;
  1790. // Default case - an error has occured.
  1791. default: return false;
  1792. }
  1793. // Now process the resulting unit to see what it means....
  1794. switch (TLBCacheUnit)
  1795. {
  1796. case 0x00: break;
  1797. case 0x01: STORE_TLBCACHE_INFO (TLBCode, 4); break;
  1798. case 0x02: STORE_TLBCACHE_INFO (TLBCode, 4096); break;
  1799. case 0x03: STORE_TLBCACHE_INFO (TLBData, 4); break;
  1800. case 0x04: STORE_TLBCACHE_INFO (TLBData, 4096); break;
  1801. case 0x06: STORE_TLBCACHE_INFO (L1Code, 8); break;
  1802. case 0x08: STORE_TLBCACHE_INFO (L1Code, 16); break;
  1803. case 0x0a: STORE_TLBCACHE_INFO (L1Data, 8); break;
  1804. case 0x0c: STORE_TLBCACHE_INFO (L1Data, 16); break;
  1805. case 0x10: STORE_TLBCACHE_INFO (L1Data, 16); break; // <-- FIXME: IA-64 Only
  1806. case 0x15: STORE_TLBCACHE_INFO (L1Code, 16); break; // <-- FIXME: IA-64 Only
  1807. case 0x1a: STORE_TLBCACHE_INFO (L2Unified, 96); break; // <-- FIXME: IA-64 Only
  1808. case 0x22: STORE_TLBCACHE_INFO (L3Unified, 512); break;
  1809. case 0x23: STORE_TLBCACHE_INFO (L3Unified, 1024); break;
  1810. case 0x25: STORE_TLBCACHE_INFO (L3Unified, 2048); break;
  1811. case 0x29: STORE_TLBCACHE_INFO (L3Unified, 4096); break;
  1812. case 0x39: STORE_TLBCACHE_INFO (L2Unified, 128); break;
  1813. case 0x3c: STORE_TLBCACHE_INFO (L2Unified, 256); break;
  1814. case 0x40: STORE_TLBCACHE_INFO (L2Unified, 0); break; // <-- FIXME: No integrated L2 cache (P6 core) or L3 cache (P4 core).
  1815. case 0x41: STORE_TLBCACHE_INFO (L2Unified, 128); break;
  1816. case 0x42: STORE_TLBCACHE_INFO (L2Unified, 256); break;
  1817. case 0x43: STORE_TLBCACHE_INFO (L2Unified, 512); break;
  1818. case 0x44: STORE_TLBCACHE_INFO (L2Unified, 1024); break;
  1819. case 0x45: STORE_TLBCACHE_INFO (L2Unified, 2048); break;
  1820. case 0x50: STORE_TLBCACHE_INFO (TLBCode, 4096); break;
  1821. case 0x51: STORE_TLBCACHE_INFO (TLBCode, 4096); break;
  1822. case 0x52: STORE_TLBCACHE_INFO (TLBCode, 4096); break;
  1823. case 0x5b: STORE_TLBCACHE_INFO (TLBData, 4096); break;
  1824. case 0x5c: STORE_TLBCACHE_INFO (TLBData, 4096); break;
  1825. case 0x5d: STORE_TLBCACHE_INFO (TLBData, 4096); break;
  1826. case 0x66: STORE_TLBCACHE_INFO (L1Data, 8); break;
  1827. case 0x67: STORE_TLBCACHE_INFO (L1Data, 16); break;
  1828. case 0x68: STORE_TLBCACHE_INFO (L1Data, 32); break;
  1829. case 0x70: STORE_TLBCACHE_INFO (L1Trace, 12); break;
  1830. case 0x71: STORE_TLBCACHE_INFO (L1Trace, 16); break;
  1831. case 0x72: STORE_TLBCACHE_INFO (L1Trace, 32); break;
  1832. case 0x77: STORE_TLBCACHE_INFO (L1Code, 16); break; // <-- FIXME: IA-64 Only
  1833. case 0x79: STORE_TLBCACHE_INFO (L2Unified, 128); break;
  1834. case 0x7a: STORE_TLBCACHE_INFO (L2Unified, 256); break;
  1835. case 0x7b: STORE_TLBCACHE_INFO (L2Unified, 512); break;
  1836. case 0x7c: STORE_TLBCACHE_INFO (L2Unified, 1024); break;
  1837. case 0x7e: STORE_TLBCACHE_INFO (L2Unified, 256); break;
  1838. case 0x81: STORE_TLBCACHE_INFO (L2Unified, 128); break;
  1839. case 0x82: STORE_TLBCACHE_INFO (L2Unified, 256); break;
  1840. case 0x83: STORE_TLBCACHE_INFO (L2Unified, 512); break;
  1841. case 0x84: STORE_TLBCACHE_INFO (L2Unified, 1024); break;
  1842. case 0x85: STORE_TLBCACHE_INFO (L2Unified, 2048); break;
  1843. case 0x88: STORE_TLBCACHE_INFO (L3Unified, 2048); break; // <-- FIXME: IA-64 Only
  1844. case 0x89: STORE_TLBCACHE_INFO (L3Unified, 4096); break; // <-- FIXME: IA-64 Only
  1845. case 0x8a: STORE_TLBCACHE_INFO (L3Unified, 8192); break; // <-- FIXME: IA-64 Only
  1846. case 0x8d: STORE_TLBCACHE_INFO (L3Unified, 3096); break; // <-- FIXME: IA-64 Only
  1847. case 0x90: STORE_TLBCACHE_INFO (TLBCode, 262144); break; // <-- FIXME: IA-64 Only
  1848. case 0x96: STORE_TLBCACHE_INFO (TLBCode, 262144); break; // <-- FIXME: IA-64 Only
  1849. case 0x9b: STORE_TLBCACHE_INFO (TLBCode, 262144); break; // <-- FIXME: IA-64 Only
  1850. // Default case - an error has occured.
  1851. default: return false;
  1852. }
  1853. }
  1854. // Increment the TLB pass counter.
  1855. TLBPassCounter ++;
  1856. } while ((TLBCacheData[0] & 0x000000FF) > TLBPassCounter);
  1857. // Ok - we now have the maximum TLB, L1, L2, and L3 sizes...
  1858. if ((L1Code == -1) && (L1Data == -1) && (L1Trace == -1))
  1859. {
  1860. this->Features.L1CacheSize = -1;
  1861. }
  1862. else if ((L1Code == -1) && (L1Data == -1) && (L1Trace != -1))
  1863. {
  1864. this->Features.L1CacheSize = L1Trace;
  1865. }
  1866. else if ((L1Code != -1) && (L1Data == -1))
  1867. {
  1868. this->Features.L1CacheSize = L1Code;
  1869. }
  1870. else if ((L1Code == -1) && (L1Data != -1))
  1871. {
  1872. this->Features.L1CacheSize = L1Data;
  1873. }
  1874. else if ((L1Code != -1) && (L1Data != -1))
  1875. {
  1876. this->Features.L1CacheSize = L1Code + L1Data;
  1877. }
  1878. else
  1879. {
  1880. this->Features.L1CacheSize = -1;
  1881. }
  1882. // Ok - we now have the maximum TLB, L1, L2, and L3 sizes...
  1883. if (L2Unified == -1)
  1884. {
  1885. this->Features.L2CacheSize = -1;
  1886. }
  1887. else
  1888. {
  1889. this->Features.L2CacheSize = L2Unified;
  1890. }
  1891. // Ok - we now have the maximum TLB, L1, L2, and L3 sizes...
  1892. if (L3Unified == -1)
  1893. {
  1894. this->Features.L3CacheSize = -1;
  1895. }
  1896. else
  1897. {
  1898. this->Features.L3CacheSize = L3Unified;
  1899. }
  1900. return true;
  1901. #else
  1902. return false;
  1903. #endif
  1904. }
  1905. /** */
  1906. bool SystemInformationImplementation::RetrieveCPUClockSpeed()
  1907. {
  1908. bool retrieved = false;
  1909. #if defined(_WIN32)
  1910. // First of all we check to see if the RDTSC (0x0F, 0x31) instruction is
  1911. // supported. If not, we fallback to trying to read this value from the
  1912. // registry:
  1913. //
  1914. if (!this->Features.HasTSC)
  1915. {
  1916. HKEY hKey = NULL;
  1917. LONG err = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  1918. "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0,
  1919. KEY_READ, &hKey);
  1920. if (ERROR_SUCCESS == err)
  1921. {
  1922. DWORD dwType = 0;
  1923. DWORD data = 0;
  1924. DWORD dwSize = sizeof(DWORD);
  1925. err = RegQueryValueEx(hKey, "~MHz", 0,
  1926. &dwType, (LPBYTE) &data, &dwSize);
  1927. if (ERROR_SUCCESS == err)
  1928. {
  1929. this->CPUSpeedInMHz = (float) data;
  1930. retrieved = true;
  1931. }
  1932. RegCloseKey(hKey);
  1933. hKey = NULL;
  1934. }
  1935. return retrieved;
  1936. }
  1937. unsigned int uiRepetitions = 1;
  1938. unsigned int uiMSecPerRepetition = 50;
  1939. __int64 i64Total = 0;
  1940. __int64 i64Overhead = 0;
  1941. for (unsigned int nCounter = 0; nCounter < uiRepetitions; nCounter ++)
  1942. {
  1943. i64Total += GetCyclesDifference (SystemInformationImplementation::Delay,
  1944. uiMSecPerRepetition);
  1945. i64Overhead +=
  1946. GetCyclesDifference (SystemInformationImplementation::DelayOverhead,
  1947. uiMSecPerRepetition);
  1948. }
  1949. // Calculate the MHz speed.
  1950. i64Total -= i64Overhead;
  1951. i64Total /= uiRepetitions;
  1952. i64Total /= uiMSecPerRepetition;
  1953. i64Total /= 1000;
  1954. // Save the CPU speed.
  1955. this->CPUSpeedInMHz = (float) i64Total;
  1956. retrieved = true;
  1957. #endif
  1958. return retrieved;
  1959. }
  1960. /** */
  1961. bool SystemInformationImplementation::RetrieveClassicalCPUClockSpeed()
  1962. {
  1963. #if USE_ASM_INSTRUCTIONS
  1964. LARGE_INTEGER liStart, liEnd, liCountsPerSecond;
  1965. double dFrequency, dDifference;
  1966. // Attempt to get a starting tick count.
  1967. QueryPerformanceCounter (&liStart);
  1968. __try
  1969. {
  1970. _asm
  1971. {
  1972. mov eax, 0x80000000
  1973. mov ebx, CLASSICAL_CPU_FREQ_LOOP
  1974. Timer_Loop:
  1975. bsf ecx,eax
  1976. dec ebx
  1977. jnz Timer_Loop
  1978. }
  1979. }
  1980. __except(1)
  1981. {
  1982. return false;
  1983. }
  1984. // Attempt to get a starting tick count.
  1985. QueryPerformanceCounter (&liEnd);
  1986. // Get the difference... NB: This is in seconds....
  1987. QueryPerformanceFrequency (&liCountsPerSecond);
  1988. dDifference = (((double) liEnd.QuadPart - (double) liStart.QuadPart) / (double) liCountsPerSecond.QuadPart);
  1989. // Calculate the clock speed.
  1990. if (this->ChipID.Family == 3)
  1991. {
  1992. // 80386 processors.... Loop time is 115 cycles!
  1993. dFrequency = (((CLASSICAL_CPU_FREQ_LOOP * 115) / dDifference) / 1000000);
  1994. }
  1995. else if (this->ChipID.Family == 4)
  1996. {
  1997. // 80486 processors.... Loop time is 47 cycles!
  1998. dFrequency = (((CLASSICAL_CPU_FREQ_LOOP * 47) / dDifference) / 1000000);
  1999. }
  2000. else if (this->ChipID.Family == 5)
  2001. {
  2002. // Pentium processors.... Loop time is 43 cycles!
  2003. dFrequency = (((CLASSICAL_CPU_FREQ_LOOP * 43) / dDifference) / 1000000);
  2004. }
  2005. // Save the clock speed.
  2006. this->Features.CPUSpeed = (int) dFrequency;
  2007. return true;
  2008. #else
  2009. return false;
  2010. #endif
  2011. }
  2012. /** */
  2013. bool SystemInformationImplementation::RetrieveCPUExtendedLevelSupport(int CPULevelToCheck)
  2014. {
  2015. int MaxCPUExtendedLevel = 0;
  2016. // The extended CPUID is supported by various vendors starting with the following CPU models:
  2017. //
  2018. // Manufacturer & Chip Name | Family Model Revision
  2019. //
  2020. // AMD K6, K6-2 | 5 6 x
  2021. // Cyrix GXm, Cyrix III "Joshua" | 5 4 x
  2022. // IDT C6-2 | 5 8 x
  2023. // VIA Cyrix III | 6 5 x
  2024. // Transmeta Crusoe | 5 x x
  2025. // Intel Pentium 4 | f x x
  2026. //
  2027. // We check to see if a supported processor is present...
  2028. if (this->ChipManufacturer == AMD)
  2029. {
  2030. if (this->ChipID.Family < 5) return false;
  2031. if ((this->ChipID.Family == 5) && (this->ChipID.Model < 6)) return false;
  2032. }
  2033. else if (this->ChipManufacturer == Cyrix)
  2034. {
  2035. if (this->ChipID.Family < 5) return false;
  2036. if ((this->ChipID.Family == 5) && (this->ChipID.Model < 4)) return false;
  2037. if ((this->ChipID.Family == 6) && (this->ChipID.Model < 5)) return false;
  2038. }
  2039. else if (this->ChipManufacturer == IDT)
  2040. {
  2041. if (this->ChipID.Family < 5) return false;
  2042. if ((this->ChipID.Family == 5) && (this->ChipID.Model < 8)) return false;
  2043. }
  2044. else if (this->ChipManufacturer == Transmeta)
  2045. {
  2046. if (this->ChipID.Family < 5) return false;
  2047. }
  2048. else if (this->ChipManufacturer == Intel)
  2049. {
  2050. if (this->ChipID.Family < 0xf)
  2051. {
  2052. return false;
  2053. }
  2054. }
  2055. #if USE_ASM_INSTRUCTIONS
  2056. // Use assembly to detect CPUID information...
  2057. __try {
  2058. _asm {
  2059. #ifdef CPUID_AWARE_COMPILER
  2060. ; we must push/pop the registers <<CPUID>> writes to, as the
  2061. ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
  2062. ; these registers to change.
  2063. push eax
  2064. push ebx
  2065. push ecx
  2066. push edx
  2067. #endif
  2068. ; <<CPUID>>
  2069. ; eax = 0x80000000 --> eax: maximum supported extended level
  2070. mov eax,0x80000000
  2071. CPUID_INSTRUCTION
  2072. mov MaxCPUExtendedLevel, eax
  2073. #ifdef CPUID_AWARE_COMPILER
  2074. pop edx
  2075. pop ecx
  2076. pop ebx
  2077. pop eax
  2078. #endif
  2079. }
  2080. }
  2081. __except(1)
  2082. {
  2083. return false;
  2084. }
  2085. #endif
  2086. // Now we have to check the level wanted vs level returned...
  2087. int nLevelWanted = (CPULevelToCheck & 0x7FFFFFFF);
  2088. int nLevelReturn = (MaxCPUExtendedLevel & 0x7FFFFFFF);
  2089. // Check to see if the level provided is supported...
  2090. if (nLevelWanted > nLevelReturn)
  2091. {
  2092. return false;
  2093. }
  2094. return true;
  2095. }
  2096. /** */
  2097. bool SystemInformationImplementation::RetrieveExtendedCPUFeatures()
  2098. {
  2099. // Check that we are not using an Intel processor as it does not support this.
  2100. if (this->ChipManufacturer == Intel)
  2101. {
  2102. return false;
  2103. }
  2104. // Check to see if what we are about to do is supported...
  2105. if (!RetrieveCPUExtendedLevelSupport(static_cast<int>(0x80000001)))
  2106. {
  2107. return false;
  2108. }
  2109. #if USE_ASM_INSTRUCTIONS
  2110. int localCPUExtendedFeatures = 0;
  2111. // Use assembly to detect CPUID information...
  2112. __try
  2113. {
  2114. _asm
  2115. {
  2116. #ifdef CPUID_AWARE_COMPILER
  2117. ; we must push/pop the registers <<CPUID>> writes to, as the
  2118. ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
  2119. ; these registers to change.
  2120. push eax
  2121. push ebx
  2122. push ecx
  2123. push edx
  2124. #endif
  2125. ; <<CPUID>>
  2126. ; eax = 0x80000001 --> eax: CPU ID - bits 31..16 - unused, bits 15..12 - type, bits 11..8 - family, bits 7..4 - model, bits 3..0 - mask revision
  2127. ; ebx: 31..24 - default APIC ID, 23..16 - logical processsor ID, 15..8 - CFLUSH chunk size , 7..0 - brand ID
  2128. ; edx: CPU feature flags
  2129. mov eax,0x80000001
  2130. CPUID_INSTRUCTION
  2131. mov localCPUExtendedFeatures, edx
  2132. #ifdef CPUID_AWARE_COMPILER
  2133. pop edx
  2134. pop ecx
  2135. pop ebx
  2136. pop eax
  2137. #endif
  2138. }
  2139. }
  2140. __except(1)
  2141. {
  2142. return false;
  2143. }
  2144. // Retrieve the extended features of CPU present.
  2145. this->Features.ExtendedFeatures.Has3DNow = ((localCPUExtendedFeatures & 0x80000000) != 0); // 3DNow Present --> Bit 31.
  2146. this->Features.ExtendedFeatures.Has3DNowPlus = ((localCPUExtendedFeatures & 0x40000000) != 0); // 3DNow+ Present -- > Bit 30.
  2147. this->Features.ExtendedFeatures.HasSSEMMX = ((localCPUExtendedFeatures & 0x00400000) != 0); // SSE MMX Present --> Bit 22.
  2148. this->Features.ExtendedFeatures.SupportsMP = ((localCPUExtendedFeatures & 0x00080000) != 0); // MP Capable -- > Bit 19.
  2149. // Retrieve AMD specific extended features.
  2150. if (this->ChipManufacturer == AMD)
  2151. {
  2152. this->Features.ExtendedFeatures.HasMMXPlus = ((localCPUExtendedFeatures & 0x00400000) != 0); // AMD specific: MMX-SSE --> Bit 22
  2153. }
  2154. // Retrieve Cyrix specific extended features.
  2155. if (this->ChipManufacturer == Cyrix)
  2156. {
  2157. this->Features.ExtendedFeatures.HasMMXPlus = ((localCPUExtendedFeatures & 0x01000000) != 0); // Cyrix specific: Extended MMX --> Bit 24
  2158. }
  2159. return true;
  2160. #else
  2161. return false;
  2162. #endif
  2163. }
  2164. /** */
  2165. bool SystemInformationImplementation::RetrieveProcessorSerialNumber()
  2166. {
  2167. // Check to see if the processor supports the processor serial number.
  2168. if (!this->Features.HasSerial)
  2169. {
  2170. return false;
  2171. }
  2172. #if USE_ASM_INSTRUCTIONS
  2173. int SerialNumber[3];
  2174. // Use assembly to detect CPUID information...
  2175. __try {
  2176. _asm {
  2177. #ifdef CPUID_AWARE_COMPILER
  2178. ; we must push/pop the registers <<CPUID>> writes to, as the
  2179. ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
  2180. ; these registers to change.
  2181. push eax
  2182. push ebx
  2183. push ecx
  2184. push edx
  2185. #endif
  2186. ; <<CPUID>>
  2187. ; eax = 3 --> ebx: top 32 bits are the processor signature bits --> NB: Transmeta only ?!?
  2188. ; ecx: middle 32 bits are the processor signature bits
  2189. ; edx: bottom 32 bits are the processor signature bits
  2190. mov eax, 3
  2191. CPUID_INSTRUCTION
  2192. mov SerialNumber[0 * TYPE int], ebx
  2193. mov SerialNumber[1 * TYPE int], ecx
  2194. mov SerialNumber[2 * TYPE int], edx
  2195. #ifdef CPUID_AWARE_COMPILER
  2196. pop edx
  2197. pop ecx
  2198. pop ebx
  2199. pop eax
  2200. #endif
  2201. }
  2202. }
  2203. __except(1)
  2204. {
  2205. return false;
  2206. }
  2207. // Process the returned information.
  2208. char sn[128];
  2209. sprintf (sn, "%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x",
  2210. ((SerialNumber[0] & 0xff000000) >> 24),
  2211. ((SerialNumber[0] & 0x00ff0000) >> 16),
  2212. ((SerialNumber[0] & 0x0000ff00) >> 8),
  2213. ((SerialNumber[0] & 0x000000ff) >> 0),
  2214. ((SerialNumber[1] & 0xff000000) >> 24),
  2215. ((SerialNumber[1] & 0x00ff0000) >> 16),
  2216. ((SerialNumber[1] & 0x0000ff00) >> 8),
  2217. ((SerialNumber[1] & 0x000000ff) >> 0),
  2218. ((SerialNumber[2] & 0xff000000) >> 24),
  2219. ((SerialNumber[2] & 0x00ff0000) >> 16),
  2220. ((SerialNumber[2] & 0x0000ff00) >> 8),
  2221. ((SerialNumber[2] & 0x000000ff) >> 0));
  2222. this->ChipID.SerialNumber = sn;
  2223. return true;
  2224. #else
  2225. return false;
  2226. #endif
  2227. }
  2228. /** */
  2229. bool SystemInformationImplementation::RetrieveCPUPowerManagement()
  2230. {
  2231. // Check to see if what we are about to do is supported...
  2232. if (!RetrieveCPUExtendedLevelSupport(static_cast<int>(0x80000007)))
  2233. {
  2234. this->Features.ExtendedFeatures.PowerManagement.HasFrequencyID = false;
  2235. this->Features.ExtendedFeatures.PowerManagement.HasVoltageID = false;
  2236. this->Features.ExtendedFeatures.PowerManagement.HasTempSenseDiode = false;
  2237. return false;
  2238. }
  2239. #if USE_ASM_INSTRUCTIONS
  2240. int localCPUPowerManagement = 0;
  2241. // Use assembly to detect CPUID information...
  2242. __try {
  2243. _asm {
  2244. #ifdef CPUID_AWARE_COMPILER
  2245. ; we must push/pop the registers <<CPUID>> writes to, as the
  2246. ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
  2247. ; these registers to change.
  2248. push eax
  2249. push ebx
  2250. push ecx
  2251. push edx
  2252. #endif
  2253. ; <<CPUID>>
  2254. ; eax = 0x80000007 --> edx: get processor power management
  2255. mov eax,0x80000007
  2256. CPUID_INSTRUCTION
  2257. mov localCPUPowerManagement, edx
  2258. #ifdef CPUID_AWARE_COMPILER
  2259. pop edx
  2260. pop ecx
  2261. pop ebx
  2262. pop eax
  2263. #endif
  2264. }
  2265. }
  2266. __except(1)
  2267. {
  2268. return false;
  2269. }
  2270. // Check for the power management capabilities of the CPU.
  2271. this->Features.ExtendedFeatures.PowerManagement.HasTempSenseDiode = ((localCPUPowerManagement & 0x00000001) != 0);
  2272. this->Features.ExtendedFeatures.PowerManagement.HasFrequencyID = ((localCPUPowerManagement & 0x00000002) != 0);
  2273. this->Features.ExtendedFeatures.PowerManagement.HasVoltageID = ((localCPUPowerManagement & 0x00000004) != 0);
  2274. return true;
  2275. #else
  2276. return false;
  2277. #endif
  2278. }
  2279. void SystemInformationStripLeadingSpace(kwsys_stl::string& str)
  2280. {
  2281. // Because some manufacturers have leading white space - we have to post-process the name.
  2282. kwsys_stl::string::size_type pos = str.find_first_not_of(" ");
  2283. if(pos != kwsys_stl::string::npos)
  2284. {
  2285. str = str.substr(pos);
  2286. }
  2287. }
  2288. /** */
  2289. bool SystemInformationImplementation::RetrieveExtendedCPUIdentity()
  2290. {
  2291. // Check to see if what we are about to do is supported...
  2292. if (!RetrieveCPUExtendedLevelSupport(static_cast<int>(0x80000002)))
  2293. return false;
  2294. if (!RetrieveCPUExtendedLevelSupport(static_cast<int>(0x80000003)))
  2295. return false;
  2296. if (!RetrieveCPUExtendedLevelSupport(static_cast<int>(0x80000004)))
  2297. return false;
  2298. #if USE_ASM_INSTRUCTIONS
  2299. int CPUExtendedIdentity[12];
  2300. // Use assembly to detect CPUID information...
  2301. __try {
  2302. _asm {
  2303. #ifdef CPUID_AWARE_COMPILER
  2304. ; we must push/pop the registers <<CPUID>> writes to, as the
  2305. ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
  2306. ; these registers to change.
  2307. push eax
  2308. push ebx
  2309. push ecx
  2310. push edx
  2311. #endif
  2312. ; <<CPUID>>
  2313. ; eax = 0x80000002 --> eax, ebx, ecx, edx: get processor name string (part 1)
  2314. mov eax,0x80000002
  2315. CPUID_INSTRUCTION
  2316. mov CPUExtendedIdentity[0 * TYPE int], eax
  2317. mov CPUExtendedIdentity[1 * TYPE int], ebx
  2318. mov CPUExtendedIdentity[2 * TYPE int], ecx
  2319. mov CPUExtendedIdentity[3 * TYPE int], edx
  2320. ; <<CPUID>>
  2321. ; eax = 0x80000003 --> eax, ebx, ecx, edx: get processor name string (part 2)
  2322. mov eax,0x80000003
  2323. CPUID_INSTRUCTION
  2324. mov CPUExtendedIdentity[4 * TYPE int], eax
  2325. mov CPUExtendedIdentity[5 * TYPE int], ebx
  2326. mov CPUExtendedIdentity[6 * TYPE int], ecx
  2327. mov CPUExtendedIdentity[7 * TYPE int], edx
  2328. ; <<CPUID>>
  2329. ; eax = 0x80000004 --> eax, ebx, ecx, edx: get processor name string (part 3)
  2330. mov eax,0x80000004
  2331. CPUID_INSTRUCTION
  2332. mov CPUExtendedIdentity[8 * TYPE int], eax
  2333. mov CPUExtendedIdentity[9 * TYPE int], ebx
  2334. mov CPUExtendedIdentity[10 * TYPE int], ecx
  2335. mov CPUExtendedIdentity[11 * TYPE int], edx
  2336. #ifdef CPUID_AWARE_COMPILER
  2337. pop edx
  2338. pop ecx
  2339. pop ebx
  2340. pop eax
  2341. #endif
  2342. }
  2343. }
  2344. __except(1)
  2345. {
  2346. return false;
  2347. }
  2348. // Process the returned information.
  2349. char nbuf[49];
  2350. memcpy (&(nbuf[0]), &(CPUExtendedIdentity[0]), sizeof (int));
  2351. memcpy (&(nbuf[4]), &(CPUExtendedIdentity[1]), sizeof (int));
  2352. memcpy (&(nbuf[8]), &(CPUExtendedIdentity[2]), sizeof (int));
  2353. memcpy (&(nbuf[12]), &(CPUExtendedIdentity[3]), sizeof (int));
  2354. memcpy (&(nbuf[16]), &(CPUExtendedIdentity[4]), sizeof (int));
  2355. memcpy (&(nbuf[20]), &(CPUExtendedIdentity[5]), sizeof (int));
  2356. memcpy (&(nbuf[24]), &(CPUExtendedIdentity[6]), sizeof (int));
  2357. memcpy (&(nbuf[28]), &(CPUExtendedIdentity[7]), sizeof (int));
  2358. memcpy (&(nbuf[32]), &(CPUExtendedIdentity[8]), sizeof (int));
  2359. memcpy (&(nbuf[36]), &(CPUExtendedIdentity[9]), sizeof (int));
  2360. memcpy (&(nbuf[40]), &(CPUExtendedIdentity[10]), sizeof (int));
  2361. memcpy (&(nbuf[44]), &(CPUExtendedIdentity[11]), sizeof (int));
  2362. nbuf[48] = '\0';
  2363. this->ChipID.ProcessorName = nbuf;
  2364. this->ChipID.ModelName = nbuf;
  2365. // Because some manufacturers have leading white space - we have to post-process the name.
  2366. SystemInformationStripLeadingSpace(this->ChipID.ProcessorName);
  2367. return true;
  2368. #else
  2369. return false;
  2370. #endif
  2371. }
  2372. /** */
  2373. bool SystemInformationImplementation::RetrieveClassicalCPUIdentity()
  2374. {
  2375. // Start by decided which manufacturer we are using....
  2376. switch (this->ChipManufacturer)
  2377. {
  2378. case Intel:
  2379. // Check the family / model / revision to determine the CPU ID.
  2380. switch (this->ChipID.Family) {
  2381. case 3:
  2382. this->ChipID.ProcessorName = "Newer i80386 family";
  2383. break;
  2384. case 4:
  2385. switch (this->ChipID.Model) {
  2386. case 0: this->ChipID.ProcessorName = "i80486DX-25/33"; break;
  2387. case 1: this->ChipID.ProcessorName = "i80486DX-50"; break;
  2388. case 2: this->ChipID.ProcessorName = "i80486SX"; break;
  2389. case 3: this->ChipID.ProcessorName = "i80486DX2"; break;
  2390. case 4: this->ChipID.ProcessorName = "i80486SL"; break;
  2391. case 5: this->ChipID.ProcessorName = "i80486SX2"; break;
  2392. case 7: this->ChipID.ProcessorName = "i80486DX2 WriteBack"; break;
  2393. case 8: this->ChipID.ProcessorName = "i80486DX4"; break;
  2394. case 9: this->ChipID.ProcessorName = "i80486DX4 WriteBack"; break;
  2395. default: this->ChipID.ProcessorName = "Unknown 80486 family"; return false;
  2396. }
  2397. break;
  2398. case 5:
  2399. switch (this->ChipID.Model)
  2400. {
  2401. case 0: this->ChipID.ProcessorName = "P5 A-Step"; break;
  2402. case 1: this->ChipID.ProcessorName = "P5"; break;
  2403. case 2: this->ChipID.ProcessorName = "P54C"; break;
  2404. case 3: this->ChipID.ProcessorName = "P24T OverDrive"; break;
  2405. case 4: this->ChipID.ProcessorName = "P55C"; break;
  2406. case 7: this->ChipID.ProcessorName = "P54C"; break;
  2407. case 8: this->ChipID.ProcessorName = "P55C (0.25micron)"; break;
  2408. default: this->ChipID.ProcessorName = "Unknown Pentium family"; return false;
  2409. }
  2410. break;
  2411. case 6:
  2412. switch (this->ChipID.Model)
  2413. {
  2414. case 0: this->ChipID.ProcessorName = "P6 A-Step"; break;
  2415. case 1: this->ChipID.ProcessorName = "P6"; break;
  2416. case 3: this->ChipID.ProcessorName = "Pentium II (0.28 micron)"; break;
  2417. case 5: this->ChipID.ProcessorName = "Pentium II (0.25 micron)"; break;
  2418. case 6: this->ChipID.ProcessorName = "Pentium II With On-Die L2 Cache"; break;
  2419. case 7: this->ChipID.ProcessorName = "Pentium III (0.25 micron)"; break;
  2420. case 8: this->ChipID.ProcessorName = "Pentium III (0.18 micron) With 256 KB On-Die L2 Cache "; break;
  2421. case 0xa: this->ChipID.ProcessorName = "Pentium III (0.18 micron) With 1 Or 2 MB On-Die L2 Cache "; break;
  2422. case 0xb: this->ChipID.ProcessorName = "Pentium III (0.13 micron) With 256 Or 512 KB On-Die L2 Cache "; break;
  2423. case 23: this->ChipID.ProcessorName = "Intel(R) Core(TM)2 Duo CPU T9500 @ 2.60GHz"; break;
  2424. default: this->ChipID.ProcessorName = "Unknown P6 family"; return false;
  2425. }
  2426. break;
  2427. case 7:
  2428. this->ChipID.ProcessorName = "Intel Merced (IA-64)";
  2429. break;
  2430. case 0xf:
  2431. // Check the extended family bits...
  2432. switch (this->ChipID.ExtendedFamily)
  2433. {
  2434. case 0:
  2435. switch (this->ChipID.Model)
  2436. {
  2437. case 0: this->ChipID.ProcessorName = "Pentium IV (0.18 micron)"; break;
  2438. case 1: this->ChipID.ProcessorName = "Pentium IV (0.18 micron)"; break;
  2439. case 2: this->ChipID.ProcessorName = "Pentium IV (0.13 micron)"; break;
  2440. default: this->ChipID.ProcessorName = "Unknown Pentium 4 family"; return false;
  2441. }
  2442. break;
  2443. case 1:
  2444. this->ChipID.ProcessorName = "Intel McKinley (IA-64)";
  2445. break;
  2446. default:
  2447. this->ChipID.ProcessorName = "Pentium";
  2448. }
  2449. break;
  2450. default:
  2451. this->ChipID.ProcessorName = "Unknown Intel family";
  2452. return false;
  2453. }
  2454. break;
  2455. case AMD:
  2456. // Check the family / model / revision to determine the CPU ID.
  2457. switch (this->ChipID.Family)
  2458. {
  2459. case 4:
  2460. switch (this->ChipID.Model)
  2461. {
  2462. case 3: this->ChipID.ProcessorName = "80486DX2"; break;
  2463. case 7: this->ChipID.ProcessorName = "80486DX2 WriteBack"; break;
  2464. case 8: this->ChipID.ProcessorName = "80486DX4"; break;
  2465. case 9: this->ChipID.ProcessorName = "80486DX4 WriteBack"; break;
  2466. case 0xe: this->ChipID.ProcessorName = "5x86"; break;
  2467. case 0xf: this->ChipID.ProcessorName = "5x86WB"; break;
  2468. default: this->ChipID.ProcessorName = "Unknown 80486 family"; return false;
  2469. }
  2470. break;
  2471. case 5:
  2472. switch (this->ChipID.Model)
  2473. {
  2474. case 0: this->ChipID.ProcessorName = "SSA5 (PR75, PR90 = PR100)"; break;
  2475. case 1: this->ChipID.ProcessorName = "5k86 (PR120 = PR133)"; break;
  2476. case 2: this->ChipID.ProcessorName = "5k86 (PR166)"; break;
  2477. case 3: this->ChipID.ProcessorName = "5k86 (PR200)"; break;
  2478. case 6: this->ChipID.ProcessorName = "K6 (0.30 micron)"; break;
  2479. case 7: this->ChipID.ProcessorName = "K6 (0.25 micron)"; break;
  2480. case 8: this->ChipID.ProcessorName = "K6-2"; break;
  2481. case 9: this->ChipID.ProcessorName = "K6-III"; break;
  2482. case 0xd: this->ChipID.ProcessorName = "K6-2+ or K6-III+ (0.18 micron)"; break;
  2483. default: this->ChipID.ProcessorName = "Unknown 80586 family"; return false;
  2484. }
  2485. break;
  2486. case 6:
  2487. switch (this->ChipID.Model)
  2488. {
  2489. case 1: this->ChipID.ProcessorName = "Athlon- (0.25 micron)"; break;
  2490. case 2: this->ChipID.ProcessorName = "Athlon- (0.18 micron)"; break;
  2491. case 3: this->ChipID.ProcessorName = "Duron- (SF core)"; break;
  2492. case 4: this->ChipID.ProcessorName = "Athlon- (Thunderbird core)"; break;
  2493. case 6: this->ChipID.ProcessorName = "Athlon- (Palomino core)"; break;
  2494. case 7: this->ChipID.ProcessorName = "Duron- (Morgan core)"; break;
  2495. case 8:
  2496. if (this->Features.ExtendedFeatures.SupportsMP)
  2497. this->ChipID.ProcessorName = "Athlon - MP (Thoroughbred core)";
  2498. else this->ChipID.ProcessorName = "Athlon - XP (Thoroughbred core)";
  2499. break;
  2500. default: this->ChipID.ProcessorName = "Unknown K7 family"; return false;
  2501. }
  2502. break;
  2503. default:
  2504. this->ChipID.ProcessorName = "Unknown AMD family";
  2505. return false;
  2506. }
  2507. break;
  2508. case Transmeta:
  2509. switch (this->ChipID.Family)
  2510. {
  2511. case 5:
  2512. switch (this->ChipID.Model)
  2513. {
  2514. case 4: this->ChipID.ProcessorName = "Crusoe TM3x00 and TM5x00"; break;
  2515. default: this->ChipID.ProcessorName = "Unknown Crusoe family"; return false;
  2516. }
  2517. break;
  2518. default:
  2519. this->ChipID.ProcessorName = "Unknown Transmeta family";
  2520. return false;
  2521. }
  2522. break;
  2523. case Rise:
  2524. switch (this->ChipID.Family)
  2525. {
  2526. case 5:
  2527. switch (this->ChipID.Model)
  2528. {
  2529. case 0: this->ChipID.ProcessorName = "mP6 (0.25 micron)"; break;
  2530. case 2: this->ChipID.ProcessorName = "mP6 (0.18 micron)"; break;
  2531. default: this->ChipID.ProcessorName = "Unknown Rise family"; return false;
  2532. }
  2533. break;
  2534. default:
  2535. this->ChipID.ProcessorName = "Unknown Rise family";
  2536. return false;
  2537. }
  2538. break;
  2539. case UMC:
  2540. switch (this->ChipID.Family)
  2541. {
  2542. case 4:
  2543. switch (this->ChipID.Model)
  2544. {
  2545. case 1: this->ChipID.ProcessorName = "U5D"; break;
  2546. case 2: this->ChipID.ProcessorName = "U5S"; break;
  2547. default: this->ChipID.ProcessorName = "Unknown UMC family"; return false;
  2548. }
  2549. break;
  2550. default:
  2551. this->ChipID.ProcessorName = "Unknown UMC family";
  2552. return false;
  2553. }
  2554. break;
  2555. case IDT:
  2556. switch (this->ChipID.Family)
  2557. {
  2558. case 5:
  2559. switch (this->ChipID.Model)
  2560. {
  2561. case 4: this->ChipID.ProcessorName = "C6"; break;
  2562. case 8: this->ChipID.ProcessorName = "C2"; break;
  2563. case 9: this->ChipID.ProcessorName = "C3"; break;
  2564. default: this->ChipID.ProcessorName = "Unknown IDT\\Centaur family"; return false;
  2565. }
  2566. break;
  2567. case 6:
  2568. switch (this->ChipID.Model)
  2569. {
  2570. case 6: this->ChipID.ProcessorName = "VIA Cyrix III - Samuel"; break;
  2571. default: this->ChipID.ProcessorName = "Unknown IDT\\Centaur family"; return false;
  2572. }
  2573. break;
  2574. default:
  2575. this->ChipID.ProcessorName = "Unknown IDT\\Centaur family";
  2576. return false;
  2577. }
  2578. break;
  2579. case Cyrix:
  2580. switch (this->ChipID.Family)
  2581. {
  2582. case 4:
  2583. switch (this->ChipID.Model)
  2584. {
  2585. case 4: this->ChipID.ProcessorName = "MediaGX GX = GXm"; break;
  2586. case 9: this->ChipID.ProcessorName = "5x86"; break;
  2587. default: this->ChipID.ProcessorName = "Unknown Cx5x86 family"; return false;
  2588. }
  2589. break;
  2590. case 5:
  2591. switch (this->ChipID.Model)
  2592. {
  2593. case 2: this->ChipID.ProcessorName = "Cx6x86"; break;
  2594. case 4: this->ChipID.ProcessorName = "MediaGX GXm"; break;
  2595. default: this->ChipID.ProcessorName = "Unknown Cx6x86 family"; return false;
  2596. }
  2597. break;
  2598. case 6:
  2599. switch (this->ChipID.Model)
  2600. {
  2601. case 0: this->ChipID.ProcessorName = "6x86MX"; break;
  2602. case 5: this->ChipID.ProcessorName = "Cyrix M2 Core"; break;
  2603. case 6: this->ChipID.ProcessorName = "WinChip C5A Core"; break;
  2604. case 7: this->ChipID.ProcessorName = "WinChip C5B\\C5C Core"; break;
  2605. case 8: this->ChipID.ProcessorName = "WinChip C5C-T Core"; break;
  2606. default: this->ChipID.ProcessorName = "Unknown 6x86MX\\Cyrix III family"; return false;
  2607. }
  2608. break;
  2609. default:
  2610. this->ChipID.ProcessorName = "Unknown Cyrix family";
  2611. return false;
  2612. }
  2613. break;
  2614. case NexGen:
  2615. switch (this->ChipID.Family)
  2616. {
  2617. case 5:
  2618. switch (this->ChipID.Model)
  2619. {
  2620. case 0: this->ChipID.ProcessorName = "Nx586 or Nx586FPU"; break;
  2621. default: this->ChipID.ProcessorName = "Unknown NexGen family"; return false;
  2622. }
  2623. break;
  2624. default:
  2625. this->ChipID.ProcessorName = "Unknown NexGen family";
  2626. return false;
  2627. }
  2628. break;
  2629. case NSC:
  2630. this->ChipID.ProcessorName = "Cx486SLC \\ DLC \\ Cx486S A-Step";
  2631. break;
  2632. default:
  2633. this->ChipID.ProcessorName = "Unknown family"; // We cannot identify the processor.
  2634. return false;
  2635. }
  2636. return true;
  2637. }
  2638. /** Extract a value from the CPUInfo file */
  2639. kwsys_stl::string SystemInformationImplementation::ExtractValueFromCpuInfoFile(kwsys_stl::string buffer,const char* word,size_t init)
  2640. {
  2641. size_t pos = buffer.find(word,init);
  2642. if(pos != buffer.npos)
  2643. {
  2644. this->CurrentPositionInFile = pos;
  2645. pos = buffer.find(":",pos);
  2646. size_t pos2 = buffer.find("\n",pos);
  2647. if(pos!=buffer.npos && pos2!=buffer.npos)
  2648. {
  2649. return buffer.substr(pos+2,pos2-pos-2);
  2650. }
  2651. }
  2652. this->CurrentPositionInFile = buffer.npos;
  2653. return "";
  2654. }
  2655. /** Query for the cpu status */
  2656. int SystemInformationImplementation::RetreiveInformationFromCpuInfoFile()
  2657. {
  2658. this->NumberOfLogicalCPU = 0;
  2659. this->NumberOfPhysicalCPU = 0;
  2660. kwsys_stl::string buffer;
  2661. FILE *fd = fopen("/proc/cpuinfo", "r" );
  2662. if ( !fd )
  2663. {
  2664. kwsys_ios::cout << "Problem opening /proc/cpuinfo" << kwsys_ios::endl;
  2665. return 0;
  2666. }
  2667. size_t fileSize = 0;
  2668. while(!feof(fd))
  2669. {
  2670. buffer += static_cast<char>(fgetc(fd));
  2671. fileSize++;
  2672. }
  2673. fclose( fd );
  2674. buffer.resize(fileSize-2);
  2675. // Number of logical CPUs (combination of multiple processors, multi-core
  2676. // and hyperthreading)
  2677. size_t pos = buffer.find("processor\t");
  2678. while(pos != buffer.npos)
  2679. {
  2680. this->NumberOfLogicalCPU++;
  2681. pos = buffer.find("processor\t",pos+1);
  2682. }
  2683. #ifdef __linux
  2684. // Find the largest physical id.
  2685. int maxId = -1;
  2686. kwsys_stl::string idc =
  2687. this->ExtractValueFromCpuInfoFile(buffer,"physical id");
  2688. while(this->CurrentPositionInFile != buffer.npos)
  2689. {
  2690. int id = atoi(idc.c_str());
  2691. if(id > maxId)
  2692. {
  2693. maxId=id;
  2694. }
  2695. idc = this->ExtractValueFromCpuInfoFile(buffer,"physical id",
  2696. this->CurrentPositionInFile+1);
  2697. }
  2698. // Physical ids returned by Linux don't distinguish cores.
  2699. // We want to record the total number of cores in this->NumberOfPhysicalCPU
  2700. // (checking only the first proc)
  2701. kwsys_stl::string cores =
  2702. this->ExtractValueFromCpuInfoFile(buffer,"cpu cores");
  2703. int numberOfCoresPerCPU=atoi(cores.c_str());
  2704. this->NumberOfPhysicalCPU=static_cast<unsigned int>(
  2705. numberOfCoresPerCPU*(maxId+1));
  2706. #else // __CYGWIN__
  2707. // does not have "physical id" entries, neither "cpu cores"
  2708. // this has to be fixed for hyper-threading.
  2709. kwsys_stl::string cpucount =
  2710. this->ExtractValueFromCpuInfoFile(buffer,"cpu count");
  2711. this->NumberOfPhysicalCPU=
  2712. this->NumberOfLogicalCPU = atoi(cpucount.c_str());
  2713. #endif
  2714. // gotta have one, and if this is 0 then we get a / by 0n
  2715. // beter to have a bad answer than a crash
  2716. if(this->NumberOfPhysicalCPU <= 0)
  2717. {
  2718. this->NumberOfPhysicalCPU = 1;
  2719. }
  2720. // LogicalProcessorsPerPhysical>1 => hyperthreading.
  2721. this->Features.ExtendedFeatures.LogicalProcessorsPerPhysical=
  2722. this->NumberOfLogicalCPU/this->NumberOfPhysicalCPU;
  2723. // CPU speed (checking only the first proc
  2724. kwsys_stl::string CPUSpeed = this->ExtractValueFromCpuInfoFile(buffer,"cpu MHz");
  2725. this->CPUSpeedInMHz = static_cast<float>(atof(CPUSpeed.c_str()));
  2726. // Chip family
  2727. this->ChipID.Family = atoi(this->ExtractValueFromCpuInfoFile(buffer,"cpu family").c_str());
  2728. // Chip Vendor
  2729. this->ChipID.Vendor = this->ExtractValueFromCpuInfoFile(buffer,"vendor_id");
  2730. this->FindManufacturer();
  2731. // Chip Model
  2732. this->ChipID.Model = atoi(this->ExtractValueFromCpuInfoFile(buffer,"model").c_str());
  2733. this->RetrieveClassicalCPUIdentity();
  2734. // Chip Model Name
  2735. this->ChipID.ModelName = this->ExtractValueFromCpuInfoFile(buffer,"model name").c_str();
  2736. // L1 Cache size
  2737. kwsys_stl::string cacheSize = this->ExtractValueFromCpuInfoFile(buffer,"cache size");
  2738. pos = cacheSize.find(" KB");
  2739. if(pos!=cacheSize.npos)
  2740. {
  2741. cacheSize = cacheSize.substr(0,pos);
  2742. }
  2743. this->Features.L1CacheSize = atoi(cacheSize.c_str());
  2744. return 1;
  2745. }
  2746. /**
  2747. Get total system RAM in units of KiB.
  2748. */
  2749. SystemInformation::LongLong
  2750. SystemInformationImplementation::GetHostMemoryTotal()
  2751. {
  2752. #if defined(_WIN32)
  2753. # if defined(_MSC_VER) && _MSC_VER < 1300
  2754. MEMORYSTATUS stat;
  2755. stat.dwLength = sizeof(stat);
  2756. GlobalMemoryStatus(&stat);
  2757. return stat.dwTotalPhys/1024;
  2758. # else
  2759. MEMORYSTATUSEX statex;
  2760. statex.dwLength=sizeof(statex);
  2761. GlobalMemoryStatusEx(&statex);
  2762. return statex.ullTotalPhys/1024;
  2763. # endif
  2764. #elif defined(__linux)
  2765. SystemInformation::LongLong memTotal=0;
  2766. int ierr=GetFieldFromFile("/proc/meminfo","MemTotal:",memTotal);
  2767. if (ierr)
  2768. {
  2769. return -1;
  2770. }
  2771. return memTotal;
  2772. #elif defined(__APPLE__)
  2773. uint64_t mem;
  2774. size_t len = sizeof(mem);
  2775. int ierr=sysctlbyname("hw.memsize", &mem, &len, NULL, 0);
  2776. if (ierr)
  2777. {
  2778. return -1;
  2779. }
  2780. return mem/1024;
  2781. #else
  2782. return 0;
  2783. #endif
  2784. }
  2785. /**
  2786. Get total system RAM in units of KiB. This may differ from the
  2787. host total if a host-wide resource limit is applied.
  2788. */
  2789. SystemInformation::LongLong
  2790. SystemInformationImplementation::GetHostMemoryAvailable(const char *hostLimitEnvVarName)
  2791. {
  2792. SystemInformation::LongLong memTotal=this->GetHostMemoryTotal();
  2793. // the following mechanism is provided for systems that
  2794. // apply resource limits across groups of processes.
  2795. // this is of use on certain SMP systems (eg. SGI UV)
  2796. // where the host has a large amount of ram but a given user's
  2797. // access to it is severly restricted. The system will
  2798. // apply a limit across a set of processes. Units are in KiB.
  2799. if (hostLimitEnvVarName)
  2800. {
  2801. const char *hostLimitEnvVarValue=getenv(hostLimitEnvVarName);
  2802. if (hostLimitEnvVarValue)
  2803. {
  2804. SystemInformation::LongLong hostLimit=atoLongLong(hostLimitEnvVarValue);
  2805. if (hostLimit>0)
  2806. {
  2807. memTotal=min(hostLimit,memTotal);
  2808. }
  2809. }
  2810. }
  2811. return memTotal;
  2812. }
  2813. /**
  2814. Get total system RAM in units of KiB. This may differ from the
  2815. host total if a per-process resource limit is applied.
  2816. */
  2817. SystemInformation::LongLong
  2818. SystemInformationImplementation::GetProcMemoryAvailable(
  2819. const char *hostLimitEnvVarName,
  2820. const char *procLimitEnvVarName)
  2821. {
  2822. SystemInformation::LongLong memAvail
  2823. = this->GetHostMemoryAvailable(hostLimitEnvVarName);
  2824. // the following mechanism is provide for systems where rlimits
  2825. // are not employed. Units are in KiB.
  2826. if (procLimitEnvVarName)
  2827. {
  2828. const char *procLimitEnvVarValue=getenv(procLimitEnvVarName);
  2829. if (procLimitEnvVarValue)
  2830. {
  2831. SystemInformation::LongLong procLimit=atoLongLong(procLimitEnvVarValue);
  2832. if (procLimit>0)
  2833. {
  2834. memAvail=min(procLimit,memAvail);
  2835. }
  2836. }
  2837. }
  2838. #if defined(__linux)
  2839. int ierr;
  2840. ResourceLimitType rlim;
  2841. ierr=GetResourceLimit(RLIMIT_DATA,&rlim);
  2842. if ((ierr==0) && (rlim.rlim_cur != RLIM_INFINITY))
  2843. {
  2844. memAvail=min((SystemInformation::LongLong)rlim.rlim_cur/1024,memAvail);
  2845. }
  2846. ierr=GetResourceLimit(RLIMIT_AS,&rlim);
  2847. if ((ierr==0) && (rlim.rlim_cur != RLIM_INFINITY))
  2848. {
  2849. memAvail=min((SystemInformation::LongLong)rlim.rlim_cur/1024,memAvail);
  2850. }
  2851. #elif defined(__APPLE__)
  2852. struct rlimit rlim;
  2853. int ierr;
  2854. ierr=getrlimit(RLIMIT_DATA,&rlim);
  2855. if ((ierr==0) && (rlim.rlim_cur != RLIM_INFINITY))
  2856. {
  2857. memAvail=min((SystemInformation::LongLong)rlim.rlim_cur/1024,memAvail);
  2858. }
  2859. ierr=getrlimit(RLIMIT_RSS,&rlim);
  2860. if ((ierr==0) && (rlim.rlim_cur != RLIM_INFINITY))
  2861. {
  2862. memAvail=min((SystemInformation::LongLong)rlim.rlim_cur/1024,memAvail);
  2863. }
  2864. #endif
  2865. return memAvail;
  2866. }
  2867. /**
  2868. Get RAM used by all processes in the host, in units of KiB.
  2869. */
  2870. SystemInformation::LongLong
  2871. SystemInformationImplementation::GetHostMemoryUsed()
  2872. {
  2873. #if defined(_WIN32)
  2874. # if defined(_MSC_VER) && _MSC_VER < 1300
  2875. MEMORYSTATUS stat;
  2876. stat.dwLength = sizeof(stat);
  2877. GlobalMemoryStatus(&stat);
  2878. return (stat.dwTotalPhys - stat.dwAvailPhys)/1024;
  2879. # else
  2880. MEMORYSTATUSEX statex;
  2881. statex.dwLength=sizeof(statex);
  2882. GlobalMemoryStatusEx(&statex);
  2883. return (statex.ullTotalPhys - statex.ullAvailPhys)/1024;
  2884. # endif
  2885. #elif defined(__linux)
  2886. const char *names[3]={"MemTotal:","MemFree:",NULL};
  2887. SystemInformation::LongLong values[2]={SystemInformation::LongLong(0)};
  2888. int ierr=GetFieldsFromFile("/proc/meminfo",names,values);
  2889. if (ierr)
  2890. {
  2891. return ierr;
  2892. }
  2893. SystemInformation::LongLong &memTotal=values[0];
  2894. SystemInformation::LongLong &memFree=values[1];
  2895. return memTotal - memFree;
  2896. #elif defined(__APPLE__)
  2897. SystemInformation::LongLong psz=getpagesize();
  2898. if (psz<1)
  2899. {
  2900. return -1;
  2901. }
  2902. const char *names[4]={"Pages active:","Pages inactive:","Pages wired down:",NULL};
  2903. SystemInformation::LongLong values[3]={SystemInformation::LongLong(0)};
  2904. int ierr=GetFieldsFromCommand("vm_stat", names, values);
  2905. if (ierr)
  2906. {
  2907. return -1;
  2908. }
  2909. SystemInformation::LongLong &vmActive=values[0];
  2910. SystemInformation::LongLong &vmInactive=values[1];
  2911. SystemInformation::LongLong &vmWired=values[2];
  2912. return ((vmActive+vmInactive+vmWired)*psz)/1024;
  2913. #else
  2914. return 0;
  2915. #endif
  2916. }
  2917. /**
  2918. Get system RAM used by the process associated with the given
  2919. process id in units of KiB.
  2920. */
  2921. SystemInformation::LongLong
  2922. SystemInformationImplementation::GetProcMemoryUsed()
  2923. {
  2924. #if defined(_WIN32) && defined(KWSYS_SYS_HAS_PSAPI)
  2925. long pid=GetCurrentProcessId();
  2926. HANDLE hProc;
  2927. hProc=OpenProcess(
  2928. PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,
  2929. false,
  2930. pid);
  2931. if (hProc==0)
  2932. {
  2933. return -1;
  2934. }
  2935. PROCESS_MEMORY_COUNTERS pmc;
  2936. int ok=GetProcessMemoryInfo(hProc,&pmc,sizeof(pmc));
  2937. CloseHandle(hProc);
  2938. if (!ok)
  2939. {
  2940. return -2;
  2941. }
  2942. return pmc.WorkingSetSize/1024;
  2943. #elif defined(__linux)
  2944. SystemInformation::LongLong memUsed=0;
  2945. int ierr=GetFieldFromFile("/proc/self/status","VmRSS:",memUsed);
  2946. if (ierr)
  2947. {
  2948. return -1;
  2949. }
  2950. return memUsed;
  2951. #elif defined(__APPLE__)
  2952. SystemInformation::LongLong memUsed=0;
  2953. pid_t pid=getpid();
  2954. kwsys_ios::ostringstream oss;
  2955. oss << "ps -o rss= -p " << pid;
  2956. FILE *file=popen(oss.str().c_str(),"r");
  2957. if (file==0)
  2958. {
  2959. return -1;
  2960. }
  2961. oss.str("");
  2962. while (!feof(file) && !ferror(file))
  2963. {
  2964. char buf[256]={'\0'};
  2965. errno=0;
  2966. size_t nRead=fread(buf,1,256,file);
  2967. if (ferror(file) && (errno==EINTR))
  2968. {
  2969. clearerr(file);
  2970. }
  2971. if (nRead) oss << buf;
  2972. }
  2973. int ierr=ferror(file);
  2974. pclose(file);
  2975. if (ierr)
  2976. {
  2977. return -2;
  2978. }
  2979. kwsys_ios::istringstream iss(oss.str());
  2980. iss >> memUsed;
  2981. return memUsed;
  2982. #else
  2983. return 0;
  2984. #endif
  2985. }
  2986. /**
  2987. Get the process id of the running process.
  2988. */
  2989. SystemInformation::LongLong
  2990. SystemInformationImplementation::GetProcessId()
  2991. {
  2992. #if defined(_WIN32)
  2993. return GetCurrentProcessId();
  2994. #elif defined(__linux) || defined(__APPLE__)
  2995. return getpid();
  2996. #else
  2997. return -1;
  2998. #endif
  2999. }
  3000. /**
  3001. when set print stack trace in response to common signals.
  3002. */
  3003. void SystemInformationImplementation::SetStackTraceOnError(int enable)
  3004. {
  3005. #if !defined(_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
  3006. static int saOrigValid=0;
  3007. static struct sigaction saSEGVOrig;
  3008. static struct sigaction saTERMOrig;
  3009. static struct sigaction saINTOrig;
  3010. static struct sigaction saILLOrig;
  3011. static struct sigaction saBUSOrig;
  3012. static struct sigaction saFPEOrig;
  3013. if (enable && !saOrigValid)
  3014. {
  3015. // save the current actions
  3016. sigaction(SIGSEGV,0,&saSEGVOrig);
  3017. sigaction(SIGTERM,0,&saTERMOrig);
  3018. sigaction(SIGINT,0,&saINTOrig);
  3019. sigaction(SIGILL,0,&saILLOrig);
  3020. sigaction(SIGBUS,0,&saBUSOrig);
  3021. sigaction(SIGFPE,0,&saFPEOrig);
  3022. // enable read, disable write
  3023. saOrigValid=1;
  3024. // install ours
  3025. struct sigaction sa;
  3026. sa.sa_sigaction=(SigAction)StacktraceSignalHandler;
  3027. sa.sa_flags=SA_SIGINFO|SA_RESTART;
  3028. sigemptyset(&sa.sa_mask);
  3029. sigaction(SIGSEGV,&sa,0);
  3030. sigaction(SIGTERM,&sa,0);
  3031. sigaction(SIGINT,&sa,0);
  3032. sigaction(SIGILL,&sa,0);
  3033. sigaction(SIGBUS,&sa,0);
  3034. sigaction(SIGFPE,&sa,0);
  3035. }
  3036. else
  3037. if (!enable && saOrigValid)
  3038. {
  3039. // restore previous actions
  3040. sigaction(SIGSEGV,&saSEGVOrig,0);
  3041. sigaction(SIGTERM,&saTERMOrig,0);
  3042. sigaction(SIGINT,&saINTOrig,0);
  3043. sigaction(SIGILL,&saILLOrig,0);
  3044. sigaction(SIGBUS,&saBUSOrig,0);
  3045. sigaction(SIGFPE,&saFPEOrig,0);
  3046. // enable write, disable read
  3047. saOrigValid=0;
  3048. }
  3049. #else
  3050. // avoid warning C4100
  3051. (void)enable;
  3052. #endif
  3053. }
  3054. /** Query for the memory status */
  3055. int SystemInformationImplementation::QueryMemory()
  3056. {
  3057. this->TotalVirtualMemory = 0;
  3058. this->TotalPhysicalMemory = 0;
  3059. this->AvailableVirtualMemory = 0;
  3060. this->AvailablePhysicalMemory = 0;
  3061. #ifdef __CYGWIN__
  3062. return 0;
  3063. #elif defined(_WIN32)
  3064. # if defined(_MSC_VER) && _MSC_VER < 1300
  3065. MEMORYSTATUS ms;
  3066. unsigned long tv, tp, av, ap;
  3067. ms.dwLength = sizeof(ms);
  3068. GlobalMemoryStatus(&ms);
  3069. # define MEM_VAL(value) dw##value
  3070. # else
  3071. MEMORYSTATUSEX ms;
  3072. DWORDLONG tv, tp, av, ap;
  3073. ms.dwLength = sizeof(ms);
  3074. if (0 == GlobalMemoryStatusEx(&ms))
  3075. {
  3076. return 0;
  3077. }
  3078. # define MEM_VAL(value) ull##value
  3079. # endif
  3080. tv = ms.MEM_VAL(TotalVirtual);
  3081. tp = ms.MEM_VAL(TotalPhys);
  3082. av = ms.MEM_VAL(AvailVirtual);
  3083. ap = ms.MEM_VAL(AvailPhys);
  3084. this->TotalVirtualMemory = tv>>10>>10;
  3085. this->TotalPhysicalMemory = tp>>10>>10;
  3086. this->AvailableVirtualMemory = av>>10>>10;
  3087. this->AvailablePhysicalMemory = ap>>10>>10;
  3088. return 1;
  3089. #elif defined(__linux)
  3090. unsigned long tv=0;
  3091. unsigned long tp=0;
  3092. unsigned long av=0;
  3093. unsigned long ap=0;
  3094. char buffer[1024]; // for reading lines
  3095. int linuxMajor = 0;
  3096. int linuxMinor = 0;
  3097. // Find the Linux kernel version first
  3098. struct utsname unameInfo;
  3099. int errorFlag = uname(&unameInfo);
  3100. if( errorFlag!=0 )
  3101. {
  3102. kwsys_ios::cout << "Problem calling uname(): " << strerror(errno) << kwsys_ios::endl;
  3103. return 0;
  3104. }
  3105. if( unameInfo.release!=0 && strlen(unameInfo.release)>=3 )
  3106. {
  3107. // release looks like "2.6.3-15mdk-i686-up-4GB"
  3108. char majorChar=unameInfo.release[0];
  3109. char minorChar=unameInfo.release[2];
  3110. if( isdigit(majorChar) )
  3111. {
  3112. linuxMajor=majorChar-'0';
  3113. }
  3114. if( isdigit(minorChar) )
  3115. {
  3116. linuxMinor=minorChar-'0';
  3117. }
  3118. }
  3119. FILE *fd = fopen("/proc/meminfo", "r" );
  3120. if ( !fd )
  3121. {
  3122. kwsys_ios::cout << "Problem opening /proc/meminfo" << kwsys_ios::endl;
  3123. return 0;
  3124. }
  3125. if( linuxMajor>=3 || ( (linuxMajor>=2) && (linuxMinor>=6) ) )
  3126. {
  3127. // new /proc/meminfo format since kernel 2.6.x
  3128. // Rigorously, this test should check from the developping version 2.5.x
  3129. // that introduced the new format...
  3130. enum { mMemTotal, mMemFree, mBuffers, mCached, mSwapTotal, mSwapFree };
  3131. const char* format[6] =
  3132. { "MemTotal:%lu kB", "MemFree:%lu kB", "Buffers:%lu kB",
  3133. "Cached:%lu kB", "SwapTotal:%lu kB", "SwapFree:%lu kB" };
  3134. bool have[6] = { false, false, false, false, false, false };
  3135. unsigned long value[6];
  3136. int count = 0;
  3137. while(fgets(buffer, sizeof(buffer), fd))
  3138. {
  3139. for(int i=0; i < 6; ++i)
  3140. {
  3141. if(!have[i] && sscanf(buffer, format[i], &value[i]) == 1)
  3142. {
  3143. have[i] = true;
  3144. ++count;
  3145. }
  3146. }
  3147. }
  3148. if(count == 6)
  3149. {
  3150. this->TotalPhysicalMemory = value[mMemTotal] / 1024;
  3151. this->AvailablePhysicalMemory =
  3152. (value[mMemFree] + value[mBuffers] + value[mCached]) / 1024;
  3153. this->TotalVirtualMemory = value[mSwapTotal] / 1024;
  3154. this->AvailableVirtualMemory = value[mSwapFree] / 1024;
  3155. }
  3156. else
  3157. {
  3158. kwsys_ios::cout << "Problem parsing /proc/meminfo" << kwsys_ios::endl;
  3159. fclose(fd);
  3160. return 0;
  3161. }
  3162. }
  3163. else
  3164. {
  3165. // /proc/meminfo format for kernel older than 2.6.x
  3166. unsigned long temp;
  3167. unsigned long cachedMem;
  3168. unsigned long buffersMem;
  3169. char *r=fgets(buffer, sizeof(buffer), fd); // Skip "total: used:..."
  3170. int status=0;
  3171. if(r==buffer)
  3172. {
  3173. status+=fscanf(fd, "Mem: %lu %lu %lu %lu %lu %lu\n",
  3174. &tp, &temp, &ap, &temp, &buffersMem, &cachedMem);
  3175. }
  3176. if(status==6)
  3177. {
  3178. status+=fscanf(fd, "Swap: %lu %lu %lu\n", &tv, &temp, &av);
  3179. }
  3180. if(status==9)
  3181. {
  3182. this->TotalVirtualMemory = tv>>10>>10;
  3183. this->TotalPhysicalMemory = tp>>10>>10;
  3184. this->AvailableVirtualMemory = av>>10>>10;
  3185. this->AvailablePhysicalMemory = (ap+buffersMem+cachedMem)>>10>>10;
  3186. }
  3187. else
  3188. {
  3189. kwsys_ios::cout << "Problem parsing /proc/meminfo" << kwsys_ios::endl;
  3190. fclose(fd);
  3191. return 0;
  3192. }
  3193. }
  3194. fclose( fd );
  3195. return 1;
  3196. #elif defined(__hpux)
  3197. unsigned long tv=0;
  3198. unsigned long tp=0;
  3199. unsigned long av=0;
  3200. unsigned long ap=0;
  3201. struct pst_static pst;
  3202. struct pst_dynamic pdy;
  3203. unsigned long ps = 0;
  3204. if (pstat_getstatic(&pst, sizeof(pst), (size_t) 1, 0) != -1)
  3205. {
  3206. ps = pst.page_size;
  3207. tp = pst.physical_memory *ps;
  3208. tv = (pst.physical_memory + pst.pst_maxmem) * ps;
  3209. if (pstat_getdynamic(&pdy, sizeof(pdy), (size_t) 1, 0) != -1)
  3210. {
  3211. ap = tp - pdy.psd_rm * ps;
  3212. av = tv - pdy.psd_vm;
  3213. this->TotalVirtualMemory = tv>>10>>10;
  3214. this->TotalPhysicalMemory = tp>>10>>10;
  3215. this->AvailableVirtualMemory = av>>10>>10;
  3216. this->AvailablePhysicalMemory = ap>>10>>10;
  3217. return 1;
  3218. }
  3219. }
  3220. return 0;
  3221. #else
  3222. return 0;
  3223. #endif
  3224. }
  3225. /** */
  3226. size_t SystemInformationImplementation::GetTotalVirtualMemory()
  3227. {
  3228. return this->TotalVirtualMemory;
  3229. }
  3230. /** */
  3231. size_t SystemInformationImplementation::GetAvailableVirtualMemory()
  3232. {
  3233. return this->AvailableVirtualMemory;
  3234. }
  3235. size_t SystemInformationImplementation::GetTotalPhysicalMemory()
  3236. {
  3237. return this->TotalPhysicalMemory;
  3238. }
  3239. /** */
  3240. size_t SystemInformationImplementation::GetAvailablePhysicalMemory()
  3241. {
  3242. return this->AvailablePhysicalMemory;
  3243. }
  3244. /** Get Cycle differences */
  3245. SystemInformation::LongLong
  3246. SystemInformationImplementation::GetCyclesDifference (DELAY_FUNC DelayFunction,
  3247. unsigned int uiParameter)
  3248. {
  3249. #if USE_ASM_INSTRUCTIONS
  3250. unsigned int edx1, eax1;
  3251. unsigned int edx2, eax2;
  3252. // Calculate the frequency of the CPU instructions.
  3253. __try {
  3254. _asm {
  3255. push uiParameter ; push parameter param
  3256. mov ebx, DelayFunction ; store func in ebx
  3257. RDTSC_INSTRUCTION
  3258. mov esi, eax ; esi = eax
  3259. mov edi, edx ; edi = edx
  3260. call ebx ; call the delay functions
  3261. RDTSC_INSTRUCTION
  3262. pop ebx
  3263. mov edx2, edx ; edx2 = edx
  3264. mov eax2, eax ; eax2 = eax
  3265. mov edx1, edi ; edx2 = edi
  3266. mov eax1, esi ; eax2 = esi
  3267. }
  3268. }
  3269. __except(1)
  3270. {
  3271. return -1;
  3272. }
  3273. return ((((__int64) edx2 << 32) + eax2) - (((__int64) edx1 << 32) + eax1));
  3274. #else
  3275. (void)DelayFunction;
  3276. (void)uiParameter;
  3277. return -1;
  3278. #endif
  3279. }
  3280. /** Compute the delay overhead */
  3281. void SystemInformationImplementation::DelayOverhead(unsigned int uiMS)
  3282. {
  3283. #if defined(_WIN32)
  3284. LARGE_INTEGER Frequency, StartCounter, EndCounter;
  3285. __int64 x;
  3286. // Get the frequency of the high performance counter.
  3287. if(!QueryPerformanceFrequency (&Frequency))
  3288. {
  3289. return;
  3290. }
  3291. x = Frequency.QuadPart / 1000 * uiMS;
  3292. // Get the starting position of the counter.
  3293. QueryPerformanceCounter (&StartCounter);
  3294. do {
  3295. // Get the ending position of the counter.
  3296. QueryPerformanceCounter (&EndCounter);
  3297. } while (EndCounter.QuadPart - StartCounter.QuadPart == x);
  3298. #endif
  3299. (void)uiMS;
  3300. }
  3301. /** Return the number of logical CPU per physical CPUs Works only for windows */
  3302. unsigned char SystemInformationImplementation::LogicalCPUPerPhysicalCPU(void)
  3303. {
  3304. #ifdef __APPLE__
  3305. size_t len = 4;
  3306. int cores_per_package = 0;
  3307. int err = sysctlbyname("machdep.cpu.cores_per_package", &cores_per_package, &len, NULL, 0);
  3308. if (err != 0)
  3309. {
  3310. return 1; // That name was not found, default to 1
  3311. }
  3312. return static_cast<unsigned char>(cores_per_package);
  3313. #else
  3314. unsigned int Regebx = 0;
  3315. #if USE_ASM_INSTRUCTIONS
  3316. if (!this->IsHyperThreadingSupported())
  3317. {
  3318. return static_cast<unsigned char>(1); // HT not supported
  3319. }
  3320. __asm
  3321. {
  3322. mov eax, 1
  3323. cpuid
  3324. mov Regebx, ebx
  3325. }
  3326. #endif
  3327. return static_cast<unsigned char> ((Regebx & NUM_LOGICAL_BITS) >> 16);
  3328. #endif
  3329. }
  3330. /** Works only for windows */
  3331. unsigned int SystemInformationImplementation::IsHyperThreadingSupported()
  3332. {
  3333. #if USE_ASM_INSTRUCTIONS
  3334. unsigned int Regedx = 0,
  3335. Regeax = 0,
  3336. VendorId[3] = {0, 0, 0};
  3337. __try // Verify cpuid instruction is supported
  3338. {
  3339. __asm
  3340. {
  3341. xor eax, eax // call cpuid with eax = 0
  3342. cpuid // Get vendor id string
  3343. mov VendorId, ebx
  3344. mov VendorId + 4, edx
  3345. mov VendorId + 8, ecx
  3346. mov eax, 1 // call cpuid with eax = 1
  3347. cpuid
  3348. mov Regeax, eax // eax contains family processor type
  3349. mov Regedx, edx // edx has info about the availability of hyper-Threading
  3350. }
  3351. }
  3352. __except (EXCEPTION_EXECUTE_HANDLER)
  3353. {
  3354. return(0); // cpuid is unavailable
  3355. }
  3356. if (((Regeax & FAMILY_ID) == PENTIUM4_ID) || (Regeax & EXT_FAMILY_ID))
  3357. {
  3358. if (VendorId[0] == 'uneG')
  3359. {
  3360. if (VendorId[1] == 'Ieni')
  3361. {
  3362. if (VendorId[2] == 'letn')
  3363. {
  3364. return(Regedx & HT_BIT); // Genuine Intel with hyper-Threading technology
  3365. }
  3366. }
  3367. }
  3368. }
  3369. #endif
  3370. return 0; // Not genuine Intel processor
  3371. }
  3372. /** Return the APIC Id. Works only for windows. */
  3373. unsigned char SystemInformationImplementation::GetAPICId()
  3374. {
  3375. unsigned int Regebx = 0;
  3376. #if USE_ASM_INSTRUCTIONS
  3377. if (!this->IsHyperThreadingSupported())
  3378. {
  3379. return static_cast<unsigned char>(-1); // HT not supported
  3380. } // Logical processor = 1
  3381. __asm
  3382. {
  3383. mov eax, 1
  3384. cpuid
  3385. mov Regebx, ebx
  3386. }
  3387. #endif
  3388. return static_cast<unsigned char>((Regebx & INITIAL_APIC_ID_BITS) >> 24);
  3389. }
  3390. /** Count the number of CPUs. Works only on windows. */
  3391. int SystemInformationImplementation::CPUCount()
  3392. {
  3393. #if defined(_WIN32)
  3394. unsigned char StatusFlag = 0;
  3395. SYSTEM_INFO info;
  3396. this->NumberOfPhysicalCPU = 0;
  3397. this->NumberOfLogicalCPU = 0;
  3398. info.dwNumberOfProcessors = 0;
  3399. GetSystemInfo (&info);
  3400. // Number of physical processors in a non-Intel system
  3401. // or in a 32-bit Intel system with Hyper-Threading technology disabled
  3402. this->NumberOfPhysicalCPU = (unsigned char) info.dwNumberOfProcessors;
  3403. if (this->IsHyperThreadingSupported())
  3404. {
  3405. unsigned char HT_Enabled = 0;
  3406. this->NumberOfLogicalCPU = this->LogicalCPUPerPhysicalCPU();
  3407. if (this->NumberOfLogicalCPU >= 1) // >1 Doesn't mean HT is enabled in the BIOS
  3408. {
  3409. HANDLE hCurrentProcessHandle;
  3410. #ifndef _WIN64
  3411. # define DWORD_PTR DWORD
  3412. #endif
  3413. DWORD_PTR dwProcessAffinity;
  3414. DWORD_PTR dwSystemAffinity;
  3415. DWORD dwAffinityMask;
  3416. // Calculate the appropriate shifts and mask based on the
  3417. // number of logical processors.
  3418. unsigned int i = 1;
  3419. unsigned char PHY_ID_MASK = 0xFF;
  3420. //unsigned char PHY_ID_SHIFT = 0;
  3421. while (i < this->NumberOfLogicalCPU)
  3422. {
  3423. i *= 2;
  3424. PHY_ID_MASK <<= 1;
  3425. // PHY_ID_SHIFT++;
  3426. }
  3427. hCurrentProcessHandle = GetCurrentProcess();
  3428. GetProcessAffinityMask(hCurrentProcessHandle, &dwProcessAffinity,
  3429. &dwSystemAffinity);
  3430. // Check if available process affinity mask is equal to the
  3431. // available system affinity mask
  3432. if (dwProcessAffinity != dwSystemAffinity)
  3433. {
  3434. StatusFlag = HT_CANNOT_DETECT;
  3435. this->NumberOfPhysicalCPU = (unsigned char)-1;
  3436. return StatusFlag;
  3437. }
  3438. dwAffinityMask = 1;
  3439. while (dwAffinityMask != 0 && dwAffinityMask <= dwProcessAffinity)
  3440. {
  3441. // Check if this CPU is available
  3442. if (dwAffinityMask & dwProcessAffinity)
  3443. {
  3444. if (SetProcessAffinityMask(hCurrentProcessHandle,
  3445. dwAffinityMask))
  3446. {
  3447. unsigned char APIC_ID, LOG_ID;
  3448. Sleep(0); // Give OS time to switch CPU
  3449. APIC_ID = GetAPICId();
  3450. LOG_ID = APIC_ID & ~PHY_ID_MASK;
  3451. if (LOG_ID != 0)
  3452. {
  3453. HT_Enabled = 1;
  3454. }
  3455. }
  3456. }
  3457. dwAffinityMask = dwAffinityMask << 1;
  3458. }
  3459. // Reset the processor affinity
  3460. SetProcessAffinityMask(hCurrentProcessHandle, dwProcessAffinity);
  3461. if (this->NumberOfLogicalCPU == 1) // Normal P4 : HT is disabled in hardware
  3462. {
  3463. StatusFlag = HT_DISABLED;
  3464. }
  3465. else
  3466. {
  3467. if (HT_Enabled)
  3468. {
  3469. // Total physical processors in a Hyper-Threading enabled system.
  3470. this->NumberOfPhysicalCPU /= (this->NumberOfLogicalCPU);
  3471. StatusFlag = HT_ENABLED;
  3472. }
  3473. else
  3474. {
  3475. StatusFlag = HT_SUPPORTED_NOT_ENABLED;
  3476. }
  3477. }
  3478. }
  3479. }
  3480. else
  3481. {
  3482. // Processors do not have Hyper-Threading technology
  3483. StatusFlag = HT_NOT_CAPABLE;
  3484. this->NumberOfLogicalCPU = 1;
  3485. }
  3486. return StatusFlag;
  3487. #else
  3488. return 0;
  3489. #endif
  3490. }
  3491. /** Return the number of logical CPUs on the system */
  3492. unsigned int SystemInformationImplementation::GetNumberOfLogicalCPU()
  3493. {
  3494. return this->NumberOfLogicalCPU;
  3495. }
  3496. /** Return the number of physical CPUs on the system */
  3497. unsigned int SystemInformationImplementation::GetNumberOfPhysicalCPU()
  3498. {
  3499. return this->NumberOfPhysicalCPU;
  3500. }
  3501. /** For Mac use sysctlbyname calls to find system info */
  3502. bool SystemInformationImplementation::ParseSysCtl()
  3503. {
  3504. #if defined(__APPLE__)
  3505. char retBuf[128];
  3506. int err = 0;
  3507. uint64_t value = 0;
  3508. size_t len = sizeof(value);
  3509. sysctlbyname("hw.memsize", &value, &len, NULL, 0);
  3510. this->TotalPhysicalMemory = static_cast< size_t >( value/1048576 );
  3511. // Parse values for Mac
  3512. this->AvailablePhysicalMemory = 0;
  3513. vm_statistics_data_t vmstat;
  3514. mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
  3515. if ( host_statistics(mach_host_self(), HOST_VM_INFO,
  3516. (host_info_t) &vmstat, &count) == KERN_SUCCESS )
  3517. {
  3518. len = sizeof(value);
  3519. err = sysctlbyname("hw.pagesize", &value, &len, NULL, 0);
  3520. int64_t available_memory = vmstat.free_count * value;
  3521. this->AvailablePhysicalMemory = static_cast< size_t >( available_memory / 1048576 );
  3522. }
  3523. #ifdef VM_SWAPUSAGE
  3524. // Virtual memory.
  3525. int mib[2] = { CTL_VM, VM_SWAPUSAGE };
  3526. size_t miblen = sizeof(mib) / sizeof(mib[0]);
  3527. struct xsw_usage swap;
  3528. len = sizeof(swap);
  3529. err = sysctl(mib, miblen, &swap, &len, NULL, 0);
  3530. if (err == 0)
  3531. {
  3532. this->AvailableVirtualMemory = static_cast< size_t >( swap.xsu_avail/1048576 );
  3533. this->TotalVirtualMemory = static_cast< size_t >( swap.xsu_total/1048576 );
  3534. }
  3535. #else
  3536. this->AvailableVirtualMemory = 0;
  3537. this->TotalVirtualMemory = 0;
  3538. #endif
  3539. // CPU Info
  3540. len = sizeof(this->NumberOfPhysicalCPU);
  3541. sysctlbyname("hw.physicalcpu", &this->NumberOfPhysicalCPU, &len, NULL, 0);
  3542. len = sizeof(this->NumberOfLogicalCPU);
  3543. sysctlbyname("hw.logicalcpu", &this->NumberOfLogicalCPU, &len, NULL, 0);
  3544. this->Features.ExtendedFeatures.LogicalProcessorsPerPhysical =
  3545. this->LogicalCPUPerPhysicalCPU();
  3546. len = sizeof(value);
  3547. sysctlbyname("hw.cpufrequency", &value, &len, NULL, 0);
  3548. this->CPUSpeedInMHz = static_cast< float >( value )/ 1000000;
  3549. // Chip family
  3550. len = sizeof(this->ChipID.Family);
  3551. //Seems only the intel chips will have this name so if this fails it is
  3552. //probably a PPC machine
  3553. err = sysctlbyname("machdep.cpu.family",
  3554. &this->ChipID.Family, &len, NULL, 0);
  3555. if (err != 0) // Go back to names we know but are less descriptive
  3556. {
  3557. this->ChipID.Family = 0;
  3558. ::memset(retBuf, 0, 128);
  3559. len = 32;
  3560. err = sysctlbyname("hw.machine", &retBuf, &len, NULL, 0);
  3561. kwsys_stl::string machineBuf(retBuf);
  3562. if (machineBuf.find_first_of("Power") != kwsys_stl::string::npos)
  3563. {
  3564. this->ChipID.Vendor = "IBM";
  3565. len = sizeof(this->ChipID.Family);
  3566. err = sysctlbyname("hw.cputype", &this->ChipID.Family, &len, NULL, 0);
  3567. len = sizeof(this->ChipID.Model);
  3568. err = sysctlbyname("hw.cpusubtype", &this->ChipID.Model, &len, NULL, 0);
  3569. this->FindManufacturer();
  3570. }
  3571. }
  3572. else // Should be an Intel Chip.
  3573. {
  3574. len = sizeof(this->ChipID.Family);
  3575. err =
  3576. sysctlbyname("machdep.cpu.family", &this->ChipID.Family, &len, NULL, 0);
  3577. ::memset(retBuf, 0, 128);
  3578. len = 128;
  3579. err = sysctlbyname("machdep.cpu.vendor", retBuf, &len, NULL, 0);
  3580. // Chip Vendor
  3581. this->ChipID.Vendor = retBuf;
  3582. this->FindManufacturer();
  3583. // Chip Model
  3584. len = sizeof(value);
  3585. err = sysctlbyname("machdep.cpu.model", &value, &len, NULL, 0);
  3586. this->ChipID.Model = static_cast< int >( value );
  3587. }
  3588. // brand string
  3589. ::memset(retBuf, 0, sizeof(retBuf));
  3590. len = sizeof(retBuf);
  3591. err = sysctlbyname("machdep.cpu.brand_string", retBuf, &len, NULL, 0);
  3592. if (!err)
  3593. {
  3594. this->ChipID.ProcessorName = retBuf;
  3595. this->ChipID.ModelName = retBuf;
  3596. }
  3597. // Cache size
  3598. len = sizeof(value);
  3599. err = sysctlbyname("hw.l1icachesize", &value, &len, NULL, 0);
  3600. this->Features.L1CacheSize = static_cast< int >( value );
  3601. len = sizeof(value);
  3602. err = sysctlbyname("hw.l2cachesize", &value, &len, NULL, 0);
  3603. this->Features.L2CacheSize = static_cast< int >( value );
  3604. return true;
  3605. #else
  3606. return false;
  3607. #endif
  3608. }
  3609. /** Extract a value from sysctl command */
  3610. kwsys_stl::string SystemInformationImplementation::ExtractValueFromSysCtl(const char* word)
  3611. {
  3612. size_t pos = this->SysCtlBuffer.find(word);
  3613. if(pos != this->SysCtlBuffer.npos)
  3614. {
  3615. pos = this->SysCtlBuffer.find(": ",pos);
  3616. size_t pos2 = this->SysCtlBuffer.find("\n",pos);
  3617. if(pos!=this->SysCtlBuffer.npos && pos2!=this->SysCtlBuffer.npos)
  3618. {
  3619. return this->SysCtlBuffer.substr(pos+2,pos2-pos-2);
  3620. }
  3621. }
  3622. return "";
  3623. }
  3624. /** Run a given process */
  3625. kwsys_stl::string SystemInformationImplementation::RunProcess(kwsys_stl::vector<const char*> args)
  3626. {
  3627. kwsys_stl::string buffer = "";
  3628. // Run the application
  3629. kwsysProcess* gp = kwsysProcess_New();
  3630. kwsysProcess_SetCommand(gp, &*args.begin());
  3631. kwsysProcess_SetOption(gp,kwsysProcess_Option_HideWindow,1);
  3632. kwsysProcess_Execute(gp);
  3633. char* data = NULL;
  3634. int length;
  3635. double timeout = 255;
  3636. while(kwsysProcess_WaitForData(gp,&data,&length,&timeout)) // wait for 1s
  3637. {
  3638. for(int i=0;i<length;i++)
  3639. {
  3640. buffer += data[i];
  3641. }
  3642. }
  3643. kwsysProcess_WaitForExit(gp, 0);
  3644. int result = 0;
  3645. switch(kwsysProcess_GetState(gp))
  3646. {
  3647. case kwsysProcess_State_Exited:
  3648. {
  3649. result = kwsysProcess_GetExitValue(gp);
  3650. } break;
  3651. case kwsysProcess_State_Error:
  3652. {
  3653. kwsys_ios::cerr << "Error: Could not run " << args[0] << ":\n";
  3654. kwsys_ios::cerr << kwsysProcess_GetErrorString(gp) << "\n";
  3655. } break;
  3656. case kwsysProcess_State_Exception:
  3657. {
  3658. kwsys_ios::cerr << "Error: " << args[0]
  3659. << " terminated with an exception: "
  3660. << kwsysProcess_GetExceptionString(gp) << "\n";
  3661. } break;
  3662. case kwsysProcess_State_Starting:
  3663. case kwsysProcess_State_Executing:
  3664. case kwsysProcess_State_Expired:
  3665. case kwsysProcess_State_Killed:
  3666. {
  3667. // Should not get here.
  3668. kwsys_ios::cerr << "Unexpected ending state after running " << args[0]
  3669. << kwsys_ios::endl;
  3670. } break;
  3671. }
  3672. kwsysProcess_Delete(gp);
  3673. if(result)
  3674. {
  3675. kwsys_ios::cerr << "Error " << args[0] << " returned :" << result << "\n";
  3676. }
  3677. return buffer;
  3678. }
  3679. kwsys_stl::string SystemInformationImplementation::ParseValueFromKStat(const char* arguments)
  3680. {
  3681. kwsys_stl::vector<const char*> args;
  3682. args.clear();
  3683. args.push_back("kstat");
  3684. args.push_back("-p");
  3685. kwsys_stl::string command = arguments;
  3686. size_t start = command.npos;
  3687. size_t pos = command.find(' ',0);
  3688. while(pos!=command.npos)
  3689. {
  3690. bool inQuotes = false;
  3691. // Check if we are between quotes
  3692. size_t b0 = command.find('"',0);
  3693. size_t b1 = command.find('"',b0+1);
  3694. while(b0 != command.npos && b1 != command.npos && b1>b0)
  3695. {
  3696. if(pos>b0 && pos<b1)
  3697. {
  3698. inQuotes = true;
  3699. break;
  3700. }
  3701. b0 = command.find('"',b1+1);
  3702. b1 = command.find('"',b0+1);
  3703. }
  3704. if(!inQuotes)
  3705. {
  3706. kwsys_stl::string arg = command.substr(start+1,pos-start-1);
  3707. // Remove the quotes if any
  3708. size_t quotes = arg.find('"');
  3709. while(quotes != arg.npos)
  3710. {
  3711. arg.erase(quotes,1);
  3712. quotes = arg.find('"');
  3713. }
  3714. args.push_back(arg.c_str());
  3715. start = pos;
  3716. }
  3717. pos = command.find(' ',pos+1);
  3718. }
  3719. kwsys_stl::string lastArg = command.substr(start+1,command.size()-start-1);
  3720. args.push_back(lastArg.c_str());
  3721. args.push_back(0);
  3722. kwsys_stl::string buffer = this->RunProcess(args);
  3723. kwsys_stl::string value = "";
  3724. for(size_t i=buffer.size()-1;i>0;i--)
  3725. {
  3726. if(buffer[i] == ' ' || buffer[i] == '\t')
  3727. {
  3728. break;
  3729. }
  3730. if(buffer[i] != '\n' && buffer[i] != '\r')
  3731. {
  3732. kwsys_stl::string val = value;
  3733. value = buffer[i];
  3734. value += val;
  3735. }
  3736. }
  3737. return value;
  3738. }
  3739. /** Querying for system information from Solaris */
  3740. bool SystemInformationImplementation::QuerySolarisInfo()
  3741. {
  3742. // Parse values
  3743. this->NumberOfPhysicalCPU = static_cast<unsigned int>(
  3744. atoi(this->ParseValueFromKStat("-n syste_misc -s ncpus").c_str()));
  3745. this->NumberOfLogicalCPU = this->NumberOfPhysicalCPU;
  3746. if(this->NumberOfPhysicalCPU!=0)
  3747. {
  3748. this->NumberOfLogicalCPU /= this->NumberOfPhysicalCPU;
  3749. }
  3750. this->CPUSpeedInMHz = static_cast<float>(atoi(this->ParseValueFromKStat("-s clock_MHz").c_str()));
  3751. // Chip family
  3752. this->ChipID.Family = 0;
  3753. // Chip Vendor
  3754. this->ChipID.Vendor = "Sun";
  3755. this->FindManufacturer();
  3756. // Chip Model
  3757. this->ChipID.ProcessorName = this->ParseValueFromKStat("-s cpu_type");
  3758. this->ChipID.Model = 0;
  3759. // Cache size
  3760. this->Features.L1CacheSize = 0;
  3761. this->Features.L2CacheSize = 0;
  3762. char* tail;
  3763. unsigned long totalMemory =
  3764. strtoul(this->ParseValueFromKStat("-s physmem").c_str(),&tail,0);
  3765. this->TotalPhysicalMemory = totalMemory/1024;
  3766. this->TotalPhysicalMemory *= 8192;
  3767. this->TotalPhysicalMemory /= 1024;
  3768. // Undefined values (for now at least)
  3769. this->TotalVirtualMemory = 0;
  3770. this->AvailablePhysicalMemory = 0;
  3771. this->AvailableVirtualMemory = 0;
  3772. return true;
  3773. }
  3774. /** Querying for system information from Haiku OS */
  3775. bool SystemInformationImplementation::QueryHaikuInfo()
  3776. {
  3777. #if defined(__HAIKU__)
  3778. system_info info;
  3779. get_system_info(&info);
  3780. this->NumberOfPhysicalCPU = info.cpu_count;
  3781. this->CPUSpeedInMHz = info.cpu_clock_speed / 1000000.0F;
  3782. // Physical Memory
  3783. this->TotalPhysicalMemory = (info.max_pages * B_PAGE_SIZE) / (1024 * 1024) ;
  3784. this->AvailablePhysicalMemory = this->TotalPhysicalMemory -
  3785. ((info.used_pages * B_PAGE_SIZE) / (1024 * 1024));
  3786. // NOTE: get_system_info_etc is currently a private call so just set to 0
  3787. // until it becomes public
  3788. this->TotalVirtualMemory = 0;
  3789. this->AvailableVirtualMemory = 0;
  3790. // Retrieve cpuid_info union for cpu 0
  3791. cpuid_info cpu_info;
  3792. get_cpuid(&cpu_info, 0, 0);
  3793. // Chip Vendor
  3794. // Use a temporary buffer so that we can add NULL termination to the string
  3795. char vbuf[13];
  3796. strncpy(vbuf, cpu_info.eax_0.vendor_id, 12);
  3797. vbuf[12] = '\0';
  3798. this->ChipID.Vendor = vbuf;
  3799. this->FindManufacturer();
  3800. // Retrieve cpuid_info union for cpu 0 this time using a register value of 1
  3801. get_cpuid(&cpu_info, 1, 0);
  3802. this->NumberOfLogicalCPU = cpu_info.eax_1.logical_cpus;
  3803. // Chip type
  3804. this->ChipID.Type = cpu_info.eax_1.type;
  3805. // Chip family
  3806. this->ChipID.Family = cpu_info.eax_1.family;
  3807. // Chip Model
  3808. this->ChipID.Model = cpu_info.eax_1.model;
  3809. // Chip Revision
  3810. this->ChipID.Revision = cpu_info.eax_1.stepping;
  3811. // Chip Extended Family
  3812. this->ChipID.ExtendedFamily = cpu_info.eax_1.extended_family;
  3813. // Chip Extended Model
  3814. this->ChipID.ExtendedModel = cpu_info.eax_1.extended_model;
  3815. // Get ChipID.ProcessorName from other information already gathered
  3816. this->RetrieveClassicalCPUIdentity();
  3817. // Cache size
  3818. this->Features.L1CacheSize = 0;
  3819. this->Features.L2CacheSize = 0;
  3820. return true;
  3821. #else
  3822. return false;
  3823. #endif
  3824. }
  3825. bool SystemInformationImplementation::QueryQNXMemory()
  3826. {
  3827. #if defined(__QNX__)
  3828. kwsys_stl::string buffer;
  3829. kwsys_stl::vector<const char*> args;
  3830. args.clear();
  3831. args.push_back("showmem");
  3832. args.push_back("-S");
  3833. args.push_back(0);
  3834. buffer = this->RunProcess(args);
  3835. args.clear();
  3836. size_t pos = buffer.find("System RAM:");
  3837. if (pos == buffer.npos)
  3838. return false;
  3839. pos = buffer.find(":", pos);
  3840. size_t pos2 = buffer.find("M (", pos);
  3841. if (pos2 == buffer.npos)
  3842. return false;
  3843. pos++;
  3844. while (buffer[pos] == ' ')
  3845. pos++;
  3846. this->TotalPhysicalMemory = atoi(buffer.substr(pos, pos2 - pos).c_str());
  3847. return true;
  3848. #endif
  3849. return false;
  3850. }
  3851. bool SystemInformationImplementation::QueryQNXProcessor()
  3852. {
  3853. #if defined(__QNX__)
  3854. // the output on my QNX 6.4.1 looks like this:
  3855. // Processor1: 686 Pentium II Stepping 3 2175MHz FPU
  3856. kwsys_stl::string buffer;
  3857. kwsys_stl::vector<const char*> args;
  3858. args.clear();
  3859. args.push_back("pidin");
  3860. args.push_back("info");
  3861. args.push_back(0);
  3862. buffer = this->RunProcess(args);
  3863. args.clear();
  3864. size_t pos = buffer.find("Processor1:");
  3865. if (pos == buffer.npos)
  3866. return false;
  3867. size_t pos2 = buffer.find("MHz", pos);
  3868. if (pos2 == buffer.npos)
  3869. return false;
  3870. size_t pos3 = pos2;
  3871. while (buffer[pos3] != ' ')
  3872. --pos3;
  3873. this->CPUSpeedInMHz = atoi(buffer.substr(pos3 + 1, pos2 - pos3 - 1).c_str());
  3874. pos2 = buffer.find(" Stepping", pos);
  3875. if (pos2 != buffer.npos)
  3876. {
  3877. pos2 = buffer.find(" ", pos2 + 1);
  3878. if (pos2 != buffer.npos && pos2 < pos3)
  3879. {
  3880. this->ChipID.Revision = atoi(buffer.substr(pos2 + 1, pos3 - pos2).c_str());
  3881. }
  3882. }
  3883. this->NumberOfPhysicalCPU = 0;
  3884. do
  3885. {
  3886. pos = buffer.find("\nProcessor", pos + 1);
  3887. ++this->NumberOfPhysicalCPU;
  3888. } while (pos != buffer.npos);
  3889. this->NumberOfLogicalCPU = 1;
  3890. return true;
  3891. #else
  3892. return false;
  3893. #endif
  3894. }
  3895. /** Query the operating system information */
  3896. bool SystemInformationImplementation::QueryOSInformation()
  3897. {
  3898. #if defined(_WIN32)
  3899. this->OSName = "Windows";
  3900. OSVERSIONINFOEX osvi;
  3901. BOOL bIsWindows64Bit;
  3902. BOOL bOsVersionInfoEx;
  3903. char operatingSystem[256];
  3904. // Try calling GetVersionEx using the OSVERSIONINFOEX structure.
  3905. ZeroMemory (&osvi, sizeof (OSVERSIONINFOEX));
  3906. osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEX);
  3907. bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi);
  3908. if (!bOsVersionInfoEx)
  3909. {
  3910. osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
  3911. if (!GetVersionEx ((OSVERSIONINFO *) &osvi))
  3912. {
  3913. return false;
  3914. }
  3915. }
  3916. switch (osvi.dwPlatformId)
  3917. {
  3918. case VER_PLATFORM_WIN32_NT:
  3919. // Test for the product.
  3920. if (osvi.dwMajorVersion <= 4)
  3921. {
  3922. this->OSRelease = "NT";
  3923. }
  3924. if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0)
  3925. {
  3926. this->OSRelease = "2000";
  3927. }
  3928. if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
  3929. {
  3930. this->OSRelease = "XP";
  3931. }
  3932. // XP Professional x64
  3933. if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2)
  3934. {
  3935. this->OSRelease = "XP";
  3936. }
  3937. #ifdef VER_NT_WORKSTATION
  3938. // Test for product type.
  3939. if (bOsVersionInfoEx)
  3940. {
  3941. if (osvi.wProductType == VER_NT_WORKSTATION)
  3942. {
  3943. if (osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 0)
  3944. {
  3945. this->OSRelease = "Vista";
  3946. }
  3947. if (osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 1)
  3948. {
  3949. this->OSRelease = "7";
  3950. }
  3951. // VER_SUITE_PERSONAL may not be defined
  3952. #ifdef VER_SUITE_PERSONAL
  3953. else
  3954. {
  3955. if (osvi.wSuiteMask & VER_SUITE_PERSONAL)
  3956. {
  3957. this->OSRelease += " Personal";
  3958. }
  3959. else
  3960. {
  3961. this->OSRelease += " Professional";
  3962. }
  3963. }
  3964. #endif
  3965. }
  3966. else if (osvi.wProductType == VER_NT_SERVER)
  3967. {
  3968. // Check for .NET Server instead of Windows XP.
  3969. if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
  3970. {
  3971. this->OSRelease = ".NET";
  3972. }
  3973. // Continue with the type detection.
  3974. if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
  3975. {
  3976. this->OSRelease += " DataCenter Server";
  3977. }
  3978. else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
  3979. {
  3980. this->OSRelease += " Advanced Server";
  3981. }
  3982. else
  3983. {
  3984. this->OSRelease += " Server";
  3985. }
  3986. }
  3987. sprintf (operatingSystem, "%s (Build %ld)", osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF);
  3988. this->OSVersion = operatingSystem;
  3989. }
  3990. else
  3991. #endif // VER_NT_WORKSTATION
  3992. {
  3993. HKEY hKey;
  3994. char szProductType[80];
  3995. DWORD dwBufLen;
  3996. // Query the registry to retrieve information.
  3997. RegOpenKeyEx (HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\ProductOptions", 0, KEY_QUERY_VALUE, &hKey);
  3998. RegQueryValueEx (hKey, "ProductType", NULL, NULL, (LPBYTE) szProductType, &dwBufLen);
  3999. RegCloseKey (hKey);
  4000. if (lstrcmpi ("WINNT", szProductType) == 0)
  4001. {
  4002. this->OSRelease += " Professional";
  4003. }
  4004. if (lstrcmpi ("LANMANNT", szProductType) == 0)
  4005. {
  4006. // Decide between Windows 2000 Advanced Server and Windows .NET Enterprise Server.
  4007. if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
  4008. {
  4009. this->OSRelease += " Standard Server";
  4010. }
  4011. else
  4012. {
  4013. this->OSRelease += " Server";
  4014. }
  4015. }
  4016. if (lstrcmpi ("SERVERNT", szProductType) == 0)
  4017. {
  4018. // Decide between Windows 2000 Advanced Server and Windows .NET Enterprise Server.
  4019. if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
  4020. {
  4021. this->OSRelease += " Enterprise Server";
  4022. }
  4023. else
  4024. {
  4025. this->OSRelease += " Advanced Server";
  4026. }
  4027. }
  4028. }
  4029. // Display version, service pack (if any), and build number.
  4030. if (osvi.dwMajorVersion <= 4)
  4031. {
  4032. // NB: NT 4.0 and earlier.
  4033. sprintf (operatingSystem, "version %ld.%ld %s (Build %ld)",
  4034. osvi.dwMajorVersion,
  4035. osvi.dwMinorVersion,
  4036. osvi.szCSDVersion,
  4037. osvi.dwBuildNumber & 0xFFFF);
  4038. this->OSVersion = operatingSystem;
  4039. }
  4040. else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
  4041. {
  4042. // Windows XP and .NET server.
  4043. typedef BOOL (CALLBACK* LPFNPROC) (HANDLE, BOOL *);
  4044. HINSTANCE hKernelDLL;
  4045. LPFNPROC DLLProc;
  4046. // Load the Kernel32 DLL.
  4047. hKernelDLL = LoadLibrary ("kernel32");
  4048. if (hKernelDLL != NULL) {
  4049. // Only XP and .NET Server support IsWOW64Process so... Load dynamically!
  4050. DLLProc = (LPFNPROC) GetProcAddress (hKernelDLL, "IsWow64Process");
  4051. // If the function address is valid, call the function.
  4052. if (DLLProc != NULL) (DLLProc) (GetCurrentProcess (), &bIsWindows64Bit);
  4053. else bIsWindows64Bit = false;
  4054. // Free the DLL module.
  4055. FreeLibrary (hKernelDLL);
  4056. }
  4057. }
  4058. else
  4059. {
  4060. // Windows 2000 and everything else.
  4061. sprintf (operatingSystem,"%s (Build %ld)", osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF);
  4062. this->OSVersion = operatingSystem;
  4063. }
  4064. break;
  4065. case VER_PLATFORM_WIN32_WINDOWS:
  4066. // Test for the product.
  4067. if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
  4068. {
  4069. this->OSRelease = "95";
  4070. if(osvi.szCSDVersion[1] == 'C')
  4071. {
  4072. this->OSRelease += "OSR 2.5";
  4073. }
  4074. else if(osvi.szCSDVersion[1] == 'B')
  4075. {
  4076. this->OSRelease += "OSR 2";
  4077. }
  4078. }
  4079. if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)
  4080. {
  4081. this->OSRelease = "98";
  4082. if (osvi.szCSDVersion[1] == 'A' )
  4083. {
  4084. this->OSRelease += "SE";
  4085. }
  4086. }
  4087. if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
  4088. {
  4089. this->OSRelease = "Me";
  4090. }
  4091. break;
  4092. case VER_PLATFORM_WIN32s:
  4093. this->OSRelease = "Win32s";
  4094. break;
  4095. default:
  4096. this->OSRelease = "Unknown";
  4097. break;
  4098. }
  4099. // Get the hostname
  4100. WORD wVersionRequested;
  4101. WSADATA wsaData;
  4102. char name[255];
  4103. wVersionRequested = MAKEWORD(2,0);
  4104. if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
  4105. {
  4106. gethostname(name,sizeof(name));
  4107. WSACleanup( );
  4108. }
  4109. this->Hostname = name;
  4110. const char* arch = getenv("PROCESSOR_ARCHITECTURE");
  4111. if(arch)
  4112. {
  4113. this->OSPlatform = arch;
  4114. }
  4115. #else
  4116. struct utsname unameInfo;
  4117. int errorFlag = uname(&unameInfo);
  4118. if(errorFlag == 0)
  4119. {
  4120. this->OSName = unameInfo.sysname;
  4121. this->Hostname = unameInfo.nodename;
  4122. this->OSRelease = unameInfo.release;
  4123. this->OSVersion = unameInfo.version;
  4124. this->OSPlatform = unameInfo.machine;
  4125. }
  4126. #ifdef __APPLE__
  4127. this->OSName="Unknown Apple OS";
  4128. this->OSRelease="Unknown product version";
  4129. this->OSVersion="Unknown build version";
  4130. this->CallSwVers("-productName",this->OSName);
  4131. this->CallSwVers("-productVersion",this->OSRelease);
  4132. this->CallSwVers("-buildVersion",this->OSVersion);
  4133. #endif
  4134. #endif
  4135. return true;
  4136. }
  4137. int SystemInformationImplementation::CallSwVers(
  4138. const char *arg,
  4139. kwsys_stl::string &ver)
  4140. {
  4141. #ifdef __APPLE__
  4142. kwsys_stl::vector<const char*> args;
  4143. args.push_back("sw_vers");
  4144. args.push_back(arg);
  4145. args.push_back(0);
  4146. ver = this->RunProcess(args);
  4147. this->TrimNewline(ver);
  4148. #else
  4149. // avoid C4100
  4150. (void)arg;
  4151. (void)ver;
  4152. #endif
  4153. return 0;
  4154. }
  4155. void SystemInformationImplementation::TrimNewline(kwsys_stl::string& output)
  4156. {
  4157. // remove \r
  4158. kwsys_stl::string::size_type pos=0;
  4159. while((pos = output.find("\r", pos)) != kwsys_stl::string::npos)
  4160. {
  4161. output.erase(pos);
  4162. }
  4163. // remove \n
  4164. pos = 0;
  4165. while((pos = output.find("\n", pos)) != kwsys_stl::string::npos)
  4166. {
  4167. output.erase(pos);
  4168. }
  4169. }
  4170. /** Return true if the machine is 64 bits */
  4171. bool SystemInformationImplementation::Is64Bits()
  4172. {
  4173. return (sizeof(void*) == 8);
  4174. }
  4175. } // namespace @KWSYS_NAMESPACE@