]> git.cworth.org Git - apitrace/blob - trace_snappyfile.cpp
Merge branch 'master' into on-demand-loading
[apitrace] / trace_snappyfile.cpp
1 /**************************************************************************
2  *
3  * Copyright 2011 Zack Rusin
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26
27 #include "trace_snappyfile.hpp"
28
29 #include <snappy.h>
30
31 #include <iostream>
32
33 #include <assert.h>
34 #include <string.h>
35
36 using namespace Trace;
37
38 /*
39  * Snappy file format.
40  * -------------------
41  *
42  * Snappy at its core is just a compressoin algorithm so we're
43  * creating a new file format which uses snappy compression
44  * to hold the trace data.
45  *
46  * The file is composed of a number of chunks, they are:
47  * chunk {
48  *     uint32 - specifying the length of the compressed data
49  *     compressed data, in little endian
50  * }
51  * File can contain any number of such chunks.
52  * The default size of an uncompressed chunk is specified in
53  * SNAPPY_CHUNK_SIZE.
54  *
55  * Note:
56  * Currently the default size for a a to-be-compressed data is
57  * 1mb, meaning that the compressed data will be <= 1mb.
58  * The reason it's 1mb is because it seems
59  * to offer a pretty good compression/disk io speed ratio
60  * but that might change.
61  *
62  */
63
64 SnappyFile::SnappyFile(const std::string &filename,
65                               File::Mode mode)
66     : File(),
67       m_cache(0),
68       m_cachePtr(0),
69       m_cacheSize(0)
70 {
71     size_t maxCompressedLength =
72         snappy::MaxCompressedLength(SNAPPY_CHUNK_SIZE);
73     m_compressedCache = new char[maxCompressedLength];
74 }
75
76 SnappyFile::~SnappyFile()
77 {
78     delete [] m_compressedCache;
79     delete [] m_cache;
80 }
81
82 bool SnappyFile::rawOpen(const std::string &filename, File::Mode mode)
83 {
84     std::ios_base::openmode fmode = std::fstream::binary;
85     if (mode == File::Write) {
86         fmode |= (std::fstream::out | std::fstream::trunc);
87         createCache(SNAPPY_CHUNK_SIZE);
88     } else if (mode == File::Read) {
89         fmode |= std::fstream::in;
90     }
91
92     m_stream.open(filename.c_str(), fmode);
93
94     //read in the initial buffer if we're reading
95     if (m_stream.is_open() && mode == File::Read) {
96         // read the snappy file identifier
97         unsigned char byte1, byte2;
98         m_stream >> byte1;
99         m_stream >> byte2;
100         assert(byte1 == SNAPPY_BYTE1 && byte2 == SNAPPY_BYTE2);
101
102         flushReadCache();
103     } else if (m_stream.is_open() && mode == File::Write) {
104         // write the snappy file identifier
105         m_stream << SNAPPY_BYTE1;
106         m_stream << SNAPPY_BYTE2;
107     }
108     return m_stream.is_open();
109 }
110
111 bool SnappyFile::rawWrite(const void *buffer, size_t length)
112 {
113     if (freeCacheSize() > length) {
114         memcpy(m_cachePtr, buffer, length);
115         m_cachePtr += length;
116     } else if (freeCacheSize() == length) {
117         memcpy(m_cachePtr, buffer, length);
118         m_cachePtr += length;
119         flushWriteCache();
120     } else {
121         int sizeToWrite = length;
122
123         while (sizeToWrite >= freeCacheSize()) {
124             int endSize = freeCacheSize();
125             int offset = length - sizeToWrite;
126             memcpy(m_cachePtr, (const char*)buffer + offset, endSize);
127             sizeToWrite -= endSize;
128             m_cachePtr += endSize;
129             flushWriteCache();
130         }
131         if (sizeToWrite) {
132             int offset = length - sizeToWrite;
133             memcpy(m_cachePtr, (const char*)buffer + offset, sizeToWrite);
134             m_cachePtr += sizeToWrite;
135         }
136     }
137
138     return true;
139 }
140
141 bool SnappyFile::rawRead(void *buffer, size_t length)
142 {
143     if (endOfData()) {
144         return false;
145     }
146
147     if (freeCacheSize() >= length) {
148         memcpy(buffer, m_cachePtr, length);
149         m_cachePtr += length;
150     } else {
151         size_t sizeToRead = length;
152         size_t offset = 0;
153         while (sizeToRead) {
154             size_t chunkSize = std::min(freeCacheSize(), sizeToRead);
155             offset = length - sizeToRead;
156             memcpy((char*)buffer + offset, m_cachePtr, chunkSize);
157             m_cachePtr += chunkSize;
158             sizeToRead -= chunkSize;
159             if (sizeToRead > 0) {
160                 flushReadCache();
161             }
162             if (!m_cacheSize) {
163                 break;
164             }
165         }
166     }
167
168     return true;
169 }
170
171 int SnappyFile::rawGetc()
172 {
173     int c = 0;
174     if (!rawRead(&c, 1))
175         return -1;
176     return c;
177 }
178
179 void SnappyFile::rawClose()
180 {
181     if (m_mode == File::Write) {
182         flushWriteCache();
183     }
184     m_stream.close();
185     delete [] m_cache;
186     m_cache = NULL;
187     m_cachePtr = NULL;
188 }
189
190 void SnappyFile::rawFlush()
191 {
192     assert(m_mode == File::Write);
193     flushWriteCache();
194     m_stream.flush();
195 }
196
197 void SnappyFile::flushWriteCache()
198 {
199     size_t inputLength = usedCacheSize();
200
201     if (inputLength) {
202         size_t compressedLength;
203
204         ::snappy::RawCompress(m_cache, inputLength,
205                               m_compressedCache, &compressedLength);
206
207         writeCompressedLength(compressedLength);
208         m_stream.write(m_compressedCache, compressedLength);
209         m_cachePtr = m_cache;
210     }
211     assert(m_cachePtr == m_cache);
212 }
213
214 void SnappyFile::flushReadCache()
215 {
216     //assert(m_cachePtr == m_cache + m_cacheSize);
217     m_currentOffset.chunk = m_stream.tellg();
218     size_t compressedLength;
219     compressedLength = readCompressedLength();
220
221     if (compressedLength) {
222         m_stream.read((char*)m_compressedCache, compressedLength);
223         ::snappy::GetUncompressedLength(m_compressedCache, compressedLength,
224                                         &m_cacheSize);
225         createCache(m_cacheSize);
226         ::snappy::RawUncompress(m_compressedCache, compressedLength,
227                                 m_cache);
228     } else {
229         createCache(0);
230     }
231 }
232
233 void SnappyFile::createCache(size_t size)
234 {
235     // TODO: only re-allocate if the current buffer is not big enough
236
237     if (m_cache) {
238         delete [] m_cache;
239     }
240
241     if (size) {
242         m_cache = new char[size];
243     } else {
244         m_cache = NULL;
245     }
246
247     m_cachePtr = m_cache;
248     m_cacheSize = size;
249 }
250
251 void SnappyFile::writeCompressedLength(size_t length)
252 {
253     unsigned char buf[4];
254     buf[0] = length & 0xff; length >>= 8;
255     buf[1] = length & 0xff; length >>= 8;
256     buf[2] = length & 0xff; length >>= 8;
257     buf[3] = length & 0xff; length >>= 8;
258     assert(length == 0);
259     m_stream.write((const char *)buf, sizeof buf);
260 }
261
262 size_t SnappyFile::readCompressedLength()
263 {
264     unsigned char buf[4];
265     size_t length;
266     m_stream.read((char *)buf, sizeof buf);
267     if (m_stream.fail()) {
268         length = 0;
269     } else {
270         length  =  (size_t)buf[0];
271         length |= ((size_t)buf[1] <<  8);
272         length |= ((size_t)buf[2] << 16);
273         length |= ((size_t)buf[3] << 24);
274     }
275     return length;
276 }
277
278 bool SnappyFile::supportsOffsets() const
279 {
280     return true;
281 }
282
283 File::Offset SnappyFile::currentOffset()
284 {
285     m_currentOffset.offsetInChunk = m_cachePtr - m_cache;
286     return m_currentOffset;
287 }
288
289 void SnappyFile::setCurrentOffset(const File::Offset &offset)
290 {
291     // to remove eof bit
292     m_stream.clear();
293     // seek to the start of a chunk
294     m_stream.seekg(offset.chunk, std::ios::beg);
295     // load the chunk
296     flushReadCache();
297     assert(m_cacheSize >= offset.offsetInChunk);
298     // seek within our cache to the correct location within the chunk
299     m_cachePtr = m_cache + offset.offsetInChunk;
300
301 }
302
303 bool SnappyFile::rawSkip(unsigned length)
304 {
305     if (endOfData()) {
306         return false;
307     }
308
309     if (freeCacheSize() >= length) {
310         m_cachePtr += length;
311     } else {
312         size_t sizeToRead = length;
313         while (sizeToRead) {
314             size_t chunkSize = std::min(freeCacheSize(), sizeToRead);
315             m_cachePtr += chunkSize;
316             sizeToRead -= chunkSize;
317             if (sizeToRead > 0) {
318                 flushReadCache();
319             }
320             if (!m_cacheSize) {
321                 break;
322             }
323         }
324     }
325
326     return true;
327 }