Skip to content

Commit 052d4ca

Browse files
committed
Add 3 memory handlers to replace the scoreboard area of mod-proxy.
Note the mod_scoreboard.c just uses the actual scoreboard "shared" area for the workers. git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/branches/httpd-proxy-scoreboard@423444 13f79535-47bb-0310-9956-ffa450edef68
1 parent 3d6061c commit 052d4ca

6 files changed

Lines changed: 565 additions & 0 deletions

File tree

modules/mem/Makefile.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
include $(top_srcdir)/build/special.mk

modules/mem/config5.m4

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
dnl modules enabled in this directory by default
2+
3+
dnl APACHE_MODULE(name, helptext[, objects[, structname[, default[, config]]]])
4+
5+
APACHE_MODPATH_INIT(mem)
6+
7+
APACHE_MODULE(scoreboard, memslot provider that uses scoreboard, , , no)
8+
APACHE_MODULE(sharedmem, memslot provider that shared memory, , , no)
9+
APACHE_MODULE(plainmem, memslot provider that plain memory, , , no)
10+
11+
# Ensure that other modules can pick up slotmem.h
12+
APR_ADDTO(INCLUDES, [-I\$(top_srcdir)/$modpath_current])
13+
14+
APACHE_MODPATH_FINISH

modules/mem/mod_plainmem.c

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/* Copyright 1999-2006 The Apache Software Foundation or its licensors, as
2+
* applicable.
3+
*
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
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+
/* Memory handler for a plain memory divided in slot.
18+
* This one uses plain memory.
19+
*/
20+
#define CORE_PRIVATE
21+
22+
#include "apr.h"
23+
#include "apr_pools.h"
24+
25+
#include "httpd.h"
26+
#include "http_config.h"
27+
#include "http_log.h"
28+
29+
#include "slotmem.h"
30+
31+
struct ap_slotmem {
32+
char *name;
33+
void *base;
34+
apr_size_t size;
35+
int num;
36+
struct ap_slotmem *next;
37+
};
38+
39+
/* global pool and list of slotmem we are handling */
40+
static struct ap_slotmem *globallistmem = NULL;
41+
static apr_pool_t *globalpool = NULL;
42+
43+
static apr_status_t ap_slotmem_do(ap_slotmem_t *mem, ap_slotmem_callback_fn_t *func, void *data, apr_pool_t *pool)
44+
{
45+
int i;
46+
void *ptr;
47+
48+
if (!mem)
49+
return APR_ENOSHMAVAIL;
50+
51+
ptr = mem->base;
52+
for (i = 0; i < mem->num; i++) {
53+
ptr = ptr + mem->size;
54+
func((void *)ptr, data, pool);
55+
}
56+
return 0;
57+
}
58+
static apr_status_t ap_slotmem_create(ap_slotmem_t **new, const char *name, apr_size_t item_size, int item_num, apr_pool_t *pool)
59+
{
60+
void *slotmem = NULL;
61+
ap_slotmem_t *res;
62+
ap_slotmem_t *next = globallistmem;
63+
char *fname;
64+
apr_status_t rv;
65+
66+
fname = ap_server_root_relative(pool, name);
67+
68+
/* first try to attach to existing slotmem */
69+
if (next) {
70+
for (;;) {
71+
if (strcmp(next->name, fname) == 0) {
72+
/* we already have it */
73+
*new = next;
74+
return APR_SUCCESS;
75+
}
76+
if (next->next)
77+
break;
78+
next = next->next;
79+
}
80+
}
81+
82+
/* create the memory using the globalpool */
83+
res = (ap_slotmem_t *) apr_pcalloc(globalpool, sizeof(ap_slotmem_t));
84+
res->base = apr_pcalloc(globalpool, item_size * item_num);
85+
if (!res->base)
86+
return APR_ENOSHMAVAIL;
87+
memset(res->base, 0, item_size * item_num);
88+
89+
/* For the chained slotmem stuff */
90+
res->name = apr_pstrdup(globalpool, fname);
91+
res->size = item_size;
92+
res->num = item_num;
93+
res->next = NULL;
94+
if (globallistmem==NULL)
95+
globallistmem = res;
96+
else
97+
next->next = res;
98+
99+
*new = res;
100+
return APR_SUCCESS;
101+
}
102+
static apr_status_t ap_slotmem_mem(ap_slotmem_t *score, int id, void**mem)
103+
{
104+
105+
void *ptr = score->base + score->size * id;
106+
107+
if (!score)
108+
return APR_ENOSHMAVAIL;
109+
if (id<0 || id>score->num)
110+
return APR_ENOSHMAVAIL;
111+
112+
if (!ptr)
113+
return APR_ENOSHMAVAIL;
114+
*mem = ptr;
115+
return APR_SUCCESS;
116+
}
117+
118+
static const slotmem_storage_method storage = {
119+
&ap_slotmem_do,
120+
&ap_slotmem_create,
121+
&ap_slotmem_mem
122+
};
123+
124+
static int pre_config(apr_pool_t *p, apr_pool_t *plog,
125+
apr_pool_t *ptemp)
126+
{
127+
globalpool = p;
128+
return OK;
129+
}
130+
131+
static void ap_plainmem_register_hook(apr_pool_t *p)
132+
{
133+
ap_register_provider(p, SLOTMEM_STORAGE, "plain", "0", &storage);
134+
ap_hook_pre_config(pre_config, NULL, NULL, APR_HOOK_MIDDLE);
135+
}
136+
137+
module AP_MODULE_DECLARE_DATA plainmem_module = {
138+
STANDARD20_MODULE_STUFF,
139+
NULL, /* create per-directory config structure */
140+
NULL, /* merge per-directory config structures */
141+
NULL, /* create per-server config structure */
142+
NULL, /* merge per-server config structures */
143+
NULL, /* command apr_table_t */
144+
ap_plainmem_register_hook /* register hooks */
145+
};

modules/mem/mod_scoreboard.c

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/* Copyright 1999-2006 The Apache Software Foundation or its licensors, as
2+
* applicable.
3+
*
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
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+
/* Memory handler for a shared memory divided in slot.
18+
* This one uses the scoreboard and is only the proxy_worker loadbalancer
19+
*/
20+
#define CORE_PRIVATE
21+
22+
#include "apr.h"
23+
#include "apr_pools.h"
24+
25+
#include "httpd.h"
26+
#include "http_config.h"
27+
#include "http_log.h"
28+
29+
#include "scoreboard.h"
30+
31+
#include "slotmem.h"
32+
33+
#if MODULE_MAGIC_NUMBER_MAJOR > 20020903
34+
#define PROXY_HAS_SCOREBOARD 1
35+
#else
36+
#define PROXY_HAS_SCOREBOARD 0
37+
#endif
38+
39+
struct ap_slotmem {
40+
void *ptr;
41+
apr_size_t size;
42+
int num;
43+
};
44+
45+
static apr_status_t ap_slotmem_do(ap_slotmem_t *mem, ap_slotmem_callback_fn_t *func, void *data, apr_pool_t *pool)
46+
{
47+
int i;
48+
void *ptr;
49+
50+
if (!mem)
51+
return APR_ENOSHMAVAIL;
52+
53+
#if PROXY_HAS_SCOREBOARD
54+
for (i = 0; i < mem->num; i++) {
55+
ptr = (void *)ap_get_scoreboard_lb(i);
56+
func((void *)ptr, data, pool);
57+
}
58+
return 0;
59+
#else
60+
return APR_ENOSHMAVAIL;
61+
#endif
62+
}
63+
static apr_status_t ap_slotmem_create(ap_slotmem_t **new, const char *name, apr_size_t item_size, int item_num, apr_pool_t *pool)
64+
{
65+
void *score = NULL;
66+
ap_slotmem_t *res;
67+
68+
#if PROXY_HAS_SCOREBOARD
69+
if (ap_scoreboard_image) {
70+
score = (void *)ap_get_scoreboard_lb(0);
71+
if (!score)
72+
return APR_ENOSHMAVAIL;
73+
}
74+
#else
75+
return APR_ENOSHMAVAIL;
76+
#endif
77+
if (!score)
78+
return APR_ENOSHMAVAIL;
79+
80+
res = (ap_slotmem_t *) apr_pcalloc(pool, sizeof(ap_slotmem_t));
81+
res->ptr = score;
82+
res->size = item_size;
83+
res->num = item_num;
84+
*new = res;
85+
ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
86+
"ap_slotmem_create: score %d", score);
87+
88+
return APR_SUCCESS;
89+
}
90+
static apr_status_t ap_slotmem_mem(ap_slotmem_t *score, int id, void**mem)
91+
{
92+
void *ptr;
93+
if (!score)
94+
return APR_ENOSHMAVAIL;
95+
96+
#if PROXY_HAS_SCOREBOARD
97+
if (ap_scoreboard_image)
98+
ptr = (void *)ap_get_scoreboard_lb(id);
99+
#else
100+
return APR_ENOSHMAVAIL;
101+
#endif
102+
103+
if (!ptr)
104+
return APR_ENOSHMAVAIL;
105+
*mem = ptr;
106+
ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
107+
"ap_slotmem_mem: score %d", score);
108+
return APR_SUCCESS;
109+
}
110+
111+
static const slotmem_storage_method storage = {
112+
&ap_slotmem_do,
113+
&ap_slotmem_create,
114+
&ap_slotmem_mem
115+
};
116+
117+
static int pre_config(apr_pool_t *p, apr_pool_t *plog,
118+
apr_pool_t *ptemp)
119+
{
120+
#if PROXY_HAS_SCOREBOARD
121+
return OK;
122+
#else
123+
return DECLINED;
124+
#endif
125+
}
126+
127+
static void ap_scoreboard_register_hook(apr_pool_t *p)
128+
{
129+
ap_register_provider(p, SLOTMEM_STORAGE, "score", "0", &storage);
130+
ap_hook_pre_config(pre_config, NULL, NULL, APR_HOOK_MIDDLE);
131+
}
132+
133+
module AP_MODULE_DECLARE_DATA scoreboard_module = {
134+
STANDARD20_MODULE_STUFF,
135+
NULL, /* create per-directory config structure */
136+
NULL, /* merge per-directory config structures */
137+
NULL, /* create per-server config structure */
138+
NULL, /* merge per-server config structures */
139+
NULL, /* command apr_table_t */
140+
ap_scoreboard_register_hook /* register hooks */
141+
};

0 commit comments

Comments
 (0)