xref: /titanic_44/usr/src/uts/common/fs/zfs/dmu_zfetch.c (revision 9df12a23948bd40cbe37ce88d84e272c3894e675)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/zfs_context.h>
30 #include <sys/dnode.h>
31 #include <sys/dmu_objset.h>
32 #include <sys/dmu_zfetch.h>
33 #include <sys/dmu.h>
34 #include <sys/dbuf.h>
35 
36 /*
37  * I'm against tune-ables, but these should probably exist as tweakable globals
38  * until we can get this working the way we want it to.
39  */
40 
41 /* max # of streams per zfetch */
42 uint32_t	zfetch_max_streams = 8;
43 /* min time before stream reclaim */
44 uint32_t	zfetch_min_sec_reap = 2;
45 /* max number of blocks to fetch at a time */
46 uint32_t	zfetch_block_cap = 32;
47 /* number of bytes in a array_read at which we stop prefetching (1Mb) */
48 uint64_t	zfetch_array_rd_sz = 1024 * 1024;
49 
50 /* forward decls for static routines */
51 static int		dmu_zfetch_colinear(zfetch_t *, zstream_t *);
52 static void		dmu_zfetch_dofetch(zfetch_t *, zstream_t *);
53 static uint64_t		dmu_zfetch_fetch(dnode_t *, uint64_t, uint64_t);
54 static uint64_t		dmu_zfetch_fetchsz(dnode_t *, uint64_t, uint64_t);
55 static int		dmu_zfetch_find(zfetch_t *, zstream_t *);
56 static int		dmu_zfetch_stream_insert(zfetch_t *, zstream_t *);
57 static zstream_t	*dmu_zfetch_stream_reclaim(zfetch_t *);
58 static void		dmu_zfetch_stream_remove(zfetch_t *, zstream_t *);
59 static void		dmu_zfetch_stream_update(zfetch_t *, zstream_t *);
60 static int		dmu_zfetch_streams_equal(zstream_t *, zstream_t *);
61 
62 
63 /*
64  * Given a zfetch structure and a zstream structure, determine whether the
65  * blocks to be read are part of a co-linear to a pair of existing prefetch
66  * streams.  If a set is found, coalesce the streams, removing one, and
67  * configure the prefetch so it looks for a strided access pattern.
68  *
69  * If no co-linear streams are found, return NULL.
70  */
71 static int
72 dmu_zfetch_colinear(zfetch_t *zf, zstream_t *zh)
73 {
74 	zstream_t	*z_walk;
75 	zstream_t	*z_comp;
76 
77 	if (! rw_tryenter(&zf->zf_rwlock, RW_WRITER))
78 		return (0);
79 
80 	if (zh == NULL) {
81 		rw_exit(&zf->zf_rwlock);
82 		return (0);
83 	}
84 
85 	for (z_walk = list_head(&zf->zf_stream); z_walk;
86 	    z_walk = list_next(&zf->zf_stream, z_walk)) {
87 		for (z_comp = list_next(&zf->zf_stream, z_walk); z_comp;
88 		    z_comp = list_next(&zf->zf_stream, z_comp)) {
89 			int64_t		diff;
90 
91 			if (z_walk->zst_len != z_walk->zst_stride ||
92 			    z_comp->zst_len != z_comp->zst_stride) {
93 				continue;
94 			}
95 
96 			diff = z_comp->zst_offset - z_walk->zst_offset;
97 			if (z_comp->zst_offset + diff == zh->zst_offset) {
98 				z_walk->zst_offset = zh->zst_offset;
99 				z_walk->zst_direction = diff < 0 ? -1 : 1;
100 				z_walk->zst_stride =
101 				    diff * z_walk->zst_direction;
102 				z_walk->zst_ph_offset =
103 				    zh->zst_offset + z_walk->zst_stride;
104 				dmu_zfetch_stream_remove(zf, z_comp);
105 				mutex_destroy(&z_comp->zst_lock);
106 				kmem_free(z_comp, sizeof (zstream_t));
107 
108 				dmu_zfetch_dofetch(zf, z_walk);
109 
110 				rw_exit(&zf->zf_rwlock);
111 				return (1);
112 			}
113 
114 			diff = z_walk->zst_offset - z_comp->zst_offset;
115 			if (z_walk->zst_offset + diff == zh->zst_offset) {
116 				z_walk->zst_offset = zh->zst_offset;
117 				z_walk->zst_direction = diff < 0 ? -1 : 1;
118 				z_walk->zst_stride =
119 				    diff * z_walk->zst_direction;
120 				z_walk->zst_ph_offset =
121 				    zh->zst_offset + z_walk->zst_stride;
122 				dmu_zfetch_stream_remove(zf, z_comp);
123 				mutex_destroy(&z_comp->zst_lock);
124 				kmem_free(z_comp, sizeof (zstream_t));
125 
126 				dmu_zfetch_dofetch(zf, z_walk);
127 
128 				rw_exit(&zf->zf_rwlock);
129 				return (1);
130 			}
131 		}
132 	}
133 
134 	rw_exit(&zf->zf_rwlock);
135 	return (0);
136 }
137 
138 /*
139  * Given a zstream_t, determine the bounds of the prefetch.  Then call the
140  * routine that actually prefetches the individual blocks.
141  */
142 static void
143 dmu_zfetch_dofetch(zfetch_t *zf, zstream_t *zs)
144 {
145 	uint64_t	prefetch_tail;
146 	uint64_t	prefetch_limit;
147 	uint64_t	prefetch_ofst;
148 	uint64_t	prefetch_len;
149 	uint64_t	blocks_fetched;
150 
151 	zs->zst_stride = MAX((int64_t)zs->zst_stride, zs->zst_len);
152 	zs->zst_cap = MIN(zfetch_block_cap, 2 * zs->zst_cap);
153 
154 	prefetch_tail = MAX((int64_t)zs->zst_ph_offset,
155 	    (int64_t)(zs->zst_offset + zs->zst_stride));
156 	/*
157 	 * XXX: use a faster division method?
158 	 */
159 	prefetch_limit = zs->zst_offset + zs->zst_len +
160 	    (zs->zst_cap * zs->zst_stride) / zs->zst_len;
161 
162 	while (prefetch_tail < prefetch_limit) {
163 		prefetch_ofst = zs->zst_offset + zs->zst_direction *
164 		    (prefetch_tail - zs->zst_offset);
165 
166 		prefetch_len = zs->zst_len;
167 
168 		/*
169 		 * Don't prefetch beyond the end of the file, if working
170 		 * backwards.
171 		 */
172 		if ((zs->zst_direction == ZFETCH_BACKWARD) &&
173 		    (prefetch_ofst > prefetch_tail)) {
174 			prefetch_len += prefetch_ofst;
175 			prefetch_ofst = 0;
176 		}
177 
178 		/* don't prefetch more than we're supposed to */
179 		if (prefetch_len > zs->zst_len)
180 			break;
181 
182 		blocks_fetched = dmu_zfetch_fetch(zf->zf_dnode,
183 		    prefetch_ofst, zs->zst_len);
184 
185 		prefetch_tail += zs->zst_stride;
186 		/* stop if we've run out of stuff to prefetch */
187 		if (blocks_fetched < zs->zst_len)
188 			break;
189 	}
190 	zs->zst_ph_offset = prefetch_tail;
191 	zs->zst_last = lbolt;
192 }
193 
194 /*
195  * This takes a pointer to a zfetch structure and a dnode.  It performs the
196  * necessary setup for the zfetch structure, grokking data from the
197  * associated dnode.
198  */
199 void
200 dmu_zfetch_init(zfetch_t *zf, dnode_t *dno)
201 {
202 	if (zf == NULL) {
203 		return;
204 	}
205 
206 	zf->zf_dnode = dno;
207 	zf->zf_stream_cnt = 0;
208 	zf->zf_alloc_fail = 0;
209 
210 	list_create(&zf->zf_stream, sizeof (zstream_t),
211 	    offsetof(zstream_t, zst_node));
212 
213 	rw_init(&zf->zf_rwlock, NULL, RW_DEFAULT, NULL);
214 }
215 
216 /*
217  * This function computes the actual size, in blocks, that can be prefetched,
218  * and fetches it.
219  */
220 static uint64_t
221 dmu_zfetch_fetch(dnode_t *dn, uint64_t blkid, uint64_t nblks)
222 {
223 	uint64_t	fetchsz;
224 	uint64_t	i;
225 
226 	fetchsz = dmu_zfetch_fetchsz(dn, blkid, nblks);
227 
228 	for (i = 0; i < fetchsz; i++) {
229 		dbuf_prefetch(dn, blkid + i);
230 	}
231 
232 	return (fetchsz);
233 }
234 
235 /*
236  * this function returns the number of blocks that would be prefetched, based
237  * upon the supplied dnode, blockid, and nblks.  This is used so that we can
238  * update streams in place, and then prefetch with their old value after the
239  * fact.  This way, we can delay the prefetch, but subsequent accesses to the
240  * stream won't result in the same data being prefetched multiple times.
241  */
242 static uint64_t
243 dmu_zfetch_fetchsz(dnode_t *dn, uint64_t blkid, uint64_t nblks)
244 {
245 	uint64_t	fetchsz;
246 
247 	if (blkid > dn->dn_maxblkid) {
248 		return (0);
249 	}
250 
251 	/* compute fetch size */
252 	if (blkid + nblks > dn->dn_maxblkid) {
253 		fetchsz = dn->dn_maxblkid - blkid;
254 		ASSERT(blkid + fetchsz <= dn->dn_maxblkid);
255 	} else {
256 		fetchsz = nblks;
257 	}
258 
259 
260 	return (fetchsz);
261 }
262 
263 /*
264  * given a zfetch and a zsearch structure, see if there is an associated zstream
265  * for this block read.  If so, it starts a prefetch for the stream it
266  * located and returns true, otherwise it returns false
267  */
268 static int
269 dmu_zfetch_find(zfetch_t *zf, zstream_t *zh)
270 {
271 	zstream_t	*zs;
272 	int64_t		diff;
273 	int		rc = 0;
274 
275 	if (zh == NULL)
276 		return (0);
277 
278 	/*
279 	 * XXX: This locking strategy is a bit coarse; however, it's impact has
280 	 * yet to be tested.  If this turns out to be an issue, it can be
281 	 * modified in a number of different ways.
282 	 */
283 
284 	rw_enter(&zf->zf_rwlock, RW_READER);
285 top:
286 
287 	for (zs = list_head(&zf->zf_stream); zs;
288 	    zs = list_next(&zf->zf_stream, zs)) {
289 
290 
291 		if (zs->zst_len == 0) {
292 			/* bogus stream */
293 			continue;
294 		}
295 
296 		if (zh->zst_offset - zs->zst_offset < zs->zst_len) {
297 			/* already fetched */
298 			rw_exit(&zf->zf_rwlock);
299 			return (1);
300 		}
301 
302 		if (zh->zst_offset == zs->zst_offset + zs->zst_len) {
303 			/* forward sequential access */
304 
305 			mutex_enter(&zs->zst_lock);
306 
307 			if (zh->zst_offset != zs->zst_offset + zs->zst_len) {
308 				mutex_exit(&zs->zst_lock);
309 				goto top;
310 			}
311 
312 			zs->zst_len += zh->zst_len;
313 			diff = zs->zst_len - zfetch_block_cap;
314 			if (diff > 0) {
315 				zs->zst_offset += diff;
316 				zs->zst_len = zs->zst_len > diff ?
317 				    zs->zst_len - diff : 0;
318 			}
319 			zs->zst_direction = ZFETCH_FORWARD;
320 
321 			break;
322 
323 		} else if (zh->zst_offset == zs->zst_offset - zh->zst_len) {
324 			/* backwards sequential access */
325 
326 			mutex_enter(&zs->zst_lock);
327 
328 			if (zh->zst_offset != zs->zst_offset - zh->zst_len) {
329 				mutex_exit(&zs->zst_lock);
330 				goto top;
331 			}
332 
333 			zs->zst_offset = zs->zst_offset > zh->zst_len ?
334 			    zs->zst_offset - zh->zst_len : 0;
335 			zs->zst_ph_offset = zs->zst_ph_offset > zh->zst_len ?
336 			    zs->zst_ph_offset - zh->zst_len : 0;
337 			zs->zst_len += zh->zst_len;
338 
339 			diff = zs->zst_len - zfetch_block_cap;
340 			if (diff > 0) {
341 				zs->zst_ph_offset = zs->zst_ph_offset > diff ?
342 				    zs->zst_ph_offset - diff : 0;
343 				zs->zst_len = zs->zst_len > diff ?
344 				    zs->zst_len - diff : zs->zst_len;
345 			}
346 			zs->zst_direction = ZFETCH_BACKWARD;
347 
348 			break;
349 
350 		} else if ((zh->zst_offset - zs->zst_offset - zs->zst_stride <
351 		    zs->zst_len) && (zs->zst_len != zs->zst_stride)) {
352 			/* strided forward access */
353 
354 			mutex_enter(&zs->zst_lock);
355 
356 			if ((zh->zst_offset - zs->zst_offset - zs->zst_stride >=
357 			    zs->zst_len) || (zs->zst_len == zs->zst_stride)) {
358 				mutex_exit(&zs->zst_lock);
359 				goto top;
360 			}
361 
362 			zs->zst_offset += zs->zst_stride;
363 			zs->zst_direction = ZFETCH_FORWARD;
364 
365 			break;
366 
367 		} else if ((zh->zst_offset - zs->zst_offset + zs->zst_stride <
368 		    zs->zst_len) && (zs->zst_len != zs->zst_stride)) {
369 			/* strided reverse access */
370 
371 			mutex_enter(&zs->zst_lock);
372 
373 			if ((zh->zst_offset - zs->zst_offset + zs->zst_stride >=
374 			    zs->zst_len) || (zs->zst_len == zs->zst_stride)) {
375 				mutex_exit(&zs->zst_lock);
376 				goto top;
377 			}
378 
379 			zs->zst_offset = zs->zst_offset > zs->zst_stride ?
380 			    zs->zst_offset - zs->zst_stride : 0;
381 			zs->zst_ph_offset = (zs->zst_ph_offset >
382 			    (2 * zs->zst_stride)) ?
383 			    (zs->zst_ph_offset - (2 * zs->zst_stride)) : 0;
384 			zs->zst_direction = ZFETCH_BACKWARD;
385 
386 			break;
387 		}
388 	}
389 
390 	if (zs) {
391 		rc = 1;
392 		dmu_zfetch_dofetch(zf, zs);
393 		mutex_exit(&zs->zst_lock);
394 	}
395 
396 	rw_exit(&zf->zf_rwlock);
397 	return (rc);
398 }
399 
400 /*
401  * Clean-up state associated with a zfetch structure.  This frees allocated
402  * structure members, empties the zf_stream tree, and generally makes things
403  * nice.  This doesn't free the zfetch_t itself, that's left to the caller.
404  */
405 void
406 dmu_zfetch_rele(zfetch_t *zf)
407 {
408 	zstream_t	*zs;
409 	zstream_t	*zs_next;
410 
411 	ASSERT(!RW_LOCK_HELD(&zf->zf_rwlock));
412 
413 	for (zs = list_head(&zf->zf_stream); zs; zs = zs_next) {
414 		zs_next = list_next(&zf->zf_stream, zs);
415 
416 		list_remove(&zf->zf_stream, zs);
417 		mutex_destroy(&zs->zst_lock);
418 		kmem_free(zs, sizeof (zstream_t));
419 	}
420 	list_destroy(&zf->zf_stream);
421 	rw_destroy(&zf->zf_rwlock);
422 
423 	zf->zf_dnode = NULL;
424 }
425 
426 /*
427  * Given a zfetch and zstream structure, insert the zstream structure into the
428  * AVL tree contained within the zfetch structure.  Peform the appropriate
429  * book-keeping.  It is possible that another thread has inserted a stream which
430  * matches one that we are about to insert, so we must be sure to check for this
431  * case.  If one is found, return failure, and let the caller cleanup the
432  * duplicates.
433  */
434 static int
435 dmu_zfetch_stream_insert(zfetch_t *zf, zstream_t *zs)
436 {
437 	zstream_t	*zs_walk;
438 	zstream_t	*zs_next;
439 
440 	ASSERT(RW_WRITE_HELD(&zf->zf_rwlock));
441 
442 	for (zs_walk = list_head(&zf->zf_stream); zs_walk; zs_walk = zs_next) {
443 		zs_next = list_next(&zf->zf_stream, zs_walk);
444 
445 		if (dmu_zfetch_streams_equal(zs_walk, zs)) {
446 		    return (0);
447 		}
448 	}
449 
450 	list_insert_head(&zf->zf_stream, zs);
451 	zf->zf_stream_cnt++;
452 
453 	return (1);
454 }
455 
456 
457 /*
458  * Walk the list of zstreams in the given zfetch, find an old one (by time), and
459  * reclaim it for use by the caller.
460  */
461 static zstream_t *
462 dmu_zfetch_stream_reclaim(zfetch_t *zf)
463 {
464 	zstream_t	*zs;
465 
466 	if (! rw_tryenter(&zf->zf_rwlock, RW_WRITER))
467 		return (0);
468 
469 	for (zs = list_head(&zf->zf_stream); zs;
470 	    zs = list_next(&zf->zf_stream, zs)) {
471 
472 		if (((lbolt - zs->zst_last) / hz) > zfetch_min_sec_reap)
473 			break;
474 	}
475 
476 	if (zs) {
477 		dmu_zfetch_stream_remove(zf, zs);
478 		mutex_destroy(&zs->zst_lock);
479 		bzero(zs, sizeof (zstream_t));
480 	} else {
481 		zf->zf_alloc_fail++;
482 	}
483 	rw_exit(&zf->zf_rwlock);
484 
485 	return (zs);
486 }
487 
488 /*
489  * Given a zfetch and zstream structure, remove the zstream structure from its
490  * container in the zfetch structure.  Perform the appropriate book-keeping.
491  */
492 static void
493 dmu_zfetch_stream_remove(zfetch_t *zf, zstream_t *zs)
494 {
495 	ASSERT(RW_WRITE_HELD(&zf->zf_rwlock));
496 
497 	list_remove(&zf->zf_stream, zs);
498 	zf->zf_stream_cnt--;
499 }
500 
501 static int
502 dmu_zfetch_streams_equal(zstream_t *zs1, zstream_t *zs2)
503 {
504 	if (zs1->zst_offset != zs2->zst_offset)
505 		return (0);
506 
507 	if (zs1->zst_len != zs2->zst_len)
508 		return (0);
509 
510 	if (zs1->zst_stride != zs2->zst_stride)
511 		return (0);
512 
513 	if (zs1->zst_ph_offset != zs2->zst_ph_offset)
514 		return (0);
515 
516 	if (zs1->zst_cap != zs2->zst_cap)
517 		return (0);
518 
519 	if (zs1->zst_direction != zs2->zst_direction)
520 		return (0);
521 
522 	return (1);
523 }
524 
525 /*
526  * This is the prefetch entry point.  It calls all of the other dmu_zfetch
527  * routines to create, delete, find, or operate upon prefetch streams.
528  */
529 void
530 dmu_zfetch(zfetch_t *zf, uint64_t offset, uint64_t size)
531 {
532 	zstream_t	zst;
533 	zstream_t	*newstream;
534 	int		fetched;
535 	int		inserted;
536 	unsigned int	blkshft;
537 	uint64_t	blksz;
538 
539 	/* files that aren't ln2 blocksz are only one block -- nothing to do */
540 	if (!zf->zf_dnode->dn_datablkshift) {
541 		return;
542 	}
543 
544 	/* convert offset and size, into blockid and nblocks */
545 	blkshft = zf->zf_dnode->dn_datablkshift;
546 	blksz = (1 << blkshft);
547 
548 	bzero(&zst, sizeof (zstream_t));
549 	zst.zst_offset = offset >> blkshft;
550 	zst.zst_len = (P2ROUNDUP(offset + size, blksz) -
551 	    P2ALIGN(offset, blksz)) >> blkshft;
552 
553 	fetched = dmu_zfetch_find(zf, &zst);
554 	if (!fetched) {
555 		fetched = dmu_zfetch_colinear(zf, &zst);
556 	}
557 
558 	if (!fetched) {
559 		newstream = dmu_zfetch_stream_reclaim(zf);
560 
561 		/*
562 		 * we still couldn't find a stream, drop the lock, and allocate
563 		 * one if possible.  Otherwise, give up and go home.
564 		 */
565 		if (newstream == NULL) {
566 			uint64_t	maxblocks;
567 			uint32_t	max_streams;
568 			uint32_t	cur_streams;
569 
570 			cur_streams = zf->zf_stream_cnt;
571 			maxblocks = zf->zf_dnode->dn_maxblkid;
572 
573 			max_streams = MIN(zfetch_max_streams,
574 			    (maxblocks / zfetch_block_cap));
575 			if (max_streams == 0) {
576 				max_streams++;
577 			}
578 
579 			if (cur_streams >= max_streams) {
580 				return;
581 			}
582 
583 			newstream = kmem_zalloc(sizeof (zstream_t), KM_SLEEP);
584 		}
585 
586 		newstream->zst_offset = zst.zst_offset;
587 		newstream->zst_len = zst.zst_len;
588 		newstream->zst_stride = zst.zst_len;
589 		newstream->zst_ph_offset = zst.zst_len + zst.zst_offset;
590 		newstream->zst_cap = zst.zst_len;
591 		newstream->zst_direction = ZFETCH_FORWARD;
592 		newstream->zst_last = lbolt;
593 
594 		mutex_init(&newstream->zst_lock, NULL, MUTEX_DEFAULT, NULL);
595 
596 		rw_enter(&zf->zf_rwlock, RW_WRITER);
597 		inserted = dmu_zfetch_stream_insert(zf, newstream);
598 		rw_exit(&zf->zf_rwlock);
599 
600 		if (!inserted) {
601 			mutex_destroy(&newstream->zst_lock);
602 			kmem_free(newstream, sizeof (zstream_t));
603 		}
604 	}
605 }
606