1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
// -*- C++ -*-
//
// Package: CondCore/HDF5ESSource
// Class : HDF5ProductResolver
//
// Implementation:
// [Notes on implementation]
//
// Original Author: Christopher Jones
// Created: Tue, 20 Jun 2023 13:52:59 GMT
//
// system include files
#include <iostream>
#include <fstream>
#include <cassert>
#include "zlib.h"
#include "lzma.h"
// user include files
#include "HDF5ProductResolver.h"
#include "convertSyncValue.h"
#include "FWCore/Framework/interface/EventSetupRecordImpl.h"
#include "FWCore/ServiceRegistry/interface/ESModuleCallingContext.h"
#include "FWCore/ServiceRegistry/interface/ActivityRegistry.h"
#include "FWCore/Concurrency/interface/SerialTaskQueue.h"
#include "FWCore/Utilities/interface/Exception.h"
#include "FWCore/Utilities/interface/thread_safety_macros.h"
#include "h5_DataSet.h"
#include "h5_Attribute.h"
//
// constants, enums and typedefs
//
//
// static data member definitions
//
//
// constructors and destructor
//
HDF5ProductResolver::HDF5ProductResolver(edm::SerialTaskQueue* iQueue,
std::unique_ptr<cond::serialization::SerializationHelperBase> iHelper,
cms::h5::File const* iFile,
std::string const& iFileName,
cond::hdf5::Compression iCompression,
cond::hdf5::Record const* iRecord,
cond::hdf5::DataProduct const* iDataProduct)
: edm::eventsetup::ESSourceProductResolverBase(),
queue_(iQueue),
helper_(std::move(iHelper)),
file_(iFile),
fileName_(iFileName),
record_(iRecord),
dataProduct_(iDataProduct),
compression_(iCompression) {}
// HDF5ProductResolver::HDF5ProductResolver(const HDF5ProductResolver& rhs)
// {
// // do actual copying here;
// }
HDF5ProductResolver::~HDF5ProductResolver() {}
//
// member functions
//
void HDF5ProductResolver::prefetchAsyncImpl(edm::WaitingTaskHolder iTask,
edm::eventsetup::EventSetupRecordImpl const& iRecord,
edm::eventsetup::DataKey const& iKey,
edm::EventSetupImpl const*,
edm::ServiceToken const&,
edm::ESParentContext const& iParent) noexcept {
prefetchAsyncImplTemplate(
[this, iov = iRecord.validityInterval(), iParent, &iRecord](auto& iGroup, auto iActivity) {
queue_->push(iGroup, [this, &iGroup, act = std::move(iActivity), iov, iParent, &iRecord] {
CMS_SA_ALLOW try {
edm::ESModuleCallingContext context(providerDescription(),
reinterpret_cast<std::uintptr_t>(this),
edm::ESModuleCallingContext::State::kRunning,
iParent);
iRecord.activityRegistry()->preESModuleSignal_.emit(iRecord.key(), context);
struct EndGuard {
EndGuard(edm::eventsetup::EventSetupRecordImpl const& iRecord,
edm::ESModuleCallingContext const& iContext)
: record_{iRecord}, context_{iContext} {}
~EndGuard() { record_.activityRegistry()->postESModuleSignal_.emit(record_.key(), context_); }
edm::eventsetup::EventSetupRecordImpl const& record_;
edm::ESModuleCallingContext const& context_;
} guardAR(iRecord, context);
auto index = indexForInterval(iov);
readFromHDF5api(index);
iGroup.run(std::move(act));
exceptPtr_ = {};
} catch (...) {
exceptPtr_ = std::current_exception();
}
});
},
[]() { return true; },
std::move(iTask),
iRecord,
iKey,
iParent);
}
std::ptrdiff_t HDF5ProductResolver::indexForInterval(edm::ValidityInterval const& iIOV) const {
using namespace cond::hdf5;
auto firstSync = convertSyncValue(iIOV.first(), record_->iovIsRunLumi_);
auto itFound = findMatchingFirst(record_->iovFirsts_, firstSync);
assert(itFound != record_->iovFirsts_.end());
return itFound - record_->iovFirsts_.begin();
}
void HDF5ProductResolver::readFromHDF5api(std::ptrdiff_t iIndex) {
auto payloadRef = dataProduct_->payloadForIOVs_[iIndex];
auto ds = file_->derefDataSet(payloadRef);
storageSize_ = ds->storageSize();
if (storageSize_ == 0) {
return;
}
fileOffset_ = ds->fileOffset();
memSize_ = ds->findAttribute("memsize")->readUInt32();
type_ = ds->findAttribute("type")->readString();
}
void HDF5ProductResolver::prefetch(edm::eventsetup::DataKey const& iKey, edm::EventSetupRecordDetails iRecord) {
if (exceptPtr_) {
rethrow_exception(exceptPtr_);
}
if (storageSize_ == 0) {
return;
}
threadFriendlyPrefetch(fileOffset_, storageSize_, memSize_, type_);
}
std::vector<char> HDF5ProductResolver::decompress_zlib(std::vector<char> compressedBuffer, std::size_t iMemSize) const {
std::vector<char> buffer;
if (iMemSize == compressedBuffer.size()) {
//memory was not compressed
//std::cout <<"NOT COMPRESSED"<<std::endl;
buffer = std::move(compressedBuffer);
} else {
//zlib compression was used
z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
auto ret = inflateInit(&strm);
assert(ret == Z_OK);
strm.avail_in = compressedBuffer.size();
strm.next_in = reinterpret_cast<unsigned char*>(compressedBuffer.data());
buffer = std::vector<char>(iMemSize);
strm.avail_out = buffer.size();
strm.next_out = reinterpret_cast<unsigned char*>(buffer.data());
ret = inflate(&strm, Z_FINISH);
assert(ret != Z_STREAM_ERROR);
//if(ret != Z_STREAM_END) {std::cout <<"mem "<<memSize<<" "<<ret<<" out "<<strm.avail_out<<std::endl;}
assert(ret == Z_STREAM_END);
(void)inflateEnd(&strm);
}
return buffer;
}
std::vector<char> HDF5ProductResolver::decompress_lzma(std::vector<char> compressedBuffer, std::size_t iMemSize) const {
std::vector<char> buffer;
if (iMemSize == compressedBuffer.size()) {
//memory was not compressed
//std::cout <<"NOT COMPRESSED"<<std::endl;
buffer = std::move(compressedBuffer);
} else {
// code 'cribbed' from ROOT
lzma_stream stream = LZMA_STREAM_INIT;
auto returnStatus = lzma_stream_decoder(&stream, UINT64_MAX, 0U);
if (returnStatus != LZMA_OK) {
throw cms::Exception("H5CondFailedDecompress") << "failed to setup lzma";
}
stream.next_in = reinterpret_cast<uint8_t*>(compressedBuffer.data());
stream.avail_in = compressedBuffer.size();
buffer = std::vector<char>(iMemSize);
stream.next_out = reinterpret_cast<uint8_t*>(buffer.data());
stream.avail_out = buffer.size();
returnStatus = lzma_code(&stream, LZMA_FINISH);
lzma_end(&stream);
if (returnStatus != LZMA_STREAM_END) {
throw cms::Exception("H5CondFailedDecompress") << "failed to decompress buffer using lzma";
}
}
return buffer;
}
void HDF5ProductResolver::threadFriendlyPrefetch(uint64_t iFileOffset,
std::size_t iStorageSize,
std::size_t iMemSize,
const std::string& iTypeName) {
//Done interacting with the hdf5 API
//std::cout <<" prefetch "<<dataProduct_->fileOffsets_[index]<<" "<<dataProduct_->storageSizes_[index]<<" "<<memSize<<std::endl;
std::vector<char> compressedBuffer(iStorageSize);
std::fstream file(fileName_.c_str());
file.seekg(iFileOffset);
file.read(compressedBuffer.data(), compressedBuffer.size());
std::vector<char> buffer;
if (compression_ == cond::hdf5::Compression::kZLIB) {
buffer = decompress_zlib(std::move(compressedBuffer), iMemSize);
} else if (compression_ == cond::hdf5::Compression::kLZMA) {
buffer = decompress_lzma(std::move(compressedBuffer), iMemSize);
} else {
buffer = std::move(compressedBuffer);
}
std::stringbuf sBuffer;
sBuffer.pubsetbuf(&buffer[0], buffer.size());
data_ = helper_->deserialize(sBuffer, iTypeName);
if (data_.get() == nullptr) {
throw cms::Exception("H5CondFailedDeserialization")
<< "failed to deserialize: buffer size:" << buffer.size() << " type: '" << iTypeName << "'";
}
}
void HDF5ProductResolver::invalidateCache() {
ESSourceProductResolverBase::invalidateCache();
data_ = cond::serialization::unique_void_ptr();
}
//
// const member functions
//
void const* HDF5ProductResolver::getAfterPrefetchImpl() const { return data_.get(); }
//
// static member functions
//
|