Skip to content

Commit 6ca2d68

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

1 file changed

Lines changed: 47 additions & 16 deletions

File tree

cv.c

Lines changed: 47 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,27 @@ 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+
off_t diff = f2_->size - f1_->size;
482+
if (diff < 0)
483+
return -1;
484+
else if (diff > 0)
485+
return 1;
486+
else
487+
return 0;
488+
}
489+
470490
int monitor_processes(int *nb_pid)
471491
{
472492
int pid_count, fd_count, result_count;
473-
int i,j;
493+
int i, j, k;
474494
pidinfo_t pidinfo_list[MAX_PIDS];
475495
fdinfo_t fdinfo;
476-
fdinfo_t biggest_fd;
496+
fdinfo_t fdinfos[MAX_FD_PER_PID];
477497
int fdnum_list[MAX_FD_PER_PID];
478-
off_t max_size;
479498
char fsize[64];
480499
char fpos[64];
481500
char ftroughput[64];
@@ -537,19 +556,13 @@ int monitor_processes(int *nb_pid)
537556
for (i = 0; i < pid_count; i++) {
538557
fd_count = find_fd_for_pid(pidinfo_list[i].pid, fdnum_list, MAX_FD_PER_PID);
539558

540-
max_size = 0;
541-
542559
// 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);
560+
for (j = 0; j < fd_count; j++)
561+
get_fdinfo(pidinfo_list[i].pid, fdnum_list[j], &fdinfos[j]);
545562

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

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

559572
// We've our biggest_fd now, let's store the result
560573
results[result_count].pid = pidinfo_list[i];
561-
results[result_count].fd = biggest_fd;
574+
results[result_count].fd = fdinfos[0];
562575
results[result_count].hbegin = NULL;
563576
results[result_count].hend = NULL;
564577
results[result_count].hsize = 0;
565578

566579
result_count++;
580+
581+
int found = 1;
582+
for (k = 1; k < fd_count && found < numfiles; ++k) {
583+
if (fdinfos[k].pos == fdinfos[k - 1].pos &&
584+
fdinfos[k].size == fdinfos[k - 1].size &&
585+
strcmp(fdinfos[k].name, fdinfos[k - 1].name) == 0)
586+
continue;
587+
588+
results[result_count].pid = pidinfo_list[i];
589+
results[result_count].fd = fdinfos[k];
590+
results[result_count].hbegin = NULL;
591+
results[result_count].hend = NULL;
592+
results[result_count].hsize = 0;
593+
594+
result_count++;
595+
596+
found++;
597+
}
567598
}
568599

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

0 commit comments

Comments
 (0)