Skip to content

Commit

Permalink
px4iofirmware: add PX4IO_PERF define to completely disable perf counters
Browse files Browse the repository at this point in the history
  • Loading branch information
dagar authored and julianoes committed Feb 10, 2020
1 parent 2015cc2 commit 8cdbc4c
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 30 deletions.
6 changes: 5 additions & 1 deletion platforms/nuttx/src/px4/nxp/imxrt/hrt/hrt.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@

#include <px4_platform_common/px4_config.h>
#include <systemlib/px4_macros.h>
#include <lib/perf/perf_counter.h>
#include <nuttx/arch.h>
#include <nuttx/irq.h>

Expand Down Expand Up @@ -181,6 +180,11 @@ static uint32_t latency_baseline;
/* timer count at interrupt (for latency purposes) */
static uint32_t latency_actual;

/* latency histogram */
const uint16_t latency_bucket_count = LATENCY_BUCKET_COUNT;
const uint16_t latency_buckets[LATENCY_BUCKET_COUNT] = { 1, 2, 5, 10, 20, 50, 100, 1000 };
__EXPORT uint32_t latency_counters[LATENCY_BUCKET_COUNT + 1];

/* timer-specific functions */
static void hrt_tim_init(void);
static int hrt_tim_isr(int irq, void *context, void *args);
Expand Down
6 changes: 5 additions & 1 deletion platforms/nuttx/src/px4/nxp/kinetis/hrt/hrt.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@

#include <px4_platform_common/px4_config.h>
#include <systemlib/px4_macros.h>
#include <lib/perf/perf_counter.h>
#include <nuttx/arch.h>
#include <nuttx/irq.h>

Expand Down Expand Up @@ -179,6 +178,11 @@ static uint16_t latency_baseline;
/* timer count at interrupt (for latency purposes) */
static uint16_t latency_actual;

/* latency histogram */
const uint16_t latency_bucket_count = LATENCY_BUCKET_COUNT;
const uint16_t latency_buckets[LATENCY_BUCKET_COUNT] = { 1, 2, 5, 10, 20, 50, 100, 1000 };
__EXPORT uint32_t latency_counters[LATENCY_BUCKET_COUNT + 1];

/* timer-specific functions */
static void hrt_tim_init(void);
static int hrt_tim_isr(int irq, void *context, void *args);
Expand Down
6 changes: 5 additions & 1 deletion platforms/nuttx/src/px4/stm/stm32_common/hrt/hrt.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@

#include <board_config.h>
#include <drivers/drv_hrt.h>
#include <lib/perf/perf_counter.h>


#include "stm32_gpio.h"
Expand Down Expand Up @@ -256,6 +255,11 @@ static uint16_t latency_baseline;
/* timer count at interrupt (for latency purposes) */
static uint16_t latency_actual;

/* latency histogram */
const uint16_t latency_bucket_count = LATENCY_BUCKET_COUNT;
const uint16_t latency_buckets[LATENCY_BUCKET_COUNT] = { 1, 2, 5, 10, 20, 50, 100, 1000 };
__EXPORT uint32_t latency_counters[LATENCY_BUCKET_COUNT + 1];

/* timer-specific functions */
static void hrt_tim_init(void);
static int hrt_tim_isr(int irq, void *context, void *arg);
Expand Down
6 changes: 5 additions & 1 deletion platforms/posix/src/px4/common/drv_hrt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
#include <px4_platform_common/workqueue.h>
#include <px4_platform_common/tasks.h>
#include <drivers/drv_hrt.h>
#include <lib/perf/perf_counter.h>

#include <semaphore.h>
#include <time.h>
Expand All @@ -70,6 +69,11 @@ static uint64_t latency_baseline;
/* timer count at interrupt (for latency purposes) */
static uint64_t latency_actual;

/* latency histogram */
const uint16_t latency_bucket_count = LATENCY_BUCKET_COUNT;
const uint16_t latency_buckets[LATENCY_BUCKET_COUNT] = { 1, 2, 5, 10, 20, 50, 100, 1000 };
__EXPORT uint32_t latency_counters[LATENCY_BUCKET_COUNT + 1];

static px4_sem_t _hrt_lock;
static struct work_s _hrt_work;

Expand Down
6 changes: 5 additions & 1 deletion platforms/qurt/src/px4/common/drv_hrt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
#include <px4_platform_common/workqueue.h>
#include <px4_platform_common/tasks.h>
#include <drivers/drv_hrt.h>
#include <lib/perf/perf_counter.h>

#include <semaphore.h>
#include <time.h>
Expand All @@ -66,6 +65,11 @@ static uint64_t latency_baseline;
/* timer count at interrupt (for latency purposes) */
static uint64_t latency_actual;

/* latency histogram */
const uint16_t latency_bucket_count = LATENCY_BUCKET_COUNT;
const uint16_t latency_buckets[LATENCY_BUCKET_COUNT] = { 1, 2, 5, 10, 20, 50, 100, 1000 };
__EXPORT uint32_t latency_counters[LATENCY_BUCKET_COUNT + 1];

static px4_sem_t _hrt_lock;
static struct work_s _hrt_work;

Expand Down
6 changes: 6 additions & 0 deletions src/drivers/drv_hrt.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ typedef struct hrt_call {
void *arg;
} *hrt_call_t;


#define LATENCY_BUCKET_COUNT 8
extern const uint16_t latency_bucket_count;
extern const uint16_t latency_buckets[LATENCY_BUCKET_COUNT];
extern uint32_t latency_counters[LATENCY_BUCKET_COUNT + 1];

/**
* Get absolute time in [us] (does not wrap).
*/
Expand Down
6 changes: 0 additions & 6 deletions src/lib/perf/perf_counter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@

#include "perf_counter.h"

/* latency histogram */
const uint16_t latency_bucket_count = LATENCY_BUCKET_COUNT;
const uint16_t latency_buckets[LATENCY_BUCKET_COUNT] = { 1, 2, 5, 10, 20, 50, 100, 1000 };
__EXPORT uint32_t latency_counters[LATENCY_BUCKET_COUNT + 1];


#ifdef __PX4_QURT
// There is presumably no dprintf on QURT. Therefore use the usual output to mini-dm.
#define dprintf(_fd, _text, ...) ((_fd) == 1 ? PX4_INFO((_text), ##__VA_ARGS__) : (void)(_fd))
Expand Down
6 changes: 0 additions & 6 deletions src/lib/perf/perf_counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@
#include <stdint.h>
#include <px4_platform_common/defines.h>

#define LATENCY_BUCKET_COUNT 8

extern const uint16_t latency_bucket_count;
extern const uint16_t latency_buckets[LATENCY_BUCKET_COUNT];
extern uint32_t latency_counters[LATENCY_BUCKET_COUNT + 1];

/**
* Counter types.
*/
Expand Down
10 changes: 8 additions & 2 deletions src/modules/px4iofirmware/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#
############################################################################

option(PX4IO_PERF "Enable px4io perf counters" OFF)

add_library(px4iofirmware
adc.c
controls.c
Expand All @@ -39,7 +41,7 @@ add_library(px4iofirmware
registers.c
safety.c
serial.c
)
)

set_property(GLOBAL APPEND PROPERTY PX4_MODULE_LIBRARIES px4iofirmware)
target_link_libraries(px4iofirmware
Expand All @@ -50,6 +52,10 @@ target_link_libraries(px4iofirmware
nuttx_c
mixer
rc
perf
output_limit
)

if(PX4IO_PERF)
target_compile_definitions(px4iofirmware PRIVATE PX4IO_PERF)
target_link_libraries(px4iofirmware PRIVATE perf)
endif()
16 changes: 14 additions & 2 deletions src/modules/px4iofirmware/adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@
#include <stm32.h>

#include <drivers/drv_hrt.h>
#include <perf/perf_counter.h>

#if defined(PX4IO_PERF)
# include <perf/perf_counter.h>
#endif

#define DEBUG
#include "px4io.h"
Expand Down Expand Up @@ -76,12 +79,16 @@
#define rJDR4 REG(STM32_ADC_JDR4_OFFSET)
#define rDR REG(STM32_ADC_DR_OFFSET)

#if defined(PX4IO_PERF)
perf_counter_t adc_perf;
#endif

int
adc_init(void)
{
#if defined(PX4IO_PERF)
adc_perf = perf_alloc(PC_ELAPSED, "adc");
#endif

/* put the ADC into power-down mode */
rCR2 &= ~ADC_CR2_ADON;
Expand Down Expand Up @@ -136,8 +143,9 @@ adc_init(void)
uint16_t
adc_measure(unsigned channel)
{

#if defined(PX4IO_PERF)
perf_begin(adc_perf);
#endif

/* clear any previous EOC */
rSR = 0;
Expand All @@ -154,7 +162,9 @@ adc_measure(unsigned channel)

/* never spin forever - this will give a bogus result though */
if (hrt_elapsed_time(&now) > 100) {
#if defined(PX4IO_PERF)
perf_end(adc_perf);
#endif
return 0xffff;
}
}
Expand All @@ -163,6 +173,8 @@ adc_measure(unsigned channel)
uint16_t result = rDR;
rSR = 0;

#if defined(PX4IO_PERF)
perf_end(adc_perf);
#endif
return result;
}
25 changes: 24 additions & 1 deletion src/modules/px4iofirmware/controls.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,16 @@

#include <drivers/drv_hrt.h>
#include <drivers/drv_rc_input.h>
#include <perf/perf_counter.h>
#include <systemlib/ppm_decode.h>
#include <rc/st24.h>
#include <rc/sumd.h>
#include <rc/sbus.h>
#include <rc/dsm.h>

#if defined(PX4IO_PERF)
# include <perf/perf_counter.h>
#endif

#include "px4io.h"

#define RC_CHANNEL_HIGH_THRESH 5000 /* 75% threshold */
Expand All @@ -59,9 +62,11 @@
static bool ppm_input(uint16_t *values, uint16_t *num_values, uint16_t *frame_len);
static bool dsm_port_input(uint16_t *rssi, bool *dsm_updated, bool *st24_updated, bool *sumd_updated);

#if defined(PX4IO_PERF)
static perf_counter_t c_gather_dsm;
static perf_counter_t c_gather_sbus;
static perf_counter_t c_gather_ppm;
#endif

static int _dsm_fd = -1;
int _sbus_fd = -1;
Expand All @@ -78,7 +83,9 @@ static uint16_t _rssi = 0;

bool dsm_port_input(uint16_t *rssi, bool *dsm_updated, bool *st24_updated, bool *sumd_updated)
{
#if defined(PX4IO_PERF)
perf_begin(c_gather_dsm);
#endif
uint8_t n_bytes = 0;
uint8_t *bytes;
bool dsm_11_bit;
Expand Down Expand Up @@ -107,7 +114,9 @@ bool dsm_port_input(uint16_t *rssi, bool *dsm_updated, bool *st24_updated, bool
}
}

#if defined(PX4IO_PERF)
perf_end(c_gather_dsm);
#endif

/* get data from FD and attempt to parse with DSM and ST24 libs */
uint8_t st24_rssi, lost_count;
Expand Down Expand Up @@ -196,9 +205,11 @@ controls_init(void)
r_page_rc_input_config[base + PX4IO_P_RC_CONFIG_OPTIONS] = PX4IO_P_RC_CONFIG_OPTIONS_ENABLED;
}

#if defined(PX4IO_PERF)
c_gather_dsm = perf_alloc(PC_ELAPSED, "c_gather_dsm");
c_gather_sbus = perf_alloc(PC_ELAPSED, "c_gather_sbus");
c_gather_ppm = perf_alloc(PC_ELAPSED, "c_gather_ppm");
#endif
}

void
Expand Down Expand Up @@ -239,7 +250,9 @@ controls_tick()
_rssi = 0;
}

#if defined(PX4IO_PERF)
perf_begin(c_gather_sbus);
#endif

bool sbus_failsafe, sbus_frame_drop;
bool sbus_updated = sbus_input(_sbus_fd, r_raw_rc_values, &r_raw_rc_count, &sbus_failsafe, &sbus_frame_drop,
Expand Down Expand Up @@ -272,14 +285,18 @@ controls_tick()

}

#if defined(PX4IO_PERF)
perf_end(c_gather_sbus);
#endif

/*
* XXX each S.bus frame will cause a PPM decoder interrupt
* storm (lots of edges). It might be sensible to actually
* disable the PPM decoder completely if we have S.bus signal.
*/
#if defined(PX4IO_PERF)
perf_begin(c_gather_ppm);
#endif
bool ppm_updated = ppm_input(r_raw_rc_values, &r_raw_rc_count, &r_page_raw_rc_input[PX4IO_P_RAW_RC_DATA]);

if (ppm_updated) {
Expand All @@ -289,12 +306,16 @@ controls_tick()
r_raw_rc_flags &= ~(PX4IO_P_RAW_RC_FLAGS_FAILSAFE);
}

#if defined(PX4IO_PERF)
perf_end(c_gather_ppm);
#endif

bool dsm_updated = false, st24_updated = false, sumd_updated = false;

if (!((r_status_flags & PX4IO_P_STATUS_FLAGS_RC_SBUS) || (r_status_flags & PX4IO_P_STATUS_FLAGS_RC_PPM))) {
#if defined(PX4IO_PERF)
perf_begin(c_gather_dsm);
#endif

(void)dsm_port_input(&_rssi, &dsm_updated, &st24_updated, &sumd_updated);

Expand All @@ -310,7 +331,9 @@ controls_tick()
atomic_modify_or(&r_status_flags, PX4IO_P_STATUS_FLAGS_RC_SUMD);
}

#if defined(PX4IO_PERF)
perf_end(c_gather_dsm);
#endif
}

/* limit number of channels to allowable data size */
Expand Down
Loading

0 comments on commit 8cdbc4c

Please sign in to comment.