Files
UnrealEngineUWP/Engine/Source/Developer/DirectoryWatcher/Private/Linux/DirectoryWatchRequestLinux.h
Michael Sartain c52bda1302 Clean up Linux inotify warnings and potential leaks
We still have some duplicate inotify watches, but this first pass will spew a lot more information when we hit inotify limits.

Adds a "DumpINotifyStats" command in non-release builds
  Spews global inotify & UE stats, along with physical count of directories, etc.

Canonicalize directory path in FDirectoryWatcherLinux::RegisterDirectoryChangedCallback_Handle
  Shootergame was adding 141 duplicate watches for Samples/Games/ShooterGame/Content w/o this. Was 1136, is now 995.

Change PathsToWatchDescriptors tmap to PathNameHashSet
  Don't need to store full paths for each watch directory twice

Fix bugs in TestPAL in addition to adding DumpStats() command, which looks ~ like this:

LogDirectoryWatcher: Warning: inotify limits
LogDirectoryWatcher: Warning:   max_queued_events: 16384
LogDirectoryWatcher: Warning:   max_user_instances: 128
LogDirectoryWatcher: Warning:   max_user_watches: 65536
LogDirectoryWatcher: Warning: inotify per-process stats
LogDirectoryWatcher: Warning:   systemd (pid 2239) watches:23 instances:3
...
LogDirectoryWatcher: Warning:   plugin_host-3.3 (pid 395041) watches:62 instances:1
LogDirectoryWatcher: Warning:   plugin_host-3.8 (pid 395044) watches:62 instances:1
LogDirectoryWatcher: Warning:   TestPAL (pid 396852) watches:2 instances:1
LogDirectoryWatcher: Warning: Total inotify Watches:392 Instances:28
LogDirectoryWatcher: Warning: Current watch requests
LogDirectoryWatcher: Warning:   /var/tmp/DirectoryWatcherTest396852: 2 watches
LogDirectoryWatcher: Warning:   Total count:2

The above is also dumped (once) when we fail to init or add a inotify watch.

Need to create better documentation and add a pointer to it, similar to what VSCode does: (hat tip Brandon)

    https://code.visualstudio.com/docs/setup/linux#_visual-studio-code-is-unable-to-watch-for-file-changes-in-this-large-workspace-error-enospc

Related bugs:

; FPS BP Cooking Content - errno = 28, Out of inotify watches
https://jira.it.epicgames.com/browse/UE-125210

; inotify Warnings when Cooking Content for Linux
https://jira.it.epicgames.com/browse/UE-119696

; Time Niagara Sequencer failed to play | Error: Couldn't find file for package
https://jira.it.epicgames.com/browse/UE-89750

; inotify warnings from Linux command line builds
https://jira.it.epicgames.com/browse/UE-76562

#review-17483609 @Brandon.Schaefer, @James.Singer
#jira UE-76562, UE-89750, UE-119696, UE-125210

[CL 17498916 by Michael Sartain in ue5-main branch]
2021-09-13 19:04:54 -04:00

76 lines
2.3 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "IDirectoryWatcher.h"
#include <sys/inotify.h>
class FDirectoryWatchRequestLinux
{
public:
FDirectoryWatchRequestLinux();
virtual ~FDirectoryWatchRequestLinux();
/** Sets up the directory handle and request information */
bool Init(const FString& InDirectory, uint32 Flags);
/** Adds a delegate to get fired when the directory changes */
FDelegateHandle AddDelegate( const IDirectoryWatcher::FDirectoryChanged& InDelegate, uint32 Flags );
/** Removes a delegate to get fired when the directory changes */
bool RemoveDelegate( FDelegateHandle InHandle );
/** Returns true if this request has any delegates listening to directory changes */
bool HasDelegates() const;
/** Prepares the request for deletion */
void EndWatchRequest();
/** Call ProcessPendingNotifications on each delegate */
static void ProcessNotifications(TMap<FString, FDirectoryWatchRequestLinux*>& RequestMap);
/** Dump inotify stats */
static void DumpStats(TMap<FString, FDirectoryWatchRequestLinux*>& RequestMap);
private:
/** Triggers all pending file change notifications */
void ProcessPendingNotifications();
/** Adds watches for all files (and subdirectories) in a directory. */
void WatchDirectoryTree(const FString& RootAbsolutePath);
/** Removes all watches for path */
void UnwatchDirectoryTree(const FString& RootAbsolutePath);
void Shutdown();
void ProcessChanges(TArray<TPair<FFileChangeData, bool>>& FileChanges);
static void SetINotifyErrorMsg(const FString &ErrorMsg);
static void DumpINotifyErrorDetails(TMap<FString, FDirectoryWatchRequestLinux*>& RequestMap);
private:
/** Whether or not watch subtree. */
bool bWatchSubtree;
/** EndWatchRequest called? */
bool bEndWatchRequestInvoked;
/** INotify file descriptor */
int FileDescriptor;
/** Absolute path to our root watch directory */
FString WatchDirectory;
/** Mapping from watch descriptors to watched directory names */
TMap<int32, FString> WatchDescriptorsToPaths;
/** Set of hashed directory names we're watching */
TSet<uint32> PathNameHashSet;
/** A delegate with its corresponding IDirectoryWatcher::WatchOptions flags */
typedef TPair<IDirectoryWatcher::FDirectoryChanged, uint32> FWatchDelegate;
TArray<FWatchDelegate> Delegates;
};