SystemInformation.cxx 97 KB

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