Unity-Technologies / EntityComponentSystemSamples

Other
7.25k stars 1.62k forks source link

Segmentation fault when creating a large number of entities #272

Open PaulGao96 opened 1 year ago

PaulGao96 commented 1 year ago

I wrote a simple test program that creates 100 entities per frame, each entity has a buffer, as follows:

void Execute(Entity projectileEntity, [ChunkIndexInQuery] int chunkIndex, ref LocalTransform transform, in Projectile projectile)
{
    for(int j = 0; j < 100; j++) {
        var e = ECBWriter.CreateEntity(chunkIndex);
        var as_path = ECBWriter.AddBuffer<ASN>(chunkIndex, e);
        for(uint i = 0; i < 10; i++){
            as_path.Add(new ASN { routerID = i });
        }
    }
}

I compiled the program to run on a linux server, after running for some time, it crashed after about 2e8 entities had been created, there are ~1e6 chunks and took up 22GB of RAM, but my linux server had 128GB of RAM. The error is as follows: (Editor: 2022.3.5f1c1 with Entities-1.0.14)

Caught fatal signal - signo:11 code:1 errno:0 addr:(nil)
Segmentation fault (core dumped)

(Editor: 2021.3.11f1c2 with Entities-0.51.1-preview.21)

Caught fatal signal - signo:11 code:1 errno:0 addr:(nil)
Obtained 6 stack frames.
#0  0x007f6752ad9980 in funlockfile
#1  0x007f6705882ac4 in Unity.Entities.ChunkDataUtility.AddEmptyChunk(Unity.Entities.Archetype* archetype, Unity.Entities.Chunk* chunk, Unity.Entities.SharedComponentValues sharedComponentValues) -> void_08f2b62234a514a789dc69881ac7811a
#2  0x007f6705890f29 in Unity.Entities.EntityComponentStore.GetChunkWithEmptySlotsWithAddedComponent(Unity.Entities.EntityComponentStore* this, Unity.Entities.Chunk* srcChunk, Unity.Entities.ComponentType componentType, int sharedComponentIndex) -> Unity.Entities.Chunk*_08f2b62234a514a789dc69881ac7811a
#3  0x007f67058dfaf0 in Unity.Entities.EntityDataAccess.AddComponentDuringStructuralChange(Unity.Entities.EntityDataAccess* this, Unity.Entities.Entity entity, Unity.Entities.ComponentType componentType, ref Unity.Entities.SystemHandleUntyped originSystem) -> bool_08f2b62234a514a789dc69881ac7811a
#4  0x007f67058ddd14 in Unity.Entities.EntityCommandBuffer.EcbWalker`1<Unity.Entities.EntityCommandBuffer.PlaybackProcessor>.ProcessChain(Unity.Entities.EntityCommandBuffer.EcbWalker`1<Unity.Entities.EntityCommandBuffer.PlaybackProcessor>* this, Unity.Entities.ECBChainPlaybackState* chainStates, int currentChain, int nextChain) -> void_08f2b62234a514a789dc69881ac7811a
#5  0x000000404b8e18 in (wrapper managed-to-native) object:wrapper_native_0x7f6705982900 (intptr,int,intptr,int,int)
Segmentation fault (core dumped)

Why is there an error at AddEmptyChunk before the memory ran out?

PaulGao96 commented 1 year ago

I found the reason. Looking through the entities source code, I found this:

internal const long k_MaximumEntitiesPerWorld = 128L * 1024L * 1024L; // roughly 128 million Entities per World, maximum

The number of entities I created exceeded the limit. Can k_MaximumEntitiesPerWorld be set larger?