1 //===-- stats_test.cpp ------------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "tests/scudo_unit_test.h" 10 11 #include "stats.h" 12 13 TEST(ScudoStatsTest, LocalStats) { 14 scudo::LocalStats LStats; 15 LStats.init(); 16 for (scudo::uptr I = 0; I < scudo::StatCount; I++) 17 EXPECT_EQ(LStats.get(static_cast<scudo::StatType>(I)), 0U); 18 LStats.add(scudo::StatAllocated, 4096U); 19 EXPECT_EQ(LStats.get(scudo::StatAllocated), 4096U); 20 LStats.sub(scudo::StatAllocated, 4096U); 21 EXPECT_EQ(LStats.get(scudo::StatAllocated), 0U); 22 LStats.set(scudo::StatAllocated, 4096U); 23 EXPECT_EQ(LStats.get(scudo::StatAllocated), 4096U); 24 } 25 26 TEST(ScudoStatsTest, GlobalStats) { 27 scudo::GlobalStats GStats; 28 GStats.init(); 29 scudo::uptr Counters[scudo::StatCount] = {}; 30 GStats.get(Counters); 31 for (scudo::uptr I = 0; I < scudo::StatCount; I++) 32 EXPECT_EQ(Counters[I], 0U); 33 scudo::LocalStats LStats; 34 LStats.init(); 35 GStats.link(&LStats); 36 for (scudo::uptr I = 0; I < scudo::StatCount; I++) 37 LStats.add(static_cast<scudo::StatType>(I), 4096U); 38 GStats.get(Counters); 39 for (scudo::uptr I = 0; I < scudo::StatCount; I++) 40 EXPECT_EQ(Counters[I], 4096U); 41 // Unlinking the local stats move numbers to the global stats. 42 GStats.unlink(&LStats); 43 GStats.get(Counters); 44 for (scudo::uptr I = 0; I < scudo::StatCount; I++) 45 EXPECT_EQ(Counters[I], 4096U); 46 } 47