Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat Current thread count

Tomcat and appache2 are connected through AJP. I have these configurations for tomcat and apache:

<Connector port="8009" protocol="AJP/1.3"
           URIEncoding="UTF-8"
           redirectPort="8443"
           connectionTimeout="20000"
           maxThreads="512" />


<IfModule mpm_event_module>
    StartServers          5
    MinSpareThreads      25
    MaxSpareThreads      75
    ThreadLimit          64
    ThreadsPerChild      25
    ServerLimit        1024
    MaxClients          512
    MaxRequestsPerChild   0
</IfModule>

When I go to tomcat manager, I see :

"ajp-bio-8009"
Max threads: 512 Current thread count: 256 Current thread busy: 231

The current thread busy sometimes reaches 256. Why isn't the current thread count set to 512?

like image 736
mossaab Avatar asked Nov 04 '22 09:11

mossaab


1 Answers

The current thread is dynamically managed by apache.

from worker.c in apache's source code: thread is started as needed. following is the main flow to start worker thread.

static int worker_run(apr_pool_t *_pconf, apr_pool_t *plog, server_rec *s)
  startup_children(remaining_children_to_start);  //server/mpm/worker/worker.c
     if (make_child(ap_server_conf, i) < 0) {     //server/mpm/worker/worker.c
       child_main(slot);                          //server/mpm/worker/worker.c
         rv = apr_thread_create(&start_thread_id, thread_attr, start_threads
         rv = apr_thread_create(&threads[i], thread_attr, worker_thread, my_info, pchild);     

And when idle thread > max_spare_threads, apache will try to reduce the spare thread by kill child process/thread to reduce the idle threads count.

if (idle_thread_count > max_spare_threads) {
    /* Kill off one child */
    ap_worker_pod_signal(pod, TRUE);
    retained->idle_spawn_rate = 1;
}

internally apache manage these child process, and using signal to communicate with child process to adjust threads as needed. all threads are managed in ap_scoreboard_image->servers[i][j];

apache will monitor the thread count, and using socket send out to let infoCollector know. roughly by following code (unrelated line were deleted)

./httpd/modules/cluster/mod_heartbeat.c
    for (i = 0; i < ctx->server_limit; i++) {
         ....
            for (j = 0; j < ctx->thread_limit; j++) {
                ws = &ap_scoreboard_image->servers[i][j];
                if (res == SERVER_READY && ps->generation == mpm_generation) {
                    ready++;
                }
            }
        }

    len = apr_snprintf(buf, sizeof(buf), "v=%u&ready=%u&busy=%u", MSG_VERSION, ready, busy);
    ...
    rv = apr_socket_sendto(sock, ctx->mcast_addr, 0, buf, &len);

In Tomcat: these thread info are received by: ./tomcat/java/org/apache/catalina/ha/backend/CollectedInfo.java and displayed by: /Users/twer/lab/tomcat/java/org/apache/catalina/manager/StatusTransformer.java

writer.write(" currentThreadCount=\"" + mBeanServer.getAttribute(tpName, "currentThreadCount") + "\"");

most of the code show here are main flow, unrelated line are deleted. download the source to find out more. download apache src: http://www.apache.org/dist/httpd/?C=S;O=A download tomcat src: http://tomcat.apache.org/download-70.cgi

like image 120
whunmr Avatar answered Nov 10 '22 05:11

whunmr