tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject cvs commit: jakarta-tomcat-connectors/jk/native/common jk_shm.c jk_shm.h
Date Mon, 07 Feb 2005 19:06:35 GMT
mturk       2005/02/07 11:06:35

  Modified:    jk/native/common jk_shm.c jk_shm.h
  Log:
  Change shared memory API. There will be always one and only shared
  memory, so make it's instance static. Also use two types of shared memory
  to lower the memory if JkShmFile is not defined in config. The overhed in
  that case is 2K for maximum of 512 workers.
  
  Revision  Changes    Path
  1.7       +132 -119  jakarta-tomcat-connectors/jk/native/common/jk_shm.c
  
  Index: jk_shm.c
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-connectors/jk/native/common/jk_shm.c,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- jk_shm.c	6 Feb 2005 11:30:02 -0000	1.6
  +++ jk_shm.c	7 Feb 2005 19:06:34 -0000	1.7
  @@ -23,66 +23,72 @@
   #include "jk_pool.h"
   #include "jk_shm.h"
   
  -const char shm_signature[] = { JK_SHM_MAGIC };
  +/** jk shm header record structure */
  +struct jk_shm_h_rec
  +{
  +    /* Shared memory magic JK_SHM_MAGIC */
  +    char            magic[8];
  +    int             workers;
  +    int             allocated;
  +    jk_shm_w_rec_t  *pr[1];
  +    jk_shm_w_rec_t  rr[1];
  +};
  +typedef struct jk_shm_h_rec jk_shm_h_rec_t;
  +
  +/** jk shm structure */
  +struct jk_shm
  +{
  +    size_t     size;
  +    const char *filename;
  +    int        fd;
  +    int        attached;
  +    jk_shm_h_rec_t  *hdr;
  +};
   
  -#if defined (WIN32) || defined(NETWARE)
  +typedef struct jk_shm jk_shm_t;
   
  -/* Simulate file */
  -static jk_shm_t *jk_global_shm = NULL;
  +static const char shm_signature[] = { JK_SHM_MAGIC };
  +static jk_shm_t jk_shmem = { 0, NULL, -1, 0, NULL};;
  +
  +#if defined (WIN32) || defined(NETWARE)
   
   /* Use plain memory */
  -int jk_shm_open(const char *fname, int workers, int dynamic, jk_shm_t *shm)
  +int jk_shm_open(const char *fname)
   {
  -    jk_shm_h_rec_t *hdr;
  -
  -    if (jk_global_shm) {
  -        shm->attached = jk_global_shm->attached;
  -        shm->size = jk_global_shm->size;
  -        shm->base = jk_global_shm->base;
  -        shm->filename = jk_global_shm->filename;
  +    if (jk_shmem.hdr) {
           return 0;
       }
  -    if (workers < 1 || workers > JK_SHM_MAX_WORKERS)
  -        workers = JK_SHM_MAX_WORKERS;
  -    if (dynamic < 1 || dynamic > JK_SHM_MAX_WORKERS)
  -        dynamic = JK_SHM_MAX_WORKERS;
   
  -    shm->size = sizeof(jk_shm_h_rec_t) + (workers + dynamic) * sizeof(jk_shm_w_rec_t);
  +    jk_shmem.size =  JK_SHM_ALIGN(sizeof(jk_shm_h_rec_t) + JK_SHM_MAX_WORKERS * sizeof(jk_shm_w_rec_t));
   
  -    shm->base = calloc(1, shm->size);
  -    if (!shm->base)
  +    jk_shmem.hdr = (jk_shm_h_rec_t *)calloc(1, jk_shmem.size);
  +    if (!jk_shmem.hdr)
           return -1;
  -    shm->filename = "memory";
  -    shm->fd = -1;
  -    shm->attached = 0;
  -
  -    hdr = (jk_shm_h_rec_t *)shm->base;
  +    jk_shmem.filename = "memory";
  +    jk_shmem.fd       = 0;
  +    jk_shmem.attached = 0;
   
  -    memcpy(hdr->magic, shm_signature, 8);
  -    hdr->workers = workers;
  -    hdr->dynamic = dynamic;
  -
  -    jk_global_shm = shm;
  +    memcpy(jk_shmem.hdr->magic, shm_signature, 8);
  +    jk_shmem.hdr->workers = JK_SHM_MAX_WORKERS;
       return 0;
   }
   
  -int jk_shm_attach(const char *fname, int workers, int dynamic, jk_shm_t *shm)
  +int jk_shm_attach(const char *fname)
   {
  -    if (!jk_shm_open(fname, workers, dynamic, shm)) {
  -        shm->attached = 1;
  +    if (!jk_shm_open(fname)) {
  +        jk_shmem.attached = 1;
           return 0;
       }
       else
           return -1;
   }
   
  -void jk_shm_close(jk_shm_t *shm)
  +void jk_shm_close()
   {
  -    if (shm) {
  -        if (shm->base)
  -            free(shm->base);
  -        shm->base = NULL;
  +    if (jk_shmem.hdr) {
  +        free(jk_shmem.hdr);
       }
  +    jk_shmem.hdr = NULL;
   }
   
   #else
  @@ -102,147 +108,154 @@
   #define MAP_FILE    (0)
   #endif
   
  -static int do_shm_open(const char *fname, int workers, int dynamic,
  -                       jk_shm_t *shm, int attached)
  +static int do_shm_open(const char *fname, int attached)
   {
  +    int rc;
       int fd;
       int flags = O_RDWR;
  +    void *base;
   
  -    shm->size = 0;
  -    shm->base = NULL;
  -
  -    if (workers < 1 || workers > JK_SHM_MAX_WORKERS)
  -        workers = JK_SHM_MAX_WORKERS;
  -    if (dynamic < 1 || dynamic > JK_SHM_MAX_WORKERS)
  -        dynamic = JK_SHM_MAX_WORKERS;
  -
  -    shm->filename = fname;
  -    shm->attached = attached;
  -    shm->size = sizeof(jk_shm_h_rec_t) + (workers + dynamic) * sizeof(jk_shm_w_rec_t);
  +    if (jk_shmem.hdr)
  +        jk_shm_close();
  +    jk_shmem.filename = fname;
  +    jk_shmem.attached = attached;
   
       /* Use plain memory in case there is no file name */
       if (!fname) {
  -        jk_shm_h_rec_t *hdr;
  -        shm->base = calloc(1, shm->size);
  -        if (!shm->base)
  +        jk_shmem.size = JK_SHM_ALIGN(sizeof(jk_shm_h_rec_t) + JK_SHM_MAX_WORKERS * sizeof(jk_shm_w_rec_t
*));
  +        jk_shmem.hdr  = calloc(1, jk_shmem.size);
  +        if (!jk_shmem.hdr)
               return -1;
  -        hdr = (jk_shm_h_rec_t *)shm->base;
  -
  -        memcpy(hdr->magic, shm_signature, 8);
  -        hdr->workers = workers;
  -        hdr->dynamic = dynamic;
  -        shm->filename = "memory";
  +        memcpy(jk_shmem.hdr->magic, shm_signature, 8);
  +        jk_shmem.hdr->workers   = JK_SHM_MAX_WORKERS;
  +        jk_shmem.hdr->allocated = 0;
  +        jk_shmem.filename       = "memory";
           return 0;
       }
  +    
  +    jk_shmem.size = JK_SHM_ALIGN(sizeof(jk_shm_h_rec_t) + JK_SHM_MAX_WORKERS * sizeof(jk_shm_w_rec_t));
  +    
       if (!attached)
           flags |= (O_CREAT|O_TRUNC);
       fd = open(fname, flags, 0666);
       if (fd == -1) {
  -        shm->size = 0;
  -        return -1;
  +        jk_shmem.size = 0;
  +        return errno;
       }
   
       if (!attached) {
           size_t size = lseek(fd, 0, SEEK_END);
  -        if (size < shm->size) {
  -            size = shm->size;
  -            if (ftruncate(fd, shm->size)) {
  +        if (size < jk_shmem.size) {
  +            size = jk_shmem.size;
  +            if (ftruncate(fd, jk_shmem.size)) {
  +                rc = errno;
                   close(fd);
  -                shm->size = 0;
  -                return -1;
  +                jk_shmem.size = 0;
  +                return rc;
               }
           }
       }
       if (lseek(fd, 0, SEEK_SET) != 0) {
  +        rc = errno;
           close(fd);
  -        shm->size = 0;
  -        return -1;
  +        jk_shmem.size = 0;
  +        return rc;
       }
   
  -    shm->base = mmap(NULL, shm->size,
  -                     PROT_READ | PROT_WRITE,
  -                     MAP_FILE | MAP_SHARED,
  -                     fd, 0);
  -    if (shm->base == (void *)MAP_FAILED) {
  -        shm->base = NULL;
  -    }
  -    if (!shm->base) {
  +    base = mmap(NULL, jk_shmem.size,
  +                PROT_READ | PROT_WRITE,
  +                MAP_FILE | MAP_SHARED,
  +                fd, 0);
  +    if (base == (void *)MAP_FAILED || base == NULL) {
  +        rc = errno;
           close(fd);
  -        shm->size = 0;
  -        return -1;
  +        jk_shmem.size = 0;
  +        return rc;
       }
  -    shm->fd = fd;
  +    jk_shmem.hdr = base;
  +    jk_shmem.fd  = fd;
   
       /* Clear shared memory */
       if (!attached) {
  -        jk_shm_h_rec_t *hdr;
  -
  -        memset(shm->base, 0, shm->size);
  -        hdr = (jk_shm_h_rec_t *)shm->base;
  -
  -        memcpy(hdr->magic, shm_signature, 8);
  -        hdr->workers = workers;
  -        hdr->dynamic = dynamic;
  +        memset(jk_shmem.hdr, 0, jk_shmem.size);
  +        memcpy(jk_shmem.hdr->magic, shm_signature, 8);
  +        jk_shmem.hdr->workers = JK_SHM_MAX_WORKERS;
       }
       else {
           /* TODO: check header magic */
       }
  -
       return 0;
   }
   
  -int jk_shm_open(const char *fname, int workers, int dynamic, jk_shm_t *shm)
  +int jk_shm_open(const char *fname)
   {
  -    return do_shm_open(fname, workers, dynamic, shm, 0);
  +    return do_shm_open(fname, 0);
   }
   
  -int jk_shm_attach(const char *fname, int workers, int dynamic, jk_shm_t *shm)
  +int jk_shm_attach(const char *fname)
   {
  -    return do_shm_open(fname, workers, dynamic, shm, 1);
  +    return do_shm_open(fname, 1);
   }
   
  -void jk_shm_close(jk_shm_t *shm)
  +void jk_shm_close()
   {
  -    if (shm) {
  -        if (shm->base) {
  -            if (shm->fd >= 0) {
  -                munmap(shm->base, shm->size);
  -                close(shm->fd);
  +    if (jk_shmem.hdr) {
  +        if (jk_shmem.fd >= 0) {
  +            munmap((void *)jk_shmem.hdr, jk_shmem.size);
  +            close(jk_shmem.fd);
  +        }
  +        else {
  +            int i;
  +            for (i = 0; i < jk_shmem.hdr->allocated; i++) {
  +                if (jk_shmem.hdr->pr[i])
  +                    free(jk_shmem.hdr->pr[i]);
               }
  -            else
  -                free(shm->base);
  +            free(jk_shmem.hdr);
           }
  -        shm->base = NULL;
  -        shm->fd = -1;
       }
  +    jk_shmem.hdr = NULL;
  +    jk_shmem.fd  = -1;
   }
   
   
   #endif
   
  -jk_shm_h_rec_t *jk_shm_header(jk_shm_t *shm)
  +jk_shm_w_rec_t *jk_shm_worker(int id)
   {
  -    if (shm && shm->base) {
  -        return (jk_shm_h_rec_t *)shm->base;
  +    jk_shm_w_rec_t *w = NULL;
  +    if (jk_shmem.hdr && id >= 0) {
  +        if (id < jk_shmem.hdr->allocated) {
  +            if (jk_shmem.fd >= 0)
  +                w = &(jk_shmem.hdr->rr[id]);
  +            else
  +                w = jk_shmem.hdr->pr[id];
  +            if (id != w->id)
  +                w = NULL;            
  +        }
       }
  -    else
  -        return NULL;
  +    return w;
   }
   
  -jk_shm_w_rec_t *jk_shm_worker(jk_shm_t *shm, int id)
  +jk_shm_w_rec_t *jk_shm_worker_alloc()
   {
       jk_shm_w_rec_t *w = NULL;
  -    if (shm && shm->base && id >= 0) {
  -        jk_shm_h_rec_t *hdr = (jk_shm_h_rec_t *)shm->base;
  -        if (id < (hdr->workers + hdr->dynamic)) {
  -            unsigned char *base = (unsigned char *)shm->base;
  -            base += (sizeof(jk_shm_h_rec_t) + id * sizeof(jk_shm_w_rec_t));
  -            w = (jk_shm_w_rec_t *)base;
  -            /* Check worker id */
  -            if (id < hdr->workers && id != w->id)
  -                w = NULL;
  +    if (jk_shmem.hdr) {
  +        if (jk_shmem.hdr->allocated < jk_shmem.hdr->workers) {
  +            jk_shmem.hdr->allocated++;
  +            if (jk_shmem.fd >= 0) {
  +                w = &(jk_shmem.hdr->rr[jk_shmem.hdr->allocated]);
  +            }
  +            else {
  +                jk_shmem.hdr->pr[jk_shmem.hdr->allocated] = calloc(1, sizeof(jk_shm_w_rec_t));
  +                w = jk_shmem.hdr->pr[jk_shmem.hdr->allocated];
  +            }
  +            w->id = jk_shmem.hdr->allocated;
           }
  -
       }
       return w;
   }
  +
  +const char *jk_shm_name()
  +{
  +    return jk_shmem.filename;
  +}
  
  
  
  1.3       +14 -52    jakarta-tomcat-connectors/jk/native/common/jk_shm.h
  
  Index: jk_shm.h
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-connectors/jk/native/common/jk_shm.h,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- jk_shm.h	6 Feb 2005 10:35:55 -0000	1.2
  +++ jk_shm.h	7 Feb 2005 19:06:35 -0000	1.3
  @@ -23,6 +23,7 @@
   #define _JK_SHM_H
   
   #include "jk_global.h"
  +#include "jk_pool.h"
   
   #ifdef __cplusplus
   extern "C"
  @@ -42,41 +43,10 @@
   #define JK_SHM_DYNAMIC  16
   #define JK_SHM_MAGIC    '!', 'J', 'K', 'S', 'H', 'M', JK_SHM_MAJOR, JK_SHM_MINOR
   
  -/* Really huge numbers, but 1024 workers should be enough */
  -#define JK_SHM_MAX_WORKERS  768
  -#define JK_SHM_MAX_DYNAMIC  256
  +/* Really huge numbers, but 512 workers should be enough */
  +#define JK_SHM_MAX_WORKERS  512
   
  -/** jk shm structure */
  -struct jk_shm
  -{
  -    size_t     size;
  -    const char *filename;
  -    int        fd;
  -    void       *base;
  -    int        attached;
  -};
  -
  -typedef struct jk_shm jk_shm_t;
  -
  -/** jk shm header record structure */
  -struct jk_shm_h_rec
  -{
  -    /* Shared memory magic JK_SHM_MAGIC */
  -    char    magic[8];
  -    int     workers;
  -    int     dynamic;
  -
  -    /* Align to 128 bytes */
  -
  -    /* XXX:  the following should be #if sizeof(int) == 4 */
  -#if 1
  -    char    reserved[112];
  -#else
  -    char    reserved[104];
  -#endif
  -};
  -
  -typedef struct jk_shm_h_rec jk_shm_h_rec_t;
  +#define JK_SHM_ALIGN(x)  JK_ALIGN(x, 1024)
   
   /** jk shm worker record structure */
   struct jk_shm_w_rec
  @@ -117,40 +87,32 @@
       int     elected;
       /* Number of non 200 responses */
       int     errors;
  -    /* Align to 512 bytes */
  -
  -    /* XXX:  the following should be #if sizeof(int) == 4 */
  -#if 1
  -    char    reserved[244];
  -#else
  -    char    reserved[168];
  -#endif
  -
   };
  -
   typedef struct jk_shm_w_rec jk_shm_w_rec_t;
   
  +const char *jk_shm_name();
  +
   /* Open the shared memory creating file if needed
    */
  -int jk_shm_open(const char *fname, int workers, int dynamic, jk_shm_t *shm);
  +int jk_shm_open(const char *fname);
   
   /* Close the shared memory
    */
  -void jk_shm_close(jk_shm_t *shm);
  +void jk_shm_close();
   
   /* Attach the shared memory in child process.
    * File has to be opened in parent.
    */
  -int jk_shm_attach(const char *fname, int workers, int dynamic, jk_shm_t *shm);
  +int jk_shm_attach(const char *fname);
   
   
  -/* Return shm header record
  +/* Return shm worker record
    */
  -jk_shm_h_rec_t *jk_shm_header(jk_shm_t *shm);
  +jk_shm_w_rec_t *jk_shm_worker(int id);
   
  -/* Return shm worker record
  +/* allocate shm worker record
    */
  -jk_shm_w_rec_t *jk_shm_worker(jk_shm_t *shm, int id);
  +jk_shm_w_rec_t *jk_shm_alloc();
   
   
   #ifdef __cplusplus
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-dev-help@jakarta.apache.org


Mime
View raw message