2012-03-28 04:04:20 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef FILE_BLOCK_CACHE_H_
|
|
|
|
#define FILE_BLOCK_CACHE_H_
|
|
|
|
|
2013-05-30 00:43:41 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2017-03-16 23:54:23 +03:00
|
|
|
#include "mozilla/MozPromise.h"
|
2017-05-17 07:36:26 +03:00
|
|
|
#include "mozilla/Mutex.h"
|
2015-12-03 02:07:59 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2017-03-16 23:54:23 +03:00
|
|
|
#include "mozilla/AbstractThread.h"
|
2012-03-28 04:04:20 +04:00
|
|
|
#include "nsTArray.h"
|
2017-06-09 05:26:22 +03:00
|
|
|
#include "MediaBlockCacheBase.h"
|
2012-03-28 04:04:20 +04:00
|
|
|
#include "nsDeque.h"
|
2013-09-06 00:25:17 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2016-03-20 08:52:34 +03:00
|
|
|
#include <deque>
|
2013-09-06 00:25:17 +04:00
|
|
|
|
|
|
|
struct PRFileDesc;
|
2012-03-28 04:04:20 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
// Manages file I/O for the media cache. Data comes in over the network
|
|
|
|
// via callbacks on the main thread, however we don't want to write the
|
|
|
|
// incoming data to the media cache on the main thread, as this could block
|
2014-10-13 02:53:42 +04:00
|
|
|
// causing UI jank.
|
2012-03-28 04:04:20 +04:00
|
|
|
//
|
|
|
|
// So FileBlockCache provides an abstraction for a temporary file accessible
|
|
|
|
// as an array of blocks, which supports a block move operation, and
|
|
|
|
// allows synchronous reading and writing from any thread, with writes being
|
|
|
|
// buffered so as not to block.
|
|
|
|
//
|
|
|
|
// Writes and cache block moves (which require reading) are deferred to
|
|
|
|
// their own non-main thread. This object also ensures that data which has
|
|
|
|
// been scheduled to be written, but hasn't actually *been* written, is read
|
|
|
|
// as if it had, i.e. pending writes are cached in readable memory until
|
|
|
|
// they're flushed to file.
|
|
|
|
//
|
|
|
|
// To improve efficiency, writes can only be done at block granularity,
|
|
|
|
// whereas reads can be done with byte granularity.
|
|
|
|
//
|
|
|
|
// Note it's also recommended not to read from the media cache from the main
|
|
|
|
// thread to prevent jank.
|
|
|
|
//
|
|
|
|
// When WriteBlock() or MoveBlock() are called, data about how to complete
|
|
|
|
// the block change is added to mBlockChanges, indexed by block index, and
|
|
|
|
// the block index is appended to the mChangeIndexList. This enables
|
|
|
|
// us to quickly tell if a block has been changed, and ensures we can perform
|
|
|
|
// the changes in the correct order. An event is dispatched to perform the
|
|
|
|
// changes listed in mBlockChanges to file. Read() checks mBlockChanges and
|
|
|
|
// determines the current data to return, reading from file or from
|
|
|
|
// mBlockChanges as necessary.
|
2017-06-09 05:26:22 +03:00
|
|
|
class FileBlockCache : public MediaBlockCacheBase {
|
2012-03-28 04:04:20 +04:00
|
|
|
public:
|
|
|
|
FileBlockCache();
|
|
|
|
|
2014-07-15 19:37:45 +04:00
|
|
|
protected:
|
2017-06-09 05:26:22 +03:00
|
|
|
virtual ~FileBlockCache();
|
2012-03-28 04:04:20 +04:00
|
|
|
|
2014-07-15 19:37:45 +04:00
|
|
|
public:
|
2017-06-15 06:55:39 +03:00
|
|
|
// Launch thread and open temporary file.
|
2017-06-09 05:26:22 +03:00
|
|
|
nsresult Init() override;
|
2012-03-28 04:04:20 +04:00
|
|
|
|
2017-09-18 05:52:17 +03:00
|
|
|
// Will discard pending changes if any.
|
|
|
|
void Flush() override;
|
|
|
|
|
2017-07-10 01:23:02 +03:00
|
|
|
// Maximum number of blocks allowed in this block cache.
|
|
|
|
// Calculated from "media.cache_size" pref.
|
2019-05-07 02:38:02 +03:00
|
|
|
size_t GetMaxBlocks(size_t aCacheSizeInKB) const override;
|
2017-07-10 01:23:02 +03:00
|
|
|
|
2012-03-28 04:04:20 +04:00
|
|
|
// Can be called on any thread. This defers to a non-main thread.
|
2017-04-11 10:47:46 +03:00
|
|
|
nsresult WriteBlock(uint32_t aBlockIndex, Span<const uint8_t> aData1,
|
2017-06-09 05:26:22 +03:00
|
|
|
Span<const uint8_t> aData2) override;
|
2012-03-28 04:04:20 +04:00
|
|
|
|
|
|
|
// Synchronously reads data from file. May read from file or memory
|
|
|
|
// depending on whether written blocks have been flushed to file yet.
|
|
|
|
// Not recommended to be called from the main thread, as can cause jank.
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult Read(int64_t aOffset, uint8_t* aData, int32_t aLength,
|
2017-06-09 05:26:22 +03:00
|
|
|
int32_t* aBytes) override;
|
2012-03-28 04:04:20 +04:00
|
|
|
|
|
|
|
// Moves a block asynchronously. Can be called on any thread.
|
|
|
|
// This defers file I/O to a non-main thread.
|
2017-06-09 05:26:22 +03:00
|
|
|
nsresult MoveBlock(int32_t aSourceBlockIndex,
|
|
|
|
int32_t aDestBlockIndex) override;
|
2012-03-28 04:04:20 +04:00
|
|
|
|
|
|
|
// Represents a change yet to be made to a block in the file. The change
|
|
|
|
// is either a write (and the data to be written is stored in this struct)
|
|
|
|
// or a move (and the index of the source block is stored instead).
|
2015-03-21 19:28:04 +03:00
|
|
|
struct BlockChange final {
|
2012-03-28 04:04:20 +04:00
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(BlockChange)
|
|
|
|
|
|
|
|
// This block is waiting in memory to be written.
|
|
|
|
// Stores a copy of the block, so we can write it asynchronously.
|
2014-09-01 07:50:23 +04:00
|
|
|
explicit BlockChange(const uint8_t* aData) : mSourceBlockIndex(-1) {
|
2015-12-03 02:07:59 +03:00
|
|
|
mData = MakeUnique<uint8_t[]>(BLOCK_SIZE);
|
2012-03-28 04:04:20 +04:00
|
|
|
memcpy(mData.get(), aData, BLOCK_SIZE);
|
|
|
|
}
|
|
|
|
|
2017-04-11 10:47:46 +03:00
|
|
|
BlockChange(Span<const uint8_t> aData1, Span<const uint8_t> aData2)
|
|
|
|
: mSourceBlockIndex(-1) {
|
|
|
|
MOZ_ASSERT(aData1.Length() + aData2.Length() == BLOCK_SIZE);
|
|
|
|
mData = MakeUnique<uint8_t[]>(BLOCK_SIZE);
|
|
|
|
memcpy(mData.get(), aData1.Elements(), aData1.Length());
|
|
|
|
memcpy(mData.get() + aData1.Length(), aData2.Elements(), aData2.Length());
|
|
|
|
}
|
|
|
|
|
2012-03-28 04:04:20 +04:00
|
|
|
// This block's contents are located in another file
|
|
|
|
// block, i.e. this block has been moved.
|
2014-09-01 07:50:23 +04:00
|
|
|
explicit BlockChange(int32_t aSourceBlockIndex)
|
2012-03-28 04:04:20 +04:00
|
|
|
: mSourceBlockIndex(aSourceBlockIndex) {}
|
|
|
|
|
2015-12-03 02:07:59 +03:00
|
|
|
UniquePtr<uint8_t[]> mData;
|
2012-08-22 19:56:38 +04:00
|
|
|
const int32_t mSourceBlockIndex;
|
2012-03-28 04:04:20 +04:00
|
|
|
|
|
|
|
bool IsMove() const { return mSourceBlockIndex != -1; }
|
|
|
|
bool IsWrite() const {
|
|
|
|
return mSourceBlockIndex == -1 && mData.get() != nullptr;
|
|
|
|
}
|
2014-04-02 20:21:11 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Private destructor, to discourage deletion outside of Release():
|
|
|
|
~BlockChange() {}
|
2012-03-28 04:04:20 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2013-01-11 00:21:50 +04:00
|
|
|
int64_t BlockIndexToOffset(int32_t aBlockIndex) {
|
|
|
|
return static_cast<int64_t>(aBlockIndex) * BLOCK_SIZE;
|
|
|
|
}
|
|
|
|
|
2017-03-16 23:54:23 +03:00
|
|
|
void SetCacheFile(PRFileDesc* aFD);
|
|
|
|
|
2017-06-15 05:10:05 +03:00
|
|
|
// Close file in thread and terminate thread.
|
|
|
|
void Close();
|
|
|
|
|
2017-06-09 05:51:17 +03:00
|
|
|
// Performs block writes and block moves on its own thread.
|
|
|
|
void PerformBlockIOs();
|
|
|
|
|
2017-05-17 07:36:26 +03:00
|
|
|
// Mutex which controls access to mFD and mFDCurrentPos. Don't hold
|
|
|
|
// mDataMutex while holding mFileMutex! mFileMutex must be owned
|
2012-03-28 04:04:20 +04:00
|
|
|
// while accessing any of the following data fields or methods.
|
2017-05-17 07:36:26 +03:00
|
|
|
Mutex mFileMutex;
|
2012-03-28 04:04:20 +04:00
|
|
|
// Moves a block already committed to file.
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult MoveBlockInFile(int32_t aSourceBlockIndex, int32_t aDestBlockIndex);
|
2012-03-28 04:04:20 +04:00
|
|
|
// Seeks file pointer.
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult Seek(int64_t aOffset);
|
2012-03-28 04:04:20 +04:00
|
|
|
// Reads data from file offset.
|
2013-01-11 00:21:50 +04:00
|
|
|
nsresult ReadFromFile(int64_t aOffset, uint8_t* aDest, int32_t aBytesToRead,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t& aBytesRead);
|
|
|
|
nsresult WriteBlockToFile(int32_t aBlockIndex, const uint8_t* aBlockData);
|
2012-03-28 04:04:20 +04:00
|
|
|
// File descriptor we're writing to. This is created externally, but
|
|
|
|
// shutdown by us.
|
|
|
|
PRFileDesc* mFD;
|
|
|
|
// The current file offset in the file.
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t mFDCurrentPos;
|
2012-03-28 04:04:20 +04:00
|
|
|
|
2017-05-17 07:36:26 +03:00
|
|
|
// Mutex which controls access to all data in this class, except mFD
|
|
|
|
// and mFDCurrentPos. Don't hold mDataMutex while holding mFileMutex!
|
|
|
|
// mDataMutex must be owned while accessing any of the following data
|
2012-03-28 04:04:20 +04:00
|
|
|
// fields or methods.
|
2017-05-17 07:36:26 +03:00
|
|
|
Mutex mDataMutex;
|
2012-03-28 04:04:20 +04:00
|
|
|
// Ensures we either are running the event to preform IO, or an event
|
|
|
|
// has been dispatched to preform the IO.
|
2017-05-17 07:36:26 +03:00
|
|
|
// mDataMutex must be owned while calling this.
|
2012-03-28 04:04:20 +04:00
|
|
|
void EnsureWriteScheduled();
|
2017-03-16 23:54:23 +03:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
// Array of block changes to made. If mBlockChanges[offset/BLOCK_SIZE] ==
|
2012-03-28 04:04:20 +04:00
|
|
|
// nullptr, then the block has no pending changes to be written, but if
|
2012-07-30 18:20:58 +04:00
|
|
|
// mBlockChanges[offset/BLOCK_SIZE] != nullptr, then either there's a block
|
2012-03-28 04:04:20 +04:00
|
|
|
// cached in memory waiting to be written, or this block is the target of a
|
|
|
|
// block move.
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<BlockChange> > mBlockChanges;
|
2012-03-28 04:04:20 +04:00
|
|
|
// Thread upon which block writes and block moves are performed. This is
|
|
|
|
// created upon open, and shutdown (asynchronously) upon close (on the
|
|
|
|
// main thread).
|
|
|
|
nsCOMPtr<nsIThread> mThread;
|
|
|
|
// Queue of pending block indexes that need to be written or moved.
|
2016-03-20 08:52:34 +03:00
|
|
|
std::deque<int32_t> mChangeIndexList;
|
2012-03-28 04:04:20 +04:00
|
|
|
// True if we've dispatched an event to commit all pending block changes
|
|
|
|
// to file on mThread.
|
|
|
|
bool mIsWriteScheduled;
|
2017-05-17 08:10:57 +03:00
|
|
|
// True when a read is happening. Pending writes may be postponed, to give
|
|
|
|
// higher priority to reads (which may be blocking the caller).
|
|
|
|
bool mIsReading;
|
2017-04-07 08:51:47 +03:00
|
|
|
// True if we've got a temporary file descriptor. Note: we don't use mFD
|
2017-05-17 07:36:26 +03:00
|
|
|
// directly as that's synchronized via mFileMutex and we need to make
|
|
|
|
// decisions about whether we can write while holding mDataMutex.
|
2017-04-07 08:51:47 +03:00
|
|
|
bool mInitialized = false;
|
2012-03-28 04:04:20 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // End namespace mozilla.
|
|
|
|
|
|
|
|
#endif /* FILE_BLOCK_CACHE_H_ */
|