Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A sample application to demonstrate use of kernel event logger feature. #4552

Merged
merged 2 commits into from
Nov 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions samples/subsys/logging/kernel_event_logger/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
BOARD ?= qemu_x86
CONF_FILE = prj.conf

include ${ZEPHYR_BASE}/Makefile.inc
39 changes: 39 additions & 0 deletions samples/subsys/logging/kernel_event_logger/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
.. _kerneleventlogger_sample:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the first document in the logging area, so you'll need to also add a file samples/subsys/logging/logging.rst that looks like samples/subsys/console/console.rst (copy that one and edit the title). This will remove the "not included in a toc-tree" warning.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok


Kernel Event Logger Sample
################################

Overview
********

A simple application that demonstrates use of kernel event
logger feature. Two threads (A and B) of the same priority
are created and main thread is configured to have low priority,
which reads the events from kernel event logger's buffer and
prints to the console. When thread 'A' gets scheduled it will
sleep for 1 second. Similarly when thread 'B' gets scheduled
it will sleep for 0.5 seconds. When both A and B are sleeping,
the main thread gets scheduled and retrieves the events captured
from kernel event logger's buffer and prints to the console.

Building and Running
********************

This project outputs to the console. It can be built and executed
on QEMU as follows:

.. code-block:: console

$ cd samples/subsys/kernel_event_logger
$ make run

Sample Output
=============

.. code-block:: console

tid of context switched thread = 400080 at time = 96538
tid of context switched thread = 4000c0 at time = 98047
thread = 400080, is moved to = REDAY_Q , at time = 51019657
thread = 400040, is moved to = REDAY_Q , at time = 51024998

9 changes: 9 additions & 0 deletions samples/subsys/logging/kernel_event_logger/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CONFIG_BOOT_BANNER=y

CONFIG_MAIN_THREAD_PRIORITY=3
CONFIG_RING_BUFFER=y
CONFIG_KERNEL_EVENT_LOGGER=y
CONFIG_KERNEL_EVENT_LOGGER_CONTEXT_SWITCH=y
CONFIG_KERNEL_EVENT_LOGGER_INTERRUPT=n
CONFIG_KERNEL_EVENT_LOGGER_THREAD=y
CONFIG_USERSPACE=n
9 changes: 9 additions & 0 deletions samples/subsys/logging/kernel_event_logger/sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
sample:
description: A simple application that demonstrate use of
kernel event logging feature.
name: Kernel event logger Sample
tests:
- test:
build_only: true
tags: apps
platform_whitelist: qemu_x86
1 change: 1 addition & 0 deletions samples/subsys/logging/kernel_event_logger/src/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
obj-y += main.o
101 changes: 101 additions & 0 deletions samples/subsys/logging/kernel_event_logger/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright (c) 2017 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <kernel_structs.h>
#include <logging/kernel_event_logger.h>
#include <kernel_structs.h>
#include <misc/printk.h>
#include <misc/util.h>
#include <zephyr.h>

K_THREAD_STACK_DEFINE(threadA_stack, 1024);
K_THREAD_STACK_DEFINE(threadB_stack, 1024);
K_THREAD_STACK_DEFINE(event_logger_stack, 4096);

K_SEM_DEFINE(sem_sync, 0, 1); /* starts off "not available" */

static struct k_thread threadA_data;
static struct k_thread threadB_data;
static u32_t timestamp;
char event_type[][12] = {"REDAY_Q", "PEND_STATE", "EXIT_STATE"};

static void event_logger(void)
{
u32_t event_data[4];
u16_t event_id;
u8_t dropped;
u8_t event_data_size = (u8_t)ARRAY_SIZE(event_data);
int ret;

for (;;) {

ret = sys_k_event_logger_get_wait(&event_id,
&dropped,
event_data,
&event_data_size);
if (ret < 0) {
continue;
}

timestamp = event_data[0];

switch (event_id) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a default to the switch

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

case KERNEL_EVENT_LOGGER_CONTEXT_SWITCH_EVENT_ID:
printk("tid of context switched thread = %x at "
"time = %d\n", event_data[1], timestamp);
break;
case KERNEL_EVENT_LOGGER_INTERRUPT_EVENT_ID:
break;
case KERNEL_EVENT_LOGGER_SLEEP_EVENT_ID:
break;
case KERNEL_EVENT_LOGGER_THREAD_EVENT_ID:
printk("thread = %x, is moved to = %s ,at time = %d\n"
, event_data[1], event_type[event_data[2]], timestamp);
break;
default:
break;
}

}
}

static void threadB(void)
{
unsigned int i = 10;

for (; i ; i--) {
k_sleep(MSEC_PER_SEC / 2);
}

k_sem_take(&sem_sync, K_FOREVER);
}

static void threadA(void)
{
unsigned int j = 10;

for (; j ; j--) {

k_sleep(MSEC_PER_SEC);
}

k_sem_give(&sem_sync);
}

void main(void)
{
k_thread_create(&threadB_data, threadB_stack,
K_THREAD_STACK_SIZEOF(threadB_stack),
(k_thread_entry_t)threadB,
NULL, NULL, NULL, K_PRIO_PREEMPT(2), 0, 0);

k_thread_create(&threadA_data, threadA_stack,
K_THREAD_STACK_SIZEOF(threadA_stack),
(k_thread_entry_t)threadA,
NULL, NULL, NULL, K_PRIO_PREEMPT(2), 0, 0);

event_logger();
}
10 changes: 10 additions & 0 deletions samples/subsys/logging/logging.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.. _logging-samples:

Logging Sample
################

.. toctree::
:maxdepth: 1
:glob:

**/*
29 changes: 26 additions & 3 deletions subsys/logging/event_logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <logging/event_logger.h>
#include <misc/ring_buffer.h>
#include <kernel_structs.h>

void sys_event_logger_init(struct event_logger *logger,
u32_t *logger_buffer, u32_t buffer_size)
Expand Down Expand Up @@ -44,7 +45,18 @@ static void event_logger_put(struct event_logger *logger, u16_t event_id,
void sys_event_logger_put(struct event_logger *logger, u16_t event_id,
u32_t *event_data, u8_t data_size)
{
event_logger_put(logger, event_id, event_data, data_size, k_sem_give);
/* The thread invoking sys_k_event_logger_get_wait() supposed
* to only read the events of the threads which logged to kernel event
* logger buffer. But it should not write to kernel event logger
* buffer. Otherwise it would cause the race condition.
*/

struct k_thread *event_logger_thread =
(struct k_thread *)sys_dlist_peek_head(&(logger->sync_sema.wait_q));
if (_current != event_logger_thread) {
event_logger_put(logger, event_id, event_data,
data_size, k_sem_give);
}
}


Expand All @@ -71,8 +83,19 @@ void _sys_event_logger_put_non_preemptible(struct event_logger *logger,
{
extern void _sem_give_non_preemptible(struct k_sem *sem);

event_logger_put(logger, event_id, event_data, data_size,
_sem_give_non_preemptible);
/* The thread invoking sys_k_event_logger_get_wait() supposed
* to only read the events of the threads which logged to kernel event
* logger buffer. But it should not write to kernel event logger
* buffer. Otherwise it would cause the race condition.
*/

struct k_thread *event_logger_thread =
(struct k_thread *)sys_dlist_peek_head(&(logger->sync_sema.wait_q));

if (_current != event_logger_thread) {
event_logger_put(logger, event_id, event_data, data_size,
_sem_give_non_preemptible);
}
}


Expand Down