2 * blktrace output analysis: generate a timeline & gather statistics
4 * Copyright (C) 2006 Alan D. Brunelle <Alan.Brunelle@hp.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <sys/types.h>
28 #include <sys/resource.h>
31 #define INLINE_DECLARE
35 struct list_head head;
41 struct list_head head;
45 LIST_HEAD(files_to_clean);
48 static void clean_files(void)
50 struct list_head *p, *q;
52 list_for_each_safe(p, q, &files_to_clean) {
54 struct file_info *fip = list_entry(p, struct file_info, head);
57 if (!stat(fip->oname, &buf) && (buf.st_size == 0))
66 static void clean_bufs(void)
68 struct list_head *p, *q;
70 list_for_each_safe(p, q, &all_bufs) {
71 struct buf_info *bip = list_entry(p, struct buf_info, head);
80 * Due to the N(devs) parts of a lot of the output features provided
81 * by btt, it will fail opens on large(ish) systems. Here we try to
82 * keep bumping our open file limits, and if those fail, we return NULL.
84 * Root users will probably be OK with this, others...
86 static int increase_limit(int resource, rlim_t increase)
89 int save_errno = errno;
91 if (!getrlimit(resource, &rlim)) {
92 rlim.rlim_cur += increase;
93 if (rlim.rlim_cur >= rlim.rlim_max)
94 rlim.rlim_max = rlim.rlim_cur + increase;
96 if (!setrlimit(resource, &rlim))
104 static int handle_open_failure(void)
106 if (errno == ENFILE || errno == EMFILE)
107 return increase_limit(RLIMIT_NOFILE, 16);
112 void add_file(FILE *fp, char *oname)
114 struct file_info *fip = malloc(sizeof(*fip));
118 list_add_tail(&fip->head, &files_to_clean);
121 void add_buf(void *buf)
123 struct buf_info *bip = malloc(sizeof(*bip));
126 list_add_tail(&bip->head, &all_bufs);
129 void clean_allocs(void)
135 char *make_dev_hdr(char *pad, size_t len, struct d_info *dip, int add_parens)
138 snprintf(pad, len, "%s", dip->devmap);
140 snprintf(pad, len, "(%3d,%3d)",
141 MAJOR(dip->device), MINOR(dip->device));
143 snprintf(pad, len, "%d,%d",
144 MAJOR(dip->device), MINOR(dip->device));
149 FILE *my_fopen(const char *path, const char *mode)
154 fp = fopen(path, mode);
155 } while (fp == NULL && handle_open_failure());
160 int my_open(const char *path, int flags)
165 fd = open(path, flags);
166 } while (fd < 0 && handle_open_failure());
171 void dbg_ping(void) {}