]> git.cworth.org Git - apitrace/blob - trace_snappyfile.cpp
Split build instructions. Document advanced usage.
[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 <assert.h>
32 #include <string.h>
33
34 using namespace Trace;
35
36 /*
37  * Snappy file format.
38  * -------------------
39  *
40  * Snappy at its core is just a compressoin algorithm so we're
41  * creating a new file format which uses snappy compression
42  * to hold the trace data.
43  *
44  * The file is composed of a number of chunks, they are:
45  * chunk {
46  *     uint32 - specifying the length of the compressed data
47  *     compressed data, in little endian
48  * }
49  * File can contain any number of such chunks.
50  * The default size of an uncompressed chunk is specified in
51  * SNAPPY_CHUNK_SIZE.
52  *
53  * Note:
54  * Currently the default size for a a to-be-compressed data is
55  * 1mb, meaning that the compressed data will be <= 1mb.
56  * The reason it's 1mb is because it seems
57  * to offer a pretty good compression/disk io speed ratio
58  * but that might change.
59  *
60  */
61
62 SnappyFile::SnappyFile(const std::string &filename,
63                               File::Mode mode)
64     : File(),
65       m_cache(0),
66       m_cachePtr(0),
67       m_cacheSize(0)
68 {
69     size_t maxCompressedLength =
70         snappy::MaxCompressedLength(SNAPPY_CHUNK_SIZE);
71     m_compressedCache = new char[maxCompressedLength];
72 }
73
74 SnappyFile::~SnappyFile()
75 {
76     delete [] m_compressedCache;
77     delete [] m_cache;
78 }
79
80 bool SnappyFile::rawOpen(const std::string &filename, File::Mode mode)
81 {
82     std::ios_base::openmode fmode = std::fstream::binary;
83     if (mode == File::Write) {
84         fmode |= (std::fstream::out | std::fstream::trunc);
85         createCache(SNAPPY_CHUNK_SIZE);
86     } else if (mode == File::Read) {
87         fmode |= std::fstream::in;
88     }
89
90     m_stream.open(filename.c_str(), fmode);
91
92     //read in the initial buffer if we're reading
93     if (m_stream.is_open() && mode == File::Read) {
94         // read the snappy file identifier
95         unsigned char byte1, byte2;
96         m_stream >> byte1;
97         m_stream >> byte2;
98         assert(byte1 == SNAPPY_BYTE1 && byte2 == SNAPPY_BYTE2);
99
100         flushReadCache();
101     } else if (m_stream.is_open() && mode == File::Write) {
102         // write the snappy file identifier
103         m_stream << SNAPPY_BYTE1;
104         m_stream << SNAPPY_BYTE2;
105     }
106     return m_stream.is_open();
107 }
108
109 bool SnappyFile::rawWrite(const void *buffer, size_t length)
110 {
111     if (freeCacheSize() > length) {
112         memcpy(m_cachePtr, buffer, length);
113         m_cachePtr += length;
114     } else if (freeCacheSize() == length) {
115         memcpy(m_cachePtr, buffer, length);
116         m_cachePtr += length;
117         flushWriteCache();
118     } else {
119         int sizeToWrite = length;
120
121         while (sizeToWrite >= freeCacheSize()) {
122             int endSize = freeCacheSize();
123             int offset = length - sizeToWrite;
124             memcpy(m_cachePtr, (const char*)buffer + offset, endSize);
125             sizeToWrite -= endSize;
126             m_cachePtr += endSize;
127             flushWriteCache();
128         }
129         if (sizeToWrite) {
130             int offset = length - sizeToWrite;
131             memcpy(m_cachePtr, (const char*)buffer + offset, sizeToWrite);
132             m_cachePtr += sizeToWrite;
133         }
134     }
135
136     return true;
137 }
138
139 bool SnappyFile::rawRead(void *buffer, size_t length)
140 {
141     if (endOfData()) {
142         return false;
143     }
144
145     if (freeCacheSize() >= length) {
146         memcpy(buffer, m_cachePtr, length);
147         m_cachePtr += length;
148     } else {
149         size_t sizeToRead = length;
150         size_t offset = 0;
151         while (sizeToRead) {
152             size_t chunkSize = std::min(freeCacheSize(), sizeToRead);
153             offset = length - sizeToRead;
154             memcpy((char*)buffer + offset, m_cachePtr, chunkSize);
155             m_cachePtr += chunkSize;
156             sizeToRead -= chunkSize;
157             if (sizeToRead > 0) {
158                 flushReadCache();
159             }
160             if (!m_cacheSize) {
161                 break;
162             }
163         }
164     }
165
166     return true;
167 }
168
169 int SnappyFile::rawGetc()
170 {
171     int c = 0;
172     if (!rawRead(&c, 1))
173         return -1;
174     return c;
175 }
176
177 void SnappyFile::rawClose()
178 {
179     if (m_mode == File::Write) {
180         flushWriteCache();
181     }
182     m_stream.close();
183     delete [] m_cache;
184     m_cache = NULL;
185     m_cachePtr = NULL;
186 }
187
188 void SnappyFile::rawFlush()
189 {
190     assert(m_mode == File::Write);
191     flushWriteCache();
192     m_stream.flush();
193 }
194
195 void SnappyFile::flushWriteCache()
196 {
197     size_t inputLength = usedCacheSize();
198
199     if (inputLength) {
200         size_t compressedLength;
201
202         ::snappy::RawCompress(m_cache, inputLength,
203                               m_compressedCache, &compressedLength);
204
205         writeCompressedLength(compressedLength);
206         m_stream.write(m_compressedCache, compressedLength);
207         m_cachePtr = m_cache;
208     }
209     assert(m_cachePtr == m_cache);
210 }
211
212 void SnappyFile::flushReadCache()
213 {
214     //assert(m_cachePtr == m_cache + m_cacheSize);
215     size_t compressedLength;
216     compressedLength = readCompressedLength();
217
218     if (compressedLength) {
219         m_stream.read((char*)m_compressedCache, compressedLength);
220         ::snappy::GetUncompressedLength(m_compressedCache, compressedLength,
221                                         &m_cacheSize);
222         createCache(m_cacheSize);
223         ::snappy::RawUncompress(m_compressedCache, compressedLength,
224                                 m_cache);
225     } else {
226         createCache(0);
227     }
228 }
229
230 void SnappyFile::createCache(size_t size)
231 {
232     // TODO: only re-allocate if the current buffer is not big enough
233
234     if (m_cache) {
235         delete [] m_cache;
236     }
237
238     if (size) {
239         m_cache = new char[size];
240     } else {
241         m_cache = NULL;
242     }
243
244     m_cachePtr = m_cache;
245     m_cacheSize = size;
246 }
247
248 void SnappyFile::writeCompressedLength(size_t length)
249 {
250     unsigned char buf[4];
251     buf[0] = length & 0xff; length >>= 8;
252     buf[1] = length & 0xff; length >>= 8;
253     buf[2] = length & 0xff; length >>= 8;
254     buf[3] = length & 0xff; length >>= 8;
255     assert(length == 0);
256     m_stream.write((const char *)buf, sizeof buf);
257 }
258
259 size_t SnappyFile::readCompressedLength()
260 {
261     unsigned char buf[4];
262     size_t length;
263     m_stream.read((char *)buf, sizeof buf);
264     if (m_stream.fail()) {
265         length = 0;
266     } else {
267         length  =  (size_t)buf[0];
268         length |= ((size_t)buf[1] <<  8);
269         length |= ((size_t)buf[2] << 16);
270         length |= ((size_t)buf[3] << 24);
271     }
272     return length;
273 }