simple_children.c revision e811d6e3aec5ec21c6de99652feff5d1f883daa6
842ae4bd224140319ae7feec1872b93dfd491143fielding/* Licensed to the Apache Software Foundation (ASF) under one or more
842ae4bd224140319ae7feec1872b93dfd491143fielding * contributor license agreements. See the NOTICE file distributed with
842ae4bd224140319ae7feec1872b93dfd491143fielding * this work for additional information regarding copyright ownership.
842ae4bd224140319ae7feec1872b93dfd491143fielding * The ASF licenses this file to You under the Apache License, Version 2.0
842ae4bd224140319ae7feec1872b93dfd491143fielding * (the "License"); you may not use this file except in compliance with
842ae4bd224140319ae7feec1872b93dfd491143fielding * the License. You may obtain a copy of the License at
2d2eda71267231c2526be701fe655db125852c1ffielding *
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * http://www.apache.org/licenses/LICENSE-2.0
2d2eda71267231c2526be701fe655db125852c1ffielding *
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.
2d2eda71267231c2526be701fe655db125852c1ffielding */
2d2eda71267231c2526be701fe655db125852c1ffielding
759f4a24d09e28c4eaca9f97311b497fc15cb5c7ben#include "httpd.h"
0432a26b69eedfb9ca5f34fba590236378a24851ben#include "http_config.h"
0432a26b69eedfb9ca5f34fba590236378a24851ben#include "simple_types.h"
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh#include "simple_event.h"
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh#include "simple_run.h"
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh#include "simple_children.h"
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh#include "apr_hash.h"
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh#define SPAWN_CHILDREN_INTERVAL (apr_time_from_sec(5))
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianhstatic void
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianhsimple_kill_random_child(simple_core_t *sc)
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh{
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh apr_thread_mutex_lock(sc->mtx);
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh /* See comment in simple_spawn_child for why we check here. */
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh if (!sc->run_single_process) {
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding apr_thread_mutex_unlock(sc->mtx);
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh}
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianhstatic void
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbbsimple_spawn_child(simple_core_t *sc)
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb{
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb apr_thread_mutex_lock(sc->mtx);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb /* Although we could cut this off 'earlier', and not even invoke this
2d2eda71267231c2526be701fe655db125852c1ffielding * function, I would like to keep the functions invoked when in debug mode
30c289e6bc6d28d210b21edd800ab2cfc78a8381wrowe * to be as close as possible to those when not in debug... So, we just skip
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein * the actual spawn itself, but go through all of the motions...
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein */
cd39d2139743ca0ef899953c6496dcf99e9c791atrawick if (!sc->run_single_process) {
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein
a261b81a1044c0e2ea3f21cf9b7019297bad16aajorton }
b627048681b27fe30f979ba471b523be3a6a22adrbb apr_thread_mutex_unlock(sc->mtx);
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein}
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein
44c46ef733836b32585d135d2d90856e7cfd9929rbbvoid
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbbsimple_check_children_size(simple_core_t *sc,
e6cc28a5eb3371ba0c38e941855e71ff0054f50erbb void *baton)
d9039a9aaeaa1359c1147af66c09aeac56ae3ee0pquerna{
cdccf2ac94172d1a4a54bc2e17324e1ef682ccf3pquerna simple_register_timer(sc,
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb simple_check_children_size,
9cfc48b742c224c1fbc2c26a4119a3266192c7d2wrowe NULL,
9cfc48b742c224c1fbc2c26a4119a3266192c7d2wrowe SPAWN_CHILDREN_INTERVAL);
ef5650b61a8e35f3cc93ec07e73efc17ea329894jorton unsigned int count;
7c7372abe2484e7fcf81937b93496d1246e5b816gstein int wanted;
a261b81a1044c0e2ea3f21cf9b7019297bad16aajorton int i;
a261b81a1044c0e2ea3f21cf9b7019297bad16aajorton
a261b81a1044c0e2ea3f21cf9b7019297bad16aajorton if (sc->run_single_process && sc->restart_num == 2) {
a261b81a1044c0e2ea3f21cf9b7019297bad16aajorton static int run = 0;
d71fd7cd9c4815d0647425f21ba3a803919a9148jorton /* This is kinda of hack, but rather than spawning a child process,
7c7372abe2484e7fcf81937b93496d1246e5b816gstein * we register the normal IO handlers in the main event loop....
7c7372abe2484e7fcf81937b93496d1246e5b816gstein */
7c7372abe2484e7fcf81937b93496d1246e5b816gstein if (run == 0) {
7c7372abe2484e7fcf81937b93496d1246e5b816gstein simple_single_process_hack(sc);
7c7372abe2484e7fcf81937b93496d1246e5b816gstein run++;
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding }
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh
2d2eda71267231c2526be701fe655db125852c1ffielding {
2d2eda71267231c2526be701fe655db125852c1ffielding apr_thread_mutex_lock(sc->mtx);
2d2eda71267231c2526be701fe655db125852c1ffielding count = apr_hash_count(sc->children);
4a13940dc2990df0a798718d3a3f9cf1566c2217bjh wanted = sc->procmgr.proc_count;
4a13940dc2990df0a798718d3a3f9cf1566c2217bjh apr_thread_mutex_unlock(sc->mtx);
4a13940dc2990df0a798718d3a3f9cf1566c2217bjh }
4a13940dc2990df0a798718d3a3f9cf1566c2217bjh
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh if (count > wanted) {
2d2eda71267231c2526be701fe655db125852c1ffielding /* kill some kids */
dd5cbadf2df719db2f3c769d03ec847da25854e6bnicholes int to_kill = count - wanted;
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh for (i = 0;
dd5cbadf2df719db2f3c769d03ec847da25854e6bnicholes i < to_kill;
2d2eda71267231c2526be701fe655db125852c1ffielding i++)
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh {
2d2eda71267231c2526be701fe655db125852c1ffielding simple_kill_random_child(sc);
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding else if (count < wanted) {
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb /* spawn some kids */
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb int to_spawn = wanted - count;
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb for (i = 0;
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb i < to_spawn;
72a4ef8eac1adef882246c5bfb9b8bbd82d613c4coar i++)
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb {
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb simple_spawn_child(sc);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb }
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh }
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh else {
2d2eda71267231c2526be701fe655db125852c1ffielding /* juuuuust right. */
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding}
4a13940dc2990df0a798718d3a3f9cf1566c2217bjh