ARGOBOTS  1.1
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),
53  if ((stacksize & (2 * ABT_CONFIG_STATIC_CACHELINE_SIZE - 1)) == 0) {
54  /* Avoid a multiple of 2 * cacheline size to avoid cache bank conflict.
55  */
57  }
58  ABTI_mem_pool_init_global_pool(&p_global->mem_pool_stack,
59  p_global->mem_max_stacks /
61  stacksize, thread_stacksize,
62  p_global->mem_sp_size, requested_types,
63  num_requested_types,
64  p_global->mem_page_size);
65  /* The last four bytes will be used to store a mempool flag */
68  ABTI_mem_pool_init_global_pool(&p_global->mem_pool_desc,
69  p_global->mem_max_descs /
72  p_global->mem_page_size, requested_types,
73  num_requested_types,
74  p_global->mem_page_size);
75 #ifndef ABT_CONFIG_DISABLE_EXT_THREAD
76  int abt_errno;
77  ABTD_spinlock_clear(&p_global->mem_pool_stack_lock);
78  abt_errno = ABTI_mem_pool_init_local_pool(&p_global->mem_pool_stack_ext,
79  &p_global->mem_pool_stack);
80  if (abt_errno != ABT_SUCCESS) {
81  ABTI_mem_pool_destroy_global_pool(&p_global->mem_pool_stack);
82  ABTI_mem_pool_destroy_global_pool(&p_global->mem_pool_desc);
83  ABTI_HANDLE_ERROR(abt_errno);
84  }
85  ABTD_spinlock_clear(&p_global->mem_pool_desc_lock);
86  abt_errno = ABTI_mem_pool_init_local_pool(&p_global->mem_pool_desc_ext,
87  &p_global->mem_pool_desc);
88  if (abt_errno != ABT_SUCCESS) {
89  ABTI_mem_pool_destroy_local_pool(&p_global->mem_pool_stack_ext);
90  ABTI_mem_pool_destroy_global_pool(&p_global->mem_pool_stack);
91  ABTI_mem_pool_destroy_global_pool(&p_global->mem_pool_desc);
92  ABTI_HANDLE_ERROR(abt_errno);
93  }
94 #endif
95  return ABT_SUCCESS;
96 }
97 
99  ABTI_xstream *p_local_xstream)
100 {
101  int abt_errno;
102  abt_errno = ABTI_mem_pool_init_local_pool(&p_local_xstream->mem_pool_stack,
103  &p_global->mem_pool_stack);
104  ABTI_CHECK_ERROR(abt_errno);
105  abt_errno = ABTI_mem_pool_init_local_pool(&p_local_xstream->mem_pool_desc,
106  &p_global->mem_pool_desc);
107  if (abt_errno != ABT_SUCCESS) {
108  ABTI_mem_pool_destroy_local_pool(&p_local_xstream->mem_pool_stack);
109  ABTI_HANDLE_ERROR(abt_errno);
110  }
111  return ABT_SUCCESS;
112 }
113 
114 void ABTI_mem_finalize(ABTI_global *p_global)
115 {
116 #ifndef ABT_CONFIG_DISABLE_EXT_THREAD
117  ABTI_mem_pool_destroy_local_pool(&p_global->mem_pool_stack_ext);
118  ABTI_mem_pool_destroy_local_pool(&p_global->mem_pool_desc_ext);
119 #endif
120  ABTI_mem_pool_destroy_global_pool(&p_global->mem_pool_stack);
121  ABTI_mem_pool_destroy_global_pool(&p_global->mem_pool_desc);
122 }
123 
124 void ABTI_mem_finalize_local(ABTI_xstream *p_local_xstream)
125 {
126  ABTI_mem_pool_destroy_local_pool(&p_local_xstream->mem_pool_stack);
127  ABTI_mem_pool_destroy_local_pool(&p_local_xstream->mem_pool_desc);
128 }
129 
130 int ABTI_mem_check_lp_alloc(ABTI_global *p_global, int lp_alloc)
131 {
132  size_t sp_size = p_global->mem_sp_size;
133  size_t pg_size = p_global->mem_page_size;
134  size_t alignment = ABT_CONFIG_STATIC_CACHELINE_SIZE;
135  switch (lp_alloc) {
136  case ABTI_MEM_LP_MMAP_RP:
137  if (ABTU_is_supported_largepage_type(pg_size, alignment,
139  return ABTI_MEM_LP_MMAP_RP;
140  } else {
141  return ABTI_MEM_LP_MALLOC;
142  }
145  sp_size, alignment, ABTU_MEM_LARGEPAGE_MMAP_HUGEPAGE)) {
146  return ABTI_MEM_LP_MMAP_HP_RP;
147  } else if (
148  ABTU_is_supported_largepage_type(pg_size, alignment,
150  return ABTI_MEM_LP_MMAP_RP;
151  } else {
152  return ABTI_MEM_LP_MALLOC;
153  }
156  sp_size, alignment, ABTU_MEM_LARGEPAGE_MMAP_HUGEPAGE)) {
158  } else if (
160  p_global->huge_page_size,
162  return ABTI_MEM_LP_THP;
163  } else {
164  return ABTI_MEM_LP_MALLOC;
165  }
166  case ABTI_MEM_LP_THP:
168  p_global->huge_page_size,
170  return ABTI_MEM_LP_THP;
171  } else {
172  return ABTI_MEM_LP_MALLOC;
173  }
174  default:
175  return ABTI_MEM_LP_MALLOC;
176  }
177 }
178 
179 #else /* !ABT_CONFIG_USE_MEM_POOL */
180 
182 {
183  return ABT_SUCCESS;
184 }
185 
187  ABTI_xstream *p_local_xstream)
188 {
189  return ABT_SUCCESS;
190 }
191 
193 {
194 }
195 
196 void ABTI_mem_finalize_local(ABTI_xstream *p_local_xstream)
197 {
198 }
199 
200 #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:144
ABTI_MEM_LP_MMAP_HP_THP
@ ABTI_MEM_LP_MMAP_HP_THP
Definition: abti_mem.h:21
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_mem_init
ABTU_ret_err int ABTI_mem_init(ABTI_global *p_global)
Definition: malloc.c:181
ABTU_roundup_size
static size_t ABTU_roundup_size(size_t val, size_t multiple)
Definition: abtu.h:95
ABTI_CHECK_ERROR
#define ABTI_CHECK_ERROR(abt_errno)
Definition: abti_error.h:120
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
ABTU_MEM_LARGEPAGE_MMAP
@ ABTU_MEM_LARGEPAGE_MMAP
Definition: abtu.h:300
ABTI_xstream
Definition: abti.h:264
ABTU_MEM_LARGEPAGE_MMAP_HUGEPAGE
@ ABTU_MEM_LARGEPAGE_MMAP_HUGEPAGE
Definition: abtu.h:301
ABTI_MEM_LP_THP
@ ABTI_MEM_LP_THP
Definition: abti_mem.h:22
abti.h
ABTI_global::thread_stacksize
size_t thread_stacksize
Definition: abti.h:209
ABTI_mem_finalize
void ABTI_mem_finalize(ABTI_global *p_global)
Definition: malloc.c:192
ABTI_HANDLE_ERROR
#define ABTI_HANDLE_ERROR(n)
Definition: abti_error.h:114
ABTI_global::huge_page_size
size_t huge_page_size
Definition: abti.h:218
ABTI_mem_finalize_local
void ABTI_mem_finalize_local(ABTI_xstream *p_local_xstream)
Definition: malloc.c:196
ABTI_MEM_LP_MMAP_HP_RP
@ ABTI_MEM_LP_MMAP_HP_RP
Definition: abti_mem.h:20
ABTI_ASSERT
#define ABTI_ASSERT(cond)
Definition: abti_error.h:12
ABTU_MEM_LARGEPAGE_TYPE
ABTU_MEM_LARGEPAGE_TYPE
Definition: abtu.h:297
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)
Definition: mem_pool.c:77
ABT_CONFIG_STATIC_CACHELINE_SIZE
#define ABT_CONFIG_STATIC_CACHELINE_SIZE
Definition: abt_config.h:72
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:129
ABTI_STATIC_ASSERT
#define ABTI_STATIC_ASSERT(cond)
Definition: abti_error.h:19
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:186
ABTU_MEM_LARGEPAGE_MALLOC
@ ABTU_MEM_LARGEPAGE_MALLOC
Definition: abtu.h:298
ABTU_ret_err
#define ABTU_ret_err
Definition: abtu.h:146
ABTD_spinlock_clear
static void ABTD_spinlock_clear(ABTD_spinlock *p_lock)
Definition: abtd_spinlock.h:23
ABTI_ythread
Definition: abti.h:406
ABTI_global
Definition: abti.h:196
ABTU_MEM_LARGEPAGE_MEMALIGN
@ ABTU_MEM_LARGEPAGE_MEMALIGN
Definition: abtu.h:299
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:105
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