| 1 | #include "sys_fs_cgroup.h" |
| 2 | |
| 3 | #ifndef NETDATA_CGROUP_INTERNALS_H |
| 4 | #define NETDATA_CGROUP_INTERNALS_H 1 |
| 5 | |
| 6 | #ifdef NETDATA_INTERNAL_CHECKS |
| 7 | #define CGROUP_PROCFILE_FLAG PROCFILE_FLAG_DEFAULT |
| 8 | #else |
| 9 | #define CGROUP_PROCFILE_FLAG PROCFILE_FLAG_NO_ERROR_ON_FILE_IO |
| 10 | #endif |
| 11 | |
| 12 | struct blkio { |
| 13 | char *filename; |
| 14 | bool staterr; |
| 15 | |
| 16 | int updated; |
| 17 | |
| 18 | unsigned long long Read; |
| 19 | unsigned long long Write; |
| 20 | }; |
| 21 | |
| 22 | struct pids { |
| 23 | char *filename; |
| 24 | bool staterr; |
| 25 | |
| 26 | int updated; |
| 27 | |
| 28 | unsigned long long pids_current; |
| 29 | }; |
| 30 | |
| 31 | // https://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt |
| 32 | struct memory { |
| 33 | ARL_BASE *arl_base; |
| 34 | ARL_ENTRY *arl_dirty; |
| 35 | ARL_ENTRY *arl_swap; |
| 36 | |
| 37 | char *filename_usage_in_bytes; |
| 38 | char *filename_detailed; |
| 39 | char *filename_msw_usage_in_bytes; |
| 40 | char *filename_failcnt; |
| 41 | |
| 42 | bool staterr_mem_current; |
| 43 | bool staterr_mem_stat; |
| 44 | bool staterr_failcnt; |
| 45 | bool staterr_swap; |
| 46 | |
| 47 | int updated_usage_in_bytes; |
| 48 | int updated_detailed; |
| 49 | int updated_msw_usage_in_bytes; |
| 50 | int updated_failcnt; |
| 51 | |
| 52 | int detailed_has_dirty; |
| 53 | int detailed_has_swap; |
| 54 | |
| 55 | unsigned long long anon; |
| 56 | unsigned long long kernel_stack; |
| 57 | unsigned long long slab; |
| 58 | unsigned long long sock; |
| 59 | unsigned long long anon_thp; |
| 60 | |
| 61 | unsigned long long total_cache; |
| 62 | unsigned long long total_rss; |
| 63 | unsigned long long total_rss_huge; |
| 64 | unsigned long long total_mapped_file; |
| 65 | unsigned long long total_writeback; |
| 66 | unsigned long long total_dirty; |
| 67 | unsigned long long total_swap; |
| 68 | unsigned long long total_pgpgin; |
| 69 | unsigned long long total_pgpgout; |
| 70 | unsigned long long total_pgfault; |
| 71 | unsigned long long total_pgmajfault; |
| 72 | |
| 73 | unsigned long long total_inactive_file; |
| 74 | |
| 75 | // single file metrics |
| 76 | unsigned long long usage_in_bytes; |
| 77 | unsigned long long msw_usage_in_bytes; |
| 78 | unsigned long long failcnt; |
| 79 | }; |
| 80 | |
| 81 | // https://www.kernel.org/doc/Documentation/cgroup-v1/cpuacct.txt |
| 82 | struct cpuacct_stat { |
| 83 | char *filename; |
| 84 | bool staterr; |
| 85 | |
| 86 | int updated; |
| 87 | |
| 88 | unsigned long long user; // v1, v2(user_usec) |
| 89 | unsigned long long system; // v1, v2(system_usec) |
| 90 | }; |
| 91 | |
| 92 | // https://www.kernel.org/doc/Documentation/cgroup-v1/cpuacct.txt |
| 93 | struct cpuacct_usage { |
| 94 | char *filename; |
| 95 | bool disabled; |
| 96 | int updated; |
| 97 | |
| 98 | unsigned int cpus; |
| 99 | unsigned long long *cpu_percpu; |
| 100 | }; |
| 101 | |
| 102 | // represents cpuacct/cpu.stat, for v2 'cpuacct_stat' is used for 'user_usec', 'system_usec' |
| 103 | struct cpuacct_cpu_throttling { |
| 104 | char *filename; |
| 105 | bool staterr; |
| 106 | |
| 107 | int updated; |
| 108 | |
| 109 | unsigned long long nr_periods; |
| 110 | unsigned long long nr_throttled; |
| 111 | unsigned long long throttled_time; |
| 112 | |
| 113 | unsigned long long nr_throttled_perc; |
| 114 | }; |
| 115 | |
| 116 | // https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/resource_management_guide/sec-cpu#sect-cfs |
| 117 | // https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/managing_monitoring_and_updating_the_kernel/using-cgroups-v2-to-control-distribution-of-cpu-time-for-applications_managing-monitoring-and-updating-the-kernel#proc_controlling-distribution-of-cpu-time-for-applications-by-adjusting-cpu-weight_using-cgroups-v2-to-control-distribution-of-cpu-time-for-applications |
| 118 | struct cpuacct_cpu_shares { |
| 119 | char *filename; |
| 120 | bool staterr; |
| 121 | |
| 122 | int updated; |
| 123 | |
| 124 | unsigned long long shares; |
| 125 | }; |
| 126 | |
| 127 | struct cgroup_network_interface { |
| 128 | const char *host_device; |
| 129 | const char *container_device; |
| 130 | struct cgroup_network_interface *next; |
| 131 | }; |
| 132 | |
| 133 | enum cgroups_container_orchestrator { |
| 134 | CGROUPS_ORCHESTRATOR_UNSET, |
| 135 | CGROUPS_ORCHESTRATOR_UNKNOWN, |
| 136 | CGROUPS_ORCHESTRATOR_K8S |
| 137 | }; |
| 138 | |
| 139 | |
| 140 | // *** WARNING *** The fields are not thread safe. Take care of safe usage. |
| 141 | struct cgroup { |
| 142 | uint32_t options; |
| 143 | |
| 144 | int first_time_seen; // first time seen by the discoverer |
| 145 | int processed; // the discoverer is done processing a cgroup (resolved name, set 'enabled' option) |
| 146 | |
| 147 | char available; // found in the filesystem |
| 148 | char enabled; // enabled in the config |
| 149 | |
| 150 | bool function_ready; // true after the first iteration of chart creation/update |
| 151 | |
| 152 | char pending_renames; |
| 153 | |
| 154 | char *id; |
| 155 | uint32_t hash; |
| 156 | |
| 157 | char *intermediate_id; // TODO: remove it when the renaming script is fixed |
| 158 | |
| 159 | char *chart_id; |
| 160 | uint32_t hash_chart_id; |
| 161 | |
| 162 | // 'cgroup_name' label value. |
| 163 | // by default this is the *id (path), later changed to the resolved name (cgroup-name.sh) or systemd service name. |
| 164 | char *name; |
| 165 | |
| 166 | RRDLABELS *chart_labels; |
| 167 | |
| 168 | int container_orchestrator; |
| 169 | |
| 170 | struct cpuacct_stat cpuacct_stat; |
| 171 | struct cpuacct_usage cpuacct_usage; |
| 172 | struct cpuacct_cpu_throttling cpuacct_cpu_throttling; |
| 173 | struct cpuacct_cpu_shares cpuacct_cpu_shares; |
| 174 | |
| 175 | struct memory memory; |
| 176 | |
| 177 | struct blkio io_service_bytes; // bytes |
| 178 | struct blkio io_serviced; // operations |
| 179 | |
| 180 | struct blkio throttle_io_service_bytes; // bytes |
| 181 | struct blkio throttle_io_serviced; // operations |
| 182 | |
| 183 | struct blkio io_merged; // operations |
| 184 | struct blkio io_queued; // operations |
| 185 | |
| 186 | struct pids pids_current; |
| 187 | |
| 188 | struct cgroup_network_interface *interfaces; |
| 189 | |
| 190 | struct pressure cpu_pressure; |
| 191 | struct pressure io_pressure; |
| 192 | struct pressure memory_pressure; |
| 193 | struct pressure irq_pressure; |
| 194 | |
| 195 | // Cpu |
| 196 | RRDSET *st_cpu; |
| 197 | RRDDIM *st_cpu_rd_user; |
| 198 | RRDDIM *st_cpu_rd_system; |
| 199 | |
| 200 | RRDSET *st_cpu_limit; |
| 201 | RRDSET *st_cpu_per_core; |
| 202 | RRDSET *st_cpu_nr_throttled; |
| 203 | RRDSET *st_cpu_throttled_time; |
| 204 | RRDSET *st_cpu_shares; |
| 205 | |
| 206 | // Memory |
| 207 | RRDSET *st_mem; |
| 208 | RRDDIM *st_mem_rd_ram; |
| 209 | RRDDIM *st_mem_rd_swap; |
| 210 | |
| 211 | RRDSET *st_mem_utilization; |
| 212 | RRDSET *st_writeback; |
| 213 | RRDSET *st_mem_activity; |
| 214 | RRDSET *st_pgfaults; |
| 215 | RRDSET *st_mem_usage; |
| 216 | RRDSET *st_mem_usage_limit; |
| 217 | RRDSET *st_mem_failcnt; |
| 218 | |
| 219 | // Blkio |
| 220 | RRDSET *st_io; |
| 221 | RRDDIM *st_io_rd_read; |
| 222 | RRDDIM *st_io_rd_written; |
| 223 | |
| 224 | RRDSET *st_serviced_ops; |
| 225 | |
| 226 | RRDSET *st_throttle_io; |
| 227 | RRDDIM *st_throttle_io_rd_read; |
| 228 | RRDDIM *st_throttle_io_rd_written; |
| 229 | |
| 230 | RRDSET *st_throttle_serviced_ops; |
| 231 | |
| 232 | RRDSET *st_queued_ops; |
| 233 | RRDSET *st_merged_ops; |
| 234 | |
| 235 | // Pids |
| 236 | RRDSET *st_pids; |
| 237 | RRDDIM *st_pids_rd_pids_current; |
| 238 | |
| 239 | // per cgroup chart variables |
| 240 | char *filename_cpuset_cpus; |
| 241 | unsigned long long cpuset_cpus; |
| 242 | |
| 243 | char *filename_cpu_cfs_period; |
| 244 | unsigned long long cpu_cfs_period; |
| 245 | |
| 246 | char *filename_cpu_cfs_quota; |
| 247 | unsigned long long cpu_cfs_quota; |
| 248 | |
| 249 | const RRDVAR_ACQUIRED *chart_var_cpu_limit; |
| 250 | NETDATA_DOUBLE prev_cpu_usage; |
| 251 | |
| 252 | char *filename_memory_limit; |
| 253 | unsigned long long memory_limit; |
| 254 | const RRDVAR_ACQUIRED *chart_var_memory_limit; |
| 255 | |
| 256 | const DICTIONARY_ITEM *cgroup_netdev_link; |
| 257 | |
| 258 | struct cgroup *next; |
| 259 | struct cgroup *discovered_next; |
| 260 | |
| 261 | }; |
| 262 | |
| 263 | struct discovery_thread { |
| 264 | ND_THREAD *thread; |
| 265 | netdata_mutex_t mutex; |
| 266 | netdata_cond_t cond_var; |
| 267 | int exited; |
| 268 | }; |
| 269 | |
| 270 | extern struct discovery_thread discovery_thread; |
| 271 | |
| 272 | extern const char *cgroups_rename_script; |
| 273 | extern char cgroup_chart_id_prefix[]; |
| 274 | extern char services_chart_id_prefix[]; |
| 275 | extern netdata_mutex_t cgroup_root_mutex; |
| 276 | |
| 277 | void cgroup_discovery_worker(void *ptr); |
| 278 | |
| 279 | extern bool is_inside_k8s; |
| 280 | extern long system_page_size; |
| 281 | |
| 282 | extern int cgroup_use_unified_cgroups; |
| 283 | extern bool cgroup_unified_exist; |
| 284 | |
| 285 | extern bool cgroup_enable_cpuacct; |
| 286 | extern bool cgroup_enable_memory; |
| 287 | extern bool cgroup_enable_blkio; |
| 288 | extern bool cgroup_enable_pressure; |
| 289 | extern bool cgroup_enable_cpuacct_cpu_shares; |
| 290 | |
| 291 | extern int cgroup_check_for_new_every; |
| 292 | extern int cgroup_update_every; |
| 293 | |
| 294 | extern char *cgroup_cpuacct_base; |
| 295 | extern char *cgroup_cpuset_base; |
| 296 | extern char *cgroup_blkio_base; |
| 297 | extern char *cgroup_memory_base; |
| 298 | extern char *cgroup_pids_base; |
| 299 | extern char *cgroup_unified_base; |
| 300 | |
| 301 | extern int cgroup_root_count; |
| 302 | extern int cgroup_root_max; |
| 303 | extern int cgroup_max_depth; |
| 304 | |
| 305 | extern SIMPLE_PATTERN *enabled_cgroup_paths; |
| 306 | extern SIMPLE_PATTERN *enabled_cgroup_names; |
| 307 | extern SIMPLE_PATTERN *search_cgroup_paths; |
| 308 | extern SIMPLE_PATTERN *enabled_cgroup_renames; |
| 309 | extern SIMPLE_PATTERN *systemd_services_cgroups; |
| 310 | extern SIMPLE_PATTERN *entrypoint_parent_process_comm; |
| 311 | |
| 312 | extern const char *cgroups_network_interface_script; |
| 313 | |
| 314 | extern int cgroups_check; |
| 315 | |
| 316 | extern uint32_t Read_hash; |
| 317 | extern uint32_t Write_hash; |
| 318 | extern uint32_t user_hash; |
| 319 | extern uint32_t system_hash; |
| 320 | extern uint32_t user_usec_hash; |
| 321 | extern uint32_t system_usec_hash; |
| 322 | extern uint32_t nr_periods_hash; |
| 323 | extern uint32_t nr_throttled_hash; |
| 324 | extern uint32_t throttled_time_hash; |
| 325 | extern uint32_t throttled_usec_hash; |
| 326 | |
| 327 | extern struct cgroup *cgroup_root; |
| 328 | |
| 329 | enum cgroups_type { CGROUPS_AUTODETECT_FAIL, CGROUPS_V1, CGROUPS_V2 }; |
| 330 | |
| 331 | enum cgroups_systemd_setting { |
| 332 | SYSTEMD_CGROUP_ERR, |
| 333 | SYSTEMD_CGROUP_LEGACY, |
| 334 | SYSTEMD_CGROUP_HYBRID, |
| 335 | SYSTEMD_CGROUP_UNIFIED |
| 336 | }; |
| 337 | |
| 338 | struct cgroups_systemd_config_setting { |
| 339 | char *name; |
| 340 | enum cgroups_systemd_setting setting; |
| 341 | }; |
| 342 | |
| 343 | extern struct cgroups_systemd_config_setting cgroups_systemd_options[]; |
| 344 | |
| 345 | static inline int matches_enabled_cgroup_paths(char *id) { |
| 346 | return simple_pattern_matches(enabled_cgroup_paths, id); |
| 347 | } |
| 348 | |
| 349 | static inline int matches_enabled_cgroup_names(char *name) { |
| 350 | return simple_pattern_matches(enabled_cgroup_names, name); |
| 351 | } |
| 352 | |
| 353 | static inline int matches_enabled_cgroup_renames(char *id) { |
| 354 | return simple_pattern_matches(enabled_cgroup_renames, id); |
| 355 | } |
| 356 | |
| 357 | static inline int matches_systemd_services_cgroups(char *id) { |
| 358 | return simple_pattern_matches(systemd_services_cgroups, id); |
| 359 | } |
| 360 | |
| 361 | static inline int matches_search_cgroup_paths(const char *dir) { |
| 362 | return simple_pattern_matches(search_cgroup_paths, dir); |
| 363 | } |
| 364 | |
| 365 | static inline int matches_entrypoint_parent_process_comm(const char *comm) { |
| 366 | return simple_pattern_matches(entrypoint_parent_process_comm, comm); |
| 367 | } |
| 368 | |
| 369 | static inline int is_cgroup_systemd_service(struct cgroup *cg) { |
| 370 | return (int)(cg->options & CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE); |
| 371 | } |
| 372 | |
| 373 | static inline int k8s_is_kubepod(struct cgroup *cg) { |
| 374 | return cg->container_orchestrator == CGROUPS_ORCHESTRATOR_K8S; |
| 375 | } |
| 376 | |
| 377 | static inline char *cgroup_chart_type(char *buffer, struct cgroup *cg) { |
| 378 | buffer[0] = '\0'; |
| 379 | |
| 380 | if (cg->chart_id[0] == '\0' || (cg->chart_id[0] == '/' && cg->chart_id[1] == '\0')) |
| 381 | strncpyz(buffer, "cgroup_root", RRD_ID_LENGTH_MAX - 1); |
| 382 | else if (is_cgroup_systemd_service(cg)) |
| 383 | snprintfz(buffer, RRD_ID_LENGTH_MAX, "%s%s", services_chart_id_prefix, cg->chart_id); |
| 384 | else |
| 385 | snprintfz(buffer, RRD_ID_LENGTH_MAX, "%s%s", cgroup_chart_id_prefix, cg->chart_id); |
| 386 | |
| 387 | return buffer; |
| 388 | } |
| 389 | |
| 390 | #define RRDFUNCTIONS_CGTOP_HELP "Lists active containers and cgroups with resource usage including CPU, memory, disk I/O, and network traffic." |
| 391 | #define RRDFUNCTIONS_SYSTEMD_SERVICES_HELP "Shows systemd service cgroups with their process counts and resource consumption (CPU, memory, I/O)." |
| 392 | |
| 393 | int cgroup_function_cgroup_top(BUFFER *wb, const char *function, BUFFER *payload, const char *source); |
| 394 | int cgroup_function_systemd_top(BUFFER *wb, const char *function, BUFFER *payload, const char *source); |
| 395 | |
| 396 | void cgroup_netdev_link_init(void); |
| 397 | void cgroup_netdev_link_destroy(void); |
| 398 | |
| 399 | const DICTIONARY_ITEM *cgroup_netdev_get(struct cgroup *cg); |
| 400 | void cgroup_netdev_delete(struct cgroup *cg); |
| 401 | |
| 402 | void update_cpu_utilization_chart(struct cgroup *cg); |
| 403 | void update_cpu_utilization_limit_chart(struct cgroup *cg, NETDATA_DOUBLE cpu_limit); |
| 404 | void update_cpu_throttled_chart(struct cgroup *cg); |
| 405 | void update_cpu_throttled_duration_chart(struct cgroup *cg); |
| 406 | void update_cpu_shares_chart(struct cgroup *cg); |
| 407 | void update_cpu_per_core_usage_chart(struct cgroup *cg); |
| 408 | |
| 409 | void update_mem_usage_limit_chart(struct cgroup *cg, unsigned long long memory_limit); |
| 410 | void update_mem_utilization_chart(struct cgroup *cg, unsigned long long memory_limit); |
| 411 | void update_mem_usage_detailed_chart(struct cgroup *cg); |
| 412 | void update_mem_writeback_chart(struct cgroup *cg); |
| 413 | void update_mem_activity_chart(struct cgroup *cg); |
| 414 | void update_mem_pgfaults_chart(struct cgroup *cg); |
| 415 | void update_mem_failcnt_chart(struct cgroup *cg); |
| 416 | void update_mem_usage_chart(struct cgroup *cg); |
| 417 | |
| 418 | void update_io_serviced_bytes_chart(struct cgroup *cg); |
| 419 | void update_io_serviced_ops_chart(struct cgroup *cg); |
| 420 | void update_throttle_io_serviced_bytes_chart(struct cgroup *cg); |
| 421 | void update_throttle_io_serviced_ops_chart(struct cgroup *cg); |
| 422 | void update_io_queued_ops_chart(struct cgroup *cg); |
| 423 | void update_io_merged_ops_chart(struct cgroup *cg); |
| 424 | |
| 425 | void update_pids_current_chart(struct cgroup *cg); |
| 426 | |
| 427 | void update_cpu_some_pressure_chart(struct cgroup *cg); |
| 428 | void update_cpu_some_pressure_stall_time_chart(struct cgroup *cg); |
| 429 | void update_cpu_full_pressure_chart(struct cgroup *cg); |
| 430 | void update_cpu_full_pressure_stall_time_chart(struct cgroup *cg); |
| 431 | |
| 432 | void update_mem_some_pressure_chart(struct cgroup *cg); |
| 433 | void update_mem_some_pressure_stall_time_chart(struct cgroup *cg); |
| 434 | void update_mem_full_pressure_chart(struct cgroup *cg); |
| 435 | void update_mem_full_pressure_stall_time_chart(struct cgroup *cg); |
| 436 | |
| 437 | void update_irq_some_pressure_chart(struct cgroup *cg); |
| 438 | void update_irq_some_pressure_stall_time_chart(struct cgroup *cg); |
| 439 | void update_irq_full_pressure_chart(struct cgroup *cg); |
| 440 | void update_irq_full_pressure_stall_time_chart(struct cgroup *cg); |
| 441 | |
| 442 | void update_io_some_pressure_chart(struct cgroup *cg); |
| 443 | void update_io_some_pressure_stall_time_chart(struct cgroup *cg); |
| 444 | void update_io_full_pressure_chart(struct cgroup *cg); |
| 445 | void update_io_full_pressure_stall_time_chart(struct cgroup *cg); |
| 446 | |
| 447 | #endif // NETDATA_CGROUP_INTERNALS_H |