SystemInformation.cxx 144 KB

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