1 /*
2 * Copyright (c) 2021 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8 #include <zephyr/ztest.h>
9
10 #define BLK_SZ 64
11 #define NUM_BLOCKS 8
12
13 K_MEM_SLAB_DEFINE(kmslab, BLK_SZ, NUM_BLOCKS, 4);
14
ZTEST(lib_mem_slab_stats_test,test_mem_slab_stats_invalid_params)15 ZTEST(lib_mem_slab_stats_test, test_mem_slab_stats_invalid_params)
16 {
17 struct sys_memory_stats stats;
18 int status;
19
20 /*
21 * Verify that k_mem_slab_runtime_stats_get() returns -EINVAL
22 * when an invalid parameter is passed.
23 */
24
25 status = k_mem_slab_runtime_stats_get(NULL, &stats);
26 zassert_equal(status, -EINVAL, "Routine returned %d instead of %d",
27 status, -EINVAL);
28
29 status = k_mem_slab_runtime_stats_get(&kmslab, NULL);
30 zassert_equal(status, -EINVAL, "Routine returned %d instead of %d",
31 status, -EINVAL);
32
33 /*
34 * Verify that k_mem_slab_runtime_stats_reset_max() returns -EINVAL
35 * when an invalid parameter is passed.
36 */
37
38 status = k_mem_slab_runtime_stats_reset_max(NULL);
39 zassert_equal(status, -EINVAL, "Routine returned %d instead of %d",
40 status, -EINVAL);
41 }
42
ZTEST(lib_mem_slab_stats_test,test_mem_slab_runtime_stats)43 ZTEST(lib_mem_slab_stats_test, test_mem_slab_runtime_stats)
44 {
45 struct sys_memory_stats stats;
46 int status;
47 void *memory[3];
48
49 /* Verify initial stats */
50
51 status = k_mem_slab_runtime_stats_get(&kmslab, &stats);
52 zassert_equal(status, 0, "Routine failed with status %d\n", status);
53
54 zassert_equal(stats.free_bytes, BLK_SZ * NUM_BLOCKS,
55 "Expected %zu free bytes, not %zu\n",
56 BLK_SZ * NUM_BLOCKS, stats.free_bytes);
57 zassert_equal(stats.allocated_bytes, 0,
58 "Expected 0 allocated bytes, not %zu\n",
59 stats.allocated_bytes);
60 zassert_equal(stats.max_allocated_bytes, 0,
61 "Expected 0 max allocated bytes, not %zu\n",
62 stats.max_allocated_bytes);
63
64 /* Allocate three blocks, and then verify the stats. */
65
66 status = k_mem_slab_alloc(&kmslab, &memory[0], K_NO_WAIT);
67 zassert_equal(status, 0, "Routine failed to allocate 1st block (%d)\n",
68 status);
69 status = k_mem_slab_alloc(&kmslab, &memory[1], K_NO_WAIT);
70 zassert_equal(status, 0, "Routine failed to allocate 2nd block (%d)\n",
71 status);
72 status = k_mem_slab_alloc(&kmslab, &memory[2], K_NO_WAIT);
73 zassert_equal(status, 0, "Routine failed to allocate 3rd block (%d)\n",
74 status);
75 status = k_mem_slab_runtime_stats_get(&kmslab, &stats);
76 zassert_equal(status, 0, "Routine failed with status %d\n", status);
77
78 zassert_equal(stats.free_bytes, BLK_SZ * (NUM_BLOCKS - 3),
79 "Expected %zu free bytes, not %zu\n",
80 BLK_SZ * (NUM_BLOCKS - 3), stats.free_bytes);
81 zassert_equal(stats.allocated_bytes, 3 * BLK_SZ,
82 "Expected %zu allocated bytes, not %zu\n",
83 3 * BLK_SZ, stats.allocated_bytes);
84 zassert_equal(stats.max_allocated_bytes, 3 * BLK_SZ,
85 "Expected %zu max allocated bytes, not %zu\n",
86 3 * BLK_SZ, stats.max_allocated_bytes);
87
88 /* Free blocks 1 and 2, and then verify the stats. */
89
90 k_mem_slab_free(&kmslab, memory[2]);
91 k_mem_slab_free(&kmslab, memory[1]);
92
93 status = k_mem_slab_runtime_stats_get(&kmslab, &stats);
94 zassert_equal(status, 0, "Routine failed with status %d\n", status);
95
96 zassert_equal(stats.free_bytes, BLK_SZ * (NUM_BLOCKS - 1),
97 "Expected %zu free bytes, not %zu\n",
98 BLK_SZ * (NUM_BLOCKS - 1), stats.free_bytes);
99 zassert_equal(stats.allocated_bytes, 1 * BLK_SZ,
100 "Expected %zu allocated bytes, not %zu\n",
101 1 * BLK_SZ, stats.allocated_bytes);
102 zassert_equal(stats.max_allocated_bytes, 3 * BLK_SZ,
103 "Expected %zu max allocated bytes, not %zu\n",
104 3 * BLK_SZ, stats.max_allocated_bytes);
105
106 /* Allocate 1 block and verify the max is still at 3 */
107
108 status = k_mem_slab_alloc(&kmslab, &memory[1], K_NO_WAIT);
109 zassert_equal(status, 0, "Routine failed with status %d\n", status);
110
111 status = k_mem_slab_runtime_stats_get(&kmslab, &stats);
112 zassert_equal(status, 0, "Routine failed with status %d\n", status);
113
114 zassert_equal(stats.free_bytes, BLK_SZ * (NUM_BLOCKS - 2),
115 "Expected %zu free bytes, not %zu\n",
116 BLK_SZ * (NUM_BLOCKS - 2), stats.free_bytes);
117 zassert_equal(stats.allocated_bytes, 2 * BLK_SZ,
118 "Expected %zu allocated bytes, not %zu\n",
119 2 * BLK_SZ, stats.allocated_bytes);
120 zassert_equal(stats.max_allocated_bytes, 3 * BLK_SZ,
121 "Expected %zu max allocated bytes, not %zu\n",
122 3 * BLK_SZ, stats.max_allocated_bytes);
123
124
125 /* Reset the max allocated blocks; verify max is 2 blocks */
126
127 status = k_mem_slab_runtime_stats_reset_max(&kmslab);
128 zassert_equal(status, 0, "Routine failed with status %d\n", status);
129
130 status = k_mem_slab_runtime_stats_get(&kmslab, &stats);
131 zassert_equal(status, 0, "Routine failed with status %d\n", status);
132
133 zassert_equal(stats.free_bytes, BLK_SZ * (NUM_BLOCKS - 2),
134 "Expected %zu free bytes, not %zu\n",
135 BLK_SZ * (NUM_BLOCKS - 2), stats.free_bytes);
136 zassert_equal(stats.allocated_bytes, 2 * BLK_SZ,
137 "Expected %zu allocated bytes, not %zu\n",
138 2 * BLK_SZ, stats.allocated_bytes);
139 zassert_equal(stats.max_allocated_bytes, 2 * BLK_SZ,
140 "Expected %zu max allocated bytes, not %zu\n",
141 2 * BLK_SZ, stats.max_allocated_bytes);
142
143 /* Free the last two blocks; verify stats results */
144
145 k_mem_slab_free(&kmslab, memory[0]);
146 k_mem_slab_free(&kmslab, memory[1]);
147
148 status = k_mem_slab_runtime_stats_get(&kmslab, &stats);
149 zassert_equal(status, 0, "Routine failed with status %d\n", status);
150
151 zassert_equal(stats.free_bytes, BLK_SZ * NUM_BLOCKS,
152 "Expected %zu free bytes, not %zu\n",
153 BLK_SZ * NUM_BLOCKS, stats.free_bytes);
154 zassert_equal(stats.allocated_bytes, 0,
155 "Expected %zu allocated bytes, not %zu\n",
156 0, stats.allocated_bytes);
157 zassert_equal(stats.max_allocated_bytes, 2 * BLK_SZ,
158 "Expected %zu max allocated bytes, not %zu\n",
159 2 * BLK_SZ, stats.max_allocated_bytes);
160 }
161
162 ZTEST_SUITE(lib_mem_slab_stats_test, NULL, NULL, NULL, NULL, NULL);
163