annotate data/fileio/MatrixFile.cpp @ 233:0d943a3fcc77

...
author Chris Cannam
date Fri, 16 Feb 2007 13:00:17 +0000
parents 6ec58bb8f729
children 3e6fee4e4257
rev   line source
Chris@148 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@148 2
Chris@148 3 /*
Chris@148 4 Sonic Visualiser
Chris@148 5 An audio file viewer and annotation editor.
Chris@148 6 Centre for Digital Music, Queen Mary, University of London.
Chris@148 7 This file copyright 2006 Chris Cannam.
Chris@148 8
Chris@148 9 This program is free software; you can redistribute it and/or
Chris@148 10 modify it under the terms of the GNU General Public License as
Chris@148 11 published by the Free Software Foundation; either version 2 of the
Chris@148 12 License, or (at your option) any later version. See the file
Chris@148 13 COPYING included with this distribution for more information.
Chris@148 14 */
Chris@148 15
Chris@148 16 #include "MatrixFile.h"
Chris@148 17 #include "base/TempDirectory.h"
Chris@150 18 #include "system/System.h"
Chris@148 19 #include "base/Profiler.h"
Chris@148 20 #include "base/Exceptions.h"
Chris@148 21
Chris@148 22 #include <sys/types.h>
Chris@148 23 #include <sys/stat.h>
Chris@148 24 #include <fcntl.h>
Chris@148 25 #include <unistd.h>
Chris@148 26
Chris@148 27 #include <iostream>
Chris@148 28
Chris@148 29 #include <cstdio>
Chris@148 30 #include <cassert>
Chris@148 31
Chris@148 32 #include <QFileInfo>
Chris@148 33 #include <QDir>
Chris@148 34
Chris@154 35 //#define DEBUG_MATRIX_FILE 1
Chris@154 36 //#define DEBUG_MATRIX_FILE_READ_SET 1
Chris@148 37
Chris@148 38 #ifdef DEBUG_MATRIX_FILE_READ_SET
Chris@153 39 #ifndef DEBUG_MATRIX_FILE
Chris@148 40 #define DEBUG_MATRIX_FILE 1
Chris@148 41 #endif
Chris@153 42 #endif
Chris@148 43
Chris@148 44 std::map<QString, int> MatrixFile::m_refcount;
Chris@148 45 QMutex MatrixFile::m_refcountMutex;
Chris@148 46
Chris@148 47 MatrixFile::ResizeableBitsetMap MatrixFile::m_columnBitsets;
Chris@148 48 QMutex MatrixFile::m_columnBitsetWriteMutex;
Chris@148 49
Chris@148 50 FileReadThread *MatrixFile::m_readThread = 0;
Chris@148 51
Chris@148 52 static size_t totalStorage = 0;
Chris@148 53 static size_t totalMemory = 0;
Chris@148 54 static size_t totalCount = 0;
Chris@148 55
Chris@148 56 MatrixFile::MatrixFile(QString fileBase, Mode mode,
Chris@148 57 size_t cellSize, bool eagerCache) :
Chris@148 58 m_fd(-1),
Chris@148 59 m_mode(mode),
Chris@148 60 m_flags(0),
Chris@148 61 m_fmode(0),
Chris@148 62 m_cellSize(cellSize),
Chris@148 63 m_width(0),
Chris@148 64 m_height(0),
Chris@148 65 m_headerSize(2 * sizeof(size_t)),
Chris@148 66 m_defaultCacheWidth(1024),
Chris@148 67 m_prevX(0),
Chris@148 68 m_eagerCache(eagerCache),
Chris@148 69 m_requestToken(-1),
Chris@148 70 m_spareData(0),
Chris@148 71 m_columnBitset(0)
Chris@148 72 {
Chris@148 73 Profiler profiler("MatrixFile::MatrixFile", true);
Chris@148 74
Chris@148 75 if (!m_readThread) {
Chris@148 76 m_readThread = new FileReadThread;
Chris@148 77 m_readThread->start();
Chris@148 78 }
Chris@148 79
Chris@148 80 m_cache.data = 0;
Chris@148 81
Chris@148 82 QDir tempDir(TempDirectory::getInstance()->getPath());
Chris@148 83 QString fileName(tempDir.filePath(QString("%1.mfc").arg(fileBase)));
Chris@148 84 bool newFile = !QFileInfo(fileName).exists();
Chris@148 85
Chris@148 86 if (newFile && m_mode == ReadOnly) {
Chris@148 87 std::cerr << "ERROR: MatrixFile::MatrixFile: Read-only mode "
Chris@148 88 << "specified, but cache file does not exist" << std::endl;
Chris@148 89 throw FileNotFound(fileName);
Chris@148 90 }
Chris@148 91
Chris@148 92 if (!newFile && m_mode == ReadWrite) {
Chris@148 93 std::cerr << "Note: MatrixFile::MatrixFile: Read/write mode "
Chris@148 94 << "specified, but file already exists; falling back to "
Chris@148 95 << "read-only mode" << std::endl;
Chris@148 96 m_mode = ReadOnly;
Chris@148 97 }
Chris@148 98
Chris@148 99 if (!eagerCache && m_mode == ReadOnly) {
Chris@148 100 std::cerr << "WARNING: MatrixFile::MatrixFile: Eager cacheing not "
Chris@148 101 << "specified, but file is open in read-only mode -- cache "
Chris@148 102 << "will not be used" << std::endl;
Chris@148 103 }
Chris@148 104
Chris@148 105 m_flags = 0;
Chris@148 106 m_fmode = S_IRUSR | S_IWUSR;
Chris@148 107
Chris@148 108 if (m_mode == ReadWrite) {
Chris@148 109 m_flags = O_RDWR | O_CREAT;
Chris@148 110 } else {
Chris@148 111 m_flags = O_RDONLY;
Chris@148 112 }
Chris@148 113
Chris@233 114 #ifdef _WIN32
Chris@233 115 m_flags |= O_BINARY;
Chris@233 116 #endif
Chris@233 117
Chris@148 118 #ifdef DEBUG_MATRIX_FILE
Chris@148 119 std::cerr << "MatrixFile::MatrixFile: opening " << fileName.toStdString() << "..." << std::endl;
Chris@148 120 #endif
Chris@148 121
Chris@148 122 if ((m_fd = ::open(fileName.toLocal8Bit(), m_flags, m_fmode)) < 0) {
Chris@148 123 ::perror("Open failed");
Chris@148 124 std::cerr << "ERROR: MatrixFile::MatrixFile: "
Chris@148 125 << "Failed to open cache file \""
Chris@148 126 << fileName.toStdString() << "\"";
Chris@148 127 if (m_mode == ReadWrite) std::cerr << " for writing";
Chris@148 128 std::cerr << std::endl;
Chris@148 129 throw FailedToOpenFile(fileName);
Chris@148 130 }
Chris@148 131
Chris@148 132 if (newFile) {
Chris@148 133 resize(0, 0); // write header
Chris@148 134 } else {
Chris@148 135 size_t header[2];
Chris@148 136 if (::read(m_fd, header, 2 * sizeof(size_t)) < 0) {
Chris@148 137 perror("Read failed");
Chris@148 138 std::cerr << "ERROR: MatrixFile::MatrixFile: "
Chris@148 139 << "Failed to read header (fd " << m_fd << ", file \""
Chris@148 140 << fileName.toStdString() << "\")" << std::endl;
Chris@148 141 throw FileReadFailed(fileName);
Chris@148 142 }
Chris@148 143 m_width = header[0];
Chris@148 144 m_height = header[1];
Chris@148 145 seekTo(0, 0);
Chris@148 146 }
Chris@148 147
Chris@148 148 m_fileName = fileName;
Chris@148 149
Chris@148 150 m_columnBitsetWriteMutex.lock();
Chris@148 151
Chris@148 152 if (m_columnBitsets.find(m_fileName) == m_columnBitsets.end()) {
Chris@148 153 m_columnBitsets[m_fileName] = new ResizeableBitset;
Chris@148 154 }
Chris@148 155 m_columnBitset = m_columnBitsets[m_fileName];
Chris@148 156
Chris@148 157 m_columnBitsetWriteMutex.unlock();
Chris@148 158
Chris@148 159 QMutexLocker locker(&m_refcountMutex);
Chris@148 160 ++m_refcount[fileName];
Chris@148 161
Chris@148 162 std::cerr << "MatrixFile(" << this << "): fd " << m_fd << ", file " << fileName.toStdString() << ", ref " << m_refcount[fileName] << std::endl;
Chris@148 163
Chris@148 164 // std::cerr << "MatrixFile::MatrixFile: Done, size is " << "(" << m_width << ", " << m_height << ")" << std::endl;
Chris@148 165
Chris@148 166 ++totalCount;
Chris@148 167
Chris@148 168 }
Chris@148 169
Chris@148 170 MatrixFile::~MatrixFile()
Chris@148 171 {
Chris@148 172 char *requestData = 0;
Chris@148 173
Chris@148 174 if (m_requestToken >= 0) {
Chris@148 175 FileReadThread::Request request;
Chris@148 176 if (m_readThread->getRequest(m_requestToken, request)) {
Chris@148 177 requestData = request.data;
Chris@148 178 }
Chris@148 179 m_readThread->cancel(m_requestToken);
Chris@148 180 }
Chris@148 181
Chris@148 182 if (requestData) free(requestData);
Chris@148 183 if (m_cache.data) free(m_cache.data);
Chris@148 184 if (m_spareData) free(m_spareData);
Chris@148 185
Chris@148 186 if (m_fd >= 0) {
Chris@148 187 if (::close(m_fd) < 0) {
Chris@148 188 ::perror("MatrixFile::~MatrixFile: close failed");
Chris@148 189 }
Chris@148 190 }
Chris@148 191
Chris@148 192 if (m_fileName != "") {
Chris@148 193
Chris@148 194 QMutexLocker locker(&m_refcountMutex);
Chris@148 195
Chris@148 196 if (--m_refcount[m_fileName] == 0) {
Chris@148 197
Chris@148 198 if (::unlink(m_fileName.toLocal8Bit())) {
Chris@148 199 ::perror("Unlink failed");
Chris@148 200 std::cerr << "WARNING: MatrixFile::~MatrixFile: reference count reached 0, but failed to unlink file \"" << m_fileName.toStdString() << "\"" << std::endl;
Chris@148 201 } else {
Chris@148 202 std::cerr << "deleted " << m_fileName.toStdString() << std::endl;
Chris@148 203 }
Chris@148 204
Chris@148 205 QMutexLocker locker2(&m_columnBitsetWriteMutex);
Chris@148 206 m_columnBitsets.erase(m_fileName);
Chris@148 207 delete m_columnBitset;
Chris@148 208 }
Chris@148 209 }
Chris@148 210
Chris@148 211 totalStorage -= (m_headerSize + (m_width * m_height * m_cellSize));
Chris@148 212 totalMemory -= (2 * m_defaultCacheWidth * m_height * m_cellSize);
Chris@148 213 totalCount --;
Chris@148 214
Chris@148 215 std::cerr << "MatrixFile::~MatrixFile: " << std::endl;
Chris@148 216 std::cerr << "Total storage now " << totalStorage/1024 << "K, theoretical max memory "
Chris@148 217 << totalMemory/1024 << "K in " << totalCount << " instances" << std::endl;
Chris@148 218
Chris@148 219 }
Chris@148 220
Chris@148 221 void
Chris@148 222 MatrixFile::resize(size_t w, size_t h)
Chris@148 223 {
Chris@148 224 Profiler profiler("MatrixFile::resize", true);
Chris@148 225
Chris@148 226 assert(m_mode == ReadWrite);
Chris@148 227
Chris@148 228 QMutexLocker locker(&m_fdMutex);
Chris@148 229
Chris@148 230 totalStorage -= (m_headerSize + (m_width * m_height * m_cellSize));
Chris@148 231 totalMemory -= (2 * m_defaultCacheWidth * m_height * m_cellSize);
Chris@148 232
Chris@148 233 off_t off = m_headerSize + (w * h * m_cellSize);
Chris@148 234
Chris@148 235 #ifdef DEBUG_MATRIX_FILE
Chris@148 236 std::cerr << "MatrixFile::resize(" << w << ", " << h << "): resizing file" << std::endl;
Chris@148 237 #endif
Chris@148 238
Chris@148 239 if (w * h < m_width * m_height) {
Chris@148 240 if (::ftruncate(m_fd, off) < 0) {
Chris@148 241 ::perror("WARNING: MatrixFile::resize: ftruncate failed");
Chris@148 242 throw FileOperationFailed(m_fileName, "ftruncate");
Chris@148 243 }
Chris@148 244 }
Chris@148 245
Chris@148 246 m_width = 0;
Chris@148 247 m_height = 0;
Chris@148 248
Chris@148 249 if (::lseek(m_fd, 0, SEEK_SET) == (off_t)-1) {
Chris@148 250 ::perror("ERROR: MatrixFile::resize: Seek to write header failed");
Chris@148 251 throw FileOperationFailed(m_fileName, "lseek");
Chris@148 252 }
Chris@148 253
Chris@148 254 size_t header[2];
Chris@148 255 header[0] = w;
Chris@148 256 header[1] = h;
Chris@148 257 if (::write(m_fd, header, 2 * sizeof(size_t)) != 2 * sizeof(size_t)) {
Chris@148 258 ::perror("ERROR: MatrixFile::resize: Failed to write header");
Chris@148 259 throw FileOperationFailed(m_fileName, "write");
Chris@148 260 }
Chris@148 261
Chris@148 262 if (w > 0 && m_defaultCacheWidth > w) {
Chris@148 263 m_defaultCacheWidth = w;
Chris@148 264 }
Chris@148 265
Chris@148 266 static size_t maxCacheMB = 16;
Chris@148 267 if (2 * m_defaultCacheWidth * h * m_cellSize > maxCacheMB * 1024 * 1024) { //!!!
Chris@148 268 m_defaultCacheWidth = (maxCacheMB * 1024 * 1024) / (2 * h * m_cellSize);
Chris@148 269 if (m_defaultCacheWidth < 16) m_defaultCacheWidth = 16;
Chris@148 270 }
Chris@148 271
Chris@148 272 if (m_columnBitset) {
Chris@148 273 QMutexLocker locker(&m_columnBitsetWriteMutex);
Chris@148 274 m_columnBitset->resize(w);
Chris@148 275 }
Chris@148 276
Chris@148 277 if (m_cache.data) {
Chris@148 278 free(m_cache.data);
Chris@148 279 m_cache.data = 0;
Chris@148 280 }
Chris@148 281
Chris@148 282 if (m_spareData) {
Chris@148 283 free(m_spareData);
Chris@148 284 m_spareData = 0;
Chris@148 285 }
Chris@148 286
Chris@148 287 m_width = w;
Chris@148 288 m_height = h;
Chris@148 289
Chris@148 290 totalStorage += (m_headerSize + (m_width * m_height * m_cellSize));
Chris@148 291 totalMemory += (2 * m_defaultCacheWidth * m_height * m_cellSize);
Chris@148 292
Chris@148 293 #ifdef DEBUG_MATRIX_FILE
Chris@148 294 std::cerr << "MatrixFile::resize(" << w << ", " << h << "): cache width "
Chris@148 295 << m_defaultCacheWidth << ", storage "
Chris@148 296 << (m_headerSize + w * h * m_cellSize) << ", mem "
Chris@148 297 << (2 * h * m_defaultCacheWidth * m_cellSize) << std::endl;
Chris@148 298
Chris@148 299 std::cerr << "Total storage " << totalStorage/1024 << "K, theoretical max memory "
Chris@148 300 << totalMemory/1024 << "K in " << totalCount << " instances" << std::endl;
Chris@148 301 #endif
Chris@148 302
Chris@148 303 seekTo(0, 0);
Chris@148 304 }
Chris@148 305
Chris@148 306 void
Chris@148 307 MatrixFile::reset()
Chris@148 308 {
Chris@148 309 Profiler profiler("MatrixFile::reset", true);
Chris@148 310
Chris@148 311 assert (m_mode == ReadWrite);
Chris@148 312
Chris@148 313 if (m_eagerCache) {
Chris@148 314 void *emptyCol = calloc(m_height, m_cellSize);
Chris@148 315 for (size_t x = 0; x < m_width; ++x) setColumnAt(x, emptyCol);
Chris@148 316 free(emptyCol);
Chris@148 317 }
Chris@148 318
Chris@148 319 if (m_columnBitset) {
Chris@148 320 QMutexLocker locker(&m_columnBitsetWriteMutex);
Chris@148 321 m_columnBitset->resize(m_width);
Chris@148 322 }
Chris@148 323 }
Chris@148 324
Chris@148 325 void
Chris@148 326 MatrixFile::getColumnAt(size_t x, void *data)
Chris@148 327 {
Chris@148 328 // Profiler profiler("MatrixFile::getColumnAt");
Chris@148 329
Chris@148 330 // assert(haveSetColumnAt(x));
Chris@148 331
Chris@148 332 if (getFromCache(x, 0, m_height, data)) return;
Chris@148 333
Chris@148 334 // Profiler profiler2("MatrixFile::getColumnAt (uncached)");
Chris@148 335
Chris@148 336 ssize_t r = 0;
Chris@148 337
Chris@148 338 #ifdef DEBUG_MATRIX_FILE
Chris@148 339 std::cerr << "MatrixFile::getColumnAt(" << x << ")"
Chris@148 340 << ": reading the slow way";
Chris@148 341
Chris@148 342 if (m_requestToken >= 0 &&
Chris@148 343 x >= m_requestingX &&
Chris@148 344 x < m_requestingX + m_requestingWidth) {
Chris@148 345
Chris@148 346 std::cerr << " (awaiting " << m_requestingX << ", " << m_requestingWidth << " from disk)";
Chris@148 347 }
Chris@148 348
Chris@148 349 std::cerr << std::endl;
Chris@148 350 #endif
Chris@148 351
Chris@148 352 m_fdMutex.lock();
Chris@148 353
Chris@148 354 if (seekTo(x, 0)) {
Chris@148 355 r = ::read(m_fd, data, m_height * m_cellSize);
Chris@148 356 }
Chris@148 357
Chris@148 358 m_fdMutex.unlock();
Chris@148 359
Chris@148 360 if (r < 0) {
Chris@148 361 ::perror("MatrixFile::getColumnAt: read failed");
Chris@148 362 throw FileReadFailed(m_fileName);
Chris@148 363 }
Chris@148 364
Chris@148 365 return;
Chris@148 366 }
Chris@148 367
Chris@148 368 bool
Chris@148 369 MatrixFile::getFromCache(size_t x, size_t ystart, size_t ycount, void *data)
Chris@148 370 {
Chris@148 371 m_cacheMutex.lock();
Chris@148 372
Chris@148 373 if (!m_cache.data || x < m_cache.x || x >= m_cache.x + m_cache.width) {
Chris@148 374 bool left = (m_cache.data && x < m_cache.x);
Chris@148 375 m_cacheMutex.unlock();
Chris@148 376 primeCache(x, left); // this doesn't take effect until a later callback
Chris@148 377 m_prevX = x;
Chris@148 378 return false;
Chris@148 379 }
Chris@148 380
Chris@148 381 memcpy(data,
Chris@148 382 m_cache.data + m_cellSize * ((x - m_cache.x) * m_height + ystart),
Chris@148 383 ycount * m_cellSize);
Chris@148 384
Chris@148 385 m_cacheMutex.unlock();
Chris@148 386
Chris@148 387 if (m_cache.x > 0 && x < m_prevX && x < m_cache.x + m_cache.width/4) {
Chris@148 388 primeCache(x, true);
Chris@148 389 }
Chris@148 390
Chris@148 391 if (m_cache.x + m_cache.width < m_width &&
Chris@148 392 x > m_prevX &&
Chris@148 393 x > m_cache.x + (m_cache.width * 3) / 4) {
Chris@148 394 primeCache(x, false);
Chris@148 395 }
Chris@148 396
Chris@148 397 m_prevX = x;
Chris@148 398 return true;
Chris@148 399 }
Chris@148 400
Chris@148 401 void
Chris@148 402 MatrixFile::setColumnAt(size_t x, const void *data)
Chris@148 403 {
Chris@148 404 assert(m_mode == ReadWrite);
Chris@148 405
Chris@148 406 #ifdef DEBUG_MATRIX_FILE_READ_SET
Chris@148 407 std::cerr << "MatrixFile::setColumnAt(" << x << ")" << std::endl;
Chris@148 408 #endif
Chris@148 409
Chris@148 410 ssize_t w = 0;
Chris@148 411 bool seekFailed = false;
Chris@148 412
Chris@148 413 m_fdMutex.lock();
Chris@148 414
Chris@148 415 if (seekTo(x, 0)) {
Chris@148 416 w = ::write(m_fd, data, m_height * m_cellSize);
Chris@148 417 } else {
Chris@148 418 seekFailed = true;
Chris@148 419 }
Chris@148 420
Chris@148 421 m_fdMutex.unlock();
Chris@148 422
Chris@148 423 if (!seekFailed && w != ssize_t(m_height * m_cellSize)) {
Chris@148 424 ::perror("WARNING: MatrixFile::setColumnAt: write failed");
Chris@148 425 throw FileOperationFailed(m_fileName, "write");
Chris@148 426 } else if (seekFailed) {
Chris@148 427 throw FileOperationFailed(m_fileName, "seek");
Chris@148 428 } else {
Chris@148 429 QMutexLocker locker(&m_columnBitsetWriteMutex);
Chris@148 430 m_columnBitset->set(x);
Chris@148 431 }
Chris@148 432 }
Chris@148 433
Chris@148 434 void
Chris@148 435 MatrixFile::suspend()
Chris@148 436 {
Chris@148 437 QMutexLocker locker(&m_fdMutex);
Chris@148 438 QMutexLocker locker2(&m_cacheMutex);
Chris@148 439
Chris@148 440 if (m_fd < 0) return; // already suspended
Chris@148 441
Chris@148 442 #ifdef DEBUG_MATRIX_FILE
Chris@148 443 std::cerr << "MatrixFile(" << this << ":" << m_fileName.toStdString() << ")::suspend(): fd was " << m_fd << std::endl;
Chris@148 444 #endif
Chris@148 445
Chris@148 446 if (m_requestToken >= 0) {
Chris@148 447 void *data = 0;
Chris@148 448 FileReadThread::Request request;
Chris@148 449 if (m_readThread->getRequest(m_requestToken, request)) {
Chris@148 450 data = request.data;
Chris@148 451 }
Chris@148 452 m_readThread->cancel(m_requestToken);
Chris@148 453 if (data) free(data);
Chris@148 454 m_requestToken = -1;
Chris@148 455 }
Chris@148 456
Chris@148 457 if (m_cache.data) {
Chris@148 458 free(m_cache.data);
Chris@148 459 m_cache.data = 0;
Chris@148 460 }
Chris@148 461
Chris@148 462 if (m_spareData) {
Chris@148 463 free(m_spareData);
Chris@148 464 m_spareData = 0;
Chris@148 465 }
Chris@148 466
Chris@148 467 if (::close(m_fd) < 0) {
Chris@148 468 ::perror("WARNING: MatrixFile::suspend: close failed");
Chris@148 469 throw FileOperationFailed(m_fileName, "close");
Chris@148 470 }
Chris@148 471
Chris@148 472 m_fd = -1;
Chris@148 473 }
Chris@148 474
Chris@148 475 void
Chris@148 476 MatrixFile::resume()
Chris@148 477 {
Chris@148 478 if (m_fd >= 0) return;
Chris@148 479
Chris@148 480 #ifdef DEBUG_MATRIX_FILE
Chris@148 481 std::cerr << "MatrixFile(" << this << ")::resume()" << std::endl;
Chris@148 482 #endif
Chris@148 483
Chris@148 484 if ((m_fd = ::open(m_fileName.toLocal8Bit(), m_flags, m_fmode)) < 0) {
Chris@148 485 ::perror("Open failed");
Chris@148 486 std::cerr << "ERROR: MatrixFile::resume: "
Chris@148 487 << "Failed to open cache file \""
Chris@148 488 << m_fileName.toStdString() << "\"";
Chris@148 489 if (m_mode == ReadWrite) std::cerr << " for writing";
Chris@148 490 std::cerr << std::endl;
Chris@148 491 throw FailedToOpenFile(m_fileName);
Chris@148 492 }
Chris@148 493
Chris@148 494 std::cerr << "MatrixFile(" << this << ":" << m_fileName.toStdString() << ")::resume(): fd is " << m_fd << std::endl;
Chris@148 495 }
Chris@148 496
Chris@148 497 void
Chris@148 498 MatrixFile::primeCache(size_t x, bool goingLeft)
Chris@148 499 {
Chris@148 500 // Profiler profiler("MatrixFile::primeCache");
Chris@148 501
Chris@148 502 #ifdef DEBUG_MATRIX_FILE_READ_SET
Chris@148 503 std::cerr << "MatrixFile::primeCache(" << x << ", " << goingLeft << ")" << std::endl;
Chris@148 504 #endif
Chris@148 505
Chris@148 506 size_t rx = x;
Chris@148 507 size_t rw = m_defaultCacheWidth;
Chris@148 508
Chris@148 509 size_t left = rw / 3;
Chris@148 510 if (goingLeft) left = (rw * 2) / 3;
Chris@148 511
Chris@148 512 if (rx > left) rx -= left;
Chris@148 513 else rx = 0;
Chris@148 514
Chris@148 515 if (rx + rw > m_width) rw = m_width - rx;
Chris@148 516
Chris@148 517 if (!m_eagerCache) {
Chris@148 518
Chris@148 519 size_t ti = 0;
Chris@148 520
Chris@148 521 for (ti = 0; ti < rw; ++ti) {
Chris@148 522 if (!m_columnBitset->get(rx + ti)) break;
Chris@148 523 }
Chris@148 524
Chris@148 525 #ifdef DEBUG_MATRIX_FILE
Chris@148 526 if (ti < rw) {
Chris@148 527 std::cerr << "eagerCache is false and there's a hole at "
Chris@148 528 << rx + ti << ", reducing rw from " << rw << " to "
Chris@148 529 << ti << std::endl;
Chris@148 530 }
Chris@148 531 #endif
Chris@148 532
Chris@148 533 rw = std::min(rw, ti);
Chris@148 534 if (rw < 10 || rx + rw <= x) return;
Chris@148 535 }
Chris@148 536
Chris@148 537 QMutexLocker locker(&m_cacheMutex);
Chris@148 538
Chris@148 539 FileReadThread::Request request;
Chris@148 540
Chris@148 541 if (m_requestToken >= 0 &&
Chris@148 542 m_readThread->getRequest(m_requestToken, request)) {
Chris@148 543
Chris@148 544 if (x >= m_requestingX &&
Chris@148 545 x < m_requestingX + m_requestingWidth) {
Chris@148 546
Chris@148 547 if (m_readThread->isReady(m_requestToken)) {
Chris@148 548
Chris@148 549 if (!request.successful) {
Chris@148 550 std::cerr << "ERROR: MatrixFile::primeCache: Last request was unsuccessful" << std::endl;
Chris@148 551 throw FileReadFailed(m_fileName);
Chris@148 552 }
Chris@148 553
Chris@148 554 #ifdef DEBUG_MATRIX_FILE_READ_SET
Chris@148 555 std::cerr << "last request is ready! (" << m_requestingX << ", "<< m_requestingWidth << ")" << std::endl;
Chris@148 556 #endif
Chris@148 557
Chris@148 558 m_cache.x = (request.start - m_headerSize) / (m_height * m_cellSize);
Chris@148 559 m_cache.width = request.size / (m_height * m_cellSize);
Chris@148 560
Chris@148 561 #ifdef DEBUG_MATRIX_FILE_READ_SET
Chris@148 562 std::cerr << "received last request: actual size is: " << m_cache.x << ", " << m_cache.width << std::endl;
Chris@148 563 #endif
Chris@148 564
Chris@148 565 if (m_cache.data) {
Chris@148 566 if (m_spareData) free(m_spareData);
Chris@148 567 m_spareData = m_cache.data;
Chris@148 568 }
Chris@148 569 m_cache.data = request.data;
Chris@148 570
Chris@148 571 m_readThread->done(m_requestToken);
Chris@148 572 m_requestToken = -1;
Chris@148 573 }
Chris@148 574
Chris@148 575 // already requested something covering this area; wait for it
Chris@148 576 return;
Chris@148 577 }
Chris@148 578
Chris@148 579 // the current request is no longer of any use
Chris@148 580 m_readThread->cancel(m_requestToken);
Chris@148 581
Chris@148 582 // crude way to avoid leaking the data
Chris@148 583 while (!m_readThread->isCancelled(m_requestToken)) {
Chris@148 584 usleep(10000);
Chris@148 585 }
Chris@148 586
Chris@148 587 #ifdef DEBUG_MATRIX_FILE_READ_SET
Chris@148 588 std::cerr << "cancelled " << m_requestToken << std::endl;
Chris@148 589 #endif
Chris@148 590
Chris@148 591 if (m_spareData) free(m_spareData);
Chris@148 592 m_spareData = request.data;
Chris@148 593 m_readThread->done(m_requestToken);
Chris@148 594
Chris@148 595 m_requestToken = -1;
Chris@148 596 }
Chris@148 597
Chris@148 598 if (m_fd < 0) {
Chris@148 599 m_fdMutex.lock();
Chris@148 600 if (m_fd < 0) resume();
Chris@148 601 m_fdMutex.unlock();
Chris@148 602 }
Chris@148 603
Chris@148 604 request.fd = m_fd;
Chris@148 605 request.mutex = &m_fdMutex;
Chris@148 606 request.start = m_headerSize + rx * m_height * m_cellSize;
Chris@148 607 request.size = rw * m_height * m_cellSize;
Chris@148 608 request.data = (char *)realloc(m_spareData, rw * m_height * m_cellSize);
Chris@148 609 MUNLOCK(request.data, rw * m_height * m_cellSize);
Chris@148 610 m_spareData = 0;
Chris@148 611
Chris@148 612 m_requestingX = rx;
Chris@148 613 m_requestingWidth = rw;
Chris@148 614
Chris@148 615 int token = m_readThread->request(request);
Chris@148 616 #ifdef DEBUG_MATRIX_FILE_READ_SET
Chris@148 617 std::cerr << "MatrixFile::primeCache: request token is "
Chris@148 618 << token << " (x = [" << rx << "], w = [" << rw << "], left = [" << goingLeft << "])" << std::endl;
Chris@148 619 #endif
Chris@148 620 m_requestToken = token;
Chris@148 621 }
Chris@148 622
Chris@148 623 bool
Chris@148 624 MatrixFile::seekTo(size_t x, size_t y)
Chris@148 625 {
Chris@148 626 if (m_fd < 0) resume();
Chris@148 627
Chris@148 628 off_t off = m_headerSize + (x * m_height + y) * m_cellSize;
Chris@148 629
Chris@148 630 if (::lseek(m_fd, off, SEEK_SET) == (off_t)-1) {
Chris@148 631 ::perror("Seek failed");
Chris@148 632 std::cerr << "ERROR: MatrixFile::seekTo(" << x << ", " << y
Chris@148 633 << ") failed" << std::endl;
Chris@148 634 return false;
Chris@148 635 }
Chris@148 636
Chris@148 637 return true;
Chris@148 638 }
Chris@148 639