File: disk_parse.c

package info (click to toggle)
raft 0.22.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,504 kB
  • sloc: ansic: 37,539; makefile: 264; sh: 77; python: 22
file content (121 lines) | stat: -rw-r--r-- 3,067 bytes parent folder | download | duplicates (2)
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
#include <argp.h>
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "disk.h"
#include "disk_parse.h"

#define MEGABYTE (1024 * 1024)

static char doc[] = "Benchmark sequential write performance\n";

/* Order of fields: {NAME, KEY, ARG, FLAGS, DOC, GROUP}.*/
static struct argp_option options[] = {
    {"dir", 'd', "DIR", 0, "Directory to use for temp files (default '.')", 0},
    {"buf", 'b', "BUF", 0, "Write buffer size (default 4096)", 0},
    {"size", 's', "S", 0, "Size of the file to write (default 8M)", 0},
    {"perf", 'p', NULL, 0, "Report kernel subsystems performance metrics", 0},
    {"trace", 't', "TRACE", 0, "Comma-separated kernel subsystems to trace", 0},
    {0}};

static error_t argpParser(int key, char *arg, struct argp_state *state);

static struct argp argp = {
    .options = options,
    .parser = argpParser,
    .doc = doc,
};

/* Parse a comma-separated list of kernels subsystem names. */
static void parseTracing(struct diskOptions *opts, char *arg)
{
    char *token;
    unsigned n_tokens = 1;
    unsigned i;

    /* Count the number of comma-separated tokens in this argument. */
    for (i = 0; i < strlen(arg); i++) {
        if (arg[i] == ',')
            n_tokens++;
    }

    for (i = 0; i < n_tokens; i++) {
        if (i == 0) {
            token = strtok(arg, ",");
        } else {
            token = strtok(NULL, ",");
        }
        assert(token != NULL);
        opts->traces[opts->n_traces] = token;
        opts->n_traces++;
    }
}

static error_t argpParser(int key, char *arg, struct argp_state *state)
{
    struct diskOptions *opts = state->input;

    switch (key) {
        case 'd':
            opts->dir = arg;
            break;
        case 'b':
            opts->buf = (size_t)atoi(arg);
            break;
        case 's':
            opts->size = (unsigned)atoi(arg);
            break;
        case 'p':
            opts->perf = true;
            break;
        case 't':
            parseTracing(opts, arg);
            break;
        default:
            return ARGP_ERR_UNKNOWN;
    }

    return 0;
}

static void optionsInit(struct diskOptions *opts)
{
    opts->dir = ".";
    opts->buf = 4096;
    opts->size = 8 * MEGABYTE;
    opts->perf = false;
    opts->n_traces = 0;
}

static void optionsCheck(struct diskOptions *opts)
{
    if (opts->buf == 0 || opts->buf > MEGABYTE) {
        printf("Invalid buffer size %zu\n", opts->buf);
        exit(1);
    }
    if (opts->size == 0 || opts->size % 4096 != 0) {
        printf("Invalid file size %u\n", opts->size);
        exit(1);
    }
    if (opts->perf && getuid() != 0) {
        printf("Performance measurment requires root\n");
        exit(1);
    }
    if (opts->n_traces > 0 && getuid() != 0) {
        printf("Tracing requires root\n");
        exit(1);
    }
}

void DiskParse(int argc, char *argv[], struct diskOptions *opts)
{
    optionsInit(opts);

    argv[0] = "benchmark/run disk";
    argp_parse(&argp, argc, argv, 0, 0, opts);

    optionsCheck(opts);
}