Chris@87: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@87: Chris@87: /* Chris@87: Sonic Visualiser Chris@87: An audio file viewer and annotation editor. Chris@87: Centre for Digital Music, Queen Mary, University of London. Chris@87: This file copyright 2006 Chris Cannam. Chris@87: Chris@87: This program is free software; you can redistribute it and/or Chris@87: modify it under the terms of the GNU General Public License as Chris@87: published by the Free Software Foundation; either version 2 of the Chris@87: License, or (at your option) any later version. See the file Chris@87: COPYING included with this distribution for more information. Chris@87: */ Chris@87: Chris@87: #include "MatrixFileCache.h" Chris@87: #include "base/TempDirectory.h" Chris@90: #include "base/System.h" Chris@87: Chris@87: #include Chris@87: #include Chris@87: #include Chris@87: #include Chris@87: Chris@87: #include Chris@87: Chris@87: #include Chris@87: Chris@87: #include Chris@87: #include Chris@87: Chris@91: std::map MatrixFileCache::m_refcount; Chris@91: QMutex MatrixFileCache::m_refcountMutex; Chris@91: Chris@87: MatrixFileCache::MatrixFileCache(QString fileBase, Mode mode) : Chris@87: m_fd(-1), Chris@87: m_mode(mode), Chris@87: m_width(0), Chris@87: m_height(0), Chris@90: m_headerSize(2 * sizeof(size_t)), Chris@95: m_defaultCacheWidth(2048), Chris@95: m_prevX(0), Chris@95: m_requestToken(-1) Chris@87: { Chris@95: m_cache.data = 0; Chris@90: Chris@87: QDir tempDir(TempDirectory::instance()->getPath()); Chris@87: QString fileName(tempDir.filePath(QString("%1.mfc").arg(fileBase))); Chris@87: bool newFile = !QFileInfo(fileName).exists(); Chris@87: Chris@87: if (newFile && mode == ReadOnly) { Chris@87: std::cerr << "ERROR: MatrixFileCache::MatrixFileCache: Read-only mode " Chris@87: << "specified, but cache file does not exist" << std::endl; Chris@87: return; Chris@87: } Chris@87: Chris@87: int flags = 0; Chris@87: mode_t fmode = S_IRUSR | S_IWUSR; Chris@87: Chris@87: if (mode == ReadWrite) { Chris@87: flags = O_RDWR | O_CREAT; Chris@87: } else { Chris@87: flags = O_RDONLY; Chris@87: } Chris@87: Chris@90: if ((m_fd = ::open(fileName.toLocal8Bit(), flags, fmode)) < 0) { Chris@87: ::perror("Open failed"); Chris@87: std::cerr << "ERROR: MatrixFileCache::MatrixFileCache: " Chris@87: << "Failed to open cache file \"" Chris@87: << fileName.toStdString() << "\""; Chris@87: if (mode == ReadWrite) std::cerr << " for writing"; Chris@87: std::cerr << std::endl; Chris@91: return; Chris@87: } Chris@87: Chris@87: if (newFile) { Chris@87: resize(0, 0); // write header Chris@87: } else { Chris@87: size_t header[2]; Chris@90: if (::read(m_fd, header, 2 * sizeof(size_t)) < 0) { Chris@87: perror("Read failed"); Chris@87: std::cerr << "ERROR: MatrixFileCache::MatrixFileCache: " Chris@90: << "Failed to read header (fd " << m_fd << ", file \"" Chris@90: << fileName.toStdString() << "\")" << std::endl; Chris@87: return; Chris@87: } Chris@87: m_width = header[0]; Chris@87: m_height = header[1]; Chris@87: seekTo(0, 0); Chris@87: } Chris@87: Chris@91: m_fileName = fileName; Chris@95: Chris@95: //!!! why isn't this signal being delivered? Chris@95: connect(&m_readThread, SIGNAL(cancelled(int)), Chris@95: this, SLOT(requestCancelled(int))); Chris@95: Chris@95: m_readThread.start(); Chris@95: Chris@91: QMutexLocker locker(&m_refcountMutex); Chris@91: ++m_refcount[fileName]; Chris@91: Chris@90: std::cerr << "MatrixFileCache::MatrixFileCache: Done, size is " << "(" << m_width << ", " << m_height << ")" << std::endl; Chris@87: Chris@87: } Chris@87: Chris@87: MatrixFileCache::~MatrixFileCache() Chris@87: { Chris@95: float *requestData = 0; Chris@95: Chris@95: if (m_requestToken >= 0) { Chris@95: FileReadThread::Request request; Chris@95: if (m_readThread.getRequest(m_requestToken, request)) { Chris@95: requestData = (float *)request.data; Chris@95: } Chris@90: } Chris@90: Chris@95: m_readThread.finish(); Chris@95: m_readThread.wait(); Chris@95: Chris@95: if (requestData) delete[] requestData; Chris@95: if (m_cache.data) delete[] m_cache.data; Chris@95: Chris@87: if (m_fd >= 0) { Chris@87: if (::close(m_fd) < 0) { Chris@87: ::perror("MatrixFileCache::~MatrixFileCache: close failed"); Chris@87: } Chris@87: } Chris@90: Chris@91: if (m_fileName != "") { Chris@91: QMutexLocker locker(&m_refcountMutex); Chris@91: if (--m_refcount[m_fileName] == 0) { Chris@95: if (::unlink(m_fileName.toLocal8Bit())) { Chris@95: ::perror("Unlink failed"); Chris@91: std::cerr << "WARNING: MatrixFileCache::~MatrixFileCache: reference count reached 0, but failed to unlink file \"" << m_fileName.toStdString() << "\"" << std::endl; Chris@91: } else { Chris@91: std::cerr << "deleted " << m_fileName.toStdString() << std::endl; Chris@91: } Chris@91: } Chris@91: } Chris@87: } Chris@87: Chris@87: size_t Chris@87: MatrixFileCache::getWidth() const Chris@87: { Chris@87: return m_width; Chris@87: } Chris@87: Chris@87: size_t Chris@87: MatrixFileCache::getHeight() const Chris@87: { Chris@87: return m_height; Chris@87: } Chris@87: Chris@87: void Chris@87: MatrixFileCache::resize(size_t w, size_t h) Chris@87: { Chris@87: if (m_mode != ReadWrite) { Chris@87: std::cerr << "ERROR: MatrixFileCache::resize called on read-only cache" Chris@87: << std::endl; Chris@87: return; Chris@87: } Chris@87: Chris@95: QMutexLocker locker(&m_fdMutex); Chris@95: Chris@87: off_t off = m_headerSize + (w * h * sizeof(float)); Chris@87: Chris@87: if (w * h > m_width * m_height) { Chris@87: Chris@95: if (::lseek(m_fd, off - sizeof(float), SEEK_SET) == (off_t)-1) { Chris@95: ::perror("Seek failed"); Chris@95: std::cerr << "ERROR: MatrixFileCache::resize(" << w << ", " Chris@95: << h << "): seek failed, cannot resize" << std::endl; Chris@95: return; Chris@95: } Chris@95: Chris@95: // guess this requires efficient support for sparse files Chris@90: Chris@95: float f(0); Chris@95: if (::write(m_fd, &f, sizeof(float)) != sizeof(float)) { Chris@95: ::perror("WARNING: MatrixFileCache::resize: write failed"); Chris@87: } Chris@95: Chris@87: } else { Chris@87: Chris@87: if (::ftruncate(m_fd, off) < 0) { Chris@90: ::perror("WARNING: MatrixFileCache::resize: ftruncate failed"); Chris@87: } Chris@87: } Chris@87: Chris@87: m_width = 0; Chris@87: m_height = 0; Chris@87: Chris@87: if (::lseek(m_fd, 0, SEEK_SET) == (off_t)-1) { Chris@87: ::perror("ERROR: MatrixFileCache::resize: Seek to write header failed"); Chris@87: return; Chris@87: } Chris@87: Chris@87: size_t header[2]; Chris@87: header[0] = w; Chris@87: header[1] = h; Chris@87: if (::write(m_fd, header, 2 * sizeof(size_t)) != 2 * sizeof(size_t)) { Chris@87: ::perror("ERROR: MatrixFileCache::resize: Failed to write header"); Chris@87: return; Chris@87: } Chris@87: Chris@87: m_width = w; Chris@87: m_height = h; Chris@87: Chris@87: seekTo(0, 0); Chris@87: } Chris@87: Chris@87: void Chris@87: MatrixFileCache::reset() Chris@87: { Chris@87: if (m_mode != ReadWrite) { Chris@87: std::cerr << "ERROR: MatrixFileCache::reset called on read-only cache" Chris@87: << std::endl; Chris@87: return; Chris@87: } Chris@87: Chris@95: QMutexLocker locker(&m_fdMutex); Chris@95: Chris@90: float *emptyCol = new float[m_height]; Chris@90: for (size_t y = 0; y < m_height; ++y) emptyCol[y] = 0.f; Chris@90: Chris@90: seekTo(0, 0); Chris@90: for (size_t x = 0; x < m_width; ++x) setColumnAt(x, emptyCol); Chris@90: Chris@90: delete[] emptyCol; Chris@87: } Chris@87: Chris@95: float Chris@95: MatrixFileCache::getValueAt(size_t x, size_t y) Chris@87: { Chris@95: float value = 0.f; Chris@95: if (getValuesFromCache(x, y, 1, &value)) return value; Chris@90: Chris@95: ssize_t r = 0; Chris@87: Chris@95: // std::cout << "MatrixFileCache::getValueAt(" << x << ", " << y << ")" Chris@95: // << ": reading the slow way" << std::endl; Chris@95: Chris@95: m_fdMutex.lock(); Chris@95: Chris@95: if (seekTo(x, y)) { Chris@95: r = ::read(m_fd, &value, sizeof(float)); Chris@87: } Chris@87: Chris@95: m_fdMutex.unlock(); Chris@95: Chris@91: if (r < 0) { Chris@91: ::perror("MatrixFileCache::getValueAt: Read failed"); Chris@91: } Chris@90: if (r != sizeof(float)) { Chris@90: value = 0.f; Chris@87: } Chris@95: Chris@87: return value; Chris@87: } Chris@87: Chris@87: void Chris@95: MatrixFileCache::getColumnAt(size_t x, float *values) Chris@87: { Chris@95: if (getValuesFromCache(x, 0, m_height, values)) return; Chris@95: Chris@95: ssize_t r = 0; Chris@95: Chris@95: std::cout << "MatrixFileCache::getColumnAt(" << x << ")" Chris@95: << ": reading the slow way" << std::endl; Chris@95: Chris@95: m_fdMutex.lock(); Chris@95: Chris@95: if (seekTo(x, 0)) { Chris@95: r = ::read(m_fd, values, m_height * sizeof(float)); Chris@87: } Chris@87: Chris@95: m_fdMutex.unlock(); Chris@95: Chris@91: if (r < 0) { Chris@87: ::perror("MatrixFileCache::getColumnAt: read failed"); Chris@87: } Chris@95: } Chris@95: Chris@95: bool Chris@95: MatrixFileCache::getValuesFromCache(size_t x, size_t ystart, size_t ycount, Chris@95: float *values) Chris@95: { Chris@95: m_cacheMutex.lock(); Chris@95: Chris@95: if (!m_cache.data || x < m_cache.x || x >= m_cache.x + m_cache.width) { Chris@95: bool left = (m_cache.data && x < m_cache.x); Chris@95: m_cacheMutex.unlock(); Chris@95: primeCache(x, left); // this doesn't take effect until a later callback Chris@95: m_prevX = x; Chris@95: return false; Chris@95: } Chris@95: Chris@95: for (size_t y = ystart; y < ystart + ycount; ++y) { Chris@95: values[y - ystart] = m_cache.data[(x - m_cache.x) * m_height + y]; Chris@95: } Chris@95: m_cacheMutex.unlock(); Chris@95: Chris@95: if (m_cache.x > 0 && x < m_prevX && x < m_cache.x + m_cache.width/4) { Chris@95: primeCache(x, true); Chris@95: } Chris@95: Chris@95: if (m_cache.x + m_cache.width < m_width && Chris@95: x > m_prevX && Chris@95: x > m_cache.x + (m_cache.width * 3) / 4) { Chris@95: primeCache(x, false); Chris@95: } Chris@95: Chris@95: m_prevX = x; Chris@95: return true; Chris@87: } Chris@87: Chris@87: void Chris@87: MatrixFileCache::setValueAt(size_t x, size_t y, float value) Chris@87: { Chris@87: if (m_mode != ReadWrite) { Chris@87: std::cerr << "ERROR: MatrixFileCache::setValueAt called on read-only cache" Chris@87: << std::endl; Chris@87: return; Chris@87: } Chris@87: Chris@95: ssize_t w = 0; Chris@95: bool seekFailed = false; Chris@95: Chris@95: m_fdMutex.lock(); Chris@95: Chris@95: if (seekTo(x, y)) { Chris@95: w = ::write(m_fd, &value, sizeof(float)); Chris@95: } else { Chris@95: seekFailed = true; Chris@95: } Chris@95: Chris@95: m_fdMutex.unlock(); Chris@95: Chris@95: if (!seekFailed && w != sizeof(float)) { Chris@87: ::perror("WARNING: MatrixFileCache::setValueAt: write failed"); Chris@87: } Chris@87: Chris@95: //... update cache as appropriate Chris@87: } Chris@87: Chris@87: void Chris@87: MatrixFileCache::setColumnAt(size_t x, float *values) Chris@87: { Chris@87: if (m_mode != ReadWrite) { Chris@87: std::cerr << "ERROR: MatrixFileCache::setColumnAt called on read-only cache" Chris@87: << std::endl; Chris@87: return; Chris@87: } Chris@87: Chris@95: ssize_t w = 0; Chris@95: bool seekFailed = false; Chris@95: Chris@95: m_fdMutex.lock(); Chris@95: Chris@95: if (seekTo(x, 0)) { Chris@95: w = ::write(m_fd, values, m_height * sizeof(float)); Chris@95: } else { Chris@95: seekFailed = true; Chris@95: } Chris@95: Chris@95: m_fdMutex.unlock(); Chris@95: Chris@95: if (!seekFailed && w != ssize_t(m_height * sizeof(float))) { Chris@87: ::perror("WARNING: MatrixFileCache::setColumnAt: write failed"); Chris@87: } Chris@87: Chris@95: //... update cache as appropriate Chris@90: } Chris@90: Chris@95: void Chris@95: MatrixFileCache::primeCache(size_t x, bool goingLeft) Chris@90: { Chris@95: // std::cerr << "MatrixFileCache::primeCache(" << x << ", " << goingLeft << ")" << std::endl; Chris@90: Chris@95: size_t rx = x; Chris@95: size_t rw = m_defaultCacheWidth; Chris@90: Chris@95: size_t left = rw / 3; Chris@95: if (goingLeft) left = (rw * 2) / 3; Chris@95: Chris@95: if (rx > left) rx -= left; Chris@95: else rx = 0; Chris@95: Chris@95: if (rx + rw > m_width) rw = m_width - rx; Chris@95: Chris@95: QMutexLocker locker(&m_cacheMutex); Chris@95: Chris@95: if (m_requestToken >= 0) { Chris@95: Chris@95: if (x >= m_requestingX && Chris@95: x < m_requestingX + m_requestingWidth) { Chris@95: Chris@95: if (m_readThread.isReady(m_requestToken)) { Chris@95: Chris@95: std::cerr << "last request is ready! (" << m_requestingX << ", "<< m_requestingWidth << ")" << std::endl; Chris@95: Chris@95: FileReadThread::Request request; Chris@95: if (m_readThread.getRequest(m_requestToken, request)) { Chris@95: Chris@95: m_cache.x = (request.start - m_headerSize) / (m_height * sizeof(float)); Chris@95: m_cache.width = request.size / (m_height * sizeof(float)); Chris@95: Chris@95: std::cerr << "actual: " << m_cache.x << ", " << m_cache.width << std::endl; Chris@95: Chris@95: if (m_cache.data) delete[] m_cache.data; Chris@95: m_cache.data = (float *)request.data; Chris@95: } Chris@95: Chris@95: m_readThread.done(m_requestToken); Chris@95: m_requestToken = -1; Chris@95: } Chris@95: Chris@95: return; Chris@95: } Chris@95: Chris@95: std::cerr << "cancelling last request" << std::endl; Chris@95: m_readThread.cancel(m_requestToken); Chris@95: //!!! Chris@95: m_requestToken = -1; Chris@95: } Chris@95: Chris@95: FileReadThread::Request request; Chris@95: request.fd = m_fd; Chris@95: request.mutex = &m_fdMutex; Chris@95: request.start = m_headerSize + rx * m_height * sizeof(float); Chris@95: request.size = rw * m_height * sizeof(float); Chris@95: request.data = (char *)(new float[rw * m_height]); Chris@95: Chris@95: m_requestingX = rx; Chris@95: m_requestingWidth = rw; Chris@95: Chris@95: int token = m_readThread.request(request); Chris@95: std::cerr << "MatrixFileCache::primeCache: request token is " Chris@95: << token << std::endl; Chris@95: Chris@95: m_requestToken = token; Chris@95: } Chris@95: Chris@95: void Chris@95: MatrixFileCache::requestCancelled(int token) Chris@95: { Chris@95: std::cerr << "MatrixFileCache::requestCancelled(" << token << ")" << std::endl; Chris@95: Chris@95: FileReadThread::Request request; Chris@95: if (m_readThread.getRequest(token, request)) { Chris@95: delete[] ((float *)request.data); Chris@95: m_readThread.done(token); Chris@95: } Chris@90: } Chris@90: Chris@90: bool Chris@95: MatrixFileCache::seekTo(size_t x, size_t y) Chris@87: { Chris@87: off_t off = m_headerSize + (x * m_height + y) * sizeof(float); Chris@90: Chris@87: if (::lseek(m_fd, off, SEEK_SET) == (off_t)-1) { Chris@87: ::perror("Seek failed"); Chris@87: std::cerr << "ERROR: MatrixFileCache::seekTo(" << x << ", " << y Chris@87: << ") failed" << std::endl; Chris@87: return false; Chris@87: } Chris@87: Chris@87: return true; Chris@87: } Chris@87: