Skip to content

Commit

Permalink
fix cgroup error
Browse files Browse the repository at this point in the history
  • Loading branch information
whalechoi committed Aug 19, 2024
1 parent eccfc3c commit ccbac2c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.22'
go-version: '1.23'
check-latest: true

- name: Get project dependencies
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module XrayHelper

go 1.22
go 1.23

require (
github.com/coreos/go-iptables v0.7.0
Expand Down
Empty file modified go.sum
100755 → 100644
Empty file.
2 changes: 1 addition & 1 deletion main/builds/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const (
VersionX byte = 1
VersionY byte = 4
VersionZ byte = 5
Build = "-release"
Build = "-fix1"
Intro = "A unified helper for Android to control system proxy.\n\nTelegram channel: https://t.me/Asterisk4Magisk\nTelegram chat: https://t.me/AsteriskFactory\n\nReport issues at https://github.com/Asterisk4Magisk/XrayHelper/issues\n"
)

Expand Down
99 changes: 49 additions & 50 deletions main/cgroup/cgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,59 +59,58 @@ func LimitProcess(pid int) error {
return err
}
mountPoint = mp
// create cpu limit
if cpuLimit != 100.0 {
if err := os.MkdirAll(filepath.Join(mountPoint, "cpuctl", name), 0o755); err != nil {
return e.New("cannot create cpuctl cgroup, ", err).WithPrefix(tagCgroup)
}
if err := os.WriteFile(
filepath.Join(mountPoint, "cpuctl", name, "cpu.uclamp.max"),
[]byte(strconv.FormatFloat(cpuLimit, 'f', 2, 64)),
os.FileMode(0),
); err != nil {
log.HandleDebug("kernel not support uclamp, skip cpu.uclamp.max")
}
if err := os.WriteFile(
filepath.Join(mountPoint, "cpuctl", name, "cpu.shares"),
[]byte(strconv.FormatInt(int64(cpuLimit*0.01*1024), 10)),
os.FileMode(0),
); err != nil {
return e.New("cannot apply cpuctl cgroup, ", err).WithPrefix(tagCgroup)
}
}
// create cpu limit
if cpuLimit != 100.0 {
if err := os.MkdirAll(filepath.Join(mountPoint, "cpuctl", name), 0o755); err != nil {
return e.New("cannot create cpuctl cgroup, ", err).WithPrefix(tagCgroup)
}
// create memory limit
if memLimit > 0 {
if err := os.MkdirAll(filepath.Join(mountPoint, "memcg", name), 0o755); err != nil {
return e.New("cannot create memcg cgroup, ", err).WithPrefix(tagCgroup)
}
if err := os.WriteFile(
filepath.Join(mountPoint, "memcg", name, "memory.limit_in_bytes"),
// convert limit to bytes
[]byte(strconv.FormatInt(int64(memLimit*1024*1024), 10)),
os.FileMode(0),
); err != nil {
return e.New("cannot apply memcg cgroup, ", err).WithPrefix(tagCgroup)
}
if err := os.WriteFile(
filepath.Join(mountPoint, "cpuctl", name, "cpu.uclamp.max"),
[]byte(strconv.FormatFloat(cpuLimit, 'f', 2, 64)),
os.FileMode(0),
); err != nil {
log.HandleDebug("kernel not support uclamp, skip cpu.uclamp.max")
}
if err := os.WriteFile(
filepath.Join(mountPoint, "cpuctl", name, "cpu.shares"),
[]byte(strconv.FormatInt(int64(cpuLimit*0.01*1024), 10)),
os.FileMode(0),
); err != nil {
return e.New("cannot apply cpuctl cgroup, ", err).WithPrefix(tagCgroup)
}
// apply cpu limit
f, err := os.OpenFile(filepath.Join(mountPoint, "cpuctl", name, "cgroup.procs"), os.O_CREATE|os.O_WRONLY|os.O_APPEND, os.FileMode(0))
if err != nil {
return e.New("cannot open cpuctl cgroup procs, ", err).WithPrefix(tagCgroup)
}
defer f.Close()
if _, err := f.WriteString(strconv.FormatInt(int64(pid), 10) + "\n"); err != nil {
return e.New("cannot add process to cpuctl cgroup, ", err).WithPrefix(tagCgroup)
}

}
// apply cpu limit
f, err := os.OpenFile(filepath.Join(mountPoint, "cpuctl", name, "cgroup.procs"), os.O_CREATE|os.O_WRONLY|os.O_APPEND, os.FileMode(0))
if err != nil {
return e.New("cannot open cpuctl cgroup procs, ", err).WithPrefix(tagCgroup)
}
defer f.Close()
if _, err := f.WriteString(strconv.FormatInt(int64(pid), 10) + "\n"); err != nil {
return e.New("cannot add process to cpuctl cgroup, ", err).WithPrefix(tagCgroup)
}
// apply memory limit
f2, err := os.OpenFile(filepath.Join(mountPoint, "memcg", name, "cgroup.procs"), os.O_CREATE|os.O_WRONLY|os.O_APPEND, os.FileMode(0))
if err != nil {
return e.New("cannot open memcg cgroup procs, ", err).WithPrefix(tagCgroup)
}
defer f2.Close()
if _, err := f2.WriteString(strconv.FormatInt(int64(pid), 10) + "\n"); err != nil {
return e.New("cannot add process to memcg cgroup, ", err).WithPrefix(tagCgroup)
// create memory limit
if memLimit > 0 {
if err := os.MkdirAll(filepath.Join(mountPoint, "memcg", name), 0o755); err != nil {
return e.New("cannot create memcg cgroup, ", err).WithPrefix(tagCgroup)
}
if err := os.WriteFile(
filepath.Join(mountPoint, "memcg", name, "memory.limit_in_bytes"),
// convert limit to bytes
[]byte(strconv.FormatInt(int64(memLimit*1024*1024), 10)),
os.FileMode(0),
); err != nil {
return e.New("cannot apply memcg cgroup, ", err).WithPrefix(tagCgroup)
}
// apply memory limit
f2, err := os.OpenFile(filepath.Join(mountPoint, "memcg", name, "cgroup.procs"), os.O_CREATE|os.O_WRONLY|os.O_APPEND, os.FileMode(0))
if err != nil {
return e.New("cannot open memcg cgroup procs, ", err).WithPrefix(tagCgroup)
}
defer f2.Close()
if _, err := f2.WriteString(strconv.FormatInt(int64(pid), 10) + "\n"); err != nil {
return e.New("cannot add process to memcg cgroup, ", err).WithPrefix(tagCgroup)
}
}
return nil
}

0 comments on commit ccbac2c

Please sign in to comment.