Initial move before enhancing cgroups package.
Docker-DCO-1.1-Signed-off-by: Paul Nasrat <pnasrat@gmail.com> (github: pnasrat)
| 2 | 1 |
deleted file mode 100644 |
| ... | ... |
@@ -1,106 +0,0 @@ |
| 1 |
-package cgroups |
|
| 2 |
- |
|
| 3 |
-import ( |
|
| 4 |
- "bufio" |
|
| 5 |
- "fmt" |
|
| 6 |
- "github.com/dotcloud/docker/pkg/mount" |
|
| 7 |
- "io" |
|
| 8 |
- "io/ioutil" |
|
| 9 |
- "os" |
|
| 10 |
- "path/filepath" |
|
| 11 |
- "strconv" |
|
| 12 |
- "strings" |
|
| 13 |
-) |
|
| 14 |
- |
|
| 15 |
-type Values struct {
|
|
| 16 |
- Memory int64 `json:"memory"` |
|
| 17 |
- MemorySwap int64 `json:"memory_swap"` |
|
| 18 |
- CpuShares int64 `json:"cpu_shares"` |
|
| 19 |
-} |
|
| 20 |
- |
|
| 21 |
-// https://www.kernel.org/doc/Documentation/cgroups/cgroups.txt |
|
| 22 |
-func FindCgroupMountpoint(subsystem string) (string, error) {
|
|
| 23 |
- mounts, err := mount.GetMounts() |
|
| 24 |
- if err != nil {
|
|
| 25 |
- return "", err |
|
| 26 |
- } |
|
| 27 |
- |
|
| 28 |
- for _, mount := range mounts {
|
|
| 29 |
- if mount.Fstype == "cgroup" {
|
|
| 30 |
- for _, opt := range strings.Split(mount.VfsOpts, ",") {
|
|
| 31 |
- if opt == subsystem {
|
|
| 32 |
- return mount.Mountpoint, nil |
|
| 33 |
- } |
|
| 34 |
- } |
|
| 35 |
- } |
|
| 36 |
- } |
|
| 37 |
- |
|
| 38 |
- return "", fmt.Errorf("cgroup mountpoint not found for %s", subsystem)
|
|
| 39 |
-} |
|
| 40 |
- |
|
| 41 |
-// Returns the relative path to the cgroup docker is running in. |
|
| 42 |
-func getThisCgroupDir(subsystem string) (string, error) {
|
|
| 43 |
- f, err := os.Open("/proc/self/cgroup")
|
|
| 44 |
- if err != nil {
|
|
| 45 |
- return "", err |
|
| 46 |
- } |
|
| 47 |
- defer f.Close() |
|
| 48 |
- |
|
| 49 |
- return parseCgroupFile(subsystem, f) |
|
| 50 |
-} |
|
| 51 |
- |
|
| 52 |
-func parseCgroupFile(subsystem string, r io.Reader) (string, error) {
|
|
| 53 |
- s := bufio.NewScanner(r) |
|
| 54 |
- |
|
| 55 |
- for s.Scan() {
|
|
| 56 |
- if err := s.Err(); err != nil {
|
|
| 57 |
- return "", err |
|
| 58 |
- } |
|
| 59 |
- text := s.Text() |
|
| 60 |
- parts := strings.Split(text, ":") |
|
| 61 |
- if parts[1] == subsystem {
|
|
| 62 |
- return parts[2], nil |
|
| 63 |
- } |
|
| 64 |
- } |
|
| 65 |
- return "", fmt.Errorf("cgroup '%s' not found in /proc/self/cgroup", subsystem)
|
|
| 66 |
-} |
|
| 67 |
- |
|
| 68 |
-// Returns a list of pids for the given container. |
|
| 69 |
-func GetPidsForContainer(id string) ([]int, error) {
|
|
| 70 |
- pids := []int{}
|
|
| 71 |
- |
|
| 72 |
- // memory is chosen randomly, any cgroup used by docker works |
|
| 73 |
- subsystem := "memory" |
|
| 74 |
- |
|
| 75 |
- cgroupRoot, err := FindCgroupMountpoint(subsystem) |
|
| 76 |
- if err != nil {
|
|
| 77 |
- return pids, err |
|
| 78 |
- } |
|
| 79 |
- |
|
| 80 |
- cgroupDir, err := getThisCgroupDir(subsystem) |
|
| 81 |
- if err != nil {
|
|
| 82 |
- return pids, err |
|
| 83 |
- } |
|
| 84 |
- |
|
| 85 |
- filename := filepath.Join(cgroupRoot, cgroupDir, id, "tasks") |
|
| 86 |
- if _, err := os.Stat(filename); os.IsNotExist(err) {
|
|
| 87 |
- // With more recent lxc versions use, cgroup will be in lxc/ |
|
| 88 |
- filename = filepath.Join(cgroupRoot, cgroupDir, "lxc", id, "tasks") |
|
| 89 |
- } |
|
| 90 |
- |
|
| 91 |
- output, err := ioutil.ReadFile(filename) |
|
| 92 |
- if err != nil {
|
|
| 93 |
- return pids, err |
|
| 94 |
- } |
|
| 95 |
- for _, p := range strings.Split(string(output), "\n") {
|
|
| 96 |
- if len(p) == 0 {
|
|
| 97 |
- continue |
|
| 98 |
- } |
|
| 99 |
- pid, err := strconv.Atoi(p) |
|
| 100 |
- if err != nil {
|
|
| 101 |
- return pids, fmt.Errorf("Invalid pid '%s': %s", p, err)
|
|
| 102 |
- } |
|
| 103 |
- pids = append(pids, pid) |
|
| 104 |
- } |
|
| 105 |
- return pids, nil |
|
| 106 |
-} |
| 107 | 1 |
deleted file mode 100644 |
| ... | ... |
@@ -1,27 +0,0 @@ |
| 1 |
-package cgroups |
|
| 2 |
- |
|
| 3 |
-import ( |
|
| 4 |
- "bytes" |
|
| 5 |
- "testing" |
|
| 6 |
-) |
|
| 7 |
- |
|
| 8 |
-const ( |
|
| 9 |
- cgroupsContents = `11:hugetlb:/ |
|
| 10 |
-10:perf_event:/ |
|
| 11 |
-9:blkio:/ |
|
| 12 |
-8:net_cls:/ |
|
| 13 |
-7:freezer:/ |
|
| 14 |
-6:devices:/ |
|
| 15 |
-5:memory:/ |
|
| 16 |
-4:cpuacct,cpu:/ |
|
| 17 |
-3:cpuset:/ |
|
| 18 |
-2:name=systemd:/user.slice/user-1000.slice/session-16.scope` |
|
| 19 |
-) |
|
| 20 |
- |
|
| 21 |
-func TestParseCgroups(t *testing.T) {
|
|
| 22 |
- r := bytes.NewBuffer([]byte(cgroupsContents)) |
|
| 23 |
- _, err := parseCgroupFile("blkio", r)
|
|
| 24 |
- if err != nil {
|
|
| 25 |
- t.Fatal(err) |
|
| 26 |
- } |
|
| 27 |
-} |
| ... | ... |
@@ -5,9 +5,9 @@ import ( |
| 5 | 5 |
"errors" |
| 6 | 6 |
"fmt" |
| 7 | 7 |
"github.com/dotcloud/docker/archive" |
| 8 |
- "github.com/dotcloud/docker/cgroups" |
|
| 9 | 8 |
"github.com/dotcloud/docker/execdriver" |
| 10 | 9 |
"github.com/dotcloud/docker/graphdriver" |
| 10 |
+ "github.com/dotcloud/docker/pkg/cgroups" |
|
| 11 | 11 |
"github.com/dotcloud/docker/pkg/mount" |
| 12 | 12 |
"github.com/dotcloud/docker/pkg/term" |
| 13 | 13 |
"github.com/dotcloud/docker/utils" |
| 0 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,106 @@ |
| 0 |
+package cgroups |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "bufio" |
|
| 4 |
+ "fmt" |
|
| 5 |
+ "github.com/dotcloud/docker/pkg/mount" |
|
| 6 |
+ "io" |
|
| 7 |
+ "io/ioutil" |
|
| 8 |
+ "os" |
|
| 9 |
+ "path/filepath" |
|
| 10 |
+ "strconv" |
|
| 11 |
+ "strings" |
|
| 12 |
+) |
|
| 13 |
+ |
|
| 14 |
+type Values struct {
|
|
| 15 |
+ Memory int64 `json:"memory"` |
|
| 16 |
+ MemorySwap int64 `json:"memory_swap"` |
|
| 17 |
+ CpuShares int64 `json:"cpu_shares"` |
|
| 18 |
+} |
|
| 19 |
+ |
|
| 20 |
+// https://www.kernel.org/doc/Documentation/cgroups/cgroups.txt |
|
| 21 |
+func FindCgroupMountpoint(subsystem string) (string, error) {
|
|
| 22 |
+ mounts, err := mount.GetMounts() |
|
| 23 |
+ if err != nil {
|
|
| 24 |
+ return "", err |
|
| 25 |
+ } |
|
| 26 |
+ |
|
| 27 |
+ for _, mount := range mounts {
|
|
| 28 |
+ if mount.Fstype == "cgroup" {
|
|
| 29 |
+ for _, opt := range strings.Split(mount.VfsOpts, ",") {
|
|
| 30 |
+ if opt == subsystem {
|
|
| 31 |
+ return mount.Mountpoint, nil |
|
| 32 |
+ } |
|
| 33 |
+ } |
|
| 34 |
+ } |
|
| 35 |
+ } |
|
| 36 |
+ |
|
| 37 |
+ return "", fmt.Errorf("cgroup mountpoint not found for %s", subsystem)
|
|
| 38 |
+} |
|
| 39 |
+ |
|
| 40 |
+// Returns the relative path to the cgroup docker is running in. |
|
| 41 |
+func getThisCgroupDir(subsystem string) (string, error) {
|
|
| 42 |
+ f, err := os.Open("/proc/self/cgroup")
|
|
| 43 |
+ if err != nil {
|
|
| 44 |
+ return "", err |
|
| 45 |
+ } |
|
| 46 |
+ defer f.Close() |
|
| 47 |
+ |
|
| 48 |
+ return parseCgroupFile(subsystem, f) |
|
| 49 |
+} |
|
| 50 |
+ |
|
| 51 |
+func parseCgroupFile(subsystem string, r io.Reader) (string, error) {
|
|
| 52 |
+ s := bufio.NewScanner(r) |
|
| 53 |
+ |
|
| 54 |
+ for s.Scan() {
|
|
| 55 |
+ if err := s.Err(); err != nil {
|
|
| 56 |
+ return "", err |
|
| 57 |
+ } |
|
| 58 |
+ text := s.Text() |
|
| 59 |
+ parts := strings.Split(text, ":") |
|
| 60 |
+ if parts[1] == subsystem {
|
|
| 61 |
+ return parts[2], nil |
|
| 62 |
+ } |
|
| 63 |
+ } |
|
| 64 |
+ return "", fmt.Errorf("cgroup '%s' not found in /proc/self/cgroup", subsystem)
|
|
| 65 |
+} |
|
| 66 |
+ |
|
| 67 |
+// Returns a list of pids for the given container. |
|
| 68 |
+func GetPidsForContainer(id string) ([]int, error) {
|
|
| 69 |
+ pids := []int{}
|
|
| 70 |
+ |
|
| 71 |
+ // memory is chosen randomly, any cgroup used by docker works |
|
| 72 |
+ subsystem := "memory" |
|
| 73 |
+ |
|
| 74 |
+ cgroupRoot, err := FindCgroupMountpoint(subsystem) |
|
| 75 |
+ if err != nil {
|
|
| 76 |
+ return pids, err |
|
| 77 |
+ } |
|
| 78 |
+ |
|
| 79 |
+ cgroupDir, err := getThisCgroupDir(subsystem) |
|
| 80 |
+ if err != nil {
|
|
| 81 |
+ return pids, err |
|
| 82 |
+ } |
|
| 83 |
+ |
|
| 84 |
+ filename := filepath.Join(cgroupRoot, cgroupDir, id, "tasks") |
|
| 85 |
+ if _, err := os.Stat(filename); os.IsNotExist(err) {
|
|
| 86 |
+ // With more recent lxc versions use, cgroup will be in lxc/ |
|
| 87 |
+ filename = filepath.Join(cgroupRoot, cgroupDir, "lxc", id, "tasks") |
|
| 88 |
+ } |
|
| 89 |
+ |
|
| 90 |
+ output, err := ioutil.ReadFile(filename) |
|
| 91 |
+ if err != nil {
|
|
| 92 |
+ return pids, err |
|
| 93 |
+ } |
|
| 94 |
+ for _, p := range strings.Split(string(output), "\n") {
|
|
| 95 |
+ if len(p) == 0 {
|
|
| 96 |
+ continue |
|
| 97 |
+ } |
|
| 98 |
+ pid, err := strconv.Atoi(p) |
|
| 99 |
+ if err != nil {
|
|
| 100 |
+ return pids, fmt.Errorf("Invalid pid '%s': %s", p, err)
|
|
| 101 |
+ } |
|
| 102 |
+ pids = append(pids, pid) |
|
| 103 |
+ } |
|
| 104 |
+ return pids, nil |
|
| 105 |
+} |
| 0 | 106 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,27 @@ |
| 0 |
+package cgroups |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "bytes" |
|
| 4 |
+ "testing" |
|
| 5 |
+) |
|
| 6 |
+ |
|
| 7 |
+const ( |
|
| 8 |
+ cgroupsContents = `11:hugetlb:/ |
|
| 9 |
+10:perf_event:/ |
|
| 10 |
+9:blkio:/ |
|
| 11 |
+8:net_cls:/ |
|
| 12 |
+7:freezer:/ |
|
| 13 |
+6:devices:/ |
|
| 14 |
+5:memory:/ |
|
| 15 |
+4:cpuacct,cpu:/ |
|
| 16 |
+3:cpuset:/ |
|
| 17 |
+2:name=systemd:/user.slice/user-1000.slice/session-16.scope` |
|
| 18 |
+) |
|
| 19 |
+ |
|
| 20 |
+func TestParseCgroups(t *testing.T) {
|
|
| 21 |
+ r := bytes.NewBuffer([]byte(cgroupsContents)) |
|
| 22 |
+ _, err := parseCgroupFile("blkio", r)
|
|
| 23 |
+ if err != nil {
|
|
| 24 |
+ t.Fatal(err) |
|
| 25 |
+ } |
|
| 26 |
+} |
| ... | ... |
@@ -6,8 +6,8 @@ import ( |
| 6 | 6 |
"fmt" |
| 7 | 7 |
"github.com/dotcloud/docker/archive" |
| 8 | 8 |
"github.com/dotcloud/docker/auth" |
| 9 |
- "github.com/dotcloud/docker/cgroups" |
|
| 10 | 9 |
"github.com/dotcloud/docker/engine" |
| 10 |
+ "github.com/dotcloud/docker/pkg/cgroups" |
|
| 11 | 11 |
"github.com/dotcloud/docker/pkg/graphdb" |
| 12 | 12 |
"github.com/dotcloud/docker/registry" |
| 13 | 13 |
"github.com/dotcloud/docker/utils" |