SystemInformation.cxx 105 KB

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