ATLAS Offline Software
v5_ESCompression.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 
6 #define ZLIB_CONST
8 #include "zlib.h"
9 #include <string.h>
10 #include <boost/assert.hpp>
11 #include "ers/ers.h"
12 
13 #include <string>
14 
16 offline_EventStorage_v5::string_to_type(const std::string& type){
17 
18  if(type=="reserved"){
20  }else if(type=="unknown"){
22  }else if(type=="none"){
24  }else if(type=="zlib"){
26  }else{
28  }
29 }
30 
31 std::string
32 offline_EventStorage_v5::type_to_string(const CompressionType& type){
33 
34  switch(type){
35  case NONE:
36  return "none";
37  break;
38  case RESERVED:
39  return "reserved";
40  break;
41  case UNKNOWN:
42  return "unknown";
43  break;
44  case ZLIB:
45  return "zlib";
46  break;
47  }
48 
49  return "unknown";
50 }
51 
52 
53 namespace offline_EventStorage_v5{
54  std::string zmsg(const z_stream& strm){
55  std::string zmsg("None");
56  if(strm.msg){
57  zmsg = strm.msg;
58  }
59  return zmsg;
60  }
61 }
62 
63 void offline_EventStorage_v5::zlibcompress(offline_EventStorage_v5::CompressionBuffer& compressed,
64  uint32_t& compressedsize,
65  const uint32_t& entries,
66  const struct iovec_const* iov,
67  const uint32_t& totalsize,
68  const uint32_t& level){
69 
70  if(compressed.buffersize() < totalsize*1.1){
71  compressed.realloc(totalsize*1.1);
72  }
73 
74  z_stream strm;
75 
76  strm.zalloc = Z_NULL;
77  strm.zfree = Z_NULL;
78  strm.opaque = Z_NULL;
79  int ret = ::deflateInit(&strm, level); //level
80 
81  if(ret != Z_OK){
82  offline_EventStorage_v5::ZlibFailed issue(ERS_HERE, "Zlib init failed", ret, "none");
83  throw issue;
84  }
85 
86  uint32_t compsize = 0;
87  uint32_t available = compressed.buffersize();
88  strm.avail_out = available;
89  strm.next_out = reinterpret_cast<Bytef*>(compressed.handle());
90 
91  for (unsigned int i=0; i < entries; ++i) {
92 
93  strm.next_in = static_cast<const Bytef*>(iov[i].iov_base);
94  strm.avail_in = iov[i].iov_len;
95 
96  int flush = ((i+1)==entries) ? Z_FINISH : Z_NO_FLUSH;
97 
98  while(true){
99  ret = ::deflate(&strm, flush);
100 
101  if(ret == Z_STREAM_ERROR){
102  offline_EventStorage_v5::ZlibFailed issue(ERS_HERE, "Zlib deflate failed",
104  throw issue;
105  }
106 
107  if(strm.avail_out){
108  break;
109  }else{
110  compsize += available;
111 
112  //Double available buffer space
113  compressed.grow(2*compressed.buffersize());
114  available = compressed.buffersize()/2;
115  strm.avail_out = available;
116  strm.next_out = static_cast<Bytef*>(compressed.handle()) +available;
117  }
118  }
119  }
120 
121  compsize += (available-strm.avail_out);
122  ret = ::deflateEnd(&strm);
123 
124  if(ret != Z_OK){
125  offline_EventStorage_v5::ZlibFailed issue(ERS_HERE, "Zlib deflateEnd failed",
127  throw issue;
128  }
129 
130  //Pad @4byte with zeros:
131  uint32_t tobeadded = sizeof(uint32_t)-(compsize%sizeof(uint32_t));
132  if(tobeadded>0 && tobeadded<sizeof(uint32_t)){
133 
134  if(tobeadded>(compressed.buffersize()-compsize)){
135  //Grow buffer
136  compressed.grow(compsize+tobeadded);
137  }
138 
139  ::bzero(static_cast<Bytef*>(compressed.handle())+compsize,tobeadded);
140  compsize += tobeadded;
141  }
142 
143  compressedsize = compsize;
144 }
145 
146 void
147 offline_EventStorage_v5::zlibdecompress(offline_EventStorage_v5::CompressionBuffer& decompressed,
148  uint32_t& decompressedsize,
149  const void * compressed,
150  const uint32_t& compsize){
151 
152  uLong bufsize;
153  int ret;
154 
155  if(decompressed.buffersize() < compsize*3){
156  decompressed.realloc(compsize*3);
157  }
158 
159  while(true){
160 
161  bufsize = decompressed.buffersize();
162 
163  ret = ::uncompress(reinterpret_cast<Bytef*>(decompressed.handle()),
164  &bufsize,
165  static_cast<const Bytef*>(compressed),
166  compsize);
167 
168  if(ret == Z_OK){
169  decompressedsize = bufsize;
170  break;
171  }else if(ret == Z_DATA_ERROR){
172  ::memcpy(decompressed.handle(), compressed, compsize);
173  decompressedsize = compsize;
174 
176  issue(ERS_HERE,
177  "Zlib decompression failed with Z_DATA_ERROR",
178  ret, "none");
179  throw issue;
180 
181  }else if(ret == Z_MEM_ERROR){
182  //Not enough memory. Not much to do here!
183  ERS_LOG("Zlib decompressed failed with MEM_ERROR. Code: " << ret);
184  ERS_ASSERT(ret != Z_MEM_ERROR);
185  }else if(ret == Z_BUF_ERROR){
186  //We need a bigger buffer
187  decompressed.realloc(decompressed.buffersize()*2);
188  }
189  }
190 }
191 
NONE
@ NONE
Definition: sTGCenumeration.h:13
python.StoreID.UNKNOWN
int UNKNOWN
Definition: StoreID.py:16
v5_ESCompression.h
offline_EventStorage_v5::zmsg
std::string zmsg(const z_stream &strm)
Definition: v5_ESCompression.cxx:54
dqt_zlumi_alleff_HIST.iov
iov
Definition: dqt_zlumi_alleff_HIST.py:119
FullCPAlgorithmsTest_eljob.flush
flush
Definition: FullCPAlgorithmsTest_eljob.py:168
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
python.iconfTool.models.loaders.level
level
Definition: loaders.py:20
lumiFormat.i
int i
Definition: lumiFormat.py:92
ret
T ret(T t)
Definition: rootspy.cxx:260
offline_EventStorage_v5::ZLIB
@ ZLIB
Definition: v5_EventStorageRecords.h:41
offline_EventStorage_v5
Definition: ByteStreamDataWriterV5.h:15
offline_EventStorage_v5::RESERVED
@ RESERVED
Definition: v5_EventStorageRecords.h:41
ZlibFailed
ZlibFailed
Definition: v5_ESCompression.h:22
offline_EventStorage_v5::CompressionType
CompressionType
Definition: v5_EventStorageRecords.h:41
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
entries
double entries
Definition: listroot.cxx:49
offline_EventStorage_v5::NONE
@ NONE
Definition: v5_EventStorageRecords.h:41
offline_EventStorage_v5::UNKNOWN
@ UNKNOWN
Definition: v5_EventStorageRecords.h:41
H5Utils::defaults::deflate
const int deflate
Definition: defaults.h:10