{"id":13461571,"url":"https://github.com/containerd/cgroups","last_synced_at":"2025-05-14T11:08:29.226Z","repository":{"id":38806984,"uuid":"70637366","full_name":"containerd/cgroups","owner":"containerd","description":"cgroups package for Go","archived":false,"fork":false,"pushed_at":"2025-05-05T18:31:47.000Z","size":871,"stargazers_count":1135,"open_issues_count":26,"forks_count":241,"subscribers_count":35,"default_branch":"main","last_synced_at":"2025-05-07T10:52:42.810Z","etag":null,"topics":["cgroups","golang","linux"],"latest_commit_sha":null,"homepage":"https://containerd.io","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/containerd.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2016-10-11T21:34:38.000Z","updated_at":"2025-05-04T14:19:35.000Z","dependencies_parsed_at":"2023-02-19T04:31:01.576Z","dependency_job_id":"db2b59a9-f8b9-470c-85a6-f9ded5ca2bd5","html_url":"https://github.com/containerd/cgroups","commit_stats":{"total_commits":383,"total_committers":93,"mean_commits":4.118279569892473,"dds":0.7911227154046998,"last_synced_commit":"bce3c7e5fbf05852294998684293918fcb3f59dd"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/containerd%2Fcgroups","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/containerd%2Fcgroups/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/containerd%2Fcgroups/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/containerd%2Fcgroups/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/containerd","download_url":"https://codeload.github.com/containerd/cgroups/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254077529,"owners_count":22010734,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cgroups","golang","linux"],"created_at":"2024-07-31T11:00:44.903Z","updated_at":"2025-05-14T11:08:29.204Z","avatar_url":"https://github.com/containerd.png","language":"Go","readme":"# cgroups\n\n[![Build Status](https://github.com/containerd/cgroups/workflows/CI/badge.svg)](https://github.com/containerd/cgroups/actions?query=workflow%3ACI)\n[![codecov](https://codecov.io/gh/containerd/cgroups/branch/main/graph/badge.svg)](https://codecov.io/gh/containerd/cgroups)\n[![GoDoc](https://godoc.org/github.com/containerd/cgroups?status.svg)](https://godoc.org/github.com/containerd/cgroups)\n[![Go Report Card](https://goreportcard.com/badge/github.com/containerd/cgroups)](https://goreportcard.com/report/github.com/containerd/cgroups)\n\nGo package for creating, managing, inspecting, and destroying cgroups.\nThe resources format for settings on the cgroup uses the OCI runtime-spec found\n[here](https://github.com/opencontainers/runtime-spec).\n\n## Examples (v1)\n\n### Create a new cgroup\n\nThis creates a new cgroup using a static path for all subsystems under `/test`.\n\n* /sys/fs/cgroup/cpu/test\n* /sys/fs/cgroup/memory/test\n* etc....\n\nIt uses a single hierarchy and specifies cpu shares as a resource constraint and\nuses the v1 implementation of cgroups.\n\n\n```go\nshares := uint64(100)\ncontrol, err := cgroup1.New(cgroup1.StaticPath(\"/test\"), \u0026specs.LinuxResources{\n    CPU: \u0026specs.LinuxCPU{\n        Shares: \u0026shares,\n    },\n})\ndefer control.Delete()\n```\n\n### Create with systemd slice support\n\n\n```go\ncontrol, err := cgroup1.New(cgroup1.Systemd, cgroup1.Slice(\"system.slice\", \"runc-test\"), \u0026specs.LinuxResources{\n    CPU: \u0026specs.CPU{\n        Shares: \u0026shares,\n    },\n})\n\n```\n\n### Load an existing cgroup\n\n```go\ncontrol, err = cgroup1.Load(cgroup1.Default, cgroups.StaticPath(\"/test\"))\n```\n\n### Add a process to the cgroup\n\n```go\nif err := control.Add(cgroup1.Process{Pid:1234}); err != nil {\n}\n```\n\n###  Update the cgroup\n\nTo update the resources applied in the cgroup\n\n```go\nshares = uint64(200)\nif err := control.Update(\u0026specs.LinuxResources{\n    CPU: \u0026specs.LinuxCPU{\n        Shares: \u0026shares,\n    },\n}); err != nil {\n}\n```\n\n### Freeze and Thaw the cgroup\n\n```go\nif err := control.Freeze(); err != nil {\n}\nif err := control.Thaw(); err != nil {\n}\n```\n\n### List all processes in the cgroup or recursively\n\n```go\nprocesses, err := control.Processes(cgroup1.Devices, recursive)\n```\n\n### Get Stats on the cgroup\n\n```go\nstats, err := control.Stat()\n```\n\nBy adding `cgroups.IgnoreNotExist` all non-existent files will be ignored, e.g. swap memory stats without swap enabled\n```go\nstats, err := control.Stat(cgroup1.IgnoreNotExist)\n```\n\n### Move process across cgroups\n\nThis allows you to take processes from one cgroup and move them to another.\n\n```go\nerr := control.MoveTo(destination)\n```\n\n### Create subcgroup\n\n```go\nsubCgroup, err := control.New(\"child\", resources)\n```\n\n### Registering for memory events\n\nThis allows you to get notified by an eventfd for v1 memory cgroups events.\n\n```go\nevent := cgroup1.MemoryThresholdEvent(50 * 1024 * 1024, false)\nefd, err := control.RegisterMemoryEvent(event)\n```\n\n```go\nevent := cgroup1.MemoryPressureEvent(cgroup1.MediumPressure, cgroup1.DefaultMode)\nefd, err := control.RegisterMemoryEvent(event)\n```\n\n```go\nefd, err := control.OOMEventFD()\n// or by using RegisterMemoryEvent\nevent := cgroup1.OOMEvent()\nefd, err := control.RegisterMemoryEvent(event)\n```\n\n## Examples (v2/unified)\n\n### Check that the current system is running cgroups v2\n\n```go\nvar cgroupV2 bool\nif cgroups.Mode() == cgroups.Unified {\n\tcgroupV2 = true\n}\n```\n\n### Create a new cgroup\n\nThis creates a new systemd v2 cgroup slice. Systemd slices consider [\"-\" a special character](https://www.freedesktop.org/software/systemd/man/systemd.slice.html),\nso the resulting slice would be located here on disk:\n\n* /sys/fs/cgroup/my.slice/my-cgroup.slice/my-cgroup-abc.slice\n\n```go\nimport (\n    \"github.com/containerd/cgroups/v3/cgroup2\"\n    specs \"github.com/opencontainers/runtime-spec/specs-go\"\n)\n\nres := cgroup2.Resources{}\n// dummy PID of -1 is used for creating a \"general slice\" to be used as a parent cgroup.\n// see https://github.com/containerd/cgroups/blob/1df78138f1e1e6ee593db155c6b369466f577651/v2/manager.go#L732-L735\nm, err := cgroup2.NewSystemd(\"/\", \"my-cgroup-abc.slice\", -1, \u0026res)\nif err != nil {\n\treturn err\n}\n```\n\n### Load an existing cgroup\n\n```go\nm, err := cgroup2.LoadSystemd(\"/\", \"my-cgroup-abc.slice\")\nif err != nil {\n\treturn err\n}\n```\n\n### Delete a cgroup\n\n```go\nm, err := cgroup2.LoadSystemd(\"/\", \"my-cgroup-abc.slice\")\nif err != nil {\n\treturn err\n}\nerr = m.DeleteSystemd()\nif err != nil {\n\treturn err\n}\n```\n\n### Kill all processes in a cgroup\n\n```go\nm, err := cgroup2.LoadSystemd(\"/\", \"my-cgroup-abc.slice\")\nif err != nil {\n\treturn err\n}\nerr = m.Kill()\nif err != nil {\n\treturn err\n}\n```\n\n\n### Get and set cgroup type\n```go\nm, err := cgroup2.LoadSystemd(\"/\", \"my-cgroup-abc.slice\")\nif err != nil {\n    return err\n}\n\n// https://www.kernel.org/doc/html/v5.0/admin-guide/cgroup-v2.html#threads\ncgType, err := m.GetType()\nif err != nil {\n    return err\n}\nfmt.Println(cgType)\n\nerr = m.SetType(cgroup2.Threaded)\nif err != nil {\n    return err\n}\n```\n\n### Attention\n\nAll static path should not include `/sys/fs/cgroup/` prefix, it should start with your own cgroups name\n\n## Project details\n\nCgroups is a containerd sub-project, licensed under the [Apache 2.0 license](./LICENSE).\nAs a containerd sub-project, you will find the:\n\n * [Project governance](https://github.com/containerd/project/blob/main/GOVERNANCE.md),\n * [Maintainers](https://github.com/containerd/project/blob/main/MAINTAINERS),\n * and [Contributing guidelines](https://github.com/containerd/project/blob/main/CONTRIBUTING.md)\n\ninformation in our [`containerd/project`](https://github.com/containerd/project) repository.\n","funding_links":[],"categories":["Go"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcontainerd%2Fcgroups","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcontainerd%2Fcgroups","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcontainerd%2Fcgroups/lists"}