tudocomp
– The TU Dortmund Compression Framework
PhaseData.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <tudocomp_stat/Json.hpp>
5 
7 
8 namespace tdc {
9 
10 class PhaseData {
11 private:
12  static constexpr size_t STR_BUFFER_SIZE = 64;
13 
14  struct keyval {
15  keyval* next;
16  char key[STR_BUFFER_SIZE];
17  char val[STR_BUFFER_SIZE];
18 
19  inline keyval() : next(nullptr) {
20  }
21 
22  ~keyval() {
23  if(next) delete next;
24  }
25  };
26 
27  char m_title[STR_BUFFER_SIZE];
28 
29 public:
30  unsigned long time_start;
31  unsigned long time_end;
32  ssize_t mem_off;
33  ssize_t mem_current;
34  ssize_t mem_peak;
35 
36  keyval* first_stat;
37 
38  PhaseData* first_child;
39  PhaseData* next_sibling;
40 
41  inline PhaseData()
42  : first_stat(nullptr),
43  first_child(nullptr),
44  next_sibling(nullptr) {
45  }
46 
47  inline ~PhaseData() {
48  if(first_stat) delete first_stat;
49  if(first_child) delete first_child;
50  if(next_sibling) delete next_sibling;
51  }
52 
53  inline const char* title() const {
54  return m_title;
55  }
56 
57  inline void title(const char* title) {
58  strncpy(m_title, title, STR_BUFFER_SIZE);
59  }
60 
61  template<typename T>
62  inline void log_stat(const char* key, const T& value) {
63  keyval* kv = new keyval();
64 
65  strncpy(kv->key, key, STR_BUFFER_SIZE);
66  strncpy(kv->val, std::to_string(value).c_str(), STR_BUFFER_SIZE);
67 
68  if(first_stat) {
69  keyval* last = first_stat;
70  while(last->next) {
71  last = last->next;
72  }
73  last->next = kv;
74  } else {
75  first_stat = kv;
76  }
77  }
78 
79  inline json::Object to_json() const {
80  json::Object obj;
81  obj.set("title", m_title);
82  obj.set("timeStart", time_start);
83  obj.set("timeEnd", time_end);
84  obj.set("memOff", mem_off);
85  obj.set("memPeak", mem_peak);
86  obj.set("memFinal", mem_current);
87 
88  json::Array stats;
89  keyval* kv = first_stat;
90  while(kv) {
91  json::Object pair;
92  pair.set("key", std::string(kv->key));
93  pair.set("value", std::string(kv->val));
94  stats.add(pair);
95  kv = kv->next;
96  }
97  obj.set("stats", stats);
98 
99  json::Array sub;
100 
101  PhaseData* child = first_child;
102  while(child) {
103  sub.add(child->to_json());
104  child = child->next_sibling;
105  }
106 
107  obj.set("sub", sub);
108 
109  return obj;
110  }
111 };
112 
113 }
114 
116 
Contains the text compression and encoding framework.
Definition: namespaces.hpp:11
std::string to_string(tdc::uint_impl_t< N > value)
Definition: uint_t.hpp:241