SystemInformation.cxx 96 KB

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