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