SystemInformation.cxx 148 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing#kwsys for details. */
  3. #if defined(_WIN32)
  4. #define NOMINMAX // use our min,max
  5. #if !defined(_WIN32_WINNT) && !(defined(_MSC_VER) && _MSC_VER < 1300)
  6. #define _WIN32_WINNT 0x0501
  7. #endif
  8. #include <winsock.h> // WSADATA, include before sys/types.h
  9. #endif
  10. #if (defined(__GNUC__) || defined(__PGI)) && !defined(_GNU_SOURCE)
  11. #define _GNU_SOURCE
  12. #endif
  13. // TODO:
  14. // We need an alternative implementation for many functions in this file
  15. // when USE_ASM_INSTRUCTIONS gets defined as 0.
  16. //
  17. // Consider using these on Win32/Win64 for some of them:
  18. //
  19. // IsProcessorFeaturePresent
  20. // http://msdn.microsoft.com/en-us/library/ms724482(VS.85).aspx
  21. //
  22. // GetProcessMemoryInfo
  23. // http://msdn.microsoft.com/en-us/library/ms683219(VS.85).aspx
  24. #include "kwsysPrivate.h"
  25. #include KWSYS_HEADER(SystemInformation.hxx)
  26. #include KWSYS_HEADER(Process.h)
  27. // Work-around CMake dependency scanning limitation. This must
  28. // duplicate the above list of headers.
  29. #if 0
  30. #include "Process.h.in"
  31. #include "SystemInformation.hxx.in"
  32. #endif
  33. #include <algorithm>
  34. #include <bitset>
  35. #include <cassert>
  36. #include <fstream>
  37. #include <iostream>
  38. #include <limits>
  39. #include <set>
  40. #include <sstream>
  41. #include <string>
  42. #include <vector>
  43. #if defined(_WIN32)
  44. #include <windows.h>
  45. #if defined(_MSC_VER) && _MSC_VER >= 1800
  46. #define KWSYS_WINDOWS_DEPRECATED_GetVersionEx
  47. #endif
  48. #include <errno.h>
  49. #if defined(KWSYS_SYS_HAS_PSAPI)
  50. #include <psapi.h>
  51. #endif
  52. #if !defined(siginfo_t)
  53. typedef int siginfo_t;
  54. #endif
  55. #else
  56. #include <sys/types.h>
  57. #include <errno.h> // extern int errno;
  58. #include <fcntl.h>
  59. #include <signal.h>
  60. #include <sys/resource.h> // getrlimit
  61. #include <sys/time.h>
  62. #include <sys/utsname.h> // int uname(struct utsname *buf);
  63. #include <unistd.h>
  64. #endif
  65. #if defined(__CYGWIN__) && !defined(_WIN32)
  66. #include <windows.h>
  67. #undef _WIN32
  68. #endif
  69. #if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
  70. defined(__DragonFly__)
  71. #include <netdb.h>
  72. #include <netinet/in.h>
  73. #include <sys/param.h>
  74. #include <sys/socket.h>
  75. #include <sys/sysctl.h>
  76. #if defined(KWSYS_SYS_HAS_IFADDRS_H)
  77. #include <ifaddrs.h>
  78. #include <net/if.h>
  79. #define KWSYS_SYSTEMINFORMATION_IMPLEMENT_FQDN
  80. #endif
  81. #endif
  82. #if defined(KWSYS_SYS_HAS_MACHINE_CPU_H)
  83. #include <machine/cpu.h>
  84. #endif
  85. #ifdef __APPLE__
  86. #include <mach/host_info.h>
  87. #include <mach/mach.h>
  88. #include <mach/mach_types.h>
  89. #include <mach/vm_statistics.h>
  90. #include <netdb.h>
  91. #include <netinet/in.h>
  92. #include <sys/socket.h>
  93. #include <sys/sysctl.h>
  94. #if defined(KWSYS_SYS_HAS_IFADDRS_H)
  95. #include <ifaddrs.h>
  96. #include <net/if.h>
  97. #define KWSYS_SYSTEMINFORMATION_IMPLEMENT_FQDN
  98. #endif
  99. #if !(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ - 0 >= 1050)
  100. #undef KWSYS_SYSTEMINFORMATION_HAS_BACKTRACE
  101. #endif
  102. #endif
  103. #if defined(__linux) || defined(__sun) || defined(_SCO_DS)
  104. #include <netdb.h>
  105. #include <netinet/in.h>
  106. #include <sys/socket.h>
  107. #if defined(KWSYS_SYS_HAS_IFADDRS_H)
  108. #include <ifaddrs.h>
  109. #include <net/if.h>
  110. #if defined(__LSB_VERSION__)
  111. /* LSB has no getifaddrs */
  112. #elif defined(__ANDROID_API__) && __ANDROID_API__ < 24
  113. /* Android has no getifaddrs prior to API 24. */
  114. #else
  115. #define KWSYS_SYSTEMINFORMATION_IMPLEMENT_FQDN
  116. #endif
  117. #endif
  118. #if defined(KWSYS_CXX_HAS_RLIMIT64)
  119. typedef struct rlimit64 ResourceLimitType;
  120. #define GetResourceLimit getrlimit64
  121. #else
  122. typedef struct rlimit ResourceLimitType;
  123. #define GetResourceLimit getrlimit
  124. #endif
  125. #elif defined(__hpux)
  126. #include <sys/param.h>
  127. #include <sys/pstat.h>
  128. #if defined(KWSYS_SYS_HAS_MPCTL_H)
  129. #include <sys/mpctl.h>
  130. #endif
  131. #endif
  132. #ifdef __HAIKU__
  133. #include <OS.h>
  134. #endif
  135. #if defined(KWSYS_SYSTEMINFORMATION_HAS_BACKTRACE)
  136. #include <execinfo.h>
  137. #if defined(KWSYS_SYSTEMINFORMATION_HAS_CPP_DEMANGLE)
  138. #include <cxxabi.h>
  139. #endif
  140. #if defined(KWSYS_SYSTEMINFORMATION_HAS_SYMBOL_LOOKUP)
  141. #include <dlfcn.h>
  142. #endif
  143. #else
  144. #undef KWSYS_SYSTEMINFORMATION_HAS_CPP_DEMANGLE
  145. #undef KWSYS_SYSTEMINFORMATION_HAS_SYMBOL_LOOKUP
  146. #endif
  147. #include <ctype.h> // int isdigit(int c);
  148. #include <memory.h>
  149. #include <stdio.h>
  150. #include <stdlib.h>
  151. #include <string.h>
  152. #if defined(KWSYS_USE_LONG_LONG)
  153. #if defined(KWSYS_IOS_HAS_OSTREAM_LONG_LONG)
  154. #define iostreamLongLong(x) (x)
  155. #else
  156. #define iostreamLongLong(x) ((long)(x))
  157. #endif
  158. #elif defined(KWSYS_USE___INT64)
  159. #if defined(KWSYS_IOS_HAS_OSTREAM___INT64)
  160. #define iostreamLongLong(x) (x)
  161. #else
  162. #define iostreamLongLong(x) ((long)(x))
  163. #endif
  164. #else
  165. #error "No Long Long"
  166. #endif
  167. #if defined(KWSYS_CXX_HAS_ATOLL)
  168. #define atoLongLong atoll
  169. #else
  170. #if defined(KWSYS_CXX_HAS__ATOI64)
  171. #define atoLongLong _atoi64
  172. #elif defined(KWSYS_CXX_HAS_ATOL)
  173. #define atoLongLong atol
  174. #else
  175. #define atoLongLong atoi
  176. #endif
  177. #endif
  178. #if defined(_MSC_VER) && (_MSC_VER >= 1300) && !defined(_WIN64) && \
  179. !defined(__clang__)
  180. #define USE_ASM_INSTRUCTIONS 1
  181. #else
  182. #define USE_ASM_INSTRUCTIONS 0
  183. #endif
  184. #if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(__clang__)
  185. #include <intrin.h>
  186. #define USE_CPUID_INTRINSICS 1
  187. #else
  188. #define USE_CPUID_INTRINSICS 0
  189. #endif
  190. #if USE_ASM_INSTRUCTIONS || USE_CPUID_INTRINSICS || \
  191. defined(KWSYS_CXX_HAS_BORLAND_ASM_CPUID)
  192. #define USE_CPUID 1
  193. #else
  194. #define USE_CPUID 0
  195. #endif
  196. #if USE_CPUID
  197. #define CPUID_AWARE_COMPILER
  198. /**
  199. * call CPUID instruction
  200. *
  201. * Will return false if the instruction failed.
  202. */
  203. static bool call_cpuid(int select, int result[4])
  204. {
  205. #if USE_CPUID_INTRINSICS
  206. __cpuid(result, select);
  207. return true;
  208. #else
  209. int tmp[4];
  210. #if defined(_MSC_VER)
  211. // Use SEH to determine CPUID presence
  212. __try {
  213. _asm {
  214. #ifdef CPUID_AWARE_COMPILER
  215. ; we must push/pop the registers <<CPUID>> writes to, as the
  216. ; optimiser does not know about <<CPUID>>, and so does not expect
  217. ; these registers to change.
  218. push eax
  219. push ebx
  220. push ecx
  221. push edx
  222. #endif
  223. ; <<CPUID>>
  224. mov eax, select
  225. #ifdef CPUID_AWARE_COMPILER
  226. cpuid
  227. #else
  228. _asm _emit 0x0f
  229. _asm _emit 0xa2
  230. #endif
  231. mov tmp[0 * TYPE int], eax
  232. mov tmp[1 * TYPE int], ebx
  233. mov tmp[2 * TYPE int], ecx
  234. mov tmp[3 * TYPE int], edx
  235. #ifdef CPUID_AWARE_COMPILER
  236. pop edx
  237. pop ecx
  238. pop ebx
  239. pop eax
  240. #endif
  241. }
  242. } __except (1) {
  243. return false;
  244. }
  245. memcpy(result, tmp, sizeof(tmp));
  246. #elif defined(KWSYS_CXX_HAS_BORLAND_ASM_CPUID)
  247. unsigned int a, b, c, d;
  248. __asm {
  249. mov EAX, select;
  250. cpuid
  251. mov a, EAX;
  252. mov b, EBX;
  253. mov c, ECX;
  254. mov d, EDX;
  255. }
  256. result[0] = a;
  257. result[1] = b;
  258. result[2] = c;
  259. result[3] = d;
  260. #endif
  261. // The cpuid instruction succeeded.
  262. return true;
  263. #endif
  264. }
  265. #endif
  266. namespace KWSYS_NAMESPACE {
  267. template <typename T>
  268. T min(T a, T b)
  269. {
  270. return a < b ? a : b;
  271. }
  272. extern "C" {
  273. typedef void (*SigAction)(int, siginfo_t*, void*);
  274. }
  275. // Define SystemInformationImplementation class
  276. typedef void (*DELAY_FUNC)(unsigned int uiMS);
  277. class SystemInformationImplementation
  278. {
  279. public:
  280. typedef SystemInformation::LongLong LongLong;
  281. SystemInformationImplementation();
  282. ~SystemInformationImplementation();
  283. const char* GetVendorString();
  284. const char* GetVendorID();
  285. std::string GetTypeID();
  286. std::string GetFamilyID();
  287. std::string GetModelID();
  288. std::string GetModelName();
  289. std::string GetSteppingCode();
  290. const char* GetExtendedProcessorName();
  291. const char* GetProcessorSerialNumber();
  292. int GetProcessorCacheSize();
  293. unsigned int GetLogicalProcessorsPerPhysical();
  294. float GetProcessorClockFrequency();
  295. int GetProcessorAPICID();
  296. int GetProcessorCacheXSize(long int);
  297. bool DoesCPUSupportFeature(long int);
  298. const char* GetOSName();
  299. const char* GetHostname();
  300. int GetFullyQualifiedDomainName(std::string& fqdn);
  301. const char* GetOSRelease();
  302. const char* GetOSVersion();
  303. const char* GetOSPlatform();
  304. bool Is64Bits();
  305. unsigned int GetNumberOfLogicalCPU(); // per physical cpu
  306. unsigned int GetNumberOfPhysicalCPU();
  307. bool DoesCPUSupportCPUID();
  308. // Retrieve memory information in MiB.
  309. size_t GetTotalVirtualMemory();
  310. size_t GetAvailableVirtualMemory();
  311. size_t GetTotalPhysicalMemory();
  312. size_t GetAvailablePhysicalMemory();
  313. LongLong GetProcessId();
  314. // Retrieve memory information in KiB.
  315. LongLong GetHostMemoryTotal();
  316. LongLong GetHostMemoryAvailable(const char* envVarName);
  317. LongLong GetHostMemoryUsed();
  318. LongLong GetProcMemoryAvailable(const char* hostLimitEnvVarName,
  319. const char* procLimitEnvVarName);
  320. LongLong GetProcMemoryUsed();
  321. double GetLoadAverage();
  322. // enable/disable stack trace signal handler.
  323. static void SetStackTraceOnError(int enable);
  324. // get current stack
  325. static std::string GetProgramStack(int firstFrame, int wholePath);
  326. /** Run the different checks */
  327. void RunCPUCheck();
  328. void RunOSCheck();
  329. void RunMemoryCheck();
  330. public:
  331. typedef struct tagID
  332. {
  333. int Type;
  334. int Family;
  335. int Model;
  336. int Revision;
  337. int ExtendedFamily;
  338. int ExtendedModel;
  339. std::string ProcessorName;
  340. std::string Vendor;
  341. std::string SerialNumber;
  342. std::string ModelName;
  343. } ID;
  344. typedef struct tagCPUPowerManagement
  345. {
  346. bool HasVoltageID;
  347. bool HasFrequencyID;
  348. bool HasTempSenseDiode;
  349. } CPUPowerManagement;
  350. typedef struct tagCPUExtendedFeatures
  351. {
  352. bool Has3DNow;
  353. bool Has3DNowPlus;
  354. bool SupportsMP;
  355. bool HasMMXPlus;
  356. bool HasSSEMMX;
  357. unsigned int LogicalProcessorsPerPhysical;
  358. int APIC_ID;
  359. CPUPowerManagement PowerManagement;
  360. } CPUExtendedFeatures;
  361. typedef struct CPUtagFeatures
  362. {
  363. bool HasFPU;
  364. bool HasTSC;
  365. bool HasMMX;
  366. bool HasSSE;
  367. bool HasSSEFP;
  368. bool HasSSE2;
  369. bool HasIA64;
  370. bool HasAPIC;
  371. bool HasCMOV;
  372. bool HasMTRR;
  373. bool HasACPI;
  374. bool HasSerial;
  375. bool HasThermal;
  376. int CPUSpeed;
  377. int L1CacheSize;
  378. int L2CacheSize;
  379. int L3CacheSize;
  380. CPUExtendedFeatures ExtendedFeatures;
  381. } CPUFeatures;
  382. enum Manufacturer
  383. {
  384. AMD,
  385. Intel,
  386. NSC,
  387. UMC,
  388. Cyrix,
  389. NexGen,
  390. IDT,
  391. Rise,
  392. Transmeta,
  393. Sun,
  394. IBM,
  395. Motorola,
  396. HP,
  397. UnknownManufacturer
  398. };
  399. protected:
  400. // For windows
  401. bool RetrieveCPUFeatures();
  402. bool RetrieveCPUIdentity();
  403. bool RetrieveCPUCacheDetails();
  404. bool RetrieveClassicalCPUCacheDetails();
  405. bool RetrieveCPUClockSpeed();
  406. bool RetrieveClassicalCPUClockSpeed();
  407. bool RetrieveCPUExtendedLevelSupport(int);
  408. bool RetrieveExtendedCPUFeatures();
  409. bool RetrieveProcessorSerialNumber();
  410. bool RetrieveCPUPowerManagement();
  411. bool RetrieveClassicalCPUIdentity();
  412. bool RetrieveExtendedCPUIdentity();
  413. // Processor information
  414. Manufacturer ChipManufacturer;
  415. CPUFeatures Features;
  416. ID ChipID;
  417. float CPUSpeedInMHz;
  418. unsigned int NumberOfLogicalCPU;
  419. unsigned int NumberOfPhysicalCPU;
  420. void CPUCountWindows(); // For windows
  421. unsigned char GetAPICId(); // For windows
  422. bool IsSMTSupported();
  423. static LongLong GetCyclesDifference(DELAY_FUNC, unsigned int); // For windows
  424. // For Linux and Cygwin, /proc/cpuinfo formats are slightly different
  425. bool RetreiveInformationFromCpuInfoFile();
  426. std::string ExtractValueFromCpuInfoFile(std::string buffer, const char* word,
  427. size_t init = 0);
  428. bool QueryLinuxMemory();
  429. bool QueryCygwinMemory();
  430. static void Delay(unsigned int);
  431. static void DelayOverhead(unsigned int);
  432. void FindManufacturer(const std::string& family = "");
  433. // For Mac
  434. bool ParseSysCtl();
  435. int CallSwVers(const char* arg, std::string& ver);
  436. void TrimNewline(std::string&);
  437. std::string ExtractValueFromSysCtl(const char* word);
  438. std::string SysCtlBuffer;
  439. // For Solaris
  440. bool QuerySolarisMemory();
  441. bool QuerySolarisProcessor();
  442. std::string ParseValueFromKStat(const char* arguments);
  443. std::string RunProcess(std::vector<const char*> args);
  444. // For Haiku OS
  445. bool QueryHaikuInfo();
  446. // For QNX
  447. bool QueryQNXMemory();
  448. bool QueryQNXProcessor();
  449. // For OpenBSD, FreeBSD, NetBSD, DragonFly
  450. bool QueryBSDMemory();
  451. bool QueryBSDProcessor();
  452. // For HP-UX
  453. bool QueryHPUXMemory();
  454. bool QueryHPUXProcessor();
  455. // For Microsoft Windows
  456. bool QueryWindowsMemory();
  457. // For AIX
  458. bool QueryAIXMemory();
  459. bool QueryProcessorBySysconf();
  460. bool QueryProcessor();
  461. // Evaluate the memory information.
  462. bool QueryMemoryBySysconf();
  463. bool QueryMemory();
  464. size_t TotalVirtualMemory;
  465. size_t AvailableVirtualMemory;
  466. size_t TotalPhysicalMemory;
  467. size_t AvailablePhysicalMemory;
  468. size_t CurrentPositionInFile;
  469. // Operating System information
  470. bool QueryOSInformation();
  471. std::string OSName;
  472. std::string Hostname;
  473. std::string OSRelease;
  474. std::string OSVersion;
  475. std::string OSPlatform;
  476. bool OSIs64Bit;
  477. };
  478. SystemInformation::SystemInformation()
  479. {
  480. this->Implementation = new SystemInformationImplementation;
  481. }
  482. SystemInformation::~SystemInformation()
  483. {
  484. delete this->Implementation;
  485. }
  486. const char* SystemInformation::GetVendorString()
  487. {
  488. return this->Implementation->GetVendorString();
  489. }
  490. const char* SystemInformation::GetVendorID()
  491. {
  492. return this->Implementation->GetVendorID();
  493. }
  494. std::string SystemInformation::GetTypeID()
  495. {
  496. return this->Implementation->GetTypeID();
  497. }
  498. std::string SystemInformation::GetFamilyID()
  499. {
  500. return this->Implementation->GetFamilyID();
  501. }
  502. std::string SystemInformation::GetModelID()
  503. {
  504. return this->Implementation->GetModelID();
  505. }
  506. std::string SystemInformation::GetModelName()
  507. {
  508. return this->Implementation->GetModelName();
  509. }
  510. std::string SystemInformation::GetSteppingCode()
  511. {
  512. return this->Implementation->GetSteppingCode();
  513. }
  514. const char* SystemInformation::GetExtendedProcessorName()
  515. {
  516. return this->Implementation->GetExtendedProcessorName();
  517. }
  518. const char* SystemInformation::GetProcessorSerialNumber()
  519. {
  520. return this->Implementation->GetProcessorSerialNumber();
  521. }
  522. int SystemInformation::GetProcessorCacheSize()
  523. {
  524. return this->Implementation->GetProcessorCacheSize();
  525. }
  526. unsigned int SystemInformation::GetLogicalProcessorsPerPhysical()
  527. {
  528. return this->Implementation->GetLogicalProcessorsPerPhysical();
  529. }
  530. float SystemInformation::GetProcessorClockFrequency()
  531. {
  532. return this->Implementation->GetProcessorClockFrequency();
  533. }
  534. int SystemInformation::GetProcessorAPICID()
  535. {
  536. return this->Implementation->GetProcessorAPICID();
  537. }
  538. int SystemInformation::GetProcessorCacheXSize(long int l)
  539. {
  540. return this->Implementation->GetProcessorCacheXSize(l);
  541. }
  542. bool SystemInformation::DoesCPUSupportFeature(long int i)
  543. {
  544. return this->Implementation->DoesCPUSupportFeature(i);
  545. }
  546. std::string SystemInformation::GetCPUDescription()
  547. {
  548. std::ostringstream oss;
  549. oss << this->GetNumberOfPhysicalCPU() << " core ";
  550. if (this->GetModelName().empty()) {
  551. oss << this->GetProcessorClockFrequency() << " MHz "
  552. << this->GetVendorString() << " " << this->GetExtendedProcessorName();
  553. } else {
  554. oss << this->GetModelName();
  555. }
  556. // remove extra spaces
  557. std::string tmp = oss.str();
  558. size_t pos;
  559. while ((pos = tmp.find(" ")) != std::string::npos) {
  560. tmp.replace(pos, 2, " ");
  561. }
  562. return tmp;
  563. }
  564. const char* SystemInformation::GetOSName()
  565. {
  566. return this->Implementation->GetOSName();
  567. }
  568. const char* SystemInformation::GetHostname()
  569. {
  570. return this->Implementation->GetHostname();
  571. }
  572. std::string SystemInformation::GetFullyQualifiedDomainName()
  573. {
  574. std::string fqdn;
  575. this->Implementation->GetFullyQualifiedDomainName(fqdn);
  576. return fqdn;
  577. }
  578. const char* SystemInformation::GetOSRelease()
  579. {
  580. return this->Implementation->GetOSRelease();
  581. }
  582. const char* SystemInformation::GetOSVersion()
  583. {
  584. return this->Implementation->GetOSVersion();
  585. }
  586. const char* SystemInformation::GetOSPlatform()
  587. {
  588. return this->Implementation->GetOSPlatform();
  589. }
  590. int SystemInformation::GetOSIsWindows()
  591. {
  592. #if defined(_WIN32)
  593. return 1;
  594. #else
  595. return 0;
  596. #endif
  597. }
  598. int SystemInformation::GetOSIsLinux()
  599. {
  600. #if defined(__linux)
  601. return 1;
  602. #else
  603. return 0;
  604. #endif
  605. }
  606. int SystemInformation::GetOSIsApple()
  607. {
  608. #if defined(__APPLE__)
  609. return 1;
  610. #else
  611. return 0;
  612. #endif
  613. }
  614. std::string SystemInformation::GetOSDescription()
  615. {
  616. std::ostringstream oss;
  617. oss << this->GetOSName() << " " << this->GetOSRelease() << " "
  618. << this->GetOSVersion();
  619. return oss.str();
  620. }
  621. bool SystemInformation::Is64Bits()
  622. {
  623. return this->Implementation->Is64Bits();
  624. }
  625. unsigned int SystemInformation::GetNumberOfLogicalCPU() // per physical cpu
  626. {
  627. return this->Implementation->GetNumberOfLogicalCPU();
  628. }
  629. unsigned int SystemInformation::GetNumberOfPhysicalCPU()
  630. {
  631. return this->Implementation->GetNumberOfPhysicalCPU();
  632. }
  633. bool SystemInformation::DoesCPUSupportCPUID()
  634. {
  635. return this->Implementation->DoesCPUSupportCPUID();
  636. }
  637. // Retrieve memory information in MiB.
  638. size_t SystemInformation::GetTotalVirtualMemory()
  639. {
  640. return this->Implementation->GetTotalVirtualMemory();
  641. }
  642. size_t SystemInformation::GetAvailableVirtualMemory()
  643. {
  644. return this->Implementation->GetAvailableVirtualMemory();
  645. }
  646. size_t SystemInformation::GetTotalPhysicalMemory()
  647. {
  648. return this->Implementation->GetTotalPhysicalMemory();
  649. }
  650. size_t SystemInformation::GetAvailablePhysicalMemory()
  651. {
  652. return this->Implementation->GetAvailablePhysicalMemory();
  653. }
  654. std::string SystemInformation::GetMemoryDescription(
  655. const char* hostLimitEnvVarName, const char* procLimitEnvVarName)
  656. {
  657. std::ostringstream oss;
  658. oss << "Host Total: " << iostreamLongLong(this->GetHostMemoryTotal())
  659. << " KiB, Host Available: "
  660. << iostreamLongLong(this->GetHostMemoryAvailable(hostLimitEnvVarName))
  661. << " KiB, Process Available: "
  662. << iostreamLongLong(this->GetProcMemoryAvailable(hostLimitEnvVarName,
  663. procLimitEnvVarName))
  664. << " KiB";
  665. return oss.str();
  666. }
  667. // host memory info in units of KiB.
  668. SystemInformation::LongLong SystemInformation::GetHostMemoryTotal()
  669. {
  670. return this->Implementation->GetHostMemoryTotal();
  671. }
  672. SystemInformation::LongLong SystemInformation::GetHostMemoryAvailable(
  673. const char* hostLimitEnvVarName)
  674. {
  675. return this->Implementation->GetHostMemoryAvailable(hostLimitEnvVarName);
  676. }
  677. SystemInformation::LongLong SystemInformation::GetHostMemoryUsed()
  678. {
  679. return this->Implementation->GetHostMemoryUsed();
  680. }
  681. // process memory info in units of KiB.
  682. SystemInformation::LongLong SystemInformation::GetProcMemoryAvailable(
  683. const char* hostLimitEnvVarName, const char* procLimitEnvVarName)
  684. {
  685. return this->Implementation->GetProcMemoryAvailable(hostLimitEnvVarName,
  686. procLimitEnvVarName);
  687. }
  688. SystemInformation::LongLong SystemInformation::GetProcMemoryUsed()
  689. {
  690. return this->Implementation->GetProcMemoryUsed();
  691. }
  692. double SystemInformation::GetLoadAverage()
  693. {
  694. return this->Implementation->GetLoadAverage();
  695. }
  696. SystemInformation::LongLong SystemInformation::GetProcessId()
  697. {
  698. return this->Implementation->GetProcessId();
  699. }
  700. void SystemInformation::SetStackTraceOnError(int enable)
  701. {
  702. SystemInformationImplementation::SetStackTraceOnError(enable);
  703. }
  704. std::string SystemInformation::GetProgramStack(int firstFrame, int wholePath)
  705. {
  706. return SystemInformationImplementation::GetProgramStack(firstFrame,
  707. wholePath);
  708. }
  709. /** Run the different checks */
  710. void SystemInformation::RunCPUCheck()
  711. {
  712. this->Implementation->RunCPUCheck();
  713. }
  714. void SystemInformation::RunOSCheck()
  715. {
  716. this->Implementation->RunOSCheck();
  717. }
  718. void SystemInformation::RunMemoryCheck()
  719. {
  720. this->Implementation->RunMemoryCheck();
  721. }
  722. // SystemInformationImplementation starts here
  723. #define STORE_TLBCACHE_INFO(x, y) x = (x < (y)) ? (y) : x
  724. #define TLBCACHE_INFO_UNITS (15)
  725. #define CLASSICAL_CPU_FREQ_LOOP 10000000
  726. #define RDTSC_INSTRUCTION _asm _emit 0x0f _asm _emit 0x31
  727. // Status Flag
  728. #define HT_NOT_CAPABLE 0
  729. #define HT_ENABLED 1
  730. #define HT_DISABLED 2
  731. #define HT_SUPPORTED_NOT_ENABLED 3
  732. #define HT_CANNOT_DETECT 4
  733. // EDX[28] Bit 28 is set if HT is supported
  734. #define HT_BIT 0x10000000
  735. // EAX[11:8] Bit 8-11 contains family processor ID.
  736. #define FAMILY_ID 0x0F00
  737. #define PENTIUM4_ID 0x0F00
  738. // EAX[23:20] Bit 20-23 contains extended family processor ID
  739. #define EXT_FAMILY_ID 0x0F00000
  740. // EBX[23:16] Bit 16-23 in ebx contains the number of logical
  741. #define NUM_LOGICAL_BITS 0x00FF0000
  742. // processors per physical processor when execute cpuid with
  743. // eax set to 1
  744. // EBX[31:24] Bits 24-31 (8 bits) return the 8-bit unique
  745. #define INITIAL_APIC_ID_BITS 0xFF000000
  746. // initial APIC ID for the processor this code is running on.
  747. // Default value = 0xff if HT is not supported
  748. // Hide implementation details in an anonymous namespace.
  749. namespace {
  750. // *****************************************************************************
  751. #if defined(__linux) || defined(__APPLE__)
  752. int LoadLines(FILE* file, std::vector<std::string>& lines)
  753. {
  754. // Load each line in the given file into a the vector.
  755. int nRead = 0;
  756. const int bufSize = 1024;
  757. char buf[bufSize] = { '\0' };
  758. while (!feof(file) && !ferror(file)) {
  759. errno = 0;
  760. if (fgets(buf, bufSize, file) == KWSYS_NULLPTR) {
  761. if (ferror(file) && (errno == EINTR)) {
  762. clearerr(file);
  763. }
  764. continue;
  765. }
  766. char* pBuf = buf;
  767. while (*pBuf) {
  768. if (*pBuf == '\n')
  769. *pBuf = '\0';
  770. pBuf += 1;
  771. }
  772. lines.push_back(buf);
  773. ++nRead;
  774. }
  775. if (ferror(file)) {
  776. return 0;
  777. }
  778. return nRead;
  779. }
  780. #if defined(__linux)
  781. // *****************************************************************************
  782. int LoadLines(const char* fileName, std::vector<std::string>& lines)
  783. {
  784. FILE* file = fopen(fileName, "r");
  785. if (file == 0) {
  786. return 0;
  787. }
  788. int nRead = LoadLines(file, lines);
  789. fclose(file);
  790. return nRead;
  791. }
  792. #endif
  793. // ****************************************************************************
  794. template <typename T>
  795. int NameValue(std::vector<std::string> const& lines, std::string const& name,
  796. T& value)
  797. {
  798. size_t nLines = lines.size();
  799. for (size_t i = 0; i < nLines; ++i) {
  800. size_t at = lines[i].find(name);
  801. if (at == std::string::npos) {
  802. continue;
  803. }
  804. std::istringstream is(lines[i].substr(at + name.size()));
  805. is >> value;
  806. return 0;
  807. }
  808. return -1;
  809. }
  810. #endif
  811. #if defined(__linux)
  812. // ****************************************************************************
  813. template <typename T>
  814. int GetFieldsFromFile(const char* fileName, const char** fieldNames, T* values)
  815. {
  816. std::vector<std::string> fields;
  817. if (!LoadLines(fileName, fields)) {
  818. return -1;
  819. }
  820. int i = 0;
  821. while (fieldNames[i] != NULL) {
  822. int ierr = NameValue(fields, fieldNames[i], values[i]);
  823. if (ierr) {
  824. return -(i + 2);
  825. }
  826. i += 1;
  827. }
  828. return 0;
  829. }
  830. // ****************************************************************************
  831. template <typename T>
  832. int GetFieldFromFile(const char* fileName, const char* fieldName, T& value)
  833. {
  834. const char* fieldNames[2] = { fieldName, NULL };
  835. T values[1] = { T(0) };
  836. int ierr = GetFieldsFromFile(fileName, fieldNames, values);
  837. if (ierr) {
  838. return ierr;
  839. }
  840. value = values[0];
  841. return 0;
  842. }
  843. #endif
  844. // ****************************************************************************
  845. #if defined(__APPLE__)
  846. template <typename T>
  847. int GetFieldsFromCommand(const char* command, const char** fieldNames,
  848. T* values)
  849. {
  850. FILE* file = popen(command, "r");
  851. if (file == KWSYS_NULLPTR) {
  852. return -1;
  853. }
  854. std::vector<std::string> fields;
  855. int nl = LoadLines(file, fields);
  856. pclose(file);
  857. if (nl == 0) {
  858. return -1;
  859. }
  860. int i = 0;
  861. while (fieldNames[i] != KWSYS_NULLPTR) {
  862. int ierr = NameValue(fields, fieldNames[i], values[i]);
  863. if (ierr) {
  864. return -(i + 2);
  865. }
  866. i += 1;
  867. }
  868. return 0;
  869. }
  870. #endif
  871. // ****************************************************************************
  872. #if !defined(_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
  873. void StacktraceSignalHandler(int sigNo, siginfo_t* sigInfo,
  874. void* /*sigContext*/)
  875. {
  876. #if defined(__linux) || defined(__APPLE__)
  877. std::ostringstream oss;
  878. oss << std::endl
  879. << "========================================================="
  880. << std::endl
  881. << "Process id " << getpid() << " ";
  882. switch (sigNo) {
  883. case SIGINT:
  884. oss << "Caught SIGINT";
  885. break;
  886. case SIGTERM:
  887. oss << "Caught SIGTERM";
  888. break;
  889. case SIGABRT:
  890. oss << "Caught SIGABRT";
  891. break;
  892. case SIGFPE:
  893. oss << "Caught SIGFPE at "
  894. << (sigInfo->si_addr == KWSYS_NULLPTR ? "0x" : "")
  895. << sigInfo->si_addr << " ";
  896. switch (sigInfo->si_code) {
  897. #if defined(FPE_INTDIV)
  898. case FPE_INTDIV:
  899. oss << "integer division by zero";
  900. break;
  901. #endif
  902. #if defined(FPE_INTOVF)
  903. case FPE_INTOVF:
  904. oss << "integer overflow";
  905. break;
  906. #endif
  907. case FPE_FLTDIV:
  908. oss << "floating point divide by zero";
  909. break;
  910. case FPE_FLTOVF:
  911. oss << "floating point overflow";
  912. break;
  913. case FPE_FLTUND:
  914. oss << "floating point underflow";
  915. break;
  916. case FPE_FLTRES:
  917. oss << "floating point inexact result";
  918. break;
  919. case FPE_FLTINV:
  920. oss << "floating point invalid operation";
  921. break;
  922. #if defined(FPE_FLTSUB)
  923. case FPE_FLTSUB:
  924. oss << "floating point subscript out of range";
  925. break;
  926. #endif
  927. default:
  928. oss << "code " << sigInfo->si_code;
  929. break;
  930. }
  931. break;
  932. case SIGSEGV:
  933. oss << "Caught SIGSEGV at "
  934. << (sigInfo->si_addr == KWSYS_NULLPTR ? "0x" : "")
  935. << sigInfo->si_addr << " ";
  936. switch (sigInfo->si_code) {
  937. case SEGV_MAPERR:
  938. oss << "address not mapped to object";
  939. break;
  940. case SEGV_ACCERR:
  941. oss << "invalid permission for mapped object";
  942. break;
  943. default:
  944. oss << "code " << sigInfo->si_code;
  945. break;
  946. }
  947. break;
  948. case SIGBUS:
  949. oss << "Caught SIGBUS at "
  950. << (sigInfo->si_addr == KWSYS_NULLPTR ? "0x" : "")
  951. << sigInfo->si_addr << " ";
  952. switch (sigInfo->si_code) {
  953. case BUS_ADRALN:
  954. oss << "invalid address alignment";
  955. break;
  956. #if defined(BUS_ADRERR)
  957. case BUS_ADRERR:
  958. oss << "nonexistent physical address";
  959. break;
  960. #endif
  961. #if defined(BUS_OBJERR)
  962. case BUS_OBJERR:
  963. oss << "object-specific hardware error";
  964. break;
  965. #endif
  966. #if defined(BUS_MCEERR_AR)
  967. case BUS_MCEERR_AR:
  968. oss << "Hardware memory error consumed on a machine check; action "
  969. "required.";
  970. break;
  971. #endif
  972. #if defined(BUS_MCEERR_AO)
  973. case BUS_MCEERR_AO:
  974. oss << "Hardware memory error detected in process but not consumed; "
  975. "action optional.";
  976. break;
  977. #endif
  978. default:
  979. oss << "code " << sigInfo->si_code;
  980. break;
  981. }
  982. break;
  983. case SIGILL:
  984. oss << "Caught SIGILL at "
  985. << (sigInfo->si_addr == KWSYS_NULLPTR ? "0x" : "")
  986. << sigInfo->si_addr << " ";
  987. switch (sigInfo->si_code) {
  988. case ILL_ILLOPC:
  989. oss << "illegal opcode";
  990. break;
  991. #if defined(ILL_ILLOPN)
  992. case ILL_ILLOPN:
  993. oss << "illegal operand";
  994. break;
  995. #endif
  996. #if defined(ILL_ILLADR)
  997. case ILL_ILLADR:
  998. oss << "illegal addressing mode.";
  999. break;
  1000. #endif
  1001. case ILL_ILLTRP:
  1002. oss << "illegal trap";
  1003. break;
  1004. case ILL_PRVOPC:
  1005. oss << "privileged opcode";
  1006. break;
  1007. #if defined(ILL_PRVREG)
  1008. case ILL_PRVREG:
  1009. oss << "privileged register";
  1010. break;
  1011. #endif
  1012. #if defined(ILL_COPROC)
  1013. case ILL_COPROC:
  1014. oss << "co-processor error";
  1015. break;
  1016. #endif
  1017. #if defined(ILL_BADSTK)
  1018. case ILL_BADSTK:
  1019. oss << "internal stack error";
  1020. break;
  1021. #endif
  1022. default:
  1023. oss << "code " << sigInfo->si_code;
  1024. break;
  1025. }
  1026. break;
  1027. default:
  1028. oss << "Caught " << sigNo << " code " << sigInfo->si_code;
  1029. break;
  1030. }
  1031. oss << std::endl
  1032. << "Program Stack:" << std::endl
  1033. << SystemInformationImplementation::GetProgramStack(2, 0)
  1034. << "========================================================="
  1035. << std::endl;
  1036. std::cerr << oss.str() << std::endl;
  1037. // restore the previously registered handlers
  1038. // and abort
  1039. SystemInformationImplementation::SetStackTraceOnError(0);
  1040. abort();
  1041. #else
  1042. // avoid warning C4100
  1043. (void)sigNo;
  1044. (void)sigInfo;
  1045. #endif
  1046. }
  1047. #endif
  1048. #if defined(KWSYS_SYSTEMINFORMATION_HAS_BACKTRACE)
  1049. #define safes(_arg) ((_arg) ? (_arg) : "???")
  1050. // Description:
  1051. // A container for symbol properties. Each instance
  1052. // must be Initialized.
  1053. class SymbolProperties
  1054. {
  1055. public:
  1056. SymbolProperties();
  1057. // Description:
  1058. // The SymbolProperties instance must be initialized by
  1059. // passing a stack address.
  1060. void Initialize(void* address);
  1061. // Description:
  1062. // Get the symbol's stack address.
  1063. void* GetAddress() const { return this->Address; }
  1064. // Description:
  1065. // If not set paths will be removed. eg, from a binary
  1066. // or source file.
  1067. void SetReportPath(int rp) { this->ReportPath = rp; }
  1068. // Description:
  1069. // Set/Get the name of the binary file that the symbol
  1070. // is found in.
  1071. void SetBinary(const char* binary) { this->Binary = safes(binary); }
  1072. std::string GetBinary() const;
  1073. // Description:
  1074. // Set the name of the function that the symbol is found in.
  1075. // If c++ demangling is supported it will be demangled.
  1076. void SetFunction(const char* function)
  1077. {
  1078. this->Function = this->Demangle(function);
  1079. }
  1080. std::string GetFunction() const { return this->Function; }
  1081. // Description:
  1082. // Set/Get the name of the source file where the symbol
  1083. // is defined.
  1084. void SetSourceFile(const char* sourcefile)
  1085. {
  1086. this->SourceFile = safes(sourcefile);
  1087. }
  1088. std::string GetSourceFile() const
  1089. {
  1090. return this->GetFileName(this->SourceFile);
  1091. }
  1092. // Description:
  1093. // Set/Get the line number where the symbol is defined
  1094. void SetLineNumber(long linenumber) { this->LineNumber = linenumber; }
  1095. long GetLineNumber() const { return this->LineNumber; }
  1096. // Description:
  1097. // Set the address where the binary image is mapped
  1098. // into memory.
  1099. void SetBinaryBaseAddress(void* address)
  1100. {
  1101. this->BinaryBaseAddress = address;
  1102. }
  1103. private:
  1104. void* GetRealAddress() const
  1105. {
  1106. return (void*)((char*)this->Address - (char*)this->BinaryBaseAddress);
  1107. }
  1108. std::string GetFileName(const std::string& path) const;
  1109. std::string Demangle(const char* symbol) const;
  1110. private:
  1111. std::string Binary;
  1112. void* BinaryBaseAddress;
  1113. void* Address;
  1114. std::string SourceFile;
  1115. std::string Function;
  1116. long LineNumber;
  1117. int ReportPath;
  1118. };
  1119. std::ostream& operator<<(std::ostream& os, const SymbolProperties& sp)
  1120. {
  1121. #if defined(KWSYS_SYSTEMINFORMATION_HAS_SYMBOL_LOOKUP)
  1122. os << std::hex << sp.GetAddress() << " : " << sp.GetFunction() << " [("
  1123. << sp.GetBinary() << ") " << sp.GetSourceFile() << ":" << std::dec
  1124. << sp.GetLineNumber() << "]";
  1125. #elif defined(KWSYS_SYSTEMINFORMATION_HAS_BACKTRACE)
  1126. void* addr = sp.GetAddress();
  1127. char** syminfo = backtrace_symbols(&addr, 1);
  1128. os << safes(syminfo[0]);
  1129. free(syminfo);
  1130. #else
  1131. (void)os;
  1132. (void)sp;
  1133. #endif
  1134. return os;
  1135. }
  1136. SymbolProperties::SymbolProperties()
  1137. {
  1138. // not using an initializer list
  1139. // to avoid some PGI compiler warnings
  1140. this->SetBinary("???");
  1141. this->SetBinaryBaseAddress(KWSYS_NULLPTR);
  1142. this->Address = KWSYS_NULLPTR;
  1143. this->SetSourceFile("???");
  1144. this->SetFunction("???");
  1145. this->SetLineNumber(-1);
  1146. this->SetReportPath(0);
  1147. // avoid PGI compiler warnings
  1148. this->GetRealAddress();
  1149. this->GetFunction();
  1150. this->GetSourceFile();
  1151. this->GetLineNumber();
  1152. }
  1153. std::string SymbolProperties::GetFileName(const std::string& path) const
  1154. {
  1155. std::string file(path);
  1156. if (!this->ReportPath) {
  1157. size_t at = file.rfind("/");
  1158. if (at != std::string::npos) {
  1159. file = file.substr(at + 1);
  1160. }
  1161. }
  1162. return file;
  1163. }
  1164. std::string SymbolProperties::GetBinary() const
  1165. {
  1166. // only linux has proc fs
  1167. #if defined(__linux__)
  1168. if (this->Binary == "/proc/self/exe") {
  1169. std::string binary;
  1170. char buf[1024] = { '\0' };
  1171. ssize_t ll = 0;
  1172. if ((ll = readlink("/proc/self/exe", buf, 1024)) > 0 && ll < 1024) {
  1173. buf[ll] = '\0';
  1174. binary = buf;
  1175. } else {
  1176. binary = "/proc/self/exe";
  1177. }
  1178. return this->GetFileName(binary);
  1179. }
  1180. #endif
  1181. return this->GetFileName(this->Binary);
  1182. }
  1183. std::string SymbolProperties::Demangle(const char* symbol) const
  1184. {
  1185. std::string result = safes(symbol);
  1186. #if defined(KWSYS_SYSTEMINFORMATION_HAS_CPP_DEMANGLE)
  1187. int status = 0;
  1188. size_t bufferLen = 1024;
  1189. char* buffer = (char*)malloc(1024);
  1190. char* demangledSymbol =
  1191. abi::__cxa_demangle(symbol, buffer, &bufferLen, &status);
  1192. if (!status) {
  1193. result = demangledSymbol;
  1194. }
  1195. free(buffer);
  1196. #else
  1197. (void)symbol;
  1198. #endif
  1199. return result;
  1200. }
  1201. void SymbolProperties::Initialize(void* address)
  1202. {
  1203. this->Address = address;
  1204. #if defined(KWSYS_SYSTEMINFORMATION_HAS_SYMBOL_LOOKUP)
  1205. // first fallback option can demangle c++ functions
  1206. Dl_info info;
  1207. int ierr = dladdr(this->Address, &info);
  1208. if (ierr && info.dli_sname && info.dli_saddr) {
  1209. this->SetBinary(info.dli_fname);
  1210. this->SetFunction(info.dli_sname);
  1211. }
  1212. #else
  1213. // second fallback use builtin backtrace_symbols
  1214. // to decode the bactrace.
  1215. #endif
  1216. }
  1217. #endif // don't define this class if we're not using it
  1218. #if defined(_WIN32) || defined(__CYGWIN__)
  1219. #define KWSYS_SYSTEMINFORMATION_USE_GetSystemTimes
  1220. #endif
  1221. #if defined(_MSC_VER) && _MSC_VER < 1310
  1222. #undef KWSYS_SYSTEMINFORMATION_USE_GetSystemTimes
  1223. #endif
  1224. #if defined(KWSYS_SYSTEMINFORMATION_USE_GetSystemTimes)
  1225. double calculateCPULoad(unsigned __int64 idleTicks,
  1226. unsigned __int64 totalTicks)
  1227. {
  1228. static double previousLoad = -0.0;
  1229. static unsigned __int64 previousIdleTicks = 0;
  1230. static unsigned __int64 previousTotalTicks = 0;
  1231. unsigned __int64 const idleTicksSinceLastTime =
  1232. idleTicks - previousIdleTicks;
  1233. unsigned __int64 const totalTicksSinceLastTime =
  1234. totalTicks - previousTotalTicks;
  1235. double load;
  1236. if (previousTotalTicks == 0 || totalTicksSinceLastTime == 0) {
  1237. // No new information. Use previous result.
  1238. load = previousLoad;
  1239. } else {
  1240. // Calculate load since last time.
  1241. load = 1.0 - double(idleTicksSinceLastTime) / totalTicksSinceLastTime;
  1242. // Smooth if possible.
  1243. if (previousLoad > 0) {
  1244. load = 0.25 * load + 0.75 * previousLoad;
  1245. }
  1246. }
  1247. previousLoad = load;
  1248. previousIdleTicks = idleTicks;
  1249. previousTotalTicks = totalTicks;
  1250. return load;
  1251. }
  1252. unsigned __int64 fileTimeToUInt64(FILETIME const& ft)
  1253. {
  1254. LARGE_INTEGER out;
  1255. out.HighPart = ft.dwHighDateTime;
  1256. out.LowPart = ft.dwLowDateTime;
  1257. return out.QuadPart;
  1258. }
  1259. #endif
  1260. } // anonymous namespace
  1261. SystemInformationImplementation::SystemInformationImplementation()
  1262. {
  1263. this->TotalVirtualMemory = 0;
  1264. this->AvailableVirtualMemory = 0;
  1265. this->TotalPhysicalMemory = 0;
  1266. this->AvailablePhysicalMemory = 0;
  1267. this->CurrentPositionInFile = 0;
  1268. this->ChipManufacturer = UnknownManufacturer;
  1269. memset(&this->Features, 0, sizeof(CPUFeatures));
  1270. this->ChipID.Type = 0;
  1271. this->ChipID.Family = 0;
  1272. this->ChipID.Model = 0;
  1273. this->ChipID.Revision = 0;
  1274. this->ChipID.ExtendedFamily = 0;
  1275. this->ChipID.ExtendedModel = 0;
  1276. this->CPUSpeedInMHz = 0;
  1277. this->NumberOfLogicalCPU = 0;
  1278. this->NumberOfPhysicalCPU = 0;
  1279. this->OSName = "";
  1280. this->Hostname = "";
  1281. this->OSRelease = "";
  1282. this->OSVersion = "";
  1283. this->OSPlatform = "";
  1284. this->OSIs64Bit = (sizeof(void*) == 8);
  1285. }
  1286. SystemInformationImplementation::~SystemInformationImplementation()
  1287. {
  1288. }
  1289. void SystemInformationImplementation::RunCPUCheck()
  1290. {
  1291. #ifdef _WIN32
  1292. // Check to see if this processor supports CPUID.
  1293. bool supportsCPUID = DoesCPUSupportCPUID();
  1294. if (supportsCPUID) {
  1295. // Retrieve the CPU details.
  1296. RetrieveCPUIdentity();
  1297. this->FindManufacturer();
  1298. RetrieveCPUFeatures();
  1299. }
  1300. // These two may be called without support for the CPUID instruction.
  1301. // (But if the instruction is there, they should be called *after*
  1302. // the above call to RetrieveCPUIdentity... that's why the two if
  1303. // blocks exist with the same "if (supportsCPUID)" logic...
  1304. //
  1305. if (!RetrieveCPUClockSpeed()) {
  1306. RetrieveClassicalCPUClockSpeed();
  1307. }
  1308. if (supportsCPUID) {
  1309. // Retrieve cache information.
  1310. if (!RetrieveCPUCacheDetails()) {
  1311. RetrieveClassicalCPUCacheDetails();
  1312. }
  1313. // Retrieve the extended CPU details.
  1314. if (!RetrieveExtendedCPUIdentity()) {
  1315. RetrieveClassicalCPUIdentity();
  1316. }
  1317. RetrieveExtendedCPUFeatures();
  1318. RetrieveCPUPowerManagement();
  1319. // Now attempt to retrieve the serial number (if possible).
  1320. RetrieveProcessorSerialNumber();
  1321. }
  1322. this->CPUCountWindows();
  1323. #elif defined(__APPLE__)
  1324. this->ParseSysCtl();
  1325. #elif defined(__SVR4) && defined(__sun)
  1326. this->QuerySolarisProcessor();
  1327. #elif defined(__HAIKU__)
  1328. this->QueryHaikuInfo();
  1329. #elif defined(__QNX__)
  1330. this->QueryQNXProcessor();
  1331. #elif defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
  1332. defined(__DragonFly__)
  1333. this->QueryBSDProcessor();
  1334. #elif defined(__hpux)
  1335. this->QueryHPUXProcessor();
  1336. #elif defined(__linux) || defined(__CYGWIN__)
  1337. this->RetreiveInformationFromCpuInfoFile();
  1338. #else
  1339. this->QueryProcessor();
  1340. #endif
  1341. }
  1342. void SystemInformationImplementation::RunOSCheck()
  1343. {
  1344. this->QueryOSInformation();
  1345. }
  1346. void SystemInformationImplementation::RunMemoryCheck()
  1347. {
  1348. #if defined(__APPLE__)
  1349. this->ParseSysCtl();
  1350. #elif defined(__SVR4) && defined(__sun)
  1351. this->QuerySolarisMemory();
  1352. #elif defined(__HAIKU__)
  1353. this->QueryHaikuInfo();
  1354. #elif defined(__QNX__)
  1355. this->QueryQNXMemory();
  1356. #elif defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
  1357. defined(__DragonFly__)
  1358. this->QueryBSDMemory();
  1359. #elif defined(__CYGWIN__)
  1360. this->QueryCygwinMemory();
  1361. #elif defined(_WIN32)
  1362. this->QueryWindowsMemory();
  1363. #elif defined(__hpux)
  1364. this->QueryHPUXMemory();
  1365. #elif defined(__linux)
  1366. this->QueryLinuxMemory();
  1367. #elif defined(_AIX)
  1368. this->QueryAIXMemory();
  1369. #else
  1370. this->QueryMemory();
  1371. #endif
  1372. }
  1373. /** Get the vendor string */
  1374. const char* SystemInformationImplementation::GetVendorString()
  1375. {
  1376. return this->ChipID.Vendor.c_str();
  1377. }
  1378. /** Get the OS Name */
  1379. const char* SystemInformationImplementation::GetOSName()
  1380. {
  1381. return this->OSName.c_str();
  1382. }
  1383. /** Get the hostname */
  1384. const char* SystemInformationImplementation::GetHostname()
  1385. {
  1386. if (this->Hostname.empty()) {
  1387. this->Hostname = "localhost";
  1388. #if defined(_WIN32)
  1389. WORD wVersionRequested;
  1390. WSADATA wsaData;
  1391. char name[255];
  1392. wVersionRequested = MAKEWORD(2, 0);
  1393. if (WSAStartup(wVersionRequested, &wsaData) == 0) {
  1394. gethostname(name, sizeof(name));
  1395. WSACleanup();
  1396. }
  1397. this->Hostname = name;
  1398. #else
  1399. struct utsname unameInfo;
  1400. int errorFlag = uname(&unameInfo);
  1401. if (errorFlag == 0) {
  1402. this->Hostname = unameInfo.nodename;
  1403. }
  1404. #endif
  1405. }
  1406. return this->Hostname.c_str();
  1407. }
  1408. /** Get the FQDN */
  1409. int SystemInformationImplementation::GetFullyQualifiedDomainName(
  1410. std::string& fqdn)
  1411. {
  1412. // in the event of absolute failure return localhost.
  1413. fqdn = "localhost";
  1414. #if defined(_WIN32)
  1415. int ierr;
  1416. // TODO - a more robust implementation for windows, see comments
  1417. // in unix implementation.
  1418. WSADATA wsaData;
  1419. WORD ver = MAKEWORD(2, 0);
  1420. ierr = WSAStartup(ver, &wsaData);
  1421. if (ierr) {
  1422. return -1;
  1423. }
  1424. char base[256] = { '\0' };
  1425. ierr = gethostname(base, 256);
  1426. if (ierr) {
  1427. WSACleanup();
  1428. return -2;
  1429. }
  1430. fqdn = base;
  1431. HOSTENT* hent = gethostbyname(base);
  1432. if (hent) {
  1433. fqdn = hent->h_name;
  1434. }
  1435. WSACleanup();
  1436. return 0;
  1437. #elif defined(KWSYS_SYSTEMINFORMATION_IMPLEMENT_FQDN)
  1438. // gethostname typical returns an alias for loopback interface
  1439. // we want the fully qualified domain name. Because there are
  1440. // any number of interfaces on this system we look for the
  1441. // first of these that contains the name returned by gethostname
  1442. // and is longer. failing that we return gethostname and indicate
  1443. // with a failure code. Return of a failure code is not necessarily
  1444. // an indication of an error. for instance gethostname may return
  1445. // the fully qualified domain name, or there may not be one if the
  1446. // system lives on a private network such as in the case of a cluster
  1447. // node.
  1448. int ierr = 0;
  1449. char base[NI_MAXHOST];
  1450. ierr = gethostname(base, NI_MAXHOST);
  1451. if (ierr) {
  1452. return -1;
  1453. }
  1454. size_t baseSize = strlen(base);
  1455. fqdn = base;
  1456. struct ifaddrs* ifas;
  1457. struct ifaddrs* ifa;
  1458. ierr = getifaddrs(&ifas);
  1459. if (ierr) {
  1460. return -2;
  1461. }
  1462. for (ifa = ifas; ifa != KWSYS_NULLPTR; ifa = ifa->ifa_next) {
  1463. int fam = ifa->ifa_addr ? ifa->ifa_addr->sa_family : -1;
  1464. // Skip Loopback interfaces
  1465. if (((fam == AF_INET) || (fam == AF_INET6)) &&
  1466. !(ifa->ifa_flags & IFF_LOOPBACK)) {
  1467. char host[NI_MAXHOST] = { '\0' };
  1468. const size_t addrlen = (fam == AF_INET ? sizeof(struct sockaddr_in)
  1469. : sizeof(struct sockaddr_in6));
  1470. ierr = getnameinfo(ifa->ifa_addr, static_cast<socklen_t>(addrlen), host,
  1471. NI_MAXHOST, KWSYS_NULLPTR, 0, NI_NAMEREQD);
  1472. if (ierr) {
  1473. // don't report the failure now since we may succeed on another
  1474. // interface. If all attempts fail then return the failure code.
  1475. ierr = -3;
  1476. continue;
  1477. }
  1478. std::string candidate = host;
  1479. if ((candidate.find(base) != std::string::npos) &&
  1480. baseSize < candidate.size()) {
  1481. // success, stop now.
  1482. ierr = 0;
  1483. fqdn = candidate;
  1484. break;
  1485. }
  1486. }
  1487. }
  1488. freeifaddrs(ifas);
  1489. return ierr;
  1490. #else
  1491. /* TODO: Implement on more platforms. */
  1492. fqdn = this->GetHostname();
  1493. return -1;
  1494. #endif
  1495. }
  1496. /** Get the OS release */
  1497. const char* SystemInformationImplementation::GetOSRelease()
  1498. {
  1499. return this->OSRelease.c_str();
  1500. }
  1501. /** Get the OS version */
  1502. const char* SystemInformationImplementation::GetOSVersion()
  1503. {
  1504. return this->OSVersion.c_str();
  1505. }
  1506. /** Get the OS platform */
  1507. const char* SystemInformationImplementation::GetOSPlatform()
  1508. {
  1509. return this->OSPlatform.c_str();
  1510. }
  1511. /** Get the vendor ID */
  1512. const char* SystemInformationImplementation::GetVendorID()
  1513. {
  1514. // Return the vendor ID.
  1515. switch (this->ChipManufacturer) {
  1516. case Intel:
  1517. return "Intel Corporation";
  1518. case AMD:
  1519. return "Advanced Micro Devices";
  1520. case NSC:
  1521. return "National Semiconductor";
  1522. case Cyrix:
  1523. return "Cyrix Corp., VIA Inc.";
  1524. case NexGen:
  1525. return "NexGen Inc., Advanced Micro Devices";
  1526. case IDT:
  1527. return "IDT\\Centaur, Via Inc.";
  1528. case UMC:
  1529. return "United Microelectronics Corp.";
  1530. case Rise:
  1531. return "Rise";
  1532. case Transmeta:
  1533. return "Transmeta";
  1534. case Sun:
  1535. return "Sun Microelectronics";
  1536. case IBM:
  1537. return "IBM";
  1538. case Motorola:
  1539. return "Motorola";
  1540. case HP:
  1541. return "Hewlett-Packard";
  1542. case UnknownManufacturer:
  1543. default:
  1544. return "Unknown Manufacturer";
  1545. }
  1546. }
  1547. /** Return the type ID of the CPU */
  1548. std::string SystemInformationImplementation::GetTypeID()
  1549. {
  1550. std::ostringstream str;
  1551. str << this->ChipID.Type;
  1552. return str.str();
  1553. }
  1554. /** Return the family of the CPU present */
  1555. std::string SystemInformationImplementation::GetFamilyID()
  1556. {
  1557. std::ostringstream str;
  1558. str << this->ChipID.Family;
  1559. return str.str();
  1560. }
  1561. // Return the model of CPU present */
  1562. std::string SystemInformationImplementation::GetModelID()
  1563. {
  1564. std::ostringstream str;
  1565. str << this->ChipID.Model;
  1566. return str.str();
  1567. }
  1568. // Return the model name of CPU present */
  1569. std::string SystemInformationImplementation::GetModelName()
  1570. {
  1571. return this->ChipID.ModelName;
  1572. }
  1573. /** Return the stepping code of the CPU present. */
  1574. std::string SystemInformationImplementation::GetSteppingCode()
  1575. {
  1576. std::ostringstream str;
  1577. str << this->ChipID.Revision;
  1578. return str.str();
  1579. }
  1580. /** Return the stepping code of the CPU present. */
  1581. const char* SystemInformationImplementation::GetExtendedProcessorName()
  1582. {
  1583. return this->ChipID.ProcessorName.c_str();
  1584. }
  1585. /** Return the serial number of the processor
  1586. * in hexadecimal: xxxx-xxxx-xxxx-xxxx-xxxx-xxxx. */
  1587. const char* SystemInformationImplementation::GetProcessorSerialNumber()
  1588. {
  1589. return this->ChipID.SerialNumber.c_str();
  1590. }
  1591. /** Return the logical processors per physical */
  1592. unsigned int SystemInformationImplementation::GetLogicalProcessorsPerPhysical()
  1593. {
  1594. return this->Features.ExtendedFeatures.LogicalProcessorsPerPhysical;
  1595. }
  1596. /** Return the processor clock frequency. */
  1597. float SystemInformationImplementation::GetProcessorClockFrequency()
  1598. {
  1599. return this->CPUSpeedInMHz;
  1600. }
  1601. /** Return the APIC ID. */
  1602. int SystemInformationImplementation::GetProcessorAPICID()
  1603. {
  1604. return this->Features.ExtendedFeatures.APIC_ID;
  1605. }
  1606. /** Return the L1 cache size. */
  1607. int SystemInformationImplementation::GetProcessorCacheSize()
  1608. {
  1609. return this->Features.L1CacheSize;
  1610. }
  1611. /** Return the chosen cache size. */
  1612. int SystemInformationImplementation::GetProcessorCacheXSize(long int dwCacheID)
  1613. {
  1614. switch (dwCacheID) {
  1615. case SystemInformation::CPU_FEATURE_L1CACHE:
  1616. return this->Features.L1CacheSize;
  1617. case SystemInformation::CPU_FEATURE_L2CACHE:
  1618. return this->Features.L2CacheSize;
  1619. case SystemInformation::CPU_FEATURE_L3CACHE:
  1620. return this->Features.L3CacheSize;
  1621. }
  1622. return -1;
  1623. }
  1624. bool SystemInformationImplementation::DoesCPUSupportFeature(long int dwFeature)
  1625. {
  1626. bool bHasFeature = false;
  1627. // Check for MMX instructions.
  1628. if (((dwFeature & SystemInformation::CPU_FEATURE_MMX) != 0) &&
  1629. this->Features.HasMMX)
  1630. bHasFeature = true;
  1631. // Check for MMX+ instructions.
  1632. if (((dwFeature & SystemInformation::CPU_FEATURE_MMX_PLUS) != 0) &&
  1633. this->Features.ExtendedFeatures.HasMMXPlus)
  1634. bHasFeature = true;
  1635. // Check for SSE FP instructions.
  1636. if (((dwFeature & SystemInformation::CPU_FEATURE_SSE) != 0) &&
  1637. this->Features.HasSSE)
  1638. bHasFeature = true;
  1639. // Check for SSE FP instructions.
  1640. if (((dwFeature & SystemInformation::CPU_FEATURE_SSE_FP) != 0) &&
  1641. this->Features.HasSSEFP)
  1642. bHasFeature = true;
  1643. // Check for SSE MMX instructions.
  1644. if (((dwFeature & SystemInformation::CPU_FEATURE_SSE_MMX) != 0) &&
  1645. this->Features.ExtendedFeatures.HasSSEMMX)
  1646. bHasFeature = true;
  1647. // Check for SSE2 instructions.
  1648. if (((dwFeature & SystemInformation::CPU_FEATURE_SSE2) != 0) &&
  1649. this->Features.HasSSE2)
  1650. bHasFeature = true;
  1651. // Check for 3DNow! instructions.
  1652. if (((dwFeature & SystemInformation::CPU_FEATURE_AMD_3DNOW) != 0) &&
  1653. this->Features.ExtendedFeatures.Has3DNow)
  1654. bHasFeature = true;
  1655. // Check for 3DNow+ instructions.
  1656. if (((dwFeature & SystemInformation::CPU_FEATURE_AMD_3DNOW_PLUS) != 0) &&
  1657. this->Features.ExtendedFeatures.Has3DNowPlus)
  1658. bHasFeature = true;
  1659. // Check for IA64 instructions.
  1660. if (((dwFeature & SystemInformation::CPU_FEATURE_IA64) != 0) &&
  1661. this->Features.HasIA64)
  1662. bHasFeature = true;
  1663. // Check for MP capable.
  1664. if (((dwFeature & SystemInformation::CPU_FEATURE_MP_CAPABLE) != 0) &&
  1665. this->Features.ExtendedFeatures.SupportsMP)
  1666. bHasFeature = true;
  1667. // Check for a serial number for the processor.
  1668. if (((dwFeature & SystemInformation::CPU_FEATURE_SERIALNUMBER) != 0) &&
  1669. this->Features.HasSerial)
  1670. bHasFeature = true;
  1671. // Check for a local APIC in the processor.
  1672. if (((dwFeature & SystemInformation::CPU_FEATURE_APIC) != 0) &&
  1673. this->Features.HasAPIC)
  1674. bHasFeature = true;
  1675. // Check for CMOV instructions.
  1676. if (((dwFeature & SystemInformation::CPU_FEATURE_CMOV) != 0) &&
  1677. this->Features.HasCMOV)
  1678. bHasFeature = true;
  1679. // Check for MTRR instructions.
  1680. if (((dwFeature & SystemInformation::CPU_FEATURE_MTRR) != 0) &&
  1681. this->Features.HasMTRR)
  1682. bHasFeature = true;
  1683. // Check for L1 cache size.
  1684. if (((dwFeature & SystemInformation::CPU_FEATURE_L1CACHE) != 0) &&
  1685. (this->Features.L1CacheSize != -1))
  1686. bHasFeature = true;
  1687. // Check for L2 cache size.
  1688. if (((dwFeature & SystemInformation::CPU_FEATURE_L2CACHE) != 0) &&
  1689. (this->Features.L2CacheSize != -1))
  1690. bHasFeature = true;
  1691. // Check for L3 cache size.
  1692. if (((dwFeature & SystemInformation::CPU_FEATURE_L3CACHE) != 0) &&
  1693. (this->Features.L3CacheSize != -1))
  1694. bHasFeature = true;
  1695. // Check for ACPI capability.
  1696. if (((dwFeature & SystemInformation::CPU_FEATURE_ACPI) != 0) &&
  1697. this->Features.HasACPI)
  1698. bHasFeature = true;
  1699. // Check for thermal monitor support.
  1700. if (((dwFeature & SystemInformation::CPU_FEATURE_THERMALMONITOR) != 0) &&
  1701. this->Features.HasThermal)
  1702. bHasFeature = true;
  1703. // Check for temperature sensing diode support.
  1704. if (((dwFeature & SystemInformation::CPU_FEATURE_TEMPSENSEDIODE) != 0) &&
  1705. this->Features.ExtendedFeatures.PowerManagement.HasTempSenseDiode)
  1706. bHasFeature = true;
  1707. // Check for frequency ID support.
  1708. if (((dwFeature & SystemInformation::CPU_FEATURE_FREQUENCYID) != 0) &&
  1709. this->Features.ExtendedFeatures.PowerManagement.HasFrequencyID)
  1710. bHasFeature = true;
  1711. // Check for voltage ID support.
  1712. if (((dwFeature & SystemInformation::CPU_FEATURE_VOLTAGEID_FREQUENCY) !=
  1713. 0) &&
  1714. this->Features.ExtendedFeatures.PowerManagement.HasVoltageID)
  1715. bHasFeature = true;
  1716. // Check for FPU support.
  1717. if (((dwFeature & SystemInformation::CPU_FEATURE_FPU) != 0) &&
  1718. this->Features.HasFPU)
  1719. bHasFeature = true;
  1720. return bHasFeature;
  1721. }
  1722. void SystemInformationImplementation::Delay(unsigned int uiMS)
  1723. {
  1724. #ifdef _WIN32
  1725. LARGE_INTEGER Frequency, StartCounter, EndCounter;
  1726. __int64 x;
  1727. // Get the frequency of the high performance counter.
  1728. if (!QueryPerformanceFrequency(&Frequency))
  1729. return;
  1730. x = Frequency.QuadPart / 1000 * uiMS;
  1731. // Get the starting position of the counter.
  1732. QueryPerformanceCounter(&StartCounter);
  1733. do {
  1734. // Get the ending position of the counter.
  1735. QueryPerformanceCounter(&EndCounter);
  1736. } while (EndCounter.QuadPart - StartCounter.QuadPart < x);
  1737. #endif
  1738. (void)uiMS;
  1739. }
  1740. bool SystemInformationImplementation::DoesCPUSupportCPUID()
  1741. {
  1742. #if USE_CPUID
  1743. int dummy[4] = { 0, 0, 0, 0 };
  1744. #if USE_ASM_INSTRUCTIONS
  1745. return call_cpuid(0, dummy);
  1746. #else
  1747. call_cpuid(0, dummy);
  1748. return dummy[0] || dummy[1] || dummy[2] || dummy[3];
  1749. #endif
  1750. #else
  1751. // Assume no cpuid instruction.
  1752. return false;
  1753. #endif
  1754. }
  1755. bool SystemInformationImplementation::RetrieveCPUFeatures()
  1756. {
  1757. #if USE_CPUID
  1758. int cpuinfo[4] = { 0, 0, 0, 0 };
  1759. if (!call_cpuid(1, cpuinfo)) {
  1760. return false;
  1761. }
  1762. // Retrieve the features of CPU present.
  1763. this->Features.HasFPU =
  1764. ((cpuinfo[3] & 0x00000001) != 0); // FPU Present --> Bit 0
  1765. this->Features.HasTSC =
  1766. ((cpuinfo[3] & 0x00000010) != 0); // TSC Present --> Bit 4
  1767. this->Features.HasAPIC =
  1768. ((cpuinfo[3] & 0x00000200) != 0); // APIC Present --> Bit 9
  1769. this->Features.HasMTRR =
  1770. ((cpuinfo[3] & 0x00001000) != 0); // MTRR Present --> Bit 12
  1771. this->Features.HasCMOV =
  1772. ((cpuinfo[3] & 0x00008000) != 0); // CMOV Present --> Bit 15
  1773. this->Features.HasSerial =
  1774. ((cpuinfo[3] & 0x00040000) != 0); // Serial Present --> Bit 18
  1775. this->Features.HasACPI =
  1776. ((cpuinfo[3] & 0x00400000) != 0); // ACPI Capable --> Bit 22
  1777. this->Features.HasMMX =
  1778. ((cpuinfo[3] & 0x00800000) != 0); // MMX Present --> Bit 23
  1779. this->Features.HasSSE =
  1780. ((cpuinfo[3] & 0x02000000) != 0); // SSE Present --> Bit 25
  1781. this->Features.HasSSE2 =
  1782. ((cpuinfo[3] & 0x04000000) != 0); // SSE2 Present --> Bit 26
  1783. this->Features.HasThermal =
  1784. ((cpuinfo[3] & 0x20000000) != 0); // Thermal Monitor Present --> Bit 29
  1785. this->Features.HasIA64 =
  1786. ((cpuinfo[3] & 0x40000000) != 0); // IA64 Present --> Bit 30
  1787. #if USE_ASM_INSTRUCTIONS
  1788. // Retrieve extended SSE capabilities if SSE is available.
  1789. if (this->Features.HasSSE) {
  1790. // Attempt to __try some SSE FP instructions.
  1791. __try {
  1792. // Perform: orps xmm0, xmm0
  1793. _asm
  1794. {
  1795. _emit 0x0f
  1796. _emit 0x56
  1797. _emit 0xc0
  1798. }
  1799. // SSE FP capable processor.
  1800. this->Features.HasSSEFP = true;
  1801. } __except (1) {
  1802. // bad instruction - processor or OS cannot handle SSE FP.
  1803. this->Features.HasSSEFP = false;
  1804. }
  1805. } else {
  1806. // Set the advanced SSE capabilities to not available.
  1807. this->Features.HasSSEFP = false;
  1808. }
  1809. #else
  1810. this->Features.HasSSEFP = false;
  1811. #endif
  1812. // Retrieve Intel specific extended features.
  1813. if (this->ChipManufacturer == Intel) {
  1814. bool SupportsSMT =
  1815. ((cpuinfo[3] & 0x10000000) != 0); // Intel specific: SMT --> Bit 28
  1816. if ((SupportsSMT) && (this->Features.HasAPIC)) {
  1817. // Retrieve APIC information if there is one present.
  1818. this->Features.ExtendedFeatures.APIC_ID =
  1819. ((cpuinfo[1] & 0xFF000000) >> 24);
  1820. }
  1821. }
  1822. return true;
  1823. #else
  1824. return false;
  1825. #endif
  1826. }
  1827. /** Find the manufacturer given the vendor id */
  1828. void SystemInformationImplementation::FindManufacturer(
  1829. const std::string& family)
  1830. {
  1831. if (this->ChipID.Vendor == "GenuineIntel")
  1832. this->ChipManufacturer = Intel; // Intel Corp.
  1833. else if (this->ChipID.Vendor == "UMC UMC UMC ")
  1834. this->ChipManufacturer = UMC; // United Microelectronics Corp.
  1835. else if (this->ChipID.Vendor == "AuthenticAMD")
  1836. this->ChipManufacturer = AMD; // Advanced Micro Devices
  1837. else if (this->ChipID.Vendor == "AMD ISBETTER")
  1838. this->ChipManufacturer = AMD; // Advanced Micro Devices (1994)
  1839. else if (this->ChipID.Vendor == "CyrixInstead")
  1840. this->ChipManufacturer = Cyrix; // Cyrix Corp., VIA Inc.
  1841. else if (this->ChipID.Vendor == "NexGenDriven")
  1842. this->ChipManufacturer = NexGen; // NexGen Inc. (now AMD)
  1843. else if (this->ChipID.Vendor == "CentaurHauls")
  1844. this->ChipManufacturer = IDT; // IDT/Centaur (now VIA)
  1845. else if (this->ChipID.Vendor == "RiseRiseRise")
  1846. this->ChipManufacturer = Rise; // Rise
  1847. else if (this->ChipID.Vendor == "GenuineTMx86")
  1848. this->ChipManufacturer = Transmeta; // Transmeta
  1849. else if (this->ChipID.Vendor == "TransmetaCPU")
  1850. this->ChipManufacturer = Transmeta; // Transmeta
  1851. else if (this->ChipID.Vendor == "Geode By NSC")
  1852. this->ChipManufacturer = NSC; // National Semiconductor
  1853. else if (this->ChipID.Vendor == "Sun")
  1854. this->ChipManufacturer = Sun; // Sun Microelectronics
  1855. else if (this->ChipID.Vendor == "IBM")
  1856. this->ChipManufacturer = IBM; // IBM Microelectronics
  1857. else if (this->ChipID.Vendor == "Hewlett-Packard")
  1858. this->ChipManufacturer = HP; // Hewlett-Packard
  1859. else if (this->ChipID.Vendor == "Motorola")
  1860. this->ChipManufacturer = Motorola; // Motorola Microelectronics
  1861. else if (family.substr(0, 7) == "PA-RISC")
  1862. this->ChipManufacturer = HP; // Hewlett-Packard
  1863. else
  1864. this->ChipManufacturer = UnknownManufacturer; // Unknown manufacturer
  1865. }
  1866. /** */
  1867. bool SystemInformationImplementation::RetrieveCPUIdentity()
  1868. {
  1869. #if USE_CPUID
  1870. int localCPUVendor[4];
  1871. int localCPUSignature[4];
  1872. if (!call_cpuid(0, localCPUVendor)) {
  1873. return false;
  1874. }
  1875. if (!call_cpuid(1, localCPUSignature)) {
  1876. return false;
  1877. }
  1878. // Process the returned information.
  1879. // ; eax = 0 --> eax: maximum value of CPUID instruction.
  1880. // ; ebx: part 1 of 3; CPU signature.
  1881. // ; edx: part 2 of 3; CPU signature.
  1882. // ; ecx: part 3 of 3; CPU signature.
  1883. char vbuf[13];
  1884. memcpy(&(vbuf[0]), &(localCPUVendor[1]), sizeof(int));
  1885. memcpy(&(vbuf[4]), &(localCPUVendor[3]), sizeof(int));
  1886. memcpy(&(vbuf[8]), &(localCPUVendor[2]), sizeof(int));
  1887. vbuf[12] = '\0';
  1888. this->ChipID.Vendor = vbuf;
  1889. // Retrieve the family of CPU present.
  1890. // ; eax = 1 --> eax: CPU ID - bits 31..16 - unused, bits 15..12 - type,
  1891. // bits 11..8 - family, bits 7..4 - model, bits 3..0 - mask revision
  1892. // ; ebx: 31..24 - default APIC ID, 23..16 - logical processor ID,
  1893. // 15..8 - CFLUSH chunk size , 7..0 - brand ID
  1894. // ; edx: CPU feature flags
  1895. this->ChipID.ExtendedFamily =
  1896. ((localCPUSignature[0] & 0x0FF00000) >> 20); // Bits 27..20 Used
  1897. this->ChipID.ExtendedModel =
  1898. ((localCPUSignature[0] & 0x000F0000) >> 16); // Bits 19..16 Used
  1899. this->ChipID.Type =
  1900. ((localCPUSignature[0] & 0x0000F000) >> 12); // Bits 15..12 Used
  1901. this->ChipID.Family =
  1902. ((localCPUSignature[0] & 0x00000F00) >> 8); // Bits 11..8 Used
  1903. this->ChipID.Model =
  1904. ((localCPUSignature[0] & 0x000000F0) >> 4); // Bits 7..4 Used
  1905. this->ChipID.Revision =
  1906. ((localCPUSignature[0] & 0x0000000F) >> 0); // Bits 3..0 Used
  1907. return true;
  1908. #else
  1909. return false;
  1910. #endif
  1911. }
  1912. /** */
  1913. bool SystemInformationImplementation::RetrieveCPUCacheDetails()
  1914. {
  1915. #if USE_CPUID
  1916. int L1Cache[4] = { 0, 0, 0, 0 };
  1917. int L2Cache[4] = { 0, 0, 0, 0 };
  1918. // Check to see if what we are about to do is supported...
  1919. if (RetrieveCPUExtendedLevelSupport(0x80000005)) {
  1920. if (!call_cpuid(0x80000005, L1Cache)) {
  1921. return false;
  1922. }
  1923. // Save the L1 data cache size (in KB) from ecx: bits 31..24 as well as
  1924. // data cache size from edx: bits 31..24.
  1925. this->Features.L1CacheSize = ((L1Cache[2] & 0xFF000000) >> 24);
  1926. this->Features.L1CacheSize += ((L1Cache[3] & 0xFF000000) >> 24);
  1927. } else {
  1928. // Store -1 to indicate the cache could not be queried.
  1929. this->Features.L1CacheSize = -1;
  1930. }
  1931. // Check to see if what we are about to do is supported...
  1932. if (RetrieveCPUExtendedLevelSupport(0x80000006)) {
  1933. if (!call_cpuid(0x80000006, L2Cache)) {
  1934. return false;
  1935. }
  1936. // Save the L2 unified cache size (in KB) from ecx: bits 31..16.
  1937. this->Features.L2CacheSize = ((L2Cache[2] & 0xFFFF0000) >> 16);
  1938. } else {
  1939. // Store -1 to indicate the cache could not be queried.
  1940. this->Features.L2CacheSize = -1;
  1941. }
  1942. // Define L3 as being not present as we cannot test for it.
  1943. this->Features.L3CacheSize = -1;
  1944. #endif
  1945. // Return failure if we cannot detect either cache with this method.
  1946. return ((this->Features.L1CacheSize == -1) &&
  1947. (this->Features.L2CacheSize == -1))
  1948. ? false
  1949. : true;
  1950. }
  1951. /** */
  1952. bool SystemInformationImplementation::RetrieveClassicalCPUCacheDetails()
  1953. {
  1954. #if USE_CPUID
  1955. int TLBCode = -1, TLBData = -1, L1Code = -1, L1Data = -1, L1Trace = -1,
  1956. L2Unified = -1, L3Unified = -1;
  1957. int TLBCacheData[4] = { 0, 0, 0, 0 };
  1958. int TLBPassCounter = 0;
  1959. int TLBCacheUnit = 0;
  1960. do {
  1961. if (!call_cpuid(2, TLBCacheData)) {
  1962. return false;
  1963. }
  1964. int bob = ((TLBCacheData[0] & 0x00FF0000) >> 16);
  1965. (void)bob;
  1966. // Process the returned TLB and cache information.
  1967. for (int nCounter = 0; nCounter < TLBCACHE_INFO_UNITS; nCounter++) {
  1968. // First of all - decide which unit we are dealing with.
  1969. switch (nCounter) {
  1970. // eax: bits 8..15 : bits 16..23 : bits 24..31
  1971. case 0:
  1972. TLBCacheUnit = ((TLBCacheData[0] & 0x0000FF00) >> 8);
  1973. break;
  1974. case 1:
  1975. TLBCacheUnit = ((TLBCacheData[0] & 0x00FF0000) >> 16);
  1976. break;
  1977. case 2:
  1978. TLBCacheUnit = ((TLBCacheData[0] & 0xFF000000) >> 24);
  1979. break;
  1980. // ebx: bits 0..7 : bits 8..15 : bits 16..23 : bits 24..31
  1981. case 3:
  1982. TLBCacheUnit = ((TLBCacheData[1] & 0x000000FF) >> 0);
  1983. break;
  1984. case 4:
  1985. TLBCacheUnit = ((TLBCacheData[1] & 0x0000FF00) >> 8);
  1986. break;
  1987. case 5:
  1988. TLBCacheUnit = ((TLBCacheData[1] & 0x00FF0000) >> 16);
  1989. break;
  1990. case 6:
  1991. TLBCacheUnit = ((TLBCacheData[1] & 0xFF000000) >> 24);
  1992. break;
  1993. // ecx: bits 0..7 : bits 8..15 : bits 16..23 : bits 24..31
  1994. case 7:
  1995. TLBCacheUnit = ((TLBCacheData[2] & 0x000000FF) >> 0);
  1996. break;
  1997. case 8:
  1998. TLBCacheUnit = ((TLBCacheData[2] & 0x0000FF00) >> 8);
  1999. break;
  2000. case 9:
  2001. TLBCacheUnit = ((TLBCacheData[2] & 0x00FF0000) >> 16);
  2002. break;
  2003. case 10:
  2004. TLBCacheUnit = ((TLBCacheData[2] & 0xFF000000) >> 24);
  2005. break;
  2006. // edx: bits 0..7 : bits 8..15 : bits 16..23 : bits 24..31
  2007. case 11:
  2008. TLBCacheUnit = ((TLBCacheData[3] & 0x000000FF) >> 0);
  2009. break;
  2010. case 12:
  2011. TLBCacheUnit = ((TLBCacheData[3] & 0x0000FF00) >> 8);
  2012. break;
  2013. case 13:
  2014. TLBCacheUnit = ((TLBCacheData[3] & 0x00FF0000) >> 16);
  2015. break;
  2016. case 14:
  2017. TLBCacheUnit = ((TLBCacheData[3] & 0xFF000000) >> 24);
  2018. break;
  2019. // Default case - an error has occurred.
  2020. default:
  2021. return false;
  2022. }
  2023. // Now process the resulting unit to see what it means....
  2024. switch (TLBCacheUnit) {
  2025. case 0x00:
  2026. break;
  2027. case 0x01:
  2028. STORE_TLBCACHE_INFO(TLBCode, 4);
  2029. break;
  2030. case 0x02:
  2031. STORE_TLBCACHE_INFO(TLBCode, 4096);
  2032. break;
  2033. case 0x03:
  2034. STORE_TLBCACHE_INFO(TLBData, 4);
  2035. break;
  2036. case 0x04:
  2037. STORE_TLBCACHE_INFO(TLBData, 4096);
  2038. break;
  2039. case 0x06:
  2040. STORE_TLBCACHE_INFO(L1Code, 8);
  2041. break;
  2042. case 0x08:
  2043. STORE_TLBCACHE_INFO(L1Code, 16);
  2044. break;
  2045. case 0x0a:
  2046. STORE_TLBCACHE_INFO(L1Data, 8);
  2047. break;
  2048. case 0x0c:
  2049. STORE_TLBCACHE_INFO(L1Data, 16);
  2050. break;
  2051. case 0x10:
  2052. STORE_TLBCACHE_INFO(L1Data, 16);
  2053. break; // <-- FIXME: IA-64 Only
  2054. case 0x15:
  2055. STORE_TLBCACHE_INFO(L1Code, 16);
  2056. break; // <-- FIXME: IA-64 Only
  2057. case 0x1a:
  2058. STORE_TLBCACHE_INFO(L2Unified, 96);
  2059. break; // <-- FIXME: IA-64 Only
  2060. case 0x22:
  2061. STORE_TLBCACHE_INFO(L3Unified, 512);
  2062. break;
  2063. case 0x23:
  2064. STORE_TLBCACHE_INFO(L3Unified, 1024);
  2065. break;
  2066. case 0x25:
  2067. STORE_TLBCACHE_INFO(L3Unified, 2048);
  2068. break;
  2069. case 0x29:
  2070. STORE_TLBCACHE_INFO(L3Unified, 4096);
  2071. break;
  2072. case 0x39:
  2073. STORE_TLBCACHE_INFO(L2Unified, 128);
  2074. break;
  2075. case 0x3c:
  2076. STORE_TLBCACHE_INFO(L2Unified, 256);
  2077. break;
  2078. case 0x40:
  2079. STORE_TLBCACHE_INFO(L2Unified, 0);
  2080. break; // <-- FIXME: No integrated L2 cache (P6 core) or L3 cache (P4
  2081. // core).
  2082. case 0x41:
  2083. STORE_TLBCACHE_INFO(L2Unified, 128);
  2084. break;
  2085. case 0x42:
  2086. STORE_TLBCACHE_INFO(L2Unified, 256);
  2087. break;
  2088. case 0x43:
  2089. STORE_TLBCACHE_INFO(L2Unified, 512);
  2090. break;
  2091. case 0x44:
  2092. STORE_TLBCACHE_INFO(L2Unified, 1024);
  2093. break;
  2094. case 0x45:
  2095. STORE_TLBCACHE_INFO(L2Unified, 2048);
  2096. break;
  2097. case 0x50:
  2098. STORE_TLBCACHE_INFO(TLBCode, 4096);
  2099. break;
  2100. case 0x51:
  2101. STORE_TLBCACHE_INFO(TLBCode, 4096);
  2102. break;
  2103. case 0x52:
  2104. STORE_TLBCACHE_INFO(TLBCode, 4096);
  2105. break;
  2106. case 0x5b:
  2107. STORE_TLBCACHE_INFO(TLBData, 4096);
  2108. break;
  2109. case 0x5c:
  2110. STORE_TLBCACHE_INFO(TLBData, 4096);
  2111. break;
  2112. case 0x5d:
  2113. STORE_TLBCACHE_INFO(TLBData, 4096);
  2114. break;
  2115. case 0x66:
  2116. STORE_TLBCACHE_INFO(L1Data, 8);
  2117. break;
  2118. case 0x67:
  2119. STORE_TLBCACHE_INFO(L1Data, 16);
  2120. break;
  2121. case 0x68:
  2122. STORE_TLBCACHE_INFO(L1Data, 32);
  2123. break;
  2124. case 0x70:
  2125. STORE_TLBCACHE_INFO(L1Trace, 12);
  2126. break;
  2127. case 0x71:
  2128. STORE_TLBCACHE_INFO(L1Trace, 16);
  2129. break;
  2130. case 0x72:
  2131. STORE_TLBCACHE_INFO(L1Trace, 32);
  2132. break;
  2133. case 0x77:
  2134. STORE_TLBCACHE_INFO(L1Code, 16);
  2135. break; // <-- FIXME: IA-64 Only
  2136. case 0x79:
  2137. STORE_TLBCACHE_INFO(L2Unified, 128);
  2138. break;
  2139. case 0x7a:
  2140. STORE_TLBCACHE_INFO(L2Unified, 256);
  2141. break;
  2142. case 0x7b:
  2143. STORE_TLBCACHE_INFO(L2Unified, 512);
  2144. break;
  2145. case 0x7c:
  2146. STORE_TLBCACHE_INFO(L2Unified, 1024);
  2147. break;
  2148. case 0x7e:
  2149. STORE_TLBCACHE_INFO(L2Unified, 256);
  2150. break;
  2151. case 0x81:
  2152. STORE_TLBCACHE_INFO(L2Unified, 128);
  2153. break;
  2154. case 0x82:
  2155. STORE_TLBCACHE_INFO(L2Unified, 256);
  2156. break;
  2157. case 0x83:
  2158. STORE_TLBCACHE_INFO(L2Unified, 512);
  2159. break;
  2160. case 0x84:
  2161. STORE_TLBCACHE_INFO(L2Unified, 1024);
  2162. break;
  2163. case 0x85:
  2164. STORE_TLBCACHE_INFO(L2Unified, 2048);
  2165. break;
  2166. case 0x88:
  2167. STORE_TLBCACHE_INFO(L3Unified, 2048);
  2168. break; // <-- FIXME: IA-64 Only
  2169. case 0x89:
  2170. STORE_TLBCACHE_INFO(L3Unified, 4096);
  2171. break; // <-- FIXME: IA-64 Only
  2172. case 0x8a:
  2173. STORE_TLBCACHE_INFO(L3Unified, 8192);
  2174. break; // <-- FIXME: IA-64 Only
  2175. case 0x8d:
  2176. STORE_TLBCACHE_INFO(L3Unified, 3096);
  2177. break; // <-- FIXME: IA-64 Only
  2178. case 0x90:
  2179. STORE_TLBCACHE_INFO(TLBCode, 262144);
  2180. break; // <-- FIXME: IA-64 Only
  2181. case 0x96:
  2182. STORE_TLBCACHE_INFO(TLBCode, 262144);
  2183. break; // <-- FIXME: IA-64 Only
  2184. case 0x9b:
  2185. STORE_TLBCACHE_INFO(TLBCode, 262144);
  2186. break; // <-- FIXME: IA-64 Only
  2187. // Default case - an error has occurred.
  2188. default:
  2189. return false;
  2190. }
  2191. }
  2192. // Increment the TLB pass counter.
  2193. TLBPassCounter++;
  2194. } while ((TLBCacheData[0] & 0x000000FF) > TLBPassCounter);
  2195. // Ok - we now have the maximum TLB, L1, L2, and L3 sizes...
  2196. if ((L1Code == -1) && (L1Data == -1) && (L1Trace == -1)) {
  2197. this->Features.L1CacheSize = -1;
  2198. } else if ((L1Code == -1) && (L1Data == -1) && (L1Trace != -1)) {
  2199. this->Features.L1CacheSize = L1Trace;
  2200. } else if ((L1Code != -1) && (L1Data == -1)) {
  2201. this->Features.L1CacheSize = L1Code;
  2202. } else if ((L1Code == -1) && (L1Data != -1)) {
  2203. this->Features.L1CacheSize = L1Data;
  2204. } else if ((L1Code != -1) && (L1Data != -1)) {
  2205. this->Features.L1CacheSize = L1Code + L1Data;
  2206. } else {
  2207. this->Features.L1CacheSize = -1;
  2208. }
  2209. // Ok - we now have the maximum TLB, L1, L2, and L3 sizes...
  2210. if (L2Unified == -1) {
  2211. this->Features.L2CacheSize = -1;
  2212. } else {
  2213. this->Features.L2CacheSize = L2Unified;
  2214. }
  2215. // Ok - we now have the maximum TLB, L1, L2, and L3 sizes...
  2216. if (L3Unified == -1) {
  2217. this->Features.L3CacheSize = -1;
  2218. } else {
  2219. this->Features.L3CacheSize = L3Unified;
  2220. }
  2221. return true;
  2222. #else
  2223. return false;
  2224. #endif
  2225. }
  2226. /** */
  2227. bool SystemInformationImplementation::RetrieveCPUClockSpeed()
  2228. {
  2229. bool retrieved = false;
  2230. #if defined(_WIN32)
  2231. unsigned int uiRepetitions = 1;
  2232. unsigned int uiMSecPerRepetition = 50;
  2233. __int64 i64Total = 0;
  2234. __int64 i64Overhead = 0;
  2235. // Check if the TSC implementation works at all
  2236. if (this->Features.HasTSC &&
  2237. GetCyclesDifference(SystemInformationImplementation::Delay,
  2238. uiMSecPerRepetition) > 0) {
  2239. for (unsigned int nCounter = 0; nCounter < uiRepetitions; nCounter++) {
  2240. i64Total += GetCyclesDifference(SystemInformationImplementation::Delay,
  2241. uiMSecPerRepetition);
  2242. i64Overhead += GetCyclesDifference(
  2243. SystemInformationImplementation::DelayOverhead, uiMSecPerRepetition);
  2244. }
  2245. // Calculate the MHz speed.
  2246. i64Total -= i64Overhead;
  2247. i64Total /= uiRepetitions;
  2248. i64Total /= uiMSecPerRepetition;
  2249. i64Total /= 1000;
  2250. // Save the CPU speed.
  2251. this->CPUSpeedInMHz = (float)i64Total;
  2252. retrieved = true;
  2253. }
  2254. // If RDTSC is not supported, we fallback to trying to read this value
  2255. // from the registry:
  2256. if (!retrieved) {
  2257. HKEY hKey = NULL;
  2258. LONG err =
  2259. RegOpenKeyExW(HKEY_LOCAL_MACHINE,
  2260. L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0,
  2261. KEY_READ, &hKey);
  2262. if (ERROR_SUCCESS == err) {
  2263. DWORD dwType = 0;
  2264. DWORD data = 0;
  2265. DWORD dwSize = sizeof(DWORD);
  2266. err =
  2267. RegQueryValueExW(hKey, L"~MHz", 0, &dwType, (LPBYTE)&data, &dwSize);
  2268. if (ERROR_SUCCESS == err) {
  2269. this->CPUSpeedInMHz = (float)data;
  2270. retrieved = true;
  2271. }
  2272. RegCloseKey(hKey);
  2273. hKey = NULL;
  2274. }
  2275. }
  2276. #endif
  2277. return retrieved;
  2278. }
  2279. /** */
  2280. bool SystemInformationImplementation::RetrieveClassicalCPUClockSpeed()
  2281. {
  2282. #if USE_ASM_INSTRUCTIONS
  2283. LARGE_INTEGER liStart, liEnd, liCountsPerSecond;
  2284. double dFrequency, dDifference;
  2285. // Attempt to get a starting tick count.
  2286. QueryPerformanceCounter(&liStart);
  2287. __try {
  2288. _asm {
  2289. mov eax, 0x80000000
  2290. mov ebx, CLASSICAL_CPU_FREQ_LOOP
  2291. Timer_Loop:
  2292. bsf ecx,eax
  2293. dec ebx
  2294. jnz Timer_Loop
  2295. }
  2296. } __except (1) {
  2297. return false;
  2298. }
  2299. // Attempt to get a starting tick count.
  2300. QueryPerformanceCounter(&liEnd);
  2301. // Get the difference... NB: This is in seconds....
  2302. QueryPerformanceFrequency(&liCountsPerSecond);
  2303. dDifference = (((double)liEnd.QuadPart - (double)liStart.QuadPart) /
  2304. (double)liCountsPerSecond.QuadPart);
  2305. // Calculate the clock speed.
  2306. if (this->ChipID.Family == 3) {
  2307. // 80386 processors.... Loop time is 115 cycles!
  2308. dFrequency = (((CLASSICAL_CPU_FREQ_LOOP * 115) / dDifference) / 1000000);
  2309. } else if (this->ChipID.Family == 4) {
  2310. // 80486 processors.... Loop time is 47 cycles!
  2311. dFrequency = (((CLASSICAL_CPU_FREQ_LOOP * 47) / dDifference) / 1000000);
  2312. } else if (this->ChipID.Family == 5) {
  2313. // Pentium processors.... Loop time is 43 cycles!
  2314. dFrequency = (((CLASSICAL_CPU_FREQ_LOOP * 43) / dDifference) / 1000000);
  2315. }
  2316. // Save the clock speed.
  2317. this->Features.CPUSpeed = (int)dFrequency;
  2318. return true;
  2319. #else
  2320. return false;
  2321. #endif
  2322. }
  2323. /** */
  2324. bool SystemInformationImplementation::RetrieveCPUExtendedLevelSupport(
  2325. int CPULevelToCheck)
  2326. {
  2327. int cpuinfo[4] = { 0, 0, 0, 0 };
  2328. // The extended CPUID is supported by various vendors starting with the
  2329. // following CPU models:
  2330. //
  2331. // Manufacturer & Chip Name | Family Model Revision
  2332. //
  2333. // AMD K6, K6-2 | 5 6 x
  2334. // Cyrix GXm, Cyrix III "Joshua" | 5 4 x
  2335. // IDT C6-2 | 5 8 x
  2336. // VIA Cyrix III | 6 5 x
  2337. // Transmeta Crusoe | 5 x x
  2338. // Intel Pentium 4 | f x x
  2339. //
  2340. // We check to see if a supported processor is present...
  2341. if (this->ChipManufacturer == AMD) {
  2342. if (this->ChipID.Family < 5)
  2343. return false;
  2344. if ((this->ChipID.Family == 5) && (this->ChipID.Model < 6))
  2345. return false;
  2346. } else if (this->ChipManufacturer == Cyrix) {
  2347. if (this->ChipID.Family < 5)
  2348. return false;
  2349. if ((this->ChipID.Family == 5) && (this->ChipID.Model < 4))
  2350. return false;
  2351. if ((this->ChipID.Family == 6) && (this->ChipID.Model < 5))
  2352. return false;
  2353. } else if (this->ChipManufacturer == IDT) {
  2354. if (this->ChipID.Family < 5)
  2355. return false;
  2356. if ((this->ChipID.Family == 5) && (this->ChipID.Model < 8))
  2357. return false;
  2358. } else if (this->ChipManufacturer == Transmeta) {
  2359. if (this->ChipID.Family < 5)
  2360. return false;
  2361. } else if (this->ChipManufacturer == Intel) {
  2362. if (this->ChipID.Family < 0xf) {
  2363. return false;
  2364. }
  2365. }
  2366. #if USE_CPUID
  2367. if (!call_cpuid(0x80000000, cpuinfo)) {
  2368. return false;
  2369. }
  2370. #endif
  2371. // Now we have to check the level wanted vs level returned...
  2372. int nLevelWanted = (CPULevelToCheck & 0x7FFFFFFF);
  2373. int nLevelReturn = (cpuinfo[0] & 0x7FFFFFFF);
  2374. // Check to see if the level provided is supported...
  2375. if (nLevelWanted > nLevelReturn) {
  2376. return false;
  2377. }
  2378. return true;
  2379. }
  2380. /** */
  2381. bool SystemInformationImplementation::RetrieveExtendedCPUFeatures()
  2382. {
  2383. // Check that we are not using an Intel processor as it does not support
  2384. // this.
  2385. if (this->ChipManufacturer == Intel) {
  2386. return false;
  2387. }
  2388. // Check to see if what we are about to do is supported...
  2389. if (!RetrieveCPUExtendedLevelSupport(static_cast<int>(0x80000001))) {
  2390. return false;
  2391. }
  2392. #if USE_CPUID
  2393. int localCPUExtendedFeatures[4] = { 0, 0, 0, 0 };
  2394. if (!call_cpuid(0x80000001, localCPUExtendedFeatures)) {
  2395. return false;
  2396. }
  2397. // Retrieve the extended features of CPU present.
  2398. this->Features.ExtendedFeatures.Has3DNow =
  2399. ((localCPUExtendedFeatures[3] & 0x80000000) !=
  2400. 0); // 3DNow Present --> Bit 31.
  2401. this->Features.ExtendedFeatures.Has3DNowPlus =
  2402. ((localCPUExtendedFeatures[3] & 0x40000000) !=
  2403. 0); // 3DNow+ Present -- > Bit 30.
  2404. this->Features.ExtendedFeatures.HasSSEMMX =
  2405. ((localCPUExtendedFeatures[3] & 0x00400000) !=
  2406. 0); // SSE MMX Present --> Bit 22.
  2407. this->Features.ExtendedFeatures.SupportsMP =
  2408. ((localCPUExtendedFeatures[3] & 0x00080000) !=
  2409. 0); // MP Capable -- > Bit 19.
  2410. // Retrieve AMD specific extended features.
  2411. if (this->ChipManufacturer == AMD) {
  2412. this->Features.ExtendedFeatures.HasMMXPlus =
  2413. ((localCPUExtendedFeatures[3] & 0x00400000) !=
  2414. 0); // AMD specific: MMX-SSE --> Bit 22
  2415. }
  2416. // Retrieve Cyrix specific extended features.
  2417. if (this->ChipManufacturer == Cyrix) {
  2418. this->Features.ExtendedFeatures.HasMMXPlus =
  2419. ((localCPUExtendedFeatures[3] & 0x01000000) !=
  2420. 0); // Cyrix specific: Extended MMX --> Bit 24
  2421. }
  2422. return true;
  2423. #else
  2424. return false;
  2425. #endif
  2426. }
  2427. /** */
  2428. bool SystemInformationImplementation::RetrieveProcessorSerialNumber()
  2429. {
  2430. // Check to see if the processor supports the processor serial number.
  2431. if (!this->Features.HasSerial) {
  2432. return false;
  2433. }
  2434. #if USE_CPUID
  2435. int SerialNumber[4];
  2436. if (!call_cpuid(3, SerialNumber)) {
  2437. return false;
  2438. }
  2439. // Process the returned information.
  2440. // ; eax = 3 --> ebx: top 32 bits are the processor signature bits --> NB:
  2441. // Transmeta only ?!?
  2442. // ; ecx: middle 32 bits are the processor signature bits
  2443. // ; edx: bottom 32 bits are the processor signature bits
  2444. char sn[128];
  2445. sprintf(sn, "%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x",
  2446. ((SerialNumber[1] & 0xff000000) >> 24),
  2447. ((SerialNumber[1] & 0x00ff0000) >> 16),
  2448. ((SerialNumber[1] & 0x0000ff00) >> 8),
  2449. ((SerialNumber[1] & 0x000000ff) >> 0),
  2450. ((SerialNumber[2] & 0xff000000) >> 24),
  2451. ((SerialNumber[2] & 0x00ff0000) >> 16),
  2452. ((SerialNumber[2] & 0x0000ff00) >> 8),
  2453. ((SerialNumber[2] & 0x000000ff) >> 0),
  2454. ((SerialNumber[3] & 0xff000000) >> 24),
  2455. ((SerialNumber[3] & 0x00ff0000) >> 16),
  2456. ((SerialNumber[3] & 0x0000ff00) >> 8),
  2457. ((SerialNumber[3] & 0x000000ff) >> 0));
  2458. this->ChipID.SerialNumber = sn;
  2459. return true;
  2460. #else
  2461. return false;
  2462. #endif
  2463. }
  2464. /** */
  2465. bool SystemInformationImplementation::RetrieveCPUPowerManagement()
  2466. {
  2467. // Check to see if what we are about to do is supported...
  2468. if (!RetrieveCPUExtendedLevelSupport(static_cast<int>(0x80000007))) {
  2469. this->Features.ExtendedFeatures.PowerManagement.HasFrequencyID = false;
  2470. this->Features.ExtendedFeatures.PowerManagement.HasVoltageID = false;
  2471. this->Features.ExtendedFeatures.PowerManagement.HasTempSenseDiode = false;
  2472. return false;
  2473. }
  2474. #if USE_CPUID
  2475. int localCPUPowerManagement[4] = { 0, 0, 0, 0 };
  2476. if (!call_cpuid(0x80000007, localCPUPowerManagement)) {
  2477. return false;
  2478. }
  2479. // Check for the power management capabilities of the CPU.
  2480. this->Features.ExtendedFeatures.PowerManagement.HasTempSenseDiode =
  2481. ((localCPUPowerManagement[3] & 0x00000001) != 0);
  2482. this->Features.ExtendedFeatures.PowerManagement.HasFrequencyID =
  2483. ((localCPUPowerManagement[3] & 0x00000002) != 0);
  2484. this->Features.ExtendedFeatures.PowerManagement.HasVoltageID =
  2485. ((localCPUPowerManagement[3] & 0x00000004) != 0);
  2486. return true;
  2487. #else
  2488. return false;
  2489. #endif
  2490. }
  2491. #if USE_CPUID
  2492. // Used only in USE_CPUID implementation below.
  2493. static void SystemInformationStripLeadingSpace(std::string& str)
  2494. {
  2495. // Because some manufacturers have leading white space - we have to
  2496. // post-process the name.
  2497. std::string::size_type pos = str.find_first_not_of(" ");
  2498. if (pos != std::string::npos) {
  2499. str = str.substr(pos);
  2500. }
  2501. }
  2502. #endif
  2503. /** */
  2504. bool SystemInformationImplementation::RetrieveExtendedCPUIdentity()
  2505. {
  2506. // Check to see if what we are about to do is supported...
  2507. if (!RetrieveCPUExtendedLevelSupport(static_cast<int>(0x80000002)))
  2508. return false;
  2509. if (!RetrieveCPUExtendedLevelSupport(static_cast<int>(0x80000003)))
  2510. return false;
  2511. if (!RetrieveCPUExtendedLevelSupport(static_cast<int>(0x80000004)))
  2512. return false;
  2513. #if USE_CPUID
  2514. int CPUExtendedIdentity[12];
  2515. if (!call_cpuid(0x80000002, CPUExtendedIdentity)) {
  2516. return false;
  2517. }
  2518. if (!call_cpuid(0x80000003, CPUExtendedIdentity + 4)) {
  2519. return false;
  2520. }
  2521. if (!call_cpuid(0x80000004, CPUExtendedIdentity + 8)) {
  2522. return false;
  2523. }
  2524. // Process the returned information.
  2525. char nbuf[49];
  2526. memcpy(&(nbuf[0]), &(CPUExtendedIdentity[0]), sizeof(int));
  2527. memcpy(&(nbuf[4]), &(CPUExtendedIdentity[1]), sizeof(int));
  2528. memcpy(&(nbuf[8]), &(CPUExtendedIdentity[2]), sizeof(int));
  2529. memcpy(&(nbuf[12]), &(CPUExtendedIdentity[3]), sizeof(int));
  2530. memcpy(&(nbuf[16]), &(CPUExtendedIdentity[4]), sizeof(int));
  2531. memcpy(&(nbuf[20]), &(CPUExtendedIdentity[5]), sizeof(int));
  2532. memcpy(&(nbuf[24]), &(CPUExtendedIdentity[6]), sizeof(int));
  2533. memcpy(&(nbuf[28]), &(CPUExtendedIdentity[7]), sizeof(int));
  2534. memcpy(&(nbuf[32]), &(CPUExtendedIdentity[8]), sizeof(int));
  2535. memcpy(&(nbuf[36]), &(CPUExtendedIdentity[9]), sizeof(int));
  2536. memcpy(&(nbuf[40]), &(CPUExtendedIdentity[10]), sizeof(int));
  2537. memcpy(&(nbuf[44]), &(CPUExtendedIdentity[11]), sizeof(int));
  2538. nbuf[48] = '\0';
  2539. this->ChipID.ProcessorName = nbuf;
  2540. this->ChipID.ModelName = nbuf;
  2541. // Because some manufacturers have leading white space - we have to
  2542. // post-process the name.
  2543. SystemInformationStripLeadingSpace(this->ChipID.ProcessorName);
  2544. return true;
  2545. #else
  2546. return false;
  2547. #endif
  2548. }
  2549. /** */
  2550. bool SystemInformationImplementation::RetrieveClassicalCPUIdentity()
  2551. {
  2552. // Start by decided which manufacturer we are using....
  2553. switch (this->ChipManufacturer) {
  2554. case Intel:
  2555. // Check the family / model / revision to determine the CPU ID.
  2556. switch (this->ChipID.Family) {
  2557. case 3:
  2558. this->ChipID.ProcessorName = "Newer i80386 family";
  2559. break;
  2560. case 4:
  2561. switch (this->ChipID.Model) {
  2562. case 0:
  2563. this->ChipID.ProcessorName = "i80486DX-25/33";
  2564. break;
  2565. case 1:
  2566. this->ChipID.ProcessorName = "i80486DX-50";
  2567. break;
  2568. case 2:
  2569. this->ChipID.ProcessorName = "i80486SX";
  2570. break;
  2571. case 3:
  2572. this->ChipID.ProcessorName = "i80486DX2";
  2573. break;
  2574. case 4:
  2575. this->ChipID.ProcessorName = "i80486SL";
  2576. break;
  2577. case 5:
  2578. this->ChipID.ProcessorName = "i80486SX2";
  2579. break;
  2580. case 7:
  2581. this->ChipID.ProcessorName = "i80486DX2 WriteBack";
  2582. break;
  2583. case 8:
  2584. this->ChipID.ProcessorName = "i80486DX4";
  2585. break;
  2586. case 9:
  2587. this->ChipID.ProcessorName = "i80486DX4 WriteBack";
  2588. break;
  2589. default:
  2590. this->ChipID.ProcessorName = "Unknown 80486 family";
  2591. return false;
  2592. }
  2593. break;
  2594. case 5:
  2595. switch (this->ChipID.Model) {
  2596. case 0:
  2597. this->ChipID.ProcessorName = "P5 A-Step";
  2598. break;
  2599. case 1:
  2600. this->ChipID.ProcessorName = "P5";
  2601. break;
  2602. case 2:
  2603. this->ChipID.ProcessorName = "P54C";
  2604. break;
  2605. case 3:
  2606. this->ChipID.ProcessorName = "P24T OverDrive";
  2607. break;
  2608. case 4:
  2609. this->ChipID.ProcessorName = "P55C";
  2610. break;
  2611. case 7:
  2612. this->ChipID.ProcessorName = "P54C";
  2613. break;
  2614. case 8:
  2615. this->ChipID.ProcessorName = "P55C (0.25micron)";
  2616. break;
  2617. default:
  2618. this->ChipID.ProcessorName = "Unknown Pentium family";
  2619. return false;
  2620. }
  2621. break;
  2622. case 6:
  2623. switch (this->ChipID.Model) {
  2624. case 0:
  2625. this->ChipID.ProcessorName = "P6 A-Step";
  2626. break;
  2627. case 1:
  2628. this->ChipID.ProcessorName = "P6";
  2629. break;
  2630. case 3:
  2631. this->ChipID.ProcessorName = "Pentium II (0.28 micron)";
  2632. break;
  2633. case 5:
  2634. this->ChipID.ProcessorName = "Pentium II (0.25 micron)";
  2635. break;
  2636. case 6:
  2637. this->ChipID.ProcessorName = "Pentium II With On-Die L2 Cache";
  2638. break;
  2639. case 7:
  2640. this->ChipID.ProcessorName = "Pentium III (0.25 micron)";
  2641. break;
  2642. case 8:
  2643. this->ChipID.ProcessorName =
  2644. "Pentium III (0.18 micron) With 256 KB On-Die L2 Cache ";
  2645. break;
  2646. case 0xa:
  2647. this->ChipID.ProcessorName =
  2648. "Pentium III (0.18 micron) With 1 Or 2 MB On-Die L2 Cache ";
  2649. break;
  2650. case 0xb:
  2651. this->ChipID.ProcessorName = "Pentium III (0.13 micron) With "
  2652. "256 Or 512 KB On-Die L2 Cache ";
  2653. break;
  2654. case 23:
  2655. this->ChipID.ProcessorName =
  2656. "Intel(R) Core(TM)2 Duo CPU T9500 @ 2.60GHz";
  2657. break;
  2658. default:
  2659. this->ChipID.ProcessorName = "Unknown P6 family";
  2660. return false;
  2661. }
  2662. break;
  2663. case 7:
  2664. this->ChipID.ProcessorName = "Intel Merced (IA-64)";
  2665. break;
  2666. case 0xf:
  2667. // Check the extended family bits...
  2668. switch (this->ChipID.ExtendedFamily) {
  2669. case 0:
  2670. switch (this->ChipID.Model) {
  2671. case 0:
  2672. this->ChipID.ProcessorName = "Pentium IV (0.18 micron)";
  2673. break;
  2674. case 1:
  2675. this->ChipID.ProcessorName = "Pentium IV (0.18 micron)";
  2676. break;
  2677. case 2:
  2678. this->ChipID.ProcessorName = "Pentium IV (0.13 micron)";
  2679. break;
  2680. default:
  2681. this->ChipID.ProcessorName = "Unknown Pentium 4 family";
  2682. return false;
  2683. }
  2684. break;
  2685. case 1:
  2686. this->ChipID.ProcessorName = "Intel McKinley (IA-64)";
  2687. break;
  2688. default:
  2689. this->ChipID.ProcessorName = "Pentium";
  2690. }
  2691. break;
  2692. default:
  2693. this->ChipID.ProcessorName = "Unknown Intel family";
  2694. return false;
  2695. }
  2696. break;
  2697. case AMD:
  2698. // Check the family / model / revision to determine the CPU ID.
  2699. switch (this->ChipID.Family) {
  2700. case 4:
  2701. switch (this->ChipID.Model) {
  2702. case 3:
  2703. this->ChipID.ProcessorName = "80486DX2";
  2704. break;
  2705. case 7:
  2706. this->ChipID.ProcessorName = "80486DX2 WriteBack";
  2707. break;
  2708. case 8:
  2709. this->ChipID.ProcessorName = "80486DX4";
  2710. break;
  2711. case 9:
  2712. this->ChipID.ProcessorName = "80486DX4 WriteBack";
  2713. break;
  2714. case 0xe:
  2715. this->ChipID.ProcessorName = "5x86";
  2716. break;
  2717. case 0xf:
  2718. this->ChipID.ProcessorName = "5x86WB";
  2719. break;
  2720. default:
  2721. this->ChipID.ProcessorName = "Unknown 80486 family";
  2722. return false;
  2723. }
  2724. break;
  2725. case 5:
  2726. switch (this->ChipID.Model) {
  2727. case 0:
  2728. this->ChipID.ProcessorName = "SSA5 (PR75, PR90 = PR100)";
  2729. break;
  2730. case 1:
  2731. this->ChipID.ProcessorName = "5k86 (PR120 = PR133)";
  2732. break;
  2733. case 2:
  2734. this->ChipID.ProcessorName = "5k86 (PR166)";
  2735. break;
  2736. case 3:
  2737. this->ChipID.ProcessorName = "5k86 (PR200)";
  2738. break;
  2739. case 6:
  2740. this->ChipID.ProcessorName = "K6 (0.30 micron)";
  2741. break;
  2742. case 7:
  2743. this->ChipID.ProcessorName = "K6 (0.25 micron)";
  2744. break;
  2745. case 8:
  2746. this->ChipID.ProcessorName = "K6-2";
  2747. break;
  2748. case 9:
  2749. this->ChipID.ProcessorName = "K6-III";
  2750. break;
  2751. case 0xd:
  2752. this->ChipID.ProcessorName = "K6-2+ or K6-III+ (0.18 micron)";
  2753. break;
  2754. default:
  2755. this->ChipID.ProcessorName = "Unknown 80586 family";
  2756. return false;
  2757. }
  2758. break;
  2759. case 6:
  2760. switch (this->ChipID.Model) {
  2761. case 1:
  2762. this->ChipID.ProcessorName = "Athlon- (0.25 micron)";
  2763. break;
  2764. case 2:
  2765. this->ChipID.ProcessorName = "Athlon- (0.18 micron)";
  2766. break;
  2767. case 3:
  2768. this->ChipID.ProcessorName = "Duron- (SF core)";
  2769. break;
  2770. case 4:
  2771. this->ChipID.ProcessorName = "Athlon- (Thunderbird core)";
  2772. break;
  2773. case 6:
  2774. this->ChipID.ProcessorName = "Athlon- (Palomino core)";
  2775. break;
  2776. case 7:
  2777. this->ChipID.ProcessorName = "Duron- (Morgan core)";
  2778. break;
  2779. case 8:
  2780. if (this->Features.ExtendedFeatures.SupportsMP)
  2781. this->ChipID.ProcessorName = "Athlon - MP (Thoroughbred core)";
  2782. else
  2783. this->ChipID.ProcessorName = "Athlon - XP (Thoroughbred core)";
  2784. break;
  2785. default:
  2786. this->ChipID.ProcessorName = "Unknown K7 family";
  2787. return false;
  2788. }
  2789. break;
  2790. default:
  2791. this->ChipID.ProcessorName = "Unknown AMD family";
  2792. return false;
  2793. }
  2794. break;
  2795. case Transmeta:
  2796. switch (this->ChipID.Family) {
  2797. case 5:
  2798. switch (this->ChipID.Model) {
  2799. case 4:
  2800. this->ChipID.ProcessorName = "Crusoe TM3x00 and TM5x00";
  2801. break;
  2802. default:
  2803. this->ChipID.ProcessorName = "Unknown Crusoe family";
  2804. return false;
  2805. }
  2806. break;
  2807. default:
  2808. this->ChipID.ProcessorName = "Unknown Transmeta family";
  2809. return false;
  2810. }
  2811. break;
  2812. case Rise:
  2813. switch (this->ChipID.Family) {
  2814. case 5:
  2815. switch (this->ChipID.Model) {
  2816. case 0:
  2817. this->ChipID.ProcessorName = "mP6 (0.25 micron)";
  2818. break;
  2819. case 2:
  2820. this->ChipID.ProcessorName = "mP6 (0.18 micron)";
  2821. break;
  2822. default:
  2823. this->ChipID.ProcessorName = "Unknown Rise family";
  2824. return false;
  2825. }
  2826. break;
  2827. default:
  2828. this->ChipID.ProcessorName = "Unknown Rise family";
  2829. return false;
  2830. }
  2831. break;
  2832. case UMC:
  2833. switch (this->ChipID.Family) {
  2834. case 4:
  2835. switch (this->ChipID.Model) {
  2836. case 1:
  2837. this->ChipID.ProcessorName = "U5D";
  2838. break;
  2839. case 2:
  2840. this->ChipID.ProcessorName = "U5S";
  2841. break;
  2842. default:
  2843. this->ChipID.ProcessorName = "Unknown UMC family";
  2844. return false;
  2845. }
  2846. break;
  2847. default:
  2848. this->ChipID.ProcessorName = "Unknown UMC family";
  2849. return false;
  2850. }
  2851. break;
  2852. case IDT:
  2853. switch (this->ChipID.Family) {
  2854. case 5:
  2855. switch (this->ChipID.Model) {
  2856. case 4:
  2857. this->ChipID.ProcessorName = "C6";
  2858. break;
  2859. case 8:
  2860. this->ChipID.ProcessorName = "C2";
  2861. break;
  2862. case 9:
  2863. this->ChipID.ProcessorName = "C3";
  2864. break;
  2865. default:
  2866. this->ChipID.ProcessorName = "Unknown IDT\\Centaur family";
  2867. return false;
  2868. }
  2869. break;
  2870. case 6:
  2871. switch (this->ChipID.Model) {
  2872. case 6:
  2873. this->ChipID.ProcessorName = "VIA Cyrix III - Samuel";
  2874. break;
  2875. default:
  2876. this->ChipID.ProcessorName = "Unknown IDT\\Centaur family";
  2877. return false;
  2878. }
  2879. break;
  2880. default:
  2881. this->ChipID.ProcessorName = "Unknown IDT\\Centaur family";
  2882. return false;
  2883. }
  2884. break;
  2885. case Cyrix:
  2886. switch (this->ChipID.Family) {
  2887. case 4:
  2888. switch (this->ChipID.Model) {
  2889. case 4:
  2890. this->ChipID.ProcessorName = "MediaGX GX = GXm";
  2891. break;
  2892. case 9:
  2893. this->ChipID.ProcessorName = "5x86";
  2894. break;
  2895. default:
  2896. this->ChipID.ProcessorName = "Unknown Cx5x86 family";
  2897. return false;
  2898. }
  2899. break;
  2900. case 5:
  2901. switch (this->ChipID.Model) {
  2902. case 2:
  2903. this->ChipID.ProcessorName = "Cx6x86";
  2904. break;
  2905. case 4:
  2906. this->ChipID.ProcessorName = "MediaGX GXm";
  2907. break;
  2908. default:
  2909. this->ChipID.ProcessorName = "Unknown Cx6x86 family";
  2910. return false;
  2911. }
  2912. break;
  2913. case 6:
  2914. switch (this->ChipID.Model) {
  2915. case 0:
  2916. this->ChipID.ProcessorName = "6x86MX";
  2917. break;
  2918. case 5:
  2919. this->ChipID.ProcessorName = "Cyrix M2 Core";
  2920. break;
  2921. case 6:
  2922. this->ChipID.ProcessorName = "WinChip C5A Core";
  2923. break;
  2924. case 7:
  2925. this->ChipID.ProcessorName = "WinChip C5B\\C5C Core";
  2926. break;
  2927. case 8:
  2928. this->ChipID.ProcessorName = "WinChip C5C-T Core";
  2929. break;
  2930. default:
  2931. this->ChipID.ProcessorName = "Unknown 6x86MX\\Cyrix III family";
  2932. return false;
  2933. }
  2934. break;
  2935. default:
  2936. this->ChipID.ProcessorName = "Unknown Cyrix family";
  2937. return false;
  2938. }
  2939. break;
  2940. case NexGen:
  2941. switch (this->ChipID.Family) {
  2942. case 5:
  2943. switch (this->ChipID.Model) {
  2944. case 0:
  2945. this->ChipID.ProcessorName = "Nx586 or Nx586FPU";
  2946. break;
  2947. default:
  2948. this->ChipID.ProcessorName = "Unknown NexGen family";
  2949. return false;
  2950. }
  2951. break;
  2952. default:
  2953. this->ChipID.ProcessorName = "Unknown NexGen family";
  2954. return false;
  2955. }
  2956. break;
  2957. case NSC:
  2958. this->ChipID.ProcessorName = "Cx486SLC \\ DLC \\ Cx486S A-Step";
  2959. break;
  2960. case Sun:
  2961. case IBM:
  2962. case Motorola:
  2963. case HP:
  2964. case UnknownManufacturer:
  2965. default:
  2966. this->ChipID.ProcessorName =
  2967. "Unknown family"; // We cannot identify the processor.
  2968. return false;
  2969. }
  2970. return true;
  2971. }
  2972. /** Extract a value from the CPUInfo file */
  2973. std::string SystemInformationImplementation::ExtractValueFromCpuInfoFile(
  2974. std::string buffer, const char* word, size_t init)
  2975. {
  2976. size_t pos = buffer.find(word, init);
  2977. if (pos != std::string::npos) {
  2978. this->CurrentPositionInFile = pos;
  2979. pos = buffer.find(":", pos);
  2980. size_t pos2 = buffer.find("\n", pos);
  2981. if (pos != std::string::npos && pos2 != std::string::npos) {
  2982. // It may happen that the beginning matches, but this is still not the
  2983. // requested key.
  2984. // An example is looking for "cpu" when "cpu family" comes first. So we
  2985. // check that
  2986. // we have only spaces from here to pos, otherwise we search again.
  2987. for (size_t i = this->CurrentPositionInFile + strlen(word); i < pos;
  2988. ++i) {
  2989. if (buffer[i] != ' ' && buffer[i] != '\t') {
  2990. return this->ExtractValueFromCpuInfoFile(buffer, word, pos2);
  2991. }
  2992. }
  2993. return buffer.substr(pos + 2, pos2 - pos - 2);
  2994. }
  2995. }
  2996. this->CurrentPositionInFile = std::string::npos;
  2997. return "";
  2998. }
  2999. /** Query for the cpu status */
  3000. bool SystemInformationImplementation::RetreiveInformationFromCpuInfoFile()
  3001. {
  3002. this->NumberOfLogicalCPU = 0;
  3003. this->NumberOfPhysicalCPU = 0;
  3004. std::string buffer;
  3005. FILE* fd = fopen("/proc/cpuinfo", "r");
  3006. if (!fd) {
  3007. std::cout << "Problem opening /proc/cpuinfo" << std::endl;
  3008. return false;
  3009. }
  3010. size_t fileSize = 0;
  3011. while (!feof(fd)) {
  3012. buffer += static_cast<char>(fgetc(fd));
  3013. fileSize++;
  3014. }
  3015. fclose(fd);
  3016. buffer.resize(fileSize - 2);
  3017. // Number of logical CPUs (combination of multiple processors, multi-core
  3018. // and SMT)
  3019. size_t pos = buffer.find("processor\t");
  3020. while (pos != std::string::npos) {
  3021. this->NumberOfLogicalCPU++;
  3022. pos = buffer.find("processor\t", pos + 1);
  3023. }
  3024. #ifdef __linux
  3025. // Count sockets.
  3026. std::set<int> PhysicalIDs;
  3027. std::string idc = this->ExtractValueFromCpuInfoFile(buffer, "physical id");
  3028. while (this->CurrentPositionInFile != std::string::npos) {
  3029. int id = atoi(idc.c_str());
  3030. PhysicalIDs.insert(id);
  3031. idc = this->ExtractValueFromCpuInfoFile(buffer, "physical id",
  3032. this->CurrentPositionInFile + 1);
  3033. }
  3034. uint64_t NumberOfSockets = PhysicalIDs.size();
  3035. NumberOfSockets = std::max(NumberOfSockets, (uint64_t)1);
  3036. // Physical ids returned by Linux don't distinguish cores.
  3037. // We want to record the total number of cores in this->NumberOfPhysicalCPU
  3038. // (checking only the first proc)
  3039. std::string Cores = this->ExtractValueFromCpuInfoFile(buffer, "cpu cores");
  3040. unsigned int NumberOfCoresPerSocket = (unsigned int)atoi(Cores.c_str());
  3041. NumberOfCoresPerSocket = std::max(NumberOfCoresPerSocket, 1u);
  3042. this->NumberOfPhysicalCPU =
  3043. NumberOfCoresPerSocket * (unsigned int)NumberOfSockets;
  3044. #else // __CYGWIN__
  3045. // does not have "physical id" entries, neither "cpu cores"
  3046. // this has to be fixed for hyper-threading.
  3047. std::string cpucount =
  3048. this->ExtractValueFromCpuInfoFile(buffer, "cpu count");
  3049. this->NumberOfPhysicalCPU = this->NumberOfLogicalCPU =
  3050. atoi(cpucount.c_str());
  3051. #endif
  3052. // gotta have one, and if this is 0 then we get a / by 0n
  3053. // better to have a bad answer than a crash
  3054. if (this->NumberOfPhysicalCPU <= 0) {
  3055. this->NumberOfPhysicalCPU = 1;
  3056. }
  3057. // LogicalProcessorsPerPhysical>1 => SMT.
  3058. this->Features.ExtendedFeatures.LogicalProcessorsPerPhysical =
  3059. this->NumberOfLogicalCPU / this->NumberOfPhysicalCPU;
  3060. // CPU speed (checking only the first processor)
  3061. std::string CPUSpeed = this->ExtractValueFromCpuInfoFile(buffer, "cpu MHz");
  3062. if (!CPUSpeed.empty()) {
  3063. this->CPUSpeedInMHz = static_cast<float>(atof(CPUSpeed.c_str()));
  3064. }
  3065. #ifdef __linux
  3066. else {
  3067. // Linux Sparc: CPU speed is in Hz and encoded in hexadecimal
  3068. CPUSpeed = this->ExtractValueFromCpuInfoFile(buffer, "Cpu0ClkTck");
  3069. this->CPUSpeedInMHz =
  3070. static_cast<float>(strtoull(CPUSpeed.c_str(), 0, 16)) / 1000000.0f;
  3071. }
  3072. #endif
  3073. // Chip family
  3074. std::string familyStr =
  3075. this->ExtractValueFromCpuInfoFile(buffer, "cpu family");
  3076. if (familyStr.empty()) {
  3077. familyStr = this->ExtractValueFromCpuInfoFile(buffer, "CPU architecture");
  3078. }
  3079. this->ChipID.Family = atoi(familyStr.c_str());
  3080. // Chip Vendor
  3081. this->ChipID.Vendor = this->ExtractValueFromCpuInfoFile(buffer, "vendor_id");
  3082. this->FindManufacturer(familyStr);
  3083. // second try for setting family
  3084. if (this->ChipID.Family == 0 && this->ChipManufacturer == HP) {
  3085. if (familyStr == "PA-RISC 1.1a")
  3086. this->ChipID.Family = 0x11a;
  3087. else if (familyStr == "PA-RISC 2.0")
  3088. this->ChipID.Family = 0x200;
  3089. // If you really get CMake to work on a machine not belonging to
  3090. // any of those families I owe you a dinner if you get it to
  3091. // contribute nightly builds regularly.
  3092. }
  3093. // Chip Model
  3094. this->ChipID.Model =
  3095. atoi(this->ExtractValueFromCpuInfoFile(buffer, "model").c_str());
  3096. if (!this->RetrieveClassicalCPUIdentity()) {
  3097. // Some platforms (e.g. PA-RISC) tell us their CPU name here.
  3098. // Note: x86 does not.
  3099. std::string cpuname = this->ExtractValueFromCpuInfoFile(buffer, "cpu");
  3100. if (!cpuname.empty()) {
  3101. this->ChipID.ProcessorName = cpuname;
  3102. }
  3103. }
  3104. // Chip revision
  3105. std::string cpurev = this->ExtractValueFromCpuInfoFile(buffer, "stepping");
  3106. if (cpurev.empty()) {
  3107. cpurev = this->ExtractValueFromCpuInfoFile(buffer, "CPU revision");
  3108. }
  3109. this->ChipID.Revision = atoi(cpurev.c_str());
  3110. // Chip Model Name
  3111. this->ChipID.ModelName =
  3112. this->ExtractValueFromCpuInfoFile(buffer, "model name").c_str();
  3113. // L1 Cache size
  3114. // Different architectures may show different names for the caches.
  3115. // Sum up everything we find.
  3116. std::vector<const char*> cachename;
  3117. cachename.clear();
  3118. cachename.push_back("cache size"); // e.g. x86
  3119. cachename.push_back("I-cache"); // e.g. PA-RISC
  3120. cachename.push_back("D-cache"); // e.g. PA-RISC
  3121. this->Features.L1CacheSize = 0;
  3122. for (size_t index = 0; index < cachename.size(); index++) {
  3123. std::string cacheSize =
  3124. this->ExtractValueFromCpuInfoFile(buffer, cachename[index]);
  3125. if (!cacheSize.empty()) {
  3126. pos = cacheSize.find(" KB");
  3127. if (pos != std::string::npos) {
  3128. cacheSize = cacheSize.substr(0, pos);
  3129. }
  3130. this->Features.L1CacheSize += atoi(cacheSize.c_str());
  3131. }
  3132. }
  3133. // processor feature flags (probably x86 specific)
  3134. std::string cpuflags = this->ExtractValueFromCpuInfoFile(buffer, "flags");
  3135. if (!cpurev.empty()) {
  3136. // now we can match every flags as space + flag + space
  3137. cpuflags = " " + cpuflags + " ";
  3138. if ((cpuflags.find(" fpu ") != std::string::npos)) {
  3139. this->Features.HasFPU = true;
  3140. }
  3141. if ((cpuflags.find(" tsc ") != std::string::npos)) {
  3142. this->Features.HasTSC = true;
  3143. }
  3144. if ((cpuflags.find(" mmx ") != std::string::npos)) {
  3145. this->Features.HasMMX = true;
  3146. }
  3147. if ((cpuflags.find(" sse ") != std::string::npos)) {
  3148. this->Features.HasSSE = true;
  3149. }
  3150. if ((cpuflags.find(" sse2 ") != std::string::npos)) {
  3151. this->Features.HasSSE2 = true;
  3152. }
  3153. if ((cpuflags.find(" apic ") != std::string::npos)) {
  3154. this->Features.HasAPIC = true;
  3155. }
  3156. if ((cpuflags.find(" cmov ") != std::string::npos)) {
  3157. this->Features.HasCMOV = true;
  3158. }
  3159. if ((cpuflags.find(" mtrr ") != std::string::npos)) {
  3160. this->Features.HasMTRR = true;
  3161. }
  3162. if ((cpuflags.find(" acpi ") != std::string::npos)) {
  3163. this->Features.HasACPI = true;
  3164. }
  3165. if ((cpuflags.find(" 3dnow ") != std::string::npos)) {
  3166. this->Features.ExtendedFeatures.Has3DNow = true;
  3167. }
  3168. }
  3169. return true;
  3170. }
  3171. bool SystemInformationImplementation::QueryProcessorBySysconf()
  3172. {
  3173. #if defined(_SC_NPROC_ONLN) && !defined(_SC_NPROCESSORS_ONLN)
  3174. // IRIX names this slightly different
  3175. #define _SC_NPROCESSORS_ONLN _SC_NPROC_ONLN
  3176. #endif
  3177. #ifdef _SC_NPROCESSORS_ONLN
  3178. long c = sysconf(_SC_NPROCESSORS_ONLN);
  3179. if (c <= 0) {
  3180. return false;
  3181. }
  3182. this->NumberOfPhysicalCPU = static_cast<unsigned int>(c);
  3183. this->NumberOfLogicalCPU = this->NumberOfPhysicalCPU;
  3184. return true;
  3185. #else
  3186. return false;
  3187. #endif
  3188. }
  3189. bool SystemInformationImplementation::QueryProcessor()
  3190. {
  3191. return this->QueryProcessorBySysconf();
  3192. }
  3193. /**
  3194. Get total system RAM in units of KiB.
  3195. */
  3196. SystemInformation::LongLong
  3197. SystemInformationImplementation::GetHostMemoryTotal()
  3198. {
  3199. #if defined(_WIN32)
  3200. #if defined(_MSC_VER) && _MSC_VER < 1300
  3201. MEMORYSTATUS stat;
  3202. stat.dwLength = sizeof(stat);
  3203. GlobalMemoryStatus(&stat);
  3204. return stat.dwTotalPhys / 1024;
  3205. #else
  3206. MEMORYSTATUSEX statex;
  3207. statex.dwLength = sizeof(statex);
  3208. GlobalMemoryStatusEx(&statex);
  3209. return statex.ullTotalPhys / 1024;
  3210. #endif
  3211. #elif defined(__linux)
  3212. SystemInformation::LongLong memTotal = 0;
  3213. int ierr = GetFieldFromFile("/proc/meminfo", "MemTotal:", memTotal);
  3214. if (ierr) {
  3215. return -1;
  3216. }
  3217. return memTotal;
  3218. #elif defined(__APPLE__)
  3219. uint64_t mem;
  3220. size_t len = sizeof(mem);
  3221. int ierr = sysctlbyname("hw.memsize", &mem, &len, KWSYS_NULLPTR, 0);
  3222. if (ierr) {
  3223. return -1;
  3224. }
  3225. return mem / 1024;
  3226. #else
  3227. return 0;
  3228. #endif
  3229. }
  3230. /**
  3231. Get total system RAM in units of KiB. This may differ from the
  3232. host total if a host-wide resource limit is applied.
  3233. */
  3234. SystemInformation::LongLong
  3235. SystemInformationImplementation::GetHostMemoryAvailable(
  3236. const char* hostLimitEnvVarName)
  3237. {
  3238. SystemInformation::LongLong memTotal = this->GetHostMemoryTotal();
  3239. // the following mechanism is provided for systems that
  3240. // apply resource limits across groups of processes.
  3241. // this is of use on certain SMP systems (eg. SGI UV)
  3242. // where the host has a large amount of ram but a given user's
  3243. // access to it is severely restricted. The system will
  3244. // apply a limit across a set of processes. Units are in KiB.
  3245. if (hostLimitEnvVarName) {
  3246. const char* hostLimitEnvVarValue = getenv(hostLimitEnvVarName);
  3247. if (hostLimitEnvVarValue) {
  3248. SystemInformation::LongLong hostLimit =
  3249. atoLongLong(hostLimitEnvVarValue);
  3250. if (hostLimit > 0) {
  3251. memTotal = min(hostLimit, memTotal);
  3252. }
  3253. }
  3254. }
  3255. return memTotal;
  3256. }
  3257. /**
  3258. Get total system RAM in units of KiB. This may differ from the
  3259. host total if a per-process resource limit is applied.
  3260. */
  3261. SystemInformation::LongLong
  3262. SystemInformationImplementation::GetProcMemoryAvailable(
  3263. const char* hostLimitEnvVarName, const char* procLimitEnvVarName)
  3264. {
  3265. SystemInformation::LongLong memAvail =
  3266. this->GetHostMemoryAvailable(hostLimitEnvVarName);
  3267. // the following mechanism is provide for systems where rlimits
  3268. // are not employed. Units are in KiB.
  3269. if (procLimitEnvVarName) {
  3270. const char* procLimitEnvVarValue = getenv(procLimitEnvVarName);
  3271. if (procLimitEnvVarValue) {
  3272. SystemInformation::LongLong procLimit =
  3273. atoLongLong(procLimitEnvVarValue);
  3274. if (procLimit > 0) {
  3275. memAvail = min(procLimit, memAvail);
  3276. }
  3277. }
  3278. }
  3279. #if defined(__linux)
  3280. int ierr;
  3281. ResourceLimitType rlim;
  3282. ierr = GetResourceLimit(RLIMIT_DATA, &rlim);
  3283. if ((ierr == 0) && (rlim.rlim_cur != RLIM_INFINITY)) {
  3284. memAvail =
  3285. min((SystemInformation::LongLong)rlim.rlim_cur / 1024, memAvail);
  3286. }
  3287. ierr = GetResourceLimit(RLIMIT_AS, &rlim);
  3288. if ((ierr == 0) && (rlim.rlim_cur != RLIM_INFINITY)) {
  3289. memAvail =
  3290. min((SystemInformation::LongLong)rlim.rlim_cur / 1024, memAvail);
  3291. }
  3292. #elif defined(__APPLE__)
  3293. struct rlimit rlim;
  3294. int ierr;
  3295. ierr = getrlimit(RLIMIT_DATA, &rlim);
  3296. if ((ierr == 0) && (rlim.rlim_cur != RLIM_INFINITY)) {
  3297. memAvail =
  3298. min((SystemInformation::LongLong)rlim.rlim_cur / 1024, memAvail);
  3299. }
  3300. ierr = getrlimit(RLIMIT_RSS, &rlim);
  3301. if ((ierr == 0) && (rlim.rlim_cur != RLIM_INFINITY)) {
  3302. memAvail =
  3303. min((SystemInformation::LongLong)rlim.rlim_cur / 1024, memAvail);
  3304. }
  3305. #endif
  3306. return memAvail;
  3307. }
  3308. /**
  3309. Get RAM used by all processes in the host, in units of KiB.
  3310. */
  3311. SystemInformation::LongLong
  3312. SystemInformationImplementation::GetHostMemoryUsed()
  3313. {
  3314. #if defined(_WIN32)
  3315. #if defined(_MSC_VER) && _MSC_VER < 1300
  3316. MEMORYSTATUS stat;
  3317. stat.dwLength = sizeof(stat);
  3318. GlobalMemoryStatus(&stat);
  3319. return (stat.dwTotalPhys - stat.dwAvailPhys) / 1024;
  3320. #else
  3321. MEMORYSTATUSEX statex;
  3322. statex.dwLength = sizeof(statex);
  3323. GlobalMemoryStatusEx(&statex);
  3324. return (statex.ullTotalPhys - statex.ullAvailPhys) / 1024;
  3325. #endif
  3326. #elif defined(__linux)
  3327. // First try to use MemAvailable, but it only works on newer kernels
  3328. const char* names2[3] = { "MemTotal:", "MemAvailable:", NULL };
  3329. SystemInformation::LongLong values2[2] = { SystemInformation::LongLong(0) };
  3330. int ierr = GetFieldsFromFile("/proc/meminfo", names2, values2);
  3331. if (ierr) {
  3332. const char* names4[5] = { "MemTotal:", "MemFree:", "Buffers:", "Cached:",
  3333. NULL };
  3334. SystemInformation::LongLong values4[4] = { SystemInformation::LongLong(
  3335. 0) };
  3336. ierr = GetFieldsFromFile("/proc/meminfo", names4, values4);
  3337. if (ierr) {
  3338. return ierr;
  3339. }
  3340. SystemInformation::LongLong& memTotal = values4[0];
  3341. SystemInformation::LongLong& memFree = values4[1];
  3342. SystemInformation::LongLong& memBuffers = values4[2];
  3343. SystemInformation::LongLong& memCached = values4[3];
  3344. return memTotal - memFree - memBuffers - memCached;
  3345. }
  3346. SystemInformation::LongLong& memTotal = values2[0];
  3347. SystemInformation::LongLong& memAvail = values2[1];
  3348. return memTotal - memAvail;
  3349. #elif defined(__APPLE__)
  3350. SystemInformation::LongLong psz = getpagesize();
  3351. if (psz < 1) {
  3352. return -1;
  3353. }
  3354. const char* names[3] = { "Pages wired down:", "Pages active:",
  3355. KWSYS_NULLPTR };
  3356. SystemInformation::LongLong values[2] = { SystemInformation::LongLong(0) };
  3357. int ierr = GetFieldsFromCommand("vm_stat", names, values);
  3358. if (ierr) {
  3359. return -1;
  3360. }
  3361. SystemInformation::LongLong& vmWired = values[0];
  3362. SystemInformation::LongLong& vmActive = values[1];
  3363. return ((vmActive + vmWired) * psz) / 1024;
  3364. #else
  3365. return 0;
  3366. #endif
  3367. }
  3368. /**
  3369. Get system RAM used by the process associated with the given
  3370. process id in units of KiB.
  3371. */
  3372. SystemInformation::LongLong
  3373. SystemInformationImplementation::GetProcMemoryUsed()
  3374. {
  3375. #if defined(_WIN32) && defined(KWSYS_SYS_HAS_PSAPI)
  3376. long pid = GetCurrentProcessId();
  3377. HANDLE hProc;
  3378. hProc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, pid);
  3379. if (hProc == 0) {
  3380. return -1;
  3381. }
  3382. PROCESS_MEMORY_COUNTERS pmc;
  3383. int ok = GetProcessMemoryInfo(hProc, &pmc, sizeof(pmc));
  3384. CloseHandle(hProc);
  3385. if (!ok) {
  3386. return -2;
  3387. }
  3388. return pmc.WorkingSetSize / 1024;
  3389. #elif defined(__linux)
  3390. SystemInformation::LongLong memUsed = 0;
  3391. int ierr = GetFieldFromFile("/proc/self/status", "VmRSS:", memUsed);
  3392. if (ierr) {
  3393. return -1;
  3394. }
  3395. return memUsed;
  3396. #elif defined(__APPLE__)
  3397. SystemInformation::LongLong memUsed = 0;
  3398. pid_t pid = getpid();
  3399. std::ostringstream oss;
  3400. oss << "ps -o rss= -p " << pid;
  3401. FILE* file = popen(oss.str().c_str(), "r");
  3402. if (file == KWSYS_NULLPTR) {
  3403. return -1;
  3404. }
  3405. oss.str("");
  3406. while (!feof(file) && !ferror(file)) {
  3407. char buf[256] = { '\0' };
  3408. errno = 0;
  3409. size_t nRead = fread(buf, 1, 256, file);
  3410. if (ferror(file) && (errno == EINTR)) {
  3411. clearerr(file);
  3412. }
  3413. if (nRead)
  3414. oss << buf;
  3415. }
  3416. int ierr = ferror(file);
  3417. pclose(file);
  3418. if (ierr) {
  3419. return -2;
  3420. }
  3421. std::istringstream iss(oss.str());
  3422. iss >> memUsed;
  3423. return memUsed;
  3424. #else
  3425. return 0;
  3426. #endif
  3427. }
  3428. double SystemInformationImplementation::GetLoadAverage()
  3429. {
  3430. #if defined(KWSYS_CXX_HAS_GETLOADAVG)
  3431. double loadavg[3] = { 0.0, 0.0, 0.0 };
  3432. if (getloadavg(loadavg, 3) > 0) {
  3433. return loadavg[0];
  3434. }
  3435. return -0.0;
  3436. #elif defined(KWSYS_SYSTEMINFORMATION_USE_GetSystemTimes)
  3437. // Old windows.h headers do not provide GetSystemTimes.
  3438. typedef BOOL(WINAPI * GetSystemTimesType)(LPFILETIME, LPFILETIME,
  3439. LPFILETIME);
  3440. static GetSystemTimesType pGetSystemTimes =
  3441. (GetSystemTimesType)GetProcAddress(GetModuleHandleW(L"kernel32"),
  3442. "GetSystemTimes");
  3443. FILETIME idleTime, kernelTime, userTime;
  3444. if (pGetSystemTimes && pGetSystemTimes(&idleTime, &kernelTime, &userTime)) {
  3445. unsigned __int64 const idleTicks = fileTimeToUInt64(idleTime);
  3446. unsigned __int64 const totalTicks =
  3447. fileTimeToUInt64(kernelTime) + fileTimeToUInt64(userTime);
  3448. return calculateCPULoad(idleTicks, totalTicks) * GetNumberOfPhysicalCPU();
  3449. }
  3450. return -0.0;
  3451. #else
  3452. // Not implemented on this platform.
  3453. return -0.0;
  3454. #endif
  3455. }
  3456. /**
  3457. Get the process id of the running process.
  3458. */
  3459. SystemInformation::LongLong SystemInformationImplementation::GetProcessId()
  3460. {
  3461. #if defined(_WIN32)
  3462. return GetCurrentProcessId();
  3463. #elif defined(__linux) || defined(__APPLE__)
  3464. return getpid();
  3465. #else
  3466. return -1;
  3467. #endif
  3468. }
  3469. /**
  3470. return current program stack in a string
  3471. demangle cxx symbols if possible.
  3472. */
  3473. std::string SystemInformationImplementation::GetProgramStack(int firstFrame,
  3474. int wholePath)
  3475. {
  3476. std::string programStack = ""
  3477. #if !defined(KWSYS_SYSTEMINFORMATION_HAS_BACKTRACE)
  3478. "WARNING: The stack could not be examined "
  3479. "because backtrace is not supported.\n"
  3480. #elif !defined(KWSYS_SYSTEMINFORMATION_HAS_DEBUG_BUILD)
  3481. "WARNING: The stack trace will not use advanced "
  3482. "capabilities because this is a release build.\n"
  3483. #else
  3484. #if !defined(KWSYS_SYSTEMINFORMATION_HAS_SYMBOL_LOOKUP)
  3485. "WARNING: Function names will not be demangled "
  3486. "because "
  3487. "dladdr is not available.\n"
  3488. #endif
  3489. #if !defined(KWSYS_SYSTEMINFORMATION_HAS_CPP_DEMANGLE)
  3490. "WARNING: Function names will not be demangled "
  3491. "because cxxabi is not available.\n"
  3492. #endif
  3493. #endif
  3494. ;
  3495. std::ostringstream oss;
  3496. #if defined(KWSYS_SYSTEMINFORMATION_HAS_BACKTRACE)
  3497. void* stackSymbols[256];
  3498. int nFrames = backtrace(stackSymbols, 256);
  3499. for (int i = firstFrame; i < nFrames; ++i) {
  3500. SymbolProperties symProps;
  3501. symProps.SetReportPath(wholePath);
  3502. symProps.Initialize(stackSymbols[i]);
  3503. oss << symProps << std::endl;
  3504. }
  3505. #else
  3506. (void)firstFrame;
  3507. (void)wholePath;
  3508. #endif
  3509. programStack += oss.str();
  3510. return programStack;
  3511. }
  3512. /**
  3513. when set print stack trace in response to common signals.
  3514. */
  3515. void SystemInformationImplementation::SetStackTraceOnError(int enable)
  3516. {
  3517. #if !defined(_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
  3518. static int saOrigValid = 0;
  3519. static struct sigaction saABRTOrig;
  3520. static struct sigaction saSEGVOrig;
  3521. static struct sigaction saTERMOrig;
  3522. static struct sigaction saINTOrig;
  3523. static struct sigaction saILLOrig;
  3524. static struct sigaction saBUSOrig;
  3525. static struct sigaction saFPEOrig;
  3526. if (enable && !saOrigValid) {
  3527. // save the current actions
  3528. sigaction(SIGABRT, KWSYS_NULLPTR, &saABRTOrig);
  3529. sigaction(SIGSEGV, KWSYS_NULLPTR, &saSEGVOrig);
  3530. sigaction(SIGTERM, KWSYS_NULLPTR, &saTERMOrig);
  3531. sigaction(SIGINT, KWSYS_NULLPTR, &saINTOrig);
  3532. sigaction(SIGILL, KWSYS_NULLPTR, &saILLOrig);
  3533. sigaction(SIGBUS, KWSYS_NULLPTR, &saBUSOrig);
  3534. sigaction(SIGFPE, KWSYS_NULLPTR, &saFPEOrig);
  3535. // enable read, disable write
  3536. saOrigValid = 1;
  3537. // install ours
  3538. struct sigaction sa;
  3539. sa.sa_sigaction = (SigAction)StacktraceSignalHandler;
  3540. sa.sa_flags = SA_SIGINFO | SA_RESETHAND;
  3541. #ifdef SA_RESTART
  3542. sa.sa_flags |= SA_RESTART;
  3543. #endif
  3544. sigemptyset(&sa.sa_mask);
  3545. sigaction(SIGABRT, &sa, KWSYS_NULLPTR);
  3546. sigaction(SIGSEGV, &sa, KWSYS_NULLPTR);
  3547. sigaction(SIGTERM, &sa, KWSYS_NULLPTR);
  3548. sigaction(SIGINT, &sa, KWSYS_NULLPTR);
  3549. sigaction(SIGILL, &sa, KWSYS_NULLPTR);
  3550. sigaction(SIGBUS, &sa, KWSYS_NULLPTR);
  3551. sigaction(SIGFPE, &sa, KWSYS_NULLPTR);
  3552. } else if (!enable && saOrigValid) {
  3553. // restore previous actions
  3554. sigaction(SIGABRT, &saABRTOrig, KWSYS_NULLPTR);
  3555. sigaction(SIGSEGV, &saSEGVOrig, KWSYS_NULLPTR);
  3556. sigaction(SIGTERM, &saTERMOrig, KWSYS_NULLPTR);
  3557. sigaction(SIGINT, &saINTOrig, KWSYS_NULLPTR);
  3558. sigaction(SIGILL, &saILLOrig, KWSYS_NULLPTR);
  3559. sigaction(SIGBUS, &saBUSOrig, KWSYS_NULLPTR);
  3560. sigaction(SIGFPE, &saFPEOrig, KWSYS_NULLPTR);
  3561. // enable write, disable read
  3562. saOrigValid = 0;
  3563. }
  3564. #else
  3565. // avoid warning C4100
  3566. (void)enable;
  3567. #endif
  3568. }
  3569. bool SystemInformationImplementation::QueryWindowsMemory()
  3570. {
  3571. #if defined(_WIN32)
  3572. #if defined(_MSC_VER) && _MSC_VER < 1300
  3573. MEMORYSTATUS ms;
  3574. unsigned long tv, tp, av, ap;
  3575. ms.dwLength = sizeof(ms);
  3576. GlobalMemoryStatus(&ms);
  3577. #define MEM_VAL(value) dw##value
  3578. #else
  3579. MEMORYSTATUSEX ms;
  3580. DWORDLONG tv, tp, av, ap;
  3581. ms.dwLength = sizeof(ms);
  3582. if (0 == GlobalMemoryStatusEx(&ms)) {
  3583. return 0;
  3584. }
  3585. #define MEM_VAL(value) ull##value
  3586. #endif
  3587. tv = ms.MEM_VAL(TotalPageFile);
  3588. tp = ms.MEM_VAL(TotalPhys);
  3589. av = ms.MEM_VAL(AvailPageFile);
  3590. ap = ms.MEM_VAL(AvailPhys);
  3591. this->TotalVirtualMemory = tv >> 10 >> 10;
  3592. this->TotalPhysicalMemory = tp >> 10 >> 10;
  3593. this->AvailableVirtualMemory = av >> 10 >> 10;
  3594. this->AvailablePhysicalMemory = ap >> 10 >> 10;
  3595. return true;
  3596. #else
  3597. return false;
  3598. #endif
  3599. }
  3600. bool SystemInformationImplementation::QueryLinuxMemory()
  3601. {
  3602. #if defined(__linux)
  3603. unsigned long tv = 0;
  3604. unsigned long tp = 0;
  3605. unsigned long av = 0;
  3606. unsigned long ap = 0;
  3607. char buffer[1024]; // for reading lines
  3608. int linuxMajor = 0;
  3609. int linuxMinor = 0;
  3610. // Find the Linux kernel version first
  3611. struct utsname unameInfo;
  3612. int errorFlag = uname(&unameInfo);
  3613. if (errorFlag != 0) {
  3614. std::cout << "Problem calling uname(): " << strerror(errno) << std::endl;
  3615. return false;
  3616. }
  3617. if (strlen(unameInfo.release) >= 3) {
  3618. // release looks like "2.6.3-15mdk-i686-up-4GB"
  3619. char majorChar = unameInfo.release[0];
  3620. char minorChar = unameInfo.release[2];
  3621. if (isdigit(majorChar)) {
  3622. linuxMajor = majorChar - '0';
  3623. }
  3624. if (isdigit(minorChar)) {
  3625. linuxMinor = minorChar - '0';
  3626. }
  3627. }
  3628. FILE* fd = fopen("/proc/meminfo", "r");
  3629. if (!fd) {
  3630. std::cout << "Problem opening /proc/meminfo" << std::endl;
  3631. return false;
  3632. }
  3633. if (linuxMajor >= 3 || ((linuxMajor >= 2) && (linuxMinor >= 6))) {
  3634. // new /proc/meminfo format since kernel 2.6.x
  3635. // Rigorously, this test should check from the developing version 2.5.x
  3636. // that introduced the new format...
  3637. enum
  3638. {
  3639. mMemTotal,
  3640. mMemFree,
  3641. mBuffers,
  3642. mCached,
  3643. mSwapTotal,
  3644. mSwapFree
  3645. };
  3646. const char* format[6] = { "MemTotal:%lu kB", "MemFree:%lu kB",
  3647. "Buffers:%lu kB", "Cached:%lu kB",
  3648. "SwapTotal:%lu kB", "SwapFree:%lu kB" };
  3649. bool have[6] = { false, false, false, false, false, false };
  3650. unsigned long value[6];
  3651. int count = 0;
  3652. while (fgets(buffer, static_cast<int>(sizeof(buffer)), fd)) {
  3653. for (int i = 0; i < 6; ++i) {
  3654. if (!have[i] && sscanf(buffer, format[i], &value[i]) == 1) {
  3655. have[i] = true;
  3656. ++count;
  3657. }
  3658. }
  3659. }
  3660. if (count == 6) {
  3661. this->TotalPhysicalMemory = value[mMemTotal] / 1024;
  3662. this->AvailablePhysicalMemory =
  3663. (value[mMemFree] + value[mBuffers] + value[mCached]) / 1024;
  3664. this->TotalVirtualMemory = value[mSwapTotal] / 1024;
  3665. this->AvailableVirtualMemory = value[mSwapFree] / 1024;
  3666. } else {
  3667. std::cout << "Problem parsing /proc/meminfo" << std::endl;
  3668. fclose(fd);
  3669. return false;
  3670. }
  3671. } else {
  3672. // /proc/meminfo format for kernel older than 2.6.x
  3673. unsigned long temp;
  3674. unsigned long cachedMem;
  3675. unsigned long buffersMem;
  3676. // Skip "total: used:..."
  3677. char* r = fgets(buffer, static_cast<int>(sizeof(buffer)), fd);
  3678. int status = 0;
  3679. if (r == buffer) {
  3680. status += fscanf(fd, "Mem: %lu %lu %lu %lu %lu %lu\n", &tp, &temp, &ap,
  3681. &temp, &buffersMem, &cachedMem);
  3682. }
  3683. if (status == 6) {
  3684. status += fscanf(fd, "Swap: %lu %lu %lu\n", &tv, &temp, &av);
  3685. }
  3686. if (status == 9) {
  3687. this->TotalVirtualMemory = tv >> 10 >> 10;
  3688. this->TotalPhysicalMemory = tp >> 10 >> 10;
  3689. this->AvailableVirtualMemory = av >> 10 >> 10;
  3690. this->AvailablePhysicalMemory =
  3691. (ap + buffersMem + cachedMem) >> 10 >> 10;
  3692. } else {
  3693. std::cout << "Problem parsing /proc/meminfo" << std::endl;
  3694. fclose(fd);
  3695. return false;
  3696. }
  3697. }
  3698. fclose(fd);
  3699. return true;
  3700. #else
  3701. return false;
  3702. #endif
  3703. }
  3704. bool SystemInformationImplementation::QueryCygwinMemory()
  3705. {
  3706. #ifdef __CYGWIN__
  3707. // _SC_PAGE_SIZE does return the mmap() granularity on Cygwin,
  3708. // see http://cygwin.com/ml/cygwin/2006-06/msg00350.html
  3709. // Therefore just use 4096 as the page size of Windows.
  3710. long m = sysconf(_SC_PHYS_PAGES);
  3711. if (m < 0) {
  3712. return false;
  3713. }
  3714. this->TotalPhysicalMemory = m >> 8;
  3715. return true;
  3716. #else
  3717. return false;
  3718. #endif
  3719. }
  3720. bool SystemInformationImplementation::QueryAIXMemory()
  3721. {
  3722. #if defined(_AIX) && defined(_SC_AIX_REALMEM)
  3723. long c = sysconf(_SC_AIX_REALMEM);
  3724. if (c <= 0) {
  3725. return false;
  3726. }
  3727. this->TotalPhysicalMemory = c / 1024;
  3728. return true;
  3729. #else
  3730. return false;
  3731. #endif
  3732. }
  3733. bool SystemInformationImplementation::QueryMemoryBySysconf()
  3734. {
  3735. #if defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
  3736. // Assume the mmap() granularity as returned by _SC_PAGESIZE is also
  3737. // the system page size. The only known system where this isn't true
  3738. // is Cygwin.
  3739. long p = sysconf(_SC_PHYS_PAGES);
  3740. long m = sysconf(_SC_PAGESIZE);
  3741. if (p < 0 || m < 0) {
  3742. return false;
  3743. }
  3744. // assume pagesize is a power of 2 and smaller 1 MiB
  3745. size_t pagediv = (1024 * 1024 / m);
  3746. this->TotalPhysicalMemory = p;
  3747. this->TotalPhysicalMemory /= pagediv;
  3748. #if defined(_SC_AVPHYS_PAGES)
  3749. p = sysconf(_SC_AVPHYS_PAGES);
  3750. if (p < 0) {
  3751. return false;
  3752. }
  3753. this->AvailablePhysicalMemory = p;
  3754. this->AvailablePhysicalMemory /= pagediv;
  3755. #endif
  3756. return true;
  3757. #else
  3758. return false;
  3759. #endif
  3760. }
  3761. /** Query for the memory status */
  3762. bool SystemInformationImplementation::QueryMemory()
  3763. {
  3764. return this->QueryMemoryBySysconf();
  3765. }
  3766. /** */
  3767. size_t SystemInformationImplementation::GetTotalVirtualMemory()
  3768. {
  3769. return this->TotalVirtualMemory;
  3770. }
  3771. /** */
  3772. size_t SystemInformationImplementation::GetAvailableVirtualMemory()
  3773. {
  3774. return this->AvailableVirtualMemory;
  3775. }
  3776. size_t SystemInformationImplementation::GetTotalPhysicalMemory()
  3777. {
  3778. return this->TotalPhysicalMemory;
  3779. }
  3780. /** */
  3781. size_t SystemInformationImplementation::GetAvailablePhysicalMemory()
  3782. {
  3783. return this->AvailablePhysicalMemory;
  3784. }
  3785. /** Get Cycle differences */
  3786. SystemInformation::LongLong
  3787. SystemInformationImplementation::GetCyclesDifference(DELAY_FUNC DelayFunction,
  3788. unsigned int uiParameter)
  3789. {
  3790. #if defined(_MSC_VER) && (_MSC_VER >= 1400)
  3791. unsigned __int64 stamp1, stamp2;
  3792. stamp1 = __rdtsc();
  3793. DelayFunction(uiParameter);
  3794. stamp2 = __rdtsc();
  3795. return stamp2 - stamp1;
  3796. #elif USE_ASM_INSTRUCTIONS
  3797. unsigned int edx1, eax1;
  3798. unsigned int edx2, eax2;
  3799. // Calculate the frequency of the CPU instructions.
  3800. __try {
  3801. _asm {
  3802. push uiParameter ; push parameter param
  3803. mov ebx, DelayFunction ; store func in ebx
  3804. RDTSC_INSTRUCTION
  3805. mov esi, eax ; esi = eax
  3806. mov edi, edx ; edi = edx
  3807. call ebx ; call the delay functions
  3808. RDTSC_INSTRUCTION
  3809. pop ebx
  3810. mov edx2, edx ; edx2 = edx
  3811. mov eax2, eax ; eax2 = eax
  3812. mov edx1, edi ; edx2 = edi
  3813. mov eax1, esi ; eax2 = esi
  3814. }
  3815. } __except (1) {
  3816. return -1;
  3817. }
  3818. return ((((__int64)edx2 << 32) + eax2) - (((__int64)edx1 << 32) + eax1));
  3819. #else
  3820. (void)DelayFunction;
  3821. (void)uiParameter;
  3822. return -1;
  3823. #endif
  3824. }
  3825. /** Compute the delay overhead */
  3826. void SystemInformationImplementation::DelayOverhead(unsigned int uiMS)
  3827. {
  3828. #if defined(_WIN32)
  3829. LARGE_INTEGER Frequency, StartCounter, EndCounter;
  3830. __int64 x;
  3831. // Get the frequency of the high performance counter.
  3832. if (!QueryPerformanceFrequency(&Frequency)) {
  3833. return;
  3834. }
  3835. x = Frequency.QuadPart / 1000 * uiMS;
  3836. // Get the starting position of the counter.
  3837. QueryPerformanceCounter(&StartCounter);
  3838. do {
  3839. // Get the ending position of the counter.
  3840. QueryPerformanceCounter(&EndCounter);
  3841. } while (EndCounter.QuadPart - StartCounter.QuadPart == x);
  3842. #endif
  3843. (void)uiMS;
  3844. }
  3845. /** Works only for windows */
  3846. bool SystemInformationImplementation::IsSMTSupported()
  3847. {
  3848. return this->Features.ExtendedFeatures.LogicalProcessorsPerPhysical > 1;
  3849. }
  3850. /** Return the APIC Id. Works only for windows. */
  3851. unsigned char SystemInformationImplementation::GetAPICId()
  3852. {
  3853. int Regs[4] = { 0, 0, 0, 0 };
  3854. #if USE_CPUID
  3855. if (!this->IsSMTSupported()) {
  3856. return static_cast<unsigned char>(-1); // HT not supported
  3857. } // Logical processor = 1
  3858. call_cpuid(1, Regs);
  3859. #endif
  3860. return static_cast<unsigned char>((Regs[1] & INITIAL_APIC_ID_BITS) >> 24);
  3861. }
  3862. /** Count the number of CPUs. Works only on windows. */
  3863. void SystemInformationImplementation::CPUCountWindows()
  3864. {
  3865. #if defined(_WIN32)
  3866. this->NumberOfPhysicalCPU = 0;
  3867. this->NumberOfLogicalCPU = 0;
  3868. typedef BOOL(WINAPI * GetLogicalProcessorInformationType)(
  3869. PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD);
  3870. static GetLogicalProcessorInformationType pGetLogicalProcessorInformation =
  3871. (GetLogicalProcessorInformationType)GetProcAddress(
  3872. GetModuleHandleW(L"kernel32"), "GetLogicalProcessorInformation");
  3873. if (!pGetLogicalProcessorInformation) {
  3874. // Fallback to approximate implementation on ancient Windows versions.
  3875. SYSTEM_INFO info;
  3876. ZeroMemory(&info, sizeof(info));
  3877. GetSystemInfo(&info);
  3878. this->NumberOfPhysicalCPU =
  3879. static_cast<unsigned int>(info.dwNumberOfProcessors);
  3880. this->NumberOfLogicalCPU = this->NumberOfPhysicalCPU;
  3881. return;
  3882. }
  3883. std::vector<SYSTEM_LOGICAL_PROCESSOR_INFORMATION> ProcInfo;
  3884. {
  3885. DWORD Length = 0;
  3886. DWORD rc = pGetLogicalProcessorInformation(NULL, &Length);
  3887. assert(FALSE == rc);
  3888. (void)rc; // Silence unused variable warning in Borland C++ 5.81
  3889. assert(GetLastError() == ERROR_INSUFFICIENT_BUFFER);
  3890. ProcInfo.resize(Length / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION));
  3891. rc = pGetLogicalProcessorInformation(&ProcInfo[0], &Length);
  3892. assert(rc != FALSE);
  3893. (void)rc; // Silence unused variable warning in Borland C++ 5.81
  3894. }
  3895. typedef std::vector<SYSTEM_LOGICAL_PROCESSOR_INFORMATION>::iterator
  3896. pinfoIt_t;
  3897. for (pinfoIt_t it = ProcInfo.begin(); it != ProcInfo.end(); ++it) {
  3898. SYSTEM_LOGICAL_PROCESSOR_INFORMATION PInfo = *it;
  3899. if (PInfo.Relationship != RelationProcessorCore) {
  3900. continue;
  3901. }
  3902. std::bitset<std::numeric_limits<ULONG_PTR>::digits> ProcMask(
  3903. (unsigned long long)PInfo.ProcessorMask);
  3904. unsigned int count = (unsigned int)ProcMask.count();
  3905. if (count == 0) { // I think this should never happen, but just to be safe.
  3906. continue;
  3907. }
  3908. this->NumberOfPhysicalCPU++;
  3909. this->NumberOfLogicalCPU += (unsigned int)count;
  3910. this->Features.ExtendedFeatures.LogicalProcessorsPerPhysical = count;
  3911. }
  3912. this->NumberOfPhysicalCPU = std::max(1u, this->NumberOfPhysicalCPU);
  3913. this->NumberOfLogicalCPU = std::max(1u, this->NumberOfLogicalCPU);
  3914. #else
  3915. #endif
  3916. }
  3917. /** Return the number of logical CPUs on the system */
  3918. unsigned int SystemInformationImplementation::GetNumberOfLogicalCPU()
  3919. {
  3920. return this->NumberOfLogicalCPU;
  3921. }
  3922. /** Return the number of physical CPUs on the system */
  3923. unsigned int SystemInformationImplementation::GetNumberOfPhysicalCPU()
  3924. {
  3925. return this->NumberOfPhysicalCPU;
  3926. }
  3927. /** For Mac use sysctlbyname calls to find system info */
  3928. bool SystemInformationImplementation::ParseSysCtl()
  3929. {
  3930. #if defined(__APPLE__)
  3931. char retBuf[128];
  3932. int err = 0;
  3933. uint64_t value = 0;
  3934. size_t len = sizeof(value);
  3935. sysctlbyname("hw.memsize", &value, &len, KWSYS_NULLPTR, 0);
  3936. this->TotalPhysicalMemory = static_cast<size_t>(value / 1048576);
  3937. // Parse values for Mac
  3938. this->AvailablePhysicalMemory = 0;
  3939. vm_statistics_data_t vmstat;
  3940. mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
  3941. if (host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmstat,
  3942. &count) == KERN_SUCCESS) {
  3943. len = sizeof(value);
  3944. err = sysctlbyname("hw.pagesize", &value, &len, KWSYS_NULLPTR, 0);
  3945. int64_t available_memory = vmstat.free_count * value;
  3946. this->AvailablePhysicalMemory =
  3947. static_cast<size_t>(available_memory / 1048576);
  3948. }
  3949. #ifdef VM_SWAPUSAGE
  3950. // Virtual memory.
  3951. int mib[2] = { CTL_VM, VM_SWAPUSAGE };
  3952. size_t miblen = sizeof(mib) / sizeof(mib[0]);
  3953. struct xsw_usage swap;
  3954. len = sizeof(swap);
  3955. err = sysctl(mib, miblen, &swap, &len, KWSYS_NULLPTR, 0);
  3956. if (err == 0) {
  3957. this->AvailableVirtualMemory =
  3958. static_cast<size_t>(swap.xsu_avail / 1048576);
  3959. this->TotalVirtualMemory = static_cast<size_t>(swap.xsu_total / 1048576);
  3960. }
  3961. #else
  3962. this->AvailableVirtualMemory = 0;
  3963. this->TotalVirtualMemory = 0;
  3964. #endif
  3965. // CPU Info
  3966. len = sizeof(this->NumberOfPhysicalCPU);
  3967. sysctlbyname("hw.physicalcpu", &this->NumberOfPhysicalCPU, &len,
  3968. KWSYS_NULLPTR, 0);
  3969. len = sizeof(this->NumberOfLogicalCPU);
  3970. sysctlbyname("hw.logicalcpu", &this->NumberOfLogicalCPU, &len, KWSYS_NULLPTR,
  3971. 0);
  3972. int cores_per_package = 0;
  3973. len = sizeof(cores_per_package);
  3974. err = sysctlbyname("machdep.cpu.cores_per_package", &cores_per_package, &len,
  3975. KWSYS_NULLPTR, 0);
  3976. // That name was not found, default to 1
  3977. this->Features.ExtendedFeatures.LogicalProcessorsPerPhysical =
  3978. err != 0 ? 1 : static_cast<unsigned char>(cores_per_package);
  3979. len = sizeof(value);
  3980. sysctlbyname("hw.cpufrequency", &value, &len, KWSYS_NULLPTR, 0);
  3981. this->CPUSpeedInMHz = static_cast<float>(value) / 1000000;
  3982. // Chip family
  3983. len = sizeof(this->ChipID.Family);
  3984. // Seems only the intel chips will have this name so if this fails it is
  3985. // probably a PPC machine
  3986. err = sysctlbyname("machdep.cpu.family", &this->ChipID.Family, &len,
  3987. KWSYS_NULLPTR, 0);
  3988. if (err != 0) // Go back to names we know but are less descriptive
  3989. {
  3990. this->ChipID.Family = 0;
  3991. ::memset(retBuf, 0, 128);
  3992. len = 32;
  3993. err = sysctlbyname("hw.machine", &retBuf, &len, KWSYS_NULLPTR, 0);
  3994. std::string machineBuf(retBuf);
  3995. if (machineBuf.find_first_of("Power") != std::string::npos) {
  3996. this->ChipID.Vendor = "IBM";
  3997. len = sizeof(this->ChipID.Family);
  3998. err = sysctlbyname("hw.cputype", &this->ChipID.Family, &len,
  3999. KWSYS_NULLPTR, 0);
  4000. len = sizeof(this->ChipID.Model);
  4001. err = sysctlbyname("hw.cpusubtype", &this->ChipID.Model, &len,
  4002. KWSYS_NULLPTR, 0);
  4003. this->FindManufacturer();
  4004. }
  4005. } else // Should be an Intel Chip.
  4006. {
  4007. len = sizeof(this->ChipID.Family);
  4008. err = sysctlbyname("machdep.cpu.family", &this->ChipID.Family, &len,
  4009. KWSYS_NULLPTR, 0);
  4010. ::memset(retBuf, 0, 128);
  4011. len = 128;
  4012. err = sysctlbyname("machdep.cpu.vendor", retBuf, &len, KWSYS_NULLPTR, 0);
  4013. // Chip Vendor
  4014. this->ChipID.Vendor = retBuf;
  4015. this->FindManufacturer();
  4016. // Chip Model
  4017. len = sizeof(value);
  4018. err = sysctlbyname("machdep.cpu.model", &value, &len, KWSYS_NULLPTR, 0);
  4019. this->ChipID.Model = static_cast<int>(value);
  4020. // Chip Stepping
  4021. len = sizeof(value);
  4022. value = 0;
  4023. err = sysctlbyname("machdep.cpu.stepping", &value, &len, KWSYS_NULLPTR, 0);
  4024. if (!err) {
  4025. this->ChipID.Revision = static_cast<int>(value);
  4026. }
  4027. // feature string
  4028. char* buf = KWSYS_NULLPTR;
  4029. size_t allocSize = 128;
  4030. err = 0;
  4031. len = 0;
  4032. // sysctlbyname() will return with err==0 && len==0 if the buffer is too
  4033. // small
  4034. while (err == 0 && len == 0) {
  4035. delete[] buf;
  4036. allocSize *= 2;
  4037. buf = new char[allocSize];
  4038. if (!buf) {
  4039. break;
  4040. }
  4041. buf[0] = ' ';
  4042. len = allocSize - 2; // keep space for leading and trailing space
  4043. err =
  4044. sysctlbyname("machdep.cpu.features", buf + 1, &len, KWSYS_NULLPTR, 0);
  4045. }
  4046. if (!err && buf && len) {
  4047. // now we can match every flags as space + flag + space
  4048. buf[len + 1] = ' ';
  4049. std::string cpuflags(buf, len + 2);
  4050. if ((cpuflags.find(" FPU ") != std::string::npos)) {
  4051. this->Features.HasFPU = true;
  4052. }
  4053. if ((cpuflags.find(" TSC ") != std::string::npos)) {
  4054. this->Features.HasTSC = true;
  4055. }
  4056. if ((cpuflags.find(" MMX ") != std::string::npos)) {
  4057. this->Features.HasMMX = true;
  4058. }
  4059. if ((cpuflags.find(" SSE ") != std::string::npos)) {
  4060. this->Features.HasSSE = true;
  4061. }
  4062. if ((cpuflags.find(" SSE2 ") != std::string::npos)) {
  4063. this->Features.HasSSE2 = true;
  4064. }
  4065. if ((cpuflags.find(" APIC ") != std::string::npos)) {
  4066. this->Features.HasAPIC = true;
  4067. }
  4068. if ((cpuflags.find(" CMOV ") != std::string::npos)) {
  4069. this->Features.HasCMOV = true;
  4070. }
  4071. if ((cpuflags.find(" MTRR ") != std::string::npos)) {
  4072. this->Features.HasMTRR = true;
  4073. }
  4074. if ((cpuflags.find(" ACPI ") != std::string::npos)) {
  4075. this->Features.HasACPI = true;
  4076. }
  4077. }
  4078. delete[] buf;
  4079. }
  4080. // brand string
  4081. ::memset(retBuf, 0, sizeof(retBuf));
  4082. len = sizeof(retBuf);
  4083. err =
  4084. sysctlbyname("machdep.cpu.brand_string", retBuf, &len, KWSYS_NULLPTR, 0);
  4085. if (!err) {
  4086. this->ChipID.ProcessorName = retBuf;
  4087. this->ChipID.ModelName = retBuf;
  4088. }
  4089. // Cache size
  4090. len = sizeof(value);
  4091. err = sysctlbyname("hw.l1icachesize", &value, &len, KWSYS_NULLPTR, 0);
  4092. this->Features.L1CacheSize = static_cast<int>(value);
  4093. len = sizeof(value);
  4094. err = sysctlbyname("hw.l2cachesize", &value, &len, KWSYS_NULLPTR, 0);
  4095. this->Features.L2CacheSize = static_cast<int>(value);
  4096. return true;
  4097. #else
  4098. return false;
  4099. #endif
  4100. }
  4101. /** Extract a value from sysctl command */
  4102. std::string SystemInformationImplementation::ExtractValueFromSysCtl(
  4103. const char* word)
  4104. {
  4105. size_t pos = this->SysCtlBuffer.find(word);
  4106. if (pos != std::string::npos) {
  4107. pos = this->SysCtlBuffer.find(": ", pos);
  4108. size_t pos2 = this->SysCtlBuffer.find("\n", pos);
  4109. if (pos != std::string::npos && pos2 != std::string::npos) {
  4110. return this->SysCtlBuffer.substr(pos + 2, pos2 - pos - 2);
  4111. }
  4112. }
  4113. return "";
  4114. }
  4115. /** Run a given process */
  4116. std::string SystemInformationImplementation::RunProcess(
  4117. std::vector<const char*> args)
  4118. {
  4119. std::string buffer = "";
  4120. // Run the application
  4121. kwsysProcess* gp = kwsysProcess_New();
  4122. kwsysProcess_SetCommand(gp, &*args.begin());
  4123. kwsysProcess_SetOption(gp, kwsysProcess_Option_HideWindow, 1);
  4124. kwsysProcess_Execute(gp);
  4125. char* data = KWSYS_NULLPTR;
  4126. int length;
  4127. double timeout = 255;
  4128. int pipe; // pipe id as returned by kwsysProcess_WaitForData()
  4129. while ((static_cast<void>(
  4130. pipe = kwsysProcess_WaitForData(gp, &data, &length, &timeout)),
  4131. (pipe == kwsysProcess_Pipe_STDOUT ||
  4132. pipe == kwsysProcess_Pipe_STDERR))) // wait for 1s
  4133. {
  4134. buffer.append(data, length);
  4135. }
  4136. kwsysProcess_WaitForExit(gp, KWSYS_NULLPTR);
  4137. int result = 0;
  4138. switch (kwsysProcess_GetState(gp)) {
  4139. case kwsysProcess_State_Exited: {
  4140. result = kwsysProcess_GetExitValue(gp);
  4141. } break;
  4142. case kwsysProcess_State_Error: {
  4143. std::cerr << "Error: Could not run " << args[0] << ":\n";
  4144. std::cerr << kwsysProcess_GetErrorString(gp) << "\n";
  4145. } break;
  4146. case kwsysProcess_State_Exception: {
  4147. std::cerr << "Error: " << args[0] << " terminated with an exception: "
  4148. << kwsysProcess_GetExceptionString(gp) << "\n";
  4149. } break;
  4150. case kwsysProcess_State_Starting:
  4151. case kwsysProcess_State_Executing:
  4152. case kwsysProcess_State_Expired:
  4153. case kwsysProcess_State_Killed: {
  4154. // Should not get here.
  4155. std::cerr << "Unexpected ending state after running " << args[0]
  4156. << std::endl;
  4157. } break;
  4158. }
  4159. kwsysProcess_Delete(gp);
  4160. if (result) {
  4161. std::cerr << "Error " << args[0] << " returned :" << result << "\n";
  4162. }
  4163. return buffer;
  4164. }
  4165. std::string SystemInformationImplementation::ParseValueFromKStat(
  4166. const char* arguments)
  4167. {
  4168. std::vector<const char*> args;
  4169. args.clear();
  4170. args.push_back("kstat");
  4171. args.push_back("-p");
  4172. std::string command = arguments;
  4173. size_t start = std::string::npos;
  4174. size_t pos = command.find(' ', 0);
  4175. while (pos != std::string::npos) {
  4176. bool inQuotes = false;
  4177. // Check if we are between quotes
  4178. size_t b0 = command.find('"', 0);
  4179. size_t b1 = command.find('"', b0 + 1);
  4180. while (b0 != std::string::npos && b1 != std::string::npos && b1 > b0) {
  4181. if (pos > b0 && pos < b1) {
  4182. inQuotes = true;
  4183. break;
  4184. }
  4185. b0 = command.find('"', b1 + 1);
  4186. b1 = command.find('"', b0 + 1);
  4187. }
  4188. if (!inQuotes) {
  4189. std::string arg = command.substr(start + 1, pos - start - 1);
  4190. // Remove the quotes if any
  4191. size_t quotes = arg.find('"');
  4192. while (quotes != std::string::npos) {
  4193. arg.erase(quotes, 1);
  4194. quotes = arg.find('"');
  4195. }
  4196. args.push_back(arg.c_str());
  4197. start = pos;
  4198. }
  4199. pos = command.find(' ', pos + 1);
  4200. }
  4201. std::string lastArg = command.substr(start + 1, command.size() - start - 1);
  4202. args.push_back(lastArg.c_str());
  4203. args.push_back(KWSYS_NULLPTR);
  4204. std::string buffer = this->RunProcess(args);
  4205. std::string value = "";
  4206. for (size_t i = buffer.size() - 1; i > 0; i--) {
  4207. if (buffer[i] == ' ' || buffer[i] == '\t') {
  4208. break;
  4209. }
  4210. if (buffer[i] != '\n' && buffer[i] != '\r') {
  4211. std::string val = value;
  4212. value = buffer[i];
  4213. value += val;
  4214. }
  4215. }
  4216. return value;
  4217. }
  4218. /** Querying for system information from Solaris */
  4219. bool SystemInformationImplementation::QuerySolarisMemory()
  4220. {
  4221. #if defined(__SVR4) && defined(__sun)
  4222. // Solaris allows querying this value by sysconf, but if this is
  4223. // a 32 bit process on a 64 bit host the returned memory will be
  4224. // limited to 4GiB. So if this is a 32 bit process or if the sysconf
  4225. // method fails use the kstat interface.
  4226. #if SIZEOF_VOID_P == 8
  4227. if (this->QueryMemoryBySysconf()) {
  4228. return true;
  4229. }
  4230. #endif
  4231. char* tail;
  4232. unsigned long totalMemory =
  4233. strtoul(this->ParseValueFromKStat("-s physmem").c_str(), &tail, 0);
  4234. this->TotalPhysicalMemory = totalMemory / 128;
  4235. return true;
  4236. #else
  4237. return false;
  4238. #endif
  4239. }
  4240. bool SystemInformationImplementation::QuerySolarisProcessor()
  4241. {
  4242. if (!this->QueryProcessorBySysconf()) {
  4243. return false;
  4244. }
  4245. // Parse values
  4246. this->CPUSpeedInMHz = static_cast<float>(
  4247. atoi(this->ParseValueFromKStat("-s clock_MHz").c_str()));
  4248. // Chip family
  4249. this->ChipID.Family = 0;
  4250. // Chip Model
  4251. this->ChipID.ProcessorName = this->ParseValueFromKStat("-s cpu_type");
  4252. this->ChipID.Model = 0;
  4253. // Chip Vendor
  4254. if (this->ChipID.ProcessorName != "i386") {
  4255. this->ChipID.Vendor = "Sun";
  4256. this->FindManufacturer();
  4257. }
  4258. return true;
  4259. }
  4260. /** Querying for system information from Haiku OS */
  4261. bool SystemInformationImplementation::QueryHaikuInfo()
  4262. {
  4263. #if defined(__HAIKU__)
  4264. // CPU count
  4265. system_info info;
  4266. get_system_info(&info);
  4267. this->NumberOfPhysicalCPU = info.cpu_count;
  4268. // CPU speed
  4269. uint32 topologyNodeCount = 0;
  4270. cpu_topology_node_info* topology = 0;
  4271. get_cpu_topology_info(0, &topologyNodeCount);
  4272. if (topologyNodeCount != 0)
  4273. topology = new cpu_topology_node_info[topologyNodeCount];
  4274. get_cpu_topology_info(topology, &topologyNodeCount);
  4275. for (uint32 i = 0; i < topologyNodeCount; i++) {
  4276. if (topology[i].type == B_TOPOLOGY_CORE) {
  4277. this->CPUSpeedInMHz =
  4278. topology[i].data.core.default_frequency / 1000000.0f;
  4279. break;
  4280. }
  4281. }
  4282. delete[] topology;
  4283. // Physical Memory
  4284. this->TotalPhysicalMemory = (info.max_pages * B_PAGE_SIZE) / (1024 * 1024);
  4285. this->AvailablePhysicalMemory = this->TotalPhysicalMemory -
  4286. ((info.used_pages * B_PAGE_SIZE) / (1024 * 1024));
  4287. // NOTE: get_system_info_etc is currently a private call so just set to 0
  4288. // until it becomes public
  4289. this->TotalVirtualMemory = 0;
  4290. this->AvailableVirtualMemory = 0;
  4291. // Retrieve cpuid_info union for cpu 0
  4292. cpuid_info cpu_info;
  4293. get_cpuid(&cpu_info, 0, 0);
  4294. // Chip Vendor
  4295. // Use a temporary buffer so that we can add NULL termination to the string
  4296. char vbuf[13];
  4297. strncpy(vbuf, cpu_info.eax_0.vendor_id, 12);
  4298. vbuf[12] = '\0';
  4299. this->ChipID.Vendor = vbuf;
  4300. this->FindManufacturer();
  4301. // Retrieve cpuid_info union for cpu 0 this time using a register value of 1
  4302. get_cpuid(&cpu_info, 1, 0);
  4303. this->NumberOfLogicalCPU = cpu_info.eax_1.logical_cpus;
  4304. // Chip type
  4305. this->ChipID.Type = cpu_info.eax_1.type;
  4306. // Chip family
  4307. this->ChipID.Family = cpu_info.eax_1.family;
  4308. // Chip Model
  4309. this->ChipID.Model = cpu_info.eax_1.model;
  4310. // Chip Revision
  4311. this->ChipID.Revision = cpu_info.eax_1.stepping;
  4312. // Chip Extended Family
  4313. this->ChipID.ExtendedFamily = cpu_info.eax_1.extended_family;
  4314. // Chip Extended Model
  4315. this->ChipID.ExtendedModel = cpu_info.eax_1.extended_model;
  4316. // Get ChipID.ProcessorName from other information already gathered
  4317. this->RetrieveClassicalCPUIdentity();
  4318. // Cache size
  4319. this->Features.L1CacheSize = 0;
  4320. this->Features.L2CacheSize = 0;
  4321. return true;
  4322. #else
  4323. return false;
  4324. #endif
  4325. }
  4326. bool SystemInformationImplementation::QueryQNXMemory()
  4327. {
  4328. #if defined(__QNX__)
  4329. std::string buffer;
  4330. std::vector<const char*> args;
  4331. args.clear();
  4332. args.push_back("showmem");
  4333. args.push_back("-S");
  4334. args.push_back(0);
  4335. buffer = this->RunProcess(args);
  4336. args.clear();
  4337. size_t pos = buffer.find("System RAM:");
  4338. if (pos == std::string::npos)
  4339. return false;
  4340. pos = buffer.find(":", pos);
  4341. size_t pos2 = buffer.find("M (", pos);
  4342. if (pos2 == std::string::npos)
  4343. return false;
  4344. pos++;
  4345. while (buffer[pos] == ' ')
  4346. pos++;
  4347. this->TotalPhysicalMemory = atoi(buffer.substr(pos, pos2 - pos).c_str());
  4348. return true;
  4349. #endif
  4350. return false;
  4351. }
  4352. bool SystemInformationImplementation::QueryBSDMemory()
  4353. {
  4354. #if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
  4355. defined(__DragonFly__)
  4356. int ctrl[2] = { CTL_HW, HW_PHYSMEM };
  4357. #if defined(HW_PHYSMEM64)
  4358. int64_t k;
  4359. ctrl[1] = HW_PHYSMEM64;
  4360. #else
  4361. int k;
  4362. #endif
  4363. size_t sz = sizeof(k);
  4364. if (sysctl(ctrl, 2, &k, &sz, NULL, 0) != 0) {
  4365. return false;
  4366. }
  4367. this->TotalPhysicalMemory = k >> 10 >> 10;
  4368. return true;
  4369. #else
  4370. return false;
  4371. #endif
  4372. }
  4373. bool SystemInformationImplementation::QueryQNXProcessor()
  4374. {
  4375. #if defined(__QNX__)
  4376. // the output on my QNX 6.4.1 looks like this:
  4377. // Processor1: 686 Pentium II Stepping 3 2175MHz FPU
  4378. std::string buffer;
  4379. std::vector<const char*> args;
  4380. args.clear();
  4381. args.push_back("pidin");
  4382. args.push_back("info");
  4383. args.push_back(0);
  4384. buffer = this->RunProcess(args);
  4385. args.clear();
  4386. size_t pos = buffer.find("Processor1:");
  4387. if (pos == std::string::npos)
  4388. return false;
  4389. size_t pos2 = buffer.find("MHz", pos);
  4390. if (pos2 == std::string::npos)
  4391. return false;
  4392. size_t pos3 = pos2;
  4393. while (buffer[pos3] != ' ')
  4394. --pos3;
  4395. this->CPUSpeedInMHz = atoi(buffer.substr(pos3 + 1, pos2 - pos3 - 1).c_str());
  4396. pos2 = buffer.find(" Stepping", pos);
  4397. if (pos2 != std::string::npos) {
  4398. pos2 = buffer.find(" ", pos2 + 1);
  4399. if (pos2 != std::string::npos && pos2 < pos3) {
  4400. this->ChipID.Revision =
  4401. atoi(buffer.substr(pos2 + 1, pos3 - pos2).c_str());
  4402. }
  4403. }
  4404. this->NumberOfPhysicalCPU = 0;
  4405. do {
  4406. pos = buffer.find("\nProcessor", pos + 1);
  4407. ++this->NumberOfPhysicalCPU;
  4408. } while (pos != std::string::npos);
  4409. this->NumberOfLogicalCPU = 1;
  4410. return true;
  4411. #else
  4412. return false;
  4413. #endif
  4414. }
  4415. bool SystemInformationImplementation::QueryBSDProcessor()
  4416. {
  4417. #if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
  4418. defined(__DragonFly__)
  4419. int k;
  4420. size_t sz = sizeof(k);
  4421. int ctrl[2] = { CTL_HW, HW_NCPU };
  4422. if (sysctl(ctrl, 2, &k, &sz, NULL, 0) != 0) {
  4423. return false;
  4424. }
  4425. this->NumberOfPhysicalCPU = k;
  4426. this->NumberOfLogicalCPU = this->NumberOfPhysicalCPU;
  4427. #if defined(HW_CPUSPEED)
  4428. ctrl[1] = HW_CPUSPEED;
  4429. if (sysctl(ctrl, 2, &k, &sz, NULL, 0) != 0) {
  4430. return false;
  4431. }
  4432. this->CPUSpeedInMHz = (float)k;
  4433. #endif
  4434. #if defined(CPU_SSE)
  4435. ctrl[0] = CTL_MACHDEP;
  4436. ctrl[1] = CPU_SSE;
  4437. if (sysctl(ctrl, 2, &k, &sz, NULL, 0) != 0) {
  4438. return false;
  4439. }
  4440. this->Features.HasSSE = (k > 0);
  4441. #endif
  4442. #if defined(CPU_SSE2)
  4443. ctrl[0] = CTL_MACHDEP;
  4444. ctrl[1] = CPU_SSE2;
  4445. if (sysctl(ctrl, 2, &k, &sz, NULL, 0) != 0) {
  4446. return false;
  4447. }
  4448. this->Features.HasSSE2 = (k > 0);
  4449. #endif
  4450. #if defined(CPU_CPUVENDOR)
  4451. ctrl[0] = CTL_MACHDEP;
  4452. ctrl[1] = CPU_CPUVENDOR;
  4453. char vbuf[25];
  4454. ::memset(vbuf, 0, sizeof(vbuf));
  4455. sz = sizeof(vbuf) - 1;
  4456. if (sysctl(ctrl, 2, vbuf, &sz, NULL, 0) != 0) {
  4457. return false;
  4458. }
  4459. this->ChipID.Vendor = vbuf;
  4460. this->FindManufacturer();
  4461. #endif
  4462. return true;
  4463. #else
  4464. return false;
  4465. #endif
  4466. }
  4467. bool SystemInformationImplementation::QueryHPUXMemory()
  4468. {
  4469. #if defined(__hpux)
  4470. unsigned long tv = 0;
  4471. unsigned long tp = 0;
  4472. unsigned long av = 0;
  4473. unsigned long ap = 0;
  4474. struct pst_static pst;
  4475. struct pst_dynamic pdy;
  4476. unsigned long ps = 0;
  4477. if (pstat_getstatic(&pst, sizeof(pst), (size_t)1, 0) == -1) {
  4478. return false;
  4479. }
  4480. ps = pst.page_size;
  4481. tp = pst.physical_memory * ps;
  4482. tv = (pst.physical_memory + pst.pst_maxmem) * ps;
  4483. if (pstat_getdynamic(&pdy, sizeof(pdy), (size_t)1, 0) == -1) {
  4484. return false;
  4485. }
  4486. ap = tp - pdy.psd_rm * ps;
  4487. av = tv - pdy.psd_vm;
  4488. this->TotalVirtualMemory = tv >> 10 >> 10;
  4489. this->TotalPhysicalMemory = tp >> 10 >> 10;
  4490. this->AvailableVirtualMemory = av >> 10 >> 10;
  4491. this->AvailablePhysicalMemory = ap >> 10 >> 10;
  4492. return true;
  4493. #else
  4494. return false;
  4495. #endif
  4496. }
  4497. bool SystemInformationImplementation::QueryHPUXProcessor()
  4498. {
  4499. #if defined(__hpux)
  4500. #if defined(KWSYS_SYS_HAS_MPCTL_H)
  4501. int c = mpctl(MPC_GETNUMSPUS_SYS, 0, 0);
  4502. if (c <= 0) {
  4503. return false;
  4504. }
  4505. this->NumberOfPhysicalCPU = c;
  4506. this->NumberOfLogicalCPU = this->NumberOfPhysicalCPU;
  4507. long t = sysconf(_SC_CPU_VERSION);
  4508. if (t == -1) {
  4509. return false;
  4510. }
  4511. switch (t) {
  4512. case CPU_PA_RISC1_0:
  4513. this->ChipID.Vendor = "Hewlett-Packard";
  4514. this->ChipID.Family = 0x100;
  4515. break;
  4516. case CPU_PA_RISC1_1:
  4517. this->ChipID.Vendor = "Hewlett-Packard";
  4518. this->ChipID.Family = 0x110;
  4519. break;
  4520. case CPU_PA_RISC2_0:
  4521. this->ChipID.Vendor = "Hewlett-Packard";
  4522. this->ChipID.Family = 0x200;
  4523. break;
  4524. #if defined(CPU_HP_INTEL_EM_1_0) || defined(CPU_IA64_ARCHREV_0)
  4525. #ifdef CPU_HP_INTEL_EM_1_0
  4526. case CPU_HP_INTEL_EM_1_0:
  4527. #endif
  4528. #ifdef CPU_IA64_ARCHREV_0
  4529. case CPU_IA64_ARCHREV_0:
  4530. #endif
  4531. this->ChipID.Vendor = "GenuineIntel";
  4532. this->Features.HasIA64 = true;
  4533. break;
  4534. #endif
  4535. default:
  4536. return false;
  4537. }
  4538. this->FindManufacturer();
  4539. return true;
  4540. #else
  4541. return false;
  4542. #endif
  4543. #else
  4544. return false;
  4545. #endif
  4546. }
  4547. /** Query the operating system information */
  4548. bool SystemInformationImplementation::QueryOSInformation()
  4549. {
  4550. #if defined(_WIN32)
  4551. this->OSName = "Windows";
  4552. OSVERSIONINFOEXW osvi;
  4553. BOOL bIsWindows64Bit;
  4554. BOOL bOsVersionInfoEx;
  4555. char operatingSystem[256];
  4556. // Try calling GetVersionEx using the OSVERSIONINFOEX structure.
  4557. ZeroMemory(&osvi, sizeof(OSVERSIONINFOEXW));
  4558. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXW);
  4559. #ifdef KWSYS_WINDOWS_DEPRECATED_GetVersionEx
  4560. #pragma warning(push)
  4561. #ifdef __INTEL_COMPILER
  4562. #pragma warning(disable : 1478)
  4563. #else
  4564. #pragma warning(disable : 4996)
  4565. #endif
  4566. #endif
  4567. bOsVersionInfoEx = GetVersionExW((OSVERSIONINFOW*)&osvi);
  4568. if (!bOsVersionInfoEx) {
  4569. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
  4570. if (!GetVersionExW((OSVERSIONINFOW*)&osvi)) {
  4571. return false;
  4572. }
  4573. }
  4574. #ifdef KWSYS_WINDOWS_DEPRECATED_GetVersionEx
  4575. #pragma warning(pop)
  4576. #endif
  4577. switch (osvi.dwPlatformId) {
  4578. case VER_PLATFORM_WIN32_NT:
  4579. // Test for the product.
  4580. if (osvi.dwMajorVersion <= 4) {
  4581. this->OSRelease = "NT";
  4582. }
  4583. if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0) {
  4584. this->OSRelease = "2000";
  4585. }
  4586. if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1) {
  4587. this->OSRelease = "XP";
  4588. }
  4589. // XP Professional x64
  4590. if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2) {
  4591. this->OSRelease = "XP";
  4592. }
  4593. #ifdef VER_NT_WORKSTATION
  4594. // Test for product type.
  4595. if (bOsVersionInfoEx) {
  4596. if (osvi.wProductType == VER_NT_WORKSTATION) {
  4597. if (osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 0) {
  4598. this->OSRelease = "Vista";
  4599. }
  4600. if (osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 1) {
  4601. this->OSRelease = "7";
  4602. }
  4603. // VER_SUITE_PERSONAL may not be defined
  4604. #ifdef VER_SUITE_PERSONAL
  4605. else {
  4606. if (osvi.wSuiteMask & VER_SUITE_PERSONAL) {
  4607. this->OSRelease += " Personal";
  4608. } else {
  4609. this->OSRelease += " Professional";
  4610. }
  4611. }
  4612. #endif
  4613. } else if (osvi.wProductType == VER_NT_SERVER) {
  4614. // Check for .NET Server instead of Windows XP.
  4615. if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1) {
  4616. this->OSRelease = ".NET";
  4617. }
  4618. // Continue with the type detection.
  4619. if (osvi.wSuiteMask & VER_SUITE_DATACENTER) {
  4620. this->OSRelease += " DataCenter Server";
  4621. } else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE) {
  4622. this->OSRelease += " Advanced Server";
  4623. } else {
  4624. this->OSRelease += " Server";
  4625. }
  4626. }
  4627. sprintf(operatingSystem, "%ls (Build %ld)", osvi.szCSDVersion,
  4628. osvi.dwBuildNumber & 0xFFFF);
  4629. this->OSVersion = operatingSystem;
  4630. } else
  4631. #endif // VER_NT_WORKSTATION
  4632. {
  4633. HKEY hKey;
  4634. wchar_t szProductType[80];
  4635. DWORD dwBufLen;
  4636. // Query the registry to retrieve information.
  4637. RegOpenKeyExW(HKEY_LOCAL_MACHINE,
  4638. L"SYSTEM\\CurrentControlSet\\Control\\ProductOptions", 0,
  4639. KEY_QUERY_VALUE, &hKey);
  4640. RegQueryValueExW(hKey, L"ProductType", NULL, NULL,
  4641. (LPBYTE)szProductType, &dwBufLen);
  4642. RegCloseKey(hKey);
  4643. if (lstrcmpiW(L"WINNT", szProductType) == 0) {
  4644. this->OSRelease += " Professional";
  4645. }
  4646. if (lstrcmpiW(L"LANMANNT", szProductType) == 0) {
  4647. // Decide between Windows 2000 Advanced Server and Windows .NET
  4648. // Enterprise Server.
  4649. if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1) {
  4650. this->OSRelease += " Standard Server";
  4651. } else {
  4652. this->OSRelease += " Server";
  4653. }
  4654. }
  4655. if (lstrcmpiW(L"SERVERNT", szProductType) == 0) {
  4656. // Decide between Windows 2000 Advanced Server and Windows .NET
  4657. // Enterprise Server.
  4658. if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1) {
  4659. this->OSRelease += " Enterprise Server";
  4660. } else {
  4661. this->OSRelease += " Advanced Server";
  4662. }
  4663. }
  4664. }
  4665. // Display version, service pack (if any), and build number.
  4666. if (osvi.dwMajorVersion <= 4) {
  4667. // NB: NT 4.0 and earlier.
  4668. sprintf(operatingSystem, "version %ld.%ld %ls (Build %ld)",
  4669. osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.szCSDVersion,
  4670. osvi.dwBuildNumber & 0xFFFF);
  4671. this->OSVersion = operatingSystem;
  4672. } else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1) {
  4673. // Windows XP and .NET server.
  4674. typedef BOOL(CALLBACK * LPFNPROC)(HANDLE, BOOL*);
  4675. HINSTANCE hKernelDLL;
  4676. LPFNPROC DLLProc;
  4677. // Load the Kernel32 DLL.
  4678. hKernelDLL = LoadLibraryW(L"kernel32");
  4679. if (hKernelDLL != NULL) {
  4680. // Only XP and .NET Server support IsWOW64Process so... Load
  4681. // dynamically!
  4682. DLLProc = (LPFNPROC)GetProcAddress(hKernelDLL, "IsWow64Process");
  4683. // If the function address is valid, call the function.
  4684. if (DLLProc != NULL)
  4685. (DLLProc)(GetCurrentProcess(), &bIsWindows64Bit);
  4686. else
  4687. bIsWindows64Bit = false;
  4688. // Free the DLL module.
  4689. FreeLibrary(hKernelDLL);
  4690. }
  4691. } else {
  4692. // Windows 2000 and everything else.
  4693. sprintf(operatingSystem, "%ls (Build %ld)", osvi.szCSDVersion,
  4694. osvi.dwBuildNumber & 0xFFFF);
  4695. this->OSVersion = operatingSystem;
  4696. }
  4697. break;
  4698. case VER_PLATFORM_WIN32_WINDOWS:
  4699. // Test for the product.
  4700. if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0) {
  4701. this->OSRelease = "95";
  4702. if (osvi.szCSDVersion[1] == 'C') {
  4703. this->OSRelease += "OSR 2.5";
  4704. } else if (osvi.szCSDVersion[1] == 'B') {
  4705. this->OSRelease += "OSR 2";
  4706. }
  4707. }
  4708. if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10) {
  4709. this->OSRelease = "98";
  4710. if (osvi.szCSDVersion[1] == 'A') {
  4711. this->OSRelease += "SE";
  4712. }
  4713. }
  4714. if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90) {
  4715. this->OSRelease = "Me";
  4716. }
  4717. break;
  4718. case VER_PLATFORM_WIN32s:
  4719. this->OSRelease = "Win32s";
  4720. break;
  4721. default:
  4722. this->OSRelease = "Unknown";
  4723. break;
  4724. }
  4725. // Get the hostname
  4726. WORD wVersionRequested;
  4727. WSADATA wsaData;
  4728. char name[255];
  4729. wVersionRequested = MAKEWORD(2, 0);
  4730. if (WSAStartup(wVersionRequested, &wsaData) == 0) {
  4731. gethostname(name, sizeof(name));
  4732. WSACleanup();
  4733. }
  4734. this->Hostname = name;
  4735. const char* arch = getenv("PROCESSOR_ARCHITECTURE");
  4736. const char* wow64 = getenv("PROCESSOR_ARCHITEW6432");
  4737. if (arch) {
  4738. this->OSPlatform = arch;
  4739. }
  4740. if (wow64) {
  4741. // the PROCESSOR_ARCHITEW6432 is only defined when running 32bit programs
  4742. // on 64bit OS
  4743. this->OSIs64Bit = true;
  4744. } else if (arch) {
  4745. // all values other than x86 map to 64bit architectures
  4746. this->OSIs64Bit = (strncmp(arch, "x86", 3) != 0);
  4747. }
  4748. #else
  4749. struct utsname unameInfo;
  4750. int errorFlag = uname(&unameInfo);
  4751. if (errorFlag == 0) {
  4752. this->OSName = unameInfo.sysname;
  4753. this->Hostname = unameInfo.nodename;
  4754. this->OSRelease = unameInfo.release;
  4755. this->OSVersion = unameInfo.version;
  4756. this->OSPlatform = unameInfo.machine;
  4757. // This is still insufficient to capture 64bit architecture such
  4758. // powerpc and possible mips and sparc
  4759. if (this->OSPlatform.find_first_of("64") != std::string::npos) {
  4760. this->OSIs64Bit = true;
  4761. }
  4762. }
  4763. #ifdef __APPLE__
  4764. this->OSName = "Unknown Apple OS";
  4765. this->OSRelease = "Unknown product version";
  4766. this->OSVersion = "Unknown build version";
  4767. this->CallSwVers("-productName", this->OSName);
  4768. this->CallSwVers("-productVersion", this->OSRelease);
  4769. this->CallSwVers("-buildVersion", this->OSVersion);
  4770. #endif
  4771. #endif
  4772. return true;
  4773. }
  4774. int SystemInformationImplementation::CallSwVers(const char* arg,
  4775. std::string& ver)
  4776. {
  4777. #ifdef __APPLE__
  4778. std::vector<const char*> args;
  4779. args.push_back("sw_vers");
  4780. args.push_back(arg);
  4781. args.push_back(KWSYS_NULLPTR);
  4782. ver = this->RunProcess(args);
  4783. this->TrimNewline(ver);
  4784. #else
  4785. // avoid C4100
  4786. (void)arg;
  4787. (void)ver;
  4788. #endif
  4789. return 0;
  4790. }
  4791. void SystemInformationImplementation::TrimNewline(std::string& output)
  4792. {
  4793. // remove \r
  4794. std::string::size_type pos = 0;
  4795. while ((pos = output.find("\r", pos)) != std::string::npos) {
  4796. output.erase(pos);
  4797. }
  4798. // remove \n
  4799. pos = 0;
  4800. while ((pos = output.find("\n", pos)) != std::string::npos) {
  4801. output.erase(pos);
  4802. }
  4803. }
  4804. /** Return true if the machine is 64 bits */
  4805. bool SystemInformationImplementation::Is64Bits()
  4806. {
  4807. return this->OSIs64Bit;
  4808. }
  4809. }