Debian Codegraph @

bwa-apache2/bwashm.c

raw
1 #include <sys/types.h>
2 #include <sys/mman.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <limits.h>
6 #include <unistd.h>
7 #include <fcntl.h>
8 #include <errno.h>
9 #include <stdio.h>
10 #include "bwa.h"
11
12 #ifndef PATH_MAX
13 # define PATH_MAX 1024
14 #endif
15
16 int bwa_shm_stage(bwaidx_t *idx, const char *hint, const char *_tmpfn)
17 {
18 const char *name;
19 uint8_t *shm, *shm_idx;
20 uint16_t *cnt;
21 int shmid, to_init = 0, l;
22 char path[PATH_MAX + 1], *tmpfn = (char*)_tmpfn;
23
24 if (hint == 0 || hint[0] == 0) return -1;
25 for (name = hint + strlen(hint) - 1; name >= hint && *name != '/'; --name);
26 ++name;
27
28 if ((shmid = shm_open("/bwactl", O_RDWR, 0)) < 0) {
29 shmid = shm_open("/bwactl", O_CREAT|O_RDWR|O_EXCL, 0644);
30 to_init = 1;
31 }
32 if (shmid < 0) return -1;
33 ftruncate(shmid, BWA_CTL_SIZE);
34 shm = mmap(0, BWA_CTL_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, shmid, 0);
35 cnt = (uint16_t*)shm;
36 if (to_init) {
37 memset(shm, 0, BWA_CTL_SIZE);
38 cnt[1] = 4;
39 }
40
41 if (idx->mem == 0) bwa_idx2mem(idx);
42
43 if (tmpfn) {
44 FILE *fp;
45 if ((fp = fopen(tmpfn, "wb")) != 0) {
46 int64_t rest = idx->l_mem;
47 while (rest > 0) {
48 int64_t l = rest < 0x1000000? rest : 0x1000000;
49 rest -= fwrite(&idx->mem[idx->l_mem - rest], 1, l, fp);
50 }
51 fclose(fp);
52 free(idx->mem); idx->mem = 0;
53 } else {
54 fprintf(stderr, "[W::%s] fail to create the temporary file. Option '-f' is ignored.\n", __func__);
55 tmpfn = 0;
56 }
57 }
58
59 strcat(strcpy(path, "/bwaidx-"), name);
60 if ((shmid = shm_open(path, O_CREAT|O_RDWR|O_EXCL, 0644)) < 0) {
61 shm_unlink(path);
62 perror("shm_open()");
63 return -1;
64 }
65 l = 8 + strlen(name) + 1;
66 if (cnt[1] + l > BWA_CTL_SIZE) return -1;
67 memcpy(shm + cnt[1], &idx->l_mem, 8);
68 memcpy(shm + cnt[1] + 8, name, l - 8);
69 cnt[1] += l; ++cnt[0];
70 ftruncate(shmid, idx->l_mem);
71 shm_idx = mmap(0, idx->l_mem, PROT_READ|PROT_WRITE, MAP_SHARED, shmid, 0);
72 if (tmpfn) {
73 FILE *fp;
74 fp = fopen(tmpfn, "rb");
75 int64_t rest = idx->l_mem;
76 while (rest > 0) {
77 int64_t l = rest < 0x1000000? rest : 0x1000000;
78 rest -= fread(&shm_idx[idx->l_mem - rest], 1, l, fp);
79 }
80 fclose(fp);
81 unlink(tmpfn);
82 } else {
83 memcpy(shm_idx, idx->mem, idx->l_mem);
84 free(idx->mem);
85 }
86 bwa_mem2idx(idx->l_mem, shm_idx, idx);
87 idx->is_shm = 1;
88 return 0;
89 }
90
91 bwaidx_t *bwa_idx_load_from_shm(const char *hint)
92 {
93 const char *name;
94 uint8_t *shm, *shm_idx;
95 uint16_t *cnt, i;
96 char *p, path[PATH_MAX + 1];
97 int shmid;
98 int64_t l_mem;
99 bwaidx_t *idx;
100
101 if (hint == 0 || hint[0] == 0) return 0;
102 for (name = hint + strlen(hint) - 1; name >= hint && *name != '/'; --name);
103 ++name;
104 if ((shmid = shm_open("/bwactl", O_RDONLY, 0)) < 0) return 0;
105 shm = mmap(0, BWA_CTL_SIZE, PROT_READ, MAP_SHARED, shmid, 0);
106 cnt = (uint16_t*)shm;
107 if (cnt[0] == 0) return 0;
108 for (i = 0, p = (char*)(shm + 4); i < cnt[0]; ++i) {
109 memcpy(&l_mem, p, 8); p += 8;
110 if (strcmp(p, name) == 0) break;
111 p += strlen(p) + 1;
112 }
113 if (i == cnt[0]) return 0;
114
115 strcat(strcpy(path, "/bwaidx-"), name);
116 if ((shmid = shm_open(path, O_RDONLY, 0)) < 0) return 0;
117 shm_idx = mmap(0, l_mem, PROT_READ, MAP_SHARED, shmid, 0);
118 idx = calloc(1, sizeof(bwaidx_t));
119 bwa_mem2idx(l_mem, shm_idx, idx);
120 idx->is_shm = 1;
121 return idx;
122 }
123
124 int bwa_shm_test(const char *hint)
125 {
126 int shmid;
127 uint16_t *cnt, i;
128 char *p, *shm;
129 const char *name;
130
131 if (hint == 0 || hint[0] == 0) return 0;
132 for (name = hint + strlen(hint) - 1; name >= hint && *name != '/'; --name);
133 ++name;
134 if ((shmid = shm_open("/bwactl", O_RDONLY, 0)) < 0) return 0;
135 shm = mmap(0, BWA_CTL_SIZE, PROT_READ, MAP_SHARED, shmid, 0);
136 cnt = (uint16_t*)shm;
137 for (i = 0, p = shm + 4; i < cnt[0]; ++i) {
138 if (strcmp(p + 8, name) == 0) return 1;
139 p += strlen(p) + 9;
140 }
141 return 0;
142 }
143
144 int bwa_shm_list(void)
145 {
146 int shmid;
147 uint16_t *cnt, i;
148 char *p, *shm;
149 if ((shmid = shm_open("/bwactl", O_RDONLY, 0)) < 0) return -1;
150 shm = mmap(0, BWA_CTL_SIZE, PROT_READ, MAP_SHARED, shmid, 0);
151 cnt = (uint16_t*)shm;
152 for (i = 0, p = shm + 4; i < cnt[0]; ++i) {
153 int64_t l_mem;
154 memcpy(&l_mem, p, 8); p += 8;
155 printf("%s\t%ld\n", p, (long)l_mem);
156 p += strlen(p) + 1;
157 }
158 return 0;
159 }
160
161 int bwa_shm_destroy(void)
162 {
163 int shmid;
164 uint16_t *cnt, i;
165 char *p, *shm;
166 char path[PATH_MAX + 1];
167
168 if ((shmid = shm_open("/bwactl", O_RDONLY, 0)) < 0) return -1;
169 shm = mmap(0, BWA_CTL_SIZE, PROT_READ, MAP_SHARED, shmid, 0);
170 cnt = (uint16_t*)shm;
171 for (i = 0, p = shm + 4; i < cnt[0]; ++i) {
172 int64_t l_mem;
173 memcpy(&l_mem, p, 8); p += 8;
174 strcat(strcpy(path, "/bwaidx-"), p);
175 shm_unlink(path);
176 p += strlen(p) + 1;
177 }
178 munmap(shm, BWA_CTL_SIZE);
179 shm_unlink("/bwactl");
180 return 0;
181 }
182
183 int main_shm(int argc, char *argv[])
184 {
185 int c, to_list = 0, to_drop = 0, ret = 0;
186 char *tmpfn = 0;
187 while ((c = getopt(argc, argv, "ldf:")) >= 0) {
188 if (c == 'l') to_list = 1;
189 else if (c == 'd') to_drop = 1;
190 else if (c == 'f') tmpfn = optarg;
191 }
192 if (optind == argc && !to_list && !to_drop) {
193 fprintf(stderr, "\nUsage: bwa shm [-d|-l] [-f tmpFile] [idxbase]\n\n");
194 fprintf(stderr, "Options: -d destroy all indices in shared memory\n");
195 fprintf(stderr, " -l list names of indices in shared memory\n");
196 fprintf(stderr, " -f FILE temporary file to reduce peak memory\n\n");
197 return 1;
198 }
199 if (optind < argc && (to_list || to_drop)) {
200 fprintf(stderr, "[E::%s] open -l or -d cannot be used when 'idxbase' is present\n", __func__);
201 return 1;
202 }
203 if (optind < argc) {
204 if (bwa_shm_test(argv[optind]) == 0) {
205 bwaidx_t *idx;
206 idx = bwa_idx_load_from_disk(argv[optind], BWA_IDX_ALL);
207 if (bwa_shm_stage(idx, argv[optind], tmpfn) < 0) {
208 fprintf(stderr, "[E::%s] failed to stage the index in shared memory\n", __func__);
209 ret = 1;
210 }
211 bwa_idx_destroy(idx);
212 } else fprintf(stderr, "[M::%s] index '%s' is already in shared memory\n", __func__, argv[optind]);
213 }
214 if (to_list) bwa_shm_list();
215 if (to_drop) bwa_shm_destroy();
216 return ret;
217 }