-
Notifications
You must be signed in to change notification settings - Fork 22
/
frequency.c
956 lines (860 loc) · 27 KB
/
frequency.c
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
/*****************************************************************************
F R E Q U E N C Y T E S T
*****************************************************************************/
/*
* This code has been heavily modified by the following people:
*
* Landon Curt Noll
* Tom Gilgan
* Riccardo Paccagnella
*
* See the README.md and the initial comment in sts.c for more information.
*
* WE (THOSE LISTED ABOVE WHO HEAVILY MODIFIED THIS CODE) DISCLAIM ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL WE (THOSE LISTED ABOVE
* WHO HEAVILY MODIFIED THIS CODE) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
* USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*
* chongo (Landon Curt Noll, http://www.isthe.com/chongo/index.html) /\oo/\
*
* Share and enjoy! :-)
*/
// Exit codes: 70 thru 79
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "../utils/externs.h"
#include "../utils/utilities.h"
#include "../utils/debug.h"
#include "../utils/cephes.h"
/*
* Private stats - stats.txt information for this test
*/
struct Frequency_private_stats {
bool success; // Success or failure of iteration test
long int S_n; // nth partial sum
};
/*
* Static const variables declarations
*/
static const enum test test_num = TEST_FREQUENCY; // This test number
/*
* Forward static function declarations
*/
static bool Frequency_print_stat(FILE * stream, struct state *state, struct Frequency_private_stats *stat, double p_value);
static bool Frequency_print_p_value(FILE * stream, double p_value);
static void Frequency_metric_print(struct state *state, long int sampleCount, long int toolow, long int *freqPerBin);
/*
* Frequency_init - initialize the Frequency test
*
* given:
* state // run state to test under
*
* This function is called for each and every iteration noted in state->tp.numOfBitStreams.
*/
void
Frequency_init(struct state *state)
{
long int n; // Length of a single bit stream
/*
* Check preconditions (firewall)
*/
if (state == NULL) {
err(70, __func__, "state arg is NULL");
}
if (state->testVector[test_num] != true) {
return;
}
if (state->cSetup != true) {
dbg(DBG_LOW, "init driver interface for %s[%d] called when test vector was false", state->testNames[test_num],
test_num);
err(70, __func__, "test constants not setup prior to calling %s for %s[%d]", __func__,
state->testNames[test_num], test_num);
}
/*
* Collect parameters from state
*/
n = state->tp.n;
/*
* Disable test if conditions do not permit this test from being run
*/
if (n < MIN_LENGTH_FREQUENCY) {
warn(__func__, "disabling test %s[%d]: requires bitcount(n): %ld >= %d",
state->testNames[test_num], test_num, n, MIN_LENGTH_FREQUENCY);
state->testVector[test_num] = false;
return;
}
/*
* Create working sub-directory if forming files such as results.txt and stats.txt
*/
if (state->resultstxtFlag == true) {
state->subDir[test_num] = precheckSubdir(state, state->testNames[test_num]);
dbg(DBG_HIGH, "test %s[%d] will use subdir: %s", state->testNames[test_num], test_num, state->subDir[test_num]);
}
/*
* Allocate dynamic arrays
*/
if (state->resultstxtFlag == true) {
state->stats[test_num] = create_dyn_array(sizeof(struct Frequency_private_stats),
DEFAULT_CHUNK, state->tp.numOfBitStreams, false); // stats.txt
}
state->p_val[test_num] = create_dyn_array(sizeof(double),
DEFAULT_CHUNK, state->tp.numOfBitStreams, false); // results.txt
/*
* Determine format of data*.txt filenames based on state->partitionCount[test_num]
* NOTE: If we are not partitioning the p_values, no data*.txt filenames are needed
*/
state->datatxt_fmt[test_num] = data_filename_format(state->partitionCount[test_num]);
dbg(DBG_HIGH, "%s[%d] will form data*.txt filenames with the following format: %s",
state->testNames[test_num], test_num, state->datatxt_fmt[test_num]);
return;
}
/*
* Frequency_iterate - iterate one bit stream for Frequency test
*
* given:
* state // run state to test under
*
* This function is called for each and every iteration noted in state->tp.numOfBitStreams.
*
* NOTE: The initialize function must be called before this function is called.
*/
void
Frequency_iterate(struct thread_state *thread_state)
{
struct Frequency_private_stats stat; // Stats for this iteration
long int n; // Length of a single bit stream
double f; // Term in the p-value formula
double s_obs; // Test statistic
double p_value; // p_value iteration test result(s)
long int i;
/*
* Check preconditions (firewall)
*/
if (thread_state == NULL) {
err(71, __func__, "thread_state arg is NULL");
}
struct state *state = thread_state->global_state;
if (state == NULL) {
err(71, __func__, "state arg is NULL");
}
if (state->testVector[test_num] != true) {
dbg(DBG_LOW, "iterate driver interface for %s[%d] called when test vector was false", state->testNames[test_num],
test_num);
return;
}
if (state->epsilon == NULL) {
err(71, __func__, "state->epsilon is NULL");
}
if (state->epsilon[thread_state->thread_id] == NULL) {
err(71, __func__, "state->epsilon[%ld] is NULL", thread_state->thread_id);
}
if (state->cSetup != true) {
err(71, __func__, "test constants not setup prior to calling %s for %s[%d]",
__func__, state->testNames[test_num], test_num);
}
/*
* Collect parameters from state
*/
n = state->tp.n;
/*
* Step 1: compute S_n
*/
stat.S_n = 0;
for (i = 0; i < n; i++) {
if ((int) state->epsilon[thread_state->thread_id][i] == 1) {
stat.S_n++;
} else if ((int) state->epsilon[thread_state->thread_id][i] == 0) {
stat.S_n--;
} else {
err(41, __func__, "found a bit different than 1 or 0 in the sequence");
}
}
/*
* Step 2: compute the test statistic
*/
s_obs = fabs((double) stat.S_n) / state->c.sqrtn;
/*
* Step 3: compute the test P-value
*/
f = s_obs / state->c.sqrt2;
p_value = erfc(f);
/*
* Lock mutex before making changes to the shared state
*/
if (thread_state->mutex != NULL) {
pthread_mutex_lock(thread_state->mutex);
}
/*
* Record success or failure for this iteration
*/
state->count[test_num]++; // Count this iteration
state->valid[test_num]++; // Count this valid iteration
if (isNegative(p_value)) {
state->failure[test_num]++; // Bogus p_value < 0.0 treated as a failure
stat.success = false; // FAILURE
warn(__func__, "iteration %ld of test %s[%d] produced bogus p_value: %f < 0.0\n",
thread_state->iteration_being_done + 1, state->testNames[test_num], test_num, p_value);
} else if (isGreaterThanOne(p_value)) {
state->failure[test_num]++; // Bogus p_value > 1.0 treated as a failure
stat.success = false; // FAILURE
warn(__func__, "iteration %ld of test %s[%d] produced bogus p_value: %f > 1.0\n",
thread_state->iteration_being_done + 1, state->testNames[test_num], test_num, p_value);
} else if (p_value < state->tp.alpha) {
state->valid_p_val[test_num]++; // Valid p_value in [0.0, 1.0] range
state->failure[test_num]++; // Valid p_value but too low is a failure
stat.success = false; // FAILURE
} else {
state->valid_p_val[test_num]++; // Valid p_value in [0.0, 1.0] range
state->success[test_num]++; // Valid p_value not too low is a success
stat.success = true; // SUCCESS
}
/*
* Record values computed during this iteration
*/
if (state->resultstxtFlag == true) {
append_value(state->stats[test_num], &stat);
}
append_value(state->p_val[test_num], &p_value);
/*
* Unlock mutex after making changes to the shared state
*/
if (thread_state->mutex != NULL) {
pthread_mutex_unlock(thread_state->mutex);
}
return;
}
/*
* Frequency_print_stat - print private_stats information to the end of an open file
*
* given:
* stream // open writable FILE stream
* state // run state to test under
* stat // struct Frequency_private_stats for format and print
* p_value // p_value iteration test result(s)
*
* returns:
* true --> no errors
* false --> an I/O error occurred
*/
static bool
Frequency_print_stat(FILE * stream, struct state *state, struct Frequency_private_stats *stat, double p_value)
{
int io_ret; // I/O return status
/*
* Check preconditions (firewall)
*/
if (stream == NULL) {
err(72, __func__, "stream arg is NULL");
}
if (state == NULL) {
err(72, __func__, "state arg is NULL");
}
if (stat == NULL) {
err(72, __func__, "stat arg is NULL");
}
if (p_value == NON_P_VALUE && stat->success == true) {
err(72, __func__, "p_value was set to NON_P_VALUE but stat->success == true");
}
/*
* Print stat to a file
*/
if (state->legacy_output == true) {
io_ret = fprintf(stream, "\t\t\t FREQUENCY TEST\n");
if (io_ret <= 0) {
return false;
}
io_ret = fprintf(stream, "\t\t---------------------------------------------\n");
if (io_ret <= 0) {
return false;
}
io_ret = fprintf(stream, "\t\tCOMPUTATIONAL INFORMATION:\n");
if (io_ret <= 0) {
return false;
}
} else {
io_ret = fprintf(stream, "\t\t\t Frequency test\n");
if (io_ret <= 0) {
return false;
}
}
io_ret = fprintf(stream, "\t\t---------------------------------------------\n");
if (io_ret <= 0) {
return false;
}
io_ret = fprintf(stream, "\t\t(a) The nth partial sum = %ld\n", stat->S_n);
if (io_ret <= 0) {
return false;
}
io_ret = fprintf(stream, "\t\t(b) S_n/n = %f\n", (double) stat->S_n / state->tp.n);
if (io_ret <= 0) {
return false;
}
io_ret = fprintf(stream, "\t\t---------------------------------------------\n");
if (io_ret <= 0) {
return false;
}
/*
* Report success or failure
*/
if (stat->success == true) {
io_ret = fprintf(stream, "SUCCESS\t\tp_value = %f\n\n", p_value);
if (io_ret <= 0) {
return false;
}
} else if (p_value == NON_P_VALUE) {
io_ret = fprintf(stream, "FAILURE\t\tp_value = __INVALID__\n\n");
if (io_ret <= 0) {
return false;
}
} else {
io_ret = fprintf(stream, "FAILURE\t\tp_value = %f\n\n", p_value);
if (io_ret <= 0) {
return false;
}
}
/*
* All printing successful
*/
return true;
}
/*
* Frequency_print_p_value - print p_value information to the end of an open file
*
* given:
* stream // open writable FILE stream
* stat // struct Frequency_private_stats for format and print
* p_value // p_value iteration test result(s)
*
* returns:
* true --> no errors
* false --> an I/O error occurred
*/
static bool
Frequency_print_p_value(FILE * stream, double p_value)
{
int io_ret; // I/O return status
/*
* Check preconditions (firewall)
*/
if (stream == NULL) {
err(73, __func__, "stream arg is NULL");
}
/*
* Print p_value to a file
*/
if (p_value == NON_P_VALUE) {
io_ret = fprintf(stream, "__INVALID__\n");
if (io_ret <= 0) {
return false;
}
} else {
io_ret = fprintf(stream, "%f\n", p_value);
if (io_ret <= 0) {
return false;
}
}
/*
* All printing successful
*/
return true;
}
/*
* Frequency_print - print to results.txt, data*.txt, stats.txt for all iterations
*
* given:
* state // run state to test under
*
* This function is called for once to print dynamic arrays into
* results.txt, data*.txt, stats.txt.
*
* NOTE: The initialize and iterate functions must be called before this function is called.
*/
void
Frequency_print(struct state *state)
{
struct Frequency_private_stats *stat; // Pointer to statistics of an iteration
double p_value; // p_value iteration test result(s)
FILE *stats = NULL; // Open stats.txt file
FILE *results = NULL; // Open results.txt file
FILE *data = NULL; // Open data*.txt file
char *stats_txt = NULL; // Pathname for stats.txt
char *results_txt = NULL; // Pathname for results.txt
char *data_txt = NULL; // Pathname for data*.txt
char data_filename[BUFSIZ + 1]; // Basename for a given data*.txt pathname
bool ok; // true -> I/O was OK
int snprintf_ret; // snprintf return value
int io_ret; // I/O return status
long int i;
long int j;
/*
* Check preconditions (firewall)
*/
if (state == NULL) {
err(74, __func__, "state arg is NULL");
}
if (state->testVector[test_num] != true) {
dbg(DBG_HIGH, "Print driver interface for %s[%d] called when test vector was false", state->testNames[test_num],
test_num);
return;
}
if (state->resultstxtFlag == false) {
dbg(DBG_HIGH, "Print driver interface for %s[%d] was not enabled with -s", state->testNames[test_num], test_num);
return;
}
if (state->partitionCount[test_num] < 1) {
err(74, __func__,
"print driver interface for %s[%d] called with state.partitionCount: %d < 0",
state->testNames[test_num], test_num, state->partitionCount[test_num]);
}
if (state->p_val[test_num]->count != (state->tp.numOfBitStreams * state->partitionCount[test_num])) {
err(74, __func__,
"print driver interface for %s[%d] called with p_val count: %ld != %ld*%d=%ld",
state->testNames[test_num], test_num, state->p_val[test_num]->count,
state->tp.numOfBitStreams, state->partitionCount[test_num],
state->tp.numOfBitStreams * state->partitionCount[test_num]);
}
if (state->datatxt_fmt[test_num] == NULL) {
err(74, __func__, "format for data0*.txt filename is NULL");
}
/*
* Open stats.txt file
*/
stats_txt = filePathName(state->subDir[test_num], "stats.txt");
dbg(DBG_HIGH, "about to open/truncate: %s", stats_txt);
stats = openTruncate(stats_txt);
/*
* Open results.txt file
*/
results_txt = filePathName(state->subDir[test_num], "results.txt");
dbg(DBG_HIGH, "about to open/truncate: %s", results_txt);
results = openTruncate(results_txt);
/*
* Write results.txt and stats.txt files
*/
for (i = 0; i < state->stats[test_num]->count; ++i) {
/*
* Locate stat for this iteration
*/
stat = addr_value(state->stats[test_num], struct Frequency_private_stats, i);
/*
* Get p_value for this iteration
*/
p_value = get_value(state->p_val[test_num], double, i);
/*
* Print stat to stats.txt
*/
errno = 0; // paranoia
ok = Frequency_print_stat(stats, state, stat, p_value);
if (ok == false) {
errp(74, __func__, "error in writing to %s", stats_txt);
}
/*
* Print p_value to results.txt
*/
errno = 0; // paranoia
ok = Frequency_print_p_value(results, p_value);
if (ok == false) {
errp(74, __func__, "error in writing to %s", results_txt);
}
}
/*
* Flush and close stats.txt, free pathname
*/
errno = 0; // paranoia
io_ret = fflush(stats);
if (io_ret != 0) {
errp(74, __func__, "error flushing to: %s", stats_txt);
}
errno = 0; // paranoia
io_ret = fclose(stats);
if (io_ret != 0) {
errp(74, __func__, "error closing: %s", stats_txt);
}
free(stats_txt);
stats_txt = NULL;
/*
* Flush and close results.txt, free pathname
*/
errno = 0; // paranoia
io_ret = fflush(results);
if (io_ret != 0) {
errp(74, __func__, "error flushing to: %s", results_txt);
}
errno = 0; // paranoia
io_ret = fclose(results);
if (io_ret != 0) {
errp(74, __func__, "error closing: %s", results_txt);
}
free(results_txt);
results_txt = NULL;
/*
* Write data*.txt for each data file if we need to partition results
*/
if (state->partitionCount[test_num] > 1) {
for (j = 0; j < state->partitionCount[test_num]; ++j) {
/*
* Form the data*.txt basename
*/
errno = 0; // paranoia
snprintf_ret = snprintf(data_filename, BUFSIZ, state->datatxt_fmt[test_num], j + 1);
data_filename[BUFSIZ] = '\0'; // paranoia
if (snprintf_ret <= 0 || snprintf_ret >= BUFSIZ || errno != 0) {
errp(74, __func__, "snprintf failed for %d bytes for data%03ld.txt, returned: %d", BUFSIZ,
j + 1, snprintf_ret);
}
/*
* Form the data*.txt filename
*/
data_txt = filePathName(state->subDir[test_num], data_filename);
dbg(DBG_HIGH, "about to open/truncate: %s", data_txt);
data = openTruncate(data_txt);
/*
* Write this particular data*.txt filename
*/
if (j < state->p_val[test_num]->count) {
for (i = j; i < state->p_val[test_num]->count; i += state->partitionCount[test_num]) {
/*
* Get p_value for an iteration belonging to this data*.txt filename
*/
p_value = get_value(state->p_val[test_num], double, i);
/*
* Print p_value to results.txt
*/
errno = 0; // paranoia
ok = Frequency_print_p_value(data, p_value);
if (ok == false) {
errp(74, __func__, "error in writing to %s", data_txt);
}
}
}
/*
* Flush and close data*.txt, free pathname
*/
errno = 0; // paranoia
io_ret = fflush(data);
if (io_ret != 0) {
errp(74, __func__, "error flushing to: %s", data_txt);
}
errno = 0; // paranoia
io_ret = fclose(data);
if (io_ret != 0) {
errp(74, __func__, "error closing: %s", data_txt);
}
free(data_txt);
data_txt = NULL;
}
}
return;
}
/*
* Frequency_metric_print - print uniformity and proportional information for a tallied count
*
* given:
* state // run state to test under
* sampleCount // number of bitstreams in which we counted p_values
* toolow // p_values that were below alpha
* freqPerBin // uniformity frequency bins
*/
static void
Frequency_metric_print(struct state *state, long int sampleCount, long int toolow, long int *freqPerBin)
{
long int passCount; // p_values that pass
double p_hat; // 1 - alpha
double proportion_threshold_max; // When passCount is too high
double proportion_threshold_min; // When passCount is too low
double chi2; // Sum of chi^2 for each tenth
double uniformity; // Uniformity of frequency bins
double expCount; // Sample size divided by frequency bin count
int io_ret; // I/O return status
long int i;
/*
* Check preconditions (firewall)
*/
if (state == NULL) {
err(75, __func__, "state arg is NULL");
}
if (freqPerBin == NULL) {
err(75, __func__, "freqPerBin arg is NULL");
}
/*
* Determine the number tests that passed
*/
if ((sampleCount <= 0) || (sampleCount < toolow)) {
passCount = 0;
} else {
passCount = sampleCount - toolow;
}
/*
* Determine proportion thresholds
*/
p_hat = 1.0 - state->tp.alpha;
proportion_threshold_max = (p_hat + 3.0 * sqrt((p_hat * state->tp.alpha) / sampleCount)) * sampleCount;
proportion_threshold_min = (p_hat - 3.0 * sqrt((p_hat * state->tp.alpha) / sampleCount)) * sampleCount;
/*
* Compute uniformity p-value
*/
chi2 = 0.0;
expCount = (double)sampleCount / state->tp.uniformity_bins;
if (expCount <= 0.0) {
uniformity = 0.0; // Not enough samples for uniformity check
} else {
// Sum chi squared of the frequency bins
for (i = 0; i < state->tp.uniformity_bins; ++i) {
chi2 += (freqPerBin[i] - expCount) * (freqPerBin[i] - expCount) / expCount;
}
uniformity = cephes_igamc((state->tp.uniformity_bins - 1.0) / 2.0, chi2 / 2.0); // Uniformity threshold level
}
/*
* Save or print results
*/
if (state->legacy_output == true) {
/*
* Output uniformity results in traditional format to finalAnalysisReport.txt
*/
for (i = 0; i < state->tp.uniformity_bins; ++i) {
fprintf(state->finalRept, "%3ld ", freqPerBin[i]);
}
if (expCount <= 0.0) {
// Not enough samples for uniformity check
fprintf(state->finalRept, " ---- ");
dbg(DBG_HIGH, "too few iterations for uniformity check on %s", state->testNames[test_num]);
} else if (uniformity < state->tp.uniformity_level) {
// Uniformity failure (the uniformity p-value is smaller than the minimum uniformity_level (default 0.0001)
fprintf(state->finalRept, " %8.6f * ", uniformity);
dbg(DBG_HIGH, "metrics detected uniformity failure for %s", state->testNames[test_num]);
} else {
// Uniformity success
fprintf(state->finalRept, " %8.6f ", uniformity);
dbg(DBG_HIGH, "metrics detected uniformity success for %s", state->testNames[test_num]);
}
/*
* Output proportional results in traditional format to finalAnalysisReport.txt
*/
if (sampleCount == 0) {
// Not enough samples for proportional check
fprintf(state->finalRept, " ------ %s\n", state->testNames[test_num]);
dbg(DBG_HIGH, "too few samples for proportional check on %s", state->testNames[test_num]);
} else if ((passCount < proportion_threshold_min) || (passCount > proportion_threshold_max)) {
// Proportional failure
fprintf(state->finalRept, "%4ld/%-4ld * %s\n", passCount, sampleCount, state->testNames[test_num]);
dbg(DBG_HIGH, "metrics detected proportional failure for %s", state->testNames[test_num]);
} else {
// Proportional success
fprintf(state->finalRept, "%4ld/%-4ld %s\n", passCount, sampleCount, state->testNames[test_num]);
dbg(DBG_HIGH, "metrics detected proportional success for %s", state->testNames[test_num]);
}
/*
* Flush the output file buffer
*/
errno = 0; // paranoia
io_ret = fflush(state->finalRept);
if (io_ret != 0) {
errp(75, __func__, "error flushing to: %s", state->finalReptPath);
}
} else {
bool uniformity_passed = true;
bool proportion_passed = true;
/*
* Check uniformity results
*/
if (expCount <= 0.0 || uniformity < state->tp.uniformity_level) {
// Uniformity failure or not enough samples for uniformity check
uniformity_passed = false;
dbg(DBG_HIGH, "metrics detected uniformity failure for %s", state->testNames[test_num]);
}
/*
* Check proportional results
*/
if (sampleCount == 0 || (passCount < proportion_threshold_min) || (passCount > proportion_threshold_max)) {
// Proportional failure or not enough samples for proportional check
proportion_passed = false;
dbg(DBG_HIGH, "metrics detected proportional failure for %s", state->testNames[test_num]);
}
if (proportion_passed == false && uniformity_passed == false) {
state->metric_results.frequency = FAILED_BOTH;
} else if (proportion_passed == false) {
state->metric_results.frequency = FAILED_PROPORTION;
} else if (uniformity_passed == false) {
state->metric_results.frequency = FAILED_UNIFORMITY;
} else {
state->metric_results.frequency = PASSED_BOTH;
state->successful_tests++;
}
}
return;
}
/*
* Frequency_metrics - uniformity and proportional analysis of a test
*
* given:
* state // run state to test under
*
* This function is called once to complete the test analysis for all iterations.
*
* NOTE: The initialize and iterate functions must be called before this function is called.
*/
void
Frequency_metrics(struct state *state)
{
long int sampleCount; // Number of bitstreams in which we will count p_values
long int toolow; // p_values that were below alpha
double p_value; // p_value iteration test result(s)
long int *freqPerBin; // Uniformity frequency bins
long int i;
long int j;
/*
* Check preconditions (firewall)
*/
if (state == NULL) {
err(76, __func__, "state arg is NULL");
}
if (state->testVector[test_num] != true) {
dbg(DBG_LOW, "metrics driver interface for %s[%d] called when test vector was false",
state->testNames[test_num], test_num);
return;
}
if (state->partitionCount[test_num] < 1) {
err(76, __func__,
"metrics driver interface for %s[%d] called with state.partitionCount: %d < 0",
state->testNames[test_num], test_num, state->partitionCount[test_num]);
}
if (state->p_val[test_num]->count != (state->tp.numOfBitStreams * state->partitionCount[test_num])) {
warn(__func__,
"metrics driver interface for %s[%d] called with p_val length: %ld != bit streams: %ld",
state->testNames[test_num], test_num, state->p_val[test_num]->count,
state->tp.numOfBitStreams * state->partitionCount[test_num]);
}
/*
* Allocate uniformity frequency bins
*/
freqPerBin = malloc(state->tp.uniformity_bins * sizeof(freqPerBin[0]));
if (freqPerBin == NULL) {
errp(76, __func__, "cannot malloc of %ld elements of %ld bytes each for freqPerBin",
state->tp.uniformity_bins, sizeof(long int));
}
/*
* Print for each partition (or the whole set of p_values if partitionCount is 1)
*/
for (j = 0; j < state->partitionCount[test_num]; ++j) {
/*
* Set counters to zero
*/
toolow = 0;
sampleCount = 0;
memset(freqPerBin, 0, state->tp.uniformity_bins * sizeof(freqPerBin[0]));
/*
* Tally p_value
*/
for (i = j; i < state->p_val[test_num]->count; i += state->partitionCount[test_num]) {
// Get the iteration p_value
p_value = get_value(state->p_val[test_num], double, i);
if (p_value == NON_P_VALUE) {
continue; // the test was not possible for this iteration
}
// Case: random excursion test
if (state->is_excursion[test_num] == true) {
// Random excursion tests only sample > 0 p_values
if (p_value > 0.0) {
++sampleCount;
} else {
// Ignore p_value of 0 for random excursion tests
continue;
}
}
// Case: general (non-random excursion) test
else {
// All other tests count all p_values
++sampleCount;
}
// Count the number of p_values below alpha
if (p_value < state->tp.alpha) {
++toolow;
}
// Tally the p_value in a uniformity bin
if (p_value >= 1.0) {
++freqPerBin[state->tp.uniformity_bins - 1];
} else if (p_value >= 0.0) {
++freqPerBin[(int) floor(p_value * (double) state->tp.uniformity_bins)];
} else {
++freqPerBin[0];
}
}
/*
* Print uniformity and proportional information for a tallied count
*/
Frequency_metric_print(state, sampleCount, toolow, freqPerBin);
/*
* Track maximum samples
*/
if (state->is_excursion[test_num] == true) {
if (sampleCount > state->maxRandomExcursionSampleSize) {
state->maxRandomExcursionSampleSize = sampleCount;
}
} else {
if (sampleCount > state->maxGeneralSampleSize) {
state->maxGeneralSampleSize = sampleCount;
}
}
}
/*
* Free allocated storage
*/
free(freqPerBin);
freqPerBin = NULL;
return;
}
/*
* Frequency_destroy - post process results for this text
*
* given:
* state // run state to test under
*
* This function is called once to cleanup any storage or state
* associated with this test.
*/
void
Frequency_destroy(struct state *state)
{
/*
* Check preconditions (firewall)
*/
if (state == NULL) {
err(77, __func__, "state arg is NULL");
}
if (state->testVector[test_num] != true) {
dbg(DBG_LOW, "destroy function[%d] %s called when test vector was false", test_num, __func__);
return;
}
/*
* Free dynamic arrays
*/
if (state->stats[test_num] != NULL) {
free_dyn_array(state->stats[test_num]);
free(state->stats[test_num]);
state->stats[test_num] = NULL;
}
if (state->p_val[test_num] != NULL) {
free_dyn_array(state->p_val[test_num]);
free(state->p_val[test_num]);
state->p_val[test_num] = NULL;
}
/*
* Free other test storage
*/
if (state->datatxt_fmt[test_num] != NULL) {
free(state->datatxt_fmt[test_num]);
state->datatxt_fmt[test_num] = NULL;
}
if (state->subDir[test_num] != NULL) {
free(state->subDir[test_num]);
state->subDir[test_num] = NULL;
}
return;
}