ebpf.c 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include <sys/time.h>
  3. #include <sys/resource.h>
  4. #include <ifaddrs.h>
  5. #include "ebpf.h"
  6. #include "ebpf_socket.h"
  7. #include "ebpf_unittest.h"
  8. #include "libnetdata/required_dummies.h"
  9. /*****************************************************************
  10. *
  11. * GLOBAL VARIABLES
  12. *
  13. *****************************************************************/
  14. char *ebpf_plugin_dir = PLUGINS_DIR;
  15. static char *ebpf_configured_log_dir = LOG_DIR;
  16. char *ebpf_algorithms[] = {"absolute", "incremental"};
  17. struct config collector_config = { .first_section = NULL,
  18. .last_section = NULL,
  19. .mutex = NETDATA_MUTEX_INITIALIZER,
  20. .index = { .avl_tree = { .root = NULL, .compar = appconfig_section_compare },
  21. .rwlock = AVL_LOCK_INITIALIZER } };
  22. int running_on_kernel = 0;
  23. int ebpf_nprocs;
  24. int isrh = 0;
  25. int main_thread_id = 0;
  26. int process_pid_fd = -1;
  27. pthread_mutex_t lock;
  28. pthread_mutex_t ebpf_exit_cleanup;
  29. pthread_mutex_t collect_data_mutex;
  30. struct netdata_static_thread cgroup_integration_thread = {
  31. .name = "EBPF CGROUP INT",
  32. .config_section = NULL,
  33. .config_name = NULL,
  34. .env_name = NULL,
  35. .enabled = 1,
  36. .thread = NULL,
  37. .init_routine = NULL,
  38. .start_routine = NULL
  39. };
  40. ebpf_module_t ebpf_modules[] = {
  41. { .thread_name = "process", .config_name = "process", .enabled = 0, .start_routine = ebpf_process_thread,
  42. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  43. .apps_level = NETDATA_APPS_LEVEL_REAL_PARENT, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  44. .apps_routine = ebpf_process_create_apps_charts, .maps = NULL,
  45. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &process_config,
  46. .config_file = NETDATA_PROCESS_CONFIG_FILE,
  47. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_10 |
  48. NETDATA_V5_14,
  49. .load = EBPF_LOAD_LEGACY, .targets = NULL, .probe_links = NULL, .objects = NULL,
  50. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES},
  51. { .thread_name = "socket", .config_name = "socket", .enabled = 0, .start_routine = ebpf_socket_thread,
  52. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  53. .apps_level = NETDATA_APPS_LEVEL_REAL_PARENT, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  54. .apps_routine = ebpf_socket_create_apps_charts, .maps = NULL,
  55. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &socket_config,
  56. .config_file = NETDATA_NETWORK_CONFIG_FILE,
  57. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14,
  58. .load = EBPF_LOAD_LEGACY, .targets = socket_targets, .probe_links = NULL, .objects = NULL,
  59. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES},
  60. { .thread_name = "cachestat", .config_name = "cachestat", .enabled = 0, .start_routine = ebpf_cachestat_thread,
  61. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  62. .apps_level = NETDATA_APPS_LEVEL_REAL_PARENT, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  63. .apps_routine = ebpf_cachestat_create_apps_charts, .maps = cachestat_maps,
  64. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &cachestat_config,
  65. .config_file = NETDATA_CACHESTAT_CONFIG_FILE,
  66. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18|
  67. NETDATA_V5_4 | NETDATA_V5_14 | NETDATA_V5_15 | NETDATA_V5_16,
  68. .load = EBPF_LOAD_LEGACY, .targets = cachestat_targets, .probe_links = NULL, .objects = NULL,
  69. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES},
  70. { .thread_name = "sync", .config_name = "sync", .enabled = 0, .start_routine = ebpf_sync_thread,
  71. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  72. .apps_level = NETDATA_APPS_NOT_SET, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  73. .apps_routine = NULL, .maps = NULL, .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &sync_config,
  74. .config_file = NETDATA_SYNC_CONFIG_FILE,
  75. // All syscalls have the same kernels
  76. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14,
  77. .load = EBPF_LOAD_LEGACY, .targets = sync_targets, .probe_links = NULL, .objects = NULL,
  78. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES},
  79. { .thread_name = "dc", .config_name = "dc", .enabled = 0, .start_routine = ebpf_dcstat_thread,
  80. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  81. .apps_level = NETDATA_APPS_LEVEL_REAL_PARENT, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  82. .apps_routine = ebpf_dcstat_create_apps_charts, .maps = dcstat_maps,
  83. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &dcstat_config,
  84. .config_file = NETDATA_DIRECTORY_DCSTAT_CONFIG_FILE,
  85. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14,
  86. .load = EBPF_LOAD_LEGACY, .targets = dc_targets, .probe_links = NULL, .objects = NULL,
  87. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES},
  88. { .thread_name = "swap", .config_name = "swap", .enabled = 0, .start_routine = ebpf_swap_thread,
  89. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  90. .apps_level = NETDATA_APPS_LEVEL_REAL_PARENT, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  91. .apps_routine = ebpf_swap_create_apps_charts, .maps = NULL,
  92. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &swap_config,
  93. .config_file = NETDATA_DIRECTORY_SWAP_CONFIG_FILE,
  94. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14,
  95. .load = EBPF_LOAD_LEGACY, .targets = swap_targets, .probe_links = NULL, .objects = NULL,
  96. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES},
  97. { .thread_name = "vfs", .config_name = "vfs", .enabled = 0, .start_routine = ebpf_vfs_thread,
  98. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  99. .apps_level = NETDATA_APPS_LEVEL_REAL_PARENT, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  100. .apps_routine = ebpf_vfs_create_apps_charts, .maps = NULL,
  101. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &vfs_config,
  102. .config_file = NETDATA_DIRECTORY_VFS_CONFIG_FILE,
  103. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14,
  104. .load = EBPF_LOAD_LEGACY, .targets = vfs_targets, .probe_links = NULL, .objects = NULL,
  105. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES},
  106. { .thread_name = "filesystem", .config_name = "filesystem", .enabled = 0, .start_routine = ebpf_filesystem_thread,
  107. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  108. .apps_level = NETDATA_APPS_NOT_SET, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  109. .apps_routine = NULL, .maps = NULL, .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &fs_config,
  110. .config_file = NETDATA_FILESYSTEM_CONFIG_FILE,
  111. //We are setting kernels as zero, because we load eBPF programs according the kernel running.
  112. .kernels = 0, .load = EBPF_LOAD_LEGACY, .targets = NULL, .probe_links = NULL, .objects = NULL,
  113. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES },
  114. { .thread_name = "disk", .config_name = "disk", .enabled = 0, .start_routine = ebpf_disk_thread,
  115. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  116. .apps_level = NETDATA_APPS_NOT_SET, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  117. .apps_routine = NULL, .maps = NULL, .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &disk_config,
  118. .config_file = NETDATA_DISK_CONFIG_FILE,
  119. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14,
  120. .load = EBPF_LOAD_LEGACY, .targets = NULL, .probe_links = NULL, .objects = NULL,
  121. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES},
  122. { .thread_name = "mount", .config_name = "mount", .enabled = 0, .start_routine = ebpf_mount_thread,
  123. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  124. .apps_level = NETDATA_APPS_NOT_SET, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  125. .apps_routine = NULL, .maps = NULL, .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &mount_config,
  126. .config_file = NETDATA_MOUNT_CONFIG_FILE,
  127. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14,
  128. .load = EBPF_LOAD_LEGACY, .targets = mount_targets, .probe_links = NULL, .objects = NULL,
  129. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES},
  130. { .thread_name = "fd", .config_name = "fd", .enabled = 0, .start_routine = ebpf_fd_thread,
  131. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  132. .apps_level = NETDATA_APPS_LEVEL_REAL_PARENT, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  133. .apps_routine = ebpf_fd_create_apps_charts, .maps = NULL,
  134. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &fd_config,
  135. .config_file = NETDATA_FD_CONFIG_FILE,
  136. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_11 |
  137. NETDATA_V5_14,
  138. .load = EBPF_LOAD_LEGACY, .targets = fd_targets, .probe_links = NULL, .objects = NULL,
  139. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES},
  140. { .thread_name = "hardirq", .config_name = "hardirq", .enabled = 0, .start_routine = ebpf_hardirq_thread,
  141. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  142. .apps_level = NETDATA_APPS_NOT_SET, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  143. .apps_routine = NULL, .maps = NULL, .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &hardirq_config,
  144. .config_file = NETDATA_HARDIRQ_CONFIG_FILE,
  145. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14,
  146. .load = EBPF_LOAD_LEGACY, .targets = NULL, .probe_links = NULL, .objects = NULL,
  147. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES},
  148. { .thread_name = "softirq", .config_name = "softirq", .enabled = 0, .start_routine = ebpf_softirq_thread,
  149. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  150. .apps_level = NETDATA_APPS_NOT_SET, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  151. .apps_routine = NULL, .maps = NULL, .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &softirq_config,
  152. .config_file = NETDATA_SOFTIRQ_CONFIG_FILE,
  153. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14,
  154. .load = EBPF_LOAD_LEGACY, .targets = NULL, .probe_links = NULL, .objects = NULL,
  155. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES},
  156. { .thread_name = "oomkill", .config_name = "oomkill", .enabled = 0, .start_routine = ebpf_oomkill_thread,
  157. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  158. .apps_level = NETDATA_APPS_LEVEL_REAL_PARENT, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  159. .apps_routine = ebpf_oomkill_create_apps_charts, .maps = NULL,
  160. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &oomkill_config,
  161. .config_file = NETDATA_OOMKILL_CONFIG_FILE,
  162. .kernels = NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14,
  163. .load = EBPF_LOAD_LEGACY, .targets = NULL, .probe_links = NULL, .objects = NULL,
  164. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES},
  165. { .thread_name = "shm", .config_name = "shm", .enabled = 0, .start_routine = ebpf_shm_thread,
  166. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  167. .apps_level = NETDATA_APPS_LEVEL_REAL_PARENT, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  168. .apps_routine = ebpf_shm_create_apps_charts, .maps = NULL,
  169. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &shm_config,
  170. .config_file = NETDATA_DIRECTORY_SHM_CONFIG_FILE,
  171. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14,
  172. .load = EBPF_LOAD_LEGACY, .targets = shm_targets, .probe_links = NULL, .objects = NULL,
  173. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES},
  174. { .thread_name = "mdflush", .config_name = "mdflush", .enabled = 0, .start_routine = ebpf_mdflush_thread,
  175. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  176. .apps_level = NETDATA_APPS_NOT_SET, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  177. .apps_routine = NULL, .maps = NULL, .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &mdflush_config,
  178. .config_file = NETDATA_DIRECTORY_MDFLUSH_CONFIG_FILE,
  179. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14,
  180. .load = EBPF_LOAD_LEGACY, .targets = NULL, .probe_links = NULL, .objects = NULL,
  181. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES},
  182. { .thread_name = NULL, .enabled = 0, .start_routine = NULL, .update_every = EBPF_DEFAULT_UPDATE_EVERY,
  183. .global_charts = 0, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO, .apps_level = NETDATA_APPS_NOT_SET,
  184. .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0, .apps_routine = NULL, .maps = NULL,
  185. .pid_map_size = 0, .names = NULL, .cfg = NULL, .config_name = NULL, .kernels = 0, .load = EBPF_LOAD_LEGACY,
  186. .targets = NULL, .probe_links = NULL, .objects = NULL, .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES},
  187. };
  188. struct netdata_static_thread ebpf_threads[] = {
  189. {
  190. .name = "EBPF PROCESS",
  191. .config_section = NULL,
  192. .config_name = NULL,
  193. .env_name = NULL,
  194. .enabled = 1,
  195. .thread = NULL,
  196. .init_routine = NULL,
  197. .start_routine = NULL
  198. },
  199. {
  200. .name = "EBPF SOCKET",
  201. .config_section = NULL,
  202. .config_name = NULL,
  203. .env_name = NULL,
  204. .enabled = 1,
  205. .thread = NULL,
  206. .init_routine = NULL,
  207. .start_routine = NULL
  208. },
  209. {
  210. .name = "EBPF CACHESTAT",
  211. .config_section = NULL,
  212. .config_name = NULL,
  213. .env_name = NULL,
  214. .enabled = 1,
  215. .thread = NULL,
  216. .init_routine = NULL,
  217. .start_routine = NULL
  218. },
  219. {
  220. .name = "EBPF SYNC",
  221. .config_section = NULL,
  222. .config_name = NULL,
  223. .env_name = NULL,
  224. .enabled = 1,
  225. .thread = NULL,
  226. .init_routine = NULL,
  227. .start_routine = NULL
  228. },
  229. {
  230. .name = "EBPF DCSTAT",
  231. .config_section = NULL,
  232. .config_name = NULL,
  233. .env_name = NULL,
  234. .enabled = 1,
  235. .thread = NULL,
  236. .init_routine = NULL,
  237. .start_routine = NULL
  238. },
  239. {
  240. .name = "EBPF SWAP",
  241. .config_section = NULL,
  242. .config_name = NULL,
  243. .env_name = NULL,
  244. .enabled = 1,
  245. .thread = NULL,
  246. .init_routine = NULL,
  247. .start_routine = NULL
  248. },
  249. {
  250. .name = "EBPF VFS",
  251. .config_section = NULL,
  252. .config_name = NULL,
  253. .env_name = NULL,
  254. .enabled = 1,
  255. .thread = NULL,
  256. .init_routine = NULL,
  257. .start_routine = NULL
  258. },
  259. {
  260. .name = "EBPF FILESYSTEM",
  261. .config_section = NULL,
  262. .config_name = NULL,
  263. .env_name = NULL,
  264. .enabled = 1,
  265. .thread = NULL,
  266. .init_routine = NULL,
  267. .start_routine = NULL
  268. },
  269. {
  270. .name = "EBPF DISK",
  271. .config_section = NULL,
  272. .config_name = NULL,
  273. .env_name = NULL,
  274. .enabled = 1,
  275. .thread = NULL,
  276. .init_routine = NULL,
  277. .start_routine = NULL
  278. },
  279. {
  280. .name = "EBPF MOUNT",
  281. .config_section = NULL,
  282. .config_name = NULL,
  283. .env_name = NULL,
  284. .enabled = 1,
  285. .thread = NULL,
  286. .init_routine = NULL,
  287. .start_routine = NULL
  288. },
  289. {
  290. .name = "EBPF FD",
  291. .config_section = NULL,
  292. .config_name = NULL,
  293. .env_name = NULL,
  294. .enabled = 1,
  295. .thread = NULL,
  296. .init_routine = NULL,
  297. .start_routine = NULL
  298. },
  299. {
  300. .name = "EBPF HARDIRQ",
  301. .config_section = NULL,
  302. .config_name = NULL,
  303. .env_name = NULL,
  304. .enabled = 1,
  305. .thread = NULL,
  306. .init_routine = NULL,
  307. .start_routine = NULL
  308. },
  309. {
  310. .name = "EBPF SOFTIRQ",
  311. .config_section = NULL,
  312. .config_name = NULL,
  313. .env_name = NULL,
  314. .enabled = 1,
  315. .thread = NULL,
  316. .init_routine = NULL,
  317. .start_routine = NULL
  318. },
  319. {
  320. .name = "EBPF OOMKILL",
  321. .config_section = NULL,
  322. .config_name = NULL,
  323. .env_name = NULL,
  324. .enabled = 1,
  325. .thread = NULL,
  326. .init_routine = NULL,
  327. .start_routine = NULL
  328. },
  329. {
  330. .name = "EBPF SHM",
  331. .config_section = NULL,
  332. .config_name = NULL,
  333. .env_name = NULL,
  334. .enabled = 1,
  335. .thread = NULL,
  336. .init_routine = NULL,
  337. .start_routine = NULL
  338. },
  339. {
  340. .name = "EBPF MDFLUSH",
  341. .config_section = NULL,
  342. .config_name = NULL,
  343. .env_name = NULL,
  344. .enabled = 1,
  345. .thread = NULL,
  346. .init_routine = NULL,
  347. .start_routine = NULL
  348. },
  349. {
  350. .name = NULL,
  351. .config_section = NULL,
  352. .config_name = NULL,
  353. .env_name = NULL,
  354. .enabled = 0,
  355. .thread = NULL,
  356. .init_routine = NULL,
  357. .start_routine = NULL
  358. },
  359. };
  360. ebpf_filesystem_partitions_t localfs[] =
  361. {{.filesystem = "ext4",
  362. .optional_filesystem = NULL,
  363. .family = "ext4",
  364. .objects = NULL,
  365. .probe_links = NULL,
  366. .flags = NETDATA_FILESYSTEM_FLAG_NO_PARTITION,
  367. .enabled = CONFIG_BOOLEAN_YES,
  368. .addresses = {.function = NULL, .addr = 0},
  369. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4,
  370. .fs_maps = NULL},
  371. {.filesystem = "xfs",
  372. .optional_filesystem = NULL,
  373. .family = "xfs",
  374. .objects = NULL,
  375. .probe_links = NULL,
  376. .flags = NETDATA_FILESYSTEM_FLAG_NO_PARTITION,
  377. .enabled = CONFIG_BOOLEAN_YES,
  378. .addresses = {.function = NULL, .addr = 0},
  379. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4,
  380. .fs_maps = NULL},
  381. {.filesystem = "nfs",
  382. .optional_filesystem = "nfs4",
  383. .family = "nfs",
  384. .objects = NULL,
  385. .probe_links = NULL,
  386. .flags = NETDATA_FILESYSTEM_ATTR_CHARTS,
  387. .enabled = CONFIG_BOOLEAN_YES,
  388. .addresses = {.function = NULL, .addr = 0},
  389. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4,
  390. .fs_maps = NULL},
  391. {.filesystem = "zfs",
  392. .optional_filesystem = NULL,
  393. .family = "zfs",
  394. .objects = NULL,
  395. .probe_links = NULL,
  396. .flags = NETDATA_FILESYSTEM_FLAG_NO_PARTITION,
  397. .enabled = CONFIG_BOOLEAN_YES,
  398. .addresses = {.function = NULL, .addr = 0},
  399. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4,
  400. .fs_maps = NULL},
  401. {.filesystem = "btrfs",
  402. .optional_filesystem = NULL,
  403. .family = "btrfs",
  404. .objects = NULL,
  405. .probe_links = NULL,
  406. .flags = NETDATA_FILESYSTEM_FILL_ADDRESS_TABLE,
  407. .enabled = CONFIG_BOOLEAN_YES,
  408. .addresses = {.function = "btrfs_file_operations", .addr = 0},
  409. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_10,
  410. .fs_maps = NULL},
  411. {.filesystem = NULL,
  412. .optional_filesystem = NULL,
  413. .family = NULL,
  414. .objects = NULL,
  415. .probe_links = NULL,
  416. .flags = NETDATA_FILESYSTEM_FLAG_NO_PARTITION,
  417. .enabled = CONFIG_BOOLEAN_YES,
  418. .addresses = {.function = NULL, .addr = 0},
  419. .kernels = 0, .fs_maps = NULL}};
  420. ebpf_sync_syscalls_t local_syscalls[] = {
  421. {.syscall = NETDATA_SYSCALLS_SYNC, .enabled = CONFIG_BOOLEAN_YES, .objects = NULL, .probe_links = NULL,
  422. #ifdef LIBBPF_MAJOR_VERSION
  423. .sync_obj = NULL,
  424. #endif
  425. .sync_maps = NULL
  426. },
  427. {.syscall = NETDATA_SYSCALLS_SYNCFS, .enabled = CONFIG_BOOLEAN_YES, .objects = NULL, .probe_links = NULL,
  428. #ifdef LIBBPF_MAJOR_VERSION
  429. .sync_obj = NULL,
  430. #endif
  431. .sync_maps = NULL
  432. },
  433. {.syscall = NETDATA_SYSCALLS_MSYNC, .enabled = CONFIG_BOOLEAN_YES, .objects = NULL, .probe_links = NULL,
  434. #ifdef LIBBPF_MAJOR_VERSION
  435. .sync_obj = NULL,
  436. #endif
  437. .sync_maps = NULL
  438. },
  439. {.syscall = NETDATA_SYSCALLS_FSYNC, .enabled = CONFIG_BOOLEAN_YES, .objects = NULL, .probe_links = NULL,
  440. #ifdef LIBBPF_MAJOR_VERSION
  441. .sync_obj = NULL,
  442. #endif
  443. .sync_maps = NULL
  444. },
  445. {.syscall = NETDATA_SYSCALLS_FDATASYNC, .enabled = CONFIG_BOOLEAN_YES, .objects = NULL, .probe_links = NULL,
  446. #ifdef LIBBPF_MAJOR_VERSION
  447. .sync_obj = NULL,
  448. #endif
  449. .sync_maps = NULL
  450. },
  451. {.syscall = NETDATA_SYSCALLS_SYNC_FILE_RANGE, .enabled = CONFIG_BOOLEAN_YES, .objects = NULL, .probe_links = NULL,
  452. #ifdef LIBBPF_MAJOR_VERSION
  453. .sync_obj = NULL,
  454. #endif
  455. .sync_maps = NULL
  456. },
  457. {.syscall = NULL, .enabled = CONFIG_BOOLEAN_NO, .objects = NULL, .probe_links = NULL,
  458. #ifdef LIBBPF_MAJOR_VERSION
  459. .sync_obj = NULL,
  460. #endif
  461. .sync_maps = NULL
  462. }
  463. };
  464. // Link with cgroup.plugin
  465. netdata_ebpf_cgroup_shm_t shm_ebpf_cgroup = {NULL, NULL};
  466. int shm_fd_ebpf_cgroup = -1;
  467. sem_t *shm_sem_ebpf_cgroup = SEM_FAILED;
  468. pthread_mutex_t mutex_cgroup_shm;
  469. //Network viewer
  470. ebpf_network_viewer_options_t network_viewer_opt;
  471. // Statistic
  472. ebpf_plugin_stats_t plugin_statistics = {.core = 0, .legacy = 0, .running = 0, .threads = 0, .tracepoints = 0,
  473. .probes = 0, .retprobes = 0, .trampolines = 0, .memlock_kern = 0,
  474. .hash_tables = 0};
  475. #ifdef LIBBPF_MAJOR_VERSION
  476. struct btf *default_btf = NULL;
  477. struct cachestat_bpf *cachestat_bpf_obj = NULL;
  478. struct dc_bpf *dc_bpf_obj = NULL;
  479. struct fd_bpf *fd_bpf_obj = NULL;
  480. struct mount_bpf *mount_bpf_obj = NULL;
  481. struct shm_bpf *shm_bpf_obj = NULL;
  482. struct socket_bpf *socket_bpf_obj = NULL;
  483. struct swap_bpf *bpf_obj = NULL;
  484. struct vfs_bpf *vfs_bpf_obj = NULL;
  485. #else
  486. void *default_btf = NULL;
  487. #endif
  488. char *btf_path = NULL;
  489. /*****************************************************************
  490. *
  491. * FUNCTIONS USED TO ALLOCATE APPS/CGROUP MEMORIES (ARAL)
  492. *
  493. *****************************************************************/
  494. /**
  495. * Allocate PID ARAL
  496. *
  497. * Allocate memory using ARAL functions to speed up processing.
  498. *
  499. * @param name the internal name used for allocated region.
  500. * @param size size of each element inside allocated space
  501. *
  502. * @return It returns the address on success and NULL otherwise.
  503. */
  504. ARAL *ebpf_allocate_pid_aral(char *name, size_t size)
  505. {
  506. static size_t max_elements = NETDATA_EBPF_ALLOC_MAX_PID;
  507. if (max_elements < NETDATA_EBPF_ALLOC_MIN_ELEMENTS) {
  508. error("Number of elements given is too small, adjusting it for %d", NETDATA_EBPF_ALLOC_MIN_ELEMENTS);
  509. max_elements = NETDATA_EBPF_ALLOC_MIN_ELEMENTS;
  510. }
  511. return aral_create(name, size,
  512. 0, max_elements,
  513. NULL, NULL, NULL, false, false);
  514. }
  515. /*****************************************************************
  516. *
  517. * FUNCTIONS USED TO CLEAN MEMORY AND OPERATE SYSTEM FILES
  518. *
  519. *****************************************************************/
  520. /**
  521. * Close the collector gracefully
  522. */
  523. static void ebpf_exit()
  524. {
  525. #ifdef LIBBPF_MAJOR_VERSION
  526. pthread_mutex_lock(&ebpf_exit_cleanup);
  527. if (default_btf) {
  528. btf__free(default_btf);
  529. default_btf = NULL;
  530. }
  531. pthread_mutex_unlock(&ebpf_exit_cleanup);
  532. #endif
  533. char filename[FILENAME_MAX + 1];
  534. ebpf_pid_file(filename, FILENAME_MAX);
  535. if (unlink(filename))
  536. error("Cannot remove PID file %s", filename);
  537. #ifdef NETDATA_INTERNAL_CHECKS
  538. error("Good bye world! I was PID %d", main_thread_id);
  539. #endif
  540. printf("DISABLE\n");
  541. pthread_mutex_lock(&mutex_cgroup_shm);
  542. if (shm_ebpf_cgroup.header) {
  543. ebpf_unmap_cgroup_shared_memory();
  544. shm_unlink(NETDATA_SHARED_MEMORY_EBPF_CGROUP_NAME);
  545. }
  546. pthread_mutex_unlock(&mutex_cgroup_shm);
  547. exit(0);
  548. }
  549. /**
  550. * Unload loegacy code
  551. *
  552. * @param objects objects loaded from eBPF programs
  553. * @param probe_links links from loader
  554. */
  555. void ebpf_unload_legacy_code(struct bpf_object *objects, struct bpf_link **probe_links)
  556. {
  557. if (!probe_links || !objects)
  558. return;
  559. struct bpf_program *prog;
  560. size_t j = 0 ;
  561. bpf_object__for_each_program(prog, objects) {
  562. bpf_link__destroy(probe_links[j]);
  563. j++;
  564. }
  565. freez(probe_links);
  566. if (objects)
  567. bpf_object__close(objects);
  568. }
  569. /**
  570. * Unload Unique maps
  571. *
  572. * This function unload all BPF maps from threads using one unique BPF object.
  573. */
  574. static void ebpf_unload_unique_maps()
  575. {
  576. int i;
  577. for (i = 0; ebpf_modules[i].thread_name; i++) {
  578. if (ebpf_modules[i].enabled != NETDATA_THREAD_EBPF_STOPPED) {
  579. if (ebpf_modules[i].enabled != NETDATA_THREAD_EBPF_NOT_RUNNING)
  580. error("Cannot unload maps for thread %s, because it is not stopped.", ebpf_modules[i].thread_name);
  581. continue;
  582. }
  583. ebpf_unload_legacy_code(ebpf_modules[i].objects, ebpf_modules[i].probe_links);
  584. switch (i) {
  585. case EBPF_MODULE_CACHESTAT_IDX: {
  586. #ifdef LIBBPF_MAJOR_VERSION
  587. if (cachestat_bpf_obj)
  588. cachestat_bpf__destroy(cachestat_bpf_obj);
  589. #endif
  590. break;
  591. }
  592. case EBPF_MODULE_DCSTAT_IDX: {
  593. #ifdef LIBBPF_MAJOR_VERSION
  594. if (dc_bpf_obj)
  595. dc_bpf__destroy(dc_bpf_obj);
  596. #endif
  597. break;
  598. }
  599. case EBPF_MODULE_FD_IDX: {
  600. #ifdef LIBBPF_MAJOR_VERSION
  601. if (fd_bpf_obj)
  602. fd_bpf__destroy(fd_bpf_obj);
  603. #endif
  604. break;
  605. }
  606. case EBPF_MODULE_MOUNT_IDX: {
  607. #ifdef LIBBPF_MAJOR_VERSION
  608. if (mount_bpf_obj)
  609. mount_bpf__destroy(mount_bpf_obj);
  610. #endif
  611. break;
  612. }
  613. case EBPF_MODULE_SHM_IDX: {
  614. #ifdef LIBBPF_MAJOR_VERSION
  615. if (shm_bpf_obj)
  616. shm_bpf__destroy(shm_bpf_obj);
  617. #endif
  618. break;
  619. }
  620. case EBPF_MODULE_SOCKET_IDX: {
  621. #ifdef LIBBPF_MAJOR_VERSION
  622. if (socket_bpf_obj)
  623. socket_bpf__destroy(socket_bpf_obj);
  624. #endif
  625. break;
  626. }
  627. case EBPF_MODULE_SWAP_IDX: {
  628. #ifdef LIBBPF_MAJOR_VERSION
  629. if (bpf_obj)
  630. swap_bpf__destroy(bpf_obj);
  631. #endif
  632. break;
  633. }
  634. case EBPF_MODULE_VFS_IDX: {
  635. #ifdef LIBBPF_MAJOR_VERSION
  636. if (vfs_bpf_obj)
  637. vfs_bpf__destroy(vfs_bpf_obj);
  638. #endif
  639. break;
  640. }
  641. case EBPF_MODULE_PROCESS_IDX:
  642. case EBPF_MODULE_DISK_IDX:
  643. case EBPF_MODULE_HARDIRQ_IDX:
  644. case EBPF_MODULE_SOFTIRQ_IDX:
  645. case EBPF_MODULE_OOMKILL_IDX:
  646. case EBPF_MODULE_MDFLUSH_IDX:
  647. default:
  648. continue;
  649. }
  650. }
  651. }
  652. /**
  653. * Unload filesystem maps
  654. *
  655. * This function unload all BPF maps from filesystem thread.
  656. */
  657. static void ebpf_unload_filesystems()
  658. {
  659. if (ebpf_modules[EBPF_MODULE_FILESYSTEM_IDX].enabled == NETDATA_THREAD_EBPF_NOT_RUNNING ||
  660. ebpf_modules[EBPF_MODULE_SYNC_IDX].enabled == NETDATA_THREAD_EBPF_RUNNING)
  661. return;
  662. int i;
  663. for (i = 0; localfs[i].filesystem != NULL; i++) {
  664. ebpf_unload_legacy_code(localfs[i].objects, localfs[i].probe_links);
  665. }
  666. }
  667. /**
  668. * Unload sync maps
  669. *
  670. * This function unload all BPF maps from sync thread.
  671. */
  672. static void ebpf_unload_sync()
  673. {
  674. if (ebpf_modules[EBPF_MODULE_SYNC_IDX].enabled == NETDATA_THREAD_EBPF_NOT_RUNNING ||
  675. ebpf_modules[EBPF_MODULE_SYNC_IDX].enabled == NETDATA_THREAD_EBPF_RUNNING)
  676. return;
  677. int i;
  678. for (i = 0; local_syscalls[i].syscall != NULL; i++) {
  679. ebpf_unload_legacy_code(local_syscalls[i].objects, local_syscalls[i].probe_links);
  680. }
  681. }
  682. int ebpf_exit_plugin = 0;
  683. /**
  684. * Close the collector gracefully
  685. *
  686. * @param sig is the signal number used to close the collector
  687. */
  688. static void ebpf_stop_threads(int sig)
  689. {
  690. UNUSED(sig);
  691. static int only_one = 0;
  692. // Child thread should be closed by itself.
  693. pthread_mutex_lock(&ebpf_exit_cleanup);
  694. if (main_thread_id != gettid() || only_one) {
  695. pthread_mutex_unlock(&ebpf_exit_cleanup);
  696. return;
  697. }
  698. only_one = 1;
  699. int i;
  700. for (i = 0; ebpf_modules[i].thread_name != NULL; i++) {
  701. if (ebpf_modules[i].enabled == NETDATA_THREAD_EBPF_RUNNING) {
  702. netdata_thread_cancel(*ebpf_modules[i].thread->thread);
  703. #ifdef NETDATA_DEV_MODE
  704. info("Sending cancel for thread %s", ebpf_modules[i].thread_name);
  705. #endif
  706. }
  707. }
  708. pthread_mutex_unlock(&ebpf_exit_cleanup);
  709. pthread_mutex_lock(&mutex_cgroup_shm);
  710. netdata_thread_cancel(*cgroup_integration_thread.thread);
  711. #ifdef NETDATA_DEV_MODE
  712. info("Sending cancel for thread %s", cgroup_integration_thread.name);
  713. #endif
  714. pthread_mutex_unlock(&mutex_cgroup_shm);
  715. ebpf_exit_plugin = 1;
  716. usec_t max = USEC_PER_SEC, step = 100000;
  717. while (i && max) {
  718. max -= step;
  719. sleep_usec(step);
  720. i = 0;
  721. int j;
  722. pthread_mutex_lock(&ebpf_exit_cleanup);
  723. for (j = 0; ebpf_modules[j].thread_name != NULL; j++) {
  724. if (ebpf_modules[j].enabled == NETDATA_THREAD_EBPF_RUNNING)
  725. i++;
  726. }
  727. pthread_mutex_unlock(&ebpf_exit_cleanup);
  728. }
  729. pthread_mutex_lock(&ebpf_exit_cleanup);
  730. ebpf_unload_unique_maps();
  731. ebpf_unload_filesystems();
  732. ebpf_unload_sync();
  733. pthread_mutex_unlock(&ebpf_exit_cleanup);
  734. ebpf_exit();
  735. }
  736. /*****************************************************************
  737. *
  738. * FUNCTIONS TO CREATE CHARTS
  739. *
  740. *****************************************************************/
  741. /**
  742. * Create apps charts
  743. *
  744. * Call ebpf_create_chart to create the charts on apps submenu.
  745. *
  746. * @param root a pointer for the targets.
  747. */
  748. static void ebpf_create_apps_charts(struct ebpf_target *root)
  749. {
  750. if (unlikely(!ebpf_all_pids))
  751. return;
  752. struct ebpf_target *w;
  753. int newly_added = 0;
  754. for (w = root; w; w = w->next) {
  755. if (w->target)
  756. continue;
  757. if (unlikely(w->processes && (debug_enabled || w->debug_enabled))) {
  758. struct ebpf_pid_on_target *pid_on_target;
  759. fprintf(
  760. stderr, "ebpf.plugin: target '%s' has aggregated %u process%s:", w->name, w->processes,
  761. (w->processes == 1) ? "" : "es");
  762. for (pid_on_target = w->root_pid; pid_on_target; pid_on_target = pid_on_target->next) {
  763. fprintf(stderr, " %d", pid_on_target->pid);
  764. }
  765. fputc('\n', stderr);
  766. }
  767. if (!w->exposed && w->processes) {
  768. newly_added++;
  769. w->exposed = 1;
  770. if (debug_enabled || w->debug_enabled)
  771. debug_log_int("%s just added - regenerating charts.", w->name);
  772. }
  773. }
  774. if (!newly_added)
  775. return;
  776. int counter;
  777. for (counter = 0; ebpf_modules[counter].thread_name; counter++) {
  778. ebpf_module_t *current = &ebpf_modules[counter];
  779. if (current->enabled == NETDATA_THREAD_EBPF_RUNNING && current->apps_charts && current->apps_routine)
  780. current->apps_routine(current, root);
  781. }
  782. }
  783. /**
  784. * Get a value from a structure.
  785. *
  786. * @param basis it is the first address of the structure
  787. * @param offset it is the offset of the data you want to access.
  788. * @return
  789. */
  790. collected_number get_value_from_structure(char *basis, size_t offset)
  791. {
  792. collected_number *value = (collected_number *)(basis + offset);
  793. collected_number ret = (collected_number)llabs(*value);
  794. // this reset is necessary to avoid keep a constant value while processing is not executing a task
  795. *value = 0;
  796. return ret;
  797. }
  798. /**
  799. * Write begin command on standard output
  800. *
  801. * @param family the chart family name
  802. * @param name the chart name
  803. */
  804. void write_begin_chart(char *family, char *name)
  805. {
  806. printf("BEGIN %s.%s\n", family, name);
  807. }
  808. /**
  809. * Write END command on stdout.
  810. */
  811. inline void write_end_chart()
  812. {
  813. printf("END\n");
  814. }
  815. /**
  816. * Write set command on standard output
  817. *
  818. * @param dim the dimension name
  819. * @param value the value for the dimension
  820. */
  821. void write_chart_dimension(char *dim, long long value)
  822. {
  823. printf("SET %s = %lld\n", dim, value);
  824. }
  825. /**
  826. * Call the necessary functions to create a chart.
  827. *
  828. * @param name the chart name
  829. * @param family the chart family
  830. * @param move the pointer with the values that will be published
  831. * @param end the number of values that will be written on standard output
  832. *
  833. * @return It returns a variable that maps the charts that did not have zero values.
  834. */
  835. void write_count_chart(char *name, char *family, netdata_publish_syscall_t *move, uint32_t end)
  836. {
  837. write_begin_chart(family, name);
  838. uint32_t i = 0;
  839. while (move && i < end) {
  840. write_chart_dimension(move->name, move->ncall);
  841. move = move->next;
  842. i++;
  843. }
  844. write_end_chart();
  845. }
  846. /**
  847. * Call the necessary functions to create a chart.
  848. *
  849. * @param name the chart name
  850. * @param family the chart family
  851. * @param move the pointer with the values that will be published
  852. * @param end the number of values that will be written on standard output
  853. */
  854. void write_err_chart(char *name, char *family, netdata_publish_syscall_t *move, int end)
  855. {
  856. write_begin_chart(family, name);
  857. int i = 0;
  858. while (move && i < end) {
  859. write_chart_dimension(move->name, move->nerr);
  860. move = move->next;
  861. i++;
  862. }
  863. write_end_chart();
  864. }
  865. /**
  866. * Write charts
  867. *
  868. * Write the current information to publish the charts.
  869. *
  870. * @param family chart family
  871. * @param chart chart id
  872. * @param dim dimension name
  873. * @param v1 value.
  874. */
  875. void ebpf_one_dimension_write_charts(char *family, char *chart, char *dim, long long v1)
  876. {
  877. write_begin_chart(family, chart);
  878. write_chart_dimension(dim, v1);
  879. write_end_chart();
  880. }
  881. /**
  882. * Call the necessary functions to create a chart.
  883. *
  884. * @param chart the chart name
  885. * @param family the chart family
  886. * @param dwrite the dimension name
  887. * @param vwrite the value for previous dimension
  888. * @param dread the dimension name
  889. * @param vread the value for previous dimension
  890. *
  891. * @return It returns a variable that maps the charts that did not have zero values.
  892. */
  893. void write_io_chart(char *chart, char *family, char *dwrite, long long vwrite, char *dread, long long vread)
  894. {
  895. write_begin_chart(family, chart);
  896. write_chart_dimension(dwrite, vwrite);
  897. write_chart_dimension(dread, vread);
  898. write_end_chart();
  899. }
  900. /**
  901. * Write chart cmd on standard output
  902. *
  903. * @param type chart type
  904. * @param id chart id
  905. * @param title chart title
  906. * @param units units label
  907. * @param family group name used to attach the chart on dashboard
  908. * @param charttype chart type
  909. * @param context chart context
  910. * @param order chart order
  911. * @param update_every update interval used by plugin
  912. * @param module chart module name, this is the eBPF thread.
  913. */
  914. void ebpf_write_chart_cmd(char *type, char *id, char *title, char *units, char *family,
  915. char *charttype, char *context, int order, int update_every, char *module)
  916. {
  917. printf("CHART %s.%s '' '%s' '%s' '%s' '%s' '%s' %d %d '' 'ebpf.plugin' '%s'\n",
  918. type,
  919. id,
  920. title,
  921. units,
  922. (family)?family:"",
  923. (context)?context:"",
  924. (charttype)?charttype:"",
  925. order,
  926. update_every,
  927. module);
  928. }
  929. /**
  930. * Write chart cmd on standard output
  931. *
  932. * @param type chart type
  933. * @param id chart id
  934. * @param title chart title
  935. * @param units units label
  936. * @param family group name used to attach the chart on dashboard
  937. * @param charttype chart type
  938. * @param context chart context
  939. * @param order chart order
  940. * @param update_every value to overwrite the update frequency set by the server.
  941. */
  942. void ebpf_write_chart_obsolete(char *type, char *id, char *title, char *units, char *family,
  943. char *charttype, char *context, int order, int update_every)
  944. {
  945. printf("CHART %s.%s '' '%s' '%s' '%s' '%s' '%s' %d %d 'obsolete'\n",
  946. type,
  947. id,
  948. title,
  949. units,
  950. (family)?family:"",
  951. (context)?context:"",
  952. (charttype)?charttype:"",
  953. order,
  954. update_every);
  955. }
  956. /**
  957. * Write the dimension command on standard output
  958. *
  959. * @param name the dimension name
  960. * @param id the dimension id
  961. * @param algo the dimension algorithm
  962. */
  963. void ebpf_write_global_dimension(char *name, char *id, char *algorithm)
  964. {
  965. printf("DIMENSION %s %s %s 1 1\n", name, id, algorithm);
  966. }
  967. /**
  968. * Call ebpf_write_global_dimension to create the dimensions for a specific chart
  969. *
  970. * @param ptr a pointer to a structure of the type netdata_publish_syscall_t
  971. * @param end the number of dimensions for the structure ptr
  972. */
  973. void ebpf_create_global_dimension(void *ptr, int end)
  974. {
  975. netdata_publish_syscall_t *move = ptr;
  976. int i = 0;
  977. while (move && i < end) {
  978. ebpf_write_global_dimension(move->name, move->dimension, move->algorithm);
  979. move = move->next;
  980. i++;
  981. }
  982. }
  983. /**
  984. * Call write_chart_cmd to create the charts
  985. *
  986. * @param type chart type
  987. * @param id chart id
  988. * @param title chart title
  989. * @param units axis label
  990. * @param family group name used to attach the chart on dashboard
  991. * @param context chart context
  992. * @param charttype chart type
  993. * @param order order number of the specified chart
  994. * @param ncd a pointer to a function called to create dimensions
  995. * @param move a pointer for a structure that has the dimensions
  996. * @param end number of dimensions for the chart created
  997. * @param update_every update interval used with chart.
  998. * @param module chart module name, this is the eBPF thread.
  999. */
  1000. void ebpf_create_chart(char *type,
  1001. char *id,
  1002. char *title,
  1003. char *units,
  1004. char *family,
  1005. char *context,
  1006. char *charttype,
  1007. int order,
  1008. void (*ncd)(void *, int),
  1009. void *move,
  1010. int end,
  1011. int update_every,
  1012. char *module)
  1013. {
  1014. ebpf_write_chart_cmd(type, id, title, units, family, charttype, context, order, update_every, module);
  1015. if (ncd) {
  1016. ncd(move, end);
  1017. }
  1018. }
  1019. /**
  1020. * Create charts on apps submenu
  1021. *
  1022. * @param id the chart id
  1023. * @param title the value displayed on vertical axis.
  1024. * @param units the value displayed on vertical axis.
  1025. * @param family Submenu that the chart will be attached on dashboard.
  1026. * @param charttype chart type
  1027. * @param order the chart order
  1028. * @param algorithm the algorithm used by dimension
  1029. * @param root structure used to create the dimensions.
  1030. * @param update_every update interval used by plugin
  1031. * @param module chart module name, this is the eBPF thread.
  1032. */
  1033. void ebpf_create_charts_on_apps(char *id, char *title, char *units, char *family, char *charttype, int order,
  1034. char *algorithm, struct ebpf_target *root, int update_every, char *module)
  1035. {
  1036. struct ebpf_target *w;
  1037. ebpf_write_chart_cmd(NETDATA_APPS_FAMILY, id, title, units, family, charttype, NULL, order,
  1038. update_every, module);
  1039. for (w = root; w; w = w->next) {
  1040. if (unlikely(w->exposed))
  1041. fprintf(stdout, "DIMENSION %s '' %s 1 1\n", w->name, algorithm);
  1042. }
  1043. }
  1044. /**
  1045. * Call the necessary functions to create a name.
  1046. *
  1047. * @param family family name
  1048. * @param name chart name
  1049. * @param hist0 histogram values
  1050. * @param dimensions dimension values.
  1051. * @param end number of bins that will be sent to Netdata.
  1052. *
  1053. * @return It returns a variable that maps the charts that did not have zero values.
  1054. */
  1055. void write_histogram_chart(char *family, char *name, const netdata_idx_t *hist, char **dimensions, uint32_t end)
  1056. {
  1057. write_begin_chart(family, name);
  1058. uint32_t i;
  1059. for (i = 0; i < end; i++) {
  1060. write_chart_dimension(dimensions[i], (long long) hist[i]);
  1061. }
  1062. write_end_chart();
  1063. fflush(stdout);
  1064. }
  1065. /**
  1066. * ARAL Charts
  1067. *
  1068. * Add chart to monitor ARAL usage
  1069. * Caller must call this function with mutex locked.
  1070. *
  1071. * @param name the name used to create aral
  1072. * @param em a pointer to the structure with the default values.
  1073. */
  1074. void ebpf_statistic_create_aral_chart(char *name, ebpf_module_t *em)
  1075. {
  1076. static int priority = 140100;
  1077. char *mem = { NETDATA_EBPF_STAT_DIMENSION_MEMORY };
  1078. char *aral = { NETDATA_EBPF_STAT_DIMENSION_ARAL };
  1079. snprintfz(em->memory_usage, NETDATA_EBPF_CHART_MEM_LENGTH -1, "aral_%s_size", name);
  1080. snprintfz(em->memory_allocations, NETDATA_EBPF_CHART_MEM_LENGTH -1, "aral_%s_alloc", name);
  1081. ebpf_write_chart_cmd(NETDATA_MONITORING_FAMILY,
  1082. em->memory_usage,
  1083. "Bytes allocated for ARAL.",
  1084. "bytes",
  1085. NETDATA_EBPF_FAMILY,
  1086. NETDATA_EBPF_CHART_TYPE_STACKED,
  1087. "netdata.ebpf_aral_stat_size",
  1088. priority++,
  1089. em->update_every,
  1090. NETDATA_EBPF_MODULE_NAME_PROCESS);
  1091. ebpf_write_global_dimension(mem,
  1092. mem,
  1093. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX]);
  1094. ebpf_write_chart_cmd(NETDATA_MONITORING_FAMILY,
  1095. em->memory_allocations,
  1096. "Calls to allocate memory.",
  1097. "calls",
  1098. NETDATA_EBPF_FAMILY,
  1099. NETDATA_EBPF_CHART_TYPE_STACKED,
  1100. "netdata.ebpf_aral_stat_alloc",
  1101. priority++,
  1102. em->update_every,
  1103. NETDATA_EBPF_MODULE_NAME_PROCESS);
  1104. ebpf_write_global_dimension(aral,
  1105. aral,
  1106. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX]);
  1107. }
  1108. /**
  1109. * Send data from aral chart
  1110. *
  1111. * Send data for eBPF plugin
  1112. *
  1113. * @param memory a pointer to the allocated address
  1114. * @param em a pointer to the structure with the default values.
  1115. */
  1116. void ebpf_send_data_aral_chart(ARAL *memory, ebpf_module_t *em)
  1117. {
  1118. char *mem = { NETDATA_EBPF_STAT_DIMENSION_MEMORY };
  1119. char *aral = { NETDATA_EBPF_STAT_DIMENSION_ARAL };
  1120. struct aral_statistics *stats = aral_statistics(memory);
  1121. write_begin_chart(NETDATA_MONITORING_FAMILY, em->memory_usage);
  1122. write_chart_dimension(mem, (long long)stats->structures.allocated_bytes);
  1123. write_end_chart();
  1124. write_begin_chart(NETDATA_MONITORING_FAMILY, em->memory_allocations);
  1125. write_chart_dimension(aral, (long long)stats->structures.allocations);
  1126. write_end_chart();
  1127. }
  1128. /*****************************************************************
  1129. *
  1130. * FUNCTIONS TO DEFINE OPTIONS
  1131. *
  1132. *****************************************************************/
  1133. /**
  1134. * Define labels used to generate charts
  1135. *
  1136. * @param is structure with information about number of calls made for a function.
  1137. * @param pio structure used to generate charts.
  1138. * @param dim a pointer for the dimensions name
  1139. * @param name a pointer for the tensor with the name of the functions.
  1140. * @param algorithm a vector with the algorithms used to make the charts
  1141. * @param end the number of elements in the previous 4 arguments.
  1142. */
  1143. void ebpf_global_labels(netdata_syscall_stat_t *is, netdata_publish_syscall_t *pio, char **dim,
  1144. char **name, int *algorithm, int end)
  1145. {
  1146. int i;
  1147. netdata_syscall_stat_t *prev = NULL;
  1148. netdata_publish_syscall_t *publish_prev = NULL;
  1149. for (i = 0; i < end; i++) {
  1150. if (prev) {
  1151. prev->next = &is[i];
  1152. }
  1153. prev = &is[i];
  1154. pio[i].dimension = dim[i];
  1155. pio[i].name = name[i];
  1156. pio[i].algorithm = ebpf_algorithms[algorithm[i]];
  1157. if (publish_prev) {
  1158. publish_prev->next = &pio[i];
  1159. }
  1160. publish_prev = &pio[i];
  1161. }
  1162. }
  1163. /**
  1164. * Define thread mode for all ebpf program.
  1165. *
  1166. * @param lmode the mode that will be used for them.
  1167. */
  1168. static inline void ebpf_set_thread_mode(netdata_run_mode_t lmode)
  1169. {
  1170. int i;
  1171. for (i = 0; ebpf_modules[i].thread_name; i++) {
  1172. ebpf_modules[i].mode = lmode;
  1173. }
  1174. }
  1175. /**
  1176. * Enable specific charts selected by user.
  1177. *
  1178. * @param em the structure that will be changed
  1179. * @param disable_apps the status about the apps charts.
  1180. * @param disable_cgroup the status about the cgroups charts.
  1181. */
  1182. static inline void ebpf_enable_specific_chart(struct ebpf_module *em, int disable_apps, int disable_cgroup)
  1183. {
  1184. em->enabled = CONFIG_BOOLEAN_YES;
  1185. // oomkill stores data inside apps submenu, so it always need to have apps_enabled for plugin to create
  1186. // its chart, without this comparison eBPF.plugin will try to store invalid data when apps is disabled.
  1187. if (!disable_apps || !strcmp(em->thread_name, "oomkill")) {
  1188. em->apps_charts = NETDATA_EBPF_APPS_FLAG_YES;
  1189. }
  1190. if (!disable_cgroup) {
  1191. em->cgroup_charts = CONFIG_BOOLEAN_YES;
  1192. }
  1193. em->global_charts = CONFIG_BOOLEAN_YES;
  1194. }
  1195. /**
  1196. * Enable all charts
  1197. *
  1198. * @param apps what is the current status of apps
  1199. * @param cgroups what is the current status of cgroups
  1200. */
  1201. static inline void ebpf_enable_all_charts(int apps, int cgroups)
  1202. {
  1203. int i;
  1204. for (i = 0; ebpf_modules[i].thread_name; i++) {
  1205. ebpf_enable_specific_chart(&ebpf_modules[i], apps, cgroups);
  1206. }
  1207. }
  1208. /**
  1209. * Disable all Global charts
  1210. *
  1211. * Disable charts
  1212. */
  1213. static inline void disable_all_global_charts()
  1214. {
  1215. int i;
  1216. for (i = 0; ebpf_modules[i].thread_name; i++) {
  1217. ebpf_modules[i].enabled = 0;
  1218. ebpf_modules[i].global_charts = 0;
  1219. }
  1220. }
  1221. /**
  1222. * Enable the specified chart group
  1223. *
  1224. * @param idx the index of ebpf_modules that I am enabling
  1225. * @param disable_apps should I keep apps charts?
  1226. */
  1227. static inline void ebpf_enable_chart(int idx, int disable_apps, int disable_cgroup)
  1228. {
  1229. int i;
  1230. for (i = 0; ebpf_modules[i].thread_name; i++) {
  1231. if (i == idx) {
  1232. ebpf_enable_specific_chart(&ebpf_modules[i], disable_apps, disable_cgroup);
  1233. break;
  1234. }
  1235. }
  1236. }
  1237. /**
  1238. * Disable APPs
  1239. *
  1240. * Disable charts for apps loading only global charts.
  1241. */
  1242. static inline void ebpf_disable_apps()
  1243. {
  1244. int i;
  1245. for (i = 0; ebpf_modules[i].thread_name; i++) {
  1246. ebpf_modules[i].apps_charts = NETDATA_EBPF_APPS_FLAG_NO;
  1247. }
  1248. }
  1249. /**
  1250. * Disable Cgroups
  1251. *
  1252. * Disable charts for apps loading only global charts.
  1253. */
  1254. static inline void ebpf_disable_cgroups()
  1255. {
  1256. int i;
  1257. for (i = 0; ebpf_modules[i].thread_name; i++) {
  1258. ebpf_modules[i].cgroup_charts = 0;
  1259. }
  1260. }
  1261. /**
  1262. * Update Disabled Plugins
  1263. *
  1264. * This function calls ebpf_update_stats to update statistics for collector.
  1265. *
  1266. * @param em a pointer to `struct ebpf_module`
  1267. */
  1268. void ebpf_update_disabled_plugin_stats(ebpf_module_t *em)
  1269. {
  1270. pthread_mutex_lock(&lock);
  1271. ebpf_update_stats(&plugin_statistics, em);
  1272. pthread_mutex_unlock(&lock);
  1273. }
  1274. /**
  1275. * Print help on standard error for user knows how to use the collector.
  1276. */
  1277. void ebpf_print_help()
  1278. {
  1279. const time_t t = time(NULL);
  1280. struct tm ct;
  1281. struct tm *test = localtime_r(&t, &ct);
  1282. int year;
  1283. if (test)
  1284. year = ct.tm_year;
  1285. else
  1286. year = 0;
  1287. fprintf(stderr,
  1288. "\n"
  1289. " Netdata ebpf.plugin %s\n"
  1290. " Copyright (C) 2016-%d Costa Tsaousis <costa@tsaousis.gr>\n"
  1291. " Released under GNU General Public License v3 or later.\n"
  1292. " All rights reserved.\n"
  1293. "\n"
  1294. " This eBPF.plugin is a data collector plugin for netdata.\n"
  1295. "\n"
  1296. " This plugin only accepts long options with one or two dashes. The available command line options are:\n"
  1297. "\n"
  1298. " SECONDS Set the data collection frequency.\n"
  1299. "\n"
  1300. " [-]-help Show this help.\n"
  1301. "\n"
  1302. " [-]-version Show software version.\n"
  1303. "\n"
  1304. " [-]-global Disable charts per application and cgroup.\n"
  1305. "\n"
  1306. " [-]-all Enable all chart groups (global, apps, and cgroup), unless -g is also given.\n"
  1307. "\n"
  1308. " [-]-cachestat Enable charts related to process run time.\n"
  1309. "\n"
  1310. " [-]-dcstat Enable charts related to directory cache.\n"
  1311. "\n"
  1312. " [-]-disk Enable charts related to disk monitoring.\n"
  1313. "\n"
  1314. " [-]-filesystem Enable chart related to filesystem run time.\n"
  1315. "\n"
  1316. " [-]-hardirq Enable chart related to hard IRQ latency.\n"
  1317. "\n"
  1318. " [-]-mdflush Enable charts related to multi-device flush.\n"
  1319. "\n"
  1320. " [-]-mount Enable charts related to mount monitoring.\n"
  1321. "\n"
  1322. " [-]-net Enable network viewer charts.\n"
  1323. "\n"
  1324. " [-]-oomkill Enable chart related to OOM kill tracking.\n"
  1325. "\n"
  1326. " [-]-process Enable charts related to process run time.\n"
  1327. "\n"
  1328. " [-]-return Run the collector in return mode.\n"
  1329. "\n"
  1330. " [-]-shm Enable chart related to shared memory tracking.\n"
  1331. "\n"
  1332. " [-]-softirq Enable chart related to soft IRQ latency.\n"
  1333. "\n"
  1334. " [-]-sync Enable chart related to sync run time.\n"
  1335. "\n"
  1336. " [-]-swap Enable chart related to swap run time.\n"
  1337. "\n"
  1338. " [-]-vfs Enable chart related to vfs run time.\n"
  1339. "\n"
  1340. " [-]-legacy Load legacy eBPF programs.\n"
  1341. "\n"
  1342. " [-]-core Use CO-RE when available(Working in progress).\n"
  1343. "\n",
  1344. VERSION,
  1345. (year >= 116) ? year + 1900 : 2020);
  1346. }
  1347. /*****************************************************************
  1348. *
  1349. * TRACEPOINT MANAGEMENT FUNCTIONS
  1350. *
  1351. *****************************************************************/
  1352. /**
  1353. * Enable a tracepoint.
  1354. *
  1355. * @return 0 on success, -1 on error.
  1356. */
  1357. int ebpf_enable_tracepoint(ebpf_tracepoint_t *tp)
  1358. {
  1359. int test = ebpf_is_tracepoint_enabled(tp->class, tp->event);
  1360. // err?
  1361. if (test == -1) {
  1362. return -1;
  1363. }
  1364. // disabled?
  1365. else if (test == 0) {
  1366. // enable it then.
  1367. if (ebpf_enable_tracing_values(tp->class, tp->event)) {
  1368. return -1;
  1369. }
  1370. }
  1371. // enabled now or already was.
  1372. tp->enabled = true;
  1373. return 0;
  1374. }
  1375. /**
  1376. * Disable a tracepoint if it's enabled.
  1377. *
  1378. * @return 0 on success, -1 on error.
  1379. */
  1380. int ebpf_disable_tracepoint(ebpf_tracepoint_t *tp)
  1381. {
  1382. int test = ebpf_is_tracepoint_enabled(tp->class, tp->event);
  1383. // err?
  1384. if (test == -1) {
  1385. return -1;
  1386. }
  1387. // enabled?
  1388. else if (test == 1) {
  1389. // disable it then.
  1390. if (ebpf_disable_tracing_values(tp->class, tp->event)) {
  1391. return -1;
  1392. }
  1393. }
  1394. // disable now or already was.
  1395. tp->enabled = false;
  1396. return 0;
  1397. }
  1398. /**
  1399. * Enable multiple tracepoints on a list of tracepoints which end when the
  1400. * class is NULL.
  1401. *
  1402. * @return the number of successful enables.
  1403. */
  1404. uint32_t ebpf_enable_tracepoints(ebpf_tracepoint_t *tps)
  1405. {
  1406. uint32_t cnt = 0;
  1407. for (int i = 0; tps[i].class != NULL; i++) {
  1408. if (ebpf_enable_tracepoint(&tps[i]) == -1) {
  1409. infoerr("failed to enable tracepoint %s:%s",
  1410. tps[i].class, tps[i].event);
  1411. }
  1412. else {
  1413. cnt += 1;
  1414. }
  1415. }
  1416. return cnt;
  1417. }
  1418. /*****************************************************************
  1419. *
  1420. * AUXILIARY FUNCTIONS USED DURING INITIALIZATION
  1421. *
  1422. *****************************************************************/
  1423. /**
  1424. * Read Local Ports
  1425. *
  1426. * Parse /proc/net/{tcp,udp} and get the ports Linux is listening.
  1427. *
  1428. * @param filename the proc file to parse.
  1429. * @param proto is the magic number associated to the protocol file we are reading.
  1430. */
  1431. static void read_local_ports(char *filename, uint8_t proto)
  1432. {
  1433. procfile *ff = procfile_open(filename, " \t:", PROCFILE_FLAG_DEFAULT);
  1434. if (!ff)
  1435. return;
  1436. ff = procfile_readall(ff);
  1437. if (!ff)
  1438. return;
  1439. size_t lines = procfile_lines(ff), l;
  1440. netdata_passive_connection_t values = {.counter = 0, .tgid = 0, .pid = 0};
  1441. for(l = 0; l < lines ;l++) {
  1442. size_t words = procfile_linewords(ff, l);
  1443. // This is header or end of file
  1444. if (unlikely(words < 14))
  1445. continue;
  1446. // https://elixir.bootlin.com/linux/v5.7.8/source/include/net/tcp_states.h
  1447. // 0A = TCP_LISTEN
  1448. if (strcmp("0A", procfile_lineword(ff, l, 5)))
  1449. continue;
  1450. // Read local port
  1451. uint16_t port = (uint16_t)strtol(procfile_lineword(ff, l, 2), NULL, 16);
  1452. update_listen_table(htons(port), proto, &values);
  1453. }
  1454. procfile_close(ff);
  1455. }
  1456. /**
  1457. * Read Local addresseses
  1458. *
  1459. * Read the local address from the interfaces.
  1460. */
  1461. static void read_local_addresses()
  1462. {
  1463. struct ifaddrs *ifaddr, *ifa;
  1464. if (getifaddrs(&ifaddr) == -1) {
  1465. error("Cannot get the local IP addresses, it is no possible to do separation between inbound and outbound connections");
  1466. return;
  1467. }
  1468. char *notext = { "No text representation" };
  1469. for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
  1470. if (ifa->ifa_addr == NULL)
  1471. continue;
  1472. if ((ifa->ifa_addr->sa_family != AF_INET) && (ifa->ifa_addr->sa_family != AF_INET6))
  1473. continue;
  1474. ebpf_network_viewer_ip_list_t *w = callocz(1, sizeof(ebpf_network_viewer_ip_list_t));
  1475. int family = ifa->ifa_addr->sa_family;
  1476. w->ver = (uint8_t) family;
  1477. char text[INET6_ADDRSTRLEN];
  1478. if (family == AF_INET) {
  1479. struct sockaddr_in *in = (struct sockaddr_in*) ifa->ifa_addr;
  1480. w->first.addr32[0] = in->sin_addr.s_addr;
  1481. w->last.addr32[0] = in->sin_addr.s_addr;
  1482. if (inet_ntop(AF_INET, w->first.addr8, text, INET_ADDRSTRLEN)) {
  1483. w->value = strdupz(text);
  1484. w->hash = simple_hash(text);
  1485. } else {
  1486. w->value = strdupz(notext);
  1487. w->hash = simple_hash(notext);
  1488. }
  1489. } else {
  1490. struct sockaddr_in6 *in6 = (struct sockaddr_in6*) ifa->ifa_addr;
  1491. memcpy(w->first.addr8, (void *)&in6->sin6_addr, sizeof(struct in6_addr));
  1492. memcpy(w->last.addr8, (void *)&in6->sin6_addr, sizeof(struct in6_addr));
  1493. if (inet_ntop(AF_INET6, w->first.addr8, text, INET_ADDRSTRLEN)) {
  1494. w->value = strdupz(text);
  1495. w->hash = simple_hash(text);
  1496. } else {
  1497. w->value = strdupz(notext);
  1498. w->hash = simple_hash(notext);
  1499. }
  1500. }
  1501. ebpf_fill_ip_list((family == AF_INET)?&network_viewer_opt.ipv4_local_ip:&network_viewer_opt.ipv6_local_ip,
  1502. w,
  1503. "selector");
  1504. }
  1505. freeifaddrs(ifaddr);
  1506. }
  1507. /**
  1508. * Start Pthread Variable
  1509. *
  1510. * This function starts all pthread variables.
  1511. */
  1512. void ebpf_start_pthread_variables()
  1513. {
  1514. pthread_mutex_init(&lock, NULL);
  1515. pthread_mutex_init(&ebpf_exit_cleanup, NULL);
  1516. pthread_mutex_init(&collect_data_mutex, NULL);
  1517. pthread_mutex_init(&mutex_cgroup_shm, NULL);
  1518. }
  1519. /**
  1520. * Am I collecting PIDs?
  1521. *
  1522. * Test if eBPF plugin needs to collect PID information.
  1523. *
  1524. * @return It returns 1 if at least one thread needs to collect the data, or zero otherwise.
  1525. */
  1526. static inline uint32_t ebpf_am_i_collect_pids()
  1527. {
  1528. uint32_t ret = 0;
  1529. int i;
  1530. for (i = 0; ebpf_modules[i].thread_name; i++) {
  1531. ret |= ebpf_modules[i].cgroup_charts | (ebpf_modules[i].apps_charts & NETDATA_EBPF_APPS_FLAG_YES);
  1532. }
  1533. return ret;
  1534. }
  1535. /**
  1536. * Allocate the vectors used for all threads.
  1537. */
  1538. static void ebpf_allocate_common_vectors()
  1539. {
  1540. if (unlikely(!ebpf_am_i_collect_pids())) {
  1541. return;
  1542. }
  1543. ebpf_all_pids = callocz((size_t)pid_max, sizeof(struct ebpf_pid_stat *));
  1544. ebpf_aral_init();
  1545. }
  1546. /**
  1547. * Define how to load the ebpf programs
  1548. *
  1549. * @param ptr the option given by users
  1550. */
  1551. static inline void how_to_load(char *ptr)
  1552. {
  1553. if (!strcasecmp(ptr, EBPF_CFG_LOAD_MODE_RETURN))
  1554. ebpf_set_thread_mode(MODE_RETURN);
  1555. else if (!strcasecmp(ptr, EBPF_CFG_LOAD_MODE_DEFAULT))
  1556. ebpf_set_thread_mode(MODE_ENTRY);
  1557. else
  1558. error("the option %s for \"ebpf load mode\" is not a valid option.", ptr);
  1559. }
  1560. /**
  1561. * Update interval
  1562. *
  1563. * Update default interval with value from user
  1564. *
  1565. * @param update_every value to overwrite the update frequency set by the server.
  1566. */
  1567. static void ebpf_update_interval(int update_every)
  1568. {
  1569. int i;
  1570. int value = (int) appconfig_get_number(&collector_config, EBPF_GLOBAL_SECTION, EBPF_CFG_UPDATE_EVERY,
  1571. update_every);
  1572. for (i = 0; ebpf_modules[i].thread_name; i++) {
  1573. ebpf_modules[i].update_every = value;
  1574. }
  1575. }
  1576. /**
  1577. * Update PID table size
  1578. *
  1579. * Update default size with value from user
  1580. */
  1581. static void ebpf_update_table_size()
  1582. {
  1583. int i;
  1584. uint32_t value = (uint32_t) appconfig_get_number(&collector_config, EBPF_GLOBAL_SECTION,
  1585. EBPF_CFG_PID_SIZE, ND_EBPF_DEFAULT_PID_SIZE);
  1586. for (i = 0; ebpf_modules[i].thread_name; i++) {
  1587. ebpf_modules[i].pid_map_size = value;
  1588. }
  1589. }
  1590. /**
  1591. * Set Load mode
  1592. *
  1593. * @param origin specify the configuration file loaded
  1594. */
  1595. static inline void ebpf_set_load_mode(netdata_ebpf_load_mode_t load, netdata_ebpf_load_mode_t origin)
  1596. {
  1597. int i;
  1598. for (i = 0; ebpf_modules[i].thread_name; i++) {
  1599. ebpf_modules[i].load &= ~NETDATA_EBPF_LOAD_METHODS;
  1600. ebpf_modules[i].load |= load | origin ;
  1601. }
  1602. }
  1603. /**
  1604. * Update mode
  1605. *
  1606. * @param str value read from configuration file.
  1607. * @param origin specify the configuration file loaded
  1608. */
  1609. static inline void epbf_update_load_mode(char *str, netdata_ebpf_load_mode_t origin)
  1610. {
  1611. netdata_ebpf_load_mode_t load = epbf_convert_string_to_load_mode(str);
  1612. ebpf_set_load_mode(load, origin);
  1613. }
  1614. /**
  1615. * Update Map per core
  1616. *
  1617. * Define the map type used with some hash tables.
  1618. */
  1619. static void ebpf_update_map_per_core()
  1620. {
  1621. int i;
  1622. int value = appconfig_get_boolean(&collector_config, EBPF_GLOBAL_SECTION,
  1623. EBPF_CFG_MAPS_PER_CORE, CONFIG_BOOLEAN_YES);
  1624. for (i = 0; ebpf_modules[i].thread_name; i++) {
  1625. ebpf_modules[i].maps_per_core = value;
  1626. }
  1627. }
  1628. /**
  1629. * Read collector values
  1630. *
  1631. * @param disable_apps variable to store information related to apps.
  1632. * @param disable_cgroups variable to store information related to cgroups.
  1633. * @param update_every value to overwrite the update frequency set by the server.
  1634. * @param origin specify the configuration file loaded
  1635. */
  1636. static void read_collector_values(int *disable_apps, int *disable_cgroups,
  1637. int update_every, netdata_ebpf_load_mode_t origin)
  1638. {
  1639. // Read global section
  1640. char *value;
  1641. if (appconfig_exists(&collector_config, EBPF_GLOBAL_SECTION, "load")) // Backward compatibility
  1642. value = appconfig_get(&collector_config, EBPF_GLOBAL_SECTION, "load",
  1643. EBPF_CFG_LOAD_MODE_DEFAULT);
  1644. else
  1645. value = appconfig_get(&collector_config, EBPF_GLOBAL_SECTION, EBPF_CFG_LOAD_MODE,
  1646. EBPF_CFG_LOAD_MODE_DEFAULT);
  1647. how_to_load(value);
  1648. btf_path = appconfig_get(&collector_config, EBPF_GLOBAL_SECTION, EBPF_CFG_PROGRAM_PATH,
  1649. EBPF_DEFAULT_BTF_PATH);
  1650. #ifdef LIBBPF_MAJOR_VERSION
  1651. default_btf = ebpf_load_btf_file(btf_path, EBPF_DEFAULT_BTF_FILE);
  1652. #endif
  1653. value = appconfig_get(&collector_config, EBPF_GLOBAL_SECTION, EBPF_CFG_TYPE_FORMAT, EBPF_CFG_DEFAULT_PROGRAM);
  1654. epbf_update_load_mode(value, origin);
  1655. ebpf_update_interval(update_every);
  1656. ebpf_update_table_size();
  1657. // This is kept to keep compatibility
  1658. uint32_t enabled = appconfig_get_boolean(&collector_config, EBPF_GLOBAL_SECTION, "disable apps",
  1659. CONFIG_BOOLEAN_NO);
  1660. if (!enabled) {
  1661. // Apps is a positive sentence, so we need to invert the values to disable apps.
  1662. enabled = appconfig_get_boolean(&collector_config, EBPF_GLOBAL_SECTION, EBPF_CFG_APPLICATION,
  1663. CONFIG_BOOLEAN_YES);
  1664. enabled = (enabled == CONFIG_BOOLEAN_NO)?CONFIG_BOOLEAN_YES:CONFIG_BOOLEAN_NO;
  1665. }
  1666. *disable_apps = (int)enabled;
  1667. // Cgroup is a positive sentence, so we need to invert the values to disable apps.
  1668. // We are using the same pattern for cgroup and apps
  1669. enabled = appconfig_get_boolean(&collector_config, EBPF_GLOBAL_SECTION, EBPF_CFG_CGROUP, CONFIG_BOOLEAN_NO);
  1670. *disable_cgroups = (enabled == CONFIG_BOOLEAN_NO)?CONFIG_BOOLEAN_YES:CONFIG_BOOLEAN_NO;
  1671. ebpf_update_map_per_core();
  1672. // Read ebpf programs section
  1673. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION,
  1674. ebpf_modules[EBPF_MODULE_PROCESS_IDX].config_name, CONFIG_BOOLEAN_YES);
  1675. int started = 0;
  1676. if (enabled) {
  1677. ebpf_enable_chart(EBPF_MODULE_PROCESS_IDX, *disable_apps, *disable_cgroups);
  1678. started++;
  1679. }
  1680. // This is kept to keep compatibility
  1681. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "network viewer",
  1682. CONFIG_BOOLEAN_NO);
  1683. if (!enabled)
  1684. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION,
  1685. ebpf_modules[EBPF_MODULE_SOCKET_IDX].config_name,
  1686. CONFIG_BOOLEAN_NO);
  1687. if (enabled) {
  1688. ebpf_enable_chart(EBPF_MODULE_SOCKET_IDX, *disable_apps, *disable_cgroups);
  1689. started++;
  1690. }
  1691. // This is kept to keep compatibility
  1692. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "network connection monitoring",
  1693. CONFIG_BOOLEAN_NO);
  1694. if (!enabled)
  1695. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "network connections",
  1696. CONFIG_BOOLEAN_NO);
  1697. network_viewer_opt.enabled = enabled;
  1698. if (enabled) {
  1699. if (!ebpf_modules[EBPF_MODULE_SOCKET_IDX].enabled)
  1700. ebpf_enable_chart(EBPF_MODULE_SOCKET_IDX, *disable_apps, *disable_cgroups);
  1701. // Read network viewer section if network viewer is enabled
  1702. // This is kept here to keep backward compatibility
  1703. parse_network_viewer_section(&collector_config);
  1704. parse_service_name_section(&collector_config);
  1705. started++;
  1706. }
  1707. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "cachestat",
  1708. CONFIG_BOOLEAN_NO);
  1709. if (enabled) {
  1710. ebpf_enable_chart(EBPF_MODULE_CACHESTAT_IDX, *disable_apps, *disable_cgroups);
  1711. started++;
  1712. }
  1713. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "sync",
  1714. CONFIG_BOOLEAN_YES);
  1715. if (enabled) {
  1716. ebpf_enable_chart(EBPF_MODULE_SYNC_IDX, *disable_apps, *disable_cgroups);
  1717. started++;
  1718. }
  1719. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "dcstat",
  1720. CONFIG_BOOLEAN_NO);
  1721. if (enabled) {
  1722. ebpf_enable_chart(EBPF_MODULE_DCSTAT_IDX, *disable_apps, *disable_cgroups);
  1723. started++;
  1724. }
  1725. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "swap",
  1726. CONFIG_BOOLEAN_NO);
  1727. if (enabled) {
  1728. ebpf_enable_chart(EBPF_MODULE_SWAP_IDX, *disable_apps, *disable_cgroups);
  1729. started++;
  1730. }
  1731. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "vfs",
  1732. CONFIG_BOOLEAN_NO);
  1733. if (enabled) {
  1734. ebpf_enable_chart(EBPF_MODULE_VFS_IDX, *disable_apps, *disable_cgroups);
  1735. started++;
  1736. }
  1737. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "filesystem",
  1738. CONFIG_BOOLEAN_NO);
  1739. if (enabled) {
  1740. ebpf_enable_chart(EBPF_MODULE_FILESYSTEM_IDX, *disable_apps, *disable_cgroups);
  1741. started++;
  1742. }
  1743. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "disk",
  1744. CONFIG_BOOLEAN_NO);
  1745. if (enabled) {
  1746. ebpf_enable_chart(EBPF_MODULE_DISK_IDX, *disable_apps, *disable_cgroups);
  1747. started++;
  1748. }
  1749. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "mount",
  1750. CONFIG_BOOLEAN_YES);
  1751. if (enabled) {
  1752. ebpf_enable_chart(EBPF_MODULE_MOUNT_IDX, *disable_apps, *disable_cgroups);
  1753. started++;
  1754. }
  1755. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "fd",
  1756. CONFIG_BOOLEAN_YES);
  1757. if (enabled) {
  1758. ebpf_enable_chart(EBPF_MODULE_FD_IDX, *disable_apps, *disable_cgroups);
  1759. started++;
  1760. }
  1761. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "hardirq",
  1762. CONFIG_BOOLEAN_YES);
  1763. if (enabled) {
  1764. ebpf_enable_chart(EBPF_MODULE_HARDIRQ_IDX, *disable_apps, *disable_cgroups);
  1765. started++;
  1766. }
  1767. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "softirq",
  1768. CONFIG_BOOLEAN_YES);
  1769. if (enabled) {
  1770. ebpf_enable_chart(EBPF_MODULE_SOFTIRQ_IDX, *disable_apps, *disable_cgroups);
  1771. started++;
  1772. }
  1773. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "oomkill",
  1774. CONFIG_BOOLEAN_YES);
  1775. if (enabled) {
  1776. ebpf_enable_chart(EBPF_MODULE_OOMKILL_IDX, *disable_apps, *disable_cgroups);
  1777. started++;
  1778. }
  1779. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "shm",
  1780. CONFIG_BOOLEAN_YES);
  1781. if (enabled) {
  1782. ebpf_enable_chart(EBPF_MODULE_SHM_IDX, *disable_apps, *disable_cgroups);
  1783. started++;
  1784. }
  1785. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "mdflush",
  1786. CONFIG_BOOLEAN_NO);
  1787. if (enabled) {
  1788. ebpf_enable_chart(EBPF_MODULE_MDFLUSH_IDX, *disable_apps, *disable_cgroups);
  1789. started++;
  1790. }
  1791. if (!started){
  1792. ebpf_enable_all_charts(*disable_apps, *disable_cgroups);
  1793. // Read network viewer section
  1794. // This is kept here to keep backward compatibility
  1795. if (network_viewer_opt.enabled) {
  1796. parse_network_viewer_section(&collector_config);
  1797. parse_service_name_section(&collector_config);
  1798. }
  1799. }
  1800. }
  1801. /**
  1802. * Load collector config
  1803. *
  1804. * @param path the path where the file ebpf.conf is stored.
  1805. * @param disable_apps variable to store the information about apps plugin status.
  1806. * @param disable_cgroups variable to store the information about cgroups plugin status.
  1807. * @param update_every value to overwrite the update frequency set by the server.
  1808. *
  1809. * @return 0 on success and -1 otherwise.
  1810. */
  1811. static int load_collector_config(char *path, int *disable_apps, int *disable_cgroups, int update_every)
  1812. {
  1813. char lpath[4096];
  1814. netdata_ebpf_load_mode_t origin;
  1815. snprintf(lpath, 4095, "%s/%s", path, NETDATA_EBPF_CONFIG_FILE);
  1816. if (!appconfig_load(&collector_config, lpath, 0, NULL)) {
  1817. snprintf(lpath, 4095, "%s/%s", path, NETDATA_EBPF_OLD_CONFIG_FILE);
  1818. if (!appconfig_load(&collector_config, lpath, 0, NULL)) {
  1819. return -1;
  1820. }
  1821. origin = EBPF_LOADED_FROM_STOCK;
  1822. } else
  1823. origin = EBPF_LOADED_FROM_USER;
  1824. read_collector_values(disable_apps, disable_cgroups, update_every, origin);
  1825. return 0;
  1826. }
  1827. /**
  1828. * Set global variables reading environment variables
  1829. */
  1830. void set_global_variables()
  1831. {
  1832. // Get environment variables
  1833. ebpf_plugin_dir = getenv("NETDATA_PLUGINS_DIR");
  1834. if (!ebpf_plugin_dir)
  1835. ebpf_plugin_dir = PLUGINS_DIR;
  1836. ebpf_user_config_dir = getenv("NETDATA_USER_CONFIG_DIR");
  1837. if (!ebpf_user_config_dir)
  1838. ebpf_user_config_dir = CONFIG_DIR;
  1839. ebpf_stock_config_dir = getenv("NETDATA_STOCK_CONFIG_DIR");
  1840. if (!ebpf_stock_config_dir)
  1841. ebpf_stock_config_dir = LIBCONFIG_DIR;
  1842. ebpf_configured_log_dir = getenv("NETDATA_LOG_DIR");
  1843. if (!ebpf_configured_log_dir)
  1844. ebpf_configured_log_dir = LOG_DIR;
  1845. ebpf_nprocs = (int)sysconf(_SC_NPROCESSORS_ONLN);
  1846. if (ebpf_nprocs < 0) {
  1847. ebpf_nprocs = NETDATA_MAX_PROCESSOR;
  1848. error("Cannot identify number of process, using default value %d", ebpf_nprocs);
  1849. }
  1850. isrh = get_redhat_release();
  1851. pid_max = get_system_pid_max();
  1852. running_on_kernel = ebpf_get_kernel_version();
  1853. }
  1854. /**
  1855. * Load collector config
  1856. */
  1857. static inline void ebpf_load_thread_config()
  1858. {
  1859. int i;
  1860. for (i = 0; ebpf_modules[i].thread_name; i++) {
  1861. ebpf_update_module(&ebpf_modules[i], default_btf, running_on_kernel, isrh);
  1862. }
  1863. }
  1864. /**
  1865. * Check Conditions
  1866. *
  1867. * This function checks kernel that plugin is running and permissions.
  1868. *
  1869. * @return It returns 0 on success and -1 otherwise
  1870. */
  1871. int ebpf_check_conditions()
  1872. {
  1873. if (!has_condition_to_run(running_on_kernel)) {
  1874. error("The current collector cannot run on this kernel.");
  1875. return -1;
  1876. }
  1877. if (!am_i_running_as_root()) {
  1878. error(
  1879. "ebpf.plugin should either run as root (now running with uid %u, euid %u) or have special capabilities..",
  1880. (unsigned int)getuid(), (unsigned int)geteuid());
  1881. return -1;
  1882. }
  1883. return 0;
  1884. }
  1885. /**
  1886. * Adjust memory
  1887. *
  1888. * Adjust memory values to load eBPF programs.
  1889. *
  1890. * @return It returns 0 on success and -1 otherwise
  1891. */
  1892. int ebpf_adjust_memory_limit()
  1893. {
  1894. struct rlimit r = { RLIM_INFINITY, RLIM_INFINITY };
  1895. if (setrlimit(RLIMIT_MEMLOCK, &r)) {
  1896. error("Setrlimit(RLIMIT_MEMLOCK)");
  1897. return -1;
  1898. }
  1899. return 0;
  1900. }
  1901. /**
  1902. * Parse arguments given from user.
  1903. *
  1904. * @param argc the number of arguments
  1905. * @param argv the pointer to the arguments
  1906. */
  1907. static void ebpf_parse_args(int argc, char **argv)
  1908. {
  1909. int disable_apps = 0;
  1910. int disable_cgroups = 1;
  1911. int freq = 0;
  1912. int option_index = 0;
  1913. uint64_t select_threads = 0;
  1914. static struct option long_options[] = {
  1915. {"process", no_argument, 0, 0 },
  1916. {"net", no_argument, 0, 0 },
  1917. {"cachestat", no_argument, 0, 0 },
  1918. {"sync", no_argument, 0, 0 },
  1919. {"dcstat", no_argument, 0, 0 },
  1920. {"swap", no_argument, 0, 0 },
  1921. {"vfs", no_argument, 0, 0 },
  1922. {"filesystem", no_argument, 0, 0 },
  1923. {"disk", no_argument, 0, 0 },
  1924. {"mount", no_argument, 0, 0 },
  1925. {"filedescriptor", no_argument, 0, 0 },
  1926. {"hardirq", no_argument, 0, 0 },
  1927. {"softirq", no_argument, 0, 0 },
  1928. {"oomkill", no_argument, 0, 0 },
  1929. {"shm", no_argument, 0, 0 },
  1930. {"mdflush", no_argument, 0, 0 },
  1931. /* INSERT NEW THREADS BEFORE THIS COMMENT TO KEEP COMPATIBILITY WITH enum ebpf_module_indexes */
  1932. {"all", no_argument, 0, 0 },
  1933. {"version", no_argument, 0, 0 },
  1934. {"help", no_argument, 0, 0 },
  1935. {"global", no_argument, 0, 0 },
  1936. {"return", no_argument, 0, 0 },
  1937. {"legacy", no_argument, 0, 0 },
  1938. {"core", no_argument, 0, 0 },
  1939. {"unittest", no_argument, 0, 0 },
  1940. {0, 0, 0, 0}
  1941. };
  1942. memset(&network_viewer_opt, 0, sizeof(network_viewer_opt));
  1943. network_viewer_opt.max_dim = NETDATA_NV_CAP_VALUE;
  1944. if (argc > 1) {
  1945. int n = (int)str2l(argv[1]);
  1946. if (n > 0) {
  1947. freq = n;
  1948. }
  1949. }
  1950. if (!freq)
  1951. freq = EBPF_DEFAULT_UPDATE_EVERY;
  1952. if (load_collector_config(ebpf_user_config_dir, &disable_apps, &disable_cgroups, freq)) {
  1953. info(
  1954. "Does not have a configuration file inside `%s/ebpf.d.conf. It will try to load stock file.",
  1955. ebpf_user_config_dir);
  1956. if (load_collector_config(ebpf_stock_config_dir, &disable_apps, &disable_cgroups, freq)) {
  1957. info("Does not have a stock file. It is starting with default options.");
  1958. }
  1959. }
  1960. ebpf_load_thread_config();
  1961. while (1) {
  1962. int c = getopt_long_only(argc, argv, "", long_options, &option_index);
  1963. if (c == -1)
  1964. break;
  1965. switch (option_index) {
  1966. case EBPF_MODULE_PROCESS_IDX: {
  1967. select_threads |= 1<<EBPF_MODULE_PROCESS_IDX;
  1968. #ifdef NETDATA_INTERNAL_CHECKS
  1969. info("EBPF enabling \"PROCESS\" charts, because it was started with the option \"[-]-process\".");
  1970. #endif
  1971. break;
  1972. }
  1973. case EBPF_MODULE_SOCKET_IDX: {
  1974. select_threads |= 1<<EBPF_MODULE_SOCKET_IDX;
  1975. #ifdef NETDATA_INTERNAL_CHECKS
  1976. info("EBPF enabling \"NET\" charts, because it was started with the option \"[-]-net\".");
  1977. #endif
  1978. break;
  1979. }
  1980. case EBPF_MODULE_CACHESTAT_IDX: {
  1981. select_threads |= 1<<EBPF_MODULE_CACHESTAT_IDX;
  1982. #ifdef NETDATA_INTERNAL_CHECKS
  1983. info("EBPF enabling \"CACHESTAT\" charts, because it was started with the option \"[-]-cachestat\".");
  1984. #endif
  1985. break;
  1986. }
  1987. case EBPF_MODULE_SYNC_IDX: {
  1988. select_threads |= 1<<EBPF_MODULE_SYNC_IDX;
  1989. #ifdef NETDATA_INTERNAL_CHECKS
  1990. info("EBPF enabling \"SYNC\" chart, because it was started with the option \"[-]-sync\".");
  1991. #endif
  1992. break;
  1993. }
  1994. case EBPF_MODULE_DCSTAT_IDX: {
  1995. select_threads |= 1<<EBPF_MODULE_DCSTAT_IDX;
  1996. #ifdef NETDATA_INTERNAL_CHECKS
  1997. info("EBPF enabling \"DCSTAT\" charts, because it was started with the option \"[-]-dcstat\".");
  1998. #endif
  1999. break;
  2000. }
  2001. case EBPF_MODULE_SWAP_IDX: {
  2002. select_threads |= 1<<EBPF_MODULE_SWAP_IDX;
  2003. #ifdef NETDATA_INTERNAL_CHECKS
  2004. info("EBPF enabling \"SWAP\" chart, because it was started with the option \"[-]-swap\".");
  2005. #endif
  2006. break;
  2007. }
  2008. case EBPF_MODULE_VFS_IDX: {
  2009. select_threads |= 1<<EBPF_MODULE_VFS_IDX;
  2010. #ifdef NETDATA_INTERNAL_CHECKS
  2011. info("EBPF enabling \"VFS\" chart, because it was started with the option \"[-]-vfs\".");
  2012. #endif
  2013. break;
  2014. }
  2015. case EBPF_MODULE_FILESYSTEM_IDX: {
  2016. select_threads |= 1<<EBPF_MODULE_FILESYSTEM_IDX;
  2017. #ifdef NETDATA_INTERNAL_CHECKS
  2018. info("EBPF enabling \"FILESYSTEM\" chart, because it was started with the option \"[-]-filesystem\".");
  2019. #endif
  2020. break;
  2021. }
  2022. case EBPF_MODULE_DISK_IDX: {
  2023. select_threads |= 1<<EBPF_MODULE_DISK_IDX;
  2024. #ifdef NETDATA_INTERNAL_CHECKS
  2025. info("EBPF enabling \"DISK\" chart, because it was started with the option \"[-]-disk\".");
  2026. #endif
  2027. break;
  2028. }
  2029. case EBPF_MODULE_MOUNT_IDX: {
  2030. select_threads |= 1<<EBPF_MODULE_MOUNT_IDX;
  2031. #ifdef NETDATA_INTERNAL_CHECKS
  2032. info("EBPF enabling \"MOUNT\" chart, because it was started with the option \"[-]-mount\".");
  2033. #endif
  2034. break;
  2035. }
  2036. case EBPF_MODULE_FD_IDX: {
  2037. select_threads |= 1<<EBPF_MODULE_FD_IDX;
  2038. #ifdef NETDATA_INTERNAL_CHECKS
  2039. info("EBPF enabling \"FILEDESCRIPTOR\" chart, because it was started with the option \"[-]-filedescriptor\".");
  2040. #endif
  2041. break;
  2042. }
  2043. case EBPF_MODULE_HARDIRQ_IDX: {
  2044. select_threads |= 1<<EBPF_MODULE_HARDIRQ_IDX;
  2045. #ifdef NETDATA_INTERNAL_CHECKS
  2046. info("EBPF enabling \"HARDIRQ\" chart, because it was started with the option \"[-]-hardirq\".");
  2047. #endif
  2048. break;
  2049. }
  2050. case EBPF_MODULE_SOFTIRQ_IDX: {
  2051. select_threads |= 1<<EBPF_MODULE_SOFTIRQ_IDX;
  2052. #ifdef NETDATA_INTERNAL_CHECKS
  2053. info("EBPF enabling \"SOFTIRQ\" chart, because it was started with the option \"[-]-softirq\".");
  2054. #endif
  2055. break;
  2056. }
  2057. case EBPF_MODULE_OOMKILL_IDX: {
  2058. select_threads |= 1<<EBPF_MODULE_OOMKILL_IDX;
  2059. #ifdef NETDATA_INTERNAL_CHECKS
  2060. info("EBPF enabling \"OOMKILL\" chart, because it was started with the option \"[-]-oomkill\".");
  2061. #endif
  2062. break;
  2063. }
  2064. case EBPF_MODULE_SHM_IDX: {
  2065. select_threads |= 1<<EBPF_MODULE_SHM_IDX;
  2066. #ifdef NETDATA_INTERNAL_CHECKS
  2067. info("EBPF enabling \"SHM\" chart, because it was started with the option \"[-]-shm\".");
  2068. #endif
  2069. break;
  2070. }
  2071. case EBPF_MODULE_MDFLUSH_IDX: {
  2072. select_threads |= 1<<EBPF_MODULE_MDFLUSH_IDX;
  2073. #ifdef NETDATA_INTERNAL_CHECKS
  2074. info("EBPF enabling \"MDFLUSH\" chart, because it was started with the option \"[-]-mdflush\".");
  2075. #endif
  2076. break;
  2077. }
  2078. case EBPF_OPTION_ALL_CHARTS: {
  2079. disable_apps = 0;
  2080. disable_cgroups = 0;
  2081. #ifdef NETDATA_INTERNAL_CHECKS
  2082. info("EBPF running with all chart groups, because it was started with the option \"[-]-all\".");
  2083. #endif
  2084. break;
  2085. }
  2086. case EBPF_OPTION_VERSION: {
  2087. printf("ebpf.plugin %s\n", VERSION);
  2088. exit(0);
  2089. }
  2090. case EBPF_OPTION_HELP: {
  2091. ebpf_print_help();
  2092. exit(0);
  2093. }
  2094. case EBPF_OPTION_GLOBAL_CHART: {
  2095. disable_apps = 1;
  2096. disable_cgroups = 1;
  2097. #ifdef NETDATA_INTERNAL_CHECKS
  2098. info("EBPF running with global chart group, because it was started with the option \"[-]-global\".");
  2099. #endif
  2100. break;
  2101. }
  2102. case EBPF_OPTION_RETURN_MODE: {
  2103. ebpf_set_thread_mode(MODE_RETURN);
  2104. #ifdef NETDATA_INTERNAL_CHECKS
  2105. info("EBPF running in \"RETURN\" mode, because it was started with the option \"[-]-return\".");
  2106. #endif
  2107. break;
  2108. }
  2109. case EBPF_OPTION_LEGACY: {
  2110. ebpf_set_load_mode(EBPF_LOAD_LEGACY, EBPF_LOADED_FROM_USER);
  2111. #ifdef NETDATA_INTERNAL_CHECKS
  2112. info("EBPF running with \"LEGACY\" code, because it was started with the option \"[-]-legacy\".");
  2113. #endif
  2114. break;
  2115. }
  2116. case EBPF_OPTION_CORE: {
  2117. ebpf_set_load_mode(EBPF_LOAD_CORE, EBPF_LOADED_FROM_USER);
  2118. #ifdef NETDATA_INTERNAL_CHECKS
  2119. info("EBPF running with \"CO-RE\" code, because it was started with the option \"[-]-core\".");
  2120. #endif
  2121. break;
  2122. }
  2123. case EBPF_OPTION_UNITTEST: {
  2124. // if we cannot run until the end, we will cancel the unittest
  2125. int exit_code = ECANCELED;
  2126. if (ebpf_check_conditions())
  2127. goto unittest;
  2128. if (ebpf_adjust_memory_limit())
  2129. goto unittest;
  2130. // Load binary in entry mode
  2131. ebpf_ut_initialize_structure(MODE_ENTRY);
  2132. if (ebpf_ut_load_real_binary())
  2133. goto unittest;
  2134. ebpf_ut_cleanup_memory();
  2135. // Do not load a binary in entry mode
  2136. ebpf_ut_initialize_structure(MODE_ENTRY);
  2137. if (ebpf_ut_load_fake_binary())
  2138. goto unittest;
  2139. ebpf_ut_cleanup_memory();
  2140. exit_code = 0;
  2141. unittest:
  2142. exit(exit_code);
  2143. }
  2144. default: {
  2145. break;
  2146. }
  2147. }
  2148. }
  2149. if (disable_apps || disable_cgroups) {
  2150. if (disable_apps)
  2151. ebpf_disable_apps();
  2152. if (disable_cgroups)
  2153. ebpf_disable_cgroups();
  2154. }
  2155. if (select_threads) {
  2156. disable_all_global_charts();
  2157. uint64_t idx;
  2158. for (idx = 0; idx < EBPF_OPTION_ALL_CHARTS; idx++) {
  2159. if (select_threads & 1<<idx)
  2160. ebpf_enable_specific_chart(&ebpf_modules[idx], disable_apps, disable_cgroups);
  2161. }
  2162. }
  2163. // Load apps_groups.conf
  2164. if (ebpf_read_apps_groups_conf(
  2165. &apps_groups_default_target, &apps_groups_root_target, ebpf_user_config_dir, "groups")) {
  2166. info("Cannot read process groups configuration file '%s/apps_groups.conf'. Will try '%s/apps_groups.conf'",
  2167. ebpf_user_config_dir, ebpf_stock_config_dir);
  2168. if (ebpf_read_apps_groups_conf(
  2169. &apps_groups_default_target, &apps_groups_root_target, ebpf_stock_config_dir, "groups")) {
  2170. error("Cannot read process groups '%s/apps_groups.conf'. There are no internal defaults. Failing.",
  2171. ebpf_stock_config_dir);
  2172. ebpf_exit();
  2173. }
  2174. } else
  2175. info("Loaded config file '%s/apps_groups.conf'", ebpf_user_config_dir);
  2176. }
  2177. /*****************************************************************
  2178. *
  2179. * COLLECTOR ENTRY POINT
  2180. *
  2181. *****************************************************************/
  2182. /**
  2183. * Update PID file
  2184. *
  2185. * Update the content of PID file
  2186. *
  2187. * @param filename is the full name of the file.
  2188. * @param pid that identifies the process
  2189. */
  2190. static void ebpf_update_pid_file(char *filename, pid_t pid)
  2191. {
  2192. FILE *fp = fopen(filename, "w");
  2193. if (!fp)
  2194. return;
  2195. fprintf(fp, "%d", pid);
  2196. fclose(fp);
  2197. }
  2198. /**
  2199. * Get Process Name
  2200. *
  2201. * Get process name from /proc/PID/status
  2202. *
  2203. * @param pid that identifies the process
  2204. */
  2205. static char *ebpf_get_process_name(pid_t pid)
  2206. {
  2207. char *name = NULL;
  2208. char filename[FILENAME_MAX + 1];
  2209. snprintfz(filename, FILENAME_MAX, "/proc/%d/status", pid);
  2210. procfile *ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
  2211. if(unlikely(!ff)) {
  2212. error("Cannot open %s", filename);
  2213. return name;
  2214. }
  2215. ff = procfile_readall(ff);
  2216. if(unlikely(!ff))
  2217. return name;
  2218. unsigned long i, lines = procfile_lines(ff);
  2219. for(i = 0; i < lines ; i++) {
  2220. char *cmp = procfile_lineword(ff, i, 0);
  2221. if (!strcmp(cmp, "Name:")) {
  2222. name = strdupz(procfile_lineword(ff, i, 1));
  2223. break;
  2224. }
  2225. }
  2226. procfile_close(ff);
  2227. return name;
  2228. }
  2229. /**
  2230. * Read Previous PID
  2231. *
  2232. * @param filename is the full name of the file.
  2233. *
  2234. * @return It returns the PID used during previous execution on success or 0 otherwise
  2235. */
  2236. static pid_t ebpf_read_previous_pid(char *filename)
  2237. {
  2238. FILE *fp = fopen(filename, "r");
  2239. if (!fp)
  2240. return 0;
  2241. char buffer[64];
  2242. size_t length = fread(buffer, sizeof(*buffer), 63, fp);
  2243. pid_t old_pid = 0;
  2244. if (length) {
  2245. if (length > 63)
  2246. length = 63;
  2247. buffer[length] = '\0';
  2248. old_pid = (pid_t) str2uint32_t(buffer, NULL);
  2249. }
  2250. fclose(fp);
  2251. return old_pid;
  2252. }
  2253. /**
  2254. * Kill previous process
  2255. *
  2256. * Kill previous process whether it was not closed.
  2257. *
  2258. * @param filename is the full name of the file.
  2259. * @param pid that identifies the process
  2260. */
  2261. static void ebpf_kill_previous_process(char *filename, pid_t pid)
  2262. {
  2263. pid_t old_pid = ebpf_read_previous_pid(filename);
  2264. if (!old_pid)
  2265. return;
  2266. // Process is not running
  2267. char *prev_name = ebpf_get_process_name(old_pid);
  2268. if (!prev_name)
  2269. return;
  2270. char *current_name = ebpf_get_process_name(pid);
  2271. if (!strcmp(prev_name, current_name))
  2272. kill(old_pid, SIGKILL);
  2273. freez(prev_name);
  2274. freez(current_name);
  2275. // wait few microseconds before start new plugin
  2276. sleep_usec(USEC_PER_MS * 300);
  2277. }
  2278. /**
  2279. * PID file
  2280. *
  2281. * Write the filename for PID inside the given vector.
  2282. *
  2283. * @param filename vector where we will store the name.
  2284. * @param length number of bytes available in filename vector
  2285. */
  2286. void ebpf_pid_file(char *filename, size_t length)
  2287. {
  2288. snprintfz(filename, length, "%s%s/ebpf.d/ebpf.pid", netdata_configured_host_prefix, ebpf_plugin_dir);
  2289. }
  2290. /**
  2291. * Manage PID
  2292. *
  2293. * This function kills another instance of eBPF whether it is necessary and update the file content.
  2294. *
  2295. * @param pid that identifies the process
  2296. */
  2297. static void ebpf_manage_pid(pid_t pid)
  2298. {
  2299. char filename[FILENAME_MAX + 1];
  2300. ebpf_pid_file(filename, FILENAME_MAX);
  2301. ebpf_kill_previous_process(filename, pid);
  2302. ebpf_update_pid_file(filename, pid);
  2303. }
  2304. /**
  2305. * Set start routine
  2306. *
  2307. * Set static routine before threads to be created.
  2308. */
  2309. static void ebpf_set_static_routine()
  2310. {
  2311. int i;
  2312. for (i = 0; ebpf_modules[i].thread_name; i++) {
  2313. ebpf_threads[i].start_routine = ebpf_modules[i].start_routine;
  2314. }
  2315. }
  2316. /**
  2317. * Entry point
  2318. *
  2319. * @param argc the number of arguments
  2320. * @param argv the pointer to the arguments
  2321. *
  2322. * @return it returns 0 on success and another integer otherwise
  2323. */
  2324. int main(int argc, char **argv)
  2325. {
  2326. stderror = stderr;
  2327. clocks_init();
  2328. main_thread_id = gettid();
  2329. set_global_variables();
  2330. ebpf_parse_args(argc, argv);
  2331. ebpf_manage_pid(getpid());
  2332. if (ebpf_check_conditions())
  2333. return 2;
  2334. // set name
  2335. program_name = "ebpf.plugin";
  2336. // disable syslog
  2337. error_log_syslog = 0;
  2338. // set errors flood protection to 100 logs per hour
  2339. error_log_errors_per_period = 100;
  2340. error_log_throttle_period = 3600;
  2341. if (ebpf_adjust_memory_limit())
  2342. return 3;
  2343. signal(SIGINT, ebpf_stop_threads);
  2344. signal(SIGQUIT, ebpf_stop_threads);
  2345. signal(SIGTERM, ebpf_stop_threads);
  2346. signal(SIGPIPE, ebpf_stop_threads);
  2347. ebpf_start_pthread_variables();
  2348. netdata_configured_host_prefix = getenv("NETDATA_HOST_PREFIX");
  2349. if(verify_netdata_host_prefix() == -1) ebpf_exit(6);
  2350. ebpf_allocate_common_vectors();
  2351. #ifdef LIBBPF_MAJOR_VERSION
  2352. libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
  2353. #endif
  2354. read_local_addresses();
  2355. read_local_ports("/proc/net/tcp", IPPROTO_TCP);
  2356. read_local_ports("/proc/net/tcp6", IPPROTO_TCP);
  2357. read_local_ports("/proc/net/udp", IPPROTO_UDP);
  2358. read_local_ports("/proc/net/udp6", IPPROTO_UDP);
  2359. ebpf_set_static_routine();
  2360. cgroup_integration_thread.thread = mallocz(sizeof(netdata_thread_t));
  2361. cgroup_integration_thread.start_routine = ebpf_cgroup_integration;
  2362. netdata_thread_create(cgroup_integration_thread.thread, cgroup_integration_thread.name,
  2363. NETDATA_THREAD_OPTION_DEFAULT, ebpf_cgroup_integration, NULL);
  2364. int i;
  2365. for (i = 0; ebpf_threads[i].name != NULL; i++) {
  2366. struct netdata_static_thread *st = &ebpf_threads[i];
  2367. ebpf_module_t *em = &ebpf_modules[i];
  2368. em->thread = st;
  2369. // We always initialize process, because it is responsible to take care of apps integration
  2370. if (em->enabled || !i) {
  2371. st->thread = mallocz(sizeof(netdata_thread_t));
  2372. em->thread_id = i;
  2373. em->enabled = NETDATA_THREAD_EBPF_RUNNING;
  2374. netdata_thread_create(st->thread, st->name, NETDATA_THREAD_OPTION_DEFAULT, st->start_routine, em);
  2375. } else {
  2376. em->enabled = NETDATA_THREAD_EBPF_NOT_RUNNING;
  2377. }
  2378. }
  2379. usec_t step = USEC_PER_SEC;
  2380. heartbeat_t hb;
  2381. heartbeat_init(&hb);
  2382. int update_apps_every = (int) EBPF_CFG_UPDATE_APPS_EVERY_DEFAULT;
  2383. int update_apps_list = update_apps_every - 1;
  2384. int process_maps_per_core = ebpf_modules[EBPF_MODULE_PROCESS_IDX].maps_per_core;
  2385. //Plugin will be killed when it receives a signal
  2386. while (!ebpf_exit_plugin) {
  2387. (void)heartbeat_next(&hb, step);
  2388. pthread_mutex_lock(&ebpf_exit_cleanup);
  2389. if (ebpf_modules[i].enabled == NETDATA_THREAD_EBPF_RUNNING && process_pid_fd != -1) {
  2390. pthread_mutex_lock(&collect_data_mutex);
  2391. if (++update_apps_list == update_apps_every) {
  2392. update_apps_list = 0;
  2393. cleanup_exited_pids();
  2394. collect_data_for_all_processes(process_pid_fd, process_maps_per_core);
  2395. pthread_mutex_lock(&lock);
  2396. ebpf_create_apps_charts(apps_groups_root_target);
  2397. pthread_mutex_unlock(&lock);
  2398. }
  2399. pthread_mutex_unlock(&collect_data_mutex);
  2400. }
  2401. pthread_mutex_unlock(&ebpf_exit_cleanup);
  2402. }
  2403. ebpf_stop_threads(0);
  2404. return 0;
  2405. }