SystemInformation.cxx 148 KB

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