stattest.cc revision 10055:6153b582c9b5
1/*
2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Nathan Binkert
29 */
30
31#include <iomanip>
32#include <iostream>
33#include <string>
34
35#include "base/cprintf.hh"
36#include "base/misc.hh"
37#include "base/statistics.hh"
38#include "base/types.hh"
39#include "sim/core.hh"
40#include "sim/stat_control.hh"
41
42// override the default main() code for this unittest
43const char *m5MainCommands[] = {
44    "import m5.stattestmain",
45    "m5.stattestmain.main()",
46    0 // sentinel is required
47};
48
49using namespace std;
50using namespace Stats;
51
52double testfunc();
53struct StatTest;
54StatTest & __stattest();
55void stattest_init();
56void stattest_run();
57
58
59double
60testfunc()
61{
62    return 9.8;
63}
64
65class TestClass {
66  public:
67    double operator()() { return 9.7; }
68};
69
70struct StatTest
71{
72    Scalar s1;
73    Scalar s2;
74    Average s3;
75    Scalar s4;
76    Vector s5;
77    Distribution s6;
78    Vector s7;
79    AverageVector s8;
80    StandardDeviation s9;
81    AverageDeviation s10;
82    Scalar s11;
83    Distribution s12;
84    VectorDistribution s13;
85    VectorStandardDeviation s14;
86    VectorAverageDeviation s15;
87    Vector2d s16;
88    Value s17;
89    Value s18;
90    Histogram h01;
91    Histogram h02;
92    Histogram h03;
93    Histogram h04;
94    Histogram h05;
95    Histogram h06;
96    Histogram h07;
97    Histogram h08;
98    Histogram h09;
99    Histogram h10;
100    Histogram h11;
101    Histogram h12;
102    SparseHistogram sh1;
103
104    Vector s19;
105    Vector s20;
106
107    Formula f1;
108    Formula f2;
109    Formula f3;
110    Formula f4;
111    Formula f5;
112    Formula f6;
113
114    void run();
115    void init();
116};
117
118StatTest &
119__stattest()
120{
121    static StatTest st;
122    return st;
123}
124
125void
126stattest_init()
127{
128    __stattest().init();
129}
130
131void
132stattest_run()
133{
134    __stattest().run();
135}
136
137void
138StatTest::init()
139{
140    EventQueue *q = getEventQueue(0);
141    curEventQueue(q);
142
143    cprintf("sizeof(Scalar) = %d\n", sizeof(Scalar));
144    cprintf("sizeof(Vector) = %d\n", sizeof(Vector));
145    cprintf("sizeof(Distribution) = %d\n", sizeof(Distribution));
146
147    s1
148        .name("Stat01")
149        .desc("this is statistic 1")
150        ;
151
152    s2
153        .name("Stat02")
154        .desc("this is statistic 2")
155        .prereq(s11)
156        ;
157
158    s3
159        .name("Stat03")
160        .desc("this is statistic 3")
161        .prereq(f5)
162        ;
163
164    s4
165        .name("Stat04")
166        .desc("this is statistic 4")
167        .prereq(s11)
168        ;
169
170    s5
171        .init(5)
172        .name("Stat05")
173        .desc("this is statistic 5")
174        .prereq(s11)
175        .subname(0, "foo1")
176        .subname(1, "foo2")
177        .subname(2, "foo3")
178        .subname(3, "foo4")
179        .subname(4, "foo5")
180        ;
181
182    s6
183        .init(1, 100, 13)
184        .name("Stat06")
185        .desc("this is statistic 6")
186        .prereq(s11)
187        ;
188
189    s7
190        .init(7)
191        .name("Stat07")
192        .desc("this is statistic 7")
193        .precision(1)
194        .flags(pdf | total)
195        .prereq(s11)
196        ;
197
198    s8
199        .init(10)
200        .name("Stat08")
201        .desc("this is statistic 8")
202        .precision(2)
203        .prereq(s11)
204        .subname(4, "blarg")
205        ;
206
207    s9
208        .name("Stat09")
209        .desc("this is statistic 9")
210        .precision(4)
211        .prereq(s11)
212        ;
213
214    s10
215        .name("Stat10")
216        .desc("this is statistic 10")
217        .prereq(s11)
218        ;
219
220    s12
221        .init(1, 100, 13)
222        .name("Stat12")
223        .desc("this is statistic 12")
224        ;
225
226    s13
227        .init(4, 0, 99, 10)
228        .name("Stat13")
229        .desc("this is statistic 13")
230        ;
231
232    s14
233        .init(9)
234        .name("Stat14")
235        .desc("this is statistic 14")
236        ;
237
238    s15
239        .init(10)
240        .name("Stat15")
241        .desc("this is statistic 15")
242        ;
243
244    s16
245        .init(2, 9)
246        .name("Stat16")
247        .desc("this is statistic 16")
248        .flags(total)
249        .subname(0, "sub0")
250        .subname(1, "sub1")
251        .ysubname(0, "y0")
252        .ysubname(1, "y1")
253        ;
254
255    s17
256        .functor(testfunc)
257        .name("Stat17")
258        .desc("this is stat 17")
259        ;
260
261    TestClass testclass;
262    s18
263        .functor(testclass)
264        .name("Stat18")
265        .desc("this is stat 18")
266        ;
267
268    h01
269        .init(11)
270        .name("Histogram01")
271        .desc("this is histogram 1")
272        ;
273
274    h02
275        .init(10)
276        .name("Histogram02")
277        .desc("this is histogram 2")
278        ;
279
280    h03
281        .init(11)
282        .name("Histogram03")
283        .desc("this is histogram 3")
284        ;
285
286    h04
287        .init(10)
288        .name("Histogram04")
289        .desc("this is histogram 4")
290        ;
291
292    h05
293        .init(11)
294        .name("Histogram05")
295        .desc("this is histogram 5")
296        ;
297
298    h06
299        .init(10)
300        .name("Histogram06")
301        .desc("this is histogram 6")
302        ;
303
304    h07
305        .init(11)
306        .name("Histogram07")
307        .desc("this is histogram 7")
308        ;
309
310    h08
311        .init(10)
312        .name("Histogram08")
313        .desc("this is histogram 8")
314        ;
315
316    h09
317        .init(11)
318        .name("Histogram09")
319        .desc("this is histogram 9")
320        ;
321
322    h10
323        .init(10)
324        .name("Histogram10")
325        .desc("this is histogram 10")
326        ;
327
328    h11
329        .init(11)
330        .name("Histogram11")
331        .desc("this is histogram 11")
332        ;
333
334    h12
335        .init(10)
336        .name("Histogram12")
337        .desc("this is histogram 12")
338        ;
339
340    sh1
341        .init(0)
342        .name("SparseHistogram1")
343        .desc("this is sparse histogram 1")
344        ;
345
346    f1
347        .name("Formula1")
348        .desc("this is formula 1")
349        .prereq(s11)
350        ;
351
352    f2
353        .name("Formula2")
354        .desc("this is formula 2")
355        .prereq(s11)
356        .precision(1)
357        ;
358
359    f3
360        .name("Formula3")
361        .desc("this is formula 3")
362        .prereq(s11)
363        .subname(0, "bar1")
364        .subname(1, "bar2")
365        .subname(2, "bar3")
366        .subname(3, "bar4")
367        .subname(4, "bar5")
368        ;
369
370    f4
371        .name("Formula4")
372        .desc("this is formula 4")
373        ;
374
375    s19
376        .init(2)
377        .name("Stat19")
378        .desc("this is statistic 19 for vector op testing")
379        .flags(total | nozero | nonan)
380    ;
381    s20
382        .init(2)
383        .name("Stat20")
384        .desc("this is statistic 20 for vector op testing")
385        .flags(total | nozero | nonan)
386    ;
387
388    f6
389        .name("vector_op_test_formula")
390        .desc("The total stat should equal 1")
391        .flags(total |nozero |nonan)
392        ;
393
394    f1 = s1 + s2;
395    f2 = (-s1) / (-s2) * (-s3 + ULL(100) + s4);
396    f3 = sum(s5) * s7;
397    f4 += constant(10.0);
398    f4 += s5[3];
399    f5 = constant(1);
400    f6 = s19/s20;
401}
402
403void
404StatTest::run()
405{
406    s16[1][0] = 1;
407    s16[0][1] = 3;
408    s16[0][0] = 2;
409    s16[1][1] = 9;
410    s16[1][1] += 9;
411    s16[1][8] += 8;
412    s16[1][7] += 7;
413    s16[1][6] += 6;
414    s16[1][5] += 5;
415    s16[1][4] += 4;
416
417    s11 = 1;
418    s3 = 9;
419    s8[3] = 9;
420    s15[0].sample(1234);
421    s15[1].sample(1234);
422    s15[2].sample(1234);
423    s15[3].sample(1234);
424    s15[4].sample(1234);
425    s15[5].sample(1234);
426    s15[6].sample(1234);
427    s15[7].sample(1234);
428    s15[8].sample(1234);
429    s15[9].sample(1234);
430
431    s10.sample(1000000000);
432    curEventQueue()->setCurTick(curTick() + ULL(1000000));
433    s10.sample(100000);
434    s10.sample(100000);
435    s10.sample(100000);
436    s10.sample(100000);
437    s10.sample(100000);
438    s10.sample(100000);
439    s10.sample(100000);
440    s10.sample(100000);
441    s10.sample(100000);
442    s10.sample(100000);
443    s10.sample(100000);
444    s10.sample(100000);
445    s10.sample(100000);
446    s13[0].sample(12);
447    s13[1].sample(29);
448    s13[2].sample(12);
449    s13[3].sample(29);
450    s13[0].sample(42);
451    s13[1].sample(29);
452    s13[2].sample(42);
453    s13[3].sample(32);
454    s13[0].sample(52);
455    s13[1].sample(49);
456    s13[2].sample(42);
457    s13[3].sample(25);
458    s13[0].sample(32);
459    s13[1].sample(49);
460    s13[2].sample(22);
461    s13[3].sample(49);
462    s13[0].sample(62);
463    s13[1].sample(99);
464    s13[2].sample(72);
465    s13[3].sample(23);
466    s13[0].sample(52);
467    s13[1].sample(78);
468    s13[2].sample(69);
469    s13[3].sample(49);
470
471    s14[0].sample(1234);
472    s14[1].sample(4134);
473    s14[4].sample(1213);
474    s14[3].sample(1124);
475    s14[2].sample(1243);
476    s14[7].sample(1244);
477    s14[4].sample(7234);
478    s14[2].sample(9234);
479    s14[3].sample(1764);
480    s14[7].sample(1564);
481    s14[3].sample(3234);
482    s14[1].sample(2234);
483    s14[5].sample(1234);
484    s14[2].sample(4334);
485    s14[2].sample(1234);
486    s14[4].sample(4334);
487    s14[6].sample(1234);
488    s14[8].sample(8734);
489    s14[1].sample(5234);
490    s14[3].sample(8234);
491    s14[7].sample(5234);
492    s14[4].sample(4434);
493    s14[3].sample(7234);
494    s14[2].sample(1934);
495    s14[1].sample(9234);
496    s14[5].sample(5634);
497    s14[3].sample(1264);
498    s14[7].sample(5223);
499    s14[0].sample(1234);
500    s14[0].sample(5434);
501    s14[3].sample(8634);
502    s14[1].sample(1234);
503
504
505    s15[0].sample(1234);
506    s15[1].sample(4134);
507    curEventQueue()->setCurTick(curTick() + ULL(1000000));
508    s15[4].sample(1213);
509    curEventQueue()->setCurTick(curTick() + ULL(1000000));
510    s15[3].sample(1124);
511    curEventQueue()->setCurTick(curTick() + ULL(1000000));
512    s15[2].sample(1243);
513    curEventQueue()->setCurTick(curTick() + ULL(1000000));
514    s15[7].sample(1244);
515    curEventQueue()->setCurTick(curTick() + ULL(1000000));
516    s15[4].sample(7234);
517    s15[2].sample(9234);
518    s15[3].sample(1764);
519    s15[7].sample(1564);
520    s15[3].sample(3234);
521    s15[1].sample(2234);
522    curEventQueue()->setCurTick(curTick() + ULL(1000000));
523    s15[5].sample(1234);
524    curEventQueue()->setCurTick(curTick() + ULL(1000000));
525    s15[9].sample(4334);
526    curEventQueue()->setCurTick(curTick() + ULL(1000000));
527    s15[2].sample(1234);
528    curEventQueue()->setCurTick(curTick() + ULL(1000000));
529    s15[4].sample(4334);
530    s15[6].sample(1234);
531    curEventQueue()->setCurTick(curTick() + ULL(1000000));
532    s15[8].sample(8734);
533    curEventQueue()->setCurTick(curTick() + ULL(1000000));
534    s15[1].sample(5234);
535    curEventQueue()->setCurTick(curTick() + ULL(1000000));
536    s15[3].sample(8234);
537    curEventQueue()->setCurTick(curTick() + ULL(1000000));
538    s15[7].sample(5234);
539    s15[4].sample(4434);
540    s15[3].sample(7234);
541    s15[2].sample(1934);
542    s15[1].sample(9234);
543    curEventQueue()->setCurTick(curTick() + ULL(1000000));
544    s15[5].sample(5634);
545    s15[3].sample(1264);
546    s15[7].sample(5223);
547    s15[0].sample(1234);
548    s15[0].sample(5434);
549    s15[3].sample(8634);
550    curEventQueue()->setCurTick(curTick() + ULL(1000000));
551    s15[1].sample(1234);
552
553    s4 = curTick();
554
555    s8[3] = 99999;
556
557    s3 = 12;
558    s3++;
559    curEventQueue()->setCurTick(curTick() + 9);
560
561    s1 = 9;
562    s1 += 9;
563    s1 -= 11;
564    s1++;
565    ++s1;
566    s1--;
567    --s1;
568
569    s2 = 9;
570
571    s5[0] += 1;
572    s5[1] += 2;
573    s5[2] += 3;
574    s5[3] += 4;
575    s5[4] += 5;
576
577    s7[0] = 10;
578    s7[1] = 20;
579    s7[2] = 30;
580    s7[3] = 40;
581    s7[4] = 50;
582    s7[5] = 60;
583    s7[6] = 70;
584
585    s6.sample(0);
586    s6.sample(1);
587    s6.sample(2);
588    s6.sample(3);
589    s6.sample(4);
590    s6.sample(5);
591    s6.sample(6);
592    s6.sample(7);
593    s6.sample(8);
594    s6.sample(9);
595
596    s6.sample(10);
597    s6.sample(10);
598    s6.sample(10);
599    s6.sample(10);
600    s6.sample(10);
601    s6.sample(10);
602    s6.sample(10);
603    s6.sample(10);
604    s6.sample(11);
605    s6.sample(19);
606    s6.sample(20);
607    s6.sample(20);
608    s6.sample(21);
609    s6.sample(21);
610    s6.sample(31);
611    s6.sample(98);
612    s6.sample(99);
613    s6.sample(99);
614    s6.sample(99);
615
616    s7[0] = 700;
617    s7[1] = 600;
618    s7[2] = 500;
619    s7[3] = 400;
620    s7[4] = 300;
621    s7[5] = 200;
622    s7[6] = 100;
623
624    s9.sample(100);
625    s9.sample(100);
626    s9.sample(100);
627    s9.sample(100);
628    s9.sample(10);
629    s9.sample(10);
630    s9.sample(10);
631    s9.sample(10);
632    s9.sample(10);
633
634    curEventQueue()->setCurTick(curTick() + 9);
635    s4 = curTick();
636    s6.sample(100);
637    s6.sample(100);
638    s6.sample(100);
639    s6.sample(101);
640    s6.sample(102);
641
642    s12.sample(100);
643    for (int i = 0; i < 100; i++) {
644        h01.sample(i);
645        h02.sample(i);
646    }
647
648    for (int i = -100; i < 100; i++) {
649        h03.sample(i);
650        h04.sample(i);
651    }
652
653    for (int i = -100; i < 1000; i++) {
654        h05.sample(i);
655        h06.sample(i);
656    }
657
658    for (int i = 100; i >= -1000; i--) {
659        h07.sample(i);
660        h08.sample(i);
661    }
662
663    for (int i = 0; i <= 1023; i++) {
664        h09.sample(i);
665        h10.sample(i);
666    }
667
668    for (int i = -1024; i <= 1023; i++) {
669        h11.sample(i);
670        h12.sample(i);
671    }
672
673    for (int i = 0; i < 1000; i++) {
674        sh1.sample(random() % 10000);
675    }
676
677    s19[0] = 1;
678    s19[1] = 100000;
679    s20[0] = 100000;
680    s20[1] = 1;
681
682}
683