1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000 The Apache Software Foundation. All rights
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. The end-user documentation included with the redistribution,
20 * if any, must include the following acknowledgment:
21 * "This product includes software developed by the
22 * Apache Software Foundation (http://www.apache.org/)."
23 * Alternately, this acknowledgment may appear in the software itself,
24 * if and wherever such third-party acknowledgments normally appear.
26 * 4. The names "Apache" and "Apache Software Foundation" must
27 * not be used to endorse or promote products derived from this
28 * software without prior written permission. For written
29 * permission, please contact apache@apache.org.
31 * 5. Products derived from this software may not be called "Apache",
32 * nor may "Apache" appear in their name, without prior written
33 * permission of the Apache Software Foundation.
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * ====================================================================
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation. For more
51 * information on the Apache Software Foundation, please see
52 * <http://www.apache.org/>.
54 * Portions of this software are based upon public domain software
55 * originally written at the National Center for Supercomputing Applications,
56 * University of Illinois, Urbana-Champaign.
59 #ifndef APACHE_SCOREBOARD_H
60 #define APACHE_SCOREBOARD_H
65 #ifdef HAVE_SYS_TIMES_H
67 #include <sys/times.h>
72 #include "mpm_default.h" /* For HARD_.*_LIMIT */
73 #include "apr_thread_proc.h"
75 /*The optimized timeout code only works if we're not using a scoreboard file*/
76 #if defined(AP_USE_MEM_BASED_SCOREBOARD)
77 #define OPTIMIZE_TIMEOUTS
80 /* Scoreboard info on a process is, for now, kept very brief ---
81 * just status value and pid (the latter so that the caretaker process
82 * can properly update the scoreboard when a process dies). We may want
83 * to eventually add a separate set of long_score structures which would
84 * give, for each process, the number of requests serviced, and info on
85 * the current, or most recent, request.
91 #define SERVER_STARTING 1 /* Server Starting up */
92 #define SERVER_READY 2 /* Waiting for connection (or accept() lock) */
93 #define SERVER_BUSY_READ 3 /* Reading a client request */
94 #define SERVER_BUSY_WRITE 4 /* Processing a client request */
95 #define SERVER_BUSY_KEEPALIVE 5 /* Waiting for more requests via keepalive */
96 #define SERVER_BUSY_LOG 6 /* Logging the request */
97 #define SERVER_BUSY_DNS 7 /* Looking up a hostname */
98 #define SERVER_GRACEFUL 8 /* server is gracefully finishing request */
99 #define SERVER_ACCEPTING 9 /* thread is accepting connections */
100 #define SERVER_QUEUEING 10 /* thread is putting connection on the queue */
101 #define SERVER_NUM_STATUS 11 /* number of status settings */
103 /* A "virtual time" is simply a counter that indicates that a child is
104 * making progress. The parent checks up on each child, and when they have
105 * made progress it resets the last_rtime element. But when the child hasn't
106 * made progress in a time that's roughly timeout_len seconds long, it is
109 * vtime is an optimization that is used only when the scoreboard is in
110 * shared memory (it's not easy/feasible to do it in a scoreboard file).
111 * The essential observation is that timeouts rarely occur, the vast majority
112 * of hits finish before any timeout happens. So it really sucks to have to
113 * ask the operating system to set up and destroy alarms many times during
116 typedef unsigned vtime_t;
118 /* Type used for generation indicies. Startup and every restart cause a
119 * new generation of children to be spawned. Children within the same
120 * generation share the same configuration information -- pointers to stuff
121 * created at config time in the parent are valid across children. For
122 * example, the vhostrec pointer in the scoreboard below is valid in all
123 * children of the same generation.
125 * The safe way to access the vhost pointer is like this:
127 * short_score *ss = pointer to whichver slot is interesting;
128 * parent_score *ps = pointer to whichver slot is interesting;
129 * server_rec *vh = ss->vhostrec;
131 * if (ps->generation != ap_my_generation) {
135 * then if vh is not NULL it's valid in this child.
137 * This avoids various race conditions around restarts.
139 typedef int ap_generation_t;
141 /* stuff which is thread/process specific */
143 #ifdef OPTIMIZE_TIMEOUTS
144 vtime_t cur_vtime; /* the child's current vtime */
145 unsigned short timeout_len; /* length of the timeout */
148 unsigned char status;
149 unsigned long access_count;
150 unsigned long bytes_served;
151 unsigned long my_access_count;
152 unsigned long my_bytes_served;
153 unsigned long conn_bytes;
154 unsigned short conn_count;
155 apr_time_t start_time;
156 apr_time_t stop_time;
160 #ifndef OPTIMIZE_TIMEOUTS
163 char client[32]; /* Keep 'em small... */
164 char request[64]; /* We just want an idea... */
165 server_rec *vhostrec; /* What virtual host is being accessed? */
166 /* SEE ABOVE FOR SAFE USAGE! */
170 ap_generation_t running_generation; /* the generation of children which
171 * should still be serving requests. */
174 /* stuff which the parent generally writes and the children rarely read */
177 ap_generation_t generation; /* generation of this child */
179 #ifdef OPTIMIZE_TIMEOUTS
180 time_t last_rtime; /* time(0) of the last change */
181 vtime_t last_vtime; /* the last vtime the parent has seen */
186 short_score servers[HARD_SERVER_LIMIT][HARD_THREAD_LIMIT];
187 parent_score parent[HARD_SERVER_LIMIT];
191 #define KEY_LENGTH 16
192 #define VALUE_LENGTH 64
194 char key[KEY_LENGTH];
195 char value[VALUE_LENGTH];
196 } status_table_entry;
198 #define STATUSES_PER_CONNECTION 10
202 table[HARD_SERVER_LIMIT*HARD_THREAD_LIMIT][STATUSES_PER_CONNECTION];
205 #define SCOREBOARD_SIZE sizeof(scoreboard)
206 #define NEW_SCOREBOARD_SIZE sizeof(new_scoreboard)
208 #define SCOREBOARD_NAME "SCOREBRD"
209 #define SCOREBOARD_FRAMES SCOREBOARD_SIZE/4095 + 1
212 AP_DECLARE(int) ap_exists_scoreboard_image(void);
213 void reinit_scoreboard(apr_pool_t *p);
214 apr_status_t ap_cleanup_shared_mem(void *d);
215 AP_DECLARE(void) ap_sync_scoreboard_image(void);
217 AP_DECLARE(void) reopen_scoreboard(apr_pool_t *p);
219 apr_inline void ap_sync_scoreboard_image(void);
220 void increment_counts(int child_num, int thread_num, request_rec *r);
221 void update_scoreboard_global(void);
222 AP_DECLARE(int) find_child_by_pid(apr_proc_t *pid);
223 int ap_update_child_status(int child_num, int thread_num, int status, request_rec *r);
224 void ap_time_process_request(int child_num, int thread_num, int status);
227 AP_DECLARE_DATA extern scoreboard *ap_scoreboard_image;
228 AP_DECLARE_DATA extern const char *ap_scoreboard_fname;
229 AP_DECLARE_DATA extern int ap_extended_status;
230 AP_DECLARE_DATA apr_time_t ap_restart_time;
232 AP_DECLARE_DATA extern ap_generation_t volatile ap_my_generation;
234 /* for time_process_request() in http_main.c */
235 #define START_PREQUEST 1
236 #define STOP_PREQUEST 2
242 #endif /* !APACHE_SCOREBOARD_H */