bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / server / scoreboard.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "apr.h"
18 #include "apr_strings.h"
19 #include "apr_portable.h"
20 #include "apr_lib.h"
21
22 #define APR_WANT_STRFUNC
23 #include "apr_want.h"
24
25 #if APR_HAVE_SYS_TYPES_H
26 #include <sys/types.h>
27 #endif
28
29 #include "ap_config.h"
30 #include "httpd.h"
31 #include "http_log.h"
32 #include "http_main.h"
33 #include "http_core.h"
34 #include "http_config.h"
35 #include "ap_mpm.h"
36
37 #include "mpm.h"
38 #include "scoreboard.h"
39
40 AP_DECLARE_DATA scoreboard *ap_scoreboard_image = NULL;
41 AP_DECLARE_DATA const char *ap_scoreboard_fname = NULL;
42 AP_DECLARE_DATA int ap_extended_status = 0;
43
44 #if APR_HAS_SHARED_MEMORY
45
46 #include "apr_shm.h"
47
48 #ifndef WIN32
49 static /* but must be exported to mpm_winnt */
50 #endif
51         apr_shm_t *ap_scoreboard_shm = NULL;
52
53 #endif
54
55 APR_HOOK_STRUCT(
56     APR_HOOK_LINK(pre_mpm)
57 )
58  
59 AP_IMPLEMENT_HOOK_RUN_ALL(int,pre_mpm,
60                           (apr_pool_t *p, ap_scoreboard_e sb_type),
61                           (p, sb_type),OK,DECLINED)
62
63 struct ap_sb_handle_t {
64     int child_num;
65     int thread_num;
66 };
67
68 static int server_limit, thread_limit;
69 static apr_size_t scoreboard_size;
70
71 /*
72  * ToDo:
73  * This function should be renamed to cleanup_shared
74  * and it should handle cleaning up a scoreboard shared
75  * between processes using any form of IPC (file, shared memory
76  * segment, etc.). Leave it as is now because it is being used
77  * by various MPMs. 
78  */
79 static apr_status_t ap_cleanup_shared_mem(void *d)
80 {
81 #if APR_HAS_SHARED_MEMORY
82     free(ap_scoreboard_image);
83     ap_scoreboard_image = NULL;
84     apr_shm_destroy(ap_scoreboard_shm);
85 #endif
86     return APR_SUCCESS;
87 }
88
89 AP_DECLARE(int) ap_calc_scoreboard_size(void)
90 {
91     ap_mpm_query(AP_MPMQ_HARD_LIMIT_THREADS, &thread_limit);
92     ap_mpm_query(AP_MPMQ_HARD_LIMIT_DAEMONS, &server_limit);
93     scoreboard_size = sizeof(global_score);
94     scoreboard_size += sizeof(process_score) * server_limit;
95     scoreboard_size += sizeof(worker_score) * server_limit * thread_limit;
96     return scoreboard_size;
97 }
98
99 void ap_init_scoreboard(void *shared_score)
100 {
101     char *more_storage;
102     int i;
103     
104     ap_calc_scoreboard_size();
105     ap_scoreboard_image = 
106         calloc(1, sizeof(scoreboard) + server_limit * sizeof(worker_score *));
107     more_storage = shared_score;
108     ap_scoreboard_image->global = (global_score *)more_storage;
109     more_storage += sizeof(global_score);
110     ap_scoreboard_image->parent = (process_score *)more_storage;
111     more_storage += sizeof(process_score) * server_limit;
112     ap_scoreboard_image->servers = 
113         (worker_score **)((char*)ap_scoreboard_image + sizeof(scoreboard));
114     for (i = 0; i < server_limit; i++) {
115         ap_scoreboard_image->servers[i] = (worker_score *)more_storage;
116         more_storage += thread_limit * sizeof(worker_score);
117     }
118     ap_assert(more_storage == (char*)shared_score + scoreboard_size);
119     ap_scoreboard_image->global->server_limit = server_limit;
120     ap_scoreboard_image->global->thread_limit = thread_limit;
121 }
122
123 /**
124  * Create a name-based scoreboard in the given pool using the
125  * given filename.
126  */
127 static apr_status_t create_namebased_scoreboard(apr_pool_t *pool,
128                                                 const char *fname)
129 {
130 #if APR_HAS_SHARED_MEMORY
131     apr_status_t rv;
132
133     /* The shared memory file must not exist before we create the
134      * segment. */
135     apr_file_remove(fname, pool); /* ignore errors */
136
137     rv = apr_shm_create(&ap_scoreboard_shm, scoreboard_size, fname, pool);
138     if (rv != APR_SUCCESS) {
139         ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
140                      "unable to create scoreboard \"%s\" "
141                      "(name-based shared memory failure)", fname);
142         return rv;
143     }
144 #endif /* APR_HAS_SHARED_MEMORY */
145     return APR_SUCCESS;
146 }
147
148 /* ToDo: This function should be made to handle setting up 
149  * a scoreboard shared between processes using any IPC technique, 
150  * not just a shared memory segment
151  */
152 static apr_status_t open_scoreboard(apr_pool_t *pconf)
153 {
154 #if APR_HAS_SHARED_MEMORY
155     apr_status_t rv;
156     char *fname = NULL;
157     apr_pool_t *global_pool;
158
159     /* We don't want to have to recreate the scoreboard after
160      * restarts, so we'll create a global pool and never clean it.
161      */
162     rv = apr_pool_create(&global_pool, NULL);
163     if (rv != APR_SUCCESS) {
164         ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
165                      "Fatal error: unable to create global pool "
166                      "for use with by the scoreboard");
167         return rv;
168     }
169
170     /* The config says to create a name-based shmem */
171     if (ap_scoreboard_fname) {
172         /* make sure it's an absolute pathname */
173         fname = ap_server_root_relative(pconf, ap_scoreboard_fname);
174         if (!fname) {
175             ap_log_error(APLOG_MARK, APLOG_CRIT, APR_EBADPATH, NULL,
176                          "Fatal error: Invalid Scoreboard path %s",
177                          ap_scoreboard_fname);
178             return APR_EBADPATH;
179         }
180         return create_namebased_scoreboard(global_pool, fname);
181     }
182     else { /* config didn't specify, we get to choose shmem type */
183         rv = apr_shm_create(&ap_scoreboard_shm, scoreboard_size, NULL,
184                             global_pool); /* anonymous shared memory */
185         if ((rv != APR_SUCCESS) && (rv != APR_ENOTIMPL)) {
186             ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
187                          "Unable to create scoreboard "
188                          "(anonymous shared memory failure)");
189             return rv;
190         }
191         /* Make up a filename and do name-based shmem */
192         else if (rv == APR_ENOTIMPL) {
193             /* Make sure it's an absolute pathname */
194             ap_scoreboard_fname = DEFAULT_SCOREBOARD;
195             fname = ap_server_root_relative(pconf, ap_scoreboard_fname);
196
197             return create_namebased_scoreboard(global_pool, fname);
198         }
199     }
200 #endif /* APR_HAS_SHARED_MEMORY */
201     return APR_SUCCESS;
202 }
203
204 /* If detach is non-zero, this is a seperate child process,
205  * if zero, it is a forked child.
206  */
207 apr_status_t ap_reopen_scoreboard(apr_pool_t *p, apr_shm_t **shm, int detached)
208 {
209 #if APR_HAS_SHARED_MEMORY
210     if (!detached) {
211         return APR_SUCCESS;
212     }
213     if (apr_shm_size_get(ap_scoreboard_shm) < scoreboard_size) {
214         ap_log_error(APLOG_MARK, APLOG_CRIT, 0, NULL,
215                      "Fatal error: shared scoreboard too small for child!");
216         apr_shm_detach(ap_scoreboard_shm);
217         ap_scoreboard_shm = NULL;
218         return APR_EINVAL;
219     }
220     /* everything will be cleared shortly */
221     if (*shm) {
222         *shm = ap_scoreboard_shm;
223     }
224 #endif
225     return APR_SUCCESS;
226 }
227
228 apr_status_t ap_cleanup_scoreboard(void *d)
229 {
230     if (ap_scoreboard_image == NULL) {
231         return APR_SUCCESS;
232     }
233     if (ap_scoreboard_image->global->sb_type == SB_SHARED) {
234         ap_cleanup_shared_mem(NULL);
235     }
236     else {
237         free(ap_scoreboard_image->global);
238         free(ap_scoreboard_image);
239         ap_scoreboard_image = NULL;
240     }
241     return APR_SUCCESS;
242 }
243
244 /* Create or reinit an existing scoreboard. The MPM can control whether
245  * the scoreboard is shared across multiple processes or not
246  */
247 int ap_create_scoreboard(apr_pool_t *p, ap_scoreboard_e sb_type)
248 {
249     int running_gen = 0;
250     int i;
251 #if APR_HAS_SHARED_MEMORY
252     apr_status_t rv;
253 #endif
254
255     if (ap_scoreboard_image) {
256         running_gen = ap_scoreboard_image->global->running_generation;
257         ap_scoreboard_image->global->restart_time = apr_time_now();
258         memset(ap_scoreboard_image->parent, 0, 
259                sizeof(process_score) * server_limit);
260         for (i = 0; i < server_limit; i++) {
261             memset(ap_scoreboard_image->servers[i], 0,
262                    sizeof(worker_score) * thread_limit);
263         }
264         return OK;
265     }
266
267     ap_calc_scoreboard_size();
268 #if APR_HAS_SHARED_MEMORY
269     if (sb_type == SB_SHARED) {
270         void *sb_shared;
271         rv = open_scoreboard(p);
272         if (rv || !(sb_shared = apr_shm_baseaddr_get(ap_scoreboard_shm))) {
273             return HTTP_INTERNAL_SERVER_ERROR;
274         }
275         memset(sb_shared, 0, scoreboard_size);
276         ap_init_scoreboard(sb_shared);
277     }
278     else 
279 #endif
280     {
281         /* A simple malloc will suffice */
282         void *sb_mem = calloc(1, scoreboard_size);
283         if (sb_mem == NULL) {
284             ap_log_error(APLOG_MARK, APLOG_CRIT, 0, NULL,
285                          "(%d)%s: cannot allocate scoreboard",
286                          errno, strerror(errno));
287             return HTTP_INTERNAL_SERVER_ERROR;
288         }
289         ap_init_scoreboard(sb_mem);
290     }
291
292     ap_scoreboard_image->global->sb_type = sb_type;
293     ap_scoreboard_image->global->running_generation = running_gen;
294     ap_scoreboard_image->global->restart_time = apr_time_now();
295
296     apr_pool_cleanup_register(p, NULL, ap_cleanup_scoreboard, apr_pool_cleanup_null);
297
298     return OK;
299 }
300
301 /* Routines called to deal with the scoreboard image
302  * --- note that we do *not* need write locks, since update_child_status
303  * only updates a *single* record in place, and only one process writes to
304  * a given scoreboard slot at a time (either the child process owning that
305  * slot, or the parent, noting that the child has died).
306  *
307  * As a final note --- setting the score entry to getpid() is always safe,
308  * since when the parent is writing an entry, it's only noting SERVER_DEAD
309  * anyway.
310  */
311
312 AP_DECLARE(int) ap_exists_scoreboard_image(void)
313 {
314     return (ap_scoreboard_image ? 1 : 0);
315 }
316
317 AP_DECLARE(void) ap_increment_counts(ap_sb_handle_t *sb, request_rec *r)
318 {
319     worker_score *ws;
320
321     ws = &ap_scoreboard_image->servers[sb->child_num][sb->thread_num];
322
323 #ifdef HAVE_TIMES
324     times(&ws->times);
325 #endif
326     ws->access_count++;
327     ws->my_access_count++;
328     ws->conn_count++;
329     ws->bytes_served += r->bytes_sent;
330     ws->my_bytes_served += r->bytes_sent;
331     ws->conn_bytes += r->bytes_sent;
332 }
333
334 AP_DECLARE(int) find_child_by_pid(apr_proc_t *pid)
335 {
336     int i;
337     int max_daemons_limit;
338
339     ap_mpm_query(AP_MPMQ_MAX_DAEMONS, &max_daemons_limit);
340
341     for (i = 0; i < max_daemons_limit; ++i) {
342         if (ap_scoreboard_image->parent[i].pid == pid->pid) {
343             return i;
344         }
345     }
346
347     return -1;
348 }
349
350 AP_DECLARE(void) ap_create_sb_handle(ap_sb_handle_t **new_sbh, apr_pool_t *p,
351                                      int child_num, int thread_num)
352 {
353     *new_sbh = (ap_sb_handle_t *)apr_palloc(p, sizeof(ap_sb_handle_t));
354     (*new_sbh)->child_num = child_num;
355     (*new_sbh)->thread_num = thread_num;
356 }
357
358 AP_DECLARE(int) ap_update_child_status_from_indexes(int child_num,
359                                                     int thread_num,
360                                                     int status,
361                                                     request_rec *r)
362 {
363     int old_status;
364     worker_score *ws;
365     process_score *ps;
366
367     if (child_num < 0) {
368         return -1;
369     }
370
371     ws = &ap_scoreboard_image->servers[child_num][thread_num];
372     old_status = ws->status;
373     ws->status = status;
374
375     ps = &ap_scoreboard_image->parent[child_num];
376     
377     if (status == SERVER_READY
378         && old_status == SERVER_STARTING) {
379         ws->thread_num = child_num * thread_limit + thread_num;
380         ps->generation = ap_my_generation;
381     }
382
383     if (ap_extended_status) {
384         ws->last_used = apr_time_now();
385         if (status == SERVER_READY || status == SERVER_DEAD) {
386             /*
387              * Reset individual counters
388              */
389             if (status == SERVER_DEAD) {
390                 ws->my_access_count = 0L;
391                 ws->my_bytes_served = 0L;
392             }
393             ws->conn_count = 0;
394             ws->conn_bytes = 0;
395         }
396         if (r) {
397             conn_rec *c = r->connection;
398             apr_cpystrn(ws->client, ap_get_remote_host(c, r->per_dir_config,
399                         REMOTE_NOLOOKUP, NULL), sizeof(ws->client));
400             if (r->the_request == NULL) {
401                 apr_cpystrn(ws->request, "NULL", sizeof(ws->request));
402             } else if (r->parsed_uri.password == NULL) {
403                 apr_cpystrn(ws->request, r->the_request, sizeof(ws->request));
404             } else {
405                 /* Don't reveal the password in the server-status view */
406                 apr_cpystrn(ws->request, apr_pstrcat(r->pool, r->method, " ",
407                             apr_uri_unparse(r->pool, &r->parsed_uri,
408                             APR_URI_UNP_OMITPASSWORD),
409                             r->assbackwards ? NULL : " ", r->protocol, NULL),
410                             sizeof(ws->request));
411             }
412             apr_cpystrn(ws->vhost, r->server->server_hostname,
413                         sizeof(ws->vhost));
414         }
415     }
416     
417     return old_status;
418 }
419
420 AP_DECLARE(int) ap_update_child_status(ap_sb_handle_t *sbh, int status,
421                                       request_rec *r)
422 {
423     return ap_update_child_status_from_indexes(sbh->child_num, sbh->thread_num,
424                                                status, r);
425 }
426
427 void ap_time_process_request(ap_sb_handle_t *sbh, int status)
428 {
429     worker_score *ws;
430
431     if (sbh->child_num < 0) {
432         return;
433     }
434
435     ws = &ap_scoreboard_image->servers[sbh->child_num][sbh->thread_num];
436
437     if (status == START_PREQUEST) {
438         ws->start_time = apr_time_now(); 
439     }
440     else if (status == STOP_PREQUEST) {
441         ws->stop_time = apr_time_now(); 
442     }
443 }
444
445 AP_DECLARE(worker_score *) ap_get_scoreboard_worker(int x, int y)
446 {
447     if (((x < 0) || (server_limit < x)) ||
448         ((y < 0) || (thread_limit < y))) {
449         return(NULL); /* Out of range */
450     }
451     return &ap_scoreboard_image->servers[x][y];
452 }
453
454 AP_DECLARE(process_score *) ap_get_scoreboard_process(int x)
455 {
456     if ((x < 0) || (server_limit < x)) {
457         return(NULL); /* Out of range */
458     }
459     return &ap_scoreboard_image->parent[x];
460 }
461
462 AP_DECLARE(global_score *) ap_get_scoreboard_global()
463 {
464     return ap_scoreboard_image->global;
465 }