Maximum thread number linux

What is a limit for number of threads?

I was wondering how many processes can I create on my machine (x64 with 8Gb of RAM and running Ubuntu). So I made simple master process which was continiously creating child processes, and that child processes were just sleeping all the time. I ended with just 11-12k processes. Then I switched processes to threads and got exactly same result. My pid_max is set to 32768, all per-user limits are disabled. Physical memory usage is just couple of bytes. Could you tell me what prevents the system to create new threads at that point? p.s. here is my source code for multiprocessing test written in C

#include #include int main() < pid_t pid; int count = 0; while (1) < pid = fork(); if (pid == -1) < printf("total: %d\n", count); return 0; >if (pid == 0) < while (1) sleep(10); >count++; > > 

But these bottlenecks are hitting painfully long before you reach hard limits (on number of threads or processes). So you really should benchmark your actual code.

1 Answer 1

I think you hit either a number of processes limit or a memory limit.

When I try your program on my computer and reach the pid == -1 state, fork() returns the error EAGAIN , with error message: Resource temporarily unavailable . As a normal user, I could create approx 15k processes.

There are several reasons this EAGAIN could happen, detailed in man 2 fork :

  • not enough memory,
  • hitting a limit like RLIMIT_NPROC,
  • deadline scheduler specifics.

In my case, I think I just hit the RLIMIT_NPROC limit, aka what ulimit -u usually shows. The best is to display this limit within the program, so you have the real value, not your shell’s limits.

RLIMIT_NPROC soft: 15608, hard: 15608 total: 15242 

Which looks reasonable as I have other processes running, including a web browser.

Now, as root, the limits don’t really apply anymore and I could fork() much more: I created more than 30k processes, close to my 32k pid_max .

Now, if I take my normal user shell’s PID ( echo $$ ), and as root in another shell, I do: prlimit —pid $SHELLPID —nproc=30000 , and then launch your program in this shell, I can create almost 30k processes:

RLIMIT_NPROC soft: 30000, hard: 30000 total: 29678 

Finally: you should also consider memory usage, because on my system, I used a lot of RAM and swap to create all those processes, and maybe it was the limit you hit. Check with free .

Источник

Maximum Number of Threads per Process in Linux

announcement - icon

The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

Читайте также:  Linux размер папок рекурсивно

Connect your cluster and start monitoring your K8s costs right away:

1. Overview

As we know, a process is a currently running program while a thread is a semi-process or a lightweight process.

In this tutorial, we’ll discuss what a multi-threaded process is. Also, we will learn why there’s a limit on the number of threads per process in Linux and what that limit is.

2. What Is a Multi-Threaded Process?

The provision of multiple threads of execution within the same program in shared memory space is called multithreading.

A multithreaded process enables us to run multiple threads concurrently. The purpose of multithreading is to increase performance.

For optimal performance, Linux has a limit on the number of threads. The threads-max kernel parameter can be set to ensure that the number of threads per process is always less than or equal to that limit. However, other factors like the amount of virtual memory and stack size may indirectly govern the number of threads allocated to a process.

3. Limit on Thread Count per Process

Linux has a setting for maximum threads per process, which specifies the maximum number of simultaneous executions that the process can handle.

Changes to this can throttle the process and minimize latencies for the executions that happen.

Reaching this limit means that the process needs that many threads at peak load. But, as long as it can serve requests in a timely manner, the process is adequately tuned.

However, when the limit is reached, threads queue up, potentially overloading the process. At this point, the process defers creating new threads until the number of active threads drops below the limit.

4. How to Retrieve Maximum Thread Count

The kernel parameter threads-max controls the maximum number of threads.

This parameter is defined in the file /proc/sys/kernel/threads-max.

Let’s view this file using the cat command:

$ cat /proc/sys/kernel/threads-max 63704

Here, the output 63704 indicates that the kernel can execute a maximum of 63,704 threads.

Alternatively, we can use the sysctl command to retrieve the threads-max value:

$ sysctl -a | grep threads-max kernel.threads-max = 63704

kernel.pid_max and vm.max_map_count specify two other limits that will also block the new thread creation at peak load.

The pid_max parameter specifies the value at which PIDs wrap around:

$ cat /proc/sys/kernel/pid_max 131072

The kernel.pid_max value of 131072 above means the kernel can execute a maximum of 131,072 processes simultaneously.

The max_map_count parameter specifies the maximum number of Virtual Memory Areas (VMAs) that a process can own:

$ cat /proc/sys/vm/max_map_count 65530

The vm.max_map_count value of 65530 above is the maximum number of memory map areas a process may have.

The Linux kernel handles both a process and a thread in the same way. So, values limiting the number of processes will indirectly also limit the number of threads.

Therefore, kernel.pid_max must be larger than the total number of simultaneous threads and processes.

Читайте также:  Make my own linux distro

Having many threads may consume too much memory for the server to work. vm.max_map_count limits both the virtual memory and the number of threads that require this memory for setting up their own private stack.

On systemd systems, the cgroup pids.max parameter enforces another limit. This is set to 12,288 by default. On certain occasions, this default resource limitation is not sufficient, or perhaps too restrictive.

Alternatively, performing specific adjustments on some of the systemd’s TasksMax settings may prove to be useful. The UserTasksMax parameter in the [Login] section of /etc/systemd/logind.conf overrides the default limit:

$ grep -i "^UserTasksMax" /etc/systemd/logind.conf UserTasksMax=50000

This is exactly how systemd also applies a thread limit for programs run from a login shell.

5. How to Set Maximum Thread Count

There are several ways to set the value for the maximum number of threads per process. These values determine how many threads a process will be allowed.

Let’s temporarily set the threads-max kernel parameter at runtime:

$ echo 120000 > /proc/sys/kernel/threads-max

We can permanently set the kernel.threads-max parameter by adding kernel.threads-max= to the /etc/sysctl.conf file:

$ sysctl -w kernel.threads-max=120000 >> /etc/sysctl.conf

Next, setting the pid_max parameter to 200000 means the kernel can execute a maximum of 200,000 processes simultaneously:

$ echo 200000 > /proc/sys/kernel/pid_max

Similarly, setting the max_map_count parameter to 600000 means a process can own a maximum number of 600,000 Virtual Memory Areas (VMAs):

$ echo 600000 > /proc/sys/vm/max_map_count

On systemd systems, the UserTasksMax specifies the TasksMax setting for all users and determines the thread limit:

$ sed -i "s/^UserTasksMax/#UserTasksMax/" /etc/systemd/system.conf $ echo "UserTasksMax=60000" >> /etc/systemd/system.conf
$ grep -i "UserTasksMax" /etc/systemd/logind.conf #UserTasksMax=50000 UserTasksMax=60000

6. Factors That Affect Maximum Thread Count

Although there are system parameters that set a limit on the number of threads per process, the OS and memory likely become the limiting factors well before that.

The limit on the number of threads a process can have is calculated using the formula:

number of threads = total virtual memory / (stack size*1024*1024)

Thus, the number of threads per process can be increased by increasing total virtual memory. The amount of stack size per thread is more likely to be the limit than anything else. Reducing the per-thread stack size is also a way to increase the total number of threads.

We can check the stack size per thread with ulimit:

$ ulimit -a | grep "stack size" stack size (kbytes, -s) 10240

The value signifies that each of the threads will get this amount of memory (10MB) assigned for its stack. With a 32-bit program and a maximum address space of 4GB, the maximum number of threads will be:

On a 64-bit processor, we can adjust the stack size per thread with ulimit:

7. Conclusion

In this article, we got an understanding of what a multi-threaded process in Linux is. We also learned the significance of the maximum number of threads per process count in Linux. Finally, we saw how to retrieve and set maximum threads per process count and the factors that affect it.

Источник

How to find the max no of threads spawned by one system?

Is there a way / program to find out the maximum no of threads a system can spawn ? i am creating a application and i am in a dilemma whether to go with event looping model or multi threaded model . so wanted to test the systems capabilities on how many threads it can handle ?

4 Answers 4

The «maximum number of threads» is not as useful a metric as you might think:

  • There is usually a system-wide maximum number of threads imposed by either the operating system or the available hardware resources.
  • The per-process maximum number of threads is often configurable and can even change on-the-fly.
  • In most cases the actual restriction comes from your hardware resources — rather than any imposed limit. Much like any other resource (e.g. memory) you have to check if you were successfull, rather than rely on some kind of limit.

In general, multi-threading has only two advantages when compared to event loops:

  • It can utilise more than one processor. Depending on the operating system you can also use multiple processes (rather than the more lightweight threads) to do that.
  • Depending on the operating system, it may offer some degree of privlilege separation.

Other than that multi-threading is usually more expensive in both memory and processing resources. A large number of threads can bring your system to a halt regardless if what they are doing is resource-intensive or not.

In most cases the best solution is a hybrid of both models i.e. a number of threads with an event loop in each one.

On modern Linux systems, the /proc/sys/kernel/threads-max file provides a system-wide limit for the number of threads. The root user can change that value if they wish to:

echo 100000 > /proc/sys/kernel/threads-max

As far as I know, the kernel does not specifically impose a per-process limit on the number of threads.

sysconf() can be used to query system limits. There are some semi-documented thread-related query variables defined in /usr/include/bits/confname.h (the _SC_THREAD* variables).

getrlimit() can be used to query per-session limits — in this case the RLIMIT_NPROC resource is related to threads.

The threading implementation in glibc may also impose its own limits on a per-process basis.

Keep in mind that, depending on your hardware and software configuration, none of these limits may be of use. On Linux a main limiting factor on the number of threads comes from the fact that each thread requires memory in the stack — if you start launching threads you can easily come upon this limit before any others.

If you really want to find the actual limit, then the only way is to start launching threads until you cannot do so any more. Even that will only give you a rough limit that is only valid at the time you run the program. It can easily change if e.g. your threads start doing actual work and increase their resource usage.

In my opinion if you are launching more than 3-4 threads per processor you should reconsider your design.

Источник

Оцените статью
Adblock
detector