scoreboard.c revision f289872b1e18c01d5cfb7df8afacebbfcdaa99fb
85816edcdb4137f90186cb437949a4dbcbf46ab5jim/* ====================================================================
85816edcdb4137f90186cb437949a4dbcbf46ab5jim * The Apache Software License, Version 1.1
85816edcdb4137f90186cb437949a4dbcbf46ab5jim *
182f52731de370a91a76b08ff73a43d1ea7bb4e8nd * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
85816edcdb4137f90186cb437949a4dbcbf46ab5jim * reserved.
85816edcdb4137f90186cb437949a4dbcbf46ab5jim *
031b91a62d25106ae69d4693475c79618dd5e884fielding * Redistribution and use in source and binary forms, with or without
031b91a62d25106ae69d4693475c79618dd5e884fielding * modification, are permitted provided that the following conditions
031b91a62d25106ae69d4693475c79618dd5e884fielding * are met:
031b91a62d25106ae69d4693475c79618dd5e884fielding *
031b91a62d25106ae69d4693475c79618dd5e884fielding * 1. Redistributions of source code must retain the above copyright
031b91a62d25106ae69d4693475c79618dd5e884fielding * notice, this list of conditions and the following disclaimer.
85816edcdb4137f90186cb437949a4dbcbf46ab5jim *
85816edcdb4137f90186cb437949a4dbcbf46ab5jim * 2. Redistributions in binary form must reproduce the above copyright
85816edcdb4137f90186cb437949a4dbcbf46ab5jim * notice, this list of conditions and the following disclaimer in
85816edcdb4137f90186cb437949a4dbcbf46ab5jim * the documentation and/or other materials provided with the
85816edcdb4137f90186cb437949a4dbcbf46ab5jim * distribution.
85816edcdb4137f90186cb437949a4dbcbf46ab5jim *
85816edcdb4137f90186cb437949a4dbcbf46ab5jim * 3. The end-user documentation included with the redistribution,
85816edcdb4137f90186cb437949a4dbcbf46ab5jim * if any, must include the following acknowledgment:
85816edcdb4137f90186cb437949a4dbcbf46ab5jim * "This product includes software developed by the
85816edcdb4137f90186cb437949a4dbcbf46ab5jim * Apache Software Foundation (http://www.apache.org/)."
85816edcdb4137f90186cb437949a4dbcbf46ab5jim * Alternately, this acknowledgment may appear in the software itself,
85816edcdb4137f90186cb437949a4dbcbf46ab5jim * if and wherever such third-party acknowledgments normally appear.
85816edcdb4137f90186cb437949a4dbcbf46ab5jim *
ee5db395bc3723609919edfa96af387eea37c491jim * 4. The names "Apache" and "Apache Software Foundation" must
85816edcdb4137f90186cb437949a4dbcbf46ab5jim * not be used to endorse or promote products derived from this
85816edcdb4137f90186cb437949a4dbcbf46ab5jim * software without prior written permission. For written
85816edcdb4137f90186cb437949a4dbcbf46ab5jim * permission, please contact apache@apache.org.
85816edcdb4137f90186cb437949a4dbcbf46ab5jim *
85816edcdb4137f90186cb437949a4dbcbf46ab5jim * 5. Products derived from this software may not be called "Apache",
85816edcdb4137f90186cb437949a4dbcbf46ab5jim * nor may "Apache" appear in their name, without prior written
85816edcdb4137f90186cb437949a4dbcbf46ab5jim * permission of the Apache Software Foundation.
85816edcdb4137f90186cb437949a4dbcbf46ab5jim *
85816edcdb4137f90186cb437949a4dbcbf46ab5jim * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
85816edcdb4137f90186cb437949a4dbcbf46ab5jim * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
85816edcdb4137f90186cb437949a4dbcbf46ab5jim * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
85816edcdb4137f90186cb437949a4dbcbf46ab5jim * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
85816edcdb4137f90186cb437949a4dbcbf46ab5jim * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
85816edcdb4137f90186cb437949a4dbcbf46ab5jim * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
85816edcdb4137f90186cb437949a4dbcbf46ab5jim * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
85816edcdb4137f90186cb437949a4dbcbf46ab5jim * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
85816edcdb4137f90186cb437949a4dbcbf46ab5jim * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
ee5db395bc3723609919edfa96af387eea37c491jim * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
85816edcdb4137f90186cb437949a4dbcbf46ab5jim * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
dfa73086d0dbdce20b24181dd127f5dd952367d4sf * SUCH DAMAGE.
dfa73086d0dbdce20b24181dd127f5dd952367d4sf * ====================================================================
82bf6fb0e137d91dc8addec5a66778c07f62532fhumbedooh *
dfa73086d0dbdce20b24181dd127f5dd952367d4sf * This software consists of voluntary contributions made by many
dfa73086d0dbdce20b24181dd127f5dd952367d4sf * individuals on behalf of the Apache Software Foundation. For more
dfa73086d0dbdce20b24181dd127f5dd952367d4sf * information on the Apache Software Foundation, please see
dfa73086d0dbdce20b24181dd127f5dd952367d4sf * <http://www.apache.org/>.
7087ccb939b4146f850f4d482fd8ab13de6335dchumbedooh *
dfa73086d0dbdce20b24181dd127f5dd952367d4sf * Portions of this software are based upon public domain software
7087ccb939b4146f850f4d482fd8ab13de6335dchumbedooh * originally written at the National Center for Supercomputing Applications,
85816edcdb4137f90186cb437949a4dbcbf46ab5jim * University of Illinois, Urbana-Champaign.
85816edcdb4137f90186cb437949a4dbcbf46ab5jim */
85816edcdb4137f90186cb437949a4dbcbf46ab5jim
85816edcdb4137f90186cb437949a4dbcbf46ab5jim#include "apr.h"
85816edcdb4137f90186cb437949a4dbcbf46ab5jim#include "apr_strings.h"
85816edcdb4137f90186cb437949a4dbcbf46ab5jim#include "apr_portable.h"
85816edcdb4137f90186cb437949a4dbcbf46ab5jim#include "apr_lib.h"
85816edcdb4137f90186cb437949a4dbcbf46ab5jim
85816edcdb4137f90186cb437949a4dbcbf46ab5jim#define APR_WANT_STRFUNC
85816edcdb4137f90186cb437949a4dbcbf46ab5jim#include "apr_want.h"
ee5db395bc3723609919edfa96af387eea37c491jim
ee5db395bc3723609919edfa96af387eea37c491jim#if APR_HAVE_SYS_TYPES_H
ee5db395bc3723609919edfa96af387eea37c491jim#include <sys/types.h>
ee5db395bc3723609919edfa96af387eea37c491jim#endif
7087ccb939b4146f850f4d482fd8ab13de6335dchumbedooh
ee5db395bc3723609919edfa96af387eea37c491jim#include "ap_config.h"
7087ccb939b4146f850f4d482fd8ab13de6335dchumbedooh#include "httpd.h"
ee5db395bc3723609919edfa96af387eea37c491jim#include "http_log.h"
ee5db395bc3723609919edfa96af387eea37c491jim#include "http_main.h"
ee5db395bc3723609919edfa96af387eea37c491jim#include "http_core.h"
ee5db395bc3723609919edfa96af387eea37c491jim#include "http_config.h"
ee5db395bc3723609919edfa96af387eea37c491jim#include "ap_mpm.h"
ee5db395bc3723609919edfa96af387eea37c491jim
ee5db395bc3723609919edfa96af387eea37c491jim#include "mpm.h"
85816edcdb4137f90186cb437949a4dbcbf46ab5jim#include "scoreboard.h"
85816edcdb4137f90186cb437949a4dbcbf46ab5jim
85816edcdb4137f90186cb437949a4dbcbf46ab5jimAP_DECLARE_DATA scoreboard *ap_scoreboard_image = NULL;
85816edcdb4137f90186cb437949a4dbcbf46ab5jimAP_DECLARE_DATA const char *ap_scoreboard_fname=NULL;
85816edcdb4137f90186cb437949a4dbcbf46ab5jimAP_DECLARE_DATA int ap_extended_status = 0;
85816edcdb4137f90186cb437949a4dbcbf46ab5jimAP_DECLARE_DATA apr_time_t ap_restart_time = 0;
ee5db395bc3723609919edfa96af387eea37c491jim
ee5db395bc3723609919edfa96af387eea37c491jim#if APR_HAS_SHARED_MEMORY
ee5db395bc3723609919edfa96af387eea37c491jim#include "apr_shmem.h"
ee5db395bc3723609919edfa96af387eea37c491jimstatic apr_shmem_t *scoreboard_shm = NULL;
7087ccb939b4146f850f4d482fd8ab13de6335dchumbedooh#endif
ee5db395bc3723609919edfa96af387eea37c491jim
7087ccb939b4146f850f4d482fd8ab13de6335dchumbedoohAPR_HOOK_STRUCT(
ee5db395bc3723609919edfa96af387eea37c491jim APR_HOOK_LINK(pre_mpm)
ee5db395bc3723609919edfa96af387eea37c491jim)
ee5db395bc3723609919edfa96af387eea37c491jim
30db430cb6aaf0c9c3c1bfe1a6c7ddb7ef8a4efcjimAP_IMPLEMENT_HOOK_VOID(pre_mpm,
30db430cb6aaf0c9c3c1bfe1a6c7ddb7ef8a4efcjim (apr_pool_t *p, ap_scoreboard_e sb_type),
85816edcdb4137f90186cb437949a4dbcbf46ab5jim (p, sb_type))
typedef struct sb_handle {
int child_num;
int thread_num;
} sb_handle;
static int server_limit, thread_limit;
static apr_size_t scoreboard_size;
/*
* ToDo:
* This function should be renamed to cleanup_shared
* 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.
*/
static apr_status_t ap_cleanup_shared_mem(void *d)
{
#if APR_HAS_SHARED_MEMORY
apr_shm_free(scoreboard_shm, ap_scoreboard_image);
ap_scoreboard_image = NULL;
apr_shm_destroy(scoreboard_shm);
#endif
return APR_SUCCESS;
}
static void calc_scoreboard_size(void)
{
ap_mpm_query(AP_MPMQ_HARD_LIMIT_THREADS, &thread_limit);
ap_mpm_query(AP_MPMQ_HARD_LIMIT_DAEMONS, &server_limit);
scoreboard_size = sizeof(scoreboard);
scoreboard_size += sizeof(process_score) * server_limit;
scoreboard_size += sizeof(worker_score * ) * server_limit;
scoreboard_size += sizeof(worker_score) * server_limit * thread_limit;
}
static void init_scoreboard(void)
{
char *more_storage;
int i;
memset(ap_scoreboard_image, 0, scoreboard_size);
more_storage = (char *)(ap_scoreboard_image + 1);
ap_scoreboard_image->parent = (process_score *)more_storage;
more_storage += sizeof(process_score) * server_limit;
ap_scoreboard_image->servers = (worker_score **)more_storage;
more_storage += server_limit * sizeof(worker_score *);
for (i = 0; i < server_limit; i++) {
ap_scoreboard_image->servers[i] = (worker_score *)more_storage;
more_storage += thread_limit * sizeof(worker_score);
}
ap_assert(more_storage == (char *)ap_scoreboard_image + scoreboard_size);
}
/* 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 setup_shared(apr_pool_t *p)
{
#if APR_HAS_SHARED_MEMORY
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) {
ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
"Fatal error: could not open(create) scoreboard");
return rv;
}
ap_scoreboard_image = apr_shm_malloc(scoreboard_shm, scoreboard_size);
if (ap_scoreboard_image == NULL) {
ap_log_error(APLOG_MARK, APLOG_CRIT | APLOG_NOERRNO, 0, NULL,
"Fatal error: cannot allocate scoreboard");
apr_shm_destroy(scoreboard_shm);
return APR_EGENERAL;
}
/* everything will be cleared shortly */
#endif
return APR_SUCCESS;
}
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;
apr_status_t rv;
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) {
rv = setup_shared(p);
if (rv) {
exit(APEXIT_INIT); /* XXX need to return an error from this function */
}
}
else {
/* A simple malloc will suffice */
ap_scoreboard_image = (scoreboard *) malloc(scoreboard_size);
if (ap_scoreboard_image == NULL) {
ap_log_error(APLOG_MARK, APLOG_CRIT | APLOG_NOERRNO, 0, NULL,
"(%d)%s: cannot allocate scoreboard",
errno, strerror(errno));
exit(APEXIT_INIT); /* XXX need to return an error from this function */
}
}
}
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);
}