gem5  v21.1.0.2
group.test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021 Daniel R. Carvalho
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 
29 #include <gtest/gtest-spi.h>
30 #include <gtest/gtest.h>
31 
32 #include "base/stats/group.hh"
33 #include "base/stats/info.hh"
34 #include "base/stats/output.hh"
35 
36 using namespace gem5;
37 
39 TEST(StatsGroupTest, ConstructNoParent)
40 {
41  Stats::Group root(nullptr);
42  ASSERT_EQ(root.getStatGroups().size(), 0);
43 }
44 
46 TEST(StatsGroupTest, AddGetSingleStatGroup)
47 {
48  Stats::Group root(nullptr);
49  Stats::Group node1(nullptr);
50  root.addStatGroup("Node1", &node1);
51 
52  const auto root_map = root.getStatGroups();
53  ASSERT_EQ(root_map.size(), 1);
54  ASSERT_NE(root_map.find("Node1"), root_map.end());
55 
56  ASSERT_EQ(node1.getStatGroups().size(), 0);
57 }
58 
60 TEST(StatsGroupDeathTest, AddUniqueNameStatGroup)
61 {
62  Stats::Group root(nullptr);
63  Stats::Group node1(nullptr);
64  Stats::Group node2(nullptr);
65  root.addStatGroup("Node1", &node1);
66  ASSERT_ANY_THROW(root.addStatGroup("Node1", &node2));
67 }
68 
70 TEST(StatsGroupTest, AddNotUniqueNameAmongGroups)
71 {
72  Stats::Group root(nullptr);
73  Stats::Group node1(nullptr);
74  Stats::Group node2(nullptr);
75  Stats::Group node1_1(nullptr);
76  root.addStatGroup("Node1", &node1);
77  node1.addStatGroup("Node1_1", &node1_1);
78  ASSERT_NO_THROW(node1.addStatGroup("Node1", &node2));
79 }
80 
82 TEST(StatsGroupDeathTest, AddNull)
83 {
84  Stats::Group root(nullptr);
85  ASSERT_ANY_THROW(root.addStatGroup("Node1", nullptr));
86 }
87 
89 TEST(StatsGroupDeathTest, AddItself)
90 {
91  Stats::Group root(nullptr);
92  ASSERT_ANY_THROW(root.addStatGroup("Node1", &root));
93 }
94 
96 TEST(StatsGroupDeathTest, DISABLED_AddCycle)
97 {
98  Stats::Group root(nullptr);
99  Stats::Group node1(nullptr);
100  Stats::Group node1_1(nullptr);
101  root.addStatGroup("Node1", &node1);
102  node1.addStatGroup("Node1_1", &node1_1);
103  ASSERT_ANY_THROW(node1_1.addStatGroup("Root", &root));
104 }
105 
107 TEST(StatsGroupTest, AddGetMultipleStatGroup)
108 {
109  Stats::Group root(nullptr);
110  Stats::Group node1(nullptr);
111  Stats::Group node2(nullptr);
112  root.addStatGroup("Node1", &node1);
113  root.addStatGroup("Node2", &node2);
114 
115  const auto root_map = root.getStatGroups();
116  ASSERT_EQ(root_map.size(), 2);
117  ASSERT_NE(root_map.find("Node1"), root_map.end());
118  ASSERT_NE(root_map.find("Node2"), root_map.end());
119 
120  ASSERT_EQ(node1.getStatGroups().size(), 0);
121  ASSERT_EQ(node2.getStatGroups().size(), 0);
122 }
123 
125 TEST(StatsGroupTest, ConstructCorrectlyAssigned)
126 {
127  Stats::Group root(nullptr);
128  Stats::Group node1(nullptr);
129  Stats::Group node1_1(nullptr);
130  Stats::Group node1_1_1(nullptr);
131  root.addStatGroup("Node1", &node1);
132  node1.addStatGroup("Node1_1", &node1_1);
133  node1_1.addStatGroup("Node1_1_1", &node1_1_1);
134 
135  ASSERT_EQ(node1.getStatGroups().find("Node1_1")->second->getStatGroups(),
136  node1_1.getStatGroups());
137 }
138 
147 TEST(StatsGroupTest, ConstructOneLevelLinear)
148 {
149  Stats::Group root(nullptr);
150  Stats::Group node1(&root, "Node1");
151 
152  const auto root_map = root.getStatGroups();
153  ASSERT_EQ(root_map.size(), 1);
154  ASSERT_NE(root_map.find("Node1"), root_map.end());
155 
156  ASSERT_EQ(node1.getStatGroups().size(), 0);
157 }
158 
167 TEST(StatsGroupTest, ConstructOneLevelOfTwoNodes)
168 {
169  Stats::Group root(nullptr);
170  Stats::Group node1(&root, "Node1");
171  Stats::Group node2(&root, "Node2");
172 
173  const auto root_map = root.getStatGroups();
174  ASSERT_EQ(root_map.size(), 2);
175  ASSERT_NE(root_map.find("Node1"), root_map.end());
176  ASSERT_NE(root_map.find("Node2"), root_map.end());
177 
178  ASSERT_EQ(node1.getStatGroups().size(), 0);
179  ASSERT_EQ(node2.getStatGroups().size(), 0);
180 }
181 
192 TEST(StatsGroupTest, ConstructTwoLevelsLinear)
193 {
194  Stats::Group root(nullptr);
195  Stats::Group node1(&root, "Node1");
196  Stats::Group node1_1(&node1, "Node1_1");
197 
198  const auto root_map = root.getStatGroups();
199  ASSERT_EQ(root_map.size(), 1);
200  ASSERT_NE(root_map.find("Node1"), root_map.end());
201  ASSERT_EQ(root_map.find("Node1_1"), root_map.end());
202 
203  ASSERT_EQ(node1.getStatGroups().size(), 1);
204  ASSERT_NE(node1.getStatGroups().find("Node1_1"),
205  node1.getStatGroups().end());
206 
207  ASSERT_EQ(node1_1.getStatGroups().size(), 0);
208 }
209 
220 TEST(StatsGroupTest, ConstructTwoLevelsUnbalancedTree)
221 {
222  Stats::Group root(nullptr);
223  Stats::Group node1(&root, "Node1");
224  Stats::Group node2(&root, "Node2");
225  Stats::Group node1_1(&node1, "Node1_1");
226  Stats::Group node2_1(&node2, "Node2_1");
227  Stats::Group node2_2(&node2, "Node2_2");
228 
229  const auto root_map = root.getStatGroups();
230  ASSERT_EQ(root_map.size(), 2);
231  ASSERT_NE(root_map.find("Node1"), root_map.end());
232  ASSERT_NE(root_map.find("Node2"), root_map.end());
233  ASSERT_EQ(root_map.find("Node1_1"), root_map.end());
234  ASSERT_EQ(root_map.find("Node2_1"), root_map.end());
235  ASSERT_EQ(root_map.find("Node2_2"), root_map.end());
236 
237  ASSERT_EQ(node1.getStatGroups().size(), 1);
238  ASSERT_NE(node1.getStatGroups().find("Node1_1"),
239  node1.getStatGroups().end());
240  ASSERT_EQ(node1.getStatGroups().find("Node2_1"),
241  node1.getStatGroups().end());
242  ASSERT_EQ(node1.getStatGroups().find("Node2_2"),
243  node1.getStatGroups().end());
244 
245  ASSERT_EQ(node2.getStatGroups().size(), 2);
246  ASSERT_EQ(node2.getStatGroups().find("Node1_1"),
247  node2.getStatGroups().end());
248  ASSERT_NE(node2.getStatGroups().find("Node2_1"),
249  node2.getStatGroups().end());
250  ASSERT_NE(node2.getStatGroups().find("Node2_2"),
251  node2.getStatGroups().end());
252 
253  ASSERT_EQ(node1_1.getStatGroups().size(), 0);
254  ASSERT_EQ(node2_1.getStatGroups().size(), 0);
255  ASSERT_EQ(node2_2.getStatGroups().size(), 0);
256 }
257 
258 class DummyInfo : public Stats::Info
259 {
260  public:
261  using Stats::Info::Info;
262 
263  int value = 0;
264 
265  bool check() const override { return true; }
266  void prepare() override {}
267  void reset() override { value = 0; }
268  bool zero() const override { return false; }
269  void visit(Stats::Output &visitor) override {}
270 };
271 
273 TEST(StatsGroupTest, AddGetStat)
274 {
275  Stats::Group root(nullptr);
276  auto info_vec = root.getStats();
277  ASSERT_EQ(info_vec.size(), 0);
278 
279  DummyInfo info;
280  info.setName("InfoAddGetStat");
281  root.addStat(&info);
282  info_vec = root.getStats();
283  ASSERT_EQ(info_vec.size(), 1);
284  ASSERT_EQ(info_vec[0]->name, "InfoAddGetStat");
285 
286  DummyInfo info2;
287  info2.setName("InfoAddGetStat2");
288  root.addStat(&info2);
289  info_vec = root.getStats();
290  ASSERT_EQ(info_vec.size(), 2);
291  ASSERT_EQ(info_vec[0]->name, "InfoAddGetStat");
292  ASSERT_EQ(info_vec[1]->name, "InfoAddGetStat2");
293 }
294 
296 TEST(StatsGroupDeathTest, MergeStatGroupNoGroup)
297 {
298  Stats::Group root(nullptr);
299  ASSERT_ANY_THROW(root.mergeStatGroup(nullptr));
300 }
301 
303 TEST(StatsGroupDeathTest, MergeStatGroupItself)
304 {
305  Stats::Group root(nullptr);
306  ASSERT_ANY_THROW(root.mergeStatGroup(&root));
307 }
308 
310 TEST(StatsGroupTest, MergeStatGroup)
311 {
312  Stats::Group root(nullptr);
313  Stats::Group node1(nullptr);
314 
315  DummyInfo info;
316  info.setName("InfoMergeStatGroup");
317  node1.addStat(&info);
318  DummyInfo info2;
319  info2.setName("InfoMergeStatGroup2");
320  node1.addStat(&info2);
321 
322  root.mergeStatGroup(&node1);
323  auto info_vec = root.getStats();
324  ASSERT_EQ(info_vec.size(), 2);
325  ASSERT_EQ(info_vec[0]->name, "InfoMergeStatGroup");
326  ASSERT_EQ(info_vec[1]->name, "InfoMergeStatGroup2");
327 }
328 
330 TEST(StatsGroupDeathTest, MergeStatGroupMergedParent)
331 {
332  Stats::Group root(nullptr);
333  Stats::Group node1(nullptr);
334  Stats::Group node2(nullptr);
335  root.mergeStatGroup(&node2);
336  ASSERT_ANY_THROW(node1.mergeStatGroup(&node2));
337 }
338 
343 TEST(StatsGroupTest, AddStatMergedParent)
344 {
345  Stats::Group root(nullptr);
346  Stats::Group node1(nullptr);
347  Stats::Group node1_1(nullptr);
348 
349  root.mergeStatGroup(&node1);
350  root.mergeStatGroup(&node1_1);
351 
352  DummyInfo info;
353  info.setName("AddStatMergedParent");
354  node1_1.addStat(&info);
355 
356  auto info_vec = root.getStats();
357  ASSERT_EQ(info_vec.size(), 1);
358  ASSERT_EQ(info_vec[0]->name, "AddStatMergedParent");
359  info_vec = node1.getStats();
360  ASSERT_EQ(info_vec.size(), 0);
361  info_vec = node1_1.getStats();
362  ASSERT_EQ(info_vec.size(), 1);
363  ASSERT_EQ(info_vec[0]->name, "AddStatMergedParent");
364 }
365 
370 TEST(StatsGroupTest, AddStatMergedParentMain)
371 {
372  Stats::Group root(nullptr);
373  Stats::Group node1(nullptr);
374 
375  root.mergeStatGroup(&node1);
376 
377  DummyInfo info;
378  info.setName("AddStatMergedParentMain");
379  root.addStat(&info);
380 
381  auto info_vec = root.getStats();
382  ASSERT_EQ(info_vec.size(), 1);
383  ASSERT_EQ(info_vec[0]->name, "AddStatMergedParentMain");
384  info_vec = node1.getStats();
385  ASSERT_EQ(info_vec.size(), 0);
386 }
387 
392 TEST(StatsGroupTest, ConstructNoName)
393 {
394  Stats::Group root(nullptr);
395  Stats::Group node1(&root);
396 
397  DummyInfo info;
398  info.setName("InfoConstructNoName");
399  node1.addStat(&info);
400 
401  auto info_vec = root.getStats();
402  ASSERT_EQ(info_vec.size(), 1);
403  ASSERT_EQ(info_vec[0]->name, "InfoConstructNoName");
404 }
405 
410 TEST(StatsGroupTest, RegStats)
411 {
412  class TestGroup : public Stats::Group
413  {
414  public:
415  using Stats::Group::Group;
416 
417  int value = 0;
418 
419  void
420  regStats() override
421  {
422  value++;
423  Stats::Group::regStats();
424  }
425  };
426 
427  TestGroup root(nullptr);
428  root.value = 1;
429  TestGroup node1(&root, "Node1");
430  node1.value = 2;
431  TestGroup node1_1(&node1, "Node1_1");
432  node1_1.value = 3;
433  TestGroup node1_2(&node1_1);
434  node1_2.value = 4;
435 
436  node1.regStats();
437  ASSERT_EQ(root.value, 1);
438  ASSERT_EQ(node1.value, 3);
439  ASSERT_EQ(node1_1.value, 4);
440  ASSERT_EQ(node1_2.value, 5);
441 }
442 
448 TEST(StatsGroupTest, ResetStats)
449 {
450  Stats::Group root(nullptr);
451  Stats::Group node1(&root, "Node1");
452  Stats::Group node1_1(&node1, "Node1_1");
453  Stats::Group node1_2(&node1_1);
454 
455  DummyInfo info;
456  info.setName("InfoResetStats");
457  info.value = 1;
458  root.addStat(&info);
459 
460  DummyInfo info2;
461  info2.setName("InfoResetStats2");
462  info2.value = 2;
463  node1.addStat(&info2);
464 
465  DummyInfo info3;
466  info3.setName("InfoResetStats3");
467  info3.value = 3;
468  node1_1.addStat(&info3);
469 
470  DummyInfo info4;
471  info4.setName("InfoResetStats4");
472  info4.value = 4;
473  node1_1.addStat(&info4);
474 
475  DummyInfo info5;
476  info5.setName("InfoResetStats5");
477  info5.value = 5;
478  node1_2.addStat(&info5);
479 
480  node1.resetStats();
481  ASSERT_EQ(info.value, 1);
482  ASSERT_EQ(info2.value, 0);
483  ASSERT_EQ(info3.value, 0);
484  ASSERT_EQ(info4.value, 0);
485  ASSERT_EQ(info5.value, 0);
486 }
487 
492 TEST(StatsGroupTest, PreDumpStats)
493 {
494  class TestGroup : public Stats::Group
495  {
496  public:
497  using Stats::Group::Group;
498 
499  int value = 0;
500 
501  void
502  preDumpStats() override
503  {
504  value++;
505  Stats::Group::preDumpStats();
506  }
507  };
508 
509  TestGroup root(nullptr);
510  root.value = 1;
511  TestGroup node1(&root, "Node1");
512  node1.value = 2;
513  TestGroup node1_1(&node1, "Node1_1");
514  node1_1.value = 3;
515  TestGroup node1_2(&node1_1);
516  node1_2.value = 4;
517 
518  node1.preDumpStats();
519  ASSERT_EQ(root.value, 1);
520  ASSERT_EQ(node1.value, 3);
521  ASSERT_EQ(node1_1.value, 4);
522  ASSERT_EQ(node1_2.value, 5);
523 }
524 
526 TEST(StatsGroupTest, ResolveStatNone)
527 {
528  Stats::Group root(nullptr);
529 
530  DummyInfo info;
531  info.setName("InfoResolveStatNone");
532  root.addStat(&info);
533 
534  auto info_found = root.resolveStat("InfoResolveStatAny");
535  ASSERT_EQ(info_found, nullptr);
536 }
537 
539 TEST(StatsGroupTest, ResolveStatSelf)
540 {
541  Stats::Group root(nullptr);
542 
543  DummyInfo info;
544  info.setName("InfoResolveStatSelf");
545  root.addStat(&info);
546 
547  DummyInfo info2;
548  info2.setName("InfoResolveStatSelf2");
549  root.addStat(&info2);
550 
551  DummyInfo info3;
552  info3.setName("InfoResolveStatSelf3");
553  root.addStat(&info3);
554 
555  auto info_found = root.resolveStat("InfoResolveStatSelf");
556  ASSERT_NE(info_found, nullptr);
557  ASSERT_EQ(info_found->name, "InfoResolveStatSelf");
558 
559  info_found = root.resolveStat("InfoResolveStatSelf2");
560  ASSERT_NE(info_found, nullptr);
561  ASSERT_EQ(info_found->name, "InfoResolveStatSelf2");
562 
563  info_found = root.resolveStat("InfoResolveStatSelf3");
564  ASSERT_NE(info_found, nullptr);
565  ASSERT_EQ(info_found->name, "InfoResolveStatSelf3");
566 }
567 
569 TEST(StatsGroupTest, ResolveSubGroupStatFromParent)
570 {
571  Stats::Group root(nullptr);
572  Stats::Group node1(&root, "Node1");
573  Stats::Group node1_1(&node1, "Node1_1");
574  Stats::Group node1_1_1(&node1_1, "Node1_1_1");
575 
576  DummyInfo info;
577  info.setName("InfoResolveSubGroupStatFromParent");
578  node1.addStat(&info);
579 
580  DummyInfo info2;
581  info2.setName("InfoResolveSubGroupStatFromParent2");
582  node1_1.addStat(&info2);
583 
584  DummyInfo info3;
585  info3.setName("InfoResolveSubGroupStatFromParent3");
586  node1_1_1.addStat(&info3);
587 
588  auto info_found =
589  root.resolveStat("Node1.InfoResolveSubGroupStatFromParent");
590  ASSERT_NE(info_found, nullptr);
591  ASSERT_EQ(info_found->name, "InfoResolveSubGroupStatFromParent");
592 
593  info_found = root.resolveStat(
594  "Node1.Node1_1.InfoResolveSubGroupStatFromParent2");
595  ASSERT_NE(info_found, nullptr);
596  ASSERT_EQ(info_found->name, "InfoResolveSubGroupStatFromParent2");
597 
598  info_found = root.resolveStat(
599  "Node1.Node1_1.Node1_1_1.InfoResolveSubGroupStatFromParent3");
600  ASSERT_NE(info_found, nullptr);
601  ASSERT_EQ(info_found->name, "InfoResolveSubGroupStatFromParent3");
602 }
603 
605 TEST(StatsGroupTest, ResolveStatSubGroupOnSubGroup)
606 {
607  Stats::Group root(nullptr);
608  Stats::Group node1(&root, "Node1");
609 
610  DummyInfo info;
611  info.setName("InfoResolveStatSubGroupOnSubGroup");
612  root.addStat(&info);
613 
614  auto info_found = node1.resolveStat("InfoResolveStatSubGroupOnSubGroup");
615  ASSERT_EQ(info_found, nullptr);
616 }
617 
619 TEST(StatsGroupTest, ResolveStatMerged)
620 {
621  Stats::Group root(nullptr);
622  Stats::Group node1(nullptr);
623 
624  DummyInfo info;
625  info.setName("InfoResolveStatMerged");
626  node1.addStat(&info);
627  DummyInfo info2;
628  info2.setName("InfoResolveStatMerged2");
629  node1.addStat(&info2);
630 
631  root.mergeStatGroup(&node1);
632 
633  auto info_found = root.resolveStat("InfoResolveStatMerged");
634  ASSERT_NE(info_found, nullptr);
635  ASSERT_EQ(info_found->name, "InfoResolveStatMerged");
636 
637  info_found = root.resolveStat("InfoResolveStatMerged2");
638  ASSERT_NE(info_found, nullptr);
639  ASSERT_EQ(info_found->name, "InfoResolveStatMerged2");
640 }
641 
643 TEST(StatsGroupTest, ResolveStatMergedSubGroup)
644 {
645  Stats::Group root(nullptr);
646  Stats::Group node1(nullptr);
647  Stats::Group node2(nullptr);
648 
649  DummyInfo info;
650  info.setName("InfoResolveStatMergedSubGroup");
651  node2.addStat(&info);
652 
653  root.addStatGroup("Node1", &node1);
654  node1.mergeStatGroup(&node2);
655 
656  auto info_found = root.resolveStat("Node1.InfoResolveStatMergedSubGroup");
657  ASSERT_NE(info_found, nullptr);
658  ASSERT_EQ(info_found->name, "InfoResolveStatMergedSubGroup");
659 }
DummyInfo::visit
void visit(Stats::Output &visitor) override
Definition: group.test.cc:269
DummyInfo
Definition: group.test.cc:258
group.hh
DummyInfo::value
int value
Definition: group.test.cc:263
DummyInfo::reset
void reset() override
Definition: group.test.cc:267
DummyInfo::prepare
void prepare() override
Definition: group.test.cc:266
info.hh
TEST
TEST(StatsGroupTest, ConstructNoParent)
Test that the constructor without a parent doesn't do anything.
Definition: group.test.cc:39
DummyInfo::zero
bool zero() const override
Definition: group.test.cc:268
name
const std::string & name()
Definition: trace.cc:49
output.hh
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40
DummyInfo::check
bool check() const override
Definition: group.test.cc:265

Generated on Tue Sep 21 2021 12:24:58 for gem5 by doxygen 1.8.17