Skip to content

Commit 0cee240

Browse files
pjdbehlendorf
authored andcommitted
Speed up 'zfs list -t snapshot -o name -s name'
FreeBSD #xxx: Dramatically optimize listing snapshots when user requests only snapshot names and wants to sort them by name, ie. when executes: # zfs list -t snapshot -o name -s name Because only name is needed we don't have to read all snapshot properties. Below you can find how long does it take to list 34509 snapshots from a single disk pool before and after this change with cold and warm cache: before: # time zfs list -t snapshot -o name -s name > /dev/null cold cache: 525s warm cache: 218s after: # time zfs list -t snapshot -o name -s name > /dev/null cold cache: 1.7s warm cache: 1.1s NOTE: This patch only appears in FreeBSD. If/when Illumos picks up the change we may want to drop this patch and adopt their version. However, for now this addresses a real issue. Ported-by: Brian Behlendorf <[email protected]> Issue #450
1 parent 74497b7 commit 0cee240

File tree

8 files changed

+85
-18
lines changed

8 files changed

+85
-18
lines changed

cmd/zfs/zfs_iter.c

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*/
2121
/*
2222
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23+
* Copyright (c) 2012 Pawel Jakub Dawidek <[email protected]>.
2324
*/
2425

2526
#include <libintl.h>
@@ -129,8 +130,11 @@ zfs_callback(zfs_handle_t *zhp, void *data)
129130
cb->cb_depth++;
130131
if (zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM)
131132
(void) zfs_iter_filesystems(zhp, zfs_callback, data);
132-
if ((zfs_get_type(zhp) != ZFS_TYPE_SNAPSHOT) && include_snaps)
133-
(void) zfs_iter_snapshots(zhp, zfs_callback, data);
133+
if ((zfs_get_type(zhp) != ZFS_TYPE_SNAPSHOT) && include_snaps) {
134+
(void) zfs_iter_snapshots(zhp,
135+
(cb->cb_flags & ZFS_ITER_SIMPLE) != 0, zfs_callback,
136+
data);
137+
}
134138
cb->cb_depth--;
135139
}
136140

@@ -184,6 +188,13 @@ zfs_free_sort_columns(zfs_sort_column_t *sc)
184188
}
185189
}
186190

191+
int
192+
zfs_sort_only_by_name(const zfs_sort_column_t *sc)
193+
{
194+
return (sc != NULL && sc->sc_next == NULL &&
195+
sc->sc_prop == ZFS_PROP_NAME);
196+
}
197+
187198
/* ARGSUSED */
188199
static int
189200
zfs_compare(const void *larg, const void *rarg, void *unused)
@@ -224,7 +235,13 @@ zfs_compare(const void *larg, const void *rarg, void *unused)
224235
lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
225236
rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
226237

227-
if (lcreate < rcreate)
238+
/*
239+
* Both lcreate and rcreate being 0 means we don't have
240+
* properties and we should compare full name.
241+
*/
242+
if (lcreate == 0 && rcreate == 0)
243+
ret = strcmp(lat + 1, rat + 1);
244+
else if (lcreate < rcreate)
228245
ret = -1;
229246
else if (lcreate > rcreate)
230247
ret = 1;
@@ -290,7 +307,14 @@ zfs_sort(const void *larg, const void *rarg, void *data)
290307
if (rvalid)
291308
verify(nvlist_lookup_string(rval,
292309
ZPROP_VALUE, &rstr) == 0);
310+
} else if (psc->sc_prop == ZFS_PROP_NAME) {
311+
lvalid = rvalid = B_TRUE;
312+
313+
(void) strlcpy(lbuf, zfs_get_name(l), sizeof(lbuf));
314+
(void) strlcpy(rbuf, zfs_get_name(r), sizeof(rbuf));
293315

316+
lstr = lbuf;
317+
rstr = rbuf;
294318
} else if (zfs_prop_is_string(psc->sc_prop)) {
295319
lvalid = (zfs_prop_get(l, psc->sc_prop, lbuf,
296320
sizeof (lbuf), NULL, NULL, 0, B_TRUE) == 0);

cmd/zfs/zfs_iter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,13 @@ typedef struct zfs_sort_column {
4343
#define ZFS_ITER_PROP_LISTSNAPS (1 << 2)
4444
#define ZFS_ITER_DEPTH_LIMIT (1 << 3)
4545
#define ZFS_ITER_RECVD_PROPS (1 << 4)
46+
#define ZFS_ITER_SIMPLE (1 << 5)
4647

4748
int zfs_for_each(int, char **, int options, zfs_type_t,
4849
zfs_sort_column_t *, zprop_list_t **, int, zfs_iter_f, void *);
4950
int zfs_add_sort_column(zfs_sort_column_t **, const char *, boolean_t);
5051
void zfs_free_sort_columns(zfs_sort_column_t *);
52+
int zfs_sort_only_by_name(const zfs_sort_column_t *);
5153

5254
#ifdef __cplusplus
5355
}

cmd/zfs/zfs_main.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2650,7 +2650,12 @@ print_dataset(zfs_handle_t *zhp, zprop_list_t *pl, boolean_t scripted)
26502650
first = B_FALSE;
26512651
}
26522652

2653-
if (pl->pl_prop != ZPROP_INVAL) {
2653+
if (pl->pl_prop == ZFS_PROP_NAME) {
2654+
(void) strlcpy(property, zfs_get_name(zhp),
2655+
sizeof(property));
2656+
propstr = property;
2657+
right_justify = zfs_prop_align_right(pl->pl_prop);
2658+
} else if (pl->pl_prop != ZPROP_INVAL) {
26542659
if (zfs_prop_get(zhp, pl->pl_prop, property,
26552660
sizeof (property), NULL, NULL, 0, B_FALSE) != 0)
26562661
propstr = "-";
@@ -2810,6 +2815,13 @@ zfs_do_list(int argc, char **argv)
28102815
if (fields == NULL)
28112816
fields = default_fields;
28122817

2818+
/*
2819+
* If we are only going to list snapshot names and sort by name,
2820+
* then we can use faster version.
2821+
*/
2822+
if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
2823+
flags |= ZFS_ITER_SIMPLE;
2824+
28132825
/*
28142826
* If "-o space" and no types were specified, don't display snapshots.
28152827
*/

include/libzfs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ extern int zfs_iter_root(libzfs_handle_t *, zfs_iter_f, void *);
516516
extern int zfs_iter_children(zfs_handle_t *, zfs_iter_f, void *);
517517
extern int zfs_iter_dependents(zfs_handle_t *, boolean_t, zfs_iter_f, void *);
518518
extern int zfs_iter_filesystems(zfs_handle_t *, zfs_iter_f, void *);
519-
extern int zfs_iter_snapshots(zfs_handle_t *, zfs_iter_f, void *);
519+
extern int zfs_iter_snapshots(zfs_handle_t *, boolean_t, zfs_iter_f, void *);
520520
extern int zfs_iter_snapshots_sorted(zfs_handle_t *, zfs_iter_f, void *);
521521

522522
typedef struct get_all_cb {

include/sys/zfs_ioctl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,8 @@ typedef struct zfs_cmd {
286286
boolean_t zc_temphold;
287287
uint64_t zc_action_handle;
288288
int zc_cleanup_fd;
289-
uint8_t zc_pad[4]; /* alignment */
289+
uint8_t zc_simple;
290+
uint8_t zc_pad[3]; /* alignment */
290291
uint64_t zc_sendobj;
291292
uint64_t zc_fromobj;
292293
uint64_t zc_createtxg;

lib/libzfs/libzfs_dataset.c

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
2424
* Copyright (c) 2010 Nexenta Systems, Inc. All rights reserved.
2525
* Copyright (c) 2011 by Delphix. All rights reserved.
26+
* Copyright (c) 2012 Pawel Jakub Dawidek <[email protected]>.
2627
*/
2728

2829
#include <ctype.h>
@@ -480,6 +481,23 @@ make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
480481
return (zhp);
481482
}
482483

484+
static zfs_handle_t *
485+
make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc)
486+
{
487+
zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
488+
489+
if (zhp == NULL)
490+
return (NULL);
491+
492+
zhp->zfs_hdl = pzhp->zfs_hdl;
493+
(void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
494+
zhp->zfs_head_type = pzhp->zfs_type;
495+
zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
496+
zhp->zpool_hdl = zpool_handle(zhp);
497+
498+
return (zhp);
499+
}
500+
483501
/*
484502
* Opens the given snapshot, filesystem, or volume. The 'types'
485503
* argument is a mask of acceptable types. The function will print an
@@ -2518,7 +2536,8 @@ zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
25182536
* Iterate over all snapshots
25192537
*/
25202538
int
2521-
zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2539+
zfs_iter_snapshots(zfs_handle_t *zhp, boolean_t simple, zfs_iter_f func,
2540+
void *data)
25222541
{
25232542
zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
25242543
zfs_handle_t *nzhp;
@@ -2527,15 +2546,19 @@ zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data)
25272546
if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
25282547
return (0);
25292548

2549+
zc.zc_simple = simple;
2550+
25302551
if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
25312552
return (-1);
25322553
while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT,
25332554
&zc)) == 0) {
25342555

2535-
if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
2536-
&zc)) == NULL) {
2556+
if (simple)
2557+
nzhp = make_dataset_simple_handle_zc(zhp, &zc);
2558+
else
2559+
nzhp = make_dataset_handle_zc(zhp->zfs_hdl, &zc);
2560+
if (nzhp == NULL)
25372561
continue;
2538-
}
25392562

25402563
if ((ret = func(nzhp, data)) != 0) {
25412564
zcmd_free_nvlists(&zc);
@@ -2557,7 +2580,7 @@ zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
25572580
if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
25582581
return (ret);
25592582

2560-
return (zfs_iter_snapshots(zhp, func, data));
2583+
return (zfs_iter_snapshots(zhp, B_FALSE, func, data));
25612584
}
25622585

25632586
/*
@@ -3287,7 +3310,7 @@ zfs_promote(zfs_handle_t *zhp)
32873310
return (-1);
32883311
(void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint,
32893312
sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE);
3290-
ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd);
3313+
ret = zfs_iter_snapshots(pzhp, B_FALSE, promote_snap_cb, &pd);
32913314
if (ret != 0) {
32923315
zfs_close(pzhp);
32933316
return (-1);
@@ -3302,7 +3325,8 @@ zfs_promote(zfs_handle_t *zhp)
33023325
if (ret != 0) {
33033326
int save_errno = errno;
33043327

3305-
(void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd);
3328+
(void) zfs_iter_snapshots(pzhp, B_FALSE, promote_snap_done_cb,
3329+
&pd);
33063330
zfs_close(pzhp);
33073331

33083332
switch (save_errno) {
@@ -3321,7 +3345,8 @@ zfs_promote(zfs_handle_t *zhp)
33213345
return (zfs_standard_error(hdl, save_errno, errbuf));
33223346
}
33233347
} else {
3324-
(void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd);
3348+
(void) zfs_iter_snapshots(zhp, B_FALSE, promote_snap_done_cb,
3349+
&pd);
33253350
}
33263351

33273352
zfs_close(pzhp);

lib/libzfs/libzfs_sendrecv.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
/*
2323
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24+
* Copyright (c) 2012 Pawel Jakub Dawidek <[email protected]>.
25+
* All rights reserved
2426
*/
2527

2628
#include <assert.h>
@@ -717,7 +719,7 @@ send_iterate_fs(zfs_handle_t *zhp, void *arg)
717719
sd->parent_fromsnap_guid = 0;
718720
VERIFY(0 == nvlist_alloc(&sd->parent_snaps, NV_UNIQUE_NAME, 0));
719721
VERIFY(0 == nvlist_alloc(&sd->snapprops, NV_UNIQUE_NAME, 0));
720-
(void) zfs_iter_snapshots(zhp, send_iterate_snap, sd);
722+
(void) zfs_iter_snapshots(zhp, B_FALSE, send_iterate_snap, sd);
721723
VERIFY(0 == nvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps));
722724
VERIFY(0 == nvlist_add_nvlist(nvfs, "snapprops", sd->snapprops));
723725
nvlist_free(sd->parent_snaps);
@@ -843,7 +845,7 @@ zfs_iter_snapshots_sorted(zfs_handle_t *zhp, zfs_iter_f callback, void *data)
843845
avl_create(&avl, zfs_snapshot_compare,
844846
sizeof (zfs_node_t), offsetof(zfs_node_t, zn_avlnode));
845847

846-
ret = zfs_iter_snapshots(zhp, zfs_sort_snaps, &avl);
848+
ret = zfs_iter_snapshots(zhp, B_FALSE, zfs_sort_snaps, &avl);
847849

848850
for (node = avl_first(&avl); node != NULL; node = AVL_NEXT(&avl, node))
849851
ret |= callback(node->zn_handle, data);

module/zfs/zfs_ioctl.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
/*
2222
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
2323
* Portions Copyright 2011 Martin Matuska
24+
* Portions Copyright 2012 Pawel Jakub Dawidek <[email protected]>
2425
* Copyright (c) 2012, Joyent, Inc. All rights reserved.
2526
*/
2627

@@ -1973,7 +1974,7 @@ zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
19731974
int error;
19741975

19751976
top:
1976-
if (zc->zc_cookie == 0)
1977+
if (zc->zc_cookie == 0 && !zc->zc_simple)
19771978
(void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch,
19781979
NULL, DS_FIND_SNAPSHOTS);
19791980

@@ -1995,7 +1996,7 @@ zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
19951996
zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
19961997
NULL);
19971998

1998-
if (error == 0) {
1999+
if (error == 0 && !zc->zc_simple) {
19992000
dsl_dataset_t *ds;
20002001
dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
20012002

0 commit comments

Comments
 (0)