Atlas Runtime
helper.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 HELPER_HPP
18 #define HELPER_HPP
19 
20 #include <cstdio>
21 #include <fstream>
22 #include <atomic>
23 #include <cassert>
24 #include <map>
25 #include <vector>
26 #include <string>
27 
28 #include "log_mgr.hpp"
29 
30 namespace Atlas {
31 
32 typedef std::map<LogEntry*, bool> Log2Bool;
33 
34 // The top-level instance of the helper thread
35 class Helper {
36  static Helper *Instance_;
37 
38 public:
39 
40  struct LogVer {
41  explicit LogVer(LogStructure *ls, Log2Bool del_logs)
42  : LS_(ls), Del_(del_logs) {}
43  LogVer() = delete;
44 
46  Log2Bool Del_;
47  };
48 
49  typedef std::vector<LogVer> LogVersions;
50  typedef std::map<LogEntry*, uint64_t> MapLog2Int;
51  typedef std::multimap<LogEntry*, uint64_t> MultiMapLog2Int;
52  typedef MultiMapLog2Int::iterator DelIter;
53 
54  static Helper& createInstance() {
55  assert(!Instance_);
56  Instance_ = new Helper();
57  return *Instance_;
58  }
59 
60  static void deleteInstance() {
61  assert(Instance_);
62  delete Instance_;
63  Instance_ = nullptr;
64  }
65 
66  static Helper& getInstance() {
67  assert(Instance_);
68  return *Instance_;
69  }
70 
71  uint64_t get_iter_num() const { return IterNum_; }
72 
73  void doConsistentUpdate(void*);
74  void addEntryToDeletedMap(LogEntry *le, uint64_t gen_num)
75  { DeletedRelLogs_.insert(std::make_pair(le, gen_num)); }
76 
77  bool isDeletedByHelperThread(LogEntry *le, uint64_t gen_num);
78  template<class T> void trace(T s)
79  { TraceStream_ << s; }
80 
81  void incrementTotalGraphBuildTime(uint64_t inc)
82  { TotalGraphBuildTime_ += inc; }
83  void incrementTotalGraphResolveTime(uint64_t inc)
84  { TotalGraphResolveTime_ += inc; }
85  void incrementTotalPruneTime(uint64_t inc)
86  { TotalPruneTime_ += inc; }
87 
88  void printStats();
89 
90 private:
91 
92  Helper() :
93  IterNum_{0},
94  IsInRecovery_{false},
95  LogVersions_{},
96  DeletedRelLogs_{},
97  ExistingRelMap_{},
98 #if defined(_NVM_TRACE) || defined(_NVM_VERBOSE_TRACE)
99  TraceStream_{"/tmp/atlas_log_pruner.txt"},
100 #endif
101  TotalGraphBuildTime_{0},
102  TotalGraphResolveTime_{0},
103  TotalPruneTime_{0}
104  {
105 #if defined(_NVM_TRACE) || defined(_NVM_VERBOSE_TRACE)
106  assert(TraceStream_ && "Error opening trace file");
107 #endif
108  }
109 
110  ~Helper() {
111 #if defined(_NVM_TRACE) || defined(_NVM_VERBOSE_TRACE)
112  TraceStream_.close();
113 #endif
114 
115  }
116 
117  uint64_t IterNum_;
118  bool IsInRecovery_;
119  LogVersions LogVersions_;
120  MultiMapLog2Int DeletedRelLogs_;
121  MapLog2Int ExistingRelMap_;
122  std::ofstream TraceStream_;
123 
124  // there is only 1 helper thread today
125  uint64_t TotalGraphBuildTime_;
126  uint64_t TotalGraphResolveTime_;
127  uint64_t TotalPruneTime_;
128 
129  void collectRelLogEntries(LogStructure *lsp);
130  bool areUserThreadsDone() const
132 };
133 
134 } // namespace Atlas
135 
136 #endif
Log2Bool Del_
Definition: helper.hpp:46
void incrementTotalGraphResolveTime(uint64_t inc)
Definition: helper.hpp:83
Definition: log_structure.hpp:30
MultiMapLog2Int::iterator DelIter
Definition: helper.hpp:52
static Helper & createInstance()
Definition: helper.hpp:54
std::map< LogEntry *, bool > Log2Bool
Definition: helper.hpp:32
void addEntryToDeletedMap(LogEntry *le, uint64_t gen_num)
Definition: helper.hpp:74
static Helper & getInstance()
Definition: helper.hpp:66
void trace(T s)
Definition: helper.hpp:78
std::vector< LogVer > LogVersions
Definition: helper.hpp:49
std::map< LogEntry *, uint64_t > MapLog2Int
Definition: helper.hpp:50
Definition: helper.hpp:35
void incrementTotalGraphBuildTime(uint64_t inc)
Definition: helper.hpp:81
bool areUserThreadsDone() const
Definition: log_mgr.hpp:154
LogStructure * LS_
Definition: helper.hpp:45
void incrementTotalPruneTime(uint64_t inc)
Definition: helper.hpp:85
static void deleteInstance()
Definition: helper.hpp:60
uint64_t get_iter_num() const
Definition: helper.hpp:71
static LogMgr & getInstance()
Definition: log_mgr.hpp:75
bool isDeletedByHelperThread(LogEntry *le, uint64_t gen_num)
Definition: helper_driver.cpp:144
Definition: log_structure.hpp:77
std::multimap< LogEntry *, uint64_t > MultiMapLog2Int
Definition: helper.hpp:51
void printStats()
Definition: helper_driver.cpp:161
Definition: helper.hpp:40
LogVer(LogStructure *ls, Log2Bool del_logs)
Definition: helper.hpp:41
Definition: atlas_alloc_cpp.hpp:21
void doConsistentUpdate(void *)
Definition: helper_driver.cpp:76