aboutsummaryrefslogtreecommitdiff
path: root/mstat.c
blob: 18e19b65edfbbea416a5adc1dfaca1b6cc1c2991 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <errno.h>
#include <dirent.h>
#include <signal.h>
#include <time.h>
#include "common.h"


struct Option {
    /** Increased verbosity */
    unsigned char verbose;
    /** Overwrite existing file(s) */
    unsigned char clobber;
    /** PID to track */
    pid_t pid;
    /** Output file handle to track */
    FILE *file;
    /** Number of times per second mstat samples a pid */
    double sample_rate;
} option;

/**
 * Interrupt handler.
 * Called on exit.
 * @param sig the trapped signal
 */
static void handle_interrupt(int sig) {
    switch (sig) {
        case SIGUSR1:
            if (option.file) {
                fprintf(stderr, "flushed handle: %p\n", option.file);
                fflush(option.file);
            } else {
                fprintf(stderr, "flush request ignored. no handle");
            }
            return;
        case 0:
        case SIGTERM:
        case SIGINT:
            if (option.file) {
                fflush(option.file);
                fclose(option.file);
            }
            exit(0);
        default:
            break;
    }
}

static void usage(char *prog) {
    char *sep;
    char *name;

    sep = strrchr(prog, '/');
    name = prog;
    if (sep) {
        name = sep + 1;
    }
    printf("usage: %s [OPTIONS] <PID>\n"
           "-h        this help message\n"
           "-c        clobber output file if it exists\n"
           "-s        set sample rate (default: %0.2lf)\n"
           "\n", name, option.sample_rate);
}

/**
 * Parse program arguments and update global config
 * @param argc
 * @param argv
 */
void parse_options(int argc, char *argv[]) {
    if (argc < 2) {
        usage(argv[0]);
        exit(1);
    }
    for (int x = 0, i = 1; i < argc; i++) {
        if (strlen(argv[i]) > 1 && *argv[i] == '-') {
            char *arg = argv[i] + 1;
            if (!strcmp(arg, "h")) {
                usage(argv[0]);
                exit(0);
            }
            if (!strcmp(arg, "v")) {
                option.verbose = 1;
            } else if (!strcmp(arg, "s")) {
               option.sample_rate = strtod(argv[i+1], NULL);
               i++;
            } else if (!strcmp(arg, "c")) {
                option.clobber = 1;
            }
        } else {
            option.pid = (pid_t) strtol(argv[i], NULL, 10);
            x++;
        }
    }
}

/**
 * Check if `pid` directory exists in /proc
 * @param pid
 * @return 0 if exists and can read the directory. -1 if not
 */
int pid_exists(pid_t pid) {
    char path[PATH_MAX] = {0};
    snprintf(path, sizeof(path) - 1, "/proc/%d", pid);
    return access(path, F_OK | R_OK | X_OK);
}

/**
 * Check /proc/`pid` provides the smaps_rollups file
 * @param pid
 * @return 0 if exists and can read the file. -1 if not
 */
int smaps_rollup_usable(pid_t pid) {
    char path[PATH_MAX] = {0};
    snprintf(path, sizeof(path) - 1, "/proc/%d/smaps_rollup", pid);
    return access(path, F_OK | R_OK);
}


int main(int argc, char *argv[]) {
    struct mstat_record_t record;
    char path[PATH_MAX];

    // Set default options
    option.sample_rate = 1;
    option.verbose = 0;
    option.clobber = 0;
    // Set options based on arguments
    parse_options(argc, argv);

    // Allow user to flush the data stream with USR1
    signal(SIGUSR1, handle_interrupt);
    // Always attempt to exit cleanly
    signal(SIGINT, handle_interrupt);
    signal(SIGTERM, handle_interrupt);

    // For each PID passed to the program, begin gathering stats
    sprintf(path, "%d.mstat", option.pid);

    if (pid_exists(option.pid) < 0) {
        fprintf(stderr, "no pid %d\n", option.pid);
        exit(1);
    }

    if (smaps_rollup_usable(option.pid) < 0) {
        fprintf(stderr, "pid %d: %s\n", option.pid, strerror(errno));
        exit(1);
    }

    if (access(path, F_OK) == 0) {
        if (option.clobber) {
            remove(path);
            fprintf(stderr, "%s clobbered\n", path);
        } else {
            fprintf(stderr, "%s file already exists\n", path);
            exit(1);
        }
    }

    option.file = mstat_open(path);
    if (!option.file) {
        perror(path);
        exit(1);
    }

    if (mstat_check_header(option.file)) {
        mstat_write_header(option.file);
    }


    size_t i;
    struct timespec ts_start, ts_end;

    // Begin tracking time.
    clock_gettime(CLOCK_MONOTONIC, &ts_start);

    // Begin sample loop
    printf("PID: %d\nSamples per second: %.2lf\n(interrupt with ctrl-c...)\n", option.pid, option.sample_rate);
    i = 0;
    while (1) {
        memset(&record, 0, sizeof(record));
        record.pid = option.pid;

        // Record run time since last call
        clock_gettime(CLOCK_MONOTONIC, &ts_end);
        record.timestamp = mstat_difftimespec(ts_end, ts_start);

        // Sample memory values
        if (mstat_attach(&record, record.pid) < 0) {
            fprintf(stderr, "lost pid %d\n", option.pid);
            break;
        }

        if (option.verbose) {
            printf("pid: %d, sample: %zu, elapsed: %lf, rss: %li\n", record.pid, i, record.timestamp, record.rss);
        }

        if (mstat_write(option.file, &record) < 0) {
            fprintf(stderr, "Unable to write record to mstat file for pid %d\n", option.pid);
            break;
        }
        usleep((int) (1e6 / option.sample_rate));
        i++;
    }

    handle_interrupt(0);
    return 0;
}