]> granicus.if.org Git - apache/blob - include/scoreboard.h
OK... a good commit point (we don't quite compile yet though...)
[apache] / include / scoreboard.h
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 /**
18  * @file  scoreboard.h
19  * @brief Apache scoreboard library
20  */
21
22 #ifndef APACHE_SCOREBOARD_H
23 #define APACHE_SCOREBOARD_H
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28
29 #ifdef HAVE_SYS_TIMES_H
30 #include <sys/time.h>
31 #include <sys/times.h>
32 #endif
33
34 #include "ap_config.h"
35 #include "http_config.h"
36 #include "apr_hooks.h"
37 #include "apr_thread_proc.h"
38 #include "apr_portable.h"
39 #include "apr_shm.h"
40 #include "apr_optional.h"
41
42 /* Scoreboard file, if there is one */
43 #ifndef DEFAULT_SCOREBOARD
44 #define DEFAULT_SCOREBOARD "logs/apache_runtime_status"
45 #endif
46
47 /* Scoreboard info on a process is, for now, kept very brief --- 
48  * just status value and pid (the latter so that the caretaker process
49  * can properly update the scoreboard when a process dies).  We may want
50  * to eventually add a separate set of long_score structures which would
51  * give, for each process, the number of requests serviced, and info on
52  * the current, or most recent, request.
53  *
54  * Status values:
55  */
56
57 #define SERVER_DEAD 0
58 #define SERVER_STARTING 1       /* Server Starting up */
59 #define SERVER_READY 2          /* Waiting for connection (or accept() lock) */
60 #define SERVER_BUSY_READ 3      /* Reading a client request */
61 #define SERVER_BUSY_WRITE 4     /* Processing a client request */
62 #define SERVER_BUSY_KEEPALIVE 5 /* Waiting for more requests via keepalive */
63 #define SERVER_BUSY_LOG 6       /* Logging the request */
64 #define SERVER_BUSY_DNS 7       /* Looking up a hostname */
65 #define SERVER_CLOSING 8        /* Closing the connection */
66 #define SERVER_GRACEFUL 9       /* server is gracefully finishing request */
67 #define SERVER_IDLE_KILL 10     /* Server is cleaning up idle children. */
68 #define SERVER_NUM_STATUS 11    /* number of status settings */
69
70 /* Type used for generation indicies.  Startup and every restart cause a
71  * new generation of children to be spawned.  Children within the same
72  * generation share the same configuration information -- pointers to stuff
73  * created at config time in the parent are valid across children.  However,
74  * this can't work effectively with non-forked architectures.  So while the
75  * arrays in the scoreboard never change between the parent and forked
76  * children, so they do not require shm storage, the contents of the shm
77  * may contain no pointers.
78  */
79 typedef int ap_generation_t;
80
81 /* Is the scoreboard shared between processes or not? 
82  * Set by the MPM when the scoreboard is created.
83  */
84 typedef enum {
85     SB_NOT_SHARED = 1,
86     SB_SHARED = 2
87 } ap_scoreboard_e;
88
89 /* stuff which is worker specific */
90 typedef struct worker_score worker_score;
91 struct worker_score {
92 #if APR_HAS_THREADS
93     apr_os_thread_t tid;
94 #endif
95     int thread_num;
96     /* With some MPMs (e.g., worker), a worker_score can represent
97      * a thread in a terminating process which is no longer
98      * represented by the corresponding process_score.  These MPMs
99      * should set pid and generation fields in the worker_score.
100      */
101     pid_t pid;
102     ap_generation_t generation;
103     unsigned char status;
104     unsigned short conn_count;
105     apr_off_t     conn_bytes;
106     unsigned long access_count;
107     apr_off_t     bytes_served;
108     unsigned long my_access_count;
109     apr_off_t     my_bytes_served;
110     apr_time_t start_time;
111     apr_time_t stop_time;
112     apr_time_t last_used;
113 #ifdef HAVE_TIMES
114     struct tms times;
115 #endif
116     char client[32];            /* Keep 'em small... */
117     char request[64];           /* We just want an idea... */
118     char vhost[32];             /* What virtual host is being accessed? */
119 };
120
121 typedef struct {
122     int             server_limit;
123     int             thread_limit;
124     ap_scoreboard_e sb_type;
125     ap_generation_t running_generation; /* the generation of children which
126                                          * should still be serving requests.
127                                          */
128     apr_time_t restart_time;
129 } global_score;
130
131 /* stuff which the parent generally writes and the children rarely read */
132 typedef struct process_score process_score;
133 struct process_score {
134     pid_t pid;
135     ap_generation_t generation; /* generation of this child */
136     int quiescing;          /* the process whose pid is stored above is
137                              * going down gracefully
138                              */
139 };
140
141 /* Scoreboard is now in 'local' memory, since it isn't updated once created,
142  * even in forked architectures.  Child created-processes (non-fork) will
143  * set up these indicies into the (possibly relocated) shmem records.
144  */
145 typedef struct {
146     global_score *global;
147     process_score *parent;
148     worker_score **servers;
149 } scoreboard;
150
151 typedef struct ap_sb_handle_t ap_sb_handle_t;
152
153 AP_DECLARE(int) ap_exists_scoreboard_image(void);
154 AP_DECLARE(void) ap_increment_counts(ap_sb_handle_t *sbh, request_rec *r);
155
156 int ap_create_scoreboard(apr_pool_t *p, ap_scoreboard_e t);
157 apr_status_t ap_reopen_scoreboard(apr_pool_t *p, apr_shm_t **shm, int detached);
158 void ap_init_scoreboard(void *shared_score);
159 AP_DECLARE(int) ap_calc_scoreboard_size(void);
160 apr_status_t ap_cleanup_scoreboard(void *d);
161
162 AP_DECLARE(void) ap_create_sb_handle(ap_sb_handle_t **new_sbh, apr_pool_t *p,
163                                      int child_num, int thread_num);
164     
165 AP_DECLARE(int) ap_find_child_by_pid(apr_proc_t *pid);
166 AP_DECLARE(int) ap_update_child_status(ap_sb_handle_t *sbh, int status, request_rec *r);
167 AP_DECLARE(int) ap_update_child_status_from_indexes(int child_num, int thread_num,
168                                                     int status, request_rec *r);
169 AP_DECLARE(int) ap_update_child_status_from_conn(ap_sb_handle_t *sbh, int status, conn_rec *c);
170 AP_DECLARE(void) ap_time_process_request(ap_sb_handle_t *sbh, int status);
171
172 AP_DECLARE(worker_score *) ap_get_scoreboard_worker(ap_sb_handle_t *sbh);
173 AP_DECLARE(worker_score *) ap_get_scoreboard_worker_from_indexes(int child_num,
174                                                                 int thread_num);
175 AP_DECLARE(process_score *) ap_get_scoreboard_process(int x);
176 AP_DECLARE(global_score *) ap_get_scoreboard_global(void);
177
178 AP_DECLARE_DATA extern scoreboard *ap_scoreboard_image;
179 AP_DECLARE_DATA extern const char *ap_scoreboard_fname;
180 AP_DECLARE_DATA extern int ap_extended_status;
181 AP_DECLARE_DATA extern int ap_mod_status_reqtail;
182
183 /*
184  * Command handlers [internal]
185  */
186 const char *ap_set_scoreboard(cmd_parms *cmd, void *dummy, const char *arg);
187 const char *ap_set_extended_status(cmd_parms *cmd, void *dummy, int arg);
188 const char *ap_set_reqtail(cmd_parms *cmd, void *dummy, int arg);
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 /* for time_process_request() in http_main.c */
201 #define START_PREQUEST 1
202 #define STOP_PREQUEST  2
203
204 #ifdef __cplusplus
205 }
206 #endif
207
208 #endif  /* !APACHE_SCOREBOARD_H */