Skip to content

Commit

Permalink
tests: Add mslab_stats test
Browse files Browse the repository at this point in the history
This test case verifies that the mem slab runtime stats routines
behave as expected.

Signed-off-by: Peter Mitsis <[email protected]>
  • Loading branch information
peter-mitsis authored and fabiobaltieri committed Jul 12, 2022
1 parent 5ed73a8 commit 4c1d33a
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 0 deletions.
8 changes: 8 additions & 0 deletions tests/kernel/mem_slab/mslab_stats/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(mem_block)

FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})
4 changes: 4 additions & 0 deletions tests/kernel/mem_slab/mslab_stats/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CONFIG_ZTEST=y
CONFIG_ZTEST_NEW_API=y
CONFIG_MEM_SLAB_TRACE_MAX_UTILIZATION=y
CONFIG_MP_NUM_CPUS=1
162 changes: 162 additions & 0 deletions tests/kernel/mem_slab/mslab_stats/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* Copyright (c) 2021 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/zephyr.h>
#include <ztest.h>

#define BLK_SZ 64
#define NUM_BLOCKS 8

K_MEM_SLAB_DEFINE(kmslab, BLK_SZ, NUM_BLOCKS, 4);

ZTEST(lib_mem_slab_stats_test, test_mem_slab_stats_invalid_params)
{
struct sys_memory_stats stats;
int status;

/*
* Verify that k_mem_slab_runtime_stats_get() returns -EINVAL
* when an invalid parameter is passed.
*/

status = k_mem_slab_runtime_stats_get(NULL, &stats);
zassert_equal(status, -EINVAL, "Routine returned %d instead of %d",
status, -EINVAL);

status = k_mem_slab_runtime_stats_get(&kmslab, NULL);
zassert_equal(status, -EINVAL, "Routine returned %d instead of %d",
status, -EINVAL);

/*
* Verify that k_mem_slab_runtime_stats_reset_max() returns -EINVAL
* when an invalid parameter is passed.
*/

status = k_mem_slab_runtime_stats_reset_max(NULL);
zassert_equal(status, -EINVAL, "Routine returned %d instead of %d",
status, -EINVAL);
}

ZTEST(lib_mem_slab_stats_test, test_mem_slab_runtime_stats)
{
struct sys_memory_stats stats;
int status;
void *memory[3];

/* Verify initial stats */

status = k_mem_slab_runtime_stats_get(&kmslab, &stats);
zassert_equal(status, 0, "Routine failed with status %d\n", status);

zassert_equal(stats.free_bytes, BLK_SZ * NUM_BLOCKS,
"Expected %zu free bytes, not %zu\n",
BLK_SZ * NUM_BLOCKS, stats.free_bytes);
zassert_equal(stats.allocated_bytes, 0,
"Expected 0 allocated bytes, not %zu\n",
stats.allocated_bytes);
zassert_equal(stats.max_allocated_bytes, 0,
"Expected 0 max allocated bytes, not %zu\n",
stats.max_allocated_bytes);

/* Allocate three blocks, and then verify the stats. */

status = k_mem_slab_alloc(&kmslab, &memory[0], K_NO_WAIT);
zassert_equal(status, 0, "Routine failed to allocate 1st block (%d)\n",
status);
status = k_mem_slab_alloc(&kmslab, &memory[1], K_NO_WAIT);
zassert_equal(status, 0, "Routine failed to allocate 2nd block (%d)\n",
status);
status = k_mem_slab_alloc(&kmslab, &memory[2], K_NO_WAIT);
zassert_equal(status, 0, "Routine failed to allocate 3rd block (%d)\n",
status);
status = k_mem_slab_runtime_stats_get(&kmslab, &stats);
zassert_equal(status, 0, "Routine failed with status %d\n", status);

zassert_equal(stats.free_bytes, BLK_SZ * (NUM_BLOCKS - 3),
"Expected %zu free bytes, not %zu\n",
BLK_SZ * (NUM_BLOCKS - 3), stats.free_bytes);
zassert_equal(stats.allocated_bytes, 3 * BLK_SZ,
"Expected %zu allocated bytes, not %zu\n",
3 * BLK_SZ, stats.allocated_bytes);
zassert_equal(stats.max_allocated_bytes, 3 * BLK_SZ,
"Expected %zu max allocated bytes, not %zu\n",
3 * BLK_SZ, stats.max_allocated_bytes);

/* Free blocks 1 and 2, and then verify the stats. */

k_mem_slab_free(&kmslab, &memory[2]);
k_mem_slab_free(&kmslab, &memory[1]);

status = k_mem_slab_runtime_stats_get(&kmslab, &stats);
zassert_equal(status, 0, "Routine failed with status %d\n", status);

zassert_equal(stats.free_bytes, BLK_SZ * (NUM_BLOCKS - 1),
"Expected %zu free bytes, not %zu\n",
BLK_SZ * (NUM_BLOCKS - 1), stats.free_bytes);
zassert_equal(stats.allocated_bytes, 1 * BLK_SZ,
"Expected %zu allocated bytes, not %zu\n",
1 * BLK_SZ, stats.allocated_bytes);
zassert_equal(stats.max_allocated_bytes, 3 * BLK_SZ,
"Expected %zu max allocated bytes, not %zu\n",
3 * BLK_SZ, stats.max_allocated_bytes);

/* Allocate 1 block and verify the max is still at 3 */

status = k_mem_slab_alloc(&kmslab, &memory[1], K_NO_WAIT);
zassert_equal(status, 0, "Routine failed with status %d\n", status);

status = k_mem_slab_runtime_stats_get(&kmslab, &stats);
zassert_equal(status, 0, "Routine failed with status %d\n", status);

zassert_equal(stats.free_bytes, BLK_SZ * (NUM_BLOCKS - 2),
"Expected %zu free bytes, not %zu\n",
BLK_SZ * (NUM_BLOCKS - 2), stats.free_bytes);
zassert_equal(stats.allocated_bytes, 2 * BLK_SZ,
"Expected %zu allocated bytes, not %zu\n",
2 * BLK_SZ, stats.allocated_bytes);
zassert_equal(stats.max_allocated_bytes, 3 * BLK_SZ,
"Expected %zu max allocated bytes, not %zu\n",
3 * BLK_SZ, stats.max_allocated_bytes);


/* Reset the max allocated blocks; verify max is 2 blocks */

status = k_mem_slab_runtime_stats_reset_max(&kmslab);
zassert_equal(status, 0, "Routine failed with status %d\n", status);

status = k_mem_slab_runtime_stats_get(&kmslab, &stats);
zassert_equal(status, 0, "Routine failed with status %d\n", status);

zassert_equal(stats.free_bytes, BLK_SZ * (NUM_BLOCKS - 2),
"Expected %zu free bytes, not %zu\n",
BLK_SZ * (NUM_BLOCKS - 2), stats.free_bytes);
zassert_equal(stats.allocated_bytes, 2 * BLK_SZ,
"Expected %zu allocated bytes, not %zu\n",
2 * BLK_SZ, stats.allocated_bytes);
zassert_equal(stats.max_allocated_bytes, 2 * BLK_SZ,
"Expected %zu max allocated bytes, not %zu\n",
2 * BLK_SZ, stats.max_allocated_bytes);

/* Free the last two blocks; verify stats results */

k_mem_slab_free(&kmslab, &memory[0]);
k_mem_slab_free(&kmslab, &memory[1]);

status = k_mem_slab_runtime_stats_get(&kmslab, &stats);
zassert_equal(status, 0, "Routine failed with status %d\n", status);

zassert_equal(stats.free_bytes, BLK_SZ * NUM_BLOCKS,
"Expected %zu free bytes, not %zu\n",
BLK_SZ * NUM_BLOCKS, stats.free_bytes);
zassert_equal(stats.allocated_bytes, 0,
"Expected %zu allocated bytes, not %zu\n",
0, stats.allocated_bytes);
zassert_equal(stats.max_allocated_bytes, 2 * BLK_SZ,
"Expected %zu max allocated bytes, not %zu\n",
2 * BLK_SZ, stats.max_allocated_bytes);
}

ZTEST_SUITE(lib_mem_slab_stats_test, NULL, NULL, NULL, NULL, NULL);
3 changes: 3 additions & 0 deletions tests/kernel/mem_slab/mslab_stats/testcase.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
tests:
kernel.memory_slab.stats:
tags: kernel

0 comments on commit 4c1d33a

Please sign in to comment.