apps_plugin.c 207 KB

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