]> git.cworth.org Git - apitrace/blob - common/trace_file_snappy.cpp
Use skiplist-based FastCallSet within trace::CallSet
[apitrace] / common / trace_file_snappy.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 /*
28  * Snappy file format.
29  * -------------------
30  *
31  * Snappy at its core is just a compressoin algorithm so we're
32  * creating a new file format which uses snappy compression
33  * to hold the trace data.
34  *
35  * The file is composed of a number of chunks, they are:
36  * chunk {
37  *     uint32 - specifying the length of the compressed data
38  *     compressed data, in little endian
39  * }
40  * File can contain any number of such chunks.
41  * The default size of an uncompressed chunk is specified in
42  * SNAPPY_CHUNK_SIZE.
43  *
44  * Note:
45  * Currently the default size for a a to-be-compressed data is
46  * 1mb, meaning that the compressed data will be <= 1mb.
47  * The reason it's 1mb is because it seems
48  * to offer a pretty good compression/disk io speed ratio
49  * but that might change.
50  *
51  */
52
53
54 #include <snappy.h>
55
56 #include <iostream>
57
58 #include <assert.h>
59 #include <string.h>
60
61 #include "trace_file.hpp"
62
63
64 #define SNAPPY_CHUNK_SIZE (1 * 1024 * 1024)
65
66
67
68 using namespace trace;
69
70
71 class SnappyFile : public File {
72 public:
73     SnappyFile(const std::string &filename = std::string(),
74                File::Mode mode = File::Read);
75     virtual ~SnappyFile();
76
77     virtual bool supportsOffsets() const;
78     virtual File::Offset currentOffset();
79     virtual void setCurrentOffset(const File::Offset &offset);
80 protected:
81     virtual bool rawOpen(const std::string &filename, File::Mode mode);
82     virtual bool rawWrite(const void *buffer, size_t length);
83     virtual size_t rawRead(void *buffer, size_t length);
84     virtual int rawGetc();
85     virtual void rawClose();
86     virtual void rawFlush();
87     virtual bool rawSkip(size_t length);
88     virtual int rawPercentRead();
89
90 private:
91     inline size_t usedCacheSize() const
92     {
93         assert(m_cachePtr >= m_cache);
94         return m_cachePtr - m_cache;
95     }
96     inline size_t freeCacheSize() const
97     {
98         assert(m_cacheSize >= usedCacheSize());
99         if (m_cacheSize > 0) {
100             return m_cacheSize - usedCacheSize();
101         } else {
102             return 0;
103         }
104     }
105     inline bool endOfData() const
106     {
107         return m_stream.eof() && freeCacheSize() == 0;
108     }
109     void flushWriteCache();
110     void flushReadCache(size_t skipLength = 0);
111     void createCache(size_t size);
112     void writeCompressedLength(size_t length);
113     size_t readCompressedLength();
114 private:
115     std::fstream m_stream;
116     size_t m_cacheMaxSize;
117     size_t m_cacheSize;
118     char *m_cache;
119     char *m_cachePtr;
120
121     char *m_compressedCache;
122
123     File::Offset m_currentOffset;
124     std::streampos m_endPos;
125 };
126
127 SnappyFile::SnappyFile(const std::string &filename,
128                               File::Mode mode)
129     : File(),
130       m_cacheMaxSize(SNAPPY_CHUNK_SIZE),
131       m_cacheSize(m_cacheMaxSize),
132       m_cache(new char [m_cacheMaxSize]),
133       m_cachePtr(m_cache)
134 {
135     size_t maxCompressedLength =
136         snappy::MaxCompressedLength(SNAPPY_CHUNK_SIZE);
137     m_compressedCache = new char[maxCompressedLength];
138 }
139
140 SnappyFile::~SnappyFile()
141 {
142     close();
143     delete [] m_compressedCache;
144     delete [] m_cache;
145 }
146
147 bool SnappyFile::rawOpen(const std::string &filename, File::Mode mode)
148 {
149     std::ios_base::openmode fmode = std::fstream::binary;
150     if (mode == File::Write) {
151         fmode |= (std::fstream::out | std::fstream::trunc);
152         createCache(SNAPPY_CHUNK_SIZE);
153     } else if (mode == File::Read) {
154         fmode |= std::fstream::in;
155     }
156
157     m_stream.open(filename.c_str(), fmode);
158
159     //read in the initial buffer if we're reading
160     if (m_stream.is_open() && mode == File::Read) {
161         m_stream.seekg(0, std::ios::end);
162         m_endPos = m_stream.tellg();
163         m_stream.seekg(0, std::ios::beg);
164
165         // read the snappy file identifier
166         unsigned char byte1, byte2;
167         m_stream >> byte1;
168         m_stream >> byte2;
169         assert(byte1 == SNAPPY_BYTE1 && byte2 == SNAPPY_BYTE2);
170
171         flushReadCache();
172     } else if (m_stream.is_open() && mode == File::Write) {
173         // write the snappy file identifier
174         m_stream << SNAPPY_BYTE1;
175         m_stream << SNAPPY_BYTE2;
176     }
177     return m_stream.is_open();
178 }
179
180 bool SnappyFile::rawWrite(const void *buffer, size_t length)
181 {
182     if (freeCacheSize() > length) {
183         memcpy(m_cachePtr, buffer, length);
184         m_cachePtr += length;
185     } else if (freeCacheSize() == length) {
186         memcpy(m_cachePtr, buffer, length);
187         m_cachePtr += length;
188         flushWriteCache();
189     } else {
190         size_t sizeToWrite = length;
191
192         while (sizeToWrite >= freeCacheSize()) {
193             size_t endSize = freeCacheSize();
194             size_t offset = length - sizeToWrite;
195             memcpy(m_cachePtr, (const char*)buffer + offset, endSize);
196             sizeToWrite -= endSize;
197             m_cachePtr += endSize;
198             flushWriteCache();
199         }
200         if (sizeToWrite) {
201             size_t offset = length - sizeToWrite;
202             memcpy(m_cachePtr, (const char*)buffer + offset, sizeToWrite);
203             m_cachePtr += sizeToWrite;
204         }
205     }
206
207     return true;
208 }
209
210 size_t SnappyFile::rawRead(void *buffer, size_t length)
211 {
212     if (endOfData()) {
213         return 0;
214     }
215
216     if (freeCacheSize() >= length) {
217         memcpy(buffer, m_cachePtr, length);
218         m_cachePtr += length;
219     } else {
220         size_t sizeToRead = length;
221         size_t offset = 0;
222         while (sizeToRead) {
223             size_t chunkSize = std::min(freeCacheSize(), sizeToRead);
224             offset = length - sizeToRead;
225             memcpy((char*)buffer + offset, m_cachePtr, chunkSize);
226             m_cachePtr += chunkSize;
227             sizeToRead -= chunkSize;
228             if (sizeToRead > 0) {
229                 flushReadCache();
230             }
231             if (!m_cacheSize) {
232                 return length - sizeToRead;
233             }
234         }
235     }
236
237     return length;
238 }
239
240 int SnappyFile::rawGetc()
241 {
242     unsigned char c = 0;
243     if (rawRead(&c, 1) != 1)
244         return -1;
245     return c;
246 }
247
248 void SnappyFile::rawClose()
249 {
250     if (m_mode == File::Write) {
251         flushWriteCache();
252     }
253     m_stream.close();
254     delete [] m_cache;
255     m_cache = NULL;
256     m_cachePtr = NULL;
257 }
258
259 void SnappyFile::rawFlush()
260 {
261     assert(m_mode == File::Write);
262     flushWriteCache();
263     m_stream.flush();
264 }
265
266 void SnappyFile::flushWriteCache()
267 {
268     size_t inputLength = usedCacheSize();
269
270     if (inputLength) {
271         size_t compressedLength;
272
273         ::snappy::RawCompress(m_cache, inputLength,
274                               m_compressedCache, &compressedLength);
275
276         writeCompressedLength(compressedLength);
277         m_stream.write(m_compressedCache, compressedLength);
278         m_cachePtr = m_cache;
279     }
280     assert(m_cachePtr == m_cache);
281 }
282
283 void SnappyFile::flushReadCache(size_t skipLength)
284 {
285     //assert(m_cachePtr == m_cache + m_cacheSize);
286     m_currentOffset.chunk = m_stream.tellg();
287     size_t compressedLength;
288     compressedLength = readCompressedLength();
289
290     if (compressedLength) {
291         m_stream.read((char*)m_compressedCache, compressedLength);
292         ::snappy::GetUncompressedLength(m_compressedCache, compressedLength,
293                                         &m_cacheSize);
294         createCache(m_cacheSize);
295         if (skipLength < m_cacheSize) {
296             ::snappy::RawUncompress(m_compressedCache, compressedLength,
297                                     m_cache);
298         }
299     } else {
300         createCache(0);
301     }
302 }
303
304 void SnappyFile::createCache(size_t size)
305 {
306     if (size > m_cacheMaxSize) {
307         do {
308             m_cacheMaxSize <<= 1;
309         } while (size > m_cacheMaxSize);
310
311         delete [] m_cache;
312         m_cache = new char[size];
313         m_cacheMaxSize = size;
314     }
315
316     m_cachePtr = m_cache;
317     m_cacheSize = size;
318 }
319
320 void SnappyFile::writeCompressedLength(size_t length)
321 {
322     unsigned char buf[4];
323     buf[0] = length & 0xff; length >>= 8;
324     buf[1] = length & 0xff; length >>= 8;
325     buf[2] = length & 0xff; length >>= 8;
326     buf[3] = length & 0xff; length >>= 8;
327     assert(length == 0);
328     m_stream.write((const char *)buf, sizeof buf);
329 }
330
331 size_t SnappyFile::readCompressedLength()
332 {
333     unsigned char buf[4];
334     size_t length;
335     m_stream.read((char *)buf, sizeof buf);
336     if (m_stream.fail()) {
337         length = 0;
338     } else {
339         length  =  (size_t)buf[0];
340         length |= ((size_t)buf[1] <<  8);
341         length |= ((size_t)buf[2] << 16);
342         length |= ((size_t)buf[3] << 24);
343     }
344     return length;
345 }
346
347 bool SnappyFile::supportsOffsets() const
348 {
349     return true;
350 }
351
352 File::Offset SnappyFile::currentOffset()
353 {
354     m_currentOffset.offsetInChunk = m_cachePtr - m_cache;
355     return m_currentOffset;
356 }
357
358 void SnappyFile::setCurrentOffset(const File::Offset &offset)
359 {
360     // to remove eof bit
361     m_stream.clear();
362     // seek to the start of a chunk
363     m_stream.seekg(offset.chunk, std::ios::beg);
364     // load the chunk
365     flushReadCache();
366     assert(m_cacheSize >= offset.offsetInChunk);
367     // seek within our cache to the correct location within the chunk
368     m_cachePtr = m_cache + offset.offsetInChunk;
369
370 }
371
372 bool SnappyFile::rawSkip(size_t length)
373 {
374     if (endOfData()) {
375         return false;
376     }
377
378     if (freeCacheSize() >= length) {
379         m_cachePtr += length;
380     } else {
381         size_t sizeToRead = length;
382         while (sizeToRead) {
383             size_t chunkSize = std::min(freeCacheSize(), sizeToRead);
384             m_cachePtr += chunkSize;
385             sizeToRead -= chunkSize;
386             if (sizeToRead > 0) {
387                 flushReadCache(sizeToRead);
388             }
389             if (!m_cacheSize) {
390                 break;
391             }
392         }
393     }
394
395     return true;
396 }
397
398 int SnappyFile::rawPercentRead()
399 {
400     return 100 * (double(m_stream.tellg()) / double(m_endPos));
401 }
402
403
404 File* File::createSnappy(void) {
405     return new SnappyFile;
406 }