SystemInformation.cxx 147 KB

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