Score:0

How to Limit CPU Cores in Ubuntu / Virtualmin?

is flag

SYSTEM INFORMATION
OS type and version
*Ubuntu Linux 20.04.4

Virtualmin version 6.17-3*

I am sole owner of my server and all the virtual servers. I want to limit the resources being used by servers. Currently I set up ram usage by editing limits.conf file like this -

oneofmyuser hard memlock 1024

Now im confused with the cpu option there in example codes -

#Where:
#<domain> can be:
#        - a user name
#        - a group name, with @group syntax
#        - the wildcard *, for default entry
#        - the wildcard %, can be also used with %group syntax,
#                 for maxlogin limit
#        - NOTE: group and wildcard limits are not applied to root.
#          To apply a limit to the root user, <domain> must be
#          the literal username root.
#
#<type> can have the two values:
#        - "soft" for enforcing the soft limits
#        - "hard" for enforcing hard limits
#
#<item> can be one of the following:
#        - core - limits the core file size (KB)
#        - data - max data size (KB)
#        - fsize - maximum filesize (KB)
#        - memlock - max locked-in-memory address space (KB)
#        - nofile - max number of open file descriptors
#        - rss - max resident set size (KB)
#        - stack - max stack size (KB)
#        - cpu - max CPU time (MIN)
#        - nproc - max number of processes
#        - as - address space limit (KB)
#        - maxlogins - max number of logins for this user
#        - maxsyslogins - max number of logins on the system
#        - priority - the priority to run user process with
#        - locks - max number of file locks the user can hold
#        - sigpending - max number of pending signals
#        - msgqueue - max memory used by POSIX message queues (bytes)
#        - nice - max nice priority allowed to raise to values: [-20, 19]
#        - rtprio - max realtime priority
#        - chroot - change root to directory (Debian-specific)

How can I limit my user to use only 1 or 2 cores? There is core value but it should be in Kb? I want to limit to for example 2 cores? So how many kb? Also for cpu ? What are the ideal values for these options for a normal server with normal wordpress site ?

Score:1
no flag

On Linux, you can pin a process to a cpu with taskset. (See man taskset). (See also https://unix.stackexchange.com/questions/425065/linux-how-to-know-which-processes-are-pinned-to-which-core)

You can also use cpulimit (see man cpulimit) which uses control groups to set a cpu usage limit where 100 would be the equivalent of one CPU.

To use either of these, you will have to find out which process is launched for the user, and append the above commands. I'm not familiar with Virtualmin, but it being open source I suspect that you will be able to find out where it launches these processes fairly easily, by grepping the source code.

The CPU time in limits.conf is CPU time in minutes as shown in the Time column by top so it's only a measure of how long the process is allowed to run exclusively.

The "core" value in limits.conf (see man limits.conf) refers to the size of a core dump file, which is a dump you can enable of a process if it crashes, for debugging.

The background to this is that Linux was designed to do exactly the opposite - spread tasks across CPUs - and it's exceedingly good at that. The only reason to reasonably limit tasks to a CPU on a multi core machine, is to do something with perfect timing - or timing attacks.

Other options are to lock the OS to certain cores from the bootloader, or you can emulate singular CPU's or run containers pinned to certain CPUs.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.