SystemInformation.cxx 148 KB

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