Bug 42889

Summary: Thread pooling declaration fix and enhancements
Product: APR Reporter: Joe Mudd <Joe.Mudd>
Component: APR-utilAssignee: Apache Portable Runtime bugs mailinglist <bugs>
Status: RESOLVED FIXED    
Severity: normal CC: henryjen
Priority: P2 Keywords: PatchAvailable
Version: HEAD   
Target Milestone: ---   
Hardware: Other   
OS: other   
Bug Depends on:    
Bug Blocks: 43876    
Attachments: source updates
APU_DECLARE and new stat prototypes
apr_thread_pool.c.diff.apu APU updates
apr_thread_pool.c.diff.timeout Time out updates
apr_thread_pool.c.diff.stats Statistics updates
apr_thread_pool.h.diff.apu APU updates
apr_thread_pool.h.diff.timeout Time out updates
apr_thread_pool.h.diff.stats Statistics updates
timeout (idle wait) update
stats patch update
timeout (idle wait) update (typo correction)

Description Joe Mudd 2007-07-13 07:57:36 UTC
The thread pooling support (apr_thread_pool.[ch]) did not follow the convention 
of apr-utils services using APU_DECLARE vs APR_DECLARE.

Added the following enhancements to the thread pooling support:

1) Time out of idle threads vs hard idle_max reap.  Helps reduce thrashing
   of threads when requests are receive in bursts and allows idle to be set
   to zero w/o being penalized for thread create/destroy.
2) More statistics (#of tasks processed, high water mark of queued tasks,
   high water mark of threads, #of idle threads that timed out waiting for
   work).

These updates are contained w/in diff files that will be attached momentarily.
Comment 1 Joe Mudd 2007-07-13 07:59:02 UTC
Created attachment 20503 [details]
source updates
Comment 2 Joe Mudd 2007-07-13 07:59:29 UTC
Created attachment 20504 [details]
APU_DECLARE and new stat prototypes
Comment 3 Davi Arnaut 2007-07-13 08:08:43 UTC
Quick review. Why did you removed the doxygen group tags? Could you separate
the patches in three (APU_DECLARE, timeout, statistics)? Thanks.
Comment 4 Joe Mudd 2007-07-13 08:12:05 UTC
I must have missed the group update.  I will resync the files and submit the 
patches separately as requested.

Do I need to submit three different bug reports for the separated patches?  Or, 
is one bug ok?
Comment 5 Davi Arnaut 2007-07-13 08:19:48 UTC
(In reply to comment #4)
> I must have missed the group update.  I will resync the files and submit the 
> patches separately as requested.

Thanks.

> Do I need to submit three different bug reports for the separated patches?  Or, 
> is one bug ok?

Just this one is ok.
Comment 6 Davi Arnaut 2007-07-13 09:03:27 UTC
CC'ing the original author.
Comment 7 Henry Jen 2007-07-13 13:48:49 UTC
Nice. :-)

One question: since the total number of tasks processed(tasks_run) is collected
over time and could overflow, would it better to collect in interval? Anyway,
that may not be a big concern.

The code stopping the timeout idle threads is not quite correct:

1. The thread should only be joined when the thread is stopped in idle mode by
trim_idle_thread, i.e, when elt->stop is 1. Otherwise, the thread should be
detached and exit as no one would be there join the thread.

2. The thd_timed_out should be increased for the thread to quit after time out,
ie. when elt->stop == 0. The code seems to have an unintended else with the if
statement.

Comment 8 Joe Mudd 2007-07-13 13:58:11 UTC
Oh, shoot!  Thanks!

I'll make these changes before submitting the updated diffs.

I'm assuming the diffs should contain:

1) existing source to source w/APU updates
2) source w/APU updates to source w/time out updates
3) source w/APU & time out updates to source w/additional statistics
Comment 9 Joe Mudd 2007-07-13 14:24:56 UTC
Created attachment 20505 [details]
apr_thread_pool.c.diff.apu  APU updates

Replacing big attachments w/incremental attachments
Comment 10 Joe Mudd 2007-07-13 14:25:44 UTC
Created attachment 20506 [details]
apr_thread_pool.c.diff.timeout Time out updates
Comment 11 Joe Mudd 2007-07-13 14:26:16 UTC
Created attachment 20507 [details]
apr_thread_pool.c.diff.stats Statistics updates
Comment 12 Joe Mudd 2007-07-13 14:26:43 UTC
Created attachment 20508 [details]
apr_thread_pool.h.diff.apu APU updates
Comment 13 Joe Mudd 2007-07-13 14:27:06 UTC
Created attachment 20509 [details]
apr_thread_pool.h.diff.timeout Time out updates
Comment 14 Joe Mudd 2007-07-13 14:27:39 UTC
Created attachment 20510 [details]
apr_thread_pool.h.diff.stats Statistics updates
Comment 15 Davi Arnaut 2007-07-22 12:30:14 UTC
APU updates patch committed to trunk in revision 558527:

http://svn.apache.org/viewvc?view=rev&revision=558527
Comment 16 Henry Jen 2007-07-23 19:48:59 UTC
Created attachment 20539 [details]
timeout (idle wait) update

The earlier patch may miss the scheduled task if the idle_wait is set and
idle_max is larger than 0. This updated patch should address the scheduled
task.

The patch also refactoring a bit to use a state rather than a boolean to signal
a thread's exit. This eliminate the additional code to quit the thread.
Comment 17 Henry Jen 2007-07-23 20:36:19 UTC
Created attachment 20540 [details]
stats patch update

A minor update to reflect changes on timeout
Comment 18 Joe Mudd 2007-07-24 05:12:01 UTC
Created attachment 20542 [details]
timeout (idle wait) update (typo correction)

Fix typo in apr_thread_pool_idle_wait_set() @param timeout documentation. 
'timeout' is microseconds... not milliseconds.

And thanks for putting this together so quickly.
Comment 19 Joe Mudd 2007-07-25 06:53:40 UTC
I applied the latest patches and observed that once a thread was put on 
probation, it was reaped even if it was woken up to process a task (vs actually 
timing out).

Shouldn't a thread on probation that was woken to process a task have its state 
transition back to run?

Thanks for your consideration.
Comment 20 Henry Jen 2007-07-25 22:34:11 UTC
I considered that, and I have no preference to either way.

The reason I left it out is because the thread will only stop after becoming
idle again, i.e., no other tasks in the queue. While this most likely mean all
other threads will be idle as well and unless the max_idle was changed, the
thread need to be stopped anyway.

Make sense?
Comment 21 Joe Mudd 2007-07-26 05:01:31 UTC
Yes, the implementation makes sense and I can go either way as well.

My comment was more of from a thread in hand position.  I was thinking that if 
the thread didn't really timeout, then there must be enough work to keep it 
busy and it should be given another chance to waste away.

As an aside, I updated the thread pool code to change the state from probation 
to run after a task had been processed.  In my use of the thread pool (~50 
second run on dual core Wintel), giving an idle thread that woke up and did 
work another chance changed the number of reaped idle threads from 28 to 17 and 
shaved around 4 seconds off of the run.

Thanks again for the implementation and your very prompt attention.
Comment 22 Joe Mudd 2007-09-17 13:51:17 UTC
I haven't seen any activity since my last response.  Is more information still 
required?  Thanks.
Comment 23 Joe Mudd 2007-11-06 12:50:54 UTC
I believe this is ready to roll so I am reassigning back to the folks that 
handle APR updates.
Comment 24 William A. Rowe Jr. 2008-05-07 12:51:11 UTC
It's my understanding from Henry that these patches are no longer required
as the patch for 43876 is now committed.  Please correct my understanding
if this is not true.
Comment 25 Joe Mudd 2008-05-07 13:25:10 UTC
Your understanding is correct.  43876 included these fixes.