Skip to content

Commit 2ac9045

Browse files
authored
record ioctl elapsed time in zpool history
Each zfs ioctl that changes on-disk state (e.g. set property, create snapshot, destroy filesystem) is recorded in the zpool history, and is printed by `zpool history -i`. For performance diagnostic purposes, it would be useful to know how long each of these ioctls took to run. This commit adds that functionality, with a new `ZPOOL_HIST_ELAPSED_NS` member of the history nvlist. Additionally, the time recorded in this history log is currently the time that the history record is written to disk. But in many cases (CLI args logging and ioctl logging), this happens asynchronously, potentially many seconds after the operation completed. This commit changes the timestamp to reflect when the history event was created, rather than when it was written to disk. Reviewed-by: Mark Maybee <[email protected]> Reviewed-by: Brian Behlendorf <[email protected]> Signed-off-by: Matthew Ahrens <[email protected]> Closes #11440
1 parent dc303dc commit 2ac9045

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

cmd/zpool/zpool_main.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9063,7 +9063,7 @@ print_history_records(nvlist_t *nvhis, hist_cbdata_t *cb)
90639063
&records, &numrecords) == 0);
90649064
for (i = 0; i < numrecords; i++) {
90659065
nvlist_t *rec = records[i];
9066-
char tbuf[30] = "";
9066+
char tbuf[64] = "";
90679067

90689068
if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
90699069
time_t tsec;
@@ -9075,6 +9075,14 @@ print_history_records(nvlist_t *nvhis, hist_cbdata_t *cb)
90759075
(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
90769076
}
90779077

9078+
if (nvlist_exists(rec, ZPOOL_HIST_ELAPSED_NS)) {
9079+
uint64_t elapsed_ns = fnvlist_lookup_int64(records[i],
9080+
ZPOOL_HIST_ELAPSED_NS);
9081+
(void) snprintf(tbuf + strlen(tbuf),
9082+
sizeof (tbuf) - strlen(tbuf),
9083+
" (%lldms)", (long long)elapsed_ns / 1000 / 1000);
9084+
}
9085+
90789086
if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
90799087
(void) printf("%s %s", tbuf,
90809088
fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));

include/sys/fs/zfs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,6 +1456,7 @@ typedef enum {
14561456
#define ZPOOL_HIST_DSNAME "dsname"
14571457
#define ZPOOL_HIST_DSID "dsid"
14581458
#define ZPOOL_HIST_ERRNO "errno"
1459+
#define ZPOOL_HIST_ELAPSED_NS "elapsed_ns"
14591460

14601461
/*
14611462
* Special nvlist name that will not have its args recorded in the pool's

module/zfs/spa_history.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,6 @@ spa_history_log_sync(void *arg, dmu_tx_t *tx)
288288
}
289289
#endif
290290

291-
fnvlist_add_uint64(nvl, ZPOOL_HIST_TIME, gethrestime_sec());
292291
fnvlist_add_string(nvl, ZPOOL_HIST_HOST, utsname()->nodename);
293292

294293
if (nvlist_exists(nvl, ZPOOL_HIST_CMD)) {
@@ -396,6 +395,12 @@ spa_history_log_nvl(spa_t *spa, nvlist_t *nvl)
396395
}
397396
fnvlist_add_uint64(nvarg, ZPOOL_HIST_WHO, crgetruid(CRED()));
398397

398+
/*
399+
* Since the history is recorded asynchronously, the effective time is
400+
* now, which may be considerably before the change is made on disk.
401+
*/
402+
fnvlist_add_uint64(nvarg, ZPOOL_HIST_TIME, gethrestime_sec());
403+
399404
/* Kick this off asynchronously; errors are ignored. */
400405
dsl_sync_task_nowait(spa_get_dsl(spa), spa_history_log_sync, nvarg, tx);
401406
dmu_tx_commit(tx);
@@ -526,6 +531,7 @@ log_internal(nvlist_t *nvl, const char *operation, spa_t *spa,
526531

527532
fnvlist_add_string(nvl, ZPOOL_HIST_INT_NAME, operation);
528533
fnvlist_add_uint64(nvl, ZPOOL_HIST_TXG, tx->tx_txg);
534+
fnvlist_add_uint64(nvl, ZPOOL_HIST_TIME, gethrestime_sec());
529535

530536
if (dmu_tx_is_syncing(tx)) {
531537
spa_history_log_sync(nvl, tx);

module/zfs/zfs_ioctl.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7416,6 +7416,7 @@ zfsdev_ioctl_common(uint_t vecnum, zfs_cmd_t *zc, int flag)
74167416
size_t saved_poolname_len = 0;
74177417
nvlist_t *innvl = NULL;
74187418
fstrans_cookie_t cookie;
7419+
hrtime_t start_time = gethrtime();
74197420

74207421
cmd = vecnum;
74217422
error = 0;
@@ -7574,6 +7575,8 @@ zfsdev_ioctl_common(uint_t vecnum, zfs_cmd_t *zc, int flag)
75747575
fnvlist_add_int64(lognv, ZPOOL_HIST_ERRNO,
75757576
error);
75767577
}
7578+
fnvlist_add_int64(lognv, ZPOOL_HIST_ELAPSED_NS,
7579+
gethrtime() - start_time);
75777580
(void) spa_history_log_nvl(spa, lognv);
75787581
spa_close(spa, FTAG);
75797582
}

0 commit comments

Comments
 (0)