Skip to content

Commit

Permalink
adding --open-mode {r|w} option to filter files opened for read or write
Browse files Browse the repository at this point in the history
  • Loading branch information
Xfennec committed Feb 9, 2016
1 parent ea34439 commit 8a61b10
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
4 changes: 4 additions & 0 deletions progress.1
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ do not report a process for 'file'. If the file does not exist yet, you must
give a full and clean absolute path. This option can be used multiple times
on the command line.
.TP
.B \-o (\-\-open-mode {r|w})
report only files opened for read or write by the process. This option is
useful when you want to monitor only output files (or input ones) of a process.
.TP
.B \-v (\-\-version)
show program version and exit
.TP
Expand Down
45 changes: 40 additions & 5 deletions progress.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ signed char flag_debug = 0;
signed char flag_throughput = 0;
signed char flag_monitor = 0;
signed char flag_monitor_continuous = 0;
signed char flag_open_mode = 0;
double throughput_wait_secs = 1;

WINDOW *mainwin;
Expand Down Expand Up @@ -349,10 +350,12 @@ struct stat stat_buf;
char fdpath[MAXPATHLEN + 1];
char line[LINE_LEN];
FILE *fp;
int flags;
#endif
struct timezone tz;

fd_info->num = fdnum;
fd_info->mode = PM_NONE;

#ifdef __APPLE__
struct vnode_fdinfowithpath vnodeInfo;
Expand Down Expand Up @@ -419,7 +422,14 @@ if (S_ISBLK(stat_buf.st_mode)) {
#ifdef __APPLE__
fd_info->pos = vnodeInfo.pfi.fi_offset;
gettimeofday(&fd_info->tv, &tz);
if (vnodeInfo.pfi.fi_openflags & FREAD)
fd_info->mode = PM_READ;
if (vnodeInfo.pfi.fi_openflags & FWRITE)
fd_info->mode = PM_WRITE;
if (vnodeInfo.pfi.fi_openflags & FREAD && vnodeInfo.pfi.fi_openflags & FWRITE)
fd_info->mode = PM_READWRITE;
#else
flags = 0;
fd_info->pos = 0;

snprintf(fdpath, MAXPATHLEN, "%s/%d/fdinfo/%d", PROC_PATH, pid, fdnum);
Expand All @@ -433,12 +443,19 @@ if (!fp) {
}

while (fgets(line, LINE_LEN - 1, fp) != NULL) {
line[4]=0;
if (!strcmp(line, "pos:")) {
if (!strncmp(line, "pos:", 4))
fd_info->pos = atoll(line + 5);
break;
}
if (!strncmp(line, "flags:", 6))
flags = atoll(line + 7);
}

if ((flags & O_ACCMODE) == O_RDONLY)
fd_info->mode = PM_READ;
if ((flags & O_ACCMODE) == O_WRONLY)
fd_info->mode = PM_WRITE;
if ((flags & O_ACCMODE) == O_RDWR)
fd_info->mode = PM_READWRITE;

fclose(fp);
#endif
return 1;
Expand Down Expand Up @@ -477,10 +494,11 @@ static struct option long_options[] = {
{"command", required_argument, 0, 'c'},
{"pid", required_argument, 0, 'p'},
{"ignore-file", required_argument, 0, 'i'},
{"open-mode", required_argument, 0, 'o'},
{0, 0, 0, 0}
};

static char *options_string = "vqdwmMhc:p:W:i:";
static char *options_string = "vqdwmMhc:p:W:i:o:";
int c,i;
int option_index = 0;
char *rp;
Expand Down Expand Up @@ -518,6 +536,7 @@ while(1) {
printf(" -c --command cmd monitor only this command name (ex: firefox)\n");
printf(" -p --pid id monitor only this process ID (ex: `pidof firefox`)\n");
printf(" -i --ignore-file file do not report process if using file\n");
printf(" -o --open-mode {r|w} report only files opened for read or write\n");
printf(" -v --version show program version and exit\n");
printf(" -h --help display this help and exit\n");
printf("\n\n");
Expand Down Expand Up @@ -572,6 +591,17 @@ while(1) {
throughput_wait_secs = atof(optarg);
break;

case 'o':
if (!strcmp("r", optarg))
flag_open_mode = PM_READ;
else if (!strcmp("w", optarg))
flag_open_mode = PM_WRITE;
else {
fprintf(stderr,"Invalid --open-mode option value '%s'.\n", optarg);
exit(EXIT_FAILURE);
}
break;

case '?':
default:
exit(EXIT_FAILURE);
Expand Down Expand Up @@ -735,6 +765,11 @@ for (i = 0 ; i < pid_count ; i++) {
for (j = 0 ; j < fd_count ; j++) {
get_fdinfo(pidinfo_list[i].pid, fdnum_list[j], &fdinfo);

if (flag_open_mode == PM_READ && fdinfo.mode != PM_READ && fdinfo.mode != PM_READWRITE)
continue;
if (flag_open_mode == PM_WRITE && fdinfo.mode != PM_WRITE && fdinfo.mode != PM_READWRITE)
continue;

if (fdinfo.size > max_size) {
biggest_fd = fdinfo;
max_size = fdinfo.size;
Expand Down
8 changes: 7 additions & 1 deletion progress.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,24 @@

#include "hlist.h"

#define PROGRESS_VERSION "0.12"
#define PROGRESS_VERSION "0.13"

#define PROC_PATH "/proc"
#define MAX_PIDS 32
#define MAX_RESULTS 32
#define MAX_FD_PER_PID 512
#define LINE_LEN 256

#define PM_NONE 0
#define PM_READ 1 // read only
#define PM_WRITE 2 // write only
#define PM_READWRITE 4

typedef struct fdinfo_t {
int num;
off_t size;
off_t pos;
signed char mode;
char name[MAXPATHLEN + 1];
struct timeval tv;
} fdinfo_t;
Expand Down

0 comments on commit 8a61b10

Please sign in to comment.