SystemInformation.cxx 136 KB

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