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)
68 using namespace trace;
71 class SnappyFile : public File {
73 SnappyFile(const std::string &filename = std::string(),
74 File::Mode mode = File::Read);
75 virtual ~SnappyFile();
77 virtual bool supportsOffsets() const;
78 virtual File::Offset currentOffset();
79 virtual void setCurrentOffset(const File::Offset &offset);
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();
91 inline size_t usedCacheSize() const
93 assert(m_cachePtr >= m_cache);
94 return m_cachePtr - m_cache;
96 inline size_t freeCacheSize() const
98 assert(m_cacheSize >= usedCacheSize());
99 if (m_cacheSize > 0) {
100 return m_cacheSize - usedCacheSize();
105 inline bool endOfData() const
107 return m_stream.eof() && freeCacheSize() == 0;
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();
115 std::fstream m_stream;
116 size_t m_cacheMaxSize;
121 char *m_compressedCache;
123 File::Offset m_currentOffset;
124 std::streampos m_endPos;
127 SnappyFile::SnappyFile(const std::string &filename,
130 m_cacheMaxSize(SNAPPY_CHUNK_SIZE),
131 m_cacheSize(m_cacheMaxSize),
132 m_cache(new char [m_cacheMaxSize]),
135 size_t maxCompressedLength =
136 snappy::MaxCompressedLength(SNAPPY_CHUNK_SIZE);
137 m_compressedCache = new char[maxCompressedLength];
140 SnappyFile::~SnappyFile()
143 delete [] m_compressedCache;
147 bool SnappyFile::rawOpen(const std::string &filename, File::Mode mode)
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;
157 m_stream.open(filename.c_str(), fmode);
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);
165 // read the snappy file identifier
166 unsigned char byte1, byte2;
169 assert(byte1 == SNAPPY_BYTE1 && byte2 == SNAPPY_BYTE2);
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;
177 return m_stream.is_open();
180 bool SnappyFile::rawWrite(const void *buffer, size_t length)
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;
190 size_t sizeToWrite = length;
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;
201 size_t offset = length - sizeToWrite;
202 memcpy(m_cachePtr, (const char*)buffer + offset, sizeToWrite);
203 m_cachePtr += sizeToWrite;
210 size_t SnappyFile::rawRead(void *buffer, size_t length)
216 if (freeCacheSize() >= length) {
217 memcpy(buffer, m_cachePtr, length);
218 m_cachePtr += length;
220 size_t sizeToRead = length;
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) {
232 return length - sizeToRead;
240 int SnappyFile::rawGetc()
243 if (rawRead(&c, 1) != 1)
248 void SnappyFile::rawClose()
250 if (m_mode == File::Write) {
259 void SnappyFile::rawFlush()
261 assert(m_mode == File::Write);
266 void SnappyFile::flushWriteCache()
268 size_t inputLength = usedCacheSize();
271 size_t compressedLength;
273 ::snappy::RawCompress(m_cache, inputLength,
274 m_compressedCache, &compressedLength);
276 writeCompressedLength(compressedLength);
277 m_stream.write(m_compressedCache, compressedLength);
278 m_cachePtr = m_cache;
280 assert(m_cachePtr == m_cache);
283 void SnappyFile::flushReadCache(size_t skipLength)
285 //assert(m_cachePtr == m_cache + m_cacheSize);
286 m_currentOffset.chunk = m_stream.tellg();
287 size_t compressedLength;
288 compressedLength = readCompressedLength();
290 if (compressedLength) {
291 m_stream.read((char*)m_compressedCache, compressedLength);
292 ::snappy::GetUncompressedLength(m_compressedCache, compressedLength,
294 createCache(m_cacheSize);
295 if (skipLength < m_cacheSize) {
296 ::snappy::RawUncompress(m_compressedCache, compressedLength,
304 void SnappyFile::createCache(size_t size)
306 if (size > m_cacheMaxSize) {
308 m_cacheMaxSize <<= 1;
309 } while (size > m_cacheMaxSize);
312 m_cache = new char[size];
313 m_cacheMaxSize = size;
316 m_cachePtr = m_cache;
320 void SnappyFile::writeCompressedLength(size_t length)
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;
328 m_stream.write((const char *)buf, sizeof buf);
331 size_t SnappyFile::readCompressedLength()
333 unsigned char buf[4];
335 m_stream.read((char *)buf, sizeof buf);
336 if (m_stream.fail()) {
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);
347 bool SnappyFile::supportsOffsets() const
352 File::Offset SnappyFile::currentOffset()
354 m_currentOffset.offsetInChunk = m_cachePtr - m_cache;
355 return m_currentOffset;
358 void SnappyFile::setCurrentOffset(const File::Offset &offset)
362 // seek to the start of a chunk
363 m_stream.seekg(offset.chunk, std::ios::beg);
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;
372 bool SnappyFile::rawSkip(size_t length)
378 if (freeCacheSize() >= length) {
379 m_cachePtr += length;
381 size_t sizeToRead = length;
383 size_t chunkSize = std::min(freeCacheSize(), sizeToRead);
384 m_cachePtr += chunkSize;
385 sizeToRead -= chunkSize;
386 if (sizeToRead > 0) {
387 flushReadCache(sizeToRead);
398 int SnappyFile::rawPercentRead()
400 return 100 * (double(m_stream.tellg()) / double(m_endPos));
404 File* File::createSnappy(void) {
405 return new SnappyFile;