SystemInformation.cxx 104 KB

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