2015-10-03 01:33:30 +02:00
|
|
|
/****************************************************************************
|
2018-12-12 23:37:38 +01:00
|
|
|
* apps/testing/ostest/sporadic.c
|
2015-07-24 21:50:31 +02:00
|
|
|
*
|
2021-03-18 20:41:20 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2015-07-24 21:50:31 +02:00
|
|
|
*
|
2021-03-18 20:41:20 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-07-24 21:50:31 +02:00
|
|
|
*
|
2021-03-18 20:41:20 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2015-07-24 21:50:31 +02:00
|
|
|
*
|
2015-10-03 01:33:30 +02:00
|
|
|
****************************************************************************/
|
2015-07-24 21:50:31 +02:00
|
|
|
|
2015-10-03 01:33:30 +02:00
|
|
|
/****************************************************************************
|
2015-07-24 21:50:31 +02:00
|
|
|
* Included Files
|
2015-10-03 01:33:30 +02:00
|
|
|
****************************************************************************/
|
2015-07-24 21:50:31 +02:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <semaphore.h>
|
|
|
|
#include <sched.h>
|
2015-07-24 22:13:01 +02:00
|
|
|
#include <time.h>
|
2015-07-24 21:50:31 +02:00
|
|
|
|
|
|
|
#include "ostest.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_SCHED_SPORADIC
|
|
|
|
|
2015-10-03 01:33:30 +02:00
|
|
|
/****************************************************************************
|
2015-07-24 21:50:31 +02:00
|
|
|
* Pre-processor Definitions
|
2015-10-03 01:33:30 +02:00
|
|
|
****************************************************************************/
|
2015-07-24 21:50:31 +02:00
|
|
|
|
2015-07-28 22:27:48 +02:00
|
|
|
/* It is actually a better test without schedule locking because that
|
|
|
|
* forces the scheduler into an uninteresting fallback mode.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#undef sched_lock
|
|
|
|
#undef sched_unlock
|
|
|
|
#define sched_lock()
|
|
|
|
#define sched_unlock()
|
|
|
|
|
2015-07-25 20:47:24 +02:00
|
|
|
#ifndef MIN
|
2022-03-16 12:13:12 +01:00
|
|
|
# define MIN(a,b) ((a) < (b) ? (a) : (b))
|
2015-07-25 20:47:24 +02:00
|
|
|
#endif
|
|
|
|
|
2015-10-03 01:33:30 +02:00
|
|
|
/****************************************************************************
|
2015-07-24 21:50:31 +02:00
|
|
|
* Private Data
|
2015-10-03 01:33:30 +02:00
|
|
|
****************************************************************************/
|
2015-07-24 21:50:31 +02:00
|
|
|
|
|
|
|
static sem_t g_sporadic_sem;
|
2015-07-25 02:16:10 +02:00
|
|
|
static time_t g_start_time;
|
2015-07-24 21:50:31 +02:00
|
|
|
|
2015-10-03 01:33:30 +02:00
|
|
|
/****************************************************************************
|
2015-07-24 21:50:31 +02:00
|
|
|
* Private Functions
|
2015-10-03 01:33:30 +02:00
|
|
|
****************************************************************************/
|
2015-07-24 21:50:31 +02:00
|
|
|
|
2017-05-19 15:13:12 +02:00
|
|
|
static void my_mdelay(unsigned int milliseconds)
|
2015-07-27 23:52:27 +02:00
|
|
|
{
|
|
|
|
volatile unsigned int i;
|
|
|
|
volatile unsigned int j;
|
|
|
|
|
|
|
|
for (i = 0; i < milliseconds; i++)
|
|
|
|
{
|
|
|
|
for (j = 0; j < CONFIG_BOARD_LOOPSPERMSEC; j++)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-19 15:13:12 +02:00
|
|
|
|
2015-07-25 02:16:10 +02:00
|
|
|
static void *nuisance_func(void *parameter)
|
|
|
|
{
|
2015-07-27 23:52:27 +02:00
|
|
|
/* Synchronized start */
|
|
|
|
|
2015-07-25 02:16:10 +02:00
|
|
|
while (sem_wait(&g_sporadic_sem) < 0);
|
|
|
|
|
2015-07-27 23:52:27 +02:00
|
|
|
/* Sleep until we are cancelled */
|
|
|
|
|
2021-03-18 20:41:20 +01:00
|
|
|
for (; ; )
|
2015-07-25 02:16:10 +02:00
|
|
|
{
|
2015-07-27 23:52:27 +02:00
|
|
|
/* Sleep gracefully for awhile */
|
|
|
|
|
2021-03-18 20:41:20 +01:00
|
|
|
usleep(500 * 1000);
|
2015-07-27 23:52:27 +02:00
|
|
|
|
|
|
|
/* Then hog some CPU time */
|
|
|
|
|
|
|
|
my_mdelay(100);
|
2015-07-25 02:16:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-07-24 21:50:31 +02:00
|
|
|
static void *fifo_func(void *parameter)
|
|
|
|
{
|
|
|
|
struct sched_param param;
|
2015-07-24 22:13:01 +02:00
|
|
|
time_t last;
|
|
|
|
time_t now;
|
2015-07-24 21:50:31 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
while (sem_wait(&g_sporadic_sem) < 0);
|
|
|
|
|
2015-07-25 02:16:10 +02:00
|
|
|
last = g_start_time;
|
2015-07-24 22:13:01 +02:00
|
|
|
|
2021-03-18 20:41:20 +01:00
|
|
|
for (; ; )
|
2015-07-24 21:50:31 +02:00
|
|
|
{
|
2015-07-24 22:13:01 +02:00
|
|
|
do
|
2015-07-24 21:50:31 +02:00
|
|
|
{
|
2015-07-25 20:29:22 +02:00
|
|
|
sched_lock(); /* Just to exercise more logic */
|
2015-07-24 21:50:31 +02:00
|
|
|
ret = sched_getparam(0, ¶m);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
printf("ERROR: sched_getparam failed\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-07-24 22:13:01 +02:00
|
|
|
|
|
|
|
now = time(NULL);
|
2015-07-25 20:29:22 +02:00
|
|
|
sched_unlock();
|
2015-07-24 21:50:31 +02:00
|
|
|
}
|
2015-07-24 22:13:01 +02:00
|
|
|
while (now == last);
|
2015-07-24 21:50:31 +02:00
|
|
|
|
2015-07-25 20:29:22 +02:00
|
|
|
sched_lock(); /* Just to exercise more logic */
|
2015-07-24 22:13:01 +02:00
|
|
|
printf("%4lu FIFO: %d\n",
|
2021-03-18 20:41:20 +01:00
|
|
|
(unsigned long)(now - g_start_time), param.sched_priority);
|
2015-07-24 22:13:01 +02:00
|
|
|
last = now;
|
2015-07-25 20:29:22 +02:00
|
|
|
sched_unlock();
|
2015-07-24 21:50:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-18 20:41:20 +01:00
|
|
|
static FAR void *sporadic_func(FAR void *parameter)
|
2015-07-24 21:50:31 +02:00
|
|
|
{
|
|
|
|
struct sched_param param;
|
2015-07-24 22:13:01 +02:00
|
|
|
time_t last;
|
|
|
|
time_t now;
|
2015-07-24 21:50:31 +02:00
|
|
|
int prio = 0;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
while (sem_wait(&g_sporadic_sem) < 0);
|
|
|
|
|
2015-07-25 02:16:10 +02:00
|
|
|
last = g_start_time;
|
2015-07-24 22:13:01 +02:00
|
|
|
|
2021-03-18 20:41:20 +01:00
|
|
|
for (; ; )
|
2015-07-24 21:50:31 +02:00
|
|
|
{
|
2015-07-24 22:13:01 +02:00
|
|
|
do
|
2015-07-24 21:50:31 +02:00
|
|
|
{
|
2015-07-25 20:29:22 +02:00
|
|
|
sched_lock(); /* Just to exercise more logic */
|
2015-07-24 21:50:31 +02:00
|
|
|
ret = sched_getparam(0, ¶m);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
printf("ERROR: sched_getparam failed\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-07-24 22:13:01 +02:00
|
|
|
now = time(NULL);
|
2015-07-25 20:29:22 +02:00
|
|
|
sched_unlock();
|
2015-07-24 21:50:31 +02:00
|
|
|
}
|
2015-07-24 22:13:01 +02:00
|
|
|
while (now == last && prio == param.sched_priority);
|
2015-07-24 21:50:31 +02:00
|
|
|
|
2015-07-25 20:29:22 +02:00
|
|
|
sched_lock(); /* Just to exercise more logic */
|
2015-07-24 22:13:01 +02:00
|
|
|
printf("%4lu SPORADIC: %d->%d\n",
|
2021-03-18 20:41:20 +01:00
|
|
|
(unsigned long)(now - g_start_time), prio,
|
|
|
|
param.sched_priority);
|
2015-07-24 21:50:31 +02:00
|
|
|
prio = param.sched_priority;
|
2015-07-24 22:13:01 +02:00
|
|
|
last = now;
|
2015-07-25 20:29:22 +02:00
|
|
|
sched_unlock();
|
2015-07-24 21:50:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-03 01:33:30 +02:00
|
|
|
/****************************************************************************
|
2015-07-24 21:50:31 +02:00
|
|
|
* Public Functions
|
2015-10-03 01:33:30 +02:00
|
|
|
****************************************************************************/
|
2015-07-24 21:50:31 +02:00
|
|
|
|
|
|
|
void sporadic_test(void)
|
|
|
|
{
|
2015-07-25 02:16:10 +02:00
|
|
|
pthread_t nuisance_thread = (pthread_t)0;
|
2015-07-24 21:50:31 +02:00
|
|
|
pthread_t sporadic_thread = (pthread_t)0;
|
|
|
|
pthread_t fifo_thread = (pthread_t)0;
|
|
|
|
#ifdef SDCC
|
|
|
|
pthread_addr_t result;
|
|
|
|
#endif
|
|
|
|
FAR void *result;
|
|
|
|
struct sched_param myparam;
|
|
|
|
struct sched_param sparam;
|
|
|
|
int prio_min;
|
|
|
|
int prio_max;
|
|
|
|
int prio_low;
|
|
|
|
int prio_mid;
|
|
|
|
int prio_high;
|
|
|
|
pthread_attr_t attr;
|
|
|
|
int ret;
|
|
|
|
|
2015-07-26 23:34:33 +02:00
|
|
|
#if CONFIG_SCHED_SPORADIC_MAXREPL < 5
|
|
|
|
printf("sporadic_test: CONFIG_SCHED_SPORADIC_MAXREPL is small: %d\n",
|
|
|
|
CONFIG_SCHED_SPORADIC_MAXREPL);
|
2017-05-11 21:40:03 +02:00
|
|
|
printf(" -- There will some errors in the replenishment interval\n");
|
2015-07-26 23:34:33 +02:00
|
|
|
#endif
|
|
|
|
|
2015-07-24 21:50:31 +02:00
|
|
|
printf("sporadic_test: Initializing semaphore to 0\n");
|
|
|
|
sem_init(&g_sporadic_sem, 0, 0);
|
|
|
|
|
|
|
|
prio_min = sched_get_priority_min(SCHED_FIFO);
|
|
|
|
prio_max = sched_get_priority_max(SCHED_FIFO);
|
|
|
|
|
|
|
|
prio_low = prio_min + ((prio_max - prio_min) >> 2);
|
|
|
|
prio_mid = (prio_min + prio_max) >> 1;
|
|
|
|
prio_high = prio_max - ((prio_max - prio_min) >> 2);
|
|
|
|
|
2015-07-25 02:16:10 +02:00
|
|
|
/* Temporarily set our priority to prio_high + 2 */
|
2015-07-24 21:50:31 +02:00
|
|
|
|
|
|
|
ret = sched_getparam(0, &myparam);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2015-07-26 18:52:47 +02:00
|
|
|
printf("sporadic_test: ERROR: sched_getparam failed, ret=%d\n", ret);
|
2015-07-24 21:50:31 +02:00
|
|
|
}
|
|
|
|
|
2015-07-25 02:16:10 +02:00
|
|
|
sparam.sched_priority = prio_high + 2;
|
2015-07-24 21:50:31 +02:00
|
|
|
ret = sched_setparam(0, &sparam);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2015-07-26 18:52:47 +02:00
|
|
|
printf("sporadic_test: ERROR: sched_setparam failed, ret=%d\n", ret);
|
2015-07-24 21:50:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = pthread_attr_init(&attr);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2015-07-26 18:52:47 +02:00
|
|
|
printf("sporadic_test: ERROR: pthread_attr_init failed, ret=%d\n",
|
|
|
|
ret);
|
2015-07-24 21:50:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This semaphore will prevent anything from running until we are ready */
|
|
|
|
|
|
|
|
sched_lock();
|
|
|
|
sem_init(&g_sporadic_sem, 0, 0);
|
|
|
|
|
2015-07-25 02:16:10 +02:00
|
|
|
/* Start a FIFO thread at the highest priority (prio_max + 1) */
|
2015-07-24 21:50:31 +02:00
|
|
|
|
|
|
|
printf("sporadic_test: Starting FIFO thread at priority %d\n", prio_mid);
|
|
|
|
|
|
|
|
ret = pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2021-03-18 20:41:20 +01:00
|
|
|
printf("sporadic_test: ERROR: pthread_attr_setschedpolicy failed, "
|
|
|
|
"ret=%d\n",
|
2015-07-26 18:52:47 +02:00
|
|
|
ret);
|
2015-07-24 21:50:31 +02:00
|
|
|
}
|
|
|
|
|
2015-07-25 02:16:10 +02:00
|
|
|
sparam.sched_priority = prio_high + 1;
|
|
|
|
ret = pthread_attr_setschedparam(&attr, &sparam);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2021-03-18 20:41:20 +01:00
|
|
|
printf("sporadic_test: ERROR: pthread_attr_setschedparam failed, "
|
|
|
|
"ret=%d\n",
|
2015-07-26 18:52:47 +02:00
|
|
|
ret);
|
2015-07-25 02:16:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = pthread_create(&nuisance_thread, &attr, nuisance_func, NULL);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2015-07-26 18:52:47 +02:00
|
|
|
printf("sporadic_test: ERROR: FIFO thread creation failed: %d\n",
|
|
|
|
ret);
|
2015-07-25 02:16:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Start a FIFO thread at the middle priority */
|
|
|
|
|
2015-07-24 21:50:31 +02:00
|
|
|
sparam.sched_priority = prio_mid;
|
|
|
|
ret = pthread_attr_setschedparam(&attr, &sparam);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2021-03-18 20:41:20 +01:00
|
|
|
printf("sporadic_test: ERROR: pthread_attr_setschedparam failed, "
|
|
|
|
"ret=%d\n",
|
2015-07-26 18:52:47 +02:00
|
|
|
ret);
|
2015-07-24 21:50:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = pthread_create(&fifo_thread, &attr, fifo_func, NULL);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2015-07-26 18:52:47 +02:00
|
|
|
printf("sporadic_test: ERROR: FIFO thread creation failed: %d\n",
|
|
|
|
ret);
|
2015-07-24 21:50:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Start a sporadic thread, with the following parameters: */
|
|
|
|
|
2021-03-18 20:41:20 +01:00
|
|
|
printf("sporadic_test: Starting sporadic thread at priority "
|
|
|
|
"%d (hi) %d (lo)\n",
|
2015-07-26 18:52:47 +02:00
|
|
|
prio_high, prio_low);
|
2015-07-24 21:50:31 +02:00
|
|
|
|
|
|
|
ret = pthread_attr_setschedpolicy(&attr, SCHED_SPORADIC);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2021-03-18 20:41:20 +01:00
|
|
|
printf("sporadic_test: ERROR: pthread_attr_setschedpolicy failed, "
|
|
|
|
"ret=%d\n",
|
2015-07-26 18:52:47 +02:00
|
|
|
ret);
|
2015-07-24 21:50:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sparam.sched_priority = prio_high;
|
|
|
|
sparam.sched_ss_low_priority = prio_low;
|
2015-07-28 22:27:48 +02:00
|
|
|
sparam.sched_ss_repl_period.tv_sec = 5;
|
2015-07-24 21:50:31 +02:00
|
|
|
sparam.sched_ss_repl_period.tv_nsec = 0;
|
|
|
|
sparam.sched_ss_init_budget.tv_sec = 2;
|
2015-07-24 22:13:01 +02:00
|
|
|
sparam.sched_ss_init_budget.tv_nsec = 0;
|
2015-07-27 16:10:59 +02:00
|
|
|
sparam.sched_ss_max_repl = CONFIG_SCHED_SPORADIC_MAXREPL;
|
2015-07-24 21:50:31 +02:00
|
|
|
|
|
|
|
ret = pthread_attr_setschedparam(&attr, &sparam);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2021-03-18 20:41:20 +01:00
|
|
|
printf("sporadic_test: ERROR: pthread_attr_setsched param failed, "
|
|
|
|
"ret=%d\n",
|
2015-07-26 18:52:47 +02:00
|
|
|
ret);
|
2015-07-24 21:50:31 +02:00
|
|
|
}
|
|
|
|
|
2021-03-18 20:41:20 +01:00
|
|
|
ret = pthread_create(&sporadic_thread, &attr, sporadic_func,
|
|
|
|
(pthread_addr_t)1);
|
2015-07-24 21:50:31 +02:00
|
|
|
if (ret != 0)
|
|
|
|
{
|
2015-07-26 18:52:47 +02:00
|
|
|
printf("sporadic_test: ERROR: sporadic thread creation failed: %d\n",
|
|
|
|
ret);
|
2015-07-24 21:50:31 +02:00
|
|
|
}
|
|
|
|
|
2015-07-25 02:16:10 +02:00
|
|
|
g_start_time = time(NULL);
|
|
|
|
|
|
|
|
sem_post(&g_sporadic_sem);
|
2015-07-24 21:50:31 +02:00
|
|
|
sem_post(&g_sporadic_sem);
|
|
|
|
sem_post(&g_sporadic_sem);
|
|
|
|
|
|
|
|
/* Wait a while then kill the FIFO thread */
|
|
|
|
|
2015-07-28 22:27:48 +02:00
|
|
|
sleep(15);
|
2015-07-24 21:50:31 +02:00
|
|
|
ret = pthread_cancel(fifo_thread);
|
|
|
|
pthread_join(fifo_thread, &result);
|
|
|
|
|
2015-07-25 02:16:10 +02:00
|
|
|
/* Wait a bit longer then kill the nuisance thread */
|
2015-07-24 21:50:31 +02:00
|
|
|
|
2015-07-28 22:27:48 +02:00
|
|
|
sleep(10);
|
2015-07-25 02:16:10 +02:00
|
|
|
ret = pthread_cancel(nuisance_thread);
|
|
|
|
pthread_join(nuisance_thread, &result);
|
|
|
|
|
|
|
|
/* Wait a bit longer then kill the sporadic thread */
|
|
|
|
|
2015-07-28 22:27:48 +02:00
|
|
|
sleep(10);
|
2015-07-24 21:50:31 +02:00
|
|
|
ret = pthread_cancel(sporadic_thread);
|
|
|
|
pthread_join(sporadic_thread, &result);
|
|
|
|
sched_unlock();
|
|
|
|
|
|
|
|
printf("sporadic_test: Done\n");
|
|
|
|
sem_destroy(&g_sporadic_sem);
|
|
|
|
|
|
|
|
ret = sched_setparam(0, &myparam);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2015-07-26 18:52:47 +02:00
|
|
|
printf("sporadic_test: ERROR: sched_setparam failed, ret=%d\n", ret);
|
2015-07-24 21:50:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-19 15:13:12 +02:00
|
|
|
#endif /* CONFIG_SCHED_SPORADIC */
|