ebpf_apps.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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/threads/threads.h"
  5. #include "libnetdata/locks/locks.h"
  6. #include "libnetdata/avl/avl.h"
  7. #include "libnetdata/clocks/clocks.h"
  8. #include "libnetdata/config/appconfig.h"
  9. #include "libnetdata/ebpf/ebpf.h"
  10. #define NETDATA_APPS_FAMILY "apps"
  11. #define NETDATA_APPS_FILE_GROUP "file_access"
  12. #define NETDATA_APPS_FILE_CGROUP_GROUP "file_access (eBPF)"
  13. #define NETDATA_APPS_PROCESS_GROUP "process (eBPF)"
  14. #define NETDATA_APPS_NET_GROUP "net"
  15. #define NETDATA_APPS_IPC_SHM_GROUP "ipc shm (eBPF)"
  16. #include "ebpf_process.h"
  17. #include "ebpf_dcstat.h"
  18. #include "ebpf_disk.h"
  19. #include "ebpf_fd.h"
  20. #include "ebpf_filesystem.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 MAX_COMPARE_NAME 100
  33. #define MAX_NAME 100
  34. // ----------------------------------------------------------------------------
  35. // process_pid_stat
  36. //
  37. // Fields read from the kernel ring for a specific PID
  38. //
  39. typedef struct process_pid_stat {
  40. uint64_t pid_tgid; // Unique identifier
  41. uint32_t pid; // process id
  42. // Count number of calls done for specific function
  43. uint32_t open_call;
  44. uint32_t write_call;
  45. uint32_t writev_call;
  46. uint32_t read_call;
  47. uint32_t readv_call;
  48. uint32_t unlink_call;
  49. uint32_t exit_call;
  50. uint32_t release_call;
  51. uint32_t fork_call;
  52. uint32_t clone_call;
  53. uint32_t close_call;
  54. // Count number of bytes written or read
  55. uint64_t write_bytes;
  56. uint64_t writev_bytes;
  57. uint64_t readv_bytes;
  58. uint64_t read_bytes;
  59. // Count number of errors for the specified function
  60. uint32_t open_err;
  61. uint32_t write_err;
  62. uint32_t writev_err;
  63. uint32_t read_err;
  64. uint32_t readv_err;
  65. uint32_t unlink_err;
  66. uint32_t fork_err;
  67. uint32_t clone_err;
  68. uint32_t close_err;
  69. } process_pid_stat_t;
  70. // ----------------------------------------------------------------------------
  71. // socket_bandwidth
  72. //
  73. // Fields read from the kernel ring for a specific PID
  74. //
  75. typedef struct socket_bandwidth {
  76. uint64_t first;
  77. uint64_t ct;
  78. uint64_t sent;
  79. uint64_t received;
  80. unsigned char removed;
  81. } socket_bandwidth_t;
  82. // ----------------------------------------------------------------------------
  83. // pid_stat
  84. //
  85. // structure to store data for each process running
  86. // see: man proc for the description of the fields
  87. struct pid_fd {
  88. int fd;
  89. #ifndef __FreeBSD__
  90. ino_t inode;
  91. char *filename;
  92. uint32_t link_hash;
  93. size_t cache_iterations_counter;
  94. size_t cache_iterations_reset;
  95. #endif
  96. };
  97. struct target {
  98. char compare[MAX_COMPARE_NAME + 1];
  99. uint32_t comparehash;
  100. size_t comparelen;
  101. char id[MAX_NAME + 1];
  102. uint32_t idhash;
  103. char name[MAX_NAME + 1];
  104. uid_t uid;
  105. gid_t gid;
  106. // Changes made to simplify integration between apps and eBPF.
  107. netdata_publish_cachestat_t cachestat;
  108. netdata_publish_dcstat_t dcstat;
  109. netdata_publish_swap_t swap;
  110. netdata_publish_vfs_t vfs;
  111. netdata_fd_stat_t fd;
  112. netdata_publish_shm_t shm;
  113. /* These variables are not necessary for eBPF collector
  114. kernel_uint_t minflt;
  115. kernel_uint_t cminflt;
  116. kernel_uint_t majflt;
  117. kernel_uint_t cmajflt;
  118. kernel_uint_t utime;
  119. kernel_uint_t stime;
  120. kernel_uint_t gtime;
  121. kernel_uint_t cutime;
  122. kernel_uint_t cstime;
  123. kernel_uint_t cgtime;
  124. kernel_uint_t num_threads;
  125. // kernel_uint_t rss;
  126. kernel_uint_t status_vmsize;
  127. kernel_uint_t status_vmrss;
  128. kernel_uint_t status_vmshared;
  129. kernel_uint_t status_rssfile;
  130. kernel_uint_t status_rssshmem;
  131. kernel_uint_t status_vmswap;
  132. kernel_uint_t io_logical_bytes_read;
  133. kernel_uint_t io_logical_bytes_written;
  134. // kernel_uint_t io_read_calls;
  135. // kernel_uint_t io_write_calls;
  136. kernel_uint_t io_storage_bytes_read;
  137. kernel_uint_t io_storage_bytes_written;
  138. // kernel_uint_t io_cancelled_write_bytes;
  139. int *target_fds;
  140. int target_fds_size;
  141. kernel_uint_t openfiles;
  142. kernel_uint_t openpipes;
  143. kernel_uint_t opensockets;
  144. kernel_uint_t openinotifies;
  145. kernel_uint_t openeventfds;
  146. kernel_uint_t opentimerfds;
  147. kernel_uint_t opensignalfds;
  148. kernel_uint_t openeventpolls;
  149. kernel_uint_t openother;
  150. */
  151. kernel_uint_t starttime;
  152. kernel_uint_t collected_starttime;
  153. /*
  154. kernel_uint_t uptime_min;
  155. kernel_uint_t uptime_sum;
  156. kernel_uint_t uptime_max;
  157. */
  158. unsigned int processes; // how many processes have been merged to this
  159. int exposed; // if set, we have sent this to netdata
  160. int hidden; // if set, we set the hidden flag on the dimension
  161. int debug_enabled;
  162. int ends_with;
  163. int starts_with; // if set, the compare string matches only the
  164. // beginning of the command
  165. struct pid_on_target *root_pid; // list of aggregated pids for target debugging
  166. struct target *target; // the one that will be reported to netdata
  167. struct target *next;
  168. };
  169. extern struct target *apps_groups_default_target;
  170. extern struct target *apps_groups_root_target;
  171. extern struct target *users_root_target;
  172. extern struct target *groups_root_target;
  173. struct pid_stat {
  174. int32_t pid;
  175. char comm[MAX_COMPARE_NAME + 1];
  176. char *cmdline;
  177. uint32_t log_thrown;
  178. // char state;
  179. int32_t ppid;
  180. // int32_t pgrp;
  181. // int32_t session;
  182. // int32_t tty_nr;
  183. // int32_t tpgid;
  184. // uint64_t flags;
  185. /*
  186. // these are raw values collected
  187. kernel_uint_t minflt_raw;
  188. kernel_uint_t cminflt_raw;
  189. kernel_uint_t majflt_raw;
  190. kernel_uint_t cmajflt_raw;
  191. kernel_uint_t utime_raw;
  192. kernel_uint_t stime_raw;
  193. kernel_uint_t gtime_raw; // guest_time
  194. kernel_uint_t cutime_raw;
  195. kernel_uint_t cstime_raw;
  196. kernel_uint_t cgtime_raw; // cguest_time
  197. // these are rates
  198. kernel_uint_t minflt;
  199. kernel_uint_t cminflt;
  200. kernel_uint_t majflt;
  201. kernel_uint_t cmajflt;
  202. kernel_uint_t utime;
  203. kernel_uint_t stime;
  204. kernel_uint_t gtime;
  205. kernel_uint_t cutime;
  206. kernel_uint_t cstime;
  207. kernel_uint_t cgtime;
  208. // int64_t priority;
  209. // int64_t nice;
  210. int32_t num_threads;
  211. // int64_t itrealvalue;
  212. kernel_uint_t collected_starttime;
  213. // kernel_uint_t vsize;
  214. // kernel_uint_t rss;
  215. // kernel_uint_t rsslim;
  216. // kernel_uint_t starcode;
  217. // kernel_uint_t endcode;
  218. // kernel_uint_t startstack;
  219. // kernel_uint_t kstkesp;
  220. // kernel_uint_t kstkeip;
  221. // uint64_t signal;
  222. // uint64_t blocked;
  223. // uint64_t sigignore;
  224. // uint64_t sigcatch;
  225. // uint64_t wchan;
  226. // uint64_t nswap;
  227. // uint64_t cnswap;
  228. // int32_t exit_signal;
  229. // int32_t processor;
  230. // uint32_t rt_priority;
  231. // uint32_t policy;
  232. // kernel_uint_t delayacct_blkio_ticks;
  233. uid_t uid;
  234. gid_t gid;
  235. kernel_uint_t status_vmsize;
  236. kernel_uint_t status_vmrss;
  237. kernel_uint_t status_vmshared;
  238. kernel_uint_t status_rssfile;
  239. kernel_uint_t status_rssshmem;
  240. kernel_uint_t status_vmswap;
  241. #ifndef __FreeBSD__
  242. ARL_BASE *status_arl;
  243. #endif
  244. kernel_uint_t io_logical_bytes_read_raw;
  245. kernel_uint_t io_logical_bytes_written_raw;
  246. // kernel_uint_t io_read_calls_raw;
  247. // kernel_uint_t io_write_calls_raw;
  248. kernel_uint_t io_storage_bytes_read_raw;
  249. kernel_uint_t io_storage_bytes_written_raw;
  250. // kernel_uint_t io_cancelled_write_bytes_raw;
  251. kernel_uint_t io_logical_bytes_read;
  252. kernel_uint_t io_logical_bytes_written;
  253. // kernel_uint_t io_read_calls;
  254. // kernel_uint_t io_write_calls;
  255. kernel_uint_t io_storage_bytes_read;
  256. kernel_uint_t io_storage_bytes_written;
  257. // kernel_uint_t io_cancelled_write_bytes;
  258. */
  259. struct pid_fd *fds; // array of fds it uses
  260. size_t fds_size; // the size of the fds array
  261. int children_count; // number of processes directly referencing this
  262. unsigned char keep : 1; // 1 when we need to keep this process in memory even after it exited
  263. int keeploops; // increases by 1 every time keep is 1 and updated 0
  264. unsigned char updated : 1; // 1 when the process is currently running
  265. unsigned char updated_twice : 1; // 1 when the process was running in the previous iteration
  266. unsigned char merged : 1; // 1 when it has been merged to its parent
  267. unsigned char read : 1; // 1 when we have already read this process for this iteration
  268. int sortlist; // higher numbers = top on the process tree
  269. // each process gets a unique number
  270. struct target *target; // app_groups.conf targets
  271. struct target *user_target; // uid based targets
  272. struct target *group_target; // gid based targets
  273. usec_t stat_collected_usec;
  274. usec_t last_stat_collected_usec;
  275. usec_t io_collected_usec;
  276. usec_t last_io_collected_usec;
  277. kernel_uint_t uptime;
  278. char *fds_dirname; // the full directory name in /proc/PID/fd
  279. char *stat_filename;
  280. char *status_filename;
  281. char *io_filename;
  282. char *cmdline_filename;
  283. struct pid_stat *parent;
  284. struct pid_stat *prev;
  285. struct pid_stat *next;
  286. };
  287. // ----------------------------------------------------------------------------
  288. // target
  289. //
  290. // target is the structure that processes are aggregated to be reported
  291. // to netdata.
  292. //
  293. // - Each entry in /etc/apps_groups.conf creates a target.
  294. // - Each user and group used by a process in the system, creates a target.
  295. struct pid_on_target {
  296. int32_t pid;
  297. struct pid_on_target *next;
  298. };
  299. // ----------------------------------------------------------------------------
  300. // Structures used to read information from kernel ring
  301. typedef struct ebpf_process_stat {
  302. uint64_t pid_tgid;
  303. uint32_t pid;
  304. //Counter
  305. uint32_t exit_call;
  306. uint32_t release_call;
  307. uint32_t create_process;
  308. uint32_t create_thread;
  309. //Counter
  310. uint32_t task_err;
  311. uint8_t removeme;
  312. } ebpf_process_stat_t;
  313. typedef struct ebpf_bandwidth {
  314. uint32_t pid;
  315. uint64_t first; // First timestamp
  316. uint64_t ct; // Last timestamp
  317. uint64_t bytes_sent; // Bytes sent
  318. uint64_t bytes_received; // Bytes received
  319. uint64_t call_tcp_sent; // Number of times tcp_sendmsg was called
  320. uint64_t call_tcp_received; // Number of times tcp_cleanup_rbuf was called
  321. uint64_t retransmit; // Number of times tcp_retransmit was called
  322. uint64_t call_udp_sent; // Number of times udp_sendmsg was called
  323. uint64_t call_udp_received; // Number of times udp_recvmsg was called
  324. uint64_t close; // Number of times tcp_close was called
  325. uint64_t drop; // THIS IS NOT USED FOR WHILE, we are in groom section
  326. uint32_t tcp_v4_connection; // Number of times tcp_v4_connection was called.
  327. uint32_t tcp_v6_connection; // Number of times tcp_v6_connection was called.
  328. } ebpf_bandwidth_t;
  329. /**
  330. * Internal function used to write debug messages.
  331. *
  332. * @param fmt the format to create the message.
  333. * @param ... the arguments to fill the format.
  334. */
  335. static inline void debug_log_int(const char *fmt, ...)
  336. {
  337. va_list args;
  338. fprintf(stderr, "apps.plugin: ");
  339. va_start(args, fmt);
  340. vfprintf(stderr, fmt, args);
  341. va_end(args);
  342. fputc('\n', stderr);
  343. }
  344. // ----------------------------------------------------------------------------
  345. // Exported variabled and functions
  346. //
  347. extern struct pid_stat **all_pids;
  348. int ebpf_read_apps_groups_conf(struct target **apps_groups_default_target,
  349. struct target **apps_groups_root_target,
  350. const char *path,
  351. const char *file);
  352. void clean_apps_groups_target(struct target *apps_groups_root_target);
  353. size_t zero_all_targets(struct target *root);
  354. int am_i_running_as_root();
  355. void cleanup_exited_pids();
  356. int ebpf_read_hash_table(void *ep, int fd, uint32_t pid);
  357. int get_pid_comm(pid_t pid, size_t n, char *dest);
  358. size_t read_processes_statistic_using_pid_on_target(ebpf_process_stat_t **ep,
  359. int fd,
  360. struct pid_on_target *pids);
  361. size_t read_bandwidth_statistic_using_pid_on_target(ebpf_bandwidth_t **ep, int fd, struct pid_on_target *pids);
  362. void collect_data_for_all_processes(int tbl_pid_stats_fd);
  363. extern ebpf_process_stat_t **global_process_stats;
  364. extern ebpf_process_publish_apps_t **current_apps_data;
  365. extern netdata_publish_cachestat_t **cachestat_pid;
  366. extern netdata_publish_dcstat_t **dcstat_pid;
  367. #endif /* NETDATA_EBPF_APPS_H */