You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modules-dev@httpd.apache.org by Simone Caruso <in...@simonecaruso.com> on 2011/02/28 18:26:17 UTC

Help with apr mutex

Hi all,

I wrote a simple cache inside my module with apr_shm and apr_rmm, but I have problems with apr mutex...

I have this peace of code:

static apr_global_mutex_t *mtx = NULL;
static void module_translate_name(request_rec *r)
{
	if(apr_global_mutex_trylock(mtx) == APR_EBUSY)
		ap_log_rerror(APLOG_MARK, APLOG_WARNING|APLOG_NOERRNO, 0, r, "Memory Locked!! ");
	apr_global_mutex_lock(mtx);
	element = get_from_cache();
	if(element == NULL){
		element = insert_into_cache();
		strcpy(element.name,"name\0");
	}
	apr_global_mutex_unlock(mtx);
}

static void module_post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
{
	result = apr_global_mutex_create(&mtx, "cache_entries_lck", APR_LOCK_DEFAULT, p);
}


static void mod_vhost_ldap_child_init(apr_pool_t * p, server_rec * s)
{
	apr_status_t ret;
	ret = apr_global_mutex_child_init(&mtx, "cache_entries_lck", p);
}



During my tests with 'ab -c 15 -n 3000' i NEVER get the "Memory Locked" error in logs, plus i get duplicated entries in 
cache but I don't know why!

Does someone have an idea? (please consider i use apr_global_mutex_trylock() only for test)
Regards.

-- 
Simone Caruso

Re: Help with apr mutex

Posted by Ben Noordhuis <in...@bnoordhuis.nl>.
On Tue, Mar 1, 2011 at 17:32, Joshua Marantz <jm...@google.com> wrote:
> Is there any design doc or examples on how these can be used to, say, construct
> a server-wide cache that can be accessed from any of the child processes in
> (say) prefork mode?

I don't know of any design documents or tutorials but as to examples:

* slotmem is for fixed size caches, it's used by mod_proxy_balancer.c
to store state about its backends.

* socache is for dynamic caches, mod_ssl and mod_authn_socache.c
respectively cache SSL sessions and user credentials with it.

mod_authn_socache.c is a good starting point, it's less than 500 lines of code.

Re: Help with apr mutex

Posted by Joshua Marantz <jm...@google.com>.
For convenience, you can look at these intriguing new facilities in the SVN
repo directly without downloading the tarball:

<http://svn.apache.org/viewvc/httpd/httpd/trunk/include/ap_socache.h?view=log>
http://svn.apache.org/viewvc/httpd/httpd/trunk/include/ap_socache.h?view=markup
<http://svn.apache.org/viewvc/httpd/httpd/trunk/include/ap_socache.h?view=markup>
http://svn.apache.org/viewvc/httpd/httpd/trunk/include/ap_slotmem.h?view=markup
<http://svn.apache.org/viewvc/httpd/httpd/trunk/include/ap_slotmem.h?view=markup>
http://svn.apache.org/viewvc/httpd/httpd/trunk/modules/cache/mod_socache_shmcb.c?view=markup

<http://svn.apache.org/viewvc/httpd/httpd/trunk/modules/cache/mod_socache_shmcb.c?view=markup>Is
there any design doc or examples on how these can be used to, say, construct
a server-wide cache that can be accessed from any of the child processes in
(say) prefork mode?

Thanks!
-Josh

On Tue, Mar 1, 2011 at 10:36 AM, Simone Caruso <in...@simonecaruso.com>wrote:

> On 28/02/2011 22:48, Ben Noordhuis wrote:
>
>> On Mon, Feb 28, 2011 at 18:26, Simone Caruso<in...@simonecaruso.com>
>>  wrote:
>>
>>> I wrote a simple cache inside my module with apr_shm and apr_rmm
>>>
>>
>> Simone, have a look at ap_socache.h and ap_slotmem.h, they're two
>> simple cache facilities that were added in 2.3.0. Might save you some
>> work. :)
>>
>
> I read trunk/modules/cache/mod_socache_shmcb.c too, looks very interesting!
> Surely I will rewrite my module for 2.3 using socache,
> thank you Ben.
>
> --
> Simone Caruso
> p.iva: 03045250838
>

Re: Help with apr mutex

Posted by Simone Caruso <in...@simonecaruso.com>.
On 28/02/2011 22:48, Ben Noordhuis wrote:
> On Mon, Feb 28, 2011 at 18:26, Simone Caruso<in...@simonecaruso.com>  wrote:
>> I wrote a simple cache inside my module with apr_shm and apr_rmm
>
> Simone, have a look at ap_socache.h and ap_slotmem.h, they're two
> simple cache facilities that were added in 2.3.0. Might save you some
> work. :)

I read trunk/modules/cache/mod_socache_shmcb.c too, looks very interesting!
Surely I will rewrite my module for 2.3 using socache,
thank you Ben.

-- 
Simone Caruso
p.iva: 03045250838

Re: Help with apr mutex

Posted by Ben Noordhuis <in...@bnoordhuis.nl>.
On Mon, Feb 28, 2011 at 18:26, Simone Caruso <in...@simonecaruso.com> wrote:
> I wrote a simple cache inside my module with apr_shm and apr_rmm

Simone, have a look at ap_socache.h and ap_slotmem.h, they're two
simple cache facilities that were added in 2.3.0. Might save you some
work. :)

Re: Help with apr mutex

Posted by Simone Caruso <in...@simonecaruso.com>.
On 28/02/2011 18:35, Jim Jagielski wrote:
> Why not check the return status of the lock and unlock calls??
>
Ehm... i'm an idiot...

i create the lock in this way:
module_post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
{
	lock = ap_server_root_relative(p, "mymod.lck");
	result = apr_global_mutex_create(&mtx, lock, APR_LOCK_DEFAULT, p);
	if(result != APR_SUCCESS)
	...
}

apr_global_mutex_create returns APR_SUCCESS, but apr_global_mutex_lock returns "Permission denied",
maybe the lock is created wrong permissions ??

-- 
Simone Caruso
IT Consultant

Re: Help with apr mutex

Posted by Jim Jagielski <ji...@jaguNET.com>.
Why not check the return status of the lock and unlock calls??

On Feb 28, 2011, at 12:26 PM, Simone Caruso wrote:

> Hi all,
> 
> I wrote a simple cache inside my module with apr_shm and apr_rmm, but I have problems with apr mutex...
> 
> I have this peace of code:
> 
> static apr_global_mutex_t *mtx = NULL;
> static void module_translate_name(request_rec *r)
> {
> 	if(apr_global_mutex_trylock(mtx) == APR_EBUSY)
> 		ap_log_rerror(APLOG_MARK, APLOG_WARNING|APLOG_NOERRNO, 0, r, "Memory Locked!! ");
> 	apr_global_mutex_lock(mtx);
> 	element = get_from_cache();
> 	if(element == NULL){
> 		element = insert_into_cache();
> 		strcpy(element.name,"name\0");
> 	}
> 	apr_global_mutex_unlock(mtx);
> }
> 
> static void module_post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
> {
> 	result = apr_global_mutex_create(&mtx, "cache_entries_lck", APR_LOCK_DEFAULT, p);
> }
> 
> 
> static void mod_vhost_ldap_child_init(apr_pool_t * p, server_rec * s)
> {
> 	apr_status_t ret;
> 	ret = apr_global_mutex_child_init(&mtx, "cache_entries_lck", p);
> }
> 
> 
> 
> During my tests with 'ab -c 15 -n 3000' i NEVER get the "Memory Locked" error in logs, plus i get duplicated entries in cache but I don't know why!
> 
> Does someone have an idea? (please consider i use apr_global_mutex_trylock() only for test)
> Regards.
> 
> -- 
> Simone Caruso
> 


Re: Help with apr mutex

Posted by Simone Caruso <in...@simonecaruso.com>.
On 28/02/2011 18:53, thomas bonfort wrote:
> a few things to try:
>
> * you should use APR_STATUS_IS_EBUSY(s) instead of ... == APR_EBUSY
> * after creating the mutex, you might have to set permissions on it:
> #ifdef AP_NEED_SET_MUTEX_PERMS
>     rc = unixd_set_global_mutex_perms(mutex);
>     if(rc != APR_SUCCESS) {
>        ap_log_error(APLOG_MARK, APLOG_CRIT, rc, s, "Could not set
> permissions on global parent mutex %s", mutex_name);
>        return rc;
>     }
> #endif
>
> * I *think* that calling the lock functions twice from a same thread
> (without unlocking first) can have undefined behavior, i.e. don't do
> the lock() just after the trylock() call.
>
> regards,
> thomas

Thanks Thoms,
i'm going to try your code!

-- 
Simone Caruso
IT Consultant
+39 349 65 90 805
p.iva: 03045250838

Re: Help with apr mutex

Posted by thomas bonfort <th...@gmail.com>.
a few things to try:

* you should use APR_STATUS_IS_EBUSY(s) instead of ... == APR_EBUSY
* after creating the mutex, you might have to set permissions on it:
#ifdef AP_NEED_SET_MUTEX_PERMS
   rc = unixd_set_global_mutex_perms(mutex);
   if(rc != APR_SUCCESS) {
      ap_log_error(APLOG_MARK, APLOG_CRIT, rc, s, "Could not set
permissions on global parent mutex %s", mutex_name);
      return rc;
   }
#endif

* I *think* that calling the lock functions twice from a same thread
(without unlocking first) can have undefined behavior, i.e. don't do
the lock() just after the trylock() call.

regards,
thomas



On Mon, Feb 28, 2011 at 18:26, Simone Caruso <in...@simonecaruso.com> wrote:
> Hi all,
>
> I wrote a simple cache inside my module with apr_shm and apr_rmm, but I have
> problems with apr mutex...
>
> I have this peace of code:
>
> static apr_global_mutex_t *mtx = NULL;
> static void module_translate_name(request_rec *r)
> {
>        if(apr_global_mutex_trylock(mtx) == APR_EBUSY)
>                ap_log_rerror(APLOG_MARK, APLOG_WARNING|APLOG_NOERRNO, 0, r,
> "Memory Locked!! ");
>        apr_global_mutex_lock(mtx);
>        element = get_from_cache();
>        if(element == NULL){
>                element = insert_into_cache();
>                strcpy(element.name,"name\0");
>        }
>        apr_global_mutex_unlock(mtx);
> }
>
> static void module_post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t
> *ptemp, server_rec *s)
> {
>        result = apr_global_mutex_create(&mtx, "cache_entries_lck",
> APR_LOCK_DEFAULT, p);
> }
>
>
> static void mod_vhost_ldap_child_init(apr_pool_t * p, server_rec * s)
> {
>        apr_status_t ret;
>        ret = apr_global_mutex_child_init(&mtx, "cache_entries_lck", p);
> }
>
>
>
> During my tests with 'ab -c 15 -n 3000' i NEVER get the "Memory Locked"
> error in logs, plus i get duplicated entries in cache but I don't know why!
>
> Does someone have an idea? (please consider i use apr_global_mutex_trylock()
> only for test)
> Regards.
>
> --
> Simone Caruso
>