diff options
Diffstat (limited to 'cpukit/include/rtems/record.h')
-rw-r--r-- | cpukit/include/rtems/record.h | 251 |
1 files changed, 251 insertions, 0 deletions
diff --git a/cpukit/include/rtems/record.h b/cpukit/include/rtems/record.h new file mode 100644 index 0000000000..8535ce4d06 --- /dev/null +++ b/cpukit/include/rtems/record.h @@ -0,0 +1,251 @@ +/* + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (C) 2018 embedded brains GmbH + * + * 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. + * + * 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. + */ + +#ifndef _RTEMS_RECORD_H +#define _RTEMS_RECORD_H + +#include <rtems/recorddata.h> + +#include <rtems/score/atomic.h> +#include <rtems/score/percpudata.h> +#include <rtems/score/watchdog.h> +#include <rtems/rtems/intr.h> +#include <rtems/rtems/tasks.h> +#include <rtems/counter.h> + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +struct Record_Control { + Atomic_Uint head; + Atomic_Uint tail; + unsigned int mask; + Watchdog_Control Watchdog; + rtems_record_item Items[ RTEMS_ZERO_LENGTH_ARRAY ]; +}; + +typedef struct Record_Control Record_Control; + +typedef struct Record_Configured_control Record_Configured_control; + +typedef struct { + Record_Control *control; + unsigned int head; + unsigned int increment; + unsigned int final_head; + uint32_t now; + rtems_interrupt_level level; +} rtems_record_context; + +PER_CPU_DATA_ITEM_DECLARE( Record_Configured_control, _Record_Per_CPU ); + +extern const unsigned int _Record_Item_count; + +void _Record_Initialize( void ); + +bool _Record_Thread_create( + struct _Thread_Control *executing, + struct _Thread_Control *created +); + +void _Record_Thread_delete( + struct _Thread_Control *executing, + struct _Thread_Control *deleted +); + +void _Record_Thread_switch( + struct _Thread_Control *executing, + struct _Thread_Control *heir +); + +void _Record_Thread_terminate( + struct _Thread_Control *executing +); + +#define RECORD_EXTENSION \ + { \ + _Record_Thread_create, \ + NULL, NULL, \ + _Record_Thread_delete, \ + _Record_Thread_switch, \ + NULL, NULL, NULL, \ + _Record_Thread_terminate \ + } + +static inline unsigned int _Record_Capacity( + const Record_Control *control, + unsigned int head, + unsigned int tail +) +{ + return ( tail - head - 1U ) & control->mask; +} + +static inline unsigned int _Record_Increment( + const Record_Control *control, + unsigned int index, + unsigned int increment +) +{ + return ( index + increment ) & control->mask; +} + +static inline unsigned int _Record_Head( const Record_Control *control ) +{ + return _Atomic_Load_uint( &control->head, ATOMIC_ORDER_RELAXED ); +} + +static inline unsigned int _Record_Tail( const Record_Control *control ) +{ + return _Atomic_Load_uint( &control->tail, ATOMIC_ORDER_RELAXED ); +} + +static inline rtems_counter_ticks _Record_Now( void ) +{ + return rtems_counter_read(); +} + +static inline void rtems_record_prepare_inline( + rtems_record_context *context, + unsigned int count +) +{ + rtems_interrupt_level level; + const Per_CPU_Control *cpu_self; + Record_Control *control; + unsigned int head; + unsigned int tail; + unsigned int capacity; + + rtems_interrupt_local_disable( level ); + context->now = RTEMS_RECORD_TIME_EVENT( _Record_Now(), 0 ); + context->level = level; + cpu_self = _Per_CPU_Get(); + control = cpu_self->record; + context->control = control; + head = _Record_Head( control ); + tail = _Record_Tail( control ); + capacity = _Record_Capacity( control, head, tail ); + context->head = head; + + if ( RTEMS_PREDICT_TRUE( capacity > count ) ) { + context->increment = 1; + } else { + context->increment = 0; + count = capacity > 0 ? 1U : 0U; + } + + context->final_head = _Record_Increment( control, head, count ); +} + +static inline void rtems_record_add_inline( + rtems_record_context *context, + rtems_record_event event, + rtems_record_data data +) +{ + Record_Control *control; + rtems_record_item *item; + unsigned int head; + + control = context->control; + head = context->head; + item = &control->Items[ head ]; + context->head = _Record_Increment( control, head, context->increment ); + + item->event = RTEMS_RECORD_TIME_EVENT( + context->now, + event & -( (uint32_t) context->increment ) + ); + item->data = data; +} + +static inline void rtems_record_commit_inline( + rtems_record_context *context, + rtems_record_event event, + rtems_record_data data +) +{ + rtems_record_add_inline( context, event, data ); + _Atomic_Store_uint( + &context->control->head, + context->final_head, + ATOMIC_ORDER_RELEASE + ); + rtems_interrupt_local_enable( context->level ); +} + +static inline bool rtems_record_is_overflow( + const rtems_record_context *context +) +{ + return context->increment == 0; +} + +rtems_record_context *rtems_record_prepare( + rtems_record_context *context, + unsigned int count +); + +rtems_record_context *rtems_record_add( + rtems_record_context *context, + rtems_record_event event, + rtems_record_data data +); + +void rtems_record_commit( + rtems_record_context *context, + rtems_record_event event, + rtems_record_data data +); + +void rtems_record_produce( rtems_record_event event, rtems_record_data data ); + +typedef void ( *rtems_record_visitor )( + const rtems_record_item *items, + size_t count, + void *arg +); + +void rtems_record_drain( rtems_record_visitor visitor, void *arg ); + +ssize_t rtems_record_writev( int fd, bool *written ); + +void rtems_record_server( uint16_t port, rtems_interval period ); + +rtems_status_code rtems_record_start_server( + rtems_task_priority priority, + uint16_t port, + rtems_interval period +); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* _RTEMS_RECORD_H */ |