blktrace: Use number of online CPUs
[blktrace.git] / blktrace.c
index b14daf20c29cf740415ab7ff2d9f36707060a22d..b445524b1f0b8b38ae9d13f030d7cdf171558a78 100644 (file)
@@ -621,13 +621,19 @@ static void dpp_free(struct devpath *dpp)
 
 static int lock_on_cpu(int cpu)
 {
-       cpu_set_t cpu_mask;
-
-       CPU_ZERO(&cpu_mask);
-       CPU_SET(cpu, &cpu_mask);
-       if (sched_setaffinity(0, sizeof(cpu_mask), &cpu_mask) < 0)
+       cpu_set_t * cpu_mask;
+       size_t size;
+       cpu_mask = CPU_ALLOC(ncpus);
+       size = CPU_ALLOC_SIZE(ncpus);
+
+       CPU_ZERO_S(size, cpu_mask);
+       CPU_SET_S(cpu, size, cpu_mask);
+       if (sched_setaffinity(0, size, cpu_mask) < 0) {
+               CPU_FREE(cpu_mask);             
                return errno;
+       }
 
+       CPU_FREE(cpu_mask);             
        return 0;
 }
 
@@ -872,8 +878,9 @@ static int net_send_header(int fd, int cpu, char *buts_name, int len)
        memset(&hdr, 0, sizeof(hdr));
 
        hdr.magic = BLK_IO_TRACE_MAGIC;
+       memset(hdr.buts_name, 0, sizeof(hdr.buts_name));
        strncpy(hdr.buts_name, buts_name, sizeof(hdr.buts_name));
-       hdr.buts_name[sizeof(hdr.buts_name)-1] = '\0';
+       hdr.buts_name[sizeof(hdr.buts_name) - 1] = '\0';
        hdr.cpu = cpu;
        hdr.max_cpus = ncpus;
        hdr.len = len;
@@ -981,7 +988,9 @@ retry:
                }
 
                memcpy(&addr->sin_addr, hent->h_addr, 4);
-               strcpy(hostname, hent->h_name);
+               memset(hostname, 0, sizeof(hostname));
+               strncpy(hostname, hent->h_name, sizeof(hostname));
+               hostname[sizeof(hostname) - 1] = '\0';
        }
 
        return 0;
@@ -1728,11 +1737,10 @@ static int handle_pfds_netclient(struct tracer *tp, int nevs, int force_read)
 {
        struct stat sb;
        int i, nentries = 0;
-       struct pdc_stats *sp;
        struct pollfd *pfd = tp->pfds;
        struct io_info *iop = tp->ios;
 
-       for (i = 0; i < ndevs; i++, pfd++, iop++, sp++) {
+       for (i = 0; i < ndevs; i++, pfd++, iop++) {
                if (pfd->revents & POLLIN || force_read) {
                        if (fstat(iop->ifd, &sb) < 0) {
                                perror(iop->ifn);
@@ -1905,6 +1913,7 @@ static void stop_tracers(void)
                struct tracer *tp = list_entry(p, struct tracer, head);
                tp->is_done = 1;
        }
+       pthread_cond_broadcast(&mt_cond);
 }
 
 static void del_tracers(void)
@@ -2076,9 +2085,13 @@ static int handle_args(int argc, char *argv[])
                                return 1;
                        }
 
-                       while (fscanf(ifp, "%s\n", dev_line) == 1)
-                               if (add_devpath(dev_line) != 0)
+                       while (fscanf(ifp, "%s\n", dev_line) == 1) {
+                               if (add_devpath(dev_line) != 0) {
+                                       fclose(ifp);
                                        return 1;
+                               }
+                       }
+                       fclose(ifp);
                        break;
                }
 
@@ -2128,7 +2141,9 @@ static int handle_args(int argc, char *argv[])
                        break;
                case 'h':
                        net_mode = Net_client;
-                       strcpy(hostname, optarg);
+                       memset(hostname, 0, sizeof(hostname));
+                       strncpy(hostname, optarg, sizeof(hostname));
+                       hostname[sizeof(hostname) - 1] = '\0';
                        break;
                case 'l':
                        net_mode = Net_server;
@@ -2183,7 +2198,10 @@ static int handle_args(int argc, char *argv[])
                piped_output = 1;
                handle_pfds = handle_pfds_entries;
                pfp = stdout;
-               setvbuf(pfp, NULL, _IONBF, 0);
+               if (setvbuf(pfp, NULL, _IONBF, 0)) {
+                       perror("setvbuf stdout");
+                       return 1;
+               }
        } else
                handle_pfds = handle_pfds_file;
        return 0;