summaryrefslogtreecommitdiffstats
path: root/contrib/diags/data_prep.c
blob: 93176a99b5f0c4e2fc0a7c4df1a0e87fb714c15a (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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/**
 ** Data preparation for diagnostic interfaces
 **
 ** Copyright 2009-2011 Gary Wuertz gary@issiweb.com
 ** Copyright 2011 BenEleventh Consulting manolson@beneleventh.com
 **
 ** This program is free software: you can redistribute it and/or modify
 ** it under the terms of the GNU General Public License as published by
 ** the Free Software Foundation, either version 3 of the License, or
 ** (at your option) any later version.
 **
 ** This program is distributed in the hope that it will be useful,
 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 ** GNU General Public License for more details.
 **
 ** You should have received a copy of the GNU General Public License
 ** along with this program.  If not, see <http://www.gnu.org/licenses/>.
 **
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <math.h>

/**
 * Something borrowed......
 */
#define  APP_BUFF_SIZE  1024
#define  NDSIZECOLLECT  0x1000000

typedef unsigned int U_INT;

struct pparams {
   int   cmd;        // command to execute
   int   options;    // debug options
   char  *outpath;   // output file;
   FILE  *input;     // input file
   FILE  *output;    // output file
   U_INT limit;      // limit delta
   U_INT repeat;     // repeat values
   U_INT value;      // inject value
   U_INT bsize;      // Buffer size
   double   xs;      // X scale
};
/**
 *  For bin output
 */
#define X_BINS  512
#define Y_BINS  2048
/**
 * Divide data into bins
 */
#define X_FACTOR (1.0 * X_BINS)/(1.0 * p->bsize)
#define Y_FACTOR (0.5 * Y_BINS)/(1.0 * 0x7fffffff)
/*
 * Scale bins back to values x = 1M, y=4.0G
 */
#define X_SCALE (p->xs/X_BINS)
#define Y_SCALE (4.0/Y_BINS)

static int  inject_output(struct pparams *p);
static void matrix_output(struct pparams *p);
static void sequence_output(struct pparams *p);
static void usage(int nopts, struct option *long_options, const char **cmds);
/**
 * Data Prep
 */
int main(int argc, char **argv)
{
   static const char* cmds[] = {
      "b", "buffer",    "1", "buffer size (k): default 1024",
      "f", "file",      "1", "output file name: default is '-' (stdout)",
      "i", "inject",    "1", "inject 0=2up, 1=1up, 2=raw 1up",
      "o", "output",    "1", "[bin|delta|inject|raw|xor|wrap] data",
      "r", "repeat",    "1", "repeat inject sequence",
      "s", "start",     "1", "start value inject sequence",
      "u", "upper",     "1", "inject sequence upper bound",
      "v", "verbose",   "1", "verbose reporting",
      "h", "help",      "0", "This help"
      };
   char  *outputs[] = {"bin","delta","inject","raw","xor","wrap",NULL};
   static int nopts = sizeof(cmds)/(4*sizeof(char *));
   struct option long_options[nopts+1];
   char short_options[1+nopts*2];
   struct pparams params;
   FILE *f;
   U_INT i, j, n;
   int   c;
   char *s;

   for(i=j=0;j<(nopts*4);j+=4) {
      long_options[i].name      = cmds[j+1];
      long_options[i].has_arg   = atoi(cmds[j+2]);
      long_options[i].flag      = NULL;
      long_options[i].val       = cmds[j][0];
      strcat(short_options,cmds[j]);
      if (long_options[i].has_arg!=0) strcat(short_options,":");
      i += 1;
      }
   memset(&long_options[i], 0, sizeof(struct option));
   memset(&params, 0, sizeof(struct pparams));
   params.outpath = "-";
   params.bsize = NDSIZECOLLECT;
   params.xs    = 1.0;
   do {
      c = getopt_long (argc, argv, short_options, long_options, NULL);
      switch(c) {
         case 'b':
            params.bsize = atoi(optarg);
            params.xs = params.bsize;
            while(params.xs >= 10.0)
               params.xs /= 10.0;
            break;
         case 'f':
            params.outpath = optarg;
            break;
         case 'i':
            params.options = atoi(optarg);
            break;
         case 'o':
            n = strlen(optarg);
            for(i=0;outputs[i]!=NULL;i++)
               if (!strncmp(optarg, outputs[i], n)) {
                  params.cmd = optarg[0];
                  break;
               }
            break;
         case 'r':
            params.repeat = atoi(optarg);
            break;
         case 's':
            params.value = atoi(optarg);
            break;
         case 'u':
            params.limit = atoi(optarg);
            break;
         case 'v':
            params.cmd = atoi(optarg);
            break;
         case '?':
         case 'h':
            usage(nopts, long_options, cmds);
         case -1:
            break;
         }
      } while (c!=-1);

   if (0==params.cmd || optind != (argc-1))
      usage(nopts, long_options, cmds);
   if (!strcmp(argv[1],"-"))
      params.input = stdin;
   else {
      params.input = fopen(argv[optind], "rb");
      if (NULL == params.input) {
         fprintf(stderr, "Unable to open input %s\n", argv[optind]);
         exit(2);
         }
      }
   if (!strcmp(params.outpath, "-"))
      params.output = stdout;
   else {
      params.output = fopen(params.outpath, "wb");
      if (NULL == params.output) {
         fprintf(stderr, "Unable to open %s\n", params.outpath);
         exit(3);
         }
      fprintf(stdout, "writing to %s\n", params.outpath);
      }
   switch(params.cmd) {
         case 'i':
            while(inject_output(&params)>0)
               ;
            break;
         case 'b':
            matrix_output(&params);
            break;
         case 'd':   case 'r':  case 'x':  case 'w':
            sequence_output(&params);
            break;
      }
   if (params.output != stdout)
      fclose(params.output);
   return 0;
}
/**
 * Create injection data - input file is log10 sequence data - can be repeated
 */
static int inject_output(struct pparams *p)
{
   U_INT    buf[APP_BUFF_SIZE];
   char     ibuf[80], *s;
   U_INT    i, j;
   double   n, delta;
   int      rv = 1;
   
   n = 0;
   for(i=0;i<APP_BUFF_SIZE && rv==1;i++) {
      buf[i] = p->value;
      s = fgets(ibuf, 80, p->input);
      if (NULL == s && p->repeat != 0) {
         p->repeat -= 1;
         rewind(p->input);
         s = fgets(ibuf, 80, p->input);
         }
      if (NULL != s) {
         if (p->options!=0)
            delta = strtod(ibuf, NULL);
         else {
            n = strtod(ibuf, &s);
            delta = strtod(s, NULL);
            }
         if (p->limit != 0 && delta > p->limit)
            delta -= p->limit;
         if (p->options == 2)
            p->value = (U_INT) delta;
         else p->value += (U_INT)pow(10.0,delta);
         }
      else rv = 0;
      }
   if (i != fwrite(buf, sizeof(U_INT), i, p->output)) {
      printf("Write error\n");
      rv = -1;
      }
   return rv;
}
/**
 * Create matrix data file
 */
static void matrix_output(struct pparams *p)
{
   U_INT   buf[APP_BUFF_SIZE];
   U_INT   **matrix;
   FILE    *f = p->input;
   int     i, n, sz, x, y;
   
   matrix = (U_INT **) malloc(sizeof(U_INT **) * X_BINS);
   if (NULL == matrix) {
      fprintf(stderr, "Unable to allocate cols\n");
      return;
      }
   sz = sizeof(U_INT *) * Y_BINS;
   for (i = 0;i< X_BINS;i++) {
       matrix[i] = (U_INT *)malloc(sz);
       if (NULL == matrix[i]) {
           fprintf(stderr, "Unable to allocate row\n");
           return;
           }
       memset(matrix[i], 0, sz);
       }
   n = 0;
   while(1) {
      sz = fread(buf, sizeof(U_INT), APP_BUFF_SIZE, f);
      if (sz < 1)
          break;
      for(i=0;i<sz;i++) {
          x = (int)(n * X_FACTOR);
          y = (int)(buf[i] * Y_FACTOR);
          matrix[x][y] += 1; 
          n += 1;
          n %= p->bsize;
          }   
      }
   for(x=0;x<X_BINS;x++)
      for(y=0;y<Y_BINS;y++)
         if (matrix[x][y]!=0)
            fprintf(p->output,"%g\t%g\t%u\n", x*X_SCALE, y*Y_SCALE, matrix[x][y]);
}
/**
 * Create sequence data file
 */
static void sequence_output(struct pparams *p)
{
   U_INT   buf[APP_BUFF_SIZE];
   FILE    *f = p->input;
   int     i, m, n, sz;
   U_INT   delta, cur, prev;
   U_INT   plus, minus;

   m = p->cmd=='r'? 1 : 0;
   n = 0;
   plus = minus = 0;
   while(1) {
      sz = fread(buf, sizeof(U_INT), APP_BUFF_SIZE, f);
      if (sz < 1)
          break;
      for(i=0;i<sz;i++) {
         prev = cur;
         cur = buf[i];
         if (m==0) {
             m = 1;
             }
         else switch(p->cmd) {
            case 'd':
               if (cur < prev)
                  delta = prev - cur;
               else delta = cur - prev;
               fprintf(p->output,"%g\t%g\n", n * 10.0/1024.0, log10(delta));
               break;
            case 'x':
               fprintf(p->output,"%g\t%g\n", n * 10.0/1024.0, log10(cur^prev));
               break;
            case 'r':
               fprintf(p->output,"%g\t%g\n", n * 10.0/1024.0, 1.0 * cur);
               break;
            case 'w':
               if (cur < prev) {
                  if (p->options & 1)
                     fprintf(p->output,"rollover %d\n", n);
                  minus++;
                  }
               else plus++;
               break;
            }
         n += 1;
         n %= p->bsize;
         }
      }
   if (p->cmd=='w')
      fprintf(p->output,"Rollover %u/%u = %g\n", minus, plus, minus*100/(double)(minus+plus));
}
/**
 * usage
 */
static void usage(int nopts, struct option *long_options, const char **cmds)
{
   int i;
  
   fprintf(stderr, "\nUsage: %s [options] <file>\n\n", "data_prep");
   fprintf(stderr, "Prepare diagnostic data from <file>\n\n");
   fprintf(stderr, "  Options:\n");
   for(i=0;long_options[i].val != 0;i++) {
      fprintf(stderr,"     --%-10s, -%c %s %s\n",
         long_options[i].name, long_options[i].val,
         long_options[i].has_arg? "[]":"  ",cmds[4*i+3]);
      }
   fprintf(stderr, "\n");
   exit(1);
}