Skip to content

Commit 9484dc2

Browse files
committed
Let user specify how many files should be monitored.
1 parent dec1c34 commit 9484dc2

1 file changed

Lines changed: 41 additions & 16 deletions

File tree

cv.c

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ signed char flag_throughput = 0;
5959
signed char flag_monitor = 0;
6060
signed char flag_monitor_continous = 0;
6161
double throughput_wait_secs = 1;
62+
static int numfiles = 1;
6263

6364
WINDOW *mainwin;
6465

@@ -334,13 +335,14 @@ void parse_options(int argc, char *argv[])
334335
{"wait-delay", required_argument, 0, 'W'},
335336
{"monitor", no_argument, 0, 'm'},
336337
{"monitor-continous", no_argument, 0, 'M'},
338+
{"files-per-process", required_argument, 0, 'n'},
337339
{"help", no_argument, 0, 'h'},
338340
{"command", required_argument, 0, 'c'},
339341
{"pid", required_argument, 0, 'p'},
340342
{0, 0, 0, 0}
341343
};
342344

343-
static char *options_string = "vqdwmMhc:p:W:";
345+
static char *options_string = "vqdwmMn:hc:p:W:";
344346
int c, i;
345347
int option_index = 0;
346348

@@ -365,13 +367,14 @@ void parse_options(int argc, char *argv[])
365367
for (i = 0; proc_names[i]; i++)
366368
printf("%s ", proc_names[i]);
367369
printf("\n");
368-
printf("Usage: %s [-qdwmM] [-W secs] [-c command] [-p pid]\n", argv[0]);
370+
printf("Usage: %s [-qdwmM] [-W secs] [-c command] [-p pid] [-n num]\n", argv[0]);
369371
printf(" -q --quiet hides all messages\n");
370372
printf(" -d --debug shows all warning/error messages\n");
371373
printf(" -w --wait estimate I/O throughput and ETA (slower display)\n");
372374
printf(" -W --wait-delay secs wait 'secs' seconds for I/O estimation (implies -w, default=%.1f)\n", throughput_wait_secs);
373375
printf(" -m --monitor loop while monitored processes are still running\n");
374376
printf(" -M --monitor-continous like monitor but never stop (similar to watch %s)\n", argv[0]);
377+
printf(" -n --files-per-process num number of files to monitor (default: 1)\n");
375378
printf(" -c --command cmd monitor only this command name (ex: firefox)\n");
376379
printf(" -p --pid id monitor only this process ID (ex: `pidof firefox`)\n");
377380
printf(" -v --version show program version and exit\n");
@@ -415,6 +418,10 @@ void parse_options(int argc, char *argv[])
415418
throughput_wait_secs = atof(optarg);
416419
break;
417420

421+
case 'n':
422+
numfiles = atoi(optarg);
423+
break;
424+
418425
case '?':
419426
default:
420427
exit(EXIT_FAILURE);
@@ -467,15 +474,21 @@ void copy_and_clean_results(result_t *results, int result_count, char copy)
467474
}
468475
}
469476

477+
static int cmp_fdinfos(const void *f1, const void *f2)
478+
{
479+
const fdinfo_t *f1_ = f1;
480+
const fdinfo_t *f2_ = f2;
481+
return f2_->size - f1_->size;
482+
}
483+
470484
int monitor_processes(int *nb_pid)
471485
{
472486
int pid_count, fd_count, result_count;
473-
int i,j;
487+
int i, j, k;
474488
pidinfo_t pidinfo_list[MAX_PIDS];
475489
fdinfo_t fdinfo;
476-
fdinfo_t biggest_fd;
490+
fdinfo_t fdinfos[MAX_FD_PER_PID];
477491
int fdnum_list[MAX_FD_PER_PID];
478-
off_t max_size;
479492
char fsize[64];
480493
char fpos[64];
481494
char ftroughput[64];
@@ -537,19 +550,13 @@ int monitor_processes(int *nb_pid)
537550
for (i = 0; i < pid_count; i++) {
538551
fd_count = find_fd_for_pid(pidinfo_list[i].pid, fdnum_list, MAX_FD_PER_PID);
539552

540-
max_size = 0;
541-
542553
// let's find the biggest opened file
543-
for (j = 0; j < fd_count; j++) {
544-
get_fdinfo(pidinfo_list[i].pid, fdnum_list[j], &fdinfo);
554+
for (j = 0; j < fd_count; j++)
555+
get_fdinfo(pidinfo_list[i].pid, fdnum_list[j], &fdinfos[j]);
545556

546-
if (fdinfo.size > max_size) {
547-
biggest_fd = fdinfo;
548-
max_size = fdinfo.size;
549-
}
550-
}
557+
qsort(fdinfos, fd_count, sizeof(fdinfos[0]), cmp_fdinfos);
551558

552-
if (!max_size) { // nothing found
559+
if (fd_count == 0 || fdinfos[0].size == 0) { // nothing found
553560
nprintf("[%5d] %s inactive/flushing/streaming/...\n",
554561
pidinfo_list[i].pid,
555562
pidinfo_list[i].name);
@@ -558,12 +565,30 @@ int monitor_processes(int *nb_pid)
558565

559566
// We've our biggest_fd now, let's store the result
560567
results[result_count].pid = pidinfo_list[i];
561-
results[result_count].fd = biggest_fd;
568+
results[result_count].fd = fdinfos[0];
562569
results[result_count].hbegin = NULL;
563570
results[result_count].hend = NULL;
564571
results[result_count].hsize = 0;
565572

566573
result_count++;
574+
575+
int found = 1;
576+
for (k = 1; k < fd_count && found < numfiles; ++k) {
577+
if (fdinfos[k].pos == fdinfos[k - 1].pos &&
578+
fdinfos[k].size == fdinfos[k - 1].size &&
579+
strcmp(fdinfos[k].name, fdinfos[k - 1].name) == 0)
580+
continue;
581+
582+
results[result_count].pid = pidinfo_list[i];
583+
results[result_count].fd = fdinfos[k];
584+
results[result_count].hbegin = NULL;
585+
results[result_count].hend = NULL;
586+
results[result_count].hsize = 0;
587+
588+
result_count++;
589+
590+
found++;
591+
}
567592
}
568593

569594
// wait a bit, so we can estimate the throughput

0 commit comments

Comments
 (0)