scoreboard.c revision bf5cf58cc30750e9f9764cc830aff426ced288f9
842ae4bd224140319ae7feec1872b93dfd491143fielding/* ====================================================================
842ae4bd224140319ae7feec1872b93dfd491143fielding * The Apache Software License, Version 1.1
842ae4bd224140319ae7feec1872b93dfd491143fielding * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
842ae4bd224140319ae7feec1872b93dfd491143fielding * reserved.
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * Redistribution and use in source and binary forms, with or without
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * modification, are permitted provided that the following conditions
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * 1. Redistributions of source code must retain the above copyright
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * notice, this list of conditions and the following disclaimer.
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * 2. Redistributions in binary form must reproduce the above copyright
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * notice, this list of conditions and the following disclaimer in
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * the documentation and/or other materials provided with the
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * distribution.
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * 3. The end-user documentation included with the redistribution,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * if any, must include the following acknowledgment:
2d728410bcf9e6e53698bb035f516e18efd76d8bnd * "This product includes software developed by the
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * Apache Software Foundation (http://www.apache.org/)."
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * Alternately, this acknowledgment may appear in the software itself,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * if and wherever such third-party acknowledgments normally appear.
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * 4. The names "Apache" and "Apache Software Foundation" must
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * not be used to endorse or promote products derived from this
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * software without prior written permission. For written
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * permission, please contact apache@apache.org.
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * 5. Products derived from this software may not be called "Apache",
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * nor may "Apache" appear in their name, without prior written
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * permission of the Apache Software Foundation.
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
806b50b4d37ade77f1c767a1593fac5a2d7786c8minfrin * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
c07866e582be4daac7f6a0a8754fa12fca044663fuankg * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
07ed2f9227b98d2cac21a7121ef7ea5328fa3f98fuankg * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
15ff8c621815e8337abc10638f2b2853ee6fd076minfrin * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * SUCH DAMAGE.
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * ====================================================================
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * This software consists of voluntary contributions made by many
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * individuals on behalf of the Apache Software Foundation. For more
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * information on the Apache Software Foundation, please see
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * Portions of this software are based upon public domain software
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * originally written at the National Center for Supercomputing Applications,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * University of Illinois, Urbana-Champaign.
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzAP_DECLARE_DATA scoreboard *ap_scoreboard_image = NULL;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzAP_DECLARE_DATA const char *ap_scoreboard_fname = NULL;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9ndstatic /* but must be exported to mpm_winnt */
2d728410bcf9e6e53698bb035f516e18efd76d8bnd * This function should be renamed to cleanup_shared
2d728410bcf9e6e53698bb035f516e18efd76d8bnd * and it should handle cleaning up a scoreboard shared
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd * between processes using any form of IPC (file, shared memory
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin * segment, etc.). Leave it as is now because it is being used
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin * by various MPMs.
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin ap_mpm_query(AP_MPMQ_HARD_LIMIT_THREADS, &thread_limit);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz scoreboard_size += sizeof(process_score) * server_limit;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz scoreboard_size += sizeof(worker_score) * server_limit * thread_limit;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz calloc(1, sizeof(scoreboard) + server_limit * sizeof(worker_score *));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_scoreboard_image->global = (global_score *)more_storage;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_scoreboard_image->parent = (process_score *)more_storage;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz more_storage += sizeof(process_score) * server_limit;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz (worker_score **)((char*)ap_scoreboard_image + sizeof(scoreboard));
a51acf58d9d82f52e0ee56106cd9282030f3f3bend for (i = 0; i < server_limit; i++) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_scoreboard_image->servers[i] = (worker_score *)more_storage;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz more_storage += thread_limit * sizeof(worker_score);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_assert(more_storage == (char*)shared_score + scoreboard_size);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_scoreboard_image->global->server_limit = server_limit;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_scoreboard_image->global->thread_limit = thread_limit;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * Create a name-based scoreboard in the given pool using the
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * given filename.
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic apr_status_t create_namebased_scoreboard(apr_pool_t *pool,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char *fname)
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz /* The shared memory file must not exist before we create the
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * segment. */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_file_remove(fname, pool); /* ignore errors */
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin rv = apr_shm_create(&ap_scoreboard_shm, scoreboard_size, fname, pool);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd "unable to create scoreboard "
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "(name-based shared memory failure)");
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#endif /* APR_HAS_SHARED_MEMORY */
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin/* ToDo: This function should be made to handle setting up
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin * a scoreboard shared between processes using any IPC technique,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * not just a shared memory segment
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrinstatic apr_status_t open_scoreboard(apr_pool_t *pconf)
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz /* We don't want to have to recreate the scoreboard after
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin * restarts, so we'll create a global pool and never clean it.
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin "Fatal error: unable to create global pool "
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin "for use with by the scoreboard");
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz /* The config says to create a name-based shmem */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz /* make sure it's an absolute pathname */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz fname = ap_server_root_relative(pconf, ap_scoreboard_fname);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return create_namebased_scoreboard(global_pool, fname);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz else { /* config didn't specify, we get to choose shmem type */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz rv = apr_shm_create(&ap_scoreboard_shm, scoreboard_size, NULL,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if ((rv != APR_SUCCESS) && (rv != APR_ENOTIMPL)) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "Unable to create scoreboard "
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "(anonymous shared memory failure)");
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin /* Make up a filename and do name-based shmem */
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin /* Make sure it's an absolute pathname */
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin fname = ap_server_root_relative(pconf, ap_scoreboard_fname);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin return create_namebased_scoreboard(global_pool, fname);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin#endif /* APR_HAS_SHARED_MEMORY */
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin/* If detach is non-zero, this is a seperate child process,
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin * if zero, it is a forked child.
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrinapr_status_t ap_reopen_scoreboard(apr_pool_t *p, apr_shm_t **shm, int detached)
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (apr_shm_size_get(ap_scoreboard_shm) < scoreboard_size) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_log_error(APLOG_MARK, APLOG_CRIT | APLOG_NOERRNO, 0, NULL,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "Fatal error: shared scoreboard too small for child!");
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin /* everything will be cleared shortly */
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin/* Create or reinit an existing scoreboard. The MPM can control whether
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin * the scoreboard is shared across multiple processes or not
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrinint ap_create_scoreboard(apr_pool_t *p, ap_scoreboard_e sb_type)
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin running_gen = ap_scoreboard_image->global->running_generation;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (rv || !(sb_shared = apr_shm_baseaddr_get(ap_scoreboard_shm))) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin /* A simple malloc will suffice */
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin ap_log_error(APLOG_MARK, APLOG_CRIT | APLOG_NOERRNO, 0, NULL,
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin "(%d)%s: cannot allocate scoreboard",
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin ap_scoreboard_image->global->running_generation = running_gen;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin ap_scoreboard_image->global->restart_time = apr_time_now();
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin apr_pool_cleanup_register(p, NULL, ap_cleanup_scoreboard, apr_pool_cleanup_null);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd/* Routines called to deal with the scoreboard image
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd * --- note that we do *not* need write locks, since update_child_status
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd * only updates a *single* record in place, and only one process writes to
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin * a given scoreboard slot at a time (either the child process owning that
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin * slot, or the parent, noting that the child has died).
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin * As a final note --- setting the score entry to getpid() is always safe,
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin * since when the parent is writing an entry, it's only noting SERVER_DEAD
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9ndstatic APR_INLINE void put_scoreboard_info(int child_num, int thread_num,
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin /* XXX - needs to be fixed to account for threads */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz force_write(scoreboard_fd, new_score_rec, sizeof(worker_score));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz force_write(scoreboard_fd, &ap_scoreboard_image->global,
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrinAP_DECLARE(void) ap_increment_counts(ap_sb_handle_t *sb, request_rec *r)
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin ws = &ap_scoreboard_image->servers[sb->child_num][sb->thread_num];
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin put_scoreboard_info(sb->child_num, sb->thread_num, ws);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd for (i = 0; i < max_daemons_limit; ++i) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrinAP_DECLARE(void) ap_create_sb_handle(ap_sb_handle_t **new_sbh, apr_pool_t *p,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin *new_sbh = (ap_sb_handle_t *)apr_palloc(p, sizeof(ap_sb_handle_t));
5876f43a746f688a32b7201bced8591ddf19bd43minfrinAP_DECLARE(int) ap_update_child_status_from_indexes(int child_num,
717473b4288c54ef043f016a4ee801aba477303erjung ws = &ap_scoreboard_image->servers[child_num][thread_num];
5876f43a746f688a32b7201bced8591ddf19bd43minfrin ws->thread_num = child_num * server_limit + thread_num;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (status == SERVER_READY || status == SERVER_DEAD) {
c840604b10dc1d368a2da321ec51362373d502a3minfrin * Reset individual counters
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_cpystrn(ws->client, ap_get_remote_host(c, r->per_dir_config,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_cpystrn(ws->request, "NULL", sizeof(ws->request));
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_cpystrn(ws->request, r->the_request, sizeof(ws->request));
5876f43a746f688a32b7201bced8591ddf19bd43minfrin /* Don't reveal the password in the server-status view */
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_cpystrn(ws->request, apr_pstrcat(r->pool, r->method, " ",
5876f43a746f688a32b7201bced8591ddf19bd43minfrinAP_DECLARE(int) ap_update_child_status(ap_sb_handle_t *sbh, int status,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin return ap_update_child_status_from_indexes(sbh->child_num, sbh->thread_num,
5876f43a746f688a32b7201bced8591ddf19bd43minfrinvoid ap_time_process_request(int child_num, int thread_num, int status)
5876f43a746f688a32b7201bced8591ddf19bd43minfrin ws = &ap_scoreboard_image->servers[child_num][thread_num];
5876f43a746f688a32b7201bced8591ddf19bd43minfrinAP_DECLARE(worker_score *) ap_get_scoreboard_worker(int x, int y)
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (((x < 0) || (server_limit < x)) ||
5876f43a746f688a32b7201bced8591ddf19bd43minfrin ((y < 0) || (thread_limit < y))) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrinAP_DECLARE(process_score *) ap_get_scoreboard_process(int x)
51c6a215cedf19fe46a20b5cbb57f01ec4d5e8c4fuankg if ((x < 0) || (server_limit < x)) {