Fix btt to handle large numbers of output files
authorAlan D. Brunelle <alan.brunelle@hp.com>
Fri, 23 Jan 2009 14:48:21 +0000 (09:48 -0500)
committerAlan D. Brunelle <alan.brunelle@hp.com>
Fri, 23 Jan 2009 14:48:21 +0000 (09:48 -0500)
Simply bump resource limits if file opens fail, and retry.

Signed-off-by: Alan D. Brunelle <alan.brunelle@hp.com>
btt/aqd.c
btt/args.c
btt/bno_dump.c
btt/devmap.c
btt/devs.c
btt/globals.h
btt/latency.c
btt/misc.c
btt/output.c
btt/plat.c
btt/seek.c

index 6dc3a7a162ab95deb71ab27b2608049c686e9574..f619975e53351110086aa2245f4adbb1bb04fabd 100644 (file)
--- a/btt/aqd.c
+++ b/btt/aqd.c
@@ -43,7 +43,7 @@ void *aqd_init(char *str)
 
        oname = malloc(strlen(aqd_name) + strlen(str) + 32);
        sprintf(oname, "%s_%s.dat", aqd_name, str);
-       if ((ap->fp = fopen(oname, "w")) == NULL) {
+       if ((ap->fp = my_fopen(oname, "w")) == NULL) {
                perror(oname);
                return NULL;
        }
index c648b9307db4975824f02eca6a1c6a65a724eba4..0ddd47c5eea6a1c134174ccd9b411fb4e494086d 100644 (file)
@@ -240,7 +240,7 @@ static FILE *setup_ofile(char *fname)
 {
        if (fname) {
                char *buf;
-               FILE *ofp = fopen(fname, "w");
+               FILE *ofp = my_fopen(fname, "w");
 
                if (!ofp) {
                        perror(fname);
@@ -382,7 +382,7 @@ void handle_args(int argc, char *argv[])
                char *fname = malloc(strlen(output_name) + 32);
 
                sprintf(fname, "%s.dat", output_name);
-               ranges_ofp = fopen(fname, "w");
+               ranges_ofp = my_fopen(fname, "w");
                if (ranges_ofp == NULL) {
                        perror(fname);
                        exit(1);
@@ -391,7 +391,7 @@ void handle_args(int argc, char *argv[])
                        printf("Sending range data to %s\n", fname);
 
                sprintf(fname, "%s.avg", output_name);
-               avgs_ofp = fopen(fname, "w");
+               avgs_ofp = my_fopen(fname, "w");
                if (avgs_ofp == NULL) {
                        perror(fname);
                        exit(1);
@@ -400,7 +400,7 @@ void handle_args(int argc, char *argv[])
                        printf("Sending stats data to %s\n", fname);
 
                sprintf(fname, "%s.msg", output_name);
-               msgs_ofp = fopen(fname, "w");
+               msgs_ofp = my_fopen(fname, "w");
                if (msgs_ofp == NULL) {
                        perror(fname);
                        exit(1);
@@ -410,7 +410,7 @@ void handle_args(int argc, char *argv[])
 
                if (easy_parse_avgs) {
                        sprintf(fname, "%s.xvg", output_name);
-                       xavgs_ofp = fopen(fname, "w");
+                       xavgs_ofp = my_fopen(fname, "w");
                        if (avgs_ofp == NULL) {
                                perror(fname);
                                exit(1);
index 1f5433b16701565a74a3b0914a731d137e607323..7f7680e6f319fa6147dcb4f42eb1694ded3fd627 100644 (file)
@@ -37,7 +37,7 @@ static FILE *bno_dump_open(__u32 device, char rwc)
 
        oname = malloc(strlen(bno_dump_name) + 32);
        sprintf(oname, "%s_%03d,%03d_%c.dat", bno_dump_name, mjr, mnr, rwc);
-       if ((fp = fopen(oname, "w")) == NULL)
+       if ((fp = my_fopen(oname, "w")) == NULL)
                perror(oname);
        else
                add_file(&bno_dump_files, fp, oname);
index a74f2247038fafa05e012171439a5060d24c3d09..584144648c5009a0fe528580f35349e0022c639b 100644 (file)
@@ -59,7 +59,7 @@ int dev_map_read(char *fname)
 {
        char line[256];
        struct devmap dm;
-       FILE *fp = fopen(fname, "r");
+       FILE *fp = my_fopen(fname, "r");
 
        if (!fp) {
                perror(fname);
index e5304773087b413c0e5307a16b94caa9c4f0f40c..ea560a2c9506dfb339509060500c333037e8ff02 100644 (file)
@@ -110,7 +110,7 @@ static inline char *mkhandle(char *str, __u32 device, char *post)
 
 static inline FILE *open_pit(char *str)
 {
-       FILE *fp = fopen(str, "w");
+       FILE *fp = my_fopen(str, "w");
 
        if (fp == NULL)
                perror(str);
index 0ee06ed0f0fe961121c3ec0a098f622f3f741435..cc83095a368c597716cb0bcdbe7ffba2b5a90806 100644 (file)
@@ -262,6 +262,7 @@ void clean_files(struct file_info **fipp);
 void add_buf(void *buf);
 void clean_bufs(void);
 char *make_dev_hdr(char *pad, size_t len, struct d_info *dip, int add_parens);
+FILE *my_fopen(const char *path, const char *mode);
 void dbg_ping(void);
 
 /* mmap.c */
index 0c6e55d422d058a91c83ffba8c1beae76174ae14..d12f60c07bf626acf5d5badba535aefbd7fba8a4 100644 (file)
@@ -41,7 +41,7 @@ FILE *latency_open(__u32 device, char *name, char *post)
 
        oname = malloc(strlen(name)+32);
        sprintf(oname, "%s_%03d,%03d_%s.dat", name, mjr, mnr, post);
-       if ((fp = fopen(oname, "w")) == NULL)
+       if ((fp = my_fopen(oname, "w")) == NULL)
                perror(oname);
        else
                add_file(&all_files, fp, oname);
index 0ddd26003c82ff64dcca2161e15849870842a8b4..c79da2c3991997c8a76143100be3ad6cb37641ee 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>
 
 #define INLINE_DECLARE
 #include "globals.h"
@@ -116,4 +119,45 @@ char *make_dev_hdr(char *pad, size_t len, struct d_info *dip, int add_parens)
        return pad;
 }
 
+/*
+ * 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...
+ */
+FILE *my_fopen(const char *path, const char *mode)
+{
+       FILE *fp;
+
+       fp = fopen(path, mode);
+       while (fp == NULL) {
+               if (errno == ENFILE || errno == EMFILE) {
+                       struct rlimit rlim;
+
+                       if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
+                               perror("get: RLIMIT_NOFILE");
+                               return NULL;
+                       }
+
+                       rlim.rlim_cur++;
+                       if (rlim.rlim_cur >= rlim.rlim_max)
+                               rlim.rlim_max++;
+
+                       if (setrlimit(RLIMIT_NOFILE, &rlim) < 0) {
+                               perror("set: RLIMIT_NOFILE");
+                               return NULL;
+                       }
+               }
+               else {
+                       perror(path);
+                       return NULL;
+               }
+
+               fp = fopen(path, mode);
+       }
+
+       return fp;
+}
+
 void dbg_ping(void) {}
index c08e7da52bdaa0b089396dd81c5631305008422f..ace1bcdfd40ab61f71fa5f4cde847d86a94d1a9c 100644 (file)
@@ -678,7 +678,7 @@ void output_histos(void)
        if (output_name == NULL) return;
 
        sprintf(fname, "%s_qhist.dat", output_name);
-       ofp = fopen(fname, "w");
+       ofp = my_fopen(fname, "w");
        if (!ofp) {
                perror(fname);
                return;
@@ -693,7 +693,7 @@ void output_histos(void)
        fclose(ofp);
 
        sprintf(fname, "%s_dhist.dat", output_name);
-       ofp = fopen(fname, "w");
+       ofp = my_fopen(fname, "w");
        if (!ofp) {
                perror(fname);
                return;
index 9b7a2a1ea5753b43805958f4f69c7c26a9cdff55..80716b943e8e7f4876e03250fff4aa7243aa2529 100644 (file)
@@ -42,7 +42,7 @@ void *plat_init(char *str)
 
        oname = malloc(strlen(str) + 32);
        sprintf(oname, "%s.dat", str);
-       if ((pp->fp = fopen(oname, "w")) == NULL) {
+       if ((pp->fp = my_fopen(oname, "w")) == NULL) {
                perror(oname);
                return NULL;
        }
index 69400c86e62d648441e47e3eb18932c50a1506cb..444013c8add8a5de075c7a780ddf240637a8cafe 100644 (file)
@@ -53,7 +53,7 @@ static FILE *seek_open(char *str, char rw)
 
        oname = malloc(strlen(seek_name) + strlen(str) + 32);
        sprintf(oname, "%s_%s_%c.dat", seek_name, str, rw);
-       if ((fp = fopen(oname, "w")) == NULL)
+       if ((fp = my_fopen(oname, "w")) == NULL)
                perror(oname);
        else
                add_file(&seek_files, fp, oname);
@@ -187,7 +187,7 @@ void *seeki_init(char *str)
 
                oname = malloc(strlen(sps_name) + strlen(str) + 32);
                sprintf(oname, "%s_%s.dat", sps_name, str);
-               if ((sip->sps_fp = fopen(oname, "w")) == NULL)
+               if ((sip->sps_fp = my_fopen(oname, "w")) == NULL)
                        perror(oname);
                else
                        add_file(&seek_files, sip->sps_fp, oname);