From c280274331f0b5c839356721545994ebc5bf2040 Mon Sep 17 00:00:00 2001 From: Heng Li Date: Fri, 17 Oct 2014 12:13:09 -0400 Subject: [PATCH 1/2] r911: check before attempting adding --- bwashm.c | 59 ++++++++++++++++++++++++++++++++++---------------------- main.c | 2 +- 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/bwashm.c b/bwashm.c index 4d60350..fe187ff 100644 --- a/bwashm.c +++ b/bwashm.c @@ -13,15 +13,15 @@ int bwa_shm_stage(bwaidx_t *idx, const char *hint, const char *_tmpfn) { const char *name; uint8_t *shm, *shm_idx; - uint16_t *cnt, i; + uint16_t *cnt; int shmid, to_init = 0, l; - char path[PATH_MAX + 1], *p, *tmpfn = (char*)_tmpfn; + char path[PATH_MAX + 1], *tmpfn = (char*)_tmpfn; if (hint == 0 || hint[0] == 0) return -1; for (name = hint + strlen(hint) - 1; name >= hint && *name != '/'; --name); ++name; - if ((shmid = shm_open("/bwactl", O_RDWR, 0444)) < 0) { + if ((shmid = shm_open("/bwactl", O_RDWR, 0)) < 0) { shmid = shm_open("/bwactl", O_CREAT|O_RDWR|O_EXCL, 0644); to_init = 1; } @@ -32,15 +32,6 @@ int bwa_shm_stage(bwaidx_t *idx, const char *hint, const char *_tmpfn) if (to_init) { memset(shm, 0, BWA_CTL_SIZE); cnt[1] = 4; - } else { - for (i = 0, p = (char*)shm + 4; i < cnt[0]; ++i) { - if (strcmp(p + 8, name) == 0) break; - p += 9 + strlen(p + 8); - } - if (i < cnt[0]) { - fprintf(stderr, "[W::%s] index '%s' is already in shared memory\n", __func__, name); - return -1; - } } if (idx->mem == 0) bwa_idx2mem(idx); @@ -106,7 +97,7 @@ bwaidx_t *bwa_idx_load_from_shm(const char *hint) if (hint == 0 || hint[0] == 0) return 0; for (name = hint + strlen(hint) - 1; name >= hint && *name != '/'; --name); ++name; - if ((shmid = shm_open("/bwactl", O_RDONLY, 0444)) < 0) return 0; + if ((shmid = shm_open("/bwactl", O_RDONLY, 0)) < 0) return 0; shm = mmap(0, BWA_CTL_SIZE, PROT_READ, MAP_SHARED, shmid, 0); cnt = (uint16_t*)shm; if (cnt[0] == 0) return 0; @@ -118,7 +109,7 @@ bwaidx_t *bwa_idx_load_from_shm(const char *hint) if (i == cnt[0]) return 0; strcat(strcpy(path, "/bwaidx-"), name); - if ((shmid = shm_open(path, O_RDONLY, 0444)) < 0) return 0; + if ((shmid = shm_open(path, O_RDONLY, 0)) < 0) return 0; shm_idx = mmap(0, l_mem, PROT_READ, MAP_SHARED, shmid, 0); idx = calloc(1, sizeof(bwaidx_t)); bwa_mem2idx(l_mem, shm_idx, idx); @@ -126,12 +117,32 @@ bwaidx_t *bwa_idx_load_from_shm(const char *hint) return idx; } +int bwa_shm_test(const char *hint) +{ + int shmid; + uint16_t *cnt, i; + char *p, *shm; + const char *name; + + if (hint == 0 || hint[0] == 0) return 0; + for (name = hint + strlen(hint) - 1; name >= hint && *name != '/'; --name); + ++name; + if ((shmid = shm_open("/bwactl", O_RDONLY, 0)) < 0) return 0; + shm = mmap(0, BWA_CTL_SIZE, PROT_READ, MAP_SHARED, shmid, 0); + cnt = (uint16_t*)shm; + for (i = 0, p = shm + 4; i < cnt[0]; ++i) { + if (strcmp(p + 8, name) == 0) return 1; + p += strlen(p) + 9; + } + return 0; +} + int bwa_shm_list(void) { int shmid; uint16_t *cnt, i; char *p, *shm; - if ((shmid = shm_open("/bwactl", O_RDONLY, 0444)) < 0) return -1; + if ((shmid = shm_open("/bwactl", O_RDONLY, 0)) < 0) return -1; shm = mmap(0, BWA_CTL_SIZE, PROT_READ, MAP_SHARED, shmid, 0); cnt = (uint16_t*)shm; for (i = 0, p = shm + 4; i < cnt[0]; ++i) { @@ -150,7 +161,7 @@ int bwa_shm_destroy(void) char *p, *shm; char path[PATH_MAX + 1]; - if ((shmid = shm_open("/bwactl", O_RDONLY, 0444)) < 0) return -1; + if ((shmid = shm_open("/bwactl", O_RDONLY, 0)) < 0) return -1; shm = mmap(0, BWA_CTL_SIZE, PROT_READ, MAP_SHARED, shmid, 0); cnt = (uint16_t*)shm; for (i = 0, p = shm + 4; i < cnt[0]; ++i) { @@ -186,13 +197,15 @@ int main_shm(int argc, char *argv[]) return 1; } if (optind < argc) { - bwaidx_t *idx; - idx = bwa_idx_load_from_disk(argv[optind], BWA_IDX_ALL); - if (bwa_shm_stage(idx, argv[optind], tmpfn) < 0) { - fprintf(stderr, "[E::%s] failed to stage the index in shared memory\n", __func__); - ret = 1; - } - bwa_idx_destroy(idx); + if (bwa_shm_test(argv[optind]) == 0) { + bwaidx_t *idx; + idx = bwa_idx_load_from_disk(argv[optind], BWA_IDX_ALL); + if (bwa_shm_stage(idx, argv[optind], tmpfn) < 0) { + fprintf(stderr, "[E::%s] failed to stage the index in shared memory\n", __func__); + ret = 1; + } + bwa_idx_destroy(idx); + } else fprintf(stderr, "[M::%s] index '%s' is already in shared memory\n", __func__, argv[optind]); } if (to_list) bwa_shm_list(); if (to_drop) bwa_shm_destroy(); diff --git a/main.c b/main.c index 313278c..aaf3c98 100644 --- a/main.c +++ b/main.c @@ -4,7 +4,7 @@ #include "utils.h" #ifndef PACKAGE_VERSION -#define PACKAGE_VERSION "0.7.10-r907-dirty" +#define PACKAGE_VERSION "0.7.10-r911-dirty" #endif int bwa_fa2pac(int argc, char *argv[]); From 8af17b34787c7fe35d65598c98a8afd3018c1dbe Mon Sep 17 00:00:00 2001 From: Heng Li Date: Fri, 17 Oct 2014 12:14:18 -0400 Subject: [PATCH 2/2] r912: very minor improvement --- bwashm.c | 8 ++++---- main.c | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/bwashm.c b/bwashm.c index fe187ff..163f764 100644 --- a/bwashm.c +++ b/bwashm.c @@ -53,15 +53,15 @@ int bwa_shm_stage(bwaidx_t *idx, const char *hint, const char *_tmpfn) } strcat(strcpy(path, "/bwaidx-"), name); - l = 8 + strlen(name) + 1; - if (cnt[1] + l > BWA_CTL_SIZE) return -1; - memcpy(shm + cnt[1], &idx->l_mem, 8); - memcpy(shm + cnt[1] + 8, name, l - 8); if ((shmid = shm_open(path, O_CREAT|O_RDWR|O_EXCL, 0644)) < 0) { shm_unlink(path); perror("shm_open()"); return -1; } + l = 8 + strlen(name) + 1; + if (cnt[1] + l > BWA_CTL_SIZE) return -1; + memcpy(shm + cnt[1], &idx->l_mem, 8); + memcpy(shm + cnt[1] + 8, name, l - 8); cnt[1] += l; ++cnt[0]; ftruncate(shmid, idx->l_mem); shm_idx = mmap(0, idx->l_mem, PROT_READ|PROT_WRITE, MAP_SHARED, shmid, 0); diff --git a/main.c b/main.c index aaf3c98..e0f0edb 100644 --- a/main.c +++ b/main.c @@ -4,7 +4,7 @@ #include "utils.h" #ifndef PACKAGE_VERSION -#define PACKAGE_VERSION "0.7.10-r911-dirty" +#define PACKAGE_VERSION "0.7.10-r912-dirty" #endif int bwa_fa2pac(int argc, char *argv[]);