SystemInformation.cxx 130 KB

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