scoreboard.c revision da885f8529a499c96c4489ae7d03762b97da5394
6ae232055d4d8a97267517c5e50074c2c819941and/* ====================================================================
6ae232055d4d8a97267517c5e50074c2c819941and * The Apache Software License, Version 1.1
6ae232055d4d8a97267517c5e50074c2c819941and *
6ae232055d4d8a97267517c5e50074c2c819941and * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
6ae232055d4d8a97267517c5e50074c2c819941and * reserved.
6ae232055d4d8a97267517c5e50074c2c819941and *
6ae232055d4d8a97267517c5e50074c2c819941and * Redistribution and use in source and binary forms, with or without
6ae232055d4d8a97267517c5e50074c2c819941and * modification, are permitted provided that the following conditions
6ae232055d4d8a97267517c5e50074c2c819941and * are met:
6ae232055d4d8a97267517c5e50074c2c819941and *
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * 1. Redistributions of source code must retain the above copyright
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * notice, this list of conditions and the following disclaimer.
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen *
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * 2. Redistributions in binary form must reproduce the above copyright
6ae232055d4d8a97267517c5e50074c2c819941and * notice, this list of conditions and the following disclaimer in
6ae232055d4d8a97267517c5e50074c2c819941and * the documentation and/or other materials provided with the
d229f940abfb2490dee17979e9a5ff31b7012eb5rbowen * distribution.
3f08db06526d6901aa08c110b5bc7dde6bc39905nd *
6ae232055d4d8a97267517c5e50074c2c819941and * 3. The end-user documentation included with the redistribution,
6ae232055d4d8a97267517c5e50074c2c819941and * if any, must include the following acknowledgment:
6ae232055d4d8a97267517c5e50074c2c819941and * "This product includes software developed by the
b43f840409794ed298e8634f6284741f193b6c4ftakashi * Apache Software Foundation (http://www.apache.org/)."
6ae232055d4d8a97267517c5e50074c2c819941and * Alternately, this acknowledgment may appear in the software itself,
b43f840409794ed298e8634f6284741f193b6c4ftakashi * if and wherever such third-party acknowledgments normally appear.
6ae232055d4d8a97267517c5e50074c2c819941and *
6ae232055d4d8a97267517c5e50074c2c819941and * 4. The names "Apache" and "Apache Software Foundation" must
4b3a8afbfcea8b265d179a122bf40dfedd1ce280takashi * not be used to endorse or promote products derived from this
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung * software without prior written permission. For written
6ae232055d4d8a97267517c5e50074c2c819941and * permission, please contact apache@apache.org.
b43f840409794ed298e8634f6284741f193b6c4ftakashi *
b43f840409794ed298e8634f6284741f193b6c4ftakashi * 5. Products derived from this software may not be called "Apache",
b43f840409794ed298e8634f6284741f193b6c4ftakashi * nor may "Apache" appear in their name, without prior written
b43f840409794ed298e8634f6284741f193b6c4ftakashi * permission of the Apache Software Foundation.
6ae232055d4d8a97267517c5e50074c2c819941and *
6ae232055d4d8a97267517c5e50074c2c819941and * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
6ae232055d4d8a97267517c5e50074c2c819941and * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
6ae232055d4d8a97267517c5e50074c2c819941and * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
6ae232055d4d8a97267517c5e50074c2c819941and * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
6ae232055d4d8a97267517c5e50074c2c819941and * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
6ae232055d4d8a97267517c5e50074c2c819941and * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
6ae232055d4d8a97267517c5e50074c2c819941and * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
6ae232055d4d8a97267517c5e50074c2c819941and * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
6ae232055d4d8a97267517c5e50074c2c819941and * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
6ae232055d4d8a97267517c5e50074c2c819941and * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
6ae232055d4d8a97267517c5e50074c2c819941and * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6ae232055d4d8a97267517c5e50074c2c819941and * SUCH DAMAGE.
6ae232055d4d8a97267517c5e50074c2c819941and * ====================================================================
6ae232055d4d8a97267517c5e50074c2c819941and *
6ae232055d4d8a97267517c5e50074c2c819941and * This software consists of voluntary contributions made by many
6ae232055d4d8a97267517c5e50074c2c819941and * individuals on behalf of the Apache Software Foundation. For more
6ae232055d4d8a97267517c5e50074c2c819941and * information on the Apache Software Foundation, please see
6ae232055d4d8a97267517c5e50074c2c819941and * <http://www.apache.org/>.
6ae232055d4d8a97267517c5e50074c2c819941and *
6ae232055d4d8a97267517c5e50074c2c819941and * Portions of this software are based upon public domain software
6ae232055d4d8a97267517c5e50074c2c819941and * originally written at the National Center for Supercomputing Applications,
6ae232055d4d8a97267517c5e50074c2c819941and * University of Illinois, Urbana-Champaign.
6ae232055d4d8a97267517c5e50074c2c819941and */
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and#include "apr.h"
6ae232055d4d8a97267517c5e50074c2c819941and#include "apr_strings.h"
6ae232055d4d8a97267517c5e50074c2c819941and#include "apr_portable.h"
6ae232055d4d8a97267517c5e50074c2c819941and#include "apr_lib.h"
6ae232055d4d8a97267517c5e50074c2c819941and
5ae30adbe59946de742ab0cd6be3b7457471a698takashi#define APR_WANT_STRFUNC
6ae232055d4d8a97267517c5e50074c2c819941and#include "apr_want.h"
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and#if APR_HAVE_SYS_TYPES_H
6ae232055d4d8a97267517c5e50074c2c819941and#include <sys/types.h>
6ae232055d4d8a97267517c5e50074c2c819941and#endif
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and#include "ap_config.h"
6ae232055d4d8a97267517c5e50074c2c819941and#include "httpd.h"
6ae232055d4d8a97267517c5e50074c2c819941and#include "http_log.h"
6ae232055d4d8a97267517c5e50074c2c819941and#include "http_main.h"
6ae232055d4d8a97267517c5e50074c2c819941and#include "http_core.h"
6ae232055d4d8a97267517c5e50074c2c819941and#include "http_config.h"
6ae232055d4d8a97267517c5e50074c2c819941and#include "ap_mpm.h"
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and#include "mpm.h"
6ae232055d4d8a97267517c5e50074c2c819941and#include "scoreboard.h"
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andAP_DECLARE_DATA scoreboard *ap_scoreboard_image = NULL;
6ae232055d4d8a97267517c5e50074c2c819941andAP_DECLARE_DATA const char *ap_scoreboard_fname=NULL;
6ae232055d4d8a97267517c5e50074c2c819941andAP_DECLARE_DATA int ap_extended_status = 0;
6ae232055d4d8a97267517c5e50074c2c819941andAP_DECLARE_DATA apr_time_t ap_restart_time = 0;
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and#if APR_HAS_SHARED_MEMORY
6ae232055d4d8a97267517c5e50074c2c819941and#include "apr_shmem.h"
6ae232055d4d8a97267517c5e50074c2c819941andstatic apr_shmem_t *scoreboard_shm = NULL;
6ae232055d4d8a97267517c5e50074c2c819941and#endif
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andAPR_HOOK_STRUCT(
6ae232055d4d8a97267517c5e50074c2c819941and APR_HOOK_LINK(pre_mpm)
6ae232055d4d8a97267517c5e50074c2c819941and)
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andAP_IMPLEMENT_HOOK_VOID(pre_mpm,
6ae232055d4d8a97267517c5e50074c2c819941and (apr_pool_t *p, ap_scoreboard_e sb_type),
6ae232055d4d8a97267517c5e50074c2c819941and (p, sb_type))
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andtypedef struct sb_handle {
6ae232055d4d8a97267517c5e50074c2c819941and int child_num;
6ae232055d4d8a97267517c5e50074c2c819941and int thread_num;
6ae232055d4d8a97267517c5e50074c2c819941and} sb_handle;
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andstatic int server_limit, thread_limit;
6ae232055d4d8a97267517c5e50074c2c819941andstatic apr_size_t scoreboard_size;
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and/*
6ae232055d4d8a97267517c5e50074c2c819941and * ToDo:
6ae232055d4d8a97267517c5e50074c2c819941and * This function should be renamed to cleanup_shared
6ae232055d4d8a97267517c5e50074c2c819941and * and it should handle cleaning up a scoreboard shared
6ae232055d4d8a97267517c5e50074c2c819941and * between processes using any form of IPC (file, shared memory
6ae232055d4d8a97267517c5e50074c2c819941and * segment, etc.). Leave it as is now because it is being used
6ae232055d4d8a97267517c5e50074c2c819941and * by various MPMs.
6ae232055d4d8a97267517c5e50074c2c819941and */
6ae232055d4d8a97267517c5e50074c2c819941andstatic apr_status_t ap_cleanup_shared_mem(void *d)
6ae232055d4d8a97267517c5e50074c2c819941and{
6ae232055d4d8a97267517c5e50074c2c819941and#if APR_HAS_SHARED_MEMORY
6ae232055d4d8a97267517c5e50074c2c819941and apr_shm_free(scoreboard_shm, ap_scoreboard_image);
6ae232055d4d8a97267517c5e50074c2c819941and ap_scoreboard_image = NULL;
6ae232055d4d8a97267517c5e50074c2c819941and apr_shm_destroy(scoreboard_shm);
6ae232055d4d8a97267517c5e50074c2c819941and#endif
6ae232055d4d8a97267517c5e50074c2c819941and return APR_SUCCESS;
6ae232055d4d8a97267517c5e50074c2c819941and}
6ae232055d4d8a97267517c5e50074c2c819941and
b43f840409794ed298e8634f6284741f193b6c4ftakashistatic void calc_scoreboard_size(void)
6ae232055d4d8a97267517c5e50074c2c819941and{
6ae232055d4d8a97267517c5e50074c2c819941and ap_mpm_query(AP_MPMQ_HARD_LIMIT_THREADS, &thread_limit);
4b3a8afbfcea8b265d179a122bf40dfedd1ce280takashi ap_mpm_query(AP_MPMQ_HARD_LIMIT_DAEMONS, &server_limit);
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung scoreboard_size = sizeof(scoreboard);
727872d18412fc021f03969b8641810d8896820bhumbedooh scoreboard_size += sizeof(process_score) * server_limit;
0d0ba3a410038e179b695446bb149cce6264e0abnd scoreboard_size += sizeof(worker_score * ) * server_limit;
727872d18412fc021f03969b8641810d8896820bhumbedooh scoreboard_size += sizeof(worker_score) * server_limit * thread_limit;
cc7e1025de9ac63bd4db6fe7f71c158b2cf09fe4humbedooh}
0d0ba3a410038e179b695446bb149cce6264e0abnd
cc7e1025de9ac63bd4db6fe7f71c158b2cf09fe4humbedoohstatic void init_scoreboard(void)
727872d18412fc021f03969b8641810d8896820bhumbedooh{
0d0ba3a410038e179b695446bb149cce6264e0abnd char *more_storage;
0d0ba3a410038e179b695446bb149cce6264e0abnd int i;
0d0ba3a410038e179b695446bb149cce6264e0abnd
ac082aefa89416cbdc9a1836eaf3bed9698201c8humbedooh memset(ap_scoreboard_image, 0, scoreboard_size);
0d0ba3a410038e179b695446bb149cce6264e0abnd more_storage = (char *)(ap_scoreboard_image + 1);
0d0ba3a410038e179b695446bb149cce6264e0abnd ap_scoreboard_image->parent = (process_score *)more_storage;
0d0ba3a410038e179b695446bb149cce6264e0abnd more_storage += sizeof(process_score) * server_limit;
727872d18412fc021f03969b8641810d8896820bhumbedooh ap_scoreboard_image->servers = (worker_score **)more_storage;
0d0ba3a410038e179b695446bb149cce6264e0abnd more_storage += server_limit * sizeof(worker_score *);
0d0ba3a410038e179b695446bb149cce6264e0abnd
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh for (i = 0; i < server_limit; i++) {
b43f840409794ed298e8634f6284741f193b6c4ftakashi ap_scoreboard_image->servers[i] = (worker_score *)more_storage;
d229f940abfb2490dee17979e9a5ff31b7012eb5rbowen more_storage += thread_limit * sizeof(worker_score);
0d0ba3a410038e179b695446bb149cce6264e0abnd }
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd ap_assert(more_storage == (char *)ap_scoreboard_image + scoreboard_size);
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd}
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd
6ae232055d4d8a97267517c5e50074c2c819941and/* 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 void setup_shared(apr_pool_t *p)
{
#if APR_HAS_SHARED_MEMORY
char buf[512];
char errmsg[120];
const char *fname;
apr_status_t rv;
fname = ap_server_root_relative(p, ap_scoreboard_fname);
rv = apr_shm_init(&scoreboard_shm, scoreboard_size, fname, p);
if (rv != APR_SUCCESS) {
apr_snprintf(buf, sizeof(buf), "%s: could not open(create) scoreboard: (%d)%s",
ap_server_argv0, rv, apr_strerror(rv, errmsg, sizeof errmsg));
fprintf(stderr, "%s\n", buf);
exit(APEXIT_INIT);
}
ap_scoreboard_image = apr_shm_malloc(scoreboard_shm, scoreboard_size);
if (ap_scoreboard_image == NULL) {
apr_snprintf(buf, sizeof(buf), "%s: cannot allocate scoreboard",
ap_server_argv0);
perror(buf); /* o.k. since MM sets errno */
apr_shm_destroy(scoreboard_shm);
exit(APEXIT_INIT);
}
/* everything will be cleared shortly */
#endif
}
AP_DECLARE(void) reopen_scoreboard(apr_pool_t *p)
{
}
/* ap_cleanup_scoreboard
*
*/
apr_status_t ap_cleanup_scoreboard(void *d) {
if (ap_scoreboard_image == NULL)
return APR_SUCCESS;
if (ap_scoreboard_image->global.sb_type == SB_SHARED) {
ap_cleanup_shared_mem(NULL);
}
else {
free(ap_scoreboard_image);
ap_scoreboard_image = NULL;
}
return APR_SUCCESS;
}
/* ap_create_scoreboard(apr_pool_t*, ap_scoreboard_e t)
*
* Create or reinit an existing scoreboard. The MPM can control whether
* the scoreboard is shared across multiple processes or not
*
* ###: Is there any reason to export this symbol in the first place?
*/
AP_DECLARE_NONSTD(void) ap_create_scoreboard(apr_pool_t *p, ap_scoreboard_e sb_type)
{
int running_gen = 0;
if (ap_scoreboard_image)
running_gen = ap_scoreboard_image->global.running_generation;
if (ap_scoreboard_image == NULL) {
calc_scoreboard_size();
if (sb_type == SB_SHARED) {
setup_shared(p);
}
else {
/* A simple malloc will suffice */
char buf[512];
ap_scoreboard_image = (scoreboard *) malloc(scoreboard_size);
if (ap_scoreboard_image == NULL) {
apr_snprintf(buf, sizeof(buf), "%s: cannot allocate scoreboard",
ap_server_argv0);
perror(buf); /* o.k. since MM sets errno */
exit(APEXIT_INIT);
}
}
}
init_scoreboard(); /* can't just memset() */
ap_scoreboard_image->global.sb_type = sb_type;
ap_scoreboard_image->global.running_generation = running_gen;
ap_restart_time = apr_time_now();
apr_pool_cleanup_register(p, NULL, ap_cleanup_scoreboard, apr_pool_cleanup_null);
}
/* Routines called to deal with the scoreboard image
* --- note that we do *not* need write locks, since update_child_status
* only updates a *single* record in place, and only one process writes to
* a given scoreboard slot at a time (either the child process owning that
* slot, or the parent, noting that the child has died).
*
* As a final note --- setting the score entry to getpid() is always safe,
* since when the parent is writing an entry, it's only noting SERVER_DEAD
* 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, (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,
(char *) &ap_scoreboard_image->global -(char *) ap_scoreboard_image, 0);
force_write(scoreboard_fd, &ap_scoreboard_image->global,
sizeof ap_scoreboard_image->global);
#endif
}
AP_DECLARE(void) ap_increment_counts(void *sbh, request_rec *r)
{
sb_handle *sb = sbh;
worker_score *ws;
ws = &ap_scoreboard_image->servers[sb->child_num][sb->thread_num];
#ifdef HAVE_TIMES
times(&ws->times);
#endif
ws->access_count++;
ws->my_access_count++;
ws->conn_count++;
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 i;
int max_daemons_limit;
ap_mpm_query(AP_MPMQ_MAX_DAEMONS, &max_daemons_limit);
for (i = 0; i < max_daemons_limit; ++i)
if (ap_scoreboard_image->parent[i].pid == pid->pid)
return i;
return -1;
}
AP_DECLARE(void) ap_create_sb_handle(void **new_handle, apr_pool_t *p,
int child_num, int thread_num)
{
sb_handle *sbh;
sbh = (sb_handle *)apr_palloc(p, sizeof *sbh);
*new_handle = sbh;
sbh->child_num = child_num;
sbh->thread_num = thread_num;
}
AP_DECLARE(int) ap_update_child_status_from_indexes(int child_num, int thread_num,
int status, request_rec *r)
{
int old_status, i;
worker_score *ws;
process_score *ps;
if (child_num < 0)
return -1;
ws = &ap_scoreboard_image->servers[child_num][thread_num];
old_status = ws->status;
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;
if (ps->generation != ap_my_generation) {
for (i = 0; i < thread_limit; i++) {
ap_scoreboard_image->servers[child_num][i].vhostrec = NULL;
}
ps->generation = ap_my_generation;
}
}
if (ap_extended_status) {
ws->last_used = apr_time_now();
if (status == SERVER_READY || status == SERVER_DEAD) {
/*
* Reset individual counters
*/
if (status == SERVER_DEAD) {
ws->my_access_count = 0L;
ws->my_bytes_served = 0L;
}
ws->conn_count = (unsigned short) 0;
ws->conn_bytes = (unsigned long) 0;
}
if (r) {
conn_rec *c = r->connection;
apr_cpystrn(ws->client, ap_get_remote_host(c, r->per_dir_config,
REMOTE_NOLOOKUP, NULL), sizeof(ws->client));
if (r->the_request == NULL) {
apr_cpystrn(ws->request, "NULL", sizeof(ws->request));
} else if (r->parsed_uri.password == NULL) {
apr_cpystrn(ws->request, r->the_request, sizeof(ws->request));
} else {
/* Don't reveal the password in the server-status view */
apr_cpystrn(ws->request, apr_pstrcat(r->pool, r->method, " ",
apr_uri_unparse(r->pool, &r->parsed_uri, APR_URI_UNP_OMITPASSWORD),
r->assbackwards ? NULL : " ", r->protocol, NULL),
sizeof(ws->request));
}
ws->vhostrec = r->server;
}
}
put_scoreboard_info(child_num, thread_num, ws);
return old_status;
}
AP_DECLARE(int)ap_update_child_status(void *sbh, int status, request_rec *r)
{
sb_handle *sb = sbh;
return ap_update_child_status_from_indexes(sb->child_num, sb->thread_num,
status, r);
}
void ap_time_process_request(int child_num, int thread_num, int status)
{
worker_score *ws;
if (child_num < 0)
return;
ws = &ap_scoreboard_image->servers[child_num][thread_num];
if (status == START_PREQUEST) {
ws->start_time = apr_time_now();
}
else if (status == STOP_PREQUEST) {
ws->stop_time = apr_time_now();
}
put_scoreboard_info(child_num, thread_num, ws);
}
AP_DECLARE(worker_score *) ap_get_servers_scoreboard(int x, int y)
{
if (((x < 0) || (server_limit < x)) ||
((y < 0) || (thread_limit < y))) {
return(NULL); /* Out of range */
}
return(&ap_scoreboard_image->servers[x][y]);
}
AP_DECLARE(process_score *) ap_get_parent_scoreboard(int x)
{
if ((x < 0) || (server_limit < x)) {
return(NULL); /* Out of range */
}
return(&ap_scoreboard_image->parent[x]);
}
AP_DECLARE(global_score *) ap_get_global_scoreboard()
{
return(&ap_scoreboard_image->global);
}