NPTL closes maximum flows at 65528?

The following code should do 100,000 threads:

/* compile with: gcc -lpthread -o thread-limit thread-limit.c */ /* originally from: http://www.volano.com/linuxnotes.html */ #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <pthread.h> #include <string.h> #define MAX_THREADS 100000 int i; void run(void) { sleep(60 * 60); } int main(int argc, char *argv[]) { int rc = 0; pthread_t thread[MAX_THREADS]; printf("Creating threads ...\n"); for (i = 0; i < MAX_THREADS && rc == 0; i++) { rc = pthread_create(&(thread[i]), NULL, (void *) &run, NULL); if (rc == 0) { pthread_detach(thread[i]); if ((i + 1) % 100 == 0) printf("%i threads so far ...\n", i + 1); } else { printf("Failed with return code %i creating thread %i (%s).\n", rc, i + 1, strerror(rc)); // can we allocate memory? char *block = NULL; block = malloc(65545); if(block == NULL) printf("Malloc failed too :( \n"); else printf("Malloc worked, hmmm\n"); } } sleep(60*60); // ctrl+c to exit; makes it easier to see mem use exit(0); } 

It runs on a 64-bit machine with 32 GB of RAM; Debian 5.0 installed, all stocks.

  • ulimit -s 512 save stack size
  • / proc / sys / kernel / pid_max is set to 1,000,000 (by default it is disabled at 32k pids).
  • ulimit -u 1,000,000 to maximize max processes (don't think it matters at all)
  • / proc / sys / kernel / threads-max is set to 1,000,000 (by default it was not installed at all)

By running this, do the following:

 65500 threads so far ... Failed with return code 12 creating thread 65529 (Cannot allocate memory). Malloc worked, hmmm 

I, of course, lack a ram; I can even run several more of these programs, all running at the same time, and they all start their 65k threads.

(Please refrain from suggesting that I am not trying to start 100,000+ threads. This is a simple test of what should work. My current epoll-based server has approximately 200k + connections at any time and various documents would suggest that the threads may be the best option. - Thank you :))

+5
source share
4 answers

the mention of /proc/sys/vm/max_map_count piccra refers to the track; increasing this value allows you to open more threads; I'm not sure which formula is involved, but a value of 1mil + allows you to use streams with a length of about 300 KB.

(For others who are experimenting with 100k + threads, look at the pthread_create mmap problems ... creating new threads becomes very slow very fast when lower memory is used.)

+6
source

One possible problem is the local thread variable in the main program. I think pthread_t will be 8 bytes on your 64-bit machine (assuming a 64-bit build). This will be 800,000 bytes on the stack. I think your 512K stack limit will be a problem. 512K / 8 = 65536, which is suspiciously close to the number of threads you create. You can try dynamically allocating this array instead of pushing it on the stack.

0
source

This can help set the stack size in the program as small as possible (if this is not enough):

 /* compile with: gcc -lpthread -o thread-limit thread-limit.c */ /* originally from: http://www.volano.com/linuxnotes.html */ #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <pthread.h> #include <string.h> #define MAX_THREADS 100000 int i; void run(void) { sleep(60 * 60); } int main(int argc, char *argv[]) { int rc = 0; pthread_t thread[MAX_THREADS]; pthread_attr_t thread_attr; pthread_attr_init(&thread_attr); pthread_attr_setstacksize(&thread_attr, PTHREAD_STACK_MIN); printf("Creating threads ...\n"); for (i = 0; i < MAX_THREADS && rc == 0; i++) { rc = pthread_create(&(thread[i]), &thread_attr, (void *) &run, NULL); if (rc == 0) { pthread_detach(thread[i]); if ((i + 1) % 100 == 0) printf("%i threads so far ...\n", i + 1); } else { printf("Failed with return code %i creating thread %i (%s).\n", rc, i + 1, strerror(rc)); // can we allocate memory? char *block = NULL; block = malloc(65545); if(block == NULL) printf("Malloc failed too :( \n"); else printf("Malloc worked, hmmm\n"); } } sleep(60*60); // ctrl+c to exit; makes it easier to see mem use exit(0); } 

in addition, you can add a call as follows: pthread_attr_setguardsize(&thread_attr, 0); right after calling pthread_attr_setstacksize() , but then you completely lose the definition and it only saves 4k address space and zero actual memory.

0
source

Are you trying to find a formula for calculating the maximum flows for each process?

Linux implements the maximum number of threads per process indirectly !!

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

Thus, the number of threads per process can be increased by increasing the total virtual memory or reducing the size of the stack. But too large a stack size can lead to code failure due to, while the maximum virtual memory is equal to the swap memory.

Check out the car:

Shared virtual memory: ulimit -v (unlimited by default, so you need to increase swap memory to increase it)

Total stack size: ulimit -s (default 8 MB)

The command to increase these values:

 ulimit -s newvalue ulimit -v newvalue 

* Replace the new value with the value you want to set as a constraint.

Literature:

http://dustycodes.wordpress.com/2012/02/09/increasing-number-of-threads-per-process/

0
source

All Articles