]> git.cworth.org Git - apitrace/blob - trace_snappyfile.cpp
Merge remote-tracking branch 'origin/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
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 }
80
81 bool SnappyFile::rawOpen(const std::string &filename, File::Mode mode)
82 {
83     std::ios_base::openmode fmode = std::fstream::binary;
84     if (mode == File::Write) {
85         fmode |= (std::fstream::out | std::fstream::trunc);
86         createCache(SNAPPY_CHUNK_SIZE);
87     } else if (mode == File::Read) {
88         fmode |= std::fstream::in;
89     }
90
91     m_stream.open(filename.c_str(), fmode);
92
93     //read in the initial buffer if we're reading
94     if (m_stream.is_open() && mode == File::Read) {
95         // read the snappy file identifier
96         unsigned char byte1, byte2;
97         m_stream >> byte1;
98         m_stream >> byte2;
99         assert(byte1 == SNAPPY_BYTE1 && byte2 == SNAPPY_BYTE2);
100
101         flushCache();
102     } else if (m_stream.is_open() && mode == File::Write) {
103         // write the snappy file identifier
104         m_stream << SNAPPY_BYTE1;
105         m_stream << SNAPPY_BYTE2;
106     }
107     return m_stream.is_open();
108 }
109
110 bool SnappyFile::rawWrite(const void *buffer, int length)
111 {
112     if (freeCacheSize() > length) {
113         memcpy(m_cachePtr, buffer, length);
114         m_cachePtr += length;
115     } else if (freeCacheSize() == length) {
116         memcpy(m_cachePtr, buffer, length);
117         m_cachePtr += length;
118         flushCache();
119     } else {
120         int sizeToWrite = length;
121
122         while (sizeToWrite >= freeCacheSize()) {
123             int endSize = freeCacheSize();
124             int offset = length - sizeToWrite;
125             memcpy(m_cachePtr, (char*)buffer + offset, endSize);
126             sizeToWrite -= endSize;
127             m_cachePtr += endSize;
128             flushCache();
129         }
130         if (sizeToWrite) {
131             int offset = length - sizeToWrite;
132             memcpy(m_cachePtr, (char*)buffer + offset, sizeToWrite);
133             m_cachePtr += sizeToWrite;
134         }
135     }
136
137     return true;
138 }
139
140 bool SnappyFile::rawRead(void *buffer, int length)
141 {
142     if (endOfData()) {
143         return false;
144     }
145
146     if (freeCacheSize() >= length) {
147         memcpy(buffer, m_cachePtr, length);
148         m_cachePtr += length;
149     } else {
150         int sizeToRead = length;
151         int offset = 0;
152         while (sizeToRead) {
153             int chunkSize = std::min(freeCacheSize(), sizeToRead);
154             offset = length - sizeToRead;
155             memcpy((char*)buffer + offset, m_cachePtr, chunkSize);
156             m_cachePtr += chunkSize;
157             sizeToRead -= chunkSize;
158             if (sizeToRead > 0)
159                 flushCache();
160             if (!m_cacheSize)
161                 break;
162         }
163     }
164
165     return true;
166 }
167
168 int SnappyFile::rawGetc()
169 {
170     int c = 0;
171     if (!rawRead(&c, 1))
172         return -1;
173     return c;
174 }
175
176 void SnappyFile::rawClose()
177 {
178     flushCache();
179     m_stream.close();
180     delete [] m_cache;
181     m_cache = NULL;
182     m_cachePtr = NULL;
183 }
184
185 void SnappyFile::rawFlush()
186 {
187     flushCache();
188     m_stream.flush();
189 }
190
191 void SnappyFile::flushCache()
192 {
193     if (m_mode == File::Write) {
194         size_t compressedLength;
195
196         ::snappy::RawCompress(m_cache, SNAPPY_CHUNK_SIZE - freeCacheSize(),
197                               m_compressedCache, &compressedLength);
198
199         writeCompressedLength(compressedLength);
200         m_stream.write(m_compressedCache, compressedLength);
201         m_cachePtr = m_cache;
202     } else if (m_mode == File::Read) {
203         if (m_stream.eof())
204             return;
205         //assert(m_cachePtr == m_cache + m_cacheSize);
206         m_currentOffset.chunk = m_stream.tellg();
207         size_t compressedLength;
208         compressedLength = readCompressedLength();
209         m_stream.read((char*)m_compressedCache, compressedLength);
210         /*
211          * The reason we peek here is because the last read will
212          * read all the way until the last character, but that will not
213          * trigger m_stream.eof() to be set, so by calling peek
214          * we assure that if we in fact have read the entire stream
215          * then the m_stream.eof() is always set.
216          */
217         m_stream.peek();
218         ::snappy::GetUncompressedLength(m_compressedCache, compressedLength,
219                                         &m_cacheSize);
220         if (m_cache)
221             delete [] m_cache;
222         createCache(m_cacheSize);
223         ::snappy::RawUncompress(m_compressedCache, compressedLength,
224                                 m_cache);
225     }
226 }
227
228 void SnappyFile::createCache(size_t size)
229 {
230     m_cache = new char[size];
231     m_cachePtr = m_cache;
232     m_cacheSize = size;
233 }
234
235 void SnappyFile::writeCompressedLength(uint32_t value)
236 {
237     m_stream.write((const char*)&value, sizeof value);
238 }
239
240 uint32_t SnappyFile::readCompressedLength()
241 {
242     uint32_t len;
243     m_stream.read((char*)&len, sizeof len);
244     return len;
245 }
246
247 bool SnappyFile::supportsOffsets() const
248 {
249     return true;
250 }
251
252 File::Offset SnappyFile::currentOffset()
253 {
254     m_currentOffset.offsetInChunk = m_cachePtr - m_cache;
255     return m_currentOffset;
256 }
257
258 void SnappyFile::setCurrentOffset(const File::Offset &offset)
259 {
260     // to remove eof bit
261     m_stream.clear();
262     // seek to the start of a chunk
263     m_stream.seekg(offset.chunk, std::ios::beg);
264     // load the chunk
265     flushCache();
266     assert(m_cacheSize >= offset.offsetInChunk);
267     // seek within our cache to the correct location within the chunk
268     m_cachePtr = m_cache + offset.offsetInChunk;
269
270 }