Skip to content

Commit

Permalink
feat: enable weighted rr
Browse files Browse the repository at this point in the history
  • Loading branch information
every-breaking-wave committed Nov 21, 2024
1 parent cb50a7f commit 5e971c3
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 30 deletions.
1 change: 1 addition & 0 deletions FreeRTOS/Demo/EDF/FreeRTOSConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#define configUSE_TICK_HOOK 1
#define configCPU_CLOCK_HZ ( ( unsigned long ) 25000000 )
#define configTICK_RATE_HZ ( ( TickType_t ) 1000 )
#define configSLICE_INTERVAL ( ( TickType_t ) 10 )
#define configMINIMAL_STACK_SIZE ( ( unsigned short ) 80 )
#define configTOTAL_HEAP_SIZE ( ( size_t ) ( 60 * 1024 ) )
#define configMAX_TASK_NAME_LEN ( 12 )
Expand Down
5 changes: 3 additions & 2 deletions FreeRTOS/Demo/ROUND_ROBIN/FreeRTOSConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@
#define configUSE_TRACE_FACILITY 0
#define configGENERATE_RUN_TIME_STATS 0

#define configUSE_PREEMPTION 0
#define configUSE_PREEMPTION 1
#define configUSE_IDLE_HOOK 0
#define configUSE_TICK_HOOK 1
#define configCPU_CLOCK_HZ ( ( unsigned long ) 25000000 )
#define configTICK_RATE_HZ ( ( TickType_t ) 1000 )
#define configSLICE_INTERVAL ( ( TickType_t ) 10 )
#define configMINIMAL_STACK_SIZE ( ( unsigned short ) 80 )
#define configTOTAL_HEAP_SIZE ( ( size_t ) ( 60 * 1024 ) )
#define configMAX_TASK_NAME_LEN ( 12 )
Expand All @@ -63,7 +64,7 @@

#define configUSE_QUEUE_SETS 1
#define configUSE_COUNTING_SEMAPHORES 1
#define configUSE_TIME_SLICING 1 // USE_PREEMPTION must be 0, USE_EDF_SCHEDULER must be 0
#define configUSE_TIME_SLICING 1 // USE_PREEMPTION must be 1, USE_EDF_SCHEDULER must be 0
#define configUSE_EDF_SCHEDULER 0

#define configMAX_PRIORITIES ( 9UL )
Expand Down
8 changes: 6 additions & 2 deletions FreeRTOS/Demo/ROUND_ROBIN_WEIGHT/FreeRTOSConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
#define configUSE_TRACE_FACILITY 0
#define configGENERATE_RUN_TIME_STATS 0

#define configUSE_PREEMPTION 0
#define configUSE_PREEMPTION 1
#define configUSE_IDLE_HOOK 0
#define configUSE_TICK_HOOK 1
#define configCPU_CLOCK_HZ ( ( unsigned long ) 25000000 )
Expand All @@ -61,7 +61,10 @@
#define configUSE_QUEUE_SETS 1
#define configUSE_COUNTING_SEMAPHORES 1
#define configUSE_TIME_SLICING 1
#define configUSE_WEIGHTED_ROUND_ROBIN 1 // USE_PREEMPTION must be 0, TIME_SLICING must be 1
#define configSLICE_INTERVAL ( ( TickType_t ) 10 )
#define configUSE_WEIGHTED_ROUND_ROBIN 1 // USE_PREEMPTION must be 1, TIME_SLICING must be 1

#define configNUMBER_OF_CORES 1

#define configMAX_PRIORITIES ( 9UL )
#define configMAX_CO_ROUTINE_PRIORITIES ( 2 )
Expand Down Expand Up @@ -139,4 +142,5 @@

#define configUSE_EDF_SCHEDULER 0

#define configUSE_TRACE_FACILITY 1
#endif /* FREERTOS_CONFIG_H */
2 changes: 1 addition & 1 deletion FreeRTOS/Demo/ROUND_ROBIN_WEIGHT/build/gcc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ SOURCE_FILES += (COMMON_DEMO_FILES)/TimerDemo.c
# Application entry point. main_blinky is self contained. main_full builds
# the above common demo (and test) files too.
#
DEMO_PROJECT = $(DEMO_ROOT)/ROUND_ROBIN
DEMO_PROJECT = $(DEMO_ROOT)/ROUND_ROBIN_WEIGHT
VPATH += $(DEMO_PROJECT)
INCLUDE_DIRS += -I$(DEMO_PROJECT) -I$(DEMO_PROJECT)/CMSIS
SOURCE_FILES += (DEMO_PROJECT)/main.c
Expand Down
40 changes: 17 additions & 23 deletions FreeRTOS/Demo/ROUND_ROBIN_WEIGHT/main_blinky.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
/* Kernel includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "timers.h"
#include "queue.h"

/* Priorities at which the tasks are created. */
Expand All @@ -75,7 +74,7 @@
#define mainTIMER_SEND_FREQUENCY_MS pdMS_TO_TICKS(2000UL)

/* The number of items the queue can hold at once. */
#define mainQUEUE_LENGTH (4)
#define mainQUEUE_LENGTH (10)

/* The values sent to the queue receive task from the queue send task and the
* queue send software timer respectively. */
Expand All @@ -90,19 +89,11 @@
// static void prvQueueReceiveTask( void * pvParameters );
// static void prvQueueSendTask( void * pvParameters );

/*
* The callback function executed when the software timer expires.
*/
// static void prvQueueSendTimerCallback( TimerHandle_t xTimerHandle );

/*-----------------------------------------------------------*/

/* The queue used by both tasks. */
static QueueHandle_t xQueue = NULL;

/* A software timer that is started from the tick hook. */
static TimerHandle_t xTimer = NULL;

/* Test functions */
unsigned long ulTaskNumber[configEXPECTED_NO_RUNNING_TASKS];

Expand All @@ -114,11 +105,10 @@ static void T2(void *pvParameters);
static void T3(void *pvParameters);
static void T4(void *pvParameters);

const unsigned long wT1 = 100;
const unsigned long wT2 = 70;
const unsigned long wT3 = 50;
const unsigned long wT4 = 400;

const unsigned int wT1 = 4;
const unsigned int wT2 = 3;
const unsigned int wT3 = 2;
const unsigned int wT4 = 1;

/*-----------------------------------------------------------*/

Expand All @@ -131,10 +121,10 @@ void main_blinky(void)
if (xQueue != NULL)
{
printf("Starting round-robin-weight Scheduler\n");
xTaskCreate(T1, (signed char *)"T1", 1000, NULL, 1, NULL);
xTaskCreate(T2, (signed char *)"T2", 1000, NULL, 1, NULL);
xTaskCreate(T3, (signed char *)"T3", 1000, NULL, 1, NULL);
xTaskCreate(T4, (signed char *)"T4", 1000, NULL, 1, NULL);
xTaskCreate(T1, (signed char *)"T1", 1000, &wT1, 1, &xT1);
xTaskCreate(T2, (signed char *)"T2", 1000, &wT2, 1, &xT2);
xTaskCreate(T3, (signed char *)"T3", 1000, &wT3, 1, &xT3);
xTaskCreate(T4, (signed char *)"T4", 1000, &wT4, 1, &xT4);
/* Start the tasks running. */
vTaskStartScheduler();
}
Expand All @@ -157,7 +147,8 @@ static void T1(void *pvParameters)
// i = 0xFFFFFFFE + 0xA;
// printf("%x\n", i);
printf("T1 Executing\n");
vTaskDelay(pdMS_TO_TICKS(100));
for (int i = 0; i < 1000000; i++);
// vTaskDelay(pdMS_TO_TICKS(100));
}
}

Expand All @@ -166,7 +157,8 @@ static void T2(void *pvParameters)
while (1)
{
printf("T2 executing\n");
vTaskDelay(pdMS_TO_TICKS(100));
for (int i = 0; i < 1000000; i++);
// vTaskDelay(pdMS_TO_TICKS(100));
}
}

Expand All @@ -175,7 +167,8 @@ static void T3(void *pvParameters)
while (1)
{
printf("T3 Executing\n");
vTaskDelay(pdMS_TO_TICKS(100));
for (int i = 0; i < 1000000; i++);
// vTaskDelay(pdMS_TO_TICKS(100));
}
}

Expand All @@ -184,6 +177,7 @@ static void T4(void *pvParameters)
while (1)
{
printf("T4 executing\n");
vTaskDelay(pdMS_TO_TICKS(100));
for (int i = 0; i < 1000000; i++);
// vTaskDelay(pdMS_TO_TICKS(100));
}
}
2 changes: 1 addition & 1 deletion FreeRTOS/Source/include/FreeRTOS.h
Original file line number Diff line number Diff line change
Expand Up @@ -3159,7 +3159,7 @@ typedef struct xSTATIC_TCB
UBaseType_t uxDummy7;
UBaseType_t uxDummy8;
#endif
#if ( configUSE_EDF_SCHEDULER == 1 )
#if ( configUSE_WEIGHTED_ROUND_ROBIN == 1 )
UBaseType_t uxDummy30;
UBaseType_t uxDummy31;
#endif
Expand Down
2 changes: 1 addition & 1 deletion FreeRTOS/Source/include/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ typedef struct xLIST
* \ingroup LinkedList
*/
#if ( configNUMBER_OF_CORES == 1 )
#if (configUSE_EDF_SCHEDULER == 1)
#if (configUSE_EDF_SCHEDULER == 0)
#define listGET_OWNER_OF_NEXT_ENTRY( pxTCB, pxList ) \
do { \
List_t * const pxConstList = ( pxList ); \
Expand Down
66 changes: 66 additions & 0 deletions FreeRTOS/Source/tasks.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
/* Standard includes. */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

/* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
* all the API functions to use the MPU wrappers. That should only be done when
Expand Down Expand Up @@ -2036,6 +2037,13 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
pxNewTCB->uxDeadLine = *(TickType_t *)pvParameters;
}
#endif

#if (configUSE_WEIGHTED_ROUND_ROBIN == 1)
{
pxNewTCB->uxWeight = *(BaseType_t *)pvParameters;
pxNewTCB->uxRemainingTicks = pxNewTCB->uxWeight * configSLICE_INTERVAL;
}
#endif
if( pxCreatedTask != NULL )
{
/* Pass the handle out in an anonymous way. The handle can be used to
Expand All @@ -2057,12 +2065,15 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
* updated. */
taskENTER_CRITICAL();
{
printf("reach prvAddNewTaskToReadyList\n");
uxCurrentNumberOfTasks = ( UBaseType_t ) ( uxCurrentNumberOfTasks + 1U );

if( pxCurrentTCB == NULL )
{
/* There are no other tasks, or all the other tasks are in
* the suspended state - make this the current task. */
printf("change current tcb from NULL to %s\n", pxNewTCB->pcTaskName);

pxCurrentTCB = pxNewTCB;

if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
Expand All @@ -2086,6 +2097,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
{
if( pxCurrentTCB->uxPriority <= pxNewTCB->uxPriority )
{
printf("change current tcb from %s to %s\n", pxCurrentTCB->pcTaskName, pxNewTCB->pcTaskName);
pxCurrentTCB = pxNewTCB;
}
else
Expand All @@ -2109,6 +2121,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
#endif /* configUSE_TRACE_FACILITY */
traceTASK_CREATE( pxNewTCB );

printf("add task %s to ready list\n", pxNewTCB->pcTaskName);
prvAddTaskToReadyList( pxNewTCB );

portSETUP_TCB( pxNewTCB );
Expand Down Expand Up @@ -4716,6 +4729,29 @@ BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp )
#endif /* INCLUDE_xTaskAbortDelay */
/*----------------------------------------------------------*/

void printAllTasks()
{
UBaseType_t uxArraySize = uxTaskGetNumberOfTasks();
TaskStatus_t *pxTaskStatusArray = pvPortMalloc( uxArraySize * sizeof( TaskStatus_t ) );

if (pxTaskStatusArray != NULL)
{
uxArraySize = uxTaskGetSystemState(pxTaskStatusArray, uxArraySize, NULL);

printf("Current TCB Info:\n");
for (UBaseType_t i = 0; i < uxArraySize; i++)
{
printf("Task: %s, Priority: %d, State: %d\n",
pxTaskStatusArray[i].pcTaskName,
pxTaskStatusArray[i].uxCurrentPriority,
pxTaskStatusArray[i].eCurrentState);
}

vPortFree(pxTaskStatusArray);
}
}


BaseType_t xTaskIncrementTick( void )
{
TCB_t * pxTCB;
Expand Down Expand Up @@ -4856,6 +4892,35 @@ BaseType_t xTaskIncrementTick( void )
{
#if ( configNUMBER_OF_CORES == 1 )
{
#if ( configUSE_WEIGHTED_ROUND_ROBIN == 1 )
{
if( pxCurrentTCB->uxPriority != tskIDLE_PRIORITY )
{
if( pxCurrentTCB->uxWeight > ( BaseType_t ) 0U )
{
pxCurrentTCB->uxRemainingTicks -= 1U;

if( pxCurrentTCB->uxRemainingTicks == ( TickType_t ) 0U )
{
xSwitchRequired = pdTRUE;
pxCurrentTCB->uxRemainingTicks = pxCurrentTCB->uxWeight * configSLICE_INTERVAL;
}
else
{
mtCOVERAGE_TEST_MARKER();
}
}
else
{
mtCOVERAGE_TEST_MARKER();
}
}
else
{
mtCOVERAGE_TEST_MARKER();
}
}
#else
if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCB->uxPriority ] ) ) > 1U )
{
xSwitchRequired = pdTRUE;
Expand All @@ -4864,6 +4929,7 @@ BaseType_t xTaskIncrementTick( void )
{
mtCOVERAGE_TEST_MARKER();
}
#endif
}
#else /* #if ( configNUMBER_OF_CORES == 1 ) */
{
Expand Down

0 comments on commit 5e971c3

Please sign in to comment.