blob: a79271e73fc69da996f423f82c8854038d3e8767 [file] [log] [blame]
/* nano_ctx_switch.c - measure context switch time between fibers */
/*
* Copyright (c) 2012-2014 Wind River Systems, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
* DESCRIPTION
* This file contains fiber context switch time measurement.
* The task starts two fibers. One fiber waits on a semaphore. The other,
* after starting, releases a semaphore which enable the first fiber to run.
* Each fiber increases a common global counter and context switch back and
* forth by yielding the cpu. When counter reaches the maximal value, fibers
* stop and the average time of context switch is displayed.
*/
#include "timestamp.h"
#include "utils.h"
#include <arch/cpu.h>
/* number of context switches */
#define NCTXSWITCH 10000
#ifndef STACKSIZE
#define STACKSIZE 512
#endif
/* stack used by the fibers */
static char __stack fiberOneStack[STACKSIZE];
static char __stack fiberTwoStack[STACKSIZE];
/* semaphore used for fibers synchronization */
static struct nano_sem syncSema;
static uint32_t timestamp;
/* context switches counter */
static volatile uint32_t ctxSwitchCounter;
/* context switch balancer. Incremented by one fiber, decremented by another*/
static volatile int ctxSwitchBalancer;
/**
*
* fiberOne
*
* Fiber makes all the test preparations: registers the interrupt handler,
* gets the first timestamp and invokes the software interrupt.
*
* @return N/A
*/
static void fiberOne(void)
{
nano_fiber_sem_take(&syncSema, TICKS_UNLIMITED);
timestamp = TIME_STAMP_DELTA_GET(0);
while (ctxSwitchCounter < NCTXSWITCH) {
fiber_yield();
ctxSwitchCounter++;
ctxSwitchBalancer--;
}
timestamp = TIME_STAMP_DELTA_GET(timestamp);
}
/**
*
* @brief Check the time when it gets executed after the semaphore
*
* Fiber starts, waits on semaphore. When the interrupt handler releases
* the semaphore, fiber measures the time.
*
* @return 0 on success
*/
static void fiberTwo(void)
{
nano_fiber_sem_give(&syncSema);
while (ctxSwitchCounter < NCTXSWITCH) {
fiber_yield();
ctxSwitchCounter++;
ctxSwitchBalancer++;
}
}
/**
*
* @brief The test main function
*
* @return 0 on success
*/
int nanoCtxSwitch(void)
{
PRINT_FORMAT(" 4- Measure average context switch time between fibers");
nano_sem_init(&syncSema);
ctxSwitchCounter = 0;
ctxSwitchBalancer = 0;
bench_test_start();
task_fiber_start(&fiberOneStack[0], STACKSIZE,
(nano_fiber_entry_t) fiberOne, 0, 0, 6, 0);
task_fiber_start(&fiberTwoStack[0], STACKSIZE,
(nano_fiber_entry_t) fiberTwo, 0, 0, 6, 0);
if (ctxSwitchBalancer > 3 || ctxSwitchBalancer < -3) {
PRINT_FORMAT(" Balance is %d. FAILED", ctxSwitchBalancer);
} else if (bench_test_end() != 0) {
errorCount++;
PRINT_OVERFLOW_ERROR();
} else {
PRINT_FORMAT(" Average context switch time is %lu tcs = %lu"
" nsec",
timestamp / ctxSwitchCounter,
SYS_CLOCK_HW_CYCLES_TO_NS_AVG(timestamp,
ctxSwitchCounter));
}
return 0;
}