ebpf_apps.h 9.3 KB

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