A way to limit CPU usage

Hi. :slight_smile: I was looking at how to limit restic’s CPU usage. For exmaple, my Mac has 8 logical cores, and I would like for restic to run in the background using at most 1 or 2 cores sometimes.

I was looking for a CLI flag and didn’t see one, but then I remembered GOMAXPROCS. It’s an easy way to limit the number of system threads the Go runtime can create: GOMAXPROCS

For anyone else wondering, I think this works:

GOMAXPROCS=1 restic ...

even without a CLI flag.

Just wanted to post this in case anyone else had any issues about it, and to let the restic developers know I am utilizing this (accidental?) feature. Thanks for making restic awesome!

3 Likes

Cool! Will bookmark your post. :smiley:

What you could also try to do is use nice to set the priority for the restic process, so other processes are preferred for CPU time, restic can use all idle CPU capacity.

1 Like

Cool – does that work on Windows too? (I don’t use Windows but am curious since my family members use Windows!)

Uh, I must admit that I have no idea, sorry :slight_smile:

1 Like

You could use start on Windows.

man start
> start /?
Starts a separate window to run a specified program or command.

START ["title"] [/D path] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED]
      [/LOW | /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /BELOWNORMAL]
      [/NODE <NUMA node>] [/AFFINITY <hex affinity mask>] [/WAIT] [/B]
      [command/program] [parameters]

    "title"     Title to display in window title bar.
    path        Starting directory.
    B           Start application without creating a new window. The
                application has ^C handling ignored. Unless the application
                enables ^C processing, ^Break is the only way to interrupt
                the application.
    I           The new environment will be the original environment passed
                to the cmd.exe and not the current environment.
    MIN         Start window minimized.
    MAX         Start window maximized.
    SEPARATE    Start 16-bit Windows program in separate memory space.
    SHARED      Start 16-bit Windows program in shared memory space.
    LOW         Start application in the IDLE priority class.
    NORMAL      Start application in the NORMAL priority class.
    HIGH        Start application in the HIGH priority class.
    REALTIME    Start application in the REALTIME priority class.
    ABOVENORMAL Start application in the ABOVENORMAL priority class.
    BELOWNORMAL Start application in the BELOWNORMAL priority class.
    NODE        Specifies the preferred Non-Uniform Memory Architecture (NUMA)
                node as a decimal integer.
    AFFINITY    Specifies the processor affinity mask as a hexadecimal number.
                The process is restricted to running on these processors.

                The affinity mask is interpreted differently when /AFFINITY and
                /NODE are combined.  Specify the affinity mask as if the NUMA
                node's processor mask is right shifted to begin at bit zero.
                The process is restricted to running on those processors in
                common between the specified affinity mask and the NUMA node.
                If no processors are in common, the process is restricted to
                running on the specified NUMA node.
    WAIT        Start application and wait for it to terminate.
    command/program
                If it is an internal cmd command or a batch file then
                the command processor is run with the /K switch to cmd.exe.
                This means that the window will remain after the command
                has been run.

                If it is not an internal cmd command or batch file then
                it is a program and will run as either a windowed application
                or a console application.

    parameters  These are the parameters passed to the command/program.

Your post gave me the idea to start experimenting. So don’t have any actual feedback yet, but I’m experimenting with: start /WAIT /B /LOW restic ...

1 Like

Thanks for this info. appreciate it.

I’m backing up my local windows machine to a remote server using Cygwin as a unix emulator and nice works like a dream :slight_smile:

1 Like

I know this is an old thread, but maybe this will help others in my situation. I have a 40GB dataset on a multi-tenant server and the restic backup command kept crashing. The server has lots of cores, but the account was limited by number of processes. I tried GOMAXPROCS, GOGC, nice and more to no avail. It turns out taskset helped me limit the cores available to the restic and prevent it from spawning too many processes.

How to limit a process to one CPU core in Linux? - Unix & Linux Stack Exchange lead me to the answer.