apps_plugin.c 207 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. /*
  3. * netdata apps.plugin
  4. * (C) Copyright 2023 Netdata Inc.
  5. * Released under GPL v3+
  6. */
  7. #include "collectors/all.h"
  8. #include "libnetdata/libnetdata.h"
  9. #include "libnetdata/required_dummies.h"
  10. #define APPS_PLUGIN_PROCESSES_FUNCTION_DESCRIPTION "Detailed information on the currently running processes."
  11. #define APPS_PLUGIN_FUNCTIONS() do { \
  12. fprintf(stdout, PLUGINSD_KEYWORD_FUNCTION " \"processes\" %d \"%s\"\n", PLUGINS_FUNCTIONS_TIMEOUT_DEFAULT, APPS_PLUGIN_PROCESSES_FUNCTION_DESCRIPTION); \
  13. } while(0)
  14. #define APPS_PLUGIN_GLOBAL_FUNCTIONS() do { \
  15. fprintf(stdout, PLUGINSD_KEYWORD_FUNCTION " GLOBAL \"processes\" %d \"%s\"\n", PLUGINS_FUNCTIONS_TIMEOUT_DEFAULT, APPS_PLUGIN_PROCESSES_FUNCTION_DESCRIPTION); \
  16. } while(0)
  17. // ----------------------------------------------------------------------------
  18. // debugging
  19. static int debug_enabled = 0;
  20. static inline void debug_log_int(const char *fmt, ... ) {
  21. va_list args;
  22. fprintf( stderr, "apps.plugin: ");
  23. va_start( args, fmt );
  24. vfprintf( stderr, fmt, args );
  25. va_end( args );
  26. fputc('\n', stderr);
  27. }
  28. #ifdef NETDATA_INTERNAL_CHECKS
  29. #define debug_log(fmt, args...) do { if(unlikely(debug_enabled)) debug_log_int(fmt, ##args); } while(0)
  30. #else
  31. static inline void debug_log_dummy(void) {}
  32. #define debug_log(fmt, args...) debug_log_dummy()
  33. #endif
  34. // ----------------------------------------------------------------------------
  35. #ifdef __FreeBSD__
  36. #include <sys/user.h>
  37. #endif
  38. // ----------------------------------------------------------------------------
  39. // per O/S configuration
  40. // the minimum PID of the system
  41. // this is also the pid of the init process
  42. #define INIT_PID 1
  43. // if the way apps.plugin will work, will read the entire process list,
  44. // including the resource utilization of each process, instantly
  45. // set this to 1
  46. // when set to 0, apps.plugin builds a sort list of processes, in order
  47. // to process children processes, before parent processes
  48. #ifdef __FreeBSD__
  49. #define ALL_PIDS_ARE_READ_INSTANTLY 1
  50. #else
  51. #define ALL_PIDS_ARE_READ_INSTANTLY 0
  52. #endif
  53. // ----------------------------------------------------------------------------
  54. // string lengths
  55. #define MAX_COMPARE_NAME 100
  56. #define MAX_NAME 100
  57. #define MAX_CMDLINE 16384
  58. // ----------------------------------------------------------------------------
  59. // the rates we are going to send to netdata will have this detail a value of:
  60. // - 1 will send just integer parts to netdata
  61. // - 100 will send 2 decimal points
  62. // - 1000 will send 3 decimal points
  63. // etc.
  64. #define RATES_DETAIL 10000ULL
  65. // ----------------------------------------------------------------------------
  66. // factor for calculating correct CPU time values depending on units of raw data
  67. static unsigned int time_factor = 0;
  68. // ----------------------------------------------------------------------------
  69. // to avoid reallocating too frequently, we can increase the number of spare
  70. // file descriptors used by processes.
  71. // IMPORTANT:
  72. // having a lot of spares, increases the CPU utilization of the plugin.
  73. #define MAX_SPARE_FDS 1
  74. // ----------------------------------------------------------------------------
  75. // command line options
  76. static int
  77. update_every = 1,
  78. enable_guest_charts = 0,
  79. #ifdef __FreeBSD__
  80. enable_file_charts = 0,
  81. #else
  82. enable_file_charts = 1,
  83. max_fds_cache_seconds = 60,
  84. #endif
  85. enable_function_cmdline = 0,
  86. enable_detailed_uptime_charts = 0,
  87. enable_users_charts = 1,
  88. enable_groups_charts = 1,
  89. include_exited_childs = 1;
  90. // will be changed to getenv(NETDATA_USER_CONFIG_DIR) if it exists
  91. static char *user_config_dir = CONFIG_DIR;
  92. static char *stock_config_dir = LIBCONFIG_DIR;
  93. // some variables for keeping track of processes count by states
  94. typedef enum {
  95. PROC_STATUS_RUNNING = 0,
  96. PROC_STATUS_SLEEPING_D, // uninterruptible sleep
  97. PROC_STATUS_SLEEPING, // interruptible sleep
  98. PROC_STATUS_ZOMBIE,
  99. PROC_STATUS_STOPPED,
  100. PROC_STATUS_END, //place holder for ending enum fields
  101. } proc_state;
  102. #ifndef __FreeBSD__
  103. static proc_state proc_state_count[PROC_STATUS_END];
  104. static const char *proc_states[] = {
  105. [PROC_STATUS_RUNNING] = "running",
  106. [PROC_STATUS_SLEEPING] = "sleeping_interruptible",
  107. [PROC_STATUS_SLEEPING_D] = "sleeping_uninterruptible",
  108. [PROC_STATUS_ZOMBIE] = "zombie",
  109. [PROC_STATUS_STOPPED] = "stopped",
  110. };
  111. #endif
  112. // ----------------------------------------------------------------------------
  113. // internal flags
  114. // handled in code (automatically set)
  115. // log each problem once per process
  116. // log flood protection flags (log_thrown)
  117. typedef enum __attribute__((packed)) {
  118. PID_LOG_IO = (1 << 0),
  119. PID_LOG_STATUS = (1 << 1),
  120. PID_LOG_CMDLINE = (1 << 2),
  121. PID_LOG_FDS = (1 << 3),
  122. PID_LOG_STAT = (1 << 4),
  123. PID_LOG_LIMITS = (1 << 5),
  124. PID_LOG_LIMITS_DETAIL = (1 << 6),
  125. } PID_LOG;
  126. static int
  127. show_guest_time = 0, // 1 when guest values are collected
  128. show_guest_time_old = 0,
  129. proc_pid_cmdline_is_needed = 0; // 1 when we need to read /proc/cmdline
  130. // ----------------------------------------------------------------------------
  131. // internal counters
  132. static size_t
  133. global_iterations_counter = 1,
  134. calls_counter = 0,
  135. file_counter = 0,
  136. filenames_allocated_counter = 0,
  137. inodes_changed_counter = 0,
  138. links_changed_counter = 0,
  139. targets_assignment_counter = 0;
  140. // ----------------------------------------------------------------------------
  141. // Normalization
  142. //
  143. // With normalization we lower the collected metrics by a factor to make them
  144. // match the total utilization of the system.
  145. // The discrepancy exists because apps.plugin needs some time to collect all
  146. // the metrics. This results in utilization that exceeds the total utilization
  147. // of the system.
  148. //
  149. // During normalization, we align the per-process utilization, to the total of
  150. // the system. We first consume the exited children utilization and it the
  151. // collected values is above the total, we proportionally scale each reported
  152. // metric.
  153. // the total system time, as reported by /proc/stat
  154. static kernel_uint_t
  155. global_utime = 0,
  156. global_stime = 0,
  157. global_gtime = 0;
  158. // the normalization ratios, as calculated by normalize_utilization()
  159. NETDATA_DOUBLE
  160. utime_fix_ratio = 1.0,
  161. stime_fix_ratio = 1.0,
  162. gtime_fix_ratio = 1.0,
  163. minflt_fix_ratio = 1.0,
  164. majflt_fix_ratio = 1.0,
  165. cutime_fix_ratio = 1.0,
  166. cstime_fix_ratio = 1.0,
  167. cgtime_fix_ratio = 1.0,
  168. cminflt_fix_ratio = 1.0,
  169. cmajflt_fix_ratio = 1.0;
  170. struct pid_on_target {
  171. int32_t pid;
  172. struct pid_on_target *next;
  173. };
  174. struct openfds {
  175. kernel_uint_t files;
  176. kernel_uint_t pipes;
  177. kernel_uint_t sockets;
  178. kernel_uint_t inotifies;
  179. kernel_uint_t eventfds;
  180. kernel_uint_t timerfds;
  181. kernel_uint_t signalfds;
  182. kernel_uint_t eventpolls;
  183. kernel_uint_t other;
  184. };
  185. #define pid_openfds_sum(p) ((p)->openfds.files + (p)->openfds.pipes + (p)->openfds.sockets + (p)->openfds.inotifies + (p)->openfds.eventfds + (p)->openfds.timerfds + (p)->openfds.signalfds + (p)->openfds.eventpolls + (p)->openfds.other)
  186. struct pid_limits {
  187. // kernel_uint_t max_cpu_time;
  188. // kernel_uint_t max_file_size;
  189. // kernel_uint_t max_data_size;
  190. // kernel_uint_t max_stack_size;
  191. // kernel_uint_t max_core_file_size;
  192. // kernel_uint_t max_resident_set;
  193. // kernel_uint_t max_processes;
  194. kernel_uint_t max_open_files;
  195. // kernel_uint_t max_locked_memory;
  196. // kernel_uint_t max_address_space;
  197. // kernel_uint_t max_file_locks;
  198. // kernel_uint_t max_pending_signals;
  199. // kernel_uint_t max_msgqueue_size;
  200. // kernel_uint_t max_nice_priority;
  201. // kernel_uint_t max_realtime_priority;
  202. // kernel_uint_t max_realtime_timeout;
  203. };
  204. // ----------------------------------------------------------------------------
  205. // target
  206. //
  207. // target is the structure that processes are aggregated to be reported
  208. // to netdata.
  209. //
  210. // - Each entry in /etc/apps_groups.conf creates a target.
  211. // - Each user and group used by a process in the system, creates a target.
  212. struct target {
  213. char compare[MAX_COMPARE_NAME + 1];
  214. uint32_t comparehash;
  215. size_t comparelen;
  216. char id[MAX_NAME + 1];
  217. uint32_t idhash;
  218. char name[MAX_NAME + 1];
  219. char clean_name[MAX_NAME + 1]; // sanitized name used in chart id (need to replace at least dots)
  220. uid_t uid;
  221. gid_t gid;
  222. bool is_other;
  223. kernel_uint_t minflt;
  224. kernel_uint_t cminflt;
  225. kernel_uint_t majflt;
  226. kernel_uint_t cmajflt;
  227. kernel_uint_t utime;
  228. kernel_uint_t stime;
  229. kernel_uint_t gtime;
  230. kernel_uint_t cutime;
  231. kernel_uint_t cstime;
  232. kernel_uint_t cgtime;
  233. kernel_uint_t num_threads;
  234. // kernel_uint_t rss;
  235. kernel_uint_t status_vmsize;
  236. kernel_uint_t status_vmrss;
  237. kernel_uint_t status_vmshared;
  238. kernel_uint_t status_rssfile;
  239. kernel_uint_t status_rssshmem;
  240. kernel_uint_t status_vmswap;
  241. kernel_uint_t status_voluntary_ctxt_switches;
  242. kernel_uint_t status_nonvoluntary_ctxt_switches;
  243. kernel_uint_t io_logical_bytes_read;
  244. kernel_uint_t io_logical_bytes_written;
  245. kernel_uint_t io_read_calls;
  246. kernel_uint_t io_write_calls;
  247. kernel_uint_t io_storage_bytes_read;
  248. kernel_uint_t io_storage_bytes_written;
  249. kernel_uint_t io_cancelled_write_bytes;
  250. int *target_fds;
  251. int target_fds_size;
  252. struct openfds openfds;
  253. NETDATA_DOUBLE max_open_files_percent;
  254. kernel_uint_t starttime;
  255. kernel_uint_t collected_starttime;
  256. kernel_uint_t uptime_min;
  257. kernel_uint_t uptime_sum;
  258. kernel_uint_t uptime_max;
  259. unsigned int processes; // how many processes have been merged to this
  260. int exposed; // if set, we have sent this to netdata
  261. int hidden; // if set, we set the hidden flag on the dimension
  262. int debug_enabled;
  263. int ends_with;
  264. int starts_with; // if set, the compare string matches only the
  265. // beginning of the command
  266. struct pid_on_target *root_pid; // list of aggregated pids for target debugging
  267. struct target *target; // the one that will be reported to netdata
  268. struct target *next;
  269. };
  270. struct target
  271. *apps_groups_default_target = NULL, // the default target
  272. *apps_groups_root_target = NULL, // apps_groups.conf defined
  273. *users_root_target = NULL, // users
  274. *groups_root_target = NULL; // user groups
  275. size_t
  276. apps_groups_targets_count = 0; // # of apps_groups.conf targets
  277. // ----------------------------------------------------------------------------
  278. // pid_stat
  279. //
  280. // structure to store data for each process running
  281. // see: man proc for the description of the fields
  282. struct pid_fd {
  283. int fd;
  284. #ifndef __FreeBSD__
  285. ino_t inode;
  286. char *filename;
  287. uint32_t link_hash;
  288. size_t cache_iterations_counter;
  289. size_t cache_iterations_reset;
  290. #endif
  291. };
  292. struct pid_stat {
  293. int32_t pid;
  294. int32_t ppid;
  295. // int32_t pgrp;
  296. // int32_t session;
  297. // int32_t tty_nr;
  298. // int32_t tpgid;
  299. // uint64_t flags;
  300. char state;
  301. char comm[MAX_COMPARE_NAME + 1];
  302. char *cmdline;
  303. // these are raw values collected
  304. kernel_uint_t minflt_raw;
  305. kernel_uint_t cminflt_raw;
  306. kernel_uint_t majflt_raw;
  307. kernel_uint_t cmajflt_raw;
  308. kernel_uint_t utime_raw;
  309. kernel_uint_t stime_raw;
  310. kernel_uint_t gtime_raw; // guest_time
  311. kernel_uint_t cutime_raw;
  312. kernel_uint_t cstime_raw;
  313. kernel_uint_t cgtime_raw; // cguest_time
  314. // these are rates
  315. kernel_uint_t minflt;
  316. kernel_uint_t cminflt;
  317. kernel_uint_t majflt;
  318. kernel_uint_t cmajflt;
  319. kernel_uint_t utime;
  320. kernel_uint_t stime;
  321. kernel_uint_t gtime;
  322. kernel_uint_t cutime;
  323. kernel_uint_t cstime;
  324. kernel_uint_t cgtime;
  325. // int64_t priority;
  326. // int64_t nice;
  327. int32_t num_threads;
  328. // int64_t itrealvalue;
  329. kernel_uint_t collected_starttime;
  330. // kernel_uint_t vsize;
  331. // kernel_uint_t rss;
  332. // kernel_uint_t rsslim;
  333. // kernel_uint_t starcode;
  334. // kernel_uint_t endcode;
  335. // kernel_uint_t startstack;
  336. // kernel_uint_t kstkesp;
  337. // kernel_uint_t kstkeip;
  338. // uint64_t signal;
  339. // uint64_t blocked;
  340. // uint64_t sigignore;
  341. // uint64_t sigcatch;
  342. // uint64_t wchan;
  343. // uint64_t nswap;
  344. // uint64_t cnswap;
  345. // int32_t exit_signal;
  346. // int32_t processor;
  347. // uint32_t rt_priority;
  348. // uint32_t policy;
  349. // kernel_uint_t delayacct_blkio_ticks;
  350. uid_t uid;
  351. gid_t gid;
  352. kernel_uint_t status_voluntary_ctxt_switches_raw;
  353. kernel_uint_t status_nonvoluntary_ctxt_switches_raw;
  354. kernel_uint_t status_vmsize;
  355. kernel_uint_t status_vmrss;
  356. kernel_uint_t status_vmshared;
  357. kernel_uint_t status_rssfile;
  358. kernel_uint_t status_rssshmem;
  359. kernel_uint_t status_vmswap;
  360. kernel_uint_t status_voluntary_ctxt_switches;
  361. kernel_uint_t status_nonvoluntary_ctxt_switches;
  362. #ifndef __FreeBSD__
  363. ARL_BASE *status_arl;
  364. #endif
  365. kernel_uint_t io_logical_bytes_read_raw;
  366. kernel_uint_t io_logical_bytes_written_raw;
  367. kernel_uint_t io_read_calls_raw;
  368. kernel_uint_t io_write_calls_raw;
  369. kernel_uint_t io_storage_bytes_read_raw;
  370. kernel_uint_t io_storage_bytes_written_raw;
  371. kernel_uint_t io_cancelled_write_bytes_raw;
  372. kernel_uint_t io_logical_bytes_read;
  373. kernel_uint_t io_logical_bytes_written;
  374. kernel_uint_t io_read_calls;
  375. kernel_uint_t io_write_calls;
  376. kernel_uint_t io_storage_bytes_read;
  377. kernel_uint_t io_storage_bytes_written;
  378. kernel_uint_t io_cancelled_write_bytes;
  379. kernel_uint_t uptime;
  380. struct pid_fd *fds; // array of fds it uses
  381. size_t fds_size; // the size of the fds array
  382. struct openfds openfds;
  383. struct pid_limits limits;
  384. NETDATA_DOUBLE openfds_limits_percent;
  385. int sortlist; // higher numbers = top on the process tree
  386. // each process gets a unique number
  387. int children_count; // number of processes directly referencing this
  388. int keeploops; // increases by 1 every time keep is 1 and updated 0
  389. PID_LOG log_thrown;
  390. bool keep; // true when we need to keep this process in memory even after it exited
  391. bool updated; // true when the process is currently running
  392. bool merged; // true when it has been merged to its parent
  393. bool read; // true when we have already read this process for this iteration
  394. bool matched_by_config;
  395. struct target *target; // app_groups.conf targets
  396. struct target *user_target; // uid based targets
  397. struct target *group_target; // gid based targets
  398. usec_t stat_collected_usec;
  399. usec_t last_stat_collected_usec;
  400. usec_t io_collected_usec;
  401. usec_t last_io_collected_usec;
  402. usec_t last_limits_collected_usec;
  403. char *fds_dirname; // the full directory name in /proc/PID/fd
  404. char *stat_filename;
  405. char *status_filename;
  406. char *io_filename;
  407. char *cmdline_filename;
  408. char *limits_filename;
  409. struct pid_stat *parent;
  410. struct pid_stat *prev;
  411. struct pid_stat *next;
  412. };
  413. size_t pagesize;
  414. kernel_uint_t global_uptime;
  415. static struct pid_stat
  416. *root_of_pids = NULL, // global list of all processes running
  417. **all_pids = NULL; // to avoid allocations, we pre-allocate
  418. // a pointer for each pid in the entire pid space.
  419. static size_t
  420. all_pids_count = 0; // the number of processes running
  421. #if (ALL_PIDS_ARE_READ_INSTANTLY == 0)
  422. // Another pre-allocated list of all possible pids.
  423. // We need it to pids and assign them a unique sortlist id, so that we
  424. // read parents before children. This is needed to prevent a situation where
  425. // a child is found running, but until we read its parent, it has exited and
  426. // its parent has accumulated its resources.
  427. static pid_t
  428. *all_pids_sortlist = NULL;
  429. #endif
  430. // ----------------------------------------------------------------------------
  431. // file descriptor
  432. //
  433. // this is used to keep a global list of all open files of the system.
  434. // it is needed in order to calculate the unique files processes have open.
  435. #define FILE_DESCRIPTORS_INCREASE_STEP 100
  436. // types for struct file_descriptor->type
  437. typedef enum fd_filetype {
  438. FILETYPE_OTHER,
  439. FILETYPE_FILE,
  440. FILETYPE_PIPE,
  441. FILETYPE_SOCKET,
  442. FILETYPE_INOTIFY,
  443. FILETYPE_EVENTFD,
  444. FILETYPE_EVENTPOLL,
  445. FILETYPE_TIMERFD,
  446. FILETYPE_SIGNALFD
  447. } FD_FILETYPE;
  448. struct file_descriptor {
  449. avl_t avl;
  450. #ifdef NETDATA_INTERNAL_CHECKS
  451. uint32_t magic;
  452. #endif /* NETDATA_INTERNAL_CHECKS */
  453. const char *name;
  454. uint32_t hash;
  455. FD_FILETYPE type;
  456. int count;
  457. int pos;
  458. } *all_files = NULL;
  459. static int
  460. all_files_len = 0,
  461. all_files_size = 0;
  462. // ----------------------------------------------------------------------------
  463. // read users and groups from files
  464. struct user_or_group_id {
  465. avl_t avl;
  466. union {
  467. uid_t uid;
  468. gid_t gid;
  469. } id;
  470. char *name;
  471. int updated;
  472. struct user_or_group_id * next;
  473. };
  474. enum user_or_group_id_type {
  475. USER_ID,
  476. GROUP_ID
  477. };
  478. struct user_or_group_ids{
  479. enum user_or_group_id_type type;
  480. avl_tree_type index;
  481. struct user_or_group_id *root;
  482. char filename[FILENAME_MAX + 1];
  483. };
  484. int user_id_compare(void* a, void* b) {
  485. if(((struct user_or_group_id *)a)->id.uid < ((struct user_or_group_id *)b)->id.uid)
  486. return -1;
  487. else if(((struct user_or_group_id *)a)->id.uid > ((struct user_or_group_id *)b)->id.uid)
  488. return 1;
  489. else
  490. return 0;
  491. }
  492. struct user_or_group_ids all_user_ids = {
  493. .type = USER_ID,
  494. .index = {
  495. NULL,
  496. user_id_compare
  497. },
  498. .root = NULL,
  499. .filename = "",
  500. };
  501. int group_id_compare(void* a, void* b) {
  502. if(((struct user_or_group_id *)a)->id.gid < ((struct user_or_group_id *)b)->id.gid)
  503. return -1;
  504. else if(((struct user_or_group_id *)a)->id.gid > ((struct user_or_group_id *)b)->id.gid)
  505. return 1;
  506. else
  507. return 0;
  508. }
  509. struct user_or_group_ids all_group_ids = {
  510. .type = GROUP_ID,
  511. .index = {
  512. NULL,
  513. group_id_compare
  514. },
  515. .root = NULL,
  516. .filename = "",
  517. };
  518. int file_changed(const struct stat *statbuf, struct timespec *last_modification_time) {
  519. if(likely(statbuf->st_mtim.tv_sec == last_modification_time->tv_sec &&
  520. statbuf->st_mtim.tv_nsec == last_modification_time->tv_nsec)) return 0;
  521. last_modification_time->tv_sec = statbuf->st_mtim.tv_sec;
  522. last_modification_time->tv_nsec = statbuf->st_mtim.tv_nsec;
  523. return 1;
  524. }
  525. int read_user_or_group_ids(struct user_or_group_ids *ids, struct timespec *last_modification_time) {
  526. struct stat statbuf;
  527. if(unlikely(stat(ids->filename, &statbuf)))
  528. return 1;
  529. else
  530. if(likely(!file_changed(&statbuf, last_modification_time))) return 0;
  531. procfile *ff = procfile_open(ids->filename, " :\t", PROCFILE_FLAG_DEFAULT);
  532. if(unlikely(!ff)) return 1;
  533. ff = procfile_readall(ff);
  534. if(unlikely(!ff)) return 1;
  535. size_t line, lines = procfile_lines(ff);
  536. for(line = 0; line < lines ;line++) {
  537. size_t words = procfile_linewords(ff, line);
  538. if(unlikely(words < 3)) continue;
  539. char *name = procfile_lineword(ff, line, 0);
  540. if(unlikely(!name || !*name)) continue;
  541. char *id_string = procfile_lineword(ff, line, 2);
  542. if(unlikely(!id_string || !*id_string)) continue;
  543. struct user_or_group_id *user_or_group_id = callocz(1, sizeof(struct user_or_group_id));
  544. if(ids->type == USER_ID)
  545. user_or_group_id->id.uid = (uid_t) str2ull(id_string, NULL);
  546. else
  547. user_or_group_id->id.gid = (uid_t) str2ull(id_string, NULL);
  548. user_or_group_id->name = strdupz(name);
  549. user_or_group_id->updated = 1;
  550. struct user_or_group_id *existing_user_id = NULL;
  551. if(likely(ids->root))
  552. existing_user_id = (struct user_or_group_id *)avl_search(&ids->index, (avl_t *) user_or_group_id);
  553. if(unlikely(existing_user_id)) {
  554. freez(existing_user_id->name);
  555. existing_user_id->name = user_or_group_id->name;
  556. existing_user_id->updated = 1;
  557. freez(user_or_group_id);
  558. }
  559. else {
  560. if(unlikely(avl_insert(&ids->index, (avl_t *) user_or_group_id) != (void *) user_or_group_id)) {
  561. netdata_log_error("INTERNAL ERROR: duplicate indexing of id during realloc");
  562. };
  563. user_or_group_id->next = ids->root;
  564. ids->root = user_or_group_id;
  565. }
  566. }
  567. procfile_close(ff);
  568. // remove unused ids
  569. struct user_or_group_id *user_or_group_id = ids->root, *prev_user_id = NULL;
  570. while(user_or_group_id) {
  571. if(unlikely(!user_or_group_id->updated)) {
  572. if(unlikely((struct user_or_group_id *)avl_remove(&ids->index, (avl_t *) user_or_group_id) != user_or_group_id))
  573. netdata_log_error("INTERNAL ERROR: removal of unused id from index, removed a different id");
  574. if(prev_user_id)
  575. prev_user_id->next = user_or_group_id->next;
  576. else
  577. ids->root = user_or_group_id->next;
  578. freez(user_or_group_id->name);
  579. freez(user_or_group_id);
  580. if(prev_user_id)
  581. user_or_group_id = prev_user_id->next;
  582. else
  583. user_or_group_id = ids->root;
  584. }
  585. else {
  586. user_or_group_id->updated = 0;
  587. prev_user_id = user_or_group_id;
  588. user_or_group_id = user_or_group_id->next;
  589. }
  590. }
  591. return 0;
  592. }
  593. // ----------------------------------------------------------------------------
  594. // apps_groups.conf
  595. // aggregate all processes in groups, to have a limited number of dimensions
  596. static struct target *get_users_target(uid_t uid) {
  597. struct target *w;
  598. for(w = users_root_target ; w ; w = w->next)
  599. if(w->uid == uid) return w;
  600. w = callocz(sizeof(struct target), 1);
  601. snprintfz(w->compare, MAX_COMPARE_NAME, "%u", uid);
  602. w->comparehash = simple_hash(w->compare);
  603. w->comparelen = strlen(w->compare);
  604. snprintfz(w->id, MAX_NAME, "%u", uid);
  605. w->idhash = simple_hash(w->id);
  606. struct user_or_group_id user_id_to_find, *user_or_group_id = NULL;
  607. user_id_to_find.id.uid = uid;
  608. if(*netdata_configured_host_prefix) {
  609. static struct timespec last_passwd_modification_time;
  610. int ret = read_user_or_group_ids(&all_user_ids, &last_passwd_modification_time);
  611. if(likely(!ret && all_user_ids.index.root))
  612. user_or_group_id = (struct user_or_group_id *)avl_search(&all_user_ids.index, (avl_t *) &user_id_to_find);
  613. }
  614. if(user_or_group_id && user_or_group_id->name && *user_or_group_id->name) {
  615. snprintfz(w->name, MAX_NAME, "%s", user_or_group_id->name);
  616. }
  617. else {
  618. struct passwd *pw = getpwuid(uid);
  619. if(!pw || !pw->pw_name || !*pw->pw_name)
  620. snprintfz(w->name, MAX_NAME, "%u", uid);
  621. else
  622. snprintfz(w->name, MAX_NAME, "%s", pw->pw_name);
  623. }
  624. strncpyz(w->clean_name, w->name, MAX_NAME);
  625. netdata_fix_chart_name(w->clean_name);
  626. w->uid = uid;
  627. w->next = users_root_target;
  628. users_root_target = w;
  629. debug_log("added uid %u ('%s') target", w->uid, w->name);
  630. return w;
  631. }
  632. struct target *get_groups_target(gid_t gid)
  633. {
  634. struct target *w;
  635. for(w = groups_root_target ; w ; w = w->next)
  636. if(w->gid == gid) return w;
  637. w = callocz(sizeof(struct target), 1);
  638. snprintfz(w->compare, MAX_COMPARE_NAME, "%u", gid);
  639. w->comparehash = simple_hash(w->compare);
  640. w->comparelen = strlen(w->compare);
  641. snprintfz(w->id, MAX_NAME, "%u", gid);
  642. w->idhash = simple_hash(w->id);
  643. struct user_or_group_id group_id_to_find, *group_id = NULL;
  644. group_id_to_find.id.gid = gid;
  645. if(*netdata_configured_host_prefix) {
  646. static struct timespec last_group_modification_time;
  647. int ret = read_user_or_group_ids(&all_group_ids, &last_group_modification_time);
  648. if(likely(!ret && all_group_ids.index.root))
  649. group_id = (struct user_or_group_id *)avl_search(&all_group_ids.index, (avl_t *) &group_id_to_find);
  650. }
  651. if(group_id && group_id->name && *group_id->name) {
  652. snprintfz(w->name, MAX_NAME, "%s", group_id->name);
  653. }
  654. else {
  655. struct group *gr = getgrgid(gid);
  656. if(!gr || !gr->gr_name || !*gr->gr_name)
  657. snprintfz(w->name, MAX_NAME, "%u", gid);
  658. else
  659. snprintfz(w->name, MAX_NAME, "%s", gr->gr_name);
  660. }
  661. strncpyz(w->clean_name, w->name, MAX_NAME);
  662. netdata_fix_chart_name(w->clean_name);
  663. w->gid = gid;
  664. w->next = groups_root_target;
  665. groups_root_target = w;
  666. debug_log("added gid %u ('%s') target", w->gid, w->name);
  667. return w;
  668. }
  669. // find or create a new target
  670. // there are targets that are just aggregated to other target (the second argument)
  671. static struct target *get_apps_groups_target(const char *id, struct target *target, const char *name) {
  672. int tdebug = 0, thidden = target?target->hidden:0, ends_with = 0;
  673. const char *nid = id;
  674. // extract the options
  675. while(nid[0] == '-' || nid[0] == '+' || nid[0] == '*') {
  676. if(nid[0] == '-') thidden = 1;
  677. if(nid[0] == '+') tdebug = 1;
  678. if(nid[0] == '*') ends_with = 1;
  679. nid++;
  680. }
  681. uint32_t hash = simple_hash(id);
  682. // find if it already exists
  683. struct target *w, *last = apps_groups_root_target;
  684. for(w = apps_groups_root_target ; w ; w = w->next) {
  685. if(w->idhash == hash && strncmp(nid, w->id, MAX_NAME) == 0)
  686. return w;
  687. last = w;
  688. }
  689. // find an existing target
  690. if(unlikely(!target)) {
  691. while(*name == '-') {
  692. if(*name == '-') thidden = 1;
  693. name++;
  694. }
  695. for(target = apps_groups_root_target ; target != NULL ; target = target->next) {
  696. if(!target->target && strcmp(name, target->name) == 0)
  697. break;
  698. }
  699. if(unlikely(debug_enabled)) {
  700. if(unlikely(target))
  701. debug_log("REUSING TARGET NAME '%s' on ID '%s'", target->name, target->id);
  702. else
  703. debug_log("NEW TARGET NAME '%s' on ID '%s'", name, id);
  704. }
  705. }
  706. if(target && target->target)
  707. fatal("Internal Error: request to link process '%s' to target '%s' which is linked to target '%s'", id, target->id, target->target->id);
  708. w = callocz(sizeof(struct target), 1);
  709. strncpyz(w->id, nid, MAX_NAME);
  710. w->idhash = simple_hash(w->id);
  711. if(unlikely(!target))
  712. // copy the name
  713. strncpyz(w->name, name, MAX_NAME);
  714. else
  715. // copy the id
  716. strncpyz(w->name, nid, MAX_NAME);
  717. // dots are used to distinguish chart type and id in streaming, so we should replace them
  718. strncpyz(w->clean_name, w->name, MAX_NAME);
  719. netdata_fix_chart_name(w->clean_name);
  720. for (char *d = w->clean_name; *d; d++) {
  721. if (*d == '.')
  722. *d = '_';
  723. }
  724. strncpyz(w->compare, nid, MAX_COMPARE_NAME);
  725. size_t len = strlen(w->compare);
  726. if(w->compare[len - 1] == '*') {
  727. w->compare[len - 1] = '\0';
  728. w->starts_with = 1;
  729. }
  730. w->ends_with = ends_with;
  731. if(w->starts_with && w->ends_with)
  732. proc_pid_cmdline_is_needed = 1;
  733. w->comparehash = simple_hash(w->compare);
  734. w->comparelen = strlen(w->compare);
  735. w->hidden = thidden;
  736. #ifdef NETDATA_INTERNAL_CHECKS
  737. w->debug_enabled = tdebug;
  738. #else
  739. if(tdebug)
  740. fprintf(stderr, "apps.plugin has been compiled without debugging\n");
  741. #endif
  742. w->target = target;
  743. // append it, to maintain the order in apps_groups.conf
  744. if(last) last->next = w;
  745. else apps_groups_root_target = w;
  746. debug_log("ADDING TARGET ID '%s', process name '%s' (%s), aggregated on target '%s', options: %s %s"
  747. , w->id
  748. , w->compare, (w->starts_with && w->ends_with)?"substring":((w->starts_with)?"prefix":((w->ends_with)?"suffix":"exact"))
  749. , w->target?w->target->name:w->name
  750. , (w->hidden)?"hidden":"-"
  751. , (w->debug_enabled)?"debug":"-"
  752. );
  753. return w;
  754. }
  755. // read the apps_groups.conf file
  756. static int read_apps_groups_conf(const char *path, const char *file)
  757. {
  758. char filename[FILENAME_MAX + 1];
  759. snprintfz(filename, FILENAME_MAX, "%s/apps_%s.conf", path, file);
  760. debug_log("process groups file: '%s'", filename);
  761. // ----------------------------------------
  762. procfile *ff = procfile_open(filename, " :\t", PROCFILE_FLAG_DEFAULT);
  763. if(!ff) return 1;
  764. procfile_set_quotes(ff, "'\"");
  765. ff = procfile_readall(ff);
  766. if(!ff)
  767. return 1;
  768. size_t line, lines = procfile_lines(ff);
  769. for(line = 0; line < lines ;line++) {
  770. size_t word, words = procfile_linewords(ff, line);
  771. if(!words) continue;
  772. char *name = procfile_lineword(ff, line, 0);
  773. if(!name || !*name) continue;
  774. // find a possibly existing target
  775. struct target *w = NULL;
  776. // loop through all words, skipping the first one (the name)
  777. for(word = 0; word < words ;word++) {
  778. char *s = procfile_lineword(ff, line, word);
  779. if(!s || !*s) continue;
  780. if(*s == '#') break;
  781. // is this the first word? skip it
  782. if(s == name) continue;
  783. // add this target
  784. struct target *n = get_apps_groups_target(s, w, name);
  785. if(!n) {
  786. netdata_log_error("Cannot create target '%s' (line %zu, word %zu)", s, line, word);
  787. continue;
  788. }
  789. // just some optimization
  790. // to avoid searching for a target for each process
  791. if(!w) w = n->target?n->target:n;
  792. }
  793. }
  794. procfile_close(ff);
  795. apps_groups_default_target = get_apps_groups_target("p+!o@w#e$i^r&7*5(-i)l-o_", NULL, "other"); // match nothing
  796. if(!apps_groups_default_target)
  797. fatal("Cannot create default target");
  798. apps_groups_default_target->is_other = true;
  799. // allow the user to override group 'other'
  800. if(apps_groups_default_target->target)
  801. apps_groups_default_target = apps_groups_default_target->target;
  802. return 0;
  803. }
  804. // ----------------------------------------------------------------------------
  805. // struct pid_stat management
  806. static inline void init_pid_fds(struct pid_stat *p, size_t first, size_t size);
  807. static inline struct pid_stat *get_pid_entry(pid_t pid) {
  808. if(unlikely(all_pids[pid]))
  809. return all_pids[pid];
  810. struct pid_stat *p = callocz(sizeof(struct pid_stat), 1);
  811. p->fds = mallocz(sizeof(struct pid_fd) * MAX_SPARE_FDS);
  812. p->fds_size = MAX_SPARE_FDS;
  813. init_pid_fds(p, 0, p->fds_size);
  814. p->pid = pid;
  815. DOUBLE_LINKED_LIST_APPEND_ITEM_UNSAFE(root_of_pids, p, prev, next);
  816. all_pids[pid] = p;
  817. all_pids_count++;
  818. return p;
  819. }
  820. static inline void del_pid_entry(pid_t pid) {
  821. struct pid_stat *p = all_pids[pid];
  822. if(unlikely(!p)) {
  823. netdata_log_error("attempted to free pid %d that is not allocated.", pid);
  824. return;
  825. }
  826. debug_log("process %d %s exited, deleting it.", pid, p->comm);
  827. DOUBLE_LINKED_LIST_REMOVE_ITEM_UNSAFE(root_of_pids, p, prev, next);
  828. // free the filename
  829. #ifndef __FreeBSD__
  830. {
  831. size_t i;
  832. for(i = 0; i < p->fds_size; i++)
  833. if(p->fds[i].filename)
  834. freez(p->fds[i].filename);
  835. }
  836. #endif
  837. freez(p->fds);
  838. freez(p->fds_dirname);
  839. freez(p->stat_filename);
  840. freez(p->status_filename);
  841. freez(p->limits_filename);
  842. #ifndef __FreeBSD__
  843. arl_free(p->status_arl);
  844. #endif
  845. freez(p->io_filename);
  846. freez(p->cmdline_filename);
  847. freez(p->cmdline);
  848. freez(p);
  849. all_pids[pid] = NULL;
  850. all_pids_count--;
  851. }
  852. // ----------------------------------------------------------------------------
  853. static inline int managed_log(struct pid_stat *p, PID_LOG log, int status) {
  854. if(unlikely(!status)) {
  855. // netdata_log_error("command failed log %u, errno %d", log, errno);
  856. if(unlikely(debug_enabled || errno != ENOENT)) {
  857. if(unlikely(debug_enabled || !(p->log_thrown & log))) {
  858. p->log_thrown |= log;
  859. switch(log) {
  860. case PID_LOG_IO:
  861. #ifdef __FreeBSD__
  862. netdata_log_error("Cannot fetch process %d I/O info (command '%s')", p->pid, p->comm);
  863. #else
  864. netdata_log_error("Cannot process %s/proc/%d/io (command '%s')", netdata_configured_host_prefix, p->pid, p->comm);
  865. #endif
  866. break;
  867. case PID_LOG_STATUS:
  868. #ifdef __FreeBSD__
  869. netdata_log_error("Cannot fetch process %d status info (command '%s')", p->pid, p->comm);
  870. #else
  871. netdata_log_error("Cannot process %s/proc/%d/status (command '%s')", netdata_configured_host_prefix, p->pid, p->comm);
  872. #endif
  873. break;
  874. case PID_LOG_CMDLINE:
  875. #ifdef __FreeBSD__
  876. netdata_log_error("Cannot fetch process %d command line (command '%s')", p->pid, p->comm);
  877. #else
  878. netdata_log_error("Cannot process %s/proc/%d/cmdline (command '%s')", netdata_configured_host_prefix, p->pid, p->comm);
  879. #endif
  880. break;
  881. case PID_LOG_FDS:
  882. #ifdef __FreeBSD__
  883. netdata_log_error("Cannot fetch process %d files (command '%s')", p->pid, p->comm);
  884. #else
  885. netdata_log_error("Cannot process entries in %s/proc/%d/fd (command '%s')", netdata_configured_host_prefix, p->pid, p->comm);
  886. #endif
  887. break;
  888. case PID_LOG_LIMITS:
  889. #ifdef __FreeBSD__
  890. ;
  891. #else
  892. netdata_log_error("Cannot process %s/proc/%d/limits (command '%s')", netdata_configured_host_prefix, p->pid, p->comm);
  893. #endif
  894. case PID_LOG_STAT:
  895. break;
  896. default:
  897. netdata_log_error("unhandled error for pid %d, command '%s'", p->pid, p->comm);
  898. break;
  899. }
  900. }
  901. }
  902. errno = 0;
  903. }
  904. else if(unlikely(p->log_thrown & log)) {
  905. // netdata_log_error("unsetting log %u on pid %d", log, p->pid);
  906. p->log_thrown &= ~log;
  907. }
  908. return status;
  909. }
  910. static inline void assign_target_to_pid(struct pid_stat *p) {
  911. targets_assignment_counter++;
  912. uint32_t hash = simple_hash(p->comm);
  913. size_t pclen = strlen(p->comm);
  914. struct target *w;
  915. for(w = apps_groups_root_target; w ; w = w->next) {
  916. // if(debug_enabled || (p->target && p->target->debug_enabled)) debug_log_int("\t\tcomparing '%s' with '%s'", w->compare, p->comm);
  917. // find it - 4 cases:
  918. // 1. the target is not a pattern
  919. // 2. the target has the prefix
  920. // 3. the target has the suffix
  921. // 4. the target is something inside cmdline
  922. if(unlikely(( (!w->starts_with && !w->ends_with && w->comparehash == hash && !strcmp(w->compare, p->comm))
  923. || (w->starts_with && !w->ends_with && !strncmp(w->compare, p->comm, w->comparelen))
  924. || (!w->starts_with && w->ends_with && pclen >= w->comparelen && !strcmp(w->compare, &p->comm[pclen - w->comparelen]))
  925. || (proc_pid_cmdline_is_needed && w->starts_with && w->ends_with && p->cmdline && strstr(p->cmdline, w->compare))
  926. ))) {
  927. p->matched_by_config = true;
  928. if(w->target) p->target = w->target;
  929. else p->target = w;
  930. if(debug_enabled || (p->target && p->target->debug_enabled))
  931. debug_log_int("%s linked to target %s", p->comm, p->target->name);
  932. break;
  933. }
  934. }
  935. }
  936. // ----------------------------------------------------------------------------
  937. // update pids from proc
  938. static inline int read_proc_pid_cmdline(struct pid_stat *p) {
  939. static char cmdline[MAX_CMDLINE + 1];
  940. #ifdef __FreeBSD__
  941. size_t i, bytes = MAX_CMDLINE;
  942. int mib[4];
  943. mib[0] = CTL_KERN;
  944. mib[1] = KERN_PROC;
  945. mib[2] = KERN_PROC_ARGS;
  946. mib[3] = p->pid;
  947. if (unlikely(sysctl(mib, 4, cmdline, &bytes, NULL, 0)))
  948. goto cleanup;
  949. #else
  950. if(unlikely(!p->cmdline_filename)) {
  951. char filename[FILENAME_MAX + 1];
  952. snprintfz(filename, FILENAME_MAX, "%s/proc/%d/cmdline", netdata_configured_host_prefix, p->pid);
  953. p->cmdline_filename = strdupz(filename);
  954. }
  955. int fd = open(p->cmdline_filename, procfile_open_flags, 0666);
  956. if(unlikely(fd == -1)) goto cleanup;
  957. ssize_t i, bytes = read(fd, cmdline, MAX_CMDLINE);
  958. close(fd);
  959. if(unlikely(bytes < 0)) goto cleanup;
  960. #endif
  961. cmdline[bytes] = '\0';
  962. for(i = 0; i < bytes ; i++) {
  963. if(unlikely(!cmdline[i])) cmdline[i] = ' ';
  964. }
  965. if(p->cmdline) freez(p->cmdline);
  966. p->cmdline = strdupz(cmdline);
  967. debug_log("Read file '%s' contents: %s", p->cmdline_filename, p->cmdline);
  968. return 1;
  969. cleanup:
  970. // copy the command to the command line
  971. if(p->cmdline) freez(p->cmdline);
  972. p->cmdline = strdupz(p->comm);
  973. return 0;
  974. }
  975. // ----------------------------------------------------------------------------
  976. // macro to calculate the incremental rate of a value
  977. // each parameter is accessed only ONCE - so it is safe to pass function calls
  978. // or other macros as parameters
  979. #define incremental_rate(rate_variable, last_kernel_variable, new_kernel_value, collected_usec, last_collected_usec) { \
  980. kernel_uint_t _new_tmp = new_kernel_value; \
  981. (rate_variable) = (_new_tmp - (last_kernel_variable)) * (USEC_PER_SEC * RATES_DETAIL) / ((collected_usec) - (last_collected_usec)); \
  982. (last_kernel_variable) = _new_tmp; \
  983. }
  984. // the same macro for struct pid members
  985. #define pid_incremental_rate(type, var, value) \
  986. incremental_rate(var, var##_raw, value, p->type##_collected_usec, p->last_##type##_collected_usec)
  987. // ----------------------------------------------------------------------------
  988. #ifndef __FreeBSD__
  989. struct arl_callback_ptr {
  990. struct pid_stat *p;
  991. procfile *ff;
  992. size_t line;
  993. };
  994. void arl_callback_status_uid(const char *name, uint32_t hash, const char *value, void *dst) {
  995. (void)name; (void)hash; (void)value;
  996. struct arl_callback_ptr *aptr = (struct arl_callback_ptr *)dst;
  997. if(unlikely(procfile_linewords(aptr->ff, aptr->line) < 5)) return;
  998. //const char *real_uid = procfile_lineword(aptr->ff, aptr->line, 1);
  999. const char *effective_uid = procfile_lineword(aptr->ff, aptr->line, 2);
  1000. //const char *saved_uid = procfile_lineword(aptr->ff, aptr->line, 3);
  1001. //const char *filesystem_uid = procfile_lineword(aptr->ff, aptr->line, 4);
  1002. if(likely(effective_uid && *effective_uid))
  1003. aptr->p->uid = (uid_t)str2l(effective_uid);
  1004. }
  1005. void arl_callback_status_gid(const char *name, uint32_t hash, const char *value, void *dst) {
  1006. (void)name; (void)hash; (void)value;
  1007. struct arl_callback_ptr *aptr = (struct arl_callback_ptr *)dst;
  1008. if(unlikely(procfile_linewords(aptr->ff, aptr->line) < 5)) return;
  1009. //const char *real_gid = procfile_lineword(aptr->ff, aptr->line, 1);
  1010. const char *effective_gid = procfile_lineword(aptr->ff, aptr->line, 2);
  1011. //const char *saved_gid = procfile_lineword(aptr->ff, aptr->line, 3);
  1012. //const char *filesystem_gid = procfile_lineword(aptr->ff, aptr->line, 4);
  1013. if(likely(effective_gid && *effective_gid))
  1014. aptr->p->gid = (uid_t)str2l(effective_gid);
  1015. }
  1016. void arl_callback_status_vmsize(const char *name, uint32_t hash, const char *value, void *dst) {
  1017. (void)name; (void)hash; (void)value;
  1018. struct arl_callback_ptr *aptr = (struct arl_callback_ptr *)dst;
  1019. if(unlikely(procfile_linewords(aptr->ff, aptr->line) < 3)) return;
  1020. aptr->p->status_vmsize = str2kernel_uint_t(procfile_lineword(aptr->ff, aptr->line, 1));
  1021. }
  1022. void arl_callback_status_vmswap(const char *name, uint32_t hash, const char *value, void *dst) {
  1023. (void)name; (void)hash; (void)value;
  1024. struct arl_callback_ptr *aptr = (struct arl_callback_ptr *)dst;
  1025. if(unlikely(procfile_linewords(aptr->ff, aptr->line) < 3)) return;
  1026. aptr->p->status_vmswap = str2kernel_uint_t(procfile_lineword(aptr->ff, aptr->line, 1));
  1027. }
  1028. void arl_callback_status_vmrss(const char *name, uint32_t hash, const char *value, void *dst) {
  1029. (void)name; (void)hash; (void)value;
  1030. struct arl_callback_ptr *aptr = (struct arl_callback_ptr *)dst;
  1031. if(unlikely(procfile_linewords(aptr->ff, aptr->line) < 3)) return;
  1032. aptr->p->status_vmrss = str2kernel_uint_t(procfile_lineword(aptr->ff, aptr->line, 1));
  1033. }
  1034. void arl_callback_status_rssfile(const char *name, uint32_t hash, const char *value, void *dst) {
  1035. (void)name; (void)hash; (void)value;
  1036. struct arl_callback_ptr *aptr = (struct arl_callback_ptr *)dst;
  1037. if(unlikely(procfile_linewords(aptr->ff, aptr->line) < 3)) return;
  1038. aptr->p->status_rssfile = str2kernel_uint_t(procfile_lineword(aptr->ff, aptr->line, 1));
  1039. }
  1040. void arl_callback_status_rssshmem(const char *name, uint32_t hash, const char *value, void *dst) {
  1041. (void)name; (void)hash; (void)value;
  1042. struct arl_callback_ptr *aptr = (struct arl_callback_ptr *)dst;
  1043. if(unlikely(procfile_linewords(aptr->ff, aptr->line) < 3)) return;
  1044. aptr->p->status_rssshmem = str2kernel_uint_t(procfile_lineword(aptr->ff, aptr->line, 1));
  1045. }
  1046. void arl_callback_status_voluntary_ctxt_switches(const char *name, uint32_t hash, const char *value, void *dst) {
  1047. (void)name; (void)hash; (void)value;
  1048. struct arl_callback_ptr *aptr = (struct arl_callback_ptr *)dst;
  1049. if(unlikely(procfile_linewords(aptr->ff, aptr->line) < 2)) return;
  1050. struct pid_stat *p = aptr->p;
  1051. pid_incremental_rate(stat, p->status_voluntary_ctxt_switches, str2kernel_uint_t(procfile_lineword(aptr->ff, aptr->line, 1)));
  1052. }
  1053. void arl_callback_status_nonvoluntary_ctxt_switches(const char *name, uint32_t hash, const char *value, void *dst) {
  1054. (void)name; (void)hash; (void)value;
  1055. struct arl_callback_ptr *aptr = (struct arl_callback_ptr *)dst;
  1056. if(unlikely(procfile_linewords(aptr->ff, aptr->line) < 2)) return;
  1057. struct pid_stat *p = aptr->p;
  1058. pid_incremental_rate(stat, p->status_nonvoluntary_ctxt_switches, str2kernel_uint_t(procfile_lineword(aptr->ff, aptr->line, 1)));
  1059. }
  1060. static void update_proc_state_count(char proc_state) {
  1061. switch (proc_state) {
  1062. case 'S':
  1063. proc_state_count[PROC_STATUS_SLEEPING] += 1;
  1064. break;
  1065. case 'R':
  1066. proc_state_count[PROC_STATUS_RUNNING] += 1;
  1067. break;
  1068. case 'D':
  1069. proc_state_count[PROC_STATUS_SLEEPING_D] += 1;
  1070. break;
  1071. case 'Z':
  1072. proc_state_count[PROC_STATUS_ZOMBIE] += 1;
  1073. break;
  1074. case 'T':
  1075. proc_state_count[PROC_STATUS_STOPPED] += 1;
  1076. break;
  1077. default:
  1078. break;
  1079. }
  1080. }
  1081. #endif // !__FreeBSD__
  1082. #define MAX_PROC_PID_LIMITS 8192
  1083. #define PROC_PID_LIMITS_MAX_OPEN_FILES_KEY "\nMax open files "
  1084. static inline kernel_uint_t get_proc_pid_limits_limit(char *buf, const char *key, size_t key_len, kernel_uint_t def) {
  1085. char *line = strstr(buf, key);
  1086. if(!line)
  1087. return def;
  1088. char *v = &line[key_len];
  1089. while(isspace(*v)) v++;
  1090. if(strcmp(v, "unlimited") == 0)
  1091. return 0;
  1092. return str2ull(v, NULL);
  1093. }
  1094. static inline int read_proc_pid_limits(struct pid_stat *p, void *ptr) {
  1095. (void)ptr;
  1096. #ifdef __FreeBSD__
  1097. return 0;
  1098. #else
  1099. static char proc_pid_limits_buffer[MAX_PROC_PID_LIMITS + 1];
  1100. int ret = 0;
  1101. bool read_limits = false;
  1102. errno = 0;
  1103. proc_pid_limits_buffer[0] = '\0';
  1104. kernel_uint_t all_fds = pid_openfds_sum(p);
  1105. if(all_fds < p->limits.max_open_files / 2 && p->io_collected_usec > p->last_limits_collected_usec && p->io_collected_usec - p->last_limits_collected_usec <= 60 * USEC_PER_SEC) {
  1106. // too frequent, we want to collect limits once per minute
  1107. ret = 1;
  1108. goto cleanup;
  1109. }
  1110. if(unlikely(!p->limits_filename)) {
  1111. char filename[FILENAME_MAX + 1];
  1112. snprintfz(filename, FILENAME_MAX, "%s/proc/%d/limits", netdata_configured_host_prefix, p->pid);
  1113. p->limits_filename = strdupz(filename);
  1114. }
  1115. int fd = open(p->limits_filename, procfile_open_flags, 0666);
  1116. if(unlikely(fd == -1)) goto cleanup;
  1117. ssize_t bytes = read(fd, proc_pid_limits_buffer, MAX_PROC_PID_LIMITS);
  1118. close(fd);
  1119. if(bytes <= 0)
  1120. goto cleanup;
  1121. // make it '\0' terminated
  1122. if(bytes < MAX_PROC_PID_LIMITS)
  1123. proc_pid_limits_buffer[bytes] = '\0';
  1124. else
  1125. proc_pid_limits_buffer[MAX_PROC_PID_LIMITS - 1] = '\0';
  1126. p->limits.max_open_files = get_proc_pid_limits_limit(proc_pid_limits_buffer, PROC_PID_LIMITS_MAX_OPEN_FILES_KEY, sizeof(PROC_PID_LIMITS_MAX_OPEN_FILES_KEY) - 1, 0);
  1127. if(p->limits.max_open_files == 1) {
  1128. // it seems a bug in the kernel or something similar
  1129. // it sets max open files to 1 but the number of files
  1130. // the process has open are more than 1...
  1131. // https://github.com/netdata/netdata/issues/15443
  1132. p->limits.max_open_files = 0;
  1133. ret = 1;
  1134. goto cleanup;
  1135. }
  1136. p->last_limits_collected_usec = p->io_collected_usec;
  1137. read_limits = true;
  1138. ret = 1;
  1139. cleanup:
  1140. if(p->limits.max_open_files)
  1141. p->openfds_limits_percent = (NETDATA_DOUBLE)all_fds * 100.0 / (NETDATA_DOUBLE)p->limits.max_open_files;
  1142. else
  1143. p->openfds_limits_percent = 0.0;
  1144. if(p->openfds_limits_percent > 100.0) {
  1145. if(!(p->log_thrown & PID_LOG_LIMITS_DETAIL)) {
  1146. char *line;
  1147. if(!read_limits) {
  1148. proc_pid_limits_buffer[0] = '\0';
  1149. line = "NOT READ";
  1150. }
  1151. else {
  1152. line = strstr(proc_pid_limits_buffer, PROC_PID_LIMITS_MAX_OPEN_FILES_KEY);
  1153. if (line) {
  1154. line++; // skip the initial newline
  1155. char *end = strchr(line, '\n');
  1156. if (end)
  1157. *end = '\0';
  1158. }
  1159. }
  1160. netdata_log_info(
  1161. "FDS_LIMITS: PID %d (%s) is using "
  1162. "%0.2f %% of its fds limits, "
  1163. "open fds = %"PRIu64 "("
  1164. "files = %"PRIu64 ", "
  1165. "pipes = %"PRIu64 ", "
  1166. "sockets = %"PRIu64", "
  1167. "inotifies = %"PRIu64", "
  1168. "eventfds = %"PRIu64", "
  1169. "timerfds = %"PRIu64", "
  1170. "signalfds = %"PRIu64", "
  1171. "eventpolls = %"PRIu64" "
  1172. "other = %"PRIu64" "
  1173. "), open fds limit = %"PRIu64", "
  1174. "%s, "
  1175. "original line [%s]",
  1176. p->pid, p->comm, p->openfds_limits_percent, all_fds,
  1177. p->openfds.files,
  1178. p->openfds.pipes,
  1179. p->openfds.sockets,
  1180. p->openfds.inotifies,
  1181. p->openfds.eventfds,
  1182. p->openfds.timerfds,
  1183. p->openfds.signalfds,
  1184. p->openfds.eventpolls,
  1185. p->openfds.other,
  1186. p->limits.max_open_files,
  1187. read_limits ? "and we have read the limits AFTER counting the fds"
  1188. : "but we have read the limits BEFORE counting the fds",
  1189. line);
  1190. p->log_thrown |= PID_LOG_LIMITS_DETAIL;
  1191. }
  1192. }
  1193. else
  1194. p->log_thrown &= ~PID_LOG_LIMITS_DETAIL;
  1195. return ret;
  1196. #endif
  1197. }
  1198. static inline int read_proc_pid_status(struct pid_stat *p, void *ptr) {
  1199. p->status_vmsize = 0;
  1200. p->status_vmrss = 0;
  1201. p->status_vmshared = 0;
  1202. p->status_rssfile = 0;
  1203. p->status_rssshmem = 0;
  1204. p->status_vmswap = 0;
  1205. p->status_voluntary_ctxt_switches = 0;
  1206. p->status_nonvoluntary_ctxt_switches = 0;
  1207. #ifdef __FreeBSD__
  1208. struct kinfo_proc *proc_info = (struct kinfo_proc *)ptr;
  1209. p->uid = proc_info->ki_uid;
  1210. p->gid = proc_info->ki_groups[0];
  1211. p->status_vmsize = proc_info->ki_size / 1024; // in KiB
  1212. p->status_vmrss = proc_info->ki_rssize * pagesize / 1024; // in KiB
  1213. // TODO: what about shared and swap memory on FreeBSD?
  1214. return 1;
  1215. #else
  1216. (void)ptr;
  1217. static struct arl_callback_ptr arl_ptr;
  1218. static procfile *ff = NULL;
  1219. if(unlikely(!p->status_arl)) {
  1220. p->status_arl = arl_create("/proc/pid/status", NULL, 60);
  1221. arl_expect_custom(p->status_arl, "Uid", arl_callback_status_uid, &arl_ptr);
  1222. arl_expect_custom(p->status_arl, "Gid", arl_callback_status_gid, &arl_ptr);
  1223. arl_expect_custom(p->status_arl, "VmSize", arl_callback_status_vmsize, &arl_ptr);
  1224. arl_expect_custom(p->status_arl, "VmRSS", arl_callback_status_vmrss, &arl_ptr);
  1225. arl_expect_custom(p->status_arl, "RssFile", arl_callback_status_rssfile, &arl_ptr);
  1226. arl_expect_custom(p->status_arl, "RssShmem", arl_callback_status_rssshmem, &arl_ptr);
  1227. arl_expect_custom(p->status_arl, "VmSwap", arl_callback_status_vmswap, &arl_ptr);
  1228. arl_expect_custom(p->status_arl, "voluntary_ctxt_switches", arl_callback_status_voluntary_ctxt_switches, &arl_ptr);
  1229. arl_expect_custom(p->status_arl, "nonvoluntary_ctxt_switches", arl_callback_status_nonvoluntary_ctxt_switches, &arl_ptr);
  1230. }
  1231. if(unlikely(!p->status_filename)) {
  1232. char filename[FILENAME_MAX + 1];
  1233. snprintfz(filename, FILENAME_MAX, "%s/proc/%d/status", netdata_configured_host_prefix, p->pid);
  1234. p->status_filename = strdupz(filename);
  1235. }
  1236. ff = procfile_reopen(ff, p->status_filename, (!ff)?" \t:,-()/":NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
  1237. if(unlikely(!ff)) return 0;
  1238. ff = procfile_readall(ff);
  1239. if(unlikely(!ff)) return 0;
  1240. calls_counter++;
  1241. // let ARL use this pid
  1242. arl_ptr.p = p;
  1243. arl_ptr.ff = ff;
  1244. size_t lines = procfile_lines(ff), l;
  1245. arl_begin(p->status_arl);
  1246. for(l = 0; l < lines ;l++) {
  1247. // debug_log("CHECK: line %zu of %zu, key '%s' = '%s'", l, lines, procfile_lineword(ff, l, 0), procfile_lineword(ff, l, 1));
  1248. arl_ptr.line = l;
  1249. if(unlikely(arl_check(p->status_arl,
  1250. procfile_lineword(ff, l, 0),
  1251. procfile_lineword(ff, l, 1)))) break;
  1252. }
  1253. p->status_vmshared = p->status_rssfile + p->status_rssshmem;
  1254. // debug_log("%s uid %d, gid %d, VmSize %zu, VmRSS %zu, RssFile %zu, RssShmem %zu, shared %zu", p->comm, (int)p->uid, (int)p->gid, p->status_vmsize, p->status_vmrss, p->status_rssfile, p->status_rssshmem, p->status_vmshared);
  1255. return 1;
  1256. #endif
  1257. }
  1258. // ----------------------------------------------------------------------------
  1259. static inline int read_proc_pid_stat(struct pid_stat *p, void *ptr) {
  1260. (void)ptr;
  1261. #ifdef __FreeBSD__
  1262. struct kinfo_proc *proc_info = (struct kinfo_proc *)ptr;
  1263. if (unlikely(proc_info->ki_tdflags & TDF_IDLETD))
  1264. goto cleanup;
  1265. #else
  1266. static procfile *ff = NULL;
  1267. if(unlikely(!p->stat_filename)) {
  1268. char filename[FILENAME_MAX + 1];
  1269. snprintfz(filename, FILENAME_MAX, "%s/proc/%d/stat", netdata_configured_host_prefix, p->pid);
  1270. p->stat_filename = strdupz(filename);
  1271. }
  1272. int set_quotes = (!ff)?1:0;
  1273. ff = procfile_reopen(ff, p->stat_filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
  1274. if(unlikely(!ff)) goto cleanup;
  1275. // if(set_quotes) procfile_set_quotes(ff, "()");
  1276. if(unlikely(set_quotes))
  1277. procfile_set_open_close(ff, "(", ")");
  1278. ff = procfile_readall(ff);
  1279. if(unlikely(!ff)) goto cleanup;
  1280. #endif
  1281. p->last_stat_collected_usec = p->stat_collected_usec;
  1282. p->stat_collected_usec = now_monotonic_usec();
  1283. calls_counter++;
  1284. #ifdef __FreeBSD__
  1285. char *comm = proc_info->ki_comm;
  1286. p->ppid = proc_info->ki_ppid;
  1287. #else
  1288. // p->pid = str2pid_t(procfile_lineword(ff, 0, 0));
  1289. char *comm = procfile_lineword(ff, 0, 1);
  1290. p->state = *(procfile_lineword(ff, 0, 2));
  1291. p->ppid = (int32_t)str2pid_t(procfile_lineword(ff, 0, 3));
  1292. // p->pgrp = (int32_t)str2pid_t(procfile_lineword(ff, 0, 4));
  1293. // p->session = (int32_t)str2pid_t(procfile_lineword(ff, 0, 5));
  1294. // p->tty_nr = (int32_t)str2pid_t(procfile_lineword(ff, 0, 6));
  1295. // p->tpgid = (int32_t)str2pid_t(procfile_lineword(ff, 0, 7));
  1296. // p->flags = str2uint64_t(procfile_lineword(ff, 0, 8));
  1297. #endif
  1298. if(strcmp(p->comm, comm) != 0) {
  1299. if(unlikely(debug_enabled)) {
  1300. if(p->comm[0])
  1301. debug_log("\tpid %d (%s) changed name to '%s'", p->pid, p->comm, comm);
  1302. else
  1303. debug_log("\tJust added %d (%s)", p->pid, comm);
  1304. }
  1305. strncpyz(p->comm, comm, MAX_COMPARE_NAME);
  1306. // /proc/<pid>/cmdline
  1307. if(likely(proc_pid_cmdline_is_needed))
  1308. managed_log(p, PID_LOG_CMDLINE, read_proc_pid_cmdline(p));
  1309. assign_target_to_pid(p);
  1310. }
  1311. #ifdef __FreeBSD__
  1312. pid_incremental_rate(stat, p->minflt, (kernel_uint_t)proc_info->ki_rusage.ru_minflt);
  1313. pid_incremental_rate(stat, p->cminflt, (kernel_uint_t)proc_info->ki_rusage_ch.ru_minflt);
  1314. pid_incremental_rate(stat, p->majflt, (kernel_uint_t)proc_info->ki_rusage.ru_majflt);
  1315. pid_incremental_rate(stat, p->cmajflt, (kernel_uint_t)proc_info->ki_rusage_ch.ru_majflt);
  1316. pid_incremental_rate(stat, p->utime, (kernel_uint_t)proc_info->ki_rusage.ru_utime.tv_sec * 100 + proc_info->ki_rusage.ru_utime.tv_usec / 10000);
  1317. pid_incremental_rate(stat, p->stime, (kernel_uint_t)proc_info->ki_rusage.ru_stime.tv_sec * 100 + proc_info->ki_rusage.ru_stime.tv_usec / 10000);
  1318. pid_incremental_rate(stat, p->cutime, (kernel_uint_t)proc_info->ki_rusage_ch.ru_utime.tv_sec * 100 + proc_info->ki_rusage_ch.ru_utime.tv_usec / 10000);
  1319. pid_incremental_rate(stat, p->cstime, (kernel_uint_t)proc_info->ki_rusage_ch.ru_stime.tv_sec * 100 + proc_info->ki_rusage_ch.ru_stime.tv_usec / 10000);
  1320. p->num_threads = proc_info->ki_numthreads;
  1321. if(enable_guest_charts) {
  1322. enable_guest_charts = 0;
  1323. netdata_log_info("Guest charts aren't supported by FreeBSD");
  1324. }
  1325. #else
  1326. pid_incremental_rate(stat, p->minflt, str2kernel_uint_t(procfile_lineword(ff, 0, 9)));
  1327. pid_incremental_rate(stat, p->cminflt, str2kernel_uint_t(procfile_lineword(ff, 0, 10)));
  1328. pid_incremental_rate(stat, p->majflt, str2kernel_uint_t(procfile_lineword(ff, 0, 11)));
  1329. pid_incremental_rate(stat, p->cmajflt, str2kernel_uint_t(procfile_lineword(ff, 0, 12)));
  1330. pid_incremental_rate(stat, p->utime, str2kernel_uint_t(procfile_lineword(ff, 0, 13)));
  1331. pid_incremental_rate(stat, p->stime, str2kernel_uint_t(procfile_lineword(ff, 0, 14)));
  1332. pid_incremental_rate(stat, p->cutime, str2kernel_uint_t(procfile_lineword(ff, 0, 15)));
  1333. pid_incremental_rate(stat, p->cstime, str2kernel_uint_t(procfile_lineword(ff, 0, 16)));
  1334. // p->priority = str2kernel_uint_t(procfile_lineword(ff, 0, 17));
  1335. // p->nice = str2kernel_uint_t(procfile_lineword(ff, 0, 18));
  1336. p->num_threads = (int32_t) str2uint32_t(procfile_lineword(ff, 0, 19), NULL);
  1337. // p->itrealvalue = str2kernel_uint_t(procfile_lineword(ff, 0, 20));
  1338. p->collected_starttime = str2kernel_uint_t(procfile_lineword(ff, 0, 21)) / system_hz;
  1339. p->uptime = (global_uptime > p->collected_starttime)?(global_uptime - p->collected_starttime):0;
  1340. // p->vsize = str2kernel_uint_t(procfile_lineword(ff, 0, 22));
  1341. // p->rss = str2kernel_uint_t(procfile_lineword(ff, 0, 23));
  1342. // p->rsslim = str2kernel_uint_t(procfile_lineword(ff, 0, 24));
  1343. // p->starcode = str2kernel_uint_t(procfile_lineword(ff, 0, 25));
  1344. // p->endcode = str2kernel_uint_t(procfile_lineword(ff, 0, 26));
  1345. // p->startstack = str2kernel_uint_t(procfile_lineword(ff, 0, 27));
  1346. // p->kstkesp = str2kernel_uint_t(procfile_lineword(ff, 0, 28));
  1347. // p->kstkeip = str2kernel_uint_t(procfile_lineword(ff, 0, 29));
  1348. // p->signal = str2kernel_uint_t(procfile_lineword(ff, 0, 30));
  1349. // p->blocked = str2kernel_uint_t(procfile_lineword(ff, 0, 31));
  1350. // p->sigignore = str2kernel_uint_t(procfile_lineword(ff, 0, 32));
  1351. // p->sigcatch = str2kernel_uint_t(procfile_lineword(ff, 0, 33));
  1352. // p->wchan = str2kernel_uint_t(procfile_lineword(ff, 0, 34));
  1353. // p->nswap = str2kernel_uint_t(procfile_lineword(ff, 0, 35));
  1354. // p->cnswap = str2kernel_uint_t(procfile_lineword(ff, 0, 36));
  1355. // p->exit_signal = str2kernel_uint_t(procfile_lineword(ff, 0, 37));
  1356. // p->processor = str2kernel_uint_t(procfile_lineword(ff, 0, 38));
  1357. // p->rt_priority = str2kernel_uint_t(procfile_lineword(ff, 0, 39));
  1358. // p->policy = str2kernel_uint_t(procfile_lineword(ff, 0, 40));
  1359. // p->delayacct_blkio_ticks = str2kernel_uint_t(procfile_lineword(ff, 0, 41));
  1360. if(enable_guest_charts) {
  1361. pid_incremental_rate(stat, p->gtime, str2kernel_uint_t(procfile_lineword(ff, 0, 42)));
  1362. pid_incremental_rate(stat, p->cgtime, str2kernel_uint_t(procfile_lineword(ff, 0, 43)));
  1363. if (show_guest_time || p->gtime || p->cgtime) {
  1364. p->utime -= (p->utime >= p->gtime) ? p->gtime : p->utime;
  1365. p->cutime -= (p->cutime >= p->cgtime) ? p->cgtime : p->cutime;
  1366. show_guest_time = 1;
  1367. }
  1368. }
  1369. #endif
  1370. if(unlikely(debug_enabled || (p->target && p->target->debug_enabled)))
  1371. debug_log_int("READ PROC/PID/STAT: %s/proc/%d/stat, process: '%s' on target '%s' (dt=%llu) VALUES: utime=" KERNEL_UINT_FORMAT ", stime=" KERNEL_UINT_FORMAT ", cutime=" KERNEL_UINT_FORMAT ", cstime=" KERNEL_UINT_FORMAT ", minflt=" KERNEL_UINT_FORMAT ", majflt=" KERNEL_UINT_FORMAT ", cminflt=" KERNEL_UINT_FORMAT ", cmajflt=" KERNEL_UINT_FORMAT ", threads=%d", netdata_configured_host_prefix, p->pid, p->comm, (p->target)?p->target->name:"UNSET", p->stat_collected_usec - p->last_stat_collected_usec, p->utime, p->stime, p->cutime, p->cstime, p->minflt, p->majflt, p->cminflt, p->cmajflt, p->num_threads);
  1372. if(unlikely(global_iterations_counter == 1)) {
  1373. p->minflt = 0;
  1374. p->cminflt = 0;
  1375. p->majflt = 0;
  1376. p->cmajflt = 0;
  1377. p->utime = 0;
  1378. p->stime = 0;
  1379. p->gtime = 0;
  1380. p->cutime = 0;
  1381. p->cstime = 0;
  1382. p->cgtime = 0;
  1383. }
  1384. #ifndef __FreeBSD__
  1385. update_proc_state_count(p->state);
  1386. #endif
  1387. return 1;
  1388. cleanup:
  1389. p->minflt = 0;
  1390. p->cminflt = 0;
  1391. p->majflt = 0;
  1392. p->cmajflt = 0;
  1393. p->utime = 0;
  1394. p->stime = 0;
  1395. p->gtime = 0;
  1396. p->cutime = 0;
  1397. p->cstime = 0;
  1398. p->cgtime = 0;
  1399. p->num_threads = 0;
  1400. // p->rss = 0;
  1401. return 0;
  1402. }
  1403. // ----------------------------------------------------------------------------
  1404. static inline int read_proc_pid_io(struct pid_stat *p, void *ptr) {
  1405. (void)ptr;
  1406. #ifdef __FreeBSD__
  1407. struct kinfo_proc *proc_info = (struct kinfo_proc *)ptr;
  1408. #else
  1409. static procfile *ff = NULL;
  1410. if(unlikely(!p->io_filename)) {
  1411. char filename[FILENAME_MAX + 1];
  1412. snprintfz(filename, FILENAME_MAX, "%s/proc/%d/io", netdata_configured_host_prefix, p->pid);
  1413. p->io_filename = strdupz(filename);
  1414. }
  1415. // open the file
  1416. ff = procfile_reopen(ff, p->io_filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
  1417. if(unlikely(!ff)) goto cleanup;
  1418. ff = procfile_readall(ff);
  1419. if(unlikely(!ff)) goto cleanup;
  1420. #endif
  1421. calls_counter++;
  1422. p->last_io_collected_usec = p->io_collected_usec;
  1423. p->io_collected_usec = now_monotonic_usec();
  1424. #ifdef __FreeBSD__
  1425. pid_incremental_rate(io, p->io_storage_bytes_read, proc_info->ki_rusage.ru_inblock);
  1426. pid_incremental_rate(io, p->io_storage_bytes_written, proc_info->ki_rusage.ru_oublock);
  1427. #else
  1428. pid_incremental_rate(io, p->io_logical_bytes_read, str2kernel_uint_t(procfile_lineword(ff, 0, 1)));
  1429. pid_incremental_rate(io, p->io_logical_bytes_written, str2kernel_uint_t(procfile_lineword(ff, 1, 1)));
  1430. pid_incremental_rate(io, p->io_read_calls, str2kernel_uint_t(procfile_lineword(ff, 2, 1)));
  1431. pid_incremental_rate(io, p->io_write_calls, str2kernel_uint_t(procfile_lineword(ff, 3, 1)));
  1432. pid_incremental_rate(io, p->io_storage_bytes_read, str2kernel_uint_t(procfile_lineword(ff, 4, 1)));
  1433. pid_incremental_rate(io, p->io_storage_bytes_written, str2kernel_uint_t(procfile_lineword(ff, 5, 1)));
  1434. pid_incremental_rate(io, p->io_cancelled_write_bytes, str2kernel_uint_t(procfile_lineword(ff, 6, 1)));
  1435. #endif
  1436. if(unlikely(global_iterations_counter == 1)) {
  1437. p->io_logical_bytes_read = 0;
  1438. p->io_logical_bytes_written = 0;
  1439. p->io_read_calls = 0;
  1440. p->io_write_calls = 0;
  1441. p->io_storage_bytes_read = 0;
  1442. p->io_storage_bytes_written = 0;
  1443. p->io_cancelled_write_bytes = 0;
  1444. }
  1445. return 1;
  1446. #ifndef __FreeBSD__
  1447. cleanup:
  1448. p->io_logical_bytes_read = 0;
  1449. p->io_logical_bytes_written = 0;
  1450. p->io_read_calls = 0;
  1451. p->io_write_calls = 0;
  1452. p->io_storage_bytes_read = 0;
  1453. p->io_storage_bytes_written = 0;
  1454. p->io_cancelled_write_bytes = 0;
  1455. return 0;
  1456. #endif
  1457. }
  1458. #ifndef __FreeBSD__
  1459. static inline int read_global_time() {
  1460. static char filename[FILENAME_MAX + 1] = "";
  1461. static procfile *ff = NULL;
  1462. static kernel_uint_t utime_raw = 0, stime_raw = 0, gtime_raw = 0, gntime_raw = 0, ntime_raw = 0;
  1463. static usec_t collected_usec = 0, last_collected_usec = 0;
  1464. if(unlikely(!ff)) {
  1465. snprintfz(filename, FILENAME_MAX, "%s/proc/stat", netdata_configured_host_prefix);
  1466. ff = procfile_open(filename, " \t:", PROCFILE_FLAG_DEFAULT);
  1467. if(unlikely(!ff)) goto cleanup;
  1468. }
  1469. ff = procfile_readall(ff);
  1470. if(unlikely(!ff)) goto cleanup;
  1471. last_collected_usec = collected_usec;
  1472. collected_usec = now_monotonic_usec();
  1473. calls_counter++;
  1474. // temporary - it is added global_ntime;
  1475. kernel_uint_t global_ntime = 0;
  1476. incremental_rate(global_utime, utime_raw, str2kernel_uint_t(procfile_lineword(ff, 0, 1)), collected_usec, last_collected_usec);
  1477. incremental_rate(global_ntime, ntime_raw, str2kernel_uint_t(procfile_lineword(ff, 0, 2)), collected_usec, last_collected_usec);
  1478. incremental_rate(global_stime, stime_raw, str2kernel_uint_t(procfile_lineword(ff, 0, 3)), collected_usec, last_collected_usec);
  1479. incremental_rate(global_gtime, gtime_raw, str2kernel_uint_t(procfile_lineword(ff, 0, 10)), collected_usec, last_collected_usec);
  1480. global_utime += global_ntime;
  1481. if(enable_guest_charts) {
  1482. // temporary - it is added global_ntime;
  1483. kernel_uint_t global_gntime = 0;
  1484. // guest nice time, on guest time
  1485. incremental_rate(global_gntime, gntime_raw, str2kernel_uint_t(procfile_lineword(ff, 0, 11)), collected_usec, last_collected_usec);
  1486. global_gtime += global_gntime;
  1487. // remove guest time from user time
  1488. global_utime -= (global_utime > global_gtime) ? global_gtime : global_utime;
  1489. }
  1490. if(unlikely(global_iterations_counter == 1)) {
  1491. global_utime = 0;
  1492. global_stime = 0;
  1493. global_gtime = 0;
  1494. }
  1495. return 1;
  1496. cleanup:
  1497. global_utime = 0;
  1498. global_stime = 0;
  1499. global_gtime = 0;
  1500. return 0;
  1501. }
  1502. #else
  1503. static inline int read_global_time() {
  1504. static kernel_uint_t utime_raw = 0, stime_raw = 0, ntime_raw = 0;
  1505. static usec_t collected_usec = 0, last_collected_usec = 0;
  1506. long cp_time[CPUSTATES];
  1507. if (unlikely(CPUSTATES != 5)) {
  1508. goto cleanup;
  1509. } else {
  1510. static int mib[2] = {0, 0};
  1511. if (unlikely(GETSYSCTL_SIMPLE("kern.cp_time", mib, cp_time))) {
  1512. goto cleanup;
  1513. }
  1514. }
  1515. last_collected_usec = collected_usec;
  1516. collected_usec = now_monotonic_usec();
  1517. calls_counter++;
  1518. // temporary - it is added global_ntime;
  1519. kernel_uint_t global_ntime = 0;
  1520. incremental_rate(global_utime, utime_raw, cp_time[0] * 100LLU / system_hz, collected_usec, last_collected_usec);
  1521. incremental_rate(global_ntime, ntime_raw, cp_time[1] * 100LLU / system_hz, collected_usec, last_collected_usec);
  1522. incremental_rate(global_stime, stime_raw, cp_time[2] * 100LLU / system_hz, collected_usec, last_collected_usec);
  1523. global_utime += global_ntime;
  1524. if(unlikely(global_iterations_counter == 1)) {
  1525. global_utime = 0;
  1526. global_stime = 0;
  1527. global_gtime = 0;
  1528. }
  1529. return 1;
  1530. cleanup:
  1531. global_utime = 0;
  1532. global_stime = 0;
  1533. global_gtime = 0;
  1534. return 0;
  1535. }
  1536. #endif /* !__FreeBSD__ */
  1537. // ----------------------------------------------------------------------------
  1538. int file_descriptor_compare(void* a, void* b) {
  1539. #ifdef NETDATA_INTERNAL_CHECKS
  1540. if(((struct file_descriptor *)a)->magic != 0x0BADCAFE || ((struct file_descriptor *)b)->magic != 0x0BADCAFE)
  1541. netdata_log_error("Corrupted index data detected. Please report this.");
  1542. #endif /* NETDATA_INTERNAL_CHECKS */
  1543. if(((struct file_descriptor *)a)->hash < ((struct file_descriptor *)b)->hash)
  1544. return -1;
  1545. else if(((struct file_descriptor *)a)->hash > ((struct file_descriptor *)b)->hash)
  1546. return 1;
  1547. else
  1548. return strcmp(((struct file_descriptor *)a)->name, ((struct file_descriptor *)b)->name);
  1549. }
  1550. // int file_descriptor_iterator(avl_t *a) { if(a) {}; return 0; }
  1551. avl_tree_type all_files_index = {
  1552. NULL,
  1553. file_descriptor_compare
  1554. };
  1555. static struct file_descriptor *file_descriptor_find(const char *name, uint32_t hash) {
  1556. struct file_descriptor tmp;
  1557. tmp.hash = (hash)?hash:simple_hash(name);
  1558. tmp.name = name;
  1559. tmp.count = 0;
  1560. tmp.pos = 0;
  1561. #ifdef NETDATA_INTERNAL_CHECKS
  1562. tmp.magic = 0x0BADCAFE;
  1563. #endif /* NETDATA_INTERNAL_CHECKS */
  1564. return (struct file_descriptor *)avl_search(&all_files_index, (avl_t *) &tmp);
  1565. }
  1566. #define file_descriptor_add(fd) avl_insert(&all_files_index, (avl_t *)(fd))
  1567. #define file_descriptor_remove(fd) avl_remove(&all_files_index, (avl_t *)(fd))
  1568. // ----------------------------------------------------------------------------
  1569. static inline void file_descriptor_not_used(int id)
  1570. {
  1571. if(id > 0 && id < all_files_size) {
  1572. #ifdef NETDATA_INTERNAL_CHECKS
  1573. if(all_files[id].magic != 0x0BADCAFE) {
  1574. netdata_log_error("Ignoring request to remove empty file id %d.", id);
  1575. return;
  1576. }
  1577. #endif /* NETDATA_INTERNAL_CHECKS */
  1578. debug_log("decreasing slot %d (count = %d).", id, all_files[id].count);
  1579. if(all_files[id].count > 0) {
  1580. all_files[id].count--;
  1581. if(!all_files[id].count) {
  1582. debug_log(" >> slot %d is empty.", id);
  1583. if(unlikely(file_descriptor_remove(&all_files[id]) != (void *)&all_files[id]))
  1584. netdata_log_error("INTERNAL ERROR: removal of unused fd from index, removed a different fd");
  1585. #ifdef NETDATA_INTERNAL_CHECKS
  1586. all_files[id].magic = 0x00000000;
  1587. #endif /* NETDATA_INTERNAL_CHECKS */
  1588. all_files_len--;
  1589. }
  1590. }
  1591. else
  1592. netdata_log_error("Request to decrease counter of fd %d (%s), while the use counter is 0",
  1593. id,
  1594. all_files[id].name);
  1595. }
  1596. else
  1597. netdata_log_error("Request to decrease counter of fd %d, which is outside the array size (1 to %d)",
  1598. id,
  1599. all_files_size);
  1600. }
  1601. static inline void all_files_grow() {
  1602. void *old = all_files;
  1603. int i;
  1604. // there is no empty slot
  1605. debug_log("extending fd array to %d entries", all_files_size + FILE_DESCRIPTORS_INCREASE_STEP);
  1606. all_files = reallocz(all_files, (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP) * sizeof(struct file_descriptor));
  1607. // if the address changed, we have to rebuild the index
  1608. // since all pointers are now invalid
  1609. if(unlikely(old && old != (void *)all_files)) {
  1610. debug_log(" >> re-indexing.");
  1611. all_files_index.root = NULL;
  1612. for(i = 0; i < all_files_size; i++) {
  1613. if(!all_files[i].count) continue;
  1614. if(unlikely(file_descriptor_add(&all_files[i]) != (void *)&all_files[i]))
  1615. netdata_log_error("INTERNAL ERROR: duplicate indexing of fd during realloc.");
  1616. }
  1617. debug_log(" >> re-indexing done.");
  1618. }
  1619. // initialize the newly added entries
  1620. for(i = all_files_size; i < (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP); i++) {
  1621. all_files[i].count = 0;
  1622. all_files[i].name = NULL;
  1623. #ifdef NETDATA_INTERNAL_CHECKS
  1624. all_files[i].magic = 0x00000000;
  1625. #endif /* NETDATA_INTERNAL_CHECKS */
  1626. all_files[i].pos = i;
  1627. }
  1628. if(unlikely(!all_files_size)) all_files_len = 1;
  1629. all_files_size += FILE_DESCRIPTORS_INCREASE_STEP;
  1630. }
  1631. static inline int file_descriptor_set_on_empty_slot(const char *name, uint32_t hash, FD_FILETYPE type) {
  1632. // check we have enough memory to add it
  1633. if(!all_files || all_files_len == all_files_size)
  1634. all_files_grow();
  1635. debug_log(" >> searching for empty slot.");
  1636. // search for an empty slot
  1637. static int last_pos = 0;
  1638. int i, c;
  1639. for(i = 0, c = last_pos ; i < all_files_size ; i++, c++) {
  1640. if(c >= all_files_size) c = 0;
  1641. if(c == 0) continue;
  1642. if(!all_files[c].count) {
  1643. debug_log(" >> Examining slot %d.", c);
  1644. #ifdef NETDATA_INTERNAL_CHECKS
  1645. if(all_files[c].magic == 0x0BADCAFE && all_files[c].name && file_descriptor_find(all_files[c].name, all_files[c].hash))
  1646. netdata_log_error("fd on position %d is not cleared properly. It still has %s in it.", c, all_files[c].name);
  1647. #endif /* NETDATA_INTERNAL_CHECKS */
  1648. debug_log(" >> %s fd position %d for %s (last name: %s)", all_files[c].name?"re-using":"using", c, name, all_files[c].name);
  1649. freez((void *)all_files[c].name);
  1650. all_files[c].name = NULL;
  1651. last_pos = c;
  1652. break;
  1653. }
  1654. }
  1655. all_files_len++;
  1656. if(i == all_files_size) {
  1657. fatal("We should find an empty slot, but there isn't any");
  1658. exit(1);
  1659. }
  1660. // else we have an empty slot in 'c'
  1661. debug_log(" >> updating slot %d.", c);
  1662. all_files[c].name = strdupz(name);
  1663. all_files[c].hash = hash;
  1664. all_files[c].type = type;
  1665. all_files[c].pos = c;
  1666. all_files[c].count = 1;
  1667. #ifdef NETDATA_INTERNAL_CHECKS
  1668. all_files[c].magic = 0x0BADCAFE;
  1669. #endif /* NETDATA_INTERNAL_CHECKS */
  1670. if(unlikely(file_descriptor_add(&all_files[c]) != (void *)&all_files[c]))
  1671. netdata_log_error("INTERNAL ERROR: duplicate indexing of fd.");
  1672. debug_log("using fd position %d (name: %s)", c, all_files[c].name);
  1673. return c;
  1674. }
  1675. static inline int file_descriptor_find_or_add(const char *name, uint32_t hash) {
  1676. if(unlikely(!hash))
  1677. hash = simple_hash(name);
  1678. debug_log("adding or finding name '%s' with hash %u", name, hash);
  1679. struct file_descriptor *fd = file_descriptor_find(name, hash);
  1680. if(fd) {
  1681. // found
  1682. debug_log(" >> found on slot %d", fd->pos);
  1683. fd->count++;
  1684. return fd->pos;
  1685. }
  1686. // not found
  1687. FD_FILETYPE type;
  1688. if(likely(name[0] == '/')) type = FILETYPE_FILE;
  1689. else if(likely(strncmp(name, "pipe:", 5) == 0)) type = FILETYPE_PIPE;
  1690. else if(likely(strncmp(name, "socket:", 7) == 0)) type = FILETYPE_SOCKET;
  1691. else if(likely(strncmp(name, "anon_inode:", 11) == 0)) {
  1692. const char *t = &name[11];
  1693. if(strcmp(t, "inotify") == 0) type = FILETYPE_INOTIFY;
  1694. else if(strcmp(t, "[eventfd]") == 0) type = FILETYPE_EVENTFD;
  1695. else if(strcmp(t, "[eventpoll]") == 0) type = FILETYPE_EVENTPOLL;
  1696. else if(strcmp(t, "[timerfd]") == 0) type = FILETYPE_TIMERFD;
  1697. else if(strcmp(t, "[signalfd]") == 0) type = FILETYPE_SIGNALFD;
  1698. else {
  1699. debug_log("UNKNOWN anonymous inode: %s", name);
  1700. type = FILETYPE_OTHER;
  1701. }
  1702. }
  1703. else if(likely(strcmp(name, "inotify") == 0)) type = FILETYPE_INOTIFY;
  1704. else {
  1705. debug_log("UNKNOWN linkname: %s", name);
  1706. type = FILETYPE_OTHER;
  1707. }
  1708. return file_descriptor_set_on_empty_slot(name, hash, type);
  1709. }
  1710. static inline void clear_pid_fd(struct pid_fd *pfd) {
  1711. pfd->fd = 0;
  1712. #ifndef __FreeBSD__
  1713. pfd->link_hash = 0;
  1714. pfd->inode = 0;
  1715. pfd->cache_iterations_counter = 0;
  1716. pfd->cache_iterations_reset = 0;
  1717. #endif
  1718. }
  1719. static inline void make_all_pid_fds_negative(struct pid_stat *p) {
  1720. struct pid_fd *pfd = p->fds, *pfdend = &p->fds[p->fds_size];
  1721. while(pfd < pfdend) {
  1722. pfd->fd = -(pfd->fd);
  1723. pfd++;
  1724. }
  1725. }
  1726. static inline void cleanup_negative_pid_fds(struct pid_stat *p) {
  1727. struct pid_fd *pfd = p->fds, *pfdend = &p->fds[p->fds_size];
  1728. while(pfd < pfdend) {
  1729. int fd = pfd->fd;
  1730. if(unlikely(fd < 0)) {
  1731. file_descriptor_not_used(-(fd));
  1732. clear_pid_fd(pfd);
  1733. }
  1734. pfd++;
  1735. }
  1736. }
  1737. static inline void init_pid_fds(struct pid_stat *p, size_t first, size_t size) {
  1738. struct pid_fd *pfd = &p->fds[first], *pfdend = &p->fds[first + size];
  1739. while(pfd < pfdend) {
  1740. #ifndef __FreeBSD__
  1741. pfd->filename = NULL;
  1742. #endif
  1743. clear_pid_fd(pfd);
  1744. pfd++;
  1745. }
  1746. }
  1747. static inline int read_pid_file_descriptors(struct pid_stat *p, void *ptr) {
  1748. (void)ptr;
  1749. #ifdef __FreeBSD__
  1750. int mib[4];
  1751. size_t size;
  1752. struct kinfo_file *fds;
  1753. static char *fdsbuf;
  1754. char *bfdsbuf, *efdsbuf;
  1755. char fdsname[FILENAME_MAX + 1];
  1756. #define SHM_FORMAT_LEN 31 // format: 21 + size: 10
  1757. char shm_name[FILENAME_MAX - SHM_FORMAT_LEN + 1];
  1758. // we make all pid fds negative, so that
  1759. // we can detect unused file descriptors
  1760. // at the end, to free them
  1761. make_all_pid_fds_negative(p);
  1762. mib[0] = CTL_KERN;
  1763. mib[1] = KERN_PROC;
  1764. mib[2] = KERN_PROC_FILEDESC;
  1765. mib[3] = p->pid;
  1766. if (unlikely(sysctl(mib, 4, NULL, &size, NULL, 0))) {
  1767. netdata_log_error("sysctl error: Can't get file descriptors data size for pid %d", p->pid);
  1768. return 0;
  1769. }
  1770. if (likely(size > 0))
  1771. fdsbuf = reallocz(fdsbuf, size);
  1772. if (unlikely(sysctl(mib, 4, fdsbuf, &size, NULL, 0))) {
  1773. netdata_log_error("sysctl error: Can't get file descriptors data for pid %d", p->pid);
  1774. return 0;
  1775. }
  1776. bfdsbuf = fdsbuf;
  1777. efdsbuf = fdsbuf + size;
  1778. while (bfdsbuf < efdsbuf) {
  1779. fds = (struct kinfo_file *)(uintptr_t)bfdsbuf;
  1780. if (unlikely(fds->kf_structsize == 0))
  1781. break;
  1782. // do not process file descriptors for current working directory, root directory,
  1783. // jail directory, ktrace vnode, text vnode and controlling terminal
  1784. if (unlikely(fds->kf_fd < 0)) {
  1785. bfdsbuf += fds->kf_structsize;
  1786. continue;
  1787. }
  1788. // get file descriptors array index
  1789. size_t fdid = fds->kf_fd;
  1790. // check if the fds array is small
  1791. if (unlikely(fdid >= p->fds_size)) {
  1792. // it is small, extend it
  1793. debug_log("extending fd memory slots for %s from %d to %d", p->comm, p->fds_size, fdid + MAX_SPARE_FDS);
  1794. p->fds = reallocz(p->fds, (fdid + MAX_SPARE_FDS) * sizeof(struct pid_fd));
  1795. // and initialize it
  1796. init_pid_fds(p, p->fds_size, (fdid + MAX_SPARE_FDS) - p->fds_size);
  1797. p->fds_size = fdid + MAX_SPARE_FDS;
  1798. }
  1799. if (unlikely(p->fds[fdid].fd == 0)) {
  1800. // we don't know this fd, get it
  1801. switch (fds->kf_type) {
  1802. case KF_TYPE_FIFO:
  1803. case KF_TYPE_VNODE:
  1804. if (unlikely(!fds->kf_path[0])) {
  1805. sprintf(fdsname, "other: inode: %lu", fds->kf_un.kf_file.kf_file_fileid);
  1806. break;
  1807. }
  1808. sprintf(fdsname, "%s", fds->kf_path);
  1809. break;
  1810. case KF_TYPE_SOCKET:
  1811. switch (fds->kf_sock_domain) {
  1812. case AF_INET:
  1813. case AF_INET6:
  1814. if (fds->kf_sock_protocol == IPPROTO_TCP)
  1815. sprintf(fdsname, "socket: %d %lx", fds->kf_sock_protocol, fds->kf_un.kf_sock.kf_sock_inpcb);
  1816. else
  1817. sprintf(fdsname, "socket: %d %lx", fds->kf_sock_protocol, fds->kf_un.kf_sock.kf_sock_pcb);
  1818. break;
  1819. case AF_UNIX:
  1820. /* print address of pcb and connected pcb */
  1821. sprintf(fdsname, "socket: %lx %lx", fds->kf_un.kf_sock.kf_sock_pcb, fds->kf_un.kf_sock.kf_sock_unpconn);
  1822. break;
  1823. default:
  1824. /* print protocol number and socket address */
  1825. #if __FreeBSD_version < 1200031
  1826. sprintf(fdsname, "socket: other: %d %s %s", fds->kf_sock_protocol, fds->kf_sa_local.__ss_pad1, fds->kf_sa_local.__ss_pad2);
  1827. #else
  1828. sprintf(fdsname, "socket: other: %d %s %s", fds->kf_sock_protocol, fds->kf_un.kf_sock.kf_sa_local.__ss_pad1, fds->kf_un.kf_sock.kf_sa_local.__ss_pad2);
  1829. #endif
  1830. }
  1831. break;
  1832. case KF_TYPE_PIPE:
  1833. sprintf(fdsname, "pipe: %lu %lu", fds->kf_un.kf_pipe.kf_pipe_addr, fds->kf_un.kf_pipe.kf_pipe_peer);
  1834. break;
  1835. case KF_TYPE_PTS:
  1836. #if __FreeBSD_version < 1200031
  1837. sprintf(fdsname, "other: pts: %u", fds->kf_un.kf_pts.kf_pts_dev);
  1838. #else
  1839. sprintf(fdsname, "other: pts: %lu", fds->kf_un.kf_pts.kf_pts_dev);
  1840. #endif
  1841. break;
  1842. case KF_TYPE_SHM:
  1843. strncpyz(shm_name, fds->kf_path, FILENAME_MAX - SHM_FORMAT_LEN);
  1844. sprintf(fdsname, "other: shm: %s size: %lu", shm_name, fds->kf_un.kf_file.kf_file_size);
  1845. break;
  1846. case KF_TYPE_SEM:
  1847. sprintf(fdsname, "other: sem: %u", fds->kf_un.kf_sem.kf_sem_value);
  1848. break;
  1849. default:
  1850. sprintf(fdsname, "other: pid: %d fd: %d", fds->kf_un.kf_proc.kf_pid, fds->kf_fd);
  1851. }
  1852. // if another process already has this, we will get
  1853. // the same id
  1854. p->fds[fdid].fd = file_descriptor_find_or_add(fdsname, 0);
  1855. }
  1856. // else make it positive again, we need it
  1857. // of course, the actual file may have changed
  1858. else
  1859. p->fds[fdid].fd = -p->fds[fdid].fd;
  1860. bfdsbuf += fds->kf_structsize;
  1861. }
  1862. #else
  1863. if(unlikely(!p->fds_dirname)) {
  1864. char dirname[FILENAME_MAX+1];
  1865. snprintfz(dirname, FILENAME_MAX, "%s/proc/%d/fd", netdata_configured_host_prefix, p->pid);
  1866. p->fds_dirname = strdupz(dirname);
  1867. }
  1868. DIR *fds = opendir(p->fds_dirname);
  1869. if(unlikely(!fds)) return 0;
  1870. struct dirent *de;
  1871. char linkname[FILENAME_MAX + 1];
  1872. // we make all pid fds negative, so that
  1873. // we can detect unused file descriptors
  1874. // at the end, to free them
  1875. make_all_pid_fds_negative(p);
  1876. while((de = readdir(fds))) {
  1877. // we need only files with numeric names
  1878. if(unlikely(de->d_name[0] < '0' || de->d_name[0] > '9'))
  1879. continue;
  1880. // get its number
  1881. int fdid = (int) str2l(de->d_name);
  1882. if(unlikely(fdid < 0)) continue;
  1883. // check if the fds array is small
  1884. if(unlikely((size_t)fdid >= p->fds_size)) {
  1885. // it is small, extend it
  1886. debug_log("extending fd memory slots for %s from %d to %d"
  1887. , p->comm
  1888. , p->fds_size
  1889. , fdid + MAX_SPARE_FDS
  1890. );
  1891. p->fds = reallocz(p->fds, (fdid + MAX_SPARE_FDS) * sizeof(struct pid_fd));
  1892. // and initialize it
  1893. init_pid_fds(p, p->fds_size, (fdid + MAX_SPARE_FDS) - p->fds_size);
  1894. p->fds_size = (size_t)fdid + MAX_SPARE_FDS;
  1895. }
  1896. if(unlikely(p->fds[fdid].fd < 0 && de->d_ino != p->fds[fdid].inode)) {
  1897. // inodes do not match, clear the previous entry
  1898. inodes_changed_counter++;
  1899. file_descriptor_not_used(-p->fds[fdid].fd);
  1900. clear_pid_fd(&p->fds[fdid]);
  1901. }
  1902. if(p->fds[fdid].fd < 0 && p->fds[fdid].cache_iterations_counter > 0) {
  1903. p->fds[fdid].fd = -p->fds[fdid].fd;
  1904. p->fds[fdid].cache_iterations_counter--;
  1905. continue;
  1906. }
  1907. if(unlikely(!p->fds[fdid].filename)) {
  1908. filenames_allocated_counter++;
  1909. char fdname[FILENAME_MAX + 1];
  1910. snprintfz(fdname, FILENAME_MAX, "%s/proc/%d/fd/%s", netdata_configured_host_prefix, p->pid, de->d_name);
  1911. p->fds[fdid].filename = strdupz(fdname);
  1912. }
  1913. file_counter++;
  1914. ssize_t l = readlink(p->fds[fdid].filename, linkname, FILENAME_MAX);
  1915. if(unlikely(l == -1)) {
  1916. // cannot read the link
  1917. if(debug_enabled || (p->target && p->target->debug_enabled))
  1918. netdata_log_error("Cannot read link %s", p->fds[fdid].filename);
  1919. if(unlikely(p->fds[fdid].fd < 0)) {
  1920. file_descriptor_not_used(-p->fds[fdid].fd);
  1921. clear_pid_fd(&p->fds[fdid]);
  1922. }
  1923. continue;
  1924. }
  1925. else
  1926. linkname[l] = '\0';
  1927. uint32_t link_hash = simple_hash(linkname);
  1928. if(unlikely(p->fds[fdid].fd < 0 && p->fds[fdid].link_hash != link_hash)) {
  1929. // the link changed
  1930. links_changed_counter++;
  1931. file_descriptor_not_used(-p->fds[fdid].fd);
  1932. clear_pid_fd(&p->fds[fdid]);
  1933. }
  1934. if(unlikely(p->fds[fdid].fd == 0)) {
  1935. // we don't know this fd, get it
  1936. // if another process already has this, we will get
  1937. // the same id
  1938. p->fds[fdid].fd = file_descriptor_find_or_add(linkname, link_hash);
  1939. p->fds[fdid].inode = de->d_ino;
  1940. p->fds[fdid].link_hash = link_hash;
  1941. }
  1942. else {
  1943. // else make it positive again, we need it
  1944. p->fds[fdid].fd = -p->fds[fdid].fd;
  1945. }
  1946. // caching control
  1947. // without this we read all the files on every iteration
  1948. if(max_fds_cache_seconds > 0) {
  1949. size_t spread = ((size_t)max_fds_cache_seconds > 10) ? 10 : (size_t)max_fds_cache_seconds;
  1950. // cache it for a few iterations
  1951. size_t max = ((size_t) max_fds_cache_seconds + (fdid % spread)) / (size_t) update_every;
  1952. p->fds[fdid].cache_iterations_reset++;
  1953. if(unlikely(p->fds[fdid].cache_iterations_reset % spread == (size_t) fdid % spread))
  1954. p->fds[fdid].cache_iterations_reset++;
  1955. if(unlikely((fdid <= 2 && p->fds[fdid].cache_iterations_reset > 5) ||
  1956. p->fds[fdid].cache_iterations_reset > max)) {
  1957. // for stdin, stdout, stderr (fdid <= 2) we have checked a few times, or if it goes above the max, goto max
  1958. p->fds[fdid].cache_iterations_reset = max;
  1959. }
  1960. p->fds[fdid].cache_iterations_counter = p->fds[fdid].cache_iterations_reset;
  1961. }
  1962. }
  1963. closedir(fds);
  1964. #endif
  1965. cleanup_negative_pid_fds(p);
  1966. return 1;
  1967. }
  1968. // ----------------------------------------------------------------------------
  1969. static inline int debug_print_process_and_parents(struct pid_stat *p, usec_t time) {
  1970. char *prefix = "\\_ ";
  1971. int indent = 0;
  1972. if(p->parent)
  1973. indent = debug_print_process_and_parents(p->parent, p->stat_collected_usec);
  1974. else
  1975. prefix = " > ";
  1976. char buffer[indent + 1];
  1977. int i;
  1978. for(i = 0; i < indent ;i++) buffer[i] = ' ';
  1979. buffer[i] = '\0';
  1980. fprintf(stderr, " %s %s%s (%d %s %"PRIu64""
  1981. , buffer
  1982. , prefix
  1983. , p->comm
  1984. , p->pid
  1985. , p->updated?"running":"exited"
  1986. , p->stat_collected_usec - time
  1987. );
  1988. if(p->utime) fprintf(stderr, " utime=" KERNEL_UINT_FORMAT, p->utime);
  1989. if(p->stime) fprintf(stderr, " stime=" KERNEL_UINT_FORMAT, p->stime);
  1990. if(p->gtime) fprintf(stderr, " gtime=" KERNEL_UINT_FORMAT, p->gtime);
  1991. if(p->cutime) fprintf(stderr, " cutime=" KERNEL_UINT_FORMAT, p->cutime);
  1992. if(p->cstime) fprintf(stderr, " cstime=" KERNEL_UINT_FORMAT, p->cstime);
  1993. if(p->cgtime) fprintf(stderr, " cgtime=" KERNEL_UINT_FORMAT, p->cgtime);
  1994. if(p->minflt) fprintf(stderr, " minflt=" KERNEL_UINT_FORMAT, p->minflt);
  1995. if(p->cminflt) fprintf(stderr, " cminflt=" KERNEL_UINT_FORMAT, p->cminflt);
  1996. if(p->majflt) fprintf(stderr, " majflt=" KERNEL_UINT_FORMAT, p->majflt);
  1997. if(p->cmajflt) fprintf(stderr, " cmajflt=" KERNEL_UINT_FORMAT, p->cmajflt);
  1998. fprintf(stderr, ")\n");
  1999. return indent + 1;
  2000. }
  2001. static inline void debug_print_process_tree(struct pid_stat *p, char *msg __maybe_unused) {
  2002. debug_log("%s: process %s (%d, %s) with parents:", msg, p->comm, p->pid, p->updated?"running":"exited");
  2003. debug_print_process_and_parents(p, p->stat_collected_usec);
  2004. }
  2005. static inline void debug_find_lost_child(struct pid_stat *pe, kernel_uint_t lost, int type) {
  2006. int found = 0;
  2007. struct pid_stat *p = NULL;
  2008. for(p = root_of_pids; p ; p = p->next) {
  2009. if(p == pe) continue;
  2010. switch(type) {
  2011. case 1:
  2012. if(p->cminflt > lost) {
  2013. fprintf(stderr, " > process %d (%s) could use the lost exited child minflt " KERNEL_UINT_FORMAT " of process %d (%s)\n", p->pid, p->comm, lost, pe->pid, pe->comm);
  2014. found++;
  2015. }
  2016. break;
  2017. case 2:
  2018. if(p->cmajflt > lost) {
  2019. fprintf(stderr, " > process %d (%s) could use the lost exited child majflt " KERNEL_UINT_FORMAT " of process %d (%s)\n", p->pid, p->comm, lost, pe->pid, pe->comm);
  2020. found++;
  2021. }
  2022. break;
  2023. case 3:
  2024. if(p->cutime > lost) {
  2025. fprintf(stderr, " > process %d (%s) could use the lost exited child utime " KERNEL_UINT_FORMAT " of process %d (%s)\n", p->pid, p->comm, lost, pe->pid, pe->comm);
  2026. found++;
  2027. }
  2028. break;
  2029. case 4:
  2030. if(p->cstime > lost) {
  2031. fprintf(stderr, " > process %d (%s) could use the lost exited child stime " KERNEL_UINT_FORMAT " of process %d (%s)\n", p->pid, p->comm, lost, pe->pid, pe->comm);
  2032. found++;
  2033. }
  2034. break;
  2035. case 5:
  2036. if(p->cgtime > lost) {
  2037. fprintf(stderr, " > process %d (%s) could use the lost exited child gtime " KERNEL_UINT_FORMAT " of process %d (%s)\n", p->pid, p->comm, lost, pe->pid, pe->comm);
  2038. found++;
  2039. }
  2040. break;
  2041. }
  2042. }
  2043. if(!found) {
  2044. switch(type) {
  2045. case 1:
  2046. fprintf(stderr, " > cannot find any process to use the lost exited child minflt " KERNEL_UINT_FORMAT " of process %d (%s)\n", lost, pe->pid, pe->comm);
  2047. break;
  2048. case 2:
  2049. fprintf(stderr, " > cannot find any process to use the lost exited child majflt " KERNEL_UINT_FORMAT " of process %d (%s)\n", lost, pe->pid, pe->comm);
  2050. break;
  2051. case 3:
  2052. fprintf(stderr, " > cannot find any process to use the lost exited child utime " KERNEL_UINT_FORMAT " of process %d (%s)\n", lost, pe->pid, pe->comm);
  2053. break;
  2054. case 4:
  2055. fprintf(stderr, " > cannot find any process to use the lost exited child stime " KERNEL_UINT_FORMAT " of process %d (%s)\n", lost, pe->pid, pe->comm);
  2056. break;
  2057. case 5:
  2058. fprintf(stderr, " > cannot find any process to use the lost exited child gtime " KERNEL_UINT_FORMAT " of process %d (%s)\n", lost, pe->pid, pe->comm);
  2059. break;
  2060. }
  2061. }
  2062. }
  2063. static inline kernel_uint_t remove_exited_child_from_parent(kernel_uint_t *field, kernel_uint_t *pfield) {
  2064. kernel_uint_t absorbed = 0;
  2065. if(*field > *pfield) {
  2066. absorbed += *pfield;
  2067. *field -= *pfield;
  2068. *pfield = 0;
  2069. }
  2070. else {
  2071. absorbed += *field;
  2072. *pfield -= *field;
  2073. *field = 0;
  2074. }
  2075. return absorbed;
  2076. }
  2077. static inline void process_exited_processes() {
  2078. struct pid_stat *p;
  2079. for(p = root_of_pids; p ; p = p->next) {
  2080. if(p->updated || !p->stat_collected_usec)
  2081. continue;
  2082. kernel_uint_t utime = (p->utime_raw + p->cutime_raw) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
  2083. kernel_uint_t stime = (p->stime_raw + p->cstime_raw) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
  2084. kernel_uint_t gtime = (p->gtime_raw + p->cgtime_raw) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
  2085. kernel_uint_t minflt = (p->minflt_raw + p->cminflt_raw) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
  2086. kernel_uint_t majflt = (p->majflt_raw + p->cmajflt_raw) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
  2087. if(utime + stime + gtime + minflt + majflt == 0)
  2088. continue;
  2089. if(unlikely(debug_enabled)) {
  2090. debug_log("Absorb %s (%d %s total resources: utime=" KERNEL_UINT_FORMAT " stime=" KERNEL_UINT_FORMAT " gtime=" KERNEL_UINT_FORMAT " minflt=" KERNEL_UINT_FORMAT " majflt=" KERNEL_UINT_FORMAT ")"
  2091. , p->comm
  2092. , p->pid
  2093. , p->updated?"running":"exited"
  2094. , utime
  2095. , stime
  2096. , gtime
  2097. , minflt
  2098. , majflt
  2099. );
  2100. debug_print_process_tree(p, "Searching parents");
  2101. }
  2102. struct pid_stat *pp;
  2103. for(pp = p->parent; pp ; pp = pp->parent) {
  2104. if(!pp->updated) continue;
  2105. kernel_uint_t absorbed;
  2106. absorbed = remove_exited_child_from_parent(&utime, &pp->cutime);
  2107. if(unlikely(debug_enabled && absorbed))
  2108. debug_log(" > process %s (%d %s) absorbed " KERNEL_UINT_FORMAT " utime (remaining: " KERNEL_UINT_FORMAT ")", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, utime);
  2109. absorbed = remove_exited_child_from_parent(&stime, &pp->cstime);
  2110. if(unlikely(debug_enabled && absorbed))
  2111. debug_log(" > process %s (%d %s) absorbed " KERNEL_UINT_FORMAT " stime (remaining: " KERNEL_UINT_FORMAT ")", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, stime);
  2112. absorbed = remove_exited_child_from_parent(&gtime, &pp->cgtime);
  2113. if(unlikely(debug_enabled && absorbed))
  2114. debug_log(" > process %s (%d %s) absorbed " KERNEL_UINT_FORMAT " gtime (remaining: " KERNEL_UINT_FORMAT ")", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, gtime);
  2115. absorbed = remove_exited_child_from_parent(&minflt, &pp->cminflt);
  2116. if(unlikely(debug_enabled && absorbed))
  2117. debug_log(" > process %s (%d %s) absorbed " KERNEL_UINT_FORMAT " minflt (remaining: " KERNEL_UINT_FORMAT ")", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, minflt);
  2118. absorbed = remove_exited_child_from_parent(&majflt, &pp->cmajflt);
  2119. if(unlikely(debug_enabled && absorbed))
  2120. debug_log(" > process %s (%d %s) absorbed " KERNEL_UINT_FORMAT " majflt (remaining: " KERNEL_UINT_FORMAT ")", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, majflt);
  2121. }
  2122. if(unlikely(utime + stime + gtime + minflt + majflt > 0)) {
  2123. if(unlikely(debug_enabled)) {
  2124. if(utime) debug_find_lost_child(p, utime, 3);
  2125. if(stime) debug_find_lost_child(p, stime, 4);
  2126. if(gtime) debug_find_lost_child(p, gtime, 5);
  2127. if(minflt) debug_find_lost_child(p, minflt, 1);
  2128. if(majflt) debug_find_lost_child(p, majflt, 2);
  2129. }
  2130. p->keep = true;
  2131. debug_log(" > remaining resources - KEEP - for another loop: %s (%d %s total resources: utime=" KERNEL_UINT_FORMAT " stime=" KERNEL_UINT_FORMAT " gtime=" KERNEL_UINT_FORMAT " minflt=" KERNEL_UINT_FORMAT " majflt=" KERNEL_UINT_FORMAT ")"
  2132. , p->comm
  2133. , p->pid
  2134. , p->updated?"running":"exited"
  2135. , utime
  2136. , stime
  2137. , gtime
  2138. , minflt
  2139. , majflt
  2140. );
  2141. for(pp = p->parent; pp ; pp = pp->parent) {
  2142. if(pp->updated) break;
  2143. pp->keep = true;
  2144. debug_log(" > - KEEP - parent for another loop: %s (%d %s)"
  2145. , pp->comm
  2146. , pp->pid
  2147. , pp->updated?"running":"exited"
  2148. );
  2149. }
  2150. p->utime_raw = utime * (p->stat_collected_usec - p->last_stat_collected_usec) / (USEC_PER_SEC * RATES_DETAIL);
  2151. p->stime_raw = stime * (p->stat_collected_usec - p->last_stat_collected_usec) / (USEC_PER_SEC * RATES_DETAIL);
  2152. p->gtime_raw = gtime * (p->stat_collected_usec - p->last_stat_collected_usec) / (USEC_PER_SEC * RATES_DETAIL);
  2153. p->minflt_raw = minflt * (p->stat_collected_usec - p->last_stat_collected_usec) / (USEC_PER_SEC * RATES_DETAIL);
  2154. p->majflt_raw = majflt * (p->stat_collected_usec - p->last_stat_collected_usec) / (USEC_PER_SEC * RATES_DETAIL);
  2155. p->cutime_raw = p->cstime_raw = p->cgtime_raw = p->cminflt_raw = p->cmajflt_raw = 0;
  2156. debug_log(" ");
  2157. }
  2158. else
  2159. debug_log(" > totally absorbed - DONE - %s (%d %s)"
  2160. , p->comm
  2161. , p->pid
  2162. , p->updated?"running":"exited"
  2163. );
  2164. }
  2165. }
  2166. static inline void link_all_processes_to_their_parents(void) {
  2167. struct pid_stat *p, *pp;
  2168. // link all children to their parents
  2169. // and update children count on parents
  2170. for(p = root_of_pids; p ; p = p->next) {
  2171. // for each process found
  2172. p->sortlist = 0;
  2173. p->parent = NULL;
  2174. if(unlikely(!p->ppid)) {
  2175. //unnecessary code from apps_plugin.c
  2176. //p->parent = NULL;
  2177. continue;
  2178. }
  2179. pp = all_pids[p->ppid];
  2180. if(likely(pp)) {
  2181. p->parent = pp;
  2182. pp->children_count++;
  2183. if(unlikely(debug_enabled || (p->target && p->target->debug_enabled)))
  2184. debug_log_int("child %d (%s, %s) on target '%s' has parent %d (%s, %s). Parent: utime=" KERNEL_UINT_FORMAT ", stime=" KERNEL_UINT_FORMAT ", gtime=" KERNEL_UINT_FORMAT ", minflt=" KERNEL_UINT_FORMAT ", majflt=" KERNEL_UINT_FORMAT ", cutime=" KERNEL_UINT_FORMAT ", cstime=" KERNEL_UINT_FORMAT ", cgtime=" KERNEL_UINT_FORMAT ", cminflt=" KERNEL_UINT_FORMAT ", cmajflt=" KERNEL_UINT_FORMAT "", p->pid, p->comm, p->updated?"running":"exited", (p->target)?p->target->name:"UNSET", pp->pid, pp->comm, pp->updated?"running":"exited", pp->utime, pp->stime, pp->gtime, pp->minflt, pp->majflt, pp->cutime, pp->cstime, pp->cgtime, pp->cminflt, pp->cmajflt);
  2185. }
  2186. else {
  2187. p->parent = NULL;
  2188. netdata_log_error("pid %d %s states parent %d, but the later does not exist.", p->pid, p->comm, p->ppid);
  2189. }
  2190. }
  2191. }
  2192. // ----------------------------------------------------------------------------
  2193. // 1. read all files in /proc
  2194. // 2. for each numeric directory:
  2195. // i. read /proc/pid/stat
  2196. // ii. read /proc/pid/status
  2197. // iii. read /proc/pid/io (requires root access)
  2198. // iii. read the entries in directory /proc/pid/fd (requires root access)
  2199. // for each entry:
  2200. // a. find or create a struct file_descriptor
  2201. // b. cleanup any old/unused file_descriptors
  2202. // after all these, some pids may be linked to targets, while others may not
  2203. // in case of errors, only 1 every 1000 errors is printed
  2204. // to avoid filling up all disk space
  2205. // if debug is enabled, all errors are printed
  2206. #if (ALL_PIDS_ARE_READ_INSTANTLY == 0)
  2207. static int compar_pid(const void *pid1, const void *pid2) {
  2208. struct pid_stat *p1 = all_pids[*((pid_t *)pid1)];
  2209. struct pid_stat *p2 = all_pids[*((pid_t *)pid2)];
  2210. if(p1->sortlist > p2->sortlist)
  2211. return -1;
  2212. else
  2213. return 1;
  2214. }
  2215. #endif
  2216. static inline int collect_data_for_pid(pid_t pid, void *ptr) {
  2217. if(unlikely(pid < 0 || pid > pid_max)) {
  2218. netdata_log_error("Invalid pid %d read (expected %d to %d). Ignoring process.", pid, 0, pid_max);
  2219. return 0;
  2220. }
  2221. struct pid_stat *p = get_pid_entry(pid);
  2222. if(unlikely(!p || p->read)) return 0;
  2223. p->read = true;
  2224. // debug_log("Reading process %d (%s), sortlist %d", p->pid, p->comm, p->sortlist);
  2225. // --------------------------------------------------------------------
  2226. // /proc/<pid>/stat
  2227. if(unlikely(!managed_log(p, PID_LOG_STAT, read_proc_pid_stat(p, ptr))))
  2228. // there is no reason to proceed if we cannot get its status
  2229. return 0;
  2230. // check its parent pid
  2231. if(unlikely(p->ppid < 0 || p->ppid > pid_max)) {
  2232. netdata_log_error("Pid %d (command '%s') states invalid parent pid %d. Using 0.", pid, p->comm, p->ppid);
  2233. p->ppid = 0;
  2234. }
  2235. // --------------------------------------------------------------------
  2236. // /proc/<pid>/io
  2237. managed_log(p, PID_LOG_IO, read_proc_pid_io(p, ptr));
  2238. // --------------------------------------------------------------------
  2239. // /proc/<pid>/status
  2240. if(unlikely(!managed_log(p, PID_LOG_STATUS, read_proc_pid_status(p, ptr))))
  2241. // there is no reason to proceed if we cannot get its status
  2242. return 0;
  2243. // --------------------------------------------------------------------
  2244. // /proc/<pid>/fd
  2245. if(enable_file_charts) {
  2246. managed_log(p, PID_LOG_FDS, read_pid_file_descriptors(p, ptr));
  2247. managed_log(p, PID_LOG_LIMITS, read_proc_pid_limits(p, ptr));
  2248. }
  2249. // --------------------------------------------------------------------
  2250. // done!
  2251. if(unlikely(debug_enabled && include_exited_childs && all_pids_count && p->ppid && all_pids[p->ppid] && !all_pids[p->ppid]->read))
  2252. debug_log("Read process %d (%s) sortlisted %d, but its parent %d (%s) sortlisted %d, is not read", p->pid, p->comm, p->sortlist, all_pids[p->ppid]->pid, all_pids[p->ppid]->comm, all_pids[p->ppid]->sortlist);
  2253. // mark it as updated
  2254. p->updated = true;
  2255. p->keep = false;
  2256. p->keeploops = 0;
  2257. return 1;
  2258. }
  2259. static int collect_data_for_all_processes(void) {
  2260. struct pid_stat *p = NULL;
  2261. #ifndef __FreeBSD__
  2262. // clear process state counter
  2263. memset(proc_state_count, 0, sizeof proc_state_count);
  2264. #else
  2265. int i, procnum;
  2266. static size_t procbase_size = 0;
  2267. static struct kinfo_proc *procbase = NULL;
  2268. size_t new_procbase_size;
  2269. int mib[3] = { CTL_KERN, KERN_PROC, KERN_PROC_PROC };
  2270. if (unlikely(sysctl(mib, 3, NULL, &new_procbase_size, NULL, 0))) {
  2271. netdata_log_error("sysctl error: Can't get processes data size");
  2272. return 0;
  2273. }
  2274. // give it some air for processes that may be started
  2275. // during this little time.
  2276. new_procbase_size += 100 * sizeof(struct kinfo_proc);
  2277. // increase the buffer if needed
  2278. if(new_procbase_size > procbase_size) {
  2279. procbase_size = new_procbase_size;
  2280. procbase = reallocz(procbase, procbase_size);
  2281. }
  2282. // sysctl() gets from new_procbase_size the buffer size
  2283. // and also returns to it the amount of data filled in
  2284. new_procbase_size = procbase_size;
  2285. // get the processes from the system
  2286. if (unlikely(sysctl(mib, 3, procbase, &new_procbase_size, NULL, 0))) {
  2287. netdata_log_error("sysctl error: Can't get processes data");
  2288. return 0;
  2289. }
  2290. // based on the amount of data filled in
  2291. // calculate the number of processes we got
  2292. procnum = new_procbase_size / sizeof(struct kinfo_proc);
  2293. #endif
  2294. if(all_pids_count) {
  2295. #if (ALL_PIDS_ARE_READ_INSTANTLY == 0)
  2296. size_t slc = 0;
  2297. #endif
  2298. for(p = root_of_pids; p ; p = p->next) {
  2299. p->read = false; // mark it as not read, so that collect_data_for_pid() will read it
  2300. p->updated = false;
  2301. p->merged = false;
  2302. p->children_count = 0;
  2303. p->parent = NULL;
  2304. #if (ALL_PIDS_ARE_READ_INSTANTLY == 0)
  2305. all_pids_sortlist[slc++] = p->pid;
  2306. #endif
  2307. }
  2308. #if (ALL_PIDS_ARE_READ_INSTANTLY == 0)
  2309. if(unlikely(slc != all_pids_count)) {
  2310. netdata_log_error("Internal error: I was thinking I had %zu processes in my arrays, but it seems there are %zu.", all_pids_count, slc);
  2311. all_pids_count = slc;
  2312. }
  2313. if(include_exited_childs) {
  2314. // Read parents before childs
  2315. // This is needed to prevent a situation where
  2316. // a child is found running, but until we read
  2317. // its parent, it has exited and its parent
  2318. // has accumulated its resources.
  2319. qsort((void *)all_pids_sortlist, (size_t)all_pids_count, sizeof(pid_t), compar_pid);
  2320. // we forward read all running processes
  2321. // collect_data_for_pid() is smart enough,
  2322. // not to read the same pid twice per iteration
  2323. for(slc = 0; slc < all_pids_count; slc++) {
  2324. collect_data_for_pid(all_pids_sortlist[slc], NULL);
  2325. }
  2326. }
  2327. #endif
  2328. }
  2329. #ifdef __FreeBSD__
  2330. for (i = 0 ; i < procnum ; ++i) {
  2331. pid_t pid = procbase[i].ki_pid;
  2332. collect_data_for_pid(pid, &procbase[i]);
  2333. }
  2334. #else
  2335. static char uptime_filename[FILENAME_MAX + 1] = "";
  2336. if(*uptime_filename == '\0')
  2337. snprintfz(uptime_filename, FILENAME_MAX, "%s/proc/uptime", netdata_configured_host_prefix);
  2338. global_uptime = (kernel_uint_t)(uptime_msec(uptime_filename) / MSEC_PER_SEC);
  2339. char dirname[FILENAME_MAX + 1];
  2340. snprintfz(dirname, FILENAME_MAX, "%s/proc", netdata_configured_host_prefix);
  2341. DIR *dir = opendir(dirname);
  2342. if(!dir) return 0;
  2343. struct dirent *de = NULL;
  2344. while((de = readdir(dir))) {
  2345. char *endptr = de->d_name;
  2346. if(unlikely(de->d_type != DT_DIR || de->d_name[0] < '0' || de->d_name[0] > '9'))
  2347. continue;
  2348. pid_t pid = (pid_t) strtoul(de->d_name, &endptr, 10);
  2349. // make sure we read a valid number
  2350. if(unlikely(endptr == de->d_name || *endptr != '\0'))
  2351. continue;
  2352. collect_data_for_pid(pid, NULL);
  2353. }
  2354. closedir(dir);
  2355. #endif
  2356. if(!all_pids_count)
  2357. return 0;
  2358. // we need /proc/stat to normalize the cpu consumption of the exited childs
  2359. read_global_time();
  2360. // build the process tree
  2361. link_all_processes_to_their_parents();
  2362. // normally this is done
  2363. // however we may have processes exited while we collected values
  2364. // so let's find the exited ones
  2365. // we do this by collecting the ownership of process
  2366. // if we manage to get the ownership, the process still runs
  2367. process_exited_processes();
  2368. return 1;
  2369. }
  2370. // ----------------------------------------------------------------------------
  2371. // update statistics on the targets
  2372. // 1. link all childs to their parents
  2373. // 2. go from bottom to top, marking as merged all childs to their parents
  2374. // this step links all parents without a target to the child target, if any
  2375. // 3. link all top level processes (the ones not merged) to the default target
  2376. // 4. go from top to bottom, linking all childs without a target, to their parent target
  2377. // after this step, all processes have a target
  2378. // [5. for each killed pid (updated = 0), remove its usage from its target]
  2379. // 6. zero all apps_groups_targets
  2380. // 7. concentrate all values on the apps_groups_targets
  2381. // 8. remove all killed processes
  2382. // 9. find the unique file count for each target
  2383. // check: update_apps_groups_statistics()
  2384. static void cleanup_exited_pids(void) {
  2385. size_t c;
  2386. struct pid_stat *p = NULL;
  2387. for(p = root_of_pids; p ;) {
  2388. if(!p->updated && (!p->keep || p->keeploops > 0)) {
  2389. if(unlikely(debug_enabled && (p->keep || p->keeploops)))
  2390. debug_log(" > CLEANUP cannot keep exited process %d (%s) anymore - removing it.", p->pid, p->comm);
  2391. for(c = 0; c < p->fds_size; c++)
  2392. if(p->fds[c].fd > 0) {
  2393. file_descriptor_not_used(p->fds[c].fd);
  2394. clear_pid_fd(&p->fds[c]);
  2395. }
  2396. pid_t r = p->pid;
  2397. p = p->next;
  2398. del_pid_entry(r);
  2399. }
  2400. else {
  2401. if(unlikely(p->keep)) p->keeploops++;
  2402. p->keep = false;
  2403. p = p->next;
  2404. }
  2405. }
  2406. }
  2407. static void apply_apps_groups_targets_inheritance(void) {
  2408. struct pid_stat *p = NULL;
  2409. // children that do not have a target
  2410. // inherit their target from their parent
  2411. int found = 1, loops = 0;
  2412. while(found) {
  2413. if(unlikely(debug_enabled)) loops++;
  2414. found = 0;
  2415. for(p = root_of_pids; p ; p = p->next) {
  2416. // if this process does not have a target,
  2417. // and it has a parent
  2418. // and its parent has a target
  2419. // then, set the parent's target to this process
  2420. if(unlikely(!p->target && p->parent && p->parent->target)) {
  2421. p->target = p->parent->target;
  2422. found++;
  2423. if(debug_enabled || (p->target && p->target->debug_enabled))
  2424. debug_log_int("TARGET INHERITANCE: %s is inherited by %d (%s) from its parent %d (%s).", p->target->name, p->pid, p->comm, p->parent->pid, p->parent->comm);
  2425. }
  2426. }
  2427. }
  2428. // find all the procs with 0 childs and merge them to their parents
  2429. // repeat, until nothing more can be done.
  2430. int sortlist = 1;
  2431. found = 1;
  2432. while(found) {
  2433. if(unlikely(debug_enabled)) loops++;
  2434. found = 0;
  2435. for(p = root_of_pids; p ; p = p->next) {
  2436. if(unlikely(!p->sortlist && !p->children_count))
  2437. p->sortlist = sortlist++;
  2438. if(unlikely(
  2439. !p->children_count // if this process does not have any children
  2440. && !p->merged // and is not already merged
  2441. && p->parent // and has a parent
  2442. && p->parent->children_count // and its parent has children
  2443. // and the target of this process and its parent is the same,
  2444. // or the parent does not have a target
  2445. && (p->target == p->parent->target || !p->parent->target)
  2446. && p->ppid != INIT_PID // and its parent is not init
  2447. )) {
  2448. // mark it as merged
  2449. p->parent->children_count--;
  2450. p->merged = true;
  2451. // the parent inherits the child's target, if it does not have a target itself
  2452. if(unlikely(p->target && !p->parent->target)) {
  2453. p->parent->target = p->target;
  2454. if(debug_enabled || (p->target && p->target->debug_enabled))
  2455. debug_log_int("TARGET INHERITANCE: %s is inherited by %d (%s) from its child %d (%s).", p->target->name, p->parent->pid, p->parent->comm, p->pid, p->comm);
  2456. }
  2457. found++;
  2458. }
  2459. }
  2460. debug_log("TARGET INHERITANCE: merged %d processes", found);
  2461. }
  2462. // init goes always to default target
  2463. if(all_pids[INIT_PID] && !all_pids[INIT_PID]->matched_by_config)
  2464. all_pids[INIT_PID]->target = apps_groups_default_target;
  2465. // pid 0 goes always to default target
  2466. if(all_pids[0] && !all_pids[INIT_PID]->matched_by_config)
  2467. all_pids[0]->target = apps_groups_default_target;
  2468. // give a default target on all top level processes
  2469. if(unlikely(debug_enabled)) loops++;
  2470. for(p = root_of_pids; p ; p = p->next) {
  2471. // if the process is not merged itself
  2472. // then it is a top level process
  2473. if(unlikely(!p->merged && !p->target))
  2474. p->target = apps_groups_default_target;
  2475. // make sure all processes have a sortlist
  2476. if(unlikely(!p->sortlist))
  2477. p->sortlist = sortlist++;
  2478. }
  2479. if(all_pids[1])
  2480. all_pids[1]->sortlist = sortlist++;
  2481. // give a target to all merged child processes
  2482. found = 1;
  2483. while(found) {
  2484. if(unlikely(debug_enabled)) loops++;
  2485. found = 0;
  2486. for(p = root_of_pids; p ; p = p->next) {
  2487. if(unlikely(!p->target && p->merged && p->parent && p->parent->target)) {
  2488. p->target = p->parent->target;
  2489. found++;
  2490. if(debug_enabled || (p->target && p->target->debug_enabled))
  2491. debug_log_int("TARGET INHERITANCE: %s is inherited by %d (%s) from its parent %d (%s) at phase 2.", p->target->name, p->pid, p->comm, p->parent->pid, p->parent->comm);
  2492. }
  2493. }
  2494. }
  2495. debug_log("apply_apps_groups_targets_inheritance() made %d loops on the process tree", loops);
  2496. }
  2497. static size_t zero_all_targets(struct target *root) {
  2498. struct target *w;
  2499. size_t count = 0;
  2500. for (w = root; w ; w = w->next) {
  2501. count++;
  2502. w->minflt = 0;
  2503. w->majflt = 0;
  2504. w->utime = 0;
  2505. w->stime = 0;
  2506. w->gtime = 0;
  2507. w->cminflt = 0;
  2508. w->cmajflt = 0;
  2509. w->cutime = 0;
  2510. w->cstime = 0;
  2511. w->cgtime = 0;
  2512. w->num_threads = 0;
  2513. // w->rss = 0;
  2514. w->processes = 0;
  2515. w->status_vmsize = 0;
  2516. w->status_vmrss = 0;
  2517. w->status_vmshared = 0;
  2518. w->status_rssfile = 0;
  2519. w->status_rssshmem = 0;
  2520. w->status_vmswap = 0;
  2521. w->status_voluntary_ctxt_switches = 0;
  2522. w->status_nonvoluntary_ctxt_switches = 0;
  2523. w->io_logical_bytes_read = 0;
  2524. w->io_logical_bytes_written = 0;
  2525. w->io_read_calls = 0;
  2526. w->io_write_calls = 0;
  2527. w->io_storage_bytes_read = 0;
  2528. w->io_storage_bytes_written = 0;
  2529. w->io_cancelled_write_bytes = 0;
  2530. // zero file counters
  2531. if(w->target_fds) {
  2532. memset(w->target_fds, 0, sizeof(int) * w->target_fds_size);
  2533. w->openfds.files = 0;
  2534. w->openfds.pipes = 0;
  2535. w->openfds.sockets = 0;
  2536. w->openfds.inotifies = 0;
  2537. w->openfds.eventfds = 0;
  2538. w->openfds.timerfds = 0;
  2539. w->openfds.signalfds = 0;
  2540. w->openfds.eventpolls = 0;
  2541. w->openfds.other = 0;
  2542. w->max_open_files_percent = 0.0;
  2543. }
  2544. w->collected_starttime = 0;
  2545. w->uptime_min = 0;
  2546. w->uptime_sum = 0;
  2547. w->uptime_max = 0;
  2548. if(unlikely(w->root_pid)) {
  2549. struct pid_on_target *pid_on_target_to_free, *pid_on_target = w->root_pid;
  2550. while(pid_on_target) {
  2551. pid_on_target_to_free = pid_on_target;
  2552. pid_on_target = pid_on_target->next;
  2553. freez(pid_on_target_to_free);
  2554. }
  2555. w->root_pid = NULL;
  2556. }
  2557. }
  2558. return count;
  2559. }
  2560. static inline void reallocate_target_fds(struct target *w) {
  2561. if(unlikely(!w))
  2562. return;
  2563. if(unlikely(!w->target_fds || w->target_fds_size < all_files_size)) {
  2564. w->target_fds = reallocz(w->target_fds, sizeof(int) * all_files_size);
  2565. memset(&w->target_fds[w->target_fds_size], 0, sizeof(int) * (all_files_size - w->target_fds_size));
  2566. w->target_fds_size = all_files_size;
  2567. }
  2568. }
  2569. static void aggregage_fd_type_on_openfds(FD_FILETYPE type, struct openfds *openfds) {
  2570. switch(type) {
  2571. case FILETYPE_FILE:
  2572. openfds->files++;
  2573. break;
  2574. case FILETYPE_PIPE:
  2575. openfds->pipes++;
  2576. break;
  2577. case FILETYPE_SOCKET:
  2578. openfds->sockets++;
  2579. break;
  2580. case FILETYPE_INOTIFY:
  2581. openfds->inotifies++;
  2582. break;
  2583. case FILETYPE_EVENTFD:
  2584. openfds->eventfds++;
  2585. break;
  2586. case FILETYPE_TIMERFD:
  2587. openfds->timerfds++;
  2588. break;
  2589. case FILETYPE_SIGNALFD:
  2590. openfds->signalfds++;
  2591. break;
  2592. case FILETYPE_EVENTPOLL:
  2593. openfds->eventpolls++;
  2594. break;
  2595. case FILETYPE_OTHER:
  2596. openfds->other++;
  2597. break;
  2598. }
  2599. }
  2600. static inline void aggregate_fd_on_target(int fd, struct target *w) {
  2601. if(unlikely(!w))
  2602. return;
  2603. if(unlikely(w->target_fds[fd])) {
  2604. // it is already aggregated
  2605. // just increase its usage counter
  2606. w->target_fds[fd]++;
  2607. return;
  2608. }
  2609. // increase its usage counter
  2610. // so that we will not add it again
  2611. w->target_fds[fd]++;
  2612. aggregage_fd_type_on_openfds(all_files[fd].type, &w->openfds);
  2613. }
  2614. static inline void aggregate_pid_fds_on_targets(struct pid_stat *p) {
  2615. if(unlikely(!p->updated)) {
  2616. // the process is not running
  2617. return;
  2618. }
  2619. struct target *w = p->target, *u = p->user_target, *g = p->group_target;
  2620. reallocate_target_fds(w);
  2621. reallocate_target_fds(u);
  2622. reallocate_target_fds(g);
  2623. p->openfds.files = 0;
  2624. p->openfds.pipes = 0;
  2625. p->openfds.sockets = 0;
  2626. p->openfds.inotifies = 0;
  2627. p->openfds.eventfds = 0;
  2628. p->openfds.timerfds = 0;
  2629. p->openfds.signalfds = 0;
  2630. p->openfds.eventpolls = 0;
  2631. p->openfds.other = 0;
  2632. long currentfds = 0;
  2633. size_t c, size = p->fds_size;
  2634. struct pid_fd *fds = p->fds;
  2635. for(c = 0; c < size ;c++) {
  2636. int fd = fds[c].fd;
  2637. if(likely(fd <= 0 || fd >= all_files_size))
  2638. continue;
  2639. currentfds++;
  2640. aggregage_fd_type_on_openfds(all_files[fd].type, &p->openfds);
  2641. aggregate_fd_on_target(fd, w);
  2642. aggregate_fd_on_target(fd, u);
  2643. aggregate_fd_on_target(fd, g);
  2644. }
  2645. }
  2646. static inline void aggregate_pid_on_target(struct target *w, struct pid_stat *p, struct target *o) {
  2647. (void)o;
  2648. if(unlikely(!p->updated)) {
  2649. // the process is not running
  2650. return;
  2651. }
  2652. if(unlikely(!w)) {
  2653. netdata_log_error("pid %d %s was left without a target!", p->pid, p->comm);
  2654. return;
  2655. }
  2656. if(p->openfds_limits_percent > w->max_open_files_percent)
  2657. w->max_open_files_percent = p->openfds_limits_percent;
  2658. w->cutime += p->cutime;
  2659. w->cstime += p->cstime;
  2660. w->cgtime += p->cgtime;
  2661. w->cminflt += p->cminflt;
  2662. w->cmajflt += p->cmajflt;
  2663. w->utime += p->utime;
  2664. w->stime += p->stime;
  2665. w->gtime += p->gtime;
  2666. w->minflt += p->minflt;
  2667. w->majflt += p->majflt;
  2668. // w->rss += p->rss;
  2669. w->status_vmsize += p->status_vmsize;
  2670. w->status_vmrss += p->status_vmrss;
  2671. w->status_vmshared += p->status_vmshared;
  2672. w->status_rssfile += p->status_rssfile;
  2673. w->status_rssshmem += p->status_rssshmem;
  2674. w->status_vmswap += p->status_vmswap;
  2675. w->status_voluntary_ctxt_switches += p->status_voluntary_ctxt_switches;
  2676. w->status_nonvoluntary_ctxt_switches += p->status_nonvoluntary_ctxt_switches;
  2677. w->io_logical_bytes_read += p->io_logical_bytes_read;
  2678. w->io_logical_bytes_written += p->io_logical_bytes_written;
  2679. w->io_read_calls += p->io_read_calls;
  2680. w->io_write_calls += p->io_write_calls;
  2681. w->io_storage_bytes_read += p->io_storage_bytes_read;
  2682. w->io_storage_bytes_written += p->io_storage_bytes_written;
  2683. w->io_cancelled_write_bytes += p->io_cancelled_write_bytes;
  2684. w->processes++;
  2685. w->num_threads += p->num_threads;
  2686. if(!w->collected_starttime || p->collected_starttime < w->collected_starttime) w->collected_starttime = p->collected_starttime;
  2687. if(!w->uptime_min || p->uptime < w->uptime_min) w->uptime_min = p->uptime;
  2688. w->uptime_sum += p->uptime;
  2689. if(!w->uptime_max || w->uptime_max < p->uptime) w->uptime_max = p->uptime;
  2690. if(unlikely(debug_enabled || w->debug_enabled)) {
  2691. debug_log_int("aggregating '%s' pid %d on target '%s' utime=" KERNEL_UINT_FORMAT ", stime=" KERNEL_UINT_FORMAT ", gtime=" KERNEL_UINT_FORMAT ", cutime=" KERNEL_UINT_FORMAT ", cstime=" KERNEL_UINT_FORMAT ", cgtime=" KERNEL_UINT_FORMAT ", minflt=" KERNEL_UINT_FORMAT ", majflt=" KERNEL_UINT_FORMAT ", cminflt=" KERNEL_UINT_FORMAT ", cmajflt=" KERNEL_UINT_FORMAT "", p->comm, p->pid, w->name, p->utime, p->stime, p->gtime, p->cutime, p->cstime, p->cgtime, p->minflt, p->majflt, p->cminflt, p->cmajflt);
  2692. struct pid_on_target *pid_on_target = mallocz(sizeof(struct pid_on_target));
  2693. pid_on_target->pid = p->pid;
  2694. pid_on_target->next = w->root_pid;
  2695. w->root_pid = pid_on_target;
  2696. }
  2697. }
  2698. static inline void post_aggregate_targets(struct target *root) {
  2699. struct target *w;
  2700. for (w = root; w ; w = w->next) {
  2701. if(w->collected_starttime) {
  2702. if (!w->starttime || w->collected_starttime < w->starttime) {
  2703. w->starttime = w->collected_starttime;
  2704. }
  2705. } else {
  2706. w->starttime = 0;
  2707. }
  2708. }
  2709. }
  2710. static void calculate_netdata_statistics(void) {
  2711. apply_apps_groups_targets_inheritance();
  2712. zero_all_targets(users_root_target);
  2713. zero_all_targets(groups_root_target);
  2714. apps_groups_targets_count = zero_all_targets(apps_groups_root_target);
  2715. // this has to be done, before the cleanup
  2716. struct pid_stat *p = NULL;
  2717. struct target *w = NULL, *o = NULL;
  2718. // concentrate everything on the targets
  2719. for(p = root_of_pids; p ; p = p->next) {
  2720. // --------------------------------------------------------------------
  2721. // apps_groups target
  2722. aggregate_pid_on_target(p->target, p, NULL);
  2723. // --------------------------------------------------------------------
  2724. // user target
  2725. o = p->user_target;
  2726. if(likely(p->user_target && p->user_target->uid == p->uid))
  2727. w = p->user_target;
  2728. else {
  2729. if(unlikely(debug_enabled && p->user_target))
  2730. debug_log("pid %d (%s) switched user from %u (%s) to %u.", p->pid, p->comm, p->user_target->uid, p->user_target->name, p->uid);
  2731. w = p->user_target = get_users_target(p->uid);
  2732. }
  2733. aggregate_pid_on_target(w, p, o);
  2734. // --------------------------------------------------------------------
  2735. // user group target
  2736. o = p->group_target;
  2737. if(likely(p->group_target && p->group_target->gid == p->gid))
  2738. w = p->group_target;
  2739. else {
  2740. if(unlikely(debug_enabled && p->group_target))
  2741. debug_log("pid %d (%s) switched group from %u (%s) to %u.", p->pid, p->comm, p->group_target->gid, p->group_target->name, p->gid);
  2742. w = p->group_target = get_groups_target(p->gid);
  2743. }
  2744. aggregate_pid_on_target(w, p, o);
  2745. // --------------------------------------------------------------------
  2746. // aggregate all file descriptors
  2747. if(enable_file_charts)
  2748. aggregate_pid_fds_on_targets(p);
  2749. }
  2750. post_aggregate_targets(apps_groups_root_target);
  2751. post_aggregate_targets(users_root_target);
  2752. post_aggregate_targets(groups_root_target);
  2753. cleanup_exited_pids();
  2754. }
  2755. // ----------------------------------------------------------------------------
  2756. // update chart dimensions
  2757. static inline void send_BEGIN(const char *type, const char *name,const char *metric, usec_t usec) {
  2758. fprintf(stdout, "BEGIN %s.%s_%s %" PRIu64 "\n", type, name, metric, usec);
  2759. }
  2760. static inline void send_SET(const char *name, kernel_uint_t value) {
  2761. fprintf(stdout, "SET %s = " KERNEL_UINT_FORMAT "\n", name, value);
  2762. }
  2763. static inline void send_END(void) {
  2764. fprintf(stdout, "END\n\n");
  2765. }
  2766. void send_resource_usage_to_netdata(usec_t dt) {
  2767. static struct timeval last = { 0, 0 };
  2768. static struct rusage me_last;
  2769. struct timeval now;
  2770. struct rusage me;
  2771. usec_t cpuuser;
  2772. usec_t cpusyst;
  2773. if(!last.tv_sec) {
  2774. now_monotonic_timeval(&last);
  2775. getrusage(RUSAGE_SELF, &me_last);
  2776. cpuuser = 0;
  2777. cpusyst = 0;
  2778. }
  2779. else {
  2780. now_monotonic_timeval(&now);
  2781. getrusage(RUSAGE_SELF, &me);
  2782. cpuuser = me.ru_utime.tv_sec * USEC_PER_SEC + me.ru_utime.tv_usec;
  2783. cpusyst = me.ru_stime.tv_sec * USEC_PER_SEC + me.ru_stime.tv_usec;
  2784. memmove(&last, &now, sizeof(struct timeval));
  2785. memmove(&me_last, &me, sizeof(struct rusage));
  2786. }
  2787. static char created_charts = 0;
  2788. if(unlikely(!created_charts)) {
  2789. created_charts = 1;
  2790. fprintf(stdout,
  2791. "CHART netdata.apps_cpu '' 'Apps Plugin CPU' 'milliseconds/s' apps.plugin netdata.apps_cpu stacked 140000 %1$d\n"
  2792. "DIMENSION user '' incremental 1 1000\n"
  2793. "DIMENSION system '' incremental 1 1000\n"
  2794. "CHART netdata.apps_sizes '' 'Apps Plugin Files' 'files/s' apps.plugin netdata.apps_sizes line 140001 %1$d\n"
  2795. "DIMENSION calls '' incremental 1 1\n"
  2796. "DIMENSION files '' incremental 1 1\n"
  2797. "DIMENSION filenames '' incremental 1 1\n"
  2798. "DIMENSION inode_changes '' incremental 1 1\n"
  2799. "DIMENSION link_changes '' incremental 1 1\n"
  2800. "DIMENSION pids '' absolute 1 1\n"
  2801. "DIMENSION fds '' absolute 1 1\n"
  2802. "DIMENSION targets '' absolute 1 1\n"
  2803. "DIMENSION new_pids 'new pids' incremental 1 1\n"
  2804. , update_every
  2805. );
  2806. fprintf(stdout,
  2807. "CHART netdata.apps_fix '' 'Apps Plugin Normalization Ratios' 'percentage' apps.plugin netdata.apps_fix line 140002 %1$d\n"
  2808. "DIMENSION utime '' absolute 1 %2$llu\n"
  2809. "DIMENSION stime '' absolute 1 %2$llu\n"
  2810. "DIMENSION gtime '' absolute 1 %2$llu\n"
  2811. "DIMENSION minflt '' absolute 1 %2$llu\n"
  2812. "DIMENSION majflt '' absolute 1 %2$llu\n"
  2813. , update_every
  2814. , RATES_DETAIL
  2815. );
  2816. if(include_exited_childs)
  2817. fprintf(stdout,
  2818. "CHART netdata.apps_children_fix '' 'Apps Plugin Exited Children Normalization Ratios' 'percentage' apps.plugin netdata.apps_children_fix line 140003 %1$d\n"
  2819. "DIMENSION cutime '' absolute 1 %2$llu\n"
  2820. "DIMENSION cstime '' absolute 1 %2$llu\n"
  2821. "DIMENSION cgtime '' absolute 1 %2$llu\n"
  2822. "DIMENSION cminflt '' absolute 1 %2$llu\n"
  2823. "DIMENSION cmajflt '' absolute 1 %2$llu\n"
  2824. , update_every
  2825. , RATES_DETAIL
  2826. );
  2827. }
  2828. fprintf(stdout,
  2829. "BEGIN netdata.apps_cpu %"PRIu64"\n"
  2830. "SET user = %"PRIu64"\n"
  2831. "SET system = %"PRIu64"\n"
  2832. "END\n"
  2833. "BEGIN netdata.apps_sizes %"PRIu64"\n"
  2834. "SET calls = %zu\n"
  2835. "SET files = %zu\n"
  2836. "SET filenames = %zu\n"
  2837. "SET inode_changes = %zu\n"
  2838. "SET link_changes = %zu\n"
  2839. "SET pids = %zu\n"
  2840. "SET fds = %d\n"
  2841. "SET targets = %zu\n"
  2842. "SET new_pids = %zu\n"
  2843. "END\n"
  2844. , dt
  2845. , cpuuser
  2846. , cpusyst
  2847. , dt
  2848. , calls_counter
  2849. , file_counter
  2850. , filenames_allocated_counter
  2851. , inodes_changed_counter
  2852. , links_changed_counter
  2853. , all_pids_count
  2854. , all_files_len
  2855. , apps_groups_targets_count
  2856. , targets_assignment_counter
  2857. );
  2858. fprintf(stdout,
  2859. "BEGIN netdata.apps_fix %"PRIu64"\n"
  2860. "SET utime = %u\n"
  2861. "SET stime = %u\n"
  2862. "SET gtime = %u\n"
  2863. "SET minflt = %u\n"
  2864. "SET majflt = %u\n"
  2865. "END\n"
  2866. , dt
  2867. , (unsigned int)(utime_fix_ratio * 100 * RATES_DETAIL)
  2868. , (unsigned int)(stime_fix_ratio * 100 * RATES_DETAIL)
  2869. , (unsigned int)(gtime_fix_ratio * 100 * RATES_DETAIL)
  2870. , (unsigned int)(minflt_fix_ratio * 100 * RATES_DETAIL)
  2871. , (unsigned int)(majflt_fix_ratio * 100 * RATES_DETAIL)
  2872. );
  2873. if(include_exited_childs)
  2874. fprintf(stdout,
  2875. "BEGIN netdata.apps_children_fix %"PRIu64"\n"
  2876. "SET cutime = %u\n"
  2877. "SET cstime = %u\n"
  2878. "SET cgtime = %u\n"
  2879. "SET cminflt = %u\n"
  2880. "SET cmajflt = %u\n"
  2881. "END\n"
  2882. , dt
  2883. , (unsigned int)(cutime_fix_ratio * 100 * RATES_DETAIL)
  2884. , (unsigned int)(cstime_fix_ratio * 100 * RATES_DETAIL)
  2885. , (unsigned int)(cgtime_fix_ratio * 100 * RATES_DETAIL)
  2886. , (unsigned int)(cminflt_fix_ratio * 100 * RATES_DETAIL)
  2887. , (unsigned int)(cmajflt_fix_ratio * 100 * RATES_DETAIL)
  2888. );
  2889. }
  2890. static void normalize_utilization(struct target *root) {
  2891. struct target *w;
  2892. // childs processing introduces spikes
  2893. // here we try to eliminate them by disabling childs processing either for specific dimensions
  2894. // or entirely. Of course, either way, we disable it just a single iteration.
  2895. kernel_uint_t max_time = get_system_cpus() * time_factor * RATES_DETAIL;
  2896. kernel_uint_t utime = 0, cutime = 0, stime = 0, cstime = 0, gtime = 0, cgtime = 0, minflt = 0, cminflt = 0, majflt = 0, cmajflt = 0;
  2897. if(global_utime > max_time) global_utime = max_time;
  2898. if(global_stime > max_time) global_stime = max_time;
  2899. if(global_gtime > max_time) global_gtime = max_time;
  2900. for(w = root; w ; w = w->next) {
  2901. if(w->target || (!w->processes && !w->exposed)) continue;
  2902. utime += w->utime;
  2903. stime += w->stime;
  2904. gtime += w->gtime;
  2905. cutime += w->cutime;
  2906. cstime += w->cstime;
  2907. cgtime += w->cgtime;
  2908. minflt += w->minflt;
  2909. majflt += w->majflt;
  2910. cminflt += w->cminflt;
  2911. cmajflt += w->cmajflt;
  2912. }
  2913. if(global_utime || global_stime || global_gtime) {
  2914. if(global_utime + global_stime + global_gtime > utime + cutime + stime + cstime + gtime + cgtime) {
  2915. // everything we collected fits
  2916. utime_fix_ratio =
  2917. stime_fix_ratio =
  2918. gtime_fix_ratio =
  2919. cutime_fix_ratio =
  2920. cstime_fix_ratio =
  2921. cgtime_fix_ratio = 1.0; //(NETDATA_DOUBLE)(global_utime + global_stime) / (NETDATA_DOUBLE)(utime + cutime + stime + cstime);
  2922. }
  2923. else if((global_utime + global_stime > utime + stime) && (cutime || cstime)) {
  2924. // children resources are too high
  2925. // lower only the children resources
  2926. utime_fix_ratio =
  2927. stime_fix_ratio =
  2928. gtime_fix_ratio = 1.0;
  2929. cutime_fix_ratio =
  2930. cstime_fix_ratio =
  2931. cgtime_fix_ratio = (NETDATA_DOUBLE)((global_utime + global_stime) - (utime + stime)) / (NETDATA_DOUBLE)(cutime + cstime);
  2932. }
  2933. else if(utime || stime) {
  2934. // even running processes are unrealistic
  2935. // zero the children resources
  2936. // lower the running processes resources
  2937. utime_fix_ratio =
  2938. stime_fix_ratio =
  2939. gtime_fix_ratio = (NETDATA_DOUBLE)(global_utime + global_stime) / (NETDATA_DOUBLE)(utime + stime);
  2940. cutime_fix_ratio =
  2941. cstime_fix_ratio =
  2942. cgtime_fix_ratio = 0.0;
  2943. }
  2944. else {
  2945. utime_fix_ratio =
  2946. stime_fix_ratio =
  2947. gtime_fix_ratio =
  2948. cutime_fix_ratio =
  2949. cstime_fix_ratio =
  2950. cgtime_fix_ratio = 0.0;
  2951. }
  2952. }
  2953. else {
  2954. utime_fix_ratio =
  2955. stime_fix_ratio =
  2956. gtime_fix_ratio =
  2957. cutime_fix_ratio =
  2958. cstime_fix_ratio =
  2959. cgtime_fix_ratio = 0.0;
  2960. }
  2961. if(utime_fix_ratio > 1.0) utime_fix_ratio = 1.0;
  2962. if(cutime_fix_ratio > 1.0) cutime_fix_ratio = 1.0;
  2963. if(stime_fix_ratio > 1.0) stime_fix_ratio = 1.0;
  2964. if(cstime_fix_ratio > 1.0) cstime_fix_ratio = 1.0;
  2965. if(gtime_fix_ratio > 1.0) gtime_fix_ratio = 1.0;
  2966. if(cgtime_fix_ratio > 1.0) cgtime_fix_ratio = 1.0;
  2967. // if(utime_fix_ratio < 0.0) utime_fix_ratio = 0.0;
  2968. // if(cutime_fix_ratio < 0.0) cutime_fix_ratio = 0.0;
  2969. // if(stime_fix_ratio < 0.0) stime_fix_ratio = 0.0;
  2970. // if(cstime_fix_ratio < 0.0) cstime_fix_ratio = 0.0;
  2971. // if(gtime_fix_ratio < 0.0) gtime_fix_ratio = 0.0;
  2972. // if(cgtime_fix_ratio < 0.0) cgtime_fix_ratio = 0.0;
  2973. // TODO
  2974. // we use cpu time to normalize page faults
  2975. // the problem is that to find the proper max values
  2976. // for page faults we have to parse /proc/vmstat
  2977. // which is quite big to do it again (netdata does it already)
  2978. //
  2979. // a better solution could be to somehow have netdata
  2980. // do this normalization for us
  2981. if(utime || stime || gtime)
  2982. majflt_fix_ratio =
  2983. minflt_fix_ratio = (NETDATA_DOUBLE)(utime * utime_fix_ratio + stime * stime_fix_ratio + gtime * gtime_fix_ratio) / (NETDATA_DOUBLE)(utime + stime + gtime);
  2984. else
  2985. minflt_fix_ratio =
  2986. majflt_fix_ratio = 1.0;
  2987. if(cutime || cstime || cgtime)
  2988. cmajflt_fix_ratio =
  2989. cminflt_fix_ratio = (NETDATA_DOUBLE)(cutime * cutime_fix_ratio + cstime * cstime_fix_ratio + cgtime * cgtime_fix_ratio) / (NETDATA_DOUBLE)(cutime + cstime + cgtime);
  2990. else
  2991. cminflt_fix_ratio =
  2992. cmajflt_fix_ratio = 1.0;
  2993. // the report
  2994. debug_log(
  2995. "SYSTEM: u=" KERNEL_UINT_FORMAT " s=" KERNEL_UINT_FORMAT " g=" KERNEL_UINT_FORMAT " "
  2996. "COLLECTED: u=" KERNEL_UINT_FORMAT " s=" KERNEL_UINT_FORMAT " g=" KERNEL_UINT_FORMAT " cu=" KERNEL_UINT_FORMAT " cs=" KERNEL_UINT_FORMAT " cg=" KERNEL_UINT_FORMAT " "
  2997. "DELTA: u=" KERNEL_UINT_FORMAT " s=" KERNEL_UINT_FORMAT " g=" KERNEL_UINT_FORMAT " "
  2998. "FIX: u=%0.2f s=%0.2f g=%0.2f cu=%0.2f cs=%0.2f cg=%0.2f "
  2999. "FINALLY: u=" KERNEL_UINT_FORMAT " s=" KERNEL_UINT_FORMAT " g=" KERNEL_UINT_FORMAT " cu=" KERNEL_UINT_FORMAT " cs=" KERNEL_UINT_FORMAT " cg=" KERNEL_UINT_FORMAT " "
  3000. , global_utime
  3001. , global_stime
  3002. , global_gtime
  3003. , utime
  3004. , stime
  3005. , gtime
  3006. , cutime
  3007. , cstime
  3008. , cgtime
  3009. , utime + cutime - global_utime
  3010. , stime + cstime - global_stime
  3011. , gtime + cgtime - global_gtime
  3012. , utime_fix_ratio
  3013. , stime_fix_ratio
  3014. , gtime_fix_ratio
  3015. , cutime_fix_ratio
  3016. , cstime_fix_ratio
  3017. , cgtime_fix_ratio
  3018. , (kernel_uint_t)(utime * utime_fix_ratio)
  3019. , (kernel_uint_t)(stime * stime_fix_ratio)
  3020. , (kernel_uint_t)(gtime * gtime_fix_ratio)
  3021. , (kernel_uint_t)(cutime * cutime_fix_ratio)
  3022. , (kernel_uint_t)(cstime * cstime_fix_ratio)
  3023. , (kernel_uint_t)(cgtime * cgtime_fix_ratio)
  3024. );
  3025. }
  3026. static void send_collected_data_to_netdata(struct target *root, const char *type, usec_t dt) {
  3027. struct target *w;
  3028. for (w = root; w ; w = w->next) {
  3029. if (unlikely(!w->exposed && !w->is_other))
  3030. continue;
  3031. send_BEGIN(type, w->clean_name, "processes", dt);
  3032. send_SET("processes", w->processes);
  3033. send_END();
  3034. send_BEGIN(type, w->clean_name, "threads", dt);
  3035. send_SET("threads", w->num_threads);
  3036. send_END();
  3037. if (unlikely(!w->processes && !w->is_other))
  3038. continue;
  3039. send_BEGIN(type, w->clean_name, "cpu_utilization", dt);
  3040. send_SET("user", (kernel_uint_t)(w->utime * utime_fix_ratio) + (include_exited_childs ? ((kernel_uint_t)(w->cutime * cutime_fix_ratio)) : 0ULL));
  3041. send_SET("system", (kernel_uint_t)(w->stime * stime_fix_ratio) + (include_exited_childs ? ((kernel_uint_t)(w->cstime * cstime_fix_ratio)) : 0ULL));
  3042. send_END();
  3043. #ifndef __FreeBSD__
  3044. if (enable_guest_charts) {
  3045. send_BEGIN(type, w->clean_name, "cpu_guest_utilization", dt);
  3046. send_SET("guest", (kernel_uint_t)(w->gtime * gtime_fix_ratio) + (include_exited_childs ? ((kernel_uint_t)(w->cgtime * cgtime_fix_ratio)) : 0ULL));
  3047. send_END();
  3048. }
  3049. send_BEGIN(type, w->clean_name, "cpu_context_switches", dt);
  3050. send_SET("voluntary", w->status_voluntary_ctxt_switches);
  3051. send_SET("involuntary", w->status_nonvoluntary_ctxt_switches);
  3052. send_END();
  3053. send_BEGIN(type, w->clean_name, "mem_private_usage", dt);
  3054. send_SET("mem", (w->status_vmrss > w->status_vmshared)?(w->status_vmrss - w->status_vmshared) : 0ULL);
  3055. send_END();
  3056. #endif
  3057. send_BEGIN(type, w->clean_name, "mem_usage", dt);
  3058. send_SET("rss", w->status_vmrss);
  3059. send_END();
  3060. send_BEGIN(type, w->clean_name, "vmem_usage", dt);
  3061. send_SET("vmem", w->status_vmsize);
  3062. send_END();
  3063. send_BEGIN(type, w->clean_name, "mem_page_faults", dt);
  3064. send_SET("minor", (kernel_uint_t)(w->minflt * minflt_fix_ratio) + (include_exited_childs ? ((kernel_uint_t)(w->cminflt * cminflt_fix_ratio)) : 0ULL));
  3065. send_SET("major", (kernel_uint_t)(w->majflt * majflt_fix_ratio) + (include_exited_childs ? ((kernel_uint_t)(w->cmajflt * cmajflt_fix_ratio)) : 0ULL));
  3066. send_END();
  3067. #ifndef __FreeBSD__
  3068. send_BEGIN(type, w->clean_name, "swap_usage", dt);
  3069. send_SET("swap", w->status_vmswap);
  3070. send_END();
  3071. #endif
  3072. #ifndef __FreeBSD__
  3073. send_BEGIN(type, w->clean_name, "uptime", dt);
  3074. send_SET("uptime", (global_uptime > w->starttime) ? (global_uptime - w->starttime) : 0);
  3075. send_END();
  3076. if (enable_detailed_uptime_charts) {
  3077. send_BEGIN(type, w->clean_name, "uptime_summary", dt);
  3078. send_SET("min", w->uptime_min);
  3079. send_SET("avg", w->processes > 0 ? w->uptime_sum / w->processes : 0);
  3080. send_SET("max", w->uptime_max);
  3081. send_END();
  3082. }
  3083. #endif
  3084. send_BEGIN(type, w->clean_name, "disk_physical_io", dt);
  3085. send_SET("reads", w->io_storage_bytes_read);
  3086. send_SET("writes", w->io_storage_bytes_written);
  3087. send_END();
  3088. #ifndef __FreeBSD__
  3089. send_BEGIN(type, w->clean_name, "disk_logical_io", dt);
  3090. send_SET("reads", w->io_logical_bytes_read);
  3091. send_SET("writes", w->io_logical_bytes_written);
  3092. send_END();
  3093. #endif
  3094. if (enable_file_charts) {
  3095. send_BEGIN(type, w->clean_name, "fds_open_limit", dt);
  3096. send_SET("limit", w->max_open_files_percent * 100.0);
  3097. send_END();
  3098. send_BEGIN(type, w->clean_name, "fds_open", dt);
  3099. send_SET("files", w->openfds.files);
  3100. send_SET("sockets", w->openfds.sockets);
  3101. send_SET("pipes", w->openfds.sockets);
  3102. send_SET("inotifies", w->openfds.inotifies);
  3103. send_SET("event", w->openfds.eventfds);
  3104. send_SET("timer", w->openfds.timerfds);
  3105. send_SET("signal", w->openfds.signalfds);
  3106. send_SET("eventpolls", w->openfds.eventpolls);
  3107. send_SET("other", w->openfds.other);
  3108. send_END();
  3109. }
  3110. }
  3111. }
  3112. // ----------------------------------------------------------------------------
  3113. // generate the charts
  3114. static void send_charts_updates_to_netdata(struct target *root, const char *type, const char *lbl_name, const char *title)
  3115. {
  3116. struct target *w;
  3117. if (debug_enabled) {
  3118. for (w = root; w; w = w->next) {
  3119. if (unlikely(w->debug_enabled && !w->target && w->processes)) {
  3120. struct pid_on_target *pid_on_target;
  3121. fprintf(stderr, "apps.plugin: target '%s' has aggregated %u process(es):", w->name, w->processes);
  3122. for (pid_on_target = w->root_pid; pid_on_target; pid_on_target = pid_on_target->next) {
  3123. fprintf(stderr, " %d", pid_on_target->pid);
  3124. }
  3125. fputc('\n', stderr);
  3126. }
  3127. }
  3128. }
  3129. for (w = root; w; w = w->next) {
  3130. if (likely(w->exposed || (!w->processes && !w->is_other)))
  3131. continue;
  3132. w->exposed = 1;
  3133. fprintf(stdout, "CHART %s.%s_cpu_utilization '' '%s CPU utilization (100%% = 1 core)' 'percentage' cpu %s.cpu_utilization stacked 20001 %d\n", type, w->clean_name, title, type, update_every);
  3134. fprintf(stdout, "CLABEL '%s' '%s' 0\n", lbl_name, w->name);
  3135. fprintf(stdout, "CLABEL_COMMIT\n");
  3136. fprintf(stdout, "DIMENSION user '' absolute 1 %llu\n", time_factor * RATES_DETAIL / 100LLU);
  3137. fprintf(stdout, "DIMENSION system '' absolute 1 %llu\n", time_factor * RATES_DETAIL / 100LLU);
  3138. #ifndef __FreeBSD__
  3139. if (enable_guest_charts) {
  3140. fprintf(stdout, "CHART %s.%s_cpu_guest_utilization '' '%s CPU guest utlization (100%% = 1 core)' 'percentage' cpu %s.cpu_guest_utilization line 20005 %d\n", type, w->clean_name, title, type, update_every);
  3141. fprintf(stdout, "CLABEL '%s' '%s' 0\n", lbl_name, w->name);
  3142. fprintf(stdout, "CLABEL_COMMIT\n");
  3143. fprintf(stdout, "DIMENSION guest '' absolute 1 %llu\n", time_factor * RATES_DETAIL / 100LLU);
  3144. }
  3145. fprintf(stdout, "CHART %s.%s_cpu_context_switches '' '%s CPU context switches' 'switches/s' cpu %s.cpu_context_switches stacked 20010 %d\n", type, w->clean_name, title, type, update_every);
  3146. fprintf(stdout, "CLABEL '%s' '%s' 0\n", lbl_name, w->name);
  3147. fprintf(stdout, "CLABEL_COMMIT\n");
  3148. fprintf(stdout, "DIMENSION voluntary '' absolute 1 %llu\n", RATES_DETAIL);
  3149. fprintf(stdout, "DIMENSION involuntary '' absolute 1 %llu\n", RATES_DETAIL);
  3150. fprintf(stdout, "CHART %s.%s_mem_private_usage '' '%s memory usage without shared' 'MiB' mem %s.mem_private_usage area 20050 %d\n", type, w->clean_name, title, type, update_every);
  3151. fprintf(stdout, "CLABEL '%s' '%s' 0\n", lbl_name, w->name);
  3152. fprintf(stdout, "CLABEL_COMMIT\n");
  3153. fprintf(stdout, "DIMENSION mem '' absolute %ld %ld\n", 1L, 1024L);
  3154. #endif
  3155. fprintf(stdout, "CHART %s.%s_mem_usage '' '%s memory RSS usage' 'MiB' mem %s.mem_usage area 20055 %d\n", type, w->clean_name, title, type, update_every);
  3156. fprintf(stdout, "CLABEL '%s' '%s' 0\n", lbl_name, w->name);
  3157. fprintf(stdout, "CLABEL_COMMIT\n");
  3158. fprintf(stdout, "DIMENSION rss '' absolute %ld %ld\n", 1L, 1024L);
  3159. fprintf(stdout, "CHART %s.%s_mem_page_faults '' '%s memory page faults' 'pgfaults/s' mem %s.mem_page_faults stacked 20060 %d\n", type, w->clean_name, title, type, update_every);
  3160. fprintf(stdout, "CLABEL '%s' '%s' 0\n", lbl_name, w->name);
  3161. fprintf(stdout, "CLABEL_COMMIT\n");
  3162. fprintf(stdout, "DIMENSION major '' absolute 1 %llu\n", RATES_DETAIL);
  3163. fprintf(stdout, "DIMENSION minor '' absolute 1 %llu\n", RATES_DETAIL);
  3164. fprintf(stdout, "CHART %s.%s_vmem_usage '' '%s virtual memory size' 'MiB' mem %s.vmem_usage line 20065 %d\n", type, w->clean_name, title, type, update_every);
  3165. fprintf(stdout, "CLABEL '%s' '%s' 0\n", lbl_name, w->name);
  3166. fprintf(stdout, "CLABEL_COMMIT\n");
  3167. fprintf(stdout, "DIMENSION vmem '' absolute %ld %ld\n", 1L, 1024L);
  3168. #ifndef __FreeBSD__
  3169. fprintf(stdout, "CHART %s.%s_swap_usage '' '%s swap usage' 'MiB' mem %s.swap_usage area 20065 %d\n", type, w->clean_name, title, type, update_every);
  3170. fprintf(stdout, "CLABEL '%s' '%s' 0\n", lbl_name, w->name);
  3171. fprintf(stdout, "CLABEL_COMMIT\n");
  3172. fprintf(stdout, "DIMENSION swap '' absolute %ld %ld\n", 1L, 1024L);
  3173. #endif
  3174. #ifndef __FreeBSD__
  3175. fprintf(stdout, "CHART %s.%s_disk_physical_io '' '%s disk physical IO' 'KiB/s' disk %s.disk_physical_io area 20100 %d\n", type, w->clean_name, title, type, update_every);
  3176. fprintf(stdout, "CLABEL '%s' '%s' 0\n", lbl_name, w->name);
  3177. fprintf(stdout, "CLABEL_COMMIT\n");
  3178. fprintf(stdout, "DIMENSION reads '' absolute 1 %llu\n", 1024LLU * RATES_DETAIL);
  3179. fprintf(stdout, "DIMENSION writes '' absolute -1 %llu\n", 1024LLU * RATES_DETAIL);
  3180. fprintf(stdout, "CHART %s.%s_disk_logical_io '' '%s disk logical IO' 'KiB/s' disk %s.disk_logical_io area 20105 %d\n", type, w->clean_name, title, type, update_every);
  3181. fprintf(stdout, "CLABEL '%s' '%s' 0\n", lbl_name, w->name);
  3182. fprintf(stdout, "CLABEL_COMMIT\n");
  3183. fprintf(stdout, "DIMENSION reads '' absolute 1 %llu\n", 1024LLU * RATES_DETAIL);
  3184. fprintf(stdout, "DIMENSION writes '' absolute -1 %llu\n", 1024LLU * RATES_DETAIL);
  3185. #else
  3186. fprintf(stdout, "CHART %s.%s_disk_physical_io '' '%s disk physical IO' 'blocks/s' disk %s.disk_physical_block_io area 20100 %d\n", type, w->clean_name, title, type, update_every);
  3187. fprintf(stdout, "CLABEL '%s' '%s' 0\n", lbl_name, w->name);
  3188. fprintf(stdout, "CLABEL_COMMIT\n");
  3189. fprintf(stdout, "DIMENSION reads '' absolute 1 %llu\n", RATES_DETAIL);
  3190. fprintf(stdout, "DIMENSION writes '' absolute -1 %llu\n", RATES_DETAIL);
  3191. #endif
  3192. fprintf(stdout, "CHART %s.%s_processes '' '%s processes' 'processes' processes %s.processes line 20150 %d\n", type, w->clean_name, title, type, update_every);
  3193. fprintf(stdout, "CLABEL '%s' '%s' 0\n", lbl_name, w->name);
  3194. fprintf(stdout, "CLABEL_COMMIT\n");
  3195. fprintf(stdout, "DIMENSION processes '' absolute 1 1\n");
  3196. fprintf(stdout, "CHART %s.%s_threads '' '%s threads' 'threads' processes %s.threads line 20155 %d\n", type, w->clean_name, title, type, update_every);
  3197. fprintf(stdout, "CLABEL '%s' '%s' 0\n", lbl_name, w->name);
  3198. fprintf(stdout, "CLABEL_COMMIT\n");
  3199. fprintf(stdout, "DIMENSION threads '' absolute 1 1\n");
  3200. if (enable_file_charts) {
  3201. fprintf(stdout, "CHART %s.%s_fds_open_limit '' '%s open file descriptors limit' '%%' fds %s.fds_open_limit line 20200 %d\n", type, w->clean_name, title, type, update_every);
  3202. fprintf(stdout, "CLABEL '%s' '%s' 0\n", lbl_name, w->name);
  3203. fprintf(stdout, "CLABEL_COMMIT\n");
  3204. fprintf(stdout, "DIMENSION limit '' absolute 1 100\n");
  3205. fprintf(stdout, "CHART %s.%s_fds_open '' '%s open files descriptors' 'fds' fds %s.fds_open stacked 20210 %d\n", type, w->clean_name, title, type, update_every);
  3206. fprintf(stdout, "CLABEL '%s' '%s' 0\n", lbl_name, w->name);
  3207. fprintf(stdout, "CLABEL_COMMIT\n");
  3208. fprintf(stdout, "DIMENSION files '' absolute 1 1\n");
  3209. fprintf(stdout, "DIMENSION sockets '' absolute 1 1\n");
  3210. fprintf(stdout, "DIMENSION pipes '' absolute 1 1\n");
  3211. fprintf(stdout, "DIMENSION inotifies '' absolute 1 1\n");
  3212. fprintf(stdout, "DIMENSION event '' absolute 1 1\n");
  3213. fprintf(stdout, "DIMENSION timer '' absolute 1 1\n");
  3214. fprintf(stdout, "DIMENSION signal '' absolute 1 1\n");
  3215. fprintf(stdout, "DIMENSION eventpolls '' absolute 1 1\n");
  3216. fprintf(stdout, "DIMENSION other '' absolute 1 1\n");
  3217. }
  3218. #ifndef __FreeBSD__
  3219. fprintf(stdout, "CHART %s.%s_uptime '' '%s uptime' 'seconds' uptime %s.uptime line 20250 %d\n", type, w->clean_name, title, type, update_every);
  3220. fprintf(stdout, "CLABEL '%s' '%s' 0\n", lbl_name, w->name);
  3221. fprintf(stdout, "CLABEL_COMMIT\n");
  3222. fprintf(stdout, "DIMENSION uptime '' absolute 1 1\n");
  3223. if (enable_detailed_uptime_charts) {
  3224. fprintf(stdout, "CHART %s.%s_uptime_summary '' '%s uptime summary' 'seconds' uptime %s.uptime_summary area 20255 %d\n", type, w->clean_name, title, type, update_every);
  3225. fprintf(stdout, "CLABEL '%s' '%s' 0\n", lbl_name, w->name);
  3226. fprintf(stdout, "CLABEL_COMMIT\n");
  3227. fprintf(stdout, "DIMENSION min '' absolute 1 1\n");
  3228. fprintf(stdout, "DIMENSION avg '' absolute 1 1\n");
  3229. fprintf(stdout, "DIMENSION max '' absolute 1 1\n");
  3230. }
  3231. #endif
  3232. }
  3233. }
  3234. #ifndef __FreeBSD__
  3235. static void send_proc_states_count(usec_t dt)
  3236. {
  3237. static bool chart_added = false;
  3238. // create chart for count of processes in different states
  3239. if (!chart_added) {
  3240. fprintf(
  3241. stdout,
  3242. "CHART system.processes_state '' 'System Processes State' 'processes' processes system.processes_state line %d %d\n",
  3243. NETDATA_CHART_PRIO_SYSTEM_PROCESS_STATES,
  3244. update_every);
  3245. for (proc_state i = PROC_STATUS_RUNNING; i < PROC_STATUS_END; i++) {
  3246. fprintf(stdout, "DIMENSION %s '' absolute 1 1\n", proc_states[i]);
  3247. }
  3248. chart_added = true;
  3249. }
  3250. // send process state count
  3251. fprintf(stdout, "BEGIN system.processes_state %" PRIu64 "\n", dt);
  3252. for (proc_state i = PROC_STATUS_RUNNING; i < PROC_STATUS_END; i++) {
  3253. send_SET(proc_states[i], proc_state_count[i]);
  3254. }
  3255. send_END();
  3256. }
  3257. #endif
  3258. // ----------------------------------------------------------------------------
  3259. // parse command line arguments
  3260. int check_proc_1_io() {
  3261. int ret = 0;
  3262. procfile *ff = procfile_open("/proc/1/io", NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
  3263. if(!ff) goto cleanup;
  3264. ff = procfile_readall(ff);
  3265. if(!ff) goto cleanup;
  3266. ret = 1;
  3267. cleanup:
  3268. procfile_close(ff);
  3269. return ret;
  3270. }
  3271. static void parse_args(int argc, char **argv)
  3272. {
  3273. int i, freq = 0;
  3274. for(i = 1; i < argc; i++) {
  3275. if(!freq) {
  3276. int n = (int)str2l(argv[i]);
  3277. if(n > 0) {
  3278. freq = n;
  3279. continue;
  3280. }
  3281. }
  3282. if(strcmp("version", argv[i]) == 0 || strcmp("-version", argv[i]) == 0 || strcmp("--version", argv[i]) == 0 || strcmp("-v", argv[i]) == 0 || strcmp("-V", argv[i]) == 0) {
  3283. printf("apps.plugin %s\n", VERSION);
  3284. exit(0);
  3285. }
  3286. if(strcmp("test-permissions", argv[i]) == 0 || strcmp("-t", argv[i]) == 0) {
  3287. if(!check_proc_1_io()) {
  3288. perror("Tried to read /proc/1/io and it failed");
  3289. exit(1);
  3290. }
  3291. printf("OK\n");
  3292. exit(0);
  3293. }
  3294. if(strcmp("debug", argv[i]) == 0) {
  3295. debug_enabled = 1;
  3296. #ifndef NETDATA_INTERNAL_CHECKS
  3297. fprintf(stderr, "apps.plugin has been compiled without debugging\n");
  3298. #endif
  3299. continue;
  3300. }
  3301. #ifndef __FreeBSD__
  3302. if(strcmp("fds-cache-secs", argv[i]) == 0) {
  3303. if(argc <= i + 1) {
  3304. fprintf(stderr, "Parameter 'fds-cache-secs' requires a number as argument.\n");
  3305. exit(1);
  3306. }
  3307. i++;
  3308. max_fds_cache_seconds = str2i(argv[i]);
  3309. if(max_fds_cache_seconds < 0) max_fds_cache_seconds = 0;
  3310. continue;
  3311. }
  3312. #endif
  3313. if(strcmp("no-childs", argv[i]) == 0 || strcmp("without-childs", argv[i]) == 0) {
  3314. include_exited_childs = 0;
  3315. continue;
  3316. }
  3317. if(strcmp("with-childs", argv[i]) == 0) {
  3318. include_exited_childs = 1;
  3319. continue;
  3320. }
  3321. if(strcmp("with-guest", argv[i]) == 0) {
  3322. enable_guest_charts = 1;
  3323. continue;
  3324. }
  3325. if(strcmp("no-guest", argv[i]) == 0 || strcmp("without-guest", argv[i]) == 0) {
  3326. enable_guest_charts = 0;
  3327. continue;
  3328. }
  3329. if(strcmp("with-files", argv[i]) == 0) {
  3330. enable_file_charts = 1;
  3331. continue;
  3332. }
  3333. if(strcmp("no-files", argv[i]) == 0 || strcmp("without-files", argv[i]) == 0) {
  3334. enable_file_charts = 0;
  3335. continue;
  3336. }
  3337. if(strcmp("no-users", argv[i]) == 0 || strcmp("without-users", argv[i]) == 0) {
  3338. enable_users_charts = 0;
  3339. continue;
  3340. }
  3341. if(strcmp("no-groups", argv[i]) == 0 || strcmp("without-groups", argv[i]) == 0) {
  3342. enable_groups_charts = 0;
  3343. continue;
  3344. }
  3345. if(strcmp("with-detailed-uptime", argv[i]) == 0) {
  3346. enable_detailed_uptime_charts = 1;
  3347. continue;
  3348. }
  3349. if(strcmp("with-function-cmdline", argv[i]) == 0) {
  3350. enable_function_cmdline = 1;
  3351. continue;
  3352. }
  3353. if(strcmp("-h", argv[i]) == 0 || strcmp("--help", argv[i]) == 0) {
  3354. fprintf(stderr,
  3355. "\n"
  3356. " netdata apps.plugin %s\n"
  3357. " Copyright (C) 2016-2017 Costa Tsaousis <costa@tsaousis.gr>\n"
  3358. " Released under GNU General Public License v3 or later.\n"
  3359. " All rights reserved.\n"
  3360. "\n"
  3361. " This program is a data collector plugin for netdata.\n"
  3362. "\n"
  3363. " Available command line options:\n"
  3364. "\n"
  3365. " SECONDS set the data collection frequency\n"
  3366. "\n"
  3367. " debug enable debugging (lot of output)\n"
  3368. "\n"
  3369. " with-function-cmdline enable reporting the complete command line for processes\n"
  3370. " it includes the command and passed arguments\n"
  3371. " it may include sensitive data such as passwords and tokens\n"
  3372. " enabling this could be a security risk\n"
  3373. "\n"
  3374. " with-childs\n"
  3375. " without-childs enable / disable aggregating exited\n"
  3376. " children resources into parents\n"
  3377. " (default is enabled)\n"
  3378. "\n"
  3379. " with-guest\n"
  3380. " without-guest enable / disable reporting guest charts\n"
  3381. " (default is disabled)\n"
  3382. "\n"
  3383. " with-files\n"
  3384. " without-files enable / disable reporting files, sockets, pipes\n"
  3385. " (default is enabled)\n"
  3386. "\n"
  3387. " without-users disable reporting per user charts\n"
  3388. "\n"
  3389. " without-groups disable reporting per user group charts\n"
  3390. "\n"
  3391. " with-detailed-uptime enable reporting min/avg/max uptime charts\n"
  3392. "\n"
  3393. #ifndef __FreeBSD__
  3394. " fds-cache-secs N cache the files of processed for N seconds\n"
  3395. " caching is adaptive per file (when a file\n"
  3396. " is found, it starts at 0 and while the file\n"
  3397. " remains open, it is incremented up to the\n"
  3398. " max given)\n"
  3399. " (default is %d seconds)\n"
  3400. "\n"
  3401. #endif
  3402. " version or -v or -V print program version and exit\n"
  3403. "\n"
  3404. , VERSION
  3405. #ifndef __FreeBSD__
  3406. , max_fds_cache_seconds
  3407. #endif
  3408. );
  3409. exit(1);
  3410. }
  3411. netdata_log_error("Cannot understand option %s", argv[i]);
  3412. exit(1);
  3413. }
  3414. if(freq > 0) update_every = freq;
  3415. if(read_apps_groups_conf(user_config_dir, "groups")) {
  3416. netdata_log_info("Cannot read process groups configuration file '%s/apps_groups.conf'. Will try '%s/apps_groups.conf'", user_config_dir, stock_config_dir);
  3417. if(read_apps_groups_conf(stock_config_dir, "groups")) {
  3418. netdata_log_error("Cannot read process groups '%s/apps_groups.conf'. There are no internal defaults. Failing.", stock_config_dir);
  3419. exit(1);
  3420. }
  3421. else
  3422. netdata_log_info("Loaded config file '%s/apps_groups.conf'", stock_config_dir);
  3423. }
  3424. else
  3425. netdata_log_info("Loaded config file '%s/apps_groups.conf'", user_config_dir);
  3426. }
  3427. static int am_i_running_as_root() {
  3428. uid_t uid = getuid(), euid = geteuid();
  3429. if(uid == 0 || euid == 0) {
  3430. if(debug_enabled) netdata_log_info("I am running with escalated privileges, uid = %u, euid = %u.", uid, euid);
  3431. return 1;
  3432. }
  3433. if(debug_enabled) netdata_log_info("I am not running with escalated privileges, uid = %u, euid = %u.", uid, euid);
  3434. return 0;
  3435. }
  3436. #ifdef HAVE_CAPABILITY
  3437. static int check_capabilities() {
  3438. cap_t caps = cap_get_proc();
  3439. if(!caps) {
  3440. netdata_log_error("Cannot get current capabilities.");
  3441. return 0;
  3442. }
  3443. else if(debug_enabled)
  3444. netdata_log_info("Received my capabilities from the system.");
  3445. int ret = 1;
  3446. cap_flag_value_t cfv = CAP_CLEAR;
  3447. if(cap_get_flag(caps, CAP_DAC_READ_SEARCH, CAP_EFFECTIVE, &cfv) == -1) {
  3448. netdata_log_error("Cannot find if CAP_DAC_READ_SEARCH is effective.");
  3449. ret = 0;
  3450. }
  3451. else {
  3452. if(cfv != CAP_SET) {
  3453. netdata_log_error("apps.plugin should run with CAP_DAC_READ_SEARCH.");
  3454. ret = 0;
  3455. }
  3456. else if(debug_enabled)
  3457. netdata_log_info("apps.plugin runs with CAP_DAC_READ_SEARCH.");
  3458. }
  3459. cfv = CAP_CLEAR;
  3460. if(cap_get_flag(caps, CAP_SYS_PTRACE, CAP_EFFECTIVE, &cfv) == -1) {
  3461. netdata_log_error("Cannot find if CAP_SYS_PTRACE is effective.");
  3462. ret = 0;
  3463. }
  3464. else {
  3465. if(cfv != CAP_SET) {
  3466. netdata_log_error("apps.plugin should run with CAP_SYS_PTRACE.");
  3467. ret = 0;
  3468. }
  3469. else if(debug_enabled)
  3470. netdata_log_info("apps.plugin runs with CAP_SYS_PTRACE.");
  3471. }
  3472. cap_free(caps);
  3473. return ret;
  3474. }
  3475. #else
  3476. static int check_capabilities() {
  3477. return 0;
  3478. }
  3479. #endif
  3480. static netdata_mutex_t apps_and_stdout_mutex = NETDATA_MUTEX_INITIALIZER;
  3481. #define PROCESS_FILTER_CATEGORY "category:"
  3482. #define PROCESS_FILTER_USER "user:"
  3483. #define PROCESS_FILTER_GROUP "group:"
  3484. #define PROCESS_FILTER_PROCESS "process:"
  3485. #define PROCESS_FILTER_PID "pid:"
  3486. #define PROCESS_FILTER_UID "uid:"
  3487. #define PROCESS_FILTER_GID "gid:"
  3488. static struct target *find_target_by_name(struct target *base, const char *name) {
  3489. struct target *t;
  3490. for(t = base; t ; t = t->next) {
  3491. if (strcmp(t->name, name) == 0)
  3492. return t;
  3493. }
  3494. return NULL;
  3495. }
  3496. static kernel_uint_t MemTotal = 0;
  3497. static void get_MemTotal(void) {
  3498. #ifdef __FreeBSD__
  3499. // TODO - fix this for FreeBSD
  3500. return;
  3501. #else
  3502. char filename[FILENAME_MAX + 1];
  3503. snprintfz(filename, FILENAME_MAX, "%s/proc/meminfo", netdata_configured_host_prefix);
  3504. procfile *ff = procfile_open(filename, ": \t", PROCFILE_FLAG_DEFAULT);
  3505. if(!ff)
  3506. return;
  3507. ff = procfile_readall(ff);
  3508. if(!ff)
  3509. return;
  3510. size_t line, lines = procfile_lines(ff);
  3511. for(line = 0; line < lines ;line++) {
  3512. size_t words = procfile_linewords(ff, line);
  3513. if(words == 3 && strcmp(procfile_lineword(ff, line, 0), "MemTotal") == 0 && strcmp(procfile_lineword(ff, line, 2), "kB") == 0) {
  3514. kernel_uint_t n = str2ull(procfile_lineword(ff, line, 1), NULL);
  3515. if(n) MemTotal = n;
  3516. break;
  3517. }
  3518. }
  3519. procfile_close(ff);
  3520. #endif
  3521. }
  3522. static void apps_plugin_function_processes_help(const char *transaction) {
  3523. BUFFER *wb = buffer_create(0, NULL);
  3524. buffer_sprintf(wb, "%s",
  3525. "apps.plugin / processes\n"
  3526. "\n"
  3527. "Function `processes` presents all the currently running processes of the system.\n"
  3528. "\n"
  3529. "The following filters are supported:\n"
  3530. "\n"
  3531. " category:NAME\n"
  3532. " Shows only processes that are assigned the category `NAME` in apps_groups.conf\n"
  3533. "\n"
  3534. " user:NAME\n"
  3535. " Shows only processes that are running as user name `NAME`.\n"
  3536. "\n"
  3537. " group:NAME\n"
  3538. " Shows only processes that are running as group name `NAME`.\n"
  3539. "\n"
  3540. " process:NAME\n"
  3541. " Shows only processes that their Command is `NAME` or their parent's Command is `NAME`.\n"
  3542. "\n"
  3543. " pid:NUMBER\n"
  3544. " Shows only processes that their PID is `NUMBER` or their parent's PID is `NUMBER`\n"
  3545. "\n"
  3546. " uid:NUMBER\n"
  3547. " Shows only processes that their UID is `NUMBER`\n"
  3548. "\n"
  3549. " gid:NUMBER\n"
  3550. " Shows only processes that their GID is `NUMBER`\n"
  3551. "\n"
  3552. "Filters can be combined. Each filter can be given only one time.\n"
  3553. );
  3554. pluginsd_function_result_to_stdout(transaction, HTTP_RESP_OK, "text/plain", now_realtime_sec() + 3600, wb);
  3555. buffer_free(wb);
  3556. }
  3557. #define add_value_field_llu_with_max(wb, key, value) do { \
  3558. unsigned long long _tmp = (value); \
  3559. key ## _max = (rows == 0) ? (_tmp) : MAX(key ## _max, _tmp); \
  3560. buffer_json_add_array_item_uint64(wb, _tmp); \
  3561. } while(0)
  3562. #define add_value_field_ndd_with_max(wb, key, value) do { \
  3563. NETDATA_DOUBLE _tmp = (value); \
  3564. key ## _max = (rows == 0) ? (_tmp) : MAX(key ## _max, _tmp); \
  3565. buffer_json_add_array_item_double(wb, _tmp); \
  3566. } while(0)
  3567. static void function_processes(const char *transaction, char *function __maybe_unused, int timeout __maybe_unused, bool *cancelled __maybe_unused) {
  3568. struct pid_stat *p;
  3569. char *words[PLUGINSD_MAX_WORDS] = { NULL };
  3570. size_t num_words = quoted_strings_splitter_pluginsd(function, words, PLUGINSD_MAX_WORDS);
  3571. struct target *category = NULL, *user = NULL, *group = NULL;
  3572. const char *process_name = NULL;
  3573. pid_t pid = 0;
  3574. uid_t uid = 0;
  3575. gid_t gid = 0;
  3576. bool filter_pid = false, filter_uid = false, filter_gid = false;
  3577. for(int i = 1; i < PLUGINSD_MAX_WORDS ;i++) {
  3578. const char *keyword = get_word(words, num_words, i);
  3579. if(!keyword) break;
  3580. if(!category && strncmp(keyword, PROCESS_FILTER_CATEGORY, strlen(PROCESS_FILTER_CATEGORY)) == 0) {
  3581. category = find_target_by_name(apps_groups_root_target, &keyword[strlen(PROCESS_FILTER_CATEGORY)]);
  3582. if(!category) {
  3583. pluginsd_function_json_error_to_stdout(transaction, HTTP_RESP_BAD_REQUEST,
  3584. "No category with that name found.");
  3585. return;
  3586. }
  3587. }
  3588. else if(!user && strncmp(keyword, PROCESS_FILTER_USER, strlen(PROCESS_FILTER_USER)) == 0) {
  3589. user = find_target_by_name(users_root_target, &keyword[strlen(PROCESS_FILTER_USER)]);
  3590. if(!user) {
  3591. pluginsd_function_json_error_to_stdout(transaction, HTTP_RESP_BAD_REQUEST,
  3592. "No user with that name found.");
  3593. return;
  3594. }
  3595. }
  3596. else if(strncmp(keyword, PROCESS_FILTER_GROUP, strlen(PROCESS_FILTER_GROUP)) == 0) {
  3597. group = find_target_by_name(groups_root_target, &keyword[strlen(PROCESS_FILTER_GROUP)]);
  3598. if(!group) {
  3599. pluginsd_function_json_error_to_stdout(transaction, HTTP_RESP_BAD_REQUEST,
  3600. "No group with that name found.");
  3601. return;
  3602. }
  3603. }
  3604. else if(!process_name && strncmp(keyword, PROCESS_FILTER_PROCESS, strlen(PROCESS_FILTER_PROCESS)) == 0) {
  3605. process_name = &keyword[strlen(PROCESS_FILTER_PROCESS)];
  3606. }
  3607. else if(!pid && strncmp(keyword, PROCESS_FILTER_PID, strlen(PROCESS_FILTER_PID)) == 0) {
  3608. pid = str2i(&keyword[strlen(PROCESS_FILTER_PID)]);
  3609. filter_pid = true;
  3610. }
  3611. else if(!uid && strncmp(keyword, PROCESS_FILTER_UID, strlen(PROCESS_FILTER_UID)) == 0) {
  3612. uid = str2i(&keyword[strlen(PROCESS_FILTER_UID)]);
  3613. filter_uid = true;
  3614. }
  3615. else if(!gid && strncmp(keyword, PROCESS_FILTER_GID, strlen(PROCESS_FILTER_GID)) == 0) {
  3616. gid = str2i(&keyword[strlen(PROCESS_FILTER_GID)]);
  3617. filter_gid = true;
  3618. }
  3619. else if(strcmp(keyword, "help") == 0) {
  3620. apps_plugin_function_processes_help(transaction);
  3621. return;
  3622. }
  3623. else {
  3624. char msg[PLUGINSD_LINE_MAX];
  3625. snprintfz(msg, PLUGINSD_LINE_MAX, "Invalid parameter '%s'", keyword);
  3626. pluginsd_function_json_error_to_stdout(transaction, HTTP_RESP_BAD_REQUEST, msg);
  3627. return;
  3628. }
  3629. }
  3630. time_t expires = now_realtime_sec() + update_every;
  3631. unsigned int cpu_divisor = time_factor * RATES_DETAIL / 100;
  3632. unsigned int memory_divisor = 1024;
  3633. unsigned int io_divisor = 1024 * RATES_DETAIL;
  3634. BUFFER *wb = buffer_create(PLUGINSD_LINE_MAX, NULL);
  3635. buffer_json_initialize(wb, "\"", "\"", 0, true, BUFFER_JSON_OPTIONS_NEWLINE_ON_ARRAY_ITEMS);
  3636. buffer_json_member_add_uint64(wb, "status", HTTP_RESP_OK);
  3637. buffer_json_member_add_string(wb, "type", "table");
  3638. buffer_json_member_add_time_t(wb, "update_every", update_every);
  3639. buffer_json_member_add_string(wb, "help", APPS_PLUGIN_PROCESSES_FUNCTION_DESCRIPTION);
  3640. buffer_json_member_add_array(wb, "data");
  3641. NETDATA_DOUBLE
  3642. UserCPU_max = 0.0
  3643. , SysCPU_max = 0.0
  3644. , GuestCPU_max = 0.0
  3645. , CUserCPU_max = 0.0
  3646. , CSysCPU_max = 0.0
  3647. , CGuestCPU_max = 0.0
  3648. , CPU_max = 0.0
  3649. , VMSize_max = 0.0
  3650. , RSS_max = 0.0
  3651. , Shared_max = 0.0
  3652. , Swap_max = 0.0
  3653. , Memory_max = 0.0
  3654. , FDsLimitPercent_max = 0.0
  3655. ;
  3656. unsigned long long
  3657. Processes_max = 0
  3658. , Threads_max = 0
  3659. , VoluntaryCtxtSwitches_max = 0
  3660. , NonVoluntaryCtxtSwitches_max = 0
  3661. , Uptime_max = 0
  3662. , MinFlt_max = 0
  3663. , CMinFlt_max = 0
  3664. , TMinFlt_max = 0
  3665. , MajFlt_max = 0
  3666. , CMajFlt_max = 0
  3667. , TMajFlt_max = 0
  3668. , PReads_max = 0
  3669. , PWrites_max = 0
  3670. , RCalls_max = 0
  3671. , WCalls_max = 0
  3672. , Files_max = 0
  3673. , Pipes_max = 0
  3674. , Sockets_max = 0
  3675. , iNotiFDs_max = 0
  3676. , EventFDs_max = 0
  3677. , TimerFDs_max = 0
  3678. , SigFDs_max = 0
  3679. , EvPollFDs_max = 0
  3680. , OtherFDs_max = 0
  3681. , FDs_max = 0
  3682. ;
  3683. #ifndef __FreeBSD__
  3684. unsigned long long
  3685. LReads_max = 0
  3686. , LWrites_max = 0
  3687. ;
  3688. #endif
  3689. int rows= 0;
  3690. for(p = root_of_pids; p ; p = p->next) {
  3691. if(!p->updated)
  3692. continue;
  3693. if(category && p->target != category)
  3694. continue;
  3695. if(user && p->user_target != user)
  3696. continue;
  3697. if(group && p->group_target != group)
  3698. continue;
  3699. if(process_name && ((strcmp(p->comm, process_name) != 0 && !p->parent) || (p->parent && strcmp(p->comm, process_name) != 0 && strcmp(p->parent->comm, process_name) != 0)))
  3700. continue;
  3701. if(filter_pid && p->pid != pid && p->ppid != pid)
  3702. continue;
  3703. if(filter_uid && p->uid != uid)
  3704. continue;
  3705. if(filter_gid && p->gid != gid)
  3706. continue;
  3707. rows++;
  3708. buffer_json_add_array_item_array(wb); // for each pid
  3709. // IMPORTANT!
  3710. // THE ORDER SHOULD BE THE SAME WITH THE FIELDS!
  3711. // pid
  3712. buffer_json_add_array_item_uint64(wb, p->pid);
  3713. // cmd
  3714. buffer_json_add_array_item_string(wb, p->comm);
  3715. // cmdline
  3716. if (enable_function_cmdline) {
  3717. buffer_json_add_array_item_string(wb, (p->cmdline && *p->cmdline) ? p->cmdline : p->comm);
  3718. }
  3719. // ppid
  3720. buffer_json_add_array_item_uint64(wb, p->ppid);
  3721. // category
  3722. buffer_json_add_array_item_string(wb, p->target ? p->target->name : "-");
  3723. // user
  3724. buffer_json_add_array_item_string(wb, p->user_target ? p->user_target->name : "-");
  3725. // uid
  3726. buffer_json_add_array_item_uint64(wb, p->uid);
  3727. // group
  3728. buffer_json_add_array_item_string(wb, p->group_target ? p->group_target->name : "-");
  3729. // gid
  3730. buffer_json_add_array_item_uint64(wb, p->gid);
  3731. // CPU utilization %
  3732. add_value_field_ndd_with_max(wb, CPU, (NETDATA_DOUBLE)(p->utime + p->stime + p->gtime + p->cutime + p->cstime + p->cgtime) / cpu_divisor);
  3733. add_value_field_ndd_with_max(wb, UserCPU, (NETDATA_DOUBLE)(p->utime) / cpu_divisor);
  3734. add_value_field_ndd_with_max(wb, SysCPU, (NETDATA_DOUBLE)(p->stime) / cpu_divisor);
  3735. add_value_field_ndd_with_max(wb, GuestCPU, (NETDATA_DOUBLE)(p->gtime) / cpu_divisor);
  3736. add_value_field_ndd_with_max(wb, CUserCPU, (NETDATA_DOUBLE)(p->cutime) / cpu_divisor);
  3737. add_value_field_ndd_with_max(wb, CSysCPU, (NETDATA_DOUBLE)(p->cstime) / cpu_divisor);
  3738. add_value_field_ndd_with_max(wb, CGuestCPU, (NETDATA_DOUBLE)(p->cgtime) / cpu_divisor);
  3739. add_value_field_llu_with_max(wb, VoluntaryCtxtSwitches, p->status_voluntary_ctxt_switches / RATES_DETAIL);
  3740. add_value_field_llu_with_max(wb, NonVoluntaryCtxtSwitches, p->status_nonvoluntary_ctxt_switches / RATES_DETAIL);
  3741. // memory MiB
  3742. if(MemTotal)
  3743. add_value_field_ndd_with_max(wb, Memory, (NETDATA_DOUBLE)p->status_vmrss * 100.0 / (NETDATA_DOUBLE)MemTotal);
  3744. add_value_field_ndd_with_max(wb, RSS, (NETDATA_DOUBLE)p->status_vmrss / memory_divisor);
  3745. add_value_field_ndd_with_max(wb, Shared, (NETDATA_DOUBLE)p->status_vmshared / memory_divisor);
  3746. add_value_field_ndd_with_max(wb, VMSize, (NETDATA_DOUBLE)p->status_vmsize / memory_divisor);
  3747. add_value_field_ndd_with_max(wb, Swap, (NETDATA_DOUBLE)p->status_vmswap / memory_divisor);
  3748. // Physical I/O
  3749. add_value_field_llu_with_max(wb, PReads, p->io_storage_bytes_read / io_divisor);
  3750. add_value_field_llu_with_max(wb, PWrites, p->io_storage_bytes_written / io_divisor);
  3751. // Logical I/O
  3752. #ifndef __FreeBSD__
  3753. add_value_field_llu_with_max(wb, LReads, p->io_logical_bytes_read / io_divisor);
  3754. add_value_field_llu_with_max(wb, LWrites, p->io_logical_bytes_written / io_divisor);
  3755. #endif
  3756. // I/O calls
  3757. add_value_field_llu_with_max(wb, RCalls, p->io_read_calls / RATES_DETAIL);
  3758. add_value_field_llu_with_max(wb, WCalls, p->io_write_calls / RATES_DETAIL);
  3759. // minor page faults
  3760. add_value_field_llu_with_max(wb, MinFlt, p->minflt / RATES_DETAIL);
  3761. add_value_field_llu_with_max(wb, CMinFlt, p->cminflt / RATES_DETAIL);
  3762. add_value_field_llu_with_max(wb, TMinFlt, (p->minflt + p->cminflt) / RATES_DETAIL);
  3763. // major page faults
  3764. add_value_field_llu_with_max(wb, MajFlt, p->majflt / RATES_DETAIL);
  3765. add_value_field_llu_with_max(wb, CMajFlt, p->cmajflt / RATES_DETAIL);
  3766. add_value_field_llu_with_max(wb, TMajFlt, (p->majflt + p->cmajflt) / RATES_DETAIL);
  3767. // open file descriptors
  3768. add_value_field_ndd_with_max(wb, FDsLimitPercent, p->openfds_limits_percent);
  3769. add_value_field_llu_with_max(wb, FDs, pid_openfds_sum(p));
  3770. add_value_field_llu_with_max(wb, Files, p->openfds.files);
  3771. add_value_field_llu_with_max(wb, Pipes, p->openfds.pipes);
  3772. add_value_field_llu_with_max(wb, Sockets, p->openfds.sockets);
  3773. add_value_field_llu_with_max(wb, iNotiFDs, p->openfds.inotifies);
  3774. add_value_field_llu_with_max(wb, EventFDs, p->openfds.eventfds);
  3775. add_value_field_llu_with_max(wb, TimerFDs, p->openfds.timerfds);
  3776. add_value_field_llu_with_max(wb, SigFDs, p->openfds.signalfds);
  3777. add_value_field_llu_with_max(wb, EvPollFDs, p->openfds.eventpolls);
  3778. add_value_field_llu_with_max(wb, OtherFDs, p->openfds.other);
  3779. // processes, threads, uptime
  3780. add_value_field_llu_with_max(wb, Processes, p->children_count);
  3781. add_value_field_llu_with_max(wb, Threads, p->num_threads);
  3782. add_value_field_llu_with_max(wb, Uptime, p->uptime);
  3783. buffer_json_array_close(wb); // for each pid
  3784. }
  3785. buffer_json_array_close(wb); // data
  3786. buffer_json_member_add_object(wb, "columns");
  3787. {
  3788. int field_id = 0;
  3789. // IMPORTANT!
  3790. // THE ORDER SHOULD BE THE SAME WITH THE VALUES!
  3791. // wb, key, name, visible, type, visualization, transform, decimal_points, units, max, sort, sortable, sticky, unique_key, pointer_to, summary, range
  3792. buffer_rrdf_table_add_field(wb, field_id++, "PID", "Process ID", RRDF_FIELD_TYPE_INTEGER,
  3793. RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER, 0, NULL, NAN,
  3794. RRDF_FIELD_SORT_ASCENDING, NULL, RRDF_FIELD_SUMMARY_COUNT,
  3795. RRDF_FIELD_FILTER_MULTISELECT,
  3796. RRDF_FIELD_OPTS_VISIBLE | RRDF_FIELD_OPTS_STICKY |
  3797. RRDF_FIELD_OPTS_UNIQUE_KEY, NULL);
  3798. buffer_rrdf_table_add_field(wb, field_id++, "Cmd", "Process Name", RRDF_FIELD_TYPE_STRING,
  3799. RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE, 0, NULL, NAN,
  3800. RRDF_FIELD_SORT_ASCENDING, NULL, RRDF_FIELD_SUMMARY_COUNT,
  3801. RRDF_FIELD_FILTER_MULTISELECT,
  3802. RRDF_FIELD_OPTS_VISIBLE | RRDF_FIELD_OPTS_STICKY, NULL);
  3803. if (enable_function_cmdline) {
  3804. buffer_rrdf_table_add_field(wb, field_id++, "CmdLine", "Command Line", RRDF_FIELD_TYPE_STRING,
  3805. RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE, 0,
  3806. NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL, RRDF_FIELD_SUMMARY_COUNT,
  3807. RRDF_FIELD_FILTER_MULTISELECT,
  3808. RRDF_FIELD_OPTS_NONE, NULL);
  3809. }
  3810. buffer_rrdf_table_add_field(wb, field_id++, "PPID", "Parent Process ID", RRDF_FIELD_TYPE_INTEGER,
  3811. RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER, 0, NULL,
  3812. NAN, RRDF_FIELD_SORT_ASCENDING, "PID", RRDF_FIELD_SUMMARY_COUNT,
  3813. RRDF_FIELD_FILTER_MULTISELECT,
  3814. RRDF_FIELD_OPTS_NONE, NULL);
  3815. buffer_rrdf_table_add_field(wb, field_id++, "Category", "Category (apps_groups.conf)", RRDF_FIELD_TYPE_STRING,
  3816. RRDF_FIELD_VISUAL_VALUE,
  3817. RRDF_FIELD_TRANSFORM_NONE,
  3818. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL, RRDF_FIELD_SUMMARY_COUNT,
  3819. RRDF_FIELD_FILTER_MULTISELECT,
  3820. RRDF_FIELD_OPTS_VISIBLE | RRDF_FIELD_OPTS_STICKY, NULL);
  3821. buffer_rrdf_table_add_field(wb, field_id++, "User", "User Owner", RRDF_FIELD_TYPE_STRING,
  3822. RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE, 0, NULL, NAN,
  3823. RRDF_FIELD_SORT_ASCENDING, NULL, RRDF_FIELD_SUMMARY_COUNT,
  3824. RRDF_FIELD_FILTER_MULTISELECT,
  3825. RRDF_FIELD_OPTS_VISIBLE, NULL);
  3826. buffer_rrdf_table_add_field(wb, field_id++, "Uid", "User ID", RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE,
  3827. RRDF_FIELD_TRANSFORM_NUMBER, 0, NULL, NAN,
  3828. RRDF_FIELD_SORT_ASCENDING, NULL, RRDF_FIELD_SUMMARY_COUNT,
  3829. RRDF_FIELD_FILTER_MULTISELECT,
  3830. RRDF_FIELD_OPTS_NONE, NULL);
  3831. buffer_rrdf_table_add_field(wb, field_id++, "Group", "Group Owner", RRDF_FIELD_TYPE_STRING,
  3832. RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE, 0, NULL, NAN,
  3833. RRDF_FIELD_SORT_ASCENDING, NULL, RRDF_FIELD_SUMMARY_COUNT,
  3834. RRDF_FIELD_FILTER_MULTISELECT,
  3835. RRDF_FIELD_OPTS_NONE, NULL);
  3836. buffer_rrdf_table_add_field(wb, field_id++, "Gid", "Group ID", RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE,
  3837. RRDF_FIELD_TRANSFORM_NUMBER, 0, NULL, NAN,
  3838. RRDF_FIELD_SORT_ASCENDING, NULL, RRDF_FIELD_SUMMARY_COUNT,
  3839. RRDF_FIELD_FILTER_MULTISELECT,
  3840. RRDF_FIELD_OPTS_NONE, NULL);
  3841. // CPU utilization
  3842. buffer_rrdf_table_add_field(wb, field_id++, "CPU", "Total CPU Time (100% = 1 core)",
  3843. RRDF_FIELD_TYPE_BAR_WITH_INTEGER, RRDF_FIELD_VISUAL_BAR,
  3844. RRDF_FIELD_TRANSFORM_NUMBER, 2, "%", CPU_max, RRDF_FIELD_SORT_DESCENDING, NULL,
  3845. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  3846. RRDF_FIELD_OPTS_VISIBLE, NULL);
  3847. buffer_rrdf_table_add_field(wb, field_id++, "UserCPU", "User CPU time (100% = 1 core)",
  3848. RRDF_FIELD_TYPE_BAR_WITH_INTEGER,
  3849. RRDF_FIELD_VISUAL_BAR, RRDF_FIELD_TRANSFORM_NUMBER, 2, "%", UserCPU_max,
  3850. RRDF_FIELD_SORT_DESCENDING, NULL, RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  3851. RRDF_FIELD_OPTS_NONE, NULL);
  3852. buffer_rrdf_table_add_field(wb, field_id++, "SysCPU", "System CPU Time (100% = 1 core)",
  3853. RRDF_FIELD_TYPE_BAR_WITH_INTEGER,
  3854. RRDF_FIELD_VISUAL_BAR, RRDF_FIELD_TRANSFORM_NUMBER, 2, "%", SysCPU_max,
  3855. RRDF_FIELD_SORT_DESCENDING, NULL, RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  3856. RRDF_FIELD_OPTS_NONE, NULL);
  3857. buffer_rrdf_table_add_field(wb, field_id++, "GuestCPU", "Guest CPU Time (100% = 1 core)",
  3858. RRDF_FIELD_TYPE_BAR_WITH_INTEGER,
  3859. RRDF_FIELD_VISUAL_BAR, RRDF_FIELD_TRANSFORM_NUMBER, 2, "%", GuestCPU_max,
  3860. RRDF_FIELD_SORT_DESCENDING, NULL, RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  3861. RRDF_FIELD_OPTS_NONE, NULL);
  3862. buffer_rrdf_table_add_field(wb, field_id++, "CUserCPU", "Children User CPU Time (100% = 1 core)",
  3863. RRDF_FIELD_TYPE_BAR_WITH_INTEGER, RRDF_FIELD_VISUAL_BAR,
  3864. RRDF_FIELD_TRANSFORM_NUMBER, 2, "%", CUserCPU_max, RRDF_FIELD_SORT_DESCENDING, NULL,
  3865. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  3866. RRDF_FIELD_OPTS_NONE, NULL);
  3867. buffer_rrdf_table_add_field(wb, field_id++, "CSysCPU", "Children System CPU Time (100% = 1 core)",
  3868. RRDF_FIELD_TYPE_BAR_WITH_INTEGER, RRDF_FIELD_VISUAL_BAR,
  3869. RRDF_FIELD_TRANSFORM_NUMBER, 2, "%", CSysCPU_max, RRDF_FIELD_SORT_DESCENDING, NULL,
  3870. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  3871. RRDF_FIELD_OPTS_NONE, NULL);
  3872. buffer_rrdf_table_add_field(wb, field_id++, "CGuestCPU", "Children Guest CPU Time (100% = 1 core)",
  3873. RRDF_FIELD_TYPE_BAR_WITH_INTEGER, RRDF_FIELD_VISUAL_BAR,
  3874. RRDF_FIELD_TRANSFORM_NUMBER, 2, "%", CGuestCPU_max, RRDF_FIELD_SORT_DESCENDING,
  3875. NULL,
  3876. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE, RRDF_FIELD_OPTS_NONE, NULL);
  3877. // CPU context switches
  3878. buffer_rrdf_table_add_field(wb, field_id++, "vCtxSwitch", "Voluntary Context Switches",
  3879. RRDF_FIELD_TYPE_BAR_WITH_INTEGER,
  3880. RRDF_FIELD_VISUAL_BAR, RRDF_FIELD_TRANSFORM_NUMBER, 2, "switches/s",
  3881. VoluntaryCtxtSwitches_max, RRDF_FIELD_SORT_DESCENDING, NULL,
  3882. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE, RRDF_FIELD_OPTS_NONE, NULL);
  3883. buffer_rrdf_table_add_field(wb, field_id++, "iCtxSwitch", "Involuntary Context Switches",
  3884. RRDF_FIELD_TYPE_BAR_WITH_INTEGER,
  3885. RRDF_FIELD_VISUAL_BAR, RRDF_FIELD_TRANSFORM_NUMBER, 2, "switches/s",
  3886. NonVoluntaryCtxtSwitches_max, RRDF_FIELD_SORT_DESCENDING, NULL,
  3887. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE, RRDF_FIELD_OPTS_NONE, NULL);
  3888. // memory
  3889. if (MemTotal)
  3890. buffer_rrdf_table_add_field(wb, field_id++, "Memory", "Memory Percentage", RRDF_FIELD_TYPE_BAR_WITH_INTEGER,
  3891. RRDF_FIELD_VISUAL_BAR,
  3892. RRDF_FIELD_TRANSFORM_NUMBER, 2, "%", 100.0, RRDF_FIELD_SORT_DESCENDING, NULL,
  3893. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  3894. RRDF_FIELD_OPTS_VISIBLE, NULL);
  3895. buffer_rrdf_table_add_field(wb, field_id++, "Resident", "Resident Set Size", RRDF_FIELD_TYPE_BAR_WITH_INTEGER,
  3896. RRDF_FIELD_VISUAL_BAR,
  3897. RRDF_FIELD_TRANSFORM_NUMBER,
  3898. 2, "MiB", RSS_max, RRDF_FIELD_SORT_DESCENDING, NULL, RRDF_FIELD_SUMMARY_SUM,
  3899. RRDF_FIELD_FILTER_RANGE,
  3900. RRDF_FIELD_OPTS_VISIBLE, NULL);
  3901. buffer_rrdf_table_add_field(wb, field_id++, "Shared", "Shared Pages", RRDF_FIELD_TYPE_BAR_WITH_INTEGER,
  3902. RRDF_FIELD_VISUAL_BAR, RRDF_FIELD_TRANSFORM_NUMBER, 2,
  3903. "MiB", Shared_max, RRDF_FIELD_SORT_DESCENDING, NULL, RRDF_FIELD_SUMMARY_SUM,
  3904. RRDF_FIELD_FILTER_RANGE,
  3905. RRDF_FIELD_OPTS_VISIBLE, NULL);
  3906. buffer_rrdf_table_add_field(wb, field_id++, "Virtual", "Virtual Memory Size", RRDF_FIELD_TYPE_BAR_WITH_INTEGER,
  3907. RRDF_FIELD_VISUAL_BAR,
  3908. RRDF_FIELD_TRANSFORM_NUMBER, 2, "MiB", VMSize_max, RRDF_FIELD_SORT_DESCENDING, NULL,
  3909. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  3910. RRDF_FIELD_OPTS_VISIBLE, NULL);
  3911. buffer_rrdf_table_add_field(wb, field_id++, "Swap", "Swap Memory", RRDF_FIELD_TYPE_BAR_WITH_INTEGER,
  3912. RRDF_FIELD_VISUAL_BAR, RRDF_FIELD_TRANSFORM_NUMBER, 2,
  3913. "MiB",
  3914. Swap_max, RRDF_FIELD_SORT_DESCENDING, NULL, RRDF_FIELD_SUMMARY_SUM,
  3915. RRDF_FIELD_FILTER_RANGE,
  3916. RRDF_FIELD_OPTS_NONE, NULL);
  3917. // Physical I/O
  3918. buffer_rrdf_table_add_field(wb, field_id++, "PReads", "Physical I/O Reads", RRDF_FIELD_TYPE_BAR_WITH_INTEGER,
  3919. RRDF_FIELD_VISUAL_BAR, RRDF_FIELD_TRANSFORM_NUMBER,
  3920. 2, "KiB/s", PReads_max, RRDF_FIELD_SORT_DESCENDING, NULL, RRDF_FIELD_SUMMARY_SUM,
  3921. RRDF_FIELD_FILTER_RANGE,
  3922. RRDF_FIELD_OPTS_VISIBLE, NULL);
  3923. buffer_rrdf_table_add_field(wb, field_id++, "PWrites", "Physical I/O Writes", RRDF_FIELD_TYPE_BAR_WITH_INTEGER,
  3924. RRDF_FIELD_VISUAL_BAR,
  3925. RRDF_FIELD_TRANSFORM_NUMBER, 2, "KiB/s", PWrites_max, RRDF_FIELD_SORT_DESCENDING,
  3926. NULL, RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  3927. RRDF_FIELD_OPTS_VISIBLE, NULL);
  3928. // Logical I/O
  3929. #ifndef __FreeBSD__
  3930. buffer_rrdf_table_add_field(wb, field_id++, "LReads", "Logical I/O Reads", RRDF_FIELD_TYPE_BAR_WITH_INTEGER,
  3931. RRDF_FIELD_VISUAL_BAR, RRDF_FIELD_TRANSFORM_NUMBER,
  3932. 2, "KiB/s", LReads_max, RRDF_FIELD_SORT_DESCENDING, NULL, RRDF_FIELD_SUMMARY_SUM,
  3933. RRDF_FIELD_FILTER_RANGE,
  3934. RRDF_FIELD_OPTS_NONE, NULL);
  3935. buffer_rrdf_table_add_field(wb, field_id++, "LWrites", "Logical I/O Writes", RRDF_FIELD_TYPE_BAR_WITH_INTEGER,
  3936. RRDF_FIELD_VISUAL_BAR,
  3937. RRDF_FIELD_TRANSFORM_NUMBER,
  3938. 2, "KiB/s", LWrites_max, RRDF_FIELD_SORT_DESCENDING, NULL, RRDF_FIELD_SUMMARY_SUM,
  3939. RRDF_FIELD_FILTER_RANGE,
  3940. RRDF_FIELD_OPTS_NONE, NULL);
  3941. #endif
  3942. // I/O calls
  3943. buffer_rrdf_table_add_field(wb, field_id++, "RCalls", "I/O Read Calls", RRDF_FIELD_TYPE_BAR_WITH_INTEGER,
  3944. RRDF_FIELD_VISUAL_BAR, RRDF_FIELD_TRANSFORM_NUMBER, 2,
  3945. "calls/s", RCalls_max, RRDF_FIELD_SORT_DESCENDING, NULL, RRDF_FIELD_SUMMARY_SUM,
  3946. RRDF_FIELD_FILTER_RANGE,
  3947. RRDF_FIELD_OPTS_NONE, NULL);
  3948. buffer_rrdf_table_add_field(wb, field_id++, "WCalls", "I/O Write Calls", RRDF_FIELD_TYPE_BAR_WITH_INTEGER,
  3949. RRDF_FIELD_VISUAL_BAR, RRDF_FIELD_TRANSFORM_NUMBER, 2,
  3950. "calls/s", WCalls_max, RRDF_FIELD_SORT_DESCENDING, NULL, RRDF_FIELD_SUMMARY_SUM,
  3951. RRDF_FIELD_FILTER_RANGE,
  3952. RRDF_FIELD_OPTS_NONE, NULL);
  3953. // minor page faults
  3954. buffer_rrdf_table_add_field(wb, field_id++, "MinFlt", "Minor Page Faults/s", RRDF_FIELD_TYPE_BAR_WITH_INTEGER,
  3955. RRDF_FIELD_VISUAL_BAR,
  3956. RRDF_FIELD_TRANSFORM_NUMBER,
  3957. 2, "pgflts/s", MinFlt_max, RRDF_FIELD_SORT_DESCENDING, NULL, RRDF_FIELD_SUMMARY_SUM,
  3958. RRDF_FIELD_FILTER_RANGE,
  3959. RRDF_FIELD_OPTS_NONE, NULL);
  3960. buffer_rrdf_table_add_field(wb, field_id++, "CMinFlt", "Children Minor Page Faults/s",
  3961. RRDF_FIELD_TYPE_BAR_WITH_INTEGER,
  3962. RRDF_FIELD_VISUAL_BAR,
  3963. RRDF_FIELD_TRANSFORM_NUMBER, 2, "pgflts/s", CMinFlt_max, RRDF_FIELD_SORT_DESCENDING,
  3964. NULL, RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  3965. RRDF_FIELD_OPTS_NONE, NULL);
  3966. buffer_rrdf_table_add_field(wb, field_id++, "TMinFlt", "Total Minor Page Faults/s",
  3967. RRDF_FIELD_TYPE_BAR_WITH_INTEGER, RRDF_FIELD_VISUAL_BAR,
  3968. RRDF_FIELD_TRANSFORM_NUMBER, 2, "pgflts/s", TMinFlt_max, RRDF_FIELD_SORT_DESCENDING,
  3969. NULL, RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  3970. RRDF_FIELD_OPTS_NONE, NULL);
  3971. // major page faults
  3972. buffer_rrdf_table_add_field(wb, field_id++, "MajFlt", "Major Page Faults/s", RRDF_FIELD_TYPE_BAR_WITH_INTEGER,
  3973. RRDF_FIELD_VISUAL_BAR,
  3974. RRDF_FIELD_TRANSFORM_NUMBER,
  3975. 2, "pgflts/s", MajFlt_max, RRDF_FIELD_SORT_DESCENDING, NULL, RRDF_FIELD_SUMMARY_SUM,
  3976. RRDF_FIELD_FILTER_RANGE,
  3977. RRDF_FIELD_OPTS_NONE, NULL);
  3978. buffer_rrdf_table_add_field(wb, field_id++, "CMajFlt", "Children Major Page Faults/s",
  3979. RRDF_FIELD_TYPE_BAR_WITH_INTEGER,
  3980. RRDF_FIELD_VISUAL_BAR,
  3981. RRDF_FIELD_TRANSFORM_NUMBER, 2, "pgflts/s", CMajFlt_max, RRDF_FIELD_SORT_DESCENDING,
  3982. NULL, RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  3983. RRDF_FIELD_OPTS_NONE, NULL);
  3984. buffer_rrdf_table_add_field(wb, field_id++, "TMajFlt", "Total Major Page Faults/s",
  3985. RRDF_FIELD_TYPE_BAR_WITH_INTEGER, RRDF_FIELD_VISUAL_BAR,
  3986. RRDF_FIELD_TRANSFORM_NUMBER, 2, "pgflts/s", TMajFlt_max, RRDF_FIELD_SORT_DESCENDING,
  3987. NULL, RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  3988. RRDF_FIELD_OPTS_NONE, NULL);
  3989. // open file descriptors
  3990. buffer_rrdf_table_add_field(wb, field_id++, "FDsLimitPercent", "Percentage of Open Descriptors vs Limits",
  3991. RRDF_FIELD_TYPE_BAR_WITH_INTEGER, RRDF_FIELD_VISUAL_BAR,
  3992. RRDF_FIELD_TRANSFORM_NUMBER, 2, "%", FDsLimitPercent_max, RRDF_FIELD_SORT_DESCENDING, NULL,
  3993. RRDF_FIELD_SUMMARY_MAX, RRDF_FIELD_FILTER_RANGE,
  3994. RRDF_FIELD_OPTS_NONE, NULL);
  3995. buffer_rrdf_table_add_field(wb, field_id++, "FDs", "All Open File Descriptors",
  3996. RRDF_FIELD_TYPE_BAR_WITH_INTEGER, RRDF_FIELD_VISUAL_BAR,
  3997. RRDF_FIELD_TRANSFORM_NUMBER, 0, "fds", FDs_max, RRDF_FIELD_SORT_DESCENDING, NULL,
  3998. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  3999. RRDF_FIELD_OPTS_NONE, NULL);
  4000. buffer_rrdf_table_add_field(wb, field_id++, "Files", "Open Files", RRDF_FIELD_TYPE_BAR_WITH_INTEGER,
  4001. RRDF_FIELD_VISUAL_BAR, RRDF_FIELD_TRANSFORM_NUMBER, 0,
  4002. "fds",
  4003. Files_max, RRDF_FIELD_SORT_DESCENDING, NULL, RRDF_FIELD_SUMMARY_SUM,
  4004. RRDF_FIELD_FILTER_RANGE,
  4005. RRDF_FIELD_OPTS_NONE, NULL);
  4006. buffer_rrdf_table_add_field(wb, field_id++, "Pipes", "Open Pipes", RRDF_FIELD_TYPE_BAR_WITH_INTEGER,
  4007. RRDF_FIELD_VISUAL_BAR, RRDF_FIELD_TRANSFORM_NUMBER, 0,
  4008. "fds",
  4009. Pipes_max, RRDF_FIELD_SORT_DESCENDING, NULL, RRDF_FIELD_SUMMARY_SUM,
  4010. RRDF_FIELD_FILTER_RANGE,
  4011. RRDF_FIELD_OPTS_NONE, NULL);
  4012. buffer_rrdf_table_add_field(wb, field_id++, "Sockets", "Open Sockets", RRDF_FIELD_TYPE_BAR_WITH_INTEGER,
  4013. RRDF_FIELD_VISUAL_BAR, RRDF_FIELD_TRANSFORM_NUMBER, 0,
  4014. "fds", Sockets_max, RRDF_FIELD_SORT_DESCENDING, NULL, RRDF_FIELD_SUMMARY_SUM,
  4015. RRDF_FIELD_FILTER_RANGE,
  4016. RRDF_FIELD_OPTS_NONE, NULL);
  4017. buffer_rrdf_table_add_field(wb, field_id++, "iNotiFDs", "Open iNotify Descriptors",
  4018. RRDF_FIELD_TYPE_BAR_WITH_INTEGER, RRDF_FIELD_VISUAL_BAR,
  4019. RRDF_FIELD_TRANSFORM_NUMBER, 0, "fds", iNotiFDs_max, RRDF_FIELD_SORT_DESCENDING,
  4020. NULL, RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  4021. RRDF_FIELD_OPTS_NONE, NULL);
  4022. buffer_rrdf_table_add_field(wb, field_id++, "EventFDs", "Open Event Descriptors",
  4023. RRDF_FIELD_TYPE_BAR_WITH_INTEGER, RRDF_FIELD_VISUAL_BAR,
  4024. RRDF_FIELD_TRANSFORM_NUMBER, 0, "fds", EventFDs_max, RRDF_FIELD_SORT_DESCENDING,
  4025. NULL, RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  4026. RRDF_FIELD_OPTS_NONE, NULL);
  4027. buffer_rrdf_table_add_field(wb, field_id++, "TimerFDs", "Open Timer Descriptors",
  4028. RRDF_FIELD_TYPE_BAR_WITH_INTEGER, RRDF_FIELD_VISUAL_BAR,
  4029. RRDF_FIELD_TRANSFORM_NUMBER, 0, "fds", TimerFDs_max, RRDF_FIELD_SORT_DESCENDING,
  4030. NULL, RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  4031. RRDF_FIELD_OPTS_NONE, NULL);
  4032. buffer_rrdf_table_add_field(wb, field_id++, "SigFDs", "Open Signal Descriptors",
  4033. RRDF_FIELD_TYPE_BAR_WITH_INTEGER, RRDF_FIELD_VISUAL_BAR,
  4034. RRDF_FIELD_TRANSFORM_NUMBER, 0, "fds", SigFDs_max, RRDF_FIELD_SORT_DESCENDING, NULL,
  4035. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  4036. RRDF_FIELD_OPTS_NONE, NULL);
  4037. buffer_rrdf_table_add_field(wb, field_id++, "EvPollFDs", "Open Event Poll Descriptors",
  4038. RRDF_FIELD_TYPE_BAR_WITH_INTEGER,
  4039. RRDF_FIELD_VISUAL_BAR, RRDF_FIELD_TRANSFORM_NUMBER, 0, "fds", EvPollFDs_max,
  4040. RRDF_FIELD_SORT_DESCENDING, NULL, RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  4041. RRDF_FIELD_OPTS_NONE, NULL);
  4042. buffer_rrdf_table_add_field(wb, field_id++, "OtherFDs", "Other Open Descriptors",
  4043. RRDF_FIELD_TYPE_BAR_WITH_INTEGER, RRDF_FIELD_VISUAL_BAR,
  4044. RRDF_FIELD_TRANSFORM_NUMBER, 0, "fds", OtherFDs_max, RRDF_FIELD_SORT_DESCENDING,
  4045. NULL, RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  4046. RRDF_FIELD_OPTS_NONE, NULL);
  4047. // processes, threads, uptime
  4048. buffer_rrdf_table_add_field(wb, field_id++, "Processes", "Processes", RRDF_FIELD_TYPE_BAR_WITH_INTEGER,
  4049. RRDF_FIELD_VISUAL_BAR, RRDF_FIELD_TRANSFORM_NUMBER, 0,
  4050. "processes", Processes_max, RRDF_FIELD_SORT_DESCENDING, NULL,
  4051. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  4052. RRDF_FIELD_OPTS_NONE, NULL);
  4053. buffer_rrdf_table_add_field(wb, field_id++, "Threads", "Threads", RRDF_FIELD_TYPE_BAR_WITH_INTEGER,
  4054. RRDF_FIELD_VISUAL_BAR, RRDF_FIELD_TRANSFORM_NUMBER, 0,
  4055. "threads", Threads_max, RRDF_FIELD_SORT_DESCENDING, NULL, RRDF_FIELD_SUMMARY_SUM,
  4056. RRDF_FIELD_FILTER_RANGE,
  4057. RRDF_FIELD_OPTS_NONE, NULL);
  4058. buffer_rrdf_table_add_field(wb, field_id++, "Uptime", "Uptime in seconds", RRDF_FIELD_TYPE_DURATION,
  4059. RRDF_FIELD_VISUAL_BAR, RRDF_FIELD_TRANSFORM_DURATION_S, 2,
  4060. "seconds", Uptime_max, RRDF_FIELD_SORT_DESCENDING, NULL, RRDF_FIELD_SUMMARY_MAX,
  4061. RRDF_FIELD_FILTER_RANGE,
  4062. RRDF_FIELD_OPTS_VISIBLE, NULL);
  4063. }
  4064. buffer_json_object_close(wb); // columns
  4065. buffer_json_member_add_string(wb, "default_sort_column", "CPU");
  4066. buffer_json_member_add_object(wb, "charts");
  4067. {
  4068. // CPU chart
  4069. buffer_json_member_add_object(wb, "CPU");
  4070. {
  4071. buffer_json_member_add_string(wb, "name", "CPU Utilization");
  4072. buffer_json_member_add_string(wb, "type", "stacked-bar");
  4073. buffer_json_member_add_array(wb, "columns");
  4074. {
  4075. buffer_json_add_array_item_string(wb, "UserCPU");
  4076. buffer_json_add_array_item_string(wb, "SysCPU");
  4077. buffer_json_add_array_item_string(wb, "GuestCPU");
  4078. buffer_json_add_array_item_string(wb, "CUserCPU");
  4079. buffer_json_add_array_item_string(wb, "CSysCPU");
  4080. buffer_json_add_array_item_string(wb, "CGuestCPU");
  4081. }
  4082. buffer_json_array_close(wb);
  4083. }
  4084. buffer_json_object_close(wb);
  4085. buffer_json_member_add_object(wb, "CPUCtxSwitches");
  4086. {
  4087. buffer_json_member_add_string(wb, "name", "CPU Context Switches");
  4088. buffer_json_member_add_string(wb, "type", "stacked-bar");
  4089. buffer_json_member_add_array(wb, "columns");
  4090. {
  4091. buffer_json_add_array_item_string(wb, "vCtxSwitch");
  4092. buffer_json_add_array_item_string(wb, "iCtxSwitch");
  4093. }
  4094. buffer_json_array_close(wb);
  4095. }
  4096. buffer_json_object_close(wb);
  4097. // Memory chart
  4098. buffer_json_member_add_object(wb, "Memory");
  4099. {
  4100. buffer_json_member_add_string(wb, "name", "Memory");
  4101. buffer_json_member_add_string(wb, "type", "stacked-bar");
  4102. buffer_json_member_add_array(wb, "columns");
  4103. {
  4104. buffer_json_add_array_item_string(wb, "Virtual");
  4105. buffer_json_add_array_item_string(wb, "Resident");
  4106. buffer_json_add_array_item_string(wb, "Shared");
  4107. buffer_json_add_array_item_string(wb, "Swap");
  4108. }
  4109. buffer_json_array_close(wb);
  4110. }
  4111. buffer_json_object_close(wb);
  4112. if(MemTotal) {
  4113. // Memory chart
  4114. buffer_json_member_add_object(wb, "MemoryPercent");
  4115. {
  4116. buffer_json_member_add_string(wb, "name", "Memory Percentage");
  4117. buffer_json_member_add_string(wb, "type", "stacked-bar");
  4118. buffer_json_member_add_array(wb, "columns");
  4119. {
  4120. buffer_json_add_array_item_string(wb, "Memory");
  4121. }
  4122. buffer_json_array_close(wb);
  4123. }
  4124. buffer_json_object_close(wb);
  4125. }
  4126. #ifndef __FreeBSD__
  4127. // I/O Reads chart
  4128. buffer_json_member_add_object(wb, "Reads");
  4129. {
  4130. buffer_json_member_add_string(wb, "name", "I/O Reads");
  4131. buffer_json_member_add_string(wb, "type", "stacked-bar");
  4132. buffer_json_member_add_array(wb, "columns");
  4133. {
  4134. buffer_json_add_array_item_string(wb, "LReads");
  4135. buffer_json_add_array_item_string(wb, "PReads");
  4136. }
  4137. buffer_json_array_close(wb);
  4138. }
  4139. buffer_json_object_close(wb);
  4140. // I/O Writes chart
  4141. buffer_json_member_add_object(wb, "Writes");
  4142. {
  4143. buffer_json_member_add_string(wb, "name", "I/O Writes");
  4144. buffer_json_member_add_string(wb, "type", "stacked-bar");
  4145. buffer_json_member_add_array(wb, "columns");
  4146. {
  4147. buffer_json_add_array_item_string(wb, "LWrites");
  4148. buffer_json_add_array_item_string(wb, "PWrites");
  4149. }
  4150. buffer_json_array_close(wb);
  4151. }
  4152. buffer_json_object_close(wb);
  4153. // Logical I/O chart
  4154. buffer_json_member_add_object(wb, "LogicalIO");
  4155. {
  4156. buffer_json_member_add_string(wb, "name", "Logical I/O");
  4157. buffer_json_member_add_string(wb, "type", "stacked-bar");
  4158. buffer_json_member_add_array(wb, "columns");
  4159. {
  4160. buffer_json_add_array_item_string(wb, "LReads");
  4161. buffer_json_add_array_item_string(wb, "LWrites");
  4162. }
  4163. buffer_json_array_close(wb);
  4164. }
  4165. buffer_json_object_close(wb);
  4166. #endif
  4167. // Physical I/O chart
  4168. buffer_json_member_add_object(wb, "PhysicalIO");
  4169. {
  4170. buffer_json_member_add_string(wb, "name", "Physical I/O");
  4171. buffer_json_member_add_string(wb, "type", "stacked-bar");
  4172. buffer_json_member_add_array(wb, "columns");
  4173. {
  4174. buffer_json_add_array_item_string(wb, "PReads");
  4175. buffer_json_add_array_item_string(wb, "PWrites");
  4176. }
  4177. buffer_json_array_close(wb);
  4178. }
  4179. buffer_json_object_close(wb);
  4180. // I/O Calls chart
  4181. buffer_json_member_add_object(wb, "IOCalls");
  4182. {
  4183. buffer_json_member_add_string(wb, "name", "I/O Calls");
  4184. buffer_json_member_add_string(wb, "type", "stacked-bar");
  4185. buffer_json_member_add_array(wb, "columns");
  4186. {
  4187. buffer_json_add_array_item_string(wb, "RCalls");
  4188. buffer_json_add_array_item_string(wb, "WCalls");
  4189. }
  4190. buffer_json_array_close(wb);
  4191. }
  4192. buffer_json_object_close(wb);
  4193. // Minor Page Faults chart
  4194. buffer_json_member_add_object(wb, "MinFlt");
  4195. {
  4196. buffer_json_member_add_string(wb, "name", "Minor Page Faults");
  4197. buffer_json_member_add_string(wb, "type", "stacked-bar");
  4198. buffer_json_member_add_array(wb, "columns");
  4199. {
  4200. buffer_json_add_array_item_string(wb, "MinFlt");
  4201. buffer_json_add_array_item_string(wb, "CMinFlt");
  4202. }
  4203. buffer_json_array_close(wb);
  4204. }
  4205. buffer_json_object_close(wb);
  4206. // Major Page Faults chart
  4207. buffer_json_member_add_object(wb, "MajFlt");
  4208. {
  4209. buffer_json_member_add_string(wb, "name", "Major Page Faults");
  4210. buffer_json_member_add_string(wb, "type", "stacked-bar");
  4211. buffer_json_member_add_array(wb, "columns");
  4212. {
  4213. buffer_json_add_array_item_string(wb, "MajFlt");
  4214. buffer_json_add_array_item_string(wb, "CMajFlt");
  4215. }
  4216. buffer_json_array_close(wb);
  4217. }
  4218. buffer_json_object_close(wb);
  4219. // Threads chart
  4220. buffer_json_member_add_object(wb, "Threads");
  4221. {
  4222. buffer_json_member_add_string(wb, "name", "Threads");
  4223. buffer_json_member_add_string(wb, "type", "stacked-bar");
  4224. buffer_json_member_add_array(wb, "columns");
  4225. {
  4226. buffer_json_add_array_item_string(wb, "Threads");
  4227. }
  4228. buffer_json_array_close(wb);
  4229. }
  4230. buffer_json_object_close(wb);
  4231. // Processes chart
  4232. buffer_json_member_add_object(wb, "Processes");
  4233. {
  4234. buffer_json_member_add_string(wb, "name", "Processes");
  4235. buffer_json_member_add_string(wb, "type", "stacked-bar");
  4236. buffer_json_member_add_array(wb, "columns");
  4237. {
  4238. buffer_json_add_array_item_string(wb, "Processes");
  4239. }
  4240. buffer_json_array_close(wb);
  4241. }
  4242. buffer_json_object_close(wb);
  4243. // FDs chart
  4244. buffer_json_member_add_object(wb, "FDs");
  4245. {
  4246. buffer_json_member_add_string(wb, "name", "File Descriptors");
  4247. buffer_json_member_add_string(wb, "type", "stacked-bar");
  4248. buffer_json_member_add_array(wb, "columns");
  4249. {
  4250. buffer_json_add_array_item_string(wb, "Files");
  4251. buffer_json_add_array_item_string(wb, "Pipes");
  4252. buffer_json_add_array_item_string(wb, "Sockets");
  4253. buffer_json_add_array_item_string(wb, "iNotiFDs");
  4254. buffer_json_add_array_item_string(wb, "EventFDs");
  4255. buffer_json_add_array_item_string(wb, "TimerFDs");
  4256. buffer_json_add_array_item_string(wb, "SigFDs");
  4257. buffer_json_add_array_item_string(wb, "EvPollFDs");
  4258. buffer_json_add_array_item_string(wb, "OtherFDs");
  4259. }
  4260. buffer_json_array_close(wb);
  4261. }
  4262. buffer_json_object_close(wb);
  4263. }
  4264. buffer_json_object_close(wb); // charts
  4265. buffer_json_member_add_array(wb, "default_charts");
  4266. {
  4267. buffer_json_add_array_item_array(wb);
  4268. buffer_json_add_array_item_string(wb, "CPU");
  4269. buffer_json_add_array_item_string(wb, "Category");
  4270. buffer_json_array_close(wb);
  4271. buffer_json_add_array_item_array(wb);
  4272. buffer_json_add_array_item_string(wb, "Memory");
  4273. buffer_json_add_array_item_string(wb, "Category");
  4274. buffer_json_array_close(wb);
  4275. }
  4276. buffer_json_array_close(wb);
  4277. buffer_json_member_add_object(wb, "group_by");
  4278. {
  4279. // group by PID
  4280. buffer_json_member_add_object(wb, "PID");
  4281. {
  4282. buffer_json_member_add_string(wb, "name", "Process Tree by PID");
  4283. buffer_json_member_add_array(wb, "columns");
  4284. {
  4285. buffer_json_add_array_item_string(wb, "PPID");
  4286. }
  4287. buffer_json_array_close(wb);
  4288. }
  4289. buffer_json_object_close(wb);
  4290. // group by Category
  4291. buffer_json_member_add_object(wb, "Category");
  4292. {
  4293. buffer_json_member_add_string(wb, "name", "Process Tree by Category");
  4294. buffer_json_member_add_array(wb, "columns");
  4295. {
  4296. buffer_json_add_array_item_string(wb, "Category");
  4297. buffer_json_add_array_item_string(wb, "PPID");
  4298. }
  4299. buffer_json_array_close(wb);
  4300. }
  4301. buffer_json_object_close(wb);
  4302. // group by User
  4303. buffer_json_member_add_object(wb, "User");
  4304. {
  4305. buffer_json_member_add_string(wb, "name", "Process Tree by User");
  4306. buffer_json_member_add_array(wb, "columns");
  4307. {
  4308. buffer_json_add_array_item_string(wb, "User");
  4309. buffer_json_add_array_item_string(wb, "PPID");
  4310. }
  4311. buffer_json_array_close(wb);
  4312. }
  4313. buffer_json_object_close(wb);
  4314. // group by Group
  4315. buffer_json_member_add_object(wb, "Group");
  4316. {
  4317. buffer_json_member_add_string(wb, "name", "Process Tree by Group");
  4318. buffer_json_member_add_array(wb, "columns");
  4319. {
  4320. buffer_json_add_array_item_string(wb, "Group");
  4321. buffer_json_add_array_item_string(wb, "PPID");
  4322. }
  4323. buffer_json_array_close(wb);
  4324. }
  4325. buffer_json_object_close(wb);
  4326. }
  4327. buffer_json_object_close(wb); // group_by
  4328. buffer_json_member_add_time_t(wb, "expires", expires);
  4329. buffer_json_finalize(wb);
  4330. pluginsd_function_result_to_stdout(transaction, HTTP_RESP_OK, "application/json", expires, wb);
  4331. buffer_free(wb);
  4332. }
  4333. static bool apps_plugin_exit = false;
  4334. int main(int argc, char **argv) {
  4335. // debug_flags = D_PROCFILE;
  4336. stderror = stderr;
  4337. clocks_init();
  4338. pagesize = (size_t)sysconf(_SC_PAGESIZE);
  4339. // set the name for logging
  4340. program_name = "apps.plugin";
  4341. // disable syslog for apps.plugin
  4342. error_log_syslog = 0;
  4343. // set errors flood protection to 100 logs per hour
  4344. error_log_errors_per_period = 100;
  4345. error_log_throttle_period = 3600;
  4346. log_set_global_severity_for_external_plugins();
  4347. bool send_resource_usage = true;
  4348. {
  4349. const char *s = getenv("NETDATA_INTERNALS_MONITORING");
  4350. if(s && *s && strcmp(s, "NO") == 0)
  4351. send_resource_usage = false;
  4352. }
  4353. // since apps.plugin runs as root, prevent it from opening symbolic links
  4354. procfile_open_flags = O_RDONLY|O_NOFOLLOW;
  4355. netdata_configured_host_prefix = getenv("NETDATA_HOST_PREFIX");
  4356. if(verify_netdata_host_prefix() == -1) exit(1);
  4357. user_config_dir = getenv("NETDATA_USER_CONFIG_DIR");
  4358. if(user_config_dir == NULL) {
  4359. // netdata_log_info("NETDATA_CONFIG_DIR is not passed from netdata");
  4360. user_config_dir = CONFIG_DIR;
  4361. }
  4362. // else netdata_log_info("Found NETDATA_USER_CONFIG_DIR='%s'", user_config_dir);
  4363. stock_config_dir = getenv("NETDATA_STOCK_CONFIG_DIR");
  4364. if(stock_config_dir == NULL) {
  4365. // netdata_log_info("NETDATA_CONFIG_DIR is not passed from netdata");
  4366. stock_config_dir = LIBCONFIG_DIR;
  4367. }
  4368. // else netdata_log_info("Found NETDATA_USER_CONFIG_DIR='%s'", user_config_dir);
  4369. #ifdef NETDATA_INTERNAL_CHECKS
  4370. if(debug_flags != 0) {
  4371. struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
  4372. if(setrlimit(RLIMIT_CORE, &rl) != 0)
  4373. netdata_log_info("Cannot request unlimited core dumps for debugging... Proceeding anyway...");
  4374. #ifdef HAVE_SYS_PRCTL_H
  4375. prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
  4376. #endif
  4377. }
  4378. #endif /* NETDATA_INTERNAL_CHECKS */
  4379. procfile_adaptive_initial_allocation = 1;
  4380. get_system_HZ();
  4381. #ifdef __FreeBSD__
  4382. time_factor = 1000000ULL / RATES_DETAIL; // FreeBSD uses usecs
  4383. #else
  4384. time_factor = system_hz; // Linux uses clock ticks
  4385. #endif
  4386. get_system_pid_max();
  4387. get_system_cpus_uncached();
  4388. parse_args(argc, argv);
  4389. if(!check_capabilities() && !am_i_running_as_root() && !check_proc_1_io()) {
  4390. uid_t uid = getuid(), euid = geteuid();
  4391. #ifdef HAVE_CAPABILITY
  4392. netdata_log_error("apps.plugin should either run as root (now running with uid %u, euid %u) or have special capabilities. "
  4393. "Without these, apps.plugin cannot report disk I/O utilization of other processes. "
  4394. "To enable capabilities run: sudo setcap cap_dac_read_search,cap_sys_ptrace+ep %s; "
  4395. "To enable setuid to root run: sudo chown root:netdata %s; sudo chmod 4750 %s; "
  4396. , uid, euid, argv[0], argv[0], argv[0]
  4397. );
  4398. #else
  4399. netdata_log_error("apps.plugin should either run as root (now running with uid %u, euid %u) or have special capabilities. "
  4400. "Without these, apps.plugin cannot report disk I/O utilization of other processes. "
  4401. "Your system does not support capabilities. "
  4402. "To enable setuid to root run: sudo chown root:netdata %s; sudo chmod 4750 %s; "
  4403. , uid, euid, argv[0], argv[0]
  4404. );
  4405. #endif
  4406. }
  4407. netdata_log_info("started on pid %d", getpid());
  4408. snprintfz(all_user_ids.filename, FILENAME_MAX, "%s/etc/passwd", netdata_configured_host_prefix);
  4409. debug_log("passwd file: '%s'", all_user_ids.filename);
  4410. snprintfz(all_group_ids.filename, FILENAME_MAX, "%s/etc/group", netdata_configured_host_prefix);
  4411. debug_log("group file: '%s'", all_group_ids.filename);
  4412. #if (ALL_PIDS_ARE_READ_INSTANTLY == 0)
  4413. all_pids_sortlist = callocz(sizeof(pid_t), (size_t)pid_max + 1);
  4414. #endif
  4415. all_pids = callocz(sizeof(struct pid_stat *), (size_t) pid_max + 1);
  4416. // ------------------------------------------------------------------------
  4417. // the event loop for functions
  4418. struct functions_evloop_globals *wg =
  4419. functions_evloop_init(1, "APPS", &apps_and_stdout_mutex, &apps_plugin_exit);
  4420. functions_evloop_add_function(wg, "processes", function_processes, PLUGINS_FUNCTIONS_TIMEOUT_DEFAULT);
  4421. // ------------------------------------------------------------------------
  4422. netdata_mutex_lock(&apps_and_stdout_mutex);
  4423. APPS_PLUGIN_GLOBAL_FUNCTIONS();
  4424. usec_t step = update_every * USEC_PER_SEC;
  4425. global_iterations_counter = 1;
  4426. heartbeat_t hb;
  4427. heartbeat_init(&hb);
  4428. for(; !apps_plugin_exit ; global_iterations_counter++) {
  4429. netdata_mutex_unlock(&apps_and_stdout_mutex);
  4430. #ifdef NETDATA_PROFILING
  4431. #warning "compiling for profiling"
  4432. static int profiling_count=0;
  4433. profiling_count++;
  4434. if(unlikely(profiling_count > 2000)) exit(0);
  4435. usec_t dt = update_every * USEC_PER_SEC;
  4436. #else
  4437. usec_t dt = heartbeat_next(&hb, step);
  4438. #endif
  4439. netdata_mutex_lock(&apps_and_stdout_mutex);
  4440. struct pollfd pollfd = { .fd = fileno(stdout), .events = POLLERR };
  4441. if (unlikely(poll(&pollfd, 1, 0) < 0)) {
  4442. netdata_mutex_unlock(&apps_and_stdout_mutex);
  4443. fatal("Cannot check if a pipe is available");
  4444. }
  4445. if (unlikely(pollfd.revents & POLLERR)) {
  4446. netdata_mutex_unlock(&apps_and_stdout_mutex);
  4447. fatal("Received error on read pipe.");
  4448. }
  4449. if(global_iterations_counter % 10 == 0)
  4450. get_MemTotal();
  4451. if(!collect_data_for_all_processes()) {
  4452. netdata_log_error("Cannot collect /proc data for running processes. Disabling apps.plugin...");
  4453. printf("DISABLE\n");
  4454. netdata_mutex_unlock(&apps_and_stdout_mutex);
  4455. exit(1);
  4456. }
  4457. calculate_netdata_statistics();
  4458. normalize_utilization(apps_groups_root_target);
  4459. if(send_resource_usage)
  4460. send_resource_usage_to_netdata(dt);
  4461. #ifndef __FreeBSD__
  4462. send_proc_states_count(dt);
  4463. #endif
  4464. send_charts_updates_to_netdata(apps_groups_root_target, "app", "app_group", "Apps");
  4465. send_collected_data_to_netdata(apps_groups_root_target, "app", dt);
  4466. if (enable_users_charts) {
  4467. send_charts_updates_to_netdata(users_root_target, "user", "user", "Users");
  4468. send_collected_data_to_netdata(users_root_target, "user", dt);
  4469. }
  4470. if (enable_groups_charts) {
  4471. send_charts_updates_to_netdata(groups_root_target, "usergroup", "user_group", "User Groups");
  4472. send_collected_data_to_netdata(groups_root_target, "usergroup", dt);
  4473. }
  4474. fflush(stdout);
  4475. show_guest_time_old = show_guest_time;
  4476. debug_log("done Loop No %zu", global_iterations_counter);
  4477. }
  4478. netdata_mutex_unlock(&apps_and_stdout_mutex);
  4479. }