Skip to content

Commit 5fddd22

Browse files
committed
run period better support & system running states api
1 parent e179791 commit 5fddd22

File tree

10 files changed

+167
-12
lines changed

10 files changed

+167
-12
lines changed

flight/modules/ManualControl/armhandler.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ void armHandler(bool newinit, FrameType_t frameType)
130130

131131
if (settings.Arming == FLIGHTMODESETTINGS_ARMING_ALWAYSDISARMED) {
132132
// In this configuration we always disarm
133-
setArmedIfChanged(FLIGHTSTATUS_ARMED_DISARMED);
133+
setArmedIfChanged(FLIGHTSTATUS_ARMED_DISARMED); // update flight status msg
134134
return;
135135
}
136136

flight/modules/Sensors/sensors.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ PERF_DEFINE_COUNTER(counterMagPeriod);
132132
PERF_DEFINE_COUNTER(counterBaroPeriod);
133133
PERF_DEFINE_COUNTER(counterSensorPeriod);
134134
PERF_DEFINE_COUNTER(counterSensorResets);
135+
PERF_DEFINE_COUNTER(counterSensorBetween);
135136

136137
#if defined(PIOS_INCLUDE_HMC5X83)
137138
void aux_hmc5x83_load_settings();
@@ -270,6 +271,7 @@ static void SensorsTask(__attribute__((unused)) void *parameters)
270271
PERF_INIT_COUNTER(counterBaroPeriod, 0x53000004);
271272
PERF_INIT_COUNTER(counterSensorPeriod, 0x53000005);
272273
PERF_INIT_COUNTER(counterSensorResets, 0x53000006);
274+
PERF_INIT_COUNTER(counterSensorBetween, 0x53000007);
273275

274276
// Test sensors
275277
bool sensors_test = true;
@@ -295,6 +297,7 @@ static void SensorsTask(__attribute__((unused)) void *parameters)
295297

296298
while (1) {
297299
// TODO: add timeouts to the sensor reads and set an error if the fail
300+
PERF_MEASURE_BETWEEN(counterSensorBetween, true);
298301
if (error) {
299302
RELOAD_WDG();
300303
lastSysTime = xTaskGetTickCount();
@@ -343,6 +346,13 @@ static void SensorsTask(__attribute__((unused)) void *parameters)
343346
}
344347
PERF_MEASURE_PERIOD(counterSensorPeriod);
345348
RELOAD_WDG();
349+
350+
PERF_MEASURE_BETWEEN(counterSensorBetween, false);
351+
// char* buffer = ((char*)pios_malloc(500));
352+
353+
// PIOS_TASK_MONITOR_GetRunTimeStats(buffer);
354+
// free(buffer);
355+
// volatile int32_t debug_value = PIOS_Instrumentation_GetCounterValue(counterSensorBetween); // about 500us
346356
vTaskDelayUntil(&lastSysTime, sensor_period_ticks);
347357
}
348358
}
@@ -496,7 +506,6 @@ static void handleMag(float *samples, float temperature)
496506

497507
rot_mult(mag_transform, mags, samples);
498508

499-
/*
500509
// Debug
501510
static uint32_t debug_ticks_pre = 0;
502511
static uint32_t debug_ticks_now = 0;
@@ -506,7 +515,6 @@ static void handleMag(float *samples, float temperature)
506515
debug_ticks_now = 0;
507516
DEBUG_PRINTF(0, "handle mag: %f,%f,%f\r\n", mags[0],mags[1],mags[2]);
508517
}
509-
*/
510518

511519
mag.x = samples[0];
512520
mag.y = samples[1];

flight/modules/Sensors/simulated/Sensors/sensors.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ static float rand_gauss();
9696
enum sensor_sim_type { CONSTANT, MODEL_AGNOSTIC, MODEL_QUADCOPTER, MODEL_AIRPLANE } sensor_sim_type;
9797

9898
#define GRAV 9.81
99+
100+
#define PIOS_INSTRUMENT_MODULE
101+
#include <pios_instrumentation_helper.h>
102+
PERF_DEFINE_COUNTER(counterSimSensorPeriod);
103+
99104
/**
100105
* Initialise the module. Called before the start function
101106
* \returns 0 on success or -1 if initialisation failed
@@ -159,8 +164,10 @@ static void SensorsTask(__attribute__((unused)) void *parameters)
159164

160165
// Main task loop
161166
lastSysTime = xTaskGetTickCount();
162-
uint32_t last_time = PIOS_DELAY_GetRaw();
167+
// uint32_t last_time = PIOS_DELAY_GetRaw();
168+
PERF_INIT_COUNTER(counterSimSensorPeriod, 0x53000101);
163169
while (1) {
170+
PERF_MEASURE_BETWEEN(counterSimSensorPeriod, true);
164171
PIOS_WDG_UpdateFlag(PIOS_WDG_SENSORS);
165172

166173
SystemSettingsData systemSettings;
@@ -183,13 +190,6 @@ static void SensorsTask(__attribute__((unused)) void *parameters)
183190
sensor_sim_type = MODEL_AGNOSTIC;
184191
}
185192

186-
static int i;
187-
i++;
188-
if (i % 5000 == 0) {
189-
// float dT = PIOS_DELAY_DiffuS(last_time) / 10.0e6;
190-
// fprintf(stderr, "Sensor relative timing: %f\n", dT);
191-
last_time = PIOS_DELAY_GetRaw();
192-
}
193193

194194
sensors_count++;
195195

@@ -207,6 +207,18 @@ static void SensorsTask(__attribute__((unused)) void *parameters)
207207
simulateModelAirplane();
208208
}
209209

210+
PERF_MEASURE_BETWEEN(counterSimSensorPeriod, false); // id = 1392509185 cycle 130us
211+
static int i;
212+
i++;
213+
// if (i % 5000 == 0) {
214+
// float dT = PIOS_DELAY_DiffuS(last_time) / 10.0e6;
215+
// fprintf(stderr, "Sensor relative timing: %f\n", dT);
216+
//last_time = PIOS_DELAY_GetRaw();
217+
//}
218+
// if (i % 5000 == 0) {
219+
// pios_perf_counter_t *counter = (pios_perf_counter_t *)counterSimSensorPeriod;
220+
// fprintf(stderr, "simulation sensors perf counter: %d\n", counter->value);
221+
// }
210222
vTaskDelay(2 / portTICK_RATE_MS);
211223
}
212224
}

flight/modules/System/systemmod.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,18 @@ static void systemTask(__attribute__((unused)) void *parameters)
249249
#endif
250250
// Main system loop
251251
while (1) {
252+
// debug only
253+
//static uint16_t i = 0;
254+
//const size_t BUFFER_SIZE = 100;
255+
//if (i != 0 && i % 20 == 0) {
256+
// char* buffer = (char*)pios_malloc(BUFFER_SIZE);
257+
// PIOS_TASK_MONITOR_GetRunTimeStats(buffer);
258+
// fprintf(stderr, "%s\n", buffer);
259+
// i = 0;
260+
//} else {
261+
// i++;
262+
//}
263+
252264
NotificationUpdateStatus();
253265
// Update the system statistics
254266
updateStats();
@@ -751,6 +763,15 @@ static void updateSystemAlarms()
751763
*/
752764
void vApplicationIdleHook(void)
753765
{
766+
/*
767+
static uint16_t c = 0;
768+
if (c > 0 && c % 5000 == 0) {
769+
fprintf(stderr, "idle hook is running:%d\n", c);
770+
c = 0;
771+
} else {
772+
c++;
773+
}
774+
*/
754775
PIOS_TASK_MONITOR_IdleHook();
755776
NotificationOnboardLedsRun();
756777
#ifdef PIOS_INCLUDE_WS2811

flight/pios/common/pios_callbackscheduler.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ void PIOS_CALLBACKSCHEDULER_ForEachCallback(CallbackSchedulerCallbackInfoCallbac
375375

376376
for (prio = 0; prio < (CALLBACK_PRIORITY_LOW + 1); prio++) {
377377
struct DelayedCallbackInfoStruct *cbinfo;
378+
/* use volatile to avoid opt*/
378379
LL_FOREACH(task->callbackQueue[prio], cbinfo) {
379380
xSemaphoreTakeRecursive(mutex, portMAX_DELAY);
380381
info.is_running = true;

flight/pios/common/pios_task_monitor.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,69 @@ uint32_t PIOS_TASK_MONITOR_GetZeroLoadTicksCount()
193193
return zeroLoadIdleCounterPerSec;
194194
}
195195

196+
/*
197+
* print a human readable table of run time stats
198+
* information is generated from raw data provided by uxTaskGetSystemState().
199+
* The human readable table is written to pcWriteBuffer. (see the vTaskList()
200+
* API function which actually does just this).
201+
*/
202+
void PIOS_TASK_MONITOR_GetRunTimeStats(/*signed*/ char *pcWriteBuffer) {
203+
TaskStatus_t *pxTaskStatusArray;
204+
volatile UBaseType_t uxArraySize, x;
205+
unsigned long ulTotalRunTime, ulStatsAsPercentage;
206+
207+
/* Make sure the write buffer does not contain a string. */
208+
*pcWriteBuffer = 0x00;
209+
210+
/* Take a snapshot of the number of tasks in case it changes while this
211+
function is executing. */
212+
uxArraySize = uxTaskGetNumberOfTasks();
213+
214+
/* Allocate a TaskStatus_t structure for each task. An array could be
215+
allocated statically at compile time. */
216+
pxTaskStatusArray = pvPortMalloc( uxArraySize * sizeof( TaskStatus_t ) );
217+
218+
if( pxTaskStatusArray != NULL ) {
219+
/* Generate raw status information about each task. */
220+
uxArraySize = uxTaskGetSystemState( pxTaskStatusArray,
221+
uxArraySize,
222+
&ulTotalRunTime );
223+
224+
/* For percentage calculations. */
225+
ulTotalRunTime /= 100UL;
226+
227+
/* Avoid divide by zero errors. */
228+
if( ulTotalRunTime > 0 ) {
229+
/* For each populated position in the pxTaskStatusArray array,
230+
format the raw data as human readable ASCII data. */
231+
for( x = 0; x < uxArraySize; x++ ) {
232+
/* What percentage of the total run time has the task used?
233+
This will always be rounded down to the nearest integer.
234+
ulTotalRunTimeDiv100 has already been divided by 100. */
235+
ulStatsAsPercentage =
236+
pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalRunTime;
237+
238+
if( ulStatsAsPercentage > 0UL ) {
239+
sprintf( pcWriteBuffer, "%s:%d,pct%d \n",
240+
pxTaskStatusArray[ x ].pcTaskName,
241+
pxTaskStatusArray[ x ].ulRunTimeCounter,
242+
ulStatsAsPercentage );
243+
}
244+
else {
245+
/* If the percentage is zero here then the task has
246+
consumed less than 1% of the total run time. */
247+
sprintf( pcWriteBuffer, "%s:%d,<1% \n",
248+
pxTaskStatusArray[ x ].pcTaskName,
249+
pxTaskStatusArray[ x ].ulRunTimeCounter );
250+
}
251+
252+
pcWriteBuffer += strlen( ( char * ) pcWriteBuffer );
253+
}
254+
}
255+
256+
/* The array is no longer needed, free the memory it consumes. */
257+
vPortFree( pxTaskStatusArray );
258+
}
259+
}
260+
196261
#endif // PIOS_INCLUDE_TASK_MONITOR

flight/pios/inc/pios_instrumentation.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,22 @@ static inline void PIOS_Instrumentation_updateCounter(pios_counter_t counter_han
6565
vPortExitCritical();
6666
}
6767

68+
/**
69+
* Return a counter value
70+
* @param counter_handle handle of the counter to update @see PIOS_Instrumentation_SearchCounter @see PIOS_Instrumentation_CreateCounter
71+
* @param none
72+
*/
73+
static inline int32_t PIOS_Instrumentation_GetCounterValue(pios_counter_t counter_handle)
74+
{
75+
int32_t value = 0;
76+
PIOS_Assert(pios_instrumentation_perf_counters && counter_handle);
77+
vPortEnterCritical();
78+
pios_perf_counter_t *counter = (pios_perf_counter_t *)counter_handle;
79+
value = counter->value;
80+
vPortExitCritical();
81+
return value;
82+
}
83+
6884
/**
6985
* Used to determine the time duration of a code block, mark the begin of the block. @see PIOS_Instrumentation_TimeEnd
7086
* @param counter_handle handle of the counter @see PIOS_Instrumentation_SearchCounter @see PIOS_Instrumentation_CreateCounter
@@ -127,6 +143,34 @@ static inline void PIOS_Instrumentation_TrackPeriod(pios_counter_t counter_handl
127143
counter->lastUpdateTS = PIOS_DELAY_GetRaw();
128144
}
129145

146+
/**
147+
* Used to determine the mean period between start to the end
148+
* @param counter_handle handle of the counter @see PIOS_Instrumentation_SearchCounter @see PIOS_Instrumentation_CreateCounter
149+
*/
150+
static inline void PIOS_Instrumentation_TrackBetween(pios_counter_t counter_handle, bool start)
151+
{
152+
PIOS_Assert(pios_instrumentation_perf_counters && counter_handle);
153+
pios_perf_counter_t *counter = (pios_perf_counter_t *)counter_handle;
154+
if (start) {
155+
counter->lastUpdateTS = PIOS_DELAY_GetRaw();
156+
} else if (counter->lastUpdateTS != 0) {
157+
vPortEnterCritical();
158+
uint32_t period = PIOS_DELAY_DiffuS(counter->lastUpdateTS);
159+
counter->value = (counter->value * 15 + period) / 16;
160+
counter->max--;
161+
if ((int32_t)period > counter->max) {
162+
counter->max = period;
163+
}
164+
counter->min++;
165+
if ((int32_t)period < counter->min) {
166+
counter->min = period;
167+
}
168+
vPortExitCritical();
169+
} else {
170+
// Invalid measurement
171+
}
172+
}
173+
130174
/**
131175
* Increment a counter with a value
132176
* @param counter_handle handle of the counter to update @see PIOS_Instrumentation_SearchCounter @see PIOS_Instrumentation_CreateCounter

flight/pios/inc/pios_instrumentation_helper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
#define PERF_TIMED_SECTION_START(x) PIOS_Instrumentation_TimeStart(x)
105105
#define PERF_TIMED_SECTION_END(x) PIOS_Instrumentation_TimeEnd(x)
106106
#define PERF_MEASURE_PERIOD(x) PIOS_Instrumentation_TrackPeriod(x)
107+
#define PERF_MEASURE_BETWEEN(x, y) PIOS_Instrumentation_TrackBetween(x, y)
107108
#define PERF_TRACK_VALUE(x, y) PIOS_Instrumentation_updateCounter(x, y)
108109
#define PERF_INCREMENT_VALUE(x) PIOS_Instrumentation_incrementCounter(x, 1)
109110
#define PERF_DECREMENT_VALUE(x) PIOS_Instrumentation_incrementCounter(x, -1)
@@ -115,6 +116,7 @@
115116
#define PERF_TIMED_SECTION_START(x)
116117
#define PERF_TIMED_SECTION_END(x)
117118
#define PERF_MEASURE_PERIOD(x)
119+
#define PERF_MEASURE_BETWEEN(x, y) (void)y
118120
#define PERF_TRACK_VALUE(x, y) (void)y
119121
#define PERF_INCREMENT_VALUE(x)
120122
#define PERF_DECREMENT_VALUE(x)

flight/pios/inc/pios_task_monitor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,6 @@ extern uint32_t PIOS_TASK_MONITOR_GetIdleTicksCount();
134134

135135
extern uint32_t PIOS_TASK_MONITOR_GetZeroLoadTicksCount();
136136

137+
extern void PIOS_TASK_MONITOR_GetRunTimeStats(/*signed*/ char*);
138+
137139
#endif // PIOS_TASK_MONITOR_H

flight/targets/boards/revolution/firmware/inc/FreeRTOSConfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#define configUSE_PREEMPTION 1
3333
#define configUSE_IDLE_HOOK 1
3434
#define configUSE_TICK_HOOK 0
35-
#define configUSE_TRACE_FACILITY 0
35+
#define configUSE_TRACE_FACILITY 1
3636
#define configUSE_16_BIT_TICKS 0
3737
#define configIDLE_SHOULD_YIELD 0
3838
#define configUSE_MUTEXES 1

0 commit comments

Comments
 (0)