Files
UnrealEngineUWP/Engine/Source/Developer/DerivedDataCache/Private/FileSystemDerivedDataBackend.cpp
Andrew Grant 98ee5066e7 Copying //UE4/Orion-Staging to //UE4/Main (Origin //Orion/Dev-General @ 2861092)
#lockdown Nick.Penwarden

==========================
MAJOR FEATURES + CHANGES
==========================

Change 2861045 on 2016/02/09 by Marcus.Wassmer

	Fix debug editor crash from async compute creating commands when it shouldn't.
	#rb none
	#test debug editor

Change 2861030 on 2016/02/09 by Michael.Noland

	Engine: Added support for debugging safe zones (visualization on any platform and simulation on platforms that don't natively provide safe zone information)

	r.DebugSafeZone.Mode controls the debug visualization overlay (0..2, default 0)
	- 0: Do not display the safe zone overlay
	- 1: Display the overlay for the title safe zone
	- 2: Display the overlay for the action safe zone

	r.DebugSafeZone.OverlayAlpha controls how opaque the debug visualization overlay is (0..1, default 0.3)

	On platforms that don't natively support safe zones, you can simulate a safe zone for quick/easy testing in the editor:
	- r.DebugSafeZone.TitleRatio controls the title safe zone margins returned in FDisplayMetrics
	- r.DebugActionZone.ActionRatio controls the action safe zone margins returned in FDisplayMetrics
	- These both range from 0..1, and default to 1 indicating 100% of the display is safe. A typical test value would be 0.9

	#codereview josh.adams
	#rb marcus.wassmer
	#tests Tested on Win64 uncooked and PS4 cooked (front-end and game)

Change 2860923 on 2016/02/09 by Andrew.Grant

	Fix client warning about HTTPChunkInstaller module not existing
	#rb none
	#tests ran Win64 client

Change 2860852 on 2016/02/09 by Daniel.Wright

	Fixed crash enabling capsule direct shadows in BP
	#rb Nick.Penwarden
	#tests Editor

Change 2860842 on 2016/02/09 by Marcus.Wassmer

	MallocLeakDetection proxy
	#rb Steve.Robb
	#test PS4/PC testing all commands.

Change 2860744 on 2016/02/09 by Josh.Markiewicz

	#UE4 - fixed possible crash when refresh auth with invalid response
	#rb sam.zamani
	#tests login flow
	#codereview justin.sargent, joe.wilcox, pter.knepley, ben.zeigler

Change 2860739 on 2016/02/09 by Laurent.Delayen

	Sync Markers
	- Reset SyncGroups every frame.
	- ::GetSyncGroupPosition() makes sure there is a valid MarkerSyncContext.

	=> Fixes SyncGroup returning 'valid' positions for TransitionLeaders that were not in between sync markers.

	#rb martin.wilson
	#codereview lina.halper
	#tests new riftmage and kurohane networked in PIE

Change 2860736 on 2016/02/09 by Daniel.Lamb

	Fixed issue with iterative cook on the fly invalidating cooked content all the time.
	#rb Marcus.Wassmer
	#test Cook on the fly iterative ps4

Change 2860598 on 2016/02/09 by Joe.Graf

	Simple log category change to match existing log messages in LoadMap

	#rb: n/a
	#test: loading, cooking, game

Change 2860559 on 2016/02/09 by Zak.Middleton

	#orion - Add flag to AIController to control whether it copies the Pawn rotation to ControlRotation if there is no focus point.

	#rb Lukasz.Furman
	#tests PIE ded server AI with lanes

Change 2860462 on 2016/02/09 by Marc.Audy

	Build system improvements
	* Added details to Empty manifest file save error
	* Removed redundent pseudo-dependencies from -showdependency output
	* Monolithic Kinds now a set and branch hacker can specify kind not to build
	#rb Ben.Marsh
	#tests Preflight

Change 2860434 on 2016/02/09 by David.Ratti

	NaN checks:
	-Targeting mode checks in orion code
	-Changed the AActor::SetTransform NaN check so that the logging is included in the NaN ensure (rather than getting cut off from the log afterwards).

	#rb FrankG
	#tests golden path vs bots

Change 2860390 on 2016/02/09 by Michael.Trepka

	Adjust 3D rendering resolution so that it stays approximately the same when user switches display modes

	#rb none
	#tests Tested editor build on PC

Change 2860364 on 2016/02/09 by Justin.Sargent

	Removed unused editor-only functions causing compiler errors when compiling the game.

	#rb keli
	#tests none

Change 2860242 on 2016/02/09 by Justin.Sargent

	Made a number of DialogueWave quality of life improvements to the editor and specifically SoundCue editing.

	New right-click option on SoundWaves to create a DialogueWave

[CL 2863630 by Andrew Grant in Main branch]
2016-02-11 14:39:50 -05:00

325 lines
12 KiB
C++

// Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.
#include "Core.h"
#include "DerivedDataBackendInterface.h"
#include "DDCCleanup.h"
#define MAX_BACKEND_KEY_LENGTH (120)
#define MAX_BACKEND_NUMBERED_SUBFOLDER_LENGTH (9)
#if PLATFORM_LINUX // PATH_MAX on Linux is 4096 (getconf PATH_MAX /, also see limits.h), so this value can be larger (note that it is still arbitrary).
// This should not affect sharing the cache between platforms as the absolute paths will be different anyway.
#define MAX_CACHE_DIR_LEN (3119)
#else
#define MAX_CACHE_DIR_LEN (119)
#endif // PLATFORM_LINUX
#define MAX_CACHE_EXTENTION_LEN (4)
/**
* Cache server that uses the OS filesystem
* The entire API should be callable from any thread (except the singleton can be assumed to be called at least once before concurrent access).
**/
class FFileSystemDerivedDataBackend : public FDerivedDataBackendInterface
{
public:
/**
* Constructor that should only be called once by the singleton, grabs the cache path from the ini
* @param InCacheDirectory directory to store the cache in
* @param bForceReadOnly if true, do not attempt to write to this cache
*/
FFileSystemDerivedDataBackend(const TCHAR* InCacheDirectory, bool bForceReadOnly, bool bTouchFiles, bool bPurgeTransientData, bool bDeleteOldFiles, int32 InDaysToDeleteUnusedFiles, int32 InMaxNumFoldersToCheck, int32 InMaxContinuousFileChecks)
: CachePath(InCacheDirectory)
, bReadOnly(bForceReadOnly)
, bFailed(true)
, bTouch(bTouchFiles)
, bPurgeTransient(bPurgeTransientData)
, DaysToDeleteUnusedFiles(InDaysToDeleteUnusedFiles)
{
// If we find a platform that has more stingent limits, this needs to be rethought.
static_assert(MAX_BACKEND_KEY_LENGTH + MAX_CACHE_DIR_LEN + MAX_BACKEND_NUMBERED_SUBFOLDER_LENGTH + MAX_CACHE_EXTENTION_LEN < PLATFORM_MAX_FILEPATH_LENGTH,
"Not enough room left for cache keys in max path.");
const double SlowInitDuration = 10.0;
double AccessDuration = 0.0;
check(CachePath.Len());
FPaths::NormalizeFilename(CachePath);
const FString AbsoluteCachePath = IFileManager::Get().ConvertToAbsolutePathForExternalAppForRead(*CachePath);
if (AbsoluteCachePath.Len() > MAX_CACHE_DIR_LEN)
{
const FText ErrorMessage = FText::Format( NSLOCTEXT("DerivedDataCache", "PathTooLong", "Cache path {0} is longer than {1} characters...please adjust [DerivedDataBackendGraph] paths to be shorter (this leaves more room for cache keys)."), FText::FromString( AbsoluteCachePath ), FText::AsNumber( MAX_CACHE_DIR_LEN ) );
FMessageDialog::Open(EAppMsgType::Ok, ErrorMessage);
UE_LOG(LogDerivedDataCache, Fatal, TEXT("%s"), *ErrorMessage.ToString());
}
if (!bReadOnly)
{
double TestStart = FPlatformTime::Seconds();
FString TempFilename = CachePath / FGuid::NewGuid().ToString() + ".tmp";
FFileHelper::SaveStringToFile(FString("TEST"),*TempFilename);
int32 TestFileSize = IFileManager::Get().FileSize(*TempFilename);
if (TestFileSize < 4)
{
UE_LOG(LogDerivedDataCache, Warning, TEXT("Fail to write to %s, derived data cache to this directory will be read only."),*CachePath);
}
else
{
bFailed = false;
}
if (TestFileSize >= 0)
{
IFileManager::Get().Delete(*TempFilename, false, false, true);
}
AccessDuration = FPlatformTime::Seconds() - TestStart;
}
if (bFailed)
{
double StartTime = FPlatformTime::Seconds();
TArray<FString> FilesAndDirectories;
IFileManager::Get().FindFiles(FilesAndDirectories,*(CachePath / TEXT("*.*")), true, true);
AccessDuration = FPlatformTime::Seconds() - StartTime;
if (FilesAndDirectories.Num() > 0)
{
bReadOnly = true;
bFailed = false;
}
}
if (FString(FCommandLine::Get()).Contains(TEXT("DerivedDataCache")) )
{
bTouch = true; // we always touch files when running the DDC commandlet
}
// The command line (-ddctouch) enables touch on all filesystem backends if specified.
bTouch = bTouch || FParse::Param(FCommandLine::Get(), TEXT("DDCTOUCH"));
if (bReadOnly)
{
bTouch = false; // we won't touch read only paths
}
if (bTouch)
{
UE_LOG(LogDerivedDataCache, Display, TEXT("Files in %s will be touched."),*CachePath);
}
if (!bFailed && AccessDuration > SlowInitDuration)
{
UE_LOG(LogDerivedDataCache, Warning, TEXT("%s access is very slow (initialization took %.2lf seconds), consider disabling it."), *CachePath, AccessDuration);
}
if (!bReadOnly && !bFailed && bDeleteOldFiles && !FParse::Param(FCommandLine::Get(),TEXT("NODDCCLEANUP")) && FDDCCleanup::Get())
{
FDDCCleanup::Get()->AddFilesystem( CachePath, InDaysToDeleteUnusedFiles, InMaxNumFoldersToCheck, InMaxContinuousFileChecks );
}
}
/** return true if the cache is usable **/
bool IsUsable()
{
return !bFailed;
}
/** return true if this cache is writable **/
virtual bool IsWritable() override
{
return !bReadOnly;
}
/**
* Synchronous test for the existence of a cache item
*
* @param CacheKey Alphanumeric+underscore key of this cache item
* @return true if the data probably will be found, this can't be guaranteed because of concurrency in the backends, corruption, etc
*/
virtual bool CachedDataProbablyExists(const TCHAR* CacheKey) override
{
#if ENABLE_DDC_STATS
static FName NAME_CachedDataProbablyExists(TEXT("CachedDataProbablyExists"));
FDDCScopeStatHelper Stat(CacheKey, NAME_CachedDataProbablyExists);
static FName NAME_FileDDCPath(TEXT("FileDDCPath"));
Stat.AddTag(NAME_FileDDCPath, CachePath);
#endif
check(!bFailed);
FString Filename = BuildFilename(CacheKey);
FDateTime TimeStamp = IFileManager::Get().GetTimeStamp(*Filename);
bool bExists = TimeStamp > FDateTime::MinValue();
if (bExists)
{
// Update file timestamp to prevent it from being deleted by DDC Cleanup.
if (bTouch ||
(!bReadOnly && (FDateTime::UtcNow() - TimeStamp).GetDays() > (DaysToDeleteUnusedFiles / 4)))
{
IFileManager::Get().SetTimeStamp(*Filename, FDateTime::UtcNow());
}
}
return bExists;
}
/**
* Synchronous retrieve of a cache item
*
* @param CacheKey Alphanumeric+underscore key of this cache item
* @param OutData Buffer to receive the results, if any were found
* @return true if any data was found, and in this case OutData is non-empty
*/
virtual bool GetCachedData(const TCHAR* CacheKey, TArray<uint8>& Data) override
{
#if ENABLE_DDC_STATS
static FName NAME_GetCachedData(TEXT("GetCachedData"));
FDDCScopeStatHelper Stat(CacheKey, NAME_GetCachedData);
static FName NAME_FileDDCPath(TEXT("FileDDCPath"));
static FName NAME_Retrieved(TEXT("Retrieved"));
Stat.AddTag(NAME_FileDDCPath, CachePath);
#endif
check(!bFailed);
FString Filename = BuildFilename(CacheKey);
double StartTime = FPlatformTime::Seconds();
if (FFileHelper::LoadFileToArray(Data,*Filename,FILEREAD_Silent))
{
double ReadDuration = FPlatformTime::Seconds() - StartTime;
double ReadSpeed = ReadDuration > 5.0 ? (Data.Num() / ReadDuration) / (1024.0 * 1024.0) : 100.0;
// Slower than 0.5MB/s?
UE_CLOG(ReadSpeed < 0.5, LogDerivedDataCache, Warning, TEXT("%s is very slow (%.2lfMB/s) when accessing %s, consider disabling it."), *CachePath, ReadSpeed, *Filename);
UE_LOG(LogDerivedDataCache, Verbose, TEXT("FileSystemDerivedDataBackend: Cache hit on %s"),*Filename);
#if ENABLE_DDC_STATS
Stat.AddTag(NAME_Retrieved, true);
#endif
return true;
}
UE_LOG(LogDerivedDataCache, Verbose, TEXT("FileSystemDerivedDataBackend: Cache miss on %s"),*Filename);
Data.Empty();
#if ENABLE_DDC_STATS
Stat.AddTag(NAME_Retrieved, false);
#endif
return false;
}
/**
* Asynchronous, fire-and-forget placement of a cache item
*
* @param CacheKey Alphanumeric+underscore key of this cache item
* @param OutData Buffer containing the data to cache, can be destroyed after the call returns, immediately
* @param bPutEvenIfExists If true, then do not attempt skip the put even if CachedDataProbablyExists returns true
*/
virtual void PutCachedData(const TCHAR* CacheKey, TArray<uint8>& Data, bool bPutEvenIfExists) override
{
#if ENABLE_DDC_STATS
static FName NAME_PutCachedData(TEXT("PutCachedData"));
FDDCScopeStatHelper Stat(CacheKey, NAME_PutCachedData);
static FName NAME_FileDDCPath(TEXT("FileDDCPath"));
Stat.AddTag(NAME_FileDDCPath, CachePath);
#endif
check(!bFailed);
if (!bReadOnly)
{
if (bPutEvenIfExists || !CachedDataProbablyExists(CacheKey))
{
check(Data.Num());
FString Filename = BuildFilename(CacheKey);
FString TempFilename(TEXT("temp."));
TempFilename += FGuid::NewGuid().ToString();
TempFilename = FPaths::GetPath(Filename) / TempFilename;
bool bResult;
{
bResult = FFileHelper::SaveArrayToFile(Data, *TempFilename);
}
if (bResult)
{
if (IFileManager::Get().FileSize(*TempFilename) == Data.Num())
{
bool DoMove = !CachedDataProbablyExists(CacheKey);
if (bPutEvenIfExists && !DoMove)
{
DoMove = true;
RemoveCachedData(CacheKey, /*bTransient=*/ false);
}
if (DoMove)
{
if (!IFileManager::Get().Move(*Filename, *TempFilename, true, true, false, true))
{
UE_LOG(LogDerivedDataCache, Log, TEXT("FFileSystemDerivedDataBackend: Move collision, attempt at redundant update, OK %s."),*Filename);
}
else
{
UE_LOG(LogDerivedDataCache, Verbose, TEXT("FFileSystemDerivedDataBackend: Successful cache put to %s"),*Filename);
}
}
}
else
{
UE_LOG(LogDerivedDataCache, Warning, TEXT("FFileSystemDerivedDataBackend: Temp file is short %s!"),*TempFilename);
}
}
else
{
UE_LOG(LogDerivedDataCache, Warning, TEXT("FFileSystemDerivedDataBackend: Could not write temp file %s!"),*TempFilename);
}
// if everything worked, this is not necessary, but we will make every effort to avoid leaving junk in the cache
if (FPaths::FileExists(TempFilename))
{
IFileManager::Get().Delete(*TempFilename, false, false, true);
}
}
}
}
void RemoveCachedData(const TCHAR* CacheKey, bool bTransient) override
{
check(!bFailed);
if (!bReadOnly && (!bTransient || bPurgeTransient))
{
FString Filename = BuildFilename(CacheKey);
if (bTransient)
{
UE_LOG(LogDerivedDataCache,Verbose,TEXT("Deleting transient cached data. Key=%s Filename=%s"),CacheKey,*Filename);
}
IFileManager::Get().Delete(*Filename, false, false, true);
}
}
private:
/**
* Threadsafe method to compute the filename from the cachekey, currently just adds a path and an extension.
*
* @param CacheKey Alphanumeric+underscore key of this cache item
* @return filename built from the cache key
*/
FString BuildFilename(const TCHAR* CacheKey)
{
FString Key = FString(CacheKey).ToUpper();
for (int32 i = 0; i < Key.Len(); i++)
{
check(FChar::IsAlnum(Key[i]) || FChar::IsUnderscore(Key[i]) || Key[i] == L'$');
}
uint32 Hash = FCrc::StrCrc_DEPRECATED(*Key);
// this creates a tree of 1000 directories
FString HashPath = FString::Printf(TEXT("%1d/%1d/%1d/"),(Hash/100)%10,(Hash/10)%10,Hash%10);
return CachePath / HashPath / Key + TEXT(".udd");
}
/** Base path we are storing the cache files in. **/
FString CachePath;
/** If true, do not attempt to write to this cache **/
bool bReadOnly;
/** If true, we failed to write to this directory and it did not contain anything so we should not be used **/
bool bFailed;
/** If true, CachedDataProbablyExists will update the file timestamps. */
bool bTouch;
/** If true, allow transient data to be removed from the cache. */
bool bPurgeTransient;
/** Age of file when it should be deleted from DDC cache. */
int32 DaysToDeleteUnusedFiles;
};
FDerivedDataBackendInterface* CreateFileSystemDerivedDataBackend(const TCHAR* CacheDirectory, bool bForceReadOnly /*= false*/, bool bTouchFiles /*= false*/, bool bPurgeTransient /*= false*/, bool bDeleteOldFiles /*= false*/, int32 InDaysToDeleteUnusedFiles /*= 60*/, int32 InMaxNumFoldersToCheck /*= -1*/, int32 InMaxContinuousFileChecks /*= -1*/)
{
FFileSystemDerivedDataBackend* FileDDB = new FFileSystemDerivedDataBackend(CacheDirectory, bForceReadOnly, bTouchFiles, bPurgeTransient, bDeleteOldFiles, InDaysToDeleteUnusedFiles, InMaxNumFoldersToCheck, InMaxContinuousFileChecks);
if (!FileDDB->IsUsable())
{
delete FileDDB;
FileDDB = NULL;
}
return FileDDB;
}