|
| 1 | +--- |
| 2 | +title: "CPU % - cgroup - sytem" |
| 3 | +description: "Documentation for CpuUtilizationCgroupSystemProvider of the Green Metrics Tool" |
| 4 | +lead: "" |
| 5 | +date: 2025-01-15T08:49:15+00:00 |
| 6 | +draft: false |
| 7 | +images: [] |
| 8 | +weight: 141 |
| 9 | +--- |
| 10 | +### What it does |
| 11 | + |
| 12 | +This metric provider calculates an estimate of the % total CPU usage based on the cgroups stats file of your docker containers. More information about cgroups can be found [here](https://www.man7.org/linux/man-pages/man7/cgroups.7.html). |
| 13 | + |
| 14 | +### Technical Details |
| 15 | + |
| 16 | +- Reads `/proc/stat` all CPU entry .... blah blah blah |
| 17 | + |
| 18 | +### Classname |
| 19 | + |
| 20 | +- `CpuUtilizationCgroupContainerProvider` |
| 21 | + |
| 22 | +### Metric Name |
| 23 | + |
| 24 | +- `cpu_utilization_cgroup_container` |
| 25 | + |
| 26 | +### Input Parameters |
| 27 | + |
| 28 | +- args |
| 29 | + - `-s`: container-ids seperated by commas |
| 30 | + - `-i`: interval in milliseconds |
| 31 | + |
| 32 | +By default the measurement interval is 100 ms. |
| 33 | + |
| 34 | +```bash |
| 35 | +./metric-provider-binary -i 100 -s 7f38a4c25fb8f9d5f8651d6ed986b3658dba20d1f5fec98a1f71c141c2b48f4b,c3592e1385d63f9c7810470b12aa00f7d6f7c0e2b9981ac2bdb4371126a0660a |
| 36 | +``` |
| 37 | + |
| 38 | + |
| 39 | +### Output |
| 40 | + |
| 41 | +This metric provider prints to Stdout a continuous stream of data. The format of the data is as follows: |
| 42 | + |
| 43 | +`TIMESTAMP READING CONTAINER-ID` |
| 44 | + |
| 45 | +Where: |
| 46 | +- `TIMESTAMP`: Unix timestamp, in microseconds |
| 47 | +- `READING`: The estimated % CPU used |
| 48 | +- `CONTAINER-ID`: The container ID that this reading is for |
| 49 | + |
| 50 | +Any errors are printed to Stderr. |
| 51 | + |
| 52 | +### How it works |
| 53 | + |
| 54 | +The provider assumes that you have [cgroups v2](https://www.man7.org/linux/man-pages/man7/cgroups.7.html) enabled on your system |
| 55 | + |
| 56 | +The provider reads from two files. To get the number of microseconds spent in the CPU by the container, during the interval, it reads from: |
| 57 | + |
| 58 | +``` |
| 59 | +/sys/fs/cgroup/user.slice/user-<USER-ID>.slice/user@<USER-ID>.service/user.slice/docker-<CONTAINER-ID>.scope/cpu.stat |
| 60 | +``` |
| 61 | + |
| 62 | +To get the total time spent by the cpu during that time interval, in Jiffies, you read from `/proc/stat`. We collect **user**, **nice**, **system**, **idle** **iowait**, **irq**, **softirq**, **steal** (see definitions [here](https://www.idnt.net/en-US/kb/941772)), add them together, divide by _SC_CLK_TCK_ (typically 100 Hz). The percentage of the cgroup time divided by this sum is the total percentage of CPU time spent by the container. |
| 63 | + |
| 64 | +Then it calculates the % cpu used via this formula: `container_reading * 10000 / main_cpu_reading` |
| 65 | + |
| 66 | +In order to work in rootless cgroup delegation must be enabled here: |
| 67 | +`/etc/systemd/system/[email protected]/delegate.conf` |
| 68 | + |
| 69 | +Currently, `<USER-ID>` is set to the calling user. |
| 70 | + |
0 commit comments