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