Skip to content

Commit

Permalink
Merge branch 'core/enh/msg-queue-metrics' (PR16174) into HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
miri64 committed May 6, 2021
2 parents 60ac4de + 92a2730 commit 8e5721e
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 7 deletions.
13 changes: 13 additions & 0 deletions core/include/cib.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ static inline void cib_init(cib_t *__restrict cib, unsigned int size)
*cib = c;
}

/**
* @brief Returns the total capacity (`size` parameter of @ref cib_init()) of
* a cib_t
*
* @param[in] cib the cib_t to check.
* Must not be NULL.
* @return The total size of @p cib.
*/
static inline unsigned int cib_size(const cib_t *cib)
{
return cib->mask + 1;
}

/**
* @brief Calculates difference between cib_put() and cib_get() accesses.
*
Expand Down
21 changes: 20 additions & 1 deletion core/include/msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -356,13 +356,32 @@ int msg_reply(msg_t *m, msg_t *reply);
int msg_reply_int(msg_t *m, msg_t *reply);

/**
* @brief Check how many messages are available in the message queue
* @brief Check how many messages are available (waiting) in the message queue
* of a specific thread
*
* @param[in] pid a PID
*
* @return Number of messages available in queue of @p pid on success
* @return -1, if no caller's message queue is initialized
*/
int msg_avail_thread(kernel_pid_t pid);

/**
* @brief Check how many messages are available (waiting) in the message queue
*
* @return Number of messages available in our queue on success
* @return -1, if no caller's message queue is initialized
*/
int msg_avail(void);

/**
* @brief Get maximum capacity of a thread's queue length
*
* @return Number of total messages that fit in the queue of @p pid on success
* @return -1, if no caller's message queue is initialized
*/
int msg_queue_capacity(kernel_pid_t pid);

/**
* @brief Initialize the current thread's message queue.
*
Expand Down
35 changes: 29 additions & 6 deletions core/msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -433,22 +433,45 @@ static int _msg_receive(msg_t *m, int block)
DEBUG("This should have never been reached!\n");
}

int msg_avail(void)
static int _msg_avail(thread_t *thread)
{
DEBUG("msg_available: %" PRIkernel_pid ": msg_available.\n",
thread_getpid());

thread_t *me = thread_get_active();
thread->pid);

int queue_index = -1;

if (thread_has_msg_queue(me)) {
queue_index = cib_avail(&(me->msg_queue));
if (thread_has_msg_queue(thread)) {
queue_index = cib_avail(&(thread->msg_queue));
}

return queue_index;
}

int msg_avail_thread(kernel_pid_t pid)
{
return _msg_avail(thread_get(pid));
}

int msg_avail(void)
{
return _msg_avail(thread_get_active());
}

int msg_queue_capacity(kernel_pid_t pid)
{
DEBUG("msg_queue_capacity: %" PRIkernel_pid ": msg_queue_capacity.\n",
pid);

thread_t *thread = thread_get(pid);
int queue_cap = -1;

if (thread_has_msg_queue(thread)) {
queue_cap = cib_size(&(thread->msg_queue));
}

return queue_cap;
}

void msg_init_queue(msg_t *array, int num)
{
thread_t *me = thread_get_active();
Expand Down
5 changes: 5 additions & 0 deletions tests/msg_queue_len/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
include ../Makefile.tests_common

USEMODULE += embunit

include $(RIOTBASE)/Makefile.include
59 changes: 59 additions & 0 deletions tests/msg_queue_len/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (C) 2015 Nick van IJzendoorn <nijzendoorn@engineering-spirit.nl>
* 2017 HAW Hamburg
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/

/**
* @ingroup tests
* @{
*
* @file
* @brief Thread test application
*
* @author Nick van IJzendoorn <nijzendoorn@engineering-spirit.nl>
* @author Sebastian Meiling <s@mlng.net>
*
* @}
*/

#include <stdio.h>
#include <inttypes.h>

#include "embUnit/embUnit.h"
#include "embUnit.h"
#include "msg.h"
#include "thread.h"

#define MSG_QUEUE_LENGTH (8)

static msg_t msg_queue[MSG_QUEUE_LENGTH];

static void test_msg_queue_len(void)
{
TEST_ASSERT_EQUAL_INT(-1, msg_queue_len(thread_getpid()));
msg_init_queue(msg_queue, MSG_QUEUE_LENGTH);
TEST_ASSERT_EQUAL_INT(MSG_QUEUE_LENGTH, msg_queue_len(thread_getpid()));
}

static Test *tests_msg_queue_len_suite(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(test_msg_queue_len),
};

EMB_UNIT_TESTCALLER(tests, NULL, NULL, fixtures);

return (Test *)&tests;
}

int main(void)
{
TESTS_START();
TESTS_RUN(tests_msg_queue_len_suite());
TESTS_END();
return 0;
}
15 changes: 15 additions & 0 deletions tests/msg_queue_len/tests/01-run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env python3

# Copyright (C) 2016 Kaspar Schleiser <kaspar@schleiser.de>
# 2017 Sebastian Meiling <s@mlng.net>
#
# This file is subject to the terms and conditions of the GNU Lesser
# General Public License v2.1. See the file LICENSE in the top level
# directory for more details.

import sys
from testrunner import run_check_unittests


if __name__ == "__main__":
sys.exit(run_check_unittests())

0 comments on commit 8e5721e

Please sign in to comment.