gem5  v21.1.0.2
hdf5.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2019 Arm Limited
3  * All rights reserved
4  *
5  * The license below extends only to copyright in the software and shall
6  * not be construed as granting a license to any other intellectual
7  * property including but not limited to intellectual property relating
8  * to a hardware implementation of the functionality of the software
9  * licensed hereunder. You may use the software subject to the license
10  * terms below provided that you ensure that this notice is replicated
11  * unmodified and in its entirety in all distributions of the software,
12  * modified or unmodified, in source code or in binary form.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions are
16  * met: redistributions of source code must retain the above copyright
17  * notice, this list of conditions and the following disclaimer;
18  * redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution;
21  * neither the name of the copyright holders nor the names of its
22  * contributors may be used to endorse or promote products derived from
23  * this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #include "base/stats/hdf5.hh"
39 
40 #include "base/logging.hh"
41 #include "base/stats/info.hh"
42 
43 namespace gem5
44 {
45 
49 template<typename T>
50 bool emptyStrings(const T &labels)
51 {
52  for (const auto &s : labels) {
53  if (!s.empty())
54  return false;
55  }
56  return true;
57 }
58 
59 
60 GEM5_DEPRECATED_NAMESPACE(Stats, statistics);
61 namespace statistics
62 {
63 
64 Hdf5::Hdf5(const std::string &file, unsigned chunking,
65  bool desc, bool formulas)
66  : fname(file), timeChunk(chunking),
67  enableDescriptions(desc), enableFormula(formulas),
68  dumpCount(0)
69 {
70  // Tell the library not to print exceptions by default. There are
71  // cases where we rely on exceptions to determine if we need to
72  // create a node or if we can just open it.
73  H5::Exception::dontPrint();
74 }
75 
77 {
78 }
79 
80 
81 void
83 {
84  h5File = H5::H5File(fname,
85  // Truncate the file if this is the first dump
86  dumpCount > 0 ? H5F_ACC_RDWR : H5F_ACC_TRUNC);
87  path.push(h5File.openGroup("/"));
88 }
89 
90 void
92 {
93  assert(valid());
94 
95  dumpCount++;
96 }
97 
98 bool
99 Hdf5::valid() const
100 {
101  return true;
102 }
103 
104 
105 void
106 Hdf5::beginGroup(const char *name)
107 {
108  auto base = path.top();
109 
110  // Try to open an existing stat group corresponding to the
111  // name. Create it if it doesn't exist.
112  H5::Group group;
113  try {
114  group = base.openGroup(name);
115  } catch (const H5::FileIException& e) {
116  group = base.createGroup(name);
117  } catch (const H5::GroupIException& e) {
118  group = base.createGroup(name);
119  }
120 
121  path.push(group);
122 }
123 
124 void
126 {
127  assert(!path.empty());
128  path.pop();
129 }
130 
131 void
133 {
134  // Since this stat is a scalar, we need 1-dimensional value in the
135  // stat file. The Hdf5::appendStat helper will populate the size
136  // of the first dimension (time).
137  hsize_t fdims[1] = { 0, };
138  double data[1] = { info.result(), };
139 
140  appendStat(info, 1, fdims, data);
141 }
142 
143 void
145 {
146  appendVectorInfo(info);
147 }
148 
149 void
150 Hdf5::visit(const DistInfo &info)
151 {
152  warn_once("HDF5 stat files don't support distributions.\n");
153 }
154 
155 void
157 {
158  warn_once("HDF5 stat files don't support vector distributions.\n");
159 }
160 
161 void
163 {
164  // Request a 3-dimensional stat, the first dimension will be
165  // populated by the Hdf5::appendStat() helper. The remaining two
166  // dimensions correspond to the stat instance.
167  hsize_t fdims[3] = { 0, info.x, info.y };
168  H5::DataSet data_set = appendStat(info, 3, fdims, info.cvec.data());
169 
170  if (dumpCount == 0) {
171  if (!info.subnames.empty() && !emptyStrings(info.subnames))
172  addMetaData(data_set, "subnames", info.subnames);
173 
174  if (!info.y_subnames.empty() && !emptyStrings(info.y_subnames))
175  addMetaData(data_set, "y_subnames", info.y_subnames);
176 
177  if (!info.subdescs.empty() && !emptyStrings(info.subdescs))
178  addMetaData(data_set, "subdescs", info.subdescs);
179  }
180 }
181 
182 void
184 {
185  if (!enableFormula)
186  return;
187 
188  H5::DataSet data_set = appendVectorInfo(info);
189 
190  if (dumpCount == 0)
191  addMetaData(data_set, "equation", info.str());
192 }
193 
194 void
196 {
197  warn_once("HDF5 stat files don't support sparse histograms.\n");
198 }
199 
200 H5::DataSet
202 {
203  const VResult &vr(info.result());
204  // Request a 2-dimensional stat, the first dimension will be
205  // populated by the Hdf5::appendStat() helper. The remaining
206  // dimension correspond to the stat instance.
207  hsize_t fdims[2] = { 0, vr.size() };
208  H5::DataSet data_set = appendStat(info, 2, fdims, vr.data());
209 
210  if (dumpCount == 0) {
211  if (!info.subnames.empty() && !emptyStrings(info.subnames))
212  addMetaData(data_set, "subnames", info.subnames);
213 
214  if (!info.subdescs.empty() && !emptyStrings(info.subdescs))
215  addMetaData(data_set, "subdescs", info.subdescs);
216  }
217 
218  return data_set;
219 }
220 
221 H5::DataSet
222 Hdf5::appendStat(const Info &info, int rank, hsize_t *dims, const double *data)
223 {
224  H5::Group group = path.top();
225  H5::DataSet data_set;
226  H5::DataSpace fspace;
227 
228  dims[0] = dumpCount + 1;
229 
230  if (dumpCount > 0) {
231  // Get the existing stat if we have already dumped this stat
232  // before.
233  data_set = group.openDataSet(info.name);
234  data_set.extend(dims);
235  fspace = data_set.getSpace();
236  } else {
237  // We don't have the stat already, create it.
238 
239  H5::DSetCreatPropList props;
240 
241  // Setup max dimensions based on the requested file dimensions
242  std::vector<hsize_t> max_dims(rank);
243  std::copy(dims, dims + rank, max_dims.begin());
244  max_dims[0] = H5S_UNLIMITED;
245 
246  // Setup chunking
247  std::vector<hsize_t> chunk_dims(rank);
248  std::copy(dims, dims + rank, chunk_dims.begin());
249  chunk_dims[0] = timeChunk;
250  props.setChunk(rank, chunk_dims.data());
251 
252  // Enable compression
253  props.setDeflate(1);
254 
255  fspace = H5::DataSpace(rank, dims, max_dims.data());
256  try {
257  data_set = group.createDataSet(info.name,
258  H5::PredType::NATIVE_DOUBLE, fspace, props);
259  } catch (const H5::Exception &e) {
260  std::string err = "Failed creating H5::DataSet " + info.name + "; ";
261  err += e.getDetailMsg() + " in " + e.getFuncName();
262  // Rethrow std exception so that it's passed on to the Python world
263  throw std::runtime_error(err);
264  }
265 
266  if (enableDescriptions && !info.desc.empty()) {
267  addMetaData(data_set, "description", info.desc);
268  }
269  }
270 
271  // The first dimension is time which isn't included in data.
272  dims[0] = 1;
273  H5::DataSpace mspace(rank, dims);
274  std::vector<hsize_t> foffset(rank, 0);
275  foffset[0] = dumpCount;
276 
277  fspace.selectHyperslab(H5S_SELECT_SET, dims, foffset.data());
278  data_set.write(data, H5::PredType::NATIVE_DOUBLE, mspace, fspace);
279 
280  return data_set;
281 }
282 
283 void
284 Hdf5::addMetaData(H5::DataSet &loc, const char *name,
285  const std::vector<const char *> &values)
286 {
287  H5::StrType type(H5::PredType::C_S1, H5T_VARIABLE);
288  hsize_t dims[1] = { values.size(), };
289  H5::DataSpace space(1, dims);
290  H5::Attribute attribute = loc.createAttribute(name, type, space);
291  attribute.write(type, values.data());
292 }
293 
294 void
295 Hdf5::addMetaData(H5::DataSet &loc, const char *name,
296  const std::vector<std::string> &values)
297 {
298  std::vector<const char *> cstrs(values.size());
299  for (int i = 0; i < values.size(); ++i)
300  cstrs[i] = values[i].c_str();
301 
302  addMetaData(loc, name, cstrs);
303 }
304 
305 void
306 Hdf5::addMetaData(H5::DataSet &loc, const char *name,
307  const std::string &value)
308 {
309  H5::StrType type(H5::PredType::C_S1, value.length() + 1);
310  hsize_t dims[1] = { 1, };
311  H5::DataSpace space(1, dims);
312  H5::Attribute attribute = loc.createAttribute(name, type, space);
313  attribute.write(type, value.c_str());
314 }
315 
316 void
317 Hdf5::addMetaData(H5::DataSet &loc, const char *name, double value)
318 {
319  hsize_t dims[1] = { 1, };
320  H5::DataSpace space(1, dims);
321  H5::Attribute attribute = loc.createAttribute(
322  name, H5::PredType::NATIVE_DOUBLE, space);
323  attribute.write(H5::PredType::NATIVE_DOUBLE, &value);
324 }
325 
326 
327 std::unique_ptr<Output>
328 initHDF5(const std::string &filename, unsigned chunking,
329  bool desc, bool formulas)
330 {
331  return std::unique_ptr<Output>(
332  new Hdf5(simout.resolve(filename), chunking, desc, formulas));
333 }
334 
335 }; // namespace statistics
336 } // namespace gem5
gem5::statistics::VectorInfo::subdescs
std::vector< std::string > subdescs
Definition: info.hh:189
gem5::statistics::DistInfo
Definition: info.hh:201
gem5::statistics::Hdf5::visit
void visit(const ScalarInfo &info) override
Definition: hdf5.cc:132
data
const char data[]
Definition: circlebuf.test.cc:48
gem5::statistics::VectorInfo
Definition: info.hh:184
warn_once
#define warn_once(...)
Definition: logging.hh:249
gem5::emptyStrings
bool emptyStrings(const T &labels)
Check if all strings in a container are empty.
Definition: hdf5.cc:50
gem5::statistics::Hdf5::~Hdf5
~Hdf5()
Definition: hdf5.cc:76
gem5::statistics::VectorInfo::result
virtual const VResult & result() const =0
gem5::ArmISA::err
Bitfield< 6 > err
Definition: misc_types.hh:750
gem5::ArmISA::e
Bitfield< 9 > e
Definition: misc_types.hh:64
gem5::simout
OutputDirectory simout
Definition: output.cc:62
gem5::statistics::Hdf5::endGroup
void endGroup() override
Definition: hdf5.cc:125
gem5::statistics::Hdf5::enableDescriptions
const bool enableDescriptions
Definition: hdf5.hh:148
gem5::statistics::Vector2dInfo::y_subnames
std::vector< std::string > y_subnames
Definition: info.hh:232
gem5::statistics::ScalarInfo
Definition: info.hh:176
std::vector< Result >
gem5::statistics::Hdf5::dumpCount
unsigned dumpCount
Definition: hdf5.hh:153
gem5::statistics::Hdf5::end
void end() override
Definition: hdf5.cc:91
gem5::X86ISA::base
Bitfield< 51, 12 > base
Definition: pagetable.hh:141
gem5::ArmISA::i
Bitfield< 7 > i
Definition: misc_types.hh:66
gem5::statistics::Vector2dInfo::subdescs
std::vector< std::string > subdescs
Definition: info.hh:231
gem5::statistics::Hdf5::enableFormula
const bool enableFormula
Definition: hdf5.hh:149
gem5::statistics::Hdf5::appendVectorInfo
H5::DataSet appendVectorInfo(const VectorInfo &info)
Helper function to append vector stats and set their metadata.
Definition: hdf5.cc:201
gem5::statistics::ScalarInfo::result
virtual Result result() const =0
gem5::statistics::Hdf5::begin
void begin() override
Definition: hdf5.cc:82
gem5::statistics::Hdf5::fname
const std::string fname
Definition: hdf5.hh:146
info.hh
gem5::statistics::Hdf5::appendStat
H5::DataSet appendStat(const Info &info, int rank, hsize_t *dims, const double *data)
Helper function to append an n-dimensional double stat to the file.
Definition: hdf5.cc:222
gem5::statistics::Hdf5::path
std::stack< H5::Group > path
Definition: hdf5.hh:151
gem5::statistics::Vector2dInfo::cvec
VCounter cvec
Local storage for the entry values, used for printing.
Definition: info.hh:238
gem5::statistics::Hdf5::Hdf5
Hdf5()=delete
gem5::statistics::SparseHistInfo
Definition: info.hh:251
gem5::statistics::FormulaInfo::str
virtual std::string str() const =0
gem5::statistics::Hdf5::h5File
H5::H5File h5File
Definition: hdf5.hh:154
gem5::X86ISA::type
type
Definition: misc.hh:733
gem5::ArmISA::s
Bitfield< 4 > s
Definition: misc_types.hh:561
gem5::OutputDirectory::resolve
std::string resolve(const std::string &name) const
Returns relative file names prepended with name of this directory.
Definition: output.cc:204
gem5::statistics::initHDF5
std::unique_ptr< Output > initHDF5(const std::string &filename, unsigned chunking, bool desc, bool formulas)
Definition: hdf5.cc:328
gem5::statistics::Hdf5::addMetaData
void addMetaData(H5::DataSet &loc, const char *name, const std::vector< const char * > &values)
Helper function to add a string vector attribute to a stat.
Definition: hdf5.cc:284
gem5::GEM5_DEPRECATED_NAMESPACE
GEM5_DEPRECATED_NAMESPACE(GuestABI, guest_abi)
gem5::statistics::Vector2dInfo::y
size_type y
Definition: info.hh:235
name
const std::string & name()
Definition: trace.cc:49
gem5::statistics::Hdf5
Definition: hdf5.hh:60
gem5::statistics::VectorDistInfo
Definition: info.hh:208
gem5::statistics::Hdf5::valid
bool valid() const override
Definition: hdf5.cc:99
gem5::statistics::Info::desc
std::string desc
The description of the stat.
Definition: info.hh:90
gem5::statistics::Vector2dInfo::x
size_type x
Definition: info.hh:234
logging.hh
hdf5.hh
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40
gem5::statistics::Hdf5::timeChunk
const hsize_t timeChunk
Definition: hdf5.hh:147
gem5::statistics::Vector2dInfo::subnames
std::vector< std::string > subnames
Names and descriptions of subfields.
Definition: info.hh:230
gem5::statistics::VectorInfo::subnames
std::vector< std::string > subnames
Names and descriptions of subfields.
Definition: info.hh:188
gem5::statistics::Info::name
std::string name
The name of the stat.
Definition: info.hh:84
gem5::statistics::FormulaInfo
Definition: info.hh:245
gem5::statistics::Vector2dInfo
Definition: info.hh:226
gem5::statistics::Hdf5::beginGroup
void beginGroup(const char *name) override
Definition: hdf5.cc:106
gem5::statistics::Info
Definition: info.hh:80

Generated on Tue Sep 21 2021 12:24:58 for gem5 by doxygen 1.8.17