chiark / gitweb /
@@@ bench wip
[mLib] / test / tvec-main.c
1 /* -*-c-*-
2  *
3  * Main entry point for test-vector processing
4  *
5  * (c) 2023 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of the mLib utilities library.
11  *
12  * mLib is free software: you can redistribute it and/or modify it under
13  * the terms of the GNU Library General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or (at
15  * your option) any later version.
16  *
17  * mLib is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
20  * License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License along with mLib.  If not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
25  * USA.
26  */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include "config.h"
31
32 #include <ctype.h>
33 #include <errno.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include <unistd.h>
39
40 #include "bench.h"
41 #include "dstr.h"
42 #include "macros.h"
43 #include "mdwopt.h"
44 #include "quis.h"
45 #include "report.h"
46 #include "tvec.h"
47
48 /*----- Main code ---------------------------------------------------------*/
49
50 /* Table of output formats. */
51 static const struct outform {
52   const char *name;
53   struct tvec_output *(*makefn)(FILE *fp);
54 } outtab[] = {
55   { "human",            tvec_humanoutput },
56   { "tap",              tvec_tapoutput },
57   { 0,                  0 }
58 };
59
60 /* Configuration for ad-hoc testing. */
61 const struct tvec_config tvec_adhocconfig =
62   { 0, 1, 1, sizeof(struct tvec_reg) };
63
64 /* Common benchmark state. */
65 static struct bench_state bench;
66
67 /* --- @find_outform@ ---
68  *
69  * Arguments:   @const char *p@ = output name
70  *
71  * Returns:     Pointer to output format record.
72  *
73  * Use:         Looks up an output format by name.  Reports a fatal error if
74  *              no matching record is found.
75  */
76
77 static const struct outform *find_outform(const char *p)
78 {
79   const struct outform *best = 0, *of;
80   size_t n = strlen(p);
81
82   for (of = outtab; of->name; of++)
83     if (!STRNCMP(p, ==, of->name, n))
84       ;
85     else if (!of->name[n])
86       return (of);
87     else if (best)
88       die(2, "ambiguous output format name (`%s' or `%s'?)",
89           best->name, of->name);
90     else
91       best = of;
92   if (best) return (best);
93   else die(2, "unknown output format `%s'", optarg);
94 }
95
96 /* --- @version@, @usage@, @help@ --- *
97  *
98  * Arguments:   @FILE *fp@ = stream to write on
99  *
100  * Returns:     ---
101  *
102  * Use:         Output information about the program.
103  */
104
105 static void version(FILE *fp)
106   { pquis(fp, "$, mLib test-vector framework version " VERSION "\n"); }
107
108 static void usage(FILE *fp)
109 {
110   pquis(fp, "\
111 usage: $ [-B CONFIG] [-f FORMAT] [-o OUTPUT] [-t SECS] [TEST-FILE ...]\n\
112 ");
113 }
114
115 static void help(FILE *fp)
116 {
117   version(fp); fputc('\n', fp);
118   usage(fp); fputs("\
119 Options:\n\
120   -h, --help                    show this help text.\n\
121   -v, --version                 show version number.\n\
122   -u, --usage                   show usage synopsis.\n\
123 \n\
124   -B, --bench-config=CONFIG     set benchmark configuration string.\n\
125   -f, --format=FORMAT           produce output in FORMAT.\n\
126   -o, --output=OUTPUT           write output to OUTPUT file.\n\
127   -t, --target-time=DURATION    run benchmarks for DURATION.\n\
128 ", fp);
129 }
130
131 /* --- @tvec_parseargs@ --- *
132  *
133  * Arguments:   @int argc@ = number of command-line arguments
134  *              @char *argv[]@ = vector of argument strings
135  *              @struct tvec_state *tv_out@ = test vector state to initialize
136  *              @int *argpos_out@ = where to leave unread argument index
137  *              @const struct tvec_config *cofig@ = test vector configuration
138  *
139  * Returns:     ---
140  *
141  * Use:         Parse arguments and set up the test vector state @*tv_out@.
142  *              If errors occur, print messages to standard error and exit
143  *              with status 2.
144  *
145  *              This function also establishes a common benchmark state.
146  */
147
148 void tvec_parseargs(int argc, char *argv[], struct tvec_state *tv_out,
149                     int *argpos_out, const struct tvec_config *config)
150 {
151   FILE *ofp = 0;
152   const struct outform *of = 0;
153   struct tvec_output *o;
154   const char *benchconf = 0;
155   double benchtime = 1.0, scale;
156   struct bench_timer *tm;
157   const char *p; char *q;
158   int opt;
159   unsigned f = 0;
160 #define f_bogus 1u
161 #define f_bench 2u
162
163   static const struct option options[] = {
164     { "help",           0,              0,      'h' },
165     { "version",        0,              0,      'v' },
166     { "usage",          0,              0,      'u' },
167
168     { "bench-config",   OPTF_ARGREQ,    0,      'B' },
169     { "format",         OPTF_ARGREQ,    0,      'f' },
170     { "output",         OPTF_ARGREQ,    0,      'o' },
171     { "target-time",    OPTF_ARGREQ,    0,      't' },
172     { 0,                0,              0,      0 }
173   };
174
175   ego(argv[0]);
176   for (;;) {
177     opt = mdwopt(argc, argv, "hvu" "B:f:o:t:", options, 0, 0, 0);
178     if (opt < 0) break;
179     switch (opt) {
180       case 'h': help(stdout); exit(0);
181       case 'v': version(stdout); exit(0);
182       case 'u': usage(stdout); exit(0);
183
184       case 'B': benchconf = optarg; f |= f_bench; break;
185       case 'f': of = find_outform(optarg); break;
186       case 'o':
187         if (ofp) fclose(ofp);
188         ofp = fopen(optarg, "w");
189         if (!ofp)
190           die(2, "failed to open `%s' for writing: %s",
191               optarg, strerror(errno));
192         break;
193       case 't':
194         if (*optarg != '.' && !ISDIGIT(*optarg)) goto bad_time;
195         errno = 0; benchtime = strtod(optarg, &q);
196         if (errno) goto bad_time;
197         p = q;
198         if (ISSPACE(*p)) {
199           do p++; while (ISSPACE(*p));
200           if (!*p) goto bad_time;
201         }
202         if (tvec_parsedurunit(&scale, &p)) goto bad_time;
203         if (*p) goto bad_time;
204         benchtime *= scale; f |= f_bench;
205         break;
206       bad_time:
207         die(2, "invalid time duration `%s'", optarg);
208
209       default:
210         f |= f_bogus;
211         break;
212     }
213   }
214   if (f&f_bogus) { usage(stderr); exit(2); }
215   if (!ofp) ofp = stdout;
216   if (!of) {
217     p = getenv("TVEC_FORMAT");
218     if (p) of = find_outform(p);
219   }
220   if (of) o = of->makefn(ofp);
221   else o = tvec_dfltout(ofp);
222
223   tm = bench_createtimer(benchconf);
224   if (tm) {
225     bench_init(&bench, tm); bench.target_s = benchtime;
226     tvec_benchstate = &bench;
227   } else if (f&f_bench) {
228     moan("failed to create benchmark timer");
229     if (!getenv("MLIB_BENCH_DEBUG"))
230       moan("set `MLIB_BENCH_DEBUG=t' in the envionment for more detail");
231     exit(2);
232   }
233
234   tvec_begin(tv_out, config, o); *argpos_out = optind;
235 }
236
237 /* --- @tvec_readstdin@, @tvec_readfile@, @tvec_readarg@ --- *
238  *
239  * Arguments:   @struct tvec_state *tv@ = test vector state
240  *              @const char *file@ = pathname of file to read
241  *              @const char *arg@ = argument to interpret
242  *
243  * Returns:     Zero on success, @-1@ on error.
244  *
245  * Use:         Read test vector data from stdin or a named file.  The
246  *              @tvec_readarg@ function reads from stdin if @arg@ is `%|-|%',
247  *              and from the named file otherwise.
248  */
249
250 int tvec_readstdin(struct tvec_state *tv)
251   { return (tvec_read(tv, "<stdin>", stdin)); }
252
253 int tvec_readfile(struct tvec_state *tv, const char *file)
254 {
255   FILE *fp = 0;
256   int rc;
257
258   fp = fopen(file, "r");
259   if (!fp) {
260     tvec_error(tv, "failed to open `%s' for reading: %s",
261                file, strerror(errno));
262     rc = -1; goto end;
263   }
264   if (tvec_read(tv, file, fp)) { rc = -1; goto end; }
265   rc = 0;
266 end:
267   if (fp) fclose(fp);
268   return (rc);
269 }
270
271 int tvec_readarg(struct tvec_state *tv, const char *arg)
272 {
273   int rc;
274
275   if (STRCMP(arg, ==, "-")) rc = tvec_readstdin(tv);
276   else rc = tvec_readfile(tv, arg);
277   return (rc);
278 }
279
280 /* --- @tvec_readdflt@ --- *
281  *
282  * Arguments:   @struct tvec_state *tv@ = test vector state
283  *              @const char *dflt@ = defsault filename or null
284  *
285  * Returns:     Zero on success, @-1@ on error.
286  *
287  * Use:         Reads from the default test vector data.  If @file@ is null,
288  *              then read from standard input, unless that's a terminal; if
289  *              @file@ is not null, then read the named file, looking in the
290  *              directory named by the `%|srcdir|%' environment variable if
291  *              that's set, or otherwise in the current directory.
292  */
293
294 int tvec_readdflt(struct tvec_state *tv, const char *dflt)
295 {
296   dstr d = DSTR_INIT;
297   const char *p;
298   int rc;
299
300   if (dflt) {
301     p = getenv("srcdir");
302     if (p) { dstr_putf(&d, "%s/%s", p, dflt); dflt = d.buf; }
303     rc = tvec_readfile(tv, dflt);
304   } else if (isatty(0))
305     rc = tvec_error(tv, "use `-' to force reading from interactive stdin");
306   else
307     rc = tvec_readstdin(tv);
308   dstr_destroy(&d);
309   return (rc);
310 }
311
312 /* --- @tvec_readargs@ --- *
313  *
314  * Arguments:   @int argc@ = number of command-line arguments
315  *              @char *argv[]@ = vector of argument strings
316  *              @struct tvec_state *tv@ = test vector state
317  *              @int *argpos_inout@ = current argument position (updated)
318  *              @const char *dflt@ = default filename or null
319  *
320  * Returns:     Zero on success, @-1@ on error.
321  *
322  * Use:         Reads from the sources indicated by the command-line
323  *              arguments, in order, interpreting each as for @tvec_readarg@;
324  *              if no arguments are given then read from @dflt@ as for
325  *              @tvec_readdflt@.
326  */
327
328 int tvec_readargs(int argc, char *argv[], struct tvec_state *tv,
329                    int *argpos_inout, const char *dflt)
330 {
331   int i = *argpos_inout;
332   int rc;
333
334   if (i == argc)
335     rc = tvec_readdflt(tv, dflt);
336   else {
337     rc = 0;
338     while (i < argc)
339       if (tvec_readarg(tv, argv[i++])) rc = -1;
340   }
341   *argpos_inout = i;
342   return (rc);
343 }
344
345 /* --- @tvec_main@ --- *
346  *
347  * Arguments:   @int argc@ = number of command-line arguments
348  *              @char *argv[]@ = vector of argument strings
349  *              @const struct tvec_config *cofig@ = test vector configuration
350  *              @const char *dflt@ = default filename or null
351  *
352  * Returns:     Exit code.
353  *
354  * Use:         All-in-one test vector front-end.  Parse options from the
355  *              command-line as for @tvec_parseargs@, and then process the
356  *              remaining positional arguments as for @tvec_readargs@.  The
357  *              function constructs and disposes of a test vector state.
358  */
359
360 int tvec_main(int argc, char *argv[],
361               const struct tvec_config *config, const char *dflt)
362 {
363   struct tvec_state tv;
364   int argpos;
365
366   tvec_parseargs(argc, argv, &tv, &argpos, config);
367   tvec_readargs(argc, argv, &tv, &argpos, dflt);
368   return (tvec_end(&tv));
369 }
370
371 /*----- That's all, folks -------------------------------------------------*/