SystemInformation.cxx 106 KB

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