ebpf.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_COLLECTOR_EBPF_H
  3. #define NETDATA_COLLECTOR_EBPF_H 1
  4. #ifndef __FreeBSD__
  5. #include <linux/perf_event.h>
  6. #endif
  7. #include <stdint.h>
  8. #include <errno.h>
  9. #include <signal.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <dlfcn.h>
  15. #include <fcntl.h>
  16. #include <ctype.h>
  17. #include <dirent.h>
  18. // From libnetdata.h
  19. #include "libnetdata/threads/threads.h"
  20. #include "libnetdata/locks/locks.h"
  21. #include "libnetdata/avl/avl.h"
  22. #include "libnetdata/clocks/clocks.h"
  23. #include "libnetdata/config/appconfig.h"
  24. #include "libnetdata/ebpf/ebpf.h"
  25. #include "libnetdata/procfile/procfile.h"
  26. #include "daemon/main.h"
  27. #include "ebpf_apps.h"
  28. #define NETDATA_EBPF_OLD_CONFIG_FILE "ebpf.conf"
  29. #define NETDATA_EBPF_CONFIG_FILE "ebpf.d.conf"
  30. typedef struct netdata_syscall_stat {
  31. unsigned long bytes; // total number of bytes
  32. uint64_t call; // total number of calls
  33. uint64_t ecall; // number of calls that returned error
  34. struct netdata_syscall_stat *next; // Link list
  35. } netdata_syscall_stat_t;
  36. typedef uint64_t netdata_idx_t;
  37. typedef struct netdata_publish_syscall {
  38. char *dimension;
  39. char *name;
  40. char *algorithm;
  41. unsigned long nbyte;
  42. unsigned long pbyte;
  43. uint64_t ncall;
  44. uint64_t pcall;
  45. uint64_t nerr;
  46. uint64_t perr;
  47. struct netdata_publish_syscall *next;
  48. } netdata_publish_syscall_t;
  49. typedef struct netdata_publish_vfs_common {
  50. long write;
  51. long read;
  52. long running;
  53. long zombie;
  54. } netdata_publish_vfs_common_t;
  55. typedef struct netdata_error_report {
  56. char comm[16];
  57. __u32 pid;
  58. int type;
  59. int err;
  60. } netdata_error_report_t;
  61. extern ebpf_module_t ebpf_modules[];
  62. enum ebpf_module_indexes {
  63. EBPF_MODULE_PROCESS_IDX,
  64. EBPF_MODULE_SOCKET_IDX,
  65. EBPF_MODULE_CACHESTAT_IDX,
  66. EBPF_MODULE_SYNC_IDX
  67. };
  68. // Copied from musl header
  69. #ifndef offsetof
  70. #if __GNUC__ > 3
  71. #define offsetof(type, member) __builtin_offsetof(type, member)
  72. #else
  73. #define offsetof(type, member) ((size_t)((char *)&(((type *)0)->member) - (char *)0))
  74. #endif
  75. #endif
  76. // Chart defintions
  77. #define NETDATA_EBPF_FAMILY "ebpf"
  78. #define NETDATA_EBPF_CHART_TYPE_LINE "line"
  79. #define NETDATA_EBPF_CHART_TYPE_STACKED "stacked"
  80. #define NETDATA_EBPF_MEMORY_GROUP "mem"
  81. // Log file
  82. #define NETDATA_DEVELOPER_LOG_FILE "developer.log"
  83. // Maximum number of processors monitored on perf events
  84. #define NETDATA_MAX_PROCESSOR 512
  85. // Kernel versions calculated with the formula:
  86. // R = MAJOR*65536 + MINOR*256 + PATCH
  87. #define NETDATA_KERNEL_V5_3 328448
  88. #define NETDATA_KERNEL_V4_15 265984
  89. #define EBPF_SYS_CLONE_IDX 11
  90. #define EBPF_MAX_MAPS 32
  91. enum ebpf_algorithms_list {
  92. NETDATA_EBPF_ABSOLUTE_IDX,
  93. NETDATA_EBPF_INCREMENTAL_IDX
  94. };
  95. // Threads
  96. extern void *ebpf_process_thread(void *ptr);
  97. extern void *ebpf_socket_thread(void *ptr);
  98. // Common variables
  99. extern pthread_mutex_t lock;
  100. extern int close_ebpf_plugin;
  101. extern int ebpf_nprocs;
  102. extern int running_on_kernel;
  103. extern char *ebpf_plugin_dir;
  104. extern char kernel_string[64];
  105. extern pthread_mutex_t collect_data_mutex;
  106. extern pthread_cond_t collect_data_cond_var;
  107. // Common functions
  108. extern void ebpf_global_labels(netdata_syscall_stat_t *is,
  109. netdata_publish_syscall_t *pio,
  110. char **dim,
  111. char **name,
  112. int *algorithm,
  113. int end);
  114. extern void ebpf_write_chart_cmd(char *type,
  115. char *id,
  116. char *title,
  117. char *units,
  118. char *family,
  119. char *charttype,
  120. char *context,
  121. int order);
  122. extern void ebpf_write_global_dimension(char *name, char *id, char *algorithm);
  123. extern void ebpf_create_global_dimension(void *ptr, int end);
  124. extern void ebpf_create_chart(char *type,
  125. char *id,
  126. char *title,
  127. char *units,
  128. char *family,
  129. char *context,
  130. char *charttype,
  131. int order,
  132. void (*ncd)(void *, int),
  133. void *move,
  134. int end);
  135. extern void write_begin_chart(char *family, char *name);
  136. extern void write_chart_dimension(char *dim, long long value);
  137. extern void write_count_chart(char *name, char *family, netdata_publish_syscall_t *move, uint32_t end);
  138. extern void write_err_chart(char *name, char *family, netdata_publish_syscall_t *move, int end);
  139. extern void write_io_chart(char *chart, char *family, char *dwrite, long long vwrite,
  140. char *dread, long long vread);
  141. extern void fill_ebpf_data(ebpf_data_t *ef);
  142. extern void ebpf_create_charts_on_apps(char *name,
  143. char *title,
  144. char *units,
  145. char *family,
  146. char *charttype,
  147. int order,
  148. char *algorithm,
  149. struct target *root);
  150. extern void write_end_chart();
  151. extern void ebpf_cleanup_publish_syscall(netdata_publish_syscall_t *nps);
  152. #define EBPF_PROGRAMS_SECTION "ebpf programs"
  153. #define EBPF_COMMON_DIMENSION_PERCENTAGE "%"
  154. #define EBPF_COMMON_DIMENSION_CALL "calls/s"
  155. #define EBPF_COMMON_DIMENSION_BITS "kilobits/s"
  156. #define EBPF_COMMON_DIMENSION_BYTES "bytes/s"
  157. #define EBPF_COMMON_DIMENSION_DIFFERENCE "difference"
  158. #define EBPF_COMMON_DIMENSION_PACKETS "packets"
  159. // Common variables
  160. extern int debug_enabled;
  161. extern struct pid_stat *root_of_pids;
  162. extern char *ebpf_algorithms[];
  163. extern struct config collector_config;
  164. extern struct pid_stat *root_of_pids;
  165. extern ebpf_process_stat_t *global_process_stat;
  166. extern size_t all_pids_count;
  167. extern int update_every;
  168. extern uint32_t finalized_threads;
  169. // Socket functions and variables
  170. // Common functions
  171. extern void ebpf_process_create_apps_charts(struct ebpf_module *em, void *ptr);
  172. extern void ebpf_socket_create_apps_charts(struct ebpf_module *em, void *ptr);
  173. extern void ebpf_cachestat_create_apps_charts(struct ebpf_module *em, void *root);
  174. extern void ebpf_one_dimension_write_charts(char *family, char *chart, char *dim, long long v1);
  175. extern collected_number get_value_from_structure(char *basis, size_t offset);
  176. #define EBPF_MAX_SYNCHRONIZATION_TIME 300
  177. #endif /* NETDATA_COLLECTOR_EBPF_H */