You are not logged in.

#1 2021-10-29 23:49:06

Alom
Member
Registered: 2021-04-24
Posts: 3

LXD/LXC doesn't show all available CPU

I have  an LXD profile:

config:
  limits.cpu: "4"
  limits.memory: 8GB
  limits.memory.swap: "false"
  linux.kernel_modules: ip_tables,ip6_tables,netlink_diag,nf_nat,overlay
  raw.lxc: "lxc.apparmor.profile=unconfined\nlxc.cap.drop= \nlxc.cgroup.devices.allow=a\nlxc.mount.auto=proc:rw
    sys:rw cgroup:rw"
  security.nesting: "true"
  security.privileged: "false"
description: Large Unprivliged Nested Virtualization
devices:
  eth0:
    name: eth0
    nictype: bridged
    parent: lxdbr0
    type: nic
  root:
    path: /
    pool: pool1
    type: disk
name: t1.large

Which is used by two containers:

$ lxc launch images:ubuntu/focal -p t1.large u1
$ lxc launch images:archlinux  -p t1.large a1

Checking the number of available processes with nproc we see four in both cases.

$  lxc exec a1 nproc
4

$ lxc exec c1 nproc
4

When other applications check the number of CPU, we start to see a difference. First, we'll exec into each container, run top and press 1 to see the individual CPUs. We see that in the archlinux container, there are only two cores:

top - 23:44:37 up 6 min,  0 users,  load average: 0.12, 0.14, 0.16
Tasks:  10 total,   1 running,   9 sleeping,   0 stopped,   0 zombie
%Cpu0  :  1.0 us,  0.0 sy,  0.0 ni, 98.7 id,  0.0 wa,  0.3 hi,  0.0 si,  0.0 st
%Cpu1  :  0.0 us,  0.0 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.3 hi,  0.0 si,  0.0 st
MiB Mem :   7629.4 total,   7472.0 free,     23.2 used,    134.2 buff/cache
MiB Swap:      0.0 total,      0.0 free,      0.0 used.   7606.2 avail Mem 

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND                                                   
    108 root      20   0    7696   3516   2984 R   0.3   0.0   0:00.01 top                                                       
      1 root      20   0  164512  10368   8196 S   0.0   0.1   0:00.27 systemd                                                   
     71 root      20   0   30844  11872  11020 S   0.0   0.2   0:00.10 systemd-journal                                           
     83 root      20   0   26096   7920   6840 S   0.0   0.1   0:00.04 systemd-udevd                                             
     88 systemd+  20   0   15636   6180   5352 S   0.0   0.1   0:00.04 systemd-network                                           
     90 systemd+  20   0   19128  10528   8764 S   0.0   0.1   0:00.08 systemd-resolve                                           
     92 dbus      20   0   12344   5488   4716 S   0.0   0.1   0:00.02 dbus-daemon                                               
     93 root      20   0   14420   6024   5276 S   0.0   0.1   0:00.02 systemd-logind                                            
     95 root      20   0    7656   3900   3404 S   0.0   0.0   0:00.00 agetty                                                    
    100 root      20   0    4496   3832   3268 S   0.0   0.0   0:00.01 bas

but in the Ubuntu container we see all four cores.

top - 23:45:36 up 38 min,  0 users,  load average: 0.24, 0.16, 0.17
Tasks:  13 total,   1 running,  12 sleeping,   0 stopped,   0 zombie
%Cpu0  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu1  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu2  :  0.3 us,  0.0 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu3  :  0.3 us,  0.3 sy,  0.0 ni, 99.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   7629.4 total,   7525.4 free,     32.8 used,     71.1 buff/cache
MiB Swap:      0.0 total,      0.0 free,      0.0 used.   7596.6 avail Mem 

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND                                                   
      1 root      20   0  167416  10480   8272 S   0.0   0.1   0:00.27 systemd                                                   
     65 root      20   0   35092  11304  10404 S   0.0   0.1   0:00.08 systemd-journal                                           
    102 root      20   0   21616   4576   3832 S   0.0   0.1   0:00.02 systemd-udevd                                             
    130 root      20   0    9420   2888   2684 S   0.0   0.0   0:00.00 cron                                                      
    131 message+  20   0    7384   4160   3736 S   0.0   0.1   0:00.02 dbus-daemon                                               
    134 root      20   0   31624  18196  10616 S   0.0   0.2   0:00.10 networkd-dispat                                           
    135 syslog    20   0  154720   4368   3856 S   0.0   0.1   0:00.00 rsyslogd                                                  
    136 root      20   0   16484   6248   5532 S   0.0   0.1   0:00.04 systemd-logind                                            
    137 systemd+  20   0   26612   6604   5796 S   0.0   0.1   0:00.06 systemd-network                                           
    138 systemd+  20   0   23968  12516   8396 S   0.0   0.2   0:00.08 systemd-resolve                                           
    144 root      20   0    8208   2284   2164 S   0.0   0.0   0:00.00 agetty                                                    
    291 root      20   0   11568   3980   3440 S   0.0   0.1   0:00.00 bash                                                      
    298 root      20   0   13448   3728   3204 R   0.0   0.0   0:00.00 top

This is an issue when other applications are trying to use multiple threads and find the wrong number. I was using PyTorch which was only use 2 cores instead of four. We can check this same information with Python's OS module.

$ lxc exec a1 bash
[root@a1 ~]# pacman -Syu python
[root@a1 ~]# python -c "import os;print(os.cpu_count())"
2

Versus Ubuntu:

$ lxc exec u1 bash
root@c1:~# python3 -c "import os;print(os.cpu_count())"
4

I'm not sure what the difference is since they are using the same profile. Is it possible to make a small change and correct this so applications like PyTorch will use all available cores in the container?  As I've shown with nproc, the container can actually use up to four CPU but the applications don't register that unless I force them to start four threads.

Last edited by Alom (2021-10-29 23:52:55)

Offline

Board footer

Powered by FluxBB