SystemInformation.cxx 95 KB

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