sys_fs_cgroup.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "cgroup-internals.h"
  3. // main cgroups thread worker jobs
  4. #define WORKER_CGROUPS_LOCK 0
  5. #define WORKER_CGROUPS_READ 1
  6. #define WORKER_CGROUPS_CHART 2
  7. // ----------------------------------------------------------------------------
  8. // cgroup globals
  9. unsigned long long host_ram_total = 0;
  10. int is_inside_k8s = 0;
  11. long system_page_size = 4096; // system will be queried via sysconf() in configuration()
  12. int cgroup_enable_cpuacct_stat = CONFIG_BOOLEAN_AUTO;
  13. int cgroup_enable_cpuacct_usage = CONFIG_BOOLEAN_NO;
  14. int cgroup_enable_cpuacct_cpu_throttling = CONFIG_BOOLEAN_YES;
  15. int cgroup_enable_cpuacct_cpu_shares = CONFIG_BOOLEAN_NO;
  16. int cgroup_enable_memory = CONFIG_BOOLEAN_AUTO;
  17. int cgroup_enable_detailed_memory = CONFIG_BOOLEAN_AUTO;
  18. int cgroup_enable_memory_failcnt = CONFIG_BOOLEAN_AUTO;
  19. int cgroup_enable_swap = CONFIG_BOOLEAN_AUTO;
  20. int cgroup_enable_blkio_io = CONFIG_BOOLEAN_AUTO;
  21. int cgroup_enable_blkio_ops = CONFIG_BOOLEAN_AUTO;
  22. int cgroup_enable_blkio_throttle_io = CONFIG_BOOLEAN_AUTO;
  23. int cgroup_enable_blkio_throttle_ops = CONFIG_BOOLEAN_AUTO;
  24. int cgroup_enable_blkio_merged_ops = CONFIG_BOOLEAN_AUTO;
  25. int cgroup_enable_blkio_queued_ops = CONFIG_BOOLEAN_AUTO;
  26. int cgroup_enable_pressure_cpu = CONFIG_BOOLEAN_AUTO;
  27. int cgroup_enable_pressure_io_some = CONFIG_BOOLEAN_AUTO;
  28. int cgroup_enable_pressure_io_full = CONFIG_BOOLEAN_AUTO;
  29. int cgroup_enable_pressure_memory_some = CONFIG_BOOLEAN_AUTO;
  30. int cgroup_enable_pressure_memory_full = CONFIG_BOOLEAN_AUTO;
  31. int cgroup_enable_pressure_irq_some = CONFIG_BOOLEAN_NO;
  32. int cgroup_enable_pressure_irq_full = CONFIG_BOOLEAN_AUTO;
  33. int cgroup_enable_systemd_services = CONFIG_BOOLEAN_YES;
  34. int cgroup_enable_systemd_services_detailed_memory = CONFIG_BOOLEAN_NO;
  35. int cgroup_used_memory = CONFIG_BOOLEAN_YES;
  36. int cgroup_use_unified_cgroups = CONFIG_BOOLEAN_NO;
  37. int cgroup_unified_exist = CONFIG_BOOLEAN_AUTO;
  38. int cgroup_search_in_devices = 1;
  39. int cgroup_check_for_new_every = 10;
  40. int cgroup_update_every = 1;
  41. int cgroup_containers_chart_priority = NETDATA_CHART_PRIO_CGROUPS_CONTAINERS;
  42. int cgroup_recheck_zero_blkio_every_iterations = 10;
  43. int cgroup_recheck_zero_mem_failcnt_every_iterations = 10;
  44. int cgroup_recheck_zero_mem_detailed_every_iterations = 10;
  45. char *cgroup_cpuacct_base = NULL;
  46. char *cgroup_cpuset_base = NULL;
  47. char *cgroup_blkio_base = NULL;
  48. char *cgroup_memory_base = NULL;
  49. char *cgroup_devices_base = NULL;
  50. char *cgroup_pids_base = NULL;
  51. char *cgroup_unified_base = NULL;
  52. int cgroup_root_count = 0;
  53. int cgroup_root_max = 1000;
  54. int cgroup_max_depth = 0;
  55. SIMPLE_PATTERN *enabled_cgroup_paths = NULL;
  56. SIMPLE_PATTERN *enabled_cgroup_names = NULL;
  57. SIMPLE_PATTERN *search_cgroup_paths = NULL;
  58. SIMPLE_PATTERN *enabled_cgroup_renames = NULL;
  59. SIMPLE_PATTERN *systemd_services_cgroups = NULL;
  60. SIMPLE_PATTERN *entrypoint_parent_process_comm = NULL;
  61. char *cgroups_network_interface_script = NULL;
  62. int cgroups_check = 0;
  63. uint32_t Read_hash = 0;
  64. uint32_t Write_hash = 0;
  65. uint32_t user_hash = 0;
  66. uint32_t system_hash = 0;
  67. uint32_t user_usec_hash = 0;
  68. uint32_t system_usec_hash = 0;
  69. uint32_t nr_periods_hash = 0;
  70. uint32_t nr_throttled_hash = 0;
  71. uint32_t throttled_time_hash = 0;
  72. uint32_t throttled_usec_hash = 0;
  73. // *** WARNING *** The fields are not thread safe. Take care of safe usage.
  74. struct cgroup *cgroup_root = NULL;
  75. uv_mutex_t cgroup_root_mutex;
  76. struct cgroups_systemd_config_setting cgroups_systemd_options[] = {
  77. { .name = "legacy", .setting = SYSTEMD_CGROUP_LEGACY },
  78. { .name = "hybrid", .setting = SYSTEMD_CGROUP_HYBRID },
  79. { .name = "unified", .setting = SYSTEMD_CGROUP_UNIFIED },
  80. { .name = NULL, .setting = SYSTEMD_CGROUP_ERR },
  81. };
  82. // Shared memory with information from detected cgroups
  83. netdata_ebpf_cgroup_shm_t shm_cgroup_ebpf = {NULL, NULL};
  84. int shm_fd_cgroup_ebpf = -1;
  85. sem_t *shm_mutex_cgroup_ebpf = SEM_FAILED;
  86. struct discovery_thread discovery_thread;
  87. /* on Fed systemd is not in PATH for some reason */
  88. #define SYSTEMD_CMD_RHEL "/usr/lib/systemd/systemd --version"
  89. #define SYSTEMD_HIERARCHY_STRING "default-hierarchy="
  90. #define MAXSIZE_PROC_CMDLINE 4096
  91. static enum cgroups_systemd_setting cgroups_detect_systemd(const char *exec)
  92. {
  93. pid_t command_pid;
  94. enum cgroups_systemd_setting retval = SYSTEMD_CGROUP_ERR;
  95. char buf[MAXSIZE_PROC_CMDLINE];
  96. char *begin, *end;
  97. FILE *fp_child_input;
  98. FILE *fp_child_output = netdata_popen(exec, &command_pid, &fp_child_input);
  99. if (!fp_child_output)
  100. return retval;
  101. fd_set rfds;
  102. struct timeval timeout;
  103. int fd = fileno(fp_child_output);
  104. int ret = -1;
  105. FD_ZERO(&rfds);
  106. FD_SET(fd, &rfds);
  107. timeout.tv_sec = 3;
  108. timeout.tv_usec = 0;
  109. if (fd != -1) {
  110. ret = select(fd + 1, &rfds, NULL, NULL, &timeout);
  111. }
  112. if (ret == -1) {
  113. collector_error("Failed to get the output of \"%s\"", exec);
  114. } else if (ret == 0) {
  115. collector_info("Cannot get the output of \"%s\" within %"PRId64" seconds", exec, (int64_t)timeout.tv_sec);
  116. } else {
  117. while (fgets(buf, MAXSIZE_PROC_CMDLINE, fp_child_output) != NULL) {
  118. if ((begin = strstr(buf, SYSTEMD_HIERARCHY_STRING))) {
  119. end = begin = begin + strlen(SYSTEMD_HIERARCHY_STRING);
  120. if (!*begin)
  121. break;
  122. while (isalpha(*end))
  123. end++;
  124. *end = 0;
  125. for (int i = 0; cgroups_systemd_options[i].name; i++) {
  126. if (!strcmp(begin, cgroups_systemd_options[i].name)) {
  127. retval = cgroups_systemd_options[i].setting;
  128. break;
  129. }
  130. }
  131. break;
  132. }
  133. }
  134. }
  135. if (netdata_pclose(fp_child_input, fp_child_output, command_pid))
  136. return SYSTEMD_CGROUP_ERR;
  137. return retval;
  138. }
  139. static enum cgroups_type cgroups_try_detect_version()
  140. {
  141. pid_t command_pid;
  142. char buf[MAXSIZE_PROC_CMDLINE];
  143. enum cgroups_systemd_setting systemd_setting;
  144. int cgroups2_available = 0;
  145. // 1. check if cgroups2 available on system at all
  146. FILE *fp_child_input;
  147. FILE *fp_child_output = netdata_popen("grep cgroup /proc/filesystems", &command_pid, &fp_child_input);
  148. if (!fp_child_output) {
  149. collector_error("popen failed");
  150. return CGROUPS_AUTODETECT_FAIL;
  151. }
  152. while (fgets(buf, MAXSIZE_PROC_CMDLINE, fp_child_output) != NULL) {
  153. if (strstr(buf, "cgroup2")) {
  154. cgroups2_available = 1;
  155. break;
  156. }
  157. }
  158. if(netdata_pclose(fp_child_input, fp_child_output, command_pid))
  159. return CGROUPS_AUTODETECT_FAIL;
  160. if(!cgroups2_available)
  161. return CGROUPS_V1;
  162. #if defined CGROUP2_SUPER_MAGIC
  163. // 2. check filesystem type for the default mountpoint
  164. char filename[FILENAME_MAX + 1];
  165. snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/fs/cgroup");
  166. struct statfs fsinfo;
  167. if (!statfs(filename, &fsinfo)) {
  168. if (fsinfo.f_type == CGROUP2_SUPER_MAGIC)
  169. return CGROUPS_V2;
  170. }
  171. #endif
  172. // 3. check systemd compiletime setting
  173. if ((systemd_setting = cgroups_detect_systemd("systemd --version")) == SYSTEMD_CGROUP_ERR)
  174. systemd_setting = cgroups_detect_systemd(SYSTEMD_CMD_RHEL);
  175. if(systemd_setting == SYSTEMD_CGROUP_ERR)
  176. return CGROUPS_AUTODETECT_FAIL;
  177. if(systemd_setting == SYSTEMD_CGROUP_LEGACY || systemd_setting == SYSTEMD_CGROUP_HYBRID) {
  178. // currently we prefer V1 if HYBRID is set as it seems to be more feature complete
  179. // in the future we might want to continue here if SYSTEMD_CGROUP_HYBRID
  180. // and go ahead with V2
  181. return CGROUPS_V1;
  182. }
  183. // 4. if we are unified as on Fedora (default cgroups2 only mode)
  184. // check kernel command line flag that can override that setting
  185. FILE *fp = fopen("/proc/cmdline", "r");
  186. if (!fp) {
  187. collector_error("Error reading kernel boot commandline parameters");
  188. return CGROUPS_AUTODETECT_FAIL;
  189. }
  190. if (!fgets(buf, MAXSIZE_PROC_CMDLINE, fp)) {
  191. collector_error("couldn't read all cmdline params into buffer");
  192. fclose(fp);
  193. return CGROUPS_AUTODETECT_FAIL;
  194. }
  195. fclose(fp);
  196. if (strstr(buf, "systemd.unified_cgroup_hierarchy=0")) {
  197. collector_info("cgroups v2 (unified cgroups) is available but are disabled on this system.");
  198. return CGROUPS_V1;
  199. }
  200. return CGROUPS_V2;
  201. }
  202. void set_cgroup_base_path(char *filename, char *path) {
  203. if (strncmp(netdata_configured_host_prefix, path, strlen(netdata_configured_host_prefix)) == 0) {
  204. snprintfz(filename, FILENAME_MAX, "%s", path);
  205. } else {
  206. snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, path);
  207. }
  208. }
  209. void read_cgroup_plugin_configuration() {
  210. system_page_size = sysconf(_SC_PAGESIZE);
  211. Read_hash = simple_hash("Read");
  212. Write_hash = simple_hash("Write");
  213. user_hash = simple_hash("user");
  214. system_hash = simple_hash("system");
  215. user_usec_hash = simple_hash("user_usec");
  216. system_usec_hash = simple_hash("system_usec");
  217. nr_periods_hash = simple_hash("nr_periods");
  218. nr_throttled_hash = simple_hash("nr_throttled");
  219. throttled_time_hash = simple_hash("throttled_time");
  220. throttled_usec_hash = simple_hash("throttled_usec");
  221. cgroup_update_every = (int)config_get_number("plugin:cgroups", "update every", localhost->rrd_update_every);
  222. if(cgroup_update_every < localhost->rrd_update_every)
  223. cgroup_update_every = localhost->rrd_update_every;
  224. cgroup_check_for_new_every = (int)config_get_number("plugin:cgroups", "check for new cgroups every", (long long)cgroup_check_for_new_every * (long long)cgroup_update_every);
  225. if(cgroup_check_for_new_every < cgroup_update_every)
  226. cgroup_check_for_new_every = cgroup_update_every;
  227. cgroup_use_unified_cgroups = config_get_boolean_ondemand("plugin:cgroups", "use unified cgroups", CONFIG_BOOLEAN_AUTO);
  228. if(cgroup_use_unified_cgroups == CONFIG_BOOLEAN_AUTO)
  229. cgroup_use_unified_cgroups = (cgroups_try_detect_version() == CGROUPS_V2);
  230. collector_info("use unified cgroups %s", cgroup_use_unified_cgroups ? "true" : "false");
  231. cgroup_containers_chart_priority = (int)config_get_number("plugin:cgroups", "containers priority", cgroup_containers_chart_priority);
  232. if(cgroup_containers_chart_priority < 1)
  233. cgroup_containers_chart_priority = NETDATA_CHART_PRIO_CGROUPS_CONTAINERS;
  234. cgroup_enable_cpuacct_stat = config_get_boolean_ondemand("plugin:cgroups", "enable cpuacct stat (total CPU)", cgroup_enable_cpuacct_stat);
  235. cgroup_enable_cpuacct_usage = config_get_boolean_ondemand("plugin:cgroups", "enable cpuacct usage (per core CPU)", cgroup_enable_cpuacct_usage);
  236. cgroup_enable_cpuacct_cpu_throttling = config_get_boolean_ondemand("plugin:cgroups", "enable cpuacct cpu throttling", cgroup_enable_cpuacct_cpu_throttling);
  237. cgroup_enable_cpuacct_cpu_shares = config_get_boolean_ondemand("plugin:cgroups", "enable cpuacct cpu shares", cgroup_enable_cpuacct_cpu_shares);
  238. cgroup_enable_memory = config_get_boolean_ondemand("plugin:cgroups", "enable memory", cgroup_enable_memory);
  239. cgroup_enable_detailed_memory = config_get_boolean_ondemand("plugin:cgroups", "enable detailed memory", cgroup_enable_detailed_memory);
  240. cgroup_enable_memory_failcnt = config_get_boolean_ondemand("plugin:cgroups", "enable memory limits fail count", cgroup_enable_memory_failcnt);
  241. cgroup_enable_swap = config_get_boolean_ondemand("plugin:cgroups", "enable swap memory", cgroup_enable_swap);
  242. cgroup_enable_blkio_io = config_get_boolean_ondemand("plugin:cgroups", "enable blkio bandwidth", cgroup_enable_blkio_io);
  243. cgroup_enable_blkio_ops = config_get_boolean_ondemand("plugin:cgroups", "enable blkio operations", cgroup_enable_blkio_ops);
  244. cgroup_enable_blkio_throttle_io = config_get_boolean_ondemand("plugin:cgroups", "enable blkio throttle bandwidth", cgroup_enable_blkio_throttle_io);
  245. cgroup_enable_blkio_throttle_ops = config_get_boolean_ondemand("plugin:cgroups", "enable blkio throttle operations", cgroup_enable_blkio_throttle_ops);
  246. cgroup_enable_blkio_queued_ops = config_get_boolean_ondemand("plugin:cgroups", "enable blkio queued operations", cgroup_enable_blkio_queued_ops);
  247. cgroup_enable_blkio_merged_ops = config_get_boolean_ondemand("plugin:cgroups", "enable blkio merged operations", cgroup_enable_blkio_merged_ops);
  248. cgroup_enable_pressure_cpu = config_get_boolean_ondemand("plugin:cgroups", "enable cpu pressure", cgroup_enable_pressure_cpu);
  249. cgroup_enable_pressure_io_some = config_get_boolean_ondemand("plugin:cgroups", "enable io some pressure", cgroup_enable_pressure_io_some);
  250. cgroup_enable_pressure_io_full = config_get_boolean_ondemand("plugin:cgroups", "enable io full pressure", cgroup_enable_pressure_io_full);
  251. cgroup_enable_pressure_memory_some = config_get_boolean_ondemand("plugin:cgroups", "enable memory some pressure", cgroup_enable_pressure_memory_some);
  252. cgroup_enable_pressure_memory_full = config_get_boolean_ondemand("plugin:cgroups", "enable memory full pressure", cgroup_enable_pressure_memory_full);
  253. cgroup_recheck_zero_blkio_every_iterations = (int)config_get_number("plugin:cgroups", "recheck zero blkio every iterations", cgroup_recheck_zero_blkio_every_iterations);
  254. cgroup_recheck_zero_mem_failcnt_every_iterations = (int)config_get_number("plugin:cgroups", "recheck zero memory failcnt every iterations", cgroup_recheck_zero_mem_failcnt_every_iterations);
  255. cgroup_recheck_zero_mem_detailed_every_iterations = (int)config_get_number("plugin:cgroups", "recheck zero detailed memory every iterations", cgroup_recheck_zero_mem_detailed_every_iterations);
  256. cgroup_enable_systemd_services = config_get_boolean("plugin:cgroups", "enable systemd services", cgroup_enable_systemd_services);
  257. cgroup_enable_systemd_services_detailed_memory = config_get_boolean("plugin:cgroups", "enable systemd services detailed memory", cgroup_enable_systemd_services_detailed_memory);
  258. cgroup_used_memory = config_get_boolean("plugin:cgroups", "report used memory", cgroup_used_memory);
  259. char filename[FILENAME_MAX + 1], *s;
  260. struct mountinfo *mi, *root = mountinfo_read(0);
  261. if(!cgroup_use_unified_cgroups) {
  262. // cgroup v1 does not have pressure metrics
  263. cgroup_enable_pressure_cpu =
  264. cgroup_enable_pressure_io_some =
  265. cgroup_enable_pressure_io_full =
  266. cgroup_enable_pressure_memory_some =
  267. cgroup_enable_pressure_memory_full = CONFIG_BOOLEAN_NO;
  268. mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "cpuacct");
  269. if (!mi)
  270. mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "cpuacct");
  271. if (!mi) {
  272. collector_error("CGROUP: cannot find cpuacct mountinfo. Assuming default: /sys/fs/cgroup/cpuacct");
  273. s = "/sys/fs/cgroup/cpuacct";
  274. } else
  275. s = mi->mount_point;
  276. set_cgroup_base_path(filename, s);
  277. cgroup_cpuacct_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/cpuacct", filename);
  278. mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "cpuset");
  279. if (!mi)
  280. mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "cpuset");
  281. if (!mi) {
  282. collector_error("CGROUP: cannot find cpuset mountinfo. Assuming default: /sys/fs/cgroup/cpuset");
  283. s = "/sys/fs/cgroup/cpuset";
  284. } else
  285. s = mi->mount_point;
  286. set_cgroup_base_path(filename, s);
  287. cgroup_cpuset_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/cpuset", filename);
  288. mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "blkio");
  289. if (!mi)
  290. mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "blkio");
  291. if (!mi) {
  292. collector_error("CGROUP: cannot find blkio mountinfo. Assuming default: /sys/fs/cgroup/blkio");
  293. s = "/sys/fs/cgroup/blkio";
  294. } else
  295. s = mi->mount_point;
  296. set_cgroup_base_path(filename, s);
  297. cgroup_blkio_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/blkio", filename);
  298. mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "memory");
  299. if (!mi)
  300. mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "memory");
  301. if (!mi) {
  302. collector_error("CGROUP: cannot find memory mountinfo. Assuming default: /sys/fs/cgroup/memory");
  303. s = "/sys/fs/cgroup/memory";
  304. } else
  305. s = mi->mount_point;
  306. set_cgroup_base_path(filename, s);
  307. cgroup_memory_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/memory", filename);
  308. mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "devices");
  309. if (!mi)
  310. mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "devices");
  311. if (!mi) {
  312. collector_error("CGROUP: cannot find devices mountinfo. Assuming default: /sys/fs/cgroup/devices");
  313. s = "/sys/fs/cgroup/devices";
  314. } else
  315. s = mi->mount_point;
  316. set_cgroup_base_path(filename, s);
  317. cgroup_devices_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/devices", filename);
  318. mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "pids");
  319. if (!mi)
  320. mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "pids");
  321. if (!mi) {
  322. collector_error("CGROUP: cannot find pids mountinfo. Assuming default: /sys/fs/cgroup/pids");
  323. s = "/sys/fs/cgroup/pids";
  324. } else
  325. s = mi->mount_point;
  326. set_cgroup_base_path(filename, s);
  327. cgroup_pids_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/pids", filename);
  328. }
  329. else {
  330. //cgroup_enable_cpuacct_stat =
  331. cgroup_enable_cpuacct_usage =
  332. //cgroup_enable_memory =
  333. //cgroup_enable_detailed_memory =
  334. cgroup_enable_memory_failcnt =
  335. //cgroup_enable_swap =
  336. //cgroup_enable_blkio_io =
  337. //cgroup_enable_blkio_ops =
  338. cgroup_enable_blkio_throttle_io =
  339. cgroup_enable_blkio_throttle_ops =
  340. cgroup_enable_blkio_merged_ops =
  341. cgroup_enable_blkio_queued_ops = CONFIG_BOOLEAN_NO;
  342. cgroup_search_in_devices = 0;
  343. cgroup_enable_systemd_services_detailed_memory = CONFIG_BOOLEAN_NO;
  344. cgroup_used_memory = CONFIG_BOOLEAN_NO; //unified cgroups use different values
  345. //TODO: can there be more than 1 cgroup2 mount point?
  346. //there is no cgroup2 specific super option - for now use 'rw' option
  347. mi = mountinfo_find_by_filesystem_super_option(root, "cgroup2", "rw");
  348. if (!mi) {
  349. mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup2", "cgroup");
  350. }
  351. if (!mi) {
  352. collector_error("CGROUP: cannot find cgroup2 mountinfo. Assuming default: /sys/fs/cgroup");
  353. s = "/sys/fs/cgroup";
  354. } else
  355. s = mi->mount_point;
  356. set_cgroup_base_path(filename, s);
  357. cgroup_unified_base = config_get("plugin:cgroups", "path to unified cgroups", filename);
  358. }
  359. cgroup_root_max = (int)config_get_number("plugin:cgroups", "max cgroups to allow", cgroup_root_max);
  360. cgroup_max_depth = (int)config_get_number("plugin:cgroups", "max cgroups depth to monitor", cgroup_max_depth);
  361. enabled_cgroup_paths = simple_pattern_create(
  362. config_get("plugin:cgroups", "enable by default cgroups matching",
  363. // ----------------------------------------------------------------
  364. " !*/init.scope " // ignore init.scope
  365. " !/system.slice/run-*.scope " // ignore system.slice/run-XXXX.scope
  366. " *.scope " // we need all other *.scope for sure
  367. // ----------------------------------------------------------------
  368. " /machine.slice/*.service " // #3367 systemd-nspawn
  369. // ----------------------------------------------------------------
  370. " */kubepods/pod*/* " // k8s containers
  371. " */kubepods/*/pod*/* " // k8s containers
  372. " */*-kubepods-pod*/* " // k8s containers
  373. " */*-kubepods-*-pod*/* " // k8s containers
  374. " !*kubepods* !*kubelet* " // all other k8s cgroups
  375. // ----------------------------------------------------------------
  376. " !*/vcpu* " // libvirtd adds these sub-cgroups
  377. " !*/emulator " // libvirtd adds these sub-cgroups
  378. " !*.mount "
  379. " !*.partition "
  380. " !*.service "
  381. " !*.service/udev "
  382. " !*.socket "
  383. " !*.slice "
  384. " !*.swap "
  385. " !*.user "
  386. " !/ "
  387. " !/docker "
  388. " !*/libvirt "
  389. " !/lxc "
  390. " !/lxc/*/* " // #1397 #2649
  391. " !/lxc.monitor* "
  392. " !/lxc.pivot "
  393. " !/lxc.payload "
  394. " !/machine "
  395. " !/qemu "
  396. " !/system "
  397. " !/systemd "
  398. " !/user "
  399. " * " // enable anything else
  400. ), NULL, SIMPLE_PATTERN_EXACT, true);
  401. enabled_cgroup_names = simple_pattern_create(
  402. config_get("plugin:cgroups", "enable by default cgroups names matching",
  403. " * "
  404. ), NULL, SIMPLE_PATTERN_EXACT, true);
  405. search_cgroup_paths = simple_pattern_create(
  406. config_get("plugin:cgroups", "search for cgroups in subpaths matching",
  407. " !*/init.scope " // ignore init.scope
  408. " !*-qemu " // #345
  409. " !*.libvirt-qemu " // #3010
  410. " !/init.scope "
  411. " !/system "
  412. " !/systemd "
  413. " !/user "
  414. " !/user.slice "
  415. " !/lxc/*/* " // #2161 #2649
  416. " !/lxc.monitor "
  417. " !/lxc.payload/*/* "
  418. " !/lxc.payload.* "
  419. " * "
  420. ), NULL, SIMPLE_PATTERN_EXACT, true);
  421. snprintfz(filename, FILENAME_MAX, "%s/cgroup-name.sh", netdata_configured_primary_plugins_dir);
  422. cgroups_rename_script = config_get("plugin:cgroups", "script to get cgroup names", filename);
  423. snprintfz(filename, FILENAME_MAX, "%s/cgroup-network", netdata_configured_primary_plugins_dir);
  424. cgroups_network_interface_script = config_get("plugin:cgroups", "script to get cgroup network interfaces", filename);
  425. enabled_cgroup_renames = simple_pattern_create(
  426. config_get("plugin:cgroups", "run script to rename cgroups matching",
  427. " !/ "
  428. " !*.mount "
  429. " !*.socket "
  430. " !*.partition "
  431. " /machine.slice/*.service " // #3367 systemd-nspawn
  432. " !*.service "
  433. " !*.slice "
  434. " !*.swap "
  435. " !*.user "
  436. " !init.scope "
  437. " !*.scope/vcpu* " // libvirtd adds these sub-cgroups
  438. " !*.scope/emulator " // libvirtd adds these sub-cgroups
  439. " *.scope "
  440. " *docker* "
  441. " *lxc* "
  442. " *qemu* "
  443. " */kubepods/pod*/* " // k8s containers
  444. " */kubepods/*/pod*/* " // k8s containers
  445. " */*-kubepods-pod*/* " // k8s containers
  446. " */*-kubepods-*-pod*/* " // k8s containers
  447. " !*kubepods* !*kubelet* " // all other k8s cgroups
  448. " *.libvirt-qemu " // #3010
  449. " * "
  450. ), NULL, SIMPLE_PATTERN_EXACT, true);
  451. if(cgroup_enable_systemd_services) {
  452. systemd_services_cgroups = simple_pattern_create(
  453. config_get("plugin:cgroups", "cgroups to match as systemd services",
  454. " !/system.slice/*/*.service "
  455. " /system.slice/*.service "
  456. ), NULL, SIMPLE_PATTERN_EXACT, true);
  457. }
  458. mountinfo_free_all(root);
  459. }
  460. void netdata_cgroup_ebpf_set_values(size_t length)
  461. {
  462. sem_wait(shm_mutex_cgroup_ebpf);
  463. shm_cgroup_ebpf.header->cgroup_max = cgroup_root_max;
  464. shm_cgroup_ebpf.header->systemd_enabled = cgroup_enable_systemd_services |
  465. cgroup_enable_systemd_services_detailed_memory |
  466. cgroup_used_memory;
  467. shm_cgroup_ebpf.header->body_length = length;
  468. sem_post(shm_mutex_cgroup_ebpf);
  469. }
  470. void netdata_cgroup_ebpf_initialize_shm()
  471. {
  472. shm_fd_cgroup_ebpf = shm_open(NETDATA_SHARED_MEMORY_EBPF_CGROUP_NAME, O_CREAT | O_RDWR, 0660);
  473. if (shm_fd_cgroup_ebpf < 0) {
  474. collector_error("Cannot initialize shared memory used by cgroup and eBPF, integration won't happen.");
  475. return;
  476. }
  477. size_t length = sizeof(netdata_ebpf_cgroup_shm_header_t) + cgroup_root_max * sizeof(netdata_ebpf_cgroup_shm_body_t);
  478. if (ftruncate(shm_fd_cgroup_ebpf, length)) {
  479. collector_error("Cannot set size for shared memory.");
  480. goto end_init_shm;
  481. }
  482. shm_cgroup_ebpf.header = (netdata_ebpf_cgroup_shm_header_t *) mmap(NULL, length,
  483. PROT_READ | PROT_WRITE, MAP_SHARED,
  484. shm_fd_cgroup_ebpf, 0);
  485. if (unlikely(MAP_FAILED == shm_cgroup_ebpf.header)) {
  486. shm_cgroup_ebpf.header = NULL;
  487. collector_error("Cannot map shared memory used between cgroup and eBPF, integration won't happen");
  488. goto end_init_shm;
  489. }
  490. shm_cgroup_ebpf.body = (netdata_ebpf_cgroup_shm_body_t *) ((char *)shm_cgroup_ebpf.header +
  491. sizeof(netdata_ebpf_cgroup_shm_header_t));
  492. shm_mutex_cgroup_ebpf = sem_open(NETDATA_NAMED_SEMAPHORE_EBPF_CGROUP_NAME, O_CREAT,
  493. S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, 1);
  494. if (shm_mutex_cgroup_ebpf != SEM_FAILED) {
  495. netdata_cgroup_ebpf_set_values(length);
  496. return;
  497. }
  498. collector_error("Cannot create semaphore, integration between eBPF and cgroup won't happen");
  499. munmap(shm_cgroup_ebpf.header, length);
  500. shm_cgroup_ebpf.header = NULL;
  501. end_init_shm:
  502. close(shm_fd_cgroup_ebpf);
  503. shm_fd_cgroup_ebpf = -1;
  504. shm_unlink(NETDATA_SHARED_MEMORY_EBPF_CGROUP_NAME);
  505. }
  506. // ---------------------------------------------------------------------------------------------
  507. static unsigned long long calc_delta(unsigned long long curr, unsigned long long prev) {
  508. if (prev > curr) {
  509. return 0;
  510. }
  511. return curr - prev;
  512. }
  513. static unsigned long long calc_percentage(unsigned long long value, unsigned long long total) {
  514. if (total == 0) {
  515. return 0;
  516. }
  517. return (unsigned long long)((NETDATA_DOUBLE)value / (NETDATA_DOUBLE)total * 100);
  518. }
  519. // ----------------------------------------------------------------------------
  520. // read values from /sys
  521. static inline void cgroup_read_cpuacct_stat(struct cpuacct_stat *cp) {
  522. static procfile *ff = NULL;
  523. if(likely(cp->filename)) {
  524. ff = procfile_reopen(ff, cp->filename, NULL, CGROUP_PROCFILE_FLAG);
  525. if(unlikely(!ff)) {
  526. cp->updated = 0;
  527. cgroups_check = 1;
  528. return;
  529. }
  530. ff = procfile_readall(ff);
  531. if(unlikely(!ff)) {
  532. cp->updated = 0;
  533. cgroups_check = 1;
  534. return;
  535. }
  536. unsigned long i, lines = procfile_lines(ff);
  537. if(unlikely(lines < 1)) {
  538. collector_error("CGROUP: file '%s' should have 1+ lines.", cp->filename);
  539. cp->updated = 0;
  540. return;
  541. }
  542. for(i = 0; i < lines ; i++) {
  543. char *s = procfile_lineword(ff, i, 0);
  544. uint32_t hash = simple_hash(s);
  545. if(unlikely(hash == user_hash && !strcmp(s, "user")))
  546. cp->user = str2ull(procfile_lineword(ff, i, 1), NULL);
  547. else if(unlikely(hash == system_hash && !strcmp(s, "system")))
  548. cp->system = str2ull(procfile_lineword(ff, i, 1), NULL);
  549. }
  550. cp->updated = 1;
  551. if(unlikely(cp->enabled == CONFIG_BOOLEAN_AUTO &&
  552. (cp->user || cp->system || netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES)))
  553. cp->enabled = CONFIG_BOOLEAN_YES;
  554. }
  555. }
  556. static inline void cgroup_read_cpuacct_cpu_stat(struct cpuacct_cpu_throttling *cp) {
  557. if (unlikely(!cp->filename)) {
  558. return;
  559. }
  560. static procfile *ff = NULL;
  561. ff = procfile_reopen(ff, cp->filename, NULL, CGROUP_PROCFILE_FLAG);
  562. if (unlikely(!ff)) {
  563. cp->updated = 0;
  564. cgroups_check = 1;
  565. return;
  566. }
  567. ff = procfile_readall(ff);
  568. if (unlikely(!ff)) {
  569. cp->updated = 0;
  570. cgroups_check = 1;
  571. return;
  572. }
  573. unsigned long lines = procfile_lines(ff);
  574. if (unlikely(lines < 3)) {
  575. collector_error("CGROUP: file '%s' should have 3 lines.", cp->filename);
  576. cp->updated = 0;
  577. return;
  578. }
  579. unsigned long long nr_periods_last = cp->nr_periods;
  580. unsigned long long nr_throttled_last = cp->nr_throttled;
  581. for (unsigned long i = 0; i < lines; i++) {
  582. char *s = procfile_lineword(ff, i, 0);
  583. uint32_t hash = simple_hash(s);
  584. if (unlikely(hash == nr_periods_hash && !strcmp(s, "nr_periods"))) {
  585. cp->nr_periods = str2ull(procfile_lineword(ff, i, 1), NULL);
  586. } else if (unlikely(hash == nr_throttled_hash && !strcmp(s, "nr_throttled"))) {
  587. cp->nr_throttled = str2ull(procfile_lineword(ff, i, 1), NULL);
  588. } else if (unlikely(hash == throttled_time_hash && !strcmp(s, "throttled_time"))) {
  589. cp->throttled_time = str2ull(procfile_lineword(ff, i, 1), NULL);
  590. }
  591. }
  592. cp->nr_throttled_perc =
  593. calc_percentage(calc_delta(cp->nr_throttled, nr_throttled_last), calc_delta(cp->nr_periods, nr_periods_last));
  594. cp->updated = 1;
  595. if (unlikely(cp->enabled == CONFIG_BOOLEAN_AUTO)) {
  596. if (likely(
  597. cp->nr_periods || cp->nr_throttled || cp->throttled_time ||
  598. netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES)) {
  599. cp->enabled = CONFIG_BOOLEAN_YES;
  600. }
  601. }
  602. }
  603. static inline void cgroup2_read_cpuacct_cpu_stat(struct cpuacct_stat *cp, struct cpuacct_cpu_throttling *cpt) {
  604. static procfile *ff = NULL;
  605. if (unlikely(!cp->filename)) {
  606. return;
  607. }
  608. ff = procfile_reopen(ff, cp->filename, NULL, CGROUP_PROCFILE_FLAG);
  609. if (unlikely(!ff)) {
  610. cp->updated = 0;
  611. cgroups_check = 1;
  612. return;
  613. }
  614. ff = procfile_readall(ff);
  615. if (unlikely(!ff)) {
  616. cp->updated = 0;
  617. cgroups_check = 1;
  618. return;
  619. }
  620. unsigned long lines = procfile_lines(ff);
  621. if (unlikely(lines < 3)) {
  622. collector_error("CGROUP: file '%s' should have at least 3 lines.", cp->filename);
  623. cp->updated = 0;
  624. return;
  625. }
  626. unsigned long long nr_periods_last = cpt->nr_periods;
  627. unsigned long long nr_throttled_last = cpt->nr_throttled;
  628. for (unsigned long i = 0; i < lines; i++) {
  629. char *s = procfile_lineword(ff, i, 0);
  630. uint32_t hash = simple_hash(s);
  631. if (unlikely(hash == user_usec_hash && !strcmp(s, "user_usec"))) {
  632. cp->user = str2ull(procfile_lineword(ff, i, 1), NULL);
  633. } else if (unlikely(hash == system_usec_hash && !strcmp(s, "system_usec"))) {
  634. cp->system = str2ull(procfile_lineword(ff, i, 1), NULL);
  635. } else if (unlikely(hash == nr_periods_hash && !strcmp(s, "nr_periods"))) {
  636. cpt->nr_periods = str2ull(procfile_lineword(ff, i, 1), NULL);
  637. } else if (unlikely(hash == nr_throttled_hash && !strcmp(s, "nr_throttled"))) {
  638. cpt->nr_throttled = str2ull(procfile_lineword(ff, i, 1), NULL);
  639. } else if (unlikely(hash == throttled_usec_hash && !strcmp(s, "throttled_usec"))) {
  640. cpt->throttled_time = str2ull(procfile_lineword(ff, i, 1), NULL) * 1000; // usec -> ns
  641. }
  642. }
  643. cpt->nr_throttled_perc =
  644. calc_percentage(calc_delta(cpt->nr_throttled, nr_throttled_last), calc_delta(cpt->nr_periods, nr_periods_last));
  645. cp->updated = 1;
  646. cpt->updated = 1;
  647. if (unlikely(cp->enabled == CONFIG_BOOLEAN_AUTO)) {
  648. if (likely(cp->user || cp->system || netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES)) {
  649. cp->enabled = CONFIG_BOOLEAN_YES;
  650. }
  651. }
  652. if (unlikely(cpt->enabled == CONFIG_BOOLEAN_AUTO)) {
  653. if (likely(
  654. cpt->nr_periods || cpt->nr_throttled || cpt->throttled_time ||
  655. netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES)) {
  656. cpt->enabled = CONFIG_BOOLEAN_YES;
  657. }
  658. }
  659. }
  660. static inline void cgroup_read_cpuacct_cpu_shares(struct cpuacct_cpu_shares *cp) {
  661. if (unlikely(!cp->filename)) {
  662. return;
  663. }
  664. if (unlikely(read_single_number_file(cp->filename, &cp->shares))) {
  665. cp->updated = 0;
  666. cgroups_check = 1;
  667. return;
  668. }
  669. cp->updated = 1;
  670. if (unlikely((cp->enabled == CONFIG_BOOLEAN_AUTO)) &&
  671. (cp->shares || netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES)) {
  672. cp->enabled = CONFIG_BOOLEAN_YES;
  673. }
  674. }
  675. static inline void cgroup_read_cpuacct_usage(struct cpuacct_usage *ca) {
  676. static procfile *ff = NULL;
  677. if(likely(ca->filename)) {
  678. ff = procfile_reopen(ff, ca->filename, NULL, CGROUP_PROCFILE_FLAG);
  679. if(unlikely(!ff)) {
  680. ca->updated = 0;
  681. cgroups_check = 1;
  682. return;
  683. }
  684. ff = procfile_readall(ff);
  685. if(unlikely(!ff)) {
  686. ca->updated = 0;
  687. cgroups_check = 1;
  688. return;
  689. }
  690. if(unlikely(procfile_lines(ff) < 1)) {
  691. collector_error("CGROUP: file '%s' should have 1+ lines but has %zu.", ca->filename, procfile_lines(ff));
  692. ca->updated = 0;
  693. return;
  694. }
  695. unsigned long i = procfile_linewords(ff, 0);
  696. if(unlikely(i == 0)) {
  697. ca->updated = 0;
  698. return;
  699. }
  700. // we may have 1 more CPU reported
  701. while(i > 0) {
  702. char *s = procfile_lineword(ff, 0, i - 1);
  703. if(!*s) i--;
  704. else break;
  705. }
  706. if(unlikely(i != ca->cpus)) {
  707. freez(ca->cpu_percpu);
  708. ca->cpu_percpu = mallocz(sizeof(unsigned long long) * i);
  709. ca->cpus = (unsigned int)i;
  710. }
  711. unsigned long long total = 0;
  712. for(i = 0; i < ca->cpus ;i++) {
  713. unsigned long long n = str2ull(procfile_lineword(ff, 0, i), NULL);
  714. ca->cpu_percpu[i] = n;
  715. total += n;
  716. }
  717. ca->updated = 1;
  718. if(unlikely(ca->enabled == CONFIG_BOOLEAN_AUTO &&
  719. (total || netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES)))
  720. ca->enabled = CONFIG_BOOLEAN_YES;
  721. }
  722. }
  723. static inline void cgroup_read_blkio(struct blkio *io) {
  724. if(unlikely(io->enabled == CONFIG_BOOLEAN_AUTO && io->delay_counter > 0)) {
  725. io->delay_counter--;
  726. return;
  727. }
  728. if(likely(io->filename)) {
  729. static procfile *ff = NULL;
  730. ff = procfile_reopen(ff, io->filename, NULL, CGROUP_PROCFILE_FLAG);
  731. if(unlikely(!ff)) {
  732. io->updated = 0;
  733. cgroups_check = 1;
  734. return;
  735. }
  736. ff = procfile_readall(ff);
  737. if(unlikely(!ff)) {
  738. io->updated = 0;
  739. cgroups_check = 1;
  740. return;
  741. }
  742. unsigned long i, lines = procfile_lines(ff);
  743. if(unlikely(lines < 1)) {
  744. collector_error("CGROUP: file '%s' should have 1+ lines.", io->filename);
  745. io->updated = 0;
  746. return;
  747. }
  748. io->Read = 0;
  749. io->Write = 0;
  750. /*
  751. io->Sync = 0;
  752. io->Async = 0;
  753. io->Total = 0;
  754. */
  755. for(i = 0; i < lines ; i++) {
  756. char *s = procfile_lineword(ff, i, 1);
  757. uint32_t hash = simple_hash(s);
  758. if(unlikely(hash == Read_hash && !strcmp(s, "Read")))
  759. io->Read += str2ull(procfile_lineword(ff, i, 2), NULL);
  760. else if(unlikely(hash == Write_hash && !strcmp(s, "Write")))
  761. io->Write += str2ull(procfile_lineword(ff, i, 2), NULL);
  762. /*
  763. else if(unlikely(hash == Sync_hash && !strcmp(s, "Sync")))
  764. io->Sync += str2ull(procfile_lineword(ff, i, 2));
  765. else if(unlikely(hash == Async_hash && !strcmp(s, "Async")))
  766. io->Async += str2ull(procfile_lineword(ff, i, 2));
  767. else if(unlikely(hash == Total_hash && !strcmp(s, "Total")))
  768. io->Total += str2ull(procfile_lineword(ff, i, 2));
  769. */
  770. }
  771. io->updated = 1;
  772. if(unlikely(io->enabled == CONFIG_BOOLEAN_AUTO)) {
  773. if(unlikely(io->Read || io->Write || netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))
  774. io->enabled = CONFIG_BOOLEAN_YES;
  775. else
  776. io->delay_counter = cgroup_recheck_zero_blkio_every_iterations;
  777. }
  778. }
  779. }
  780. static inline void cgroup2_read_blkio(struct blkio *io, unsigned int word_offset) {
  781. if(unlikely(io->enabled == CONFIG_BOOLEAN_AUTO && io->delay_counter > 0)) {
  782. io->delay_counter--;
  783. return;
  784. }
  785. if(likely(io->filename)) {
  786. static procfile *ff = NULL;
  787. ff = procfile_reopen(ff, io->filename, NULL, CGROUP_PROCFILE_FLAG);
  788. if(unlikely(!ff)) {
  789. io->updated = 0;
  790. cgroups_check = 1;
  791. return;
  792. }
  793. ff = procfile_readall(ff);
  794. if(unlikely(!ff)) {
  795. io->updated = 0;
  796. cgroups_check = 1;
  797. return;
  798. }
  799. unsigned long i, lines = procfile_lines(ff);
  800. if (unlikely(lines < 1)) {
  801. collector_error("CGROUP: file '%s' should have 1+ lines.", io->filename);
  802. io->updated = 0;
  803. return;
  804. }
  805. io->Read = 0;
  806. io->Write = 0;
  807. for (i = 0; i < lines; i++) {
  808. io->Read += str2ull(procfile_lineword(ff, i, 2 + word_offset), NULL);
  809. io->Write += str2ull(procfile_lineword(ff, i, 4 + word_offset), NULL);
  810. }
  811. io->updated = 1;
  812. if(unlikely(io->enabled == CONFIG_BOOLEAN_AUTO)) {
  813. if(unlikely(io->Read || io->Write || netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))
  814. io->enabled = CONFIG_BOOLEAN_YES;
  815. else
  816. io->delay_counter = cgroup_recheck_zero_blkio_every_iterations;
  817. }
  818. }
  819. }
  820. static inline void cgroup2_read_pressure(struct pressure *res) {
  821. static procfile *ff = NULL;
  822. if (likely(res->filename)) {
  823. ff = procfile_reopen(ff, res->filename, " =", CGROUP_PROCFILE_FLAG);
  824. if (unlikely(!ff)) {
  825. res->updated = 0;
  826. cgroups_check = 1;
  827. return;
  828. }
  829. ff = procfile_readall(ff);
  830. if (unlikely(!ff)) {
  831. res->updated = 0;
  832. cgroups_check = 1;
  833. return;
  834. }
  835. size_t lines = procfile_lines(ff);
  836. if (lines < 1) {
  837. collector_error("CGROUP: file '%s' should have 1+ lines.", res->filename);
  838. res->updated = 0;
  839. return;
  840. }
  841. bool did_some = false, did_full = false;
  842. for(size_t l = 0; l < lines ;l++) {
  843. const char *key = procfile_lineword(ff, l, 0);
  844. if(strcmp(key, "some") == 0) {
  845. res->some.share_time.value10 = strtod(procfile_lineword(ff, l, 2), NULL);
  846. res->some.share_time.value60 = strtod(procfile_lineword(ff, l, 4), NULL);
  847. res->some.share_time.value300 = strtod(procfile_lineword(ff, l, 6), NULL);
  848. res->some.total_time.value_total = str2ull(procfile_lineword(ff, l, 8), NULL) / 1000; // us->ms
  849. did_some = true;
  850. }
  851. else if(strcmp(key, "full") == 0) {
  852. res->full.share_time.value10 = strtod(procfile_lineword(ff, l, 2), NULL);
  853. res->full.share_time.value60 = strtod(procfile_lineword(ff, l, 4), NULL);
  854. res->full.share_time.value300 = strtod(procfile_lineword(ff, l, 6), NULL);
  855. res->full.total_time.value_total = str2ull(procfile_lineword(ff, l, 8), NULL) / 1000; // us->ms
  856. did_full = true;
  857. }
  858. }
  859. res->updated = (did_full || did_some) ? 1 : 0;
  860. if(unlikely(res->some.enabled == CONFIG_BOOLEAN_AUTO))
  861. res->some.enabled = (did_some) ? CONFIG_BOOLEAN_YES : CONFIG_BOOLEAN_NO;
  862. if(unlikely(res->full.enabled == CONFIG_BOOLEAN_AUTO))
  863. res->full.enabled = (did_full) ? CONFIG_BOOLEAN_YES : CONFIG_BOOLEAN_NO;
  864. }
  865. }
  866. static inline void cgroup_read_memory(struct memory *mem, char parent_cg_is_unified) {
  867. static procfile *ff = NULL;
  868. // read detailed ram usage
  869. if(likely(mem->filename_detailed)) {
  870. if(unlikely(mem->enabled_detailed == CONFIG_BOOLEAN_AUTO && mem->delay_counter_detailed > 0)) {
  871. mem->delay_counter_detailed--;
  872. goto memory_next;
  873. }
  874. ff = procfile_reopen(ff, mem->filename_detailed, NULL, CGROUP_PROCFILE_FLAG);
  875. if(unlikely(!ff)) {
  876. mem->updated_detailed = 0;
  877. cgroups_check = 1;
  878. goto memory_next;
  879. }
  880. ff = procfile_readall(ff);
  881. if(unlikely(!ff)) {
  882. mem->updated_detailed = 0;
  883. cgroups_check = 1;
  884. goto memory_next;
  885. }
  886. unsigned long i, lines = procfile_lines(ff);
  887. if(unlikely(lines < 1)) {
  888. collector_error("CGROUP: file '%s' should have 1+ lines.", mem->filename_detailed);
  889. mem->updated_detailed = 0;
  890. goto memory_next;
  891. }
  892. if(unlikely(!mem->arl_base)) {
  893. if(parent_cg_is_unified == 0){
  894. mem->arl_base = arl_create("cgroup/memory", NULL, 60);
  895. arl_expect(mem->arl_base, "total_cache", &mem->total_cache);
  896. arl_expect(mem->arl_base, "total_rss", &mem->total_rss);
  897. arl_expect(mem->arl_base, "total_rss_huge", &mem->total_rss_huge);
  898. arl_expect(mem->arl_base, "total_mapped_file", &mem->total_mapped_file);
  899. arl_expect(mem->arl_base, "total_writeback", &mem->total_writeback);
  900. mem->arl_dirty = arl_expect(mem->arl_base, "total_dirty", &mem->total_dirty);
  901. mem->arl_swap = arl_expect(mem->arl_base, "total_swap", &mem->total_swap);
  902. arl_expect(mem->arl_base, "total_pgpgin", &mem->total_pgpgin);
  903. arl_expect(mem->arl_base, "total_pgpgout", &mem->total_pgpgout);
  904. arl_expect(mem->arl_base, "total_pgfault", &mem->total_pgfault);
  905. arl_expect(mem->arl_base, "total_pgmajfault", &mem->total_pgmajfault);
  906. arl_expect(mem->arl_base, "total_inactive_file", &mem->total_inactive_file);
  907. } else {
  908. mem->arl_base = arl_create("cgroup/memory", NULL, 60);
  909. arl_expect(mem->arl_base, "anon", &mem->anon);
  910. arl_expect(mem->arl_base, "kernel_stack", &mem->kernel_stack);
  911. arl_expect(mem->arl_base, "slab", &mem->slab);
  912. arl_expect(mem->arl_base, "sock", &mem->sock);
  913. arl_expect(mem->arl_base, "anon_thp", &mem->anon_thp);
  914. arl_expect(mem->arl_base, "file", &mem->total_mapped_file);
  915. arl_expect(mem->arl_base, "file_writeback", &mem->total_writeback);
  916. mem->arl_dirty = arl_expect(mem->arl_base, "file_dirty", &mem->total_dirty);
  917. arl_expect(mem->arl_base, "pgfault", &mem->total_pgfault);
  918. arl_expect(mem->arl_base, "pgmajfault", &mem->total_pgmajfault);
  919. arl_expect(mem->arl_base, "inactive_file", &mem->total_inactive_file);
  920. }
  921. }
  922. arl_begin(mem->arl_base);
  923. for(i = 0; i < lines ; i++) {
  924. if(arl_check(mem->arl_base,
  925. procfile_lineword(ff, i, 0),
  926. procfile_lineword(ff, i, 1))) break;
  927. }
  928. if(unlikely(mem->arl_dirty->flags & ARL_ENTRY_FLAG_FOUND))
  929. mem->detailed_has_dirty = 1;
  930. if(unlikely(parent_cg_is_unified == 0 && mem->arl_swap->flags & ARL_ENTRY_FLAG_FOUND))
  931. mem->detailed_has_swap = 1;
  932. // fprintf(stderr, "READ: '%s', cache: %llu, rss: %llu, rss_huge: %llu, mapped_file: %llu, writeback: %llu, dirty: %llu, swap: %llu, pgpgin: %llu, pgpgout: %llu, pgfault: %llu, pgmajfault: %llu, inactive_anon: %llu, active_anon: %llu, inactive_file: %llu, active_file: %llu, unevictable: %llu, hierarchical_memory_limit: %llu, total_cache: %llu, total_rss: %llu, total_rss_huge: %llu, total_mapped_file: %llu, total_writeback: %llu, total_dirty: %llu, total_swap: %llu, total_pgpgin: %llu, total_pgpgout: %llu, total_pgfault: %llu, total_pgmajfault: %llu, total_inactive_anon: %llu, total_active_anon: %llu, total_inactive_file: %llu, total_active_file: %llu, total_unevictable: %llu\n", mem->filename, mem->cache, mem->rss, mem->rss_huge, mem->mapped_file, mem->writeback, mem->dirty, mem->swap, mem->pgpgin, mem->pgpgout, mem->pgfault, mem->pgmajfault, mem->inactive_anon, mem->active_anon, mem->inactive_file, mem->active_file, mem->unevictable, mem->hierarchical_memory_limit, mem->total_cache, mem->total_rss, mem->total_rss_huge, mem->total_mapped_file, mem->total_writeback, mem->total_dirty, mem->total_swap, mem->total_pgpgin, mem->total_pgpgout, mem->total_pgfault, mem->total_pgmajfault, mem->total_inactive_anon, mem->total_active_anon, mem->total_inactive_file, mem->total_active_file, mem->total_unevictable);
  933. mem->updated_detailed = 1;
  934. if(unlikely(mem->enabled_detailed == CONFIG_BOOLEAN_AUTO)) {
  935. if(( (!parent_cg_is_unified) && ( mem->total_cache || mem->total_dirty || mem->total_rss || mem->total_rss_huge || mem->total_mapped_file || mem->total_writeback
  936. || mem->total_swap || mem->total_pgpgin || mem->total_pgpgout || mem->total_pgfault || mem->total_pgmajfault || mem->total_inactive_file))
  937. || (parent_cg_is_unified && ( mem->anon || mem->total_dirty || mem->kernel_stack || mem->slab || mem->sock || mem->total_writeback
  938. || mem->anon_thp || mem->total_pgfault || mem->total_pgmajfault || mem->total_inactive_file))
  939. || netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES)
  940. mem->enabled_detailed = CONFIG_BOOLEAN_YES;
  941. else
  942. mem->delay_counter_detailed = cgroup_recheck_zero_mem_detailed_every_iterations;
  943. }
  944. }
  945. memory_next:
  946. // read usage_in_bytes
  947. if(likely(mem->filename_usage_in_bytes)) {
  948. mem->updated_usage_in_bytes = !read_single_number_file(mem->filename_usage_in_bytes, &mem->usage_in_bytes);
  949. if(unlikely(mem->updated_usage_in_bytes && mem->enabled_usage_in_bytes == CONFIG_BOOLEAN_AUTO &&
  950. (mem->usage_in_bytes || netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES)))
  951. mem->enabled_usage_in_bytes = CONFIG_BOOLEAN_YES;
  952. }
  953. if (likely(mem->updated_usage_in_bytes && mem->updated_detailed)) {
  954. mem->usage_in_bytes =
  955. (mem->usage_in_bytes > mem->total_inactive_file) ? (mem->usage_in_bytes - mem->total_inactive_file) : 0;
  956. }
  957. // read msw_usage_in_bytes
  958. if(likely(mem->filename_msw_usage_in_bytes)) {
  959. mem->updated_msw_usage_in_bytes = !read_single_number_file(mem->filename_msw_usage_in_bytes, &mem->msw_usage_in_bytes);
  960. if(unlikely(mem->updated_msw_usage_in_bytes && mem->enabled_msw_usage_in_bytes == CONFIG_BOOLEAN_AUTO &&
  961. (mem->msw_usage_in_bytes || netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES)))
  962. mem->enabled_msw_usage_in_bytes = CONFIG_BOOLEAN_YES;
  963. }
  964. // read failcnt
  965. if(likely(mem->filename_failcnt)) {
  966. if(unlikely(mem->enabled_failcnt == CONFIG_BOOLEAN_AUTO && mem->delay_counter_failcnt > 0)) {
  967. mem->updated_failcnt = 0;
  968. mem->delay_counter_failcnt--;
  969. }
  970. else {
  971. mem->updated_failcnt = !read_single_number_file(mem->filename_failcnt, &mem->failcnt);
  972. if(unlikely(mem->updated_failcnt && mem->enabled_failcnt == CONFIG_BOOLEAN_AUTO)) {
  973. if(unlikely(mem->failcnt || netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))
  974. mem->enabled_failcnt = CONFIG_BOOLEAN_YES;
  975. else
  976. mem->delay_counter_failcnt = cgroup_recheck_zero_mem_failcnt_every_iterations;
  977. }
  978. }
  979. }
  980. }
  981. static void cgroup_read_pids_current(struct pids *pids) {
  982. pids->pids_current_updated = 0;
  983. if (unlikely(!pids->pids_current_filename))
  984. return;
  985. pids->pids_current_updated = !read_single_number_file(pids->pids_current_filename, &pids->pids_current);
  986. }
  987. static inline void read_cgroup(struct cgroup *cg) {
  988. netdata_log_debug(D_CGROUP, "reading metrics for cgroups '%s'", cg->id);
  989. if(!(cg->options & CGROUP_OPTIONS_IS_UNIFIED)) {
  990. cgroup_read_cpuacct_stat(&cg->cpuacct_stat);
  991. cgroup_read_cpuacct_usage(&cg->cpuacct_usage);
  992. cgroup_read_cpuacct_cpu_stat(&cg->cpuacct_cpu_throttling);
  993. cgroup_read_cpuacct_cpu_shares(&cg->cpuacct_cpu_shares);
  994. cgroup_read_memory(&cg->memory, 0);
  995. cgroup_read_blkio(&cg->io_service_bytes);
  996. cgroup_read_blkio(&cg->io_serviced);
  997. cgroup_read_blkio(&cg->throttle_io_service_bytes);
  998. cgroup_read_blkio(&cg->throttle_io_serviced);
  999. cgroup_read_blkio(&cg->io_merged);
  1000. cgroup_read_blkio(&cg->io_queued);
  1001. cgroup_read_pids_current(&cg->pids);
  1002. }
  1003. else {
  1004. //TODO: io_service_bytes and io_serviced use same file merge into 1 function
  1005. cgroup2_read_blkio(&cg->io_service_bytes, 0);
  1006. cgroup2_read_blkio(&cg->io_serviced, 4);
  1007. cgroup2_read_cpuacct_cpu_stat(&cg->cpuacct_stat, &cg->cpuacct_cpu_throttling);
  1008. cgroup_read_cpuacct_cpu_shares(&cg->cpuacct_cpu_shares);
  1009. cgroup2_read_pressure(&cg->cpu_pressure);
  1010. cgroup2_read_pressure(&cg->io_pressure);
  1011. cgroup2_read_pressure(&cg->memory_pressure);
  1012. cgroup2_read_pressure(&cg->irq_pressure);
  1013. cgroup_read_memory(&cg->memory, 1);
  1014. cgroup_read_pids_current(&cg->pids);
  1015. }
  1016. }
  1017. static inline void read_all_discovered_cgroups(struct cgroup *root) {
  1018. netdata_log_debug(D_CGROUP, "reading metrics for all cgroups");
  1019. struct cgroup *cg;
  1020. for (cg = root; cg; cg = cg->next) {
  1021. if (cg->enabled && !cg->pending_renames) {
  1022. read_cgroup(cg);
  1023. }
  1024. }
  1025. }
  1026. // update CPU and memory limits
  1027. static inline void update_cpu_limits(char **filename, unsigned long long *value, struct cgroup *cg) {
  1028. if(*filename) {
  1029. int ret = -1;
  1030. if(value == &cg->cpuset_cpus) {
  1031. unsigned long ncpus = read_cpuset_cpus(*filename, get_system_cpus());
  1032. if(ncpus) {
  1033. *value = ncpus;
  1034. ret = 0;
  1035. }
  1036. }
  1037. else if(value == &cg->cpu_cfs_period || value == &cg->cpu_cfs_quota) {
  1038. ret = read_single_number_file(*filename, value);
  1039. }
  1040. else ret = -1;
  1041. if(ret) {
  1042. collector_error("Cannot refresh cgroup %s cpu limit by reading '%s'. Will not update its limit anymore.", cg->id, *filename);
  1043. freez(*filename);
  1044. *filename = NULL;
  1045. }
  1046. }
  1047. }
  1048. static inline void update_cpu_limits2(struct cgroup *cg) {
  1049. if(cg->filename_cpu_cfs_quota){
  1050. static procfile *ff = NULL;
  1051. ff = procfile_reopen(ff, cg->filename_cpu_cfs_quota, NULL, CGROUP_PROCFILE_FLAG);
  1052. if(unlikely(!ff)) {
  1053. goto cpu_limits2_err;
  1054. }
  1055. ff = procfile_readall(ff);
  1056. if(unlikely(!ff)) {
  1057. goto cpu_limits2_err;
  1058. }
  1059. unsigned long lines = procfile_lines(ff);
  1060. if (unlikely(lines < 1)) {
  1061. collector_error("CGROUP: file '%s' should have 1 lines.", cg->filename_cpu_cfs_quota);
  1062. return;
  1063. }
  1064. cg->cpu_cfs_period = str2ull(procfile_lineword(ff, 0, 1), NULL);
  1065. cg->cpuset_cpus = get_system_cpus();
  1066. char *s = "max\n\0";
  1067. if(strcmp(s, procfile_lineword(ff, 0, 0)) == 0){
  1068. cg->cpu_cfs_quota = cg->cpu_cfs_period * cg->cpuset_cpus;
  1069. } else {
  1070. cg->cpu_cfs_quota = str2ull(procfile_lineword(ff, 0, 0), NULL);
  1071. }
  1072. netdata_log_debug(D_CGROUP, "CPU limits values: %llu %llu %llu", cg->cpu_cfs_period, cg->cpuset_cpus, cg->cpu_cfs_quota);
  1073. return;
  1074. cpu_limits2_err:
  1075. collector_error("Cannot refresh cgroup %s cpu limit by reading '%s'. Will not update its limit anymore.", cg->id, cg->filename_cpu_cfs_quota);
  1076. freez(cg->filename_cpu_cfs_quota);
  1077. cg->filename_cpu_cfs_quota = NULL;
  1078. }
  1079. }
  1080. static inline int update_memory_limits(struct cgroup *cg) {
  1081. char **filename = &cg->filename_memory_limit;
  1082. const RRDSETVAR_ACQUIRED **chart_var = &cg->chart_var_memory_limit;
  1083. unsigned long long *value = &cg->memory_limit;
  1084. if(*filename) {
  1085. if(unlikely(!*chart_var)) {
  1086. *chart_var = rrdsetvar_custom_chart_variable_add_and_acquire(cg->st_mem_usage, "memory_limit");
  1087. if(!*chart_var) {
  1088. collector_error("Cannot create cgroup %s chart variable '%s'. Will not update its limit anymore.", cg->id, "memory_limit");
  1089. freez(*filename);
  1090. *filename = NULL;
  1091. }
  1092. }
  1093. if(*filename && *chart_var) {
  1094. if(!(cg->options & CGROUP_OPTIONS_IS_UNIFIED)) {
  1095. if(read_single_number_file(*filename, value)) {
  1096. collector_error("Cannot refresh cgroup %s memory limit by reading '%s'. Will not update its limit anymore.", cg->id, *filename);
  1097. freez(*filename);
  1098. *filename = NULL;
  1099. }
  1100. else {
  1101. rrdsetvar_custom_chart_variable_set(cg->st_mem_usage, *chart_var, (NETDATA_DOUBLE)(*value) / (1024.0 * 1024.0));
  1102. return 1;
  1103. }
  1104. } else {
  1105. char buffer[30 + 1];
  1106. int ret = read_file(*filename, buffer, 30);
  1107. if(ret) {
  1108. collector_error("Cannot refresh cgroup %s memory limit by reading '%s'. Will not update its limit anymore.", cg->id, *filename);
  1109. freez(*filename);
  1110. *filename = NULL;
  1111. return 0;
  1112. }
  1113. char *s = "max\n\0";
  1114. if(strcmp(s, buffer) == 0){
  1115. *value = UINT64_MAX;
  1116. rrdsetvar_custom_chart_variable_set(cg->st_mem_usage, *chart_var, (NETDATA_DOUBLE)(*value) / (1024.0 * 1024.0));
  1117. return 1;
  1118. }
  1119. *value = str2ull(buffer, NULL);
  1120. rrdsetvar_custom_chart_variable_set(cg->st_mem_usage, *chart_var, (NETDATA_DOUBLE)(*value) / (1024.0 * 1024.0));
  1121. return 1;
  1122. }
  1123. }
  1124. }
  1125. return 0;
  1126. }
  1127. // ----------------------------------------------------------------------------
  1128. // generate charts
  1129. void update_cgroup_systemd_services_charts() {
  1130. for (struct cgroup *cg = cgroup_root; cg; cg = cg->next) {
  1131. if (unlikely(!cg->enabled || cg->pending_renames || !is_cgroup_systemd_service(cg)))
  1132. continue;
  1133. if (likely(cg->cpuacct_stat.updated)) {
  1134. update_cpu_utilization_chart(cg);
  1135. }
  1136. if (likely(cg->memory.updated_msw_usage_in_bytes)) {
  1137. update_mem_usage_chart(cg);
  1138. }
  1139. if (likely(cg->memory.updated_failcnt)) {
  1140. update_mem_failcnt_chart(cg);
  1141. }
  1142. if (likely(cg->memory.updated_detailed)) {
  1143. update_mem_usage_detailed_chart(cg);
  1144. update_mem_writeback_chart(cg);
  1145. update_mem_pgfaults_chart(cg);
  1146. if (!(cg->options & CGROUP_OPTIONS_IS_UNIFIED)) {
  1147. update_mem_activity_chart(cg);
  1148. }
  1149. }
  1150. if (likely(cg->io_service_bytes.updated)) {
  1151. update_io_serviced_bytes_chart(cg);
  1152. }
  1153. if (likely(cg->io_serviced.updated)) {
  1154. update_io_serviced_ops_chart(cg);
  1155. }
  1156. if (likely(cg->throttle_io_service_bytes.updated)) {
  1157. update_throttle_io_serviced_bytes_chart(cg);
  1158. }
  1159. if (likely(cg->throttle_io_serviced.updated)) {
  1160. update_throttle_io_serviced_ops_chart(cg);
  1161. }
  1162. if (likely(cg->io_queued.updated)) {
  1163. update_io_queued_ops_chart(cg);
  1164. }
  1165. if (likely(cg->io_merged.updated)) {
  1166. update_io_merged_ops_chart(cg);
  1167. }
  1168. if (likely(cg->pids.pids_current_updated)) {
  1169. update_pids_current_chart(cg);
  1170. }
  1171. cg->function_ready = true;
  1172. }
  1173. }
  1174. void update_cgroup_charts() {
  1175. for (struct cgroup *cg = cgroup_root; cg; cg = cg->next) {
  1176. if(unlikely(!cg->enabled || cg->pending_renames || is_cgroup_systemd_service(cg)))
  1177. continue;
  1178. if (likely(cg->cpuacct_stat.updated && cg->cpuacct_stat.enabled == CONFIG_BOOLEAN_YES)) {
  1179. update_cpu_utilization_chart(cg);
  1180. if(likely(cg->filename_cpuset_cpus || cg->filename_cpu_cfs_period || cg->filename_cpu_cfs_quota)) {
  1181. if(!(cg->options & CGROUP_OPTIONS_IS_UNIFIED)) {
  1182. update_cpu_limits(&cg->filename_cpuset_cpus, &cg->cpuset_cpus, cg);
  1183. update_cpu_limits(&cg->filename_cpu_cfs_period, &cg->cpu_cfs_period, cg);
  1184. update_cpu_limits(&cg->filename_cpu_cfs_quota, &cg->cpu_cfs_quota, cg);
  1185. } else {
  1186. update_cpu_limits2(cg);
  1187. }
  1188. if(unlikely(!cg->chart_var_cpu_limit)) {
  1189. cg->chart_var_cpu_limit = rrdsetvar_custom_chart_variable_add_and_acquire(cg->st_cpu, "cpu_limit");
  1190. if(!cg->chart_var_cpu_limit) {
  1191. collector_error("Cannot create cgroup %s chart variable 'cpu_limit'. Will not update its limit anymore.", cg->id);
  1192. if(cg->filename_cpuset_cpus) freez(cg->filename_cpuset_cpus);
  1193. cg->filename_cpuset_cpus = NULL;
  1194. if(cg->filename_cpu_cfs_period) freez(cg->filename_cpu_cfs_period);
  1195. cg->filename_cpu_cfs_period = NULL;
  1196. if(cg->filename_cpu_cfs_quota) freez(cg->filename_cpu_cfs_quota);
  1197. cg->filename_cpu_cfs_quota = NULL;
  1198. }
  1199. } else {
  1200. NETDATA_DOUBLE value = 0, quota = 0;
  1201. if(likely( ((!(cg->options & CGROUP_OPTIONS_IS_UNIFIED)) && (cg->filename_cpuset_cpus || (cg->filename_cpu_cfs_period && cg->filename_cpu_cfs_quota)))
  1202. || ((cg->options & CGROUP_OPTIONS_IS_UNIFIED) && cg->filename_cpu_cfs_quota))) {
  1203. if(unlikely(cg->cpu_cfs_quota > 0))
  1204. quota = (NETDATA_DOUBLE)cg->cpu_cfs_quota / (NETDATA_DOUBLE)cg->cpu_cfs_period;
  1205. if(unlikely(quota > 0 && quota < cg->cpuset_cpus))
  1206. value = quota * 100;
  1207. else
  1208. value = (NETDATA_DOUBLE)cg->cpuset_cpus * 100;
  1209. }
  1210. if(likely(value)) {
  1211. update_cpu_utilization_limit_chart(cg, value);
  1212. } else {
  1213. if (unlikely(cg->st_cpu_limit)) {
  1214. rrdset_is_obsolete___safe_from_collector_thread(cg->st_cpu_limit);
  1215. cg->st_cpu_limit = NULL;
  1216. }
  1217. rrdsetvar_custom_chart_variable_set(cg->st_cpu, cg->chart_var_cpu_limit, NAN);
  1218. }
  1219. }
  1220. }
  1221. }
  1222. if (likely(cg->cpuacct_cpu_throttling.updated && cg->cpuacct_cpu_throttling.enabled == CONFIG_BOOLEAN_YES)) {
  1223. update_cpu_throttled_chart(cg);
  1224. update_cpu_throttled_duration_chart(cg);
  1225. }
  1226. if (likely(cg->cpuacct_cpu_shares.updated && cg->cpuacct_cpu_shares.enabled == CONFIG_BOOLEAN_YES)) {
  1227. update_cpu_shares_chart(cg);
  1228. }
  1229. if (likely(cg->cpuacct_usage.updated && cg->cpuacct_usage.enabled == CONFIG_BOOLEAN_YES)) {
  1230. update_cpu_per_core_usage_chart(cg);
  1231. }
  1232. if (likely(cg->memory.updated_detailed && cg->memory.enabled_detailed == CONFIG_BOOLEAN_YES)) {
  1233. update_mem_usage_detailed_chart(cg);
  1234. update_mem_writeback_chart(cg);
  1235. if(!(cg->options & CGROUP_OPTIONS_IS_UNIFIED)) {
  1236. update_mem_activity_chart(cg);
  1237. }
  1238. update_mem_pgfaults_chart(cg);
  1239. }
  1240. if (likely(cg->memory.updated_usage_in_bytes && cg->memory.enabled_usage_in_bytes == CONFIG_BOOLEAN_YES)) {
  1241. update_mem_usage_chart(cg);
  1242. // FIXME: this if should be only for unlimited charts
  1243. if(likely(host_ram_total)) {
  1244. // FIXME: do we need to update mem limits on every data collection?
  1245. if (likely(update_memory_limits(cg))) {
  1246. unsigned long long memory_limit = host_ram_total;
  1247. if (unlikely(cg->memory_limit < host_ram_total))
  1248. memory_limit = cg->memory_limit;
  1249. update_mem_usage_limit_chart(cg, memory_limit);
  1250. update_mem_utilization_chart(cg, memory_limit);
  1251. } else {
  1252. if (unlikely(cg->st_mem_usage_limit)) {
  1253. rrdset_is_obsolete___safe_from_collector_thread(cg->st_mem_usage_limit);
  1254. cg->st_mem_usage_limit = NULL;
  1255. }
  1256. if (unlikely(cg->st_mem_utilization)) {
  1257. rrdset_is_obsolete___safe_from_collector_thread(cg->st_mem_utilization);
  1258. cg->st_mem_utilization = NULL;
  1259. }
  1260. }
  1261. }
  1262. }
  1263. if (likely(cg->memory.updated_failcnt && cg->memory.enabled_failcnt == CONFIG_BOOLEAN_YES)) {
  1264. update_mem_failcnt_chart(cg);
  1265. }
  1266. if (likely(cg->io_service_bytes.updated && cg->io_service_bytes.enabled == CONFIG_BOOLEAN_YES)) {
  1267. update_io_serviced_bytes_chart(cg);
  1268. }
  1269. if (likely(cg->io_serviced.updated && cg->io_serviced.enabled == CONFIG_BOOLEAN_YES)) {
  1270. update_io_serviced_ops_chart(cg);
  1271. }
  1272. if (likely(cg->throttle_io_service_bytes.updated && cg->throttle_io_service_bytes.enabled == CONFIG_BOOLEAN_YES)) {
  1273. update_throttle_io_serviced_bytes_chart(cg);
  1274. }
  1275. if (likely(cg->throttle_io_serviced.updated && cg->throttle_io_serviced.enabled == CONFIG_BOOLEAN_YES)) {
  1276. update_throttle_io_serviced_ops_chart(cg);
  1277. }
  1278. if (likely(cg->io_queued.updated && cg->io_queued.enabled == CONFIG_BOOLEAN_YES)) {
  1279. update_io_queued_ops_chart(cg);
  1280. }
  1281. if (likely(cg->io_merged.updated && cg->io_merged.enabled == CONFIG_BOOLEAN_YES)) {
  1282. update_io_merged_ops_chart(cg);
  1283. }
  1284. if (likely(cg->pids.pids_current_updated)) {
  1285. update_pids_current_chart(cg);
  1286. }
  1287. if (cg->options & CGROUP_OPTIONS_IS_UNIFIED) {
  1288. if (likely(cg->cpu_pressure.updated)) {
  1289. if (cg->cpu_pressure.some.enabled) {
  1290. update_cpu_some_pressure_chart(cg);
  1291. update_cpu_some_pressure_stall_time_chart(cg);
  1292. }
  1293. if (cg->cpu_pressure.full.enabled) {
  1294. update_cpu_full_pressure_chart(cg);
  1295. update_cpu_full_pressure_stall_time_chart(cg);
  1296. }
  1297. }
  1298. if (likely(cg->memory_pressure.updated)) {
  1299. if (cg->memory_pressure.some.enabled) {
  1300. update_mem_some_pressure_chart(cg);
  1301. update_mem_some_pressure_stall_time_chart(cg);
  1302. }
  1303. if (cg->memory_pressure.full.enabled) {
  1304. update_mem_full_pressure_chart(cg);
  1305. update_mem_full_pressure_stall_time_chart(cg);
  1306. }
  1307. }
  1308. if (likely(cg->irq_pressure.updated)) {
  1309. if (cg->irq_pressure.some.enabled) {
  1310. update_irq_some_pressure_chart(cg);
  1311. update_irq_some_pressure_stall_time_chart(cg);
  1312. }
  1313. if (cg->irq_pressure.full.enabled) {
  1314. update_irq_full_pressure_chart(cg);
  1315. update_irq_full_pressure_stall_time_chart(cg);
  1316. }
  1317. }
  1318. if (likely(cg->io_pressure.updated)) {
  1319. if (cg->io_pressure.some.enabled) {
  1320. update_io_some_pressure_chart(cg);
  1321. update_io_some_pressure_stall_time_chart(cg);
  1322. }
  1323. if (cg->io_pressure.full.enabled) {
  1324. update_io_full_pressure_chart(cg);
  1325. update_io_full_pressure_stall_time_chart(cg);
  1326. }
  1327. }
  1328. }
  1329. cg->function_ready = true;
  1330. }
  1331. }
  1332. // ----------------------------------------------------------------------------
  1333. // cgroups main
  1334. static void cgroup_main_cleanup(void *ptr) {
  1335. worker_unregister();
  1336. struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
  1337. static_thread->enabled = NETDATA_MAIN_THREAD_EXITING;
  1338. collector_info("cleaning up...");
  1339. usec_t max = 2 * USEC_PER_SEC, step = 50000;
  1340. if (!__atomic_load_n(&discovery_thread.exited, __ATOMIC_RELAXED)) {
  1341. collector_info("waiting for discovery thread to finish...");
  1342. while (!__atomic_load_n(&discovery_thread.exited, __ATOMIC_RELAXED) && max > 0) {
  1343. uv_mutex_lock(&discovery_thread.mutex);
  1344. uv_cond_signal(&discovery_thread.cond_var);
  1345. uv_mutex_unlock(&discovery_thread.mutex);
  1346. max -= step;
  1347. sleep_usec(step);
  1348. }
  1349. }
  1350. if (shm_mutex_cgroup_ebpf != SEM_FAILED) {
  1351. sem_close(shm_mutex_cgroup_ebpf);
  1352. }
  1353. if (shm_cgroup_ebpf.header) {
  1354. shm_cgroup_ebpf.header->cgroup_root_count = 0;
  1355. munmap(shm_cgroup_ebpf.header, shm_cgroup_ebpf.header->body_length);
  1356. }
  1357. if (shm_fd_cgroup_ebpf > 0) {
  1358. close(shm_fd_cgroup_ebpf);
  1359. }
  1360. static_thread->enabled = NETDATA_MAIN_THREAD_EXITED;
  1361. }
  1362. void cgroup_read_host_total_ram() {
  1363. procfile *ff = NULL;
  1364. char filename[FILENAME_MAX + 1];
  1365. snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/proc/meminfo");
  1366. ff = procfile_open(
  1367. config_get("plugin:cgroups", "meminfo filename to monitor", filename), " \t:", PROCFILE_FLAG_DEFAULT);
  1368. if (likely((ff = procfile_readall(ff)) && procfile_lines(ff) && !strncmp(procfile_word(ff, 0), "MemTotal", 8)))
  1369. host_ram_total = str2ull(procfile_word(ff, 1), NULL) * 1024;
  1370. else
  1371. collector_error("Cannot read file %s. Will not create RAM limit charts.", filename);
  1372. procfile_close(ff);
  1373. }
  1374. void *cgroups_main(void *ptr) {
  1375. worker_register("CGROUPS");
  1376. worker_register_job_name(WORKER_CGROUPS_LOCK, "lock");
  1377. worker_register_job_name(WORKER_CGROUPS_READ, "read");
  1378. worker_register_job_name(WORKER_CGROUPS_CHART, "chart");
  1379. netdata_thread_cleanup_push(cgroup_main_cleanup, ptr);
  1380. if (getenv("KUBERNETES_SERVICE_HOST") != NULL && getenv("KUBERNETES_SERVICE_PORT") != NULL) {
  1381. is_inside_k8s = 1;
  1382. cgroup_enable_cpuacct_cpu_shares = CONFIG_BOOLEAN_YES;
  1383. }
  1384. read_cgroup_plugin_configuration();
  1385. cgroup_read_host_total_ram();
  1386. netdata_cgroup_ebpf_initialize_shm();
  1387. if (uv_mutex_init(&cgroup_root_mutex)) {
  1388. collector_error("CGROUP: cannot initialize mutex for the main cgroup list");
  1389. goto exit;
  1390. }
  1391. discovery_thread.exited = 0;
  1392. if (uv_mutex_init(&discovery_thread.mutex)) {
  1393. collector_error("CGROUP: cannot initialize mutex for discovery thread");
  1394. goto exit;
  1395. }
  1396. if (uv_cond_init(&discovery_thread.cond_var)) {
  1397. collector_error("CGROUP: cannot initialize conditional variable for discovery thread");
  1398. goto exit;
  1399. }
  1400. int error = uv_thread_create(&discovery_thread.thread, cgroup_discovery_worker, NULL);
  1401. if (error) {
  1402. collector_error("CGROUP: cannot create thread worker. uv_thread_create(): %s", uv_strerror(error));
  1403. goto exit;
  1404. }
  1405. uv_thread_set_name_np(discovery_thread.thread, "P[cgroups]");
  1406. // we register this only on localhost
  1407. // for the other nodes, the origin server should register it
  1408. rrd_collector_started(); // this creates a collector that runs for as long as netdata runs
  1409. cgroup_netdev_link_init();
  1410. rrd_function_add(localhost, NULL, "containers-vms", 10, RRDFUNCTIONS_CGTOP_HELP, true, cgroup_function_cgroup_top, NULL);
  1411. rrd_function_add(localhost, NULL, "systemd-services", 10, RRDFUNCTIONS_CGTOP_HELP, true, cgroup_function_systemd_top, NULL);
  1412. heartbeat_t hb;
  1413. heartbeat_init(&hb);
  1414. usec_t step = cgroup_update_every * USEC_PER_SEC;
  1415. usec_t find_every = cgroup_check_for_new_every * USEC_PER_SEC, find_dt = 0;
  1416. netdata_thread_disable_cancelability();
  1417. while(service_running(SERVICE_COLLECTORS)) {
  1418. worker_is_idle();
  1419. usec_t hb_dt = heartbeat_next(&hb, step);
  1420. if (unlikely(!service_running(SERVICE_COLLECTORS)))
  1421. break;
  1422. find_dt += hb_dt;
  1423. if (unlikely(find_dt >= find_every || (!is_inside_k8s && cgroups_check))) {
  1424. uv_mutex_lock(&discovery_thread.mutex);
  1425. uv_cond_signal(&discovery_thread.cond_var);
  1426. uv_mutex_unlock(&discovery_thread.mutex);
  1427. find_dt = 0;
  1428. cgroups_check = 0;
  1429. }
  1430. worker_is_busy(WORKER_CGROUPS_LOCK);
  1431. uv_mutex_lock(&cgroup_root_mutex);
  1432. worker_is_busy(WORKER_CGROUPS_READ);
  1433. read_all_discovered_cgroups(cgroup_root);
  1434. if (unlikely(!service_running(SERVICE_COLLECTORS))) {
  1435. uv_mutex_unlock(&cgroup_root_mutex);
  1436. break;
  1437. }
  1438. worker_is_busy(WORKER_CGROUPS_CHART);
  1439. update_cgroup_charts();
  1440. if (cgroup_enable_systemd_services)
  1441. update_cgroup_systemd_services_charts();
  1442. if (unlikely(!service_running(SERVICE_COLLECTORS))) {
  1443. uv_mutex_unlock(&cgroup_root_mutex);
  1444. break;
  1445. }
  1446. worker_is_idle();
  1447. uv_mutex_unlock(&cgroup_root_mutex);
  1448. }
  1449. exit:
  1450. netdata_thread_cleanup_pop(1);
  1451. return NULL;
  1452. }