gem5 v24.0.0.0
Loading...
Searching...
No Matches
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
36using namespace gem5;
37
39TEST(StatsGroupTest, ConstructNoParent)
40{
41 statistics::Group root(nullptr);
42 ASSERT_EQ(root.getStatGroups().size(), 0);
43}
44
46TEST(StatsGroupTest, AddGetSingleStatGroup)
47{
48 statistics::Group root(nullptr);
49 statistics::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
60TEST(StatsGroupDeathTest, AddUniqueNameStatGroup)
61{
62 statistics::Group root(nullptr);
63 statistics::Group node1(nullptr);
64 statistics::Group node2(nullptr);
65 root.addStatGroup("Node1", &node1);
66 ASSERT_ANY_THROW(root.addStatGroup("Node1", &node2));
67}
68
70TEST(StatsGroupTest, AddNotUniqueNameAmongGroups)
71{
72 statistics::Group root(nullptr);
73 statistics::Group node1(nullptr);
74 statistics::Group node2(nullptr);
75 statistics::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
82TEST(StatsGroupDeathTest, AddNull)
83{
84 statistics::Group root(nullptr);
85 ASSERT_ANY_THROW(root.addStatGroup("Node1", nullptr));
86}
87
89TEST(StatsGroupDeathTest, AddItself)
90{
91 statistics::Group root(nullptr);
92 ASSERT_ANY_THROW(root.addStatGroup("Node1", &root));
93}
94
96TEST(StatsGroupDeathTest, DISABLED_AddCycle)
97{
98 statistics::Group root(nullptr);
99 statistics::Group node1(nullptr);
100 statistics::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
107TEST(StatsGroupTest, AddGetMultipleStatGroup)
108{
109 statistics::Group root(nullptr);
110 statistics::Group node1(nullptr);
111 statistics::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
125TEST(StatsGroupTest, ConstructCorrectlyAssigned)
126{
127 statistics::Group root(nullptr);
128 statistics::Group node1(nullptr);
129 statistics::Group node1_1(nullptr);
130 statistics::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
147TEST(StatsGroupTest, ConstructOneLevelLinear)
148{
149 statistics::Group root(nullptr);
150 statistics::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
167TEST(StatsGroupTest, ConstructOneLevelOfTwoNodes)
168{
169 statistics::Group root(nullptr);
170 statistics::Group node1(&root, "Node1");
171 statistics::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
192TEST(StatsGroupTest, ConstructTwoLevelsLinear)
193{
194 statistics::Group root(nullptr);
195 statistics::Group node1(&root, "Node1");
196 statistics::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
220TEST(StatsGroupTest, ConstructTwoLevelsUnbalancedTree)
221{
222 statistics::Group root(nullptr);
223 statistics::Group node1(&root, "Node1");
224 statistics::Group node2(&root, "Node2");
225 statistics::Group node1_1(&node1, "Node1_1");
226 statistics::Group node2_1(&node2, "Node2_1");
227 statistics::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
259{
260 public:
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(statistics::Output &visitor) override {}
270};
271
273TEST(StatsGroupTest, AddGetStat)
274{
275 statistics::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
296TEST(StatsGroupDeathTest, MergeStatGroupNoGroup)
297{
298 statistics::Group root(nullptr);
299 ASSERT_ANY_THROW(root.mergeStatGroup(nullptr));
300}
301
303TEST(StatsGroupDeathTest, MergeStatGroupItself)
304{
305 statistics::Group root(nullptr);
306 ASSERT_ANY_THROW(root.mergeStatGroup(&root));
307}
308
310TEST(StatsGroupTest, MergeStatGroup)
311{
312 statistics::Group root(nullptr);
313 statistics::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
330TEST(StatsGroupDeathTest, MergeStatGroupMergedParent)
331{
332 statistics::Group root(nullptr);
333 statistics::Group node1(nullptr);
334 statistics::Group node2(nullptr);
335 root.mergeStatGroup(&node2);
336 ASSERT_ANY_THROW(node1.mergeStatGroup(&node2));
337}
338
343TEST(StatsGroupTest, AddStatMergedParent)
344{
345 statistics::Group root(nullptr);
346 statistics::Group node1(nullptr);
347 statistics::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
370TEST(StatsGroupTest, AddStatMergedParentMain)
371{
372 statistics::Group root(nullptr);
373 statistics::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
392TEST(StatsGroupTest, ConstructNoName)
393{
394 statistics::Group root(nullptr);
395 statistics::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
410TEST(StatsGroupTest, RegStats)
411{
412 class TestGroup : public statistics::Group
413 {
414 public:
416
417 int value = 0;
418
419 void
420 regStats() override
421 {
422 value++;
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
448TEST(StatsGroupTest, ResetStats)
449{
450 statistics::Group root(nullptr);
451 statistics::Group node1(&root, "Node1");
452 statistics::Group node1_1(&node1, "Node1_1");
453 statistics::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
492TEST(StatsGroupTest, PreDumpStats)
493{
494 class TestGroup : public statistics::Group
495 {
496 public:
498
499 int value = 0;
500
501 void
502 preDumpStats() override
503 {
504 value++;
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
526TEST(StatsGroupTest, ResolveStatNone)
527{
528 statistics::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
539TEST(StatsGroupTest, ResolveStatSelf)
540{
541 statistics::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
569TEST(StatsGroupTest, ResolveSubGroupStatFromParent)
570{
571 statistics::Group root(nullptr);
572 statistics::Group node1(&root, "Node1");
573 statistics::Group node1_1(&node1, "Node1_1");
574 statistics::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
605TEST(StatsGroupTest, ResolveStatSubGroupOnSubGroup)
606{
607 statistics::Group root(nullptr);
608 statistics::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
619TEST(StatsGroupTest, ResolveStatMerged)
620{
621 statistics::Group root(nullptr);
622 statistics::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
643TEST(StatsGroupTest, ResolveStatMergedSubGroup)
644{
645 statistics::Group root(nullptr);
646 statistics::Group node1(nullptr);
647 statistics::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}
bool zero() const override
void reset() override
Reset the stat to the default state.
void visit(statistics::Output &visitor) override
Visitor entry for outputing statistics data.
void prepare() override
Prepare the stat for dumping.
bool check() const override
Check that this stat has been set up properly and is ready for use.
Statistics container.
Definition group.hh:93
const Info * resolveStat(std::string name) const
Resolve a stat by its name within this group.
Definition group.cc:128
void mergeStatGroup(Group *block)
Merge the contents (stats & children) of a block to this block.
Definition group.cc:160
void setName(const std::string &name, bool old_style=true)
Set the name of this statistic.
Definition info.cc:127
TEST(StatsGroupTest, ConstructNoParent)
Test that the constructor without a parent doesn't do anything.
Definition group.test.cc:39
const std::map< std::string, Group * > & getStatGroups() const
Get all child groups associated with this object.
Definition group.cc:181
const std::vector< Info * > & getStats() const
Get all stats associated with this object.
Definition group.cc:187
virtual void regStats()
Callback to set stat parameters.
Definition group.cc:68
void addStatGroup(const char *name, Group *block)
Add a stat block as a child of this block.
Definition group.cc:117
virtual void preDumpStats()
Callback before stats are dumped.
Definition group.cc:99
void addStat(statistics::Info *info)
Register a stat with this group.
Definition group.cc:109
virtual void resetStats()
Callback to reset stats.
Definition group.cc:86
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
const std::string & name()
Definition trace.cc:48

Generated on Tue Jun 18 2024 16:24:01 for gem5 by doxygen 1.11.0