SystemInformation.cxx 106 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675
  1. /*============================================================================
  2. KWSys - Kitware System Library
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #ifdef _WIN32
  11. # include <winsock.h> // WSADATA, include before sys/types.h
  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(stl/string)
  26. #include KWSYS_HEADER(stl/vector)
  27. #include KWSYS_HEADER(ios/iosfwd)
  28. #include KWSYS_HEADER(SystemInformation.hxx)
  29. #include KWSYS_HEADER(Process.h)
  30. #include KWSYS_HEADER(ios/iostream)
  31. #include KWSYS_HEADER(ios/sstream)
  32. // Work-around CMake dependency scanning limitation. This must
  33. // duplicate the above list of headers.
  34. #if 0
  35. # include "SystemInformation.hxx.in"
  36. # include "Process.h.in"
  37. # include "Configure.hxx.in"
  38. # include "kwsys_stl.hxx.in"
  39. # include "kwsys_stl_vector.in"
  40. # include "kwsys_stl_iosfwd.in"
  41. # include "kwsys_ios_sstream.h.in"
  42. # include "kwsys_ios_iostream.h.in"
  43. #endif
  44. #ifndef WIN32
  45. # include <sys/utsname.h> // int uname(struct utsname *buf);
  46. #endif
  47. #ifdef _WIN32
  48. # include <windows.h>
  49. #endif
  50. #ifdef __APPLE__
  51. #include <sys/sysctl.h>
  52. #include <mach/vm_statistics.h>
  53. #include <mach/host_info.h>
  54. #include <mach/mach.h>
  55. #include <mach/mach_types.h>
  56. #endif
  57. #ifdef __linux
  58. # include <sys/types.h>
  59. # include <unistd.h>
  60. # include <fcntl.h>
  61. # include <ctype.h> // int isdigit(int c);
  62. # include <errno.h> // extern int errno;
  63. # include <sys/time.h>
  64. #elif defined( __hpux )
  65. # include <sys/param.h>
  66. # include <sys/pstat.h>
  67. #endif
  68. #ifdef __HAIKU__
  69. #include <OS.h>
  70. #endif
  71. #include <memory.h>
  72. #include <stdlib.h>
  73. #include <stdio.h>
  74. #include <string.h>
  75. namespace KWSYS_NAMESPACE
  76. {
  77. // Create longlong
  78. #if KWSYS_USE_LONG_LONG
  79. typedef long long LongLong;
  80. #elif KWSYS_USE___INT64
  81. typedef __int64 LongLong;
  82. #else
  83. # error "No Long Long"
  84. #endif
  85. // Define SystemInformationImplementation class
  86. typedef void (*DELAY_FUNC)(unsigned int uiMS);
  87. class SystemInformationImplementation
  88. {
  89. public:
  90. SystemInformationImplementation ();
  91. ~SystemInformationImplementation ();
  92. const char * GetVendorString();
  93. const char * GetVendorID();
  94. kwsys_stl::string GetTypeID();
  95. kwsys_stl::string GetFamilyID();
  96. kwsys_stl::string GetModelID();
  97. kwsys_stl::string GetSteppingCode();
  98. const char * GetExtendedProcessorName();
  99. const char * GetProcessorSerialNumber();
  100. int GetProcessorCacheSize();
  101. unsigned int GetLogicalProcessorsPerPhysical();
  102. float GetProcessorClockFrequency();
  103. int GetProcessorAPICID();
  104. int GetProcessorCacheXSize(long int);
  105. bool DoesCPUSupportFeature(long int);
  106. const char * GetOSName();
  107. const char * GetHostname();
  108. const char * GetOSRelease();
  109. const char * GetOSVersion();
  110. const char * GetOSPlatform();
  111. bool Is64Bits();
  112. unsigned int GetNumberOfLogicalCPU(); // per physical cpu
  113. unsigned int GetNumberOfPhysicalCPU();
  114. bool DoesCPUSupportCPUID();
  115. // Retrieve memory information in megabyte.
  116. size_t GetTotalVirtualMemory();
  117. size_t GetAvailableVirtualMemory();
  118. size_t GetTotalPhysicalMemory();
  119. size_t GetAvailablePhysicalMemory();
  120. /** Run the different checks */
  121. void RunCPUCheck();
  122. void RunOSCheck();
  123. void RunMemoryCheck();
  124. public:
  125. typedef struct tagID
  126. {
  127. int Type;
  128. int Family;
  129. int Model;
  130. int Revision;
  131. int ExtendedFamily;
  132. int ExtendedModel;
  133. kwsys_stl::string ProcessorName;
  134. kwsys_stl::string Vendor;
  135. kwsys_stl::string SerialNumber;
  136. } ID;
  137. typedef struct tagCPUPowerManagement
  138. {
  139. bool HasVoltageID;
  140. bool HasFrequencyID;
  141. bool HasTempSenseDiode;
  142. } CPUPowerManagement;
  143. typedef struct tagCPUExtendedFeatures
  144. {
  145. bool Has3DNow;
  146. bool Has3DNowPlus;
  147. bool SupportsMP;
  148. bool HasMMXPlus;
  149. bool HasSSEMMX;
  150. bool SupportsHyperthreading;
  151. unsigned int LogicalProcessorsPerPhysical;
  152. int APIC_ID;
  153. CPUPowerManagement PowerManagement;
  154. } CPUExtendedFeatures;
  155. typedef struct CPUtagFeatures
  156. {
  157. bool HasFPU;
  158. bool HasTSC;
  159. bool HasMMX;
  160. bool HasSSE;
  161. bool HasSSEFP;
  162. bool HasSSE2;
  163. bool HasIA64;
  164. bool HasAPIC;
  165. bool HasCMOV;
  166. bool HasMTRR;
  167. bool HasACPI;
  168. bool HasSerial;
  169. bool HasThermal;
  170. int CPUSpeed;
  171. int L1CacheSize;
  172. int L2CacheSize;
  173. int L3CacheSize;
  174. CPUExtendedFeatures ExtendedFeatures;
  175. } CPUFeatures;
  176. enum Manufacturer
  177. {
  178. AMD, Intel, NSC, UMC, Cyrix, NexGen, IDT, Rise, Transmeta, Sun, IBM,
  179. Motorola, UnknownManufacturer
  180. };
  181. protected:
  182. // Functions.
  183. bool RetrieveCPUFeatures();
  184. bool RetrieveCPUIdentity();
  185. bool RetrieveCPUCacheDetails();
  186. bool RetrieveClassicalCPUCacheDetails();
  187. bool RetrieveCPUClockSpeed();
  188. bool RetrieveClassicalCPUClockSpeed();
  189. bool RetrieveCPUExtendedLevelSupport(int);
  190. bool RetrieveExtendedCPUFeatures();
  191. bool RetrieveProcessorSerialNumber();
  192. bool RetrieveCPUPowerManagement();
  193. bool RetrieveClassicalCPUIdentity();
  194. bool RetrieveExtendedCPUIdentity();
  195. Manufacturer ChipManufacturer;
  196. CPUFeatures Features;
  197. ID ChipID;
  198. float CPUSpeedInMHz;
  199. unsigned int NumberOfLogicalCPU;
  200. unsigned int NumberOfPhysicalCPU;
  201. int CPUCount();
  202. unsigned char LogicalCPUPerPhysicalCPU();
  203. unsigned char GetAPICId();
  204. unsigned int IsHyperThreadingSupported();
  205. LongLong GetCyclesDifference(DELAY_FUNC, unsigned int);
  206. // For Linux and Cygwin, /proc/cpuinfo formats are slightly different
  207. int RetreiveInformationFromCpuInfoFile();
  208. kwsys_stl::string ExtractValueFromCpuInfoFile(kwsys_stl::string buffer,
  209. const char* word, size_t init=0);
  210. static void Delay (unsigned int);
  211. static void DelayOverhead (unsigned int);
  212. void FindManufacturer();
  213. // For Mac
  214. bool ParseSysCtl();
  215. void CallSwVers();
  216. void TrimNewline(kwsys_stl::string&);
  217. kwsys_stl::string ExtractValueFromSysCtl(const char* word);
  218. kwsys_stl::string SysCtlBuffer;
  219. // For Solaris
  220. bool QuerySolarisInfo();
  221. kwsys_stl::string ParseValueFromKStat(const char* arguments);
  222. kwsys_stl::string RunProcess(kwsys_stl::vector<const char*> args);
  223. //For Haiku OS
  224. bool QueryHaikuInfo();
  225. //For QNX
  226. bool QueryQNXMemory();
  227. bool QueryQNXProcessor();
  228. // Evaluate the memory information.
  229. int QueryMemory();
  230. size_t TotalVirtualMemory;
  231. size_t AvailableVirtualMemory;
  232. size_t TotalPhysicalMemory;
  233. size_t AvailablePhysicalMemory;
  234. size_t CurrentPositionInFile;
  235. // Operating System information
  236. bool QueryOSInformation();
  237. kwsys_stl::string OSName;
  238. kwsys_stl::string Hostname;
  239. kwsys_stl::string OSRelease;
  240. kwsys_stl::string OSVersion;
  241. kwsys_stl::string OSPlatform;
  242. };
  243. SystemInformation::SystemInformation()
  244. {
  245. this->Implementation = new SystemInformationImplementation;
  246. }
  247. SystemInformation::~SystemInformation()
  248. {
  249. delete this->Implementation;
  250. }
  251. const char * SystemInformation::GetVendorString()
  252. {
  253. return this->Implementation->GetVendorString();
  254. }
  255. const char * SystemInformation::GetVendorID()
  256. {
  257. return this->Implementation->GetVendorID();
  258. }
  259. kwsys_stl::string SystemInformation::GetTypeID()
  260. {
  261. return this->Implementation->GetTypeID();
  262. }
  263. kwsys_stl::string SystemInformation::GetFamilyID()
  264. {
  265. return this->Implementation->GetFamilyID();
  266. }
  267. kwsys_stl::string SystemInformation::GetModelID()
  268. {
  269. return this->Implementation->GetModelID();
  270. }
  271. kwsys_stl::string SystemInformation::GetSteppingCode()
  272. {
  273. return this->Implementation->GetSteppingCode();
  274. }
  275. const char * SystemInformation::GetExtendedProcessorName()
  276. {
  277. return this->Implementation->GetExtendedProcessorName();
  278. }
  279. const char * SystemInformation::GetProcessorSerialNumber()
  280. {
  281. return this->Implementation->GetProcessorSerialNumber();
  282. }
  283. int SystemInformation::GetProcessorCacheSize()
  284. {
  285. return this->Implementation->GetProcessorCacheSize();
  286. }
  287. unsigned int SystemInformation::GetLogicalProcessorsPerPhysical()
  288. {
  289. return this->Implementation->GetLogicalProcessorsPerPhysical();
  290. }
  291. float SystemInformation::GetProcessorClockFrequency()
  292. {
  293. return this->Implementation->GetProcessorClockFrequency();
  294. }
  295. int SystemInformation::GetProcessorAPICID()
  296. {
  297. return this->Implementation->GetProcessorAPICID();
  298. }
  299. int SystemInformation::GetProcessorCacheXSize(long int l)
  300. {
  301. return this->Implementation->GetProcessorCacheXSize(l);
  302. }
  303. bool SystemInformation::DoesCPUSupportFeature(long int i)
  304. {
  305. return this->Implementation->DoesCPUSupportFeature(i);
  306. }
  307. const char * SystemInformation::GetOSName()
  308. {
  309. return this->Implementation->GetOSName();
  310. }
  311. const char * SystemInformation::GetHostname()
  312. {
  313. return this->Implementation->GetHostname();
  314. }
  315. const char * SystemInformation::GetOSRelease()
  316. {
  317. return this->Implementation->GetOSRelease();
  318. }
  319. const char * SystemInformation::GetOSVersion()
  320. {
  321. return this->Implementation->GetOSVersion();
  322. }
  323. const char * SystemInformation::GetOSPlatform()
  324. {
  325. return this->Implementation->GetOSPlatform();
  326. }
  327. bool SystemInformation::Is64Bits()
  328. {
  329. return this->Implementation->Is64Bits();
  330. }
  331. unsigned int SystemInformation::GetNumberOfLogicalCPU() // per physical cpu
  332. {
  333. return this->Implementation->GetNumberOfLogicalCPU();
  334. }
  335. unsigned int SystemInformation::GetNumberOfPhysicalCPU()
  336. {
  337. return this->Implementation->GetNumberOfPhysicalCPU();
  338. }
  339. bool SystemInformation::DoesCPUSupportCPUID()
  340. {
  341. return this->Implementation->DoesCPUSupportCPUID();
  342. }
  343. // Retrieve memory information in megabyte.
  344. size_t SystemInformation::GetTotalVirtualMemory()
  345. {
  346. return this->Implementation->GetTotalVirtualMemory();
  347. }
  348. size_t SystemInformation::GetAvailableVirtualMemory()
  349. {
  350. return this->Implementation->GetAvailableVirtualMemory();
  351. }
  352. size_t SystemInformation::GetTotalPhysicalMemory()
  353. {
  354. return this->Implementation->GetTotalPhysicalMemory();
  355. }
  356. size_t SystemInformation::GetAvailablePhysicalMemory()
  357. {
  358. return this->Implementation->GetAvailablePhysicalMemory();
  359. }
  360. /** Run the different checks */
  361. void SystemInformation::RunCPUCheck()
  362. {
  363. this->Implementation->RunCPUCheck();
  364. }
  365. void SystemInformation::RunOSCheck()
  366. {
  367. this->Implementation->RunOSCheck();
  368. }
  369. void SystemInformation::RunMemoryCheck()
  370. {
  371. this->Implementation->RunMemoryCheck();
  372. }
  373. // --------------------------------------------------------------
  374. // SystemInformationImplementation starts here
  375. #if defined(_MSC_VER) && (_MSC_VER >= 1300) && !defined(_WIN64)
  376. #define USE_ASM_INSTRUCTIONS 1
  377. #else
  378. #define USE_ASM_INSTRUCTIONS 0
  379. #endif
  380. #define STORE_TLBCACHE_INFO(x,y) x = (x < y) ? y : x
  381. #define TLBCACHE_INFO_UNITS (15)
  382. #define CLASSICAL_CPU_FREQ_LOOP 10000000
  383. #define RDTSC_INSTRUCTION _asm _emit 0x0f _asm _emit 0x31
  384. #define CPUID_AWARE_COMPILER
  385. #ifdef CPUID_AWARE_COMPILER
  386. #define CPUID_INSTRUCTION cpuid
  387. #else
  388. #define CPUID_INSTRUCTION _asm _emit 0x0f _asm _emit 0xa2
  389. #endif
  390. #define MMX_FEATURE 0x00000001
  391. #define MMX_PLUS_FEATURE 0x00000002
  392. #define SSE_FEATURE 0x00000004
  393. #define SSE2_FEATURE 0x00000008
  394. #define AMD_3DNOW_FEATURE 0x00000010
  395. #define AMD_3DNOW_PLUS_FEATURE 0x00000020
  396. #define IA64_FEATURE 0x00000040
  397. #define MP_CAPABLE 0x00000080
  398. #define HYPERTHREAD_FEATURE 0x00000100
  399. #define SERIALNUMBER_FEATURE 0x00000200
  400. #define APIC_FEATURE 0x00000400
  401. #define SSE_FP_FEATURE 0x00000800
  402. #define SSE_MMX_FEATURE 0x00001000
  403. #define CMOV_FEATURE 0x00002000
  404. #define MTRR_FEATURE 0x00004000
  405. #define L1CACHE_FEATURE 0x00008000
  406. #define L2CACHE_FEATURE 0x00010000
  407. #define L3CACHE_FEATURE 0x00020000
  408. #define ACPI_FEATURE 0x00040000
  409. #define THERMALMONITOR_FEATURE 0x00080000
  410. #define TEMPSENSEDIODE_FEATURE 0x00100000
  411. #define FREQUENCYID_FEATURE 0x00200000
  412. #define VOLTAGEID_FREQUENCY 0x00400000
  413. // Status Flag
  414. #define HT_NOT_CAPABLE 0
  415. #define HT_ENABLED 1
  416. #define HT_DISABLED 2
  417. #define HT_SUPPORTED_NOT_ENABLED 3
  418. #define HT_CANNOT_DETECT 4
  419. // EDX[28] Bit 28 is set if HT is supported
  420. #define HT_BIT 0x10000000
  421. // EAX[11:8] Bit 8-11 contains family processor ID.
  422. #define FAMILY_ID 0x0F00
  423. #define PENTIUM4_ID 0x0F00
  424. // EAX[23:20] Bit 20-23 contains extended family processor ID
  425. #define EXT_FAMILY_ID 0x0F00000
  426. // EBX[23:16] Bit 16-23 in ebx contains the number of logical
  427. #define NUM_LOGICAL_BITS 0x00FF0000
  428. // processors per physical processor when execute cpuid with
  429. // eax set to 1
  430. // EBX[31:24] Bits 24-31 (8 bits) return the 8-bit unique
  431. #define INITIAL_APIC_ID_BITS 0xFF000000
  432. // initial APIC ID for the processor this code is running on.
  433. // Default value = 0xff if HT is not supported
  434. SystemInformationImplementation::SystemInformationImplementation()
  435. {
  436. this->TotalVirtualMemory = 0;
  437. this->AvailableVirtualMemory = 0;
  438. this->TotalPhysicalMemory = 0;
  439. this->AvailablePhysicalMemory = 0;
  440. this->CurrentPositionInFile = 0;
  441. this->ChipManufacturer = UnknownManufacturer;
  442. memset(&this->Features, 0, sizeof(CPUFeatures));
  443. this->ChipID.Type = 0;
  444. this->ChipID.Family = 0;
  445. this->ChipID.Model = 0;
  446. this->ChipID.Revision = 0;
  447. this->ChipID.ExtendedFamily = 0;
  448. this->ChipID.ExtendedModel = 0;
  449. this->CPUSpeedInMHz = 0;
  450. this->NumberOfLogicalCPU = 0;
  451. this->NumberOfPhysicalCPU = 0;
  452. this->OSName = "";
  453. this->Hostname = "";
  454. this->OSRelease = "";
  455. this->OSVersion = "";
  456. this->OSPlatform = "";
  457. }
  458. SystemInformationImplementation::~SystemInformationImplementation()
  459. {
  460. }
  461. void SystemInformationImplementation::RunCPUCheck()
  462. {
  463. #ifdef WIN32
  464. // Check to see if this processor supports CPUID.
  465. bool supportsCPUID = DoesCPUSupportCPUID();
  466. if (supportsCPUID)
  467. {
  468. // Retrieve the CPU details.
  469. RetrieveCPUIdentity();
  470. RetrieveCPUFeatures();
  471. }
  472. // These two may be called without support for the CPUID instruction.
  473. // (But if the instruction is there, they should be called *after*
  474. // the above call to RetrieveCPUIdentity... that's why the two if
  475. // blocks exist with the same "if (supportsCPUID)" logic...
  476. //
  477. if (!RetrieveCPUClockSpeed())
  478. {
  479. RetrieveClassicalCPUClockSpeed();
  480. }
  481. if (supportsCPUID)
  482. {
  483. // Retrieve cache information.
  484. if (!RetrieveCPUCacheDetails())
  485. {
  486. RetrieveClassicalCPUCacheDetails();
  487. }
  488. // Retrieve the extended CPU details.
  489. if (!RetrieveExtendedCPUIdentity())
  490. {
  491. RetrieveClassicalCPUIdentity();
  492. }
  493. RetrieveExtendedCPUFeatures();
  494. RetrieveCPUPowerManagement();
  495. // Now attempt to retrieve the serial number (if possible).
  496. RetrieveProcessorSerialNumber();
  497. }
  498. this->CPUCount();
  499. #elif defined(__APPLE__)
  500. this->ParseSysCtl();
  501. #elif defined (__SVR4) && defined (__sun)
  502. this->QuerySolarisInfo();
  503. #elif defined(__HAIKU__)
  504. this->QueryHaikuInfo();
  505. #elif defined(__QNX__)
  506. this->QueryQNXProcessor();
  507. #else
  508. this->RetreiveInformationFromCpuInfoFile();
  509. #endif
  510. }
  511. void SystemInformationImplementation::RunOSCheck()
  512. {
  513. this->QueryOSInformation();
  514. }
  515. void SystemInformationImplementation::RunMemoryCheck()
  516. {
  517. #if defined(__APPLE__)
  518. this->ParseSysCtl();
  519. #elif defined (__SVR4) && defined (__sun)
  520. this->QuerySolarisInfo();
  521. #elif defined(__HAIKU__)
  522. this->QueryHaikuInfo();
  523. #elif defined(__QNX__)
  524. this->QueryQNXMemory();
  525. #else
  526. this->QueryMemory();
  527. #endif
  528. }
  529. /** Get the vendor string */
  530. const char * SystemInformationImplementation::GetVendorString()
  531. {
  532. return this->ChipID.Vendor.c_str();
  533. }
  534. /** Get the OS Name */
  535. const char * SystemInformationImplementation::GetOSName()
  536. {
  537. return this->OSName.c_str();
  538. }
  539. /** Get the hostname */
  540. const char* SystemInformationImplementation::GetHostname()
  541. {
  542. return this->Hostname.c_str();
  543. }
  544. /** Get the OS release */
  545. const char* SystemInformationImplementation::GetOSRelease()
  546. {
  547. return this->OSRelease.c_str();
  548. }
  549. /** Get the OS version */
  550. const char* SystemInformationImplementation::GetOSVersion()
  551. {
  552. return this->OSVersion.c_str();
  553. }
  554. /** Get the OS platform */
  555. const char* SystemInformationImplementation::GetOSPlatform()
  556. {
  557. return this->OSPlatform.c_str();
  558. }
  559. /** Get the vendor ID */
  560. const char * SystemInformationImplementation::GetVendorID()
  561. {
  562. // Return the vendor ID.
  563. switch (this->ChipManufacturer)
  564. {
  565. case Intel:
  566. return "Intel Corporation";
  567. case AMD:
  568. return "Advanced Micro Devices";
  569. case NSC:
  570. return "National Semiconductor";
  571. case Cyrix:
  572. return "Cyrix Corp., VIA Inc.";
  573. case NexGen:
  574. return "NexGen Inc., Advanced Micro Devices";
  575. case IDT:
  576. return "IDT\\Centaur, Via Inc.";
  577. case UMC:
  578. return "United Microelectronics Corp.";
  579. case Rise:
  580. return "Rise";
  581. case Transmeta:
  582. return "Transmeta";
  583. case Sun:
  584. return "Sun Microelectronics";
  585. case IBM:
  586. return "IBM";
  587. case Motorola:
  588. return "Motorola";
  589. default:
  590. return "Unknown Manufacturer";
  591. }
  592. }
  593. /** Return the type ID of the CPU */
  594. kwsys_stl::string SystemInformationImplementation::GetTypeID()
  595. {
  596. kwsys_ios::ostringstream str;
  597. str << this->ChipID.Type;
  598. return str.str();
  599. }
  600. /** Return the family of the CPU present */
  601. kwsys_stl::string SystemInformationImplementation::GetFamilyID()
  602. {
  603. kwsys_ios::ostringstream str;
  604. str << this->ChipID.Family;
  605. return str.str();
  606. }
  607. // Return the model of CPU present */
  608. kwsys_stl::string SystemInformationImplementation::GetModelID()
  609. {
  610. kwsys_ios::ostringstream str;
  611. str << this->ChipID.Model;
  612. return str.str();
  613. }
  614. /** Return the stepping code of the CPU present. */
  615. kwsys_stl::string SystemInformationImplementation::GetSteppingCode()
  616. {
  617. kwsys_ios::ostringstream str;
  618. str << this->ChipID.Revision;
  619. return str.str();
  620. }
  621. /** Return the stepping code of the CPU present. */
  622. const char * SystemInformationImplementation::GetExtendedProcessorName()
  623. {
  624. return this->ChipID.ProcessorName.c_str();
  625. }
  626. /** Return the serial number of the processor
  627. * in hexadecimal: xxxx-xxxx-xxxx-xxxx-xxxx-xxxx. */
  628. const char * SystemInformationImplementation::GetProcessorSerialNumber()
  629. {
  630. return this->ChipID.SerialNumber.c_str();
  631. }
  632. /** Return the logical processors per physical */
  633. unsigned int SystemInformationImplementation::GetLogicalProcessorsPerPhysical()
  634. {
  635. return this->Features.ExtendedFeatures.LogicalProcessorsPerPhysical;
  636. }
  637. /** Return the processor clock frequency. */
  638. float SystemInformationImplementation::GetProcessorClockFrequency()
  639. {
  640. return this->CPUSpeedInMHz;
  641. }
  642. /** Return the APIC ID. */
  643. int SystemInformationImplementation::GetProcessorAPICID()
  644. {
  645. return this->Features.ExtendedFeatures.APIC_ID;
  646. }
  647. /** Return the L1 cache size. */
  648. int SystemInformationImplementation::GetProcessorCacheSize()
  649. {
  650. return this->Features.L1CacheSize;
  651. }
  652. /** Return the chosen cache size. */
  653. int SystemInformationImplementation::GetProcessorCacheXSize(long int dwCacheID)
  654. {
  655. switch (dwCacheID)
  656. {
  657. case L1CACHE_FEATURE:
  658. return this->Features.L1CacheSize;
  659. case L2CACHE_FEATURE:
  660. return this->Features.L2CacheSize;
  661. case L3CACHE_FEATURE:
  662. return this->Features.L3CacheSize;
  663. }
  664. return -1;
  665. }
  666. bool SystemInformationImplementation::DoesCPUSupportFeature(long int dwFeature)
  667. {
  668. bool bHasFeature = false;
  669. // Check for MMX instructions.
  670. if (((dwFeature & MMX_FEATURE) != 0) && this->Features.HasMMX) bHasFeature = true;
  671. // Check for MMX+ instructions.
  672. if (((dwFeature & MMX_PLUS_FEATURE) != 0) && this->Features.ExtendedFeatures.HasMMXPlus) bHasFeature = true;
  673. // Check for SSE FP instructions.
  674. if (((dwFeature & SSE_FEATURE) != 0) && this->Features.HasSSE) bHasFeature = true;
  675. // Check for SSE FP instructions.
  676. if (((dwFeature & SSE_FP_FEATURE) != 0) && this->Features.HasSSEFP) bHasFeature = true;
  677. // Check for SSE MMX instructions.
  678. if (((dwFeature & SSE_MMX_FEATURE) != 0) && this->Features.ExtendedFeatures.HasSSEMMX) bHasFeature = true;
  679. // Check for SSE2 instructions.
  680. if (((dwFeature & SSE2_FEATURE) != 0) && this->Features.HasSSE2) bHasFeature = true;
  681. // Check for 3DNow! instructions.
  682. if (((dwFeature & AMD_3DNOW_FEATURE) != 0) && this->Features.ExtendedFeatures.Has3DNow) bHasFeature = true;
  683. // Check for 3DNow+ instructions.
  684. if (((dwFeature & AMD_3DNOW_PLUS_FEATURE) != 0) && this->Features.ExtendedFeatures.Has3DNowPlus) bHasFeature = true;
  685. // Check for IA64 instructions.
  686. if (((dwFeature & IA64_FEATURE) != 0) && this->Features.HasIA64) bHasFeature = true;
  687. // Check for MP capable.
  688. if (((dwFeature & MP_CAPABLE) != 0) && this->Features.ExtendedFeatures.SupportsMP) bHasFeature = true;
  689. // Check for a serial number for the processor.
  690. if (((dwFeature & SERIALNUMBER_FEATURE) != 0) && this->Features.HasSerial) bHasFeature = true;
  691. // Check for a local APIC in the processor.
  692. if (((dwFeature & APIC_FEATURE) != 0) && this->Features.HasAPIC) bHasFeature = true;
  693. // Check for CMOV instructions.
  694. if (((dwFeature & CMOV_FEATURE) != 0) && this->Features.HasCMOV) bHasFeature = true;
  695. // Check for MTRR instructions.
  696. if (((dwFeature & MTRR_FEATURE) != 0) && this->Features.HasMTRR) bHasFeature = true;
  697. // Check for L1 cache size.
  698. if (((dwFeature & L1CACHE_FEATURE) != 0) && (this->Features.L1CacheSize != -1)) bHasFeature = true;
  699. // Check for L2 cache size.
  700. if (((dwFeature & L2CACHE_FEATURE) != 0) && (this->Features.L2CacheSize != -1)) bHasFeature = true;
  701. // Check for L3 cache size.
  702. if (((dwFeature & L3CACHE_FEATURE) != 0) && (this->Features.L3CacheSize != -1)) bHasFeature = true;
  703. // Check for ACPI capability.
  704. if (((dwFeature & ACPI_FEATURE) != 0) && this->Features.HasACPI) bHasFeature = true;
  705. // Check for thermal monitor support.
  706. if (((dwFeature & THERMALMONITOR_FEATURE) != 0) && this->Features.HasThermal) bHasFeature = true;
  707. // Check for temperature sensing diode support.
  708. if (((dwFeature & TEMPSENSEDIODE_FEATURE) != 0) && this->Features.ExtendedFeatures.PowerManagement.HasTempSenseDiode) bHasFeature = true;
  709. // Check for frequency ID support.
  710. if (((dwFeature & FREQUENCYID_FEATURE) != 0) && this->Features.ExtendedFeatures.PowerManagement.HasFrequencyID) bHasFeature = true;
  711. // Check for voltage ID support.
  712. if (((dwFeature & VOLTAGEID_FREQUENCY) != 0) && this->Features.ExtendedFeatures.PowerManagement.HasVoltageID) bHasFeature = true;
  713. return bHasFeature;
  714. }
  715. void SystemInformationImplementation::Delay(unsigned int uiMS)
  716. {
  717. #ifdef WIN32
  718. LARGE_INTEGER Frequency, StartCounter, EndCounter;
  719. __int64 x;
  720. // Get the frequency of the high performance counter.
  721. if (!QueryPerformanceFrequency (&Frequency)) return;
  722. x = Frequency.QuadPart / 1000 * uiMS;
  723. // Get the starting position of the counter.
  724. QueryPerformanceCounter (&StartCounter);
  725. do {
  726. // Get the ending position of the counter.
  727. QueryPerformanceCounter (&EndCounter);
  728. } while (EndCounter.QuadPart - StartCounter.QuadPart < x);
  729. #endif
  730. (void)uiMS;
  731. }
  732. bool SystemInformationImplementation::DoesCPUSupportCPUID()
  733. {
  734. #if USE_ASM_INSTRUCTIONS
  735. // Use SEH to determine CPUID presence
  736. __try {
  737. _asm {
  738. #ifdef CPUID_AWARE_COMPILER
  739. ; we must push/pop the registers <<CPUID>> writes to, as the
  740. ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
  741. ; these registers to change.
  742. push eax
  743. push ebx
  744. push ecx
  745. push edx
  746. #endif
  747. ; <<CPUID>>
  748. mov eax, 0
  749. CPUID_INSTRUCTION
  750. #ifdef CPUID_AWARE_COMPILER
  751. pop edx
  752. pop ecx
  753. pop ebx
  754. pop eax
  755. #endif
  756. }
  757. }
  758. __except(1)
  759. {
  760. // Stop the class from trying to use CPUID again!
  761. return false;
  762. }
  763. // The cpuid instruction succeeded.
  764. return true;
  765. #else
  766. // Assume no cpuid instruction.
  767. return false;
  768. #endif
  769. }
  770. bool SystemInformationImplementation::RetrieveCPUFeatures()
  771. {
  772. #if USE_ASM_INSTRUCTIONS
  773. int localCPUFeatures = 0;
  774. int localCPUAdvanced = 0;
  775. // Use assembly to detect CPUID information...
  776. __try {
  777. _asm {
  778. #ifdef CPUID_AWARE_COMPILER
  779. ; we must push/pop the registers <<CPUID>> writes to, as the
  780. ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
  781. ; these registers to change.
  782. push eax
  783. push ebx
  784. push ecx
  785. push edx
  786. #endif
  787. ; <<CPUID>>
  788. ; eax = 1 --> eax: CPU ID - bits 31..16 - unused, bits 15..12 - type, bits 11..8 - family, bits 7..4 - model, bits 3..0 - mask revision
  789. ; ebx: 31..24 - default APIC ID, 23..16 - logical processsor ID, 15..8 - CFLUSH chunk size , 7..0 - brand ID
  790. ; edx: CPU feature flags
  791. mov eax,1
  792. CPUID_INSTRUCTION
  793. mov localCPUFeatures, edx
  794. mov localCPUAdvanced, ebx
  795. #ifdef CPUID_AWARE_COMPILER
  796. pop edx
  797. pop ecx
  798. pop ebx
  799. pop eax
  800. #endif
  801. }
  802. }
  803. __except(1)
  804. {
  805. return false;
  806. }
  807. // Retrieve the features of CPU present.
  808. this->Features.HasFPU = ((localCPUFeatures & 0x00000001) != 0); // FPU Present --> Bit 0
  809. this->Features.HasTSC = ((localCPUFeatures & 0x00000010) != 0); // TSC Present --> Bit 4
  810. this->Features.HasAPIC = ((localCPUFeatures & 0x00000200) != 0); // APIC Present --> Bit 9
  811. this->Features.HasMTRR = ((localCPUFeatures & 0x00001000) != 0); // MTRR Present --> Bit 12
  812. this->Features.HasCMOV = ((localCPUFeatures & 0x00008000) != 0); // CMOV Present --> Bit 15
  813. this->Features.HasSerial = ((localCPUFeatures & 0x00040000) != 0); // Serial Present --> Bit 18
  814. this->Features.HasACPI = ((localCPUFeatures & 0x00400000) != 0); // ACPI Capable --> Bit 22
  815. this->Features.HasMMX = ((localCPUFeatures & 0x00800000) != 0); // MMX Present --> Bit 23
  816. this->Features.HasSSE = ((localCPUFeatures & 0x02000000) != 0); // SSE Present --> Bit 25
  817. this->Features.HasSSE2 = ((localCPUFeatures & 0x04000000) != 0); // SSE2 Present --> Bit 26
  818. this->Features.HasThermal = ((localCPUFeatures & 0x20000000) != 0); // Thermal Monitor Present --> Bit 29
  819. this->Features.HasIA64 = ((localCPUFeatures & 0x40000000) != 0); // IA64 Present --> Bit 30
  820. // Retrieve extended SSE capabilities if SSE is available.
  821. if (this->Features.HasSSE) {
  822. // Attempt to __try some SSE FP instructions.
  823. __try
  824. {
  825. // Perform: orps xmm0, xmm0
  826. _asm
  827. {
  828. _emit 0x0f
  829. _emit 0x56
  830. _emit 0xc0
  831. }
  832. // SSE FP capable processor.
  833. this->Features.HasSSEFP = true;
  834. }
  835. __except(1)
  836. {
  837. // bad instruction - processor or OS cannot handle SSE FP.
  838. this->Features.HasSSEFP = false;
  839. }
  840. }
  841. else
  842. {
  843. // Set the advanced SSE capabilities to not available.
  844. this->Features.HasSSEFP = false;
  845. }
  846. // Retrieve Intel specific extended features.
  847. if (this->ChipManufacturer == Intel)
  848. {
  849. this->Features.ExtendedFeatures.SupportsHyperthreading = ((localCPUFeatures & 0x10000000) != 0); // Intel specific: Hyperthreading --> Bit 28
  850. this->Features.ExtendedFeatures.LogicalProcessorsPerPhysical = (this->Features.ExtendedFeatures.SupportsHyperthreading) ? ((localCPUAdvanced & 0x00FF0000) >> 16) : 1;
  851. if ((this->Features.ExtendedFeatures.SupportsHyperthreading) && (this->Features.HasAPIC))
  852. {
  853. // Retrieve APIC information if there is one present.
  854. this->Features.ExtendedFeatures.APIC_ID = ((localCPUAdvanced & 0xFF000000) >> 24);
  855. }
  856. }
  857. return true;
  858. #else
  859. return false;
  860. #endif
  861. }
  862. /** Find the manufacturer given the vendor id */
  863. void SystemInformationImplementation::FindManufacturer()
  864. {
  865. if (this->ChipID.Vendor == "GenuineIntel") this->ChipManufacturer = Intel; // Intel Corp.
  866. else if (this->ChipID.Vendor == "UMC UMC UMC ") this->ChipManufacturer = UMC; // United Microelectronics Corp.
  867. else if (this->ChipID.Vendor == "AuthenticAMD") this->ChipManufacturer = AMD; // Advanced Micro Devices
  868. else if (this->ChipID.Vendor == "AMD ISBETTER") this->ChipManufacturer = AMD; // Advanced Micro Devices (1994)
  869. else if (this->ChipID.Vendor == "CyrixInstead") this->ChipManufacturer = Cyrix; // Cyrix Corp., VIA Inc.
  870. else if (this->ChipID.Vendor == "NexGenDriven") this->ChipManufacturer = NexGen; // NexGen Inc. (now AMD)
  871. else if (this->ChipID.Vendor == "CentaurHauls") this->ChipManufacturer = IDT; // IDT/Centaur (now VIA)
  872. else if (this->ChipID.Vendor == "RiseRiseRise") this->ChipManufacturer = Rise; // Rise
  873. else if (this->ChipID.Vendor == "GenuineTMx86") this->ChipManufacturer = Transmeta; // Transmeta
  874. else if (this->ChipID.Vendor == "TransmetaCPU") this->ChipManufacturer = Transmeta; // Transmeta
  875. else if (this->ChipID.Vendor == "Geode By NSC") this->ChipManufacturer = NSC; // National Semiconductor
  876. else if (this->ChipID.Vendor == "Sun") this->ChipManufacturer = Sun; // Sun Microelectronics
  877. else if (this->ChipID.Vendor == "IBM") this->ChipManufacturer = IBM; // IBM Microelectronics
  878. else if (this->ChipID.Vendor == "Motorola") this->ChipManufacturer = Motorola; // Motorola Microelectronics
  879. else this->ChipManufacturer = UnknownManufacturer; // Unknown manufacturer
  880. }
  881. /** */
  882. bool SystemInformationImplementation::RetrieveCPUIdentity()
  883. {
  884. #if USE_ASM_INSTRUCTIONS
  885. int localCPUVendor[3];
  886. int localCPUSignature;
  887. // Use assembly to detect CPUID information...
  888. __try
  889. {
  890. _asm
  891. {
  892. #ifdef CPUID_AWARE_COMPILER
  893. ; we must push/pop the registers <<CPUID>> writes to, as the
  894. ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
  895. ; these registers to change.
  896. push eax
  897. push ebx
  898. push ecx
  899. push edx
  900. #endif
  901. ; <<CPUID>>
  902. ; eax = 0 --> eax: maximum value of CPUID instruction.
  903. ; ebx: part 1 of 3; CPU signature.
  904. ; edx: part 2 of 3; CPU signature.
  905. ; ecx: part 3 of 3; CPU signature.
  906. mov eax, 0
  907. CPUID_INSTRUCTION
  908. mov localCPUVendor[0 * TYPE int], ebx
  909. mov localCPUVendor[1 * TYPE int], edx
  910. mov localCPUVendor[2 * TYPE int], ecx
  911. ; <<CPUID>>
  912. ; eax = 1 --> eax: CPU ID - bits 31..16 - unused, bits 15..12 - type, bits 11..8 - family, bits 7..4 - model, bits 3..0 - mask revision
  913. ; ebx: 31..24 - default APIC ID, 23..16 - logical processsor ID, 15..8 - CFLUSH chunk size , 7..0 - brand ID
  914. ; edx: CPU feature flags
  915. mov eax,1
  916. CPUID_INSTRUCTION
  917. mov localCPUSignature, eax
  918. #ifdef CPUID_AWARE_COMPILER
  919. pop edx
  920. pop ecx
  921. pop ebx
  922. pop eax
  923. #endif
  924. }
  925. }
  926. __except(1)
  927. {
  928. return false;
  929. }
  930. // Process the returned information.
  931. char vbuf[13];
  932. memcpy (&(vbuf[0]), &(localCPUVendor[0]), sizeof (int));
  933. memcpy (&(vbuf[4]), &(localCPUVendor[1]), sizeof (int));
  934. memcpy (&(vbuf[8]), &(localCPUVendor[2]), sizeof (int));
  935. vbuf[12] = '\0';
  936. this->ChipID.Vendor = vbuf;
  937. this->FindManufacturer();
  938. // Retrieve the family of CPU present.
  939. this->ChipID.ExtendedFamily = ((localCPUSignature & 0x0FF00000) >> 20); // Bits 27..20 Used
  940. this->ChipID.ExtendedModel = ((localCPUSignature & 0x000F0000) >> 16); // Bits 19..16 Used
  941. this->ChipID.Type = ((localCPUSignature & 0x0000F000) >> 12); // Bits 15..12 Used
  942. this->ChipID.Family = ((localCPUSignature & 0x00000F00) >> 8); // Bits 11..8 Used
  943. this->ChipID.Model = ((localCPUSignature & 0x000000F0) >> 4); // Bits 7..4 Used
  944. this->ChipID.Revision = ((localCPUSignature & 0x0000000F) >> 0); // Bits 3..0 Used
  945. return true;
  946. #else
  947. return false;
  948. #endif
  949. }
  950. /** */
  951. bool SystemInformationImplementation::RetrieveCPUCacheDetails()
  952. {
  953. #if USE_ASM_INSTRUCTIONS
  954. int L1Cache[4] = { 0, 0, 0, 0 };
  955. int L2Cache[4] = { 0, 0, 0, 0 };
  956. // Check to see if what we are about to do is supported...
  957. if (RetrieveCPUExtendedLevelSupport (0x80000005))
  958. {
  959. // Use assembly to retrieve the L1 cache information ...
  960. __try
  961. {
  962. _asm
  963. {
  964. #ifdef CPUID_AWARE_COMPILER
  965. ; we must push/pop the registers <<CPUID>> writes to, as the
  966. ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
  967. ; these registers to change.
  968. push eax
  969. push ebx
  970. push ecx
  971. push edx
  972. #endif
  973. ; <<CPUID>>
  974. ; eax = 0x80000005 --> eax: L1 cache information - Part 1 of 4.
  975. ; ebx: L1 cache information - Part 2 of 4.
  976. ; edx: L1 cache information - Part 3 of 4.
  977. ; ecx: L1 cache information - Part 4 of 4.
  978. mov eax, 0x80000005
  979. CPUID_INSTRUCTION
  980. mov L1Cache[0 * TYPE int], eax
  981. mov L1Cache[1 * TYPE int], ebx
  982. mov L1Cache[2 * TYPE int], ecx
  983. mov L1Cache[3 * TYPE int], edx
  984. #ifdef CPUID_AWARE_COMPILER
  985. pop edx
  986. pop ecx
  987. pop ebx
  988. pop eax
  989. #endif
  990. }
  991. }
  992. __except(1)
  993. {
  994. return false;
  995. }
  996. // Save the L1 data cache size (in KB) from ecx: bits 31..24 as well as data cache size from edx: bits 31..24.
  997. this->Features.L1CacheSize = ((L1Cache[2] & 0xFF000000) >> 24);
  998. this->Features.L1CacheSize += ((L1Cache[3] & 0xFF000000) >> 24);
  999. }
  1000. else
  1001. {
  1002. // Store -1 to indicate the cache could not be queried.
  1003. this->Features.L1CacheSize = -1;
  1004. }
  1005. // Check to see if what we are about to do is supported...
  1006. if (RetrieveCPUExtendedLevelSupport (0x80000006))
  1007. {
  1008. // Use assembly to retrieve the L2 cache information ...
  1009. __try
  1010. {
  1011. _asm
  1012. {
  1013. #ifdef CPUID_AWARE_COMPILER
  1014. ; we must push/pop the registers <<CPUID>> writes to, as the
  1015. ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
  1016. ; these registers to change.
  1017. push eax
  1018. push ebx
  1019. push ecx
  1020. push edx
  1021. #endif
  1022. ; <<CPUID>>
  1023. ; eax = 0x80000006 --> eax: L2 cache information - Part 1 of 4.
  1024. ; ebx: L2 cache information - Part 2 of 4.
  1025. ; edx: L2 cache information - Part 3 of 4.
  1026. ; ecx: L2 cache information - Part 4 of 4.
  1027. mov eax, 0x80000006
  1028. CPUID_INSTRUCTION
  1029. mov L2Cache[0 * TYPE int], eax
  1030. mov L2Cache[1 * TYPE int], ebx
  1031. mov L2Cache[2 * TYPE int], ecx
  1032. mov L2Cache[3 * TYPE int], edx
  1033. #ifdef CPUID_AWARE_COMPILER
  1034. pop edx
  1035. pop ecx
  1036. pop ebx
  1037. pop eax
  1038. #endif
  1039. }
  1040. }
  1041. __except(1)
  1042. {
  1043. return false;
  1044. }
  1045. // Save the L2 unified cache size (in KB) from ecx: bits 31..16.
  1046. this->Features.L2CacheSize = ((L2Cache[2] & 0xFFFF0000) >> 16);
  1047. }
  1048. else
  1049. {
  1050. // Store -1 to indicate the cache could not be queried.
  1051. this->Features.L2CacheSize = -1;
  1052. }
  1053. // Define L3 as being not present as we cannot test for it.
  1054. this->Features.L3CacheSize = -1;
  1055. #endif
  1056. // Return failure if we cannot detect either cache with this method.
  1057. return ((this->Features.L1CacheSize == -1) && (this->Features.L2CacheSize == -1)) ? false : true;
  1058. }
  1059. /** */
  1060. bool SystemInformationImplementation::RetrieveClassicalCPUCacheDetails()
  1061. {
  1062. #if USE_ASM_INSTRUCTIONS
  1063. int TLBCode = -1, TLBData = -1, L1Code = -1, L1Data = -1, L1Trace = -1, L2Unified = -1, L3Unified = -1;
  1064. int TLBCacheData[4] = { 0, 0, 0, 0 };
  1065. int TLBPassCounter = 0;
  1066. int TLBCacheUnit = 0;
  1067. do {
  1068. // Use assembly to retrieve the L2 cache information ...
  1069. __try {
  1070. _asm {
  1071. #ifdef CPUID_AWARE_COMPILER
  1072. ; we must push/pop the registers <<CPUID>> writes to, as the
  1073. ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
  1074. ; these registers to change.
  1075. push eax
  1076. push ebx
  1077. push ecx
  1078. push edx
  1079. #endif
  1080. ; <<CPUID>>
  1081. ; eax = 2 --> eax: TLB and cache information - Part 1 of 4.
  1082. ; ebx: TLB and cache information - Part 2 of 4.
  1083. ; ecx: TLB and cache information - Part 3 of 4.
  1084. ; edx: TLB and cache information - Part 4 of 4.
  1085. mov eax, 2
  1086. CPUID_INSTRUCTION
  1087. mov TLBCacheData[0 * TYPE int], eax
  1088. mov TLBCacheData[1 * TYPE int], ebx
  1089. mov TLBCacheData[2 * TYPE int], ecx
  1090. mov TLBCacheData[3 * TYPE int], edx
  1091. #ifdef CPUID_AWARE_COMPILER
  1092. pop edx
  1093. pop ecx
  1094. pop ebx
  1095. pop eax
  1096. #endif
  1097. }
  1098. }
  1099. __except(1)
  1100. {
  1101. return false;
  1102. }
  1103. int bob = ((TLBCacheData[0] & 0x00FF0000) >> 16);
  1104. (void)bob;
  1105. // Process the returned TLB and cache information.
  1106. for (int nCounter = 0; nCounter < TLBCACHE_INFO_UNITS; nCounter ++)
  1107. {
  1108. // First of all - decide which unit we are dealing with.
  1109. switch (nCounter)
  1110. {
  1111. // eax: bits 8..15 : bits 16..23 : bits 24..31
  1112. case 0: TLBCacheUnit = ((TLBCacheData[0] & 0x0000FF00) >> 8); break;
  1113. case 1: TLBCacheUnit = ((TLBCacheData[0] & 0x00FF0000) >> 16); break;
  1114. case 2: TLBCacheUnit = ((TLBCacheData[0] & 0xFF000000) >> 24); break;
  1115. // ebx: bits 0..7 : bits 8..15 : bits 16..23 : bits 24..31
  1116. case 3: TLBCacheUnit = ((TLBCacheData[1] & 0x000000FF) >> 0); break;
  1117. case 4: TLBCacheUnit = ((TLBCacheData[1] & 0x0000FF00) >> 8); break;
  1118. case 5: TLBCacheUnit = ((TLBCacheData[1] & 0x00FF0000) >> 16); break;
  1119. case 6: TLBCacheUnit = ((TLBCacheData[1] & 0xFF000000) >> 24); break;
  1120. // ecx: bits 0..7 : bits 8..15 : bits 16..23 : bits 24..31
  1121. case 7: TLBCacheUnit = ((TLBCacheData[2] & 0x000000FF) >> 0); break;
  1122. case 8: TLBCacheUnit = ((TLBCacheData[2] & 0x0000FF00) >> 8); break;
  1123. case 9: TLBCacheUnit = ((TLBCacheData[2] & 0x00FF0000) >> 16); break;
  1124. case 10: TLBCacheUnit = ((TLBCacheData[2] & 0xFF000000) >> 24); break;
  1125. // edx: bits 0..7 : bits 8..15 : bits 16..23 : bits 24..31
  1126. case 11: TLBCacheUnit = ((TLBCacheData[3] & 0x000000FF) >> 0); break;
  1127. case 12: TLBCacheUnit = ((TLBCacheData[3] & 0x0000FF00) >> 8); break;
  1128. case 13: TLBCacheUnit = ((TLBCacheData[3] & 0x00FF0000) >> 16); break;
  1129. case 14: TLBCacheUnit = ((TLBCacheData[3] & 0xFF000000) >> 24); break;
  1130. // Default case - an error has occured.
  1131. default: return false;
  1132. }
  1133. // Now process the resulting unit to see what it means....
  1134. switch (TLBCacheUnit)
  1135. {
  1136. case 0x00: break;
  1137. case 0x01: STORE_TLBCACHE_INFO (TLBCode, 4); break;
  1138. case 0x02: STORE_TLBCACHE_INFO (TLBCode, 4096); break;
  1139. case 0x03: STORE_TLBCACHE_INFO (TLBData, 4); break;
  1140. case 0x04: STORE_TLBCACHE_INFO (TLBData, 4096); break;
  1141. case 0x06: STORE_TLBCACHE_INFO (L1Code, 8); break;
  1142. case 0x08: STORE_TLBCACHE_INFO (L1Code, 16); break;
  1143. case 0x0a: STORE_TLBCACHE_INFO (L1Data, 8); break;
  1144. case 0x0c: STORE_TLBCACHE_INFO (L1Data, 16); break;
  1145. case 0x10: STORE_TLBCACHE_INFO (L1Data, 16); break; // <-- FIXME: IA-64 Only
  1146. case 0x15: STORE_TLBCACHE_INFO (L1Code, 16); break; // <-- FIXME: IA-64 Only
  1147. case 0x1a: STORE_TLBCACHE_INFO (L2Unified, 96); break; // <-- FIXME: IA-64 Only
  1148. case 0x22: STORE_TLBCACHE_INFO (L3Unified, 512); break;
  1149. case 0x23: STORE_TLBCACHE_INFO (L3Unified, 1024); break;
  1150. case 0x25: STORE_TLBCACHE_INFO (L3Unified, 2048); break;
  1151. case 0x29: STORE_TLBCACHE_INFO (L3Unified, 4096); break;
  1152. case 0x39: STORE_TLBCACHE_INFO (L2Unified, 128); break;
  1153. case 0x3c: STORE_TLBCACHE_INFO (L2Unified, 256); break;
  1154. case 0x40: STORE_TLBCACHE_INFO (L2Unified, 0); break; // <-- FIXME: No integrated L2 cache (P6 core) or L3 cache (P4 core).
  1155. case 0x41: STORE_TLBCACHE_INFO (L2Unified, 128); break;
  1156. case 0x42: STORE_TLBCACHE_INFO (L2Unified, 256); break;
  1157. case 0x43: STORE_TLBCACHE_INFO (L2Unified, 512); break;
  1158. case 0x44: STORE_TLBCACHE_INFO (L2Unified, 1024); break;
  1159. case 0x45: STORE_TLBCACHE_INFO (L2Unified, 2048); break;
  1160. case 0x50: STORE_TLBCACHE_INFO (TLBCode, 4096); break;
  1161. case 0x51: STORE_TLBCACHE_INFO (TLBCode, 4096); break;
  1162. case 0x52: STORE_TLBCACHE_INFO (TLBCode, 4096); break;
  1163. case 0x5b: STORE_TLBCACHE_INFO (TLBData, 4096); break;
  1164. case 0x5c: STORE_TLBCACHE_INFO (TLBData, 4096); break;
  1165. case 0x5d: STORE_TLBCACHE_INFO (TLBData, 4096); break;
  1166. case 0x66: STORE_TLBCACHE_INFO (L1Data, 8); break;
  1167. case 0x67: STORE_TLBCACHE_INFO (L1Data, 16); break;
  1168. case 0x68: STORE_TLBCACHE_INFO (L1Data, 32); break;
  1169. case 0x70: STORE_TLBCACHE_INFO (L1Trace, 12); break;
  1170. case 0x71: STORE_TLBCACHE_INFO (L1Trace, 16); break;
  1171. case 0x72: STORE_TLBCACHE_INFO (L1Trace, 32); break;
  1172. case 0x77: STORE_TLBCACHE_INFO (L1Code, 16); break; // <-- FIXME: IA-64 Only
  1173. case 0x79: STORE_TLBCACHE_INFO (L2Unified, 128); break;
  1174. case 0x7a: STORE_TLBCACHE_INFO (L2Unified, 256); break;
  1175. case 0x7b: STORE_TLBCACHE_INFO (L2Unified, 512); break;
  1176. case 0x7c: STORE_TLBCACHE_INFO (L2Unified, 1024); break;
  1177. case 0x7e: STORE_TLBCACHE_INFO (L2Unified, 256); break;
  1178. case 0x81: STORE_TLBCACHE_INFO (L2Unified, 128); break;
  1179. case 0x82: STORE_TLBCACHE_INFO (L2Unified, 256); break;
  1180. case 0x83: STORE_TLBCACHE_INFO (L2Unified, 512); break;
  1181. case 0x84: STORE_TLBCACHE_INFO (L2Unified, 1024); break;
  1182. case 0x85: STORE_TLBCACHE_INFO (L2Unified, 2048); break;
  1183. case 0x88: STORE_TLBCACHE_INFO (L3Unified, 2048); break; // <-- FIXME: IA-64 Only
  1184. case 0x89: STORE_TLBCACHE_INFO (L3Unified, 4096); break; // <-- FIXME: IA-64 Only
  1185. case 0x8a: STORE_TLBCACHE_INFO (L3Unified, 8192); break; // <-- FIXME: IA-64 Only
  1186. case 0x8d: STORE_TLBCACHE_INFO (L3Unified, 3096); break; // <-- FIXME: IA-64 Only
  1187. case 0x90: STORE_TLBCACHE_INFO (TLBCode, 262144); break; // <-- FIXME: IA-64 Only
  1188. case 0x96: STORE_TLBCACHE_INFO (TLBCode, 262144); break; // <-- FIXME: IA-64 Only
  1189. case 0x9b: STORE_TLBCACHE_INFO (TLBCode, 262144); break; // <-- FIXME: IA-64 Only
  1190. // Default case - an error has occured.
  1191. default: return false;
  1192. }
  1193. }
  1194. // Increment the TLB pass counter.
  1195. TLBPassCounter ++;
  1196. } while ((TLBCacheData[0] & 0x000000FF) > TLBPassCounter);
  1197. // Ok - we now have the maximum TLB, L1, L2, and L3 sizes...
  1198. if ((L1Code == -1) && (L1Data == -1) && (L1Trace == -1))
  1199. {
  1200. this->Features.L1CacheSize = -1;
  1201. }
  1202. else if ((L1Code == -1) && (L1Data == -1) && (L1Trace != -1))
  1203. {
  1204. this->Features.L1CacheSize = L1Trace;
  1205. }
  1206. else if ((L1Code != -1) && (L1Data == -1))
  1207. {
  1208. this->Features.L1CacheSize = L1Code;
  1209. }
  1210. else if ((L1Code == -1) && (L1Data != -1))
  1211. {
  1212. this->Features.L1CacheSize = L1Data;
  1213. }
  1214. else if ((L1Code != -1) && (L1Data != -1))
  1215. {
  1216. this->Features.L1CacheSize = L1Code + L1Data;
  1217. }
  1218. else
  1219. {
  1220. this->Features.L1CacheSize = -1;
  1221. }
  1222. // Ok - we now have the maximum TLB, L1, L2, and L3 sizes...
  1223. if (L2Unified == -1)
  1224. {
  1225. this->Features.L2CacheSize = -1;
  1226. }
  1227. else
  1228. {
  1229. this->Features.L2CacheSize = L2Unified;
  1230. }
  1231. // Ok - we now have the maximum TLB, L1, L2, and L3 sizes...
  1232. if (L3Unified == -1)
  1233. {
  1234. this->Features.L3CacheSize = -1;
  1235. }
  1236. else
  1237. {
  1238. this->Features.L3CacheSize = L3Unified;
  1239. }
  1240. return true;
  1241. #else
  1242. return false;
  1243. #endif
  1244. }
  1245. /** */
  1246. bool SystemInformationImplementation::RetrieveCPUClockSpeed()
  1247. {
  1248. bool retrieved = false;
  1249. #if defined(_WIN32)
  1250. // First of all we check to see if the RDTSC (0x0F, 0x31) instruction is
  1251. // supported. If not, we fallback to trying to read this value from the
  1252. // registry:
  1253. //
  1254. if (!this->Features.HasTSC)
  1255. {
  1256. HKEY hKey = NULL;
  1257. LONG err = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  1258. "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0,
  1259. KEY_READ, &hKey);
  1260. if (ERROR_SUCCESS == err)
  1261. {
  1262. DWORD dwType = 0;
  1263. DWORD data = 0;
  1264. DWORD dwSize = sizeof(DWORD);
  1265. err = RegQueryValueEx(hKey, "~MHz", 0,
  1266. &dwType, (LPBYTE) &data, &dwSize);
  1267. if (ERROR_SUCCESS == err)
  1268. {
  1269. this->CPUSpeedInMHz = (float) data;
  1270. retrieved = true;
  1271. }
  1272. RegCloseKey(hKey);
  1273. hKey = NULL;
  1274. }
  1275. return retrieved;
  1276. }
  1277. unsigned int uiRepetitions = 1;
  1278. unsigned int uiMSecPerRepetition = 50;
  1279. __int64 i64Total = 0;
  1280. __int64 i64Overhead = 0;
  1281. for (unsigned int nCounter = 0; nCounter < uiRepetitions; nCounter ++)
  1282. {
  1283. i64Total += GetCyclesDifference (SystemInformationImplementation::Delay,
  1284. uiMSecPerRepetition);
  1285. i64Overhead +=
  1286. GetCyclesDifference (SystemInformationImplementation::DelayOverhead,
  1287. uiMSecPerRepetition);
  1288. }
  1289. // Calculate the MHz speed.
  1290. i64Total -= i64Overhead;
  1291. i64Total /= uiRepetitions;
  1292. i64Total /= uiMSecPerRepetition;
  1293. i64Total /= 1000;
  1294. // Save the CPU speed.
  1295. this->CPUSpeedInMHz = (float) i64Total;
  1296. retrieved = true;
  1297. #endif
  1298. return retrieved;
  1299. }
  1300. /** */
  1301. bool SystemInformationImplementation::RetrieveClassicalCPUClockSpeed()
  1302. {
  1303. #if USE_ASM_INSTRUCTIONS
  1304. LARGE_INTEGER liStart, liEnd, liCountsPerSecond;
  1305. double dFrequency, dDifference;
  1306. // Attempt to get a starting tick count.
  1307. QueryPerformanceCounter (&liStart);
  1308. __try
  1309. {
  1310. _asm
  1311. {
  1312. mov eax, 0x80000000
  1313. mov ebx, CLASSICAL_CPU_FREQ_LOOP
  1314. Timer_Loop:
  1315. bsf ecx,eax
  1316. dec ebx
  1317. jnz Timer_Loop
  1318. }
  1319. }
  1320. __except(1)
  1321. {
  1322. return false;
  1323. }
  1324. // Attempt to get a starting tick count.
  1325. QueryPerformanceCounter (&liEnd);
  1326. // Get the difference... NB: This is in seconds....
  1327. QueryPerformanceFrequency (&liCountsPerSecond);
  1328. dDifference = (((double) liEnd.QuadPart - (double) liStart.QuadPart) / (double) liCountsPerSecond.QuadPart);
  1329. // Calculate the clock speed.
  1330. if (this->ChipID.Family == 3)
  1331. {
  1332. // 80386 processors.... Loop time is 115 cycles!
  1333. dFrequency = (((CLASSICAL_CPU_FREQ_LOOP * 115) / dDifference) / 1000000);
  1334. }
  1335. else if (this->ChipID.Family == 4)
  1336. {
  1337. // 80486 processors.... Loop time is 47 cycles!
  1338. dFrequency = (((CLASSICAL_CPU_FREQ_LOOP * 47) / dDifference) / 1000000);
  1339. }
  1340. else if (this->ChipID.Family == 5)
  1341. {
  1342. // Pentium processors.... Loop time is 43 cycles!
  1343. dFrequency = (((CLASSICAL_CPU_FREQ_LOOP * 43) / dDifference) / 1000000);
  1344. }
  1345. // Save the clock speed.
  1346. this->Features.CPUSpeed = (int) dFrequency;
  1347. return true;
  1348. #else
  1349. return false;
  1350. #endif
  1351. }
  1352. /** */
  1353. bool SystemInformationImplementation::RetrieveCPUExtendedLevelSupport(int CPULevelToCheck)
  1354. {
  1355. int MaxCPUExtendedLevel = 0;
  1356. // The extended CPUID is supported by various vendors starting with the following CPU models:
  1357. //
  1358. // Manufacturer & Chip Name | Family Model Revision
  1359. //
  1360. // AMD K6, K6-2 | 5 6 x
  1361. // Cyrix GXm, Cyrix III "Joshua" | 5 4 x
  1362. // IDT C6-2 | 5 8 x
  1363. // VIA Cyrix III | 6 5 x
  1364. // Transmeta Crusoe | 5 x x
  1365. // Intel Pentium 4 | f x x
  1366. //
  1367. // We check to see if a supported processor is present...
  1368. if (this->ChipManufacturer == AMD)
  1369. {
  1370. if (this->ChipID.Family < 5) return false;
  1371. if ((this->ChipID.Family == 5) && (this->ChipID.Model < 6)) return false;
  1372. }
  1373. else if (this->ChipManufacturer == Cyrix)
  1374. {
  1375. if (this->ChipID.Family < 5) return false;
  1376. if ((this->ChipID.Family == 5) && (this->ChipID.Model < 4)) return false;
  1377. if ((this->ChipID.Family == 6) && (this->ChipID.Model < 5)) return false;
  1378. }
  1379. else if (this->ChipManufacturer == IDT)
  1380. {
  1381. if (this->ChipID.Family < 5) return false;
  1382. if ((this->ChipID.Family == 5) && (this->ChipID.Model < 8)) return false;
  1383. }
  1384. else if (this->ChipManufacturer == Transmeta)
  1385. {
  1386. if (this->ChipID.Family < 5) return false;
  1387. }
  1388. else if (this->ChipManufacturer == Intel)
  1389. {
  1390. if (this->ChipID.Family < 0xf)
  1391. {
  1392. return false;
  1393. }
  1394. }
  1395. #if USE_ASM_INSTRUCTIONS
  1396. // Use assembly to detect CPUID information...
  1397. __try {
  1398. _asm {
  1399. #ifdef CPUID_AWARE_COMPILER
  1400. ; we must push/pop the registers <<CPUID>> writes to, as the
  1401. ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
  1402. ; these registers to change.
  1403. push eax
  1404. push ebx
  1405. push ecx
  1406. push edx
  1407. #endif
  1408. ; <<CPUID>>
  1409. ; eax = 0x80000000 --> eax: maximum supported extended level
  1410. mov eax,0x80000000
  1411. CPUID_INSTRUCTION
  1412. mov MaxCPUExtendedLevel, eax
  1413. #ifdef CPUID_AWARE_COMPILER
  1414. pop edx
  1415. pop ecx
  1416. pop ebx
  1417. pop eax
  1418. #endif
  1419. }
  1420. }
  1421. __except(1)
  1422. {
  1423. return false;
  1424. }
  1425. #endif
  1426. // Now we have to check the level wanted vs level returned...
  1427. int nLevelWanted = (CPULevelToCheck & 0x7FFFFFFF);
  1428. int nLevelReturn = (MaxCPUExtendedLevel & 0x7FFFFFFF);
  1429. // Check to see if the level provided is supported...
  1430. if (nLevelWanted > nLevelReturn)
  1431. {
  1432. return false;
  1433. }
  1434. return true;
  1435. }
  1436. /** */
  1437. bool SystemInformationImplementation::RetrieveExtendedCPUFeatures()
  1438. {
  1439. // Check that we are not using an Intel processor as it does not support this.
  1440. if (this->ChipManufacturer == Intel)
  1441. {
  1442. return false;
  1443. }
  1444. // Check to see if what we are about to do is supported...
  1445. if (!RetrieveCPUExtendedLevelSupport(static_cast<int>(0x80000001)))
  1446. {
  1447. return false;
  1448. }
  1449. #if USE_ASM_INSTRUCTIONS
  1450. int localCPUExtendedFeatures = 0;
  1451. // Use assembly to detect CPUID information...
  1452. __try
  1453. {
  1454. _asm
  1455. {
  1456. #ifdef CPUID_AWARE_COMPILER
  1457. ; we must push/pop the registers <<CPUID>> writes to, as the
  1458. ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
  1459. ; these registers to change.
  1460. push eax
  1461. push ebx
  1462. push ecx
  1463. push edx
  1464. #endif
  1465. ; <<CPUID>>
  1466. ; eax = 0x80000001 --> eax: CPU ID - bits 31..16 - unused, bits 15..12 - type, bits 11..8 - family, bits 7..4 - model, bits 3..0 - mask revision
  1467. ; ebx: 31..24 - default APIC ID, 23..16 - logical processsor ID, 15..8 - CFLUSH chunk size , 7..0 - brand ID
  1468. ; edx: CPU feature flags
  1469. mov eax,0x80000001
  1470. CPUID_INSTRUCTION
  1471. mov localCPUExtendedFeatures, edx
  1472. #ifdef CPUID_AWARE_COMPILER
  1473. pop edx
  1474. pop ecx
  1475. pop ebx
  1476. pop eax
  1477. #endif
  1478. }
  1479. }
  1480. __except(1)
  1481. {
  1482. return false;
  1483. }
  1484. // Retrieve the extended features of CPU present.
  1485. this->Features.ExtendedFeatures.Has3DNow = ((localCPUExtendedFeatures & 0x80000000) != 0); // 3DNow Present --> Bit 31.
  1486. this->Features.ExtendedFeatures.Has3DNowPlus = ((localCPUExtendedFeatures & 0x40000000) != 0); // 3DNow+ Present -- > Bit 30.
  1487. this->Features.ExtendedFeatures.HasSSEMMX = ((localCPUExtendedFeatures & 0x00400000) != 0); // SSE MMX Present --> Bit 22.
  1488. this->Features.ExtendedFeatures.SupportsMP = ((localCPUExtendedFeatures & 0x00080000) != 0); // MP Capable -- > Bit 19.
  1489. // Retrieve AMD specific extended features.
  1490. if (this->ChipManufacturer == AMD)
  1491. {
  1492. this->Features.ExtendedFeatures.HasMMXPlus = ((localCPUExtendedFeatures & 0x00400000) != 0); // AMD specific: MMX-SSE --> Bit 22
  1493. }
  1494. // Retrieve Cyrix specific extended features.
  1495. if (this->ChipManufacturer == Cyrix)
  1496. {
  1497. this->Features.ExtendedFeatures.HasMMXPlus = ((localCPUExtendedFeatures & 0x01000000) != 0); // Cyrix specific: Extended MMX --> Bit 24
  1498. }
  1499. return true;
  1500. #else
  1501. return false;
  1502. #endif
  1503. }
  1504. /** */
  1505. bool SystemInformationImplementation::RetrieveProcessorSerialNumber()
  1506. {
  1507. // Check to see if the processor supports the processor serial number.
  1508. if (!this->Features.HasSerial)
  1509. {
  1510. return false;
  1511. }
  1512. #if USE_ASM_INSTRUCTIONS
  1513. int SerialNumber[3];
  1514. // Use assembly to detect CPUID information...
  1515. __try {
  1516. _asm {
  1517. #ifdef CPUID_AWARE_COMPILER
  1518. ; we must push/pop the registers <<CPUID>> writes to, as the
  1519. ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
  1520. ; these registers to change.
  1521. push eax
  1522. push ebx
  1523. push ecx
  1524. push edx
  1525. #endif
  1526. ; <<CPUID>>
  1527. ; eax = 3 --> ebx: top 32 bits are the processor signature bits --> NB: Transmeta only ?!?
  1528. ; ecx: middle 32 bits are the processor signature bits
  1529. ; edx: bottom 32 bits are the processor signature bits
  1530. mov eax, 3
  1531. CPUID_INSTRUCTION
  1532. mov SerialNumber[0 * TYPE int], ebx
  1533. mov SerialNumber[1 * TYPE int], ecx
  1534. mov SerialNumber[2 * TYPE int], edx
  1535. #ifdef CPUID_AWARE_COMPILER
  1536. pop edx
  1537. pop ecx
  1538. pop ebx
  1539. pop eax
  1540. #endif
  1541. }
  1542. }
  1543. __except(1)
  1544. {
  1545. return false;
  1546. }
  1547. // Process the returned information.
  1548. char sn[128];
  1549. sprintf (sn, "%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x",
  1550. ((SerialNumber[0] & 0xff000000) >> 24),
  1551. ((SerialNumber[0] & 0x00ff0000) >> 16),
  1552. ((SerialNumber[0] & 0x0000ff00) >> 8),
  1553. ((SerialNumber[0] & 0x000000ff) >> 0),
  1554. ((SerialNumber[1] & 0xff000000) >> 24),
  1555. ((SerialNumber[1] & 0x00ff0000) >> 16),
  1556. ((SerialNumber[1] & 0x0000ff00) >> 8),
  1557. ((SerialNumber[1] & 0x000000ff) >> 0),
  1558. ((SerialNumber[2] & 0xff000000) >> 24),
  1559. ((SerialNumber[2] & 0x00ff0000) >> 16),
  1560. ((SerialNumber[2] & 0x0000ff00) >> 8),
  1561. ((SerialNumber[2] & 0x000000ff) >> 0));
  1562. this->ChipID.SerialNumber = sn;
  1563. return true;
  1564. #else
  1565. return false;
  1566. #endif
  1567. }
  1568. /** */
  1569. bool SystemInformationImplementation::RetrieveCPUPowerManagement()
  1570. {
  1571. // Check to see if what we are about to do is supported...
  1572. if (!RetrieveCPUExtendedLevelSupport(static_cast<int>(0x80000007)))
  1573. {
  1574. this->Features.ExtendedFeatures.PowerManagement.HasFrequencyID = false;
  1575. this->Features.ExtendedFeatures.PowerManagement.HasVoltageID = false;
  1576. this->Features.ExtendedFeatures.PowerManagement.HasTempSenseDiode = false;
  1577. return false;
  1578. }
  1579. #if USE_ASM_INSTRUCTIONS
  1580. int localCPUPowerManagement = 0;
  1581. // Use assembly to detect CPUID information...
  1582. __try {
  1583. _asm {
  1584. #ifdef CPUID_AWARE_COMPILER
  1585. ; we must push/pop the registers <<CPUID>> writes to, as the
  1586. ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
  1587. ; these registers to change.
  1588. push eax
  1589. push ebx
  1590. push ecx
  1591. push edx
  1592. #endif
  1593. ; <<CPUID>>
  1594. ; eax = 0x80000007 --> edx: get processor power management
  1595. mov eax,0x80000007
  1596. CPUID_INSTRUCTION
  1597. mov localCPUPowerManagement, edx
  1598. #ifdef CPUID_AWARE_COMPILER
  1599. pop edx
  1600. pop ecx
  1601. pop ebx
  1602. pop eax
  1603. #endif
  1604. }
  1605. }
  1606. __except(1)
  1607. {
  1608. return false;
  1609. }
  1610. // Check for the power management capabilities of the CPU.
  1611. this->Features.ExtendedFeatures.PowerManagement.HasTempSenseDiode = ((localCPUPowerManagement & 0x00000001) != 0);
  1612. this->Features.ExtendedFeatures.PowerManagement.HasFrequencyID = ((localCPUPowerManagement & 0x00000002) != 0);
  1613. this->Features.ExtendedFeatures.PowerManagement.HasVoltageID = ((localCPUPowerManagement & 0x00000004) != 0);
  1614. return true;
  1615. #else
  1616. return false;
  1617. #endif
  1618. }
  1619. void SystemInformationStripLeadingSpace(kwsys_stl::string& str)
  1620. {
  1621. // Because some manufacturers have leading white space - we have to post-process the name.
  1622. kwsys_stl::string::size_type pos = str.find_first_not_of(" ");
  1623. if(pos != kwsys_stl::string::npos)
  1624. {
  1625. str = str.substr(pos);
  1626. }
  1627. }
  1628. /** */
  1629. bool SystemInformationImplementation::RetrieveExtendedCPUIdentity()
  1630. {
  1631. // Check to see if what we are about to do is supported...
  1632. if (!RetrieveCPUExtendedLevelSupport(static_cast<int>(0x80000002)))
  1633. return false;
  1634. if (!RetrieveCPUExtendedLevelSupport(static_cast<int>(0x80000003)))
  1635. return false;
  1636. if (!RetrieveCPUExtendedLevelSupport(static_cast<int>(0x80000004)))
  1637. return false;
  1638. #if USE_ASM_INSTRUCTIONS
  1639. int CPUExtendedIdentity[12];
  1640. // Use assembly to detect CPUID information...
  1641. __try {
  1642. _asm {
  1643. #ifdef CPUID_AWARE_COMPILER
  1644. ; we must push/pop the registers <<CPUID>> writes to, as the
  1645. ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
  1646. ; these registers to change.
  1647. push eax
  1648. push ebx
  1649. push ecx
  1650. push edx
  1651. #endif
  1652. ; <<CPUID>>
  1653. ; eax = 0x80000002 --> eax, ebx, ecx, edx: get processor name string (part 1)
  1654. mov eax,0x80000002
  1655. CPUID_INSTRUCTION
  1656. mov CPUExtendedIdentity[0 * TYPE int], eax
  1657. mov CPUExtendedIdentity[1 * TYPE int], ebx
  1658. mov CPUExtendedIdentity[2 * TYPE int], ecx
  1659. mov CPUExtendedIdentity[3 * TYPE int], edx
  1660. ; <<CPUID>>
  1661. ; eax = 0x80000003 --> eax, ebx, ecx, edx: get processor name string (part 2)
  1662. mov eax,0x80000003
  1663. CPUID_INSTRUCTION
  1664. mov CPUExtendedIdentity[4 * TYPE int], eax
  1665. mov CPUExtendedIdentity[5 * TYPE int], ebx
  1666. mov CPUExtendedIdentity[6 * TYPE int], ecx
  1667. mov CPUExtendedIdentity[7 * TYPE int], edx
  1668. ; <<CPUID>>
  1669. ; eax = 0x80000004 --> eax, ebx, ecx, edx: get processor name string (part 3)
  1670. mov eax,0x80000004
  1671. CPUID_INSTRUCTION
  1672. mov CPUExtendedIdentity[8 * TYPE int], eax
  1673. mov CPUExtendedIdentity[9 * TYPE int], ebx
  1674. mov CPUExtendedIdentity[10 * TYPE int], ecx
  1675. mov CPUExtendedIdentity[11 * TYPE int], edx
  1676. #ifdef CPUID_AWARE_COMPILER
  1677. pop edx
  1678. pop ecx
  1679. pop ebx
  1680. pop eax
  1681. #endif
  1682. }
  1683. }
  1684. __except(1)
  1685. {
  1686. return false;
  1687. }
  1688. // Process the returned information.
  1689. char nbuf[49];
  1690. memcpy (&(nbuf[0]), &(CPUExtendedIdentity[0]), sizeof (int));
  1691. memcpy (&(nbuf[4]), &(CPUExtendedIdentity[1]), sizeof (int));
  1692. memcpy (&(nbuf[8]), &(CPUExtendedIdentity[2]), sizeof (int));
  1693. memcpy (&(nbuf[12]), &(CPUExtendedIdentity[3]), sizeof (int));
  1694. memcpy (&(nbuf[16]), &(CPUExtendedIdentity[4]), sizeof (int));
  1695. memcpy (&(nbuf[20]), &(CPUExtendedIdentity[5]), sizeof (int));
  1696. memcpy (&(nbuf[24]), &(CPUExtendedIdentity[6]), sizeof (int));
  1697. memcpy (&(nbuf[28]), &(CPUExtendedIdentity[7]), sizeof (int));
  1698. memcpy (&(nbuf[32]), &(CPUExtendedIdentity[8]), sizeof (int));
  1699. memcpy (&(nbuf[36]), &(CPUExtendedIdentity[9]), sizeof (int));
  1700. memcpy (&(nbuf[40]), &(CPUExtendedIdentity[10]), sizeof (int));
  1701. memcpy (&(nbuf[44]), &(CPUExtendedIdentity[11]), sizeof (int));
  1702. nbuf[48] = '\0';
  1703. this->ChipID.ProcessorName = nbuf;
  1704. // Because some manufacturers have leading white space - we have to post-process the name.
  1705. SystemInformationStripLeadingSpace(this->ChipID.ProcessorName);
  1706. return true;
  1707. #else
  1708. return false;
  1709. #endif
  1710. }
  1711. /** */
  1712. bool SystemInformationImplementation::RetrieveClassicalCPUIdentity()
  1713. {
  1714. // Start by decided which manufacturer we are using....
  1715. switch (this->ChipManufacturer)
  1716. {
  1717. case Intel:
  1718. // Check the family / model / revision to determine the CPU ID.
  1719. switch (this->ChipID.Family) {
  1720. case 3:
  1721. this->ChipID.ProcessorName = "Newer i80386 family";
  1722. break;
  1723. case 4:
  1724. switch (this->ChipID.Model) {
  1725. case 0: this->ChipID.ProcessorName = "i80486DX-25/33"; break;
  1726. case 1: this->ChipID.ProcessorName = "i80486DX-50"; break;
  1727. case 2: this->ChipID.ProcessorName = "i80486SX"; break;
  1728. case 3: this->ChipID.ProcessorName = "i80486DX2"; break;
  1729. case 4: this->ChipID.ProcessorName = "i80486SL"; break;
  1730. case 5: this->ChipID.ProcessorName = "i80486SX2"; break;
  1731. case 7: this->ChipID.ProcessorName = "i80486DX2 WriteBack"; break;
  1732. case 8: this->ChipID.ProcessorName = "i80486DX4"; break;
  1733. case 9: this->ChipID.ProcessorName = "i80486DX4 WriteBack"; break;
  1734. default: this->ChipID.ProcessorName = "Unknown 80486 family"; return false;
  1735. }
  1736. break;
  1737. case 5:
  1738. switch (this->ChipID.Model)
  1739. {
  1740. case 0: this->ChipID.ProcessorName = "P5 A-Step"; break;
  1741. case 1: this->ChipID.ProcessorName = "P5"; break;
  1742. case 2: this->ChipID.ProcessorName = "P54C"; break;
  1743. case 3: this->ChipID.ProcessorName = "P24T OverDrive"; break;
  1744. case 4: this->ChipID.ProcessorName = "P55C"; break;
  1745. case 7: this->ChipID.ProcessorName = "P54C"; break;
  1746. case 8: this->ChipID.ProcessorName = "P55C (0.25micron)"; break;
  1747. default: this->ChipID.ProcessorName = "Unknown Pentium family"; return false;
  1748. }
  1749. break;
  1750. case 6:
  1751. switch (this->ChipID.Model)
  1752. {
  1753. case 0: this->ChipID.ProcessorName = "P6 A-Step"; break;
  1754. case 1: this->ChipID.ProcessorName = "P6"; break;
  1755. case 3: this->ChipID.ProcessorName = "Pentium II (0.28 micron)"; break;
  1756. case 5: this->ChipID.ProcessorName = "Pentium II (0.25 micron)"; break;
  1757. case 6: this->ChipID.ProcessorName = "Pentium II With On-Die L2 Cache"; break;
  1758. case 7: this->ChipID.ProcessorName = "Pentium III (0.25 micron)"; break;
  1759. case 8: this->ChipID.ProcessorName = "Pentium III (0.18 micron) With 256 KB On-Die L2 Cache "; break;
  1760. case 0xa: this->ChipID.ProcessorName = "Pentium III (0.18 micron) With 1 Or 2 MB On-Die L2 Cache "; break;
  1761. case 0xb: this->ChipID.ProcessorName = "Pentium III (0.13 micron) With 256 Or 512 KB On-Die L2 Cache "; break;
  1762. case 23: this->ChipID.ProcessorName = "Intel(R) Core(TM)2 Duo CPU T9500 @ 2.60GHz"; break;
  1763. default: this->ChipID.ProcessorName = "Unknown P6 family"; return false;
  1764. }
  1765. break;
  1766. case 7:
  1767. this->ChipID.ProcessorName = "Intel Merced (IA-64)";
  1768. break;
  1769. case 0xf:
  1770. // Check the extended family bits...
  1771. switch (this->ChipID.ExtendedFamily)
  1772. {
  1773. case 0:
  1774. switch (this->ChipID.Model)
  1775. {
  1776. case 0: this->ChipID.ProcessorName = "Pentium IV (0.18 micron)"; break;
  1777. case 1: this->ChipID.ProcessorName = "Pentium IV (0.18 micron)"; break;
  1778. case 2: this->ChipID.ProcessorName = "Pentium IV (0.13 micron)"; break;
  1779. default: this->ChipID.ProcessorName = "Unknown Pentium 4 family"; return false;
  1780. }
  1781. break;
  1782. case 1:
  1783. this->ChipID.ProcessorName = "Intel McKinley (IA-64)";
  1784. break;
  1785. default:
  1786. this->ChipID.ProcessorName = "Pentium";
  1787. }
  1788. break;
  1789. default:
  1790. this->ChipID.ProcessorName = "Unknown Intel family";
  1791. return false;
  1792. }
  1793. break;
  1794. case AMD:
  1795. // Check the family / model / revision to determine the CPU ID.
  1796. switch (this->ChipID.Family)
  1797. {
  1798. case 4:
  1799. switch (this->ChipID.Model)
  1800. {
  1801. case 3: this->ChipID.ProcessorName = "80486DX2"; break;
  1802. case 7: this->ChipID.ProcessorName = "80486DX2 WriteBack"; break;
  1803. case 8: this->ChipID.ProcessorName = "80486DX4"; break;
  1804. case 9: this->ChipID.ProcessorName = "80486DX4 WriteBack"; break;
  1805. case 0xe: this->ChipID.ProcessorName = "5x86"; break;
  1806. case 0xf: this->ChipID.ProcessorName = "5x86WB"; break;
  1807. default: this->ChipID.ProcessorName = "Unknown 80486 family"; return false;
  1808. }
  1809. break;
  1810. case 5:
  1811. switch (this->ChipID.Model)
  1812. {
  1813. case 0: this->ChipID.ProcessorName = "SSA5 (PR75, PR90 = PR100)"; break;
  1814. case 1: this->ChipID.ProcessorName = "5k86 (PR120 = PR133)"; break;
  1815. case 2: this->ChipID.ProcessorName = "5k86 (PR166)"; break;
  1816. case 3: this->ChipID.ProcessorName = "5k86 (PR200)"; break;
  1817. case 6: this->ChipID.ProcessorName = "K6 (0.30 micron)"; break;
  1818. case 7: this->ChipID.ProcessorName = "K6 (0.25 micron)"; break;
  1819. case 8: this->ChipID.ProcessorName = "K6-2"; break;
  1820. case 9: this->ChipID.ProcessorName = "K6-III"; break;
  1821. case 0xd: this->ChipID.ProcessorName = "K6-2+ or K6-III+ (0.18 micron)"; break;
  1822. default: this->ChipID.ProcessorName = "Unknown 80586 family"; return false;
  1823. }
  1824. break;
  1825. case 6:
  1826. switch (this->ChipID.Model)
  1827. {
  1828. case 1: this->ChipID.ProcessorName = "Athlon- (0.25 micron)"; break;
  1829. case 2: this->ChipID.ProcessorName = "Athlon- (0.18 micron)"; break;
  1830. case 3: this->ChipID.ProcessorName = "Duron- (SF core)"; break;
  1831. case 4: this->ChipID.ProcessorName = "Athlon- (Thunderbird core)"; break;
  1832. case 6: this->ChipID.ProcessorName = "Athlon- (Palomino core)"; break;
  1833. case 7: this->ChipID.ProcessorName = "Duron- (Morgan core)"; break;
  1834. case 8:
  1835. if (this->Features.ExtendedFeatures.SupportsMP)
  1836. this->ChipID.ProcessorName = "Athlon - MP (Thoroughbred core)";
  1837. else this->ChipID.ProcessorName = "Athlon - XP (Thoroughbred core)";
  1838. break;
  1839. default: this->ChipID.ProcessorName = "Unknown K7 family"; return false;
  1840. }
  1841. break;
  1842. default:
  1843. this->ChipID.ProcessorName = "Unknown AMD family";
  1844. return false;
  1845. }
  1846. break;
  1847. case Transmeta:
  1848. switch (this->ChipID.Family)
  1849. {
  1850. case 5:
  1851. switch (this->ChipID.Model)
  1852. {
  1853. case 4: this->ChipID.ProcessorName = "Crusoe TM3x00 and TM5x00"; break;
  1854. default: this->ChipID.ProcessorName = "Unknown Crusoe family"; return false;
  1855. }
  1856. break;
  1857. default:
  1858. this->ChipID.ProcessorName = "Unknown Transmeta family";
  1859. return false;
  1860. }
  1861. break;
  1862. case Rise:
  1863. switch (this->ChipID.Family)
  1864. {
  1865. case 5:
  1866. switch (this->ChipID.Model)
  1867. {
  1868. case 0: this->ChipID.ProcessorName = "mP6 (0.25 micron)"; break;
  1869. case 2: this->ChipID.ProcessorName = "mP6 (0.18 micron)"; break;
  1870. default: this->ChipID.ProcessorName = "Unknown Rise family"; return false;
  1871. }
  1872. break;
  1873. default:
  1874. this->ChipID.ProcessorName = "Unknown Rise family";
  1875. return false;
  1876. }
  1877. break;
  1878. case UMC:
  1879. switch (this->ChipID.Family)
  1880. {
  1881. case 4:
  1882. switch (this->ChipID.Model)
  1883. {
  1884. case 1: this->ChipID.ProcessorName = "U5D"; break;
  1885. case 2: this->ChipID.ProcessorName = "U5S"; break;
  1886. default: this->ChipID.ProcessorName = "Unknown UMC family"; return false;
  1887. }
  1888. break;
  1889. default:
  1890. this->ChipID.ProcessorName = "Unknown UMC family";
  1891. return false;
  1892. }
  1893. break;
  1894. case IDT:
  1895. switch (this->ChipID.Family)
  1896. {
  1897. case 5:
  1898. switch (this->ChipID.Model)
  1899. {
  1900. case 4: this->ChipID.ProcessorName = "C6"; break;
  1901. case 8: this->ChipID.ProcessorName = "C2"; break;
  1902. case 9: this->ChipID.ProcessorName = "C3"; break;
  1903. default: this->ChipID.ProcessorName = "Unknown IDT\\Centaur family"; return false;
  1904. }
  1905. break;
  1906. case 6:
  1907. switch (this->ChipID.Model)
  1908. {
  1909. case 6: this->ChipID.ProcessorName = "VIA Cyrix III - Samuel"; break;
  1910. default: this->ChipID.ProcessorName = "Unknown IDT\\Centaur family"; return false;
  1911. }
  1912. break;
  1913. default:
  1914. this->ChipID.ProcessorName = "Unknown IDT\\Centaur family";
  1915. return false;
  1916. }
  1917. break;
  1918. case Cyrix:
  1919. switch (this->ChipID.Family)
  1920. {
  1921. case 4:
  1922. switch (this->ChipID.Model)
  1923. {
  1924. case 4: this->ChipID.ProcessorName = "MediaGX GX = GXm"; break;
  1925. case 9: this->ChipID.ProcessorName = "5x86"; break;
  1926. default: this->ChipID.ProcessorName = "Unknown Cx5x86 family"; return false;
  1927. }
  1928. break;
  1929. case 5:
  1930. switch (this->ChipID.Model)
  1931. {
  1932. case 2: this->ChipID.ProcessorName = "Cx6x86"; break;
  1933. case 4: this->ChipID.ProcessorName = "MediaGX GXm"; break;
  1934. default: this->ChipID.ProcessorName = "Unknown Cx6x86 family"; return false;
  1935. }
  1936. break;
  1937. case 6:
  1938. switch (this->ChipID.Model)
  1939. {
  1940. case 0: this->ChipID.ProcessorName = "6x86MX"; break;
  1941. case 5: this->ChipID.ProcessorName = "Cyrix M2 Core"; break;
  1942. case 6: this->ChipID.ProcessorName = "WinChip C5A Core"; break;
  1943. case 7: this->ChipID.ProcessorName = "WinChip C5B\\C5C Core"; break;
  1944. case 8: this->ChipID.ProcessorName = "WinChip C5C-T Core"; break;
  1945. default: this->ChipID.ProcessorName = "Unknown 6x86MX\\Cyrix III family"; return false;
  1946. }
  1947. break;
  1948. default:
  1949. this->ChipID.ProcessorName = "Unknown Cyrix family";
  1950. return false;
  1951. }
  1952. break;
  1953. case NexGen:
  1954. switch (this->ChipID.Family)
  1955. {
  1956. case 5:
  1957. switch (this->ChipID.Model)
  1958. {
  1959. case 0: this->ChipID.ProcessorName = "Nx586 or Nx586FPU"; break;
  1960. default: this->ChipID.ProcessorName = "Unknown NexGen family"; return false;
  1961. }
  1962. break;
  1963. default:
  1964. this->ChipID.ProcessorName = "Unknown NexGen family";
  1965. return false;
  1966. }
  1967. break;
  1968. case NSC:
  1969. this->ChipID.ProcessorName = "Cx486SLC \\ DLC \\ Cx486S A-Step";
  1970. break;
  1971. default:
  1972. this->ChipID.ProcessorName = "Unknown family"; // We cannot identify the processor.
  1973. return false;
  1974. }
  1975. return true;
  1976. }
  1977. /** Extract a value from the CPUInfo file */
  1978. kwsys_stl::string SystemInformationImplementation::ExtractValueFromCpuInfoFile(kwsys_stl::string buffer,const char* word,size_t init)
  1979. {
  1980. size_t pos = buffer.find(word,init);
  1981. if(pos != buffer.npos)
  1982. {
  1983. this->CurrentPositionInFile = pos;
  1984. pos = buffer.find(":",pos);
  1985. size_t pos2 = buffer.find("\n",pos);
  1986. if(pos!=buffer.npos && pos2!=buffer.npos)
  1987. {
  1988. return buffer.substr(pos+2,pos2-pos-2);
  1989. }
  1990. }
  1991. this->CurrentPositionInFile = buffer.npos;
  1992. return "";
  1993. }
  1994. /** Query for the cpu status */
  1995. int SystemInformationImplementation::RetreiveInformationFromCpuInfoFile()
  1996. {
  1997. this->NumberOfLogicalCPU = 0;
  1998. this->NumberOfPhysicalCPU = 0;
  1999. kwsys_stl::string buffer;
  2000. FILE *fd = fopen("/proc/cpuinfo", "r" );
  2001. if ( !fd )
  2002. {
  2003. kwsys_ios::cout << "Problem opening /proc/cpuinfo" << kwsys_ios::endl;
  2004. return 0;
  2005. }
  2006. size_t fileSize = 0;
  2007. while(!feof(fd))
  2008. {
  2009. buffer += static_cast<char>(fgetc(fd));
  2010. fileSize++;
  2011. }
  2012. fclose( fd );
  2013. buffer.resize(fileSize-2);
  2014. // Number of logical CPUs (combination of multiple processors, multi-core
  2015. // and hyperthreading)
  2016. size_t pos = buffer.find("processor\t");
  2017. while(pos != buffer.npos)
  2018. {
  2019. this->NumberOfLogicalCPU++;
  2020. pos = buffer.find("processor\t",pos+1);
  2021. }
  2022. #ifdef __linux
  2023. // Find the largest physical id.
  2024. int maxId = -1;
  2025. kwsys_stl::string idc =
  2026. this->ExtractValueFromCpuInfoFile(buffer,"physical id");
  2027. while(this->CurrentPositionInFile != buffer.npos)
  2028. {
  2029. int id = atoi(idc.c_str());
  2030. if(id > maxId)
  2031. {
  2032. maxId=id;
  2033. }
  2034. idc = this->ExtractValueFromCpuInfoFile(buffer,"physical id",
  2035. this->CurrentPositionInFile+1);
  2036. }
  2037. // Physical ids returned by Linux don't distinguish cores.
  2038. // We want to record the total number of cores in this->NumberOfPhysicalCPU
  2039. // (checking only the first proc)
  2040. kwsys_stl::string cores =
  2041. this->ExtractValueFromCpuInfoFile(buffer,"cpu cores");
  2042. int numberOfCoresPerCPU=atoi(cores.c_str());
  2043. this->NumberOfPhysicalCPU=static_cast<unsigned int>(
  2044. numberOfCoresPerCPU*(maxId+1));
  2045. #else // __CYGWIN__
  2046. // does not have "physical id" entries, neither "cpu cores"
  2047. // this has to be fixed for hyper-threading.
  2048. kwsys_stl::string cpucount =
  2049. this->ExtractValueFromCpuInfoFile(buffer,"cpu count");
  2050. this->NumberOfPhysicalCPU=
  2051. this->NumberOfLogicalCPU = atoi(cpucount.c_str());
  2052. #endif
  2053. // gotta have one, and if this is 0 then we get a / by 0n
  2054. // beter to have a bad answer than a crash
  2055. if(this->NumberOfPhysicalCPU <= 0)
  2056. {
  2057. this->NumberOfPhysicalCPU = 1;
  2058. }
  2059. // LogicalProcessorsPerPhysical>1 => hyperthreading.
  2060. this->Features.ExtendedFeatures.LogicalProcessorsPerPhysical=
  2061. this->NumberOfLogicalCPU/this->NumberOfPhysicalCPU;
  2062. // CPU speed (checking only the first proc
  2063. kwsys_stl::string CPUSpeed = this->ExtractValueFromCpuInfoFile(buffer,"cpu MHz");
  2064. this->CPUSpeedInMHz = static_cast<float>(atof(CPUSpeed.c_str()));
  2065. // Chip family
  2066. this->ChipID.Family = atoi(this->ExtractValueFromCpuInfoFile(buffer,"cpu family").c_str());
  2067. // Chip Vendor
  2068. this->ChipID.Vendor = this->ExtractValueFromCpuInfoFile(buffer,"vendor_id");
  2069. this->FindManufacturer();
  2070. // Chip Model
  2071. this->ChipID.Model = atoi(this->ExtractValueFromCpuInfoFile(buffer,"model").c_str());
  2072. this->RetrieveClassicalCPUIdentity();
  2073. // L1 Cache size
  2074. kwsys_stl::string cacheSize = this->ExtractValueFromCpuInfoFile(buffer,"cache size");
  2075. pos = cacheSize.find(" KB");
  2076. if(pos!=cacheSize.npos)
  2077. {
  2078. cacheSize = cacheSize.substr(0,pos);
  2079. }
  2080. this->Features.L1CacheSize = atoi(cacheSize.c_str());
  2081. return 1;
  2082. }
  2083. /** Query for the memory status */
  2084. int SystemInformationImplementation::QueryMemory()
  2085. {
  2086. this->TotalVirtualMemory = 0;
  2087. this->TotalPhysicalMemory = 0;
  2088. this->AvailableVirtualMemory = 0;
  2089. this->AvailablePhysicalMemory = 0;
  2090. #ifdef __CYGWIN__
  2091. return 0;
  2092. #elif defined(_WIN32)
  2093. #if _MSC_VER < 1300
  2094. MEMORYSTATUS ms;
  2095. unsigned long tv, tp, av, ap;
  2096. ms.dwLength = sizeof(ms);
  2097. GlobalMemoryStatus(&ms);
  2098. #define MEM_VAL(value) dw##value
  2099. #else
  2100. MEMORYSTATUSEX ms;
  2101. DWORDLONG tv, tp, av, ap;
  2102. ms.dwLength = sizeof(ms);
  2103. if (0 == GlobalMemoryStatusEx(&ms))
  2104. {
  2105. return 0;
  2106. }
  2107. #define MEM_VAL(value) ull##value
  2108. #endif
  2109. tv = ms.MEM_VAL(TotalVirtual);
  2110. tp = ms.MEM_VAL(TotalPhys);
  2111. av = ms.MEM_VAL(AvailVirtual);
  2112. ap = ms.MEM_VAL(AvailPhys);
  2113. this->TotalVirtualMemory = tv>>10>>10;
  2114. this->TotalPhysicalMemory = tp>>10>>10;
  2115. this->AvailableVirtualMemory = av>>10>>10;
  2116. this->AvailablePhysicalMemory = ap>>10>>10;
  2117. return 1;
  2118. #elif defined(__linux)
  2119. unsigned long tv=0;
  2120. unsigned long tp=0;
  2121. unsigned long av=0;
  2122. unsigned long ap=0;
  2123. char buffer[1024]; // for reading lines
  2124. int linuxMajor = 0;
  2125. int linuxMinor = 0;
  2126. // Find the Linux kernel version first
  2127. struct utsname unameInfo;
  2128. int errorFlag = uname(&unameInfo);
  2129. if( errorFlag!=0 )
  2130. {
  2131. kwsys_ios::cout << "Problem calling uname(): " << strerror(errno) << kwsys_ios::endl;
  2132. return 0;
  2133. }
  2134. if( unameInfo.release!=0 && strlen(unameInfo.release)>=3 )
  2135. {
  2136. // release looks like "2.6.3-15mdk-i686-up-4GB"
  2137. char majorChar=unameInfo.release[0];
  2138. char minorChar=unameInfo.release[2];
  2139. if( isdigit(majorChar) )
  2140. {
  2141. linuxMajor=majorChar-'0';
  2142. }
  2143. if( isdigit(minorChar) )
  2144. {
  2145. linuxMinor=minorChar-'0';
  2146. }
  2147. }
  2148. FILE *fd = fopen("/proc/meminfo", "r" );
  2149. if ( !fd )
  2150. {
  2151. kwsys_ios::cout << "Problem opening /proc/meminfo" << kwsys_ios::endl;
  2152. return 0;
  2153. }
  2154. if( linuxMajor>=3 || ( (linuxMajor>=2) && (linuxMinor>=6) ) )
  2155. {
  2156. // new /proc/meminfo format since kernel 2.6.x
  2157. // Rigorously, this test should check from the developping version 2.5.x
  2158. // that introduced the new format...
  2159. enum { mMemTotal, mMemFree, mBuffers, mCached, mSwapTotal, mSwapFree };
  2160. const char* format[6] =
  2161. { "MemTotal:%lu kB", "MemFree:%lu kB", "Buffers:%lu kB",
  2162. "Cached:%lu kB", "SwapTotal:%lu kB", "SwapFree:%lu kB" };
  2163. bool have[6] = { false, false, false, false, false, false };
  2164. unsigned long value[6];
  2165. int count = 0;
  2166. while(fgets(buffer, sizeof(buffer), fd))
  2167. {
  2168. for(int i=0; i < 6; ++i)
  2169. {
  2170. if(!have[i] && sscanf(buffer, format[i], &value[i]) == 1)
  2171. {
  2172. have[i] = true;
  2173. ++count;
  2174. }
  2175. }
  2176. }
  2177. if(count == 6)
  2178. {
  2179. this->TotalPhysicalMemory = value[mMemTotal] / 1024;
  2180. this->AvailablePhysicalMemory =
  2181. (value[mMemFree] + value[mBuffers] + value[mCached]) / 1024;
  2182. this->TotalVirtualMemory = value[mSwapTotal] / 1024;
  2183. this->AvailableVirtualMemory = value[mSwapFree] / 1024;
  2184. }
  2185. else
  2186. {
  2187. kwsys_ios::cout << "Problem parsing /proc/meminfo" << kwsys_ios::endl;
  2188. fclose(fd);
  2189. return 0;
  2190. }
  2191. }
  2192. else
  2193. {
  2194. // /proc/meminfo format for kernel older than 2.6.x
  2195. unsigned long temp;
  2196. unsigned long cachedMem;
  2197. unsigned long buffersMem;
  2198. char *r=fgets(buffer, sizeof(buffer), fd); // Skip "total: used:..."
  2199. int status=0;
  2200. if(r==buffer)
  2201. {
  2202. status+=fscanf(fd, "Mem: %lu %lu %lu %lu %lu %lu\n",
  2203. &tp, &temp, &ap, &temp, &buffersMem, &cachedMem);
  2204. }
  2205. if(status==6)
  2206. {
  2207. status+=fscanf(fd, "Swap: %lu %lu %lu\n", &tv, &temp, &av);
  2208. }
  2209. if(status==9)
  2210. {
  2211. this->TotalVirtualMemory = tv>>10>>10;
  2212. this->TotalPhysicalMemory = tp>>10>>10;
  2213. this->AvailableVirtualMemory = av>>10>>10;
  2214. this->AvailablePhysicalMemory = (ap+buffersMem+cachedMem)>>10>>10;
  2215. }
  2216. else
  2217. {
  2218. kwsys_ios::cout << "Problem parsing /proc/meminfo" << kwsys_ios::endl;
  2219. fclose(fd);
  2220. return 0;
  2221. }
  2222. }
  2223. fclose( fd );
  2224. return 1;
  2225. #elif defined(__hpux)
  2226. unsigned long tv=0;
  2227. unsigned long tp=0;
  2228. unsigned long av=0;
  2229. unsigned long ap=0;
  2230. struct pst_static pst;
  2231. struct pst_dynamic pdy;
  2232. unsigned long ps = 0;
  2233. if (pstat_getstatic(&pst, sizeof(pst), (size_t) 1, 0) != -1)
  2234. {
  2235. ps = pst.page_size;
  2236. tp = pst.physical_memory *ps;
  2237. tv = (pst.physical_memory + pst.pst_maxmem) * ps;
  2238. if (pstat_getdynamic(&pdy, sizeof(pdy), (size_t) 1, 0) != -1)
  2239. {
  2240. ap = tp - pdy.psd_rm * ps;
  2241. av = tv - pdy.psd_vm;
  2242. this->TotalVirtualMemory = tv>>10>>10;
  2243. this->TotalPhysicalMemory = tp>>10>>10;
  2244. this->AvailableVirtualMemory = av>>10>>10;
  2245. this->AvailablePhysicalMemory = ap>>10>>10;
  2246. return 1;
  2247. }
  2248. }
  2249. return 0;
  2250. #else
  2251. return 0;
  2252. #endif
  2253. }
  2254. /** */
  2255. size_t SystemInformationImplementation::GetTotalVirtualMemory()
  2256. {
  2257. return this->TotalVirtualMemory;
  2258. }
  2259. /** */
  2260. size_t SystemInformationImplementation::GetAvailableVirtualMemory()
  2261. {
  2262. return this->AvailableVirtualMemory;
  2263. }
  2264. size_t SystemInformationImplementation::GetTotalPhysicalMemory()
  2265. {
  2266. return this->TotalPhysicalMemory;
  2267. }
  2268. /** */
  2269. size_t SystemInformationImplementation::GetAvailablePhysicalMemory()
  2270. {
  2271. return this->AvailablePhysicalMemory;
  2272. }
  2273. /** Get Cycle differences */
  2274. LongLong SystemInformationImplementation::GetCyclesDifference (DELAY_FUNC DelayFunction,
  2275. unsigned int uiParameter)
  2276. {
  2277. #if USE_ASM_INSTRUCTIONS
  2278. unsigned int edx1, eax1;
  2279. unsigned int edx2, eax2;
  2280. // Calculate the frequency of the CPU instructions.
  2281. __try {
  2282. _asm {
  2283. push uiParameter ; push parameter param
  2284. mov ebx, DelayFunction ; store func in ebx
  2285. RDTSC_INSTRUCTION
  2286. mov esi, eax ; esi = eax
  2287. mov edi, edx ; edi = edx
  2288. call ebx ; call the delay functions
  2289. RDTSC_INSTRUCTION
  2290. pop ebx
  2291. mov edx2, edx ; edx2 = edx
  2292. mov eax2, eax ; eax2 = eax
  2293. mov edx1, edi ; edx2 = edi
  2294. mov eax1, esi ; eax2 = esi
  2295. }
  2296. }
  2297. __except(1)
  2298. {
  2299. return -1;
  2300. }
  2301. return ((((__int64) edx2 << 32) + eax2) - (((__int64) edx1 << 32) + eax1));
  2302. #else
  2303. (void)DelayFunction;
  2304. (void)uiParameter;
  2305. return -1;
  2306. #endif
  2307. }
  2308. /** Compute the delay overhead */
  2309. void SystemInformationImplementation::DelayOverhead(unsigned int uiMS)
  2310. {
  2311. #if defined(_WIN32)
  2312. LARGE_INTEGER Frequency, StartCounter, EndCounter;
  2313. __int64 x;
  2314. // Get the frequency of the high performance counter.
  2315. if(!QueryPerformanceFrequency (&Frequency))
  2316. {
  2317. return;
  2318. }
  2319. x = Frequency.QuadPart / 1000 * uiMS;
  2320. // Get the starting position of the counter.
  2321. QueryPerformanceCounter (&StartCounter);
  2322. do {
  2323. // Get the ending position of the counter.
  2324. QueryPerformanceCounter (&EndCounter);
  2325. } while (EndCounter.QuadPart - StartCounter.QuadPart == x);
  2326. #endif
  2327. (void)uiMS;
  2328. }
  2329. /** Return the number of logical CPU per physical CPUs Works only for windows */
  2330. unsigned char SystemInformationImplementation::LogicalCPUPerPhysicalCPU(void)
  2331. {
  2332. #ifdef __APPLE__
  2333. size_t len = 4;
  2334. int cores_per_package = 0;
  2335. int err = sysctlbyname("machdep.cpu.cores_per_package", &cores_per_package, &len, NULL, 0);
  2336. if (err != 0)
  2337. {
  2338. return 1; // That name was not found, default to 1
  2339. }
  2340. return static_cast<unsigned char>(cores_per_package);
  2341. #else
  2342. unsigned int Regebx = 0;
  2343. #if USE_ASM_INSTRUCTIONS
  2344. if (!this->IsHyperThreadingSupported())
  2345. {
  2346. return static_cast<unsigned char>(1); // HT not supported
  2347. }
  2348. __asm
  2349. {
  2350. mov eax, 1
  2351. cpuid
  2352. mov Regebx, ebx
  2353. }
  2354. #endif
  2355. return static_cast<unsigned char> ((Regebx & NUM_LOGICAL_BITS) >> 16);
  2356. #endif
  2357. }
  2358. /** Works only for windows */
  2359. unsigned int SystemInformationImplementation::IsHyperThreadingSupported()
  2360. {
  2361. #if USE_ASM_INSTRUCTIONS
  2362. unsigned int Regedx = 0,
  2363. Regeax = 0,
  2364. VendorId[3] = {0, 0, 0};
  2365. __try // Verify cpuid instruction is supported
  2366. {
  2367. __asm
  2368. {
  2369. xor eax, eax // call cpuid with eax = 0
  2370. cpuid // Get vendor id string
  2371. mov VendorId, ebx
  2372. mov VendorId + 4, edx
  2373. mov VendorId + 8, ecx
  2374. mov eax, 1 // call cpuid with eax = 1
  2375. cpuid
  2376. mov Regeax, eax // eax contains family processor type
  2377. mov Regedx, edx // edx has info about the availability of hyper-Threading
  2378. }
  2379. }
  2380. __except (EXCEPTION_EXECUTE_HANDLER)
  2381. {
  2382. return(0); // cpuid is unavailable
  2383. }
  2384. if (((Regeax & FAMILY_ID) == PENTIUM4_ID) || (Regeax & EXT_FAMILY_ID))
  2385. {
  2386. if (VendorId[0] == 'uneG')
  2387. {
  2388. if (VendorId[1] == 'Ieni')
  2389. {
  2390. if (VendorId[2] == 'letn')
  2391. {
  2392. return(Regedx & HT_BIT); // Genuine Intel with hyper-Threading technology
  2393. }
  2394. }
  2395. }
  2396. }
  2397. #endif
  2398. return 0; // Not genuine Intel processor
  2399. }
  2400. /** Return the APIC Id. Works only for windows. */
  2401. unsigned char SystemInformationImplementation::GetAPICId()
  2402. {
  2403. unsigned int Regebx = 0;
  2404. #if USE_ASM_INSTRUCTIONS
  2405. if (!this->IsHyperThreadingSupported())
  2406. {
  2407. return static_cast<unsigned char>(-1); // HT not supported
  2408. } // Logical processor = 1
  2409. __asm
  2410. {
  2411. mov eax, 1
  2412. cpuid
  2413. mov Regebx, ebx
  2414. }
  2415. #endif
  2416. return static_cast<unsigned char>((Regebx & INITIAL_APIC_ID_BITS) >> 24);
  2417. }
  2418. /** Count the number of CPUs. Works only on windows. */
  2419. int SystemInformationImplementation::CPUCount()
  2420. {
  2421. #if defined(_WIN32)
  2422. unsigned char StatusFlag = 0;
  2423. SYSTEM_INFO info;
  2424. this->NumberOfPhysicalCPU = 0;
  2425. this->NumberOfLogicalCPU = 0;
  2426. info.dwNumberOfProcessors = 0;
  2427. GetSystemInfo (&info);
  2428. // Number of physical processors in a non-Intel system
  2429. // or in a 32-bit Intel system with Hyper-Threading technology disabled
  2430. this->NumberOfPhysicalCPU = (unsigned char) info.dwNumberOfProcessors;
  2431. if (this->IsHyperThreadingSupported())
  2432. {
  2433. unsigned char HT_Enabled = 0;
  2434. this->NumberOfLogicalCPU = this->LogicalCPUPerPhysicalCPU();
  2435. if (this->NumberOfLogicalCPU >= 1) // >1 Doesn't mean HT is enabled in the BIOS
  2436. {
  2437. HANDLE hCurrentProcessHandle;
  2438. #ifndef _WIN64
  2439. # define DWORD_PTR DWORD
  2440. #endif
  2441. DWORD_PTR dwProcessAffinity;
  2442. DWORD_PTR dwSystemAffinity;
  2443. DWORD dwAffinityMask;
  2444. // Calculate the appropriate shifts and mask based on the
  2445. // number of logical processors.
  2446. unsigned int i = 1;
  2447. unsigned char PHY_ID_MASK = 0xFF;
  2448. //unsigned char PHY_ID_SHIFT = 0;
  2449. while (i < this->NumberOfLogicalCPU)
  2450. {
  2451. i *= 2;
  2452. PHY_ID_MASK <<= 1;
  2453. // PHY_ID_SHIFT++;
  2454. }
  2455. hCurrentProcessHandle = GetCurrentProcess();
  2456. GetProcessAffinityMask(hCurrentProcessHandle, &dwProcessAffinity,
  2457. &dwSystemAffinity);
  2458. // Check if available process affinity mask is equal to the
  2459. // available system affinity mask
  2460. if (dwProcessAffinity != dwSystemAffinity)
  2461. {
  2462. StatusFlag = HT_CANNOT_DETECT;
  2463. this->NumberOfPhysicalCPU = (unsigned char)-1;
  2464. return StatusFlag;
  2465. }
  2466. dwAffinityMask = 1;
  2467. while (dwAffinityMask != 0 && dwAffinityMask <= dwProcessAffinity)
  2468. {
  2469. // Check if this CPU is available
  2470. if (dwAffinityMask & dwProcessAffinity)
  2471. {
  2472. if (SetProcessAffinityMask(hCurrentProcessHandle,
  2473. dwAffinityMask))
  2474. {
  2475. unsigned char APIC_ID, LOG_ID;
  2476. Sleep(0); // Give OS time to switch CPU
  2477. APIC_ID = GetAPICId();
  2478. LOG_ID = APIC_ID & ~PHY_ID_MASK;
  2479. if (LOG_ID != 0)
  2480. {
  2481. HT_Enabled = 1;
  2482. }
  2483. }
  2484. }
  2485. dwAffinityMask = dwAffinityMask << 1;
  2486. }
  2487. // Reset the processor affinity
  2488. SetProcessAffinityMask(hCurrentProcessHandle, dwProcessAffinity);
  2489. if (this->NumberOfLogicalCPU == 1) // Normal P4 : HT is disabled in hardware
  2490. {
  2491. StatusFlag = HT_DISABLED;
  2492. }
  2493. else
  2494. {
  2495. if (HT_Enabled)
  2496. {
  2497. // Total physical processors in a Hyper-Threading enabled system.
  2498. this->NumberOfPhysicalCPU /= (this->NumberOfLogicalCPU);
  2499. StatusFlag = HT_ENABLED;
  2500. }
  2501. else
  2502. {
  2503. StatusFlag = HT_SUPPORTED_NOT_ENABLED;
  2504. }
  2505. }
  2506. }
  2507. }
  2508. else
  2509. {
  2510. // Processors do not have Hyper-Threading technology
  2511. StatusFlag = HT_NOT_CAPABLE;
  2512. this->NumberOfLogicalCPU = 1;
  2513. }
  2514. return StatusFlag;
  2515. #else
  2516. return 0;
  2517. #endif
  2518. }
  2519. /** Return the number of logical CPUs on the system */
  2520. unsigned int SystemInformationImplementation::GetNumberOfLogicalCPU()
  2521. {
  2522. return this->NumberOfLogicalCPU;
  2523. }
  2524. /** Return the number of physical CPUs on the system */
  2525. unsigned int SystemInformationImplementation::GetNumberOfPhysicalCPU()
  2526. {
  2527. return this->NumberOfPhysicalCPU;
  2528. }
  2529. /** For Mac use sysctlbyname calls to find system info */
  2530. bool SystemInformationImplementation::ParseSysCtl()
  2531. {
  2532. #if defined(__APPLE__)
  2533. int err = 0;
  2534. uint64_t value = 0;
  2535. size_t len = sizeof(value);
  2536. sysctlbyname("hw.memsize", &value, &len, NULL, 0);
  2537. this->TotalPhysicalMemory = static_cast< size_t >( value/1048576 );
  2538. // Parse values for Mac
  2539. this->AvailablePhysicalMemory = 0;
  2540. vm_statistics_data_t vmstat;
  2541. mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
  2542. if ( host_statistics(mach_host_self(), HOST_VM_INFO,
  2543. (host_info_t) &vmstat, &count) == KERN_SUCCESS )
  2544. {
  2545. err = sysctlbyname("hw.pagesize", &value, &len, NULL, 0);
  2546. int64_t available_memory = vmstat.free_count * value;
  2547. this->AvailablePhysicalMemory = static_cast< size_t >( available_memory / 1048576 );
  2548. }
  2549. #ifdef VM_SWAPUSAGE
  2550. // Virtual memory.
  2551. int mib[2] = { CTL_VM, VM_SWAPUSAGE };
  2552. size_t miblen = sizeof(mib) / sizeof(mib[0]);
  2553. struct xsw_usage swap;
  2554. len = sizeof(struct xsw_usage);
  2555. err = sysctl(mib, miblen, &swap, &len, NULL, 0);
  2556. if (err == 0)
  2557. {
  2558. this->AvailableVirtualMemory = static_cast< size_t >( swap.xsu_avail/1048576 );
  2559. this->TotalVirtualMemory = static_cast< size_t >( swap.xsu_total/1048576 );
  2560. }
  2561. #else
  2562. this->AvailableVirtualMemory = 0;
  2563. this->TotalVirtualMemory = 0;
  2564. #endif
  2565. // CPU Info
  2566. len = sizeof(this->NumberOfPhysicalCPU);
  2567. sysctlbyname("hw.physicalcpu", &this->NumberOfPhysicalCPU, &len, NULL, 0);
  2568. sysctlbyname("hw.logicalcpu", &this->NumberOfLogicalCPU, &len, NULL, 0);
  2569. this->Features.ExtendedFeatures.LogicalProcessorsPerPhysical =
  2570. this->LogicalCPUPerPhysicalCPU();
  2571. len = sizeof(value);
  2572. sysctlbyname("hw.cpufrequency", &value, &len, NULL, 0);
  2573. this->CPUSpeedInMHz = static_cast< float >( value )/ 1000000;
  2574. // Chip family
  2575. len = sizeof(this->ChipID.Family);
  2576. //Seems only the intel chips will have this name so if this fails it is
  2577. //probably a PPC machine
  2578. err = sysctlbyname("machdep.cpu.family",
  2579. &this->ChipID.Family, &len, NULL, 0);
  2580. if (err != 0) // Go back to names we know but are less descriptive
  2581. {
  2582. this->ChipID.Family = 0;
  2583. char retBuf[32];
  2584. ::memset(retBuf, 0, 32);
  2585. len = 32;
  2586. err = sysctlbyname("hw.machine", &retBuf, &len, NULL, 0);
  2587. kwsys_stl::string machineBuf(retBuf);
  2588. if (machineBuf.find_first_of("Power") != kwsys_stl::string::npos)
  2589. {
  2590. this->ChipID.Vendor = "IBM";
  2591. len = 4;
  2592. err = sysctlbyname("hw.cputype", &this->ChipID.Family, &len, NULL, 0);
  2593. err = sysctlbyname("hw.cpusubtype", &this->ChipID.Model, &len, NULL, 0);
  2594. this->FindManufacturer();
  2595. }
  2596. }
  2597. else // Should be an Intel Chip.
  2598. {
  2599. len = sizeof(this->ChipID.Family);
  2600. err =
  2601. sysctlbyname("machdep.cpu.family", &this->ChipID.Family, &len, NULL, 0);
  2602. char retBuf[128];
  2603. ::memset(retBuf, 0, 128);
  2604. len = 128;
  2605. err = sysctlbyname("machdep.cpu.vendor", retBuf, &len, NULL, 0);
  2606. // Chip Vendor
  2607. this->ChipID.Vendor = retBuf;
  2608. this->FindManufacturer();
  2609. ::memset(retBuf, 0, 128);
  2610. err =
  2611. sysctlbyname("machdep.cpu.brand_string",
  2612. retBuf, &len, NULL, 0);
  2613. this->ChipID.ProcessorName = retBuf;
  2614. // Chip Model
  2615. len = sizeof(value);
  2616. err = sysctlbyname("machdep.cpu.model", &value, &len, NULL, 0);
  2617. this->ChipID.Model = static_cast< int >( value );
  2618. }
  2619. // Cache size
  2620. len = sizeof(value);
  2621. err = sysctlbyname("hw.l1icachesize", &value, &len, NULL, 0);
  2622. this->Features.L1CacheSize = static_cast< int >( value );
  2623. err = sysctlbyname("hw.l2cachesize", &value, &len, NULL, 0);
  2624. this->Features.L2CacheSize = static_cast< int >( value );
  2625. return true;
  2626. #else
  2627. return false;
  2628. #endif
  2629. }
  2630. /** Extract a value from sysctl command */
  2631. kwsys_stl::string SystemInformationImplementation::ExtractValueFromSysCtl(const char* word)
  2632. {
  2633. size_t pos = this->SysCtlBuffer.find(word);
  2634. if(pos != this->SysCtlBuffer.npos)
  2635. {
  2636. pos = this->SysCtlBuffer.find(": ",pos);
  2637. size_t pos2 = this->SysCtlBuffer.find("\n",pos);
  2638. if(pos!=this->SysCtlBuffer.npos && pos2!=this->SysCtlBuffer.npos)
  2639. {
  2640. return this->SysCtlBuffer.substr(pos+2,pos2-pos-2);
  2641. }
  2642. }
  2643. return "";
  2644. }
  2645. /** Run a given process */
  2646. kwsys_stl::string SystemInformationImplementation::RunProcess(kwsys_stl::vector<const char*> args)
  2647. {
  2648. kwsys_stl::string buffer = "";
  2649. // Run the application
  2650. kwsysProcess* gp = kwsysProcess_New();
  2651. kwsysProcess_SetCommand(gp, &*args.begin());
  2652. kwsysProcess_SetOption(gp,kwsysProcess_Option_HideWindow,1);
  2653. kwsysProcess_Execute(gp);
  2654. char* data = NULL;
  2655. int length;
  2656. double timeout = 255;
  2657. while(kwsysProcess_WaitForData(gp,&data,&length,&timeout)) // wait for 1s
  2658. {
  2659. for(int i=0;i<length;i++)
  2660. {
  2661. buffer += data[i];
  2662. }
  2663. }
  2664. kwsysProcess_WaitForExit(gp, 0);
  2665. int result = 0;
  2666. switch(kwsysProcess_GetState(gp))
  2667. {
  2668. case kwsysProcess_State_Exited:
  2669. {
  2670. result = kwsysProcess_GetExitValue(gp);
  2671. } break;
  2672. case kwsysProcess_State_Error:
  2673. {
  2674. kwsys_ios::cerr << "Error: Could not run " << args[0] << ":\n";
  2675. kwsys_ios::cerr << kwsysProcess_GetErrorString(gp) << "\n";
  2676. } break;
  2677. case kwsysProcess_State_Exception:
  2678. {
  2679. kwsys_ios::cerr << "Error: " << args[0]
  2680. << " terminated with an exception: "
  2681. << kwsysProcess_GetExceptionString(gp) << "\n";
  2682. } break;
  2683. case kwsysProcess_State_Starting:
  2684. case kwsysProcess_State_Executing:
  2685. case kwsysProcess_State_Expired:
  2686. case kwsysProcess_State_Killed:
  2687. {
  2688. // Should not get here.
  2689. kwsys_ios::cerr << "Unexpected ending state after running " << args[0]
  2690. << kwsys_ios::endl;
  2691. } break;
  2692. }
  2693. kwsysProcess_Delete(gp);
  2694. if(result)
  2695. {
  2696. kwsys_ios::cerr << "Error " << args[0] << " returned :" << result << "\n";
  2697. }
  2698. return buffer;
  2699. }
  2700. kwsys_stl::string SystemInformationImplementation::ParseValueFromKStat(const char* arguments)
  2701. {
  2702. kwsys_stl::vector<const char*> args;
  2703. args.clear();
  2704. args.push_back("kstat");
  2705. args.push_back("-p");
  2706. kwsys_stl::string command = arguments;
  2707. size_t start = command.npos;
  2708. size_t pos = command.find(' ',0);
  2709. while(pos!=command.npos)
  2710. {
  2711. bool inQuotes = false;
  2712. // Check if we are between quotes
  2713. size_t b0 = command.find('"',0);
  2714. size_t b1 = command.find('"',b0+1);
  2715. while(b0 != command.npos && b1 != command.npos && b1>b0)
  2716. {
  2717. if(pos>b0 && pos<b1)
  2718. {
  2719. inQuotes = true;
  2720. break;
  2721. }
  2722. b0 = command.find('"',b1+1);
  2723. b1 = command.find('"',b0+1);
  2724. }
  2725. if(!inQuotes)
  2726. {
  2727. kwsys_stl::string arg = command.substr(start+1,pos-start-1);
  2728. // Remove the quotes if any
  2729. size_t quotes = arg.find('"');
  2730. while(quotes != arg.npos)
  2731. {
  2732. arg.erase(quotes,1);
  2733. quotes = arg.find('"');
  2734. }
  2735. args.push_back(arg.c_str());
  2736. start = pos;
  2737. }
  2738. pos = command.find(' ',pos+1);
  2739. }
  2740. kwsys_stl::string lastArg = command.substr(start+1,command.size()-start-1);
  2741. args.push_back(lastArg.c_str());
  2742. args.push_back(0);
  2743. kwsys_stl::string buffer = this->RunProcess(args);
  2744. kwsys_stl::string value = "";
  2745. for(size_t i=buffer.size()-1;i>0;i--)
  2746. {
  2747. if(buffer[i] == ' ' || buffer[i] == '\t')
  2748. {
  2749. break;
  2750. }
  2751. if(buffer[i] != '\n' && buffer[i] != '\r')
  2752. {
  2753. kwsys_stl::string val = value;
  2754. value = buffer[i];
  2755. value += val;
  2756. }
  2757. }
  2758. return value;
  2759. }
  2760. /** Querying for system information from Solaris */
  2761. bool SystemInformationImplementation::QuerySolarisInfo()
  2762. {
  2763. // Parse values
  2764. this->NumberOfPhysicalCPU = static_cast<unsigned int>(
  2765. atoi(this->ParseValueFromKStat("-n syste_misc -s ncpus").c_str()));
  2766. this->NumberOfLogicalCPU = this->NumberOfPhysicalCPU;
  2767. if(this->NumberOfPhysicalCPU!=0)
  2768. {
  2769. this->NumberOfLogicalCPU /= this->NumberOfPhysicalCPU;
  2770. }
  2771. this->CPUSpeedInMHz = static_cast<float>(atoi(this->ParseValueFromKStat("-s clock_MHz").c_str()));
  2772. // Chip family
  2773. this->ChipID.Family = 0;
  2774. // Chip Vendor
  2775. this->ChipID.Vendor = "Sun";
  2776. this->FindManufacturer();
  2777. // Chip Model
  2778. this->ChipID.ProcessorName = this->ParseValueFromKStat("-s cpu_type");
  2779. this->ChipID.Model = 0;
  2780. // Cache size
  2781. this->Features.L1CacheSize = 0;
  2782. this->Features.L2CacheSize = 0;
  2783. char* tail;
  2784. unsigned long totalMemory =
  2785. strtoul(this->ParseValueFromKStat("-s physmem").c_str(),&tail,0);
  2786. this->TotalPhysicalMemory = totalMemory/1024;
  2787. this->TotalPhysicalMemory *= 8192;
  2788. this->TotalPhysicalMemory /= 1024;
  2789. // Undefined values (for now at least)
  2790. this->TotalVirtualMemory = 0;
  2791. this->AvailablePhysicalMemory = 0;
  2792. this->AvailableVirtualMemory = 0;
  2793. return true;
  2794. }
  2795. /** Querying for system information from Haiku OS */
  2796. bool SystemInformationImplementation::QueryHaikuInfo()
  2797. {
  2798. #if defined(__HAIKU__)
  2799. system_info info;
  2800. get_system_info(&info);
  2801. this->NumberOfPhysicalCPU = info.cpu_count;
  2802. this->CPUSpeedInMHz = info.cpu_clock_speed / 1000000.0F;
  2803. // Physical Memory
  2804. this->TotalPhysicalMemory = (info.max_pages * B_PAGE_SIZE) / (1024 * 1024) ;
  2805. this->AvailablePhysicalMemory = this->TotalPhysicalMemory -
  2806. ((info.used_pages * B_PAGE_SIZE) / (1024 * 1024));
  2807. // NOTE: get_system_info_etc is currently a private call so just set to 0
  2808. // until it becomes public
  2809. this->TotalVirtualMemory = 0;
  2810. this->AvailableVirtualMemory = 0;
  2811. // Retrieve cpuid_info union for cpu 0
  2812. cpuid_info cpu_info;
  2813. get_cpuid(&cpu_info, 0, 0);
  2814. // Chip Vendor
  2815. // Use a temporary buffer so that we can add NULL termination to the string
  2816. char vbuf[13];
  2817. strncpy(vbuf, cpu_info.eax_0.vendor_id, 12);
  2818. vbuf[12] = '\0';
  2819. this->ChipID.Vendor = vbuf;
  2820. this->FindManufacturer();
  2821. // Retrieve cpuid_info union for cpu 0 this time using a register value of 1
  2822. get_cpuid(&cpu_info, 1, 0);
  2823. this->NumberOfLogicalCPU = cpu_info.eax_1.logical_cpus;
  2824. // Chip type
  2825. this->ChipID.Type = cpu_info.eax_1.type;
  2826. // Chip family
  2827. this->ChipID.Family = cpu_info.eax_1.family;
  2828. // Chip Model
  2829. this->ChipID.Model = cpu_info.eax_1.model;
  2830. // Chip Revision
  2831. this->ChipID.Revision = cpu_info.eax_1.stepping;
  2832. // Chip Extended Family
  2833. this->ChipID.ExtendedFamily = cpu_info.eax_1.extended_family;
  2834. // Chip Extended Model
  2835. this->ChipID.ExtendedModel = cpu_info.eax_1.extended_model;
  2836. // Get ChipID.ProcessorName from other information already gathered
  2837. this->RetrieveClassicalCPUIdentity();
  2838. // Cache size
  2839. this->Features.L1CacheSize = 0;
  2840. this->Features.L2CacheSize = 0;
  2841. return true;
  2842. #else
  2843. return false;
  2844. #endif
  2845. }
  2846. bool SystemInformationImplementation::QueryQNXMemory()
  2847. {
  2848. #if defined(__QNX__)
  2849. kwsys_stl::string buffer;
  2850. kwsys_stl::vector<const char*> args;
  2851. args.clear();
  2852. args.push_back("showmem");
  2853. args.push_back("-S");
  2854. args.push_back(0);
  2855. buffer = this->RunProcess(args);
  2856. args.clear();
  2857. size_t pos = buffer.find("System RAM:");
  2858. if (pos == buffer.npos)
  2859. return false;
  2860. pos = buffer.find(":", pos);
  2861. size_t pos2 = buffer.find("M (", pos);
  2862. if (pos2 == buffer.npos)
  2863. return false;
  2864. pos++;
  2865. while (buffer[pos] == ' ')
  2866. pos++;
  2867. this->TotalPhysicalMemory = atoi(buffer.substr(pos, pos2 - pos).c_str());
  2868. return true;
  2869. #endif
  2870. return false;
  2871. }
  2872. bool SystemInformationImplementation::QueryQNXProcessor()
  2873. {
  2874. #if defined(__QNX__)
  2875. // the output on my QNX 6.4.1 looks like this:
  2876. // Processor1: 686 Pentium II Stepping 3 2175MHz FPU
  2877. kwsys_stl::string buffer;
  2878. kwsys_stl::vector<const char*> args;
  2879. args.clear();
  2880. args.push_back("pidin");
  2881. args.push_back("info");
  2882. args.push_back(0);
  2883. buffer = this->RunProcess(args);
  2884. args.clear();
  2885. size_t pos = buffer.find("Processor1:");
  2886. if (pos == buffer.npos)
  2887. return false;
  2888. size_t pos2 = buffer.find("MHz", pos);
  2889. if (pos2 == buffer.npos)
  2890. return false;
  2891. size_t pos3 = pos2;
  2892. while (buffer[pos3] != ' ')
  2893. --pos3;
  2894. this->CPUSpeedInMHz = atoi(buffer.substr(pos3 + 1, pos2 - pos3 - 1).c_str());
  2895. pos2 = buffer.find(" Stepping", pos);
  2896. if (pos2 != buffer.npos)
  2897. {
  2898. pos2 = buffer.find(" ", pos2 + 1);
  2899. if (pos2 != buffer.npos && pos2 < pos3)
  2900. {
  2901. this->ChipID.Revision = atoi(buffer.substr(pos2 + 1, pos3 - pos2).c_str());
  2902. }
  2903. }
  2904. this->NumberOfPhysicalCPU = 0;
  2905. do
  2906. {
  2907. pos = buffer.find("\nProcessor", pos + 1);
  2908. ++this->NumberOfPhysicalCPU;
  2909. } while (pos != buffer.npos);
  2910. this->NumberOfLogicalCPU = 1;
  2911. return true;
  2912. #else
  2913. return false;
  2914. #endif
  2915. }
  2916. /** Query the operating system information */
  2917. bool SystemInformationImplementation::QueryOSInformation()
  2918. {
  2919. #if defined(_WIN32)
  2920. this->OSName = "Windows";
  2921. OSVERSIONINFOEX osvi;
  2922. BOOL bIsWindows64Bit;
  2923. BOOL bOsVersionInfoEx;
  2924. char operatingSystem[256];
  2925. // Try calling GetVersionEx using the OSVERSIONINFOEX structure.
  2926. ZeroMemory (&osvi, sizeof (OSVERSIONINFOEX));
  2927. osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEX);
  2928. bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi);
  2929. if (!bOsVersionInfoEx)
  2930. {
  2931. osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
  2932. if (!GetVersionEx ((OSVERSIONINFO *) &osvi))
  2933. {
  2934. return false;
  2935. }
  2936. }
  2937. switch (osvi.dwPlatformId)
  2938. {
  2939. case VER_PLATFORM_WIN32_NT:
  2940. // Test for the product.
  2941. if (osvi.dwMajorVersion <= 4)
  2942. {
  2943. this->OSRelease = "NT";
  2944. }
  2945. if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0)
  2946. {
  2947. this->OSRelease = "2000";
  2948. }
  2949. if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
  2950. {
  2951. this->OSRelease = "XP";
  2952. }
  2953. // XP Professional x64
  2954. if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2)
  2955. {
  2956. this->OSRelease = "XP";
  2957. }
  2958. #ifdef VER_NT_WORKSTATION
  2959. // Test for product type.
  2960. if (bOsVersionInfoEx)
  2961. {
  2962. if (osvi.wProductType == VER_NT_WORKSTATION)
  2963. {
  2964. if (osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 0)
  2965. {
  2966. this->OSRelease = "Vista";
  2967. }
  2968. if (osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 1)
  2969. {
  2970. this->OSRelease = "7";
  2971. }
  2972. // VER_SUITE_PERSONAL may not be defined
  2973. #ifdef VER_SUITE_PERSONAL
  2974. else
  2975. {
  2976. if (osvi.wSuiteMask & VER_SUITE_PERSONAL)
  2977. {
  2978. this->OSRelease += " Personal";
  2979. }
  2980. else
  2981. {
  2982. this->OSRelease += " Professional";
  2983. }
  2984. }
  2985. #endif
  2986. }
  2987. else if (osvi.wProductType == VER_NT_SERVER)
  2988. {
  2989. // Check for .NET Server instead of Windows XP.
  2990. if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
  2991. {
  2992. this->OSRelease = ".NET";
  2993. }
  2994. // Continue with the type detection.
  2995. if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
  2996. {
  2997. this->OSRelease += " DataCenter Server";
  2998. }
  2999. else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
  3000. {
  3001. this->OSRelease += " Advanced Server";
  3002. }
  3003. else
  3004. {
  3005. this->OSRelease += " Server";
  3006. }
  3007. }
  3008. sprintf (operatingSystem, "%s (Build %ld)", osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF);
  3009. this->OSVersion = operatingSystem;
  3010. }
  3011. else
  3012. #endif // VER_NT_WORKSTATION
  3013. {
  3014. HKEY hKey;
  3015. char szProductType[80];
  3016. DWORD dwBufLen;
  3017. // Query the registry to retrieve information.
  3018. RegOpenKeyEx (HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\ProductOptions", 0, KEY_QUERY_VALUE, &hKey);
  3019. RegQueryValueEx (hKey, "ProductType", NULL, NULL, (LPBYTE) szProductType, &dwBufLen);
  3020. RegCloseKey (hKey);
  3021. if (lstrcmpi ("WINNT", szProductType) == 0)
  3022. {
  3023. this->OSRelease += " Professional";
  3024. }
  3025. if (lstrcmpi ("LANMANNT", szProductType) == 0)
  3026. {
  3027. // Decide between Windows 2000 Advanced Server and Windows .NET Enterprise Server.
  3028. if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
  3029. {
  3030. this->OSRelease += " Standard Server";
  3031. }
  3032. else
  3033. {
  3034. this->OSRelease += " Server";
  3035. }
  3036. }
  3037. if (lstrcmpi ("SERVERNT", szProductType) == 0)
  3038. {
  3039. // Decide between Windows 2000 Advanced Server and Windows .NET Enterprise Server.
  3040. if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
  3041. {
  3042. this->OSRelease += " Enterprise Server";
  3043. }
  3044. else
  3045. {
  3046. this->OSRelease += " Advanced Server";
  3047. }
  3048. }
  3049. }
  3050. // Display version, service pack (if any), and build number.
  3051. if (osvi.dwMajorVersion <= 4)
  3052. {
  3053. // NB: NT 4.0 and earlier.
  3054. sprintf (operatingSystem, "version %ld.%ld %s (Build %ld)",
  3055. osvi.dwMajorVersion,
  3056. osvi.dwMinorVersion,
  3057. osvi.szCSDVersion,
  3058. osvi.dwBuildNumber & 0xFFFF);
  3059. this->OSVersion = operatingSystem;
  3060. }
  3061. else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
  3062. {
  3063. // Windows XP and .NET server.
  3064. typedef BOOL (CALLBACK* LPFNPROC) (HANDLE, BOOL *);
  3065. HINSTANCE hKernelDLL;
  3066. LPFNPROC DLLProc;
  3067. // Load the Kernel32 DLL.
  3068. hKernelDLL = LoadLibrary ("kernel32");
  3069. if (hKernelDLL != NULL) {
  3070. // Only XP and .NET Server support IsWOW64Process so... Load dynamically!
  3071. DLLProc = (LPFNPROC) GetProcAddress (hKernelDLL, "IsWow64Process");
  3072. // If the function address is valid, call the function.
  3073. if (DLLProc != NULL) (DLLProc) (GetCurrentProcess (), &bIsWindows64Bit);
  3074. else bIsWindows64Bit = false;
  3075. // Free the DLL module.
  3076. FreeLibrary (hKernelDLL);
  3077. }
  3078. }
  3079. else
  3080. {
  3081. // Windows 2000 and everything else.
  3082. sprintf (operatingSystem,"%s (Build %ld)", osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF);
  3083. this->OSVersion = operatingSystem;
  3084. }
  3085. break;
  3086. case VER_PLATFORM_WIN32_WINDOWS:
  3087. // Test for the product.
  3088. if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
  3089. {
  3090. this->OSRelease = "95";
  3091. if(osvi.szCSDVersion[1] == 'C')
  3092. {
  3093. this->OSRelease += "OSR 2.5";
  3094. }
  3095. else if(osvi.szCSDVersion[1] == 'B')
  3096. {
  3097. this->OSRelease += "OSR 2";
  3098. }
  3099. }
  3100. if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)
  3101. {
  3102. this->OSRelease = "98";
  3103. if (osvi.szCSDVersion[1] == 'A' )
  3104. {
  3105. this->OSRelease += "SE";
  3106. }
  3107. }
  3108. if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
  3109. {
  3110. this->OSRelease = "Me";
  3111. }
  3112. break;
  3113. case VER_PLATFORM_WIN32s:
  3114. this->OSRelease = "Win32s";
  3115. break;
  3116. default:
  3117. this->OSRelease = "Unknown";
  3118. break;
  3119. }
  3120. // Get the hostname
  3121. WORD wVersionRequested;
  3122. WSADATA wsaData;
  3123. char name[255];
  3124. wVersionRequested = MAKEWORD(2,0);
  3125. if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
  3126. {
  3127. gethostname(name,sizeof(name));
  3128. WSACleanup( );
  3129. }
  3130. this->Hostname = name;
  3131. const char* arch = getenv("PROCESSOR_ARCHITECTURE");
  3132. if(arch)
  3133. {
  3134. this->OSPlatform = arch;
  3135. }
  3136. #else
  3137. struct utsname unameInfo;
  3138. int errorFlag = uname(&unameInfo);
  3139. if(errorFlag == 0)
  3140. {
  3141. this->OSName = unameInfo.sysname;
  3142. this->Hostname = unameInfo.nodename;
  3143. this->OSRelease = unameInfo.release;
  3144. this->OSVersion = unameInfo.version;
  3145. this->OSPlatform = unameInfo.machine;
  3146. }
  3147. #ifdef __APPLE__
  3148. this->CallSwVers();
  3149. #endif
  3150. #endif
  3151. return true;
  3152. }
  3153. void SystemInformationImplementation::CallSwVers()
  3154. {
  3155. #ifdef __APPLE__
  3156. kwsys_stl::string output;
  3157. kwsys_stl::vector<const char*> args;
  3158. args.clear();
  3159. args.push_back("sw_vers");
  3160. args.push_back("-productName");
  3161. args.push_back(0);
  3162. output = this->RunProcess(args);
  3163. this->TrimNewline(output);
  3164. this->OSName = output;
  3165. args.clear();
  3166. args.push_back("sw_vers");
  3167. args.push_back("-productVersion");
  3168. args.push_back(0);
  3169. output = this->RunProcess(args);
  3170. this->TrimNewline(output);
  3171. this->OSRelease = output;
  3172. args.clear();
  3173. args.push_back("sw_vers");
  3174. args.push_back("-buildVersion");
  3175. args.push_back(0);
  3176. output = this->RunProcess(args);
  3177. this->TrimNewline(output);
  3178. this->OSVersion = output;
  3179. #endif
  3180. }
  3181. void SystemInformationImplementation::TrimNewline(kwsys_stl::string& output)
  3182. {
  3183. // remove \r
  3184. kwsys_stl::string::size_type pos=0;
  3185. while((pos = output.find("\r", pos)) != kwsys_stl::string::npos)
  3186. {
  3187. output.erase(pos);
  3188. }
  3189. // remove \n
  3190. pos = 0;
  3191. while((pos = output.find("\n", pos)) != kwsys_stl::string::npos)
  3192. {
  3193. output.erase(pos);
  3194. }
  3195. }
  3196. /** Return true if the machine is 64 bits */
  3197. bool SystemInformationImplementation::Is64Bits()
  3198. {
  3199. return (sizeof(void*) == 8);
  3200. }
  3201. } // namespace @KWSYS_NAMESPACE@