SystemInformation.cxx 104 KB

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