From a26cf3eea71454aa5cc466a2abbe01537c95c31c Mon Sep 17 00:00:00 2001 From: Zack Rusin Date: Sat, 6 Aug 2011 16:12:09 -0400 Subject: [PATCH 1/1] Add snappy compression/decompression code. --- CMakeLists.txt | 5 +- cmake/FindSnappy.cmake | 28 ++++++ trace_file.cpp | 204 +++++++++++++++++++++++++++++++++++++++++ trace_file.hpp | 40 ++++++++ 4 files changed, 276 insertions(+), 1 deletion(-) create mode 100644 cmake/FindSnappy.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index a543107..64e63bf 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,6 +23,7 @@ set (CMAKE_USE_PYTHON_VERSION 2.7 2.6) find_package (PythonInterp REQUIRED) find_package (OpenGL REQUIRED) +find_package (Snappy REQUIRED) if (ENABLE_GUI) if (NOT (ENABLE_GUI STREQUAL "AUTO")) @@ -140,6 +141,8 @@ add_subdirectory (thirdparty/zlib EXCLUDE_FROM_ALL) include_directories (${ZLIB_INCLUDE_DIRS}) link_libraries (${ZLIB_LIBRARIES}) +link_libraries(${SNAPPY_LIBRARIES}) + set (PNG_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/libpng) set (PNG_DEFINITIONS "") set (PNG_LIBRARIES png_bundled) @@ -339,7 +342,7 @@ else () ) target_link_libraries (glxtrace dl ${X11_X11_LIB}) - + install (TARGETS glxtrace LIBRARY DESTINATION lib) endif () diff --git a/cmake/FindSnappy.cmake b/cmake/FindSnappy.cmake new file mode 100644 index 0000000..4ddcd54 --- /dev/null +++ b/cmake/FindSnappy.cmake @@ -0,0 +1,28 @@ +# - Try to find Snappy +# Once done this will define +# SNAPPY_FOUND - System has Snappy +# SNAPPY_INCLUDE_DIRS - The Snappy include directories +# SNAPPY_LIBRARIES - The libraries needed to use Snappy +# SNAPPY_DEFINITIONS - Compiler switches required for using Snappy + +find_package(PkgConfig) +pkg_check_modules(PC_SNAPPY QUIET snappy) +set(SNAPPY_DEFINITIONS ${PC_SNAPPY_CFLAGS_OTHER}) + +find_path(SNAPPY_INCLUDE_DIR snappy.h + HINTS ${PC_SNAPPY_INCLUDEDIR} ${PC_SNAPPY_INCLUDE_DIRS} + PATH_SUFFIXES snappy ) + +find_library(SNAPPY_LIBRARY NAMES snappy libsnappy + HINTS ${PC_SNAPPY_LIBDIR} ${PC_SNAPPY_LIBRARY_DIRS} ) + +set(SNAPPY_LIBRARIES ${SNAPPY_LIBRARY} ) +set(SNAPPY_INCLUDE_DIRS ${SNAPPY_INCLUDE_DIR} ) + +include(FindPackageHandleStandardArgs) +# handle the QUIETLY and REQUIRED arguments and set SNAPPY_FOUND to TRUE +# if all listed variables are TRUE +find_package_handle_standard_args(snappy DEFAULT_MSG + SNAPPY_LIBRARY SNAPPY_INCLUDE_DIR) + +mark_as_advanced(SNAPPY_INCLUDE_DIR SNAPPY_LIBRARY ) diff --git a/trace_file.cpp b/trace_file.cpp index 1149978..e92eb75 100644 --- a/trace_file.cpp +++ b/trace_file.cpp @@ -1,6 +1,12 @@ #include "trace_file.hpp" +#include +#include + #include +#include + +#include using namespace Trace; @@ -15,8 +21,11 @@ File::File(const std::string &filename, } } +static size_t writtenRef = 0; + File::~File() { + std::cerr << "written ref = "< length) { + memcpy(m_cachePtr, buffer, length); + m_cachePtr += length; + } else if (freeCacheSize() == length) { + memcpy(m_cachePtr, buffer, length); + m_cachePtr += length; + flushCache(); + } else { + int sizeToWrite = length; + + while (sizeToWrite >= freeCacheSize()) { + int endSize = freeCacheSize(); + int offset = length - sizeToWrite; + memcpy(m_cachePtr, (char*)buffer + offset, endSize); + sizeToWrite -= endSize; + m_cachePtr += endSize; + flushCache(); + } + if (sizeToWrite) { + int offset = length - sizeToWrite; + memcpy(m_cachePtr, (char*)buffer + offset, sizeToWrite); + m_cachePtr += sizeToWrite; + } + } + + return true; +} + +bool SnappyFile::rawRead(void *buffer, int length) +{ + std::cerr << "Reading byte = "<< (m_cachePtr - m_cache) << std::endl; + if (freeCacheSize() > length) { + memcpy(buffer, m_cachePtr, length); + m_cachePtr += length; + } else if (freeCacheSize() == length) { + memcpy(buffer, m_cachePtr, length); + m_cachePtr += length; + assert(0); + flushCache(); + } else { + int sizeToRead = length; + int offset = 0; + assert(0); + while (sizeToRead) { + int chunkSize = std::min(freeCacheSize(), sizeToRead); + offset = length - sizeToRead; + memcpy((char*)buffer + offset, m_cachePtr, chunkSize); + m_cachePtr += chunkSize; + sizeToRead -= chunkSize; + if (sizeToRead > 0) + flushCache(); + } + } + + return true; +} + +char SnappyFile::rawGetc() +{ + char c; + rawRead(&c, 1); + return c; +} + +void SnappyFile::rawClose() +{ + flushCache(); + std::cerr << "written = "<< written + <<", comp = "<< writtenComp + << std::endl; + m_stream.close(); + delete [] m_cache; + m_cache = NULL; + m_cachePtr = NULL; +} + +void SnappyFile::rawFlush() +{ + m_stream.flush(); +} + +void SnappyFile::flushCache() +{ + if (m_mode == File::Write) { + size_t compressedLength; + + static bool first = true; + if (first) { + std::cerr << "Buffer = ["; + for (int i = 0; i < 512; ++i) { + std::cerr << i << " ) "<< (int)m_cache[i] << std::endl; + } + std::cerr << "]"<> compressedLength; + m_stream.read((char*)m_compressedCache, compressedLength); + ::snappy::GetUncompressedLength(m_compressedCache, compressedLength, + &m_cacheSize); + std::cerr << "compressed length = "< +#include namespace Trace { @@ -58,6 +59,45 @@ private: void *m_gzFile; }; +namespace snappy { + class File; +} + +#define SNAPPY_CHUNK_SIZE (1 * 1024 * 1024) +class SnappyFile : public File { +public: + SnappyFile(const std::string &filename = std::string(), + File::Mode mode = File::Read); + virtual ~SnappyFile(); + +protected: + virtual bool rawOpen(const std::string &filename, File::Mode mode); + virtual bool rawWrite(const void *buffer, int length); + virtual bool rawRead(void *buffer, int length); + virtual char rawGetc(); + virtual void rawClose(); + virtual void rawFlush(); + +private: + inline int freeCacheSize() const + { + if (m_cacheSize > 0) + return m_cacheSize - (m_cachePtr - m_cache); + else + return 0; + } + void flushCache(); + void createCache(size_t size); +private: + std::fstream m_stream; + char *m_cache; + char *m_cachePtr; + size_t m_cacheSize; + + char *m_compressedCache; +}; + + } #endif -- 2.43.0