aboutsummaryrefslogtreecommitdiffstats
path: root/src/opts.c
blob: 4a64b5b36d8bb6525a43e86781584a0fe69a0a10 (plain)
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* ==========================================================================
    Licensed under BSD 2clause license. See LICENSE file for more information
    Author: Michał Łyszczek <michal.lyszczek@bofc.pl>
   ========================================================================== */

/* ==== Include files ======================================================= */


#include "opts.h"

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "utils.h"
#include "version.h"


/* ==== Private macros ====================================================== */


#define HAS_OPTARG()                                                    \
    do                                                                  \
    {                                                                   \
        if (*optarg == '\0')                                            \
        {                                                               \
            fprintf(stderr, "missing argument for option '%c'\n", opt); \
            return -2;                                                  \
        }                                                               \
    }                                                                   \
    while (0);


/* ==== Global variables ==================================================== */


struct opts opts;


/* ==== Private functions =================================================== */


/* ==========================================================================
    function resets 'opts' variable to default values
   ========================================================================== */


static void opts_reset(void)
{
    opts.block_size = 16 * 1024;
    opts.report_intvl = 100 * 1024 * 1024;
    opts.num_intvl = 10;
    opts.method = METHOD_MEMCPY;
    opts.cache_size = 1 * 1024 * 1024;

#if HAVE_CLOCK_GETTIME
    opts.clock = CLK_REALTIME;
#else
    opts.clock = CLK_CLOCK;
#endif
}


/* ==========================================================================
    returns argument from 'argv'. If passed 'argv' doesn't hold valid argument
    -1 will be returned.
   ========================================================================== */


static char opts_get
(
    char*  argv  /* pointer to option */
)
{
    if (argv == NULL || argv[0] != '-' || islower(argv[1]) == 0)
    {
        return -1;
    }

    return argv[1];
}


/* ==========================================================================
    Prints help message. Who would suspect?

    Function uses more than one print, as ANSI C only requires compilers to
    support at least 509 long string literal
   ========================================================================== */


static void opts_print_help(void)
{
    printf(
"usage: memperf [-h | -v | options]\n"
"\n"
"arguments *MUST* be separated, ie -s -p is good while -sp is not\n"
"parameters must be next to argument like '-b1024' not '-b 1024'\n"
"\n"
"options:\n"
"\t-h           this help message\n"
"\t-v           prints version and exists\n"
"\t-b<mbytes>   size of a single memory block\n"
"\t-r<mbytes>   print report every 'mbytes' copied\n"
"\t-l<mbytes>   size of the cpu cache, if 0 cache flush is disabled\n"
"\t-i<number>   number of intervals\n"
);

    printf(
"\t-m<method>   copying method\n"
"\t-c<clock>    clock to use to calculate bandwith\n"
"\n"
"methods:\n"
"\tmemcpy       copy data using buildin memcpy function\n"
"\tbbb          byte by byte copy, simple for loop\n"
"\n"
"clocks:\n"
#if HAVE_CLOCK_GETTIME
"\trealtime     posix CLOCK_REALTIME clock is used\n"
#endif
"\tclock        clock_t is used\n"
);
}

/* ==== Public functions ==================================================== */


/* ==========================================================================
    resets global 'opts' object with default values, and parses input options
    overwritting default settings. If help (-h) or version (-v) option is found
    anywhere in the argv, program prints what first was found and returns with
    code 1.

    returns:
             1      -h or -v was passed
             0      all options parsed, program can continue
            -1      syntax error (ie not started with '-')
            -2      invalid argument for the option
            -3      given option is not recognized (not available)
   ========================================================================== */


int opts_parse
(
    int    argc,   /* number of arguments */
    char  *argv[]  /* array of arguments */
)
{
    float  tmp;    /* temp variable for parsing */
    long   mul;    /* multiplication for <mbytes> arguments */
    char  *ep;     /* error pointer of strtol function */
    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

    /*
     * first set program options to default
     */

    opts_reset();

    /*
     * skip program name
     */

    argc--;
    argv++;

    while (argc--)
    {
        signed char  opt;     /* current option */
        char        *optarg;  /* current option argument */
        /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

        optarg = (*argv) + 2;

        /*
         * read option, only "-C' is allowed where 'C' is [a-z]. pointer to
         * 'C' is returned and argv is set to point to next option
         */

        if ((opt = opts_get(*argv++)) == -1)
        {
            fprintf(stderr, "Syntax error in arguments\n");
            opts_print_help();
            return -1;
        }

        switch (opt)
        {
        case 'v':
            printf(APP_VERSION "\n");
            return 1;

        case 'h':
            opts_print_help();
            return 1;

        case 'b':
        case 'r':
        case 'l':
            HAS_OPTARG();

            tmp = (float)strtod(optarg, &ep);
            mul = 1;

            if (*ep)
            {
                switch (*ep)
                {
                case 'G':
                    mul *= 1024;

                case 'M':
                    mul *= 1024;

                case 'K':
                    mul *= 1024;
                    break;

                default:
                    fprintf(stderr,
                            "parameter %s for argument '%c' is invalid\n",
                            optarg,
                            opt);
                    return -2;
                }
            }

            if (opt == 'b')
            {
                opts.block_size = tmp * mul;
            }
            else if (opt == 'r')
            {
                opts.report_intvl = tmp * mul;
            }
            else
            {
                opts.cache_size = tmp * mul;
            }

            break;

        case 'i':
            HAS_OPTARG();

            tmp = strtol(optarg, &ep, 10);

            if (*ep)
            {
                fprintf(stderr,
                        "parameter %s for argument 'i' is invalid\n",
                        optarg);
                return -2;
            }

            opts.num_intvl = tmp;
            break;

        case 'c':
            HAS_OPTARG();

#if HAVE_CLOCK_GETTIME
            if (strcmp(optarg, "realtime") == 0)
            {
                opts.clock = CLK_REALTIME;
            }
            else
#endif

            if (strcmp(optarg, "clock") == 0)
            {
                opts.clock = CLK_CLOCK;
            }
            else
            {
                fprintf(stderr,
                        "parameter %s for optargument 'c' is invalid\n",
                        optarg);
                return -2;
            }

            break;

        case 'm':
            HAS_OPTARG();

            if (strcmp(optarg, "memcpy") == 0)
            {
                opts.method = METHOD_MEMCPY;
            }
            else if (strcmp(optarg, "bbb") == 0)
            {
                opts.method = METHOD_BBB;
            }
            else
            {
                fprintf(stderr,
                        "parameter %s for optargument 'm' is invalid\n",
                        optarg);
                return -2;
            }

            break;

        default:
            fprintf(stderr,
                    "Unknown option: %c (%02x)\n",
                    *optarg,
                    (unsigned char) *optarg);
            opts_print_help();
            return -3;
        }
    }

    return 0;
}