]> granicus.if.org Git - apache/blobdiff - server/scoreboard.c
Update the copyright year in all .c, .h and .xml files
[apache] / server / scoreboard.c
index 5fafa952fbb61f5efaae9a49d96398c7f1ac804a..1a5b8047978447e3e65295fe9c91939b2d42237c 100644 (file)
@@ -1,59 +1,17 @@
-/* ====================================================================
- * The Apache Software License, Version 1.1
+/* Copyright 2001-2006 The Apache Software Foundation or its licensors, as
+ * applicable.
  *
- * Copyright (c) 2000-2001 The Apache Software Foundation.  All rights
- * reserved.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
  *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
+ *     http://www.apache.org/licenses/LICENSE-2.0
  *
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in
- *    the documentation and/or other materials provided with the
- *    distribution.
- *
- * 3. The end-user documentation included with the redistribution,
- *    if any, must include the following acknowledgment:
- *       "This product includes software developed by the
- *        Apache Software Foundation (http://www.apache.org/)."
- *    Alternately, this acknowledgment may appear in the software itself,
- *    if and wherever such third-party acknowledgments normally appear.
- *
- * 4. The names "Apache" and "Apache Software Foundation" must
- *    not be used to endorse or promote products derived from this
- *    software without prior written permission. For written
- *    permission, please contact apache@apache.org.
- *
- * 5. Products derived from this software may not be called "Apache",
- *    nor may "Apache" appear in their name, without prior written
- *    permission of the Apache Software Foundation.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
- * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
- * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- * ====================================================================
- *
- * This software consists of voluntary contributions made by many
- * individuals on behalf of the Apache Software Foundation.  For more
- * information on the Apache Software Foundation, please see
- * <http://www.apache.org/>.
- *
- * Portions of this software are based upon public domain software
- * originally written at the National Center for Supercomputing Applications,
- * University of Illinois, Urbana-Champaign.
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
  */
 
 #include "apr.h"
@@ -97,17 +55,20 @@ static /* but must be exported to mpm_winnt */
 APR_HOOK_STRUCT(
     APR_HOOK_LINK(pre_mpm)
 )
+
 AP_IMPLEMENT_HOOK_RUN_ALL(int,pre_mpm,
                           (apr_pool_t *p, ap_scoreboard_e sb_type),
                           (p, sb_type),OK,DECLINED)
 
+static APR_OPTIONAL_FN_TYPE(ap_proxy_lb_workers)
+                                *proxy_lb_workers;
+
 struct ap_sb_handle_t {
     int child_num;
     int thread_num;
 };
 
-static int server_limit, thread_limit;
+static int server_limit, thread_limit, lb_limit;
 static apr_size_t scoreboard_size;
 
 /*
@@ -116,7 +77,7 @@ static apr_size_t scoreboard_size;
  * and it should handle cleaning up a scoreboard shared
  * between processes using any form of IPC (file, shared memory
  * segment, etc.). Leave it as is now because it is being used
- * by various MPMs. 
+ * by various MPMs.
  */
 static apr_status_t ap_cleanup_shared_mem(void *d)
 {
@@ -128,13 +89,24 @@ static apr_status_t ap_cleanup_shared_mem(void *d)
     return APR_SUCCESS;
 }
 
-int ap_calc_scoreboard_size(void)
+AP_DECLARE(int) ap_calc_scoreboard_size(void)
 {
     ap_mpm_query(AP_MPMQ_HARD_LIMIT_THREADS, &thread_limit);
     ap_mpm_query(AP_MPMQ_HARD_LIMIT_DAEMONS, &server_limit);
+
+    if (!proxy_lb_workers)
+        proxy_lb_workers = APR_RETRIEVE_OPTIONAL_FN(ap_proxy_lb_workers);
+    if (proxy_lb_workers)
+        lb_limit = proxy_lb_workers();
+    else
+        lb_limit = 0;
+
     scoreboard_size = sizeof(global_score);
     scoreboard_size += sizeof(process_score) * server_limit;
     scoreboard_size += sizeof(worker_score) * server_limit * thread_limit;
+    if (lb_limit)
+        scoreboard_size += sizeof(lb_score) * lb_limit;
+
     return scoreboard_size;
 }
 
@@ -142,24 +114,30 @@ void ap_init_scoreboard(void *shared_score)
 {
     char *more_storage;
     int i;
-    
-       ap_calc_scoreboard_size();
-    ap_scoreboard_image = 
-        calloc(1, sizeof(scoreboard) + server_limit * sizeof(worker_score *));
+
+    ap_calc_scoreboard_size();
+    ap_scoreboard_image =
+        calloc(1, sizeof(scoreboard) + server_limit * sizeof(worker_score *) +
+               server_limit * lb_limit * sizeof(lb_score *));
     more_storage = shared_score;
     ap_scoreboard_image->global = (global_score *)more_storage;
     more_storage += sizeof(global_score);
     ap_scoreboard_image->parent = (process_score *)more_storage;
     more_storage += sizeof(process_score) * server_limit;
-    ap_scoreboard_image->servers = 
+    ap_scoreboard_image->servers =
         (worker_score **)((char*)ap_scoreboard_image + sizeof(scoreboard));
     for (i = 0; i < server_limit; i++) {
         ap_scoreboard_image->servers[i] = (worker_score *)more_storage;
         more_storage += thread_limit * sizeof(worker_score);
     }
+    if (lb_limit) {
+        ap_scoreboard_image->balancers = (lb_score *)more_storage;
+        more_storage += lb_limit * sizeof(lb_score);
+    }
     ap_assert(more_storage == (char*)shared_score + scoreboard_size);
     ap_scoreboard_image->global->server_limit = server_limit;
     ap_scoreboard_image->global->thread_limit = thread_limit;
+    ap_scoreboard_image->global->lb_limit     = lb_limit;
 }
 
 /**
@@ -174,21 +152,21 @@ static apr_status_t create_namebased_scoreboard(apr_pool_t *pool,
 
     /* The shared memory file must not exist before we create the
      * segment. */
-    apr_file_remove(fname, pool); /* ignore errors */
+    apr_shm_remove(fname, pool); /* ignore errors */
 
     rv = apr_shm_create(&ap_scoreboard_shm, scoreboard_size, fname, pool);
     if (rv != APR_SUCCESS) {
         ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
-                     "unable to create scoreboard "
-                     "(name-based shared memory failure)");
+                     "unable to create scoreboard \"%s\" "
+                     "(name-based shared memory failure)", fname);
         return rv;
     }
 #endif /* APR_HAS_SHARED_MEMORY */
     return APR_SUCCESS;
 }
 
-/* ToDo: This function should be made to handle setting up 
- * a scoreboard shared between processes using any IPC technique, 
+/* ToDo: This function should be made to handle setting up
+ * a scoreboard shared between processes using any IPC technique,
  * not just a shared memory segment
  */
 static apr_status_t open_scoreboard(apr_pool_t *pconf)
@@ -213,7 +191,12 @@ static apr_status_t open_scoreboard(apr_pool_t *pconf)
     if (ap_scoreboard_fname) {
         /* make sure it's an absolute pathname */
         fname = ap_server_root_relative(pconf, ap_scoreboard_fname);
-
+        if (!fname) {
+            ap_log_error(APLOG_MARK, APLOG_CRIT, APR_EBADPATH, NULL,
+                         "Fatal error: Invalid Scoreboard path %s",
+                         ap_scoreboard_fname);
+            return APR_EBADPATH;
+        }
         return create_namebased_scoreboard(global_pool, fname);
     }
     else { /* config didn't specify, we get to choose shmem type */
@@ -248,7 +231,7 @@ apr_status_t ap_reopen_scoreboard(apr_pool_t *p, apr_shm_t **shm, int detached)
         return APR_SUCCESS;
     }
     if (apr_shm_size_get(ap_scoreboard_shm) < scoreboard_size) {
-        ap_log_error(APLOG_MARK, APLOG_CRIT | APLOG_NOERRNO, 0, NULL,
+        ap_log_error(APLOG_MARK, APLOG_CRIT, 0, NULL,
                      "Fatal error: shared scoreboard too small for child!");
         apr_shm_detach(ap_scoreboard_shm);
         ap_scoreboard_shm = NULL;
@@ -284,44 +267,59 @@ apr_status_t ap_cleanup_scoreboard(void *d)
 int ap_create_scoreboard(apr_pool_t *p, ap_scoreboard_e sb_type)
 {
     int running_gen = 0;
+    int i;
 #if APR_HAS_SHARED_MEMORY
     apr_status_t rv;
 #endif
 
     if (ap_scoreboard_image) {
         running_gen = ap_scoreboard_image->global->running_generation;
+        ap_scoreboard_image->global->restart_time = apr_time_now();
+        memset(ap_scoreboard_image->parent, 0,
+               sizeof(process_score) * server_limit);
+        for (i = 0; i < server_limit; i++) {
+            memset(ap_scoreboard_image->servers[i], 0,
+                   sizeof(worker_score) * thread_limit);
+        }
+        /* Clean up the lb workers data */
+        if (lb_limit) {
+            memset(ap_scoreboard_image->balancers, 0,
+                   sizeof(lb_score) * lb_limit);
+        }
+        return OK;
     }
 
-    if (ap_scoreboard_image == NULL) {
-        ap_calc_scoreboard_size();
+    ap_calc_scoreboard_size();
 #if APR_HAS_SHARED_MEMORY
-        if (sb_type == SB_SHARED) {
-            void *sb_shared;
-            rv = open_scoreboard(p);
-            if (rv || !(sb_shared = apr_shm_baseaddr_get(ap_scoreboard_shm))) {
-                return HTTP_INTERNAL_SERVER_ERROR;
-            }
-            memset(sb_shared, 0, scoreboard_size);
-            ap_init_scoreboard(sb_shared);
+    if (sb_type == SB_SHARED) {
+        void *sb_shared;
+        rv = open_scoreboard(p);
+        if (rv || !(sb_shared = apr_shm_baseaddr_get(ap_scoreboard_shm))) {
+            return HTTP_INTERNAL_SERVER_ERROR;
         }
-        else 
+        memset(sb_shared, 0, scoreboard_size);
+        ap_init_scoreboard(sb_shared);
+    }
+    else
 #endif
-        {
-            /* A simple malloc will suffice */
-            void *sb_mem = calloc(1, scoreboard_size);
-            if (sb_mem == NULL) {
-                ap_log_error(APLOG_MARK, APLOG_CRIT | APLOG_NOERRNO, 0, NULL,
-                             "(%d)%s: cannot allocate scoreboard",
-                             errno, strerror(errno));
-                return HTTP_INTERNAL_SERVER_ERROR;
-            }
-            ap_init_scoreboard(sb_mem);
+    {
+        /* A simple malloc will suffice */
+        void *sb_mem = calloc(1, scoreboard_size);
+        if (sb_mem == NULL) {
+            ap_log_error(APLOG_MARK, APLOG_CRIT, 0, NULL,
+                         "(%d)%s: cannot allocate scoreboard",
+                         errno, strerror(errno));
+            return HTTP_INTERNAL_SERVER_ERROR;
         }
+        ap_init_scoreboard(sb_mem);
     }
+
     ap_scoreboard_image->global->sb_type = sb_type;
     ap_scoreboard_image->global->running_generation = running_gen;
     ap_scoreboard_image->global->restart_time = apr_time_now();
+
     apr_pool_cleanup_register(p, NULL, ap_cleanup_scoreboard, apr_pool_cleanup_null);
+
     return OK;
 }
 
@@ -336,35 +334,11 @@ int ap_create_scoreboard(apr_pool_t *p, ap_scoreboard_e sb_type)
  * anyway.
  */
 
-void ap_sync_scoreboard_image(void)
-{
-}
-
 AP_DECLARE(int) ap_exists_scoreboard_image(void)
 {
     return (ap_scoreboard_image ? 1 : 0);
 }
 
-static APR_INLINE void put_scoreboard_info(int child_num, int thread_num, 
-                                           worker_score *new_score_rec)
-{
-    /* XXX - needs to be fixed to account for threads */
-#ifdef SCOREBOARD_FILE
-    lseek(scoreboard_fd, sizeof(global_score) 
-                         + (long)child_num * sizeof(worker_score), 0);
-    force_write(scoreboard_fd, new_score_rec, sizeof(worker_score));
-#endif
-}
-
-void update_scoreboard_global(void)
-{
-#ifdef SCOREBOARD_FILE
-    lseek(scoreboard_fd, 0, 0);
-    force_write(scoreboard_fd, &ap_scoreboard_image->global,
-                sizeof ap_scoreboard_image->global);
-#endif
-}
-
 AP_DECLARE(void) ap_increment_counts(ap_sb_handle_t *sb, request_rec *r)
 {
     worker_score *ws;
@@ -380,11 +354,9 @@ AP_DECLARE(void) ap_increment_counts(ap_sb_handle_t *sb, request_rec *r)
     ws->bytes_served += r->bytes_sent;
     ws->my_bytes_served += r->bytes_sent;
     ws->conn_bytes += r->bytes_sent;
-
-    put_scoreboard_info(sb->child_num, sb->thread_num, ws);
 }
 
-AP_DECLARE(int) find_child_by_pid(apr_proc_t *pid)
+int find_child_by_pid(apr_proc_t *pid)
 {
     int i;
     int max_daemons_limit;
@@ -426,10 +398,10 @@ AP_DECLARE(int) ap_update_child_status_from_indexes(int child_num,
     ws->status = status;
 
     ps = &ap_scoreboard_image->parent[child_num];
-    
+
     if (status == SERVER_READY
         && old_status == SERVER_STARTING) {
-        ws->thread_num = child_num * server_limit + thread_num;
+        ws->thread_num = child_num * thread_limit + thread_num;
         ps->generation = ap_my_generation;
     }
 
@@ -443,8 +415,8 @@ AP_DECLARE(int) ap_update_child_status_from_indexes(int child_num,
                 ws->my_access_count = 0L;
                 ws->my_bytes_served = 0L;
             }
-            ws->conn_count = (unsigned short)0;
-            ws->conn_bytes = (unsigned long)0;
+            ws->conn_count = 0;
+            ws->conn_bytes = 0;
         }
         if (r) {
             conn_rec *c = r->connection;
@@ -466,8 +438,7 @@ AP_DECLARE(int) ap_update_child_status_from_indexes(int child_num,
                         sizeof(ws->vhost));
         }
     }
-    
-    put_scoreboard_info(child_num, thread_num, ws);
+
     return old_status;
 }
 
@@ -478,23 +449,22 @@ AP_DECLARE(int) ap_update_child_status(ap_sb_handle_t *sbh, int status,
                                                status, r);
 }
 
-void ap_time_process_request(int child_num, int thread_num, int status)
+void ap_time_process_request(ap_sb_handle_t *sbh, int status)
 {
     worker_score *ws;
 
-    if (child_num < 0) {
+    if (sbh->child_num < 0) {
         return;
     }
 
-    ws = &ap_scoreboard_image->servers[child_num][thread_num];
+    ws = &ap_scoreboard_image->servers[sbh->child_num][sbh->thread_num];
 
     if (status == START_PREQUEST) {
-        ws->start_time = apr_time_now(); 
+        ws->start_time = apr_time_now();
     }
     else if (status == STOP_PREQUEST) {
-        ws->stop_time = apr_time_now(); 
+        ws->stop_time = apr_time_now();
     }
-    put_scoreboard_info(child_num, thread_num, ws);
 }
 
 AP_DECLARE(worker_score *) ap_get_scoreboard_worker(int x, int y)
@@ -518,3 +488,11 @@ AP_DECLARE(global_score *) ap_get_scoreboard_global()
 {
     return ap_scoreboard_image->global;
 }
+
+AP_DECLARE(lb_score *) ap_get_scoreboard_lb(int lb_num)
+{
+    if (((lb_num < 0) || (lb_limit < lb_num))) {
+        return(NULL); /* Out of range */
+    }
+    return &ap_scoreboard_image->balancers[lb_num];
+}