SystemInformation.cxx 97 KB

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