diff options
| author | Bjoern Brandenburg <bbb@mpi-sws.org> | 2012-09-10 03:11:40 -0400 |
|---|---|---|
| committer | Bjoern Brandenburg <bbb@mpi-sws.org> | 2012-10-19 12:50:28 -0400 |
| commit | 4d44afb20de33f00b0e9ffc708e360a419b11e56 (patch) | |
| tree | 86fc38fb3609ed766e0b379b5d45e1327b97bcc8 /src | |
| parent | 46a5a8976116026302e9ee9b94bc93cdb40e247f (diff) | |
Support terminating ftcat after a given number of samples has been read
Diffstat (limited to 'src')
| -rw-r--r-- | src/ftcat.c | 48 |
1 files changed, 39 insertions, 9 deletions
diff --git a/src/ftcat.c b/src/ftcat.c index 685f65b..b43a3a5 100644 --- a/src/ftcat.c +++ b/src/ftcat.c | |||
| @@ -34,7 +34,7 @@ static int fd; | |||
| 34 | static int event_count = 0; | 34 | static int event_count = 0; |
| 35 | static cmd_t ids[MAX_EVENTS]; | 35 | static cmd_t ids[MAX_EVENTS]; |
| 36 | static unsigned long total_bytes = 0; | 36 | static unsigned long total_bytes = 0; |
| 37 | 37 | static unsigned long stop_after_bytes = 0; | |
| 38 | 38 | ||
| 39 | static int disable_all(int fd) | 39 | static int disable_all(int fd) |
| 40 | { | 40 | { |
| @@ -80,18 +80,23 @@ static void cat2stdout(int fd) | |||
| 80 | while ((rd = read(fd, buf, 4096)) > 0) { | 80 | while ((rd = read(fd, buf, 4096)) > 0) { |
| 81 | total_bytes += rd; | 81 | total_bytes += rd; |
| 82 | fwrite(buf, 1, rd, stdout); | 82 | fwrite(buf, 1, rd, stdout); |
| 83 | if (stop_after_bytes && total_bytes >= stop_after_bytes) | ||
| 84 | break; | ||
| 83 | } | 85 | } |
| 84 | } | 86 | } |
| 85 | 87 | ||
| 86 | 88 | ||
| 87 | static void usage(void) | 89 | static void _usage(void) |
| 88 | { | 90 | { |
| 89 | fprintf(stderr, | 91 | fprintf(stderr, |
| 90 | "Usage: ftcat <ft device> TS1 TS2 ...." | 92 | "Usage: ftcat [OPTIONS] <ft device> TS1 TS2 ....\n" |
| 91 | "\n"); | 93 | "\nOptions:\n" |
| 94 | " -s SIZE -- stop tracing afer recording SIZE bytes\n"); | ||
| 92 | exit(1); | 95 | exit(1); |
| 93 | } | 96 | } |
| 94 | 97 | ||
| 98 | #define usage(fmt, args...) do { fprintf(stderr, "Error: " fmt "\n", ## args); _usage(); } while (0) | ||
| 99 | |||
| 95 | static void shutdown(int sig) | 100 | static void shutdown(int sig) |
| 96 | { | 101 | { |
| 97 | int ok; | 102 | int ok; |
| @@ -100,20 +105,45 @@ static void shutdown(int sig) | |||
| 100 | fprintf(stderr, "disable_all: %m\n"); | 105 | fprintf(stderr, "disable_all: %m\n"); |
| 101 | } | 106 | } |
| 102 | 107 | ||
| 108 | #define OPTSTR "s:" | ||
| 109 | |||
| 103 | int main(int argc, char** argv) | 110 | int main(int argc, char** argv) |
| 104 | { | 111 | { |
| 112 | int opt; | ||
| 113 | |||
| 105 | const char* trace_file; | 114 | const char* trace_file; |
| 115 | |||
| 116 | while ((opt = getopt(argc, argv, OPTSTR)) != -1) { | ||
| 117 | switch (opt) { | ||
| 118 | case 's': | ||
| 119 | stop_after_bytes = atol(optarg); | ||
| 120 | if (stop_after_bytes == 0) | ||
| 121 | usage("invalid size (%s)", optarg); | ||
| 122 | break; | ||
| 123 | case ':': | ||
| 124 | usage("Argument missing."); | ||
| 125 | break; | ||
| 126 | case '?': | ||
| 127 | default: | ||
| 128 | usage("Bad argument."); | ||
| 129 | break; | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | argc -= optind; | ||
| 134 | argv += optind; | ||
| 135 | |||
| 106 | if (argc < 3) | 136 | if (argc < 3) |
| 107 | usage(); | 137 | usage("Argument missing."); |
| 108 | 138 | ||
| 109 | trace_file = argv[1]; | 139 | trace_file = argv[0]; |
| 110 | fd = open(trace_file, O_RDWR); | 140 | fd = open(trace_file, O_RDWR); |
| 111 | if (fd < 0) { | 141 | if (fd < 0) { |
| 112 | perror("could not open feathertrace"); | 142 | usage("could not open feathertrace device (%s): %m", trace_file); |
| 113 | return 1; | 143 | return 1; |
| 114 | } | 144 | } |
| 115 | argc -= 2; | 145 | argc -= 1; |
| 116 | argv += 2; | 146 | argv += 1; |
| 117 | signal(SIGINT, shutdown); | 147 | signal(SIGINT, shutdown); |
| 118 | signal(SIGUSR1, shutdown); | 148 | signal(SIGUSR1, shutdown); |
| 119 | signal(SIGTERM, shutdown); | 149 | signal(SIGTERM, shutdown); |
