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

BoehmGC initial support #232

Merged
merged 9 commits into from
May 14, 2024
Merged

BoehmGC initial support #232

merged 9 commits into from
May 14, 2024

Conversation

0xF6
Copy link
Member

@0xF6 0xF6 commented May 13, 2024

No description provided.

Copy link
Contributor

coderabbitai bot commented May 13, 2024

Walkthrough

The updates encompass enhancements and modifications across various components of the runtime and VM, focusing on memory allocation and garbage collection. Key changes include the introduction of new classes and interfaces for garbage collection layouts, updates to method signatures to support different kinds of allocations, and the addition of a new conditional compilation flag for experimental features.

Changes

Files Change Summary
VeinClass.cs, VeinModule.cs Adjusted method filtering in VeinClass and added a new object in MutexStorage.
ishtar.vm.csproj, IshtarGC.cs, allocators/..., vm/... Integrated new GC strategies, updated project constants, and revised allocator interfaces and classes.
test/ishtar_test/IshtarTestBase.cs, vein_lang.sln.DotSettings Minor updates including new namespace usage and dictionary settings.

Poem

🐰💻
In the land of code, where the bits roam free,
A rabbit hopped, changing history.
With a flip and a twirl of its tiny paw,
New paths were laid, without a flaw.
Cheers to the bytes, aligned just right,
In the glow of the screen's soft light.
🌟📜


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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: 3

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between b97a7f5 and b4e6c7f.
Files selected for processing (17)
  • runtime/common/reflection/VeinClass.cs (1 hunks)
  • runtime/common/reflection/VeinModule.cs (1 hunks)
  • runtime/ishtar.vm/ishtar.vm.csproj (1 hunks)
  • runtime/ishtar.vm/runtime/IshtarGC.cs (11 hunks)
  • runtime/ishtar.vm/runtime/allocators/DebugManagedAllocator.cs (2 hunks)
  • runtime/ishtar.vm/runtime/allocators/GCLayoutAllocator.cs (1 hunks)
  • runtime/ishtar.vm/runtime/allocators/IIshtarAllocator.cs (1 hunks)
  • runtime/ishtar.vm/runtime/allocators/IIshtarAllocatorPool.cs (1 hunks)
  • runtime/ishtar.vm/runtime/allocators/IshtarAllocatorPool.cs (3 hunks)
  • runtime/ishtar.vm/runtime/allocators/NativeMemory_WindowsAllocator.cs (1 hunks)
  • runtime/ishtar.vm/runtime/allocators/NullAllocator.cs (1 hunks)
  • runtime/ishtar.vm/runtime/allocators/WindowsHeapAllocator.cs (1 hunks)
  • runtime/ishtar.vm/runtime/vm/CallFrame.cs (2 hunks)
  • runtime/ishtar.vm/runtime/vm/RuntimeIshtarClass.cs (2 hunks)
  • runtime/ishtar.vm/vm.switch.cs (1 hunks)
  • test/ishtar_test/IshtarTestBase.cs (1 hunks)
  • vein_lang.sln.DotSettings (1 hunks)
Files skipped from review due to trivial changes (1)
  • vein_lang.sln.DotSettings
Additional comments not posted (31)
runtime/ishtar.vm/runtime/allocators/IIshtarAllocatorPool.cs (1)

6-6: The addition of AllocationKind in the Rent method signature aligns well with the broader updates to the allocator interfaces.

runtime/ishtar.vm/runtime/allocators/IIshtarAllocator.cs (1)

14-19: The updated method signatures for AllocZeroed with the AllocationKind parameter are consistent and necessary for the new memory management enhancements.

runtime/ishtar.vm/runtime/allocators/NullAllocator.cs (1)

8-16: The updated AllocZeroed methods in NullAllocator correctly throw NotImplementedException, aligning with its role as a non-functional allocator for certain scenarios.

runtime/ishtar.vm/runtime/allocators/NativeMemory_WindowsAllocator.cs (1)

21-33: The updated AllocZeroed methods in NativeMemory_WindowsAllocator correctly delegate to the Allocate method, ensuring proper memory management and alignment with the new memory allocation strategies.

runtime/ishtar.vm/vm.switch.cs (1)

44-44: The addition of the DisabledFinalization property in VMConfig is well-implemented, providing a dynamic way to manage finalization features based on configuration.

runtime/ishtar.vm/runtime/allocators/GCLayoutAllocator.cs (1)

5-37: The GCLayoutAllocator class is well-implemented, correctly managing memory allocations based on the provided GCLayout and aligning with the new garbage collection mechanisms.

runtime/ishtar.vm/runtime/allocators/IshtarAllocatorPool.cs (1)

Line range hint 5-52: The updates to IshtarAllocatorPool, including the integration of GCLayout in the constructor and the modification of the Rent method to include AllocationKind, are well-implemented and crucial for the new garbage collection mechanisms.

runtime/ishtar.vm/ishtar.vm.csproj (1)

23-23: The inclusion of the BOEHM_GC definition in the DefineConstants of the debug configuration is correctly implemented, aligning with the PR's objectives to integrate Boehm's garbage collector.

runtime/ishtar.vm/runtime/allocators/DebugManagedAllocator.cs (5)

33-33: LGTM! The method correctly handles the new AllocationKind parameter.


39-39: LGTM! Consistent handling of AllocationKind across different method overloads.


45-45: LGTM! Proper handling of UIntPtr size parameter with consistent logic.


51-51: LGTM! Consistent handling of IntPtr size parameter.


57-57: LGTM! Correct handling of int size parameter, maintaining consistency with other overloads.

runtime/ishtar.vm/runtime/allocators/WindowsHeapAllocator.cs (5)

43-43: LGTM! The method correctly handles the new AllocationKind parameter by delegating to AllocFromHeap.


46-46: LGTM! Consistent handling of AllocationKind across different method overloads.


49-49: LGTM! Proper handling of UIntPtr size parameter with consistent logic.


52-52: LGTM! Consistent handling of IntPtr size parameter.


55-55: LGTM! Correct handling of int size parameter, maintaining consistency with other overloads.

runtime/common/reflection/VeinModule.cs (1)

131-131: LGTM! The introduction of GC_FINALIZER_BARRIER aligns with the enhancements to the garbage collection mechanisms.

test/ishtar_test/IshtarTestBase.cs (1)

16-16: LGTM! The inclusion of ishtar.vm.runtime namespace is necessary for consistency in the testing environment.

runtime/common/reflection/VeinClass.cs (1)

73-73: LGTM! The refinement to include constructors and deconstructors in the GetOrCreateTor method enhances its specificity.

runtime/ishtar.vm/runtime/vm/RuntimeIshtarClass.cs (2)

310-310: LGTM! The override of GetDefaultDtor to return RuntimeIshtarMethod aligns with the custom behaviors for runtime classes.


311-311: LGTM! The override of GetDefaultCtor to return RuntimeIshtarMethod ensures consistency and correctness in runtime class behavior.

runtime/ishtar.vm/runtime/IshtarGC.cs (8)

14-14: Introduced a new delegate IshtarFinalizationProc for finalization procedures.


18-39: Introduced GCLayout interface with a comprehensive set of methods for garbage collection operations.


41-45: Introduced GCLayout_Debug interface for debugging garbage collection.


263-266: Conditional compilation for Boehm GC. Ensure that the BOEHM_GC flag is properly managed across different build configurations.


365-365: Instantiation of IshtarAllocatorPool with gcLayout. Ensure that gcLayout is correctly initialized before this point to avoid null reference issues.


459-459: Usage of allocatorPool.Rent with AllocationKind.no_reference. Verify that this allocation kind is handled correctly in all scenarios.


Line range hint 720-750: Significant changes in AllocObject method to support Boehm GC. Ensure that all new logic, especially around finalizers and object registration, is correct and does not introduce memory leaks or other issues.


810-833: Introduced methods for object lifecycle management with Boehm GC. Ensure that these methods are correctly used and do not lead to improper memory management.

Verification successful

The provided script outputs show the contexts in which the methods IsAlive, ObjectRegisterFinalizer, and Collect are used within the IshtarGC.cs file. Here's a breakdown of the findings:

  1. IsAlive: This method is used within a finalizer (_direct_finalizer) to check if an object is still valid before proceeding with finalization logic. This is a correct and expected use of the method, ensuring that operations are only performed on valid objects.

  2. ObjectRegisterFinalizer: This method is used in two contexts:

    • Directly after object allocation (AllocObject), a finalizer is registered. This is a standard practice to ensure that resources are cleaned up when the object is no longer needed.
    • Within the _direct_finalizer itself, potentially to re-register a finalizer under certain conditions. This usage is less typical and could be prone to errors if not handled carefully, especially in terms of re-entrancy or double finalization issues. However, without specific details on the logic inside _direct_finalizer, it's hard to definitively say if this is incorrect.
  3. Collect: The Collect method is invoked in various cleanup and error handling scenarios to perform garbage collection. This is a standard use of such a method in a garbage-collected environment.

From the analysis, it appears that the methods are generally used correctly. The potential concern with ObjectRegisterFinalizer being used within a finalizer itself warrants a closer look to ensure it doesn't lead to unintended behaviors like double finalization. However, this would typically require a deeper understanding of the application logic and possibly runtime debugging or logging to fully validate.

Conclusion

The methods related to object lifecycle management in the Boehm GC implementation are used in contexts that align with typical garbage collection patterns. There is a potential area of concern with the re-registration of finalizers within a finalizer, which could be further investigated or documented to ensure it doesn't lead to issues.

  • Ensure that the re-registration of finalizers within _direct_finalizer is intentional and safe.
  • Consider adding more detailed logging or error handling around finalizer re-registration to catch any potential issues in runtime.
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify correct usage of object lifecycle management methods with Boehm GC.

# Test: Search for usages of lifecycle management methods (`IsAlive`, `ObjectRegisterFinalizer`, etc.) and their correctness.
rg --type cs "IsAlive|ObjectRegisterFinalizer|Collect"

Length of output: 15594



Script:

#!/bin/bash
# Extracting the context of usage for the methods `IsAlive`, `ObjectRegisterFinalizer`, and `Collect` to ensure they are used correctly.

# Context for `IsAlive`
rg --type cs --context 5 "IsAlive" runtime/ishtar.vm/runtime/IshtarGC.cs

# Context for `ObjectRegisterFinalizer`
rg --type cs --context 5 "ObjectRegisterFinalizer" runtime/ishtar.vm/runtime/IshtarGC.cs

# Context for `Collect`
rg --type cs --context 5 "Collect" runtime/ishtar.vm/runtime/IshtarGC.cs

Length of output: 4350

Comment on lines +48 to +244
public static extern void GC_finalize_all();

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void GC_stop_func();


// GC_API void GC_CALL GC_set_find_leak(int);
[DllImport(LIBNAME)]
public static extern void GC_set_find_leak(bool zero);

[DllImport(LIBNAME)]
public static extern long get_heap_size();

[DllImport(LIBNAME)]
public static extern long get_free_bytes();

[DllImport(LIBNAME)]
public static extern long GC_get_heap_usage();

[DllImport(LIBNAME)]
public static extern bool GC_is_marked(void* ptr);
}


public void init()
{
Native.GC_init();
Native.GC_set_find_leak(true);
}

public void destroy() => Native.GC_deinit();

public void* alloc(uint size) => Native.GC_debug_malloc(size, "empty.vein", 0);

/* Explicitly deallocate an object. Dangerous if used incorrectly. */
/* Requires a pointer to the base of an object. */
/* An object should not be enabled for finalization (and it should not */
/* contain registered disappearing links of any kind) when it is */
/* explicitly deallocated. */
/* GC_free(0) is a no-op, as required by ANSI C for free. */
public void free(void* ptr) => Native.GC_debug_free(ptr, "empty.vein", 0);

private static nuint hide_pointer(void* p) => ~(nuint)p;

public void link(void** link_addr, void* obj, bool trackable)
{
*link_addr = (void*)hide_pointer(obj);
if (trackable)
Native.GC_register_long_link(link_addr, obj);
else
Native.GC_general_register_disappearing_link(link_addr, obj);
}

public void unlink(void** link_addr, bool trackable)
{
if (trackable)
Native.GC_unregister_long_link(link_addr);
else
Native.GC_unregister_disappearing_link(link_addr);
*link_addr = null;
}

public void link(void** child, void* parent) => Native.GC_general_register_disappearing_link(child, parent);
public void register_finalizer_ignore_self(void* obj, IshtarFinalizationProc proc) => Native.GC_register_finalizer_ignore_self(obj, proc, null, null, null);

public void* alloc_atomic(uint size) => throw new NotImplementedException();

public void* alloc_immortal(uint size) => throw new NotImplementedException();

public void add_roots(void* ptr) => throw new NotImplementedException();

public long get_free_bytes() => Native.get_free_bytes();

public long get_heap_size() => Native.get_heap_size();

public long get_heap_usage() => Native.GC_get_heap_usage();

public bool try_collect() => Native.GC_try_to_collect(0) == 1;

public void collect() => Native.GC_gcollect();
public bool is_marked(void* obj) => Native.GC_is_marked(obj);

public void finalize_all() => Native.GC_finalize_all();

public void finalize_on_demand() => throw new NotImplementedException();

public void find_leak() => throw new NotImplementedException();


public void register_finalizer_no_order(IshtarObject* obj, IshtarFinalizationProc proc, CallFrame frame)
=> Native.GC_debug_register_finalizer_no_order(obj, proc, null, null, null, frame.Debug_GetFile(), frame.Debug_GetLine());


public void dump(string file)
{
//var ptr = Marshal.StringToHGlobalAnsi(file);
//Native.GC_dump((void*)ptr);
//Marshal.ZeroFreeGlobalAllocAnsi(ptr);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Tip

Codebase Verification

The search for unit or integration tests specifically targeting the BoehmGCLayout class did not yield any results. This absence of test cases in the search results suggests that there might not be adequate testing implemented for the BoehmGCLayout class, particularly concerning its integration with the Boehm garbage collector and aspects like memory management and thread safety.

Given the critical nature of garbage collection in managing application memory and ensuring performance and stability, the lack of tests is a significant concern. It is advisable to implement comprehensive tests that cover various scenarios including stress tests, concurrency tests, and memory leak tests to ensure the robustness of the BoehmGCLayout integration.

Action Required:

  • Implement unit and integration tests for the BoehmGCLayout class to verify its functionality, memory management, and thread safety.
  • Ensure that these tests cover a wide range of scenarios to thoroughly evaluate the garbage collector's performance under different conditions.
Analysis chain

Implemented BoehmGCLayout class which integrates native Boehm GC functions. Ensure thorough testing, especially for memory management and thread safety.


The search results indicate that the BoehmGCLayout class is indeed being used within the IshtarGC.cs file, specifically it is instantiated and assigned to a private field gcLayout. However, the results do not provide information about any specific tests or further usage details that would confirm thorough testing or integration quality, particularly concerning memory management and thread safety as mentioned in the review comment.

To verify the thoroughness of testing and the integration quality, we need to look for unit tests or integration tests that specifically target the BoehmGCLayout class. This will help us understand if the memory management and thread safety aspects are being adequately tested.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the integration and functionality of BoehmGCLayout with Boehm GC.

# Test: Search for BoehmGCLayout usage and related tests.
rg --type cs "BoehmGCLayout"

Length of output: 226



Script:

#!/bin/bash
# Description: Search for unit or integration tests specifically targeting the BoehmGCLayout class.
rg --type cs -A 30 "class.*Test" | grep "BoehmGCLayout"

Length of output: 55

Comment on lines +59 to +79
public string Debug_GetFile()
{
// TODO fetch from dbg symbols

var str = new StringBuilder();

if (this.method.Owner is not null)
str.AppendLine($"\tat {this.method.Owner.FullName.NameWithNS}.{this.method.Name}");
else
str.AppendLine($"\tat <sys>.{this.method.Name}");

var r = this.parent;

while (r != null)
{
str.AppendLine($"\tat {r.method.Owner.FullName.NameWithNS}.{r.method.Name}");
r = r.parent;
}

return str.ToString();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

LGTM! The method constructs a call stack string correctly. Remember to address the TODO for fetching file information from debug symbols.

Would you like me to help implement the debug symbol fetching or should I open a GitHub issue to track this task?

return str.ToString();
}

public int Debug_GetLine() => 0;// TODO fetch from dbg symbols
Copy link
Contributor

Choose a reason for hiding this comment

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

Placeholder implementation for line number fetching. Ensure to address the TODO for integrating debug symbol fetching.

Would you like me to help implement the debug symbol fetching or should I open a GitHub issue to track this task?

@0xF6 0xF6 self-assigned this May 13, 2024
@0xF6 0xF6 added the area-gc Area of garbage collector label May 13, 2024

public unsafe class BoehmGCLayout : GCLayout, GCLayout_Debug
{
public class Native

Choose a reason for hiding this comment

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

не совсем понял зачем этот класс нужен. либа не умеет сама платформу детектить? или ты планировал расширять?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ну просто обычно экстерны пихают во внутренний класс обертки
а препроцессоры в виде #if'ок нужны для того что сука на линуксе и маке либа по другому называется

{
var clazz = obj->decodeClass();

IshtarSync.EnterCriticalSection(ref clazz.Owner.Interlocker.GC_FINALIZER_BARRIER);

Choose a reason for hiding this comment

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

при каждой аллокации объекта фриз?

Copy link
Member Author

@0xF6 0xF6 May 13, 2024

Choose a reason for hiding this comment

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

Ну объект уже заалоцированный, в данной функции регистрирую финализатор, когда гц пернет желанием убить объект, вызовем юзер код если он есть и очистим память
EnterCriticalSection пока никак не используется, просто затычка
Это на будущее когда будет поточность будет

@0xF6 0xF6 merged commit 9d7588a into master May 14, 2024
1 of 2 checks passed
@0xF6 0xF6 deleted the feature/gc branch May 14, 2024 20:55
@vein-lang vein-lang locked and limited conversation to collaborators May 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-gc Area of garbage collector
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants