ebpf_apps.h 8.3 KB

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