SystemInformation.cxx 95 KB

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