Ticket #9458: 100-posix_fadvise_fixes_v2.patch

File 100-posix_fadvise_fixes_v2.patch, 6.4 KB (added by Jiri Fojtasek <jiri.fojtasek@…>, 13 years ago)

v2 patch

  • libs/libmythtv/fileringbuffer.cpp

    diff -u -r -p mythtv.old/libs/libmythtv/fileringbuffer.cpp mythtv/libs/libmythtv/fileringbuffer.cpp
    old new FileRingBuffer::~FileRingBuffer() 
    100100
    101101    if (fd2 >= 0)
    102102    {
     103        posix_fadvise(fd2, 0, 0, POSIX_FADV_DONTNEED);
    103104        close(fd2);
    104105        fd2 = -1;
    105106    }
    bool FileRingBuffer::OpenFile(const QStr 
    252253                {
    253254                    if (0 == lseek(fd2, 0, SEEK_SET))
    254255                    {
    255                         posix_fadvise(fd2, 0, 0, POSIX_FADV_SEQUENTIAL);
    256                         posix_fadvise(fd2, 0, 1*1024*1024, POSIX_FADV_WILLNEED);
     256                        posix_fadvise(fd2, 0, 0, POSIX_FADV_SEQUENTIAL);
    257257                        lasterror = 0;
    258258                        break;
    259259                    }
    long long FileRingBuffer::Seek(long long 
    601601                else
    602602                {
    603603                    ret = lseek64(fd2, internalreadpos, SEEK_SET);
    604                     posix_fadvise(fd2, 0,
    605                                   internalreadpos, POSIX_FADV_DONTNEED);
    606                     posix_fadvise(fd2, internalreadpos,
    607                                   1*1024*1024, POSIX_FADV_WILLNEED);
    608604                }
    609605                VERBOSE(VB_FILE, LOC +
    610606                        QString("Seek to %1 from ignore pos %2 returned %3")
    long long FileRingBuffer::Seek(long long 
    687683            else
    688684            {
    689685                ret = lseek64(fd2, ignorereadpos, SEEK_SET);
    690                 posix_fadvise(fd2, ignorereadpos, 250000, POSIX_FADV_WILLNEED);
    691686            }
    692687
    693688            if (ret < 0)
    long long FileRingBuffer::Seek(long long 
    752747    else
    753748    {
    754749        ret = lseek64(fd2, pos, whence);
    755         if (ret >= 0)
    756         {
    757             posix_fadvise(fd2, 0,   ret,         POSIX_FADV_DONTNEED);
    758             posix_fadvise(fd2, ret, 1*1024*1024, POSIX_FADV_WILLNEED);
    759         }
    760750    }
    761751
    762752    if (ret >= 0)
    763753    {
    764754        readpos = ret;
    765        
     755
    766756        ignorereadpos = -1;
    767757
    768758        if (readaheadrunning)
  • libs/libmythtv/ringbuffer.cpp

    diff -u -r -p mythtv.old/libs/libmythtv/ringbuffer.cpp mythtv/libs/libmythtv/ringbuffer.cpp
    old new void RingBuffer::run(void) 
    784784            poslock.lockForWrite();
    785785            rbwlock.lockForWrite();
    786786            internalreadpos += read_return;
    787             off_t donotneed = internalreadpos;
    788787            rbwpos = (rbwpos + read_return) % kBufferSize;
    789788            VERBOSE(VB_FILE|VB_EXTRA,
    790789                    LOC + QString("rbwpos += %1K requested %2K in read")
    void RingBuffer::run(void) 
    792791            rbwlock.unlock();
    793792            poslock.unlock();
    794793
    795             if (fd2 >=0 && donotneed > 0)
    796                 posix_fadvise(fd2, 0, donotneed, POSIX_FADV_DONTNEED);
     794            if (fd2 >=0 && read_return > 0)
     795            {
     796                posix_fadvise(fd2, (lseek(fd2, 0, SEEK_CUR) - read_return),
     797                              read_return, POSIX_FADV_DONTNEED);
     798            }
    797799        }
    798800
    799801        int used = kBufferSize - ReadBufFree();
    int RingBuffer::ReadDirect(void *buf, in 
    10171019            else
    10181020            {
    10191021                lseek64(fd2, old_pos, SEEK_SET);
    1020                 posix_fadvise(fd2, old_pos, 1*1024*1024, POSIX_FADV_WILLNEED);
    10211022            }
    10221023        }
    10231024        else
    int RingBuffer::ReadPriv(void *buf, int  
    11061107            VERBOSE(VB_FILE|VB_EXTRA, LOC + loc_desc +
    11071108                    QString(": ReadDirect checksum %1")
    11081109                    .arg(qChecksum((char*)buf,count)));
     1110
     1111            if (fd2 >=0 && ret > 0)
     1112            {
     1113                posix_fadvise(fd2, (lseek(fd2, 0, SEEK_CUR) - ret),
     1114                              ret, POSIX_FADV_DONTNEED);
     1115            }
     1116
    11091117            rwlock.unlock();
    11101118            return ret;
    11111119        }
  • libs/libmythtv/ThreadedFileWriter.cpp

    diff -u -r -p mythtv.old/libs/libmythtv/ThreadedFileWriter.cpp mythtv/libs/libmythtv/ThreadedFileWriter.cpp
    old new ThreadedFileWriter::~ThreadedFileWriter( 
    217217        Flush();
    218218        in_dtor = true; /* tells child thread to exit */
    219219
     220        bufferHasData.wakeAll();
     221        pthread_join(writer, NULL);
     222
    220223        bufferSyncWait.wakeAll();
    221224        pthread_join(syncer, NULL);
    222225
    223         bufferHasData.wakeAll();
    224         pthread_join(writer, NULL);
    225226        close(fd);
    226227        fd = -1;
    227228    }
    void ThreadedFileWriter::Sync(void) 
    385386{
    386387    if (fd >= 0)
    387388    {
    388         /// Toss any data the kernel wrote to disk on it's own from
    389         /// the cache, so we don't get penalized for preserving it
    390         /// during the sync.
    391         (void) posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
    392 
    393389#if defined(_POSIX_SYNCHRONIZED_IO) && _POSIX_SYNCHRONIZED_IO > 0
    394390        // fdatasync tries to avoid updating metadata, but will in
    395391        // practice always update metadata if any data is written
    void ThreadedFileWriter::Sync(void) 
    398394#else
    399395        fsync(fd);
    400396#endif
     397        buflock.lock();
     398        off_t write_position = m_file_wpos;
     399        buflock.unlock();
    401400
    402         // Toss any data we just synced from cache, so we don't
    403         // get penalized for it between now and the next sync.
    404         (void) posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
     401        const int may_need = (5 * 4 * 1024 *1024);
     402        if (in_dtor)
     403        {
     404            // invalidate remaining portion of the pagecache
     405            posix_fadvise(fd, m_file_sync,
     406                              write_position - m_file_sync,
     407                              POSIX_FADV_DONTNEED);
     408        }
     409        else if (write_position > may_need)
     410        {
     411            // leave portion of the file in pagecache because livetv
     412            // playback may need it very soon. This will reduce disk
     413            // io during livetv playback and decrease time required
     414            // to open next file when player switching/jumping
     415            // to next program. The size of may_need has been choosed
     416            // as 5 seconds of 35mbit playback.
     417            posix_fadvise(fd, m_file_sync,
     418                              write_position - may_need - m_file_sync,
     419                              POSIX_FADV_DONTNEED);
     420            m_file_sync = write_position - may_need;
     421        }
    405422    }
    406423}
    407424