1 /**************************************************************************
3 * Copyright 2011 Zack Rusin
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 **************************************************************************/
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.
35 * The file is composed of a number of chunks, they are:
37 * uint32 - specifying the length of the compressed data
38 * compressed data, in little endian
40 * File can contain any number of such chunks.
41 * The default size of an uncompressed chunk is specified in
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.
61 #include "trace_file.hpp"
64 #define SNAPPY_CHUNK_SIZE (1 * 1024 * 1024)
66 #define SNAPPY_BYTE1 'a'
67 #define SNAPPY_BYTE2 't'
70 using namespace trace;
73 class SnappyFile : public File {
75 SnappyFile(const std::string &filename = std::string(),
76 File::Mode mode = File::Read);
77 virtual ~SnappyFile();
79 virtual bool supportsOffsets() const;
80 virtual File::Offset currentOffset();
81 virtual void setCurrentOffset(const File::Offset &offset);
83 virtual bool rawOpen(const std::string &filename, File::Mode mode);
84 virtual bool rawWrite(const void *buffer, size_t length);
85 virtual bool rawRead(void *buffer, size_t length);
86 virtual int rawGetc();
87 virtual void rawClose();
88 virtual void rawFlush();
89 virtual bool rawSkip(size_t length);
90 virtual int rawPercentRead();
93 inline size_t usedCacheSize() const
95 assert(m_cachePtr >= m_cache);
96 return m_cachePtr - m_cache;
98 inline size_t freeCacheSize() const
100 assert(m_cacheSize >= usedCacheSize());
101 if (m_cacheSize > 0) {
102 return m_cacheSize - usedCacheSize();
107 inline bool endOfData() const
109 return m_stream.eof() && freeCacheSize() == 0;
111 void flushWriteCache();
112 void flushReadCache(size_t skipLength = 0);
113 void createCache(size_t size);
114 void writeCompressedLength(size_t length);
115 size_t readCompressedLength();
117 std::fstream m_stream;
118 size_t m_cacheMaxSize;
123 char *m_compressedCache;
125 File::Offset m_currentOffset;
126 std::streampos m_endPos;
129 SnappyFile::SnappyFile(const std::string &filename,
132 m_cacheMaxSize(SNAPPY_CHUNK_SIZE),
133 m_cacheSize(m_cacheMaxSize),
134 m_cache(new char [m_cacheMaxSize]),
137 size_t maxCompressedLength =
138 snappy::MaxCompressedLength(SNAPPY_CHUNK_SIZE);
139 m_compressedCache = new char[maxCompressedLength];
142 SnappyFile::~SnappyFile()
144 delete [] m_compressedCache;
148 bool SnappyFile::rawOpen(const std::string &filename, File::Mode mode)
150 std::ios_base::openmode fmode = std::fstream::binary;
151 if (mode == File::Write) {
152 fmode |= (std::fstream::out | std::fstream::trunc);
153 createCache(SNAPPY_CHUNK_SIZE);
154 } else if (mode == File::Read) {
155 fmode |= std::fstream::in;
158 m_stream.open(filename.c_str(), fmode);
160 //read in the initial buffer if we're reading
161 if (m_stream.is_open() && mode == File::Read) {
162 m_stream.seekg(0, std::ios::end);
163 m_endPos = m_stream.tellg();
164 m_stream.seekg(0, std::ios::beg);
166 // read the snappy file identifier
167 unsigned char byte1, byte2;
170 assert(byte1 == SNAPPY_BYTE1 && byte2 == SNAPPY_BYTE2);
173 } else if (m_stream.is_open() && mode == File::Write) {
174 // write the snappy file identifier
175 m_stream << SNAPPY_BYTE1;
176 m_stream << SNAPPY_BYTE2;
178 return m_stream.is_open();
181 bool SnappyFile::rawWrite(const void *buffer, size_t length)
183 if (freeCacheSize() > length) {
184 memcpy(m_cachePtr, buffer, length);
185 m_cachePtr += length;
186 } else if (freeCacheSize() == length) {
187 memcpy(m_cachePtr, buffer, length);
188 m_cachePtr += length;
191 size_t sizeToWrite = length;
193 while (sizeToWrite >= freeCacheSize()) {
194 size_t endSize = freeCacheSize();
195 size_t offset = length - sizeToWrite;
196 memcpy(m_cachePtr, (const char*)buffer + offset, endSize);
197 sizeToWrite -= endSize;
198 m_cachePtr += endSize;
202 size_t offset = length - sizeToWrite;
203 memcpy(m_cachePtr, (const char*)buffer + offset, sizeToWrite);
204 m_cachePtr += sizeToWrite;
211 bool SnappyFile::rawRead(void *buffer, size_t length)
217 if (freeCacheSize() >= length) {
218 memcpy(buffer, m_cachePtr, length);
219 m_cachePtr += length;
221 size_t sizeToRead = length;
224 size_t chunkSize = std::min(freeCacheSize(), sizeToRead);
225 offset = length - sizeToRead;
226 memcpy((char*)buffer + offset, m_cachePtr, chunkSize);
227 m_cachePtr += chunkSize;
228 sizeToRead -= chunkSize;
229 if (sizeToRead > 0) {
241 int SnappyFile::rawGetc()
249 void SnappyFile::rawClose()
251 if (m_mode == File::Write) {
260 void SnappyFile::rawFlush()
262 assert(m_mode == File::Write);
267 void SnappyFile::flushWriteCache()
269 size_t inputLength = usedCacheSize();
272 size_t compressedLength;
274 ::snappy::RawCompress(m_cache, inputLength,
275 m_compressedCache, &compressedLength);
277 writeCompressedLength(compressedLength);
278 m_stream.write(m_compressedCache, compressedLength);
279 m_cachePtr = m_cache;
281 assert(m_cachePtr == m_cache);
284 void SnappyFile::flushReadCache(size_t skipLength)
286 //assert(m_cachePtr == m_cache + m_cacheSize);
287 m_currentOffset.chunk = m_stream.tellg();
288 size_t compressedLength;
289 compressedLength = readCompressedLength();
291 if (compressedLength) {
292 m_stream.read((char*)m_compressedCache, compressedLength);
293 ::snappy::GetUncompressedLength(m_compressedCache, compressedLength,
295 createCache(m_cacheSize);
296 if (skipLength < m_cacheSize) {
297 ::snappy::RawUncompress(m_compressedCache, compressedLength,
305 void SnappyFile::createCache(size_t size)
307 if (size > m_cacheMaxSize) {
309 m_cacheMaxSize <<= 1;
310 } while (size > m_cacheMaxSize);
313 m_cache = new char[size];
314 m_cacheMaxSize = size;
317 m_cachePtr = m_cache;
321 void SnappyFile::writeCompressedLength(size_t length)
323 unsigned char buf[4];
324 buf[0] = length & 0xff; length >>= 8;
325 buf[1] = length & 0xff; length >>= 8;
326 buf[2] = length & 0xff; length >>= 8;
327 buf[3] = length & 0xff; length >>= 8;
329 m_stream.write((const char *)buf, sizeof buf);
332 size_t SnappyFile::readCompressedLength()
334 unsigned char buf[4];
336 m_stream.read((char *)buf, sizeof buf);
337 if (m_stream.fail()) {
340 length = (size_t)buf[0];
341 length |= ((size_t)buf[1] << 8);
342 length |= ((size_t)buf[2] << 16);
343 length |= ((size_t)buf[3] << 24);
348 bool SnappyFile::supportsOffsets() const
353 File::Offset SnappyFile::currentOffset()
355 m_currentOffset.offsetInChunk = m_cachePtr - m_cache;
356 return m_currentOffset;
359 void SnappyFile::setCurrentOffset(const File::Offset &offset)
363 // seek to the start of a chunk
364 m_stream.seekg(offset.chunk, std::ios::beg);
367 assert(m_cacheSize >= offset.offsetInChunk);
368 // seek within our cache to the correct location within the chunk
369 m_cachePtr = m_cache + offset.offsetInChunk;
373 bool SnappyFile::rawSkip(size_t length)
379 if (freeCacheSize() >= length) {
380 m_cachePtr += length;
382 size_t sizeToRead = length;
384 size_t chunkSize = std::min(freeCacheSize(), sizeToRead);
385 m_cachePtr += chunkSize;
386 sizeToRead -= chunkSize;
387 if (sizeToRead > 0) {
388 flushReadCache(sizeToRead);
399 int SnappyFile::rawPercentRead()
401 return 100 * (double(m_stream.tellg()) / double(m_endPos));
405 File* File::createSnappy(void) {
406 return new SnappyFile;
409 bool File::isSnappyCompressed(const std::string &filename)
411 std::fstream stream(filename.c_str(),
412 std::fstream::binary | std::fstream::in);
413 if (!stream.is_open())
416 unsigned char byte1, byte2;
421 return (byte1 == SNAPPY_BYTE1 && byte2 == SNAPPY_BYTE2);