SystemInformation.cxx 145 KB

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