Atlas Runtime
pregion.hpp
Go to the documentation of this file.
1 /*
2  * (c) Copyright 2016 Hewlett Packard Enterprise Development LP
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (at your option) any later version. This program is
8  * distributed in the hope that it will be useful, but WITHOUT ANY
9  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11  * for more details. You should have received a copy of the GNU Lesser
12  * General Public License along with this program. If not, see
13  * <http://www.gnu.org/licenses/>.
14  */
15 
16 
17 #ifndef PREGION_HPP
18 #define PREGION_HPP
19 
20 #include <cassert>
21 #include <cstring>
22 
23 #include "atlas_api.h"
24 #include "internal_api.h"
25 
26 #include "pmalloc.hpp"
27 #include "pmalloc_util.hpp"
28 
29 namespace Atlas {
30 
31 class PRegion {
32 public:
33  explicit PRegion(const char *nm, region_id_t rid, void *ba)
34  : Id_{rid}, BaseAddr_{ba}, IsMapped_{true}, IsDeleted_{false},
35  FileDesc_{-1} {
36  assert(strlen(nm) < kMaxlen_+1);
37  std::strcpy(Name_, nm);
38  initArenaAllocAddresses();
40  flushDirtyCacheLines();
41  }
42  ~PRegion() = default;
43  PRegion(const PRegion&) = delete;
44  PRegion(PRegion&&) = delete;
45  PRegion& operator=(const PRegion&) = delete;
46  PRegion& operator=(PRegion&&) = delete;
47 
48  void set_is_mapped(bool im) { IsMapped_ = im; NVM_FLUSH(&IsMapped_); }
49  void set_is_deleted(bool id) { IsDeleted_ = id; NVM_FLUSH(&IsDeleted_); }
50  void set_file_desc(int fd) { FileDesc_ = fd; }
51 
52  region_id_t get_id() const { return Id_; }
53  void *get_base_addr() const { return BaseAddr_; }
54  bool is_mapped() const { return IsMapped_; }
55  bool is_deleted() const { return IsDeleted_; }
56  int get_file_desc() const { return FileDesc_; }
57  const char *get_name() const { return Name_; }
58 
59  bool doesRangeCheck(const void *ptr, size_t sz) const
60  { return ptr >= BaseAddr_ &&
61  (static_cast<const char*>(ptr) + sz) <
62  (static_cast<char*>(BaseAddr_) + kPRegionSize_); }
63 
64  PArena *getArena(uint32_t index)
65  { assert(index < kNumArenas_); return &Arena_[index]; }
66 
67  void *allocMem(
68  size_t sz, bool does_need_cache_line_alignment,
69  bool does_need_logging);
70  void *callocMem(size_t nmemb, size_t sz);
71  void *reallocMem(void*, size_t);
72  void freeMem(void *ptr, bool should_log);
73 
74  void setRoot(void *new_root)
75  {
76  // TODO: This should not be logged for the helper
77  // thread. This looks like a bogus comment. The helper
78  // thread does not call setRoot. Check back later.
79 
80  // More problems: Though the following may be ok, it
81  // is better to mfence after the following flush. NVM_STR2
82  // may not call flush (for table-based scheme) until later
83  // and does not call mfence until later.
84  NVM_STR2(*(static_cast<intptr_t*>(
85  PMallocUtil::mem2ptr(BaseAddr_))),
86  reinterpret_cast<intptr_t>(new_root),
87  sizeof(intptr_t)*8);
88  full_fence();
89  }
90 
91  void *getRoot() const
92  { return reinterpret_cast<void*>(
93  *(static_cast<intptr_t*>(
94  PMallocUtil::mem2ptr(BaseAddr_))));
95  }
96 
97  void *allocRoot()
98  {
99  // Must be at a known offset, so bypass regular allocation
100  assert(kNumArenas_);
101  return Arena_[0].allocRawMem(sizeof(intptr_t));
102  }
103 
105  {
106  for (uint32_t i = 0; i < kNumArenas_; ++i)
107  getArena(i)->initTransients();
108  }
109 
110  void dumpDebugInfo() const;
111  void printStats();
112 
113 private:
114  // Region metadata follows. Except for the file descriptor, all of
115  // them are persistent and must be properly flushed out.
116 
117  // If any change to the following layout is made, the flushing
118  // code may need to change as well.
119  region_id_t Id_;
120  void *BaseAddr_;
121  bool IsMapped_;
122  bool IsDeleted_;
123  int FileDesc_;
124  char Name_[kMaxlen_];
125  PArena Arena_[kNumArenas_];
126 
127  void initArenaAllocAddresses();
128  void *allocMemFromArenas(
129  size_t sz, bool should_update_free_list,
130  bool does_need_cache_line_alignment, bool does_need_logging);
131  void flushDirtyCacheLines();
132 };
133 
134 inline void PRegion::freeMem(void *ptr, bool should_log)
135 {
136  uint32_t arena_index = (reinterpret_cast<intptr_t>(ptr) -
137  reinterpret_cast<intptr_t>(BaseAddr_))/kArenaSize_;
138  getArena(arena_index)->freeMem(ptr, should_log);
139 }
140 
141 inline void PRegion::initArenaAllocAddresses()
142 {
143  for (uint32_t i = 0; i < kNumArenas_; ++i)
145  static_cast<char*>(BaseAddr_) + i * kArenaSize_);
146 }
147 
151 inline void PRegion::dumpDebugInfo() const
152 {
153 #if defined(ATLAS_ALLOC_DUMP)
154  std::cout << Name_ << " " << Id_ << " " << BaseAddr_ << " " <<
155  (IsMapped_ ? "mapped " : "unmapped ") <<
156  (IsDeleted_ ? "deleted " : "valid ") << std::endl;
157 #endif
158 }
159 
163 inline void PRegion::printStats()
164 {
165 #if defined(ATLAS_ALLOC_STATS)
166  uint64_t total_alloced = 0;
167  for (uint32_t i = 0; i < kNumArenas_; ++i)
168  total_alloced += getArena(i)->get_actual_alloced();
169  std::cout << "[Atlas] Total bytes allocated in region " <<
170  Name_ << ":" << total_alloced << std::endl;
171 #endif
172 }
173 
174 } // namespace Atlas
175 
176 #endif
177 
Definition: pmalloc.hpp:37
void setRoot(void *new_root)
Definition: pregion.hpp:74
PRegion & operator=(const PRegion &)=delete
const uint32_t kArenaSize_
Definition: pregion_configs.hpp:31
void set_file_desc(int fd)
Definition: pregion.hpp:50
Definition: pregion.hpp:31
region_id_t get_id() const
Definition: pregion.hpp:52
const uint32_t kMaxlen_
Definition: pregion_configs.hpp:25
#define NVM_STR2(var, val, size)
Definition: internal_api.h:262
void freeMem(void *ptr, bool should_log)
Definition: pregion.hpp:134
void initArenaTransients()
Definition: pregion.hpp:104
void set_is_deleted(bool id)
Definition: pregion.hpp:49
const char * get_name() const
Definition: pregion.hpp:57
void * reallocMem(void *, size_t)
Definition: pregion.cpp:156
const uint32_t kNumArenas_
Definition: pregion_configs.hpp:30
void * allocRoot()
Definition: pregion.hpp:97
uint32_t region_id_t
Definition: pregion_configs.hpp:22
void * allocMem(size_t sz, bool does_need_cache_line_alignment, bool does_need_logging)
Definition: pregion.cpp:26
bool is_mapped() const
Definition: pregion.hpp:54
int get_file_desc() const
Definition: pregion.hpp:56
static void * mem2ptr(void *mem)
Definition: pmalloc_util.hpp:51
const uint64_t kPRegionSize_
Definition: pregion_configs.hpp:28
#define NVM_FLUSH(p)
Definition: atlas_api.h:106
void * allocRawMem(size_t)
Definition: pmalloc.cpp:339
void initAllocAddresses(void *start_addr)
Definition: pmalloc.hpp:113
void initTransients()
Definition: pmalloc.hpp:121
void set_is_mapped(bool im)
Definition: pregion.hpp:48
uint64_t get_actual_alloced() const
Definition: pmalloc.hpp:57
PArena * getArena(uint32_t index)
Definition: pregion.hpp:64
void * get_base_addr() const
Definition: pregion.hpp:53
bool is_deleted() const
Definition: pregion.hpp:55
void dumpDebugInfo() const
Definition: pregion.hpp:151
void freeMem(void *ptr, bool should_log)
Definition: pmalloc.cpp:37
void * callocMem(size_t nmemb, size_t sz)
Definition: pregion.cpp:143
static void set_default_tl_curr_arena(region_id_t rid)
Definition: pmalloc_util.hpp:24
void * getRoot() const
Definition: pregion.hpp:91
Definition: atlas_alloc_cpp.hpp:21
void printStats()
Definition: pregion.hpp:163
PRegion(const char *nm, region_id_t rid, void *ba)
Definition: pregion.hpp:33
~PRegion()=default
bool doesRangeCheck(const void *ptr, size_t sz) const
Definition: pregion.hpp:59