4

I'm trying to make ansible set InnoDB buffer pool size to some percent of available memory. But ansible_memtotal_mb and free report how much memory the host has. How do I figure out how much memory is available from inside container? Container name is not known in advance.

UPD I'm running debian jessie, and pass cgroup_enable=memory parameter to the kernel.

host
====

# lxc-checkconfig
Kernel configuration not found at /proc/config.gz; searching...
Kernel configuration found at /boot/config-3.16.0-4-amd64
--- Namespaces ---
Namespaces: enabled
Utsname namespace: enabled
Ipc namespace: enabled
Pid namespace: enabled
User namespace: enabled
Network namespace: enabled
Multiple /dev/pts instances: enabled

--- Control groups ---
Cgroup: enabled
Cgroup clone_children flag: enabled
Cgroup device: enabled
Cgroup sched: enabled
Cgroup cpu account: enabled
Cgroup memory controller: enabled
Cgroup cpuset: enabled

--- Misc ---
Veth pair device: enabled
Macvlan: enabled
Vlan: enabled
File capabilities: enabled

Note : Before booting a new kernel, you can check its configuration
usage : CONFIG=/path/to/config /usr/bin/lxc-checkconfig

# grep cgroup /var/lib/lxc/sta/config
lxc.cgroup.memory.limit_in_bytes = 1000M

# mount | grep memory
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)

# cd /sys/fs/cgroup/memory

# cat memory.limit_in_bytes
18446744073709551615

# cat lxc/sta/memory.limit_in_bytes
1048576000


container
=========

$ cat /proc/self/cgroup
9:perf_event:/lxc/sta
8:blkio:/
7:net_cls,net_prio:/lxc/sta
6:freezer:/lxc/sta
5:devices:/
4:memory:/
3:cpu,cpuacct:/
2:cpuset:/lxc/sta
1:name=systemd:/user.slice/user-0.slice/session-10304.scope/system.slice/ssh.service

# mount | grep memory
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)

# cd /sys/fs/cgroup/memory

# cat memory.limit_in_bytes
18446744073709551615

# cat lxc/sta/memory.limit_in_bytes
1048576000

1 Answer 1

3

tl;dr

cat /sys/fs/cgroup/memory$(cat /proc/self/cgroup | grep memory | cut -d: -f3)/memory.limit_in_bytes

or

cat $(mount | grep cgroup | grep memory | cut -d' ' -f3)$(cat /proc/self/cgroup | grep memory | cut -d: -f3)/memory.limit_in_bytes

If your default container configuration allows host's cgroup info from within container (based on lxc.mount.auto setting),you could simply parse cgroup info as shown below

Check your cgroup info from /proc/self/cgroup

root@my-firefox:/# grep memory /proc/self/cgroup 
4:memory:/cv/my-firefox

Now based on your cgroup mount point (could locate that from /proc/mounts), verify memory limit file content

root@my-firefox:/# cd /sys/fs/cgroup/memory/cv/my-firefox/
root@my-firefox:/sys/fs/cgroup/memory/cv/my-firefox# cat memory.limit_in_bytes 
268435456

In my case above, cgroup root was mounted at /sys/fs/cgroup so with that info and appending path /memory/cv/my-firefox, I could query all memory limits set for the container

This case the limit is 256M

PS: free & ansible_memtotal_mb are host based and they are not container aware. I am not aware of ansible, but I assume it would have something similar to facts in puppet, where you could write a custom fact to gather this info

7
  • Take a look please at my question again. My memory cgroup is /. What could this possibly mean? Is the limit not in effect?
    – x-yuri
    Nov 13, 2015 at 7:53
  • yes, looks like there is no cgroup configured under memory controller/subsystem
    – VenkatC
    Nov 13, 2015 at 13:16
  • cat lxc/sta/memory.limit_in_bytes - is this file on container or host? what's the full path? If it's for container, looks like it's not being applied to container
    – VenkatC
    Nov 13, 2015 at 13:25
  • looks like there is no cgroup configured under memory controller/subsystem How do I configure it? cat lxc/sta/memory.limit_in_bytes - is this file on container or host? on both, full path is /sys/fs/cgroup/memory/lxc/sta/memory.limit_in_bytes that's the privileged container, if anything
    – x-yuri
    Nov 13, 2015 at 14:18
  • Configuration seems fine, but I am not sure, why it's not applied though. Do you have other containers with memory limit set? or Is this your first config? I had to set both cgroup_enable=memory and swapaccount=1 in grub for the limits to work github.com/docker/docker/issues/396
    – VenkatC
    Nov 13, 2015 at 14:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .