source: rtems/cpukit/score/src/wkspace.c @ 07e2eac

5
Last change on this file since 07e2eac was 9e4f21b9, checked in by Sebastian Huber <sebastian.huber@…>, on 02/01/20 at 14:42:27

score: Optimize per-processor data placement

Only align per-processor data in SMP configurations.

  • Property mode set to 100644
File size: 6.4 KB
Line 
1/**
2 *  @file
3 *
4 *  @brief Workspace Handler Support
5 *  @ingroup RTEMSScoreWorkspace
6 */
7
8/*
9 *  COPYRIGHT (c) 1989-2009.
10 *  On-Line Applications Research Corporation (OAR).
11 *
12 *  The license and distribution terms for this file may be
13 *  found in the file LICENSE in this distribution or at
14 *  http://www.rtems.org/license/LICENSE.
15 */
16
17#if HAVE_CONFIG_H
18  #include "config.h"
19#endif
20
21#include <rtems/score/wkspace.h>
22#include <rtems/score/assert.h>
23#include <rtems/score/heapimpl.h>
24#include <rtems/score/interr.h>
25#include <rtems/score/percpudata.h>
26#include <rtems/score/threadimpl.h>
27#include <rtems/score/tls.h>
28#include <rtems/posix/pthread.h>
29#include <rtems/config.h>
30
31#include <string.h>
32
33/* #define DEBUG_WORKSPACE */
34#if defined(DEBUG_WORKSPACE)
35  #include <rtems/bspIo.h>
36#endif
37
38RTEMS_LINKER_RWSET(
39  _Per_CPU_Data,
40#if defined(RTEMS_SMP)
41  /*
42   * In SMP configurations, prevent false cache line sharing of per-processor
43   * data with a proper alignment.
44   */
45  RTEMS_ALIGNED( CPU_CACHE_LINE_BYTES )
46#endif
47  char
48);
49
50Heap_Control _Workspace_Area;
51
52static uintptr_t _Workspace_Space_for_TLS( uintptr_t page_size )
53{
54  uintptr_t tls_size;
55  uintptr_t space;
56
57  tls_size = _TLS_Get_size();
58
59  /*
60   * In case we have a non-zero TLS size, then we need a TLS area for each
61   * thread.  These areas are allocated from the workspace.  Ensure that the
62   * workspace is large enough to fulfill all requests known at configuration
63   * time (so excluding the unlimited option).  It is not possible to estimate
64   * the TLS size in the configuration at compile-time.  The TLS size is
65   * determined at application link-time.
66   */
67  if ( tls_size > 0 ) {
68    uintptr_t tls_align = _TLS_Heap_align_up( (uintptr_t) _TLS_Alignment );
69    uintptr_t tls_alloc = _TLS_Get_allocation_size( tls_size, tls_align );
70
71    /*
72     * Memory allocated with an alignment constraint is allocated from the end
73     * of a free block.  The last allocation may need one free block of minimum
74     * size.
75     */
76    space = _Heap_Min_block_size( page_size );
77
78    space += _Thread_Initial_thread_count
79      * _Heap_Size_with_overhead( page_size, tls_alloc, tls_align );
80  } else {
81    space = 0;
82  }
83
84  return space;
85}
86
87#ifdef RTEMS_SMP
88static void *_Workspace_Allocate_from_areas(
89  Heap_Area *areas,
90  size_t     area_count,
91  uintptr_t  size,
92  uintptr_t  alignment
93)
94{
95  size_t i;
96
97  for ( i = 0; i < area_count; ++i ) {
98    Heap_Area *area;
99    uintptr_t  alloc_begin;
100    uintptr_t  alloc_size;
101
102    area = &areas[ i ];
103    alloc_begin = (uintptr_t) area->begin;
104    alloc_begin = ( alloc_begin + alignment - 1 ) & ~( alignment - 1 );
105    alloc_size = size;
106    alloc_size += alloc_begin - (uintptr_t) area->begin;
107
108    if ( area->size >= alloc_size ) {
109      area->begin = (void *) ( alloc_begin + size );
110      area->size -= alloc_size;
111
112      return (void *) alloc_begin;
113    }
114  }
115
116  return NULL;
117}
118#endif
119
120static void _Workspace_Allocate_per_CPU_data(
121  Heap_Area *areas,
122  size_t area_count
123)
124{
125#ifdef RTEMS_SMP
126  uintptr_t size;
127
128  size = RTEMS_LINKER_SET_SIZE( _Per_CPU_Data );
129
130  if ( size > 0 ) {
131    Per_CPU_Control *cpu;
132    uint32_t         cpu_index;
133    uint32_t         cpu_max;
134
135    cpu = _Per_CPU_Get_by_index( 0 );
136    cpu->data = RTEMS_LINKER_SET_BEGIN( _Per_CPU_Data );
137
138    cpu_max = rtems_configuration_get_maximum_processors();
139
140    for ( cpu_index = 1 ; cpu_index < cpu_max ; ++cpu_index ) {
141      cpu = _Per_CPU_Get_by_index( cpu_index );
142      cpu->data = _Workspace_Allocate_from_areas(
143        areas,
144        area_count,
145        size,
146        CPU_CACHE_LINE_BYTES
147      );
148
149      if( cpu->data == NULL ) {
150        _Internal_error( INTERNAL_ERROR_NO_MEMORY_FOR_PER_CPU_DATA );
151      }
152
153      memcpy( cpu->data, RTEMS_LINKER_SET_BEGIN( _Per_CPU_Data ), size);
154    }
155  }
156#else
157  (void) areas;
158  (void) area_count;
159#endif
160}
161
162void _Workspace_Handler_initialization(
163  Heap_Area *areas,
164  size_t area_count,
165  Heap_Initialization_or_extend_handler extend
166)
167{
168  Heap_Initialization_or_extend_handler init_or_extend;
169  uintptr_t                             remaining;
170  bool                                  do_zero;
171  bool                                  unified;
172  uintptr_t                             page_size;
173  uintptr_t                             overhead;
174  size_t                                i;
175
176  _Workspace_Allocate_per_CPU_data( areas, area_count );
177
178  page_size = CPU_HEAP_ALIGNMENT;
179
180  remaining = rtems_configuration_get_work_space_size();
181  remaining += _Workspace_Space_for_TLS( page_size );
182
183  init_or_extend = _Heap_Initialize;
184  do_zero = rtems_configuration_get_do_zero_of_workspace();
185  unified = rtems_configuration_get_unified_work_area();
186  overhead = _Heap_Area_overhead( page_size );
187
188  for ( i = 0; i < area_count; ++i ) {
189    Heap_Area *area;
190
191    area = &areas[ i ];
192
193    if ( do_zero ) {
194      memset( area->begin, 0, area->size );
195    }
196
197    if ( area->size > overhead ) {
198      uintptr_t space_available;
199      uintptr_t size;
200
201      if ( unified ) {
202        size = area->size;
203      } else {
204        if ( remaining > 0 ) {
205          size = remaining < area->size - overhead ?
206            remaining + overhead : area->size;
207        } else {
208          size = 0;
209        }
210      }
211
212      space_available = ( *init_or_extend )(
213        &_Workspace_Area,
214        area->begin,
215        size,
216        page_size
217      );
218
219      area->begin = (char *) area->begin + size;
220      area->size -= size;
221
222      if ( space_available < remaining ) {
223        remaining -= space_available;
224      } else {
225        remaining = 0;
226      }
227
228      init_or_extend = extend;
229    }
230  }
231
232  if ( remaining > 0 ) {
233    _Internal_error( INTERNAL_ERROR_TOO_LITTLE_WORKSPACE );
234  }
235
236  _Heap_Protection_set_delayed_free_fraction( &_Workspace_Area, 1 );
237}
238
239void *_Workspace_Allocate(
240  size_t   size
241)
242{
243  void *memory;
244
245  memory = _Heap_Allocate( &_Workspace_Area, size );
246  #if defined(DEBUG_WORKSPACE)
247    printk(
248      "Workspace_Allocate(%d) from %p/%p -> %p\n",
249      size,
250      __builtin_return_address( 0 ),
251      __builtin_return_address( 1 ),
252      memory
253    );
254  #endif
255  return memory;
256}
257
258void *_Workspace_Allocate_aligned( size_t size, size_t alignment )
259{
260  return _Heap_Allocate_aligned( &_Workspace_Area, size, alignment );
261}
262
263/*
264 *  _Workspace_Free
265 */
266void _Workspace_Free(
267  void *block
268)
269{
270  #if defined(DEBUG_WORKSPACE)
271    printk(
272      "Workspace_Free(%p) from %p/%p\n",
273      block,
274      __builtin_return_address( 0 ),
275      __builtin_return_address( 1 )
276    );
277  #endif
278  _Heap_Free( &_Workspace_Area, block );
279}
Note: See TracBrowser for help on using the repository browser.