1 /* Copyright 2001-2005 The Apache Software Foundation or its licensors, as
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * 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.
17 #ifndef APACHE_SCOREBOARD_H
18 #define APACHE_SCOREBOARD_H
24 #ifdef HAVE_SYS_TIMES_H
26 #include <sys/times.h>
31 #include "ap_config.h"
32 #include "apr_hooks.h"
33 #include "apr_thread_proc.h"
34 #include "apr_portable.h"
36 #include "apr_optional.h"
38 /* Scoreboard file, if there is one */
39 #ifndef DEFAULT_SCOREBOARD
40 #define DEFAULT_SCOREBOARD "logs/apache_runtime_status"
43 /* Scoreboard info on a process is, for now, kept very brief ---
44 * just status value and pid (the latter so that the caretaker process
45 * can properly update the scoreboard when a process dies). We may want
46 * to eventually add a separate set of long_score structures which would
47 * give, for each process, the number of requests serviced, and info on
48 * the current, or most recent, request.
54 #define SERVER_STARTING 1 /* Server Starting up */
55 #define SERVER_READY 2 /* Waiting for connection (or accept() lock) */
56 #define SERVER_BUSY_READ 3 /* Reading a client request */
57 #define SERVER_BUSY_WRITE 4 /* Processing a client request */
58 #define SERVER_BUSY_KEEPALIVE 5 /* Waiting for more requests via keepalive */
59 #define SERVER_BUSY_LOG 6 /* Logging the request */
60 #define SERVER_BUSY_DNS 7 /* Looking up a hostname */
61 #define SERVER_CLOSING 8 /* Closing the connection */
62 #define SERVER_GRACEFUL 9 /* server is gracefully finishing request */
63 #define SERVER_IDLE_KILL 10 /* Server is cleaning up idle children. */
64 #define SERVER_NUM_STATUS 11 /* number of status settings */
66 /* Type used for generation indicies. Startup and every restart cause a
67 * new generation of children to be spawned. Children within the same
68 * generation share the same configuration information -- pointers to stuff
69 * created at config time in the parent are valid across children. However,
70 * this can't work effectively with non-forked architectures. So while the
71 * arrays in the scoreboard never change between the parent and forked
72 * children, so they do not require shm storage, the contents of the shm
73 * may contain no pointers.
75 typedef int ap_generation_t;
77 /* Is the scoreboard shared between processes or not?
78 * Set by the MPM when the scoreboard is created.
85 #define SB_WORKING 0 /* The server is busy and the child is useful. */
86 #define SB_IDLE_DIE 1 /* The server is idle and the child is superfluous. */
87 /* The child should check for this and exit gracefully. */
89 /* stuff which is worker specific */
90 /***********************WARNING***************************************/
91 /* These are things that are used by mod_status. Do not put anything */
92 /* in here that you cannot live without. This structure will not */
93 /* be available if mod_status is not loaded. */
94 /*********************************************************************/
95 typedef struct worker_score worker_score;
102 /* With some MPMs (e.g., worker), a worker_score can represent
103 * a thread in a terminating process which is no longer
104 * represented by the corresponding process_score. These MPMs
105 * should set pid and generation fields in the worker_score.
108 ap_generation_t generation;
109 unsigned char status;
110 unsigned long access_count;
111 apr_off_t bytes_served;
112 unsigned long my_access_count;
113 apr_off_t my_bytes_served;
114 apr_off_t conn_bytes;
115 unsigned short conn_count;
116 apr_time_t start_time;
117 apr_time_t stop_time;
121 apr_time_t last_used;
122 char client[32]; /* Keep 'em small... */
123 char request[64]; /* We just want an idea... */
124 char vhost[32]; /* What virtual host is being accessed? */
130 ap_scoreboard_e sb_type;
131 ap_generation_t running_generation; /* the generation of children which
132 * should still be serving requests.
134 apr_time_t restart_time;
138 /* stuff which the parent generally writes and the children rarely read */
139 typedef struct process_score process_score;
140 struct process_score{
142 ap_generation_t generation; /* generation of this child */
143 ap_scoreboard_e sb_type;
144 int quiescing; /* the process whose pid is stored above is
145 * going down gracefully
149 /* stuff which is lb specific */
150 typedef struct lb_score lb_score;
152 /* TODO: make a real stuct from this */
153 unsigned char data[1024];
156 /* Scoreboard is now in 'local' memory, since it isn't updated once created,
157 * even in forked architectures. Child created-processes (non-fork) will
158 * set up these indicies into the (possibly relocated) shmem records.
161 global_score *global;
162 process_score *parent;
163 worker_score **servers;
167 typedef struct ap_sb_handle_t ap_sb_handle_t;
169 AP_DECLARE(int) ap_exists_scoreboard_image(void);
170 AP_DECLARE(void) ap_increment_counts(ap_sb_handle_t *sbh, request_rec *r);
172 int ap_create_scoreboard(apr_pool_t *p, ap_scoreboard_e t);
173 apr_status_t ap_reopen_scoreboard(apr_pool_t *p, apr_shm_t **shm, int detached);
174 void ap_init_scoreboard(void *shared_score);
175 AP_DECLARE(int) ap_calc_scoreboard_size(void);
176 apr_status_t ap_cleanup_scoreboard(void *d);
178 AP_DECLARE(void) ap_create_sb_handle(ap_sb_handle_t **new_sbh, apr_pool_t *p,
179 int child_num, int thread_num);
181 int find_child_by_pid(apr_proc_t *pid);
182 AP_DECLARE(int) ap_update_child_status(ap_sb_handle_t *sbh, int status, request_rec *r);
183 AP_DECLARE(int) ap_update_child_status_from_indexes(int child_num, int thread_num,
184 int status, request_rec *r);
185 void ap_time_process_request(ap_sb_handle_t *sbh, int status);
187 AP_DECLARE(worker_score *) ap_get_scoreboard_worker(int x, int y);
188 AP_DECLARE(process_score *) ap_get_scoreboard_process(int x);
189 AP_DECLARE(global_score *) ap_get_scoreboard_global(void);
190 AP_DECLARE(lb_score *) ap_get_scoreboard_lb(int lb_num);
192 AP_DECLARE_DATA extern scoreboard *ap_scoreboard_image;
193 AP_DECLARE_DATA extern const char *ap_scoreboard_fname;
194 AP_DECLARE_DATA extern int ap_extended_status;
196 AP_DECLARE_DATA extern ap_generation_t volatile ap_my_generation;
200 * Hook for post scoreboard creation, pre mpm.
201 * @param p Apache pool to allocate from.
204 * @return OK or DECLINE on success; anything else is a error
206 AP_DECLARE_HOOK(int, pre_mpm, (apr_pool_t *p, ap_scoreboard_e sb_type))
209 * proxy load balancer
210 * @return the number of load balancer workers.
212 APR_DECLARE_OPTIONAL_FN(int, ap_proxy_lb_workers,
215 /* for time_process_request() in http_main.c */
216 #define START_PREQUEST 1
217 #define STOP_PREQUEST 2
223 #endif /* !APACHE_SCOREBOARD_H */