Skip to content

Commit

Permalink
feat(kstat): enable kstat print
Browse files Browse the repository at this point in the history
Signed-off-by: sundengyu <[email protected]>
  • Loading branch information
sundengyu committed Sep 27, 2024
1 parent 12728a2 commit b8f619b
Show file tree
Hide file tree
Showing 14 changed files with 448 additions and 23 deletions.
6 changes: 4 additions & 2 deletions include/libuzfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ extern void libuzfs_set_ops(const coroutine_ops_t *,
const co_mutex_ops_t *, const co_cond_ops_t *,
const co_rwlock_ops_t *, const aio_ops_t *,
const thread_ops_t *, const taskq_ops_t *,
void (*print_log)(const char *, int));
const stat_ops_t *);

// only have effect when in debug binary
extern void libuzfs_enable_debug_msg(void);
Expand Down Expand Up @@ -132,7 +132,7 @@ extern int libuzfs_zpool_prop_get(libuzfs_zpool_handle_t *zhp,
extern int libuzfs_dataset_create(const char *dsname);
extern void libuzfs_dataset_destroy(const char *dsname);
extern libuzfs_dataset_handle_t *libuzfs_dataset_open(const char *dsname,
int *err, uint32_t dnodesize, uint32_t max_blksz);
int *err, uint32_t dnodesize, uint32_t max_blksz, const void *metrics);
extern void libuzfs_dataset_close(libuzfs_dataset_handle_t *dhp);

extern uint64_t libuzfs_dataset_get_superblock_ino(
Expand Down Expand Up @@ -295,6 +295,8 @@ extern int libuzfs_object_next_block(libuzfs_inode_handle_t *ihp,
uint64_t *offset, uint64_t *size);

extern void libuzfs_debug_main(int argc, char **argv);

extern void libuzfs_show_stats(void *, int, const seq_file_generator_t *);
#ifdef __cplusplus
}
#endif
Expand Down
18 changes: 16 additions & 2 deletions include/sync_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,11 @@ typedef struct aio_ops {
typedef uint64_t uthread_create_func_t(void (*)(void *), void *, int);
typedef void uthread_exit_func_t(void);
typedef void uthread_join_func_t(uint64_t);
typedef void backtrace_func_t(void);

typedef struct thread_ops {
uthread_create_func_t *uthread_create;
uthread_exit_func_t *uthread_exit;
uthread_join_func_t *uthread_join;
backtrace_func_t *backtrace;
} thread_ops_t;

typedef void *taskq_create_func_t(const char *, int);
Expand Down Expand Up @@ -161,4 +159,20 @@ typedef struct taskq_ops {
taskq_nalloc_func_t *taskq_nalloc;
} taskq_ops_t;

typedef void print_log_func_t(const char *, int);
typedef void kstat_install_func_t(const char *, void *, int);
typedef void kstat_uninstall_func_t(const char *);
typedef void backtrace_func_t(void);
typedef void record_txg_delay_func_t(const void *, int, uint64_t);
typedef void record_zio_func_t(const void *, const int64_t *, int);

typedef struct stat_ops {
print_log_func_t *print_log;
kstat_install_func_t *kstat_install;
kstat_uninstall_func_t *kstat_uinstall;
backtrace_func_t *backtrace;
record_txg_delay_func_t *record_txg_delays;
record_zio_func_t *record_zio;
} stat_ops_t;

#endif
1 change: 1 addition & 0 deletions include/sys/spa_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ struct spa {
zfs_refcount_t spa_refcount; /* number of opens */

taskq_t *spa_upgrade_taskq; /* taskq for upgrade jobs */
const void *metrics; /* pointers point to metrics */
};

extern char *spa_config_path;
Expand Down
1 change: 1 addition & 0 deletions include/sys/vdev_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ struct vdev {
zfs_ratelimit_t vdev_delay_rl;
zfs_ratelimit_t vdev_deadman_rl;
zfs_ratelimit_t vdev_checksum_rl;
const void *metrics;
};

#define VDEV_PAD_SIZE (8 << 10)
Expand Down
23 changes: 22 additions & 1 deletion include/sys/zfs_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -416,16 +416,26 @@ extern void kstat_set_raw_ops(kstat_t *ksp,
* procfs list manipulation
*/

struct procfs_list;
struct seq_file;

typedef struct procfs_list {
void *pl_private;
kmutex_t pl_lock;
list_t pl_list;
uint64_t pl_next_id;
size_t pl_node_offset;
char name[KSTAT_STRLEN];
int (*show)(struct seq_file *, void *);
int (*show_header)(struct seq_file *);
int (*clear)(struct procfs_list *);
} procfs_list_t;

#ifndef __cplusplus
struct seq_file { };
struct seq_file {
char *buf;
int size;
};
void seq_printf(struct seq_file *m, const char *fmt, ...);

typedef struct procfs_list_node {
Expand All @@ -447,6 +457,17 @@ void procfs_list_destroy(procfs_list_t *procfs_list);
void procfs_list_add(procfs_list_t *procfs_list, void *p);
#endif

#define KSTAT_PROCFS 1
#define KSTAT_NORMAL 2

typedef struct seq_file_generator {
void (*generate)(void *, struct seq_file *);
void *arg;
} seq_file_generator_t;

void show_procfs_content(procfs_list_t *, const seq_file_generator_t *);
void show_kstat_content(kstat_t *, const seq_file_generator_t *);

/*
* Task queues
*/
Expand Down
2 changes: 2 additions & 0 deletions include/sys/zio.h
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,8 @@ struct zio {
metaslab_class_t *io_metaslab_class; /* dva throttle class */

uint64_t io_offset;
/* time each io stage start */
int64_t io_stage_start[ZIO_PIPELINE_STAGES];
hrtime_t io_timestamp; /* submitted at */
hrtime_t io_queued_timestamp;
hrtime_t io_target_timestamp;
Expand Down
2 changes: 2 additions & 0 deletions include/sys/zio_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ enum zio_stage {
ZIO_STAGE_DONE = 1 << 24 /* RWFCI */
};

#define ZIO_PIPELINE_STAGES 25

#define ZIO_INTERLOCK_STAGES \
(ZIO_STAGE_READY | \
ZIO_STAGE_DONE)
Expand Down
3 changes: 2 additions & 1 deletion lib/libspl/assert.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
int aok = 0;

extern thread_ops_t thread_ops;
extern stat_ops_t stat_ops;

/* printf version of libspl_assert */
void
Expand All @@ -46,7 +47,7 @@ libspl_assertf(const char *file, const char *func, int line,
return;
}

thread_ops.backtrace();
stat_ops.backtrace();

abort();
}
28 changes: 28 additions & 0 deletions lib/libspl/include/sys/kstat.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ typedef int kid_t; /* unique kstat id */

#define KSTAT_STRLEN 255 /* 254 chars + NULL; must be 16 * n - 1 */


#ifdef _KERNEL
/*
* The generic kstat header
*/
Expand Down Expand Up @@ -91,6 +93,32 @@ typedef struct kstat {
int (*ks_snapshot)(struct kstat *, void *, int);
void *ks_lock; /* protects this kstat's data */
} kstat_t;
#else

struct kstat;

typedef struct kstat_raw_ops {
int (*headers)(char *buf, size_t size);
int (*data)(char *buf, size_t size, void *data);
void *(*addr)(struct kstat *ksp, loff_t index);
} kstat_raw_ops_t;

typedef struct kstat {
char ks_name[KSTAT_STRLEN]; /* kstat name */
uchar_t ks_type; /* kstat data type */
uchar_t ks_flags; /* kstat flags */
void *ks_data; /* kstat type-specific data */
uint_t ks_ndata; /* # of type-specific data records */
size_t ks_data_size; /* total size of kstat data section */
kstat_raw_ops_t raw_ops;
/*
* Fields relevant to kernel only
*/
int (*ks_update)(struct kstat *, int); /* dynamic update */
void *ks_private; /* arbitrary provider-private data */
void *ks_lock; /* protects this kstat's data */
} kstat_t;
#endif

#ifdef _SYSCALL32

Expand Down
32 changes: 27 additions & 5 deletions lib/libuzfs/libuzfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,14 +278,14 @@ fatal(int do_perror, char *message, ...)
}

extern aio_ops_t aio_ops;
extern void (*print_log)(const char *, int);
extern stat_ops_t stat_ops;

void
libuzfs_set_ops(const coroutine_ops_t *co,
const co_mutex_ops_t *mo, const co_cond_ops_t *condo,
const co_rwlock_ops_t *ro, const aio_ops_t *ao,
const thread_ops_t *tho, const taskq_ops_t *tqo,
void (*pl)(const char *, int))
const stat_ops_t *sto)
{
co_ops = *co;
co_mutex_ops = *mo;
Expand All @@ -294,7 +294,23 @@ libuzfs_set_ops(const coroutine_ops_t *co,
aio_ops = *ao;
thread_ops = *tho;
taskq_ops = *tqo;
print_log = pl;
stat_ops = *sto;
}

void
libuzfs_show_stats(void *stat, int type,
const seq_file_generator_t *generator)
{
switch (type) {
case KSTAT_PROCFS:
show_procfs_content(stat, generator);
break;
case KSTAT_NORMAL:
show_kstat_content(stat, generator);
break;
default:
panic("unknown stat type: %d\n", type);
}
}

static uint64_t
Expand Down Expand Up @@ -1338,8 +1354,8 @@ libuzfs_dhp_fini(libuzfs_dataset_handle_t *dhp)
}

libuzfs_dataset_handle_t *
libuzfs_dataset_open(const char *dsname, int *err,
uint32_t dnodesize, uint32_t max_blksz)
libuzfs_dataset_open(const char *dsname, int *err, uint32_t dnodesize,
uint32_t max_blksz, const void *metrics)
{
libuzfs_dataset_handle_t *dhp = NULL;
objset_t *os = NULL;
Expand All @@ -1354,6 +1370,12 @@ libuzfs_dataset_open(const char *dsname, int *err,
return (NULL);
}

if (metrics != NULL) {
os->os_spa->metrics = metrics;
vdev_t *root_vdev = os->os_spa->spa_root_vdev;
root_vdev->metrics = metrics;
}

libuzfs_dhp_init(dhp, os, dnodesize);

return (dhp);
Expand Down
Loading

0 comments on commit b8f619b

Please sign in to comment.