1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.helpers.tests;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21 
22 import androidx.test.runner.AndroidJUnit4;
23 
24 import com.android.helpers.SlabinfoHelper;
25 import com.android.helpers.SlabinfoHelper.SlabinfoSample;
26 
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 
31 import java.math.RoundingMode;
32 import java.text.DecimalFormat;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.TreeMap;
37 
38 @RunWith(AndroidJUnit4.class)
39 public class SlabinfoHelperTest {
40     private SlabinfoHelper mSlabinfoHelper;
41 
42     @Before
setUp()43     public void setUp() {
44         mSlabinfoHelper = new SlabinfoHelper();
45     }
46 
47     @Test
testStartStop()48     public void testStartStop() {
49         assertTrue(
50                 "Reading /proc/slabinfo requires root and no selinux denial. Are you root?",
51                 mSlabinfoHelper.startCollecting());
52         assertTrue(mSlabinfoHelper.stopCollecting());
53     }
54 
55     @Test
testSimpleExactFits()56     public void testSimpleExactFits() {
57         List<SlabinfoSample> samples = new ArrayList<>();
58         SlabinfoSample sample = new SlabinfoSample();
59         sample.time = 0;
60         sample.slabs = new TreeMap<String, Long>();
61         sample.slabs.put("a", 0L);
62         sample.slabs.put("b", 0L);
63         sample.slabs.put("c", 0L);
64         sample.slabs.put("d", 25L);
65         sample.slabs.put("e", 50L);
66         sample.slabs.put("f", 100L);
67         samples.add(sample);
68 
69         sample = new SlabinfoSample();
70         sample.time = 100;
71         sample.slabs = new TreeMap<String, Long>();
72         sample.slabs.put("a", 100L);
73         sample.slabs.put("b", 50L);
74         sample.slabs.put("c", 25L);
75         sample.slabs.put("d", 0L);
76         sample.slabs.put("e", 0L);
77         sample.slabs.put("f", 0L);
78         samples.add(sample);
79 
80         Map<String, Double> slopes = SlabinfoHelper.fitLinesToSamples(samples);
81         assertEquals(Double.valueOf(300), slopes.get("slabinfo.a"));
82         assertEquals(Double.valueOf(150), slopes.get("slabinfo.b"));
83         assertEquals(Double.valueOf(75), slopes.get("slabinfo.c"));
84         assertEquals(Double.valueOf(-75), slopes.get("slabinfo.d"));
85         assertEquals(Double.valueOf(-150), slopes.get("slabinfo.e"));
86         assertEquals(Double.valueOf(-300), slopes.get("slabinfo.f"));
87     }
88 
89     @Test
testNoSlopes()90     public void testNoSlopes() {
91         List<SlabinfoSample> samples = new ArrayList<>();
92         SlabinfoSample sample = new SlabinfoSample();
93         sample.time = 0;
94         sample.slabs = new TreeMap<String, Long>();
95         sample.slabs.put("a", 0L);
96         sample.slabs.put("b", 0L);
97         samples.add(sample);
98 
99         sample = new SlabinfoSample();
100         sample.time = 50;
101         sample.slabs = new TreeMap<String, Long>();
102         sample.slabs.put("a", 0L);
103         sample.slabs.put("b", 50L);
104         samples.add(sample);
105 
106         sample = new SlabinfoSample();
107         sample.time = 100;
108         sample.slabs = new TreeMap<String, Long>();
109         sample.slabs.put("a", 0L);
110         sample.slabs.put("b", 0L);
111         samples.add(sample);
112 
113         Map<String, Double> slopes = SlabinfoHelper.fitLinesToSamples(samples);
114         assertEquals(Double.valueOf(0), slopes.get("slabinfo.a"));
115         assertEquals(Double.valueOf(0), slopes.get("slabinfo.b"));
116     }
117 
118     @Test
testNonexactFits()119     public void testNonexactFits() {
120         List<SlabinfoSample> samples = new ArrayList<>();
121         SlabinfoSample sample = new SlabinfoSample();
122         sample.time = 37;
123         sample.slabs = new TreeMap<String, Long>();
124         sample.slabs.put("a", 83L);
125         sample.slabs.put("b", 83L);
126         samples.add(sample);
127 
128         sample = new SlabinfoSample();
129         sample.time = 43;
130         sample.slabs = new TreeMap<String, Long>();
131         sample.slabs.put("a", 47L);
132         sample.slabs.put("b", 47L);
133         samples.add(sample);
134 
135         sample = new SlabinfoSample();
136         sample.time = 97;
137         sample.slabs = new TreeMap<String, Long>();
138         sample.slabs.put("a", 71L);
139         sample.slabs.put("b", 61L);
140         samples.add(sample);
141 
142         Map<String, Double> slopes = SlabinfoHelper.fitLinesToSamples(samples);
143         DecimalFormat df = new DecimalFormat("0.0000000");
144         df.setRoundingMode(RoundingMode.HALF_UP);
145         Double rounded_a = Double.valueOf(df.format(slopes.get("slabinfo.a")));
146         Double rounded_b = Double.valueOf(df.format(slopes.get("slabinfo.b")));
147         assertEquals(Double.valueOf(16.4835165), rounded_a);
148         assertEquals(Double.valueOf(-35.7142857), rounded_b);
149     }
150 }
151