SystemInformation.cxx 102 KB

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