summaryrefslogtreecommitdiff
path: root/usr.bin/kstat
diff options
context:
space:
mode:
authorDavid Gwynne <dlg@cvs.openbsd.org>2020-08-11 01:07:48 +0000
committerDavid Gwynne <dlg@cvs.openbsd.org>2020-08-11 01:07:48 +0000
commit21e9c0e9c8e98abe3f3863a105c4463b9512b238 (patch)
tree42081f1cb12f5958c3a8eb4c24a8282c6cd03024 /usr.bin/kstat
parent3826caa6222d888102b763cf2a3fec21ec6f1b3f (diff)
add -w so kstat can update and print stats at a specified wait interval.
Diffstat (limited to 'usr.bin/kstat')
-rw-r--r--usr.bin/kstat/kstat.c51
1 files changed, 47 insertions, 4 deletions
diff --git a/usr.bin/kstat/kstat.c b/usr.bin/kstat/kstat.c
index b2920031f58..8ec9c37a921 100644
--- a/usr.bin/kstat/kstat.c
+++ b/usr.bin/kstat/kstat.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: kstat.c,v 1.4 2020/08/10 06:51:40 dlg Exp $ */
+/* $OpenBSD: kstat.c,v 1.5 2020/08/11 01:07:47 dlg Exp $ */
/*
* Copyright (c) 2020 David Gwynne <dlg@openbsd.org>
@@ -112,14 +112,15 @@ static int kstat_filter_entry(struct kstat_filters *,
static void kstat_list(struct kstat_tree *, int, unsigned int,
struct kstat_filters *);
static void kstat_print(struct kstat_tree *);
+static void kstat_read(struct kstat_tree *, int);
__dead static void
usage(void)
{
extern char *__progname;
- fprintf(stderr, "usage: %s [name|provider:0:name:unit ...]\n",
- __progname);
+ fprintf(stderr, "usage: %s [-w wait] "
+ "[name|provider:0:name:unit ...]\n", __progname);
exit(1);
}
@@ -131,9 +132,28 @@ main(int argc, char *argv[])
struct kstat_tree kt = RBT_INITIALIZER();
unsigned int version;
int fd;
+ const char *errstr;
+ int ch;
+ struct timespec interval = { 0, 0 };
int i;
- for (i = 1; i < argc; i++) {
+ while ((ch = getopt(argc, argv, "w:")) != -1) {
+ switch (ch) {
+ case 'w':
+ interval.tv_sec = strtonum(optarg, 1, 100000000,
+ &errstr);
+ if (errstr != NULL)
+ errx(1, "wait %s: %s", optarg, errstr);
+ break;
+ default:
+ usage();
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
+ for (i = 0; i < argc; i++) {
struct kstat_filter *kf = kstat_filter_parse(argv[i]);
TAILQ_INSERT_TAIL(&kfs, kf, kf_entry);
}
@@ -148,6 +168,16 @@ main(int argc, char *argv[])
kstat_list(&kt, fd, version, &kfs);
kstat_print(&kt);
+ if (interval.tv_sec == 0)
+ return (0);
+
+ for (;;) {
+ nanosleep(&interval, NULL);
+
+ kstat_read(&kt, fd);
+ kstat_print(&kt);
+ }
+
return (0);
}
@@ -496,3 +526,16 @@ kstat_print(struct kstat_tree *kt)
}
}
}
+
+static void
+kstat_read(struct kstat_tree *kt, int fd)
+{
+ struct kstat_entry *kse;
+ struct kstat_req *ksreq;
+
+ RBT_FOREACH(kse, kstat_tree, kt) {
+ ksreq = &kse->kstat;
+ if (ioctl(fd, KSTATIOC_FIND_ID, ksreq) == -1)
+ err(1, "update id %llu", ksreq->ks_id);
+ }
+}