ARGOBOTS  dce6e727ffc4ca5b3ffc04cb9517c6689be51ec5
malloc.c
Go to the documentation of this file.
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  * See COPYRIGHT in top-level directory.
4  */
5 
6 #include "abti.h"
7 
8 #ifdef ABT_CONFIG_USE_MEM_POOL
9 /* Currently the total memory allocated for stacks and task block pages is not
10  * shrunk to avoid the thrashing overhead except that ESs are terminated or
11  * ABT_finalize is called. When an ES terminates its execution, stacks and
12  * empty pages that it holds are deallocated. Non-empty pages are added to the
13  * global data. When ABTI_finalize is called, all memory objects that we have
14  * allocated are returned to the higher-level memory allocator. */
15 
17 {
18  int num_requested_types = 0;
19  ABTU_MEM_LARGEPAGE_TYPE requested_types[3];
20  switch (p_global->mem_lp_alloc) {
22  requested_types[num_requested_types++] = ABTU_MEM_LARGEPAGE_MMAP;
23  requested_types[num_requested_types++] = ABTU_MEM_LARGEPAGE_MALLOC;
24  break;
26  requested_types[num_requested_types++] =
28  requested_types[num_requested_types++] = ABTU_MEM_LARGEPAGE_MMAP;
29  requested_types[num_requested_types++] = ABTU_MEM_LARGEPAGE_MALLOC;
30  break;
32  requested_types[num_requested_types++] =
34  requested_types[num_requested_types++] =
36  requested_types[num_requested_types++] = ABTU_MEM_LARGEPAGE_MALLOC;
37  break;
38  case ABTI_MEM_LP_THP:
39  requested_types[num_requested_types++] =
41  requested_types[num_requested_types++] = ABTU_MEM_LARGEPAGE_MALLOC;
42  break;
43  default:
44  requested_types[num_requested_types++] = ABTU_MEM_LARGEPAGE_MALLOC;
45  break;
46  }
47  size_t thread_stacksize = p_global->thread_stacksize;
48  ABTI_ASSERT((thread_stacksize & (ABT_CONFIG_STATIC_CACHELINE_SIZE - 1)) ==
49  0);
50  size_t stacksize =
51  ABTU_roundup_size(thread_stacksize + sizeof(ABTI_ythread),
56  mprotect_config.enabled = ABT_TRUE;
57  mprotect_config.check_error =
59  ? ABT_TRUE
60  : ABT_FALSE;
61  mprotect_config.offset = 0;
62  mprotect_config.page_size = p_global->sys_page_size;
63  mprotect_config.alignment = p_global->sys_page_size;
64  } else {
65  mprotect_config.enabled = ABT_FALSE;
66  }
67  if ((stacksize & (2 * ABT_CONFIG_STATIC_CACHELINE_SIZE - 1)) == 0) {
68  /* Avoid a multiple of 2 * cacheline size to avoid cache bank conflict.
69  */
71  }
73  p_global->mem_max_stacks /
75  stacksize, thread_stacksize,
76  p_global->mem_sp_size, requested_types,
77  num_requested_types, p_global->mem_page_size,
78  &mprotect_config);
79  /* The last four bytes will be used to store a mempool flag */
83  p_global->mem_max_descs /
86  p_global->mem_page_size, requested_types,
87  num_requested_types, p_global->mem_page_size,
88  NULL);
89 #ifndef ABT_CONFIG_DISABLE_EXT_THREAD
90  int abt_errno;
93  &p_global->mem_pool_stack);
94  if (abt_errno != ABT_SUCCESS) {
97  ABTI_HANDLE_ERROR(abt_errno);
98  }
100  abt_errno = ABTI_mem_pool_init_local_pool(&p_global->mem_pool_desc_ext,
101  &p_global->mem_pool_desc);
102  if (abt_errno != ABT_SUCCESS) {
106  ABTI_HANDLE_ERROR(abt_errno);
107  }
108 #endif
109  return ABT_SUCCESS;
110 }
111 
113  ABTI_xstream *p_local_xstream)
114 {
115  int abt_errno;
116  abt_errno = ABTI_mem_pool_init_local_pool(&p_local_xstream->mem_pool_stack,
117  &p_global->mem_pool_stack);
118  ABTI_CHECK_ERROR(abt_errno);
119  abt_errno = ABTI_mem_pool_init_local_pool(&p_local_xstream->mem_pool_desc,
120  &p_global->mem_pool_desc);
121  if (abt_errno != ABT_SUCCESS) {
123  ABTI_HANDLE_ERROR(abt_errno);
124  }
125  return ABT_SUCCESS;
126 }
127 
128 void ABTI_mem_finalize(ABTI_global *p_global)
129 {
130 #ifndef ABT_CONFIG_DISABLE_EXT_THREAD
133 #endif
136 }
137 
138 void ABTI_mem_finalize_local(ABTI_xstream *p_local_xstream)
139 {
142 }
143 
144 int ABTI_mem_check_lp_alloc(ABTI_global *p_global, int lp_alloc)
145 {
146  size_t sp_size = p_global->mem_sp_size;
147  size_t pg_size = p_global->mem_page_size;
148  size_t alignment = ABT_CONFIG_STATIC_CACHELINE_SIZE;
149  switch (lp_alloc) {
150  case ABTI_MEM_LP_MMAP_RP:
151  if (ABTU_is_supported_largepage_type(pg_size, alignment,
153  return ABTI_MEM_LP_MMAP_RP;
154  } else {
155  return ABTI_MEM_LP_MALLOC;
156  }
159  sp_size, alignment, ABTU_MEM_LARGEPAGE_MMAP_HUGEPAGE)) {
160  return ABTI_MEM_LP_MMAP_HP_RP;
161  } else if (
162  ABTU_is_supported_largepage_type(pg_size, alignment,
164  return ABTI_MEM_LP_MMAP_RP;
165  } else {
166  return ABTI_MEM_LP_MALLOC;
167  }
170  sp_size, alignment, ABTU_MEM_LARGEPAGE_MMAP_HUGEPAGE)) {
172  } else if (
174  p_global->huge_page_size,
176  return ABTI_MEM_LP_THP;
177  } else {
178  return ABTI_MEM_LP_MALLOC;
179  }
180  case ABTI_MEM_LP_THP:
182  p_global->huge_page_size,
184  return ABTI_MEM_LP_THP;
185  } else {
186  return ABTI_MEM_LP_MALLOC;
187  }
188  default:
189  return ABTI_MEM_LP_MALLOC;
190  }
191 }
192 
193 #else /* !ABT_CONFIG_USE_MEM_POOL */
194 
196 {
197  return ABT_SUCCESS;
198 }
199 
201  ABTI_xstream *p_local_xstream)
202 {
203  return ABT_SUCCESS;
204 }
205 
207 {
208 }
209 
210 void ABTI_mem_finalize_local(ABTI_xstream *p_local_xstream)
211 {
212 }
213 
214 #endif /* !ABT_CONFIG_USE_MEM_POOL */
ABTI_mem_pool_destroy_local_pool
void ABTI_mem_pool_destroy_local_pool(ABTI_mem_pool_local_pool *p_local_pool)
Definition: mem_pool.c:201
ABTI_global::mem_pool_desc_ext
ABTI_mem_pool_local_pool mem_pool_desc_ext
Definition: abti.h:263
ABTI_MEM_LP_MMAP_HP_THP
@ ABTI_MEM_LP_MMAP_HP_THP
Definition: abti_mem.h:21
ABTI_STACK_GUARD_MPROTECT_STRICT
@ ABTI_STACK_GUARD_MPROTECT_STRICT
Definition: abti.h:79
ABTU_is_supported_largepage_type
int ABTU_is_supported_largepage_type(size_t size, size_t alignment_hint, ABTU_MEM_LARGEPAGE_TYPE requested)
Definition: largepage.c:59
ABTI_global::mem_lp_alloc
int mem_lp_alloc
Definition: abti.h:253
ABTI_mem_init
ABTU_ret_err int ABTI_mem_init(ABTI_global *p_global)
Definition: malloc.c:195
ABTU_roundup_size
static size_t ABTU_roundup_size(size_t val, size_t multiple)
Definition: abtu.h:95
ABTI_global::mem_pool_stack
ABTI_mem_pool_global_pool mem_pool_stack
Definition: abti.h:255
ABTI_mem_pool_global_pool_mprotect_config::offset
size_t offset
Definition: abti_mem_pool.h:38
ABTI_CHECK_ERROR
#define ABTI_CHECK_ERROR(abt_errno)
Definition: abti_error.h:136
ABTI_MEM_LP_MALLOC
@ ABTI_MEM_LP_MALLOC
Definition: abti_mem.h:18
ABT_MEM_POOL_MAX_LOCAL_BUCKETS
#define ABT_MEM_POOL_MAX_LOCAL_BUCKETS
Definition: abti_mem_pool.h:9
ABTI_xstream::mem_pool_stack
ABTI_mem_pool_local_pool mem_pool_stack
Definition: abti.h:314
ABTU_MEM_LARGEPAGE_MMAP
@ ABTU_MEM_LARGEPAGE_MMAP
Definition: abtu.h:311
ABTI_xstream
Definition: abti.h:294
ABTI_global::stack_guard_kind
ABTI_stack_guard stack_guard_kind
Definition: abti.h:266
ABTU_MEM_LARGEPAGE_MMAP_HUGEPAGE
@ ABTU_MEM_LARGEPAGE_MMAP_HUGEPAGE
Definition: abtu.h:312
ABTI_MEM_LP_THP
@ ABTI_MEM_LP_THP
Definition: abti_mem.h:22
abti.h
ABTI_mem_pool_global_pool_mprotect_config::alignment
size_t alignment
Definition: abti_mem_pool.h:40
ABTI_global::sys_page_size
size_t sys_page_size
Definition: abti.h:246
ABTI_global::mem_page_size
size_t mem_page_size
Definition: abti.h:249
ABTI_global::thread_stacksize
size_t thread_stacksize
Definition: abti.h:237
ABTI_mem_finalize
void ABTI_mem_finalize(ABTI_global *p_global)
Definition: malloc.c:206
ABTI_HANDLE_ERROR
#define ABTI_HANDLE_ERROR(n)
Definition: abti_error.h:130
ABTI_global::huge_page_size
size_t huge_page_size
Definition: abti.h:247
ABTI_mem_finalize_local
void ABTI_mem_finalize_local(ABTI_xstream *p_local_xstream)
Definition: malloc.c:210
ABTI_MEM_LP_MMAP_HP_RP
@ ABTI_MEM_LP_MMAP_HP_RP
Definition: abti_mem.h:20
ABTI_global::mem_pool_stack_ext
ABTI_mem_pool_local_pool mem_pool_stack_ext
Definition: abti.h:261
ABTI_ASSERT
#define ABTI_ASSERT(cond)
Definition: abti_error.h:12
ABTI_global::mem_sp_size
size_t mem_sp_size
Definition: abti.h:250
ABTU_MEM_LARGEPAGE_TYPE
ABTU_MEM_LARGEPAGE_TYPE
Definition: abtu.h:308
ABT_CONFIG_STATIC_CACHELINE_SIZE
#define ABT_CONFIG_STATIC_CACHELINE_SIZE
Definition: abt_config.h:81
ABTI_mem_pool_init_local_pool
ABTU_ret_err int ABTI_mem_pool_init_local_pool(ABTI_mem_pool_local_pool *p_local_pool, ABTI_mem_pool_global_pool *p_global_pool)
Definition: mem_pool.c:186
ABTI_STATIC_ASSERT
#define ABTI_STATIC_ASSERT(cond)
Definition: abti_error.h:35
ABT_SUCCESS
#define ABT_SUCCESS
Error code: the routine returns successfully.
Definition: abt.h:92
ABTI_mem_init_local
ABTU_ret_err int ABTI_mem_init_local(ABTI_global *p_global, ABTI_xstream *p_local_xstream)
Definition: malloc.c:200
ABTU_MEM_LARGEPAGE_MALLOC
@ ABTU_MEM_LARGEPAGE_MALLOC
Definition: abtu.h:309
ABTU_ret_err
#define ABTU_ret_err
Definition: abtu.h:155
ABTI_xstream::mem_pool_desc
ABTI_mem_pool_local_pool mem_pool_desc
Definition: abti.h:315
ABTI_STACK_GUARD_MPROTECT
@ ABTI_STACK_GUARD_MPROTECT
Definition: abti.h:78
ABT_TRUE
#define ABT_TRUE
True constant for ABT_bool.
Definition: abt.h:784
ABTD_spinlock_clear
static void ABTD_spinlock_clear(ABTD_spinlock *p_lock)
Definition: abtd_spinlock.h:23
ABT_FALSE
#define ABT_FALSE
False constant for ABT_bool.
Definition: abt.h:786
ABTI_ythread
Definition: abti.h:456
ABTI_global::mem_max_stacks
uint32_t mem_max_stacks
Definition: abti.h:251
ABTI_mem_pool_global_pool_mprotect_config
Definition: abti_mem_pool.h:35
ABTI_global::mem_pool_desc_lock
ABTD_spinlock mem_pool_desc_lock
Definition: abti.h:262
ABTI_global::mem_pool_stack_lock
ABTD_spinlock mem_pool_stack_lock
Definition: abti.h:260
ABTI_mem_pool_global_pool_mprotect_config::page_size
size_t page_size
Definition: abti_mem_pool.h:39
ABTI_mem_pool_global_pool_mprotect_config::enabled
ABT_bool enabled
Definition: abti_mem_pool.h:36
ABTI_global
Definition: abti.h:223
ABTU_MEM_LARGEPAGE_MEMALIGN
@ ABTU_MEM_LARGEPAGE_MEMALIGN
Definition: abtu.h:310
ABTI_mem_pool_init_global_pool
void ABTI_mem_pool_init_global_pool(ABTI_mem_pool_global_pool *p_global_pool, size_t num_headers_per_bucket, size_t header_size, size_t header_offset, size_t page_size, const ABTU_MEM_LARGEPAGE_TYPE *lp_type_requests, uint32_t num_lp_type_requests, size_t alignment_hint, ABTI_mem_pool_global_pool_mprotect_config *p_mprotect_config)
Definition: mem_pool.c:89
ABTI_mem_pool_destroy_global_pool
void ABTI_mem_pool_destroy_global_pool(ABTI_mem_pool_global_pool *p_global_pool)
Definition: mem_pool.c:142
ABTI_mem_check_lp_alloc
int ABTI_mem_check_lp_alloc(ABTI_global *p_global, int lp_alloc)
ABTI_MEM_POOL_DESC_ELEM_SIZE
#define ABTI_MEM_POOL_DESC_ELEM_SIZE
Definition: abti_mem.h:14
ABTI_MEM_LP_MMAP_RP
@ ABTI_MEM_LP_MMAP_RP
Definition: abti_mem.h:19
ABTI_global::mem_pool_desc
ABTI_mem_pool_global_pool mem_pool_desc
Definition: abti.h:256
ABTI_global::mem_max_descs
uint32_t mem_max_descs
Definition: abti.h:252
ABTI_mem_pool_global_pool_mprotect_config::check_error
ABT_bool check_error
Definition: abti_mem_pool.h:37