ebpf_apps.h 8.1 KB

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