summaryrefslogtreecommitdiff
path: root/cpukit/include/rtems/record.h
blob: 8535ce4d06bc022ea4ce5d9c1ce4e0ca5fc20e45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
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 */