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