sys_fs_cgroup.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736
  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", cgroup_check_for_new_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. " *user.slice/docker-*" // allow docker rootless containers
  367. " !*user.slice*" // ignore the rest stuff in user.slice
  368. " *.scope " // we need all other *.scope for sure
  369. // ----------------------------------------------------------------
  370. " /machine.slice/*.service " // #3367 systemd-nspawn
  371. // ----------------------------------------------------------------
  372. " */kubepods/pod*/* " // k8s containers
  373. " */kubepods/*/pod*/* " // k8s containers
  374. " */*-kubepods-pod*/* " // k8s containers
  375. " */*-kubepods-*-pod*/* " // k8s containers
  376. " !*kubepods* !*kubelet* " // all other k8s cgroups
  377. // ----------------------------------------------------------------
  378. " !*/vcpu* " // libvirtd adds these sub-cgroups
  379. " !*/emulator " // libvirtd adds these sub-cgroups
  380. " !*.mount "
  381. " !*.partition "
  382. " !*.service "
  383. " !*.service/udev "
  384. " !*.socket "
  385. " !*.slice "
  386. " !*.swap "
  387. " !*.user "
  388. " !/ "
  389. " !/docker "
  390. " !*/libvirt "
  391. " !/lxc "
  392. " !/lxc/*/* " // #1397 #2649
  393. " !/lxc.monitor* "
  394. " !/lxc.pivot "
  395. " !/lxc.payload "
  396. " !*lxcfs.service/.control"
  397. " !/machine "
  398. " !/qemu "
  399. " !/system "
  400. " !/systemd "
  401. " !/user "
  402. " * " // enable anything else
  403. ), NULL, SIMPLE_PATTERN_EXACT, true);
  404. enabled_cgroup_names = simple_pattern_create(
  405. config_get("plugin:cgroups", "enable by default cgroups names matching",
  406. " * "
  407. ), NULL, SIMPLE_PATTERN_EXACT, true);
  408. search_cgroup_paths = simple_pattern_create(
  409. config_get("plugin:cgroups", "search for cgroups in subpaths matching",
  410. " !*/init.scope " // ignore init.scope
  411. " !*-qemu " // #345
  412. " !*.libvirt-qemu " // #3010
  413. " !/init.scope "
  414. " !/system "
  415. " !/systemd "
  416. " !/user "
  417. " !/lxc/*/* " // #2161 #2649
  418. " !/lxc.monitor "
  419. " !/lxc.payload/*/* "
  420. " !/lxc.payload.* "
  421. " * "
  422. ), NULL, SIMPLE_PATTERN_EXACT, true);
  423. snprintfz(filename, FILENAME_MAX, "%s/cgroup-name.sh", netdata_configured_primary_plugins_dir);
  424. cgroups_rename_script = config_get("plugin:cgroups", "script to get cgroup names", filename);
  425. snprintfz(filename, FILENAME_MAX, "%s/cgroup-network", netdata_configured_primary_plugins_dir);
  426. cgroups_network_interface_script = config_get("plugin:cgroups", "script to get cgroup network interfaces", filename);
  427. enabled_cgroup_renames = simple_pattern_create(
  428. config_get("plugin:cgroups", "run script to rename cgroups matching",
  429. " !/ "
  430. " !*.mount "
  431. " !*.socket "
  432. " !*.partition "
  433. " /machine.slice/*.service " // #3367 systemd-nspawn
  434. " !*.service "
  435. " !*.slice "
  436. " !*.swap "
  437. " !*.user "
  438. " !init.scope "
  439. " !*.scope/vcpu* " // libvirtd adds these sub-cgroups
  440. " !*.scope/emulator " // libvirtd adds these sub-cgroups
  441. " *.scope "
  442. " *docker* "
  443. " *lxc* "
  444. " *qemu* "
  445. " */kubepods/pod*/* " // k8s containers
  446. " */kubepods/*/pod*/* " // k8s containers
  447. " */*-kubepods-pod*/* " // k8s containers
  448. " */*-kubepods-*-pod*/* " // k8s containers
  449. " !*kubepods* !*kubelet* " // all other k8s cgroups
  450. " *.libvirt-qemu " // #3010
  451. " * "
  452. ), NULL, SIMPLE_PATTERN_EXACT, true);
  453. if(cgroup_enable_systemd_services) {
  454. systemd_services_cgroups = simple_pattern_create(
  455. config_get("plugin:cgroups", "cgroups to match as systemd services",
  456. " !/system.slice/*/*.service "
  457. " /system.slice/*.service "
  458. ), NULL, SIMPLE_PATTERN_EXACT, true);
  459. }
  460. mountinfo_free_all(root);
  461. }
  462. void netdata_cgroup_ebpf_set_values(size_t length)
  463. {
  464. sem_wait(shm_mutex_cgroup_ebpf);
  465. shm_cgroup_ebpf.header->cgroup_max = cgroup_root_max;
  466. shm_cgroup_ebpf.header->systemd_enabled = cgroup_enable_systemd_services |
  467. cgroup_enable_systemd_services_detailed_memory |
  468. cgroup_used_memory;
  469. shm_cgroup_ebpf.header->body_length = length;
  470. sem_post(shm_mutex_cgroup_ebpf);
  471. }
  472. void netdata_cgroup_ebpf_initialize_shm()
  473. {
  474. shm_fd_cgroup_ebpf = shm_open(NETDATA_SHARED_MEMORY_EBPF_CGROUP_NAME, O_CREAT | O_RDWR, 0660);
  475. if (shm_fd_cgroup_ebpf < 0) {
  476. collector_error("Cannot initialize shared memory used by cgroup and eBPF, integration won't happen.");
  477. return;
  478. }
  479. size_t length = sizeof(netdata_ebpf_cgroup_shm_header_t) + cgroup_root_max * sizeof(netdata_ebpf_cgroup_shm_body_t);
  480. if (ftruncate(shm_fd_cgroup_ebpf, length)) {
  481. collector_error("Cannot set size for shared memory.");
  482. goto end_init_shm;
  483. }
  484. shm_cgroup_ebpf.header = (netdata_ebpf_cgroup_shm_header_t *) mmap(NULL, length,
  485. PROT_READ | PROT_WRITE, MAP_SHARED,
  486. shm_fd_cgroup_ebpf, 0);
  487. if (unlikely(MAP_FAILED == shm_cgroup_ebpf.header)) {
  488. shm_cgroup_ebpf.header = NULL;
  489. collector_error("Cannot map shared memory used between cgroup and eBPF, integration won't happen");
  490. goto end_init_shm;
  491. }
  492. shm_cgroup_ebpf.body = (netdata_ebpf_cgroup_shm_body_t *) ((char *)shm_cgroup_ebpf.header +
  493. sizeof(netdata_ebpf_cgroup_shm_header_t));
  494. shm_mutex_cgroup_ebpf = sem_open(NETDATA_NAMED_SEMAPHORE_EBPF_CGROUP_NAME, O_CREAT,
  495. S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, 1);
  496. if (shm_mutex_cgroup_ebpf != SEM_FAILED) {
  497. netdata_cgroup_ebpf_set_values(length);
  498. return;
  499. }
  500. collector_error("Cannot create semaphore, integration between eBPF and cgroup won't happen");
  501. munmap(shm_cgroup_ebpf.header, length);
  502. shm_cgroup_ebpf.header = NULL;
  503. end_init_shm:
  504. close(shm_fd_cgroup_ebpf);
  505. shm_fd_cgroup_ebpf = -1;
  506. shm_unlink(NETDATA_SHARED_MEMORY_EBPF_CGROUP_NAME);
  507. }
  508. // ---------------------------------------------------------------------------------------------
  509. static unsigned long long calc_delta(unsigned long long curr, unsigned long long prev) {
  510. if (prev > curr) {
  511. return 0;
  512. }
  513. return curr - prev;
  514. }
  515. static unsigned long long calc_percentage(unsigned long long value, unsigned long long total) {
  516. if (total == 0) {
  517. return 0;
  518. }
  519. return (unsigned long long)((NETDATA_DOUBLE)value / (NETDATA_DOUBLE)total * 100);
  520. }
  521. // ----------------------------------------------------------------------------
  522. // read values from /sys
  523. static inline void cgroup_read_cpuacct_stat(struct cpuacct_stat *cp) {
  524. static procfile *ff = NULL;
  525. if(likely(cp->filename)) {
  526. ff = procfile_reopen(ff, cp->filename, NULL, CGROUP_PROCFILE_FLAG);
  527. if(unlikely(!ff)) {
  528. cp->updated = 0;
  529. cgroups_check = 1;
  530. return;
  531. }
  532. ff = procfile_readall(ff);
  533. if(unlikely(!ff)) {
  534. cp->updated = 0;
  535. cgroups_check = 1;
  536. return;
  537. }
  538. unsigned long i, lines = procfile_lines(ff);
  539. if(unlikely(lines < 1)) {
  540. collector_error("CGROUP: file '%s' should have 1+ lines.", cp->filename);
  541. cp->updated = 0;
  542. return;
  543. }
  544. for(i = 0; i < lines ; i++) {
  545. char *s = procfile_lineword(ff, i, 0);
  546. uint32_t hash = simple_hash(s);
  547. if(unlikely(hash == user_hash && !strcmp(s, "user")))
  548. cp->user = str2ull(procfile_lineword(ff, i, 1), NULL);
  549. else if(unlikely(hash == system_hash && !strcmp(s, "system")))
  550. cp->system = str2ull(procfile_lineword(ff, i, 1), NULL);
  551. }
  552. cp->updated = 1;
  553. if(unlikely(cp->enabled == CONFIG_BOOLEAN_AUTO &&
  554. (cp->user || cp->system || netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES)))
  555. cp->enabled = CONFIG_BOOLEAN_YES;
  556. }
  557. }
  558. static inline void cgroup_read_cpuacct_cpu_stat(struct cpuacct_cpu_throttling *cp) {
  559. if (unlikely(!cp->filename)) {
  560. return;
  561. }
  562. static procfile *ff = NULL;
  563. ff = procfile_reopen(ff, cp->filename, NULL, CGROUP_PROCFILE_FLAG);
  564. if (unlikely(!ff)) {
  565. cp->updated = 0;
  566. cgroups_check = 1;
  567. return;
  568. }
  569. ff = procfile_readall(ff);
  570. if (unlikely(!ff)) {
  571. cp->updated = 0;
  572. cgroups_check = 1;
  573. return;
  574. }
  575. unsigned long lines = procfile_lines(ff);
  576. if (unlikely(lines < 3)) {
  577. collector_error("CGROUP: file '%s' should have 3 lines.", cp->filename);
  578. cp->updated = 0;
  579. return;
  580. }
  581. unsigned long long nr_periods_last = cp->nr_periods;
  582. unsigned long long nr_throttled_last = cp->nr_throttled;
  583. for (unsigned long i = 0; i < lines; i++) {
  584. char *s = procfile_lineword(ff, i, 0);
  585. uint32_t hash = simple_hash(s);
  586. if (unlikely(hash == nr_periods_hash && !strcmp(s, "nr_periods"))) {
  587. cp->nr_periods = str2ull(procfile_lineword(ff, i, 1), NULL);
  588. } else if (unlikely(hash == nr_throttled_hash && !strcmp(s, "nr_throttled"))) {
  589. cp->nr_throttled = str2ull(procfile_lineword(ff, i, 1), NULL);
  590. } else if (unlikely(hash == throttled_time_hash && !strcmp(s, "throttled_time"))) {
  591. cp->throttled_time = str2ull(procfile_lineword(ff, i, 1), NULL);
  592. }
  593. }
  594. cp->nr_throttled_perc =
  595. calc_percentage(calc_delta(cp->nr_throttled, nr_throttled_last), calc_delta(cp->nr_periods, nr_periods_last));
  596. cp->updated = 1;
  597. if (unlikely(cp->enabled == CONFIG_BOOLEAN_AUTO)) {
  598. if (likely(
  599. cp->nr_periods || cp->nr_throttled || cp->throttled_time ||
  600. netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES)) {
  601. cp->enabled = CONFIG_BOOLEAN_YES;
  602. }
  603. }
  604. }
  605. static inline void cgroup2_read_cpuacct_cpu_stat(struct cpuacct_stat *cp, struct cpuacct_cpu_throttling *cpt) {
  606. static procfile *ff = NULL;
  607. if (unlikely(!cp->filename)) {
  608. return;
  609. }
  610. ff = procfile_reopen(ff, cp->filename, NULL, CGROUP_PROCFILE_FLAG);
  611. if (unlikely(!ff)) {
  612. cp->updated = 0;
  613. cgroups_check = 1;
  614. return;
  615. }
  616. ff = procfile_readall(ff);
  617. if (unlikely(!ff)) {
  618. cp->updated = 0;
  619. cgroups_check = 1;
  620. return;
  621. }
  622. unsigned long lines = procfile_lines(ff);
  623. if (unlikely(lines < 3)) {
  624. collector_error("CGROUP: file '%s' should have at least 3 lines.", cp->filename);
  625. cp->updated = 0;
  626. return;
  627. }
  628. unsigned long long nr_periods_last = cpt->nr_periods;
  629. unsigned long long nr_throttled_last = cpt->nr_throttled;
  630. for (unsigned long i = 0; i < lines; i++) {
  631. char *s = procfile_lineword(ff, i, 0);
  632. uint32_t hash = simple_hash(s);
  633. if (unlikely(hash == user_usec_hash && !strcmp(s, "user_usec"))) {
  634. cp->user = str2ull(procfile_lineword(ff, i, 1), NULL);
  635. } else if (unlikely(hash == system_usec_hash && !strcmp(s, "system_usec"))) {
  636. cp->system = str2ull(procfile_lineword(ff, i, 1), NULL);
  637. } else if (unlikely(hash == nr_periods_hash && !strcmp(s, "nr_periods"))) {
  638. cpt->nr_periods = str2ull(procfile_lineword(ff, i, 1), NULL);
  639. } else if (unlikely(hash == nr_throttled_hash && !strcmp(s, "nr_throttled"))) {
  640. cpt->nr_throttled = str2ull(procfile_lineword(ff, i, 1), NULL);
  641. } else if (unlikely(hash == throttled_usec_hash && !strcmp(s, "throttled_usec"))) {
  642. cpt->throttled_time = str2ull(procfile_lineword(ff, i, 1), NULL) * 1000; // usec -> ns
  643. }
  644. }
  645. cpt->nr_throttled_perc =
  646. calc_percentage(calc_delta(cpt->nr_throttled, nr_throttled_last), calc_delta(cpt->nr_periods, nr_periods_last));
  647. cp->updated = 1;
  648. cpt->updated = 1;
  649. if (unlikely(cp->enabled == CONFIG_BOOLEAN_AUTO)) {
  650. if (likely(cp->user || cp->system || netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES)) {
  651. cp->enabled = CONFIG_BOOLEAN_YES;
  652. }
  653. }
  654. if (unlikely(cpt->enabled == CONFIG_BOOLEAN_AUTO)) {
  655. if (likely(
  656. cpt->nr_periods || cpt->nr_throttled || cpt->throttled_time ||
  657. netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES)) {
  658. cpt->enabled = CONFIG_BOOLEAN_YES;
  659. }
  660. }
  661. }
  662. static inline void cgroup_read_cpuacct_cpu_shares(struct cpuacct_cpu_shares *cp) {
  663. if (unlikely(!cp->filename)) {
  664. return;
  665. }
  666. if (unlikely(read_single_number_file(cp->filename, &cp->shares))) {
  667. cp->updated = 0;
  668. cgroups_check = 1;
  669. return;
  670. }
  671. cp->updated = 1;
  672. if (unlikely((cp->enabled == CONFIG_BOOLEAN_AUTO)) &&
  673. (cp->shares || netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES)) {
  674. cp->enabled = CONFIG_BOOLEAN_YES;
  675. }
  676. }
  677. static inline void cgroup_read_cpuacct_usage(struct cpuacct_usage *ca) {
  678. static procfile *ff = NULL;
  679. if(likely(ca->filename)) {
  680. ff = procfile_reopen(ff, ca->filename, NULL, CGROUP_PROCFILE_FLAG);
  681. if(unlikely(!ff)) {
  682. ca->updated = 0;
  683. cgroups_check = 1;
  684. return;
  685. }
  686. ff = procfile_readall(ff);
  687. if(unlikely(!ff)) {
  688. ca->updated = 0;
  689. cgroups_check = 1;
  690. return;
  691. }
  692. if(unlikely(procfile_lines(ff) < 1)) {
  693. collector_error("CGROUP: file '%s' should have 1+ lines but has %zu.", ca->filename, procfile_lines(ff));
  694. ca->updated = 0;
  695. return;
  696. }
  697. unsigned long i = procfile_linewords(ff, 0);
  698. if(unlikely(i == 0)) {
  699. ca->updated = 0;
  700. return;
  701. }
  702. // we may have 1 more CPU reported
  703. while(i > 0) {
  704. char *s = procfile_lineword(ff, 0, i - 1);
  705. if(!*s) i--;
  706. else break;
  707. }
  708. if(unlikely(i != ca->cpus)) {
  709. freez(ca->cpu_percpu);
  710. ca->cpu_percpu = mallocz(sizeof(unsigned long long) * i);
  711. ca->cpus = (unsigned int)i;
  712. }
  713. unsigned long long total = 0;
  714. for(i = 0; i < ca->cpus ;i++) {
  715. unsigned long long n = str2ull(procfile_lineword(ff, 0, i), NULL);
  716. ca->cpu_percpu[i] = n;
  717. total += n;
  718. }
  719. ca->updated = 1;
  720. if(unlikely(ca->enabled == CONFIG_BOOLEAN_AUTO &&
  721. (total || netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES)))
  722. ca->enabled = CONFIG_BOOLEAN_YES;
  723. }
  724. }
  725. static inline void cgroup_read_blkio(struct blkio *io) {
  726. if(unlikely(io->enabled == CONFIG_BOOLEAN_AUTO && io->delay_counter > 0)) {
  727. io->delay_counter--;
  728. return;
  729. }
  730. if(likely(io->filename)) {
  731. static procfile *ff = NULL;
  732. ff = procfile_reopen(ff, io->filename, NULL, CGROUP_PROCFILE_FLAG);
  733. if(unlikely(!ff)) {
  734. io->updated = 0;
  735. cgroups_check = 1;
  736. return;
  737. }
  738. ff = procfile_readall(ff);
  739. if(unlikely(!ff)) {
  740. io->updated = 0;
  741. cgroups_check = 1;
  742. return;
  743. }
  744. unsigned long i, lines = procfile_lines(ff);
  745. if(unlikely(lines < 1)) {
  746. collector_error("CGROUP: file '%s' should have 1+ lines.", io->filename);
  747. io->updated = 0;
  748. return;
  749. }
  750. io->Read = 0;
  751. io->Write = 0;
  752. /*
  753. io->Sync = 0;
  754. io->Async = 0;
  755. io->Total = 0;
  756. */
  757. for(i = 0; i < lines ; i++) {
  758. char *s = procfile_lineword(ff, i, 1);
  759. uint32_t hash = simple_hash(s);
  760. if(unlikely(hash == Read_hash && !strcmp(s, "Read")))
  761. io->Read += str2ull(procfile_lineword(ff, i, 2), NULL);
  762. else if(unlikely(hash == Write_hash && !strcmp(s, "Write")))
  763. io->Write += str2ull(procfile_lineword(ff, i, 2), NULL);
  764. /*
  765. else if(unlikely(hash == Sync_hash && !strcmp(s, "Sync")))
  766. io->Sync += str2ull(procfile_lineword(ff, i, 2));
  767. else if(unlikely(hash == Async_hash && !strcmp(s, "Async")))
  768. io->Async += str2ull(procfile_lineword(ff, i, 2));
  769. else if(unlikely(hash == Total_hash && !strcmp(s, "Total")))
  770. io->Total += str2ull(procfile_lineword(ff, i, 2));
  771. */
  772. }
  773. io->updated = 1;
  774. if(unlikely(io->enabled == CONFIG_BOOLEAN_AUTO)) {
  775. if(unlikely(io->Read || io->Write || netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))
  776. io->enabled = CONFIG_BOOLEAN_YES;
  777. else
  778. io->delay_counter = cgroup_recheck_zero_blkio_every_iterations;
  779. }
  780. }
  781. }
  782. static inline void cgroup2_read_blkio(struct blkio *io, unsigned int word_offset) {
  783. if(unlikely(io->enabled == CONFIG_BOOLEAN_AUTO && io->delay_counter > 0)) {
  784. io->delay_counter--;
  785. return;
  786. }
  787. if(likely(io->filename)) {
  788. static procfile *ff = NULL;
  789. ff = procfile_reopen(ff, io->filename, NULL, CGROUP_PROCFILE_FLAG);
  790. if(unlikely(!ff)) {
  791. io->updated = 0;
  792. cgroups_check = 1;
  793. return;
  794. }
  795. ff = procfile_readall(ff);
  796. if(unlikely(!ff)) {
  797. io->updated = 0;
  798. cgroups_check = 1;
  799. return;
  800. }
  801. unsigned long i, lines = procfile_lines(ff);
  802. if (unlikely(lines < 1)) {
  803. collector_error("CGROUP: file '%s' should have 1+ lines.", io->filename);
  804. io->updated = 0;
  805. return;
  806. }
  807. io->Read = 0;
  808. io->Write = 0;
  809. for (i = 0; i < lines; i++) {
  810. io->Read += str2ull(procfile_lineword(ff, i, 2 + word_offset), NULL);
  811. io->Write += str2ull(procfile_lineword(ff, i, 4 + word_offset), NULL);
  812. }
  813. io->updated = 1;
  814. if(unlikely(io->enabled == CONFIG_BOOLEAN_AUTO)) {
  815. if(unlikely(io->Read || io->Write || netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))
  816. io->enabled = CONFIG_BOOLEAN_YES;
  817. else
  818. io->delay_counter = cgroup_recheck_zero_blkio_every_iterations;
  819. }
  820. }
  821. }
  822. static inline void cgroup2_read_pressure(struct pressure *res) {
  823. static procfile *ff = NULL;
  824. if (likely(res->filename)) {
  825. ff = procfile_reopen(ff, res->filename, " =", CGROUP_PROCFILE_FLAG);
  826. if (unlikely(!ff)) {
  827. res->updated = 0;
  828. cgroups_check = 1;
  829. return;
  830. }
  831. ff = procfile_readall(ff);
  832. if (unlikely(!ff)) {
  833. res->updated = 0;
  834. cgroups_check = 1;
  835. return;
  836. }
  837. size_t lines = procfile_lines(ff);
  838. if (lines < 1) {
  839. collector_error("CGROUP: file '%s' should have 1+ lines.", res->filename);
  840. res->updated = 0;
  841. return;
  842. }
  843. bool did_some = false, did_full = false;
  844. for(size_t l = 0; l < lines ;l++) {
  845. const char *key = procfile_lineword(ff, l, 0);
  846. if(strcmp(key, "some") == 0) {
  847. res->some.share_time.value10 = strtod(procfile_lineword(ff, l, 2), NULL);
  848. res->some.share_time.value60 = strtod(procfile_lineword(ff, l, 4), NULL);
  849. res->some.share_time.value300 = strtod(procfile_lineword(ff, l, 6), NULL);
  850. res->some.total_time.value_total = str2ull(procfile_lineword(ff, l, 8), NULL) / 1000; // us->ms
  851. did_some = true;
  852. }
  853. else if(strcmp(key, "full") == 0) {
  854. res->full.share_time.value10 = strtod(procfile_lineword(ff, l, 2), NULL);
  855. res->full.share_time.value60 = strtod(procfile_lineword(ff, l, 4), NULL);
  856. res->full.share_time.value300 = strtod(procfile_lineword(ff, l, 6), NULL);
  857. res->full.total_time.value_total = str2ull(procfile_lineword(ff, l, 8), NULL) / 1000; // us->ms
  858. did_full = true;
  859. }
  860. }
  861. res->updated = (did_full || did_some) ? 1 : 0;
  862. if(unlikely(res->some.enabled == CONFIG_BOOLEAN_AUTO))
  863. res->some.enabled = (did_some) ? CONFIG_BOOLEAN_YES : CONFIG_BOOLEAN_NO;
  864. if(unlikely(res->full.enabled == CONFIG_BOOLEAN_AUTO))
  865. res->full.enabled = (did_full) ? CONFIG_BOOLEAN_YES : CONFIG_BOOLEAN_NO;
  866. }
  867. }
  868. static inline void cgroup_read_memory(struct memory *mem, char parent_cg_is_unified) {
  869. static procfile *ff = NULL;
  870. // read detailed ram usage
  871. if(likely(mem->filename_detailed)) {
  872. if(unlikely(mem->enabled_detailed == CONFIG_BOOLEAN_AUTO && mem->delay_counter_detailed > 0)) {
  873. mem->delay_counter_detailed--;
  874. goto memory_next;
  875. }
  876. ff = procfile_reopen(ff, mem->filename_detailed, NULL, CGROUP_PROCFILE_FLAG);
  877. if(unlikely(!ff)) {
  878. mem->updated_detailed = 0;
  879. cgroups_check = 1;
  880. goto memory_next;
  881. }
  882. ff = procfile_readall(ff);
  883. if(unlikely(!ff)) {
  884. mem->updated_detailed = 0;
  885. cgroups_check = 1;
  886. goto memory_next;
  887. }
  888. unsigned long i, lines = procfile_lines(ff);
  889. if(unlikely(lines < 1)) {
  890. collector_error("CGROUP: file '%s' should have 1+ lines.", mem->filename_detailed);
  891. mem->updated_detailed = 0;
  892. goto memory_next;
  893. }
  894. if(unlikely(!mem->arl_base)) {
  895. if(parent_cg_is_unified == 0){
  896. mem->arl_base = arl_create("cgroup/memory", NULL, 60);
  897. arl_expect(mem->arl_base, "total_cache", &mem->total_cache);
  898. arl_expect(mem->arl_base, "total_rss", &mem->total_rss);
  899. arl_expect(mem->arl_base, "total_rss_huge", &mem->total_rss_huge);
  900. arl_expect(mem->arl_base, "total_mapped_file", &mem->total_mapped_file);
  901. arl_expect(mem->arl_base, "total_writeback", &mem->total_writeback);
  902. mem->arl_dirty = arl_expect(mem->arl_base, "total_dirty", &mem->total_dirty);
  903. mem->arl_swap = arl_expect(mem->arl_base, "total_swap", &mem->total_swap);
  904. arl_expect(mem->arl_base, "total_pgpgin", &mem->total_pgpgin);
  905. arl_expect(mem->arl_base, "total_pgpgout", &mem->total_pgpgout);
  906. arl_expect(mem->arl_base, "total_pgfault", &mem->total_pgfault);
  907. arl_expect(mem->arl_base, "total_pgmajfault", &mem->total_pgmajfault);
  908. arl_expect(mem->arl_base, "total_inactive_file", &mem->total_inactive_file);
  909. } else {
  910. mem->arl_base = arl_create("cgroup/memory", NULL, 60);
  911. arl_expect(mem->arl_base, "anon", &mem->anon);
  912. arl_expect(mem->arl_base, "kernel_stack", &mem->kernel_stack);
  913. arl_expect(mem->arl_base, "slab", &mem->slab);
  914. arl_expect(mem->arl_base, "sock", &mem->sock);
  915. arl_expect(mem->arl_base, "anon_thp", &mem->anon_thp);
  916. arl_expect(mem->arl_base, "file", &mem->total_mapped_file);
  917. arl_expect(mem->arl_base, "file_writeback", &mem->total_writeback);
  918. mem->arl_dirty = arl_expect(mem->arl_base, "file_dirty", &mem->total_dirty);
  919. arl_expect(mem->arl_base, "pgfault", &mem->total_pgfault);
  920. arl_expect(mem->arl_base, "pgmajfault", &mem->total_pgmajfault);
  921. arl_expect(mem->arl_base, "inactive_file", &mem->total_inactive_file);
  922. }
  923. }
  924. arl_begin(mem->arl_base);
  925. for(i = 0; i < lines ; i++) {
  926. if(arl_check(mem->arl_base,
  927. procfile_lineword(ff, i, 0),
  928. procfile_lineword(ff, i, 1))) break;
  929. }
  930. if(unlikely(mem->arl_dirty->flags & ARL_ENTRY_FLAG_FOUND))
  931. mem->detailed_has_dirty = 1;
  932. if(unlikely(parent_cg_is_unified == 0 && mem->arl_swap->flags & ARL_ENTRY_FLAG_FOUND))
  933. mem->detailed_has_swap = 1;
  934. // 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);
  935. mem->updated_detailed = 1;
  936. if(unlikely(mem->enabled_detailed == CONFIG_BOOLEAN_AUTO)) {
  937. 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
  938. || mem->total_swap || mem->total_pgpgin || mem->total_pgpgout || mem->total_pgfault || mem->total_pgmajfault || mem->total_inactive_file))
  939. || (parent_cg_is_unified && ( mem->anon || mem->total_dirty || mem->kernel_stack || mem->slab || mem->sock || mem->total_writeback
  940. || mem->anon_thp || mem->total_pgfault || mem->total_pgmajfault || mem->total_inactive_file))
  941. || netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES)
  942. mem->enabled_detailed = CONFIG_BOOLEAN_YES;
  943. else
  944. mem->delay_counter_detailed = cgroup_recheck_zero_mem_detailed_every_iterations;
  945. }
  946. }
  947. memory_next:
  948. // read usage_in_bytes
  949. if(likely(mem->filename_usage_in_bytes)) {
  950. mem->updated_usage_in_bytes = !read_single_number_file(mem->filename_usage_in_bytes, &mem->usage_in_bytes);
  951. if(unlikely(mem->updated_usage_in_bytes && mem->enabled_usage_in_bytes == CONFIG_BOOLEAN_AUTO &&
  952. (mem->usage_in_bytes || netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES)))
  953. mem->enabled_usage_in_bytes = CONFIG_BOOLEAN_YES;
  954. }
  955. if (likely(mem->updated_usage_in_bytes && mem->updated_detailed)) {
  956. mem->usage_in_bytes =
  957. (mem->usage_in_bytes > mem->total_inactive_file) ? (mem->usage_in_bytes - mem->total_inactive_file) : 0;
  958. }
  959. // read msw_usage_in_bytes
  960. if(likely(mem->filename_msw_usage_in_bytes)) {
  961. mem->updated_msw_usage_in_bytes = !read_single_number_file(mem->filename_msw_usage_in_bytes, &mem->msw_usage_in_bytes);
  962. if(unlikely(mem->updated_msw_usage_in_bytes && mem->enabled_msw_usage_in_bytes == CONFIG_BOOLEAN_AUTO &&
  963. (mem->msw_usage_in_bytes || netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES)))
  964. mem->enabled_msw_usage_in_bytes = CONFIG_BOOLEAN_YES;
  965. }
  966. // read failcnt
  967. if(likely(mem->filename_failcnt)) {
  968. if(unlikely(mem->enabled_failcnt == CONFIG_BOOLEAN_AUTO && mem->delay_counter_failcnt > 0)) {
  969. mem->updated_failcnt = 0;
  970. mem->delay_counter_failcnt--;
  971. }
  972. else {
  973. mem->updated_failcnt = !read_single_number_file(mem->filename_failcnt, &mem->failcnt);
  974. if(unlikely(mem->updated_failcnt && mem->enabled_failcnt == CONFIG_BOOLEAN_AUTO)) {
  975. if(unlikely(mem->failcnt || netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))
  976. mem->enabled_failcnt = CONFIG_BOOLEAN_YES;
  977. else
  978. mem->delay_counter_failcnt = cgroup_recheck_zero_mem_failcnt_every_iterations;
  979. }
  980. }
  981. }
  982. }
  983. static void cgroup_read_pids_current(struct pids *pids) {
  984. pids->pids_current_updated = 0;
  985. if (unlikely(!pids->pids_current_filename))
  986. return;
  987. pids->pids_current_updated = !read_single_number_file(pids->pids_current_filename, &pids->pids_current);
  988. }
  989. static inline void read_cgroup(struct cgroup *cg) {
  990. netdata_log_debug(D_CGROUP, "reading metrics for cgroups '%s'", cg->id);
  991. if(!(cg->options & CGROUP_OPTIONS_IS_UNIFIED)) {
  992. cgroup_read_cpuacct_stat(&cg->cpuacct_stat);
  993. cgroup_read_cpuacct_usage(&cg->cpuacct_usage);
  994. cgroup_read_cpuacct_cpu_stat(&cg->cpuacct_cpu_throttling);
  995. cgroup_read_cpuacct_cpu_shares(&cg->cpuacct_cpu_shares);
  996. cgroup_read_memory(&cg->memory, 0);
  997. cgroup_read_blkio(&cg->io_service_bytes);
  998. cgroup_read_blkio(&cg->io_serviced);
  999. cgroup_read_blkio(&cg->throttle_io_service_bytes);
  1000. cgroup_read_blkio(&cg->throttle_io_serviced);
  1001. cgroup_read_blkio(&cg->io_merged);
  1002. cgroup_read_blkio(&cg->io_queued);
  1003. cgroup_read_pids_current(&cg->pids);
  1004. }
  1005. else {
  1006. //TODO: io_service_bytes and io_serviced use same file merge into 1 function
  1007. cgroup2_read_blkio(&cg->io_service_bytes, 0);
  1008. cgroup2_read_blkio(&cg->io_serviced, 4);
  1009. cgroup2_read_cpuacct_cpu_stat(&cg->cpuacct_stat, &cg->cpuacct_cpu_throttling);
  1010. cgroup_read_cpuacct_cpu_shares(&cg->cpuacct_cpu_shares);
  1011. cgroup2_read_pressure(&cg->cpu_pressure);
  1012. cgroup2_read_pressure(&cg->io_pressure);
  1013. cgroup2_read_pressure(&cg->memory_pressure);
  1014. cgroup2_read_pressure(&cg->irq_pressure);
  1015. cgroup_read_memory(&cg->memory, 1);
  1016. cgroup_read_pids_current(&cg->pids);
  1017. }
  1018. }
  1019. static inline void read_all_discovered_cgroups(struct cgroup *root) {
  1020. netdata_log_debug(D_CGROUP, "reading metrics for all cgroups");
  1021. struct cgroup *cg;
  1022. for (cg = root; cg; cg = cg->next) {
  1023. if (cg->enabled && !cg->pending_renames) {
  1024. read_cgroup(cg);
  1025. }
  1026. }
  1027. }
  1028. // update CPU and memory limits
  1029. static inline void update_cpu_limits(char **filename, unsigned long long *value, struct cgroup *cg) {
  1030. if(*filename) {
  1031. int ret = -1;
  1032. if(value == &cg->cpuset_cpus) {
  1033. unsigned long ncpus = read_cpuset_cpus(*filename, get_system_cpus());
  1034. if(ncpus) {
  1035. *value = ncpus;
  1036. ret = 0;
  1037. }
  1038. }
  1039. else if(value == &cg->cpu_cfs_period || value == &cg->cpu_cfs_quota) {
  1040. ret = read_single_number_file(*filename, value);
  1041. }
  1042. else ret = -1;
  1043. if(ret) {
  1044. collector_error("Cannot refresh cgroup %s cpu limit by reading '%s'. Will not update its limit anymore.", cg->id, *filename);
  1045. freez(*filename);
  1046. *filename = NULL;
  1047. }
  1048. }
  1049. }
  1050. static inline void update_cpu_limits2(struct cgroup *cg) {
  1051. if(cg->filename_cpu_cfs_quota){
  1052. static procfile *ff = NULL;
  1053. ff = procfile_reopen(ff, cg->filename_cpu_cfs_quota, NULL, CGROUP_PROCFILE_FLAG);
  1054. if(unlikely(!ff)) {
  1055. goto cpu_limits2_err;
  1056. }
  1057. ff = procfile_readall(ff);
  1058. if(unlikely(!ff)) {
  1059. goto cpu_limits2_err;
  1060. }
  1061. unsigned long lines = procfile_lines(ff);
  1062. if (unlikely(lines < 1)) {
  1063. collector_error("CGROUP: file '%s' should have 1 lines.", cg->filename_cpu_cfs_quota);
  1064. return;
  1065. }
  1066. cg->cpu_cfs_period = str2ull(procfile_lineword(ff, 0, 1), NULL);
  1067. cg->cpuset_cpus = get_system_cpus();
  1068. char *s = "max\n\0";
  1069. if(strcmp(s, procfile_lineword(ff, 0, 0)) == 0){
  1070. cg->cpu_cfs_quota = cg->cpu_cfs_period * cg->cpuset_cpus;
  1071. } else {
  1072. cg->cpu_cfs_quota = str2ull(procfile_lineword(ff, 0, 0), NULL);
  1073. }
  1074. netdata_log_debug(D_CGROUP, "CPU limits values: %llu %llu %llu", cg->cpu_cfs_period, cg->cpuset_cpus, cg->cpu_cfs_quota);
  1075. return;
  1076. cpu_limits2_err:
  1077. collector_error("Cannot refresh cgroup %s cpu limit by reading '%s'. Will not update its limit anymore.", cg->id, cg->filename_cpu_cfs_quota);
  1078. freez(cg->filename_cpu_cfs_quota);
  1079. cg->filename_cpu_cfs_quota = NULL;
  1080. }
  1081. }
  1082. static inline int update_memory_limits(struct cgroup *cg) {
  1083. char **filename = &cg->filename_memory_limit;
  1084. const RRDSETVAR_ACQUIRED **chart_var = &cg->chart_var_memory_limit;
  1085. unsigned long long *value = &cg->memory_limit;
  1086. if(*filename) {
  1087. if(unlikely(!*chart_var)) {
  1088. *chart_var = rrdsetvar_custom_chart_variable_add_and_acquire(cg->st_mem_usage, "memory_limit");
  1089. if(!*chart_var) {
  1090. collector_error("Cannot create cgroup %s chart variable '%s'. Will not update its limit anymore.", cg->id, "memory_limit");
  1091. freez(*filename);
  1092. *filename = NULL;
  1093. }
  1094. }
  1095. if(*filename && *chart_var) {
  1096. if(!(cg->options & CGROUP_OPTIONS_IS_UNIFIED)) {
  1097. if(read_single_number_file(*filename, value)) {
  1098. collector_error("Cannot refresh cgroup %s memory limit by reading '%s'. Will not update its limit anymore.", cg->id, *filename);
  1099. freez(*filename);
  1100. *filename = NULL;
  1101. }
  1102. else {
  1103. rrdsetvar_custom_chart_variable_set(cg->st_mem_usage, *chart_var, (NETDATA_DOUBLE)(*value) / (1024.0 * 1024.0));
  1104. return 1;
  1105. }
  1106. } else {
  1107. char buffer[30 + 1];
  1108. int ret = read_file(*filename, buffer, 30);
  1109. if(ret) {
  1110. collector_error("Cannot refresh cgroup %s memory limit by reading '%s'. Will not update its limit anymore.", cg->id, *filename);
  1111. freez(*filename);
  1112. *filename = NULL;
  1113. return 0;
  1114. }
  1115. char *s = "max\n\0";
  1116. if(strcmp(s, buffer) == 0){
  1117. *value = UINT64_MAX;
  1118. rrdsetvar_custom_chart_variable_set(cg->st_mem_usage, *chart_var, (NETDATA_DOUBLE)(*value) / (1024.0 * 1024.0));
  1119. return 1;
  1120. }
  1121. *value = str2ull(buffer, NULL);
  1122. rrdsetvar_custom_chart_variable_set(cg->st_mem_usage, *chart_var, (NETDATA_DOUBLE)(*value) / (1024.0 * 1024.0));
  1123. return 1;
  1124. }
  1125. }
  1126. }
  1127. return 0;
  1128. }
  1129. // ----------------------------------------------------------------------------
  1130. // generate charts
  1131. void update_cgroup_systemd_services_charts() {
  1132. for (struct cgroup *cg = cgroup_root; cg; cg = cg->next) {
  1133. if (unlikely(!cg->enabled || cg->pending_renames || !is_cgroup_systemd_service(cg)))
  1134. continue;
  1135. if (likely(cg->cpuacct_stat.updated)) {
  1136. update_cpu_utilization_chart(cg);
  1137. }
  1138. if (likely(cg->memory.updated_msw_usage_in_bytes)) {
  1139. update_mem_usage_chart(cg);
  1140. }
  1141. if (likely(cg->memory.updated_failcnt)) {
  1142. update_mem_failcnt_chart(cg);
  1143. }
  1144. if (likely(cg->memory.updated_detailed)) {
  1145. update_mem_usage_detailed_chart(cg);
  1146. update_mem_writeback_chart(cg);
  1147. update_mem_pgfaults_chart(cg);
  1148. if (!(cg->options & CGROUP_OPTIONS_IS_UNIFIED)) {
  1149. update_mem_activity_chart(cg);
  1150. }
  1151. }
  1152. if (likely(cg->io_service_bytes.updated)) {
  1153. update_io_serviced_bytes_chart(cg);
  1154. }
  1155. if (likely(cg->io_serviced.updated)) {
  1156. update_io_serviced_ops_chart(cg);
  1157. }
  1158. if (likely(cg->throttle_io_service_bytes.updated)) {
  1159. update_throttle_io_serviced_bytes_chart(cg);
  1160. }
  1161. if (likely(cg->throttle_io_serviced.updated)) {
  1162. update_throttle_io_serviced_ops_chart(cg);
  1163. }
  1164. if (likely(cg->io_queued.updated)) {
  1165. update_io_queued_ops_chart(cg);
  1166. }
  1167. if (likely(cg->io_merged.updated)) {
  1168. update_io_merged_ops_chart(cg);
  1169. }
  1170. if (likely(cg->pids.pids_current_updated)) {
  1171. update_pids_current_chart(cg);
  1172. }
  1173. cg->function_ready = true;
  1174. }
  1175. }
  1176. void update_cgroup_charts() {
  1177. for (struct cgroup *cg = cgroup_root; cg; cg = cg->next) {
  1178. if(unlikely(!cg->enabled || cg->pending_renames || is_cgroup_systemd_service(cg)))
  1179. continue;
  1180. if (likely(cg->cpuacct_stat.updated && cg->cpuacct_stat.enabled == CONFIG_BOOLEAN_YES)) {
  1181. update_cpu_utilization_chart(cg);
  1182. if(likely(cg->filename_cpuset_cpus || cg->filename_cpu_cfs_period || cg->filename_cpu_cfs_quota)) {
  1183. if(!(cg->options & CGROUP_OPTIONS_IS_UNIFIED)) {
  1184. update_cpu_limits(&cg->filename_cpuset_cpus, &cg->cpuset_cpus, cg);
  1185. update_cpu_limits(&cg->filename_cpu_cfs_period, &cg->cpu_cfs_period, cg);
  1186. update_cpu_limits(&cg->filename_cpu_cfs_quota, &cg->cpu_cfs_quota, cg);
  1187. } else {
  1188. update_cpu_limits2(cg);
  1189. }
  1190. if(unlikely(!cg->chart_var_cpu_limit)) {
  1191. cg->chart_var_cpu_limit = rrdsetvar_custom_chart_variable_add_and_acquire(cg->st_cpu, "cpu_limit");
  1192. if(!cg->chart_var_cpu_limit) {
  1193. collector_error("Cannot create cgroup %s chart variable 'cpu_limit'. Will not update its limit anymore.", cg->id);
  1194. if(cg->filename_cpuset_cpus) freez(cg->filename_cpuset_cpus);
  1195. cg->filename_cpuset_cpus = NULL;
  1196. if(cg->filename_cpu_cfs_period) freez(cg->filename_cpu_cfs_period);
  1197. cg->filename_cpu_cfs_period = NULL;
  1198. if(cg->filename_cpu_cfs_quota) freez(cg->filename_cpu_cfs_quota);
  1199. cg->filename_cpu_cfs_quota = NULL;
  1200. }
  1201. } else {
  1202. NETDATA_DOUBLE value = 0, quota = 0;
  1203. if(likely( ((!(cg->options & CGROUP_OPTIONS_IS_UNIFIED)) && (cg->filename_cpuset_cpus || (cg->filename_cpu_cfs_period && cg->filename_cpu_cfs_quota)))
  1204. || ((cg->options & CGROUP_OPTIONS_IS_UNIFIED) && cg->filename_cpu_cfs_quota))) {
  1205. if(unlikely(cg->cpu_cfs_quota > 0))
  1206. quota = (NETDATA_DOUBLE)cg->cpu_cfs_quota / (NETDATA_DOUBLE)cg->cpu_cfs_period;
  1207. if(unlikely(quota > 0 && quota < cg->cpuset_cpus))
  1208. value = quota * 100;
  1209. else
  1210. value = (NETDATA_DOUBLE)cg->cpuset_cpus * 100;
  1211. }
  1212. if(likely(value)) {
  1213. update_cpu_utilization_limit_chart(cg, value);
  1214. } else {
  1215. if (unlikely(cg->st_cpu_limit)) {
  1216. rrdset_is_obsolete___safe_from_collector_thread(cg->st_cpu_limit);
  1217. cg->st_cpu_limit = NULL;
  1218. }
  1219. rrdsetvar_custom_chart_variable_set(cg->st_cpu, cg->chart_var_cpu_limit, NAN);
  1220. }
  1221. }
  1222. }
  1223. }
  1224. if (likely(cg->cpuacct_cpu_throttling.updated && cg->cpuacct_cpu_throttling.enabled == CONFIG_BOOLEAN_YES)) {
  1225. update_cpu_throttled_chart(cg);
  1226. update_cpu_throttled_duration_chart(cg);
  1227. }
  1228. if (likely(cg->cpuacct_cpu_shares.updated && cg->cpuacct_cpu_shares.enabled == CONFIG_BOOLEAN_YES)) {
  1229. update_cpu_shares_chart(cg);
  1230. }
  1231. if (likely(cg->cpuacct_usage.updated && cg->cpuacct_usage.enabled == CONFIG_BOOLEAN_YES)) {
  1232. update_cpu_per_core_usage_chart(cg);
  1233. }
  1234. if (likely(cg->memory.updated_detailed && cg->memory.enabled_detailed == CONFIG_BOOLEAN_YES)) {
  1235. update_mem_usage_detailed_chart(cg);
  1236. update_mem_writeback_chart(cg);
  1237. if(!(cg->options & CGROUP_OPTIONS_IS_UNIFIED)) {
  1238. update_mem_activity_chart(cg);
  1239. }
  1240. update_mem_pgfaults_chart(cg);
  1241. }
  1242. if (likely(cg->memory.updated_usage_in_bytes && cg->memory.enabled_usage_in_bytes == CONFIG_BOOLEAN_YES)) {
  1243. update_mem_usage_chart(cg);
  1244. // FIXME: this if should be only for unlimited charts
  1245. if(likely(host_ram_total)) {
  1246. // FIXME: do we need to update mem limits on every data collection?
  1247. if (likely(update_memory_limits(cg))) {
  1248. unsigned long long memory_limit = host_ram_total;
  1249. if (unlikely(cg->memory_limit < host_ram_total))
  1250. memory_limit = cg->memory_limit;
  1251. update_mem_usage_limit_chart(cg, memory_limit);
  1252. update_mem_utilization_chart(cg, memory_limit);
  1253. } else {
  1254. if (unlikely(cg->st_mem_usage_limit)) {
  1255. rrdset_is_obsolete___safe_from_collector_thread(cg->st_mem_usage_limit);
  1256. cg->st_mem_usage_limit = NULL;
  1257. }
  1258. if (unlikely(cg->st_mem_utilization)) {
  1259. rrdset_is_obsolete___safe_from_collector_thread(cg->st_mem_utilization);
  1260. cg->st_mem_utilization = NULL;
  1261. }
  1262. }
  1263. }
  1264. }
  1265. if (likely(cg->memory.updated_failcnt && cg->memory.enabled_failcnt == CONFIG_BOOLEAN_YES)) {
  1266. update_mem_failcnt_chart(cg);
  1267. }
  1268. if (likely(cg->io_service_bytes.updated && cg->io_service_bytes.enabled == CONFIG_BOOLEAN_YES)) {
  1269. update_io_serviced_bytes_chart(cg);
  1270. }
  1271. if (likely(cg->io_serviced.updated && cg->io_serviced.enabled == CONFIG_BOOLEAN_YES)) {
  1272. update_io_serviced_ops_chart(cg);
  1273. }
  1274. if (likely(cg->throttle_io_service_bytes.updated && cg->throttle_io_service_bytes.enabled == CONFIG_BOOLEAN_YES)) {
  1275. update_throttle_io_serviced_bytes_chart(cg);
  1276. }
  1277. if (likely(cg->throttle_io_serviced.updated && cg->throttle_io_serviced.enabled == CONFIG_BOOLEAN_YES)) {
  1278. update_throttle_io_serviced_ops_chart(cg);
  1279. }
  1280. if (likely(cg->io_queued.updated && cg->io_queued.enabled == CONFIG_BOOLEAN_YES)) {
  1281. update_io_queued_ops_chart(cg);
  1282. }
  1283. if (likely(cg->io_merged.updated && cg->io_merged.enabled == CONFIG_BOOLEAN_YES)) {
  1284. update_io_merged_ops_chart(cg);
  1285. }
  1286. if (likely(cg->pids.pids_current_updated)) {
  1287. update_pids_current_chart(cg);
  1288. }
  1289. if (cg->options & CGROUP_OPTIONS_IS_UNIFIED) {
  1290. if (likely(cg->cpu_pressure.updated)) {
  1291. if (cg->cpu_pressure.some.enabled) {
  1292. update_cpu_some_pressure_chart(cg);
  1293. update_cpu_some_pressure_stall_time_chart(cg);
  1294. }
  1295. if (cg->cpu_pressure.full.enabled) {
  1296. update_cpu_full_pressure_chart(cg);
  1297. update_cpu_full_pressure_stall_time_chart(cg);
  1298. }
  1299. }
  1300. if (likely(cg->memory_pressure.updated)) {
  1301. if (cg->memory_pressure.some.enabled) {
  1302. update_mem_some_pressure_chart(cg);
  1303. update_mem_some_pressure_stall_time_chart(cg);
  1304. }
  1305. if (cg->memory_pressure.full.enabled) {
  1306. update_mem_full_pressure_chart(cg);
  1307. update_mem_full_pressure_stall_time_chart(cg);
  1308. }
  1309. }
  1310. if (likely(cg->irq_pressure.updated)) {
  1311. if (cg->irq_pressure.some.enabled) {
  1312. update_irq_some_pressure_chart(cg);
  1313. update_irq_some_pressure_stall_time_chart(cg);
  1314. }
  1315. if (cg->irq_pressure.full.enabled) {
  1316. update_irq_full_pressure_chart(cg);
  1317. update_irq_full_pressure_stall_time_chart(cg);
  1318. }
  1319. }
  1320. if (likely(cg->io_pressure.updated)) {
  1321. if (cg->io_pressure.some.enabled) {
  1322. update_io_some_pressure_chart(cg);
  1323. update_io_some_pressure_stall_time_chart(cg);
  1324. }
  1325. if (cg->io_pressure.full.enabled) {
  1326. update_io_full_pressure_chart(cg);
  1327. update_io_full_pressure_stall_time_chart(cg);
  1328. }
  1329. }
  1330. }
  1331. cg->function_ready = true;
  1332. }
  1333. }
  1334. // ----------------------------------------------------------------------------
  1335. // cgroups main
  1336. static void cgroup_main_cleanup(void *ptr) {
  1337. worker_unregister();
  1338. struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
  1339. static_thread->enabled = NETDATA_MAIN_THREAD_EXITING;
  1340. collector_info("cleaning up...");
  1341. usec_t max = 2 * USEC_PER_SEC, step = 50000;
  1342. if (!__atomic_load_n(&discovery_thread.exited, __ATOMIC_RELAXED)) {
  1343. collector_info("waiting for discovery thread to finish...");
  1344. while (!__atomic_load_n(&discovery_thread.exited, __ATOMIC_RELAXED) && max > 0) {
  1345. uv_mutex_lock(&discovery_thread.mutex);
  1346. uv_cond_signal(&discovery_thread.cond_var);
  1347. uv_mutex_unlock(&discovery_thread.mutex);
  1348. max -= step;
  1349. sleep_usec(step);
  1350. }
  1351. }
  1352. if (shm_mutex_cgroup_ebpf != SEM_FAILED) {
  1353. sem_close(shm_mutex_cgroup_ebpf);
  1354. }
  1355. if (shm_cgroup_ebpf.header) {
  1356. shm_cgroup_ebpf.header->cgroup_root_count = 0;
  1357. munmap(shm_cgroup_ebpf.header, shm_cgroup_ebpf.header->body_length);
  1358. }
  1359. if (shm_fd_cgroup_ebpf > 0) {
  1360. close(shm_fd_cgroup_ebpf);
  1361. }
  1362. static_thread->enabled = NETDATA_MAIN_THREAD_EXITED;
  1363. }
  1364. void cgroup_read_host_total_ram() {
  1365. procfile *ff = NULL;
  1366. char filename[FILENAME_MAX + 1];
  1367. snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/proc/meminfo");
  1368. ff = procfile_open(
  1369. config_get("plugin:cgroups", "meminfo filename to monitor", filename), " \t:", PROCFILE_FLAG_DEFAULT);
  1370. if (likely((ff = procfile_readall(ff)) && procfile_lines(ff) && !strncmp(procfile_word(ff, 0), "MemTotal", 8)))
  1371. host_ram_total = str2ull(procfile_word(ff, 1), NULL) * 1024;
  1372. else
  1373. collector_error("Cannot read file %s. Will not create RAM limit charts.", filename);
  1374. procfile_close(ff);
  1375. }
  1376. void *cgroups_main(void *ptr) {
  1377. worker_register("CGROUPS");
  1378. worker_register_job_name(WORKER_CGROUPS_LOCK, "lock");
  1379. worker_register_job_name(WORKER_CGROUPS_READ, "read");
  1380. worker_register_job_name(WORKER_CGROUPS_CHART, "chart");
  1381. netdata_thread_cleanup_push(cgroup_main_cleanup, ptr);
  1382. if (getenv("KUBERNETES_SERVICE_HOST") != NULL && getenv("KUBERNETES_SERVICE_PORT") != NULL) {
  1383. is_inside_k8s = 1;
  1384. cgroup_enable_cpuacct_cpu_shares = CONFIG_BOOLEAN_YES;
  1385. }
  1386. read_cgroup_plugin_configuration();
  1387. cgroup_read_host_total_ram();
  1388. netdata_cgroup_ebpf_initialize_shm();
  1389. if (uv_mutex_init(&cgroup_root_mutex)) {
  1390. collector_error("CGROUP: cannot initialize mutex for the main cgroup list");
  1391. goto exit;
  1392. }
  1393. discovery_thread.exited = 0;
  1394. if (uv_mutex_init(&discovery_thread.mutex)) {
  1395. collector_error("CGROUP: cannot initialize mutex for discovery thread");
  1396. goto exit;
  1397. }
  1398. if (uv_cond_init(&discovery_thread.cond_var)) {
  1399. collector_error("CGROUP: cannot initialize conditional variable for discovery thread");
  1400. goto exit;
  1401. }
  1402. int error = uv_thread_create(&discovery_thread.thread, cgroup_discovery_worker, NULL);
  1403. if (error) {
  1404. collector_error("CGROUP: cannot create thread worker. uv_thread_create(): %s", uv_strerror(error));
  1405. goto exit;
  1406. }
  1407. uv_thread_set_name_np(discovery_thread.thread, "P[cgroups]");
  1408. // we register this only on localhost
  1409. // for the other nodes, the origin server should register it
  1410. cgroup_netdev_link_init();
  1411. rrd_function_add_inline(localhost, NULL, "containers-vms", 10,
  1412. RRDFUNCTIONS_PRIORITY_DEFAULT / 2, RRDFUNCTIONS_CGTOP_HELP,
  1413. "top", HTTP_ACCESS_ANY, cgroup_function_cgroup_top);
  1414. rrd_function_add_inline(localhost, NULL, "systemd-services", 10,
  1415. RRDFUNCTIONS_PRIORITY_DEFAULT / 3, RRDFUNCTIONS_SYSTEMD_SERVICES_HELP,
  1416. "top", HTTP_ACCESS_ANY, cgroup_function_systemd_top);
  1417. heartbeat_t hb;
  1418. heartbeat_init(&hb);
  1419. usec_t step = cgroup_update_every * USEC_PER_SEC;
  1420. usec_t find_every = cgroup_check_for_new_every * USEC_PER_SEC, find_dt = 0;
  1421. netdata_thread_disable_cancelability();
  1422. while(service_running(SERVICE_COLLECTORS)) {
  1423. worker_is_idle();
  1424. usec_t hb_dt = heartbeat_next(&hb, step);
  1425. if (unlikely(!service_running(SERVICE_COLLECTORS)))
  1426. break;
  1427. find_dt += hb_dt;
  1428. if (unlikely(find_dt >= find_every || (!is_inside_k8s && cgroups_check))) {
  1429. uv_mutex_lock(&discovery_thread.mutex);
  1430. uv_cond_signal(&discovery_thread.cond_var);
  1431. uv_mutex_unlock(&discovery_thread.mutex);
  1432. find_dt = 0;
  1433. cgroups_check = 0;
  1434. }
  1435. worker_is_busy(WORKER_CGROUPS_LOCK);
  1436. uv_mutex_lock(&cgroup_root_mutex);
  1437. worker_is_busy(WORKER_CGROUPS_READ);
  1438. read_all_discovered_cgroups(cgroup_root);
  1439. if (unlikely(!service_running(SERVICE_COLLECTORS))) {
  1440. uv_mutex_unlock(&cgroup_root_mutex);
  1441. break;
  1442. }
  1443. worker_is_busy(WORKER_CGROUPS_CHART);
  1444. update_cgroup_charts();
  1445. if (cgroup_enable_systemd_services)
  1446. update_cgroup_systemd_services_charts();
  1447. if (unlikely(!service_running(SERVICE_COLLECTORS))) {
  1448. uv_mutex_unlock(&cgroup_root_mutex);
  1449. break;
  1450. }
  1451. worker_is_idle();
  1452. uv_mutex_unlock(&cgroup_root_mutex);
  1453. }
  1454. exit:
  1455. netdata_thread_cleanup_pop(1);
  1456. return NULL;
  1457. }