SystemInformation.cxx 103 KB

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