Add ctrstat command

Like vmstat and iostat, this prints out our counters over time.

Signed-off-by: Zach Brown <zab@versity.com>
This commit is contained in:
Zach Brown
2016-04-01 00:04:26 -04:00
parent af2975111a
commit 544fd1ba9a
3 changed files with 559 additions and 1 deletions
+1 -1
View File
@@ -20,7 +20,7 @@ endif
$(BIN): $(OBJ)
$(QU) [BIN $@]
$(VE)gcc -o $@ $^ -luuid
$(VE)gcc -o $@ $^ -luuid -lm
%.o %.d: %.c Makefile sparse.sh
$(QU) [CC $<]
+248
View File
@@ -0,0 +1,248 @@
#define _XOPEN_SOURCE 700 /* 600: floorf, strtof, 700: openat */
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <limits.h>
#include <dirent.h>
#include <math.h>
#include <time.h>
#include "util.h"
#include "cmd.h"
#include "list.h"
#define SCOUTFS_SYSFS_PATH "/sys/fs/scoutfs"
struct string_item {
struct list_head head;
int dir_fd;
char *str;
int len;
};
static int add_string(char *str, struct list_head *list)
{
struct string_item *sitem;
int ret = -ENOMEM;
sitem = malloc(sizeof(struct string_item));
if (sitem) {
sitem->str = strdup(str);
if (sitem->str) {
sitem->dir_fd = -1;
list_add_tail(&sitem->head, list);
sitem->len = strlen(str);
ret = 0;
}
}
if (ret)
fprintf(stderr, "failed to alloc mem for string '%s'\n", str);
return ret;
}
/*
* Iterate over all the mounted ids and use their open dirfds to open
* and read each counter. We have to open each time we want updated counters.
* We reflect the counter length in the column's label length.
*/
static int read_and_print_counters(struct list_head *labels,
struct list_head *id_list, int print)
{
struct string_item *label;
struct string_item *id;
char buf[25];
ssize_t bytes;
int ret = 0;
int fd;
list_for_each_entry(id, id_list, head) {
list_for_each_entry(label, labels, head) {
/* id column */
if (label->str[0] == '\0') {
label->len = max(label->len, id->len);
if (print)
printf("%*s ", label->len, id->str);
continue;
}
/* have to open each time we want current counter :/ */
fd = openat(id->dir_fd, label->str, O_RDONLY);
if (fd < 0) {
ret = -errno;
goto out;
}
bytes = pread(fd, buf, sizeof(buf), 0);
close(fd);
if (bytes <= 1 || bytes >= sizeof(buf) ||
buf[bytes - 1] != '\n') {
fprintf(stderr, "counter file %s/%s read returned %zd\n",
id->str, label->str, bytes);
ret = -EIO;
goto out;
}
label->len = max(label->len, bytes - 1);
if (print) {
buf[bytes - 1] = '\0';
printf("%*s ", label->len, buf);
}
}
if (print)
printf("\n");
}
out:
return ret;
}
static int dots(char *name)
{
return name[0] == '.' &&
(name[1] == '\0' || (name[1] == '.' && name[2] == '\0'));
}
/*
* XXX deal with unmount ;)
*/
static int ctrstat_cmd(int argc, char **argv)
{
struct string_item *label;
struct string_item *id;
LIST_HEAD(label_list);
char path[PATH_MAX];
LIST_HEAD(id_list);
struct dirent *dent;
float seconds = 1.0;
struct timespec ts;
DIR *dirp;
int iter;
int ret;
if (argc > 1) {
printf("scoutfs ctrstat: too many arguments\n");
return -EINVAL;
}
/* set the sleep duration */
if (argc == 1) {
seconds = strtof(argv[0], NULL);
if (fpclassify(seconds) != FP_NORMAL || seconds <= 0) {
printf("invalid sleep duration float: %s\n", argv[0]);
return -EINVAL;
}
}
ts.tv_sec = (int)floorf(seconds);
ts.tv_nsec = (seconds - floorf(seconds)) * 1000000000;
/* find all the mounted ids */
dirp = opendir(SCOUTFS_SYSFS_PATH);
if (!dirp) {
ret = -errno;
fprintf(stderr, "failed to open '%s': %s (%d)\n",
SCOUTFS_SYSFS_PATH, strerror(errno), errno);
goto out;
}
while ((dent = readdir(dirp))) {
if (dots(dent->d_name))
continue;
ret = add_string(dent->d_name, &id_list);
if (ret)
goto out;
}
closedir(dirp);
dirp = NULL;
/* add a dummy label for the id column */
ret = add_string("", &label_list);
if (ret)
goto out;
iter = 1;
list_for_each_entry(id, &id_list, head) {
snprintf(path, PATH_MAX, SCOUTFS_SYSFS_PATH"/%s/counters",
id->str);
dirp = opendir(path);
if (!dirp) {
ret = -errno;
fprintf(stderr, "failed to open '%s': %s (%d)\n",
path, strerror(errno), errno);
goto out;
}
/* hold a dir fd open for each id */
id->dir_fd = dup(dirfd(dirp));
if (id->dir_fd < 0) {
ret = -errno;
fprintf(stderr, "couldn't dup fd for '%s': %s (%d)\n",
path, strerror(errno), errno);
goto out;
}
/* find all the counters, assume all ids have same */
while (iter && (dent = readdir(dirp))) {
if (dots(dent->d_name))
continue;
ret = add_string(dent->d_name, &label_list);
if (ret)
goto out;
}
closedir(dirp);
dirp = NULL;
iter = 0;
}
/* initial read pass to find the max lengths */
ret = read_and_print_counters(&label_list, &id_list, 0);
if (ret)
goto out;
for (iter = 0; ; iter++) {
/* print row of column labels */
if (!(iter % 25)) {
list_for_each_entry(label, &label_list, head)
printf("%*s ", label->len, label->str);
printf("\n");
}
/* print each id and its stats */
ret = read_and_print_counters(&label_list, &id_list, 1);
if (ret)
goto out;
nanosleep(&ts, NULL);
}
ret = 0;
out:
if (dirp)
closedir(dirp);
/* squish together and free all */
list_splice(&label_list, &id_list);
list_for_each_entry_safe(id, label, &id_list, head) {
list_del_init(&id->head);
if (id->dir_fd >= 0)
close(id->dir_fd);
free(id);
}
return ret;
};
static void __attribute__((constructor)) ctrstat_ctor(void)
{
cmd_register("ctrstat", "<delay secs>", "print counters over time",
ctrstat_cmd);
}
+310
View File
@@ -0,0 +1,310 @@
/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
* vim:expandtab:shiftwidth=8:tabstop=8:
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* yanked from the linux kernel..
*/
#ifndef _LIST_H_
#define _LIST_H_
/*
* Simple doubly linked list implementation.
*
* Some of the internal functions ("__xxx") are useful when
* manipulating whole lists rather than single entries, as
* sometimes we already know the next/prev entries and we can
* generate better code by using them directly rather than
* using the generic single-entry routines.
*/
struct list_head {
struct list_head *next, *prev;
};
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)
#define INIT_LIST_HEAD(ptr) do { \
(ptr)->next = (ptr); (ptr)->prev = (ptr); \
} while (0)
/*
* Insert a new entry between two known consecutive entries.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static inline void __list_add(struct list_head *new,
struct list_head *prev,
struct list_head *next)
{
next->prev = new;
new->next = next;
new->prev = prev;
prev->next = new;
}
/**
* list_add - add a new entry
* @new: new entry to be added
* @head: list head to add it after
*
* Insert a new entry after the specified head.
* This is good for implementing stacks.
*/
static inline void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
}
/**
* list_add_tail - add a new entry
* @new: new entry to be added
* @head: list head to add it before
*
* Insert a new entry before the specified head.
* This is useful for implementing queues.
*/
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
__list_add(new, head->prev, head);
}
/*
* Insert a new entry between two known consecutive entries.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static __inline__ void __list_add_rcu(struct list_head * new,
struct list_head * prev,
struct list_head * next)
{
new->next = next;
new->prev = prev;
next->prev = new;
prev->next = new;
}
/*
* Delete a list entry by making the prev/next entries
* point to each other.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
next->prev = prev;
prev->next = next;
}
/**
* list_del - deletes entry from list.
* @entry: the element to delete from the list.
* Note: list_empty on entry does not return true after this, the entry is
* in an undefined state.
*/
static inline void list_del(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
}
/**
* list_del_init - deletes entry from list and reinitialize it.
* @entry: the element to delete from the list.
*/
static inline void list_del_init(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
INIT_LIST_HEAD(entry);
}
/**
* list_move - delete from one list and add as another's head
* @list: the entry to move
* @head: the head that will precede our entry
*/
static inline void list_move(struct list_head *list, struct list_head *head)
{
__list_del(list->prev, list->next);
list_add(list, head);
}
/**
* list_move_tail - delete from one list and add as another's tail
* @list: the entry to move
* @head: the head that will follow our entry
*/
static inline void list_move_tail(struct list_head *list,
struct list_head *head)
{
__list_del(list->prev, list->next);
list_add_tail(list, head);
}
/**
* list_empty - tests whether a list is empty
* @head: the list to test.
*/
static inline int list_empty(struct list_head *head)
{
return head->next == head;
}
static inline void __list_splice(struct list_head *list,
struct list_head *head)
{
struct list_head *first = list->next;
struct list_head *last = list->prev;
struct list_head *at = head->next;
first->prev = head;
head->next = first;
last->next = at;
at->prev = last;
}
/**
* list_splice - join two lists
* @list: the new list to add.
* @head: the place to add it in the first list.
*/
static inline void list_splice(struct list_head *list, struct list_head *head)
{
if (!list_empty(list))
__list_splice(list, head);
}
/**
* list_splice_init - join two lists and reinitialise the emptied list.
* @list: the new list to add.
* @head: the place to add it in the first list.
*
* The list at @list is reinitialised
*/
static inline void list_splice_init(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list)) {
__list_splice(list, head);
INIT_LIST_HEAD(list);
}
}
/**
* list_entry - get the struct for this entry
* @ptr: the &struct list_head pointer.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_struct within the struct.
*/
#define list_entry(ptr, type, member) \
((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
/**
* list_for_each - iterate over a list
* @pos: the &struct list_head to use as a loop counter.
* @head: the head for your list.
*/
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)
/**
* list_for_each_prev - iterate over a list backwards
* @pos: the &struct list_head to use as a loop counter.
* @head: the head for your list.
*/
#define list_for_each_prev(pos, head) \
for (pos = (head)->prev; pos != (head); pos = pos->prev)
/**
* list_for_each_safe - iterate over a list safe against removal of list entry
* @pos: the &struct list_head to use as a loop counter.
* @n: another &struct list_head to use as temporary storage
* @head: the head for your list.
*/
#define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)
/**
* list_for_each_entry - iterate over list of given type
* @pos: the type * to use as a loop counter.
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*/
#define list_for_each_entry(pos, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member); \
&pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))
/**
* list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*/
#define list_for_each_entry_safe(pos, n, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member), \
n = list_entry(pos->member.next, typeof(*pos), member); \
&pos->member != (head); \
pos = n, n = list_entry(n->member.next, typeof(*n), member))
/**
* list_first_entry - get the first element from a list
* @ptr: the list head to take the element from.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_head within the struct.
*
* Note, that list is expected to be not empty.
*/
#define list_first_entry(ptr, type, member) \
list_entry((ptr)->next, type, member)
/**
* list_last_entry - get the last element from a list
* @ptr: the list head to take the element from.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_head within the struct.
*
* Note, that list is expected to be not empty.
*/
#define list_last_entry(ptr, type, member) \
list_entry((ptr)->prev, type, member)
/**
* list_first_entry_or_null - get the first element from a list
* @ptr: the list head to take the element from.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_head within the struct.
*
* Note that if the list is empty, it returns NULL.
*/
#define list_first_entry_or_null(ptr, type, member) \
(!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL)
/**
* list_next_entry - get the next element in list
* @pos: the type * to cursor
* @member: the name of the list_head within the struct.
*/
#define list_next_entry(pos, member) \
list_entry((pos)->member.next, typeof(*(pos)), member)
#endif