This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CMEM question regarding allocation.

1) When a request for a memory allocation is made does CMEM look for the first pool with blocks equal to or greater than the amount requested?  This is how I assume it works.

2) If so then when it finds a suitable pool and it's empty does CMEM continue looking for a pool with larger blocks that has an available buffer?  I am assuming that it doesn't and fails at the first suitable empty pool.

John A

  • John,

    CMEM_alloc() (which is called by Codec Engine's Memory_contigAlloc() API), when called with params->type == CMEM_POOL, first calls down to the cmemk.ko device driver to find a pool suitable for the allocation, and then it does an pool-based allocation with that explicit pool.  The first call uses a "best fit" strategy, meaning that it will find the smallest pool size with available buffers.

    So, for you questions...

    1) I suppose the correct answer is "no".  CMEM does not simply go with the first pool that has blocks that are big enough, it goes through all the pools with available blocks and sees which is the best fit.

    2) CMEM will not find an empty pool.  The first call down to the device driver will either:
        a) return a pool number with an available block that is used in the subsequent "getPool" internal call, or
        b) return a failure code indicating that there are no available buffers of any size that are big enough for the requested size.

    Regards,

    - Rob

     

  • Robert,

    Thanks for the reply.  I worded my first question a little wrong.  I expected a pool of equal size requested to be the highest priority.  I just wondered if  pools with increasing buffer sizes would be search sequentially.  It appeared that if it found a pool with an adequate buffer, but the pool was empty then it would return failure.

    I'm surprised that you said if would find *any* pool large enough, because that doesn't seem to be the case with my testing.  I used CE_DEBUG=2 piped to "grep Memory_contigAlloc" and found many larger buffers not being used but a failure to get a much smaller buffer.

    John A 

  • John,

    John Anderson said:
    Thanks for the reply.  I worded my first question a little wrong.  I expected a pool of equal size requested to be the highest priority.  I just wondered if  pools with increasing buffer sizes would be search sequentially.  It appeared that if it found a pool with an adequate buffer, but the pool was empty then it would return failure.

    I'm not sure what you're saying here.  Since CMEM uses the "best fit" strategy, it will choose an exact match (in size) if there is a buffer available in the "exact match" pool.  The "best fit" search applies to pools with available buffers.  Internally to CMEM_alloc(), it first calls down to the driver to find the pool index with an available buffer that satisfies the allocation size.  This call will not return a pool with no available buffers.

    Having said that, there is a corner case I can see when concurrent CMEM allocations are happening.  If CMEM_alloc() finds a pool with an available buffer, and for the sake of argument this pool has only one buffer available, and before it actually allocates this buffer another request preempts this one, then it's possible that the 2nd request "wins" and steals this buffer out from under the 1st request, resulting in the 1st CMEM request failing to obtain the buffer from the pool which CMEM *just* said had an available buffer.

    John Anderson said:
    I'm surprised that you said if would find *any* pool large enough, because that doesn't seem to be the case with my testing.  I used CE_DEBUG=2 piped to "grep Memory_contigAlloc" and found many larger buffers not being used but a failure to get a much smaller buffer.

    I would need to see that CE_DEBUG output to comment further on this (without the "grep Memory_contigAlloc() filtering).  CMEM certainly "promotes" requested sizes to the next larger size with an available buffer.  For example, if you had 1x4096,1x65536 for your "pools" and started with a fresh system and then did two CMEM_alloc(4096), they would both succeed - the first request gets the 4096 buffer and the 2nd request gets the much-larger-than-needed 65536 buffer.  Please explain why you think such behaviour is not happening (assuming I've read you right).

    Regards,

    - Rob

     

  • Robert,

    Seems like the grep method is inadequate.  I found that by looking in /proc/modules/cmem I could get a better view of buffers free and used.  "Best fit" matches what I said above.  You start with the pool of buffers that are the smallest needed to fulfill the request and then move up to larger buffers if the pool has no free buffers.

    If I have more definitive information regarding this, I'll post back.

    Thanks, John A