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@96: #include "MatrixFile.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@96: std::map MatrixFile::m_refcount; Chris@96: QMutex MatrixFile::m_refcountMutex; Chris@91: Chris@96: MatrixFile::MatrixFile(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@97: m_defaultCacheWidth(512), Chris@95: m_prevX(0), Chris@97: m_requestToken(-1), Chris@97: m_spareData(0) 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@96: std::cerr << "ERROR: MatrixFile::MatrixFile: 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@96: std::cerr << "ERROR: MatrixFile::MatrixFile: " 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@96: std::cerr << "ERROR: MatrixFile::MatrixFile: " 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: m_readThread.start(); Chris@95: Chris@91: QMutexLocker locker(&m_refcountMutex); Chris@91: ++m_refcount[fileName]; Chris@91: Chris@96: std::cerr << "MatrixFile::MatrixFile: Done, size is " << "(" << m_width << ", " << m_height << ")" << std::endl; Chris@87: Chris@87: } Chris@87: Chris@96: MatrixFile::~MatrixFile() 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@97: if (requestData) free(requestData); Chris@97: if (m_cache.data) free(m_cache.data); Chris@97: if (m_spareData) free(m_spareData); Chris@95: Chris@87: if (m_fd >= 0) { Chris@87: if (::close(m_fd) < 0) { Chris@96: ::perror("MatrixFile::~MatrixFile: 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@96: std::cerr << "WARNING: MatrixFile::~MatrixFile: 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@96: MatrixFile::getWidth() const Chris@87: { Chris@87: return m_width; Chris@87: } Chris@87: Chris@87: size_t Chris@96: MatrixFile::getHeight() const Chris@87: { Chris@87: return m_height; Chris@87: } Chris@87: Chris@87: void Chris@96: MatrixFile::resize(size_t w, size_t h) Chris@87: { Chris@87: if (m_mode != ReadWrite) { Chris@96: std::cerr << "ERROR: MatrixFile::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@96: std::cerr << "ERROR: MatrixFile::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@96: ::perror("WARNING: MatrixFile::resize: write failed"); Chris@87: } Chris@95: Chris@87: } else { Chris@87: Chris@87: if (::ftruncate(m_fd, off) < 0) { Chris@96: ::perror("WARNING: MatrixFile::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@96: ::perror("ERROR: MatrixFile::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@96: ::perror("ERROR: MatrixFile::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@96: MatrixFile::reset() Chris@87: { Chris@87: if (m_mode != ReadWrite) { Chris@96: std::cerr << "ERROR: MatrixFile::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@96: MatrixFile::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@96: // std::cout << "MatrixFile::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@96: ::perror("MatrixFile::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@96: MatrixFile::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@96: std::cout << "MatrixFile::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@96: ::perror("MatrixFile::getColumnAt: read failed"); Chris@87: } Chris@95: } Chris@95: Chris@95: bool Chris@96: MatrixFile::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@96: MatrixFile::setValueAt(size_t x, size_t y, float value) Chris@87: { Chris@87: if (m_mode != ReadWrite) { Chris@96: std::cerr << "ERROR: MatrixFile::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@96: ::perror("WARNING: MatrixFile::setValueAt: write failed"); Chris@87: } Chris@87: Chris@95: //... update cache as appropriate Chris@87: } Chris@87: Chris@87: void Chris@96: MatrixFile::setColumnAt(size_t x, float *values) Chris@87: { Chris@87: if (m_mode != ReadWrite) { Chris@96: std::cerr << "ERROR: MatrixFile::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@96: ::perror("WARNING: MatrixFile::setColumnAt: write failed"); Chris@87: } Chris@87: Chris@95: //... update cache as appropriate Chris@90: } Chris@90: Chris@95: void Chris@96: MatrixFile::primeCache(size_t x, bool goingLeft) Chris@90: { Chris@96: // std::cerr << "MatrixFile::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@96: FileReadThread::Request request; Chris@96: Chris@96: if (m_requestToken >= 0 && Chris@96: m_readThread.getRequest(m_requestToken, request)) { 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@96: m_cache.x = (request.start - m_headerSize) / (m_height * sizeof(float)); Chris@96: m_cache.width = request.size / (m_height * sizeof(float)); Chris@96: Chris@96: std::cerr << "actual: " << m_cache.x << ", " << m_cache.width << std::endl; Chris@95: Chris@97: if (m_cache.data) { Chris@97: if (m_spareData) free(m_spareData); Chris@97: m_spareData = (char *)m_cache.data; Chris@97: } Chris@96: m_cache.data = (float *)request.data; Chris@95: Chris@95: m_readThread.done(m_requestToken); Chris@95: m_requestToken = -1; Chris@95: } Chris@95: Chris@96: // already requested something covering this area; wait for it Chris@95: return; Chris@95: } Chris@95: Chris@96: // the current request is no longer of any use Chris@95: m_readThread.cancel(m_requestToken); Chris@96: Chris@96: // crude way to avoid leaking the data Chris@96: while (!m_readThread.isCancelled(m_requestToken)) { Chris@96: usleep(10000); Chris@96: } Chris@96: Chris@97: if (m_spareData) free(m_spareData); Chris@97: m_spareData = request.data; Chris@96: m_readThread.done(m_requestToken); Chris@96: Chris@95: m_requestToken = -1; Chris@95: } Chris@95: 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@97: request.data = (char *)realloc(m_spareData, rw * m_height * sizeof(float)); Chris@96: MUNLOCK(request.data, rw * m_height * sizeof(float)); Chris@97: m_spareData = 0; Chris@95: Chris@95: m_requestingX = rx; Chris@95: m_requestingWidth = rw; Chris@95: Chris@95: int token = m_readThread.request(request); Chris@96: std::cerr << "MatrixFile::primeCache: request token is " Chris@96: << token << " (x = " << rx << ", w = " << rw << ", left = " << goingLeft << ")" << std::endl; Chris@95: Chris@95: m_requestToken = token; Chris@95: } Chris@95: Chris@90: bool Chris@96: MatrixFile::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@96: std::cerr << "ERROR: MatrixFile::seekTo(" << x << ", " << y Chris@87: << ") failed" << std::endl; Chris@87: return false; Chris@87: } Chris@87: Chris@87: return true; Chris@87: } Chris@87: