fix hang when BLKTRACESETUP fails and "-o -" is used
[blktrace.git] / btt / latency.c
index 0c6e55d422d058a91c83ffba8c1beae76174ae14..51e6881be1f8d7f7adab43581448e34786659a2d 100644 (file)
  */
 #include "globals.h"
 
-static struct file_info *all_files = NULL;
-
 static inline void latency_out(FILE *ofp, __u64 tstamp, __u64 latency)
 {
        if (ofp)
                fprintf(ofp, "%lf %lf\n", TO_SEC(tstamp), TO_SEC(latency));
 }
 
-FILE *latency_open(__u32 device, char *name, char *post)
+FILE *latency_open(struct d_info *dip, char *name, char *post)
 {
-       FILE *fp;
-       char *oname;
-       int mjr, mnr;
-
-       if (name == NULL) return NULL;
+       FILE *fp = NULL;
 
-       mjr = device >> MINORBITS;
-       mnr = device & ((1 << MINORBITS) - 1);
+       if (name) {
+               size_t tlen = strlen(name) + strlen(dip->dip_name)
+                                          + strlen(post) + 32;
+               char oname[tlen];
 
-       oname = malloc(strlen(name)+32);
-       sprintf(oname, "%s_%03d,%03d_%s.dat", name, mjr, mnr, post);
-       if ((fp = fopen(oname, "w")) == NULL)
-               perror(oname);
-       else
-               add_file(&all_files, fp, oname);
+               sprintf(oname, "%s_%s_%s.dat", name, dip->dip_name, post);
+               if ((fp = my_fopen(oname, "w")) == NULL)
+                       perror(oname);
+               else
+                       add_file(fp, strdup(oname));
+       }
 
        return fp;
 }
 
-void latency_init(struct d_info *dip)
-{
-       dip->q2d_ofp = latency_open(dip->device, q2d_name, "q2d");
-       dip->d2c_ofp = latency_open(dip->device, d2c_name, "d2c");
-       dip->q2c_ofp = latency_open(dip->device, q2c_name, "q2c");
-}
-
-void latency_clean(void)
+void latency_alloc(struct d_info *dip)
 {
-       clean_files(&all_files);
+       dip->q2d_ofp = latency_open(dip, q2d_name, "q2d");
+       dip->d2c_ofp = latency_open(dip, d2c_name, "d2c");
+       dip->q2c_ofp = latency_open(dip, q2c_name, "q2c");
 }
 
 void latency_q2d(struct d_info *dip, __u64 tstamp, __u64 latency)