souffle  2.0.2-371-g6315b36
Relation.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2016, The Souffle Developers. All rights reserved
4  * Licensed under the Universal Permissive License v 1.0 as shown at:
5  * - https://opensource.org/licenses/UPL
6  * - <souffle root>/licenses/SOUFFLE-UPL.txt
7  */
8 
9 #pragma once
10 
12 #include "souffle/profile/Rule.h"
13 #include <algorithm>
14 #include <chrono>
15 #include <cstddef>
16 #include <memory>
17 #include <sstream>
18 #include <string>
19 #include <unordered_map>
20 #include <utility>
21 #include <vector>
22 
23 namespace souffle {
24 namespace profile {
25 
26 /*
27  * Stores the iterations and rules of a given relation
28  */
29 class Relation {
30 private:
31  const std::string name;
32  std::chrono::microseconds starttime{};
33  std::chrono::microseconds endtime{};
34  std::chrono::microseconds loadtime{};
35  std::chrono::microseconds savetime{};
36  long nonRecTuples = 0;
37  size_t preMaxRSS = 0;
38  size_t postMaxRSS = 0;
39  const std::string id;
40  std::string locator;
41  int ruleId = 0;
42  int recursiveId = 0;
43  size_t tuplesRead = 0;
44 
45  std::vector<std::shared_ptr<Iteration>> iterations;
46 
47  std::unordered_map<std::string, std::shared_ptr<Rule>> ruleMap;
48 
49  bool ready = true;
50 
51 public:
52  Relation(std::string name, std::string id) : name(std::move(name)), id(std::move(id)) {
53  ruleMap = std::unordered_map<std::string, std::shared_ptr<Rule>>();
54  iterations = std::vector<std::shared_ptr<Iteration>>();
55  }
56 
57  std::string createID() {
58  return "N" + id.substr(1) + "." + std::to_string(++ruleId);
59  }
60 
61  std::string createRecID(std::string name) {
62  for (auto& iter : iterations) {
63  for (auto& rul : iter->getRules()) {
64  if (rul.second->getName() == name) {
65  return rul.second->getId();
66  }
67  }
68  }
69  return "C" + id.substr(1) + "." + std::to_string(++recursiveId);
70  }
71 
72  std::chrono::microseconds getLoadtime() const {
73  return loadtime;
74  }
75 
76  std::chrono::microseconds getSavetime() const {
77  return savetime;
78  }
79 
80  std::chrono::microseconds getStarttime() const {
81  return starttime;
82  }
83 
84  std::chrono::microseconds getEndtime() const {
85  return endtime;
86  }
87 
88  std::chrono::microseconds getNonRecTime() const {
89  return endtime - starttime;
90  }
91 
92  std::chrono::microseconds getRecTime() const {
93  std::chrono::microseconds result{};
94  for (auto& iter : iterations) {
95  result += iter->getRuntime();
96  }
97  return result;
98  }
99 
100  std::chrono::microseconds getCopyTime() const {
101  std::chrono::microseconds result{};
102  for (auto& iter : iterations) {
103  result += iter->getCopytime();
104  }
105  return result;
106  }
107 
108  size_t size() const {
109  size_t result = 0;
110  for (auto& iter : iterations) {
111  result += iter->size();
112  }
113  return nonRecTuples + result;
114  }
115 
116  size_t getMaxRSSDiff() const {
117  return postMaxRSS - preMaxRSS;
118  }
119 
120  size_t getTotalRecursiveRuleSize() const {
121  size_t result = 0;
122  for (auto& iter : iterations) {
123  for (auto& rul : iter->getRules()) {
124  result += rul.second->size();
125  }
126  }
127  return result;
128  }
129 
130  void setLoadtime(std::chrono::microseconds loadtime) {
131  this->loadtime = loadtime;
132  }
133 
134  void setSavetime(std::chrono::microseconds savetime) {
135  this->savetime = savetime;
136  }
137 
138  void setStarttime(std::chrono::microseconds time) {
139  starttime = time;
140  }
141 
142  void setEndtime(std::chrono::microseconds time) {
143  endtime = time;
144  }
145 
146  void setNumTuples(long numTuples) {
147  nonRecTuples = numTuples;
148  }
149 
150  void setPostMaxRSS(size_t maxRSS) {
151  postMaxRSS = std::max(maxRSS, postMaxRSS);
152  }
153 
154  void setPreMaxRSS(size_t maxRSS) {
155  if (preMaxRSS == 0) {
156  preMaxRSS = maxRSS;
157  return;
158  }
159  preMaxRSS = std::min(maxRSS, preMaxRSS);
160  }
161 
162  std::string toString() const {
163  std::ostringstream output;
164  output << "{\n\"" << name << "\":[" << getNonRecTime().count() << "," << nonRecTuples
165  << "],\n\n\"onRecRules\":[\n";
166  for (auto& rul : ruleMap) {
167  output << rul.second->toString();
168  }
169  output << "\n],\n\"iterations\":\n";
170  output << "[";
171  if (iterations.empty()) {
172  output << ", ";
173  }
174  for (auto& iter : iterations) {
175  output << iter->toString();
176  output << ", ";
177  }
178  std::string retStr = output.str();
179  // substring to remove the last comma
180  return retStr.substr(0, retStr.size() - 2) + "]\n}";
181  }
182 
183  std::string getName() const {
184  return name;
185  }
186 
187  /**
188  * Return a map of Rules, indexed by srcLocator.
189  *
190  * @return the ruleMap
191  */
192  const std::unordered_map<std::string, std::shared_ptr<Rule>>& getRuleMap() const {
193  return ruleMap;
194  }
195 
196  void addRule(std::shared_ptr<Rule> rule) {
197  ruleMap[rule->getLocator()] = rule;
198  }
199 
200  std::vector<std::shared_ptr<Rule>> getRuleRecList() const {
201  std::vector<std::shared_ptr<Rule>> temp = std::vector<std::shared_ptr<Rule>>();
202  for (auto& iter : iterations) {
203  for (auto& rul : iter->getRules()) {
204  temp.push_back(rul.second);
205  }
206  }
207  return temp;
208  }
209 
210  const std::vector<std::shared_ptr<Iteration>>& getIterations() const {
211  return iterations;
212  }
213 
214  void addIteration(std::shared_ptr<Iteration> iteration) {
215  iterations.push_back(iteration);
216  if (endtime < iteration->getEndtime()) {
217  endtime = iteration->getEndtime();
218  }
219  if (starttime.count() == 0 || starttime > iteration->getStarttime()) {
220  starttime = iteration->getStarttime();
221  }
222  }
223 
224  const std::string& getId() const {
225  return id;
226  }
227 
228  const std::string& getLocator() const {
229  return locator;
230  }
231 
232  void setLocator(std::string locator) {
233  this->locator = locator;
234  }
235 
236  bool isReady() {
237  return ready;
238  }
239 
240  void setReady(bool ready) {
241  this->ready = ready;
242  }
243 
244  size_t getReads() const {
245  return tuplesRead;
246  }
247 
248  void addReads(size_t tuplesRead) {
249  this->tuplesRead += tuplesRead;
250  }
251 };
252 
253 } // namespace profile
254 } // namespace souffle
souffle::profile::Relation::getSavetime
std::chrono::microseconds getSavetime() const
Definition: Relation.h:76
souffle::profile::Relation::createID
std::string createID()
Definition: Relation.h:57
souffle::profile::Relation::tuplesRead
size_t tuplesRead
Definition: Relation.h:43
souffle::profile::Relation::savetime
std::chrono::microseconds savetime
Definition: Relation.h:35
souffle::id
A functor representing the identity function for a generic type T.
Definition: StreamUtil.h:136
souffle::profile::Relation::setReady
void setReady(bool ready)
Definition: Relation.h:240
souffle::profile::Relation::setStarttime
void setStarttime(std::chrono::microseconds time)
Definition: Relation.h:138
souffle::profile::Relation::ready
bool ready
Definition: Relation.h:49
souffle::profile::Relation::getRuleMap
const std::unordered_map< std::string, std::shared_ptr< Rule > > & getRuleMap() const
Return a map of Rules, indexed by srcLocator.
Definition: Relation.h:192
souffle::profile::Relation::getCopyTime
std::chrono::microseconds getCopyTime() const
Definition: Relation.h:100
souffle::profile::Relation::addIteration
void addIteration(std::shared_ptr< Iteration > iteration)
Definition: Relation.h:214
souffle::profile::Relation::getName
std::string getName() const
Definition: Relation.h:183
souffle::profile::Relation::getMaxRSSDiff
size_t getMaxRSSDiff() const
Definition: Relation.h:116
iteration
Iteration & iteration
Definition: Reader.h:129
souffle::profile::Relation::name
const std::string name
Definition: Relation.h:31
souffle::profile::Relation::preMaxRSS
size_t preMaxRSS
Definition: Relation.h:37
souffle::profile::Relation::setPostMaxRSS
void setPostMaxRSS(size_t maxRSS)
Definition: Relation.h:150
souffle::profile::Relation::getLoadtime
std::chrono::microseconds getLoadtime() const
Definition: Relation.h:72
souffle::profile::Relation::getRuleRecList
std::vector< std::shared_ptr< Rule > > getRuleRecList() const
Definition: Relation.h:200
souffle::profile::Relation::getNonRecTime
std::chrono::microseconds getNonRecTime() const
Definition: Relation.h:88
souffle::profile::Relation::locator
std::string locator
Definition: Relation.h:40
souffle::profile::Relation::starttime
std::chrono::microseconds starttime
Definition: Relation.h:32
souffle::profile::Relation::nonRecTuples
long nonRecTuples
Definition: Relation.h:36
rul
void rul(size_t limit, bool showLimit=true)
Definition: Tui.h:1106
Iteration.h
souffle::profile::Relation::setLoadtime
void setLoadtime(std::chrono::microseconds loadtime)
Definition: Relation.h:130
souffle::profile::Relation::postMaxRSS
size_t postMaxRSS
Definition: Relation.h:38
souffle::profile::Relation::toString
std::string toString() const
Definition: Relation.h:162
souffle::profile::Relation::getReads
size_t getReads() const
Definition: Relation.h:244
souffle::profile::Relation::setLocator
void setLocator(std::string locator)
Definition: Relation.h:232
souffle::profile::Relation::createRecID
std::string createRecID(std::string name)
Definition: Relation.h:61
souffle::profile::Relation::getIterations
const std::vector< std::shared_ptr< Iteration > > & getIterations() const
Definition: Relation.h:210
souffle::profile::Relation::setSavetime
void setSavetime(std::chrono::microseconds savetime)
Definition: Relation.h:134
souffle::profile::Relation::Relation
Relation(std::string name, std::string id)
Definition: Relation.h:52
souffle::profile::Relation::iterations
std::vector< std::shared_ptr< Iteration > > iterations
Definition: Relation.h:45
souffle::profile::Relation::isReady
bool isReady()
Definition: Relation.h:236
souffle::profile::Relation::getLocator
const std::string & getLocator() const
Definition: Relation.h:228
souffle::profile::Relation::recursiveId
int recursiveId
Definition: Relation.h:42
souffle::profile::Relation::id
const std::string id
Definition: Relation.h:39
souffle::profile::Relation::addRule
void addRule(std::shared_ptr< Rule > rule)
Definition: Relation.h:196
souffle::test::time
long time(const std::string &name, const Op &operation)
Definition: btree_multiset_test.cpp:411
souffle::profile::Relation::getRecTime
std::chrono::microseconds getRecTime() const
Definition: Relation.h:92
std
Definition: Brie.h:3053
souffle::profile::Relation::getTotalRecursiveRuleSize
size_t getTotalRecursiveRuleSize() const
Definition: Relation.h:120
Rule.h
rule
Rule & rule
Definition: Reader.h:85
souffle::profile::Relation::size
size_t size() const
Definition: Relation.h:108
souffle
Definition: AggregateOp.h:25
souffle::profile::Relation::ruleId
int ruleId
Definition: Relation.h:41
souffle::profile::Relation::addReads
void addReads(size_t tuplesRead)
Definition: Relation.h:248
souffle::profile::Relation::getEndtime
std::chrono::microseconds getEndtime() const
Definition: Relation.h:84
souffle::profile::Relation::setEndtime
void setEndtime(std::chrono::microseconds time)
Definition: Relation.h:142
souffle::profile::Relation::loadtime
std::chrono::microseconds loadtime
Definition: Relation.h:34
souffle::profile::Relation::endtime
std::chrono::microseconds endtime
Definition: Relation.h:33
souffle::profile::Relation::setPreMaxRSS
void setPreMaxRSS(size_t maxRSS)
Definition: Relation.h:154
souffle::profile::Relation::getId
const std::string & getId() const
Definition: Relation.h:224
souffle::profile::Relation
Definition: Relation.h:29
souffle::profile::Relation::getStarttime
std::chrono::microseconds getStarttime() const
Definition: Relation.h:80
souffle::profile::Relation::setNumTuples
void setNumTuples(long numTuples)
Definition: Relation.h:146
souffle::profile::Relation::ruleMap
std::unordered_map< std::string, std::shared_ptr< Rule > > ruleMap
Definition: Relation.h:47