summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/kstat/kstat.c52
-rw-r--r--usr.bin/sysctl/sysctl.c39
2 files changed, 84 insertions, 7 deletions
diff --git a/usr.bin/kstat/kstat.c b/usr.bin/kstat/kstat.c
index e3b7e6a..cbbe602 100644
--- a/usr.bin/kstat/kstat.c
+++ b/usr.bin/kstat/kstat.c
@@ -28,10 +28,51 @@
*/
#include <sys/sched.h>
+#include <sys/vmstat.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
+#define MIB_PER_GIB 1024
+
+static void
+print_size_mib(const char *name, size_t mib)
+{
+ if (name == NULL) {
+ return;
+ }
+
+ if (mib >= MIB_PER_GIB) {
+ printf("%s: %d GiB\n", name, mib / MIB_PER_GIB);
+ } else {
+ printf("%s: %d MiB\n", name, mib);
+ }
+}
+
+static void
+get_vm_stat(void)
+{
+ struct vm_stat vmstat;
+ int retval, fd;
+
+ fd = open("/ctl/vm/stat", O_RDONLY);
+ if (fd < 0) {
+ printf("failed to open '/ctl/vm/stat'\n");
+ return;
+ }
+
+ retval = read(fd, &vmstat, sizeof(vmstat));
+ if (retval <= 0) {
+ printf("failed to read vmstat\n");
+ return;
+ }
+
+ close(fd);
+ print_size_mib("memory available", vmstat.mem_avail);
+ print_size_mib("memory used", vmstat.mem_used);
+ print_size_mib("memory total", vmstat.mem_total);
+}
+
static void
get_sched_stat(void)
{
@@ -56,12 +97,10 @@ get_sched_stat(void)
nonline = (stat.ncpu - noffline);
online_percent = (uint16_t)(((double)nonline / (nonline + noffline)) * 100);
- printf("-------------------------------\n");
- printf("Number of tasks: %d\n", stat.nproc);
- printf("Number of cores online: %d\n", stat.ncpu);
- printf("Scheduler quantum: %d usec\n", stat.quantum_usec);
+ printf("number of tasks: %d\n", stat.nproc);
+ printf("number of cores online: %d\n", stat.ncpu);
+ printf("scheduler quantum: %d usec\n", stat.quantum_usec);
printf("CPU is %d%% online\n", online_percent);
- printf("-------------------------------\n");
/*
* Log out some per-cpu information
@@ -75,6 +114,9 @@ get_sched_stat(void)
int
main(void)
{
+ printf("-- scheduler statistics --\n");
get_sched_stat();
+ printf("-- memory statistics --\n");
+ get_vm_stat();
return 0;
}
diff --git a/usr.bin/sysctl/sysctl.c b/usr.bin/sysctl/sysctl.c
index d4275a7..4a84484 100644
--- a/usr.bin/sysctl/sysctl.c
+++ b/usr.bin/sysctl/sysctl.c
@@ -46,13 +46,18 @@
#define NAME_NCPU "ncpu"
#define NAME_MACHINE "machine"
+/* Proc var string constants */
+#define NAME_COUNT "count"
+
/* Name start string constants */
#define NAME_KERN "kern"
#define NAME_HW "hw"
+#define NAME_PROC "proc"
/* Name start int constants */
#define NAME_DEF_KERN 0
#define NAME_DEF_HW 1
+#define NAME_DEF_PROC 2
/*
* Print the contents read from a sysctl
@@ -105,6 +110,12 @@ name_to_def(const char *name)
}
return -1;
+ case 'p':
+ if (strcmp(name, NAME_PROC) == 0) {
+ return NAME_DEF_PROC;
+ }
+
+ return -1;
}
return -1;
@@ -178,6 +189,28 @@ hw_node(const char *node, bool *is_str)
}
/*
+ * Handle parsing of 'proc.*' node names
+ *
+ * @node: Node name to parse
+ * @is_str: Set to true if string
+ */
+static int
+proc_node(const char *node, bool *is_str)
+{
+ switch (*node) {
+ case 'c':
+ if (strcmp(node, NAME_COUNT) == 0) {
+ *is_str = false;
+ return PROC_COUNT;
+ }
+
+ return -1;
+ }
+
+ return -1;
+}
+
+/*
* Convert string node to a sysctl name
* definition.
*
@@ -215,6 +248,8 @@ node_to_def(int name, const char *node, bool *is_str)
return kern_node(node, is_str);
case NAME_DEF_HW:
return hw_node(node, is_str);
+ case NAME_DEF_PROC:
+ return proc_node(node, is_str);
}
return -1;
@@ -240,12 +275,12 @@ main(int argc, char **argv)
p = strtok(var, ".");
if (p == NULL) {
- printf("sysctl: bad var\n");
+ printf("sysctl: bad var \"%s\"\n", p);
return -1;
}
if ((root = name_to_def(p)) < 0) {
- printf("sysctl: bad var \"%s\"", p);
+ printf("sysctl: bad var \"%s\"\n", p);
return root;
}