bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / tomcat-connectors-1.2.32-src / native / common / jk_shm.h
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17
18 /***************************************************************************
19  * Description: Shared Memory object header file                           *
20  * Author:      Mladen Turk <mturk@jboss.com>                              *
21  * Author:      Rainer Jung <rjung@apache.org>                             *
22  * Version:     $Revision: 893253 $                                           *
23  ***************************************************************************/
24 #ifndef _JK_SHM_H
25 #define _JK_SHM_H
26
27 #include "jk_global.h"
28 #include "jk_pool.h"
29 #include "jk_util.h"
30
31 #ifdef __cplusplus
32 extern "C"
33 {
34 #endif                          /* __cplusplus */
35
36 /**
37  * @file jk_shm.h
38  * @brief Jk shared memory management
39  *
40  *
41  */
42
43 #define JK_SHM_MAJOR    '1'
44 #define JK_SHM_MINOR    '3'
45 #define JK_SHM_STR_SIZ  63
46 #define JK_SHM_URI_SIZ  127
47 #define JK_SHM_DYNAMIC  16
48 #define JK_SHM_MAGIC    '!', 'J', 'K', 'S', 'H', 'M', JK_SHM_MAJOR, JK_SHM_MINOR
49 #define JK_SHM_MAGIC_SIZ  8
50
51 /* Really huge numbers, but 64 workers should be enough */
52 #define JK_SHM_MAX_WORKERS        64
53 #define JK_SHM_ALIGNMENT          64
54 #define JK_SHM_ALIGN(x)           JK_ALIGN((x), JK_SHM_ALIGNMENT)
55 #define JK_SHM_AJP_WORKER_SIZE    JK_SHM_ALIGN(sizeof(jk_shm_ajp_worker_t))
56 #define JK_SHM_LB_SUB_WORKER_SIZE JK_SHM_ALIGN(sizeof(jk_shm_lb_sub_worker_t))
57 #define JK_SHM_LB_WORKER_SIZE     JK_SHM_ALIGN(sizeof(jk_shm_lb_worker_t))
58 #define JK_SHM_AJP_SIZE(x)        ((x) * JK_SHM_AJP_WORKER_SIZE)
59 #define JK_SHM_LB_SUB_SIZE(x)     ((x) * JK_SHM_LB_SUB_WORKER_SIZE)
60 #define JK_SHM_LB_SIZE(x)         ((x) * JK_SHM_LB_WORKER_SIZE)
61 #define JK_SHM_DEF_SIZE           JK_SHM_AJP_SIZE(JK_SHM_MAX_WORKERS) + JK_SHM_LB_SUB_SIZE(JK_SHM_MAX_WORKERS) + JK_SHM_LB_SIZE(JK_SHM_MAX_WORKERS)
62
63 /** jk shm generic worker record structure */
64 struct jk_shm_worker_header
65 {
66     int     id;
67     int     type;
68     /* worker name */
69     char    name[JK_SHM_STR_SIZ+1];
70     /* Sequence counter starting at 0 and increasing
71      * every time we change the config
72      */
73     volatile unsigned int sequence;
74 };
75 typedef struct jk_shm_worker_header jk_shm_worker_header_t;
76
77 /** jk shm ajp13/ajp14 worker record structure */
78 struct jk_shm_ajp_worker
79 {
80     jk_shm_worker_header_t h;
81     char host[JK_SHM_STR_SIZ+1];
82     int port;
83     volatile int addr_sequence;
84
85     /* Configuration data mirrored from ajp_worker */
86     int cache_timeout;
87     int connect_timeout;
88     int ping_timeout;
89     int reply_timeout;
90     int prepost_timeout;
91     unsigned int recovery_opts;
92     int retries;
93     int retry_interval;
94     unsigned int max_packet_size;
95     /* current error state (runtime) of the worker */
96     volatile int state;
97     /* Statistical data */
98     /* Number of currently connected channels */
99     volatile int connected;
100     /* Number of currently busy channels */
101     volatile int busy;
102     /* Maximum number of busy channels */
103     volatile int max_busy;
104     volatile time_t error_time;
105     /* Number of bytes read from remote */
106     volatile jk_uint64_t readed;
107     /* Number of bytes transferred to remote */
108     volatile jk_uint64_t transferred;
109     /* Number of times the worker was used */
110     volatile jk_uint64_t  used;
111     /* Number of times the worker was used - snapshot during maintenance */
112     volatile jk_uint64_t  used_snapshot;
113     /* Number of non 200 responses */
114     volatile jk_uint32_t  errors;
115     /* Decayed number of reply_timeout errors */
116     volatile jk_uint32_t  reply_timeouts;
117     /* Number of client errors */
118     volatile jk_uint32_t  client_errors;
119     /* Last reset time */
120     volatile time_t last_reset;
121     volatile time_t last_maintain_time;
122 };
123 typedef struct jk_shm_ajp_worker jk_shm_ajp_worker_t;
124
125 /** jk shm lb sub worker record structure */
126 struct jk_shm_lb_sub_worker
127 {
128     jk_shm_worker_header_t h;
129
130     /* route */
131     char    route[JK_SHM_STR_SIZ+1];
132     /* worker domain */
133     char    domain[JK_SHM_STR_SIZ+1];
134     /* worker redirect route */
135     char    redirect[JK_SHM_STR_SIZ+1];
136     /* Number of currently busy channels */
137     volatile int busy;
138     /* worker distance */
139     volatile int distance;
140     /* current activation state (config) of the worker */
141     volatile int activation;
142     /* current error state (runtime) of the worker */
143     volatile int state;
144     /* Current lb factor */
145     volatile int lb_factor;
146     /* Current lb reciprocal factor */
147     volatile jk_uint64_t lb_mult;
148     /* Current lb value  */
149     volatile jk_uint64_t lb_value;
150     /* Statistical data */
151     volatile time_t error_time;
152     /* Number of times the worker was elected - snapshot during maintenance */
153     volatile jk_uint64_t  elected_snapshot;
154     /* Number of non 200 responses */
155     volatile jk_uint32_t  errors;
156 };
157 typedef struct jk_shm_lb_sub_worker jk_shm_lb_sub_worker_t;
158
159 /** jk shm lb worker record structure */
160 struct jk_shm_lb_worker
161 {
162     jk_shm_worker_header_t h;
163
164     /* Number of currently busy channels */
165     volatile int busy;
166     /* Maximum number of busy channels */
167     volatile int max_busy;
168     int     sticky_session;
169     int     sticky_session_force;
170     int     recover_wait_time;
171     int     error_escalation_time;
172     int     max_reply_timeouts;
173     int     retries;
174     int     retry_interval;
175     int     lbmethod;
176     int     lblock;
177     unsigned int max_packet_size;
178     /* Last reset time */
179     volatile time_t last_reset;
180     volatile time_t last_maintain_time;
181     /* Session cookie */
182     char    session_cookie[JK_SHM_STR_SIZ+1];
183     /* Session path */
184     char    session_path[JK_SHM_STR_SIZ+1];
185
186 };
187 typedef struct jk_shm_lb_worker jk_shm_lb_worker_t;
188
189 const char *jk_shm_name(void);
190
191 /* Calculate needed shm size */
192 size_t jk_shm_calculate_size(jk_map_t *init_data, jk_logger_t *l);
193
194 /* Open the shared memory creating file if needed
195  */
196 int jk_shm_open(const char *fname, size_t sz, jk_logger_t *l);
197
198 /* Close the shared memory
199  */
200 void jk_shm_close(void);
201
202 /* Attach the shared memory in child process.
203  * File has to be opened in parent.
204  */
205 int jk_shm_attach(const char *fname, size_t sz, jk_logger_t *l);
206
207 /* allocate shm memory
208  * If there is no shm present the pool will be used instead
209  */
210 void *jk_shm_alloc(jk_pool_t *p, size_t size);
211
212 /* allocate shm ajp worker record
213  * If there is no shm present the pool will be used instead
214  */
215 jk_shm_ajp_worker_t *jk_shm_alloc_ajp_worker(jk_pool_t *p);
216
217 /* allocate shm lb sub worker record
218  * If there is no shm present the pool will be used instead
219  */
220 jk_shm_lb_sub_worker_t *jk_shm_alloc_lb_sub_worker(jk_pool_t *p);
221
222 /* allocate shm lb worker record
223  * If there is no shm present the pool will be used instead
224  */
225 jk_shm_lb_worker_t *jk_shm_alloc_lb_worker(jk_pool_t *p);
226
227 /* Return workers.properties last modified time
228  */
229 time_t jk_shm_get_workers_time(void);
230
231 /* Set workers.properties last modified time
232  */
233 void jk_shm_set_workers_time(time_t t);
234
235 /* Check if the shared memory has been modified
236  * by some other process.
237  */
238 int jk_shm_is_modified(void);
239
240 /* Synchronize access and modification time.
241  * This function should be called when the shared memory
242  * is modified and after we update the config acording
243  * to the current shared memory data.
244  */
245 void jk_shm_sync_access_time(void);
246
247
248 /* Lock shared memory for thread safe access */
249 int jk_shm_lock(void);
250
251 /* Unlock shared memory for thread safe access */
252 int jk_shm_unlock(void);
253
254
255 #ifdef __cplusplus
256 }
257 #endif  /* __cplusplus */
258 #endif  /* _JK_SHM_H */