SystemInformation.cxx 145 KB

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