apps_plugin.c 134 KB

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