]> granicus.if.org Git - apache/blob - server/scoreboard.c
Merge r1303201, r1303435, r1303827:
[apache] / 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 "scoreboard.h"
38
39 /* we know core's module_index is 0 */
40 #undef APLOG_MODULE_INDEX
41 #define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX
42
43 AP_DECLARE_DATA scoreboard *ap_scoreboard_image = NULL;
44 AP_DECLARE_DATA const char *ap_scoreboard_fname = NULL;
45 static ap_scoreboard_e scoreboard_type;
46
47 const char * ap_set_scoreboard(cmd_parms *cmd, void *dummy,
48                                const char *arg)
49 {
50     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
51     if (err != NULL) {
52         return err;
53     }
54
55     ap_scoreboard_fname = arg;
56     return NULL;
57 }
58
59 /* Default to false when mod_status is not loaded */
60 AP_DECLARE_DATA int ap_extended_status = 0;
61
62 const char *ap_set_extended_status(cmd_parms *cmd, void *dummy, int arg)
63 {
64     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
65     if (err != NULL) {
66         return err;
67     }
68     ap_extended_status = arg;
69     return NULL;
70 }
71
72 AP_DECLARE_DATA int ap_mod_status_reqtail = 0;
73
74 const char *ap_set_reqtail(cmd_parms *cmd, void *dummy, int arg)
75 {
76     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
77     if (err != NULL) {
78         return err;
79     }
80     ap_mod_status_reqtail = arg;
81     return NULL;
82 }
83
84 #if APR_HAS_SHARED_MEMORY
85
86 #include "apr_shm.h"
87
88 #ifndef WIN32
89 static /* but must be exported to mpm_winnt */
90 #endif
91         apr_shm_t *ap_scoreboard_shm = NULL;
92
93 #endif
94
95 APR_HOOK_STRUCT(
96     APR_HOOK_LINK(pre_mpm)
97 )
98
99 AP_IMPLEMENT_HOOK_RUN_ALL(int,pre_mpm,
100                           (apr_pool_t *p, ap_scoreboard_e sb_type),
101                           (p, sb_type),OK,DECLINED)
102
103 static APR_OPTIONAL_FN_TYPE(ap_logio_get_last_bytes)
104                                 *pfn_ap_logio_get_last_bytes;
105
106 struct ap_sb_handle_t {
107     int child_num;
108     int thread_num;
109 };
110
111 static int server_limit, thread_limit;
112 static apr_size_t scoreboard_size;
113
114 /*
115  * ToDo:
116  * This function should be renamed to cleanup_shared
117  * and it should handle cleaning up a scoreboard shared
118  * between processes using any form of IPC (file, shared memory
119  * segment, etc.). Leave it as is now because it is being used
120  * by various MPMs.
121  */
122 static apr_status_t ap_cleanup_shared_mem(void *d)
123 {
124 #if APR_HAS_SHARED_MEMORY
125     free(ap_scoreboard_image);
126     ap_scoreboard_image = NULL;
127     apr_shm_destroy(ap_scoreboard_shm);
128 #endif
129     return APR_SUCCESS;
130 }
131
132 AP_DECLARE(int) ap_calc_scoreboard_size(void)
133 {
134     ap_mpm_query(AP_MPMQ_HARD_LIMIT_THREADS, &thread_limit);
135     ap_mpm_query(AP_MPMQ_HARD_LIMIT_DAEMONS, &server_limit);
136
137     scoreboard_size = sizeof(global_score);
138     scoreboard_size += sizeof(process_score) * server_limit;
139     scoreboard_size += sizeof(worker_score) * server_limit * thread_limit;
140
141     pfn_ap_logio_get_last_bytes = APR_RETRIEVE_OPTIONAL_FN(ap_logio_get_last_bytes);
142
143     return scoreboard_size;
144 }
145
146 AP_DECLARE(void) ap_init_scoreboard(void *shared_score)
147 {
148     char *more_storage;
149     int i;
150
151     ap_calc_scoreboard_size();
152     ap_scoreboard_image =
153         ap_calloc(1, sizeof(scoreboard) + server_limit * sizeof(worker_score *));
154     more_storage = shared_score;
155     ap_scoreboard_image->global = (global_score *)more_storage;
156     more_storage += sizeof(global_score);
157     ap_scoreboard_image->parent = (process_score *)more_storage;
158     more_storage += sizeof(process_score) * server_limit;
159     ap_scoreboard_image->servers =
160         (worker_score **)((char*)ap_scoreboard_image + sizeof(scoreboard));
161     for (i = 0; i < server_limit; i++) {
162         ap_scoreboard_image->servers[i] = (worker_score *)more_storage;
163         more_storage += thread_limit * sizeof(worker_score);
164     }
165     ap_assert(more_storage == (char*)shared_score + scoreboard_size);
166     ap_scoreboard_image->global->server_limit = server_limit;
167     ap_scoreboard_image->global->thread_limit = thread_limit;
168 }
169
170 /**
171  * Create a name-based scoreboard in the given pool using the
172  * given filename.
173  */
174 static apr_status_t create_namebased_scoreboard(apr_pool_t *pool,
175                                                 const char *fname)
176 {
177 #if APR_HAS_SHARED_MEMORY
178     apr_status_t rv;
179
180     /* The shared memory file must not exist before we create the
181      * segment. */
182     apr_shm_remove(fname, pool); /* ignore errors */
183
184     rv = apr_shm_create(&ap_scoreboard_shm, scoreboard_size, fname, pool);
185     if (rv != APR_SUCCESS) {
186         ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf, APLOGNO(00001)
187                      "unable to create or access scoreboard \"%s\" "
188                      "(name-based shared memory failure)", fname);
189         return rv;
190     }
191 #endif /* APR_HAS_SHARED_MEMORY */
192     return APR_SUCCESS;
193 }
194
195 /* ToDo: This function should be made to handle setting up
196  * a scoreboard shared between processes using any IPC technique,
197  * not just a shared memory segment
198  */
199 static apr_status_t open_scoreboard(apr_pool_t *pconf)
200 {
201 #if APR_HAS_SHARED_MEMORY
202     apr_status_t rv;
203     char *fname = NULL;
204     apr_pool_t *global_pool;
205
206     /* We don't want to have to recreate the scoreboard after
207      * restarts, so we'll create a global pool and never clean it.
208      */
209     rv = apr_pool_create(&global_pool, NULL);
210     if (rv != APR_SUCCESS) {
211         ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf, APLOGNO(00002)
212                      "Fatal error: unable to create global pool "
213                      "for use by the scoreboard");
214         return rv;
215     }
216
217     /* The config says to create a name-based shmem */
218     if (ap_scoreboard_fname) {
219         /* make sure it's an absolute pathname */
220         fname = ap_server_root_relative(pconf, ap_scoreboard_fname);
221         if (!fname) {
222             ap_log_error(APLOG_MARK, APLOG_CRIT, APR_EBADPATH, ap_server_conf, APLOGNO(00003)
223                          "Fatal error: Invalid Scoreboard path %s",
224                          ap_scoreboard_fname);
225             return APR_EBADPATH;
226         }
227         return create_namebased_scoreboard(global_pool, fname);
228     }
229     else { /* config didn't specify, we get to choose shmem type */
230         rv = apr_shm_create(&ap_scoreboard_shm, scoreboard_size, NULL,
231                             global_pool); /* anonymous shared memory */
232         if ((rv != APR_SUCCESS) && (rv != APR_ENOTIMPL)) {
233             ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf, APLOGNO(00004)
234                          "Unable to create or access scoreboard "
235                          "(anonymous shared memory failure)");
236             return rv;
237         }
238         /* Make up a filename and do name-based shmem */
239         else if (rv == APR_ENOTIMPL) {
240             /* Make sure it's an absolute pathname */
241             ap_scoreboard_fname = DEFAULT_SCOREBOARD;
242             fname = ap_server_root_relative(pconf, ap_scoreboard_fname);
243
244             return create_namebased_scoreboard(global_pool, fname);
245         }
246     }
247 #endif /* APR_HAS_SHARED_MEMORY */
248     return APR_SUCCESS;
249 }
250
251 /* If detach is non-zero, this is a separate child process,
252  * if zero, it is a forked child.
253  */
254 AP_DECLARE(apr_status_t) ap_reopen_scoreboard(apr_pool_t *p, apr_shm_t **shm,
255                                               int detached)
256 {
257 #if APR_HAS_SHARED_MEMORY
258     if (!detached) {
259         return APR_SUCCESS;
260     }
261     if (apr_shm_size_get(ap_scoreboard_shm) < scoreboard_size) {
262         ap_log_error(APLOG_MARK, APLOG_CRIT, 0, ap_server_conf, APLOGNO(00005)
263                      "Fatal error: shared scoreboard too small for child!");
264         apr_shm_detach(ap_scoreboard_shm);
265         ap_scoreboard_shm = NULL;
266         return APR_EINVAL;
267     }
268     /* everything will be cleared shortly */
269     if (*shm) {
270         *shm = ap_scoreboard_shm;
271     }
272 #endif
273     return APR_SUCCESS;
274 }
275
276 apr_status_t ap_cleanup_scoreboard(void *d)
277 {
278     if (ap_scoreboard_image == NULL) {
279         return APR_SUCCESS;
280     }
281     if (scoreboard_type == SB_SHARED) {
282         ap_cleanup_shared_mem(NULL);
283     }
284     else {
285         free(ap_scoreboard_image->global);
286         free(ap_scoreboard_image);
287         ap_scoreboard_image = NULL;
288     }
289     return APR_SUCCESS;
290 }
291
292 /* Create or reinit an existing scoreboard. The MPM can control whether
293  * the scoreboard is shared across multiple processes or not
294  */
295 int ap_create_scoreboard(apr_pool_t *p, ap_scoreboard_e sb_type)
296 {
297     int i;
298 #if APR_HAS_SHARED_MEMORY
299     apr_status_t rv;
300 #endif
301
302     pfn_ap_logio_get_last_bytes = APR_RETRIEVE_OPTIONAL_FN(ap_logio_get_last_bytes);
303
304     if (ap_scoreboard_image) {
305         ap_scoreboard_image->global->restart_time = apr_time_now();
306         memset(ap_scoreboard_image->parent, 0,
307                sizeof(process_score) * server_limit);
308         for (i = 0; i < server_limit; i++) {
309             memset(ap_scoreboard_image->servers[i], 0,
310                    sizeof(worker_score) * thread_limit);
311         }
312         return OK;
313     }
314
315     ap_calc_scoreboard_size();
316 #if APR_HAS_SHARED_MEMORY
317     if (sb_type == SB_SHARED) {
318         void *sb_shared;
319         rv = open_scoreboard(p);
320         if (rv || !(sb_shared = apr_shm_baseaddr_get(ap_scoreboard_shm))) {
321             return HTTP_INTERNAL_SERVER_ERROR;
322         }
323         memset(sb_shared, 0, scoreboard_size);
324         ap_init_scoreboard(sb_shared);
325     }
326     else
327 #endif
328     {
329         /* A simple malloc will suffice */
330         void *sb_mem = ap_calloc(1, scoreboard_size);
331         ap_init_scoreboard(sb_mem);
332     }
333
334     scoreboard_type = sb_type;
335     ap_scoreboard_image->global->running_generation = 0;
336     ap_scoreboard_image->global->restart_time = apr_time_now();
337
338     apr_pool_cleanup_register(p, NULL, ap_cleanup_scoreboard, apr_pool_cleanup_null);
339
340     return OK;
341 }
342
343 /* Routines called to deal with the scoreboard image
344  * --- note that we do *not* need write locks, since update_child_status
345  * only updates a *single* record in place, and only one process writes to
346  * a given scoreboard slot at a time (either the child process owning that
347  * slot, or the parent, noting that the child has died).
348  *
349  * As a final note --- setting the score entry to getpid() is always safe,
350  * since when the parent is writing an entry, it's only noting SERVER_DEAD
351  * anyway.
352  */
353
354 AP_DECLARE(int) ap_exists_scoreboard_image(void)
355 {
356     return (ap_scoreboard_image ? 1 : 0);
357 }
358
359 AP_DECLARE(void) ap_increment_counts(ap_sb_handle_t *sb, request_rec *r)
360 {
361     worker_score *ws;
362     apr_off_t bytes;
363
364     if (!sb)
365         return;
366
367     ws = &ap_scoreboard_image->servers[sb->child_num][sb->thread_num];
368     if (pfn_ap_logio_get_last_bytes != NULL) {
369         bytes = pfn_ap_logio_get_last_bytes(r->connection);
370     }
371     else if (r->method_number == M_GET && r->method[0] == 'H') {
372         bytes = 0;
373     }
374     else {
375         bytes = r->bytes_sent;
376     }
377
378 #ifdef HAVE_TIMES
379     times(&ws->times);
380 #endif
381     ws->access_count++;
382     ws->my_access_count++;
383     ws->conn_count++;
384     ws->bytes_served += bytes;
385     ws->my_bytes_served += bytes;
386     ws->conn_bytes += bytes;
387 }
388
389 AP_DECLARE(int) ap_find_child_by_pid(apr_proc_t *pid)
390 {
391     int i;
392     int max_daemons_limit;
393
394     ap_mpm_query(AP_MPMQ_MAX_DAEMONS, &max_daemons_limit);
395
396     for (i = 0; i < max_daemons_limit; ++i) {
397         if (ap_scoreboard_image->parent[i].pid == pid->pid) {
398             return i;
399         }
400     }
401
402     return -1;
403 }
404
405 AP_DECLARE(void) ap_create_sb_handle(ap_sb_handle_t **new_sbh, apr_pool_t *p,
406                                      int child_num, int thread_num)
407 {
408     *new_sbh = (ap_sb_handle_t *)apr_palloc(p, sizeof(ap_sb_handle_t));
409     (*new_sbh)->child_num = child_num;
410     (*new_sbh)->thread_num = thread_num;
411 }
412
413 static void copy_request(char *rbuf, apr_size_t rbuflen, request_rec *r)
414 {
415     char *p;
416
417     if (r->the_request == NULL) {
418         apr_cpystrn(rbuf, "NULL", rbuflen);
419         return; /* short circuit below */
420     }
421
422     if (r->parsed_uri.password == NULL) {
423         p = r->the_request;
424     }
425     else {
426         /* Don't reveal the password in the server-status view */
427         p = apr_pstrcat(r->pool, r->method, " ",
428                         apr_uri_unparse(r->pool, &r->parsed_uri,
429                         APR_URI_UNP_OMITPASSWORD),
430                         r->assbackwards ? NULL : " ", r->protocol, NULL);
431     }
432
433     /* now figure out if we copy over the 1st rbuflen chars or the last */
434     if (!ap_mod_status_reqtail) {
435         apr_cpystrn(rbuf, p, rbuflen);
436     }
437     else {
438         apr_size_t slen = strlen(p);
439         if (slen < rbuflen) {
440             /* it all fits anyway */
441             apr_cpystrn(rbuf, p, rbuflen);
442         }
443         else {
444             apr_cpystrn(rbuf, p+(slen-rbuflen+1), rbuflen);
445         }
446     }
447 }
448
449 static int update_child_status_internal(int child_num,
450                                         int thread_num,
451                                         int status,
452                                         conn_rec *c,
453                                         request_rec *r)
454 {
455     int old_status;
456     worker_score *ws;
457     process_score *ps;
458     int mpm_generation;
459
460     ws = &ap_scoreboard_image->servers[child_num][thread_num];
461     old_status = ws->status;
462     ws->status = status;
463
464     ps = &ap_scoreboard_image->parent[child_num];
465
466     if (status == SERVER_READY
467         && old_status == SERVER_STARTING) {
468         ws->thread_num = child_num * thread_limit + thread_num;
469         ap_mpm_query(AP_MPMQ_GENERATION, &mpm_generation);
470         ps->generation = mpm_generation;
471     }
472
473     if (ap_extended_status) {
474         ws->last_used = apr_time_now();
475         if (status == SERVER_READY || status == SERVER_DEAD) {
476             /*
477              * Reset individual counters
478              */
479             if (status == SERVER_DEAD) {
480                 ws->my_access_count = 0L;
481                 ws->my_bytes_served = 0L;
482             }
483             ws->conn_count = 0;
484             ws->conn_bytes = 0;
485         }
486         if (r) {
487             apr_cpystrn(ws->client, ap_get_remote_host(c, r->per_dir_config,
488                         REMOTE_NOLOOKUP, NULL), sizeof(ws->client));
489             copy_request(ws->request, sizeof(ws->request), r);
490             if (r->server) {
491                 apr_cpystrn(ws->vhost, r->server->server_hostname,
492                             sizeof(ws->vhost));
493             }
494         }
495         else if (c) {
496             apr_cpystrn(ws->client, ap_get_remote_host(c, NULL,
497                         REMOTE_NOLOOKUP, NULL), sizeof(ws->client));
498             ws->request[0]='\0';
499             ws->vhost[0]='\0';
500         }
501     }
502
503     return old_status;
504 }
505
506 AP_DECLARE(int) ap_update_child_status_from_indexes(int child_num,
507                                                     int thread_num,
508                                                     int status,
509                                                     request_rec *r)
510 {
511     if (child_num < 0) {
512         return -1;
513     }
514
515     return update_child_status_internal(child_num, thread_num, status,
516                                         r ? r->connection : NULL,
517                                         r);
518 }
519
520 AP_DECLARE(int) ap_update_child_status(ap_sb_handle_t *sbh, int status,
521                                       request_rec *r)
522 {
523     if (!sbh)
524         return -1;
525
526     return update_child_status_internal(sbh->child_num, sbh->thread_num,
527                                         status,
528                                         r ? r->connection : NULL,
529                                         r);
530 }
531
532 AP_DECLARE(int) ap_update_child_status_from_conn(ap_sb_handle_t *sbh, int status,
533                                        conn_rec *c)
534 {
535     if (!sbh)
536         return -1;
537
538     return update_child_status_internal(sbh->child_num, sbh->thread_num,
539                                         status, c, NULL);
540 }
541
542 AP_DECLARE(void) ap_time_process_request(ap_sb_handle_t *sbh, int status)
543 {
544     worker_score *ws;
545
546     if (!sbh)
547         return;
548
549     if (sbh->child_num < 0) {
550         return;
551     }
552
553     ws = &ap_scoreboard_image->servers[sbh->child_num][sbh->thread_num];
554
555     if (status == START_PREQUEST) {
556         ws->start_time = apr_time_now();
557     }
558     else if (status == STOP_PREQUEST) {
559         ws->stop_time = apr_time_now();
560     }
561 }
562
563 AP_DECLARE(worker_score *) ap_get_scoreboard_worker_from_indexes(int x, int y)
564 {
565     if (((x < 0) || (x >= server_limit)) ||
566         ((y < 0) || (y >= thread_limit))) {
567         return(NULL); /* Out of range */
568     }
569     return &ap_scoreboard_image->servers[x][y];
570 }
571
572 AP_DECLARE(worker_score *) ap_get_scoreboard_worker(ap_sb_handle_t *sbh)
573 {
574     if (!sbh)
575         return NULL;
576
577     return ap_get_scoreboard_worker_from_indexes(sbh->child_num,
578                                                  sbh->thread_num);
579 }
580
581 AP_DECLARE(process_score *) ap_get_scoreboard_process(int x)
582 {
583     if ((x < 0) || (x >= server_limit)) {
584         return(NULL); /* Out of range */
585     }
586     return &ap_scoreboard_image->parent[x];
587 }
588
589 AP_DECLARE(global_score *) ap_get_scoreboard_global()
590 {
591     return ap_scoreboard_image->global;
592 }