-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(kuma-dp): detect memory limit only on linux (#4715)
Signed-off-by: Jakub Dyszkiewicz <jakub.dyszkiewicz@gmail.com>
- Loading branch information
1 parent
9837334
commit d43e390
Showing
4 changed files
with
61 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package envoy | ||
|
||
func DetectMaxMemory() uint64 { | ||
return 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,50 @@ | ||
package envoy | ||
|
||
import ( | ||
"os" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/containerd/cgroups" | ||
) | ||
|
||
type UIntOrString struct { | ||
Type string | ||
UInt uint64 | ||
String string | ||
} | ||
|
||
func DetectMaxMemory() uint64 { | ||
switch cgroups.Mode() { | ||
case cgroups.Legacy: | ||
res := maybeReadAsBytes("/sys/fs/cgroup/memory.limit_in_bytes") | ||
if res != nil && res.Type == "int" { | ||
return res.UInt | ||
} | ||
case cgroups.Hybrid, cgroups.Unified: | ||
res := maybeReadAsBytes("/sys/fs/cgroup/memory.max") | ||
if res != nil && res.Type == "int" { | ||
return res.UInt | ||
} | ||
} | ||
return 0 | ||
} | ||
|
||
func maybeReadAsBytes(path string) *UIntOrString { | ||
byteContents, err := os.ReadFile(path) | ||
if err == nil { | ||
contents := strings.TrimSpace(string(byteContents)) | ||
bytes, err := strconv.ParseUint(contents, 10, 64) | ||
if err != nil { | ||
return &UIntOrString{ | ||
Type: "string", | ||
String: contents, | ||
} | ||
} | ||
return &UIntOrString{ | ||
Type: "int", | ||
UInt: bytes, | ||
} | ||
} | ||
return 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,5 @@ | ||
package envoy | ||
|
||
func DetectMaxMemory() uint64 { | ||
return 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