ARGOBOTS  c6511494322293e01714f56f341b8d2b22c1e3c1
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
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 void ABTI_mem_init(ABTI_global *p_global)
17 {
18  int num_requested_types = 0;
19  ABTU_MEM_LARGEPAGE_TYPE requested_types[3];
20  switch (gp_ABTI_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 = (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  gp_ABTI_global->mem_page_size);
65  /* The last four bytes will be used to store a mempool flag */
66  ABTI_STATIC_ASSERT((ABTI_MEM_POOL_DESC_ELEM_SIZE &
68  ABTI_mem_pool_init_global_pool(&p_global->mem_pool_desc,
69  p_global->mem_max_descs /
71  ABTI_MEM_POOL_DESC_ELEM_SIZE, 0,
72  p_global->mem_page_size, requested_types,
73  num_requested_types,
74  gp_ABTI_global->mem_page_size);
75 #ifndef ABT_CONFIG_DISABLE_EXT_THREAD
76  ABTI_spinlock_clear(&p_global->mem_pool_stack_lock);
77  ABTI_mem_pool_init_local_pool(&p_global->mem_pool_stack_ext,
78  &p_global->mem_pool_stack);
79  ABTI_spinlock_clear(&p_global->mem_pool_desc_lock);
80  ABTI_mem_pool_init_local_pool(&p_global->mem_pool_desc_ext,
81  &p_global->mem_pool_desc);
82 #endif
83 }
84 
85 void ABTI_mem_init_local(ABTI_xstream *p_local_xstream)
86 {
87  ABTI_mem_pool_init_local_pool(&p_local_xstream->mem_pool_stack,
88  &gp_ABTI_global->mem_pool_stack);
89  ABTI_mem_pool_init_local_pool(&p_local_xstream->mem_pool_desc,
90  &gp_ABTI_global->mem_pool_desc);
91 }
92 
93 void ABTI_mem_finalize(ABTI_global *p_global)
94 {
95 #ifndef ABT_CONFIG_DISABLE_EXT_THREAD
96  ABTI_mem_pool_destroy_local_pool(&p_global->mem_pool_stack_ext);
97  ABTI_mem_pool_destroy_local_pool(&p_global->mem_pool_desc_ext);
98 #endif
99  ABTI_mem_pool_destroy_global_pool(&p_global->mem_pool_stack);
100  ABTI_mem_pool_destroy_global_pool(&p_global->mem_pool_desc);
101 }
102 
103 void ABTI_mem_finalize_local(ABTI_xstream *p_local_xstream)
104 {
105  ABTI_mem_pool_destroy_local_pool(&p_local_xstream->mem_pool_stack);
106  ABTI_mem_pool_destroy_local_pool(&p_local_xstream->mem_pool_desc);
107 }
108 
109 int ABTI_mem_check_lp_alloc(int lp_alloc)
110 {
111  size_t sp_size = gp_ABTI_global->mem_sp_size;
112  size_t pg_size = gp_ABTI_global->mem_page_size;
113  size_t alignment = ABT_CONFIG_STATIC_CACHELINE_SIZE;
114  switch (lp_alloc) {
115  case ABTI_MEM_LP_MMAP_RP:
116  if (ABTU_is_supported_largepage_type(pg_size, alignment,
118  return ABTI_MEM_LP_MMAP_RP;
119  } else {
120  return ABTI_MEM_LP_MALLOC;
121  }
122  case ABTI_MEM_LP_MMAP_HP_RP:
124  sp_size, alignment, ABTU_MEM_LARGEPAGE_MMAP_HUGEPAGE)) {
125  return ABTI_MEM_LP_MMAP_HP_RP;
126  } else if (
127  ABTU_is_supported_largepage_type(pg_size, alignment,
129  return ABTI_MEM_LP_MMAP_RP;
130  } else {
131  return ABTI_MEM_LP_MALLOC;
132  }
133  case ABTI_MEM_LP_MMAP_HP_THP:
135  sp_size, alignment, ABTU_MEM_LARGEPAGE_MMAP_HUGEPAGE)) {
136  return ABTI_MEM_LP_MMAP_HP_THP;
137  } else if (
139  gp_ABTI_global->huge_page_size,
141  return ABTI_MEM_LP_THP;
142  } else {
143  return ABTI_MEM_LP_MALLOC;
144  }
145  case ABTI_MEM_LP_THP:
147  gp_ABTI_global->huge_page_size,
149  return ABTI_MEM_LP_THP;
150  } else {
151  return ABTI_MEM_LP_MALLOC;
152  }
153  default:
154  return ABTI_MEM_LP_MALLOC;
155  }
156 }
157 
158 #else /* !ABT_CONFIG_USE_MEM_POOL */
159 
160 void ABTI_mem_init(ABTI_global *p_global)
161 {
162 }
163 
164 void ABTI_mem_init_local(ABTI_xstream *p_local_xstream)
165 {
166 }
167 
168 void ABTI_mem_finalize(ABTI_global *p_global)
169 {
170 }
171 
172 void ABTI_mem_finalize_local(ABTI_xstream *p_local_xstream)
173 {
174 }
175 
176 #endif /* !ABT_CONFIG_USE_MEM_POOL */
#define ABT_MEM_POOL_MAX_LOCAL_BUCKETS
Definition: abti_mem_pool.h:9
#define ABT_CONFIG_STATIC_CACHELINE_SIZE
Definition: abt_config.h:57
int ABTU_is_supported_largepage_type(size_t size, size_t alignment_hint, ABTU_MEM_LARGEPAGE_TYPE requested)
Definition: largepage.c:59
ABTI_global * gp_ABTI_global
Definition: global.c:18
ABTU_MEM_LARGEPAGE_TYPE
Definition: abtu.h:203