Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example usage in container #179

Open
pavolloffay opened this issue Aug 21, 2020 · 3 comments
Open

Example usage in container #179

pavolloffay opened this issue Aug 21, 2020 · 3 comments

Comments

@pavolloffay
Copy link

Hi,

sorry for creating an issue but I didn't know where to ask a quesiton.

Can this library be used in a running container to get cgroups memory limits? I would like to get value of /sys/fs/cgroup/memory/memory.limit_in_bytes. Probably it's not safe to read that file directly hence I would like to use this lib to get the memory_limit_in_bytes.

How should be the cgroups created?

control, err := cgroups.Load(cgroups.V1, cgroups.NestedPath("")) //always error

Creating it by PID does not work either as the os.Getpid() returns 1 in the container.

@pavolloffay
Copy link
Author

The reason I am looking into this is to get total memory inside a running container.

func SysTotalMemory() uint64 {
	in := &syscall.Sysinfo_t{}
	err := syscall.Sysinfo(in)
	if err != nil {
		return 0
	}

	// If this is a 32-bit system, then these fields are
	// uint32 instead of uint64.
	// So we always convert to uint64 to match signature.
	return uint64(in.Totalram) * uint64(in.Unit)
}

returns the total host memory

@pavolloffay
Copy link
Author

@edigaryev
Copy link

I would like to get value of /sys/fs/cgroup/memory/memory.limit_in_bytes.

You should just use cgroups.RootPath instead of cgroups.NestedPath("") then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants