SystemInformation.cxx 100 KB

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