Skip to content

Commit

Permalink
Do not store pointers in shared memory
Browse files Browse the repository at this point in the history
Keeping pointers to nodes and active list in shared memory was my bad idea.
Although it works, because child processes inherit shared memory mapped at the
same address as parent that allocated aformentioned shmem, this excludes usage
of shmem by other processes (ie. prospective upload progress cache browser)
Shared memory block is "partitioned" manually without use of apache rmm interface
(pointer arithmetic is easy ;-)

Signed-off-by: Michał Pokrywka <michal.pokrywka@gmail.com>
  • Loading branch information
mpokrywka committed Feb 28, 2011
1 parent d9b3243 commit 36f0b2d
Showing 1 changed file with 17 additions and 42 deletions.
59 changes: 17 additions & 42 deletions mod_upload_progress.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ typedef struct upload_progress_node_s{
typedef struct { typedef struct {
int count; int count;
int active; int active;
upload_progress_node_t *nodes; /* all nodes allocated at once */
int *list; /* static array of node indexes, list begins with indexes of active nodes */
} upload_progress_cache_t; } upload_progress_cache_t;


typedef struct { typedef struct {
Expand All @@ -115,9 +113,10 @@ typedef struct {
char *lock_file; /* filename for shm lock mutex */ char *lock_file; /* filename for shm lock mutex */
apr_size_t cache_bytes; apr_size_t cache_bytes;
apr_shm_t *cache_shm; apr_shm_t *cache_shm;
apr_rmm_t *cache_rmm;
char *cache_file; char *cache_file;
upload_progress_cache_t *cache; upload_progress_cache_t *cache;
int *list; /* static array of node indexes, list begins with indexes of active nodes */
upload_progress_node_t *nodes; /* all nodes allocated at once */
} ServerConfig; } ServerConfig;


upload_progress_node_t* insert_node(request_rec *r, const char *key); upload_progress_node_t* insert_node(request_rec *r, const char *key);
Expand Down Expand Up @@ -440,7 +439,7 @@ upload_progress_node_t* insert_node(request_rec *r, const char *key) {
ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, r->server, "Cache full"); ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, r->server, "Cache full");
return NULL; return NULL;
} }
node = &cache->nodes[cache->list[cache->active]]; node = &config->nodes[config->list[cache->active]];
cache->active += 1; cache->active += 1;


strncpy(node->key, key, ARG_MAXLEN_PROGRESSID); strncpy(node->key, key, ARG_MAXLEN_PROGRESSID);
Expand All @@ -454,8 +453,8 @@ upload_progress_node_t *find_node(request_rec *r, const char *key) {


ServerConfig *config = get_server_config(r->server); ServerConfig *config = get_server_config(r->server);
upload_progress_cache_t *cache = config->cache; upload_progress_cache_t *cache = config->cache;
upload_progress_node_t *node, *nodes = cache->nodes; upload_progress_node_t *node, *nodes = config->nodes;
int *list = cache->list; int *list = config->list;
int active = cache->active; int active = cache->active;
int i; int i;


Expand Down Expand Up @@ -493,8 +492,8 @@ static void clean_old_connections(request_rec *r) {


ServerConfig *config = get_server_config(r->server); ServerConfig *config = get_server_config(r->server);
upload_progress_cache_t *cache = config->cache; upload_progress_cache_t *cache = config->cache;
upload_progress_node_t *node, *nodes = cache->nodes; upload_progress_node_t *node, *nodes = config->nodes;
int *list = cache->list; int *list = config->list;
int i, tmp; int i, tmp;
time_t t = time(NULL); time_t t = time(NULL);


Expand All @@ -510,19 +509,12 @@ static void clean_old_connections(request_rec *r) {
} }
} }


void *rmm_calloc(apr_rmm_t *rmm, apr_size_t reqsize)
{
apr_rmm_off_t block = apr_rmm_calloc(rmm, reqsize);
return block ? apr_rmm_addr_get(rmm, block) : NULL;
}

apr_status_t upload_progress_cache_init(apr_pool_t *pool, ServerConfig *config) apr_status_t upload_progress_cache_init(apr_pool_t *pool, ServerConfig *config)
{ {
/**/up_log(APLOG_MARK, APLOG_DEBUG, 0, global_server, "upload_progress_cache_init()"); /**/up_log(APLOG_MARK, APLOG_DEBUG, 0, global_server, "upload_progress_cache_init()");


apr_status_t result; apr_status_t result;
apr_size_t size; apr_size_t size;
upload_progress_cache_t *cache;
int nodes_cnt, i; int nodes_cnt, i;


if (config->cache_file) { if (config->cache_file) {
Expand All @@ -538,33 +530,16 @@ apr_status_t upload_progress_cache_init(apr_pool_t *pool, ServerConfig *config)


/* Determine the usable size of the shm segment. */ /* Determine the usable size of the shm segment. */
size = apr_shm_size_get(config->cache_shm); size = apr_shm_size_get(config->cache_shm);

nodes_cnt = (size - sizeof(upload_progress_cache_t)) /
/* This will create a rmm "handler" to get into the shared memory area */ (sizeof(int) + sizeof(upload_progress_node_t));
result = apr_rmm_init(&config->cache_rmm, NULL,
apr_shm_baseaddr_get(config->cache_shm), size, /* init cache */
pool); config->cache = (upload_progress_cache_t *)apr_shm_baseaddr_get(config->cache_shm);
if (result != APR_SUCCESS) { config->list = (int *)(config->cache + 1);
return result; config->nodes = (upload_progress_node_t *)(config->list + nodes_cnt);
} config->cache->count = nodes_cnt;

config->cache->active = 0;
/* init cache object */ for (i = 0; i < nodes_cnt; i++) config->list[i] = i;
cache = (upload_progress_cache_t *)rmm_calloc(config->cache_rmm,
sizeof(upload_progress_cache_t));
if (!cache) return APR_ENOMEM;

config->cache = cache;
nodes_cnt = ((size - sizeof(upload_progress_cache_t)) /
(sizeof(upload_progress_node_t) + sizeof(int))) - 1;
cache->count = nodes_cnt;
cache->active = 0;

cache->list = (int *)rmm_calloc(config->cache_rmm, nodes_cnt * sizeof(int));
if (!cache->list) return APR_ENOMEM;
for (i = 0; i < nodes_cnt; i++) cache->list[i] = i;

cache->nodes = (upload_progress_node_t *)rmm_calloc(config->cache_rmm,
nodes_cnt * sizeof(upload_progress_node_t));
if (!cache->nodes) return APR_ENOMEM;


ap_log_error(APLOG_MARK, APLOG_INFO, 0, global_server, ap_log_error(APLOG_MARK, APLOG_INFO, 0, global_server,
"Upload Progress: monitoring max %i simultaneous uploads, id (%s) length %i..%i", "Upload Progress: monitoring max %i simultaneous uploads, id (%s) length %i..%i",
Expand Down

0 comments on commit 36f0b2d

Please sign in to comment.