SystemInformation.cxx 145 KB

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