[AssetProcessor] Refactor the FileWatcher to use only one watch thread
This change reworks the AssetProcessor's FileWatcher so that it only uses one thread. This is motivated by getting better support for inotify on Linux. The previous architecture required calling `inotify_init` once for each directory that was being watched, and using separate inotify instances for each watched tree. In addition, having separate threads per watched tree is not necessary, and just consumes system resources. Each platform supports watching multiple directories with the same platform-specific watcher API, so each platform has been updated accordingly. The interface to the FileWatcher class is greatly simplified. Previously, it supported client-supplied filtering of the paths that would generate notifications. This was done by subclassing `FolderWatchBase` and implementing `OnFileChange`. However, only one filter was ever used, so that filter is now hard-coded in the FileWatcher class, and the classes driving the old filtering mechanism are removed. Users of the interface now have a much easier time, they just call `AddFolderWatch` with the path to watch, and only have to connect to one set of signals, instead of separate signals per watched directory. Signed-off-by: Chris Burel <burelc@amazon.com>
This commit is contained in:
@@ -7,6 +7,8 @@
|
||||
#
|
||||
|
||||
set(FILES
|
||||
native/FileWatcher/FileWatcher_platform.h
|
||||
native/FileWatcher/FileWatcher_win.cpp
|
||||
native/FileWatcher/FileWatcher_windows.h
|
||||
native/resource.h
|
||||
)
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
/*
|
||||
* Copyright (c) Contributors to the Open 3D Engine Project.
|
||||
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <native/FileWatcher/FileWatcher_windows.h>
|
||||
+116
-93
@@ -6,125 +6,148 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include <AzCore/std/tuple.h>
|
||||
#include <AzCore/std/utils.h>
|
||||
#include <native/FileWatcher/FileWatcher.h>
|
||||
#include <native/FileWatcher/FileWatcher_platform.h>
|
||||
#include <QDir>
|
||||
|
||||
#include <AzCore/PlatformIncl.h>
|
||||
|
||||
struct FolderRootWatch::PlatformImplementation
|
||||
bool FileWatcher::PlatformStart()
|
||||
{
|
||||
PlatformImplementation() : m_directoryHandle(nullptr), m_ioHandle(nullptr) { }
|
||||
HANDLE m_directoryHandle;
|
||||
HANDLE m_ioHandle;
|
||||
};
|
||||
m_shutdownThreadSignal = false;
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
/// FolderWatchRoot
|
||||
FolderRootWatch::FolderRootWatch(const QString rootFolder)
|
||||
: m_root(rootFolder)
|
||||
, m_shutdownThreadSignal(false)
|
||||
, m_fileWatcher(nullptr)
|
||||
, m_platformImpl(new PlatformImplementation())
|
||||
{
|
||||
}
|
||||
|
||||
FolderRootWatch::~FolderRootWatch()
|
||||
{
|
||||
// Destructor is required in here since this file contains the definition of struct PlatformImplementation
|
||||
Stop();
|
||||
|
||||
delete m_platformImpl;
|
||||
}
|
||||
|
||||
bool FolderRootWatch::Start()
|
||||
{
|
||||
m_platformImpl->m_directoryHandle = ::CreateFileW(m_root.toStdWString().data(), FILE_LIST_DIRECTORY, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED, nullptr);
|
||||
|
||||
if (m_platformImpl->m_directoryHandle != INVALID_HANDLE_VALUE)
|
||||
bool allSucceeded = true;
|
||||
for (const auto& [directory, recursive] : m_folderWatchRoots)
|
||||
{
|
||||
m_platformImpl->m_ioHandle = ::CreateIoCompletionPort(m_platformImpl->m_directoryHandle, nullptr, 1, 0);
|
||||
if (m_platformImpl->m_ioHandle != INVALID_HANDLE_VALUE)
|
||||
if (QDir(directory).exists())
|
||||
{
|
||||
m_shutdownThreadSignal = false;
|
||||
m_thread = std::thread(std::bind(&FolderRootWatch::WatchFolderLoop, this));
|
||||
return true;
|
||||
allSucceeded &= m_platformImpl->AddWatchFolder(directory, recursive);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return allSucceeded;
|
||||
}
|
||||
|
||||
void FolderRootWatch::Stop()
|
||||
bool FileWatcher::PlatformImplementation::AddWatchFolder(QString root, bool recursive)
|
||||
{
|
||||
HandleUniquePtr directoryHandle{::CreateFileW(
|
||||
root.toStdWString().data(),
|
||||
FILE_LIST_DIRECTORY,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
||||
nullptr,
|
||||
OPEN_EXISTING,
|
||||
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
|
||||
nullptr
|
||||
)};
|
||||
|
||||
if (directoryHandle.get() == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
AZ_Warning("FileWatcher", false, "Failed to start watching %s", root.toUtf8().constData());
|
||||
return false;
|
||||
}
|
||||
|
||||
// Associate this file handle with our existing io completion port handle
|
||||
if (!::CreateIoCompletionPort(directoryHandle.get(), m_ioHandle.get(), /*CompletionKey =*/ static_cast<ULONG_PTR>(PlatformImplementation::EventType::FileRead), 1))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
auto id = AZStd::make_unique<OVERLAPPED>();
|
||||
auto* idp = id.get();
|
||||
const auto& [folderWatch, inserted] = m_folderRootWatches.emplace(AZStd::piecewise_construct, AZStd::forward_as_tuple(idp),
|
||||
AZStd::forward_as_tuple(AZStd::move(id), AZStd::move(directoryHandle), root, recursive));
|
||||
|
||||
if (!inserted)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return folderWatch->second.ReadChanges();
|
||||
}
|
||||
|
||||
bool FileWatcher::PlatformImplementation::FolderRootWatch::ReadChanges()
|
||||
{
|
||||
// Register to get directory change notifications for our directory handle
|
||||
return ::ReadDirectoryChangesW(
|
||||
m_directoryHandle.get(),
|
||||
&m_fileNotifyInformationList,
|
||||
sizeof(m_fileNotifyInformationList),
|
||||
m_recursive,
|
||||
FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_FILE_NAME,
|
||||
nullptr,
|
||||
m_overlapped.get(),
|
||||
nullptr
|
||||
);
|
||||
}
|
||||
|
||||
void FileWatcher::PlatformStop()
|
||||
{
|
||||
m_shutdownThreadSignal = true;
|
||||
CloseHandle(m_platformImpl->m_ioHandle);
|
||||
m_platformImpl->m_ioHandle = nullptr;
|
||||
|
||||
// Send a special signal to the child thread, that is blocked in a GetQueuedCompletionStatus call, with a completion
|
||||
// key set to Shutdown. The child thread will stop its processing when it receives this value for the completion key
|
||||
PostQueuedCompletionStatus(m_platformImpl->m_ioHandle.get(), 0, /*CompletionKey =*/ static_cast<ULONG_PTR>(PlatformImplementation::EventType::Shutdown), nullptr);
|
||||
if (m_thread.joinable())
|
||||
{
|
||||
m_thread.join(); // wait for the thread to finish
|
||||
m_thread = std::thread(); //destroy
|
||||
}
|
||||
CloseHandle(m_platformImpl->m_directoryHandle);
|
||||
m_platformImpl->m_directoryHandle = nullptr;
|
||||
}
|
||||
|
||||
void FolderRootWatch::WatchFolderLoop()
|
||||
void FileWatcher::WatchFolderLoop()
|
||||
{
|
||||
FILE_NOTIFY_INFORMATION aFileNotifyInformationList[50000];
|
||||
QString path;
|
||||
OVERLAPPED aOverlapped;
|
||||
LPOVERLAPPED pOverlapped;
|
||||
DWORD dwByteCount;
|
||||
ULONG_PTR ulKey;
|
||||
LPOVERLAPPED directoryId = nullptr;
|
||||
ULONG_PTR completionKey = 0;
|
||||
|
||||
while (!m_shutdownThreadSignal)
|
||||
{
|
||||
::memset(aFileNotifyInformationList, 0, sizeof(aFileNotifyInformationList));
|
||||
::memset(&aOverlapped, 0, sizeof(aOverlapped));
|
||||
|
||||
if (::ReadDirectoryChangesW(m_platformImpl->m_directoryHandle, aFileNotifyInformationList, sizeof(aFileNotifyInformationList), true, FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_FILE_NAME, nullptr, &aOverlapped, nullptr))
|
||||
DWORD dwByteCount = 0;
|
||||
if (::GetQueuedCompletionStatus(m_platformImpl->m_ioHandle.get(), &dwByteCount, &completionKey, &directoryId, INFINITE))
|
||||
{
|
||||
//wait for up to a second for I/O to signal
|
||||
dwByteCount = 0;
|
||||
if (::GetQueuedCompletionStatus(m_platformImpl->m_ioHandle, &dwByteCount, &ulKey, &pOverlapped, INFINITE))
|
||||
if (m_shutdownThreadSignal || completionKey == static_cast<ULONG_PTR>(PlatformImplementation::EventType::Shutdown))
|
||||
{
|
||||
//if we are signaled to shutdown bypass
|
||||
if (!m_shutdownThreadSignal && ulKey)
|
||||
{
|
||||
if (dwByteCount)
|
||||
{
|
||||
int offset = 0;
|
||||
FILE_NOTIFY_INFORMATION* pFileNotifyInformation = aFileNotifyInformationList;
|
||||
do
|
||||
{
|
||||
pFileNotifyInformation = (FILE_NOTIFY_INFORMATION*)((char*)pFileNotifyInformation + offset);
|
||||
|
||||
path.clear();
|
||||
path.append(m_root);
|
||||
path.append(QString::fromWCharArray(pFileNotifyInformation->FileName, pFileNotifyInformation->FileNameLength / 2));
|
||||
|
||||
QString file = QDir::toNativeSeparators(QDir::cleanPath(path));
|
||||
|
||||
switch (pFileNotifyInformation->Action)
|
||||
{
|
||||
case FILE_ACTION_ADDED:
|
||||
case FILE_ACTION_RENAMED_NEW_NAME:
|
||||
ProcessNewFileEvent(file);
|
||||
break;
|
||||
case FILE_ACTION_REMOVED:
|
||||
case FILE_ACTION_RENAMED_OLD_NAME:
|
||||
ProcessDeleteFileEvent(file);
|
||||
break;
|
||||
case FILE_ACTION_MODIFIED:
|
||||
ProcessModifyFileEvent(file);
|
||||
break;
|
||||
}
|
||||
|
||||
offset = pFileNotifyInformation->NextEntryOffset;
|
||||
} while (offset);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (dwByteCount == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
const auto foundFolderRoot = m_platformImpl->m_folderRootWatches.find(directoryId);
|
||||
if (foundFolderRoot == end(m_platformImpl->m_folderRootWatches))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
PlatformImplementation::FolderRootWatch& folderRoot = foundFolderRoot->second;
|
||||
|
||||
// Initialize offset to 1 to ensure that the first iteration is always processed
|
||||
DWORD offset = 1;
|
||||
for (
|
||||
const FILE_NOTIFY_INFORMATION* pFileNotifyInformation = reinterpret_cast<const FILE_NOTIFY_INFORMATION*>(&folderRoot.m_fileNotifyInformationList);
|
||||
offset;
|
||||
pFileNotifyInformation = reinterpret_cast<const FILE_NOTIFY_INFORMATION*>(reinterpret_cast<const char*>(pFileNotifyInformation) + offset)
|
||||
){
|
||||
const QString file = QDir::toNativeSeparators(QDir(folderRoot.m_directoryRoot)
|
||||
.filePath(QString::fromWCharArray(pFileNotifyInformation->FileName, pFileNotifyInformation->FileNameLength / 2)));
|
||||
|
||||
switch (pFileNotifyInformation->Action)
|
||||
{
|
||||
case FILE_ACTION_ADDED:
|
||||
case FILE_ACTION_RENAMED_NEW_NAME:
|
||||
rawFileAdded(file, {});
|
||||
break;
|
||||
case FILE_ACTION_REMOVED:
|
||||
case FILE_ACTION_RENAMED_OLD_NAME:
|
||||
rawFileRemoved(file, {});
|
||||
break;
|
||||
case FILE_ACTION_MODIFIED:
|
||||
rawFileModified(file, {});
|
||||
break;
|
||||
}
|
||||
|
||||
offset = pFileNotifyInformation->NextEntryOffset;
|
||||
}
|
||||
|
||||
folderRoot.ReadChanges();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright (c) Contributors to the Open 3D Engine Project.
|
||||
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AzCore/std/containers/unordered_map.h>
|
||||
#include <AzCore/std/typetraits/aligned_storage.h>
|
||||
#include <AzCore/std/typetraits/remove_pointer.h>
|
||||
#include <AzCore/std/smart_ptr/unique_ptr.h>
|
||||
|
||||
#include <native/FileWatcher/FileWatcher.h>
|
||||
#include <AzCore/PlatformIncl.h>
|
||||
|
||||
struct HandleDeleter
|
||||
{
|
||||
void operator()(HANDLE handle)
|
||||
{
|
||||
if (handle && handle != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
CloseHandle(handle);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
using HandleUniquePtr = AZStd::unique_ptr<AZStd::remove_pointer_t<HANDLE>, HandleDeleter>;
|
||||
|
||||
class FileWatcher::PlatformImplementation
|
||||
{
|
||||
public:
|
||||
bool AddWatchFolder(QString folder, bool recursive);
|
||||
|
||||
struct FolderRootWatch
|
||||
{
|
||||
FolderRootWatch(AZStd::unique_ptr<OVERLAPPED>&& overlapped, HandleUniquePtr&& directoryHandle, QString root, bool recursive)
|
||||
: m_overlapped(AZStd::move(overlapped))
|
||||
, m_directoryHandle(AZStd::move(directoryHandle))
|
||||
, m_directoryRoot(AZStd::move(root))
|
||||
, m_recursive(recursive)
|
||||
{
|
||||
}
|
||||
|
||||
bool ReadChanges();
|
||||
|
||||
AZStd::unique_ptr<OVERLAPPED> m_overlapped; // Identifies this root watch
|
||||
HandleUniquePtr m_directoryHandle;
|
||||
QString m_directoryRoot;
|
||||
bool m_recursive;
|
||||
AZStd::aligned_storage_t<64 * 1024, sizeof(DWORD)> m_fileNotifyInformationList{};
|
||||
};
|
||||
|
||||
enum class EventType
|
||||
{
|
||||
FileRead,
|
||||
Shutdown
|
||||
};
|
||||
|
||||
AZStd::unordered_map<LPOVERLAPPED, FolderRootWatch> m_folderRootWatches;
|
||||
|
||||
HandleUniquePtr m_ioHandle{CreateIoCompletionPort(INVALID_HANDLE_VALUE, nullptr, /*CompletionKey =*/ static_cast<ULONG_PTR>(EventType::FileRead), 1)};
|
||||
};
|
||||
Reference in New Issue
Block a user