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