scoreboard.h 9.7 KB

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