ebpf_apps.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_EBPF_APPS_H
  3. #define NETDATA_EBPF_APPS_H 1
  4. #include "libnetdata/locks/locks.h"
  5. #include "libnetdata/avl/avl.h"
  6. #include "libnetdata/clocks/clocks.h"
  7. #include "libnetdata/config/appconfig.h"
  8. #include "libnetdata/ebpf/ebpf.h"
  9. #define NETDATA_APPS_FAMILY "apps"
  10. #define NETDATA_APP_FAMILY "app"
  11. #define NETDATA_APPS_FILE_GROUP "file_access"
  12. #define NETDATA_APPS_FILE_FDS "fds"
  13. #define NETDATA_APPS_FILE_CGROUP_GROUP "file_access (eBPF)"
  14. #define NETDATA_APPS_PROCESS_GROUP "process (eBPF)"
  15. #define NETDATA_APPS_NET_GROUP "net"
  16. #define NETDATA_APPS_IPC_SHM_GROUP "ipc shm"
  17. #include "ebpf_process.h"
  18. #include "ebpf_dcstat.h"
  19. #include "ebpf_disk.h"
  20. #include "ebpf_fd.h"
  21. #include "ebpf_filesystem.h"
  22. #include "ebpf_functions.h"
  23. #include "ebpf_hardirq.h"
  24. #include "ebpf_cachestat.h"
  25. #include "ebpf_mdflush.h"
  26. #include "ebpf_mount.h"
  27. #include "ebpf_oomkill.h"
  28. #include "ebpf_shm.h"
  29. #include "ebpf_socket.h"
  30. #include "ebpf_softirq.h"
  31. #include "ebpf_sync.h"
  32. #include "ebpf_swap.h"
  33. #include "ebpf_vfs.h"
  34. #define EBPF_MAX_COMPARE_NAME 100
  35. #define EBPF_MAX_NAME 100
  36. // ----------------------------------------------------------------------------
  37. // pid_stat
  38. //
  39. struct ebpf_target {
  40. char compare[EBPF_MAX_COMPARE_NAME + 1];
  41. uint32_t comparehash;
  42. size_t comparelen;
  43. char id[EBPF_MAX_NAME + 1];
  44. uint32_t idhash;
  45. uint32_t charts_created;
  46. char name[EBPF_MAX_NAME + 1];
  47. char clean_name[EBPF_MAX_NAME + 1]; // sanitized name used in chart id (need to replace at least dots)
  48. // Changes made to simplify integration between apps and eBPF.
  49. netdata_publish_cachestat_t cachestat;
  50. netdata_publish_dcstat_t dcstat;
  51. netdata_publish_swap_t swap;
  52. netdata_publish_vfs_t vfs;
  53. netdata_fd_stat_t fd;
  54. netdata_publish_shm_t shm;
  55. kernel_uint_t starttime;
  56. kernel_uint_t collected_starttime;
  57. unsigned int processes; // how many processes have been merged to this
  58. int exposed; // if set, we have sent this to netdata
  59. int hidden; // if set, we set the hidden flag on the dimension
  60. int debug_enabled;
  61. int ends_with;
  62. int starts_with; // if set, the compare string matches only the
  63. // beginning of the command
  64. struct ebpf_pid_on_target *root_pid; // list of aggregated pids for target debugging
  65. struct ebpf_target *target; // the one that will be reported to netdata
  66. struct ebpf_target *next;
  67. };
  68. extern struct ebpf_target *apps_groups_default_target;
  69. extern struct ebpf_target *apps_groups_root_target;
  70. extern struct ebpf_target *users_root_target;
  71. extern struct ebpf_target *groups_root_target;
  72. struct ebpf_pid_stat {
  73. int32_t pid;
  74. char comm[EBPF_MAX_COMPARE_NAME + 1];
  75. char *cmdline;
  76. uint32_t log_thrown;
  77. // char state;
  78. int32_t ppid;
  79. int children_count; // number of processes directly referencing this
  80. unsigned char keep : 1; // 1 when we need to keep this process in memory even after it exited
  81. int keeploops; // increases by 1 every time keep is 1 and updated 0
  82. unsigned char updated : 1; // 1 when the process is currently running
  83. unsigned char updated_twice : 1; // 1 when the process was running in the previous iteration
  84. unsigned char merged : 1; // 1 when it has been merged to its parent
  85. unsigned char read : 1; // 1 when we have already read this process for this iteration
  86. int sortlist; // higher numbers = top on the process tree
  87. // each process gets a unique number
  88. struct ebpf_target *target; // app_groups.conf targets
  89. struct ebpf_target *user_target; // uid based targets
  90. struct ebpf_target *group_target; // gid based targets
  91. usec_t stat_collected_usec;
  92. usec_t last_stat_collected_usec;
  93. char *stat_filename;
  94. char *status_filename;
  95. char *io_filename;
  96. char *cmdline_filename;
  97. struct ebpf_pid_stat *parent;
  98. struct ebpf_pid_stat *prev;
  99. struct ebpf_pid_stat *next;
  100. };
  101. // ----------------------------------------------------------------------------
  102. // target
  103. //
  104. // target is the structure that processes are aggregated to be reported
  105. // to netdata.
  106. //
  107. // - Each entry in /etc/apps_groups.conf creates a target.
  108. // - Each user and group used by a process in the system, creates a target.
  109. struct ebpf_pid_on_target {
  110. int32_t pid;
  111. struct ebpf_pid_on_target *next;
  112. };
  113. // ----------------------------------------------------------------------------
  114. // Structures used to read information from kernel ring
  115. typedef struct ebpf_process_stat {
  116. uint64_t pid_tgid; // This cannot be removed, because it is used inside kernel ring.
  117. uint32_t pid;
  118. //Counter
  119. uint32_t exit_call;
  120. uint32_t release_call;
  121. uint32_t create_process;
  122. uint32_t create_thread;
  123. //Counter
  124. uint32_t task_err;
  125. uint8_t removeme;
  126. } ebpf_process_stat_t;
  127. /**
  128. * Internal function used to write debug messages.
  129. *
  130. * @param fmt the format to create the message.
  131. * @param ... the arguments to fill the format.
  132. */
  133. static inline void debug_log_int(const char *fmt, ...)
  134. {
  135. va_list args;
  136. fprintf(stderr, "apps.plugin: ");
  137. va_start(args, fmt);
  138. vfprintf(stderr, fmt, args);
  139. va_end(args);
  140. fputc('\n', stderr);
  141. }
  142. // ----------------------------------------------------------------------------
  143. // Exported variabled and functions
  144. //
  145. extern struct ebpf_pid_stat **ebpf_all_pids;
  146. int ebpf_read_apps_groups_conf(struct ebpf_target **apps_groups_default_target,
  147. struct ebpf_target **apps_groups_root_target,
  148. const char *path,
  149. const char *file);
  150. void clean_apps_groups_target(struct ebpf_target *apps_groups_root_target);
  151. size_t zero_all_targets(struct ebpf_target *root);
  152. int am_i_running_as_root();
  153. void cleanup_exited_pids();
  154. int ebpf_read_hash_table(void *ep, int fd, uint32_t pid);
  155. int get_pid_comm(pid_t pid, size_t n, char *dest);
  156. void collect_data_for_all_processes(int tbl_pid_stats_fd, int maps_per_core);
  157. void ebpf_process_apps_accumulator(ebpf_process_stat_t *out, int maps_per_core);
  158. extern ebpf_process_stat_t **global_process_stats;
  159. extern netdata_publish_cachestat_t **cachestat_pid;
  160. extern netdata_publish_dcstat_t **dcstat_pid;
  161. extern netdata_publish_swap_t **swap_pid;
  162. extern netdata_publish_vfs_t **vfs_pid;
  163. extern netdata_fd_stat_t **fd_pid;
  164. extern netdata_publish_shm_t **shm_pid;
  165. // The default value is at least 32 times smaller than maximum number of PIDs allowed on system,
  166. // this is only possible because we are using ARAL (https://github.com/netdata/netdata/tree/master/libnetdata/aral).
  167. #ifndef NETDATA_EBPF_ALLOC_MAX_PID
  168. # define NETDATA_EBPF_ALLOC_MAX_PID 1024
  169. #endif
  170. #define NETDATA_EBPF_ALLOC_MIN_ELEMENTS 256
  171. // ARAL Sectiion
  172. extern void ebpf_aral_init(void);
  173. extern ebpf_process_stat_t *ebpf_process_stat_get(void);
  174. extern void ebpf_process_stat_release(ebpf_process_stat_t *stat);
  175. extern ebpf_process_stat_t *process_stat_vector;
  176. extern ARAL *ebpf_aral_socket_pid;
  177. void ebpf_socket_aral_init();
  178. ebpf_socket_publish_apps_t *ebpf_socket_stat_get(void);
  179. extern ARAL *ebpf_aral_cachestat_pid;
  180. void ebpf_cachestat_aral_init();
  181. netdata_publish_cachestat_t *ebpf_publish_cachestat_get(void);
  182. void ebpf_cachestat_release(netdata_publish_cachestat_t *stat);
  183. extern ARAL *ebpf_aral_dcstat_pid;
  184. void ebpf_dcstat_aral_init();
  185. netdata_publish_dcstat_t *ebpf_publish_dcstat_get(void);
  186. void ebpf_dcstat_release(netdata_publish_dcstat_t *stat);
  187. extern ARAL *ebpf_aral_vfs_pid;
  188. void ebpf_vfs_aral_init();
  189. netdata_publish_vfs_t *ebpf_vfs_get(void);
  190. void ebpf_vfs_release(netdata_publish_vfs_t *stat);
  191. extern ARAL *ebpf_aral_fd_pid;
  192. void ebpf_fd_aral_init();
  193. netdata_fd_stat_t *ebpf_fd_stat_get(void);
  194. void ebpf_fd_release(netdata_fd_stat_t *stat);
  195. extern ARAL *ebpf_aral_shm_pid;
  196. void ebpf_shm_aral_init();
  197. netdata_publish_shm_t *ebpf_shm_stat_get(void);
  198. void ebpf_shm_release(netdata_publish_shm_t *stat);
  199. // ARAL Section end
  200. // Threads integrated with apps
  201. extern ebpf_socket_publish_apps_t **socket_bandwidth_curr;
  202. // Threads integrated with apps
  203. #include "libnetdata/threads/threads.h"
  204. // ARAL variables
  205. extern ARAL *ebpf_aral_apps_pid_stat;
  206. extern ARAL *ebpf_aral_process_stat;
  207. #define NETDATA_EBPF_PROC_ARAL_NAME "ebpf_proc_stat"
  208. #endif /* NETDATA_EBPF_APPS_H */