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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @brief Apache scoreboard library
22 #ifndef APACHE_SCOREBOARD_H
23 #define APACHE_SCOREBOARD_H
29 #ifdef HAVE_SYS_TIMES_H
31 #include <sys/times.h>
34 #include "ap_config.h"
35 #include "apr_hooks.h"
36 #include "apr_thread_proc.h"
37 #include "apr_portable.h"
39 #include "apr_optional.h"
41 /* Scoreboard file, if there is one */
42 #ifndef DEFAULT_SCOREBOARD
43 #define DEFAULT_SCOREBOARD "logs/apache_runtime_status"
46 /* Scoreboard info on a process is, for now, kept very brief ---
47 * just status value and pid (the latter so that the caretaker process
48 * can properly update the scoreboard when a process dies). We may want
49 * to eventually add a separate set of long_score structures which would
50 * give, for each process, the number of requests serviced, and info on
51 * the current, or most recent, request.
57 #define SERVER_STARTING 1 /* Server Starting up */
58 #define SERVER_READY 2 /* Waiting for connection (or accept() lock) */
59 #define SERVER_BUSY_READ 3 /* Reading a client request */
60 #define SERVER_BUSY_WRITE 4 /* Processing a client request */
61 #define SERVER_BUSY_KEEPALIVE 5 /* Waiting for more requests via keepalive */
62 #define SERVER_BUSY_LOG 6 /* Logging the request */
63 #define SERVER_BUSY_DNS 7 /* Looking up a hostname */
64 #define SERVER_CLOSING 8 /* Closing the connection */
65 #define SERVER_GRACEFUL 9 /* server is gracefully finishing request */
66 #define SERVER_IDLE_KILL 10 /* Server is cleaning up idle children. */
67 #define SERVER_NUM_STATUS 11 /* number of status settings */
69 /* Type used for generation indicies. Startup and every restart cause a
70 * new generation of children to be spawned. Children within the same
71 * generation share the same configuration information -- pointers to stuff
72 * created at config time in the parent are valid across children. However,
73 * this can't work effectively with non-forked architectures. So while the
74 * arrays in the scoreboard never change between the parent and forked
75 * children, so they do not require shm storage, the contents of the shm
76 * may contain no pointers.
78 typedef int ap_generation_t;
80 /* Is the scoreboard shared between processes or not?
81 * Set by the MPM when the scoreboard is created.
88 /* stuff which is worker specific */
89 typedef struct worker_score worker_score;
95 /* With some MPMs (e.g., worker), a worker_score can represent
96 * a thread in a terminating process which is no longer
97 * represented by the corresponding process_score. These MPMs
98 * should set pid and generation fields in the worker_score.
101 ap_generation_t generation;
102 unsigned char status;
103 unsigned long access_count;
104 apr_off_t bytes_served;
105 unsigned long my_access_count;
106 apr_off_t my_bytes_served;
107 apr_off_t conn_bytes;
108 unsigned short conn_count;
109 apr_time_t start_time;
110 apr_time_t stop_time;
114 apr_time_t last_used;
115 char client[32]; /* Keep 'em small... */
116 char request[64]; /* We just want an idea... */
117 char vhost[32]; /* What virtual host is being accessed? */
123 ap_scoreboard_e sb_type;
124 ap_generation_t running_generation; /* the generation of children which
125 * should still be serving requests.
127 apr_time_t restart_time;
131 /* stuff which the parent generally writes and the children rarely read */
132 typedef struct process_score process_score;
133 struct process_score {
135 ap_generation_t generation; /* generation of this child */
136 int quiescing; /* the process whose pid is stored above is
137 * going down gracefully
141 /* stuff which is lb specific */
142 typedef struct lb_score lb_score;
144 unsigned char data[1024];
147 /* Scoreboard is now in 'local' memory, since it isn't updated once created,
148 * even in forked architectures. Child created-processes (non-fork) will
149 * set up these indicies into the (possibly relocated) shmem records.
152 global_score *global;
153 process_score *parent;
154 worker_score **servers;
158 typedef struct ap_sb_handle_t ap_sb_handle_t;
160 AP_DECLARE(int) ap_exists_scoreboard_image(void);
161 AP_DECLARE(void) ap_increment_counts(ap_sb_handle_t *sbh, request_rec *r);
163 int ap_create_scoreboard(apr_pool_t *p, ap_scoreboard_e t);
164 apr_status_t ap_reopen_scoreboard(apr_pool_t *p, apr_shm_t **shm, int detached);
165 void ap_init_scoreboard(void *shared_score);
166 AP_DECLARE(int) ap_calc_scoreboard_size(void);
167 apr_status_t ap_cleanup_scoreboard(void *d);
169 AP_DECLARE(void) ap_create_sb_handle(ap_sb_handle_t **new_sbh, apr_pool_t *p,
170 int child_num, int thread_num);
172 AP_DECLARE(int) ap_find_child_by_pid(apr_proc_t *pid);
173 AP_DECLARE(int) ap_update_child_status(ap_sb_handle_t *sbh, int status, request_rec *r);
174 AP_DECLARE(int) ap_update_child_status_from_indexes(int child_num, int thread_num,
175 int status, request_rec *r);
176 AP_DECLARE(void) ap_time_process_request(ap_sb_handle_t *sbh, int status);
178 AP_DECLARE(worker_score *) ap_get_scoreboard_worker(ap_sb_handle_t *sbh);
179 AP_DECLARE(worker_score *) ap_get_scoreboard_worker_from_indexes(int child_num,
181 AP_DECLARE(process_score *) ap_get_scoreboard_process(int x);
182 AP_DECLARE(global_score *) ap_get_scoreboard_global(void);
183 AP_DECLARE(lb_score *) ap_get_scoreboard_lb(int lb_num);
185 AP_DECLARE_DATA extern scoreboard *ap_scoreboard_image;
186 AP_DECLARE_DATA extern const char *ap_scoreboard_fname;
187 AP_DECLARE_DATA extern int ap_extended_status;
188 AP_DECLARE_DATA extern int ap_mod_status_reqtail;
190 AP_DECLARE_DATA extern ap_generation_t volatile ap_my_generation;
194 * Hook for post scoreboard creation, pre mpm.
195 * @param p Apache pool to allocate from.
198 * @return OK or DECLINE on success; anything else is a error
200 AP_DECLARE_HOOK(int, pre_mpm, (apr_pool_t *p, ap_scoreboard_e sb_type))
203 * proxy load balancer
204 * @return the number of load balancer workers.
206 APR_DECLARE_OPTIONAL_FN(int, ap_proxy_lb_workers,
209 * proxy load balancer
210 * @return the size of lb_workers.
212 APR_DECLARE_OPTIONAL_FN(int, ap_proxy_lb_worker_size,
215 /* for time_process_request() in http_main.c */
216 #define START_PREQUEST 1
217 #define STOP_PREQUEST 2
223 #endif /* !APACHE_SCOREBOARD_H */