1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
/*
* blktrace output analysis: generate a timeline & gather statistics
*
* Copyright (C) 2006 Alan D. Brunelle <Alan.Brunelle@hp.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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., 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 <sys/time.h>
#include <sys/resource.h>
#include <fcntl.h>
#define INLINE_DECLARE
#include "globals.h"
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)
{
struct list_head *p, *q;
list_for_each_safe(p, q, &files_to_clean) {
struct stat buf;
struct file_info *fip = list_entry(p, struct file_info, head);
fclose(fip->ofp);
if (!stat(fip->oname, &buf) && (buf.st_size == 0))
unlink(fip->oname);
list_del(&fip->head);
free(fip->oname);
free(fip);
}
}
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);
}
}
/*
* 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 rlimit rlim;
int save_errno = errno;
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;
}
static int handle_open_failure(void)
{
if (errno == ENFILE || errno == EMFILE)
return increase_limit(RLIMIT_NOFILE, 16);
return 0;
}
void add_file(FILE *fp, char *oname)
{
struct file_info *fip = malloc(sizeof(*fip));
fip->ofp = fp;
fip->oname = oname;
list_add_tail(&fip->head, &files_to_clean);
}
void add_buf(void *buf)
{
struct buf_info *bip = malloc(sizeof(*bip));
bip->buf = buf;
list_add_tail(&bip->head, &all_bufs);
}
void clean_allocs(void)
{
clean_files();
clean_bufs();
}
char *make_dev_hdr(char *pad, size_t len, struct d_info *dip, int add_parens)
{
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, "%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) {}
|