Unreal 技术笔记

unreal 项目,IOS下排查错误: Memory mappe

2023-09-14  本文已影响0人  子夜书案

现象:

调查:

1,错误定位:

报错位置在 Engine\Source\Runtime\CoreUObject\Private\Serialization\BulkData2.cpp line:1681
很明显是个 check 失败,内容如下:

checkf(IsAligned(MappedRegion->GetMappedPtr(), FPlatformProperties::GetMemoryMappingAlignment()), TEXT("Memory mapped file has the wrong alignment!"));

ISAligned 从字面含义来看就是内存是否对齐,具体代码内容如下:

/**
 * Checks if a pointer is aligned to the specified alignment.
 *
 * @param  Val        The value to align.
 * @param  Alignment  The alignment value, must be a power of two.
 *
 * @return true if the pointer is aligned to the specified alignment, false otherwise.
 */
template <typename T>
FORCEINLINE constexpr bool IsAligned(T Val, uint64 Alignment)
{
    static_assert(TIsIntegral<T>::Value || TIsPointer<T>::Value, "IsAligned expects an integer or pointer type");

    return !((uint64)Val & (Alignment - 1));
}

那这个错误基本就是内存没对齐造成的问题,现在只要搞清楚为什么只在 IOS 会报这个错误。

2,错误排查:

通过阅读上面代码,可以得知,对齐方式是通过 FPlatformProperties::GetMemoryMappingAlignment() 来确定的。在 unreal 中,一看到 FPlatform 下意识就会想到,一开始的入口并不是最终的调用入口,下面肯定隐藏着各平台的实现。果不然,在项目中分别找到了各个实现:

#ifdef PROPERTY_HEADER_SHOULD_DEFINE_TYPE
typedef FIOSPlatformProperties FPlatformProperties;
#endif

PROPERTY_HEADER_SHOULD_DEFINE_TYPE 定义在 Engine\Source\Runtime\Core\Public\HAL\PlatformProperties.h

// note that this is not defined to 1 like normal, because we don't want to have to define it to 0 whenever
// the Properties.h files are included in all other places, so just use #ifdef not #if in this special case
#define PROPERTY_HEADER_SHOULD_DEFINE_TYPE

完整结合起来的含义就是:

那么 IOS 平台下,GetMemoryMappingAlignment 是怎么定义的?代码在 Engine\Source\Runtime\Core\Public\IOS\IOSPlatformProperties.h line:83:

static FORCEINLINE int64 GetMemoryMappingAlignment()
{
    return 16384;
}

对,你没看出,就是写死在代码中,固定为 16384,16进制表示则为:0X4000
其它平台没有单独实现,调用的是默认值 0 。

3,unreal pak 流程中是怎么处理这个问题的?

问题找到这就很好奇,unreal 的 pak 流程中是怎么处理这个对齐问题的,于是翻开项目,找到了 Engine\Source\Programs\AutomationTool\Scripts\CopyBuildToStagingDirectory.Automation.cs文件,在其中搜索 MemoryMappedFiles便能找到这么段代码:

string BulkOption = "";
{
    ConfigHierarchy PlatformEngineConfig;
    if (Params.EngineConfigs.TryGetValue(SC.StageTargetPlatform.PlatformType, out PlatformEngineConfig))
    {
        bool bMasterEnable = false;
        PlatformEngineConfig.GetBool("MemoryMappedFiles", "MasterEnable", out bMasterEnable);
        if (bMasterEnable)
        {
            int Value = 0;
            PlatformEngineConfig.GetInt32("MemoryMappedFiles", "Alignment", out Value);
            if (Value > 0)
            {
                BulkOption = String.Format(" -AlignForMemoryMapping={0}", Value);
            }
        }
    }
}

然后在Engine\Config\IOS\IOSEngine.ini中能找到这样的定义:

[MemoryMappedFiles]
MasterEnable=true
Alignment=16384

ini 中的 16384 和 IOSPlatformProperties.h 中的一致,也就意味着在 unreal pak 过程中传入了 AlignForMemoryMapping 参数。

然后再来看看 Engine\Source\Developer\PakFileUtilities\Private\PakFileUtilities.cpp 中是怎么处理这个参数:

bool bIsMappedBulk = Input.Source.EndsWith(TEXT(".m.ubulk"));
// Align bulk data
if (bIsMappedBulk && CmdLineParameters.AlignForMemoryMapping > 0 && OriginalFileSize != INDEX_NONE && !bDeleted)
{
    if (!IsAligned(NewEntryOffset + NewEntry.Info.GetSerializedSize(FPakInfo::PakFile_Version_Latest), CmdLineParameters.AlignForMemoryMapping))
    {
        int64 OldOffset = NewEntryOffset;
        NewEntryOffset = AlignArbitrary(NewEntryOffset + NewEntry.Info.GetSerializedSize(FPakInfo::PakFile_Version_Latest), CmdLineParameters.AlignForMemoryMapping) - NewEntry.Info.GetSerializedSize(FPakInfo::PakFile_Version_Latest);
        int64 PaddingRequired = NewEntryOffset - OldOffset;

        check(PaddingRequired > 0);
        check(PaddingBuffer && PaddingBufferSize >= PaddingRequired);

        {
            UE_LOG(LogPakFile, Verbose, TEXT("%14llu - %14llu : %14llu bulk padding."), PakFileHandle->Tell(), PakFileHandle->Tell() + PaddingRequired, PaddingRequired);
            PakFileHandle->Serialize(PaddingBuffer, PaddingRequired);
            check(PakFileHandle->Tell() == NewEntryOffset);
        }
    }
}

4, 综合上述线索,我们可以得出结论:

解决方法:

上一篇 下一篇

猜你喜欢

热点阅读