ebpf_apps.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. typedef struct ebpf_bandwidth {
  124. uint32_t pid;
  125. uint64_t first; // First timestamp
  126. uint64_t ct; // Last timestamp
  127. uint64_t bytes_sent; // Bytes sent
  128. uint64_t bytes_received; // Bytes received
  129. uint64_t call_tcp_sent; // Number of times tcp_sendmsg was called
  130. uint64_t call_tcp_received; // Number of times tcp_cleanup_rbuf was called
  131. uint64_t retransmit; // Number of times tcp_retransmit was called
  132. uint64_t call_udp_sent; // Number of times udp_sendmsg was called
  133. uint64_t call_udp_received; // Number of times udp_recvmsg was called
  134. uint64_t close; // Number of times tcp_close was called
  135. uint64_t drop; // THIS IS NOT USED FOR WHILE, we are in groom section
  136. uint32_t tcp_v4_connection; // Number of times tcp_v4_connection was called.
  137. uint32_t tcp_v6_connection; // Number of times tcp_v6_connection was called.
  138. } ebpf_bandwidth_t;
  139. /**
  140. * Internal function used to write debug messages.
  141. *
  142. * @param fmt the format to create the message.
  143. * @param ... the arguments to fill the format.
  144. */
  145. static inline void debug_log_int(const char *fmt, ...)
  146. {
  147. va_list args;
  148. fprintf(stderr, "apps.plugin: ");
  149. va_start(args, fmt);
  150. vfprintf(stderr, fmt, args);
  151. va_end(args);
  152. fputc('\n', stderr);
  153. }
  154. // ----------------------------------------------------------------------------
  155. // Exported variabled and functions
  156. //
  157. extern struct ebpf_pid_stat **ebpf_all_pids;
  158. int ebpf_read_apps_groups_conf(struct ebpf_target **apps_groups_default_target,
  159. struct ebpf_target **apps_groups_root_target,
  160. const char *path,
  161. const char *file);
  162. void clean_apps_groups_target(struct ebpf_target *apps_groups_root_target);
  163. size_t zero_all_targets(struct ebpf_target *root);
  164. int am_i_running_as_root();
  165. void cleanup_exited_pids();
  166. int ebpf_read_hash_table(void *ep, int fd, uint32_t pid);
  167. int get_pid_comm(pid_t pid, size_t n, char *dest);
  168. size_t read_processes_statistic_using_pid_on_target(ebpf_process_stat_t **ep,
  169. int fd,
  170. struct ebpf_pid_on_target *pids);
  171. size_t read_bandwidth_statistic_using_pid_on_target(ebpf_bandwidth_t **ep, int fd, struct ebpf_pid_on_target *pids);
  172. void collect_data_for_all_processes(int tbl_pid_stats_fd, int maps_per_core);
  173. void ebpf_process_apps_accumulator(ebpf_process_stat_t *out, int maps_per_core);
  174. extern ebpf_process_stat_t **global_process_stats;
  175. extern netdata_publish_cachestat_t **cachestat_pid;
  176. extern netdata_publish_dcstat_t **dcstat_pid;
  177. extern netdata_publish_swap_t **swap_pid;
  178. extern netdata_publish_vfs_t **vfs_pid;
  179. extern netdata_fd_stat_t **fd_pid;
  180. extern netdata_publish_shm_t **shm_pid;
  181. // The default value is at least 32 times smaller than maximum number of PIDs allowed on system,
  182. // this is only possible because we are using ARAL (https://github.com/netdata/netdata/tree/master/libnetdata/aral).
  183. #ifndef NETDATA_EBPF_ALLOC_MAX_PID
  184. # define NETDATA_EBPF_ALLOC_MAX_PID 1024
  185. #endif
  186. #define NETDATA_EBPF_ALLOC_MIN_ELEMENTS 256
  187. // ARAL Sectiion
  188. extern void ebpf_aral_init(void);
  189. extern ebpf_process_stat_t *ebpf_process_stat_get(void);
  190. extern void ebpf_process_stat_release(ebpf_process_stat_t *stat);
  191. extern ebpf_process_stat_t *process_stat_vector;
  192. extern ARAL *ebpf_aral_socket_pid;
  193. void ebpf_socket_aral_init();
  194. ebpf_socket_publish_apps_t *ebpf_socket_stat_get(void);
  195. void ebpf_socket_release(ebpf_socket_publish_apps_t *stat);
  196. extern ARAL *ebpf_aral_cachestat_pid;
  197. void ebpf_cachestat_aral_init();
  198. netdata_publish_cachestat_t *ebpf_publish_cachestat_get(void);
  199. void ebpf_cachestat_release(netdata_publish_cachestat_t *stat);
  200. extern ARAL *ebpf_aral_dcstat_pid;
  201. void ebpf_dcstat_aral_init();
  202. netdata_publish_dcstat_t *ebpf_publish_dcstat_get(void);
  203. void ebpf_dcstat_release(netdata_publish_dcstat_t *stat);
  204. extern ARAL *ebpf_aral_vfs_pid;
  205. void ebpf_vfs_aral_init();
  206. netdata_publish_vfs_t *ebpf_vfs_get(void);
  207. void ebpf_vfs_release(netdata_publish_vfs_t *stat);
  208. extern ARAL *ebpf_aral_fd_pid;
  209. void ebpf_fd_aral_init();
  210. netdata_fd_stat_t *ebpf_fd_stat_get(void);
  211. void ebpf_fd_release(netdata_fd_stat_t *stat);
  212. extern ARAL *ebpf_aral_shm_pid;
  213. void ebpf_shm_aral_init();
  214. netdata_publish_shm_t *ebpf_shm_stat_get(void);
  215. void ebpf_shm_release(netdata_publish_shm_t *stat);
  216. // ARAL Section end
  217. // Threads integrated with apps
  218. extern ebpf_socket_publish_apps_t **socket_bandwidth_curr;
  219. // Threads integrated with apps
  220. #include "libnetdata/threads/threads.h"
  221. // ARAL variables
  222. extern ARAL *ebpf_aral_apps_pid_stat;
  223. extern ARAL *ebpf_aral_process_stat;
  224. #define NETDATA_EBPF_PROC_ARAL_NAME "ebpf_proc_stat"
  225. #endif /* NETDATA_EBPF_APPS_H */