]> granicus.if.org Git - apache/blob - include/scoreboard.h
Get rid of some Apache 1.x OPTIMIZE_TIMEOUTS cruft. Other logic
[apache] / include / scoreboard.h
1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000-2001 The Apache Software Foundation.  All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
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
17  *    distribution.
18  *
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.
25  *
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.
30  *
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.
34  *
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
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
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/>.
53  *
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.
57  */
58
59 #ifndef APACHE_SCOREBOARD_H
60 #define APACHE_SCOREBOARD_H
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64
65 #ifdef HAVE_SYS_TIMES_H
66 #include <sys/time.h>
67 #include <sys/times.h>
68 #elif defined(TPF)
69 #include <time.h>
70 #endif
71
72 #include "mpm_default.h"        /* For HARD_.*_LIMIT */
73 #include "apr_thread_proc.h"
74 #include "apr_portable.h"
75
76 /* Scoreboard info on a process is, for now, kept very brief --- 
77  * just status value and pid (the latter so that the caretaker process
78  * can properly update the scoreboard when a process dies).  We may want
79  * to eventually add a separate set of long_score structures which would
80  * give, for each process, the number of requests serviced, and info on
81  * the current, or most recent, request.
82  *
83  * Status values:
84  */
85
86 #define SERVER_DEAD 0
87 #define SERVER_STARTING 1       /* Server Starting up */
88 #define SERVER_READY 2          /* Waiting for connection (or accept() lock) */
89 #define SERVER_BUSY_READ 3      /* Reading a client request */
90 #define SERVER_BUSY_WRITE 4     /* Processing a client request */
91 #define SERVER_BUSY_KEEPALIVE 5 /* Waiting for more requests via keepalive */
92 #define SERVER_BUSY_LOG 6       /* Logging the request */
93 #define SERVER_BUSY_DNS 7       /* Looking up a hostname */
94 #define SERVER_GRACEFUL 8       /* server is gracefully finishing request */
95 #define SERVER_ACCEPTING 9      /* thread is accepting connections */
96 #define SERVER_QUEUEING 10      /* thread is putting connection on the queue */
97 #define SERVER_IDLE_KILL 11     /* Server is cleaning up idle children. */
98 #define SERVER_NUM_STATUS 12    /* number of status settings */
99
100 /* A "virtual time" is simply a counter that indicates that a child is
101  * making progress.  The parent checks up on each child, and when they have
102  * made progress it resets the last_rtime element.  But when the child hasn't
103  * made progress in a time that's roughly timeout_len seconds long, it is
104  * sent a SIGALRM.
105  *
106  * vtime is an optimization that is used only when the scoreboard is in
107  * shared memory (it's not easy/feasible to do it in a scoreboard file).
108  * The essential observation is that timeouts rarely occur, the vast majority
109  * of hits finish before any timeout happens.  So it really sucks to have to
110  * ask the operating system to set up and destroy alarms many times during
111  * a request.
112  */
113 typedef unsigned vtime_t;
114
115 /* Type used for generation indicies.  Startup and every restart cause a
116  * new generation of children to be spawned.  Children within the same
117  * generation share the same configuration information -- pointers to stuff
118  * created at config time in the parent are valid across children.  For
119  * example, the vhostrec pointer in the scoreboard below is valid in all
120  * children of the same generation.
121  *
122  * The safe way to access the vhost pointer is like this:
123  *
124  * short_score *ss = pointer to whichver slot is interesting;
125  * parent_score *ps = pointer to whichver slot is interesting;
126  * server_rec *vh = ss->vhostrec;
127  *
128  * if (ps->generation != ap_my_generation) {
129  *     vh = NULL;
130  * }
131  *
132  * then if vh is not NULL it's valid in this child.
133  *
134  * This avoids various race conditions around restarts.
135  */
136 typedef int ap_generation_t;
137
138 /* Is the scoreboard shared between processes or not? 
139  * Set by the MPM when the scoreboard is created.
140  */
141 typedef enum {
142     SB_SHARED = 1,
143     SB_NOT_SHARED = 2
144 } ap_scoreboard_e;
145
146 #define SB_WORKING  0  /* The server is busy and the child is useful. */
147 #define SB_IDLE_DIE 1  /* The server is idle and the child is superfluous. */
148                        /*   The child should check for this and exit gracefully. */
149
150 /* stuff which is thread/process specific */
151 typedef struct {
152     int thread_num;
153 #if APR_HAS_THREADS
154     apr_os_thread_t tid;
155 #endif
156     unsigned char status;
157     unsigned long access_count;
158     unsigned long bytes_served;
159     unsigned long my_access_count;
160     unsigned long my_bytes_served;
161     unsigned long conn_bytes;
162     unsigned short conn_count;
163     unsigned short life_status;    /* Either SB_WORKING or SB_IDLE_DIE */
164     apr_time_t start_time;
165     apr_time_t stop_time;
166 #ifdef HAVE_TIMES
167     struct tms times;
168 #endif
169     time_t last_used;
170     char client[32];            /* Keep 'em small... */
171     char request[64];           /* We just want an idea... */
172     server_rec *vhostrec;       /* What virtual host is being accessed? */
173                                 /* SEE ABOVE FOR SAFE USAGE! */
174 } short_score;
175
176 typedef struct {
177     ap_scoreboard_e sb_type;
178     ap_generation_t running_generation; /* the generation of children which
179                                          * should still be serving requests. */
180 } global_score;
181
182 /* stuff which the parent generally writes and the children rarely read */
183 typedef struct {
184     pid_t pid;
185     ap_generation_t generation; /* generation of this child */
186     int worker_threads;
187 } parent_score;
188
189 typedef struct {
190     short_score servers[HARD_SERVER_LIMIT][HARD_THREAD_LIMIT];
191     parent_score parent[HARD_SERVER_LIMIT];
192     global_score global;
193 } scoreboard;
194
195 #define KEY_LENGTH 16
196 #define VALUE_LENGTH 64
197 typedef struct {
198     char key[KEY_LENGTH];
199     char value[VALUE_LENGTH];
200 } status_table_entry;
201
202 #define STATUSES_PER_CONNECTION 10
203
204 typedef struct {
205     status_table_entry
206         table[HARD_SERVER_LIMIT*HARD_THREAD_LIMIT][STATUSES_PER_CONNECTION];
207 } new_scoreboard;
208
209 #define SCOREBOARD_SIZE         sizeof(scoreboard)
210 #define NEW_SCOREBOARD_SIZE     sizeof(new_scoreboard)
211 #ifdef TPF
212 #define SCOREBOARD_NAME         "SCOREBRD"
213 #define SCOREBOARD_FRAMES               SCOREBOARD_SIZE/4095 + 1
214 #endif
215
216 AP_DECLARE(int) ap_exists_scoreboard_image(void);
217 AP_DECLARE(void) ap_create_scoreboard(apr_pool_t *p, ap_scoreboard_e t);
218 AP_DECLARE(void) ap_increment_counts(int child_num, int thread_num, request_rec *r);
219
220 apr_status_t ap_cleanup_scoreboard(void *d);
221
222 AP_DECLARE(void) reopen_scoreboard(apr_pool_t *p);
223
224 void ap_sync_scoreboard_image(void);
225
226 void update_scoreboard_global(void);
227 AP_DECLARE(int) find_child_by_pid(apr_proc_t *pid);
228 int ap_update_child_status(int child_num, int thread_num, int status, request_rec *r);
229 void ap_time_process_request(int child_num, int thread_num, int status);
230
231
232 AP_DECLARE_DATA extern scoreboard *ap_scoreboard_image;
233 AP_DECLARE_DATA extern const char *ap_scoreboard_fname;
234 AP_DECLARE_DATA extern int ap_extended_status;
235 AP_DECLARE_DATA extern apr_time_t ap_restart_time;
236
237 AP_DECLARE_DATA extern ap_generation_t volatile ap_my_generation;
238
239 /* for time_process_request() in http_main.c */
240 #define START_PREQUEST 1
241 #define STOP_PREQUEST  2
242
243 #ifdef __cplusplus
244 }
245 #endif
246
247 #endif  /* !APACHE_SCOREBOARD_H */