de-prometheus the stats, add more metaclasses

This commit is contained in:
Michael Bishop 2022-10-20 05:32:38 -03:00
parent bbec9ab76d
commit 309be3a05c
2 changed files with 78 additions and 42 deletions

View File

@ -902,7 +902,7 @@ typedef struct spa_stats {
spa_history_kstat_t state; /* pool state */ spa_history_kstat_t state; /* pool state */
spa_history_kstat_t guid; /* pool guid */ spa_history_kstat_t guid; /* pool guid */
spa_history_kstat_t iostats; spa_history_kstat_t iostats;
spa_history_kstat_t fragmentation; spa_history_kstat_t fragmentation[5];
} spa_stats_t; } spa_stats_t;
typedef enum txg_state { typedef enum txg_state {

View File

@ -303,67 +303,103 @@ spa_txg_history_init(spa_t *spa)
offsetof(spa_txg_history_t, sth_node)); offsetof(spa_txg_history_t, sth_node));
} }
typedef struct {
spa_t *spa;
int metaclass;
} freespace_histogram_stat_t;
static void * static void *
spa_frag_addr(kstat_t *ksp, loff_t n) spa_frag_addr(kstat_t *ksp, loff_t n)
{ {
if (n == 0) if (n == 0) {
return ksp->ks_private; freespace_histogram_stat_t *stat = ksp->ks_private;
return NULL; switch (stat->metaclass) {
case 0:
return (spa_normal_class(stat->spa));
case 1:
return (spa_log_class(stat->spa));
case 2:
return (spa_embedded_log_class(stat->spa));
case 3:
return (spa_special_class(stat->spa));
case 4:
return (spa_dedup_class(stat->spa));
}
}
return (NULL);
} }
static int static int
spa_frag_data(char *buf, size_t size, void *data) { spa_frag_data(char *buf, size_t size, void *data)
spa_t *spa = (spa_t *)data; {
metaslab_class_t *mc = data;
size_t offset = 0; size_t offset = 0;
const char *name = spa_name(spa); int res;
// without this, it becomes a data-leak, and un-initialized strings
// can be returned if all buckets are 0
buf[0] = 0;
metaslab_class_t *mc = spa_normal_class(spa); for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) {
for (int i=0; i<RANGE_TREE_HISTOGRAM_SIZE; i++) {
if (mc->mc_histogram[i] > 0) { if (mc->mc_histogram[i] > 0) {
int res = snprintf(buf + offset, size, "zfs_fragmentation_normal{power=\"%d\",pool=\"%s\"} %llu\n", i, name, (u_longlong_t)mc->mc_histogram[i]); res = snprintf(buf + offset, size, "%d %llu\n", i,
(u_longlong_t)mc->mc_histogram[i]);
offset += res; offset += res;
size -= res; size -= res;
} }
} }
metaslab_class_t *smc = spa_special_class(spa); return (0);
for (int i=0; i<RANGE_TREE_HISTOGRAM_SIZE; i++) {
if (smc->mc_histogram[i] > 0) {
int res = snprintf(buf + offset, size, "zfs_fragmentation_special{power=\"%d\",pool=\"%s\"} %llu\n", i, name, (u_longlong_t)smc->mc_histogram[i]);
offset += res;
size -= res;
}
}
return 0;
} }
static void static void
spa_fragmentation_init(spa_t *spa) spa_fragmentation_init(spa_t *spa)
{ {
char *name; char *dirname;
spa_history_kstat_t *shk = &spa->spa_stats.fragmentation; const char *statnames[] = {
"fragmentation_normal",
"fragmentation_log",
"fragmentation_embedded_log",
"fragmentation_special",
"fragmentation_dedup"
};
for (int i = 0; i < 5; i++) {
spa_history_kstat_t *shk = &spa->spa_stats.fragmentation[i];
kstat_t *ksp; kstat_t *ksp;
name = kmem_asprintf("zfs/%s", spa_name(spa)); dirname = kmem_asprintf("zfs/%s", spa_name(spa));
ksp = kstat_create(name, 0, "fragmentation", "misc", KSTAT_TYPE_RAW, 0, KSTAT_FLAG_VIRTUAL); ksp = kstat_create(dirname, 0, statnames[i], "misc",
KSTAT_TYPE_RAW, 0, KSTAT_FLAG_VIRTUAL);
kmem_strfree(dirname);
shk->kstat = ksp; shk->kstat = ksp;
if (ksp) { if (ksp) {
ksp->ks_private = spa; freespace_histogram_stat_t *stat = kmem_zalloc(
sizeof (freespace_histogram_stat_t), KM_SLEEP);
stat->spa = spa;
stat->metaclass = i;
ksp->ks_private = stat;
ksp->ks_flags |= KSTAT_FLAG_NO_HEADERS; ksp->ks_flags |= KSTAT_FLAG_NO_HEADERS;
kstat_set_raw_ops(ksp, NULL, spa_frag_data, spa_frag_addr); kstat_set_raw_ops(ksp, NULL, spa_frag_data,
spa_frag_addr);
kstat_install(ksp); kstat_install(ksp);
} }
kmem_strfree(name); }
} }
static void static void
spa_fragmentation_destroy(spa_t *spa) spa_fragmentation_destroy(spa_t *spa)
{ {
spa_history_kstat_t *shk = &spa->spa_stats.fragmentation; for (int i = 0; i < 5; i++) {
spa_history_kstat_t *shk = &spa->spa_stats.fragmentation[i];
kstat_t *ksp = shk->kstat; kstat_t *ksp = shk->kstat;
if (ksp) if (ksp) {
if (ksp->ks_private) {
kmem_free(ksp->ks_private,
sizeof (freespace_histogram_stat_t));
}
kstat_delete(ksp); kstat_delete(ksp);
}
}
} }
static void static void