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 
16 ABTU_ret_err int ABTI_mem_init(ABTI_global *p_global)
17 {
18  int num_requested_types = 0;
19  ABTU_MEM_LARGEPAGE_TYPE requested_types[3];
20  switch (p_global->mem_lp_alloc) {
21  case ABTI_MEM_LP_MMAP_RP:
22  requested_types[num_requested_types++] = ABTU_MEM_LARGEPAGE_MMAP;
23  requested_types[num_requested_types++] = ABTU_MEM_LARGEPAGE_MALLOC;
24  break;
25  case ABTI_MEM_LP_MMAP_HP_RP:
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;
31  case ABTI_MEM_LP_MMAP_HP_THP:
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),
53  ABTI_mem_pool_global_pool_mprotect_config mprotect_config;
54  if (p_global->stack_guard_kind == ABTI_STACK_GUARD_MPROTECT ||
55  p_global->stack_guard_kind == ABTI_STACK_GUARD_MPROTECT_STRICT) {
56  mprotect_config.enabled = ABT_TRUE;
57  mprotect_config.check_error =
58  (p_global->stack_guard_kind == ABTI_STACK_GUARD_MPROTECT_STRICT)
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  }
72  ABTI_mem_pool_init_global_pool(&p_global->mem_pool_stack,
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 */
80  ABTI_STATIC_ASSERT((ABTI_MEM_POOL_DESC_ELEM_SIZE &
82  ABTI_mem_pool_init_global_pool(&p_global->mem_pool_desc,
83  p_global->mem_max_descs /
85  ABTI_MEM_POOL_DESC_ELEM_SIZE, 0,
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;
91  ABTD_spinlock_clear(&p_global->mem_pool_stack_lock);
92  abt_errno = ABTI_mem_pool_init_local_pool(&p_global->mem_pool_stack_ext,
93  &p_global->mem_pool_stack);
94  if (abt_errno != ABT_SUCCESS) {
95  ABTI_mem_pool_destroy_global_pool(&p_global->mem_pool_stack);
96  ABTI_mem_pool_destroy_global_pool(&p_global->mem_pool_desc);
97  ABTI_HANDLE_ERROR(abt_errno);
98  }
99  ABTD_spinlock_clear(&p_global->mem_pool_desc_lock);
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) {
103  ABTI_mem_pool_destroy_local_pool(&p_global->mem_pool_stack_ext);
104  ABTI_mem_pool_destroy_global_pool(&p_global->mem_pool_stack);
105  ABTI_mem_pool_destroy_global_pool(&p_global->mem_pool_desc);
106  ABTI_HANDLE_ERROR(abt_errno);
107  }
108 #endif
109  return ABT_SUCCESS;
110 }
111 
112 ABTU_ret_err int ABTI_mem_init_local(ABTI_global *p_global,
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) {
122  ABTI_mem_pool_destroy_local_pool(&p_local_xstream->mem_pool_stack);
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
131  ABTI_mem_pool_destroy_local_pool(&p_global->mem_pool_stack_ext);
132  ABTI_mem_pool_destroy_local_pool(&p_global->mem_pool_desc_ext);
133 #endif
134  ABTI_mem_pool_destroy_global_pool(&p_global->mem_pool_stack);
135  ABTI_mem_pool_destroy_global_pool(&p_global->mem_pool_desc);
136 }
137 
138 void ABTI_mem_finalize_local(ABTI_xstream *p_local_xstream)
139 {
140  ABTI_mem_pool_destroy_local_pool(&p_local_xstream->mem_pool_stack);
141  ABTI_mem_pool_destroy_local_pool(&p_local_xstream->mem_pool_desc);
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  }
157  case ABTI_MEM_LP_MMAP_HP_RP:
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  }
168  case ABTI_MEM_LP_MMAP_HP_THP:
170  sp_size, alignment, ABTU_MEM_LARGEPAGE_MMAP_HUGEPAGE)) {
171  return ABTI_MEM_LP_MMAP_HP_THP;
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 
195 ABTU_ret_err int ABTI_mem_init(ABTI_global *p_global)
196 {
197  return ABT_SUCCESS;
198 }
199 
200 ABTU_ret_err int ABTI_mem_init_local(ABTI_global *p_global,
201  ABTI_xstream *p_local_xstream)
202 {
203  return ABT_SUCCESS;
204 }
205 
206 void ABTI_mem_finalize(ABTI_global *p_global)
207 {
208 }
209 
210 void ABTI_mem_finalize_local(ABTI_xstream *p_local_xstream)
211 {
212 }
213 
214 #endif /* !ABT_CONFIG_USE_MEM_POOL */
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
ABTU_roundup_size
static size_t ABTU_roundup_size(size_t val, size_t multiple)
Definition: abtu.h:95
ABT_MEM_POOL_MAX_LOCAL_BUCKETS
#define ABT_MEM_POOL_MAX_LOCAL_BUCKETS
Definition: abti_mem_pool.h:9
ABTU_MEM_LARGEPAGE_MMAP
@ ABTU_MEM_LARGEPAGE_MMAP
Definition: abtu.h:311
ABTU_MEM_LARGEPAGE_MMAP_HUGEPAGE
@ ABTU_MEM_LARGEPAGE_MMAP_HUGEPAGE
Definition: abtu.h:312
abti.h
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
ABT_SUCCESS
#define ABT_SUCCESS
Error code: the routine returns successfully.
Definition: abt.h:92
ABTU_MEM_LARGEPAGE_MALLOC
@ ABTU_MEM_LARGEPAGE_MALLOC
Definition: abtu.h:309
ABTU_ret_err
#define ABTU_ret_err
Definition: abtu.h:155
ABT_TRUE
#define ABT_TRUE
True constant for ABT_bool.
Definition: abt.h:784
ABT_FALSE
#define ABT_FALSE
False constant for ABT_bool.
Definition: abt.h:786
ABTU_MEM_LARGEPAGE_MEMALIGN
@ ABTU_MEM_LARGEPAGE_MEMALIGN
Definition: abtu.h:310