Sign in to follow this  

what about my server memory pool?

This topic is 3720 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

My memory pool has a 3 layer structure. 1. The bottom layer : CMemoryChunk. When a CMemoryChunk object is inited, 64K( or multiple of 64K, if block is larger than this number ) is commited with VitualAllocate. Each chunk is divied into fixed size blocks(1k-8M). The first byte of a free block is used to store the index of the next free block. 2. CFixedAllocator : Use several CMemoryChunks to allocate size-fixed blocks. Init new chunks if needed. 3. CMemoryCatch : At startup time, allocate an array of CFixedAllocator. I expect 8M max allocate unit, so the array has 8K elements. The first CFixedAllocator allocate 1k, the second 2K... When an allocation request comes, I expand the size+4 to multiply of 1Ks. The first DWORD used to store the number of 1Ks( or the index of CFixedAllocator ) When a deallocation request comes, I just find the index and push the address into a std::vector inside the right CFixedAllocator. So this vector will be considered first to allocate memory. At last, there are paddings. If the block size is 9K, each chunk contains 7 blocks and a 1k padding. If the block size is 80K, the chunk( 64*2K ) just has one block and 128-80=48 1k paddings. All of the 1k paddings got their pointers pushed into the first CFixedAllocator`s vector to allocate 1k memorys. After looking into the memory usage, I found that the most frequently allocated blocks are 1K 7K and 9K(RecvBuffer). A few number of 80k or 90k used to allocate socket pool or client pool at init time. For multithread things, each CFixedAllocator has a lock. What about my design? I need ur suggestions!

Share this post


Link to post
Share on other sites
Your design seems fine, its a close approximation to how my memory manager performs.

But, you haven't really said what design principals you have adopted when implementing your memory manager. For example, my manager is for performance purposes and is actually quite lazy when it comes to deallocating memory, it also assumes the system it is running on has an infinite amount of memory and will take vast amounts of resources all in the name of performance. The best part of the system is that I can clear the entire memory pool by reseting a few pointers to zero.

The question is, what do you want from your memory manager; performance or efficency; there is always a compromise between these two when implementing a memory manager, you can balance it nicely, or you can favour one for the other; it all comes down to the way you want it too behave.

The only piece of advise I can give is that your scheme should be simple, and not too complicated; you can get too involved in how to manage fragmented memory, and how everything should be structured when something is deallocated.

Share this post


Link to post
Share on other sites
Now, this memory pool is used in our MMORPG Login server. We plan to run our server in a system will several GBs of physical memory and 2 physical CPUs(4 logical ones). The login server should be able to cantain at least 3000 client login infos. Then performance is my heart.

I really worry about the locks. Allthough I have a single lock for each fixed allocator, but the allocation pattern concentrate on a few conditions( 1k and 9k ). socket must wait for ohters in recv and send. Maybe I should dive the single memory pool into small pools with the same structure and each thread( 4 IOCP worker threads and one task dispatching thread ) gets one of them.

Share this post


Link to post
Share on other sites
Quote:
After looking into the memory usage, I found that the most frequently allocated blocks are 1K 7K and 9K(RecvBuffer). A few number of 80k or 90k used to allocate socket pool or client pool at init time.


Why do you need dynamic allocations here?

You can receive into a per-connection pre-allocated buffer. You might only need to copy data to dispatch it.

Share this post


Link to post
Share on other sites
What's wrong with operator new? (Or, for that matter, malloc() ?)

Unless you have measured a performance problem with new or malloc, why replace them? Modern implementations are very good, and may out-perform the design you describe.

If you want to cluster allocations, then creating one pool per allocation size (typically, per object type) is often a lot cleaner -- you just have a template allocator that allocates a block of fixed-size objects at a time. The objects so managed would derive from a template base that gives it the proper operator new and delete.

Btw: 4-byte alignment isn't very efficient on modern CPUs. SSE needs 16-byte alignment, and even regular structure layout may benefit from 8-byte alignment.

Share this post


Link to post
Share on other sites
I am talking about Ks not Bytes.

the default operator new or malloc allocates memroy from the process`s default heap. Heaps are managed by system heap managers, which are optimized for small memory allocation. There are 128 look-aside lists per heap, which handle allocations up to 1 KB on 32-bit platforms and up to 2 KB on 64-bit
platforms. And U see that, my allocation granularity is 1K, so one or two look-aside list will be over-burden while others empty.

And the fragmentation is the biggest problem.

Share this post


Link to post
Share on other sites
There are other implementations of malloc you can use.

Anyway, to combat fragmentation, one allocator per (rounded) allocation size is hard to beat for performance and simplicity.

But if yours works for your workload, and is already debugged, no reason to switch now.

Share this post


Link to post
Share on other sites

This topic is 3720 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this