souffle  2.0.2-371-g6315b36
OutputProcessor.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 
11 #include "souffle/profile/Cell.h"
16 #include "souffle/profile/Row.h"
17 #include "souffle/profile/Rule.h"
18 #include "souffle/profile/Table.h"
19 #include <chrono>
20 #include <memory>
21 #include <ratio>
22 #include <set>
23 #include <string>
24 #include <unordered_map>
25 #include <utility>
26 #include <vector>
27 
28 namespace souffle {
29 namespace profile {
30 
31 /*
32  * Class to format profiler data structures into tables
33  */
35 private:
36  std::shared_ptr<ProgramRun> programRun;
37 
38 public:
40  programRun = std::make_shared<ProgramRun>(ProgramRun());
41  }
42 
43  const std::shared_ptr<ProgramRun>& getProgramRun() const {
44  return programRun;
45  }
46 
47  Table getRelTable() const;
48 
49  Table getRulTable() const;
50 
51  Table getSubrulTable(std::string strRel, std::string strRul) const;
52 
53  Table getAtomTable(std::string strRel, std::string strRul) const;
54 
55  Table getVersions(std::string strRel, std::string strRul) const;
56 
57  Table getVersionAtoms(std::string strRel, std::string strRul, int version) const;
58 };
59 
60 /*
61  * rel table :
62  * ROW[0] = TOT_T
63  * ROW[1] = NREC_T
64  * ROW[2] = REC_T
65  * ROW[3] = COPY_T
66  * ROW[4] = TUPLES
67  * ROW[5] = REL NAME
68  * ROW[6] = ID
69  * ROW[7] = SRC
70  * ROW[8] = PERFOR
71  * ROW[9] = LOADTIME
72  * ROW[10] = SAVETIME
73  * ROW[11] = MAXRSSDIFF
74  * ROW[12] = READS
75  *
76  */
78  const std::unordered_map<std::string, std::shared_ptr<Relation>>& relationMap =
79  programRun->getRelationMap();
80  Table table;
81  for (auto& rel : relationMap) {
82  std::shared_ptr<Relation> r = rel.second;
83  Row row(13);
84  auto total_time = r->getNonRecTime() + r->getRecTime() + r->getCopyTime();
85  row[0] = std::make_shared<Cell<std::chrono::microseconds>>(total_time);
86  row[1] = std::make_shared<Cell<std::chrono::microseconds>>(r->getNonRecTime());
87  row[2] = std::make_shared<Cell<std::chrono::microseconds>>(r->getRecTime());
88  row[3] = std::make_shared<Cell<std::chrono::microseconds>>(r->getCopyTime());
89  row[4] = std::make_shared<Cell<long>>(r->size());
90  row[5] = std::make_shared<Cell<std::string>>(r->getName());
91  row[6] = std::make_shared<Cell<std::string>>(r->getId());
92  row[7] = std::make_shared<Cell<std::string>>(r->getLocator());
93  if (total_time.count() != 0) {
94  row[8] = std::make_shared<Cell<long>>(r->size() / (total_time.count() / 1000000.0));
95  } else {
96  row[8] = std::make_shared<Cell<long>>(r->size());
97  }
98  row[9] = std::make_shared<Cell<std::chrono::microseconds>>(r->getLoadtime());
99  row[10] = std::make_shared<Cell<std::chrono::microseconds>>(r->getSavetime());
100  row[11] = std::make_shared<Cell<long>>(r->getMaxRSSDiff());
101  row[12] = std::make_shared<Cell<long>>(r->getReads());
102 
103  table.addRow(std::make_shared<Row>(row));
104  }
105  return table;
106 }
107 /*
108  * rul table :
109  * ROW[0] = TOT_T
110  * ROW[1] = NREC_T
111  * ROW[2] = REC_T
112  * ROW[3] = COPY_T
113  * ROW[4] = TUPLES
114  * ROW[5] = RUL NAME
115  * ROW[6] = ID
116  * ROW[7] = SRC
117  * ROW[8] = PERFOR
118  * ROW[9] = VER
119  * ROW[10]= REL_NAME
120  */
122  const std::unordered_map<std::string, std::shared_ptr<Relation>>& relationMap =
123  programRun->getRelationMap();
124  std::unordered_map<std::string, std::shared_ptr<Row>> ruleMap;
125 
126  for (auto& rel : relationMap) {
127  for (auto& current : rel.second->getRuleMap()) {
128  Row row(11);
129  std::shared_ptr<Rule> rule = current.second;
130  row[0] = std::make_shared<Cell<std::chrono::microseconds>>(rule->getRuntime());
131  row[1] = std::make_shared<Cell<std::chrono::microseconds>>(rule->getRuntime());
132  row[2] = std::make_shared<Cell<std::chrono::microseconds>>(std::chrono::microseconds(0));
133  row[3] = std::make_shared<Cell<std::chrono::microseconds>>(std::chrono::microseconds(0));
134  row[4] = std::make_shared<Cell<long>>(rule->size());
135  row[5] = std::make_shared<Cell<std::string>>(rule->getName());
136  row[6] = std::make_shared<Cell<std::string>>(rule->getId());
137  row[7] = std::make_shared<Cell<std::string>>(rel.second->getName());
138  row[8] = std::make_shared<Cell<long>>(0);
139  row[10] = std::make_shared<Cell<std::string>>(rule->getLocator());
140  ruleMap.emplace(rule->getName(), std::make_shared<Row>(row));
141  }
142  for (auto& iter : rel.second->getIterations()) {
143  for (auto& current : iter->getRules()) {
144  std::shared_ptr<Rule> rule = current.second;
145  if (ruleMap.find(rule->getName()) != ruleMap.end()) {
146  Row row = *ruleMap[rule->getName()];
147  row[2] = std::make_shared<Cell<std::chrono::microseconds>>(
148  row[2]->getTimeVal() + rule->getRuntime());
149  row[4] = std::make_shared<Cell<long>>(row[4]->getLongVal() + rule->size());
150  row[0] = std::make_shared<Cell<std::chrono::microseconds>>(
151  row[0]->getTimeVal() + rule->getRuntime());
152  ruleMap[rule->getName()] = std::make_shared<Row>(row);
153  } else {
154  Row row(11);
155  row[0] = std::make_shared<Cell<std::chrono::microseconds>>(rule->getRuntime());
156  row[1] = std::make_shared<Cell<std::chrono::microseconds>>(std::chrono::microseconds(0));
157  row[2] = std::make_shared<Cell<std::chrono::microseconds>>(rule->getRuntime());
158  row[3] = std::make_shared<Cell<std::chrono::microseconds>>(std::chrono::microseconds(0));
159  row[4] = std::make_shared<Cell<long>>(rule->size());
160  row[5] = std::make_shared<Cell<std::string>>(rule->getName());
161  row[6] = std::make_shared<Cell<std::string>>(rule->getId());
162  row[7] = std::make_shared<Cell<std::string>>(rel.second->getName());
163  row[8] = std::make_shared<Cell<long>>(rule->getVersion());
164  row[10] = std::make_shared<Cell<std::string>>(rule->getLocator());
165  ruleMap[rule->getName()] = std::make_shared<Row>(row);
166  }
167  }
168  }
169  for (auto& current : ruleMap) {
170  std::shared_ptr<Row> row = current.second;
171  Row t = *row;
172  std::chrono::microseconds val = t[1]->getTimeVal() + t[2]->getTimeVal() + t[3]->getTimeVal();
173 
174  t[0] = std::make_shared<Cell<std::chrono::microseconds>>(val);
175 
176  if (t[0]->getTimeVal().count() != 0) {
177  t[9] = std::make_shared<Cell<double>>(t[4]->getLongVal() / (t[0]->getDoubleVal() * 1000));
178  } else {
179  t[9] = std::make_shared<Cell<double>>(t[4]->getLongVal() / 1.0);
180  }
181  current.second = std::make_shared<Row>(t);
182  }
183  }
184 
185  Table table;
186  for (auto& current : ruleMap) {
187  table.addRow(current.second);
188  }
189  return table;
190 }
191 
192 /*
193  * atom table :
194  * ROW[0] = clause
195  * ROW[1] = atom
196  * ROW[2] = level
197  * ROW[3] = frequency
198  */
199 Table inline OutputProcessor::getAtomTable(std::string strRel, std::string strRul) const {
200  const std::unordered_map<std::string, std::shared_ptr<Relation>>& relationMap =
201  programRun->getRelationMap();
202 
203  Table table;
204  for (auto& current : relationMap) {
205  std::shared_ptr<Relation> rel = current.second;
206 
207  if (rel->getId() != strRel) {
208  continue;
209  }
210 
211  for (auto& current : rel->getRuleMap()) {
212  std::shared_ptr<Rule> rule = current.second;
213  if (rule->getId() != strRul) {
214  continue;
215  }
216  for (auto& atom : rule->getAtoms()) {
217  Row row(4);
218  row[0] = std::make_shared<Cell<std::string>>(atom.rule);
219  row[1] = std::make_shared<Cell<std::string>>(atom.identifier);
220  row[2] = std::make_shared<Cell<long>>(atom.level);
221  row[3] = std::make_shared<Cell<long>>(atom.frequency);
222 
223  table.addRow(std::make_shared<Row>(row));
224  }
225  }
226  }
227  return table;
228 }
229 
230 /*
231  * subrule table :
232  * ROW[0] = subrule
233  */
234 Table inline OutputProcessor::getSubrulTable(std::string strRel, std::string strRul) const {
235  const std::unordered_map<std::string, std::shared_ptr<Relation>>& relationMap =
236  programRun->getRelationMap();
237 
238  Table table;
239  for (auto& current : relationMap) {
240  std::shared_ptr<Relation> rel = current.second;
241 
242  if (rel->getId() != strRel) {
243  continue;
244  }
245 
246  for (auto& current : rel->getRuleMap()) {
247  std::shared_ptr<Rule> rule = current.second;
248  if (rule->getId() != strRul) {
249  continue;
250  }
251  for (auto& atom : rule->getAtoms()) {
252  Row row(1);
253  row[0] = std::make_shared<Cell<std::string>>(atom.rule);
254 
255  table.addRow(std::make_shared<Row>(row));
256  }
257  }
258  }
259  return table;
260 }
261 
262 /*
263  * ver table :
264  * ROW[0] = TOT_T
265  * ROW[1] = NREC_T
266  * ROW[2] = REC_T
267  * ROW[3] = COPY_T
268  * ROW[4] = TUPLES
269  * ROW[5] = RUL NAME
270  * ROW[6] = ID
271  * ROW[7] = SRC
272  * ROW[8] = PERFOR
273  * ROW[9] = VER
274  * ROW[10]= REL_NAME
275  */
276 Table inline OutputProcessor::getVersions(std::string strRel, std::string strRul) const {
277  const std::unordered_map<std::string, std::shared_ptr<Relation>>& relationMap =
278  programRun->getRelationMap();
279  Table table;
280 
281  std::shared_ptr<Relation> rel;
282  for (auto& current : relationMap) {
283  if (current.second->getId() == strRel) {
284  rel = current.second;
285  break;
286  }
287  }
288  if (rel == nullptr) {
289  return table;
290  }
291 
292  std::unordered_map<std::string, std::shared_ptr<Row>> ruleMap;
293  for (auto& iter : rel->getIterations()) {
294  for (auto& current : iter->getRules()) {
295  std::shared_ptr<Rule> rule = current.second;
296  if (rule->getId() == strRul) {
297  std::string strTemp =
298  rule->getName() + rule->getLocator() + std::to_string(rule->getVersion());
299 
300  if (ruleMap.find(strTemp) != ruleMap.end()) {
301  Row row = *ruleMap[strTemp];
302  row[2] = std::make_shared<Cell<std::chrono::microseconds>>(
303  row[2]->getTimeVal() + rule->getRuntime());
304  row[4] = std::make_shared<Cell<long>>(row[4]->getLongVal() + rule->size());
305  row[0] = std::make_shared<Cell<std::chrono::microseconds>>(rule->getRuntime());
306  ruleMap[strTemp] = std::make_shared<Row>(row);
307  } else {
308  Row row(10);
309  row[1] = std::make_shared<Cell<std::chrono::microseconds>>(std::chrono::microseconds(0));
310  row[2] = std::make_shared<Cell<std::chrono::microseconds>>(rule->getRuntime());
311  row[3] = std::make_shared<Cell<std::chrono::microseconds>>(std::chrono::microseconds(0));
312  row[4] = std::make_shared<Cell<long>>(rule->size());
313  row[5] = std::make_shared<Cell<std::string>>(rule->getName());
314  row[6] = std::make_shared<Cell<std::string>>(rule->getId());
315  row[7] = std::make_shared<Cell<std::string>>(rel->getName());
316  row[8] = std::make_shared<Cell<long>>(rule->getVersion());
317  row[9] = std::make_shared<Cell<std::string>>(rule->getLocator());
318  row[0] = std::make_shared<Cell<std::chrono::microseconds>>(rule->getRuntime());
319  ruleMap[strTemp] = std::make_shared<Row>(row);
320  }
321  }
322  }
323  }
324 
325  for (auto row : ruleMap) {
326  Row t = *row.second;
327  t[0] = std::make_shared<Cell<std::chrono::microseconds>>(
328  t[1]->getTimeVal() + t[2]->getTimeVal() + t[3]->getTimeVal());
329  ruleMap[row.first] = std::make_shared<Row>(t);
330  }
331 
332  for (auto& current : ruleMap) {
333  table.addRow(current.second);
334  }
335  return table;
336 }
337 
338 /*
339  * atom table :
340  * ROW[0] = rule
341  * ROW[1] = atom
342  * ROW[2] = level
343  * ROW[3] = frequency
344  */
345 Table inline OutputProcessor::getVersionAtoms(std::string strRel, std::string srcLocator, int version) const {
346  const std::unordered_map<std::string, std::shared_ptr<Relation>>& relationMap =
347  programRun->getRelationMap();
348  Table table;
349  std::shared_ptr<Relation> rel;
350 
351  for (auto& current : relationMap) {
352  if (current.second->getId() == strRel) {
353  rel = current.second;
354  break;
355  }
356  }
357  if (rel == nullptr) {
358  return table;
359  }
360 
361  for (auto& iter : rel->getIterations()) {
362  for (auto& current : iter->getRules()) {
363  std::shared_ptr<Rule> rule = current.second;
364  if (rule->getLocator() == srcLocator && rule->getVersion() == version) {
365  for (auto& atom : rule->getAtoms()) {
366  Row row(4);
367  row[0] = std::make_shared<Cell<std::string>>(atom.rule);
368  row[1] = std::make_shared<Cell<std::string>>(atom.identifier);
369  row[2] = std::make_shared<Cell<long>>(atom.level);
370  row[3] = std::make_shared<Cell<long>>(atom.frequency);
371  table.addRow(std::make_shared<Row>(row));
372  }
373  }
374  }
375  }
376 
377  return table;
378 }
379 
380 } // namespace profile
381 } // namespace souffle
souffle::profile::OutputProcessor::getVersionAtoms
Table getVersionAtoms(std::string strRel, std::string strRul, int version) const
Definition: OutputProcessor.h:345
Table.h
souffle::profile::OutputProcessor::getRulTable
Table getRulTable() const
Definition: OutputProcessor.h:121
souffle::profile::ProgramRun
Definition: ProgramRun.h:31
CellInterface.h
souffle::profile::Table::addRow
void addRow(std::shared_ptr< Row > row)
Definition: Table.h:30
souffle::profile::OutputProcessor::getRelTable
Table getRelTable() const
Definition: OutputProcessor.h:77
souffle::profile::OutputProcessor::programRun
std::shared_ptr< ProgramRun > programRun
Definition: OutputProcessor.h:36
ProgramRun.h
souffle::profile::OutputProcessor
Definition: OutputProcessor.h:34
souffle::profile::Row
Definition: Row.h:22
Iteration.h
souffle::profile::OutputProcessor::getSubrulTable
Table getSubrulTable(std::string strRel, std::string strRul) const
Definition: OutputProcessor.h:234
souffle::profile::OutputProcessor::getVersions
Table getVersions(std::string strRel, std::string strRul) const
Definition: OutputProcessor.h:276
souffle::profile::OutputProcessor::OutputProcessor
OutputProcessor()
Definition: OutputProcessor.h:39
souffle::profile::OutputProcessor::getAtomTable
Table getAtomTable(std::string strRel, std::string strRul) const
Definition: OutputProcessor.h:199
Rule.h
rule
Rule & rule
Definition: Reader.h:85
souffle::profile::OutputProcessor::getProgramRun
const std::shared_ptr< ProgramRun > & getProgramRun() const
Definition: OutputProcessor.h:43
souffle
Definition: AggregateOp.h:25
Row.h
Cell.h
rel
void rel(size_t limit, bool showLimit=true)
Definition: Tui.h:1086
Relation.h
souffle::profile::Table
Definition: Table.h:24