📄 worker.c
字号:
if (threads[i]) { /* if we ever created this thread */#ifdef HAVE_PTHREAD_KILL apr_os_thread_t *worker_os_thread; apr_os_thread_get(&worker_os_thread, threads[i]); pthread_kill(*worker_os_thread, WORKER_SIGNAL);#endif rv = apr_thread_join(&thread_rv, threads[i]); if (rv != APR_SUCCESS) { ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf, "apr_thread_join: unable to join worker " "thread %d", i); } } }}static void join_start_thread(apr_thread_t *start_thread_id){ apr_status_t rv, thread_rv; start_thread_may_exit = 1; /* tell it to give up in case it is still * trying to take over slots from a * previous generation */ rv = apr_thread_join(&thread_rv, start_thread_id); if (rv != APR_SUCCESS) { ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf, "apr_thread_join: unable to join the start " "thread"); }}static void child_main(int child_num_arg){ apr_thread_t **threads; apr_status_t rv; thread_starter *ts; apr_threadattr_t *thread_attr; apr_thread_t *start_thread_id; mpm_state = AP_MPMQ_STARTING; /* for benefit of any hooks that run as this * child initializes */ ap_my_pid = getpid(); ap_fatal_signal_child_setup(ap_server_conf); apr_pool_create(&pchild, pconf); /*stuff to do before we switch id's, so we have permissions.*/ ap_reopen_scoreboard(pchild, NULL, 0); rv = SAFE_ACCEPT(apr_proc_mutex_child_init(&accept_mutex, ap_lock_fname, pchild)); if (rv != APR_SUCCESS) { ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf, "Couldn't initialize cross-process lock in child"); clean_child_exit(APEXIT_CHILDFATAL); } if (unixd_setup_child()) { clean_child_exit(APEXIT_CHILDFATAL); } ap_run_child_init(pchild, ap_server_conf); /* done with init critical section */ /* Just use the standard apr_setup_signal_thread to block all signals * from being received. The child processes no longer use signals for * any communication with the parent process. */ rv = apr_setup_signal_thread(); if (rv != APR_SUCCESS) { ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf, "Couldn't initialize signal thread"); clean_child_exit(APEXIT_CHILDFATAL); } if (ap_max_requests_per_child) { requests_this_child = ap_max_requests_per_child; } else { /* coding a value of zero means infinity */ requests_this_child = INT_MAX; } /* Setup worker threads */ /* clear the storage; we may not create all our threads immediately, * and we want a 0 entry to indicate a thread which was not created */ threads = (apr_thread_t **)calloc(1, sizeof(apr_thread_t *) * ap_threads_per_child); if (threads == NULL) { ap_log_error(APLOG_MARK, APLOG_ALERT, errno, ap_server_conf, "malloc: out of memory"); clean_child_exit(APEXIT_CHILDFATAL); } ts = (thread_starter *)apr_palloc(pchild, sizeof(*ts)); apr_threadattr_create(&thread_attr, pchild); /* 0 means PTHREAD_CREATE_JOINABLE */ apr_threadattr_detach_set(thread_attr, 0); if (ap_thread_stacksize != 0) { apr_threadattr_stacksize_set(thread_attr, ap_thread_stacksize); } ts->threads = threads; ts->listener = NULL; ts->child_num_arg = child_num_arg; ts->threadattr = thread_attr; rv = apr_thread_create(&start_thread_id, thread_attr, start_threads, ts, pchild); if (rv != APR_SUCCESS) { ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf, "apr_thread_create: unable to create worker thread"); /* let the parent decide how bad this really is */ clean_child_exit(APEXIT_CHILDSICK); } mpm_state = AP_MPMQ_RUNNING; /* If we are only running in one_process mode, we will want to * still handle signals. */ if (one_process) { /* Block until we get a terminating signal. */ apr_signal_thread(check_signal); /* make sure the start thread has finished; signal_threads() * and join_workers() depend on that */ /* XXX join_start_thread() won't be awakened if one of our * threads encounters a critical error and attempts to * shutdown this child */ join_start_thread(start_thread_id); signal_threads(ST_UNGRACEFUL); /* helps us terminate a little more * quickly than the dispatch of the signal thread * beats the Pipe of Death and the browsers */ /* A terminating signal was received. Now join each of the * workers to clean them up. * If the worker already exited, then the join frees * their resources and returns. * If the worker hasn't exited, then this blocks until * they have (then cleans up). */ join_workers(ts->listener, threads); } else { /* !one_process */ /* remove SIGTERM from the set of blocked signals... if one of * the other threads in the process needs to take us down * (e.g., for MaxRequestsPerChild) it will send us SIGTERM */ unblock_signal(SIGTERM); apr_signal(SIGTERM, dummy_signal_handler); /* Watch for any messages from the parent over the POD */ while (1) { rv = ap_mpm_pod_check(pod); if (rv == AP_NORESTART) { /* see if termination was triggered while we slept */ switch(terminate_mode) { case ST_GRACEFUL: rv = AP_GRACEFUL; break; case ST_UNGRACEFUL: rv = AP_RESTART; break; } } if (rv == AP_GRACEFUL || rv == AP_RESTART) { /* make sure the start thread has finished; * signal_threads() and join_workers depend on that */ join_start_thread(start_thread_id); signal_threads(rv == AP_GRACEFUL ? ST_GRACEFUL : ST_UNGRACEFUL); break; } } /* A terminating signal was received. Now join each of the * workers to clean them up. * If the worker already exited, then the join frees * their resources and returns. * If the worker hasn't exited, then this blocks until * they have (then cleans up). */ join_workers(ts->listener, threads); } free(threads); clean_child_exit(resource_shortage ? APEXIT_CHILDSICK : 0);}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); } 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 */ ap_update_child_status_from_indexes(slot, 0, SERVER_DEAD, 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. */ apr_sleep(apr_time_from_sec(10)); return -1; } if (!pid) {#ifdef HAVE_BINDPROCESSOR /* By default, AIX binds to a single processor. This bit unbinds * children which will then bind to another CPU. */ int status = bindprocessor(BINDPROCESS, (int)getpid(), PROCESSOR_CLASS_ANY); if (status != OK) ap_log_error(APLOG_MARK, 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 */ if (ap_scoreboard_image->parent[slot].pid != 0) { /* This new child process is squatting on the scoreboard * entry owned by an exiting child process, which cannot * exit until all active requests complete. * Don't forget about this exiting child process, or we * won't be able to kill it if it doesn't exit by the * time the server is shut down. */ ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf, "taking over scoreboard slot from %" APR_PID_T_FMT "%s", ap_scoreboard_image->parent[slot].pid, ap_scoreboard_image->parent[slot].quiescing ? " (quiescing)" : ""); ap_register_extra_mpm_process(ap_scoreboard_image->parent[slot].pid); } ap_scoreboard_image->parent[slot].quiescing = 0; 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; worker_score *ws; process_score *ps; int free_length; int totally_free_length = 0; int free_slots[MAX_SPAWN_RATE]; int last_non_dead; int total_non_dead; int active_thread_count = 0; /* initialize the free_list */ free_length = 0; idle_thread_count = 0; last_non_dead = -1; total_non_dead = 0; 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 any_dead_threads = 0; int all_dead_threads = 1; if (i >= ap_max_daemons_limit && totally_free_length == idle_spawn_rate) break; ps = &ap_scoreboard_image->parent[i]; for (j = 0; j < ap_threads_per_child; j++) { ws = &ap_scoreboard_image->servers[i][j]; status = ws->status; /* XXX any_dying_threads is probably no longer needed GLA */ any_dying_threads = any_dying_threads || (status == SERVER_GRACEFUL); any_dead_threads = any_dead_threads || (status == SERVER_DEAD); all_dead_threads = all_dead_threads && (status == SERVER_DEAD || status == SERVER_GRACEFUL); /* 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 (ps->pid != 0) { /* XXX just set all_dead_threads in outer for loop if no pid? not much else matters */ if (status <= SERVER_READY && !ps->quiescing && ps->generation == ap_my_generation) { ++idle_thread_count; } if (status >= SERVER_READY && status < SERVER_GRACEFUL) { ++active_thread_count; } } } if (any_dead_threads && totally_free_length < idle_spawn_rate && free_length < MAX_SPAWN_RATE && (!ps->pid /* no process in the slot */ || ps->quiescing)) { /* or at least one is going away */ if (all_dead_threads) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -