Skip to content

[Bugfix] fix builtin algo lookup in plugin via unifed register#317

Open
haochengxia wants to merge 1 commit into
1a1a11a:developfrom
haochengxia:fixplugin
Open

[Bugfix] fix builtin algo lookup in plugin via unifed register#317
haochengxia wants to merge 1 commit into
1a1a11a:developfrom
haochengxia:fixplugin

Conversation

@haochengxia
Copy link
Copy Markdown
Collaborator

@haochengxia haochengxia commented Jun 6, 2026

For issue #304

In the original plugin system, we would find the symbol always via

sprintf(cache_init_func_name, "%s_init", cache_alg_name);
dlsym(..., cache_init_func_name);

This is safe when we impl plugin init function and name it correctly, but it would affect the user experience when they need to use builtin init functions. To achieve unified experiment like cachesim, you can refer to the init function via lru, 's3fifo', etc., we can share the lookup table via a register layer and move it into the core lib.

After the modifiction, command like the following one would be safe

MPLBACKEND=Agg python3 scripts/profile_mrc.py \
  --tracepath data/twitter_cluster52.csv \
  --trace-format csv \
  --trace-format-params 'time-col=1,obj-id-col=2,obj-size-col=3,delimiter=,,obj-id-is-num=1' \
  --profiler=MINISIM \
  --algos=lru,fifo \
  --profiler-params=FIX_RATE,0.1,10 \
  --sizes=0.01,0.02 \
  --name /tmp/libcachesim_profile_mrc_test

Summary by CodeRabbit

  • New Features

    • Algorithm names are now case-insensitive (e.g., LRU, lru, and Lru are equivalent).
    • Built-in eviction algorithms are now registered through a centralized registry for improved discovery.
  • Documentation

    • Updated integration guide for adding new eviction algorithms with simplified steps.
    • Clarified case-insensitivity behavior in MINISIM profiling and command-line tools.
  • Tests

    • Added tests for lowercase algorithm name handling.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jun 6, 2026

Looking for one thing? Review this PR in Change Stack to search files, summaries, diffs, and code without losing your place.

Review Change Stack

📝 Walkthrough

Walkthrough

This PR introduces a built-in eviction algorithm registry system that enables case-insensitive algorithm name lookup and simplifies the plugin loading pathway. The registry maps algorithm name strings to initialization functions, consolidating algorithm discovery into a single lookup mechanism used by both the CLI and plugin systems.

Changes

Built-in Algorithm Registry System

Layer / File(s) Summary
Registry infrastructure and public API
libCacheSim/cache/evictionAlgoRegister.c, libCacheSim/include/libCacheSim/evictionAlgo.h, libCacheSim/cache/CMakeLists.txt
Defines a static eviction_algo_entry_t struct mapping algorithm names to cache_init_func_ptr initializer functions. Implements get_builtin_cache_init() with case-insensitive strcasecmp lookup over a built-in algorithm table that includes many standard algorithm names and feature-gated conditional entries (e.g., ENABLE_3L_CACHE, ENABLE_GLCACHE, ENABLE_LRB). Exposes the lookup function in the public header and adds the registry source to the CMake build.
Plugin system integration with registry lookup
libCacheSim/cache/plugin.c
Updates create_cache_internal to attempt builtin initialization first via get_builtin_cache_init() before falling back to dlopen/dlsym. Refactors dynamic-symbol-resolution error handling: clears prior dlerror, uses a typed function-pointer wrapper for dlsym, and now logs a warning and returns NULL on resolution failure instead of aborting, allowing fallback to external plugin loading.
Cache initialization refactoring for registry fallback
libCacheSim/bin/cachesim/cache_init.h
Decouples the hyperbolic algorithm check from the prior else if chain by converting it to a standalone if statement. Replaces the unsupported-algorithm error path with a call to get_builtin_cache_init(eviction_algo); if a builtin initializer is found, uses it to create the cache; otherwise, prints an error and aborts.
Tests and documentation for case-insensitive algorithm names
test/test_evictionAlgo.c, doc/advanced_lib_extend.md, doc/quickstart_mrcProfiler.md, scripts/README.md
Adds test_create_cache_using_plugin_lowercase() to verify that lowercase algorithm names (lru, fifo) resolve correctly via the registry. Updates documentation to describe the new registry-based integration workflow for custom eviction algorithms, clarifies that built-in algorithm names are case-insensitive (e.g., lru and LRU are equivalent), and removes outdated instructions about CLI-option modifications.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related PRs

  • 1a1a11a/libCacheSim#314: Both PRs touch eviction-algorithm initialization/registration: the main PR adds get_builtin_cache_init and changes create_cache to use it (affecting builtin algorithm resolution), while the retrieved PR adds the Clock2QPlus initializer declaration and registers "clock2qplus" in create_cache's simple_algos, so Clock2QPlus selection flows through the same initialization logic.

Poem

🐰 A registry is born, case-blind and keen,
Where algorithm names find homes serene,
From CLI to plugin, the paths now align,
No more hardcoded branches to tighten—
The cache builders leap through this cleaner design! 🎉

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 14.29% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title mentions 'builtin algo lookup in plugin via unified register,' which directly matches the core change: introducing a unified registry to enable builtin algorithm lookup by name in the plugin system.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 Infer (1.2.0)
libCacheSim/cache/evictionAlgoRegister.c

libCacheSim/cache/evictionAlgoRegister.c:3:10: fatal error: 'libCacheSim/evictionAlgo.h' file not found
3 | #include "libCacheSim/evictionAlgo.h"
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
Error: the following clang command did not run successfully:
/opt/infer-linux-x86_64-v1.2.0/lib/infer/facebook-clang-plugins/clang/install/bin/clang-18
@/tmp/coderabbit-infer/f0f98a88a8b72d76fc20174a25fd3bdd0eca399d-9b7cf1ed869f76ac/tmp/clang_command_.tmp.9f5149.txt
++Contents of '/tmp/coderabbit-infer/f0f98a88a8b72d76fc20174a25fd3bdd0eca399d-9b7cf1ed869f76ac/tmp/clang_command_.tmp.9f5149.txt':
"-cc1" "-load"
"/opt/infer-linux-x86_64-v1.2.0/lib/infer/infer/bin/../../facebook-clang-plugins/libtooling/build/FacebookClangPlugin.dylib"
"-add-plugin" "BiniouASTExporter" "-plugin-arg-BiniouASTExporter" "-"
"-plugin-arg-BiniouASTExporter" "PREPEND_CURRENT_DIR=1"
"-plugin-arg-BiniouASTExporter" "MAX_STRING_SIZE=65535" "-cc1" "-triple"
"x86_64-unknow

... [truncated 789 characters] ...

.2.0/lib/infer/facebook-clang-plugins/clang/install/lib/clang/18/include"
"-internal-isystem" "/usr/local/include" "-internal-isystem"
"/usr/lib/gcc/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/include"
"-internal-externc-isystem" "/usr/include/x86_64-linux-gnu"
"-internal-externc-isystem" "/include" "-internal-externc-isystem"
"/usr/include" "-Wno-ignored-optimization-argument" "-Wno-everything"
"-ferror-limit" "19" "-fgnuc-version=4.2.1" "-fskip-odr-check-in-gmf"
"-D__GCC_HAVE_DWARF2_CFI_ASM=1" "-o"
"/tmp/coderabbit-infer/9b7cf1ed869f76ac/file.o" "-x" "c"
"libCacheSim/cache/evictionAlgoRegister.c" "-O0" "-fno-builtin"
"-include"
"/opt/infer-linux-x86_64-v1.2.0/lib/infer/infer/bin/../lib/clang_wrappers/global_defines.h"
"-Wno-everything"

libCacheSim/cache/plugin.c

libCacheSim/cache/plugin.c:5:10: fatal error: 'libCacheSim/plugin.h' file not found
5 | #include "libCacheSim/plugin.h"
| ^~~~~~~~~~~~~~~~~~~~~~
1 error generated.
Error: the following clang command did not run successfully:
/opt/infer-linux-x86_64-v1.2.0/lib/infer/facebook-clang-plugins/clang/install/bin/clang-18
@/tmp/coderabbit-infer/f0f98a88a8b72d76fc20174a25fd3bdd0eca399d-270ce802f01cb493/tmp/clang_command_.tmp.d58b7b.txt
++Contents of '/tmp/coderabbit-infer/f0f98a88a8b72d76fc20174a25fd3bdd0eca399d-270ce802f01cb493/tmp/clang_command_.tmp.d58b7b.txt':
"-cc1" "-load"
"/opt/infer-linux-x86_64-v1.2.0/lib/infer/infer/bin/../../facebook-clang-plugins/libtooling/build/FacebookClangPlugin.dylib"
"-add-plugin" "BiniouASTExporter" "-plugin-arg-BiniouASTExporter" "-"
"-plugin-arg-BiniouASTExporter" "PREPEND_CURRENT_DIR=1"
"-plugin-arg-BiniouASTExporter" "MAX_STRING_SIZE=65535" "-cc1" "-triple"
"x86_64-unknown-linux-gnu" "-emit-obj" "-mrela

... [truncated 721 characters] ...

er-linux-x86_64-v1.2.0/lib/infer/facebook-clang-plugins/clang/install/lib/clang/18/include"
"-internal-isystem" "/usr/local/include" "-internal-isystem"
"/usr/lib/gcc/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/include"
"-internal-externc-isystem" "/usr/include/x86_64-linux-gnu"
"-internal-externc-isystem" "/include" "-internal-externc-isystem"
"/usr/include" "-Wno-ignored-optimization-argument" "-Wno-everything"
"-ferror-limit" "19" "-fgnuc-version=4.2.1" "-fskip-odr-check-in-gmf"
"-D__GCC_HAVE_DWARF2_CFI_ASM=1" "-o"
"/tmp/coderabbit-infer/270ce802f01cb493/file.o" "-x" "c"
"libCacheSim/cache/plugin.c" "-O0" "-fno-builtin" "-include"
"/opt/infer-linux-x86_64-v1.2.0/lib/infer/infer/bin/../lib/clang_wrappers/global_defines.h"
"-Wno-everything"

test/test_evictionAlgo.c

In file included from test/test_evictionAlgo.c:5:
test/common.h:8:10: fatal error: 'glib.h' file not found
8 | #include <glib.h>
| ^~~~~~~~
1 error generated.
Error: the following clang command did not run successfully:
/opt/infer-linux-x86_64-v1.2.0/lib/infer/facebook-clang-plugins/clang/install/bin/clang-18
@/tmp/coderabbit-infer/f0f98a88a8b72d76fc20174a25fd3bdd0eca399d-98cdb18218f63ee2/tmp/clang_command_.tmp.113f1e.txt
++Contents of '/tmp/coderabbit-infer/f0f98a88a8b72d76fc20174a25fd3bdd0eca399d-98cdb18218f63ee2/tmp/clang_command_.tmp.113f1e.txt':
"-cc1" "-load"
"/opt/infer-linux-x86_64-v1.2.0/lib/infer/infer/bin/../../facebook-clang-plugins/libtooling/build/FacebookClangPlugin.dylib"
"-add-plugin" "BiniouASTExporter" "-plugin-arg-BiniouASTExporter" "-"
"-plugin-arg-BiniouASTExporter" "PREPEND_CURRENT_DIR=1"
"-plugin-arg-BiniouASTExporter" "MAX_STRING_SIZE=65535" "-cc1" "-triple"
"x86_64-unknown-linux-gnu" "-emit-obj" "-mrelax-all

... [truncated 729 characters] ...

nfer-linux-x86_64-v1.2.0/lib/infer/facebook-clang-plugins/clang/install/lib/clang/18/include"
"-internal-isystem" "/usr/local/include" "-internal-isystem"
"/usr/lib/gcc/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/include"
"-internal-externc-isystem" "/usr/include/x86_64-linux-gnu"
"-internal-externc-isystem" "/include" "-internal-externc-isystem"
"/usr/include" "-Wno-ignored-optimization-argument" "-Wno-everything"
"-ferror-limit" "19" "-fgnuc-version=4.2.1" "-fskip-odr-check-in-gmf"
"-D__GCC_HAVE_DWARF2_CFI_ASM=1" "-o"
"/tmp/coderabbit-infer/98cdb18218f63ee2/file.o" "-x" "c"
"test/test_evictionAlgo.c" "-O0" "-fno-builtin" "-include"
"/opt/infer-linux-x86_64-v1.2.0/lib/infer/infer/bin/../lib/clang_wrappers/global_defines.h"
"-Wno-everything"


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the eviction algorithm registration by introducing a centralized registry (evictionAlgoRegister.c) and a helper function get_builtin_cache_init to look up built-in cache initialization functions. This replaces the inline registry in cache_init.h and allows unified lookup for both cachesim and plugin-based cache creation, enabling case-insensitive algorithm resolution. The feedback suggests adding defensive NULL checks for algorithm names, handling potential NULL values from dlopen and dlerror in plugin.c, and registering "tinyLFU" as an alias for "wtinyLFU" in the new registry.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines 59 to +62
cache_t *create_cache_internal(const char *const cache_alg_name,
common_cache_params_t cc_params,
void *cache_specific_params) {
cache_t *(*cache_init)(common_cache_params_t, void *) = NULL;
cache_init_func_ptr cache_init = get_builtin_cache_init(cache_alg_name);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

Add a defensive check to ensure cache_alg_name is not NULL before proceeding. If cache_alg_name is NULL, passing it to get_builtin_cache_init or sprintf later in the function will cause a segmentation fault.

cache_t *create_cache_internal(const char *const cache_alg_name,
                               common_cache_params_t cc_params,
                               void *cache_specific_params) {
  if (cache_alg_name == NULL) {
    return NULL;
  }
  cache_init_func_ptr cache_init = get_builtin_cache_init(cache_alg_name);

cache_init_func_ptr init_func;
} eviction_algo_entry_t;

cache_init_func_ptr get_builtin_cache_init(const char *const cache_alg_name) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

Add a defensive check to ensure cache_alg_name is not NULL before calling strcasecmp. If cache_alg_name is NULL, strcasecmp will dereference a null pointer and cause a segmentation fault.

cache_init_func_ptr get_builtin_cache_init(const char *const cache_alg_name) {
  if (cache_alg_name == NULL) {
    return NULL;
  }

Comment on lines +71 to 73
void *handle = dlopen(NULL, RTLD_LAZY | RTLD_GLOBAL);
/* should not check err here, otherwise ubuntu will report err even though
* everything is OK */
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Add a defensive check to ensure handle is not NULL after calling dlopen. If dlopen fails and returns NULL, passing it to dlsym can cause undefined behavior or a crash on some platforms.

  void *handle = dlopen(NULL, RTLD_LAZY | RTLD_GLOBAL);
  if (handle == NULL) {
    return NULL;
  }
  /* should not check err here, otherwise ubuntu will report err even though
   * everything is OK */

Comment on lines 89 to 92
if (cache_init == NULL) {
WARN("cannot load internal cache %s: error %s\n", cache_alg_name, err);
abort();
return NULL;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Handle the case where err is NULL gracefully. If dlerror() returns NULL, passing it directly to %s in WARN can cause undefined behavior or a crash on some platforms.

  if (cache_init == NULL) {
    WARN("cannot load internal cache %s: error %s\n", cache_alg_name, err ? err : "symbol not found");
    return NULL;
  }

Comment on lines +68 to +69
{"twoq", TwoQ_init},
{"wtinyLFU", WTinyLFU_init},
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Add "tinyLFU" as an alias for WTinyLFU_init in the registry. Currently, only "wtinyLFU" is registered, but "tinyLFU" is a very common name used by users (e.g., in cachesim command line and potentially in mrcProfiler). Registering it as an alias ensures it can be resolved correctly through the unified registry.

Suggested change
{"twoq", TwoQ_init},
{"wtinyLFU", WTinyLFU_init},
{"twoq", TwoQ_init},
{"tinyLFU", WTinyLFU_init},
{"wtinyLFU", WTinyLFU_init},

Copy link
Copy Markdown

@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

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@libCacheSim/cache/evictionAlgoRegister.c`:
- Line 32: The mapping for the algorithm name "fifo-reinsertion" incorrectly
points to Clock_init; change the initializer to FIFO_Reinsertion_init in the
registration table so the string "fifo-reinsertion" is associated with the
correct initializer function (ensure FIFO_Reinsertion_init is declared/visible
where the table is defined).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e7c7aa52-41e7-4ba2-8a1c-2ac2550641a6

📥 Commits

Reviewing files that changed from the base of the PR and between da022c2 and f0f98a8.

📒 Files selected for processing (9)
  • doc/advanced_lib_extend.md
  • doc/quickstart_mrcProfiler.md
  • libCacheSim/bin/cachesim/cache_init.h
  • libCacheSim/cache/CMakeLists.txt
  • libCacheSim/cache/evictionAlgoRegister.c
  • libCacheSim/cache/plugin.c
  • libCacheSim/include/libCacheSim/evictionAlgo.h
  • scripts/README.md
  • test/test_evictionAlgo.c

{"cr_lfu", CR_LFU_init},
{"fifo", FIFO_init},
{"fifo-merge", FIFO_Merge_init},
{"fifo-reinsertion", Clock_init},
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Map fifo-reinsertion to the correct initializer

Line 32 maps "fifo-reinsertion" to Clock_init, which will initialize the wrong policy for that algorithm name. This should point to FIFO_Reinsertion_init.

Suggested fix
-      {"fifo-reinsertion", Clock_init},
+      {"fifo-reinsertion", FIFO_Reinsertion_init},
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
{"fifo-reinsertion", Clock_init},
{"fifo-reinsertion", FIFO_Reinsertion_init},
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@libCacheSim/cache/evictionAlgoRegister.c` at line 32, The mapping for the
algorithm name "fifo-reinsertion" incorrectly points to Clock_init; change the
initializer to FIFO_Reinsertion_init in the registration table so the string
"fifo-reinsertion" is associated with the correct initializer function (ensure
FIFO_Reinsertion_init is declared/visible where the table is defined).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant