SystemInformation.cxx 82 KB

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