-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
279 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright 2023 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// | ||
// ============================================================ | ||
// Forked from https://github.com/KimMachineGun/automemlimit | ||
// | ||
// Written by Geon Kim <geon0250@gmail.com> | ||
// limitations under the MIT License. | ||
|
||
//go:build linux | ||
// +build linux | ||
|
||
package cgroup | ||
|
||
import ( | ||
"github.com/containerd/cgroups/v3" | ||
"github.com/containerd/cgroups/v3/cgroup1" | ||
"github.com/containerd/cgroups/v3/cgroup2" | ||
) | ||
|
||
// FromCgroup returns the memory limit based on the cgroups version on this system. | ||
func FromCgroup() (uint64, error) { | ||
switch cgroups.Mode() { | ||
case cgroups.Legacy: | ||
return FromCgroupV1() | ||
case cgroups.Hybrid, cgroups.Unified: | ||
return FromCgroupV2() | ||
} | ||
return 0, ErrNoCgroup | ||
} | ||
|
||
// FromCgroupV1 returns the memory limit from the cgroup v1. | ||
func FromCgroupV1() (uint64, error) { | ||
cg, err := cgroup1.Load(cgroup1.RootPath, cgroup1.WithHiearchy( | ||
cgroup1.SingleSubsystem(cgroup1.Default, cgroup1.Memory), | ||
)) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
metrics, err := cg.Stat(cgroup1.IgnoreNotExist) | ||
if err != nil { | ||
return 0, err | ||
} else if metrics.Memory == nil { | ||
return 0, ErrNoLimit | ||
} | ||
|
||
return metrics.Memory.HierarchicalMemoryLimit, nil | ||
} | ||
|
||
// FromCgroupV2 returns the memory limit from the cgroup v2. | ||
func FromCgroupV2() (uint64, error) { | ||
path, err := cgroup2.NestedGroupPath("") | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
m, err := cgroup2.Load(path, cgroup2.WithMountpoint(cgroupMountPoint)) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
stats, err := m.Stat() | ||
if err != nil { | ||
return 0, err | ||
} else if stats.Memory == nil { | ||
return 0, ErrNoLimit | ||
} | ||
|
||
return stats.Memory.UsageLimit, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright 2023 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// | ||
// ============================================================ | ||
// Forked from https://github.com/KimMachineGun/automemlimit | ||
// | ||
// Written by Geon Kim <geon0250@gmail.com> | ||
// limitations under the MIT License. | ||
|
||
//go:build linux | ||
// +build linux | ||
|
||
package cgroup | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/containerd/cgroups/v3" | ||
) | ||
|
||
func TestFromCgroup(t *testing.T) { | ||
limit, err := FromCgroup() | ||
if cgVersion == cgroups.Unavailable && err != ErrNoCgroup { | ||
t.Fatalf("FromCgroup() error = %v, wantErr %v", err, ErrNoCgroup) | ||
} | ||
|
||
if err != nil { | ||
t.Fatalf("FromCgroup() error = %v, wantErr %v", err, nil) | ||
} | ||
if limit != expected { | ||
t.Fatalf("FromCgroup() got = %v, want %v", limit, expected) | ||
} | ||
} | ||
|
||
func TestFromCgroupV1(t *testing.T) { | ||
if cgVersion != cgroups.Legacy { | ||
t.Skip("cgroups v1 is not supported") | ||
} | ||
limit, err := FromCgroupV1() | ||
if err != nil { | ||
t.Fatalf("FromCgroupV1() error = %v, wantErr %v", err, nil) | ||
} | ||
if limit != expected { | ||
t.Fatalf("FromCgroupV1() got = %v, want %v", limit, expected) | ||
} | ||
} | ||
|
||
func TestFromCgroupV2(t *testing.T) { | ||
if cgVersion != cgroups.Hybrid && cgVersion != cgroups.Unified { | ||
t.Skip("cgroups v2 is not supported") | ||
} | ||
limit, err := FromCgroupV2() | ||
if err != nil { | ||
t.Fatalf("FromCgroupV2() error = %v, wantErr %v", err, nil) | ||
} | ||
if limit != expected { | ||
t.Fatalf("FromCgroupV2() got = %v, want %v", limit, expected) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2023 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// | ||
// ============================================================ | ||
// Forked from https://github.com/KimMachineGun/automemlimit | ||
// | ||
// Written by Geon Kim <geon0250@gmail.com> | ||
// limitations under the MIT License. | ||
|
||
//go:build !linux | ||
// +build !linux | ||
|
||
package cgroup | ||
|
||
func FromCgroup() (uint64, error) { | ||
return 0, ErrCgroupsNotSupported | ||
} | ||
|
||
func FromCgroupV1() (uint64, error) { | ||
return 0, ErrCgroupsNotSupported | ||
} | ||
|
||
func FromCgroupV2() (uint64, error) { | ||
return 0, ErrCgroupsNotSupported | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright 2023 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// | ||
// ============================================================ | ||
// Forked from https://github.com/KimMachineGun/automemlimit | ||
// | ||
// Written by Geon Kim <geon0250@gmail.com> | ||
// limitations under the MIT License. | ||
|
||
//go:build !linux | ||
// +build !linux | ||
|
||
package cgroup | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestFromCgroup(t *testing.T) { | ||
limit, err := FromCgroup() | ||
if err != ErrCgroupsNotSupported { | ||
t.Fatalf("FromCgroup() error = %v, wantErr %v", err, ErrCgroupsNotSupported) | ||
} | ||
if limit != 0 { | ||
t.Fatalf("FromCgroup() got = %v, want %v", limit, 0) | ||
} | ||
} | ||
|
||
func TestFromCgroupV1(t *testing.T) { | ||
limit, err := FromCgroupV1() | ||
if err != ErrCgroupsNotSupported { | ||
t.Fatalf("FromCgroupV1() error = %v, wantErr %v", err, ErrCgroupsNotSupported) | ||
} | ||
if limit != 0 { | ||
t.Fatalf("FromCgroupV1() got = %v, want %v", limit, 0) | ||
} | ||
} | ||
|
||
func TestFromCgroupV2(t *testing.T) { | ||
limit, err := FromCgroupV2() | ||
if err != ErrCgroupsNotSupported { | ||
t.Fatalf("FromCgroupV2() error = %v, wantErr %v", err, ErrCgroupsNotSupported) | ||
} | ||
if limit != 0 { | ||
t.Fatalf("FromCgroupV2() got = %v, want %v", limit, 0) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2023 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// | ||
// ============================================================ | ||
// Forked from https://github.com/KimMachineGun/automemlimit | ||
// | ||
// Written by Geon Kim <geon0250@gmail.com> | ||
// limitations under the MIT License. | ||
|
||
package cgroup | ||
|
||
import "errors" | ||
|
||
const ( | ||
cgroupMountPoint = "/sys/fs/cgroup" | ||
) | ||
|
||
var ( | ||
// ErrNoCgroup is returned when the process is not in cgroup. | ||
ErrNoCgroup = errors.New("process is not in cgroup") | ||
// ErrCgroupsNotSupported is returned when the system does not support cgroups. | ||
ErrCgroupsNotSupported = errors.New("cgroups is not supported on this system") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters