btt: Added no remap option
[blktrace.git] / btt / misc.c
index 0ddd26003c82ff64dcca2161e15849870842a8b4..022955d1c200d640c14e8066143bae8ed9694969 100644 (file)
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  */
+#include <errno.h>
 #include <stdio.h>
 #include <string.h>
+#include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <unistd.h>
+#include <sys/time.h>
+#include <sys/resource.h>
+#include <fcntl.h>
 
 #define INLINE_DECLARE
 #include "globals.h"
 
-int in_devices(struct blk_io_trace *t)
+struct file_info {
+       struct list_head head;
+       FILE *ofp;
+       char *oname;
+};
+
+struct buf_info {
+       struct list_head head;
+       void *buf;
+};
+
+LIST_HEAD(files_to_clean);
+LIST_HEAD(all_bufs);
+
+static void clean_files(void)
 {
-       int i;
-       unsigned int mjr, mnr;
-       char *p = devices;
+       struct list_head *p, *q;
 
-       if (p == NULL) return 1;        /* Allow anything */
+       list_for_each_safe(p, q, &files_to_clean) {
+               struct stat buf;
+               struct file_info *fip = list_entry(p, struct file_info, head);
 
-       for (;;) {
-               i = sscanf(p, "%u,%u;", &mjr, &mnr);
-               if ((mjr == MAJOR(t->device) && (mnr == MINOR(t->device))))
-                       return 1;
+               fclose(fip->ofp);
+               if (!stat(fip->oname, &buf) && (buf.st_size == 0))
+                       unlink(fip->oname);
 
-               p = strchr(p, ';');
-               if (!p)
-                       break;
-               p++;
+               list_del(&fip->head);
+               free(fip->oname);
+               free(fip);
        }
+}
 
-       return 0;
+static void clean_bufs(void)
+{
+       struct list_head *p, *q;
+
+       list_for_each_safe(p, q, &all_bufs) {
+               struct buf_info *bip = list_entry(p, struct buf_info, head);
+
+               list_del(&bip->head);
+               free(bip->buf);
+               free(bip);
+       }
 }
 
-void add_file(struct file_info **fipp, FILE *fp, char *oname)
+/*
+ * Due to the N(devs) parts of a lot of the output features provided
+ * by btt, it will fail opens on large(ish) systems. Here we try to
+ * keep bumping our open file limits, and if those fail, we return NULL.
+ *
+ * Root users will probably be OK with this, others...
+ */
+static int increase_limit(int resource, rlim_t increase)
 {
-       struct file_info *fip = malloc(sizeof(*fip));
+       struct rlimit rlim;
+       int save_errno = errno;
 
-       fip->ofp = fp;
-       fip->oname = oname;
-       fip->next = *fipp;
-       *fipp = fip;
+       if (!getrlimit(resource, &rlim)) {
+               rlim.rlim_cur += increase;
+               if (rlim.rlim_cur >= rlim.rlim_max)
+                       rlim.rlim_max = rlim.rlim_cur + increase;
+
+               if (!setrlimit(resource, &rlim))
+                       return 1;
+       }
+
+       errno = save_errno;
+       return 0;
 }
 
-void clean_files(struct file_info **fipp)
+static int handle_open_failure(void)
 {
-       struct stat buf;
-       struct file_info *fip;
+       if (errno == ENFILE || errno == EMFILE)
+               return increase_limit(RLIMIT_NOFILE, 16);
 
-       while ((fip = *fipp) != NULL) {
-               *fipp = fip->next;
+       return 0;
+}
 
-               fclose(fip->ofp);
-               if (!stat(fip->oname, &buf) && (buf.st_size == 0))
-                       unlink(fip->oname);
+void add_file(FILE *fp, char *oname)
+{
+       struct file_info *fip = malloc(sizeof(*fip));
 
-               free(fip->oname);
-               free(fip);
-       }
+       fip->ofp = fp;
+       fip->oname = oname;
+       list_add_tail(&fip->head, &files_to_clean);
 }
 
-struct buf_info {
-       struct buf_info *next;
-       void *buf;
-} *all_bufs;
 void add_buf(void *buf)
 {
        struct buf_info *bip = malloc(sizeof(*bip));
 
        bip->buf = buf;
-       bip->next = all_bufs;
-       all_bufs = bip;
+       list_add_tail(&bip->head, &all_bufs);
 }
 
-void clean_bufs(void)
+void clean_allocs(void)
 {
-       struct buf_info *bip;
-
-       while ((bip = all_bufs) != NULL) {
-               all_bufs = bip->next;
-               free(bip->buf);
-               free(bip);
-       }
+       clean_files();
+       clean_bufs();
 }
 
 char *make_dev_hdr(char *pad, size_t len, struct d_info *dip, int add_parens)
 {
-       if (dip->map == NULL) {
-               if (add_parens)
-                       snprintf(pad, len, "(%3d,%3d)",
-                                MAJOR(dip->device), MINOR(dip->device));
-               else
-                       snprintf(pad, len, "%d,%d",
-                                MAJOR(dip->device), MINOR(dip->device));
-       }
+       if (dip->devmap)
+               snprintf(pad, len, "%s", dip->devmap);
+       else if (add_parens)
+               snprintf(pad, len, "(%3d,%3d)",
+                        MAJOR(dip->device), MINOR(dip->device));
        else
-               snprintf(pad, len, "%s", dip->map->device);
+               snprintf(pad, len, "%d,%d",
+                        MAJOR(dip->device), MINOR(dip->device));
 
        return pad;
 }
 
+FILE *my_fopen(const char *path, const char *mode)
+{
+       FILE *fp;
+
+       do {
+               fp = fopen(path, mode);
+       } while (fp == NULL && handle_open_failure());
+
+       return fp;
+}
+
+int my_open(const char *path, int flags)
+{
+       int fd;
+
+       do {
+               fd = open(path, flags);
+       } while (fd < 0 && handle_open_failure());
+
+       return fd;
+}
+
 void dbg_ping(void) {}