mirror of
https://github.com/apache/nuttx.git
synced 2026-09-11 03:16:45 +00:00
sched/sched_cpuload_oneshot: Use the oneshot timer with optional entropy to measuer cPU load if so configured.
This commit is contained in:
parent
6f833be9d5
commit
300361539a
6 changed files with 359 additions and 4 deletions
|
|
@ -173,7 +173,7 @@ static int sim_max_delay(FAR struct oneshot_lowerhalf_s *lower,
|
|||
DEBUGASSERT(lower != NULL && ts != NULL);
|
||||
|
||||
ts->tv_sec = INT_MAX;
|
||||
ts->tv_nsec = LONG_MAX;
|
||||
ts->tv_nsec = 1000000000ul - 1;
|
||||
return OK;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -45,6 +45,7 @@
|
|||
#include <debug.h>
|
||||
|
||||
#include <nuttx/board.h>
|
||||
#include <nuttx/clock.h>
|
||||
#include <nuttx/timers/oneshot.h>
|
||||
|
||||
#include "up_internal.h"
|
||||
|
|
@ -88,7 +89,7 @@ int sim_bringup(void)
|
|||
#endif
|
||||
|
||||
#ifdef CONFIG_ONESHOT
|
||||
/* Initialize the simulated analog joystick input device */
|
||||
/* Get an instance of the simulated oneshot timer */
|
||||
|
||||
oneshot = oneshot_initialize(0, 0);
|
||||
if (oneshot == NULL)
|
||||
|
|
@ -96,13 +97,22 @@ int sim_bringup(void)
|
|||
_err("ERROR: oneshot_initialize faile\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
{
|
||||
#ifdef CONFIG_CPULOAD_ONESHOT
|
||||
/* Configure the oneshot timer to support CPU load measurement */
|
||||
|
||||
sched_oneshot_extclk(oneshot);
|
||||
|
||||
#else
|
||||
/* Initialize the simulated oneshot driver */
|
||||
|
||||
ret = oneshot_register("/dev/oneshot", oneshot);
|
||||
if (ret < 0)
|
||||
{
|
||||
_err("ERROR: Failed to register oneshot at /dev/oneshot: %d\n",
|
||||
ret);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
|
|
|
|||
|
|
@ -329,6 +329,28 @@ int clock_systimespec(FAR struct timespec *ts);
|
|||
int clock_cpuload(int pid, FAR struct cpuload_s *cpuload);
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: sched_oneshot_extclk
|
||||
*
|
||||
* Description:
|
||||
* Configure to use a oneshot timer as described in
|
||||
* include/nuttx/timers/oneshot.h to provid external clocking to assess
|
||||
* the CPU load.
|
||||
*
|
||||
* Input Parameters:
|
||||
* lower - An instance of the oneshot timer interface as defined in
|
||||
* include/nuttx/timers/oneshot.h
|
||||
*
|
||||
* Returned Value:
|
||||
* None
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_CPULOAD_ONESHOT
|
||||
struct oneshot_lowerhalf_s;
|
||||
void sched_oneshot_extclk(FAR struct oneshot_lowerhalf_s *lower);
|
||||
#endif
|
||||
|
||||
#undef EXTERN
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
|||
|
|
@ -582,10 +582,11 @@ config SCHED_CPULOAD_EXTCLK
|
|||
sched_process_cpuload() at each timer expiration with interrupts
|
||||
disabled.
|
||||
|
||||
if SCHED_CPULOAD_EXTCLK
|
||||
|
||||
config SCHED_CPULOAD_TICKSPERSEC
|
||||
int "External clock rate"
|
||||
default 100
|
||||
depends on SCHED_CPULOAD_EXTCLK
|
||||
---help---
|
||||
If an external clock is used to drive the sampling for the CPU load
|
||||
calculations, then this value must be provided. This value provides
|
||||
|
|
@ -594,6 +595,43 @@ config SCHED_CPULOAD_TICKSPERSEC
|
|||
is the default frequency of the system time and, hence, the worst
|
||||
possible choice in most cases.
|
||||
|
||||
config CPULOAD_ONESHOT
|
||||
bool "Use Oneshot timer"
|
||||
default n
|
||||
---help---
|
||||
Use am MCU-specific oneshot timer as the external clock. The
|
||||
oneshot timer must be configured by board specific logic which must
|
||||
then call:
|
||||
|
||||
void sched_oneshot_extclk(FAR struct oneshot_lowerhalf_s *lower);
|
||||
|
||||
To start the CPU load measurement. See include/nuttx/clock.h
|
||||
|
||||
config CPULOAD_ONESHOT_ENTROPY
|
||||
int "Bits of entropy"
|
||||
default 6
|
||||
range 0 30
|
||||
---help---
|
||||
This is the number of bits of entropy that will be applied. The
|
||||
oneshot will be set to this interval:
|
||||
|
||||
CPULOAD_ONESHOT_NOMINAL - (CPULOAD_ONESHOT_ENTROPY / 2) +
|
||||
error + nrand(CPULOAD_ONESHOT_ENTROPY)
|
||||
|
||||
Where
|
||||
|
||||
CPULOAD_ONESHOT_NOMINAL is CONFIG_SCHED_CPULOAD_TICKSPERSEC in
|
||||
units of microseconds.
|
||||
CPULOAD_ONESHOT_ENTROPY is (1 << CONFIG_CPULOAD_ONESHOT_ENTROPY),
|
||||
and
|
||||
'error' is an error value that is retained from interval to
|
||||
interval so that although individual intervals are randomized,
|
||||
the average will still be CONFIG_SCHED_CPULOAD_TICKSPERSEC.
|
||||
|
||||
This special value of zero disables entropy.
|
||||
|
||||
endif # SCHED_CPULOAD_EXTCLK
|
||||
|
||||
config SCHED_CPULOAD_TIMECONSTANT
|
||||
int "CPU load time constant"
|
||||
default 2
|
||||
|
|
|
|||
|
|
@ -80,6 +80,9 @@ endif
|
|||
|
||||
ifeq ($(CONFIG_SCHED_CPULOAD),y)
|
||||
CSRCS += sched_cpuload.c
|
||||
ifeq ($(CONFIG_CPULOAD_ONESHOT),y)
|
||||
CSRCS += sched_cpuload_oneshot.c
|
||||
endif
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_SCHED_TICKLESS),y)
|
||||
|
|
|
|||
282
sched/sched/sched_cpuload_oneshot.c
Normal file
282
sched/sched/sched_cpuload_oneshot.c
Normal file
|
|
@ -0,0 +1,282 @@
|
|||
/****************************************************************************
|
||||
* sched/sched/sched_cpuload_oneshot.c
|
||||
*
|
||||
* Copyright (C) 2016 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name NuttX nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <time.h>
|
||||
#include <assert.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include <nuttx/arch.h>
|
||||
#include <nuttx/clock.h>
|
||||
#include <nuttx/lib/xorshift128.h>
|
||||
#include <nuttx/timers/oneshot.h>
|
||||
|
||||
#include "clock/clock.h"
|
||||
|
||||
#ifdef CONFIG_CPULOAD_ONESHOT
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
#if !defined(CONFIG_SCHED_CPULOAD) || !defined(CONFIG_SCHED_CPULOAD_EXTCLK)
|
||||
# error CONFIG_SCHED_CPULOAD and CONFIG_SCHED_CPULOAD_EXTCLK must be defined
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_SCHED_CPULOAD_TICKSPERSEC
|
||||
# error CONFIG_SCHED_CPULOAD_TICKSPERSEC not defined
|
||||
#endif
|
||||
|
||||
#define CPULOAD_ONESHOT_NOMINAL (CONFIG_SCHED_CPULOAD_TICKSPERSEC * 1000)
|
||||
|
||||
#if CPULOAD_ONESHOT_NOMINAL < 1 || CPULOAD_ONESHOT_NOMINAL > 0x7fffffff
|
||||
# error CPULOAD_ONESHOT_NOMINAL is out of range
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_CPULOAD_ONESHOT_ENTROPY
|
||||
# warning CONFIG_CPULOAD_ONESHOT_ENTROPY not defined
|
||||
# define CONFIG_CPULOAD_ONESHOT_ENTROPY 0
|
||||
#endif
|
||||
|
||||
#define CPULOAD_ONESHOT_ENTROPY (1 << CONFIG_CPULOAD_ONESHOT_ENTROPY)
|
||||
|
||||
#if CPULOAD_ONESHOT_NOMINAL < CPULOAD_ONESHOT_ENTROPY
|
||||
# error CPULOAD_ONESHOT_NOMINAL too small for CONFIG_CPULOAD_ONESHOT_ENTROPY
|
||||
#endif
|
||||
|
||||
#define CPULOAD_ONESHOT_ENTROPY_MASK (CPULOAD_ONESHOT_ENTROPY - 1)
|
||||
|
||||
/****************************************************************************
|
||||
* Private Types
|
||||
****************************************************************************/
|
||||
|
||||
struct sched_oneshot_s
|
||||
{
|
||||
FAR struct oneshot_lowerhalf_s *oneshot;
|
||||
#if CONFIG_CPULOAD_ONESHOT_ENTROPY > 0
|
||||
int32_t maxdelay;
|
||||
int32_t error;
|
||||
#endif
|
||||
};
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
static void sched_oneshot_start(void);
|
||||
static void sched_oneshot_callback(FAR struct oneshot_lowerhalf_s *lower,
|
||||
FAR void *arg);
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
static struct sched_oneshot_s g_sched_oneshot;
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: sched_oneshot_start
|
||||
*
|
||||
* Description:
|
||||
* [Re-]start the oneshot timer, applying entropy as configured
|
||||
*
|
||||
* Input Parameters:
|
||||
* None
|
||||
* lower - An instance of the lower half driver
|
||||
* arg - The opaque argument provided when the interrupt was registered
|
||||
*
|
||||
* Returned Value:
|
||||
* None
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static void sched_oneshot_start(void)
|
||||
{
|
||||
#if CONFIG_CPULOAD_ONESHOT_ENTROPY > 0
|
||||
struct timespec ts;
|
||||
uint32_t entropy;
|
||||
int32_t secs;
|
||||
#endif
|
||||
int32_t usecs;
|
||||
|
||||
/* Get the next delay */
|
||||
|
||||
#if CONFIG_CPULOAD_ONESHOT_ENTROPY > 0
|
||||
/* The one shot will be set to this interval:
|
||||
*
|
||||
* CPULOAD_ONESHOT_NOMINAL - (CPULOAD_ONESHOT_ENTROPY / 2) + error
|
||||
* + nrand(CPULOAD_ONESHOT_ENTROPY)
|
||||
*/
|
||||
|
||||
usecs = (CPULOAD_ONESHOT_NOMINAL - CPULOAD_ONESHOT_ENTROPY / 2) +
|
||||
g_sched_oneshot.error;
|
||||
|
||||
/* Add the random value in the range 0..(CPULOAD_ONESHOT_ENTRY - 1) */
|
||||
|
||||
entropy = xorshift128();
|
||||
usecs += (int32_t)(entropy & CPULOAD_ONESHOT_ENTROPY_MASK);
|
||||
|
||||
DEBUGASSERT(usecs > 0); /* Check for overflow to negative or zero */
|
||||
|
||||
/* Make sure that the accumulated value does not exceed the maximum delay */
|
||||
|
||||
if (usecs > g_sched_oneshot.maxdelay)
|
||||
{
|
||||
tmrwarn("WARNING: Truncating\n");
|
||||
usecs = g_sched_oneshot.maxdelay;
|
||||
}
|
||||
|
||||
/* Save the new error value */
|
||||
|
||||
g_sched_oneshot.error = CPULOAD_ONESHOT_NOMINAL +
|
||||
g_sched_oneshot.error - usecs;
|
||||
#else
|
||||
/* No entropy */
|
||||
|
||||
usecs = CPULOAD_ONESHOT_NOMINAL;
|
||||
#endif
|
||||
|
||||
/* Then re-start the oneshot timer */
|
||||
|
||||
secs = usecs / 1000000;
|
||||
usecs -= 100000 * secs;
|
||||
|
||||
ts.tv_sec = secs;
|
||||
ts.tv_nsec = 1000 * usecs;
|
||||
|
||||
DEBUGVERIFY(ONESHOT_START(g_sched_oneshot.oneshot, sched_oneshot_callback,
|
||||
NULL, &ts));
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: sched_oneshot_callback
|
||||
*
|
||||
* Description:
|
||||
* This is the callback function that will be invoked when the oneshot
|
||||
* timer expires.
|
||||
*
|
||||
* Input Parameters:
|
||||
* lower - An instance of the lower half driver
|
||||
* arg - The opaque argument provided when the interrupt was registered
|
||||
*
|
||||
* Returned Value:
|
||||
* None
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static void sched_oneshot_callback(FAR struct oneshot_lowerhalf_s *lower,
|
||||
FAR void *arg)
|
||||
{
|
||||
/* Perform CPU load measurements */
|
||||
|
||||
#ifdef CONFIG_HAVE_WEAKFUNCTIONS
|
||||
if (sched_process_cpuload != NULL)
|
||||
#endif
|
||||
{
|
||||
sched_process_cpuload();
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Then restart the oneshot */
|
||||
|
||||
sched_oneshot_start();
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: sched_oneshot_extclk
|
||||
*
|
||||
* Description:
|
||||
* Configure to use a oneshot timer as described in
|
||||
* include/nuttx/timers/oneshot.h to provid external clocking to assess
|
||||
* the CPU load.
|
||||
*
|
||||
* Input Parameters:
|
||||
* lower - An instance of the oneshot timer interface as defined in
|
||||
* include/nuttx/timers/oneshot.h
|
||||
*
|
||||
* Returned Value:
|
||||
* None
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void sched_oneshot_extclk(FAR struct oneshot_lowerhalf_s *lower)
|
||||
{
|
||||
#if CONFIG_CPULOAD_ONESHOT_ENTROPY > 0
|
||||
struct timespec ts;
|
||||
#endif
|
||||
|
||||
DEBUGASSERT(lower != NULL && lower->ops != NULL);
|
||||
DEBUGASSERT(lower->ops->start != NULL);
|
||||
|
||||
#if CONFIG_CPULOAD_ONESHOT_ENTROPY > 0
|
||||
DEBUGASSERT(lower->ops->max_delay != NULL);
|
||||
|
||||
/* Get the maximum delay */
|
||||
|
||||
DEBUGVERIFY(ONESHOT_MAX_DELAY(lower, &ts));
|
||||
if (ts.tv_sec >= 0)
|
||||
{
|
||||
g_sched_oneshot.maxdelay = INT32_MAX;
|
||||
}
|
||||
else
|
||||
{
|
||||
g_sched_oneshot.maxdelay = ts.tv_nsec / 1000;
|
||||
}
|
||||
|
||||
tmrinfo("madelay = %ld usec\n", (long)g_sched_oneshot.maxdelay);
|
||||
DEBUGASSERT(CPULOAD_ONESHOT_NOMINAL < g_sched_oneshot.maxdelay);
|
||||
|
||||
/* Seed the PRNG */
|
||||
|
||||
xorshift128_seed(97, 101, 97 << 17, 101 << 25);
|
||||
#endif
|
||||
|
||||
/* Then start the oneshot */
|
||||
|
||||
g_sched_oneshot.oneshot = lower;
|
||||
sched_oneshot_start();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue