]> granicus.if.org Git - apache/blob - include/scoreboard.h
Keep track of time taken to process requests again.
[apache] / include / scoreboard.h
1 /* Copyright 2001-2004 The Apache Software Foundation
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #ifndef APACHE_SCOREBOARD_H
17 #define APACHE_SCOREBOARD_H
18
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22
23 #ifdef HAVE_SYS_TIMES_H
24 #include <sys/time.h>
25 #include <sys/times.h>
26 #elif defined(TPF)
27 #include <time.h>
28 #endif
29
30 #include "ap_config.h"
31 #include "apr_hooks.h"
32 #include "apr_thread_proc.h"
33 #include "apr_portable.h"
34 #include "apr_shm.h"
35 #include "apr_optional.h"
36
37 /* Scoreboard file, if there is one */
38 #ifndef DEFAULT_SCOREBOARD
39 #define DEFAULT_SCOREBOARD "logs/apache_runtime_status"
40 #endif
41
42 /* Scoreboard info on a process is, for now, kept very brief --- 
43  * just status value and pid (the latter so that the caretaker process
44  * can properly update the scoreboard when a process dies).  We may want
45  * to eventually add a separate set of long_score structures which would
46  * give, for each process, the number of requests serviced, and info on
47  * the current, or most recent, request.
48  *
49  * Status values:
50  */
51
52 #define SERVER_DEAD 0
53 #define SERVER_STARTING 1       /* Server Starting up */
54 #define SERVER_READY 2          /* Waiting for connection (or accept() lock) */
55 #define SERVER_BUSY_READ 3      /* Reading a client request */
56 #define SERVER_BUSY_WRITE 4     /* Processing a client request */
57 #define SERVER_BUSY_KEEPALIVE 5 /* Waiting for more requests via keepalive */
58 #define SERVER_BUSY_LOG 6       /* Logging the request */
59 #define SERVER_BUSY_DNS 7       /* Looking up a hostname */
60 #define SERVER_CLOSING 8        /* Closing the connection */
61 #define SERVER_GRACEFUL 9       /* server is gracefully finishing request */
62 #define SERVER_IDLE_KILL 10     /* Server is cleaning up idle children. */
63 #define SERVER_NUM_STATUS 11    /* number of status settings */
64
65 /* Type used for generation indicies.  Startup and every restart cause a
66  * new generation of children to be spawned.  Children within the same
67  * generation share the same configuration information -- pointers to stuff
68  * created at config time in the parent are valid across children.  However,
69  * this can't work effectively with non-forked architectures.  So while the
70  * arrays in the scoreboard never change between the parent and forked
71  * children, so they do not require shm storage, the contents of the shm
72  * may contain no pointers.
73  */
74 typedef int ap_generation_t;
75
76 /* Is the scoreboard shared between processes or not? 
77  * Set by the MPM when the scoreboard is created.
78  */
79 typedef enum {
80     SB_NOT_SHARED = 1,
81     SB_SHARED = 2
82 } ap_scoreboard_e;
83
84 #define SB_WORKING  0  /* The server is busy and the child is useful. */
85 #define SB_IDLE_DIE 1  /* The server is idle and the child is superfluous. */
86                        /*   The child should check for this and exit gracefully. */
87
88 /* stuff which is worker specific */
89 /***********************WARNING***************************************/
90 /* These are things that are used by mod_status. Do not put anything */
91 /*   in here that you cannot live without. This structure will not   */
92 /*   be available if mod_status is not loaded.                       */
93 /*********************************************************************/
94 typedef struct worker_score worker_score;
95
96 struct worker_score {
97     int thread_num;
98 #if APR_HAS_THREADS
99     apr_os_thread_t tid;
100 #endif
101     unsigned char status;
102     unsigned long access_count;
103     apr_off_t     bytes_served;
104     unsigned long my_access_count;
105     apr_off_t     my_bytes_served;
106     apr_off_t     conn_bytes;
107     unsigned short conn_count;
108     apr_time_t start_time;
109     apr_time_t stop_time;
110 #ifdef HAVE_TIMES
111     struct tms times;
112 #endif
113     apr_time_t last_used;
114     char client[32];            /* Keep 'em small... */
115     char request[64];           /* We just want an idea... */
116     char vhost[32];             /* What virtual host is being accessed? */
117 };
118
119 typedef struct {
120     int             server_limit;
121     int             thread_limit;
122     ap_scoreboard_e sb_type;
123     ap_generation_t running_generation; /* the generation of children which
124                                          * should still be serving requests.
125                                          */
126     apr_time_t restart_time;
127     int             lb_limit;
128 } global_score;
129
130 /* stuff which the parent generally writes and the children rarely read */
131 typedef struct process_score process_score;
132 struct process_score{
133     pid_t pid;
134     ap_generation_t generation; /* generation of this child */
135     ap_scoreboard_e sb_type;
136     int quiescing;          /* the process whose pid is stored above is
137                              * going down gracefully
138                              */
139 };
140
141 /* stuff which is lb specific */
142 typedef struct lb_score lb_score;
143 struct lb_score{
144     /* TODO: make a real stuct from this */
145     unsigned char data[1024];
146 };
147
148 /* Scoreboard is now in 'local' memory, since it isn't updated once created,
149  * even in forked architectures.  Child created-processes (non-fork) will
150  * set up these indicies into the (possibly relocated) shmem records.
151  */
152 typedef struct {
153     global_score *global;
154     process_score *parent;
155     worker_score **servers;
156     lb_score     *balancers;
157 } scoreboard;
158
159 typedef struct ap_sb_handle_t ap_sb_handle_t;
160
161 AP_DECLARE(int) ap_exists_scoreboard_image(void);
162 AP_DECLARE(void) ap_increment_counts(ap_sb_handle_t *sbh, request_rec *r);
163
164 int ap_create_scoreboard(apr_pool_t *p, ap_scoreboard_e t);
165 apr_status_t ap_reopen_scoreboard(apr_pool_t *p, apr_shm_t **shm, int detached);
166 void ap_init_scoreboard(void *shared_score);
167 AP_DECLARE(int) ap_calc_scoreboard_size(void);
168 apr_status_t ap_cleanup_scoreboard(void *d);
169
170 AP_DECLARE(void) ap_create_sb_handle(ap_sb_handle_t **new_sbh, apr_pool_t *p,
171                                      int child_num, int thread_num);
172     
173 AP_DECLARE(int) find_child_by_pid(apr_proc_t *pid);
174 AP_DECLARE(int) ap_update_child_status(ap_sb_handle_t *sbh, int status, request_rec *r);
175 AP_DECLARE(int) ap_update_child_status_from_indexes(int child_num, int thread_num,
176                                                     int status, request_rec *r);
177 void ap_time_process_request(ap_sb_handle_t *sbh, int status);
178
179 AP_DECLARE(worker_score *) ap_get_scoreboard_worker(int x, int y);
180 AP_DECLARE(process_score *) ap_get_scoreboard_process(int x);
181 AP_DECLARE(global_score *) ap_get_scoreboard_global(void);
182 AP_DECLARE(lb_score *) ap_get_scoreboard_lb(int lb_num);
183
184 AP_DECLARE_DATA extern scoreboard *ap_scoreboard_image;
185 AP_DECLARE_DATA extern const char *ap_scoreboard_fname;
186 AP_DECLARE_DATA extern int ap_extended_status;
187
188 AP_DECLARE_DATA extern ap_generation_t volatile ap_my_generation;
189
190 /* Hooks */
191 /**
192   * Hook for post scoreboard creation, pre mpm.
193   * @param p       Apache pool to allocate from.
194   * @param sb_type 
195   * @ingroup hooks
196   * @return OK or DECLINE on success; anything else is a error
197   */  
198 AP_DECLARE_HOOK(int, pre_mpm, (apr_pool_t *p, ap_scoreboard_e sb_type))
199
200 /**
201   * proxy load balancer
202   * @return the number of load balancer workers.
203   */  
204 APR_DECLARE_OPTIONAL_FN(int, ap_proxy_lb_workers,
205                         (void));
206
207 /* for time_process_request() in http_main.c */
208 #define START_PREQUEST 1
209 #define STOP_PREQUEST  2
210
211 #ifdef __cplusplus
212 }
213 #endif
214
215 #endif  /* !APACHE_SCOREBOARD_H */