]> granicus.if.org Git - apache/blob - include/scoreboard.h
Fix windows compile break
[apache] / include / scoreboard.h
1
2 /* ====================================================================
3  * The Apache Software License, Version 1.1
4  *
5  * Copyright (c) 2000 The Apache Software Foundation.  All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
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
18  *    distribution.
19  *
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.
26  *
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.
31  *
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.
35  *
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
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
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/>.
54  *
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.
58  */
59
60 #ifndef APACHE_SCOREBOARD_H
61 #define APACHE_SCOREBOARD_H
62 #ifdef __cplusplus
63 extern "C" {
64 #endif
65
66 #ifdef HAVE_SYS_TIMES_H
67 #include <sys/time.h>
68 #include <sys/times.h>
69 #elif defined(TPF)
70 #include <time.h>
71 #endif
72
73 #include "mpm_default.h"        /* For HARD_.*_LIMIT */
74 #include "apr_thread_proc.h"
75 #include "apr_portable.h"
76
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
80 #endif
81
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.
88  *
89  * Status values:
90  */
91
92 #define SERVER_DEAD 0
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 */
104
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
109  * sent a SIGALRM.
110  *
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
116  * a request.
117  */
118 typedef unsigned vtime_t;
119
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.
126  *
127  * The safe way to access the vhost pointer is like this:
128  *
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;
132  *
133  * if (ps->generation != ap_my_generation) {
134  *     vh = NULL;
135  * }
136  *
137  * then if vh is not NULL it's valid in this child.
138  *
139  * This avoids various race conditions around restarts.
140  */
141 typedef int ap_generation_t;
142
143 /* Is the scoreboard shared between processes or not? 
144  * Set by the MPM when the scoreboard is created.
145  */
146 typedef enum {
147     SB_SHARED = 1,
148     SB_NOT_SHARED = 2
149 } ap_scoreboard_e;
150
151 /* stuff which is thread/process specific */
152 typedef struct {
153 #ifdef OPTIMIZE_TIMEOUTS
154     vtime_t cur_vtime;          /* the child's current vtime */
155     unsigned short timeout_len; /* length of the timeout */
156 #endif
157     int thread_num;
158 #if APR_HAS_THREADS
159     apr_os_thread_t tid;
160 #endif
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;
170 #ifdef HAVE_TIMES
171     struct tms times;
172 #endif
173 #ifndef OPTIMIZE_TIMEOUTS
174     time_t last_used;
175 #endif
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! */
180 } short_score;
181
182 typedef struct {
183     ap_scoreboard_e sb_type;
184     ap_generation_t running_generation; /* the generation of children which
185                                          * should still be serving requests. */
186 } global_score;
187
188 /* stuff which the parent generally writes and the children rarely read */
189 typedef struct {
190     pid_t pid;
191     ap_generation_t generation; /* generation of this child */
192     int worker_threads;
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 */
196 #endif
197 } parent_score;
198
199 typedef struct {
200     short_score servers[HARD_SERVER_LIMIT][HARD_THREAD_LIMIT];
201     parent_score parent[HARD_SERVER_LIMIT];
202     global_score global;
203 } scoreboard;
204
205 #define KEY_LENGTH 16
206 #define VALUE_LENGTH 64
207 typedef struct {
208     char key[KEY_LENGTH];
209     char value[VALUE_LENGTH];
210 } status_table_entry;
211
212 #define STATUSES_PER_CONNECTION 10
213
214 typedef struct {
215     status_table_entry
216         table[HARD_SERVER_LIMIT*HARD_THREAD_LIMIT][STATUSES_PER_CONNECTION];
217 } new_scoreboard;
218
219 #define SCOREBOARD_SIZE         sizeof(scoreboard)
220 #define NEW_SCOREBOARD_SIZE     sizeof(new_scoreboard)
221 #ifdef TPF
222 #define SCOREBOARD_NAME         "SCOREBRD"
223 #define SCOREBOARD_FRAMES               SCOREBOARD_SIZE/4095 + 1
224 #endif
225
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);
230
231 AP_DECLARE(void) reopen_scoreboard(apr_pool_t *p);
232
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);
239
240
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;
245
246 AP_DECLARE_DATA extern ap_generation_t volatile ap_my_generation;
247
248 /* for time_process_request() in http_main.c */
249 #define START_PREQUEST 1
250 #define STOP_PREQUEST  2
251
252 #ifdef __cplusplus
253 }
254 #endif
255
256 #endif  /* !APACHE_SCOREBOARD_H */