From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fc89c,bf2ea6865e8fab59,start X-Google-Attributes: gidfc89c,public X-Google-Thread: fee84,bf2ea6865e8fab59,start X-Google-Attributes: gidfee84,public X-Google-Thread: 10650a,bf2ea6865e8fab59,start X-Google-Attributes: gid10650a,public From: heiby_u@falkor.chi.il.us (Ron Heiby) Subject: Lots of Threads = Death? Date: 1998/04/28 Message-ID: <35464b25.29547822@149.174.211.108> X-Deja-AN: 348574462 Content-Transfer-Encoding: 7bit MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Organization: Strategis Consulting Inc. Newsgroups: comp.unix.osf.osf1,comp.unix.internals,comp.unix.programmer Hi! I'm seeing some strange goings on with creation and destruction of lots of threads. I'm using a DEC Alpha system with 128Meg of RAM running 3.2G. Before the start of each test, the system had approximately 9250 pages of free memory, according to vmstat. As a test, I wrote a program that creates a thread (pthread_create), passing it a value that came from the command line (int). The initial thread then does a join on the thread it created, effectively blocking until that thread terminates. The newly created thread checks to see if the value it got is zero. If it's zero, it just blocks on a gets() call. If it's > 0, then the thread creates a thread and does a join on that thread. The new new thread is passed a value one less than its parent received, and is instructed to start executing the same function that its parent was executing. This has the effect of causing N threads to be created, with no more than one thread created by each thread. I used vmstat to monitor the progress of this process' execution and the system resources that were being used along the way. At an appropriate point, after vmstat showed the system stable again, I press Enter, satisfying the last thread's gets() call. It exits, causing its parent thread's join to return. When the join returns, the thread exits. Thus, the satisfaction of the gets() causes (eventually) all of the threads to terminate. The initial thread, when its join returns, displays a message and waits on another gets() call. So, we've got something fairly simple here. A bunch of threads get created and do nothing but create a thread and wait for it to exit. Eventually, the waits are satisfied and all the threads exit, leaving just the initial thread. After running this program with just a few to a dozen threads, to verify its correct operation, I opened it up. I tried 2000 threads. (We had earlier tuned the kernel to allow 4000 threads per user.) Watching vmstat's output, I saw pretty much what I expected to see. I saw the number of waiting threads increase by 2000. I saw the amount of free memory drop. I saw a lost of user and system cpu time. But, all the threads got created in about 10 seconds. The system stayed pretty happy throughout the several minutes I allowed it to sit there with the extra 2000 waiting threads. Then, I hit the Enter key and watched the threads self-destruct. First, I was surprised to see even more memory being used as the threads exited. There was a spike in zero-fill page requests of at least one per thread. Also, the thread destructions took significantly longer to finish than their creation had. Another thing that surprised me was that the available memory did not all return until the initial thread (and the process as a whole) terminated. This implies that the pages allocated for the threads were being allocated within the context of the process, and were not being given back to the system until process termination. Deciding to push the envelope a bit, I fired up my program for 2500 threads. This behaved much like the 2000 thread case, except that it drove available memory lower. Based on something I'd seen in an earlier test, I pushed a bit further, to 2600 threads. This time, the number of free pages was reduced to about 100, at which point it appears that a kernel process kicked in to attempt to recover free memory space. After a while, it had managed to return the free memory reading to a little over 3500 pages. During the time it was doing its thing, however, the number of waiting threads changed. The number stopped reflecting the 2600 threads in my program, returning to the value it had before I started the test. When the memory recover operation concluded, the number of waiting threads returned to its value indicating my waiting threads. It's as though they vanished for a minute or so, then re-appeared. They did not appear on either of the other two columns in vmstat. I continued to push things until a run where I asked for 5000 threads. It didn't make it. I got an error message where I ran the program: $ ./tc 5000 Main Thread 5000 started - Thread ID= -1071781384 %DECthreads bugcheck (version V3.12-311), terminating execution. % Running on DEC OSF/1 AXP [OSF1 alpha V3.2(62); cpu type 30, configured for 1 % cpu, 1 cpu in box, 128Mb] % Reason: set_noaccess: 12 protecting 0x8010000 to 0x8011fff % See 'cma_dump.log' for state information. Resources lost(coredump) I found both a cma_dump.log file (about 4,700,000 bytes) and a (quite large) core. According to the cma_dump.log, everything was going along pretty well until I got to thread 3761. It's information from the log file is: Thread 3761 (running) "" (0x7eb8018) Scheduling: throughput policy at priority 19 There is 1 thread waiting to join Thread specific data: 1: 0x7ea3f88 Stack: 0x800f788; base is 0x8010000, guard area at 0x8009fff General cancelability enabled, asynch cancelability disabled Current vp is 11285, synch port is 11286 VP state is run: Mach policy throughput, priority 19 (RT kernel), state running; synch port has 0 messages out of 1 Join uses mutex 7598 and condition variable 7521; wait uses mutex 7599 and condition variable 7522 The thread's start function and argument are 0x120013cc0 (0x4d8) The thread's latest errno is 12 So, it looks like I ran out of some kind of space for thread stacks. To try to work around this, I set the thread attributes for thread creation to use a stack size of 16000 (figuring I'd get 16K instead of 32K, allowing me about twice as many threads). Although I can confirm that this value was set, 32K was still given to each thread, causing my next test to blow up at exactly the same point. So, what's up? What am I running out of? What do I need to do to fix it? What should I be doing differently, smarter, better? Thanks!