apps_plugin.c 132 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. /*
  3. * netdata apps.plugin
  4. * (C) Copyright 2016-2017 Costa Tsaousis <costa@tsaousis.gr>
  5. * Released under GPL v3+
  6. */
  7. #include "../../libnetdata/libnetdata.h"
  8. // ----------------------------------------------------------------------------
  9. // callback required by fatal()
  10. void netdata_cleanup_and_exit(int ret) {
  11. exit(ret);
  12. }
  13. // callbacks required by popen()
  14. void signals_block(void) {};
  15. void signals_unblock(void) {};
  16. void signals_reset(void) {};
  17. // callback required by eval()
  18. int health_variable_lookup(const char *variable, uint32_t hash, struct rrdcalc *rc, calculated_number *result) {
  19. (void)variable;
  20. (void)hash;
  21. (void)rc;
  22. (void)result;
  23. return 0;
  24. };
  25. // required by get_system_cpus()
  26. char *netdata_configured_host_prefix = "";
  27. // ----------------------------------------------------------------------------
  28. // debugging
  29. static int debug_enabled = 0;
  30. static inline void debug_log_int(const char *fmt, ... ) {
  31. va_list args;
  32. fprintf( stderr, "apps.plugin: ");
  33. va_start( args, fmt );
  34. vfprintf( stderr, fmt, args );
  35. va_end( args );
  36. fputc('\n', stderr);
  37. }
  38. #ifdef NETDATA_INTERNAL_CHECKS
  39. #define debug_log(fmt, args...) do { if(unlikely(debug_enabled)) debug_log_int(fmt, ##args); } while(0)
  40. #else
  41. static inline void debug_log_dummy(void) {}
  42. #define debug_log(fmt, args...) debug_log_dummy()
  43. #endif
  44. // ----------------------------------------------------------------------------
  45. #ifdef __FreeBSD__
  46. #include <sys/user.h>
  47. #endif
  48. // ----------------------------------------------------------------------------
  49. // per O/S configuration
  50. // the minimum PID of the system
  51. // this is also the pid of the init process
  52. #define INIT_PID 1
  53. // if the way apps.plugin will work, will read the entire process list,
  54. // including the resource utilization of each process, instantly
  55. // set this to 1
  56. // when set to 0, apps.plugin builds a sort list of processes, in order
  57. // to process children processes, before parent processes
  58. #ifdef __FreeBSD__
  59. #define ALL_PIDS_ARE_READ_INSTANTLY 1
  60. #else
  61. #define ALL_PIDS_ARE_READ_INSTANTLY 0
  62. #endif
  63. // ----------------------------------------------------------------------------
  64. // string lengths
  65. #define MAX_COMPARE_NAME 100
  66. #define MAX_NAME 100
  67. #define MAX_CMDLINE 16384
  68. // ----------------------------------------------------------------------------
  69. // the rates we are going to send to netdata will have this detail a value of:
  70. // - 1 will send just integer parts to netdata
  71. // - 100 will send 2 decimal points
  72. // - 1000 will send 3 decimal points
  73. // etc.
  74. #define RATES_DETAIL 10000ULL
  75. // ----------------------------------------------------------------------------
  76. // to avoid reallocating too frequently, we can increase the number of spare
  77. // file descriptors used by processes.
  78. // IMPORTANT:
  79. // having a lot of spares, increases the CPU utilization of the plugin.
  80. #define MAX_SPARE_FDS 1
  81. // ----------------------------------------------------------------------------
  82. // command line options
  83. static int
  84. update_every = 1,
  85. enable_guest_charts = 0,
  86. #ifdef __FreeBSD__
  87. enable_file_charts = 0,
  88. #else
  89. enable_file_charts = 1,
  90. max_fds_cache_seconds = 60,
  91. #endif
  92. enable_users_charts = 1,
  93. enable_groups_charts = 1,
  94. include_exited_childs = 1;
  95. // will be changed to getenv(NETDATA_USER_CONFIG_DIR) if it exists
  96. static char *user_config_dir = CONFIG_DIR;
  97. static char *stock_config_dir = LIBCONFIG_DIR;
  98. // ----------------------------------------------------------------------------
  99. // internal flags
  100. // handled in code (automatically set)
  101. static int
  102. show_guest_time = 0, // 1 when guest values are collected
  103. show_guest_time_old = 0,
  104. proc_pid_cmdline_is_needed = 0; // 1 when we need to read /proc/cmdline
  105. // ----------------------------------------------------------------------------
  106. // internal counters
  107. static size_t
  108. global_iterations_counter = 1,
  109. calls_counter = 0,
  110. file_counter = 0,
  111. filenames_allocated_counter = 0,
  112. inodes_changed_counter = 0,
  113. links_changed_counter = 0,
  114. targets_assignment_counter = 0;
  115. // ----------------------------------------------------------------------------
  116. // Normalization
  117. //
  118. // With normalization we lower the collected metrics by a factor to make them
  119. // match the total utilization of the system.
  120. // The discrepancy exists because apps.plugin needs some time to collect all
  121. // the metrics. This results in utilization that exceeds the total utilization
  122. // of the system.
  123. //
  124. // With normalization we align the per-process utilization, to the total of
  125. // the system. We first consume the exited children utilization and it the
  126. // collected values is above the total, we proportionally scale each reported
  127. // metric.
  128. // the total system time, as reported by /proc/stat
  129. #if (ALL_PIDS_ARE_READ_INSTANTLY == 0)
  130. static kernel_uint_t
  131. global_utime = 0,
  132. global_stime = 0,
  133. global_gtime = 0;
  134. #endif
  135. // the normalization ratios, as calculated by normalize_utilization()
  136. double utime_fix_ratio = 1.0,
  137. stime_fix_ratio = 1.0,
  138. gtime_fix_ratio = 1.0,
  139. minflt_fix_ratio = 1.0,
  140. majflt_fix_ratio = 1.0,
  141. cutime_fix_ratio = 1.0,
  142. cstime_fix_ratio = 1.0,
  143. cgtime_fix_ratio = 1.0,
  144. cminflt_fix_ratio = 1.0,
  145. cmajflt_fix_ratio = 1.0;
  146. // ----------------------------------------------------------------------------
  147. // target
  148. //
  149. // target is the structure that processes are aggregated to be reported
  150. // to netdata.
  151. //
  152. // - Each entry in /etc/apps_groups.conf creates a target.
  153. // - Each user and group used by a process in the system, creates a target.
  154. struct target {
  155. char compare[MAX_COMPARE_NAME + 1];
  156. uint32_t comparehash;
  157. size_t comparelen;
  158. char id[MAX_NAME + 1];
  159. uint32_t idhash;
  160. char name[MAX_NAME + 1];
  161. uid_t uid;
  162. gid_t gid;
  163. kernel_uint_t minflt;
  164. kernel_uint_t cminflt;
  165. kernel_uint_t majflt;
  166. kernel_uint_t cmajflt;
  167. kernel_uint_t utime;
  168. kernel_uint_t stime;
  169. kernel_uint_t gtime;
  170. kernel_uint_t cutime;
  171. kernel_uint_t cstime;
  172. kernel_uint_t cgtime;
  173. kernel_uint_t num_threads;
  174. // kernel_uint_t rss;
  175. kernel_uint_t status_vmsize;
  176. kernel_uint_t status_vmrss;
  177. kernel_uint_t status_vmshared;
  178. kernel_uint_t status_rssfile;
  179. kernel_uint_t status_rssshmem;
  180. kernel_uint_t status_vmswap;
  181. kernel_uint_t io_logical_bytes_read;
  182. kernel_uint_t io_logical_bytes_written;
  183. // kernel_uint_t io_read_calls;
  184. // kernel_uint_t io_write_calls;
  185. kernel_uint_t io_storage_bytes_read;
  186. kernel_uint_t io_storage_bytes_written;
  187. // kernel_uint_t io_cancelled_write_bytes;
  188. int *target_fds;
  189. int target_fds_size;
  190. kernel_uint_t openfiles;
  191. kernel_uint_t openpipes;
  192. kernel_uint_t opensockets;
  193. kernel_uint_t openinotifies;
  194. kernel_uint_t openeventfds;
  195. kernel_uint_t opentimerfds;
  196. kernel_uint_t opensignalfds;
  197. kernel_uint_t openeventpolls;
  198. kernel_uint_t openother;
  199. unsigned int processes; // how many processes have been merged to this
  200. int exposed; // if set, we have sent this to netdata
  201. int hidden; // if set, we set the hidden flag on the dimension
  202. int debug_enabled;
  203. int ends_with;
  204. int starts_with; // if set, the compare string matches only the
  205. // beginning of the command
  206. struct target *target; // the one that will be reported to netdata
  207. struct target *next;
  208. };
  209. struct target
  210. *apps_groups_default_target = NULL, // the default target
  211. *apps_groups_root_target = NULL, // apps_groups.conf defined
  212. *users_root_target = NULL, // users
  213. *groups_root_target = NULL; // user groups
  214. size_t
  215. apps_groups_targets_count = 0; // # of apps_groups.conf targets
  216. // ----------------------------------------------------------------------------
  217. // pid_stat
  218. //
  219. // structure to store data for each process running
  220. // see: man proc for the description of the fields
  221. struct pid_fd {
  222. int fd;
  223. #ifndef __FreeBSD__
  224. ino_t inode;
  225. char *filename;
  226. uint32_t link_hash;
  227. size_t cache_iterations_counter;
  228. size_t cache_iterations_reset;
  229. #endif
  230. };
  231. struct pid_stat {
  232. int32_t pid;
  233. char comm[MAX_COMPARE_NAME + 1];
  234. char *cmdline;
  235. uint32_t log_thrown;
  236. // char state;
  237. int32_t ppid;
  238. // int32_t pgrp;
  239. // int32_t session;
  240. // int32_t tty_nr;
  241. // int32_t tpgid;
  242. // uint64_t flags;
  243. // these are raw values collected
  244. kernel_uint_t minflt_raw;
  245. kernel_uint_t cminflt_raw;
  246. kernel_uint_t majflt_raw;
  247. kernel_uint_t cmajflt_raw;
  248. kernel_uint_t utime_raw;
  249. kernel_uint_t stime_raw;
  250. kernel_uint_t gtime_raw; // guest_time
  251. kernel_uint_t cutime_raw;
  252. kernel_uint_t cstime_raw;
  253. kernel_uint_t cgtime_raw; // cguest_time
  254. // these are rates
  255. kernel_uint_t minflt;
  256. kernel_uint_t cminflt;
  257. kernel_uint_t majflt;
  258. kernel_uint_t cmajflt;
  259. kernel_uint_t utime;
  260. kernel_uint_t stime;
  261. kernel_uint_t gtime;
  262. kernel_uint_t cutime;
  263. kernel_uint_t cstime;
  264. kernel_uint_t cgtime;
  265. // int64_t priority;
  266. // int64_t nice;
  267. int32_t num_threads;
  268. // int64_t itrealvalue;
  269. // kernel_uint_t starttime;
  270. // kernel_uint_t vsize;
  271. // kernel_uint_t rss;
  272. // kernel_uint_t rsslim;
  273. // kernel_uint_t starcode;
  274. // kernel_uint_t endcode;
  275. // kernel_uint_t startstack;
  276. // kernel_uint_t kstkesp;
  277. // kernel_uint_t kstkeip;
  278. // uint64_t signal;
  279. // uint64_t blocked;
  280. // uint64_t sigignore;
  281. // uint64_t sigcatch;
  282. // uint64_t wchan;
  283. // uint64_t nswap;
  284. // uint64_t cnswap;
  285. // int32_t exit_signal;
  286. // int32_t processor;
  287. // uint32_t rt_priority;
  288. // uint32_t policy;
  289. // kernel_uint_t delayacct_blkio_ticks;
  290. uid_t uid;
  291. gid_t gid;
  292. kernel_uint_t status_vmsize;
  293. kernel_uint_t status_vmrss;
  294. kernel_uint_t status_vmshared;
  295. kernel_uint_t status_rssfile;
  296. kernel_uint_t status_rssshmem;
  297. kernel_uint_t status_vmswap;
  298. #ifndef __FreeBSD__
  299. ARL_BASE *status_arl;
  300. #endif
  301. kernel_uint_t io_logical_bytes_read_raw;
  302. kernel_uint_t io_logical_bytes_written_raw;
  303. // kernel_uint_t io_read_calls_raw;
  304. // kernel_uint_t io_write_calls_raw;
  305. kernel_uint_t io_storage_bytes_read_raw;
  306. kernel_uint_t io_storage_bytes_written_raw;
  307. // kernel_uint_t io_cancelled_write_bytes_raw;
  308. kernel_uint_t io_logical_bytes_read;
  309. kernel_uint_t io_logical_bytes_written;
  310. // kernel_uint_t io_read_calls;
  311. // kernel_uint_t io_write_calls;
  312. kernel_uint_t io_storage_bytes_read;
  313. kernel_uint_t io_storage_bytes_written;
  314. // kernel_uint_t io_cancelled_write_bytes;
  315. struct pid_fd *fds; // array of fds it uses
  316. size_t fds_size; // the size of the fds array
  317. int children_count; // number of processes directly referencing this
  318. unsigned char keep:1; // 1 when we need to keep this process in memory even after it exited
  319. int keeploops; // increases by 1 every time keep is 1 and updated 0
  320. unsigned char updated:1; // 1 when the process is currently running
  321. unsigned char merged:1; // 1 when it has been merged to its parent
  322. unsigned char read:1; // 1 when we have already read this process for this iteration
  323. int sortlist; // higher numbers = top on the process tree
  324. // each process gets a unique number
  325. struct target *target; // app_groups.conf targets
  326. struct target *user_target; // uid based targets
  327. struct target *group_target; // gid based targets
  328. usec_t stat_collected_usec;
  329. usec_t last_stat_collected_usec;
  330. usec_t io_collected_usec;
  331. usec_t last_io_collected_usec;
  332. char *fds_dirname; // the full directory name in /proc/PID/fd
  333. char *stat_filename;
  334. char *status_filename;
  335. char *io_filename;
  336. char *cmdline_filename;
  337. struct pid_stat *parent;
  338. struct pid_stat *prev;
  339. struct pid_stat *next;
  340. };
  341. size_t pagesize;
  342. // log each problem once per process
  343. // log flood protection flags (log_thrown)
  344. #define PID_LOG_IO 0x00000001
  345. #define PID_LOG_STATUS 0x00000002
  346. #define PID_LOG_CMDLINE 0x00000004
  347. #define PID_LOG_FDS 0x00000008
  348. #define PID_LOG_STAT 0x00000010
  349. static struct pid_stat
  350. *root_of_pids = NULL, // global list of all processes running
  351. **all_pids = NULL; // to avoid allocations, we pre-allocate the
  352. // the entire pid space.
  353. static size_t
  354. all_pids_count = 0; // the number of processes running
  355. #if (ALL_PIDS_ARE_READ_INSTANTLY == 0)
  356. // Another pre-allocated list of all possible pids.
  357. // We need it to pids and assign them a unique sortlist id, so that we
  358. // read parents before children. This is needed to prevent a situation where
  359. // a child is found running, but until we read its parent, it has exited and
  360. // its parent has accumulated its resources.
  361. static pid_t
  362. *all_pids_sortlist = NULL;
  363. #endif
  364. // ----------------------------------------------------------------------------
  365. // file descriptor
  366. //
  367. // this is used to keep a global list of all open files of the system.
  368. // it is needed in order to calculate the unique files processes have open.
  369. #define FILE_DESCRIPTORS_INCREASE_STEP 100
  370. // types for struct file_descriptor->type
  371. typedef enum fd_filetype {
  372. FILETYPE_OTHER,
  373. FILETYPE_FILE,
  374. FILETYPE_PIPE,
  375. FILETYPE_SOCKET,
  376. FILETYPE_INOTIFY,
  377. FILETYPE_EVENTFD,
  378. FILETYPE_EVENTPOLL,
  379. FILETYPE_TIMERFD,
  380. FILETYPE_SIGNALFD
  381. } FD_FILETYPE;
  382. struct file_descriptor {
  383. avl avl;
  384. #ifdef NETDATA_INTERNAL_CHECKS
  385. uint32_t magic;
  386. #endif /* NETDATA_INTERNAL_CHECKS */
  387. const char *name;
  388. uint32_t hash;
  389. FD_FILETYPE type;
  390. int count;
  391. int pos;
  392. } *all_files = NULL;
  393. static int
  394. all_files_len = 0,
  395. all_files_size = 0;
  396. // ----------------------------------------------------------------------------
  397. // apps_groups.conf
  398. // aggregate all processes in groups, to have a limited number of dimensions
  399. static struct target *get_users_target(uid_t uid) {
  400. struct target *w;
  401. for(w = users_root_target ; w ; w = w->next)
  402. if(w->uid == uid) return w;
  403. w = callocz(sizeof(struct target), 1);
  404. snprintfz(w->compare, MAX_COMPARE_NAME, "%u", uid);
  405. w->comparehash = simple_hash(w->compare);
  406. w->comparelen = strlen(w->compare);
  407. snprintfz(w->id, MAX_NAME, "%u", uid);
  408. w->idhash = simple_hash(w->id);
  409. struct passwd *pw = getpwuid(uid);
  410. if(!pw || !pw->pw_name || !*pw->pw_name)
  411. snprintfz(w->name, MAX_NAME, "%u", uid);
  412. else
  413. snprintfz(w->name, MAX_NAME, "%s", pw->pw_name);
  414. netdata_fix_chart_name(w->name);
  415. w->uid = uid;
  416. w->next = users_root_target;
  417. users_root_target = w;
  418. debug_log("added uid %u ('%s') target", w->uid, w->name);
  419. return w;
  420. }
  421. struct target *get_groups_target(gid_t gid)
  422. {
  423. struct target *w;
  424. for(w = groups_root_target ; w ; w = w->next)
  425. if(w->gid == gid) return w;
  426. w = callocz(sizeof(struct target), 1);
  427. snprintfz(w->compare, MAX_COMPARE_NAME, "%u", gid);
  428. w->comparehash = simple_hash(w->compare);
  429. w->comparelen = strlen(w->compare);
  430. snprintfz(w->id, MAX_NAME, "%u", gid);
  431. w->idhash = simple_hash(w->id);
  432. struct group *gr = getgrgid(gid);
  433. if(!gr || !gr->gr_name || !*gr->gr_name)
  434. snprintfz(w->name, MAX_NAME, "%u", gid);
  435. else
  436. snprintfz(w->name, MAX_NAME, "%s", gr->gr_name);
  437. netdata_fix_chart_name(w->name);
  438. w->gid = gid;
  439. w->next = groups_root_target;
  440. groups_root_target = w;
  441. debug_log("added gid %u ('%s') target", w->gid, w->name);
  442. return w;
  443. }
  444. // find or create a new target
  445. // there are targets that are just aggregated to other target (the second argument)
  446. static struct target *get_apps_groups_target(const char *id, struct target *target, const char *name) {
  447. int tdebug = 0, thidden = target?target->hidden:0, ends_with = 0;
  448. const char *nid = id;
  449. // extract the options
  450. while(nid[0] == '-' || nid[0] == '+' || nid[0] == '*') {
  451. if(nid[0] == '-') thidden = 1;
  452. if(nid[0] == '+') tdebug = 1;
  453. if(nid[0] == '*') ends_with = 1;
  454. nid++;
  455. }
  456. uint32_t hash = simple_hash(id);
  457. // find if it already exists
  458. struct target *w, *last = apps_groups_root_target;
  459. for(w = apps_groups_root_target ; w ; w = w->next) {
  460. if(w->idhash == hash && strncmp(nid, w->id, MAX_NAME) == 0)
  461. return w;
  462. last = w;
  463. }
  464. // find an existing target
  465. if(unlikely(!target)) {
  466. while(*name == '-') {
  467. if(*name == '-') thidden = 1;
  468. name++;
  469. }
  470. for(target = apps_groups_root_target ; target != NULL ; target = target->next) {
  471. if(!target->target && strcmp(name, target->name) == 0)
  472. break;
  473. }
  474. if(unlikely(debug_enabled)) {
  475. if(unlikely(target))
  476. debug_log("REUSING TARGET NAME '%s' on ID '%s'", target->name, target->id);
  477. else
  478. debug_log("NEW TARGET NAME '%s' on ID '%s'", name, id);
  479. }
  480. }
  481. if(target && target->target)
  482. fatal("Internal Error: request to link process '%s' to target '%s' which is linked to target '%s'", id, target->id, target->target->id);
  483. w = callocz(sizeof(struct target), 1);
  484. strncpyz(w->id, nid, MAX_NAME);
  485. w->idhash = simple_hash(w->id);
  486. if(unlikely(!target))
  487. // copy the name
  488. strncpyz(w->name, name, MAX_NAME);
  489. else
  490. // copy the id
  491. strncpyz(w->name, nid, MAX_NAME);
  492. strncpyz(w->compare, nid, MAX_COMPARE_NAME);
  493. size_t len = strlen(w->compare);
  494. if(w->compare[len - 1] == '*') {
  495. w->compare[len - 1] = '\0';
  496. w->starts_with = 1;
  497. }
  498. w->ends_with = ends_with;
  499. if(w->starts_with && w->ends_with)
  500. proc_pid_cmdline_is_needed = 1;
  501. w->comparehash = simple_hash(w->compare);
  502. w->comparelen = strlen(w->compare);
  503. w->hidden = thidden;
  504. #ifdef NETDATA_INTERNAL_CHECKS
  505. w->debug_enabled = tdebug;
  506. #else
  507. if(tdebug)
  508. fprintf(stderr, "apps.plugin has been compiled without debugging\n");
  509. #endif
  510. w->target = target;
  511. // append it, to maintain the order in apps_groups.conf
  512. if(last) last->next = w;
  513. else apps_groups_root_target = w;
  514. debug_log("ADDING TARGET ID '%s', process name '%s' (%s), aggregated on target '%s', options: %s %s"
  515. , w->id
  516. , w->compare, (w->starts_with && w->ends_with)?"substring":((w->starts_with)?"prefix":((w->ends_with)?"suffix":"exact"))
  517. , w->target?w->target->name:w->name
  518. , (w->hidden)?"hidden":"-"
  519. , (w->debug_enabled)?"debug":"-"
  520. );
  521. return w;
  522. }
  523. // read the apps_groups.conf file
  524. static int read_apps_groups_conf(const char *path, const char *file)
  525. {
  526. char filename[FILENAME_MAX + 1];
  527. snprintfz(filename, FILENAME_MAX, "%s/apps_%s.conf", path, file);
  528. debug_log("process groups file: '%s'", filename);
  529. // ----------------------------------------
  530. procfile *ff = procfile_open(filename, " :\t", PROCFILE_FLAG_DEFAULT);
  531. if(!ff) return 1;
  532. procfile_set_quotes(ff, "'\"");
  533. ff = procfile_readall(ff);
  534. if(!ff)
  535. return 1;
  536. size_t line, lines = procfile_lines(ff);
  537. for(line = 0; line < lines ;line++) {
  538. size_t word, words = procfile_linewords(ff, line);
  539. if(!words) continue;
  540. char *name = procfile_lineword(ff, line, 0);
  541. if(!name || !*name) continue;
  542. // find a possibly existing target
  543. struct target *w = NULL;
  544. // loop through all words, skipping the first one (the name)
  545. for(word = 0; word < words ;word++) {
  546. char *s = procfile_lineword(ff, line, word);
  547. if(!s || !*s) continue;
  548. if(*s == '#') break;
  549. // is this the first word? skip it
  550. if(s == name) continue;
  551. // add this target
  552. struct target *n = get_apps_groups_target(s, w, name);
  553. if(!n) {
  554. error("Cannot create target '%s' (line %zu, word %zu)", s, line, word);
  555. continue;
  556. }
  557. // just some optimization
  558. // to avoid searching for a target for each process
  559. if(!w) w = n->target?n->target:n;
  560. }
  561. }
  562. procfile_close(ff);
  563. apps_groups_default_target = get_apps_groups_target("p+!o@w#e$i^r&7*5(-i)l-o_", NULL, "other"); // match nothing
  564. if(!apps_groups_default_target)
  565. fatal("Cannot create default target");
  566. // allow the user to override group 'other'
  567. if(apps_groups_default_target->target)
  568. apps_groups_default_target = apps_groups_default_target->target;
  569. return 0;
  570. }
  571. // ----------------------------------------------------------------------------
  572. // struct pid_stat management
  573. static inline void init_pid_fds(struct pid_stat *p, size_t first, size_t size);
  574. static inline struct pid_stat *get_pid_entry(pid_t pid) {
  575. if(unlikely(all_pids[pid]))
  576. return all_pids[pid];
  577. struct pid_stat *p = callocz(sizeof(struct pid_stat), 1);
  578. p->fds = mallocz(sizeof(struct pid_fd) * MAX_SPARE_FDS);
  579. p->fds_size = MAX_SPARE_FDS;
  580. init_pid_fds(p, 0, p->fds_size);
  581. if(likely(root_of_pids))
  582. root_of_pids->prev = p;
  583. p->next = root_of_pids;
  584. root_of_pids = p;
  585. p->pid = pid;
  586. all_pids[pid] = p;
  587. all_pids_count++;
  588. return p;
  589. }
  590. static inline void del_pid_entry(pid_t pid) {
  591. struct pid_stat *p = all_pids[pid];
  592. if(unlikely(!p)) {
  593. error("attempted to free pid %d that is not allocated.", pid);
  594. return;
  595. }
  596. debug_log("process %d %s exited, deleting it.", pid, p->comm);
  597. if(root_of_pids == p)
  598. root_of_pids = p->next;
  599. if(p->next) p->next->prev = p->prev;
  600. if(p->prev) p->prev->next = p->next;
  601. // free the filename
  602. #ifndef __FreeBSD__
  603. {
  604. size_t i;
  605. for(i = 0; i < p->fds_size; i++)
  606. if(p->fds[i].filename)
  607. freez(p->fds[i].filename);
  608. }
  609. #endif
  610. freez(p->fds);
  611. freez(p->fds_dirname);
  612. freez(p->stat_filename);
  613. freez(p->status_filename);
  614. #ifndef __FreeBSD__
  615. arl_free(p->status_arl);
  616. #endif
  617. freez(p->io_filename);
  618. freez(p->cmdline_filename);
  619. freez(p->cmdline);
  620. freez(p);
  621. all_pids[pid] = NULL;
  622. all_pids_count--;
  623. }
  624. // ----------------------------------------------------------------------------
  625. static inline int managed_log(struct pid_stat *p, uint32_t log, int status) {
  626. if(unlikely(!status)) {
  627. // error("command failed log %u, errno %d", log, errno);
  628. if(unlikely(debug_enabled || errno != ENOENT)) {
  629. if(unlikely(debug_enabled || !(p->log_thrown & log))) {
  630. p->log_thrown |= log;
  631. switch(log) {
  632. case PID_LOG_IO:
  633. #ifdef __FreeBSD__
  634. error("Cannot fetch process %d I/O info (command '%s')", p->pid, p->comm);
  635. #else
  636. error("Cannot process %s/proc/%d/io (command '%s')", netdata_configured_host_prefix, p->pid, p->comm);
  637. #endif
  638. break;
  639. case PID_LOG_STATUS:
  640. #ifdef __FreeBSD__
  641. error("Cannot fetch process %d status info (command '%s')", p->pid, p->comm);
  642. #else
  643. error("Cannot process %s/proc/%d/status (command '%s')", netdata_configured_host_prefix, p->pid, p->comm);
  644. #endif
  645. break;
  646. case PID_LOG_CMDLINE:
  647. #ifdef __FreeBSD__
  648. error("Cannot fetch process %d command line (command '%s')", p->pid, p->comm);
  649. #else
  650. error("Cannot process %s/proc/%d/cmdline (command '%s')", netdata_configured_host_prefix, p->pid, p->comm);
  651. #endif
  652. break;
  653. case PID_LOG_FDS:
  654. #ifdef __FreeBSD__
  655. error("Cannot fetch process %d files (command '%s')", p->pid, p->comm);
  656. #else
  657. error("Cannot process entries in %s/proc/%d/fd (command '%s')", netdata_configured_host_prefix, p->pid, p->comm);
  658. #endif
  659. break;
  660. case PID_LOG_STAT:
  661. break;
  662. default:
  663. error("unhandled error for pid %d, command '%s'", p->pid, p->comm);
  664. break;
  665. }
  666. }
  667. }
  668. errno = 0;
  669. }
  670. else if(unlikely(p->log_thrown & log)) {
  671. // error("unsetting log %u on pid %d", log, p->pid);
  672. p->log_thrown &= ~log;
  673. }
  674. return status;
  675. }
  676. static inline void assign_target_to_pid(struct pid_stat *p) {
  677. targets_assignment_counter++;
  678. uint32_t hash = simple_hash(p->comm);
  679. size_t pclen = strlen(p->comm);
  680. struct target *w;
  681. for(w = apps_groups_root_target; w ; w = w->next) {
  682. // if(debug_enabled || (p->target && p->target->debug_enabled)) debug_log_int("\t\tcomparing '%s' with '%s'", w->compare, p->comm);
  683. // find it - 4 cases:
  684. // 1. the target is not a pattern
  685. // 2. the target has the prefix
  686. // 3. the target has the suffix
  687. // 4. the target is something inside cmdline
  688. if(unlikely(( (!w->starts_with && !w->ends_with && w->comparehash == hash && !strcmp(w->compare, p->comm))
  689. || (w->starts_with && !w->ends_with && !strncmp(w->compare, p->comm, w->comparelen))
  690. || (!w->starts_with && w->ends_with && pclen >= w->comparelen && !strcmp(w->compare, &p->comm[pclen - w->comparelen]))
  691. || (proc_pid_cmdline_is_needed && w->starts_with && w->ends_with && p->cmdline && strstr(p->cmdline, w->compare))
  692. ))) {
  693. if(w->target) p->target = w->target;
  694. else p->target = w;
  695. if(debug_enabled || (p->target && p->target->debug_enabled))
  696. debug_log_int("%s linked to target %s", p->comm, p->target->name);
  697. break;
  698. }
  699. }
  700. }
  701. // ----------------------------------------------------------------------------
  702. // update pids from proc
  703. static inline int read_proc_pid_cmdline(struct pid_stat *p) {
  704. static char cmdline[MAX_CMDLINE + 1];
  705. #ifdef __FreeBSD__
  706. size_t i, bytes = MAX_CMDLINE;
  707. int mib[4];
  708. mib[0] = CTL_KERN;
  709. mib[1] = KERN_PROC;
  710. mib[2] = KERN_PROC_ARGS;
  711. mib[3] = p->pid;
  712. if (unlikely(sysctl(mib, 4, cmdline, &bytes, NULL, 0)))
  713. goto cleanup;
  714. #else
  715. if(unlikely(!p->cmdline_filename)) {
  716. char filename[FILENAME_MAX + 1];
  717. snprintfz(filename, FILENAME_MAX, "%s/proc/%d/cmdline", netdata_configured_host_prefix, p->pid);
  718. p->cmdline_filename = strdupz(filename);
  719. }
  720. int fd = open(p->cmdline_filename, procfile_open_flags, 0666);
  721. if(unlikely(fd == -1)) goto cleanup;
  722. ssize_t i, bytes = read(fd, cmdline, MAX_CMDLINE);
  723. close(fd);
  724. if(unlikely(bytes < 0)) goto cleanup;
  725. #endif
  726. cmdline[bytes] = '\0';
  727. for(i = 0; i < bytes ; i++) {
  728. if(unlikely(!cmdline[i])) cmdline[i] = ' ';
  729. }
  730. if(p->cmdline) freez(p->cmdline);
  731. p->cmdline = strdupz(cmdline);
  732. debug_log("Read file '%s' contents: %s", p->cmdline_filename, p->cmdline);
  733. return 1;
  734. cleanup:
  735. // copy the command to the command line
  736. if(p->cmdline) freez(p->cmdline);
  737. p->cmdline = strdupz(p->comm);
  738. return 0;
  739. }
  740. // ----------------------------------------------------------------------------
  741. // macro to calculate the incremental rate of a value
  742. // each parameter is accessed only ONCE - so it is safe to pass function calls
  743. // or other macros as parameters
  744. #define incremental_rate(rate_variable, last_kernel_variable, new_kernel_value, collected_usec, last_collected_usec) { \
  745. kernel_uint_t _new_tmp = new_kernel_value; \
  746. (rate_variable) = (_new_tmp - (last_kernel_variable)) * (USEC_PER_SEC * RATES_DETAIL) / ((collected_usec) - (last_collected_usec)); \
  747. (last_kernel_variable) = _new_tmp; \
  748. }
  749. // the same macro for struct pid members
  750. #define pid_incremental_rate(type, var, value) \
  751. incremental_rate(var, var##_raw, value, p->type##_collected_usec, p->last_##type##_collected_usec)
  752. // ----------------------------------------------------------------------------
  753. #ifndef __FreeBSD__
  754. struct arl_callback_ptr {
  755. struct pid_stat *p;
  756. procfile *ff;
  757. size_t line;
  758. };
  759. void arl_callback_status_uid(const char *name, uint32_t hash, const char *value, void *dst) {
  760. (void)name; (void)hash; (void)value;
  761. struct arl_callback_ptr *aptr = (struct arl_callback_ptr *)dst;
  762. if(unlikely(procfile_linewords(aptr->ff, aptr->line) < 5)) return;
  763. //const char *real_uid = procfile_lineword(aptr->ff, aptr->line, 1);
  764. const char *effective_uid = procfile_lineword(aptr->ff, aptr->line, 2);
  765. //const char *saved_uid = procfile_lineword(aptr->ff, aptr->line, 3);
  766. //const char *filesystem_uid = procfile_lineword(aptr->ff, aptr->line, 4);
  767. if(likely(effective_uid && *effective_uid))
  768. aptr->p->uid = (uid_t)str2l(effective_uid);
  769. }
  770. void arl_callback_status_gid(const char *name, uint32_t hash, const char *value, void *dst) {
  771. (void)name; (void)hash; (void)value;
  772. struct arl_callback_ptr *aptr = (struct arl_callback_ptr *)dst;
  773. if(unlikely(procfile_linewords(aptr->ff, aptr->line) < 5)) return;
  774. //const char *real_gid = procfile_lineword(aptr->ff, aptr->line, 1);
  775. const char *effective_gid = procfile_lineword(aptr->ff, aptr->line, 2);
  776. //const char *saved_gid = procfile_lineword(aptr->ff, aptr->line, 3);
  777. //const char *filesystem_gid = procfile_lineword(aptr->ff, aptr->line, 4);
  778. if(likely(effective_gid && *effective_gid))
  779. aptr->p->gid = (uid_t)str2l(effective_gid);
  780. }
  781. void arl_callback_status_vmsize(const char *name, uint32_t hash, const char *value, void *dst) {
  782. (void)name; (void)hash; (void)value;
  783. struct arl_callback_ptr *aptr = (struct arl_callback_ptr *)dst;
  784. if(unlikely(procfile_linewords(aptr->ff, aptr->line) < 3)) return;
  785. aptr->p->status_vmsize = str2kernel_uint_t(procfile_lineword(aptr->ff, aptr->line, 1));
  786. }
  787. void arl_callback_status_vmswap(const char *name, uint32_t hash, const char *value, void *dst) {
  788. (void)name; (void)hash; (void)value;
  789. struct arl_callback_ptr *aptr = (struct arl_callback_ptr *)dst;
  790. if(unlikely(procfile_linewords(aptr->ff, aptr->line) < 3)) return;
  791. aptr->p->status_vmswap = str2kernel_uint_t(procfile_lineword(aptr->ff, aptr->line, 1));
  792. }
  793. void arl_callback_status_vmrss(const char *name, uint32_t hash, const char *value, void *dst) {
  794. (void)name; (void)hash; (void)value;
  795. struct arl_callback_ptr *aptr = (struct arl_callback_ptr *)dst;
  796. if(unlikely(procfile_linewords(aptr->ff, aptr->line) < 3)) return;
  797. aptr->p->status_vmrss = str2kernel_uint_t(procfile_lineword(aptr->ff, aptr->line, 1));
  798. }
  799. void arl_callback_status_rssfile(const char *name, uint32_t hash, const char *value, void *dst) {
  800. (void)name; (void)hash; (void)value;
  801. struct arl_callback_ptr *aptr = (struct arl_callback_ptr *)dst;
  802. if(unlikely(procfile_linewords(aptr->ff, aptr->line) < 3)) return;
  803. aptr->p->status_rssfile = str2kernel_uint_t(procfile_lineword(aptr->ff, aptr->line, 1));
  804. }
  805. void arl_callback_status_rssshmem(const char *name, uint32_t hash, const char *value, void *dst) {
  806. (void)name; (void)hash; (void)value;
  807. struct arl_callback_ptr *aptr = (struct arl_callback_ptr *)dst;
  808. if(unlikely(procfile_linewords(aptr->ff, aptr->line) < 3)) return;
  809. aptr->p->status_rssshmem = str2kernel_uint_t(procfile_lineword(aptr->ff, aptr->line, 1));
  810. }
  811. #endif // !__FreeBSD__
  812. static inline int read_proc_pid_status(struct pid_stat *p, void *ptr) {
  813. p->status_vmsize = 0;
  814. p->status_vmrss = 0;
  815. p->status_vmshared = 0;
  816. p->status_rssfile = 0;
  817. p->status_rssshmem = 0;
  818. p->status_vmswap = 0;
  819. #ifdef __FreeBSD__
  820. struct kinfo_proc *proc_info = (struct kinfo_proc *)ptr;
  821. p->uid = proc_info->ki_uid;
  822. p->gid = proc_info->ki_groups[0];
  823. p->status_vmsize = proc_info->ki_size / 1024; // in kB
  824. p->status_vmrss = proc_info->ki_rssize * pagesize / 1024; // in kB
  825. // TODO: what about shared and swap memory on FreeBSD?
  826. return 1;
  827. #else
  828. (void)ptr;
  829. static struct arl_callback_ptr arl_ptr;
  830. static procfile *ff = NULL;
  831. if(unlikely(!p->status_arl)) {
  832. p->status_arl = arl_create("/proc/pid/status", NULL, 60);
  833. arl_expect_custom(p->status_arl, "Uid", arl_callback_status_uid, &arl_ptr);
  834. arl_expect_custom(p->status_arl, "Gid", arl_callback_status_gid, &arl_ptr);
  835. arl_expect_custom(p->status_arl, "VmSize", arl_callback_status_vmsize, &arl_ptr);
  836. arl_expect_custom(p->status_arl, "VmRSS", arl_callback_status_vmrss, &arl_ptr);
  837. arl_expect_custom(p->status_arl, "RssFile", arl_callback_status_rssfile, &arl_ptr);
  838. arl_expect_custom(p->status_arl, "RssShmem", arl_callback_status_rssshmem, &arl_ptr);
  839. arl_expect_custom(p->status_arl, "VmSwap", arl_callback_status_vmswap, &arl_ptr);
  840. }
  841. if(unlikely(!p->status_filename)) {
  842. char filename[FILENAME_MAX + 1];
  843. snprintfz(filename, FILENAME_MAX, "%s/proc/%d/status", netdata_configured_host_prefix, p->pid);
  844. p->status_filename = strdupz(filename);
  845. }
  846. ff = procfile_reopen(ff, p->status_filename, (!ff)?" \t:,-()/":NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
  847. if(unlikely(!ff)) return 0;
  848. ff = procfile_readall(ff);
  849. if(unlikely(!ff)) return 0;
  850. calls_counter++;
  851. // let ARL use this pid
  852. arl_ptr.p = p;
  853. arl_ptr.ff = ff;
  854. size_t lines = procfile_lines(ff), l;
  855. arl_begin(p->status_arl);
  856. for(l = 0; l < lines ;l++) {
  857. // debug_log("CHECK: line %zu of %zu, key '%s' = '%s'", l, lines, procfile_lineword(ff, l, 0), procfile_lineword(ff, l, 1));
  858. arl_ptr.line = l;
  859. if(unlikely(arl_check(p->status_arl,
  860. procfile_lineword(ff, l, 0),
  861. procfile_lineword(ff, l, 1)))) break;
  862. }
  863. p->status_vmshared = p->status_rssfile + p->status_rssshmem;
  864. // 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);
  865. return 1;
  866. #endif
  867. }
  868. // ----------------------------------------------------------------------------
  869. static inline int read_proc_pid_stat(struct pid_stat *p, void *ptr) {
  870. (void)ptr;
  871. #ifdef __FreeBSD__
  872. struct kinfo_proc *proc_info = (struct kinfo_proc *)ptr;
  873. if (unlikely(proc_info->ki_tdflags & TDF_IDLETD))
  874. goto cleanup;
  875. #else
  876. static procfile *ff = NULL;
  877. if(unlikely(!p->stat_filename)) {
  878. char filename[FILENAME_MAX + 1];
  879. snprintfz(filename, FILENAME_MAX, "%s/proc/%d/stat", netdata_configured_host_prefix, p->pid);
  880. p->stat_filename = strdupz(filename);
  881. }
  882. int set_quotes = (!ff)?1:0;
  883. ff = procfile_reopen(ff, p->stat_filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
  884. if(unlikely(!ff)) goto cleanup;
  885. // if(set_quotes) procfile_set_quotes(ff, "()");
  886. if(unlikely(set_quotes))
  887. procfile_set_open_close(ff, "(", ")");
  888. ff = procfile_readall(ff);
  889. if(unlikely(!ff)) goto cleanup;
  890. #endif
  891. p->last_stat_collected_usec = p->stat_collected_usec;
  892. p->stat_collected_usec = now_monotonic_usec();
  893. calls_counter++;
  894. #ifdef __FreeBSD__
  895. char *comm = proc_info->ki_comm;
  896. p->ppid = proc_info->ki_ppid;
  897. #else
  898. // p->pid = str2pid_t(procfile_lineword(ff, 0, 0));
  899. char *comm = procfile_lineword(ff, 0, 1);
  900. // p->state = *(procfile_lineword(ff, 0, 2));
  901. p->ppid = (int32_t)str2pid_t(procfile_lineword(ff, 0, 3));
  902. // p->pgrp = (int32_t)str2pid_t(procfile_lineword(ff, 0, 4));
  903. // p->session = (int32_t)str2pid_t(procfile_lineword(ff, 0, 5));
  904. // p->tty_nr = (int32_t)str2pid_t(procfile_lineword(ff, 0, 6));
  905. // p->tpgid = (int32_t)str2pid_t(procfile_lineword(ff, 0, 7));
  906. // p->flags = str2uint64_t(procfile_lineword(ff, 0, 8));
  907. #endif
  908. if(strcmp(p->comm, comm) != 0) {
  909. if(unlikely(debug_enabled)) {
  910. if(p->comm[0])
  911. debug_log("\tpid %d (%s) changed name to '%s'", p->pid, p->comm, comm);
  912. else
  913. debug_log("\tJust added %d (%s)", p->pid, comm);
  914. }
  915. strncpyz(p->comm, comm, MAX_COMPARE_NAME);
  916. // /proc/<pid>/cmdline
  917. if(likely(proc_pid_cmdline_is_needed))
  918. managed_log(p, PID_LOG_CMDLINE, read_proc_pid_cmdline(p));
  919. assign_target_to_pid(p);
  920. }
  921. #ifdef __FreeBSD__
  922. pid_incremental_rate(stat, p->minflt, (kernel_uint_t)proc_info->ki_rusage.ru_minflt);
  923. pid_incremental_rate(stat, p->cminflt, (kernel_uint_t)proc_info->ki_rusage_ch.ru_minflt);
  924. pid_incremental_rate(stat, p->majflt, (kernel_uint_t)proc_info->ki_rusage.ru_majflt);
  925. pid_incremental_rate(stat, p->cmajflt, (kernel_uint_t)proc_info->ki_rusage_ch.ru_majflt);
  926. 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);
  927. 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);
  928. 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);
  929. 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);
  930. p->num_threads = proc_info->ki_numthreads;
  931. if(enable_guest_charts) {
  932. enable_guest_charts = 0;
  933. info("Guest charts aren't supported by FreeBSD");
  934. }
  935. #else
  936. pid_incremental_rate(stat, p->minflt, str2kernel_uint_t(procfile_lineword(ff, 0, 9)));
  937. pid_incremental_rate(stat, p->cminflt, str2kernel_uint_t(procfile_lineword(ff, 0, 10)));
  938. pid_incremental_rate(stat, p->majflt, str2kernel_uint_t(procfile_lineword(ff, 0, 11)));
  939. pid_incremental_rate(stat, p->cmajflt, str2kernel_uint_t(procfile_lineword(ff, 0, 12)));
  940. pid_incremental_rate(stat, p->utime, str2kernel_uint_t(procfile_lineword(ff, 0, 13)));
  941. pid_incremental_rate(stat, p->stime, str2kernel_uint_t(procfile_lineword(ff, 0, 14)));
  942. pid_incremental_rate(stat, p->cutime, str2kernel_uint_t(procfile_lineword(ff, 0, 15)));
  943. pid_incremental_rate(stat, p->cstime, str2kernel_uint_t(procfile_lineword(ff, 0, 16)));
  944. // p->priority = str2kernel_uint_t(procfile_lineword(ff, 0, 17));
  945. // p->nice = str2kernel_uint_t(procfile_lineword(ff, 0, 18));
  946. p->num_threads = (int32_t)str2uint32_t(procfile_lineword(ff, 0, 19));
  947. // p->itrealvalue = str2kernel_uint_t(procfile_lineword(ff, 0, 20));
  948. // p->starttime = str2kernel_uint_t(procfile_lineword(ff, 0, 21));
  949. // p->vsize = str2kernel_uint_t(procfile_lineword(ff, 0, 22));
  950. // p->rss = str2kernel_uint_t(procfile_lineword(ff, 0, 23));
  951. // p->rsslim = str2kernel_uint_t(procfile_lineword(ff, 0, 24));
  952. // p->starcode = str2kernel_uint_t(procfile_lineword(ff, 0, 25));
  953. // p->endcode = str2kernel_uint_t(procfile_lineword(ff, 0, 26));
  954. // p->startstack = str2kernel_uint_t(procfile_lineword(ff, 0, 27));
  955. // p->kstkesp = str2kernel_uint_t(procfile_lineword(ff, 0, 28));
  956. // p->kstkeip = str2kernel_uint_t(procfile_lineword(ff, 0, 29));
  957. // p->signal = str2kernel_uint_t(procfile_lineword(ff, 0, 30));
  958. // p->blocked = str2kernel_uint_t(procfile_lineword(ff, 0, 31));
  959. // p->sigignore = str2kernel_uint_t(procfile_lineword(ff, 0, 32));
  960. // p->sigcatch = str2kernel_uint_t(procfile_lineword(ff, 0, 33));
  961. // p->wchan = str2kernel_uint_t(procfile_lineword(ff, 0, 34));
  962. // p->nswap = str2kernel_uint_t(procfile_lineword(ff, 0, 35));
  963. // p->cnswap = str2kernel_uint_t(procfile_lineword(ff, 0, 36));
  964. // p->exit_signal = str2kernel_uint_t(procfile_lineword(ff, 0, 37));
  965. // p->processor = str2kernel_uint_t(procfile_lineword(ff, 0, 38));
  966. // p->rt_priority = str2kernel_uint_t(procfile_lineword(ff, 0, 39));
  967. // p->policy = str2kernel_uint_t(procfile_lineword(ff, 0, 40));
  968. // p->delayacct_blkio_ticks = str2kernel_uint_t(procfile_lineword(ff, 0, 41));
  969. if(enable_guest_charts) {
  970. pid_incremental_rate(stat, p->gtime, str2kernel_uint_t(procfile_lineword(ff, 0, 42)));
  971. pid_incremental_rate(stat, p->cgtime, str2kernel_uint_t(procfile_lineword(ff, 0, 43)));
  972. if (show_guest_time || p->gtime || p->cgtime) {
  973. p->utime -= (p->utime >= p->gtime) ? p->gtime : p->utime;
  974. p->cutime -= (p->cutime >= p->cgtime) ? p->cgtime : p->cutime;
  975. show_guest_time = 1;
  976. }
  977. }
  978. #endif
  979. if(unlikely(debug_enabled || (p->target && p->target->debug_enabled)))
  980. 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);
  981. if(unlikely(global_iterations_counter == 1)) {
  982. p->minflt = 0;
  983. p->cminflt = 0;
  984. p->majflt = 0;
  985. p->cmajflt = 0;
  986. p->utime = 0;
  987. p->stime = 0;
  988. p->gtime = 0;
  989. p->cutime = 0;
  990. p->cstime = 0;
  991. p->cgtime = 0;
  992. }
  993. return 1;
  994. cleanup:
  995. p->minflt = 0;
  996. p->cminflt = 0;
  997. p->majflt = 0;
  998. p->cmajflt = 0;
  999. p->utime = 0;
  1000. p->stime = 0;
  1001. p->gtime = 0;
  1002. p->cutime = 0;
  1003. p->cstime = 0;
  1004. p->cgtime = 0;
  1005. p->num_threads = 0;
  1006. // p->rss = 0;
  1007. return 0;
  1008. }
  1009. static inline int read_proc_pid_io(struct pid_stat *p, void *ptr) {
  1010. (void)ptr;
  1011. #ifdef __FreeBSD__
  1012. struct kinfo_proc *proc_info = (struct kinfo_proc *)ptr;
  1013. #else
  1014. static procfile *ff = NULL;
  1015. if(unlikely(!p->io_filename)) {
  1016. char filename[FILENAME_MAX + 1];
  1017. snprintfz(filename, FILENAME_MAX, "%s/proc/%d/io", netdata_configured_host_prefix, p->pid);
  1018. p->io_filename = strdupz(filename);
  1019. }
  1020. // open the file
  1021. ff = procfile_reopen(ff, p->io_filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
  1022. if(unlikely(!ff)) goto cleanup;
  1023. ff = procfile_readall(ff);
  1024. if(unlikely(!ff)) goto cleanup;
  1025. #endif
  1026. calls_counter++;
  1027. p->last_io_collected_usec = p->io_collected_usec;
  1028. p->io_collected_usec = now_monotonic_usec();
  1029. #ifdef __FreeBSD__
  1030. pid_incremental_rate(io, p->io_storage_bytes_read, proc_info->ki_rusage.ru_inblock);
  1031. pid_incremental_rate(io, p->io_storage_bytes_written, proc_info->ki_rusage.ru_oublock);
  1032. #else
  1033. pid_incremental_rate(io, p->io_logical_bytes_read, str2kernel_uint_t(procfile_lineword(ff, 0, 1)));
  1034. pid_incremental_rate(io, p->io_logical_bytes_written, str2kernel_uint_t(procfile_lineword(ff, 1, 1)));
  1035. // pid_incremental_rate(io, p->io_read_calls, str2kernel_uint_t(procfile_lineword(ff, 2, 1)));
  1036. // pid_incremental_rate(io, p->io_write_calls, str2kernel_uint_t(procfile_lineword(ff, 3, 1)));
  1037. pid_incremental_rate(io, p->io_storage_bytes_read, str2kernel_uint_t(procfile_lineword(ff, 4, 1)));
  1038. pid_incremental_rate(io, p->io_storage_bytes_written, str2kernel_uint_t(procfile_lineword(ff, 5, 1)));
  1039. // pid_incremental_rate(io, p->io_cancelled_write_bytes, str2kernel_uint_t(procfile_lineword(ff, 6, 1)));
  1040. #endif
  1041. if(unlikely(global_iterations_counter == 1)) {
  1042. p->io_logical_bytes_read = 0;
  1043. p->io_logical_bytes_written = 0;
  1044. // p->io_read_calls = 0;
  1045. // p->io_write_calls = 0;
  1046. p->io_storage_bytes_read = 0;
  1047. p->io_storage_bytes_written = 0;
  1048. // p->io_cancelled_write_bytes = 0;
  1049. }
  1050. return 1;
  1051. #ifndef __FreeBSD__
  1052. cleanup:
  1053. p->io_logical_bytes_read = 0;
  1054. p->io_logical_bytes_written = 0;
  1055. // p->io_read_calls = 0;
  1056. // p->io_write_calls = 0;
  1057. p->io_storage_bytes_read = 0;
  1058. p->io_storage_bytes_written = 0;
  1059. // p->io_cancelled_write_bytes = 0;
  1060. return 0;
  1061. #endif
  1062. }
  1063. #if (ALL_PIDS_ARE_READ_INSTANTLY == 0)
  1064. static inline int read_proc_stat() {
  1065. static char filename[FILENAME_MAX + 1] = "";
  1066. static procfile *ff = NULL;
  1067. static kernel_uint_t utime_raw = 0, stime_raw = 0, gtime_raw = 0, gntime_raw = 0, ntime_raw = 0;
  1068. static usec_t collected_usec = 0, last_collected_usec = 0;
  1069. if(unlikely(!ff)) {
  1070. snprintfz(filename, FILENAME_MAX, "%s/proc/stat", netdata_configured_host_prefix);
  1071. ff = procfile_open(filename, " \t:", PROCFILE_FLAG_DEFAULT);
  1072. if(unlikely(!ff)) goto cleanup;
  1073. }
  1074. ff = procfile_readall(ff);
  1075. if(unlikely(!ff)) goto cleanup;
  1076. last_collected_usec = collected_usec;
  1077. collected_usec = now_monotonic_usec();
  1078. calls_counter++;
  1079. // temporary - it is added global_ntime;
  1080. kernel_uint_t global_ntime = 0;
  1081. incremental_rate(global_utime, utime_raw, str2kernel_uint_t(procfile_lineword(ff, 0, 1)), collected_usec, last_collected_usec);
  1082. incremental_rate(global_ntime, ntime_raw, str2kernel_uint_t(procfile_lineword(ff, 0, 2)), collected_usec, last_collected_usec);
  1083. incremental_rate(global_stime, stime_raw, str2kernel_uint_t(procfile_lineword(ff, 0, 3)), collected_usec, last_collected_usec);
  1084. incremental_rate(global_gtime, gtime_raw, str2kernel_uint_t(procfile_lineword(ff, 0, 10)), collected_usec, last_collected_usec);
  1085. global_utime += global_ntime;
  1086. if(enable_guest_charts) {
  1087. // temporary - it is added global_ntime;
  1088. kernel_uint_t global_gntime = 0;
  1089. // guest nice time, on guest time
  1090. incremental_rate(global_gntime, gntime_raw, str2kernel_uint_t(procfile_lineword(ff, 0, 11)), collected_usec, last_collected_usec);
  1091. global_gtime += global_gntime;
  1092. // remove guest time from user time
  1093. global_utime -= (global_utime > global_gtime) ? global_gtime : global_utime;
  1094. }
  1095. if(unlikely(global_iterations_counter == 1)) {
  1096. global_utime = 0;
  1097. global_stime = 0;
  1098. global_gtime = 0;
  1099. }
  1100. return 1;
  1101. cleanup:
  1102. global_utime = 0;
  1103. global_stime = 0;
  1104. global_gtime = 0;
  1105. return 0;
  1106. }
  1107. #else
  1108. static inline int read_proc_stat() {
  1109. return 0;
  1110. }
  1111. #endif
  1112. // ----------------------------------------------------------------------------
  1113. int file_descriptor_compare(void* a, void* b) {
  1114. #ifdef NETDATA_INTERNAL_CHECKS
  1115. if(((struct file_descriptor *)a)->magic != 0x0BADCAFE || ((struct file_descriptor *)b)->magic != 0x0BADCAFE)
  1116. error("Corrupted index data detected. Please report this.");
  1117. #endif /* NETDATA_INTERNAL_CHECKS */
  1118. if(((struct file_descriptor *)a)->hash < ((struct file_descriptor *)b)->hash)
  1119. return -1;
  1120. else if(((struct file_descriptor *)a)->hash > ((struct file_descriptor *)b)->hash)
  1121. return 1;
  1122. else
  1123. return strcmp(((struct file_descriptor *)a)->name, ((struct file_descriptor *)b)->name);
  1124. }
  1125. // int file_descriptor_iterator(avl *a) { if(a) {}; return 0; }
  1126. avl_tree all_files_index = {
  1127. NULL,
  1128. file_descriptor_compare
  1129. };
  1130. static struct file_descriptor *file_descriptor_find(const char *name, uint32_t hash) {
  1131. struct file_descriptor tmp;
  1132. tmp.hash = (hash)?hash:simple_hash(name);
  1133. tmp.name = name;
  1134. tmp.count = 0;
  1135. tmp.pos = 0;
  1136. #ifdef NETDATA_INTERNAL_CHECKS
  1137. tmp.magic = 0x0BADCAFE;
  1138. #endif /* NETDATA_INTERNAL_CHECKS */
  1139. return (struct file_descriptor *)avl_search(&all_files_index, (avl *) &tmp);
  1140. }
  1141. #define file_descriptor_add(fd) avl_insert(&all_files_index, (avl *)(fd))
  1142. #define file_descriptor_remove(fd) avl_remove(&all_files_index, (avl *)(fd))
  1143. // ----------------------------------------------------------------------------
  1144. static inline void file_descriptor_not_used(int id)
  1145. {
  1146. if(id > 0 && id < all_files_size) {
  1147. #ifdef NETDATA_INTERNAL_CHECKS
  1148. if(all_files[id].magic != 0x0BADCAFE) {
  1149. error("Ignoring request to remove empty file id %d.", id);
  1150. return;
  1151. }
  1152. #endif /* NETDATA_INTERNAL_CHECKS */
  1153. debug_log("decreasing slot %d (count = %d).", id, all_files[id].count);
  1154. if(all_files[id].count > 0) {
  1155. all_files[id].count--;
  1156. if(!all_files[id].count) {
  1157. debug_log(" >> slot %d is empty.", id);
  1158. if(unlikely(file_descriptor_remove(&all_files[id]) != (void *)&all_files[id]))
  1159. error("INTERNAL ERROR: removal of unused fd from index, removed a different fd");
  1160. #ifdef NETDATA_INTERNAL_CHECKS
  1161. all_files[id].magic = 0x00000000;
  1162. #endif /* NETDATA_INTERNAL_CHECKS */
  1163. all_files_len--;
  1164. }
  1165. }
  1166. else
  1167. error("Request to decrease counter of fd %d (%s), while the use counter is 0", id, all_files[id].name);
  1168. }
  1169. else error("Request to decrease counter of fd %d, which is outside the array size (1 to %d)", id, all_files_size);
  1170. }
  1171. static inline void all_files_grow() {
  1172. void *old = all_files;
  1173. int i;
  1174. // there is no empty slot
  1175. debug_log("extending fd array to %d entries", all_files_size + FILE_DESCRIPTORS_INCREASE_STEP);
  1176. all_files = reallocz(all_files, (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP) * sizeof(struct file_descriptor));
  1177. // if the address changed, we have to rebuild the index
  1178. // since all pointers are now invalid
  1179. if(unlikely(old && old != (void *)all_files)) {
  1180. debug_log(" >> re-indexing.");
  1181. all_files_index.root = NULL;
  1182. for(i = 0; i < all_files_size; i++) {
  1183. if(!all_files[i].count) continue;
  1184. if(unlikely(file_descriptor_add(&all_files[i]) != (void *)&all_files[i]))
  1185. error("INTERNAL ERROR: duplicate indexing of fd during realloc.");
  1186. }
  1187. debug_log(" >> re-indexing done.");
  1188. }
  1189. // initialize the newly added entries
  1190. for(i = all_files_size; i < (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP); i++) {
  1191. all_files[i].count = 0;
  1192. all_files[i].name = NULL;
  1193. #ifdef NETDATA_INTERNAL_CHECKS
  1194. all_files[i].magic = 0x00000000;
  1195. #endif /* NETDATA_INTERNAL_CHECKS */
  1196. all_files[i].pos = i;
  1197. }
  1198. if(unlikely(!all_files_size)) all_files_len = 1;
  1199. all_files_size += FILE_DESCRIPTORS_INCREASE_STEP;
  1200. }
  1201. static inline int file_descriptor_set_on_empty_slot(const char *name, uint32_t hash, FD_FILETYPE type) {
  1202. // check we have enough memory to add it
  1203. if(!all_files || all_files_len == all_files_size)
  1204. all_files_grow();
  1205. debug_log(" >> searching for empty slot.");
  1206. // search for an empty slot
  1207. static int last_pos = 0;
  1208. int i, c;
  1209. for(i = 0, c = last_pos ; i < all_files_size ; i++, c++) {
  1210. if(c >= all_files_size) c = 0;
  1211. if(c == 0) continue;
  1212. if(!all_files[c].count) {
  1213. debug_log(" >> Examining slot %d.", c);
  1214. #ifdef NETDATA_INTERNAL_CHECKS
  1215. if(all_files[c].magic == 0x0BADCAFE && all_files[c].name && file_descriptor_find(all_files[c].name, all_files[c].hash))
  1216. error("fd on position %d is not cleared properly. It still has %s in it.", c, all_files[c].name);
  1217. #endif /* NETDATA_INTERNAL_CHECKS */
  1218. debug_log(" >> %s fd position %d for %s (last name: %s)", all_files[c].name?"re-using":"using", c, name, all_files[c].name);
  1219. freez((void *)all_files[c].name);
  1220. all_files[c].name = NULL;
  1221. last_pos = c;
  1222. break;
  1223. }
  1224. }
  1225. all_files_len++;
  1226. if(i == all_files_size) {
  1227. fatal("We should find an empty slot, but there isn't any");
  1228. exit(1);
  1229. }
  1230. // else we have an empty slot in 'c'
  1231. debug_log(" >> updating slot %d.", c);
  1232. all_files[c].name = strdupz(name);
  1233. all_files[c].hash = hash;
  1234. all_files[c].type = type;
  1235. all_files[c].pos = c;
  1236. all_files[c].count = 1;
  1237. #ifdef NETDATA_INTERNAL_CHECKS
  1238. all_files[c].magic = 0x0BADCAFE;
  1239. #endif /* NETDATA_INTERNAL_CHECKS */
  1240. if(unlikely(file_descriptor_add(&all_files[c]) != (void *)&all_files[c]))
  1241. error("INTERNAL ERROR: duplicate indexing of fd.");
  1242. debug_log("using fd position %d (name: %s)", c, all_files[c].name);
  1243. return c;
  1244. }
  1245. static inline int file_descriptor_find_or_add(const char *name, uint32_t hash) {
  1246. if(unlikely(!hash))
  1247. hash = simple_hash(name);
  1248. debug_log("adding or finding name '%s' with hash %u", name, hash);
  1249. struct file_descriptor *fd = file_descriptor_find(name, hash);
  1250. if(fd) {
  1251. // found
  1252. debug_log(" >> found on slot %d", fd->pos);
  1253. fd->count++;
  1254. return fd->pos;
  1255. }
  1256. // not found
  1257. FD_FILETYPE type;
  1258. if(likely(name[0] == '/')) type = FILETYPE_FILE;
  1259. else if(likely(strncmp(name, "pipe:", 5) == 0)) type = FILETYPE_PIPE;
  1260. else if(likely(strncmp(name, "socket:", 7) == 0)) type = FILETYPE_SOCKET;
  1261. else if(likely(strncmp(name, "anon_inode:", 11) == 0)) {
  1262. const char *t = &name[11];
  1263. if(strcmp(t, "inotify") == 0) type = FILETYPE_INOTIFY;
  1264. else if(strcmp(t, "[eventfd]") == 0) type = FILETYPE_EVENTFD;
  1265. else if(strcmp(t, "[eventpoll]") == 0) type = FILETYPE_EVENTPOLL;
  1266. else if(strcmp(t, "[timerfd]") == 0) type = FILETYPE_TIMERFD;
  1267. else if(strcmp(t, "[signalfd]") == 0) type = FILETYPE_SIGNALFD;
  1268. else {
  1269. debug_log("UNKNOWN anonymous inode: %s", name);
  1270. type = FILETYPE_OTHER;
  1271. }
  1272. }
  1273. else if(likely(strcmp(name, "inotify") == 0)) type = FILETYPE_INOTIFY;
  1274. else {
  1275. debug_log("UNKNOWN linkname: %s", name);
  1276. type = FILETYPE_OTHER;
  1277. }
  1278. return file_descriptor_set_on_empty_slot(name, hash, type);
  1279. }
  1280. static inline void clear_pid_fd(struct pid_fd *pfd) {
  1281. pfd->fd = 0;
  1282. #ifndef __FreeBSD__
  1283. pfd->link_hash = 0;
  1284. pfd->inode = 0;
  1285. pfd->cache_iterations_counter = 0;
  1286. pfd->cache_iterations_reset = 0;
  1287. #endif
  1288. }
  1289. static inline void make_all_pid_fds_negative(struct pid_stat *p) {
  1290. struct pid_fd *pfd = p->fds, *pfdend = &p->fds[p->fds_size];
  1291. while(pfd < pfdend) {
  1292. pfd->fd = -(pfd->fd);
  1293. pfd++;
  1294. }
  1295. }
  1296. static inline void cleanup_negative_pid_fds(struct pid_stat *p) {
  1297. struct pid_fd *pfd = p->fds, *pfdend = &p->fds[p->fds_size];
  1298. while(pfd < pfdend) {
  1299. int fd = pfd->fd;
  1300. if(unlikely(fd < 0)) {
  1301. file_descriptor_not_used(-(fd));
  1302. clear_pid_fd(pfd);
  1303. }
  1304. pfd++;
  1305. }
  1306. }
  1307. static inline void init_pid_fds(struct pid_stat *p, size_t first, size_t size) {
  1308. struct pid_fd *pfd = &p->fds[first], *pfdend = &p->fds[first + size];
  1309. size_t i = first;
  1310. while(pfd < pfdend) {
  1311. #ifndef __FreeBSD__
  1312. pfd->filename = NULL;
  1313. #endif
  1314. clear_pid_fd(pfd);
  1315. pfd++;
  1316. i++;
  1317. }
  1318. }
  1319. static inline int read_pid_file_descriptors(struct pid_stat *p, void *ptr) {
  1320. (void)ptr;
  1321. #ifdef __FreeBSD__
  1322. int mib[4];
  1323. size_t size;
  1324. struct kinfo_file *fds;
  1325. static char *fdsbuf;
  1326. char *bfdsbuf, *efdsbuf;
  1327. char fdsname[FILENAME_MAX + 1];
  1328. // we make all pid fds negative, so that
  1329. // we can detect unused file descriptors
  1330. // at the end, to free them
  1331. make_all_pid_fds_negative(p);
  1332. mib[0] = CTL_KERN;
  1333. mib[1] = KERN_PROC;
  1334. mib[2] = KERN_PROC_FILEDESC;
  1335. mib[3] = p->pid;
  1336. if (unlikely(sysctl(mib, 4, NULL, &size, NULL, 0))) {
  1337. error("sysctl error: Can't get file descriptors data size for pid %d", p->pid);
  1338. return 0;
  1339. }
  1340. if (likely(size > 0))
  1341. fdsbuf = reallocz(fdsbuf, size);
  1342. if (unlikely(sysctl(mib, 4, fdsbuf, &size, NULL, 0))) {
  1343. error("sysctl error: Can't get file descriptors data for pid %d", p->pid);
  1344. return 0;
  1345. }
  1346. bfdsbuf = fdsbuf;
  1347. efdsbuf = fdsbuf + size;
  1348. while (bfdsbuf < efdsbuf) {
  1349. fds = (struct kinfo_file *)(uintptr_t)bfdsbuf;
  1350. if (unlikely(fds->kf_structsize == 0))
  1351. break;
  1352. // do not process file descriptors for current working directory, root directory,
  1353. // jail directory, ktrace vnode, text vnode and controlling terminal
  1354. if (unlikely(fds->kf_fd < 0)) {
  1355. bfdsbuf += fds->kf_structsize;
  1356. continue;
  1357. }
  1358. // get file descriptors array index
  1359. int fdid = fds->kf_fd;
  1360. // check if the fds array is small
  1361. if (unlikely(fdid >= p->fds_size)) {
  1362. // it is small, extend it
  1363. debug_log("extending fd memory slots for %s from %d to %d", p->comm, p->fds_size, fdid + MAX_SPARE_FDS);
  1364. p->fds = reallocz(p->fds, (fdid + MAX_SPARE_FDS) * sizeof(struct pid_fd));
  1365. // and initialize it
  1366. init_pid_fds(p, p->fds_size, (fdid + MAX_SPARE_FDS) - p->fds_size);
  1367. p->fds_size = fdid + MAX_SPARE_FDS;
  1368. }
  1369. if (unlikely(p->fds[fdid].fd == 0)) {
  1370. // we don't know this fd, get it
  1371. switch (fds->kf_type) {
  1372. case KF_TYPE_FIFO:
  1373. case KF_TYPE_VNODE:
  1374. if (unlikely(!fds->kf_path[0])) {
  1375. sprintf(fdsname, "other: inode: %lu", fds->kf_un.kf_file.kf_file_fileid);
  1376. break;
  1377. }
  1378. sprintf(fdsname, "%s", fds->kf_path);
  1379. break;
  1380. case KF_TYPE_SOCKET:
  1381. switch (fds->kf_sock_domain) {
  1382. case AF_INET:
  1383. case AF_INET6:
  1384. if (fds->kf_sock_protocol == IPPROTO_TCP)
  1385. sprintf(fdsname, "socket: %d %lx", fds->kf_sock_protocol, fds->kf_un.kf_sock.kf_sock_inpcb);
  1386. else
  1387. sprintf(fdsname, "socket: %d %lx", fds->kf_sock_protocol, fds->kf_un.kf_sock.kf_sock_pcb);
  1388. break;
  1389. case AF_UNIX:
  1390. /* print address of pcb and connected pcb */
  1391. sprintf(fdsname, "socket: %lx %lx", fds->kf_un.kf_sock.kf_sock_pcb, fds->kf_un.kf_sock.kf_sock_unpconn);
  1392. break;
  1393. default:
  1394. /* print protocol number and socket address */
  1395. #if __FreeBSD_version < 1200031
  1396. sprintf(fdsname, "socket: other: %d %s %s", fds->kf_sock_protocol, fds->kf_sa_local.__ss_pad1, fds->kf_sa_local.__ss_pad2);
  1397. #else
  1398. 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);
  1399. #endif
  1400. }
  1401. break;
  1402. case KF_TYPE_PIPE:
  1403. sprintf(fdsname, "pipe: %lu %lu", fds->kf_un.kf_pipe.kf_pipe_addr, fds->kf_un.kf_pipe.kf_pipe_peer);
  1404. break;
  1405. case KF_TYPE_PTS:
  1406. #if __FreeBSD_version < 1200031
  1407. sprintf(fdsname, "other: pts: %u", fds->kf_un.kf_pts.kf_pts_dev);
  1408. #else
  1409. sprintf(fdsname, "other: pts: %lu", fds->kf_un.kf_pts.kf_pts_dev);
  1410. #endif
  1411. break;
  1412. case KF_TYPE_SHM:
  1413. sprintf(fdsname, "other: shm: %s size: %lu", fds->kf_path, fds->kf_un.kf_file.kf_file_size);
  1414. break;
  1415. case KF_TYPE_SEM:
  1416. sprintf(fdsname, "other: sem: %u", fds->kf_un.kf_sem.kf_sem_value);
  1417. break;
  1418. default:
  1419. sprintf(fdsname, "other: pid: %d fd: %d", fds->kf_un.kf_proc.kf_pid, fds->kf_fd);
  1420. }
  1421. // if another process already has this, we will get
  1422. // the same id
  1423. p->fds[fdid].fd = file_descriptor_find_or_add(fdsname, 0);
  1424. }
  1425. // else make it positive again, we need it
  1426. // of course, the actual file may have changed
  1427. else
  1428. p->fds[fdid].fd = -p->fds[fdid].fd;
  1429. bfdsbuf += fds->kf_structsize;
  1430. }
  1431. #else
  1432. if(unlikely(!p->fds_dirname)) {
  1433. char dirname[FILENAME_MAX+1];
  1434. snprintfz(dirname, FILENAME_MAX, "%s/proc/%d/fd", netdata_configured_host_prefix, p->pid);
  1435. p->fds_dirname = strdupz(dirname);
  1436. }
  1437. DIR *fds = opendir(p->fds_dirname);
  1438. if(unlikely(!fds)) return 0;
  1439. struct dirent *de;
  1440. char linkname[FILENAME_MAX + 1];
  1441. // we make all pid fds negative, so that
  1442. // we can detect unused file descriptors
  1443. // at the end, to free them
  1444. make_all_pid_fds_negative(p);
  1445. while((de = readdir(fds))) {
  1446. // we need only files with numeric names
  1447. if(unlikely(de->d_name[0] < '0' || de->d_name[0] > '9'))
  1448. continue;
  1449. // get its number
  1450. int fdid = (int) str2l(de->d_name);
  1451. if(unlikely(fdid < 0)) continue;
  1452. // check if the fds array is small
  1453. if(unlikely((size_t)fdid >= p->fds_size)) {
  1454. // it is small, extend it
  1455. debug_log("extending fd memory slots for %s from %d to %d"
  1456. , p->comm
  1457. , p->fds_size
  1458. , fdid + MAX_SPARE_FDS
  1459. );
  1460. p->fds = reallocz(p->fds, (fdid + MAX_SPARE_FDS) * sizeof(struct pid_fd));
  1461. // and initialize it
  1462. init_pid_fds(p, p->fds_size, (fdid + MAX_SPARE_FDS) - p->fds_size);
  1463. p->fds_size = (size_t)fdid + MAX_SPARE_FDS;
  1464. }
  1465. if(unlikely(p->fds[fdid].fd < 0 && de->d_ino != p->fds[fdid].inode)) {
  1466. // inodes do not match, clear the previous entry
  1467. inodes_changed_counter++;
  1468. file_descriptor_not_used(-p->fds[fdid].fd);
  1469. clear_pid_fd(&p->fds[fdid]);
  1470. }
  1471. if(p->fds[fdid].fd < 0 && p->fds[fdid].cache_iterations_counter > 0) {
  1472. p->fds[fdid].fd = -p->fds[fdid].fd;
  1473. p->fds[fdid].cache_iterations_counter--;
  1474. continue;
  1475. }
  1476. if(unlikely(!p->fds[fdid].filename)) {
  1477. filenames_allocated_counter++;
  1478. char fdname[FILENAME_MAX + 1];
  1479. snprintfz(fdname, FILENAME_MAX, "%s/proc/%d/fd/%s", netdata_configured_host_prefix, p->pid, de->d_name);
  1480. p->fds[fdid].filename = strdupz(fdname);
  1481. }
  1482. file_counter++;
  1483. ssize_t l = readlink(p->fds[fdid].filename, linkname, FILENAME_MAX);
  1484. if(unlikely(l == -1)) {
  1485. // cannot read the link
  1486. if(debug_enabled || (p->target && p->target->debug_enabled))
  1487. error("Cannot read link %s", p->fds[fdid].filename);
  1488. if(unlikely(p->fds[fdid].fd < 0)) {
  1489. file_descriptor_not_used(-p->fds[fdid].fd);
  1490. clear_pid_fd(&p->fds[fdid]);
  1491. }
  1492. continue;
  1493. }
  1494. else
  1495. linkname[l] = '\0';
  1496. uint32_t link_hash = simple_hash(linkname);
  1497. if(unlikely(p->fds[fdid].fd < 0 && p->fds[fdid].link_hash != link_hash)) {
  1498. // the link changed
  1499. links_changed_counter++;
  1500. file_descriptor_not_used(-p->fds[fdid].fd);
  1501. clear_pid_fd(&p->fds[fdid]);
  1502. }
  1503. if(unlikely(p->fds[fdid].fd == 0)) {
  1504. // we don't know this fd, get it
  1505. // if another process already has this, we will get
  1506. // the same id
  1507. p->fds[fdid].fd = file_descriptor_find_or_add(linkname, link_hash);
  1508. p->fds[fdid].inode = de->d_ino;
  1509. p->fds[fdid].link_hash = link_hash;
  1510. }
  1511. else {
  1512. // else make it positive again, we need it
  1513. p->fds[fdid].fd = -p->fds[fdid].fd;
  1514. }
  1515. // caching control
  1516. // without this we read all the files on every iteration
  1517. if(max_fds_cache_seconds > 0) {
  1518. size_t spread = ((size_t)max_fds_cache_seconds > 10) ? 10 : (size_t)max_fds_cache_seconds;
  1519. // cache it for a few iterations
  1520. size_t max = ((size_t) max_fds_cache_seconds + (fdid % spread)) / (size_t) update_every;
  1521. p->fds[fdid].cache_iterations_reset++;
  1522. if(unlikely(p->fds[fdid].cache_iterations_reset % spread == (size_t) fdid % spread))
  1523. p->fds[fdid].cache_iterations_reset++;
  1524. if(unlikely((fdid <= 2 && p->fds[fdid].cache_iterations_reset > 5) ||
  1525. p->fds[fdid].cache_iterations_reset > max)) {
  1526. // for stdin, stdout, stderr (fdid <= 2) we have checked a few times, or if it goes above the max, goto max
  1527. p->fds[fdid].cache_iterations_reset = max;
  1528. }
  1529. p->fds[fdid].cache_iterations_counter = p->fds[fdid].cache_iterations_reset;
  1530. }
  1531. }
  1532. closedir(fds);
  1533. #endif
  1534. cleanup_negative_pid_fds(p);
  1535. return 1;
  1536. }
  1537. // ----------------------------------------------------------------------------
  1538. static inline int debug_print_process_and_parents(struct pid_stat *p, usec_t time) {
  1539. char *prefix = "\\_ ";
  1540. int indent = 0;
  1541. if(p->parent)
  1542. indent = debug_print_process_and_parents(p->parent, p->stat_collected_usec);
  1543. else
  1544. prefix = " > ";
  1545. char buffer[indent + 1];
  1546. int i;
  1547. for(i = 0; i < indent ;i++) buffer[i] = ' ';
  1548. buffer[i] = '\0';
  1549. fprintf(stderr, " %s %s%s (%d %s %llu"
  1550. , buffer
  1551. , prefix
  1552. , p->comm
  1553. , p->pid
  1554. , p->updated?"running":"exited"
  1555. , p->stat_collected_usec - time
  1556. );
  1557. if(p->utime) fprintf(stderr, " utime=" KERNEL_UINT_FORMAT, p->utime);
  1558. if(p->stime) fprintf(stderr, " stime=" KERNEL_UINT_FORMAT, p->stime);
  1559. if(p->gtime) fprintf(stderr, " gtime=" KERNEL_UINT_FORMAT, p->gtime);
  1560. if(p->cutime) fprintf(stderr, " cutime=" KERNEL_UINT_FORMAT, p->cutime);
  1561. if(p->cstime) fprintf(stderr, " cstime=" KERNEL_UINT_FORMAT, p->cstime);
  1562. if(p->cgtime) fprintf(stderr, " cgtime=" KERNEL_UINT_FORMAT, p->cgtime);
  1563. if(p->minflt) fprintf(stderr, " minflt=" KERNEL_UINT_FORMAT, p->minflt);
  1564. if(p->cminflt) fprintf(stderr, " cminflt=" KERNEL_UINT_FORMAT, p->cminflt);
  1565. if(p->majflt) fprintf(stderr, " majflt=" KERNEL_UINT_FORMAT, p->majflt);
  1566. if(p->cmajflt) fprintf(stderr, " cmajflt=" KERNEL_UINT_FORMAT, p->cmajflt);
  1567. fprintf(stderr, ")\n");
  1568. return indent + 1;
  1569. }
  1570. static inline void debug_print_process_tree(struct pid_stat *p, char *msg) {
  1571. debug_log("%s: process %s (%d, %s) with parents:", msg, p->comm, p->pid, p->updated?"running":"exited");
  1572. debug_print_process_and_parents(p, p->stat_collected_usec);
  1573. }
  1574. static inline void debug_find_lost_child(struct pid_stat *pe, kernel_uint_t lost, int type) {
  1575. int found = 0;
  1576. struct pid_stat *p = NULL;
  1577. for(p = root_of_pids; p ; p = p->next) {
  1578. if(p == pe) continue;
  1579. switch(type) {
  1580. case 1:
  1581. if(p->cminflt > lost) {
  1582. 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);
  1583. found++;
  1584. }
  1585. break;
  1586. case 2:
  1587. if(p->cmajflt > lost) {
  1588. 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);
  1589. found++;
  1590. }
  1591. break;
  1592. case 3:
  1593. if(p->cutime > lost) {
  1594. 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);
  1595. found++;
  1596. }
  1597. break;
  1598. case 4:
  1599. if(p->cstime > lost) {
  1600. 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);
  1601. found++;
  1602. }
  1603. break;
  1604. case 5:
  1605. if(p->cgtime > lost) {
  1606. 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);
  1607. found++;
  1608. }
  1609. break;
  1610. }
  1611. }
  1612. if(!found) {
  1613. switch(type) {
  1614. case 1:
  1615. 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);
  1616. break;
  1617. case 2:
  1618. 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);
  1619. break;
  1620. case 3:
  1621. 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);
  1622. break;
  1623. case 4:
  1624. 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);
  1625. break;
  1626. case 5:
  1627. 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);
  1628. break;
  1629. }
  1630. }
  1631. }
  1632. static inline kernel_uint_t remove_exited_child_from_parent(kernel_uint_t *field, kernel_uint_t *pfield) {
  1633. kernel_uint_t absorbed = 0;
  1634. if(*field > *pfield) {
  1635. absorbed += *pfield;
  1636. *field -= *pfield;
  1637. *pfield = 0;
  1638. }
  1639. else {
  1640. absorbed += *field;
  1641. *pfield -= *field;
  1642. *field = 0;
  1643. }
  1644. return absorbed;
  1645. }
  1646. static inline void process_exited_processes() {
  1647. struct pid_stat *p;
  1648. for(p = root_of_pids; p ; p = p->next) {
  1649. if(p->updated || !p->stat_collected_usec)
  1650. continue;
  1651. kernel_uint_t utime = (p->utime_raw + p->cutime_raw) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
  1652. kernel_uint_t stime = (p->stime_raw + p->cstime_raw) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
  1653. kernel_uint_t gtime = (p->gtime_raw + p->cgtime_raw) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
  1654. kernel_uint_t minflt = (p->minflt_raw + p->cminflt_raw) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
  1655. kernel_uint_t majflt = (p->majflt_raw + p->cmajflt_raw) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
  1656. if(utime + stime + gtime + minflt + majflt == 0)
  1657. continue;
  1658. if(unlikely(debug_enabled)) {
  1659. 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 ")"
  1660. , p->comm
  1661. , p->pid
  1662. , p->updated?"running":"exited"
  1663. , utime
  1664. , stime
  1665. , gtime
  1666. , minflt
  1667. , majflt
  1668. );
  1669. debug_print_process_tree(p, "Searching parents");
  1670. }
  1671. struct pid_stat *pp;
  1672. for(pp = p->parent; pp ; pp = pp->parent) {
  1673. if(!pp->updated) continue;
  1674. kernel_uint_t absorbed;
  1675. absorbed = remove_exited_child_from_parent(&utime, &pp->cutime);
  1676. if(unlikely(debug_enabled && absorbed))
  1677. 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);
  1678. absorbed = remove_exited_child_from_parent(&stime, &pp->cstime);
  1679. if(unlikely(debug_enabled && absorbed))
  1680. 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);
  1681. absorbed = remove_exited_child_from_parent(&gtime, &pp->cgtime);
  1682. if(unlikely(debug_enabled && absorbed))
  1683. 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);
  1684. absorbed = remove_exited_child_from_parent(&minflt, &pp->cminflt);
  1685. if(unlikely(debug_enabled && absorbed))
  1686. 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);
  1687. absorbed = remove_exited_child_from_parent(&majflt, &pp->cmajflt);
  1688. if(unlikely(debug_enabled && absorbed))
  1689. 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);
  1690. }
  1691. if(unlikely(utime + stime + gtime + minflt + majflt > 0)) {
  1692. if(unlikely(debug_enabled)) {
  1693. if(utime) debug_find_lost_child(p, utime, 3);
  1694. if(stime) debug_find_lost_child(p, stime, 4);
  1695. if(gtime) debug_find_lost_child(p, gtime, 5);
  1696. if(minflt) debug_find_lost_child(p, minflt, 1);
  1697. if(majflt) debug_find_lost_child(p, majflt, 2);
  1698. }
  1699. p->keep = 1;
  1700. 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 ")"
  1701. , p->comm
  1702. , p->pid
  1703. , p->updated?"running":"exited"
  1704. , utime
  1705. , stime
  1706. , gtime
  1707. , minflt
  1708. , majflt
  1709. );
  1710. for(pp = p->parent; pp ; pp = pp->parent) {
  1711. if(pp->updated) break;
  1712. pp->keep = 1;
  1713. debug_log(" > - KEEP - parent for another loop: %s (%d %s)"
  1714. , pp->comm
  1715. , pp->pid
  1716. , pp->updated?"running":"exited"
  1717. );
  1718. }
  1719. p->utime_raw = utime * (p->stat_collected_usec - p->last_stat_collected_usec) / (USEC_PER_SEC * RATES_DETAIL);
  1720. p->stime_raw = stime * (p->stat_collected_usec - p->last_stat_collected_usec) / (USEC_PER_SEC * RATES_DETAIL);
  1721. p->gtime_raw = gtime * (p->stat_collected_usec - p->last_stat_collected_usec) / (USEC_PER_SEC * RATES_DETAIL);
  1722. p->minflt_raw = minflt * (p->stat_collected_usec - p->last_stat_collected_usec) / (USEC_PER_SEC * RATES_DETAIL);
  1723. p->majflt_raw = majflt * (p->stat_collected_usec - p->last_stat_collected_usec) / (USEC_PER_SEC * RATES_DETAIL);
  1724. p->cutime_raw = p->cstime_raw = p->cgtime_raw = p->cminflt_raw = p->cmajflt_raw = 0;
  1725. debug_log(" ");
  1726. }
  1727. else
  1728. debug_log(" > totally absorbed - DONE - %s (%d %s)"
  1729. , p->comm
  1730. , p->pid
  1731. , p->updated?"running":"exited"
  1732. );
  1733. }
  1734. }
  1735. static inline void link_all_processes_to_their_parents(void) {
  1736. struct pid_stat *p, *pp;
  1737. // link all children to their parents
  1738. // and update children count on parents
  1739. for(p = root_of_pids; p ; p = p->next) {
  1740. // for each process found
  1741. p->sortlist = 0;
  1742. p->parent = NULL;
  1743. if(unlikely(!p->ppid)) {
  1744. p->parent = NULL;
  1745. continue;
  1746. }
  1747. pp = all_pids[p->ppid];
  1748. if(likely(pp)) {
  1749. p->parent = pp;
  1750. pp->children_count++;
  1751. if(unlikely(debug_enabled || (p->target && p->target->debug_enabled)))
  1752. 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);
  1753. }
  1754. else {
  1755. p->parent = NULL;
  1756. error("pid %d %s states parent %d, but the later does not exist.", p->pid, p->comm, p->ppid);
  1757. }
  1758. }
  1759. }
  1760. // ----------------------------------------------------------------------------
  1761. // 1. read all files in /proc
  1762. // 2. for each numeric directory:
  1763. // i. read /proc/pid/stat
  1764. // ii. read /proc/pid/status
  1765. // iii. read /proc/pid/io (requires root access)
  1766. // iii. read the entries in directory /proc/pid/fd (requires root access)
  1767. // for each entry:
  1768. // a. find or create a struct file_descriptor
  1769. // b. cleanup any old/unused file_descriptors
  1770. // after all these, some pids may be linked to targets, while others may not
  1771. // in case of errors, only 1 every 1000 errors is printed
  1772. // to avoid filling up all disk space
  1773. // if debug is enabled, all errors are printed
  1774. #if (ALL_PIDS_ARE_READ_INSTANTLY == 0)
  1775. static int compar_pid(const void *pid1, const void *pid2) {
  1776. struct pid_stat *p1 = all_pids[*((pid_t *)pid1)];
  1777. struct pid_stat *p2 = all_pids[*((pid_t *)pid2)];
  1778. if(p1->sortlist > p2->sortlist)
  1779. return -1;
  1780. else
  1781. return 1;
  1782. }
  1783. #endif
  1784. static inline int collect_data_for_pid(pid_t pid, void *ptr) {
  1785. if(unlikely(pid < 0 || pid > pid_max)) {
  1786. error("Invalid pid %d read (expected %d to %d). Ignoring process.", pid, 0, pid_max);
  1787. return 0;
  1788. }
  1789. struct pid_stat *p = get_pid_entry(pid);
  1790. if(unlikely(!p || p->read)) return 0;
  1791. p->read = 1;
  1792. // debug_log("Reading process %d (%s), sortlist %d", p->pid, p->comm, p->sortlist);
  1793. // --------------------------------------------------------------------
  1794. // /proc/<pid>/stat
  1795. if(unlikely(!managed_log(p, PID_LOG_STAT, read_proc_pid_stat(p, ptr))))
  1796. // there is no reason to proceed if we cannot get its status
  1797. return 0;
  1798. // check its parent pid
  1799. if(unlikely(p->ppid < 0 || p->ppid > pid_max)) {
  1800. error("Pid %d (command '%s') states invalid parent pid %d. Using 0.", pid, p->comm, p->ppid);
  1801. p->ppid = 0;
  1802. }
  1803. // --------------------------------------------------------------------
  1804. // /proc/<pid>/io
  1805. managed_log(p, PID_LOG_IO, read_proc_pid_io(p, ptr));
  1806. // --------------------------------------------------------------------
  1807. // /proc/<pid>/status
  1808. if(unlikely(!managed_log(p, PID_LOG_STATUS, read_proc_pid_status(p, ptr))))
  1809. // there is no reason to proceed if we cannot get its status
  1810. return 0;
  1811. // --------------------------------------------------------------------
  1812. // /proc/<pid>/fd
  1813. if(enable_file_charts)
  1814. managed_log(p, PID_LOG_FDS, read_pid_file_descriptors(p, ptr));
  1815. // --------------------------------------------------------------------
  1816. // done!
  1817. if(unlikely(debug_enabled && include_exited_childs && all_pids_count && p->ppid && all_pids[p->ppid] && !all_pids[p->ppid]->read))
  1818. 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);
  1819. // mark it as updated
  1820. p->updated = 1;
  1821. p->keep = 0;
  1822. p->keeploops = 0;
  1823. return 1;
  1824. }
  1825. static int collect_data_for_all_processes(void) {
  1826. struct pid_stat *p = NULL;
  1827. #ifdef __FreeBSD__
  1828. int i, procnum;
  1829. static size_t procbase_size = 0;
  1830. static struct kinfo_proc *procbase = NULL;
  1831. size_t new_procbase_size;
  1832. int mib[3] = { CTL_KERN, KERN_PROC, KERN_PROC_PROC };
  1833. if (unlikely(sysctl(mib, 3, NULL, &new_procbase_size, NULL, 0))) {
  1834. error("sysctl error: Can't get processes data size");
  1835. return 0;
  1836. }
  1837. // give it some air for processes that may be started
  1838. // during this little time.
  1839. new_procbase_size += 100 * sizeof(struct kinfo_proc);
  1840. // increase the buffer if needed
  1841. if(new_procbase_size > procbase_size) {
  1842. procbase_size = new_procbase_size;
  1843. procbase = reallocz(procbase, procbase_size);
  1844. }
  1845. // sysctl() gets from new_procbase_size the buffer size
  1846. // and also returns to it the amount of data filled in
  1847. new_procbase_size = procbase_size;
  1848. // get the processes from the system
  1849. if (unlikely(sysctl(mib, 3, procbase, &new_procbase_size, NULL, 0))) {
  1850. error("sysctl error: Can't get processes data");
  1851. return 0;
  1852. }
  1853. // based on the amount of data filled in
  1854. // calculate the number of processes we got
  1855. procnum = new_procbase_size / sizeof(struct kinfo_proc);
  1856. #endif
  1857. if(all_pids_count) {
  1858. #if (ALL_PIDS_ARE_READ_INSTANTLY == 0)
  1859. size_t slc = 0;
  1860. #endif
  1861. for(p = root_of_pids; p ; p = p->next) {
  1862. p->read = 0; // mark it as not read, so that collect_data_for_pid() will read it
  1863. p->updated = 0;
  1864. p->merged = 0;
  1865. p->children_count = 0;
  1866. p->parent = NULL;
  1867. #if (ALL_PIDS_ARE_READ_INSTANTLY == 0)
  1868. all_pids_sortlist[slc++] = p->pid;
  1869. #endif
  1870. }
  1871. #if (ALL_PIDS_ARE_READ_INSTANTLY == 0)
  1872. if(unlikely(slc != all_pids_count)) {
  1873. error("Internal error: I was thinking I had %zu processes in my arrays, but it seems there are %zu.", all_pids_count, slc);
  1874. all_pids_count = slc;
  1875. }
  1876. if(include_exited_childs) {
  1877. // Read parents before childs
  1878. // This is needed to prevent a situation where
  1879. // a child is found running, but until we read
  1880. // its parent, it has exited and its parent
  1881. // has accumulated its resources.
  1882. qsort((void *)all_pids_sortlist, (size_t)all_pids_count, sizeof(pid_t), compar_pid);
  1883. // we forward read all running processes
  1884. // collect_data_for_pid() is smart enough,
  1885. // not to read the same pid twice per iteration
  1886. for(slc = 0; slc < all_pids_count; slc++)
  1887. collect_data_for_pid(all_pids_sortlist[slc], NULL);
  1888. }
  1889. #endif
  1890. }
  1891. #ifdef __FreeBSD__
  1892. for (i = 0 ; i < procnum ; ++i) {
  1893. pid_t pid = procbase[i].ki_pid;
  1894. collect_data_for_pid(pid, &procbase[i]);
  1895. }
  1896. #else
  1897. char dirname[FILENAME_MAX + 1];
  1898. snprintfz(dirname, FILENAME_MAX, "%s/proc", netdata_configured_host_prefix);
  1899. DIR *dir = opendir(dirname);
  1900. if(!dir) return 0;
  1901. struct dirent *de = NULL;
  1902. while((de = readdir(dir))) {
  1903. char *endptr = de->d_name;
  1904. if(unlikely(de->d_type != DT_DIR || de->d_name[0] < '0' || de->d_name[0] > '9'))
  1905. continue;
  1906. pid_t pid = (pid_t) strtoul(de->d_name, &endptr, 10);
  1907. // make sure we read a valid number
  1908. if(unlikely(endptr == de->d_name || *endptr != '\0'))
  1909. continue;
  1910. collect_data_for_pid(pid, NULL);
  1911. }
  1912. closedir(dir);
  1913. #endif
  1914. if(!all_pids_count)
  1915. return 0;
  1916. // we need /proc/stat to normalize the cpu consumption of the exited childs
  1917. read_proc_stat();
  1918. // build the process tree
  1919. link_all_processes_to_their_parents();
  1920. // normally this is done
  1921. // however we may have processes exited while we collected values
  1922. // so let's find the exited ones
  1923. // we do this by collecting the ownership of process
  1924. // if we manage to get the ownership, the process still runs
  1925. process_exited_processes();
  1926. return 1;
  1927. }
  1928. // ----------------------------------------------------------------------------
  1929. // update statistics on the targets
  1930. // 1. link all childs to their parents
  1931. // 2. go from bottom to top, marking as merged all childs to their parents
  1932. // this step links all parents without a target to the child target, if any
  1933. // 3. link all top level processes (the ones not merged) to the default target
  1934. // 4. go from top to bottom, linking all childs without a target, to their parent target
  1935. // after this step, all processes have a target
  1936. // [5. for each killed pid (updated = 0), remove its usage from its target]
  1937. // 6. zero all apps_groups_targets
  1938. // 7. concentrate all values on the apps_groups_targets
  1939. // 8. remove all killed processes
  1940. // 9. find the unique file count for each target
  1941. // check: update_apps_groups_statistics()
  1942. static void cleanup_exited_pids(void) {
  1943. size_t c;
  1944. struct pid_stat *p = NULL;
  1945. for(p = root_of_pids; p ;) {
  1946. if(!p->updated && (!p->keep || p->keeploops > 0)) {
  1947. if(unlikely(debug_enabled && (p->keep || p->keeploops)))
  1948. debug_log(" > CLEANUP cannot keep exited process %d (%s) anymore - removing it.", p->pid, p->comm);
  1949. for(c = 0; c < p->fds_size; c++)
  1950. if(p->fds[c].fd > 0) {
  1951. file_descriptor_not_used(p->fds[c].fd);
  1952. clear_pid_fd(&p->fds[c]);
  1953. }
  1954. pid_t r = p->pid;
  1955. p = p->next;
  1956. del_pid_entry(r);
  1957. }
  1958. else {
  1959. if(unlikely(p->keep)) p->keeploops++;
  1960. p->keep = 0;
  1961. p = p->next;
  1962. }
  1963. }
  1964. }
  1965. static void apply_apps_groups_targets_inheritance(void) {
  1966. struct pid_stat *p = NULL;
  1967. // children that do not have a target
  1968. // inherit their target from their parent
  1969. int found = 1, loops = 0;
  1970. while(found) {
  1971. if(unlikely(debug_enabled)) loops++;
  1972. found = 0;
  1973. for(p = root_of_pids; p ; p = p->next) {
  1974. // if this process does not have a target
  1975. // and it has a parent
  1976. // and its parent has a target
  1977. // then, set the parent's target to this process
  1978. if(unlikely(!p->target && p->parent && p->parent->target)) {
  1979. p->target = p->parent->target;
  1980. found++;
  1981. if(debug_enabled || (p->target && p->target->debug_enabled))
  1982. 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);
  1983. }
  1984. }
  1985. }
  1986. // find all the procs with 0 childs and merge them to their parents
  1987. // repeat, until nothing more can be done.
  1988. int sortlist = 1;
  1989. found = 1;
  1990. while(found) {
  1991. if(unlikely(debug_enabled)) loops++;
  1992. found = 0;
  1993. for(p = root_of_pids; p ; p = p->next) {
  1994. if(unlikely(!p->sortlist && !p->children_count))
  1995. p->sortlist = sortlist++;
  1996. if(unlikely(
  1997. !p->children_count // if this process does not have any children
  1998. && !p->merged // and is not already merged
  1999. && p->parent // and has a parent
  2000. && p->parent->children_count // and its parent has children
  2001. // and the target of this process and its parent is the same,
  2002. // or the parent does not have a target
  2003. && (p->target == p->parent->target || !p->parent->target)
  2004. && p->ppid != INIT_PID // and its parent is not init
  2005. )) {
  2006. // mark it as merged
  2007. p->parent->children_count--;
  2008. p->merged = 1;
  2009. // the parent inherits the child's target, if it does not have a target itself
  2010. if(unlikely(p->target && !p->parent->target)) {
  2011. p->parent->target = p->target;
  2012. if(debug_enabled || (p->target && p->target->debug_enabled))
  2013. 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);
  2014. }
  2015. found++;
  2016. }
  2017. }
  2018. debug_log("TARGET INHERITANCE: merged %d processes", found);
  2019. }
  2020. // init goes always to default target
  2021. if(all_pids[INIT_PID])
  2022. all_pids[INIT_PID]->target = apps_groups_default_target;
  2023. // pid 0 goes always to default target
  2024. if(all_pids[0])
  2025. all_pids[0]->target = apps_groups_default_target;
  2026. // give a default target on all top level processes
  2027. if(unlikely(debug_enabled)) loops++;
  2028. for(p = root_of_pids; p ; p = p->next) {
  2029. // if the process is not merged itself
  2030. // then is is a top level process
  2031. if(unlikely(!p->merged && !p->target))
  2032. p->target = apps_groups_default_target;
  2033. // make sure all processes have a sortlist
  2034. if(unlikely(!p->sortlist))
  2035. p->sortlist = sortlist++;
  2036. }
  2037. if(all_pids[1])
  2038. all_pids[1]->sortlist = sortlist++;
  2039. // give a target to all merged child processes
  2040. found = 1;
  2041. while(found) {
  2042. if(unlikely(debug_enabled)) loops++;
  2043. found = 0;
  2044. for(p = root_of_pids; p ; p = p->next) {
  2045. if(unlikely(!p->target && p->merged && p->parent && p->parent->target)) {
  2046. p->target = p->parent->target;
  2047. found++;
  2048. if(debug_enabled || (p->target && p->target->debug_enabled))
  2049. 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);
  2050. }
  2051. }
  2052. }
  2053. debug_log("apply_apps_groups_targets_inheritance() made %d loops on the process tree", loops);
  2054. }
  2055. static size_t zero_all_targets(struct target *root) {
  2056. struct target *w;
  2057. size_t count = 0;
  2058. for (w = root; w ; w = w->next) {
  2059. count++;
  2060. w->minflt = 0;
  2061. w->majflt = 0;
  2062. w->utime = 0;
  2063. w->stime = 0;
  2064. w->gtime = 0;
  2065. w->cminflt = 0;
  2066. w->cmajflt = 0;
  2067. w->cutime = 0;
  2068. w->cstime = 0;
  2069. w->cgtime = 0;
  2070. w->num_threads = 0;
  2071. // w->rss = 0;
  2072. w->processes = 0;
  2073. w->status_vmsize = 0;
  2074. w->status_vmrss = 0;
  2075. w->status_vmshared = 0;
  2076. w->status_rssfile = 0;
  2077. w->status_rssshmem = 0;
  2078. w->status_vmswap = 0;
  2079. w->io_logical_bytes_read = 0;
  2080. w->io_logical_bytes_written = 0;
  2081. // w->io_read_calls = 0;
  2082. // w->io_write_calls = 0;
  2083. w->io_storage_bytes_read = 0;
  2084. w->io_storage_bytes_written = 0;
  2085. // w->io_cancelled_write_bytes = 0;
  2086. // zero file counters
  2087. if(w->target_fds) {
  2088. memset(w->target_fds, 0, sizeof(int) * w->target_fds_size);
  2089. w->openfiles = 0;
  2090. w->openpipes = 0;
  2091. w->opensockets = 0;
  2092. w->openinotifies = 0;
  2093. w->openeventfds = 0;
  2094. w->opentimerfds = 0;
  2095. w->opensignalfds = 0;
  2096. w->openeventpolls = 0;
  2097. w->openother = 0;
  2098. }
  2099. }
  2100. return count;
  2101. }
  2102. static inline void reallocate_target_fds(struct target *w) {
  2103. if(unlikely(!w))
  2104. return;
  2105. if(unlikely(!w->target_fds || w->target_fds_size < all_files_size)) {
  2106. w->target_fds = reallocz(w->target_fds, sizeof(int) * all_files_size);
  2107. memset(&w->target_fds[w->target_fds_size], 0, sizeof(int) * (all_files_size - w->target_fds_size));
  2108. w->target_fds_size = all_files_size;
  2109. }
  2110. }
  2111. static inline void aggregate_fd_on_target(int fd, struct target *w) {
  2112. if(unlikely(!w))
  2113. return;
  2114. if(unlikely(w->target_fds[fd])) {
  2115. // it is already aggregated
  2116. // just increase its usage counter
  2117. w->target_fds[fd]++;
  2118. return;
  2119. }
  2120. // increase its usage counter
  2121. // so that we will not add it again
  2122. w->target_fds[fd]++;
  2123. switch(all_files[fd].type) {
  2124. case FILETYPE_FILE:
  2125. w->openfiles++;
  2126. break;
  2127. case FILETYPE_PIPE:
  2128. w->openpipes++;
  2129. break;
  2130. case FILETYPE_SOCKET:
  2131. w->opensockets++;
  2132. break;
  2133. case FILETYPE_INOTIFY:
  2134. w->openinotifies++;
  2135. break;
  2136. case FILETYPE_EVENTFD:
  2137. w->openeventfds++;
  2138. break;
  2139. case FILETYPE_TIMERFD:
  2140. w->opentimerfds++;
  2141. break;
  2142. case FILETYPE_SIGNALFD:
  2143. w->opensignalfds++;
  2144. break;
  2145. case FILETYPE_EVENTPOLL:
  2146. w->openeventpolls++;
  2147. break;
  2148. case FILETYPE_OTHER:
  2149. w->openother++;
  2150. break;
  2151. }
  2152. }
  2153. static inline void aggregate_pid_fds_on_targets(struct pid_stat *p) {
  2154. if(unlikely(!p->updated)) {
  2155. // the process is not running
  2156. return;
  2157. }
  2158. struct target *w = p->target, *u = p->user_target, *g = p->group_target;
  2159. reallocate_target_fds(w);
  2160. reallocate_target_fds(u);
  2161. reallocate_target_fds(g);
  2162. size_t c, size = p->fds_size;
  2163. struct pid_fd *fds = p->fds;
  2164. for(c = 0; c < size ;c++) {
  2165. int fd = fds[c].fd;
  2166. if(likely(fd <= 0 || fd >= all_files_size))
  2167. continue;
  2168. aggregate_fd_on_target(fd, w);
  2169. aggregate_fd_on_target(fd, u);
  2170. aggregate_fd_on_target(fd, g);
  2171. }
  2172. }
  2173. static inline void aggregate_pid_on_target(struct target *w, struct pid_stat *p, struct target *o) {
  2174. (void)o;
  2175. if(unlikely(!p->updated)) {
  2176. // the process is not running
  2177. return;
  2178. }
  2179. if(unlikely(!w)) {
  2180. error("pid %d %s was left without a target!", p->pid, p->comm);
  2181. return;
  2182. }
  2183. w->cutime += p->cutime;
  2184. w->cstime += p->cstime;
  2185. w->cgtime += p->cgtime;
  2186. w->cminflt += p->cminflt;
  2187. w->cmajflt += p->cmajflt;
  2188. w->utime += p->utime;
  2189. w->stime += p->stime;
  2190. w->gtime += p->gtime;
  2191. w->minflt += p->minflt;
  2192. w->majflt += p->majflt;
  2193. // w->rss += p->rss;
  2194. w->status_vmsize += p->status_vmsize;
  2195. w->status_vmrss += p->status_vmrss;
  2196. w->status_vmshared += p->status_vmshared;
  2197. w->status_rssfile += p->status_rssfile;
  2198. w->status_rssshmem += p->status_rssshmem;
  2199. w->status_vmswap += p->status_vmswap;
  2200. w->io_logical_bytes_read += p->io_logical_bytes_read;
  2201. w->io_logical_bytes_written += p->io_logical_bytes_written;
  2202. // w->io_read_calls += p->io_read_calls;
  2203. // w->io_write_calls += p->io_write_calls;
  2204. w->io_storage_bytes_read += p->io_storage_bytes_read;
  2205. w->io_storage_bytes_written += p->io_storage_bytes_written;
  2206. // w->io_cancelled_write_bytes += p->io_cancelled_write_bytes;
  2207. w->processes++;
  2208. w->num_threads += p->num_threads;
  2209. if(unlikely(debug_enabled || w->debug_enabled))
  2210. 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);
  2211. }
  2212. static void calculate_netdata_statistics(void) {
  2213. apply_apps_groups_targets_inheritance();
  2214. zero_all_targets(users_root_target);
  2215. zero_all_targets(groups_root_target);
  2216. apps_groups_targets_count = zero_all_targets(apps_groups_root_target);
  2217. // this has to be done, before the cleanup
  2218. struct pid_stat *p = NULL;
  2219. struct target *w = NULL, *o = NULL;
  2220. // concentrate everything on the targets
  2221. for(p = root_of_pids; p ; p = p->next) {
  2222. // --------------------------------------------------------------------
  2223. // apps_groups target
  2224. aggregate_pid_on_target(p->target, p, NULL);
  2225. // --------------------------------------------------------------------
  2226. // user target
  2227. o = p->user_target;
  2228. if(likely(p->user_target && p->user_target->uid == p->uid))
  2229. w = p->user_target;
  2230. else {
  2231. if(unlikely(debug_enabled && p->user_target))
  2232. 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);
  2233. w = p->user_target = get_users_target(p->uid);
  2234. }
  2235. aggregate_pid_on_target(w, p, o);
  2236. // --------------------------------------------------------------------
  2237. // user group target
  2238. o = p->group_target;
  2239. if(likely(p->group_target && p->group_target->gid == p->gid))
  2240. w = p->group_target;
  2241. else {
  2242. if(unlikely(debug_enabled && p->group_target))
  2243. 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);
  2244. w = p->group_target = get_groups_target(p->gid);
  2245. }
  2246. aggregate_pid_on_target(w, p, o);
  2247. // --------------------------------------------------------------------
  2248. // aggregate all file descriptors
  2249. if(enable_file_charts)
  2250. aggregate_pid_fds_on_targets(p);
  2251. }
  2252. cleanup_exited_pids();
  2253. }
  2254. // ----------------------------------------------------------------------------
  2255. // update chart dimensions
  2256. static inline void send_BEGIN(const char *type, const char *id, usec_t usec) {
  2257. fprintf(stdout, "BEGIN %s.%s %llu\n", type, id, usec);
  2258. }
  2259. static inline void send_SET(const char *name, kernel_uint_t value) {
  2260. fprintf(stdout, "SET %s = " KERNEL_UINT_FORMAT "\n", name, value);
  2261. }
  2262. static inline void send_END(void) {
  2263. fprintf(stdout, "END\n");
  2264. }
  2265. void send_resource_usage_to_netdata(usec_t dt) {
  2266. static struct timeval last = { 0, 0 };
  2267. static struct rusage me_last;
  2268. struct timeval now;
  2269. struct rusage me;
  2270. usec_t cpuuser;
  2271. usec_t cpusyst;
  2272. if(!last.tv_sec) {
  2273. now_monotonic_timeval(&last);
  2274. getrusage(RUSAGE_SELF, &me_last);
  2275. cpuuser = 0;
  2276. cpusyst = 0;
  2277. }
  2278. else {
  2279. now_monotonic_timeval(&now);
  2280. getrusage(RUSAGE_SELF, &me);
  2281. cpuuser = me.ru_utime.tv_sec * USEC_PER_SEC + me.ru_utime.tv_usec;
  2282. cpusyst = me.ru_stime.tv_sec * USEC_PER_SEC + me.ru_stime.tv_usec;
  2283. memmove(&last, &now, sizeof(struct timeval));
  2284. memmove(&me_last, &me, sizeof(struct rusage));
  2285. }
  2286. static char created_charts = 0;
  2287. if(unlikely(!created_charts)) {
  2288. created_charts = 1;
  2289. fprintf(stdout,
  2290. "CHART netdata.apps_cpu '' 'Apps Plugin CPU' 'milliseconds/s' apps.plugin netdata.apps_cpu stacked 140000 %1$d\n"
  2291. "DIMENSION user '' incremental 1 1000\n"
  2292. "DIMENSION system '' incremental 1 1000\n"
  2293. "CHART netdata.apps_sizes '' 'Apps Plugin Files' 'files/s' apps.plugin netdata.apps_sizes line 140001 %1$d\n"
  2294. "DIMENSION calls '' incremental 1 1\n"
  2295. "DIMENSION files '' incremental 1 1\n"
  2296. "DIMENSION filenames '' incremental 1 1\n"
  2297. "DIMENSION inode_changes '' incremental 1 1\n"
  2298. "DIMENSION link_changes '' incremental 1 1\n"
  2299. "DIMENSION pids '' absolute 1 1\n"
  2300. "DIMENSION fds '' absolute 1 1\n"
  2301. "DIMENSION targets '' absolute 1 1\n"
  2302. "DIMENSION new_pids 'new pids' incremental 1 1\n"
  2303. , update_every
  2304. );
  2305. #if (ALL_PIDS_ARE_READ_INSTANTLY == 0)
  2306. fprintf(stdout,
  2307. "CHART netdata.apps_fix '' 'Apps Plugin Normalization Ratios' 'percentage' apps.plugin netdata.apps_fix line 140002 %1$d\n"
  2308. "DIMENSION utime '' absolute 1 %2$llu\n"
  2309. "DIMENSION stime '' absolute 1 %2$llu\n"
  2310. "DIMENSION gtime '' absolute 1 %2$llu\n"
  2311. "DIMENSION minflt '' absolute 1 %2$llu\n"
  2312. "DIMENSION majflt '' absolute 1 %2$llu\n"
  2313. , update_every
  2314. , RATES_DETAIL
  2315. );
  2316. if(include_exited_childs)
  2317. fprintf(stdout,
  2318. "CHART netdata.apps_children_fix '' 'Apps Plugin Exited Children Normalization Ratios' 'percentage' apps.plugin netdata.apps_children_fix line 140003 %1$d\n"
  2319. "DIMENSION cutime '' absolute 1 %2$llu\n"
  2320. "DIMENSION cstime '' absolute 1 %2$llu\n"
  2321. "DIMENSION cgtime '' absolute 1 %2$llu\n"
  2322. "DIMENSION cminflt '' absolute 1 %2$llu\n"
  2323. "DIMENSION cmajflt '' absolute 1 %2$llu\n"
  2324. , update_every
  2325. , RATES_DETAIL
  2326. );
  2327. #endif
  2328. }
  2329. fprintf(stdout,
  2330. "BEGIN netdata.apps_cpu %llu\n"
  2331. "SET user = %llu\n"
  2332. "SET system = %llu\n"
  2333. "END\n"
  2334. "BEGIN netdata.apps_sizes %llu\n"
  2335. "SET calls = %zu\n"
  2336. "SET files = %zu\n"
  2337. "SET filenames = %zu\n"
  2338. "SET inode_changes = %zu\n"
  2339. "SET link_changes = %zu\n"
  2340. "SET pids = %zu\n"
  2341. "SET fds = %d\n"
  2342. "SET targets = %zu\n"
  2343. "SET new_pids = %zu\n"
  2344. "END\n"
  2345. , dt
  2346. , cpuuser
  2347. , cpusyst
  2348. , dt
  2349. , calls_counter
  2350. , file_counter
  2351. , filenames_allocated_counter
  2352. , inodes_changed_counter
  2353. , links_changed_counter
  2354. , all_pids_count
  2355. , all_files_len
  2356. , apps_groups_targets_count
  2357. , targets_assignment_counter
  2358. );
  2359. #if (ALL_PIDS_ARE_READ_INSTANTLY == 0)
  2360. fprintf(stdout,
  2361. "BEGIN netdata.apps_fix %llu\n"
  2362. "SET utime = %u\n"
  2363. "SET stime = %u\n"
  2364. "SET gtime = %u\n"
  2365. "SET minflt = %u\n"
  2366. "SET majflt = %u\n"
  2367. "END\n"
  2368. , dt
  2369. , (unsigned int)(utime_fix_ratio * 100 * RATES_DETAIL)
  2370. , (unsigned int)(stime_fix_ratio * 100 * RATES_DETAIL)
  2371. , (unsigned int)(gtime_fix_ratio * 100 * RATES_DETAIL)
  2372. , (unsigned int)(minflt_fix_ratio * 100 * RATES_DETAIL)
  2373. , (unsigned int)(majflt_fix_ratio * 100 * RATES_DETAIL)
  2374. );
  2375. if(include_exited_childs)
  2376. fprintf(stdout,
  2377. "BEGIN netdata.apps_children_fix %llu\n"
  2378. "SET cutime = %u\n"
  2379. "SET cstime = %u\n"
  2380. "SET cgtime = %u\n"
  2381. "SET cminflt = %u\n"
  2382. "SET cmajflt = %u\n"
  2383. "END\n"
  2384. , dt
  2385. , (unsigned int)(cutime_fix_ratio * 100 * RATES_DETAIL)
  2386. , (unsigned int)(cstime_fix_ratio * 100 * RATES_DETAIL)
  2387. , (unsigned int)(cgtime_fix_ratio * 100 * RATES_DETAIL)
  2388. , (unsigned int)(cminflt_fix_ratio * 100 * RATES_DETAIL)
  2389. , (unsigned int)(cmajflt_fix_ratio * 100 * RATES_DETAIL)
  2390. );
  2391. #endif
  2392. }
  2393. #if (ALL_PIDS_ARE_READ_INSTANTLY == 0)
  2394. static void normalize_utilization(struct target *root) {
  2395. struct target *w;
  2396. // childs processing introduces spikes
  2397. // here we try to eliminate them by disabling childs processing either for specific dimensions
  2398. // or entirely. Of course, either way, we disable it just a single iteration.
  2399. kernel_uint_t max_time = processors * system_hz * RATES_DETAIL;
  2400. kernel_uint_t utime = 0, cutime = 0, stime = 0, cstime = 0, gtime = 0, cgtime = 0, minflt = 0, cminflt = 0, majflt = 0, cmajflt = 0;
  2401. if(global_utime > max_time) global_utime = max_time;
  2402. if(global_stime > max_time) global_stime = max_time;
  2403. if(global_gtime > max_time) global_gtime = max_time;
  2404. for(w = root; w ; w = w->next) {
  2405. if(w->target || (!w->processes && !w->exposed)) continue;
  2406. utime += w->utime;
  2407. stime += w->stime;
  2408. gtime += w->gtime;
  2409. cutime += w->cutime;
  2410. cstime += w->cstime;
  2411. cgtime += w->cgtime;
  2412. minflt += w->minflt;
  2413. majflt += w->majflt;
  2414. cminflt += w->cminflt;
  2415. cmajflt += w->cmajflt;
  2416. }
  2417. if((global_utime || global_stime || global_gtime) && (utime || stime || gtime)) {
  2418. if(global_utime + global_stime + global_gtime > utime + cutime + stime + cstime + gtime + cgtime) {
  2419. // everything we collected fits
  2420. utime_fix_ratio =
  2421. stime_fix_ratio =
  2422. gtime_fix_ratio =
  2423. cutime_fix_ratio =
  2424. cstime_fix_ratio =
  2425. cgtime_fix_ratio = 1.0; //(double)(global_utime + global_stime) / (double)(utime + cutime + stime + cstime);
  2426. }
  2427. else if(global_utime + global_stime > utime + stime) {
  2428. // childrens resources are too high
  2429. // lower only the children resources
  2430. utime_fix_ratio =
  2431. stime_fix_ratio =
  2432. gtime_fix_ratio = 1.0;
  2433. cutime_fix_ratio =
  2434. cstime_fix_ratio =
  2435. cgtime_fix_ratio = (double)((global_utime + global_stime) - (utime + stime)) / (double)(cutime + cstime);
  2436. }
  2437. else {
  2438. // even running processes are unrealistic
  2439. // zero the children resources
  2440. // lower the running processes resources
  2441. utime_fix_ratio =
  2442. stime_fix_ratio =
  2443. gtime_fix_ratio = (double)(global_utime + global_stime) / (double)(utime + stime);
  2444. cutime_fix_ratio =
  2445. cstime_fix_ratio =
  2446. cgtime_fix_ratio = 0.0;
  2447. }
  2448. }
  2449. else {
  2450. utime_fix_ratio =
  2451. stime_fix_ratio =
  2452. gtime_fix_ratio =
  2453. cutime_fix_ratio =
  2454. cstime_fix_ratio =
  2455. cgtime_fix_ratio = 0.0;
  2456. }
  2457. if(utime_fix_ratio > 1.0) utime_fix_ratio = 1.0;
  2458. if(cutime_fix_ratio > 1.0) cutime_fix_ratio = 1.0;
  2459. if(stime_fix_ratio > 1.0) stime_fix_ratio = 1.0;
  2460. if(cstime_fix_ratio > 1.0) cstime_fix_ratio = 1.0;
  2461. if(gtime_fix_ratio > 1.0) gtime_fix_ratio = 1.0;
  2462. if(cgtime_fix_ratio > 1.0) cgtime_fix_ratio = 1.0;
  2463. // if(utime_fix_ratio < 0.0) utime_fix_ratio = 0.0;
  2464. // if(cutime_fix_ratio < 0.0) cutime_fix_ratio = 0.0;
  2465. // if(stime_fix_ratio < 0.0) stime_fix_ratio = 0.0;
  2466. // if(cstime_fix_ratio < 0.0) cstime_fix_ratio = 0.0;
  2467. // if(gtime_fix_ratio < 0.0) gtime_fix_ratio = 0.0;
  2468. // if(cgtime_fix_ratio < 0.0) cgtime_fix_ratio = 0.0;
  2469. // TODO
  2470. // we use cpu time to normalize page faults
  2471. // the problem is that to find the proper max values
  2472. // for page faults we have to parse /proc/vmstat
  2473. // which is quite big to do it again (netdata does it already)
  2474. //
  2475. // a better solution could be to somehow have netdata
  2476. // do this normalization for us
  2477. if(utime || stime || gtime)
  2478. majflt_fix_ratio =
  2479. minflt_fix_ratio = (double)(utime * utime_fix_ratio + stime * stime_fix_ratio + gtime * gtime_fix_ratio) / (double)(utime + stime + gtime);
  2480. else
  2481. minflt_fix_ratio =
  2482. majflt_fix_ratio = 1.0;
  2483. if(cutime || cstime || cgtime)
  2484. cmajflt_fix_ratio =
  2485. cminflt_fix_ratio = (double)(cutime * cutime_fix_ratio + cstime * cstime_fix_ratio + cgtime * cgtime_fix_ratio) / (double)(cutime + cstime + cgtime);
  2486. else
  2487. cminflt_fix_ratio =
  2488. cmajflt_fix_ratio = 1.0;
  2489. // the report
  2490. debug_log(
  2491. "SYSTEM: u=" KERNEL_UINT_FORMAT " s=" KERNEL_UINT_FORMAT " g=" KERNEL_UINT_FORMAT " "
  2492. "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 " "
  2493. "DELTA: u=" KERNEL_UINT_FORMAT " s=" KERNEL_UINT_FORMAT " g=" KERNEL_UINT_FORMAT " "
  2494. "FIX: u=%0.2f s=%0.2f g=%0.2f cu=%0.2f cs=%0.2f cg=%0.2f "
  2495. "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 " "
  2496. , global_utime
  2497. , global_stime
  2498. , global_gtime
  2499. , utime
  2500. , stime
  2501. , gtime
  2502. , cutime
  2503. , cstime
  2504. , cgtime
  2505. , utime + cutime - global_utime
  2506. , stime + cstime - global_stime
  2507. , gtime + cgtime - global_gtime
  2508. , utime_fix_ratio
  2509. , stime_fix_ratio
  2510. , gtime_fix_ratio
  2511. , cutime_fix_ratio
  2512. , cstime_fix_ratio
  2513. , cgtime_fix_ratio
  2514. , (kernel_uint_t)(utime * utime_fix_ratio)
  2515. , (kernel_uint_t)(stime * stime_fix_ratio)
  2516. , (kernel_uint_t)(gtime * gtime_fix_ratio)
  2517. , (kernel_uint_t)(cutime * cutime_fix_ratio)
  2518. , (kernel_uint_t)(cstime * cstime_fix_ratio)
  2519. , (kernel_uint_t)(cgtime * cgtime_fix_ratio)
  2520. );
  2521. }
  2522. #else // ALL_PIDS_ARE_READ_INSTANTLY == 1
  2523. static void normalize_utilization(struct target *root) {
  2524. (void)root;
  2525. }
  2526. #endif // ALL_PIDS_ARE_READ_INSTANTLY
  2527. static void send_collected_data_to_netdata(struct target *root, const char *type, usec_t dt) {
  2528. struct target *w;
  2529. send_BEGIN(type, "cpu", dt);
  2530. for (w = root; w ; w = w->next) {
  2531. if(unlikely(w->exposed))
  2532. send_SET(w->name, (kernel_uint_t)(w->utime * utime_fix_ratio) + (kernel_uint_t)(w->stime * stime_fix_ratio) + (kernel_uint_t)(w->gtime * gtime_fix_ratio) + (include_exited_childs?((kernel_uint_t)(w->cutime * cutime_fix_ratio) + (kernel_uint_t)(w->cstime * cstime_fix_ratio) + (kernel_uint_t)(w->cgtime * cgtime_fix_ratio)):0ULL));
  2533. }
  2534. send_END();
  2535. send_BEGIN(type, "cpu_user", dt);
  2536. for (w = root; w ; w = w->next) {
  2537. if(unlikely(w->exposed))
  2538. send_SET(w->name, (kernel_uint_t)(w->utime * utime_fix_ratio) + (include_exited_childs?((kernel_uint_t)(w->cutime * cutime_fix_ratio)):0ULL));
  2539. }
  2540. send_END();
  2541. send_BEGIN(type, "cpu_system", dt);
  2542. for (w = root; w ; w = w->next) {
  2543. if(unlikely(w->exposed))
  2544. send_SET(w->name, (kernel_uint_t)(w->stime * stime_fix_ratio) + (include_exited_childs?((kernel_uint_t)(w->cstime * cstime_fix_ratio)):0ULL));
  2545. }
  2546. send_END();
  2547. if(show_guest_time) {
  2548. send_BEGIN(type, "cpu_guest", dt);
  2549. for (w = root; w ; w = w->next) {
  2550. if(unlikely(w->exposed))
  2551. send_SET(w->name, (kernel_uint_t)(w->gtime * gtime_fix_ratio) + (include_exited_childs?((kernel_uint_t)(w->cgtime * cgtime_fix_ratio)):0ULL));
  2552. }
  2553. send_END();
  2554. }
  2555. send_BEGIN(type, "threads", dt);
  2556. for (w = root; w ; w = w->next) {
  2557. if(unlikely(w->exposed))
  2558. send_SET(w->name, w->num_threads);
  2559. }
  2560. send_END();
  2561. send_BEGIN(type, "processes", dt);
  2562. for (w = root; w ; w = w->next) {
  2563. if(unlikely(w->exposed))
  2564. send_SET(w->name, w->processes);
  2565. }
  2566. send_END();
  2567. send_BEGIN(type, "mem", dt);
  2568. for (w = root; w ; w = w->next) {
  2569. if(unlikely(w->exposed))
  2570. send_SET(w->name, (w->status_vmrss > w->status_vmshared)?(w->status_vmrss - w->status_vmshared):0ULL);
  2571. }
  2572. send_END();
  2573. send_BEGIN(type, "vmem", dt);
  2574. for (w = root; w ; w = w->next) {
  2575. if(unlikely(w->exposed))
  2576. send_SET(w->name, w->status_vmsize);
  2577. }
  2578. send_END();
  2579. #ifndef __FreeBSD__
  2580. send_BEGIN(type, "swap", dt);
  2581. for (w = root; w ; w = w->next) {
  2582. if(unlikely(w->exposed))
  2583. send_SET(w->name, w->status_vmswap);
  2584. }
  2585. send_END();
  2586. #endif
  2587. send_BEGIN(type, "minor_faults", dt);
  2588. for (w = root; w ; w = w->next) {
  2589. if(unlikely(w->exposed))
  2590. send_SET(w->name, (kernel_uint_t)(w->minflt * minflt_fix_ratio) + (include_exited_childs?((kernel_uint_t)(w->cminflt * cminflt_fix_ratio)):0ULL));
  2591. }
  2592. send_END();
  2593. send_BEGIN(type, "major_faults", dt);
  2594. for (w = root; w ; w = w->next) {
  2595. if(unlikely(w->exposed))
  2596. send_SET(w->name, (kernel_uint_t)(w->majflt * majflt_fix_ratio) + (include_exited_childs?((kernel_uint_t)(w->cmajflt * cmajflt_fix_ratio)):0ULL));
  2597. }
  2598. send_END();
  2599. #ifndef __FreeBSD__
  2600. send_BEGIN(type, "lreads", dt);
  2601. for (w = root; w ; w = w->next) {
  2602. if(unlikely(w->exposed))
  2603. send_SET(w->name, w->io_logical_bytes_read);
  2604. }
  2605. send_END();
  2606. send_BEGIN(type, "lwrites", dt);
  2607. for (w = root; w ; w = w->next) {
  2608. if(unlikely(w->exposed))
  2609. send_SET(w->name, w->io_logical_bytes_written);
  2610. }
  2611. send_END();
  2612. #endif
  2613. send_BEGIN(type, "preads", dt);
  2614. for (w = root; w ; w = w->next) {
  2615. if(unlikely(w->exposed))
  2616. send_SET(w->name, w->io_storage_bytes_read);
  2617. }
  2618. send_END();
  2619. send_BEGIN(type, "pwrites", dt);
  2620. for (w = root; w ; w = w->next) {
  2621. if(unlikely(w->exposed))
  2622. send_SET(w->name, w->io_storage_bytes_written);
  2623. }
  2624. send_END();
  2625. if(enable_file_charts) {
  2626. send_BEGIN(type, "files", dt);
  2627. for (w = root; w; w = w->next) {
  2628. if (unlikely(w->exposed))
  2629. send_SET(w->name, w->openfiles);
  2630. }
  2631. send_END();
  2632. send_BEGIN(type, "sockets", dt);
  2633. for (w = root; w; w = w->next) {
  2634. if (unlikely(w->exposed))
  2635. send_SET(w->name, w->opensockets);
  2636. }
  2637. send_END();
  2638. send_BEGIN(type, "pipes", dt);
  2639. for (w = root; w; w = w->next) {
  2640. if (unlikely(w->exposed))
  2641. send_SET(w->name, w->openpipes);
  2642. }
  2643. send_END();
  2644. }
  2645. }
  2646. // ----------------------------------------------------------------------------
  2647. // generate the charts
  2648. static void send_charts_updates_to_netdata(struct target *root, const char *type, const char *title)
  2649. {
  2650. struct target *w;
  2651. int newly_added = 0;
  2652. for(w = root ; w ; w = w->next) {
  2653. if (w->target) continue;
  2654. if (!w->exposed && w->processes) {
  2655. newly_added++;
  2656. w->exposed = 1;
  2657. if (debug_enabled || w->debug_enabled)
  2658. debug_log_int("%s just added - regenerating charts.", w->name);
  2659. }
  2660. }
  2661. // nothing more to show
  2662. if(!newly_added && show_guest_time == show_guest_time_old) return;
  2663. // we have something new to show
  2664. // update the charts
  2665. fprintf(stdout, "CHART %s.cpu '' '%s CPU Time (%d%% = %d core%s)' 'cpu time %%' cpu %s.cpu stacked 20001 %d\n", type, title, (processors * 100), processors, (processors>1)?"s":"", type, update_every);
  2666. for (w = root; w ; w = w->next) {
  2667. if(unlikely(w->exposed))
  2668. fprintf(stdout, "DIMENSION %s '' absolute 1 %llu %s\n", w->name, system_hz * RATES_DETAIL / 100, w->hidden ? "hidden" : "");
  2669. }
  2670. fprintf(stdout, "CHART %s.mem '' '%s Real Memory (w/o shared)' 'MB' mem %s.mem stacked 20003 %d\n", type, title, type, update_every);
  2671. for (w = root; w ; w = w->next) {
  2672. if(unlikely(w->exposed))
  2673. fprintf(stdout, "DIMENSION %s '' absolute %ld %ld\n", w->name, 1L, 1024L);
  2674. }
  2675. fprintf(stdout, "CHART %s.vmem '' '%s Virtual Memory Size' 'MB' mem %s.vmem stacked 20005 %d\n", type, title, type, update_every);
  2676. for (w = root; w ; w = w->next) {
  2677. if(unlikely(w->exposed))
  2678. fprintf(stdout, "DIMENSION %s '' absolute %ld %ld\n", w->name, 1L, 1024L);
  2679. }
  2680. fprintf(stdout, "CHART %s.threads '' '%s Threads' 'threads' processes %s.threads stacked 20006 %d\n", type, title, type, update_every);
  2681. for (w = root; w ; w = w->next) {
  2682. if(unlikely(w->exposed))
  2683. fprintf(stdout, "DIMENSION %s '' absolute 1 1\n", w->name);
  2684. }
  2685. fprintf(stdout, "CHART %s.processes '' '%s Processes' 'processes' processes %s.processes stacked 20007 %d\n", type, title, type, update_every);
  2686. for (w = root; w ; w = w->next) {
  2687. if(unlikely(w->exposed))
  2688. fprintf(stdout, "DIMENSION %s '' absolute 1 1\n", w->name);
  2689. }
  2690. fprintf(stdout, "CHART %s.cpu_user '' '%s CPU User Time (%d%% = %d core%s)' 'cpu time %%' cpu %s.cpu_user stacked 20020 %d\n", type, title, (processors * 100), processors, (processors>1)?"s":"", type, update_every);
  2691. for (w = root; w ; w = w->next) {
  2692. if(unlikely(w->exposed))
  2693. fprintf(stdout, "DIMENSION %s '' absolute 1 %llu\n", w->name, system_hz * RATES_DETAIL / 100LLU);
  2694. }
  2695. fprintf(stdout, "CHART %s.cpu_system '' '%s CPU System Time (%d%% = %d core%s)' 'cpu time %%' cpu %s.cpu_system stacked 20021 %d\n", type, title, (processors * 100), processors, (processors>1)?"s":"", type, update_every);
  2696. for (w = root; w ; w = w->next) {
  2697. if(unlikely(w->exposed))
  2698. fprintf(stdout, "DIMENSION %s '' absolute 1 %llu\n", w->name, system_hz * RATES_DETAIL / 100LLU);
  2699. }
  2700. if(show_guest_time) {
  2701. fprintf(stdout, "CHART %s.cpu_guest '' '%s CPU Guest Time (%d%% = %d core%s)' 'cpu time %%' cpu %s.cpu_system stacked 20022 %d\n", type, title, (processors * 100), processors, (processors > 1) ? "s" : "", type, update_every);
  2702. for (w = root; w; w = w->next) {
  2703. if(unlikely(w->exposed))
  2704. fprintf(stdout, "DIMENSION %s '' absolute 1 %llu\n", w->name, system_hz * RATES_DETAIL / 100LLU);
  2705. }
  2706. }
  2707. #ifndef __FreeBSD__
  2708. fprintf(stdout, "CHART %s.swap '' '%s Swap Memory' 'MB' swap %s.swap stacked 20011 %d\n", type, title, type, update_every);
  2709. for (w = root; w ; w = w->next) {
  2710. if(unlikely(w->exposed))
  2711. fprintf(stdout, "DIMENSION %s '' absolute %ld %ld\n", w->name, 1L, 1024L);
  2712. }
  2713. #endif
  2714. fprintf(stdout, "CHART %s.major_faults '' '%s Major Page Faults (swap read)' 'page faults/s' swap %s.major_faults stacked 20012 %d\n", type, title, type, update_every);
  2715. for (w = root; w ; w = w->next) {
  2716. if(unlikely(w->exposed))
  2717. fprintf(stdout, "DIMENSION %s '' absolute 1 %llu\n", w->name, RATES_DETAIL);
  2718. }
  2719. fprintf(stdout, "CHART %s.minor_faults '' '%s Minor Page Faults' 'page faults/s' mem %s.minor_faults stacked 20011 %d\n", type, title, type, update_every);
  2720. for (w = root; w ; w = w->next) {
  2721. if(unlikely(w->exposed))
  2722. fprintf(stdout, "DIMENSION %s '' absolute 1 %llu\n", w->name, RATES_DETAIL);
  2723. }
  2724. #ifdef __FreeBSD__
  2725. fprintf(stdout, "CHART %s.preads '' '%s Disk Reads' 'blocks/s' disk %s.preads stacked 20002 %d\n", type, title, type, update_every);
  2726. for (w = root; w ; w = w->next) {
  2727. if(unlikely(w->exposed))
  2728. fprintf(stdout, "DIMENSION %s '' absolute 1 %llu\n", w->name, RATES_DETAIL);
  2729. }
  2730. fprintf(stdout, "CHART %s.pwrites '' '%s Disk Writes' 'blocks/s' disk %s.pwrites stacked 20002 %d\n", type, title, type, update_every);
  2731. for (w = root; w ; w = w->next) {
  2732. if(unlikely(w->exposed))
  2733. fprintf(stdout, "DIMENSION %s '' absolute 1 %llu\n", w->name, RATES_DETAIL);
  2734. }
  2735. #else
  2736. fprintf(stdout, "CHART %s.preads '' '%s Disk Reads' 'kilobytes/s' disk %s.preads stacked 20002 %d\n", type, title, type, update_every);
  2737. for (w = root; w ; w = w->next) {
  2738. if(unlikely(w->exposed))
  2739. fprintf(stdout, "DIMENSION %s '' absolute 1 %llu\n", w->name, 1024LLU * RATES_DETAIL);
  2740. }
  2741. fprintf(stdout, "CHART %s.pwrites '' '%s Disk Writes' 'kilobytes/s' disk %s.pwrites stacked 20002 %d\n", type, title, type, update_every);
  2742. for (w = root; w ; w = w->next) {
  2743. if(unlikely(w->exposed))
  2744. fprintf(stdout, "DIMENSION %s '' absolute 1 %llu\n", w->name, 1024LLU * RATES_DETAIL);
  2745. }
  2746. fprintf(stdout, "CHART %s.lreads '' '%s Disk Logical Reads' 'kilobytes/s' disk %s.lreads stacked 20042 %d\n", type, title, type, update_every);
  2747. for (w = root; w ; w = w->next) {
  2748. if(unlikely(w->exposed))
  2749. fprintf(stdout, "DIMENSION %s '' absolute 1 %llu\n", w->name, 1024LLU * RATES_DETAIL);
  2750. }
  2751. fprintf(stdout, "CHART %s.lwrites '' '%s I/O Logical Writes' 'kilobytes/s' disk %s.lwrites stacked 20042 %d\n", type, title, type, update_every);
  2752. for (w = root; w ; w = w->next) {
  2753. if(unlikely(w->exposed))
  2754. fprintf(stdout, "DIMENSION %s '' absolute 1 %llu\n", w->name, 1024LLU * RATES_DETAIL);
  2755. }
  2756. #endif
  2757. if(enable_file_charts) {
  2758. fprintf(stdout, "CHART %s.files '' '%s Open Files' 'open files' disk %s.files stacked 20050 %d\n", type,
  2759. title, type, update_every);
  2760. for (w = root; w; w = w->next) {
  2761. if (unlikely(w->exposed))
  2762. fprintf(stdout, "DIMENSION %s '' absolute 1 1\n", w->name);
  2763. }
  2764. fprintf(stdout, "CHART %s.sockets '' '%s Open Sockets' 'open sockets' net %s.sockets stacked 20051 %d\n",
  2765. type, title, type, update_every);
  2766. for (w = root; w; w = w->next) {
  2767. if (unlikely(w->exposed))
  2768. fprintf(stdout, "DIMENSION %s '' absolute 1 1\n", w->name);
  2769. }
  2770. fprintf(stdout, "CHART %s.pipes '' '%s Pipes' 'open pipes' processes %s.pipes stacked 20053 %d\n", type,
  2771. title, type, update_every);
  2772. for (w = root; w; w = w->next) {
  2773. if (unlikely(w->exposed))
  2774. fprintf(stdout, "DIMENSION %s '' absolute 1 1\n", w->name);
  2775. }
  2776. }
  2777. }
  2778. // ----------------------------------------------------------------------------
  2779. // parse command line arguments
  2780. int check_proc_1_io() {
  2781. int ret = 0;
  2782. procfile *ff = procfile_open("/proc/1/io", NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
  2783. if(!ff) goto cleanup;
  2784. ff = procfile_readall(ff);
  2785. if(!ff) goto cleanup;
  2786. ret = 1;
  2787. cleanup:
  2788. procfile_close(ff);
  2789. return ret;
  2790. }
  2791. static void parse_args(int argc, char **argv)
  2792. {
  2793. int i, freq = 0;
  2794. for(i = 1; i < argc; i++) {
  2795. if(!freq) {
  2796. int n = (int)str2l(argv[i]);
  2797. if(n > 0) {
  2798. freq = n;
  2799. continue;
  2800. }
  2801. }
  2802. 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) {
  2803. printf("apps.plugin %s\n", VERSION);
  2804. exit(0);
  2805. }
  2806. if(strcmp("test-permissions", argv[i]) == 0 || strcmp("-t", argv[i]) == 0) {
  2807. if(!check_proc_1_io()) {
  2808. perror("Tried to read /proc/1/io and it failed");
  2809. exit(1);
  2810. }
  2811. printf("OK\n");
  2812. exit(0);
  2813. }
  2814. if(strcmp("debug", argv[i]) == 0) {
  2815. #ifdef NETDATA_INTERNAL_CHECKS
  2816. debug_enabled = 1;
  2817. #else
  2818. fprintf(stderr, "apps.plugin has been compiled without debugging\n");
  2819. #endif
  2820. continue;
  2821. }
  2822. #ifndef __FreeBSD__
  2823. if(strcmp("fds-cache-secs", argv[i]) == 0) {
  2824. if(argc <= i + 1) {
  2825. fprintf(stderr, "Parameter 'fds-cache-secs' requires a number as argument.\n");
  2826. exit(1);
  2827. }
  2828. i++;
  2829. max_fds_cache_seconds = str2i(argv[i]);
  2830. if(max_fds_cache_seconds < 0) max_fds_cache_seconds = 0;
  2831. continue;
  2832. }
  2833. #endif
  2834. if(strcmp("no-childs", argv[i]) == 0 || strcmp("without-childs", argv[i]) == 0) {
  2835. include_exited_childs = 0;
  2836. continue;
  2837. }
  2838. if(strcmp("with-childs", argv[i]) == 0) {
  2839. include_exited_childs = 1;
  2840. continue;
  2841. }
  2842. if(strcmp("with-guest", argv[i]) == 0) {
  2843. enable_guest_charts = 1;
  2844. continue;
  2845. }
  2846. if(strcmp("no-guest", argv[i]) == 0 || strcmp("without-guest", argv[i]) == 0) {
  2847. enable_guest_charts = 0;
  2848. continue;
  2849. }
  2850. if(strcmp("with-files", argv[i]) == 0) {
  2851. enable_file_charts = 1;
  2852. continue;
  2853. }
  2854. if(strcmp("no-files", argv[i]) == 0 || strcmp("without-files", argv[i]) == 0) {
  2855. enable_file_charts = 0;
  2856. continue;
  2857. }
  2858. if(strcmp("no-users", argv[i]) == 0 || strcmp("without-users", argv[i]) == 0) {
  2859. enable_users_charts = 0;
  2860. continue;
  2861. }
  2862. if(strcmp("no-groups", argv[i]) == 0 || strcmp("without-groups", argv[i]) == 0) {
  2863. enable_groups_charts = 0;
  2864. continue;
  2865. }
  2866. if(strcmp("-h", argv[i]) == 0 || strcmp("--help", argv[i]) == 0) {
  2867. fprintf(stderr,
  2868. "\n"
  2869. " netdata apps.plugin %s\n"
  2870. " Copyright (C) 2016-2017 Costa Tsaousis <costa@tsaousis.gr>\n"
  2871. " Released under GNU General Public License v3 or later.\n"
  2872. " All rights reserved.\n"
  2873. "\n"
  2874. " This program is a data collector plugin for netdata.\n"
  2875. "\n"
  2876. " Available command line options:\n"
  2877. "\n"
  2878. " SECONDS set the data collection frequency\n"
  2879. "\n"
  2880. " debug enable debugging (lot of output)\n"
  2881. "\n"
  2882. " with-childs\n"
  2883. " without-childs enable / disable aggregating exited\n"
  2884. " children resources into parents\n"
  2885. " (default is enabled)\n"
  2886. "\n"
  2887. " with-guest\n"
  2888. " without-guest enable / disable reporting guest charts\n"
  2889. " (default is disabled)\n"
  2890. "\n"
  2891. " with-files\n"
  2892. " without-files enable / disable reporting files, sockets, pipes\n"
  2893. " (default is enabled)\n"
  2894. "\n"
  2895. #ifndef __FreeBSD__
  2896. " fds-cache-secs N cache the files of processed for N seconds\n"
  2897. " caching is adaptive per file (when a file\n"
  2898. " is found, it starts at 0 and while the file\n"
  2899. " remains open, it is incremented up to the\n"
  2900. " max given)\n"
  2901. " (default is %d seconds)\n"
  2902. "\n"
  2903. #endif
  2904. " version or -v or -V print program version and exit\n"
  2905. "\n"
  2906. , VERSION
  2907. #ifndef __FreeBSD__
  2908. , max_fds_cache_seconds
  2909. #endif
  2910. );
  2911. exit(1);
  2912. }
  2913. error("Cannot understand option %s", argv[i]);
  2914. exit(1);
  2915. }
  2916. if(freq > 0) update_every = freq;
  2917. if(read_apps_groups_conf(user_config_dir, "groups")) {
  2918. info("Cannot read process groups configuration file '%s/apps_groups.conf'. Will try '%s/apps_groups.conf'", user_config_dir, stock_config_dir);
  2919. if(read_apps_groups_conf(stock_config_dir, "groups")) {
  2920. error("Cannot read process groups '%s/apps_groups.conf'. There are no internal defaults. Failing.", stock_config_dir);
  2921. exit(1);
  2922. }
  2923. else
  2924. info("Loaded config file '%s/apps_groups.conf'", stock_config_dir);
  2925. }
  2926. else
  2927. info("Loaded config file '%s/apps_groups.conf'", user_config_dir);
  2928. }
  2929. static int am_i_running_as_root() {
  2930. uid_t uid = getuid(), euid = geteuid();
  2931. if(uid == 0 || euid == 0) {
  2932. if(debug_enabled) info("I am running with escalated privileges, uid = %u, euid = %u.", uid, euid);
  2933. return 1;
  2934. }
  2935. if(debug_enabled) info("I am not running with escalated privileges, uid = %u, euid = %u.", uid, euid);
  2936. return 0;
  2937. }
  2938. #ifdef HAVE_CAPABILITY
  2939. static int check_capabilities() {
  2940. cap_t caps = cap_get_proc();
  2941. if(!caps) {
  2942. error("Cannot get current capabilities.");
  2943. return 0;
  2944. }
  2945. else if(debug_enabled)
  2946. info("Received my capabilities from the system.");
  2947. int ret = 1;
  2948. cap_flag_value_t cfv = CAP_CLEAR;
  2949. if(cap_get_flag(caps, CAP_DAC_READ_SEARCH, CAP_EFFECTIVE, &cfv) == -1) {
  2950. error("Cannot find if CAP_DAC_READ_SEARCH is effective.");
  2951. ret = 0;
  2952. }
  2953. else {
  2954. if(cfv != CAP_SET) {
  2955. error("apps.plugin should run with CAP_DAC_READ_SEARCH.");
  2956. ret = 0;
  2957. }
  2958. else if(debug_enabled)
  2959. info("apps.plugin runs with CAP_DAC_READ_SEARCH.");
  2960. }
  2961. cfv = CAP_CLEAR;
  2962. if(cap_get_flag(caps, CAP_SYS_PTRACE, CAP_EFFECTIVE, &cfv) == -1) {
  2963. error("Cannot find if CAP_SYS_PTRACE is effective.");
  2964. ret = 0;
  2965. }
  2966. else {
  2967. if(cfv != CAP_SET) {
  2968. error("apps.plugin should run with CAP_SYS_PTRACE.");
  2969. ret = 0;
  2970. }
  2971. else if(debug_enabled)
  2972. info("apps.plugin runs with CAP_SYS_PTRACE.");
  2973. }
  2974. cap_free(caps);
  2975. return ret;
  2976. }
  2977. #else
  2978. static int check_capabilities() {
  2979. return 0;
  2980. }
  2981. #endif
  2982. int main(int argc, char **argv) {
  2983. // debug_flags = D_PROCFILE;
  2984. pagesize = (size_t)sysconf(_SC_PAGESIZE);
  2985. // set the name for logging
  2986. program_name = "apps.plugin";
  2987. // disable syslog for apps.plugin
  2988. error_log_syslog = 0;
  2989. // set errors flood protection to 100 logs per hour
  2990. error_log_errors_per_period = 100;
  2991. error_log_throttle_period = 3600;
  2992. // since apps.plugin runs as root, prevent it from opening symbolic links
  2993. procfile_open_flags = O_RDONLY|O_NOFOLLOW;
  2994. netdata_configured_host_prefix = getenv("NETDATA_HOST_PREFIX");
  2995. if(verify_netdata_host_prefix() == -1) exit(1);
  2996. user_config_dir = getenv("NETDATA_USER_CONFIG_DIR");
  2997. if(user_config_dir == NULL) {
  2998. // info("NETDATA_CONFIG_DIR is not passed from netdata");
  2999. user_config_dir = CONFIG_DIR;
  3000. }
  3001. // else info("Found NETDATA_USER_CONFIG_DIR='%s'", user_config_dir);
  3002. stock_config_dir = getenv("NETDATA_STOCK_CONFIG_DIR");
  3003. if(stock_config_dir == NULL) {
  3004. // info("NETDATA_CONFIG_DIR is not passed from netdata");
  3005. stock_config_dir = LIBCONFIG_DIR;
  3006. }
  3007. // else info("Found NETDATA_USER_CONFIG_DIR='%s'", user_config_dir);
  3008. #ifdef NETDATA_INTERNAL_CHECKS
  3009. if(debug_flags != 0) {
  3010. struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
  3011. if(setrlimit(RLIMIT_CORE, &rl) != 0)
  3012. info("Cannot request unlimited core dumps for debugging... Proceeding anyway...");
  3013. #ifdef HAVE_SYS_PRCTL_H
  3014. prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
  3015. #endif
  3016. }
  3017. #endif /* NETDATA_INTERNAL_CHECKS */
  3018. procfile_adaptive_initial_allocation = 1;
  3019. time_t started_t = now_monotonic_sec();
  3020. get_system_HZ();
  3021. get_system_pid_max();
  3022. get_system_cpus();
  3023. parse_args(argc, argv);
  3024. if(!check_capabilities() && !am_i_running_as_root() && !check_proc_1_io()) {
  3025. uid_t uid = getuid(), euid = geteuid();
  3026. #ifdef HAVE_CAPABILITY
  3027. error("apps.plugin should either run as root (now running with uid %u, euid %u) or have special capabilities. "
  3028. "Without these, apps.plugin cannot report disk I/O utilization of other processes. "
  3029. "To enable capabilities run: sudo setcap cap_dac_read_search,cap_sys_ptrace+ep %s; "
  3030. "To enable setuid to root run: sudo chown root:netdata %s; sudo chmod 4750 %s; "
  3031. , uid, euid, argv[0], argv[0], argv[0]
  3032. );
  3033. #else
  3034. error("apps.plugin should either run as root (now running with uid %u, euid %u) or have special capabilities. "
  3035. "Without these, apps.plugin cannot report disk I/O utilization of other processes. "
  3036. "Your system does not support capabilities. "
  3037. "To enable setuid to root run: sudo chown root:netdata %s; sudo chmod 4750 %s; "
  3038. , uid, euid, argv[0], argv[0]
  3039. );
  3040. #endif
  3041. }
  3042. info("started on pid %d", getpid());
  3043. #if (ALL_PIDS_ARE_READ_INSTANTLY == 0)
  3044. all_pids_sortlist = callocz(sizeof(pid_t), (size_t)pid_max);
  3045. #endif
  3046. all_pids = callocz(sizeof(struct pid_stat *), (size_t) pid_max);
  3047. usec_t step = update_every * USEC_PER_SEC;
  3048. global_iterations_counter = 1;
  3049. heartbeat_t hb;
  3050. heartbeat_init(&hb);
  3051. for(;1; global_iterations_counter++) {
  3052. #ifdef NETDATA_PROFILING
  3053. #warning "compiling for profiling"
  3054. static int profiling_count=0;
  3055. profiling_count++;
  3056. if(unlikely(profiling_count > 2000)) exit(0);
  3057. usec_t dt = update_every * USEC_PER_SEC;
  3058. #else
  3059. usec_t dt = heartbeat_next(&hb, step);
  3060. #endif
  3061. if(!collect_data_for_all_processes()) {
  3062. error("Cannot collect /proc data for running processes. Disabling apps.plugin...");
  3063. printf("DISABLE\n");
  3064. exit(1);
  3065. }
  3066. calculate_netdata_statistics();
  3067. normalize_utilization(apps_groups_root_target);
  3068. send_resource_usage_to_netdata(dt);
  3069. // this is smart enough to show only newly added apps, when needed
  3070. send_charts_updates_to_netdata(apps_groups_root_target, "apps", "Apps");
  3071. if(likely(enable_users_charts))
  3072. send_charts_updates_to_netdata(users_root_target, "users", "Users");
  3073. if(likely(enable_groups_charts))
  3074. send_charts_updates_to_netdata(groups_root_target, "groups", "User Groups");
  3075. send_collected_data_to_netdata(apps_groups_root_target, "apps", dt);
  3076. if(likely(enable_users_charts))
  3077. send_collected_data_to_netdata(users_root_target, "users", dt);
  3078. if(likely(enable_groups_charts))
  3079. send_collected_data_to_netdata(groups_root_target, "groups", dt);
  3080. fflush(stdout);
  3081. show_guest_time_old = show_guest_time;
  3082. debug_log("done Loop No %zu", global_iterations_counter);
  3083. // restart check (14400 seconds)
  3084. if(now_monotonic_sec() - started_t > 14400) exit(0);
  3085. }
  3086. }