Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cascaded Multi Size Based Allocators Achiving the 'infinite' space ability as ACE_Malloc #2135

Open
wants to merge 98 commits into
base: master
Choose a base branch
from

Conversation

smithAchang
Copy link

@smithAchang smithAchang commented Sep 23, 2023

ACE_Malloc memory pool has only one linked list,

so when it has a very long list and has various, random chunk size distribution, the cost of its memory management is great.

In our case, the free API of ACE_Malloc has consumed much CPU time.

So I use the ACE_Cascaded_Dynamic_Cached_Allocator class provided in my previous PR to form a allocator hierarchy with various fixed-size achieving the 'infinite' space ability as ACE_Malloc by a cost of O(1).


Main Design

  • Use size-based allocator to reduce the cost of malloc/free

  • Decrease ACE_Cascaded_Dynamic_Cached_Allocator's initial_n_chunks constructor parameter according to the hierarchy level, but
    can be adjusted by a threshold min_initial_n_chunks parameter

  • Increase ACE_Cascaded_Dynamic_Cached_Allocator's chunk_size constructor parameter according to the hierarchy level, it will enable bigger chunks can be malloced

Summary by CodeRabbit

  • New Features

    • Introduced an advanced, multi-level memory management system that optimizes resource allocation to enhance overall performance and reliability.
  • Tests

    • Delivered a comprehensive test suite covering various allocation and deallocation scenarios to ensure robust and consistent system behavior.
    • Added a new test entry for the "Allocator Cascaded Test" to the test suite.

2. using instead of typedef
2、make simple for statement at one line
3、add the pool_sum API for component transparency
4、move pool_depth API from INL file to source file for its complexity
2、modify the test case to use lock strategy, that will more likely find some compiling error than free lock
Copy link
Contributor

coderabbitai bot commented Feb 1, 2025

Walkthrough

This change introduces two new cascaded memory allocator classes into the ACE framework. Both allocators implement dynamic allocation strategies with cascaded approaches and support memory operations using methods such as malloc, calloc, and free. In addition, inline methods are added for locking and pool metrics. The update extends the public API through header modifications and integrates a comprehensive set of tests along with corresponding project configurations, ensuring the new allocation schemes function correctly.

Changes

File Change Summary
ACE/ace/Malloc_T.cpp Added new classes ACE_Cascaded_Dynamic_Cached_Allocator and ACE_Cascaded_Multi_Size_Based_Allocator with constructors, destructors, and methods (malloc, calloc, free, remove, bind, trybind, find, unbind, pool_depth, pool_sum, and dump); added #include <memory>.
ACE/ace/Malloc_T.h Introduced declarations for the new classes and added new header includes (ace/Null_Mutex.h, <vector>) with detailed comments explaining functionality.
ACE/ace/Malloc_T.inl Added inline methods: pool_sum for ACE_Cascaded_Dynamic_Cached_Allocator and mutex methods for both allocator classes.
ACE/tests/Allocator_Cascaded_Test.cpp Added a comprehensive test suite with functions to validate basic API behavior, multi-size allocation, hierarchy management, free operations, and differential scenarios.
ACE/tests/run_test.lst & ACE/tests/tests.mpc Integrated a new test entry (Allocator_Cascaded_Test) into the test list and project file for automated builds.

Sequence Diagram(s)

sequenceDiagram
    participant Client as Client
    participant DCA as CascadedDynamicAllocator
    participant CMA as CascadedMultiSizeAllocator
    
    Client->>DCA: Request malloc(nbytes)
    DCA->>DCA: Check if nbytes ≤ chunk_size
    alt Memory Available in Current Chunk
        DCA-->>Client: Return pointer
    else Memory Not Sufficient
        DCA->>DCA: Create/find child allocator
        DCA-->>Client: Return pointer from child
    end

    Client->>CMA: Request malloc(nbytes)
    CMA->>CMA: Evaluate available block sizes
    alt Suitable Block Found
        CMA-->>Client: Return pointer
    else Create New Allocator as Needed
        CMA->>CMA: Update hierarchy and metrics
        CMA-->>Client: Return pointer
    end
Loading

Poem

In the realm of memory, I joyfully bound,
Cascading allocators are the cleverest found.
With each new malloc hop, my code leaps high,
Like a bunny darting through an endless sky.
Debug and test, the changes now reside—
A hop of delight, in every stride! 🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 204e580 and 5566797.

📒 Files selected for processing (1)
  • ACE/tests/Allocator_Cascaded_Test.cpp (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • ACE/tests/Allocator_Cascaded_Test.cpp
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Codacy Static Code Analysis

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (7)
ACE/ace/Malloc_T.h (2)

215-231: Naming and clarity
The new doc comments explain that the allocator can branch out in a hierarchy. Consider emphasizing any limitations or overhead when many nested allocators exist.


336-425: Cascaded multi-size-based allocator design

  1. Binary search usage: The approach to find the minimal position in the hierarchy is clear.
  2. Resize then allocate: Good practice to resize the vector before allocating. If resize throws, no heap memory is lost.
  3. Single-octet chunk header: Offsets by one byte for the user pointer. This is a well-known approach; just ensure that alignment is always sufficient for all target platforms.
ACE/ace/Malloc_T.cpp (1)

481-515: Binary search
The binary search to find the chunk size is a good optimization. Watch out for edge cases with very small or zero requests. The code returns nullptr if the shift limit is exceeded, which is a safe fallback.

ACE/tests/Allocator_Cascaded_Test.cpp (4)

22-44: Add documentation for the DELTA macro.

While the test macros are well-implemented, the DELTA macro's purpose and calculation could be clearer with documentation.

Add a comment explaining the DELTA macro's purpose and calculation:

+// Calculates the number of chunks for a given hierarchy level.
+// If the calculated chunks (initial_n_chunks >> level) is greater than min_initial_n_chunks,
+// use the calculated value; otherwise, use min_initial_n_chunks.
#define DELTA(level, initial_n_chunks, min_initial_n_chunks) \
  (initial_n_chunks >> level) > min_initial_n_chunks ? (initial_n_chunks >> level) : min_initial_n_chunks

45-170: Use RAII to prevent memory leaks in test cases.

While the tests are thorough, there's a risk of memory leaks if tests fail between malloc and free calls. Consider using RAII to ensure cleanup.

Consider using a simple RAII wrapper:

class ScopedMemory {
public:
  ScopedMemory(void* ptr, ACE_Cascaded_Dynamic_Cached_Allocator<ACE_SYNCH_MUTEX>& alloc)
    : ptr_(ptr), alloc_(alloc) {}
  ~ScopedMemory() { if (ptr_) alloc_.free(ptr_); }
  void* release() { void* tmp = ptr_; ptr_ = nullptr; return tmp; }
private:
  void* ptr_;
  ACE_Cascaded_Dynamic_Cached_Allocator<ACE_SYNCH_MUTEX>& alloc_;
};

// Usage example:
void* ptr = alloc.malloc(nbytes);
ScopedMemory guard(ptr, alloc);
// Test the allocated memory
// Memory is automatically freed when guard goes out of scope

172-521: Reduce code duplication in test functions.

The test functions share similar setup code and assertions. Consider extracting common patterns into test helpers.

Consider creating test helpers:

// Test helper for common allocator setup
template<typename Allocator>
struct AllocatorTestHelper {
  static Allocator create_allocator(size_t initial_n_chunks = 11,
                                  size_t chunk_size = sizeof(void*) + 5,
                                  size_t min_initial_n_chunks = 2) {
    return Allocator(initial_n_chunks, chunk_size, min_initial_n_chunks);
  }
  
  static void verify_pool_metrics(Allocator& alloc,
                                size_t expected_sum,
                                size_t expected_depth,
                                const char* context) {
    std::stringstream ss;
    ss << context << " pool sum must be " << expected_sum;
    ACE_ASSERT_RETURN(alloc.pool_sum() == expected_sum, ss.str().c_str());
    
    ss.str("");
    ss << context << " pool depth must be " << expected_depth;
    ACE_ASSERT_RETURN(alloc.pool_depth() == expected_depth, ss.str().c_str());
  }
};

// Usage example:
using Helper = AllocatorTestHelper<ACE_Cascaded_Multi_Size_Based_Allocator<ACE_SYNCH_MUTEX>>;
auto alloc = Helper::create_allocator();
Helper::verify_pool_metrics(alloc, expected_sum, expected_depth, "After allocation");

523-542: Enhance error reporting in the main test function.

While the test execution is well-structured, it could provide more detailed feedback when tests fail.

Consider enhancing error reporting:

 int
 run_main (int, ACE_TCHAR *[])
 {
   ACE_START_TEST (ACE_TEXT ("Allocator_Cascaded_Test"));
 
   int retval = 0;
+  struct TestCase {
+    const char* name;
+    int (*func)();
+  };
+  TestCase tests[] = {
+    {"Cascaded_Allocator", run_cascaded_allocator_test},
+    {"Cascaded_Multi_Size_Based_Allocator_Basic", run_cascaded_multi_size_based_allocator_basic_test},
+    {"Cascaded_Multi_Size_Based_Allocator_Hierarchy", run_cascaded_multi_size_based_allocator_hierarchy_test},
+    {"Cascaded_Multi_Size_Based_Allocator_Hierarchy_Free", run_cascaded_multi_size_based_allocator_hierarchy_free_test},
+    {"Cascaded_Multi_Size_Based_Allocator_Hierarchy_Differential", run_cascaded_multi_size_based_allocator_hierarchy_differential_test}
+  };
 
-  ACE_DEBUG ((LM_INFO, "%C Run the tests for Cascaded_Allocator ...\n", __func__));
-  retval += run_cascaded_allocator_test();
-
-  ACE_DEBUG ((LM_INFO, "%C Run the tests for Cascaded_Multi_Size_Based_Allocator ...\n", __func__));
-  retval += run_cascaded_multi_size_based_allocator_basic_test();
-  retval += run_cascaded_multi_size_based_allocator_hierarchy_test ();
-  retval += run_cascaded_multi_size_based_allocator_hierarchy_free_test ();
-  retval += run_cascaded_multi_size_based_allocator_hierarchy_differential_test ();
+  for (const auto& test : tests) {
+    ACE_DEBUG ((LM_INFO, "%C Running test: %C\n", __func__, test.name));
+    int result = test.func();
+    if (result != 0) {
+      ACE_ERROR ((LM_ERROR, "%C Test failed: %C\n", __func__, test.name));
+    }
+    retval += result;
+  }
 
   ACE_END_TEST;
 
   return retval;
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6c3ca3f and 204e580.

📒 Files selected for processing (6)
  • ACE/ace/Malloc_T.cpp (2 hunks)
  • ACE/ace/Malloc_T.h (4 hunks)
  • ACE/ace/Malloc_T.inl (1 hunks)
  • ACE/tests/Allocator_Cascaded_Test.cpp (1 hunks)
  • ACE/tests/run_test.lst (1 hunks)
  • ACE/tests/tests.mpc (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Codacy Static Code Analysis
🔇 Additional comments (26)
ACE/ace/Malloc_T.h (6)

8-9: Authors updated successfully
No issues with adding a new author line here.


19-19: Include for ACE_Null_Mutex
Good addition since the new classes use ACE_Null_Mutex.


29-30: STL vector import
Bringing in <vector> is necessary for tracking allocator hierarchies.


210-214: Documentation is clear
The class description is detailed and helps clarify the purpose of the cascaded dynamic allocator.


232-311: Concurrency consideration
Lock usage with ACE_MT(ACE_GUARD) is employed in critical methods. This is good. Verify that concurrent calls to malloc() and free() on different nested allocators won't lead to unexpected cross-allocator state issues.


313-335: Multi-size-based allocator docs
Documentation is succinct and points out the hierarchical approach for multiple chunk sizes. No issues spotted.

ACE/ace/Malloc_T.cpp (14)

17-17: Smart pointer imports
Including <memory> is needed for std::unique_ptr; recommended usage for exception safety.


181-203: Constructor’s exception safety
Storing the newly allocated child allocator in a std::unique_ptr then pushing back prevents leaks. The final release() call is appropriate once push_back succeeds.


204-215: Hierarchy deallocation
Destroying all allocators within the vector is correct. Good cleanup in the destructor.


216-260: malloc method concurrency
The method locks around the allocation logic. This is essential because it potentially modifies the allocator hierarchy. Good job ensuring consistency.


216-260: Possible integer overflow
When creating a new child allocator, 1 << size is used to scale chunks. This could overflow on large values. A pre-check or safe cast is recommended.


262-297: calloc and free
The calloc method reuses malloc. The free method routes deallocation to the first allocator in the list. This design is consistent with the doc but verify that each pointer truly originates from the hierarchy.


299-364: Stubs for map-like operations
remove, bind, trybind, etc., are explicitly not supported. Returning -1 is fine—just ensure the calling code either handles or never calls these.


366-404: pool_depth and dump
Implementation is consistent with the doc. Using the lock inside pool_depth ensures thread safety. Dump logic is guarded by ACE_HAS_DUMP.


406-449: Multi-size-based constructor
Allocates an initial cascade level with slight overhead for the “header” byte. This clarifies how each chunk is tracked.


451-479: Multi-size-based destructor
Looping through the hierarchy and deleting all non-null members is correct.


517-526: Zero-initialized allocation
calloc method simply calls malloc then uses memset. Straightforward approach.


528-549: Free logic with single-octet header
Subtracting one byte to find the chunk header is a known approach. Should be validated that alignment is correct on all platforms.


551-647: Map-like stubs
Same approach of returning -1 so the user knows these operations are unsupported.


649-672: Dump method
Dumps internal hierarchy for debugging. The per-allocator dump is also invoked.

ACE/ace/Malloc_T.inl (3)

41-47: pool_sum() accessor
Excellent use of ACE_GUARD_RETURN for concurrency and returning the chunk_sum_ safely.


49-53: Mutex accessor
Exposes the class-level lock reference, enabling external locking patterns if needed.


55-59: Lock accessor in multi-size-based allocator
Completes the concurrency model. This matches the approach in the cascaded dynamic allocator.

ACE/tests/run_test.lst (1)

19-19: LGTM!

The test entry is correctly added and follows the file's format. Since no platform restrictions are specified, the test will run on all platforms, which is appropriate for testing the new allocator classes.

ACE/tests/Allocator_Cascaded_Test.cpp (1)

1-21: LGTM!

The file header and includes are well-structured and contain all necessary dependencies for the test implementation.

ACE/tests/tests.mpc (1)

148-154: LGTM!

The project configuration is correctly structured and includes all necessary settings:

  • Properly avoids ace_for_tao dependency
  • Correctly specifies the executable name
  • Includes the appropriate source file

Comment on lines +232 to +311
template <class ACE_LOCK>
class ACE_Cascaded_Dynamic_Cached_Allocator : public ACE_Allocator
{
public:
// Useful STL-style traits.
using comb_alloc_type = ACE_Dynamic_Cached_Allocator<ACE_Null_Mutex>;
using comb_alloc_ptr = comb_alloc_type*;

/// Create a cached memory pool with @a initial_n_chunks chunks
/// each with @a chunk_size size.
ACE_Cascaded_Dynamic_Cached_Allocator (size_t initial_n_chunks, size_t chunk_size);

/// Clear things up.
~ACE_Cascaded_Dynamic_Cached_Allocator ();

/**
* Get a chunk of memory from free list cache. Note that @a nbytes is
* only checked to make sure that it's less or equal to @a chunk_size,
* and is otherwise ignored since malloc() always returns a pointer to an
* item of @a chunk_size size.
*/
virtual void *malloc (size_t nbytes);

/**
* Get a chunk of memory from free list cache, giving them
* @a initial_value. Note that @a nbytes is only checked to make sure
* that it's less or equal to @a chunk_size, and is otherwise ignored
* since calloc() always returns a pointer to an item of @a chunk_size.
*/
virtual void *calloc (size_t nbytes, char initial_value = '\0');

/// This method is a no-op and just returns 0 since the free list
/// only works with fixed sized entities.
virtual void *calloc (size_t n_elem, size_t elem_size, char initial_value = '\0');

/// Return a chunk of memory back to free list cache.
virtual void free (void *ptr);

/// Dump the state of this object.
virtual void dump () const;

/// These methods are no-ops.
virtual int remove ();
virtual int bind (const char *name, void *pointer, int duplicates = 0);
virtual int trybind (const char *name, void *&pointer);
virtual int find (const char *name, void *&pointer);
virtual int find (const char *name);
virtual int unbind (const char *name);
virtual int unbind (const char *name, void *&pointer);
virtual int sync (ssize_t len = -1, int flags = MS_SYNC);
virtual int sync (void *addr, size_t len, int flags = MS_SYNC);
virtual int protect (ssize_t len = -1, int prot = PROT_RDWR);
virtual int protect (void *addr, size_t len, int prot = PROT_RDWR);

/// Return the number of chunks available in the hierarchy.
size_t pool_depth ();

/// Return the sum of chunks including used and freed in the hierarchy.
size_t pool_sum ();

/// Returns a reference to the lock used to provide mutual exclusion to
/// the allocator hierarchy.
ACE_LOCK &mutex ();

private:
/// Synchronization variable for API.
ACE_LOCK mutex_;

/// Remember how we allocate the memory so we can clear things up later.
std::vector<comb_alloc_ptr> hierarchy_;

/// Remember the size of initial n_chunks for creating fresh allocator in future.
const size_t initial_n_chunks_;

/// Remember the size of our chunks for creating fresh allocator in future.
const size_t chunk_size_;

/// Remember the sum of our chunks including used and freed
size_t chunk_sum_;
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Review Verification on Potential Integer Overflow with Bit Shifting in ACE/ace/Malloc_T.cpp

After reviewing the grep output and inspecting the context of the 1 << size usage in the cascaded allocator in the file ACE/ace/Malloc_T.cpp, the check for potential overflow appears to be localized. In the implementation, the shift operation is used in a calculation to determine the number of chunks by multiplying this->initial_n_chunks_ with (1 << size). There isn’t any explicit bounds check around size in the context we observed.

Since the shift magnitude depends on the value of size, and given that no safeguards or maximum bounds are enforced on it within the bit-shift expression, there is indeed a risk that a large, potentially user-influenced, size value could trigger undefined behavior by causing an integer overflow.

Attention Points:

  • Verify that the value of size (likely derived from the number of allocators in the hierarchy) is either capped or controlled so that it never becomes large enough to trigger an integer overflow.
  • Consider adding a bounds check before performing (1 << size) (or using a safe shift utility) to ensure that size remains within a defined, safe range.
🔗 Analysis chain

Potential integer overflow when shifting
The logic in the malloc() method (in the corresponding .cpp file) shifts by size (1 << size). Ensure that size remains small enough and not user-controlled in a way that triggers large shifts. A bounds check with a well-defined maximum would help protect against overflow.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search all references to "1 <<" to see if user inputs can cause large shifts.
rg -A 3 '1 <<'

Length of output: 18264

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement needs review Needs to be reviewed
Development

Successfully merging this pull request may close these issues.

3 participants