scoreboard.c revision a72ba68ecbbc61e4b513e50d6000245c33f753dc
08cb74ca432a8c24e39f17dedce527e6a47b8001jerenkrantz/* Licensed to the Apache Software Foundation (ASF) under one or more
08cb74ca432a8c24e39f17dedce527e6a47b8001jerenkrantz * contributor license agreements. See the NOTICE file distributed with
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * this work for additional information regarding copyright ownership.
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * The ASF licenses this file to You under the Apache License, Version 2.0
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * (the "License"); you may not use this file except in compliance with
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * the License. You may obtain a copy of the License at
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * Unless required by applicable law or agreed to in writing, software
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * distributed under the License is distributed on an "AS IS" BASIS,
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * See the License for the specific language governing permissions and
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * limitations under the License.
7fae9cc4639013f3c04c085547256c68814aee8ftrawickAP_DECLARE_DATA scoreboard *ap_scoreboard_image = NULL;
7fae9cc4639013f3c04c085547256c68814aee8ftrawickAP_DECLARE_DATA const char *ap_scoreboard_fname = NULL;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic /* but must be exported to mpm_winnt */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic int server_limit, thread_limit, lb_limit, lb_size;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * This function should be renamed to cleanup_shared
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * and it should handle cleaning up a scoreboard shared
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * between processes using any form of IPC (file, shared memory
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * segment, etc.). Leave it as is now because it is being used
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * by various MPMs.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_mpm_query(AP_MPMQ_HARD_LIMIT_THREADS, &thread_limit);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_mpm_query(AP_MPMQ_HARD_LIMIT_DAEMONS, &server_limit);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding pfn_proxy_lb_workers = APR_RETRIEVE_OPTIONAL_FN(ap_proxy_lb_workers);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding pfn_proxy_lb_worker_size = APR_RETRIEVE_OPTIONAL_FN(ap_proxy_lb_worker_size);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding scoreboard_size += sizeof(process_score) * server_limit;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding scoreboard_size += sizeof(worker_score) * server_limit * thread_limit;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding calloc(1, sizeof(scoreboard) + server_limit * sizeof(worker_score *));
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_scoreboard_image->global = (global_score *)more_storage;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_scoreboard_image->parent = (process_score *)more_storage;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding more_storage += sizeof(process_score) * server_limit;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding (worker_score **)((char*)ap_scoreboard_image + sizeof(scoreboard));
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (i = 0; i < server_limit; i++) {
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm ap_scoreboard_image->servers[i] = (worker_score *)more_storage;
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick ap_scoreboard_image->balancers = (void *)more_storage;
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick ap_assert(more_storage == (char*)shared_score + scoreboard_size);
58fd79b56eb624bf011772994e9761d3c2e228c1orlikowski ap_scoreboard_image->global->server_limit = server_limit;
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick ap_scoreboard_image->global->thread_limit = thread_limit;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Create a name-based scoreboard in the given pool using the
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick * given filename.
e160b861b50a3a8dcc013b8cd3ef849fe777e52fgregamesstatic apr_status_t create_namebased_scoreboard(apr_pool_t *pool,
e160b861b50a3a8dcc013b8cd3ef849fe777e52fgregames const char *fname)
e160b861b50a3a8dcc013b8cd3ef849fe777e52fgregames /* The shared memory file must not exist before we create the
e160b861b50a3a8dcc013b8cd3ef849fe777e52fgregames * segment. */
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick rv = apr_shm_create(&ap_scoreboard_shm, scoreboard_size, fname, pool);
560f6ac786d611b858b2bad932713d9e971f0716trawick "unable to create or access scoreboard \"%s\" "
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick#endif /* APR_HAS_SHARED_MEMORY */
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick/* ToDo: This function should be made to handle setting up
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick * a scoreboard shared between processes using any IPC technique,
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick * not just a shared memory segment
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawickstatic apr_status_t open_scoreboard(apr_pool_t *pconf)
785be1b6298010956622771c870ab3cd8ca57a2faaron /* We don't want to have to recreate the scoreboard after
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick * restarts, so we'll create a global pool and never clean it.
066877f1a045103acfdd376d48cdd473c33f409bdougm "Fatal error: unable to create global pool "
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick "for use by the scoreboard");
74b39333600dee3260355ad3a06e36ef6c61c8f1dreid /* The config says to create a name-based shmem */
74b39333600dee3260355ad3a06e36ef6c61c8f1dreid /* make sure it's an absolute pathname */
97c78987224dcd037076d393aad1867c26b2c8cftrawick fname = ap_server_root_relative(pconf, ap_scoreboard_fname);
97c78987224dcd037076d393aad1867c26b2c8cftrawick ap_log_error(APLOG_MARK, APLOG_CRIT, APR_EBADPATH, NULL,
97c78987224dcd037076d393aad1867c26b2c8cftrawick "Fatal error: Invalid Scoreboard path %s",
97c78987224dcd037076d393aad1867c26b2c8cftrawick return create_namebased_scoreboard(global_pool, fname);
97c78987224dcd037076d393aad1867c26b2c8cftrawick else { /* config didn't specify, we get to choose shmem type */
97c78987224dcd037076d393aad1867c26b2c8cftrawick rv = apr_shm_create(&ap_scoreboard_shm, scoreboard_size, NULL,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "Unable to create or access scoreboard "
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "(anonymous shared memory failure)");
785be1b6298010956622771c870ab3cd8ca57a2faaron /* Make up a filename and do name-based shmem */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* Make sure it's an absolute pathname */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding fname = ap_server_root_relative(pconf, ap_scoreboard_fname);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return create_namebased_scoreboard(global_pool, fname);
785be1b6298010956622771c870ab3cd8ca57a2faaron#endif /* APR_HAS_SHARED_MEMORY */
785be1b6298010956622771c870ab3cd8ca57a2faaron/* If detach is non-zero, this is a seperate child process,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * if zero, it is a forked child.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingapr_status_t ap_reopen_scoreboard(apr_pool_t *p, apr_shm_t **shm, int detached)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (apr_shm_size_get(ap_scoreboard_shm) < scoreboard_size) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "Fatal error: shared scoreboard too small for child!");
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* everything will be cleared shortly */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (ap_scoreboard_image->global->sb_type == SB_SHARED) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* Create or reinit an existing scoreboard. The MPM can control whether
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * the scoreboard is shared across multiple processes or not
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingint ap_create_scoreboard(apr_pool_t *p, ap_scoreboard_e sb_type)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding running_gen = ap_scoreboard_image->global->running_generation;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_scoreboard_image->global->restart_time = apr_time_now();
785be1b6298010956622771c870ab3cd8ca57a2faaron for (i = 0; i < server_limit; i++) {
785be1b6298010956622771c870ab3cd8ca57a2faaron /* Clean up the lb workers data */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (rv || !(sb_shared = apr_shm_baseaddr_get(ap_scoreboard_shm))) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* A simple malloc will suffice */
a7ed9c525f9460187f327cea953bf90ecf1bdc51gstein "(%d)%s: cannot allocate scoreboard",
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick ap_scoreboard_image->global->running_generation = running_gen;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_scoreboard_image->global->restart_time = apr_time_now();
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick apr_pool_cleanup_register(p, NULL, ap_cleanup_scoreboard, apr_pool_cleanup_null);
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick/* Routines called to deal with the scoreboard image
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * --- note that we do *not* need write locks, since update_child_status
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * only updates a *single* record in place, and only one process writes to
785be1b6298010956622771c870ab3cd8ca57a2faaron * a given scoreboard slot at a time (either the child process owning that
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * slot, or the parent, noting that the child has died).
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * As a final note --- setting the score entry to getpid() is always safe,
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * since when the parent is writing an entry, it's only noting SERVER_DEAD
785be1b6298010956622771c870ab3cd8ca57a2faaronAP_DECLARE(void) ap_increment_counts(ap_sb_handle_t *sb, request_rec *r)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ws = &ap_scoreboard_image->servers[sb->child_num][sb->thread_num];
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick ap_mpm_query(AP_MPMQ_MAX_DAEMONS, &max_daemons_limit);
39b76a07959a0a332366c735a23894d9e8ed6872trawick for (i = 0; i < max_daemons_limit; ++i) {
8fd7c5046d164fb0959222497e5925dfc6a52ff3trawick if (ap_scoreboard_image->parent[i].pid == pid->pid) {
8f8ec0957334f50b7ac11359f90490ee467258eedreidAP_DECLARE(void) ap_create_sb_handle(ap_sb_handle_t **new_sbh, apr_pool_t *p,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *new_sbh = (ap_sb_handle_t *)apr_palloc(p, sizeof(ap_sb_handle_t));
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingAP_DECLARE(int) ap_update_child_status_from_indexes(int child_num,
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf return -1;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ws = &ap_scoreboard_image->servers[child_num][thread_num];
785be1b6298010956622771c870ab3cd8ca57a2faaron ws->thread_num = child_num * thread_limit + thread_num;
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf * Reset individual counters
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf apr_cpystrn(ws->client, ap_get_remote_host(c, r->per_dir_config,
785be1b6298010956622771c870ab3cd8ca57a2faaron apr_cpystrn(ws->request, "NULL", sizeof(ws->request));
785be1b6298010956622771c870ab3cd8ca57a2faaron apr_cpystrn(ws->request, r->the_request, sizeof(ws->request));
785be1b6298010956622771c870ab3cd8ca57a2faaron /* Don't reveal the password in the server-status view */
785be1b6298010956622771c870ab3cd8ca57a2faaron apr_cpystrn(ws->request, apr_pstrcat(r->pool, r->method, " ",
785be1b6298010956622771c870ab3cd8ca57a2faaronAP_DECLARE(int) ap_update_child_status(ap_sb_handle_t *sbh, int status,
785be1b6298010956622771c870ab3cd8ca57a2faaron return ap_update_child_status_from_indexes(sbh->child_num, sbh->thread_num,
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanfvoid ap_time_process_request(ap_sb_handle_t *sbh, int status)
785be1b6298010956622771c870ab3cd8ca57a2faaron ws = &ap_scoreboard_image->servers[sbh->child_num][sbh->thread_num];
785be1b6298010956622771c870ab3cd8ca57a2faaronAP_DECLARE(worker_score *) ap_get_scoreboard_worker_from_indexes(int x, int y)
785be1b6298010956622771c870ab3cd8ca57a2faaron if (((x < 0) || (x > server_limit)) ||
2d399cd7535887fceaa9f8f116eb98ce68ddd602trawick ((y < 0) || (y > thread_limit))) {
785be1b6298010956622771c870ab3cd8ca57a2faaronAP_DECLARE(worker_score *) ap_get_scoreboard_worker(ap_sb_handle_t *sbh)
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf return ap_get_scoreboard_worker_from_indexes(sbh->child_num,
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanfAP_DECLARE(process_score *) ap_get_scoreboard_process(int x)
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf if ((x < 0) || (x >= server_limit)) {
785be1b6298010956622771c870ab3cd8ca57a2faaronAP_DECLARE(lb_score *) ap_get_scoreboard_lb(int lb_num)
785be1b6298010956622771c870ab3cd8ca57a2faaron if ( (lb_num < 0) || (lb_limit < lb_num) || (lb_size==0) ) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return (lb_score *) ( ((char *) ap_scoreboard_image->balancers) +