ebpf_process.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include <sys/resource.h>
  3. #include "ebpf.h"
  4. #include "ebpf_process.h"
  5. /*****************************************************************
  6. *
  7. * GLOBAL VARIABLES
  8. *
  9. *****************************************************************/
  10. static char *process_dimension_names[NETDATA_KEY_PUBLISH_PROCESS_END] = { "open", "close", "delete", "read", "write",
  11. "process", "task", "process", "thread" };
  12. static char *process_id_names[NETDATA_KEY_PUBLISH_PROCESS_END] = { "do_sys_open", "__close_fd", "vfs_unlink",
  13. "vfs_read", "vfs_write", "do_exit",
  14. "release_task", "_do_fork", "sys_clone" };
  15. static char *status[] = { "process", "zombie" };
  16. static netdata_idx_t *process_hash_values = NULL;
  17. static netdata_syscall_stat_t process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_END];
  18. static netdata_publish_syscall_t process_publish_aggregated[NETDATA_KEY_PUBLISH_PROCESS_END];
  19. static ebpf_data_t process_data;
  20. ebpf_process_stat_t **global_process_stats = NULL;
  21. ebpf_process_publish_apps_t **current_apps_data = NULL;
  22. int process_enabled = 0;
  23. static int *map_fd = NULL;
  24. static struct bpf_object *objects = NULL;
  25. static struct bpf_link **probe_links = NULL;
  26. struct config process_config = { .first_section = NULL,
  27. .last_section = NULL,
  28. .mutex = NETDATA_MUTEX_INITIALIZER,
  29. .index = { .avl_tree = { .root = NULL, .compar = appconfig_section_compare },
  30. .rwlock = AVL_LOCK_INITIALIZER } };
  31. /*****************************************************************
  32. *
  33. * PROCESS DATA AND SEND TO NETDATA
  34. *
  35. *****************************************************************/
  36. /**
  37. * Update publish structure before to send data to Netdata.
  38. *
  39. * @param publish the first output structure with independent dimensions
  40. * @param pvc the second output structure with correlated dimensions
  41. * @param input the structure with the input data.
  42. */
  43. static void ebpf_update_global_publish(
  44. netdata_publish_syscall_t *publish, netdata_publish_vfs_common_t *pvc, netdata_syscall_stat_t *input)
  45. {
  46. netdata_publish_syscall_t *move = publish;
  47. int selector = NETDATA_KEY_PUBLISH_PROCESS_OPEN;
  48. while (move) {
  49. // Until NETDATA_KEY_PUBLISH_PROCESS_READ we are creating accumulators, so it is possible
  50. // to use incremental charts, but after this we will do some math with the values, so we are storing
  51. // absolute values
  52. if (selector < NETDATA_KEY_PUBLISH_PROCESS_READ) {
  53. move->ncall = input->call;
  54. move->nbyte = input->bytes;
  55. move->nerr = input->ecall;
  56. } else {
  57. move->ncall = (input->call > move->pcall) ? input->call - move->pcall : move->pcall - input->call;
  58. move->nbyte = (input->bytes > move->pbyte) ? input->bytes - move->pbyte : move->pbyte - input->bytes;
  59. move->nerr = (input->ecall > move->nerr) ? input->ecall - move->perr : move->perr - input->ecall;
  60. move->pcall = input->call;
  61. move->pbyte = input->bytes;
  62. move->perr = input->ecall;
  63. }
  64. input = input->next;
  65. move = move->next;
  66. selector++;
  67. }
  68. pvc->write = -((long)publish[NETDATA_KEY_PUBLISH_PROCESS_WRITE].nbyte);
  69. pvc->read = (long)publish[NETDATA_KEY_PUBLISH_PROCESS_READ].nbyte;
  70. pvc->running = (long)publish[NETDATA_KEY_PUBLISH_PROCESS_FORK].ncall - (long)publish[NETDATA_KEY_PUBLISH_PROCESS_CLONE].ncall;
  71. publish[NETDATA_KEY_PUBLISH_PROCESS_RELEASE_TASK].ncall = -publish[NETDATA_KEY_PUBLISH_PROCESS_RELEASE_TASK].ncall;
  72. pvc->zombie = (long)publish[NETDATA_KEY_PUBLISH_PROCESS_EXIT].ncall + (long)publish[NETDATA_KEY_PUBLISH_PROCESS_RELEASE_TASK].ncall;
  73. }
  74. /**
  75. * Call the necessary functions to create a chart.
  76. *
  77. * @param family the chart family
  78. * @param move the pointer with the values that will be published
  79. */
  80. static void write_status_chart(char *family, netdata_publish_vfs_common_t *pvc)
  81. {
  82. write_begin_chart(family, NETDATA_PROCESS_STATUS_NAME);
  83. write_chart_dimension(status[0], (long long)pvc->running);
  84. write_chart_dimension(status[1], (long long)pvc->zombie);
  85. write_end_chart();
  86. }
  87. /**
  88. * Send data to Netdata calling auxiliar functions.
  89. *
  90. * @param em the structure with thread information
  91. */
  92. static void ebpf_process_send_data(ebpf_module_t *em)
  93. {
  94. netdata_publish_vfs_common_t pvc;
  95. ebpf_update_global_publish(process_publish_aggregated, &pvc, process_aggregated_data);
  96. write_count_chart(
  97. NETDATA_FILE_OPEN_CLOSE_COUNT, NETDATA_EBPF_FAMILY, process_publish_aggregated, 2);
  98. write_count_chart(
  99. NETDATA_VFS_FILE_CLEAN_COUNT, NETDATA_EBPF_FAMILY, &process_publish_aggregated[NETDATA_DEL_START], 1);
  100. write_count_chart(
  101. NETDATA_VFS_FILE_IO_COUNT, NETDATA_EBPF_FAMILY, &process_publish_aggregated[NETDATA_IN_START_BYTE], 2);
  102. write_count_chart(
  103. NETDATA_EXIT_SYSCALL, NETDATA_EBPF_FAMILY, &process_publish_aggregated[NETDATA_EXIT_START], 2);
  104. write_count_chart(
  105. NETDATA_PROCESS_SYSCALL, NETDATA_EBPF_FAMILY, &process_publish_aggregated[NETDATA_PROCESS_START], 2);
  106. write_status_chart(NETDATA_EBPF_FAMILY, &pvc);
  107. if (em->mode < MODE_ENTRY) {
  108. write_err_chart(
  109. NETDATA_FILE_OPEN_ERR_COUNT, NETDATA_EBPF_FAMILY, process_publish_aggregated, 2);
  110. write_err_chart(
  111. NETDATA_VFS_FILE_ERR_COUNT, NETDATA_EBPF_FAMILY, &process_publish_aggregated[2], NETDATA_VFS_ERRORS);
  112. write_err_chart(
  113. NETDATA_PROCESS_ERROR_NAME, NETDATA_EBPF_FAMILY, &process_publish_aggregated[NETDATA_PROCESS_START], 2);
  114. }
  115. write_io_chart(NETDATA_VFS_IO_FILE_BYTES, NETDATA_EBPF_FAMILY,
  116. process_id_names[NETDATA_KEY_PUBLISH_PROCESS_WRITE], (long long) pvc.write,
  117. process_id_names[NETDATA_KEY_PUBLISH_PROCESS_READ], (long long)pvc.read);
  118. }
  119. /**
  120. * Sum values for pid
  121. *
  122. * @param root the structure with all available PIDs
  123. *
  124. * @param offset the address that we are reading
  125. *
  126. * @return it returns the sum of all PIDs
  127. */
  128. long long ebpf_process_sum_values_for_pids(struct pid_on_target *root, size_t offset)
  129. {
  130. long long ret = 0;
  131. while (root) {
  132. int32_t pid = root->pid;
  133. ebpf_process_publish_apps_t *w = current_apps_data[pid];
  134. if (w) {
  135. ret += get_value_from_structure((char *)w, offset);
  136. }
  137. root = root->next;
  138. }
  139. return ret;
  140. }
  141. /**
  142. * Remove process pid
  143. *
  144. * Remove from PID task table when task_release was called.
  145. */
  146. void ebpf_process_remove_pids()
  147. {
  148. struct pid_stat *pids = root_of_pids;
  149. int pid_fd = map_fd[0];
  150. while (pids) {
  151. uint32_t pid = pids->pid;
  152. ebpf_process_stat_t *w = global_process_stats[pid];
  153. if (w) {
  154. if (w->removeme) {
  155. freez(w);
  156. global_process_stats[pid] = NULL;
  157. bpf_map_delete_elem(pid_fd, &pid);
  158. }
  159. }
  160. pids = pids->next;
  161. }
  162. }
  163. /**
  164. * Send data to Netdata calling auxiliar functions.
  165. *
  166. * @param em the structure with thread information
  167. * @param root the target list.
  168. */
  169. void ebpf_process_send_apps_data(ebpf_module_t *em, struct target *root)
  170. {
  171. struct target *w;
  172. collected_number value;
  173. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_FILE_OPEN);
  174. for (w = root; w; w = w->next) {
  175. if (unlikely(w->exposed && w->processes)) {
  176. value = ebpf_process_sum_values_for_pids(w->root_pid, offsetof(ebpf_process_publish_apps_t, call_sys_open));
  177. write_chart_dimension(w->name, value);
  178. }
  179. }
  180. write_end_chart();
  181. if (em->mode < MODE_ENTRY) {
  182. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_FILE_OPEN_ERROR);
  183. for (w = root; w; w = w->next) {
  184. if (unlikely(w->exposed && w->processes)) {
  185. value = ebpf_process_sum_values_for_pids(
  186. w->root_pid, offsetof(ebpf_process_publish_apps_t, ecall_sys_open));
  187. write_chart_dimension(w->name, value);
  188. }
  189. }
  190. write_end_chart();
  191. }
  192. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_FILE_CLOSED);
  193. for (w = root; w; w = w->next) {
  194. if (unlikely(w->exposed && w->processes)) {
  195. value =
  196. ebpf_process_sum_values_for_pids(w->root_pid, offsetof(ebpf_process_publish_apps_t, call_close_fd));
  197. write_chart_dimension(w->name, value);
  198. }
  199. }
  200. write_end_chart();
  201. if (em->mode < MODE_ENTRY) {
  202. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_FILE_CLOSE_ERROR);
  203. for (w = root; w; w = w->next) {
  204. if (unlikely(w->exposed && w->processes)) {
  205. value = ebpf_process_sum_values_for_pids(
  206. w->root_pid, offsetof(ebpf_process_publish_apps_t, ecall_close_fd));
  207. write_chart_dimension(w->name, value);
  208. }
  209. }
  210. write_end_chart();
  211. }
  212. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_FILE_DELETED);
  213. for (w = root; w; w = w->next) {
  214. if (unlikely(w->exposed && w->processes)) {
  215. value =
  216. ebpf_process_sum_values_for_pids(w->root_pid, offsetof(ebpf_process_publish_apps_t, call_vfs_unlink));
  217. write_chart_dimension(w->name, value);
  218. }
  219. }
  220. write_end_chart();
  221. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_VFS_WRITE_CALLS);
  222. for (w = root; w; w = w->next) {
  223. if (unlikely(w->exposed && w->processes)) {
  224. value = ebpf_process_sum_values_for_pids(
  225. w->root_pid, offsetof(ebpf_process_publish_apps_t, call_write));
  226. write_chart_dimension(w->name, value);
  227. }
  228. }
  229. write_end_chart();
  230. if (em->mode < MODE_ENTRY) {
  231. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_VFS_WRITE_CALLS_ERROR);
  232. for (w = root; w; w = w->next) {
  233. if (unlikely(w->exposed && w->processes)) {
  234. value = ebpf_process_sum_values_for_pids(
  235. w->root_pid, offsetof(ebpf_process_publish_apps_t, ecall_write));
  236. write_chart_dimension(w->name, value);
  237. }
  238. }
  239. write_end_chart();
  240. }
  241. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_VFS_READ_CALLS);
  242. for (w = root; w; w = w->next) {
  243. if (unlikely(w->exposed && w->processes)) {
  244. value =
  245. ebpf_process_sum_values_for_pids(w->root_pid, offsetof(ebpf_process_publish_apps_t, call_read));
  246. write_chart_dimension(w->name, value);
  247. }
  248. }
  249. write_end_chart();
  250. if (em->mode < MODE_ENTRY) {
  251. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_VFS_READ_CALLS_ERROR);
  252. for (w = root; w; w = w->next) {
  253. if (unlikely(w->exposed && w->processes)) {
  254. value = ebpf_process_sum_values_for_pids(
  255. w->root_pid, offsetof(ebpf_process_publish_apps_t, ecall_read));
  256. write_chart_dimension(w->name, value);
  257. }
  258. }
  259. write_end_chart();
  260. }
  261. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_VFS_WRITE_BYTES);
  262. for (w = root; w; w = w->next) {
  263. if (unlikely(w->exposed && w->processes)) {
  264. value = ebpf_process_sum_values_for_pids(
  265. w->root_pid, offsetof(ebpf_process_publish_apps_t, bytes_written));
  266. write_chart_dimension(w->name, value);
  267. }
  268. }
  269. write_end_chart();
  270. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_VFS_READ_BYTES);
  271. for (w = root; w; w = w->next) {
  272. if (unlikely(w->exposed && w->processes)) {
  273. value = ebpf_process_sum_values_for_pids(
  274. w->root_pid, offsetof(ebpf_process_publish_apps_t, bytes_read));
  275. write_chart_dimension(w->name, value);
  276. }
  277. }
  278. write_end_chart();
  279. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_TASK_PROCESS);
  280. for (w = root; w; w = w->next) {
  281. if (unlikely(w->exposed && w->processes)) {
  282. value =
  283. ebpf_process_sum_values_for_pids(w->root_pid, offsetof(ebpf_process_publish_apps_t, call_do_fork));
  284. write_chart_dimension(w->name, value);
  285. }
  286. }
  287. write_end_chart();
  288. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_TASK_THREAD);
  289. for (w = root; w; w = w->next) {
  290. if (unlikely(w->exposed && w->processes)) {
  291. value =
  292. ebpf_process_sum_values_for_pids(w->root_pid, offsetof(ebpf_process_publish_apps_t, call_sys_clone));
  293. write_chart_dimension(w->name, value);
  294. }
  295. }
  296. write_end_chart();
  297. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_TASK_CLOSE);
  298. for (w = root; w; w = w->next) {
  299. if (unlikely(w->exposed && w->processes)) {
  300. value = ebpf_process_sum_values_for_pids(w->root_pid, offsetof(ebpf_process_publish_apps_t,
  301. call_release_task));
  302. write_chart_dimension(w->name, value);
  303. }
  304. }
  305. write_end_chart();
  306. ebpf_process_remove_pids();
  307. }
  308. /*****************************************************************
  309. *
  310. * READ INFORMATION FROM KERNEL RING
  311. *
  312. *****************************************************************/
  313. /**
  314. * Read the hash table and store data to allocated vectors.
  315. */
  316. static void read_hash_global_tables()
  317. {
  318. uint64_t idx;
  319. netdata_idx_t res[NETDATA_GLOBAL_VECTOR];
  320. netdata_idx_t *val = process_hash_values;
  321. for (idx = 0; idx < NETDATA_GLOBAL_VECTOR; idx++) {
  322. if (!bpf_map_lookup_elem(map_fd[1], &idx, val)) {
  323. uint64_t total = 0;
  324. int i;
  325. int end = (running_on_kernel < NETDATA_KERNEL_V4_15) ? 1 : ebpf_nprocs;
  326. for (i = 0; i < end; i++)
  327. total += val[i];
  328. res[idx] = total;
  329. } else {
  330. res[idx] = 0;
  331. }
  332. }
  333. process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_OPEN].call = res[NETDATA_KEY_CALLS_DO_SYS_OPEN];
  334. process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_CLOSE].call = res[NETDATA_KEY_CALLS_CLOSE_FD];
  335. process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_UNLINK].call = res[NETDATA_KEY_CALLS_VFS_UNLINK];
  336. process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_READ].call = res[NETDATA_KEY_CALLS_VFS_READ] + res[NETDATA_KEY_CALLS_VFS_READV];
  337. process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_WRITE].call = res[NETDATA_KEY_CALLS_VFS_WRITE] + res[NETDATA_KEY_CALLS_VFS_WRITEV];
  338. process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_EXIT].call = res[NETDATA_KEY_CALLS_DO_EXIT];
  339. process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_RELEASE_TASK].call = res[NETDATA_KEY_CALLS_RELEASE_TASK];
  340. process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_FORK].call = res[NETDATA_KEY_CALLS_DO_FORK];
  341. process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_CLONE].call = res[NETDATA_KEY_CALLS_SYS_CLONE];
  342. process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_OPEN].ecall = res[NETDATA_KEY_ERROR_DO_SYS_OPEN];
  343. process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_CLOSE].ecall = res[NETDATA_KEY_ERROR_CLOSE_FD];
  344. process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_UNLINK].ecall = res[NETDATA_KEY_ERROR_VFS_UNLINK];
  345. process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_READ].ecall = res[NETDATA_KEY_ERROR_VFS_READ] + res[NETDATA_KEY_ERROR_VFS_READV];
  346. process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_WRITE].ecall = res[NETDATA_KEY_ERROR_VFS_WRITE] + res[NETDATA_KEY_ERROR_VFS_WRITEV];
  347. process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_FORK].ecall = res[NETDATA_KEY_ERROR_DO_FORK];
  348. process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_CLONE].ecall = res[NETDATA_KEY_ERROR_SYS_CLONE];
  349. process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_WRITE].bytes = (uint64_t)res[NETDATA_KEY_BYTES_VFS_WRITE] +
  350. (uint64_t)res[NETDATA_KEY_BYTES_VFS_WRITEV];
  351. process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_READ].bytes = (uint64_t)res[NETDATA_KEY_BYTES_VFS_READ] +
  352. (uint64_t)res[NETDATA_KEY_BYTES_VFS_READV];
  353. }
  354. /**
  355. * Read the hash table and store data to allocated vectors.
  356. */
  357. static void ebpf_process_update_apps_data()
  358. {
  359. struct pid_stat *pids = root_of_pids;
  360. while (pids) {
  361. uint32_t current_pid = pids->pid;
  362. ebpf_process_stat_t *ps = global_process_stats[current_pid];
  363. if (!ps) {
  364. pids = pids->next;
  365. continue;
  366. }
  367. ebpf_process_publish_apps_t *cad = current_apps_data[current_pid];
  368. if (!cad) {
  369. cad = callocz(1, sizeof(ebpf_process_publish_apps_t));
  370. current_apps_data[current_pid] = cad;
  371. }
  372. //Read data
  373. cad->call_sys_open = ps->open_call;
  374. cad->call_close_fd = ps->close_call;
  375. cad->call_vfs_unlink = ps->unlink_call;
  376. cad->call_read = ps->read_call + ps->readv_call;
  377. cad->call_write = ps->write_call + ps->writev_call;
  378. cad->call_do_exit = ps->exit_call;
  379. cad->call_release_task = ps->release_call;
  380. cad->call_do_fork = ps->fork_call;
  381. cad->call_sys_clone = ps->clone_call;
  382. cad->ecall_sys_open = ps->open_err;
  383. cad->ecall_close_fd = ps->close_err;
  384. cad->ecall_vfs_unlink = ps->unlink_err;
  385. cad->ecall_read = ps->read_err + ps->readv_err;
  386. cad->ecall_write = ps->write_err + ps->writev_err;
  387. cad->ecall_do_fork = ps->fork_err;
  388. cad->ecall_sys_clone = ps->clone_err;
  389. cad->bytes_written = (uint64_t)ps->write_bytes + (uint64_t)ps->write_bytes;
  390. cad->bytes_read = (uint64_t)ps->read_bytes + (uint64_t)ps->readv_bytes;
  391. pids = pids->next;
  392. }
  393. }
  394. /*****************************************************************
  395. *
  396. * FUNCTIONS TO CREATE CHARTS
  397. *
  398. *****************************************************************/
  399. /**
  400. * Create IO chart
  401. *
  402. * @param family the chart family
  403. * @param name the chart name
  404. * @param axis the axis label
  405. * @param web the group name used to attach the chart on dashaboard
  406. * @param order the order number of the specified chart
  407. * @param algorithm the algorithm used to make the charts.
  408. */
  409. static void ebpf_create_io_chart(char *family, char *name, char *axis, char *web, int order, int algorithm)
  410. {
  411. printf("CHART %s.%s '' 'Bytes written and read' '%s' '%s' '' line %d %d\n",
  412. family,
  413. name,
  414. axis,
  415. web,
  416. order,
  417. update_every);
  418. printf("DIMENSION %s %s %s 1 1\n",
  419. process_id_names[NETDATA_KEY_PUBLISH_PROCESS_READ],
  420. process_dimension_names[NETDATA_KEY_PUBLISH_PROCESS_READ],
  421. ebpf_algorithms[algorithm]);
  422. printf("DIMENSION %s %s %s 1 1\n",
  423. process_id_names[NETDATA_KEY_PUBLISH_PROCESS_WRITE],
  424. process_dimension_names[NETDATA_KEY_PUBLISH_PROCESS_WRITE],
  425. ebpf_algorithms[algorithm]);
  426. }
  427. /**
  428. * Create process status chart
  429. *
  430. * @param family the chart family
  431. * @param name the chart name
  432. * @param axis the axis label
  433. * @param web the group name used to attach the chart on dashaboard
  434. * @param order the order number of the specified chart
  435. */
  436. static void ebpf_process_status_chart(char *family, char *name, char *axis,
  437. char *web, char *algorithm, int order)
  438. {
  439. printf("CHART %s.%s '' 'Process not closed' '%s' '%s' '' line %d %d ''\n",
  440. family,
  441. name,
  442. axis,
  443. web,
  444. order,
  445. update_every);
  446. printf("DIMENSION %s '' %s 1 1\n", status[0], algorithm);
  447. printf("DIMENSION %s '' %s 1 1\n", status[1], algorithm);
  448. }
  449. /**
  450. * Create global charts
  451. *
  452. * Call ebpf_create_chart to create the charts for the collector.
  453. *
  454. * @param em a pointer to the structure with the default values.
  455. */
  456. static void ebpf_create_global_charts(ebpf_module_t *em)
  457. {
  458. ebpf_create_chart(NETDATA_EBPF_FAMILY,
  459. NETDATA_FILE_OPEN_CLOSE_COUNT,
  460. "Open and close calls",
  461. EBPF_COMMON_DIMENSION_CALL,
  462. NETDATA_FILE_GROUP,
  463. NULL,
  464. NETDATA_EBPF_CHART_TYPE_LINE,
  465. 21000,
  466. ebpf_create_global_dimension,
  467. process_publish_aggregated,
  468. 2);
  469. if (em->mode < MODE_ENTRY) {
  470. ebpf_create_chart(NETDATA_EBPF_FAMILY,
  471. NETDATA_FILE_OPEN_ERR_COUNT,
  472. "Open fails",
  473. EBPF_COMMON_DIMENSION_CALL,
  474. NETDATA_FILE_GROUP,
  475. NULL,
  476. NETDATA_EBPF_CHART_TYPE_LINE,
  477. 21001,
  478. ebpf_create_global_dimension,
  479. process_publish_aggregated,
  480. 2);
  481. }
  482. ebpf_create_chart(NETDATA_EBPF_FAMILY,
  483. NETDATA_VFS_FILE_CLEAN_COUNT,
  484. "Remove files",
  485. EBPF_COMMON_DIMENSION_CALL,
  486. NETDATA_VFS_GROUP,
  487. NULL,
  488. NETDATA_EBPF_CHART_TYPE_LINE,
  489. 21002,
  490. ebpf_create_global_dimension,
  491. &process_publish_aggregated[NETDATA_DEL_START],
  492. 1);
  493. ebpf_create_chart(NETDATA_EBPF_FAMILY,
  494. NETDATA_VFS_FILE_IO_COUNT,
  495. "Calls to IO",
  496. EBPF_COMMON_DIMENSION_CALL,
  497. NETDATA_VFS_GROUP,
  498. NULL,
  499. NETDATA_EBPF_CHART_TYPE_LINE,
  500. 21003,
  501. ebpf_create_global_dimension,
  502. &process_publish_aggregated[NETDATA_IN_START_BYTE],
  503. 2);
  504. ebpf_create_io_chart(NETDATA_EBPF_FAMILY,
  505. NETDATA_VFS_IO_FILE_BYTES, EBPF_COMMON_DIMENSION_BYTES,
  506. NETDATA_VFS_GROUP,
  507. 21004,
  508. NETDATA_EBPF_ABSOLUTE_IDX);
  509. if (em->mode < MODE_ENTRY) {
  510. ebpf_create_chart(NETDATA_EBPF_FAMILY,
  511. NETDATA_VFS_FILE_ERR_COUNT,
  512. "Fails to write or read",
  513. EBPF_COMMON_DIMENSION_CALL,
  514. NETDATA_VFS_GROUP,
  515. NULL,
  516. NETDATA_EBPF_CHART_TYPE_LINE,
  517. 21005,
  518. ebpf_create_global_dimension,
  519. &process_publish_aggregated[2],
  520. NETDATA_VFS_ERRORS);
  521. }
  522. ebpf_create_chart(NETDATA_EBPF_FAMILY,
  523. NETDATA_PROCESS_SYSCALL,
  524. "Start process",
  525. EBPF_COMMON_DIMENSION_CALL,
  526. NETDATA_PROCESS_GROUP,
  527. NULL,
  528. NETDATA_EBPF_CHART_TYPE_LINE,
  529. 21006,
  530. ebpf_create_global_dimension,
  531. &process_publish_aggregated[NETDATA_PROCESS_START],
  532. 2);
  533. ebpf_create_chart(NETDATA_EBPF_FAMILY,
  534. NETDATA_EXIT_SYSCALL,
  535. "Exit process",
  536. EBPF_COMMON_DIMENSION_CALL,
  537. NETDATA_PROCESS_GROUP,
  538. NULL,
  539. NETDATA_EBPF_CHART_TYPE_LINE,
  540. 21007,
  541. ebpf_create_global_dimension,
  542. &process_publish_aggregated[NETDATA_EXIT_START],
  543. 2);
  544. ebpf_process_status_chart(NETDATA_EBPF_FAMILY,
  545. NETDATA_PROCESS_STATUS_NAME,
  546. EBPF_COMMON_DIMENSION_DIFFERENCE,
  547. NETDATA_PROCESS_GROUP,
  548. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX],
  549. 21008);
  550. if (em->mode < MODE_ENTRY) {
  551. ebpf_create_chart(NETDATA_EBPF_FAMILY,
  552. NETDATA_PROCESS_ERROR_NAME,
  553. "Fails to create process",
  554. EBPF_COMMON_DIMENSION_CALL,
  555. NETDATA_PROCESS_GROUP,
  556. NULL,
  557. NETDATA_EBPF_CHART_TYPE_LINE,
  558. 21009,
  559. ebpf_create_global_dimension,
  560. &process_publish_aggregated[NETDATA_PROCESS_START],
  561. 2);
  562. }
  563. }
  564. /**
  565. * Create process apps charts
  566. *
  567. * Call ebpf_create_chart to create the charts on apps submenu.
  568. *
  569. * @param em a pointer to the structure with the default values.
  570. * @param ptr a pointer for the targets.
  571. */
  572. void ebpf_process_create_apps_charts(struct ebpf_module *em, void *ptr)
  573. {
  574. struct target *root = ptr;
  575. ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_FILE_OPEN,
  576. "Number of open files",
  577. EBPF_COMMON_DIMENSION_CALL,
  578. NETDATA_APPS_FILE_GROUP,
  579. NETDATA_EBPF_CHART_TYPE_STACKED,
  580. 20061,
  581. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  582. root);
  583. if (em->mode < MODE_ENTRY) {
  584. ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_FILE_OPEN_ERROR,
  585. "Fails to open files",
  586. EBPF_COMMON_DIMENSION_CALL,
  587. NETDATA_APPS_FILE_GROUP,
  588. NETDATA_EBPF_CHART_TYPE_STACKED,
  589. 20062,
  590. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  591. root);
  592. }
  593. ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_FILE_CLOSED,
  594. "Files closed",
  595. EBPF_COMMON_DIMENSION_CALL,
  596. NETDATA_APPS_FILE_GROUP,
  597. NETDATA_EBPF_CHART_TYPE_STACKED,
  598. 20063,
  599. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  600. root);
  601. if (em->mode < MODE_ENTRY) {
  602. ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_FILE_CLOSE_ERROR,
  603. "Fails to close files",
  604. EBPF_COMMON_DIMENSION_CALL,
  605. NETDATA_APPS_FILE_GROUP,
  606. NETDATA_EBPF_CHART_TYPE_STACKED,
  607. 20064,
  608. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  609. root);
  610. }
  611. ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_FILE_DELETED,
  612. "Files deleted",
  613. EBPF_COMMON_DIMENSION_CALL,
  614. NETDATA_APPS_VFS_GROUP,
  615. NETDATA_EBPF_CHART_TYPE_STACKED,
  616. 20065,
  617. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  618. root);
  619. ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_VFS_WRITE_CALLS,
  620. "Write to disk",
  621. EBPF_COMMON_DIMENSION_CALL,
  622. NETDATA_APPS_VFS_GROUP,
  623. NETDATA_EBPF_CHART_TYPE_STACKED,
  624. 20066,
  625. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  626. apps_groups_root_target);
  627. if (em->mode < MODE_ENTRY) {
  628. ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_VFS_WRITE_CALLS_ERROR,
  629. "Fails to write",
  630. EBPF_COMMON_DIMENSION_CALL,
  631. NETDATA_APPS_VFS_GROUP,
  632. NETDATA_EBPF_CHART_TYPE_STACKED,
  633. 20067,
  634. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  635. root);
  636. }
  637. ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_VFS_READ_CALLS,
  638. "Read from disk",
  639. EBPF_COMMON_DIMENSION_CALL,
  640. NETDATA_APPS_VFS_GROUP,
  641. NETDATA_EBPF_CHART_TYPE_STACKED,
  642. 20068,
  643. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  644. root);
  645. if (em->mode < MODE_ENTRY) {
  646. ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_VFS_READ_CALLS_ERROR,
  647. "Fails to read",
  648. EBPF_COMMON_DIMENSION_CALL,
  649. NETDATA_APPS_VFS_GROUP,
  650. NETDATA_EBPF_CHART_TYPE_STACKED,
  651. 20069,
  652. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  653. root);
  654. }
  655. ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_VFS_WRITE_BYTES,
  656. "Bytes written on disk", EBPF_COMMON_DIMENSION_BYTES,
  657. NETDATA_APPS_VFS_GROUP,
  658. NETDATA_EBPF_CHART_TYPE_STACKED,
  659. 20070,
  660. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  661. root);
  662. ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_VFS_READ_BYTES,
  663. "Bytes read from disk", EBPF_COMMON_DIMENSION_BYTES,
  664. NETDATA_APPS_VFS_GROUP,
  665. NETDATA_EBPF_CHART_TYPE_STACKED,
  666. 20071,
  667. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  668. root);
  669. ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_TASK_PROCESS,
  670. "Process started",
  671. EBPF_COMMON_DIMENSION_CALL,
  672. NETDATA_APPS_PROCESS_GROUP,
  673. NETDATA_EBPF_CHART_TYPE_STACKED,
  674. 20072,
  675. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX],
  676. root);
  677. ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_TASK_THREAD,
  678. "Threads started",
  679. EBPF_COMMON_DIMENSION_CALL,
  680. NETDATA_APPS_PROCESS_GROUP,
  681. NETDATA_EBPF_CHART_TYPE_STACKED,
  682. 20073,
  683. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX],
  684. root);
  685. ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_TASK_CLOSE,
  686. "Tasks closed",
  687. EBPF_COMMON_DIMENSION_CALL,
  688. NETDATA_APPS_PROCESS_GROUP,
  689. NETDATA_EBPF_CHART_TYPE_STACKED,
  690. 20074,
  691. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX],
  692. root);
  693. }
  694. /**
  695. * Create apps charts
  696. *
  697. * Call ebpf_create_chart to create the charts on apps submenu.
  698. *
  699. * @param em a pointer to the structure with the default values.
  700. * @param root a pointer for the targets.
  701. */
  702. static void ebpf_create_apps_charts(ebpf_module_t *em, struct target *root)
  703. {
  704. struct target *w;
  705. int newly_added = 0;
  706. for (w = root; w; w = w->next) {
  707. if (w->target)
  708. continue;
  709. if (unlikely(w->processes && (debug_enabled || w->debug_enabled))) {
  710. struct pid_on_target *pid_on_target;
  711. fprintf(
  712. stderr, "ebpf.plugin: target '%s' has aggregated %u process%s:", w->name, w->processes,
  713. (w->processes == 1) ? "" : "es");
  714. for (pid_on_target = w->root_pid; pid_on_target; pid_on_target = pid_on_target->next) {
  715. fprintf(stderr, " %d", pid_on_target->pid);
  716. }
  717. fputc('\n', stderr);
  718. }
  719. if (!w->exposed && w->processes) {
  720. newly_added++;
  721. w->exposed = 1;
  722. if (debug_enabled || w->debug_enabled)
  723. debug_log_int("%s just added - regenerating charts.", w->name);
  724. }
  725. }
  726. if (!newly_added)
  727. return;
  728. int counter;
  729. for (counter = 0; ebpf_modules[counter].thread_name; counter++) {
  730. ebpf_module_t *current = &ebpf_modules[counter];
  731. if (current->enabled && current->apps_charts && current->apps_routine)
  732. current->apps_routine(em, root);
  733. }
  734. }
  735. /*****************************************************************
  736. *
  737. * FUNCTIONS WITH THE MAIN LOOP
  738. *
  739. *****************************************************************/
  740. /**
  741. * Main loop for this collector.
  742. *
  743. * @param step the number of microseconds used with heart beat
  744. * @param em the structure with thread information
  745. */
  746. static void process_collector(usec_t step, ebpf_module_t *em)
  747. {
  748. heartbeat_t hb;
  749. heartbeat_init(&hb);
  750. int publish_global = em->global_charts;
  751. int apps_enabled = em->apps_charts;
  752. int pid_fd = map_fd[0];
  753. while (!close_ebpf_plugin) {
  754. usec_t dt = heartbeat_next(&hb, step);
  755. (void)dt;
  756. read_hash_global_tables();
  757. pthread_mutex_lock(&collect_data_mutex);
  758. cleanup_exited_pids();
  759. collect_data_for_all_processes(pid_fd);
  760. ebpf_create_apps_charts(em, apps_groups_root_target);
  761. pthread_cond_broadcast(&collect_data_cond_var);
  762. pthread_mutex_unlock(&collect_data_mutex);
  763. int publish_apps = 0;
  764. if (apps_enabled && all_pids_count > 0) {
  765. publish_apps = 1;
  766. ebpf_process_update_apps_data();
  767. }
  768. pthread_mutex_lock(&lock);
  769. if (publish_global) {
  770. ebpf_process_send_data(em);
  771. }
  772. if (publish_apps) {
  773. ebpf_process_send_apps_data(em, apps_groups_root_target);
  774. }
  775. pthread_mutex_unlock(&lock);
  776. fflush(stdout);
  777. }
  778. }
  779. /*****************************************************************
  780. *
  781. * FUNCTIONS TO CLOSE THE THREAD
  782. *
  783. *****************************************************************/
  784. void clean_global_memory() {
  785. int pid_fd = map_fd[0];
  786. struct pid_stat *pids = root_of_pids;
  787. while (pids) {
  788. uint32_t pid = pids->pid;
  789. freez(global_process_stats[pid]);
  790. bpf_map_delete_elem(pid_fd, &pid);
  791. freez(current_apps_data[pid]);
  792. pids = pids->next;
  793. }
  794. }
  795. void clean_pid_on_target(struct pid_on_target *ptr) {
  796. while (ptr) {
  797. struct pid_on_target *next = ptr->next;
  798. freez(ptr);
  799. ptr = next;
  800. }
  801. }
  802. void clean_apps_structures(struct target *ptr) {
  803. struct target *agdt = ptr;
  804. while (agdt) {
  805. struct target *next = agdt->next;
  806. clean_pid_on_target(agdt->root_pid);
  807. freez(agdt);
  808. agdt = next;
  809. }
  810. }
  811. /**
  812. * Clean up the main thread.
  813. *
  814. * @param ptr thread data.
  815. */
  816. static void ebpf_process_cleanup(void *ptr)
  817. {
  818. UNUSED(ptr);
  819. heartbeat_t hb;
  820. heartbeat_init(&hb);
  821. uint32_t tick = 50*USEC_PER_MS;
  822. while (!finalized_threads) {
  823. usec_t dt = heartbeat_next(&hb, tick);
  824. UNUSED(dt);
  825. }
  826. ebpf_cleanup_publish_syscall(process_publish_aggregated);
  827. freez(process_hash_values);
  828. clean_global_memory();
  829. freez(global_process_stats);
  830. freez(current_apps_data);
  831. clean_apps_structures(apps_groups_root_target);
  832. freez(process_data.map_fd);
  833. struct bpf_program *prog;
  834. size_t i = 0 ;
  835. bpf_object__for_each_program(prog, objects) {
  836. bpf_link__destroy(probe_links[i]);
  837. i++;
  838. }
  839. bpf_object__close(objects);
  840. }
  841. /*****************************************************************
  842. *
  843. * FUNCTIONS TO START THREAD
  844. *
  845. *****************************************************************/
  846. /**
  847. * Allocate vectors used with this thread.
  848. * We are not testing the return, because callocz does this and shutdown the software
  849. * case it was not possible to allocate.
  850. *
  851. * @param length is the length for the vectors used inside the collector.
  852. */
  853. static void ebpf_process_allocate_global_vectors(size_t length)
  854. {
  855. memset(process_aggregated_data, 0, length * sizeof(netdata_syscall_stat_t));
  856. memset(process_publish_aggregated, 0, length * sizeof(netdata_publish_syscall_t));
  857. process_hash_values = callocz(ebpf_nprocs, sizeof(netdata_idx_t));
  858. global_process_stats = callocz((size_t)pid_max, sizeof(ebpf_process_stat_t *));
  859. current_apps_data = callocz((size_t)pid_max, sizeof(ebpf_process_publish_apps_t *));
  860. }
  861. static void change_syscalls()
  862. {
  863. static char *lfork = { "do_fork" };
  864. process_id_names[7] = lfork;
  865. }
  866. /**
  867. * Set local variables
  868. *
  869. */
  870. static void set_local_pointers()
  871. {
  872. map_fd = process_data.map_fd;
  873. if (process_data.isrh >= NETDATA_MINIMUM_RH_VERSION && process_data.isrh < NETDATA_RH_8)
  874. change_syscalls();
  875. }
  876. /*****************************************************************
  877. *
  878. * EBPF PROCESS THREAD
  879. *
  880. *****************************************************************/
  881. /**
  882. *
  883. */
  884. static void wait_for_all_threads_die()
  885. {
  886. ebpf_modules[EBPF_MODULE_PROCESS_IDX].enabled = 0;
  887. heartbeat_t hb;
  888. heartbeat_init(&hb);
  889. int max = 10;
  890. int i;
  891. for (i = 0; i < max; i++) {
  892. heartbeat_next(&hb, 200000);
  893. size_t j, counter = 0, compare = 0;
  894. for (j = 0; ebpf_modules[j].thread_name; j++) {
  895. if (!ebpf_modules[j].enabled)
  896. counter++;
  897. compare++;
  898. }
  899. if (counter == compare)
  900. break;
  901. }
  902. }
  903. /**
  904. * Process thread
  905. *
  906. * Thread used to generate process charts.
  907. *
  908. * @param ptr a pointer to `struct ebpf_module`
  909. *
  910. * @return It always return NULL
  911. */
  912. void *ebpf_process_thread(void *ptr)
  913. {
  914. netdata_thread_cleanup_push(ebpf_process_cleanup, ptr);
  915. ebpf_module_t *em = (ebpf_module_t *)ptr;
  916. process_enabled = em->enabled;
  917. fill_ebpf_data(&process_data);
  918. pthread_mutex_lock(&lock);
  919. ebpf_process_allocate_global_vectors(NETDATA_KEY_PUBLISH_PROCESS_END);
  920. if (ebpf_update_kernel(&process_data)) {
  921. pthread_mutex_unlock(&lock);
  922. goto endprocess;
  923. }
  924. ebpf_update_module(em, &process_config, NETDATA_PROCESS_CONFIG_FILE);
  925. set_local_pointers();
  926. probe_links = ebpf_load_program(ebpf_plugin_dir, em, kernel_string, &objects, process_data.map_fd);
  927. if (!probe_links) {
  928. pthread_mutex_unlock(&lock);
  929. goto endprocess;
  930. }
  931. int algorithms[NETDATA_KEY_PUBLISH_PROCESS_END] = {
  932. NETDATA_EBPF_INCREMENTAL_IDX, NETDATA_EBPF_INCREMENTAL_IDX,NETDATA_EBPF_INCREMENTAL_IDX, //open, close, unlink
  933. NETDATA_EBPF_ABSOLUTE_IDX, NETDATA_EBPF_ABSOLUTE_IDX, NETDATA_EBPF_ABSOLUTE_IDX,
  934. NETDATA_EBPF_ABSOLUTE_IDX, NETDATA_EBPF_ABSOLUTE_IDX, NETDATA_EBPF_ABSOLUTE_IDX
  935. };
  936. ebpf_global_labels(
  937. process_aggregated_data, process_publish_aggregated, process_dimension_names, process_id_names,
  938. algorithms, NETDATA_KEY_PUBLISH_PROCESS_END);
  939. if (process_enabled) {
  940. ebpf_create_global_charts(em);
  941. }
  942. pthread_mutex_unlock(&lock);
  943. process_collector((usec_t)(em->update_time * USEC_PER_SEC), em);
  944. endprocess:
  945. wait_for_all_threads_die();
  946. netdata_thread_cleanup_pop(1);
  947. return NULL;
  948. }