SystemInformation.cxx 105 KB

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