SystemInformation.cxx 82 KB

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