Skip to content

Commit

Permalink
chore: consolidated schedulers
Browse files Browse the repository at this point in the history
  • Loading branch information
charliekush committed Aug 8, 2024
1 parent 5f2f6a6 commit 310d33c
Show file tree
Hide file tree
Showing 8 changed files with 287 additions and 22 deletions.
7 changes: 0 additions & 7 deletions src/a_test.cpp

This file was deleted.

4 changes: 2 additions & 2 deletions src/abstractScheduler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
#include <cstdint>
#include "deploymentSchedule.hpp"



class AbstractScheduler {
public:
//DeploymentSchedule_t* scheduleTable;
//AbstractScheduler(DeploymentSchedule_* schedule):scheduleTable(schedule){};
/**
* Creates and initializes the schedule
*/
Expand Down
2 changes: 1 addition & 1 deletion src/charlie_scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void Scheduler::initializeScheduler()
pDeployment->state.firstRunTime = UINT32_MAX;
pDeployment->state.nMeasurements = UINT32_MAX;
pDeployment->state.lastMeasurementTime = UINT32_MAX;
pDeployment->init (pDeployment);
pDeployment->init(pDeployment);
pDeployment++;
}
}
Expand Down
11 changes: 2 additions & 9 deletions src/charlie_scheduler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,8 @@ struct DeploymentSchedule_
class Scheduler : public AbstractScheduler {
public:
DeploymentSchedule_t* scheduleTable;
void printTable()
{
DeploymentSchedule_t* pDeployment = scheduleTable;
uint32_t lastEndTime = 0;
while (pDeployment->init)
{
SF_OSAL_printf("Ensemble %s\n",pDeployment->taskName);
}
};




Scheduler(DeploymentSchedule_t* scheduler);
Expand Down
3 changes: 2 additions & 1 deletion src/product.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,13 @@

#define CHARLIE_VERSION 0
#define ANTARA_VERSION 1
#define CONSOLODATED_VERSION 2
#ifdef SCHEDULER_VERSION
#undef SCHEDULER_VERSION
#endif

#ifndef SCHEDULER_VERSION
#define SCHEDULER_VERSION CHARLIE_VERSION
#define SCHEDULER_VERSION CONSOLODATED_VERSION
#endif


Expand Down
134 changes: 134 additions & 0 deletions src/scheduler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/**
* @file scheduler.cpp
* @brief Contains definitions for functions defined in @ref scheduler.hpp
* @author Charlie Kushelevsky ([email protected])
* @version 1
*/

#ifndef TEST_VERSION
#include "Particle.h"
#else
#include "scheduler_test_system.hpp"
#endif
#include "cli/conio.hpp"
#include "ensembles.hpp"
#include "scheduler.hpp"
#include "consts.hpp"
#include <cli/flog.hpp>
#include <string.h>


#if SCHEDULER_VERSION == CONSOLODATED_VERSION
Scheduler::Scheduler(DeploymentSchedule_t schedule[])
: scheduleTable(schedule){}
/**
* @brief Initializes ensembles within schedule table.
*
*/
void Scheduler::initializeScheduler()
{
DeploymentSchedule_t* pDeployment = scheduleTable;
uint32_t lastEndTime = 0;
tableSize = 0;
while (pDeployment->init)
{
tableSize++;
memset(&(pDeployment->state), 0,
sizeof(StateInformation));
pDeployment->state.lastMeasurementTime = UINT32_MAX;
pDeployment->init(pDeployment);
pDeployment++;
}
}



/**
* @brief Retrieves the next schedu2led event
*
* This function iterates through a schedule table to find the event that
* should run next based on the current system time.
* It ensures that no overlapping events are scheduled by checking with
* @ref SCH_willOverlap().
*
* @see tests/gtest.cpp for intended behavior
*
* @param scheduleTable Pointer to the first element of an array of
* DeploymentSchedule_t, which contains the scheduling information.
* @param p_nextEvent Pointer to a pointer where the next event to be executed
* will be stored.
* @param p_nextTime Pointer to where the time for the next event will
* be stored.
*
* @return Returns SUCCESS if a suitable event is found,
* TASK_SEARCH_FAIL otherwise.
*/

int Scheduler::getNextTask(DeploymentSchedule_t** p_nextEvent,
std::uint32_t* p_nextTime,
std::uint32_t currentTime)
{
uint32_t minNextTime = UINT32_MAX;
DeploymentSchedule_t* nextEvent = nullptr;

// Iterate through each event in the schedule table.

for(int idx = tableSize - 1; idx>=0; idx--)
{
bool canSet = true;
DeploymentSchedule_t& currentEvent = scheduleTable[idx];
StateInformation& state = scheduleTable[idx].state;
std::uint32_t runTime = state.nextRunTime;
int delay = currentTime - runTime;
if (delay > 0)
{
runTime = currentTime;
}
else
{
delay = 0;
}
if (delay == currentEvent.maxDelay)
{
//send warning
}
int completion = runTime + currentEvent.maxDuration;
int j = 0;
while (j < idx && canSet)
{
if (scheduleTable[j].state.nextRunTime < completion)
{
canSet = false;
}
j++;
}
if (canSet)
{
*p_nextEvent = &currentEvent;
state.nextRunTime = runTime + currentEvent.ensembleInterval;

state.measurementCount++;
*p_nextTime = runTime;
if(delay>0){
FLOG_AddError(FLOG_SCHEDULER_DELAY_EXCEEDED,
state.measurementCount);
SF_OSAL_printf("Task %s shifted at time %zu" __NL__ ,
currentEvent.taskName,currentTime);
}
return SUCCESS;
}



}

return TASK_SEARCH_FAIL;

}






#endif
146 changes: 146 additions & 0 deletions src/scheduler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/**
* @file charlie_scheduler.hpp
* @author Charlie Kushelevsky ([email protected])
* @brief Header file for scheduler defined in @ref scheduler.cpp
* @version 1
*/
#ifndef __CONSOLODATED_SCHEDULER__HPP__
#define __CHARLIE_SCHEDULER__HPP__
#include "product.hpp"
#if SCHEDULER_VERSION == CONSOLODATED_VERSION

#include <stddef.h>
#include <cstdint>

#include "deploymentSchedule.hpp"
#include "abstractScheduler.hpp"
#include "cli/conio.hpp"
#ifndef TEST_VERSION
#include "Particle.h"
#else
#include "scheduler_test_system.hpp"
#endif


/**
* @brief Ensemble function.
*
* This function executes once to update the ensemble state. This should update
* the accumulators with a new measurement. If the accumulators have
* accumulated the proper amount of data, this function should then record the
* proper data.
*
* Essentially, this will be called every ensembleInterval ms after
* ensembleDelay ms from the start of the deployment.
*
* @param pDeployment the schedule table
*/
typedef void (*EnsembleFunction)(DeploymentSchedule_t* pDeployment);
/**
* @brief Ensemble initialization function.
*
* This function is executed once when all of the
* @param pDeployment the schedule table
*/
typedef void (*EnsembleInit)(DeploymentSchedule_t* pDeployment);
/**
* @brief contains stat information for each ensemble
*/

struct StateInformation
{
//! how many times ensemble was scheduled
uint32_t measurementCount;

//! store the next time the task should run
uint32_t nextRunTime;


};
/**
* @brief Records and writes ensemble
* @param pDeployment the schedule table
*/
typedef void (*EnsembleProccess)(DeploymentSchedule_t* pDeployment);
struct DeploymentSchedule_
{

EnsembleFunction measure; //!< measurement function
EnsembleInit init; //!< initialization function
//EnsembleProccess process; //!< processing function

uint32_t measurementsToAccumulate; //!< measurements before processing
uint32_t ensembleDelay; //!< delay after deployment start
uint32_t ensembleInterval; //!< time between ensembles
//! store max running time of measurement
uint32_t maxDuration;
//! maximum delay before throwing flag and resetting
uint32_t maxDelay;
//! task name of ensemble
const char* taskName;
//! state information
StateInformation state;


};

class Scheduler : public AbstractScheduler {
private:
uint32_t tableSize;
public:
DeploymentSchedule_t* scheduleTable;




Scheduler(DeploymentSchedule_t* scheduler);

/**
* @brief Initializes ensemble variables
*
* @param scheduleTable the schedule table
* @param startTime the start time of the deployment
*/
void initializeScheduler();

/**
* @brief Determines if a task will overlap with other scheduled tasks.
*
* This function checks if the proposed start and end times of a task
* overlap with any other tasks in the schedule table. It ensures that
* tasks are scheduled without conflicts.
*
* @param scheduleTable The table containing all scheduled tasks.
* @param idx The index of the current task in the schedule table.
* @param currentTime The current system time.
* @param nextStartTime The proposed start time of the current task.
* @return True if there is an overlap with another task; false otherwise.
*/
int getNextTask(DeploymentSchedule_t** p_next_task,
uint32_t* p_next_runtime,
uint32_t current_time);

bool willOverlap(uint32_t i, system_tick_t currentTime,
uint32_t nextStartTime);

};

typedef enum error_
{
SUCCESS,
TASK_SEARCH_FAIL,
}SCH_error_e;











#endif

#endif //__CHARLIE_SCHEDULER__HPP__
2 changes: 0 additions & 2 deletions tests/test_file_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ def generate_delays(self):
input.write()
print(f'{file_num}/{(250/step)**2}' + "\r")





'''
START #only one number
Expand Down

0 comments on commit 310d33c

Please sign in to comment.