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