Debian Codegraph @

src/mpi/romio/adio/ad_lustre/ad_lustre_lock.c

raw symbols SCIP
1 /*
2 * Copyright (C) by Argonne National Laboratory
3 * See COPYRIGHT in top-level directory
4 */
5
6 #include "adio.h"
7
8 #include "ad_lustre.h"
9
10 #ifdef HAVE_UNISTD_H
11 #include <unistd.h>
12 #endif
13
14 #include <stdint.h>
15
16 /* If necessary (older luster client headers) define the new
17 locking structures. */
18
19
20
21
22
23
24 //#define LOCK_AHEAD_DEBUG
25
26 #ifndef LL_IOC_LADVISE
27 #define LL_IOC_LADVISE _IOR('f', 250, struct llapi_lu_ladvise)
28
29 enum lu_ladvise_type {
30 LU_LADVISE_INVALID = 0,
31 LU_LADVISE_WILLREAD = 1,
32 LU_LADVISE_DONTNEED = 2,
33 LU_LADVISE_LOCKNOEXPAND = 3,
34 LU_LADVISE_LOCKAHEAD = 4,
35 LU_LADVISE_MAX
36 };
37
38 #define LU_LADVISE_NAMES { \
39 [LU_LADVISE_WILLREAD] = "willread", \
40 [LU_LADVISE_DONTNEED] = "dontneed", \
41 [LU_LADVISE_LOCKNOEXPAND] = "locknoexpand", \
42 [LU_LADVISE_LOCKAHEAD] = "lockahead", \
43 }
44
45 /* This is the userspace argument for ladvise. It is currently the same as
46 * what goes on the wire (struct lu_ladvise), but is defined separately as we
47 * may need info which is only used locally. */
48 struct llapi_lu_ladvise {
49 __u16 lla_advice; /* advice type */
50 __u16 lla_value1; /* values for different advice types */
51 __u32 lla_value2;
52 __u64 lla_start; /* first byte of extent for advice */
53 __u64 lla_end; /* last byte of extent for advice */
54 __u32 lla_value3;
55 __u32 lla_value4;
56 };
57 enum ladvise_flag {
58 LF_ASYNC = 0x00000001,
59 LF_UNSET = 0x00000002,
60 /* For lock requests */
61 LF_NONBLOCK = 0x00000003,
62 };
63
64 #define LADVISE_MAGIC 0x1ADF1CE0
65 /* Masks of valid flags for each advice */
66 #define LF_LOCKNOEXPAND_MASK LF_UNSET
67 #define LF_LOCKAHEAD_MASK LF_NONBLOCK
68 /* Flags valid for all advices not explicitly specified */
69 #define LF_DEFAULT_MASK LF_ASYNC
70 /* All flags */
71 #define LF_MASK (LF_ASYNC | LF_UNSET | LF_NONBLOCK)
72
73 #define lla_lockahead_mode lla_value1
74 #define lla_peradvice_flags lla_value2
75 #define lla_lockahead_result lla_value3
76
77 /* This is the userspace argument for ladvise, corresponds to ladvise_hdr which
78 * is used on the wire. It is defined separately as we may need info which is
79 * only used locally. */
80 struct llapi_ladvise_hdr {
81 __u32 lah_magic; /* LADVISE_MAGIC */
82 __u32 lah_count; /* number of advices */
83 __u64 lah_flags; /* from enum ladvise_flag */
84 __u32 lah_value1; /* unused */
85 __u32 lah_value2; /* unused */
86 __u64 lah_value3; /* unused */
87 struct llapi_lu_ladvise lah_advise[0]; /* advices in this header */
88 };
89
90 #define LAH_COUNT_MAX (1024)
91
92 enum lock_mode_user {
93 MODE_READ_USER = 1,
94 MODE_WRITE_USER,
95 MODE_MAX_USER,
96 };
97
98 #define LOCK_MODE_NAMES { \
99 [MODE_READ_USER] = "READ",\
100 [MODE_WRITE_USER] = "WRITE"\
101 }
102
103 enum lockahead_results {
104 LLA_RESULT_SENT = 0,
105 LLA_RESULT_DIFFERENT,
106 LLA_RESULT_SAME,
107 };
108 #endif
109
110
111 int llapi_ladvise_lock(ADIO_File fd, unsigned long long flags, int num_advise,
112 ADIO_Offset * offset, int stripe_size, int num_extents,
113 ADIO_Offset step_size)
114 {
115 struct llapi_ladvise_hdr *ladvise_hdr;
116 int rc;
117 int i;
118 enum lock_mode_user mode = 0;
119
120 if (num_advise < 1 || num_advise >= LAH_COUNT_MAX) {
121 errno = EINVAL;
122 /*llapi_error(LLAPI_MSG_ERROR, -EINVAL,
123 * "bad advice number %d", num_advise); */
124 return -1;
125 }
126
127 ladvise_hdr =
128 ADIOI_Malloc(sizeof(struct llapi_ladvise_hdr) +
129 sizeof(struct llapi_lu_ladvise) * num_advise);
130
131 if (ladvise_hdr == NULL) {
132 errno = ENOMEM;
133 //llapi_error(LLAPI_MSG_ERROR, -ENOMEM, "not enough memory");
134 return -1;
135 }
136 ladvise_hdr->lah_magic = LADVISE_MAGIC;
137 ladvise_hdr->lah_count = num_advise;
138 ladvise_hdr->lah_flags = flags & LF_MASK;
139 ladvise_hdr->lah_value1 = 0;
140 ladvise_hdr->lah_value2 = 0;
141 ladvise_hdr->lah_value3 = 0;
142
143 if (fd->hints->fs_hints.lustre.lock_ahead_write)
144 mode = MODE_WRITE_USER;
145 else if (fd->hints->fs_hints.lustre.lock_ahead_read) /* read only */
146 mode = MODE_READ_USER;
147 else
148 MPI_Abort(MPI_COMM_WORLD, 1);
149
150 for (i = 0; i < num_extents; ++i) {
151 ladvise_hdr->lah_advise[i].lla_advice = LU_LADVISE_LOCKAHEAD;
152 ladvise_hdr->lah_advise[i].lla_lockahead_mode = mode;
153 ladvise_hdr->lah_advise[i].lla_peradvice_flags = flags | LF_ASYNC;
154 ladvise_hdr->lah_advise[i].lla_start = *offset;
155 ladvise_hdr->lah_advise[i].lla_end = *offset + stripe_size - 1;;
156 ladvise_hdr->lah_advise[i].lla_value3 = 0;
157 ladvise_hdr->lah_advise[i].lla_value4 = 0;
158 ladvise_hdr->lah_advise[i].lla_lockahead_result = 0;
159 *offset += step_size;
160 }
161
162
163 rc = ioctl(fd->fd_sys, LL_IOC_LADVISE, ladvise_hdr);
164
165 if (rc < 0) {
166 ADIOI_Free(ladvise_hdr);
167 //llapi_error(LLAPI_MSG_ERROR, -errno, "cannot give advice");
168 return -1;
169 }
170
171
172 /* Simply save the new start/end extents, forget what we already had locked
173 * since lustre may reclaim it at any time. */
174 fd->hints->fs_hints.lustre.lock_ahead_start_extent = ladvise_hdr->lah_advise[0].lla_start;
175 fd->hints->fs_hints.lustre.lock_ahead_end_extent =
176 ladvise_hdr->lah_advise[num_extents - 1].lla_end;
177
178
179 #ifdef LOCK_AHEAD_DEBUG
180 /* Print any per extent errors */
181 for (i = 0; i < num_extents; ++i) {
182 if (ladvise_hdr->lah_advise[i].lla_lockahead_result) {
183 fprintf(stderr, "%s(%d) "
184 "lock ahead extent[%4.4d] {%ld,%ld} stripe {%ld,%ld} error %d\n",
185 __func__, __LINE__,
186 i,
187 (long int) ladvise_hdr->lah_advise[i].lla_start,
188 (long int) ladvise_hdr->lah_advise[i].lla_end,
189 (long int) ladvise_hdr->lah_advise[i].lla_start / stripe_size,
190 (long int) ladvise_hdr->lah_advise[i].lla_end / stripe_size,
191 ladvise_hdr->lah_advise[i].lla_lockahead_result);
192 }
193 }
194
195 #endif
196 ADIOI_Free(ladvise_hdr);
197
198 return 0;
199 }
200
201
202
203
204
205
206
207 /* Set lustre locks to only lock the requested byte range, do not
208 extend any locks to 'infinity' which is the normal behavior.
209 This will enhance 'lock ahead' extent locking, which we do not
210 want to auto-extend. */
211 int ADIOI_LUSTRE_request_only_lock_ioctl(ADIO_File fd)
212 {
213 int err = 0;
214
215 struct llapi_ladvise_hdr *noexpand_hdr;
216 noexpand_hdr = ADIOI_Malloc(sizeof(struct llapi_ladvise_hdr) + sizeof(struct llapi_lu_ladvise));
217 if (!noexpand_hdr) {
218 err = -ENOMEM;
219 goto out;
220 }
221
222 noexpand_hdr->lah_magic = LADVISE_MAGIC;
223 noexpand_hdr->lah_count = 1;
224 noexpand_hdr->lah_flags = 0;
225 noexpand_hdr->lah_value1 = 0;
226 noexpand_hdr->lah_value2 = 0;
227 noexpand_hdr->lah_value3 = 0;
228 noexpand_hdr->lah_advise[0].lla_advice = LU_LADVISE_LOCKNOEXPAND;
229 noexpand_hdr->lah_advise[0].lla_peradvice_flags = 0;
230
231 noexpand_hdr->lah_advise[0].lla_value1 = 0;
232 noexpand_hdr->lah_advise[0].lla_start = 0;
233 noexpand_hdr->lah_advise[0].lla_end = 0;
234 noexpand_hdr->lah_advise[0].lla_value3 = 0;
235 noexpand_hdr->lah_advise[0].lla_value4 = 0;
236
237 int rc = ioctl(fd->fd_sys, LL_IOC_LADVISE, noexpand_hdr);
238 if (rc < 0) {
239 ADIOI_Free(noexpand_hdr);
240 //llapi_error(LLAPI_MSG_ERROR, -errno, "cannot give advice");
241 return -1;
242 }
243
244 ADIOI_Free(noexpand_hdr);
245
246
247 out:
248
249 return err;
250 }
251
252 /* Use group locks to 'clear' existing locks on the file
253 before attempting 'lock ahead' extent locking. */
254 int ADIOI_LUSTRE_clear_locks(ADIO_File fd)
255 {
256 int err = 0;
257 int id;
258
259 if (!fd->my_cb_nodes_index) {
260 srand(time(NULL));
261 id = rand();
262 err = ioctl(fd->fd_sys, LL_IOC_GROUP_LOCK, id);
263 err = ioctl(fd->fd_sys, LL_IOC_GROUP_UNLOCK, id);
264 }
265 return err;
266 }
267
268 /* Lock a predefined series of 'extents' in the file.
269 The intent is to match the aggregator locking pattern. */
270 void ADIOI_LUSTRE_lock_ahead_ioctl(ADIO_File fd, int avail_cb_nodes, ADIO_Offset next_offset,
271 int *error_code)
272 {
273
274 int err = 0;
275 int num_extents = fd->hints->fs_hints.lustre.lock_ahead_num_extents;
276 int flags = fd->hints->fs_hints.lustre.lock_ahead_flags;
277 ADIO_Offset offset = 0, step_size = 0;
278 int stripe_size = fd->hints->striping_unit;
279
280 int agg_idx = fd->my_cb_nodes_index;
281
282 /* Not a collective aggregator? Do nothing and return
283 * since current code is based on aggregator/stripes */
284 if (agg_idx < 0) {
285 /* Disable further lock ahead ...
286 * fd->hints->fs_hints.lustre.lock_ahead_read = 0;
287 * fd->hints->fs_hints.lustre.lock_ahead_write = 0;
288 * fd->hints->fs_hints.lustre.lock_ahead_start_extent = 0;
289 * fd->hints->fs_hints.lustre.lock_ahead_end_extent = INT64_MAX;
290 */
291 return;
292 }
293 #ifdef LOCK_AHEAD_DEBUG
294 {
295 /* Debug check. Calculate the expected rank for this stripe */
296 int rank_index;
297 rank_index = (int) ((next_offset / stripe_size) % avail_cb_nodes);
298 /* Not sure why, but this happens in the generic read coll?
299 * It doesn't do the aggregation striped quite as expected.
300 * We'll probably lock the wrong stripes for this read ...
301 * but we're more interested in write locks than read locks
302 * so stick with the lustre specific calculations for now.
303 * Consider dropping read support if performance isn't improved
304 * or ad_lustre doesn't add read coll code.
305 */
306 if (agg_idx != rank_index) {
307 fprintf(stderr, "%s(%d) rank[%d] file system %d "
308 "lock ahead debug R(%d)/W(%d), "
309 "aggregator %d(%d)/%d(%d), "
310 "offset %lld, start offset %lld, stripe %lld "
311 "num_extents %d\n",
312 __func__, __LINE__,
313 fd->hints->ranklist[agg_idx],
314 fd->file_system,
315 fd->hints->fs_hints.lustre.lock_ahead_read,
316 fd->hints->fs_hints.lustre.lock_ahead_write,
317 agg_idx, rank_index,
318 avail_cb_nodes, fd->hints->cb_nodes,
319 (long long) next_offset, (long long) (next_offset / stripe_size * stripe_size),
320 (long long) next_offset / stripe_size, num_extents);
321 }
322 /* Just checking the config vs what was passed in */
323 if (agg_idx >= avail_cb_nodes) {
324 fprintf(stderr, "%s(%d) file system %d "
325 "lock ahead debug R(%d)/W(%d), "
326 "aggregator %d(%d)/%d(%d), "
327 "num_extents %d\n",
328 __func__, __LINE__, fd->file_system,
329 fd->hints->fs_hints.lustre.lock_ahead_read,
330 fd->hints->fs_hints.lustre.lock_ahead_write,
331 agg_idx, rank_index, avail_cb_nodes, fd->hints->cb_nodes, num_extents);
332 MPI_Abort(MPI_COMM_WORLD, 1);
333 }
334 }
335 #endif
336
337 /* Check file access vs requested lock ahead */
338 if (fd->access_mode & ADIO_RDONLY) {
339 /* Don't need write lock ahead */
340 fd->hints->fs_hints.lustre.lock_ahead_write = 0;
341
342 /* Do need read lock ahead or give up. */
343 if (!(fd->hints->fs_hints.lustre.lock_ahead_read)) {
344 fd->hints->fs_hints.lustre.lock_ahead_start_extent = 0;
345 fd->hints->fs_hints.lustre.lock_ahead_end_extent = INT64_MAX;
346 return;
347 }
348 }
349 if (fd->access_mode & ADIO_WRONLY) {
350 /* Don't need read lock ahead */
351 fd->hints->fs_hints.lustre.lock_ahead_read = 0;
352
353 /* Do need write lock ahead or give up. */
354 if (!(fd->hints->fs_hints.lustre.lock_ahead_write)) {
355 fd->hints->fs_hints.lustre.lock_ahead_start_extent = 0;
356 fd->hints->fs_hints.lustre.lock_ahead_end_extent = INT64_MAX;
357 return;
358 }
359 }
360
361
362 step_size = (ADIO_Offset) avail_cb_nodes *stripe_size;
363
364 if (next_offset == 0) { /* 1st call, calculate our starting offset */
365 offset = (ADIO_Offset) agg_idx *stripe_size;
366 } else /* Have to assume we're writing to one of our stripes */
367 offset = next_offset / stripe_size * stripe_size; /* start of stripe */
368
369 err = llapi_ladvise_lock(fd, flags, num_extents, &offset, stripe_size, num_extents, step_size);
370
371
372 if (err == -1) { /* turn off lock ahead after a failure */
373 #ifdef LOCK_AHEAD_DEBUG
374 fprintf(stderr, "%s(%d) file system %d "
375 "lock ahead failure R(%d)/W(%d), "
376 "aggregator %d/%d, "
377 "next offset %lld, stripe %lld, "
378 "last offset %lld, stripe %lld, "
379 "step %lld, stripe size %lld "
380 "num_extents %d\n",
381 __func__, __LINE__, fd->file_system,
382 fd->hints->fs_hints.lustre.lock_ahead_read,
383 fd->hints->fs_hints.lustre.lock_ahead_write,
384 agg_idx,
385 avail_cb_nodes,
386 (long long) next_offset, (long long) next_offset / stripe_size,
387 (long long) offset, (long long) offset / stripe_size,
388 (long long) step_size, (long long) stripe_size, num_extents);
389 #endif
390 fd->hints->fs_hints.lustre.lock_ahead_read = 0;
391 fd->hints->fs_hints.lustre.lock_ahead_write = 0;
392 fd->hints->fs_hints.lustre.lock_ahead_start_extent = 0;
393 fd->hints->fs_hints.lustre.lock_ahead_end_extent = INT64_MAX;
394
395 *error_code = ADIOI_Err_create_code("ADIOI_LUSTRE_lock_ahead_ioctl", fd->filename, errno);
396 if (agg_idx == 0) {
397 fprintf(stderr, "%s: ioctl(LL_IOC_LADVISE) \'%s\'\n", __func__, strerror(errno));
398 }
399 /* Note: it's too late to turn off 'request only' locking, which
400 * could affect performance without also having 'lock ahead'.
401 *
402 * We expect lustre to support this (turning it off) later */
403 }
404
405
406 return;
407 }