Skip to content

Commit

Permalink
r911: check before attempting adding
Browse files Browse the repository at this point in the history
  • Loading branch information
lh3 committed Oct 17, 2014
1 parent 67c2d69 commit c280274
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
59 changes: 36 additions & 23 deletions bwashm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -118,20 +109,40 @@ 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);
idx->is_shm = 1;
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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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[]);
Expand Down

0 comments on commit c280274

Please sign in to comment.