]> granicus.if.org Git - apache/blob - server/scoreboard.c
scratch an old itch - give lingering close its own state in the scoreboard.
[apache] / server / scoreboard.c
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 #include "apr.h"
60 #include "apr_strings.h"
61 #include "apr_portable.h"
62 #include "apr_lib.h"
63
64 #define APR_WANT_STRFUNC
65 #include "apr_want.h"
66
67 #if APR_HAVE_SYS_TYPES_H
68 #include <sys/types.h>
69 #endif
70
71 #include "ap_config.h"
72 #include "httpd.h"
73 #include "http_log.h"
74 #include "http_main.h"
75 #include "http_core.h"
76 #include "http_config.h"
77 #include "ap_mpm.h"
78
79 #include "mpm.h"
80 #include "scoreboard.h"
81
82 AP_DECLARE_DATA scoreboard *ap_scoreboard_image = NULL;
83 AP_DECLARE_DATA const char *ap_scoreboard_fname=NULL;
84 AP_DECLARE_DATA int ap_extended_status = 0;
85 AP_DECLARE_DATA apr_time_t ap_restart_time = 0;
86
87 #if APR_HAS_SHARED_MEMORY
88 #include "apr_shmem.h"
89 static apr_shmem_t *scoreboard_shm = NULL;
90 #endif
91 /*
92  * ToDo:
93  * This function should be renamed to cleanup_shared
94  * and it should handle cleaning up a scoreboard shared
95  * between processes using any form of IPC (file, shared memory
96  * segment, etc.). Leave it as is now because it is being used
97  * by various MPMs. 
98  */
99 static apr_status_t ap_cleanup_shared_mem(void *d)
100 {
101 #if APR_HAS_SHARED_MEMORY
102     apr_shm_free(scoreboard_shm, ap_scoreboard_image);
103     ap_scoreboard_image = NULL;
104     apr_shm_destroy(scoreboard_shm);
105 #endif
106     return APR_SUCCESS;
107 }
108
109 /* ToDo: This function should be made to handle setting up 
110  * a scoreboard shared between processes using any IPC technique, 
111  * not just a shared memory segment
112  */
113 static void setup_shared(apr_pool_t *p)
114 {
115 #if APR_HAS_SHARED_MEMORY
116     char buf[512];
117     char errmsg[120];
118     const char *fname;
119     apr_status_t rv;
120
121     fname = ap_server_root_relative(p, ap_scoreboard_fname);
122     rv = apr_shm_init(&scoreboard_shm, SCOREBOARD_SIZE, fname, p);
123     if (rv != APR_SUCCESS) {
124         apr_snprintf(buf, sizeof(buf), "%s: could not open(create) scoreboard: %s",
125                     ap_server_argv0, apr_strerror(rv, errmsg, sizeof errmsg));
126         fprintf(stderr, "%s\n", buf);
127         exit(APEXIT_INIT);
128     }
129     ap_scoreboard_image = apr_shm_malloc(scoreboard_shm, SCOREBOARD_SIZE);
130     if (ap_scoreboard_image == NULL) {
131         apr_snprintf(buf, sizeof(buf), "%s: cannot allocate scoreboard",
132                     ap_server_argv0);
133         perror(buf); /* o.k. since MM sets errno */
134         apr_shm_destroy(scoreboard_shm);
135         exit(APEXIT_INIT);
136     }
137     ap_scoreboard_image->global.running_generation = 0;
138 #endif
139 }
140
141 AP_DECLARE(void) reopen_scoreboard(apr_pool_t *p)
142 {
143 }
144
145 /* ap_cleanup_scoreboard
146  * 
147  */
148 apr_status_t ap_cleanup_scoreboard(void *d) {
149     if (ap_scoreboard_image == NULL)
150         return APR_SUCCESS;
151     if (ap_scoreboard_image->global.sb_type == SB_SHARED) {
152         ap_cleanup_shared_mem(NULL);
153     }
154     else {
155         free(ap_scoreboard_image);
156         ap_scoreboard_image = NULL;
157     }
158     return APR_SUCCESS;
159 }
160
161 /* ap_create_scoreboard(apr_pool_t*, ap_scoreboard_e t)
162  *
163  * Create or reinit an existing scoreboard. The MPM can control whether
164  * the scoreboard is shared across multiple processes or not
165  */
166 AP_DECLARE(void) ap_create_scoreboard(apr_pool_t *p, ap_scoreboard_e sb_type)
167 {
168     int running_gen = 0;
169     if (ap_scoreboard_image)
170         running_gen = ap_scoreboard_image->global.running_generation;
171     if (ap_scoreboard_image == NULL) {
172         if (sb_type == SB_SHARED) {
173             setup_shared(p);
174         }
175         else {
176             /* A simple malloc will suffice */
177             char buf[512];
178             ap_scoreboard_image = (scoreboard *) malloc(SCOREBOARD_SIZE);
179             if (ap_scoreboard_image == NULL) {
180                 apr_snprintf(buf, sizeof(buf), "%s: cannot allocate scoreboard",
181                              ap_server_argv0);
182                 perror(buf); /* o.k. since MM sets errno */
183                 exit(APEXIT_INIT);            
184             }
185         }
186     }
187     memset(ap_scoreboard_image, 0, SCOREBOARD_SIZE);
188     ap_scoreboard_image->global.sb_type = sb_type;
189     ap_scoreboard_image->global.running_generation = running_gen;
190     ap_restart_time = apr_time_now();
191     apr_pool_cleanup_register(p, NULL, ap_cleanup_scoreboard, apr_pool_cleanup_null);
192 }
193
194 /* Routines called to deal with the scoreboard image
195  * --- note that we do *not* need write locks, since update_child_status
196  * only updates a *single* record in place, and only one process writes to
197  * a given scoreboard slot at a time (either the child process owning that
198  * slot, or the parent, noting that the child has died).
199  *
200  * As a final note --- setting the score entry to getpid() is always safe,
201  * since when the parent is writing an entry, it's only noting SERVER_DEAD
202  * anyway.
203  */
204
205 void ap_sync_scoreboard_image(void)
206 {
207 }
208
209 AP_DECLARE(int) ap_exists_scoreboard_image(void)
210 {
211     return (ap_scoreboard_image ? 1 : 0);
212 }
213
214 static APR_INLINE void put_scoreboard_info(int child_num, int thread_num, 
215                                        worker_score *new_score_rec)
216 {
217     /* XXX - needs to be fixed to account for threads */
218 #ifdef SCOREBOARD_FILE
219     lseek(scoreboard_fd, (long) child_num * sizeof(worker_score), 0);
220     force_write(scoreboard_fd, new_score_rec, sizeof(worker_score));
221 #endif
222 }
223
224 void update_scoreboard_global(void)
225 {
226 #ifdef SCOREBOARD_FILE
227     lseek(scoreboard_fd,
228           (char *) &ap_scoreboard_image->global -(char *) ap_scoreboard_image, 0);
229     force_write(scoreboard_fd, &ap_scoreboard_image->global,
230                 sizeof ap_scoreboard_image->global);
231 #endif
232 }
233
234 AP_DECLARE(void) ap_increment_counts(int child_num, int thread_num, request_rec *r)
235 {
236     worker_score *ws;
237
238     ws = &ap_scoreboard_image->servers[child_num][thread_num];
239
240 #ifdef HAVE_TIMES
241     times(&ws->times);
242 #endif
243     ws->access_count++;
244     ws->my_access_count++;
245     ws->conn_count++;
246     ws->bytes_served += r->bytes_sent;
247     ws->my_bytes_served += r->bytes_sent;
248     ws->conn_bytes += r->bytes_sent;
249
250     put_scoreboard_info(child_num, thread_num, ws);
251 }
252
253 AP_DECLARE(int) find_child_by_pid(apr_proc_t *pid)
254 {
255     int i;
256     int max_daemons_limit;
257
258     ap_mpm_query(AP_MPMQ_MAX_DAEMONS, &max_daemons_limit);
259
260     for (i = 0; i < max_daemons_limit; ++i)
261         if (ap_scoreboard_image->parent[i].pid == pid->pid)
262             return i;
263
264     return -1;
265 }
266
267 int ap_update_child_status(int child_num, int thread_num, int status, request_rec *r)
268 {
269     int old_status;
270     worker_score *ws;
271     process_score *ps;
272
273     if (child_num < 0)
274         return -1;
275
276     ws = &ap_scoreboard_image->servers[child_num][thread_num];
277     old_status = ws->status;
278     ws->status = status;
279
280     ps = &ap_scoreboard_image->parent[child_num];
281     
282     if (status == SERVER_READY
283         && old_status == SERVER_STARTING) {
284         ws->thread_num = child_num * HARD_SERVER_LIMIT + thread_num;
285         ps->generation = ap_my_generation;
286         ps->worker_threads = ap_threads_per_child;
287     }
288
289     if (ap_extended_status) {
290     ws->last_used = apr_time_now();
291         if (status == SERVER_READY || status == SERVER_DEAD) {
292             /*
293              * Reset individual counters
294              */
295             if (status == SERVER_DEAD) {
296                 ws->my_access_count = 0L;
297                 ws->my_bytes_served = 0L;
298             }
299             ws->conn_count = (unsigned short) 0;
300             ws->conn_bytes = (unsigned long) 0;
301         }
302         if (r) {
303             conn_rec *c = r->connection;
304             apr_cpystrn(ws->client, ap_get_remote_host(c, r->per_dir_config,
305                                   REMOTE_NOLOOKUP, NULL), sizeof(ws->client));
306             if (r->the_request == NULL) {
307                     apr_cpystrn(ws->request, "NULL", sizeof(ws->request));
308             } else if (r->parsed_uri.password == NULL) {
309                     apr_cpystrn(ws->request, r->the_request, sizeof(ws->request));
310             } else {
311                 /* Don't reveal the password in the server-status view */
312                     apr_cpystrn(ws->request, apr_pstrcat(r->pool, r->method, " ",
313                                                apr_uri_unparse_components(r->pool, &r->parsed_uri, UNP_OMITPASSWORD),
314                                                r->assbackwards ? NULL : " ", r->protocol, NULL),
315                                        sizeof(ws->request));
316             }
317             ws->vhostrec =  r->server;
318         }
319     }
320     
321     put_scoreboard_info(child_num, thread_num, ws);
322     return old_status;
323 }
324
325 void ap_time_process_request(int child_num, int thread_num, int status)
326 {
327     worker_score *ws;
328
329     if (child_num < 0)
330         return;
331
332     ws = &ap_scoreboard_image->servers[child_num][thread_num];
333
334     if (status == START_PREQUEST) {
335         ws->start_time = apr_time_now(); 
336     }
337     else if (status == STOP_PREQUEST) {
338         ws->stop_time = apr_time_now(); 
339     }
340     put_scoreboard_info(child_num, thread_num, ws);
341 }
342
343 worker_score *ap_get_servers_scoreboard(int x, int y)
344 {
345     if (((x < 0) || (HARD_SERVER_LIMIT < x)) ||
346         ((y < 0) || (HARD_THREAD_LIMIT < y))) {
347         return(NULL); /* Out of range */
348     }
349     return(&ap_scoreboard_image->servers[x][y]);
350 }
351
352 process_score *ap_get_parent_scoreboard(int x)
353 {
354     if ((x < 0) || (HARD_SERVER_LIMIT < x)) {
355         return(NULL); /* Out of range */
356     }
357     return(&ap_scoreboard_image->parent[x]);
358 }
359