File: osbf-util.c

package info (click to toggle)
crm114 20100106-9
  • links: PTS
  • area: main
  • in suites: buster
  • size: 3,184 kB
  • sloc: ansic: 34,910; sh: 617; makefile: 578; lisp: 208
file content (518 lines) | stat: -rw-r--r-- 14,158 bytes parent folder | download | duplicates (6)
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
//	osbf-util.c - utility for munging css files, version X0.1

// Copyright 2004 Fidelis Assis
// Copyright 2004-2009 William S. Yerazunis.
// This file is under GPLv3, as described in COPYING.

//  OBS: This program is a modified version of the original cssutil,
//       specific for the new osbf format. It is not compatible with
//       the original css format. -- Fidelis Assis

//  include some standard files
#include "crm114_sysincludes.h"

//  include any local crm114 configuration file
#include "crm114_config.h"

//  include the crm114 data structures file
#include "crm114_structs.h"

//  and include the routine declarations file
#include "crm114.h"

#include "crm114_osbf.h"

char version[] = "1.1";

void
helptext ()
{
  fprintf (stdout,
	   "osbf-util version %s - generic osbf file utility.\n"
	   "Usage: osbfutil [options]... css-filename\n"
	   "		-b   - brief; print only summary\n"
	   "		-h   - print this help\n"
	   "		-q   - quite mode; no warning messages\n"
	   "		-r   - report then exit (no menu)\n"
	   "		-s css-size  - if no css file found, create new\n"
	   "			       one with this many buckets.\n"
	   "		-S css-size  - same as -s, but round up to next\n"
	   "			       2^n + 1 boundary.\n"
	   "		-v   - print version and exit\n"
	   "		-D   - dump css file to stdout in CSV format.\n"
	   "		-R csv-file  - create and restore css from CSV.\n"
	   "		               Options -s and -S are ignored when"
	   " restoring.\n", VERSION);
}

int
main (int argc, char **argv)
{

  long i, k;			//  some random counters, when we need a loop
  long v;
  long sparse_spectrum_file_length = OSBF_DEFAULT_SPARSE_SPECTRUM_FILE_LENGTH;
  long user_set_css_length = 0;
  long hfsize;
  long long sum;		// sum of the hits... can be _big_.
  int brief = 0, quiet = 0, dump = 0, restore = 0;
  int opt, fields;
  int report_only = 0;

  long *bcounts;
  long maxchain;
  long curchain;
  long totchain;
  long fbuckets;
  long nchains;
  long ofbins;

  char cmdstr[255];
  char cssfile[255];
  char csvfile[255];
  unsigned char cmdchr[2];
  char crapchr[2];
  float cmdval;
  int zloop, cmdloop, version_index;

  //    the following for crm114.h's happiness

  char *newinputbuf;
  newinputbuf = (char *) &hfsize;

  bcounts = malloc (sizeof (unsigned long) * OSBF_FEATUREBUCKET_VALUE_MAX);

  {
    struct stat statbuf;	//  filestat buffer
    OSBF_FEATURE_HEADER_STRUCT *header;	//  the header of the hash file
    OSBF_FEATUREBUCKET_STRUCT *hashes;	//  the text of the hash file

    // parse cmdline options
    while ((opt = getopt (argc, argv, "bDhR:rqs:S:v")) != -1)
      {
	switch (opt)
	  {
	  case 'b':
	    brief = 1;		// brief, no 'bin value ...' lines
	    break;
	  case 'D':
	    dump = 1;		// dump css file, no cmd menu
	    break;
	  case 'q':
	    quiet = 1;		// quiet mode, no warning messages
	    break;
	  case 'R':
	    {
	      FILE *f;
	      unsigned long key, hash, value;
	      OSBF_FEATURE_HEADER_STRUCT h;

	      // count lines to determine the number of buckets and check CSV format
	      if (user_trace)
		fprintf (stderr, "Opening OSBF file %s for read\n", optarg);
	      if ((f = fopen (optarg, "rb")) != NULL)
		{

		  // try to find the header reading first 2 "buckets"
		  if (fscanf
		      (f, "%lu;%lu;%lu\n", (unsigned long *) h.version,
		       &(h.flags), &(h.buckets_start)) != 3)
		    {
		      fprintf (stderr,
			       "\n %s is not in the right CSV format.\n",
			       optarg);
		      exit (EXIT_FAILURE);
		    }
		  if (*((unsigned long *) h.version) != OSBF_VERSION)
		    {
		      fprintf (stderr,
			       "\n %s is not an OSBF CSV file.\n", optarg);
		      fclose (f);
		      exit (EXIT_FAILURE);
		    }
		  if (fscanf (f, "%lu;%lu;%lu\n", &(h.buckets), &hash, &value)
		      != 3)
		    {
		      fprintf (stderr,
			       "\n %s is not in the right CSV format.\n",
			       optarg);
		      exit (EXIT_FAILURE);
		    }

		  // start with -headersize buckets, discounting 2 "buckets" alread read
		  sparse_spectrum_file_length = 2 - h.buckets_start;

		  while (!feof (f))
		    if (fscanf (f, "%lu;%lu;%lu\n", &key, &hash, &value) == 3)
		      sparse_spectrum_file_length++;
		    else
		      {
			fprintf (stderr,
				 "\n %s is not in the right CSV format.\n",
				 optarg);
			exit (EXIT_FAILURE);
		      }
		  fclose (f);

		  // check the number of buckets
		  if (sparse_spectrum_file_length != h.buckets)
		    {
		      fprintf (stderr,
			       "\n Wrong number of buckets! %s is not in the right CSV format.\n",
			       optarg);
		      exit (EXIT_FAILURE);
		    }
		  strcpy (csvfile, optarg);
		}
	      else
		{
		  fprintf (stderr,
			   "\n Couldn't open csv file %s; errno=%d.\n",
			   optarg, errno);
		  exit (EXIT_FAILURE);
		}
	    }
	    restore = 1;	// restore css file, no cmd menu
	    break;
	  case 'r':
	    report_only = 1;	// print stats only, no cmd menu.
	    break;
	  case 's':		// set css size to option value
	  case 'S':		// same as above but round up to next 2^n+1
	    if (restore)
	      {
		fprintf (stderr,
			 "\nOptions -s, -S ignored when restoring.\n");
		break;
	      }
	    if (sscanf (optarg, "%ld", &sparse_spectrum_file_length))
	      {
		if (!quiet)
		  fprintf (stderr,
			   "\nOverride css creation length to %ld\n",
			   sparse_spectrum_file_length);
		user_set_css_length = 1;
	      }
	    else
	      {
		fprintf (stderr,
			 "On -%c flag: Missing or incomprehensible number of buckets.\n",
			 opt);
		exit (EXIT_FAILURE);
	      }
	    if (opt == 'S')	// round up to next 2^n+1
	      {
		int k;

		k = (long) floor (log10 (sparse_spectrum_file_length - 1)
				  / log10 (2.0));
		while ((2 << k) + 1 < sparse_spectrum_file_length)
		  k++;
		sparse_spectrum_file_length = (2 << k) + 1;
		user_set_css_length = 1;
	      }
	    break;
	  case 'v':
	    fprintf (stderr, " This is osbf-util, version %s\n", version);
	    fprintf (stderr, " Copyright 2004-2006 William S. Yerazunis.\n");
	    fprintf (stderr,
		     " This software is licensed under the GPL with ABSOLUTELY NO WARRANTY\n");
	    exit (EXIT_SUCCESS);
	  default:
	    helptext ();
	    exit (EXIT_SUCCESS);
	    break;
	  }
      }

    if (optind < argc)
      strncpy (cssfile, argv[optind], sizeof (cssfile));
    else
      {
	helptext ();
	exit (EXIT_SUCCESS);
      }

    //       and stat it to get it's length
    k = stat (cssfile, &statbuf);
    //       quick check- does the file even exist?
    if (k == 0)
      {
	if (restore)
	  {
	    fprintf (stderr,
		     "\n.CSS file %s exists! Restore operation aborted.\n",
		     cssfile);
	    exit (EXIT_FAILURE);
	  }
	hfsize = statbuf.st_size;
	if (!quiet && user_set_css_length)
	  fprintf (stderr,
		   "\n.CSS file %s exists; -s, -S options ignored.\n",
		   cssfile);
      }
    else
      {
	//      file didn't exist... create it
	if (!quiet && !restore)
	  fprintf (stdout, "\nHad to create .CSS file %s with %lu buckets\n",
		   cssfile, sparse_spectrum_file_length);
	if (crm_osbf_create_cssfile
	    (cssfile, sparse_spectrum_file_length, OSBF_VERSION, 0,
	     OSBF_CSS_SPECTRA_START) != EXIT_SUCCESS)
	  exit (EXIT_FAILURE);
	k = stat (cssfile, &statbuf);
	hfsize = statbuf.st_size;
      }
    //
    //   mmap the hash file into memory so we can bitwhack it
    header = crm_mmap_file ( cssfile,
			     0, hfsize,
			     PROT_READ | PROT_WRITE,
			     MAP_SHARED,
			     NULL);
    if (header == MAP_FAILED)
      {
	fprintf (stderr,
		 "\n Couldn't mmap file %s into memory; errno=%d .\n",
		 cssfile, errno);
	exit (EXIT_FAILURE);
      }
    if (*((unsigned long *) (header->version)) != OSBF_VERSION)
      {
	fprintf (stderr,
		 "\n %s is the wrong version. We're expecting a %s css file.\n",
		 cssfile, CSS_version_name[OSBF_VERSION]);
	crm_munmap_file ((void *) header);
	exit (EXIT_FAILURE);
      }

    hashes = (OSBF_FEATUREBUCKET_STRUCT *) header + header->buckets_start;
    if (hashes == MAP_FAILED)
      {
	fprintf (stderr,
		 "\n Couldn't open RW file %s; errno=%d .\n", cssfile, errno);
	exit (EXIT_FAILURE);
      }
    //   from now on, hfsize is buckets, not bytes.
    hfsize = statbuf.st_size / sizeof (OSBF_FEATUREBUCKET_STRUCT);

    if (dump)
      {
	/* dump the css file */
	OSBF_FEATUREBUCKET_STRUCT *bucket;
	unsigned long *p;

	bucket = (OSBF_FEATUREBUCKET_STRUCT *) header;
	for (i = 0; i < hfsize; i++)
	  {
	    p = (unsigned long *) &bucket[i];
	    printf ("%lu;%lu;%lu\n", p[0], p[1], p[2]);
	  }
      }

    if (restore)
      {
	FILE *f;
	OSBF_FEATUREBUCKET_STRUCT *bucket;
	unsigned long *p;

	// restore the css file  - note that if we DIDN'T create
	// it already, then this will fail.
	//
	if ((f = fopen (csvfile, "rb")) == NULL)
	  {
	    fprintf (stderr, "\n Couldn't open csv file %s; errno=%d.\n",
		     csvfile, errno);
	    exit (EXIT_FAILURE);
	  }

	bucket = (OSBF_FEATUREBUCKET_STRUCT *) header;
	for (i = 0; i < hfsize; i++)
	  {
	    p = (unsigned long *) &bucket[i];
	    dontcare = fscanf (f, "%lu;%lu;%lu\n", &p[0], &p[1], &p[2]);
	  }
	fclose (f);
      }

    zloop = 1;
    while (zloop == 1 && !restore && !dump)
      {
	zloop = 0;
	crm_osbf_packcss (header, 0, header->buckets - 1);
	sum = 0;
	maxchain = 0;
	curchain = 0;
	totchain = 0;
	fbuckets = 0;
	nchains = 0;
	ofbins = 0;
	for (i = 0; i < header->buckets; i++)
	  {
	    sum += GET_BUCKET_VALUE(hashes[i]);
	    if (GET_BUCKET_VALUE(hashes[i]) != 0)
	      {
		fbuckets++;
		curchain++;
		if (GET_BUCKET_VALUE(hashes[i]) >= OSBF_FEATUREBUCKET_VALUE_MAX)
		  ofbins++;
	      }
	    else
	      {
		if (curchain > 0)
		  {
		    totchain += curchain;
		    nchains++;
	            if (curchain > maxchain)
	              maxchain = curchain;
		    curchain = 0;
		  }
	      }
	  }

	version_index = *((unsigned long *) header->version);
	if (version_index < 0 || version_index > UNKNOWN_VERSION)
	  version_index = UNKNOWN_VERSION;
	fprintf (stdout, "\n Sparse spectra file %s statistics: \n", cssfile);
	fprintf (stdout, "\n CSS file version                 : %12s",
		 CSS_version_name[version_index]);
	fprintf (stdout, "\n Header size (bytes)              : %12ld",
		 header->buckets_start * sizeof (OSBF_FEATUREBUCKET_STRUCT));
	fprintf (stdout, "\n Bucket size (bytes)              : %12lu",
		 (unsigned long)sizeof(OSBF_FEATUREBUCKET_STRUCT));
	fprintf (stdout, "\n Total available buckets          : %12ld",
		 header->buckets);
	fprintf (stdout, "\n Total buckets in use             : %12ld",
		 fbuckets);
	fprintf (stdout, "\n Number of trainings              : %12lu",
		 header->learnings);
	fprintf (stdout, "\n Total buckets with value >= max  : %12ld",
		 ofbins);
	fprintf (stdout, "\n Total hashed datums in file      : %12lld", sum);
	fprintf (stdout, "\n Average datums per bucket        : %12.2f",
		 (fbuckets > 0) ? (sum * 1.0) / (fbuckets * 1.0) : 0);
	fprintf (stdout, "\n Number of chains                 : %12ld",
		 nchains);
	fprintf (stdout, "\n Maximum length of overflow chain : %12ld",
		 maxchain);
	fprintf (stdout, "\n Average length of overflow chain : %12.2f",
		 nchains > 0 ? (totchain * 1.0) / (nchains * 1.0) : 0);
	fprintf (stdout, "\n Average packing density          : %12.2f\n",
		 (fbuckets * 1.0) / (header->buckets * 1.0));
	for (i = 0; i < OSBF_FEATUREBUCKET_VALUE_MAX; i++)
	  bcounts[i] = 0;
	for (v = 0; v < header->buckets; v++)
	  {
	    if (GET_BUCKET_VALUE(hashes[v]) < OSBF_FEATUREBUCKET_VALUE_MAX)
	      bcounts[GET_BUCKET_VALUE(hashes[v])]++;
	  }

	if (!brief)
	  for (i = 0; i < OSBF_FEATUREBUCKET_VALUE_MAX; i++)
	    {
	      if (bcounts[i] > 0)
		{
		  fprintf (stdout, "\n bin value %8ld found %9ld times",
			   i, bcounts[i]);
		}
	    }

	fprintf (stdout, "\n");
	cmdloop = 1;
	while (!report_only && cmdloop)
	  {
	    // clear command buffer
	    cmdchr[0] = '\0';
	    fprintf (stdout, "Options:\n");
	    fprintf (stdout, "   Z n - zero bins at or below a value\n");
	    fprintf (stdout, "   S n - subtract a constant from all bins\n");
	    fprintf (stdout, "   D n - divide all bins by a constant\n");
	    fprintf (stdout, "   R - rescan\n");
	    fprintf (stdout, "   P - pack\n");
	    fprintf (stdout, "   Q - quit\n");
	    fprintf (stdout, ">>> ");
	    clearerr (stdin);
	    dontcare = fscanf (stdin, "%[^\n]", cmdstr);
	    dontcare = fscanf (stdin, "%c", crapchr);
	    fields = sscanf (cmdstr, "%s %f", cmdchr, &cmdval);
	    if (strlen ( (char *)cmdchr) != 1)
	      {
		fprintf (stdout, "Unknown command: %s\n", cmdchr);
		continue;
	      }
	    switch (tolower ((int)cmdchr[0]))
	      {
	      case 'z':
		if (fields != 2)
		  fprintf (stdout,
			   "Z command requires a numeric argument!\n");
		else
		  {
		    fprintf (stdout, "Working...");
		    for (i = 0; i < header->buckets; i++)
		      if (GET_BUCKET_VALUE(hashes[i]) <= cmdval)
			BUCKET_RAW_VALUE(hashes[i]) = 0;
		    fprintf (stdout, "done.\n");
		  }
		break;
	      case 's':
		if (fields != 2)
		  fprintf (stdout,
			   "S command requires a numeric argument!\n");
		else
		  {
		    fprintf (stdout, "Working...");
		    for (i = 0; i < header->buckets; i++)
		      {
			if (GET_BUCKET_VALUE(hashes[i]) > (int) cmdval)
			  {
			    BUCKET_RAW_VALUE(hashes[i]) =
			      GET_BUCKET_VALUE(hashes[i]) - cmdval;
			  }
			else
			  {
			    BUCKET_RAW_VALUE(hashes[i]) = 0;
			  }
		      }
		    fprintf (stdout, "done.\n");
		  }
		break;
	      case 'd':
		if (fields != 2)
		  fprintf (stdout,
			   "D command requires a numeric argument!\n");
		else if (cmdval == 0)
		  fprintf (stdout, "You can't divide by zero, nimrod!\n");
		else
		  {
		    fprintf (stdout, "Working...");
		    for (i = 0; i < header->buckets; i++)
		      BUCKET_RAW_VALUE(hashes[i]) =
			  GET_BUCKET_VALUE(hashes[i]) / cmdval;
		    fprintf (stdout, "done.\n");
		  }
		break;
	      case 'r':
		zloop = 1;
		cmdloop = 0;
		break;
	      case 'p':
		fprintf (stdout, "Working...");
		crm_osbf_packcss (header, 0, header->buckets - 1);
		zloop = 1;
		cmdloop = 0;
		break;
	      case 'q':
		fprintf (stdout, "Bye! \n");
		cmdloop = 0;
		break;
	      default:
		fprintf (stdout, "Unknown command: %c\n", cmdchr[0]);
		break;
	      }
	  }
      }
  }
  return 0;
}