📄 threaded.c
字号:
} my_info->pid = my_child_num; my_info->tid = i; my_info->sd = 0; apr_pool_create(&my_info->tpool, pchild); ap_update_child_status(my_child_num, i, SERVER_STARTING, (request_rec *) NULL); worker_thread(my_info);}static int make_child(server_rec *s, int slot) { int pid; if (slot + 1 > ap_max_daemons_limit) { ap_max_daemons_limit = slot + 1; } if (one_process) { set_signals(); ap_scoreboard_image->parent[slot].pid = getpid(); child_main(slot); } /* Tag this slot as occupied so that perform_idle_server_maintenance * doesn't try to steal it */ (void) ap_update_child_status(slot, 0, SERVER_STARTING, (request_rec *) NULL); if ((pid = fork()) == -1) { ap_log_error(APLOG_MARK, APLOG_ERR, errno, s, "fork: Unable to fork new process"); /* fork didn't succeed. Fix the scoreboard or else * it will say SERVER_STARTING forever and ever */ (void) ap_update_child_status(slot, 0, SERVER_DEAD, (request_rec *) NULL); /* In case system resources are maxxed out, we don't want Apache running away with the CPU trying to fork over and over and over again. */ sleep(10); return -1; } if (!pid) {#ifdef AIX_BIND_PROCESSOR /* By default, AIX binds to a single processor. This bit unbinds children which will then bind to another CPU. */#include <sys/processor.h> int status = bindprocessor(BINDPROCESS, (int)getpid(), PROCESSOR_CLASS_ANY); if (status != OK) ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, errno, ap_server_conf, "processor unbind failed %d", status);#endif RAISE_SIGSTOP(MAKE_CHILD); apr_signal(SIGTERM, just_die); child_main(slot); clean_child_exit(0); } /* else */ ap_scoreboard_image->parent[slot].pid = pid; return 0;}/* start up a bunch of children */static void startup_children(int number_to_start){ int i; for (i = 0; number_to_start && i < ap_daemons_limit; ++i) { if (ap_scoreboard_image->parent[i].pid != 0) { continue; } if (make_child(ap_server_conf, i) < 0) { break; } --number_to_start; }}/* * idle_spawn_rate is the number of children that will be spawned on the * next maintenance cycle if there aren't enough idle servers. It is * doubled up to MAX_SPAWN_RATE, and reset only when a cycle goes by * without the need to spawn. */static int idle_spawn_rate = 1;#ifndef MAX_SPAWN_RATE#define MAX_SPAWN_RATE (32)#endifstatic int hold_off_on_exponential_spawning;static void perform_idle_server_maintenance(void){ int i, j; int idle_thread_count; short_score *ss; int free_length; int free_slots[MAX_SPAWN_RATE]; int last_non_dead; int total_non_dead; apr_size_t one = 1; apr_status_t rv; /* initialize the free_list */ free_length = 0; idle_thread_count = 0; last_non_dead = -1; total_non_dead = 0; ap_sync_scoreboard_image(); for (i = 0; i < ap_daemons_limit; ++i) { /* Initialization to satisfy the compiler. It doesn't know * that ap_threads_per_child is always > 0 */ int status = SERVER_DEAD; int any_dying_threads = 0; int all_dead_threads = 1; int idle_thread_addition = 0; if (i >= ap_max_daemons_limit && free_length == idle_spawn_rate) break; for (j = 0; j < ap_threads_per_child; j++) { ss = &ap_scoreboard_image->servers[i][j]; status = ss->status; any_dying_threads = any_dying_threads || (status == SERVER_DEAD) || (status == SERVER_GRACEFUL); all_dead_threads = all_dead_threads && (status == SERVER_DEAD); /* We consider a starting server as idle because we started it * at least a cycle ago, and if it still hasn't finished starting * then we're just going to swamp things worse by forking more. * So we hopefully won't need to fork more if we count it. * This depends on the ordering of SERVER_READY and SERVER_STARTING. */ if (status <= SERVER_READY) { ++idle_thread_addition; } } if (all_dead_threads && free_length < idle_spawn_rate) { free_slots[free_length] = i; ++free_length; } if (!all_dead_threads) { last_non_dead = i; } if (!any_dying_threads) { ++total_non_dead; idle_thread_count += idle_thread_addition; } } ap_max_daemons_limit = last_non_dead + 1; if (idle_thread_count > max_spare_threads) { /* Kill off one child */ char char_of_death = '!'; if ((rv = apr_file_write(pipe_of_death_out, &char_of_death, &one)) != APR_SUCCESS) { ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf, "write pipe_of_death"); } idle_spawn_rate = 1; } else if (idle_thread_count < min_spare_threads) { /* terminate the free list */ if (free_length == 0) { /* only report this condition once */ static int reported = 0; if (!reported) { ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, ap_server_conf, "server reached MaxClients setting, consider" " raising the MaxClients setting"); reported = 1; } idle_spawn_rate = 1; } else { if (idle_spawn_rate >= 8) { ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, 0, ap_server_conf, "server seems busy, (you may need " "to increase StartServers, ThreadsPerChild " "or Min/MaxSpareThreads), " "spawning %d children, there are around %d idle " "threads, and %d total children", idle_spawn_rate, idle_thread_count, total_non_dead); } for (i = 0; i < free_length; ++i) { make_child(ap_server_conf, free_slots[i]); } /* the next time around we want to spawn twice as many if this * wasn't good enough, but not if we've just done a graceful */ if (hold_off_on_exponential_spawning) { --hold_off_on_exponential_spawning; } else if (idle_spawn_rate < MAX_SPAWN_RATE) { idle_spawn_rate *= 2; } } } else { idle_spawn_rate = 1; }}static void server_main_loop(int remaining_children_to_start){ int child_slot; apr_wait_t status; apr_proc_t pid; int i; while (!restart_pending && !shutdown_pending) { ap_wait_or_timeout(&status, &pid, pconf); if (pid.pid != -1) { ap_process_child_status(&pid, status); /* non-fatal death... note that it's gone in the scoreboard. */ child_slot = find_child_by_pid(&pid); if (child_slot >= 0) { for (i = 0; i < ap_threads_per_child; i++) ap_update_child_status(child_slot, i, SERVER_DEAD, (request_rec *) NULL); if (remaining_children_to_start && child_slot < ap_daemons_limit) { /* we're still doing a 1-for-1 replacement of dead * children with new children */ make_child(ap_server_conf, child_slot); --remaining_children_to_start; }#if APR_HAS_OTHER_CHILD } else if (apr_proc_other_child_read(&pid, status) == 0) { /* handled */#endif } else if (is_graceful) { /* Great, we've probably just lost a slot in the * scoreboard. Somehow we don't know about this child. */ ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, 0, ap_server_conf, "long lost child came home! (pid %ld)", (long)pid.pid); } /* Don't perform idle maintenance when a child dies, * only do it when there's a timeout. Remember only a * finite number of children can die, and it's pretty * pathological for a lot to die suddenly. */ continue; } else if (remaining_children_to_start) { /* we hit a 1 second timeout in which none of the previous * generation of children needed to be reaped... so assume * they're all done, and pick up the slack if any is left. */ startup_children(remaining_children_to_start); remaining_children_to_start = 0; /* In any event we really shouldn't do the code below because * few of the servers we just started are in the IDLE state * yet, so we'd mistakenly create an extra server. */ continue; } perform_idle_server_maintenance(); }}int ap_mpm_run(apr_pool_t *_pconf, apr_pool_t *plog, server_rec *s){ int remaining_children_to_start; apr_status_t rv; apr_size_t one = 1; pconf = _pconf; ap_server_conf = s; rv = apr_file_pipe_create(&pipe_of_death_in, &pipe_of_death_out, pconf); if (rv != APR_SUCCESS) { ap_log_error(APLOG_MARK, APLOG_ERR, rv, (const server_rec*) ap_server_conf, "apr_file_pipe_create (pipe_of_death)"); exit(1); } if ((rv = apr_file_pipe_timeout_set(pipe_of_death_in, 0)) != APR_SUCCESS) { ap_log_error(APLOG_MARK, APLOG_ERR, rv, (const server_rec*) ap_server_conf, "apr_file_pipe_timeout_set (pipe_of_death)"); exit(1); } ap_server_conf = s; if ((num_listensocks = ap_setup_listeners(ap_server_conf)) < 1) { /* XXX: hey, what's the right way for the mpm to indicate a fatal error? */ ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ALERT, 0, s, "no listening sockets available, shutting down"); return 1; } ap_log_pid(pconf, ap_pid_fname); /* Initialize cross-process accept lock */ lock_fname = apr_psprintf(_pconf, "%s.%u", ap_server_root_relative(_pconf, lock_fname), ap_my_pid); rv = apr_lock_create(&accept_mutex, APR_MUTEX, APR_LOCKALL, lock_fname, _pconf); if (rv != APR_SUCCESS) { ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, "Couldn't create accept lock"); return 1; } if (!is_graceful) { ap_create_scoreboard(pconf, SB_SHARED); } set_signals(); /* Don't thrash... */ if (max_spare_threads < min_spare_threads + ap_threads_per_child) max_spare_threads = min_spare_threads + ap_threads_per_child; /* If we're doing a graceful_restart then we're going to see a lot * of children exiting immediately when we get into the main loop * below (because we just sent them SIGWINCH). This happens pretty * rapidly... and for each one that exits we'll start a new one until * we reach at least daemons_min_free. But we may be permitted to * start more than that, so we'll just keep track of how many we're * supposed to start up without the 1 second penalty between each fork. */ remaining_children_to_start = ap_daemons_to_start; if (remaining_children_to_start > ap_daemons_limit) { remaining_children_to_start = ap_daemons_limit; } if (!is_graceful) { startup_children(remaining_children_to_start); remaining_children_to_start = 0; } else { /* give the system some time to recover before kicking into * exponential mode */ hold_off_on_exponential_spawning = 10;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -