SystemInformation.cxx 104 KB

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