Skip to content

Define custom memory allocation definitions #193

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

Merged
merged 4 commits into from
Feb 24, 2022
Merged

Define custom memory allocation definitions #193

merged 4 commits into from
Feb 24, 2022

Conversation

warmwaffles
Copy link
Member

@warmwaffles warmwaffles commented Feb 22, 2022

What my aim here is to expose the amount of memory being utilized by an erlang vm process that is currently running.

I am opening this up for comment and am probably going to add in some benchmarks and some load testing scripts to go along with this.

What my aim here is to expose the amount of memory being utilized by an
erlang process that is currently running.
@warmwaffles warmwaffles added this to the 1.0 milestone Feb 22, 2022
@laszlohegedus
Copy link

Hey Matthew, our app fails with segmentation fault with these changes. I'll look into it, but can you also check what may cause it?

@warmwaffles
Copy link
Member Author

Hey Matthew, our app fails with segmentation fault with these changes. I'll look into it, but can you also check what may cause it?

This is good to know. I'll need to do some more load testing with it.

@warmwaffles
Copy link
Member Author

@laszlohegedus could you provide some information on what may have caused the segfault?

@laszlohegedus
Copy link

@laszlohegedus could you provide some information on what may have caused the segfault?

Sure, it actually happens on startup. I want to check it with Valgrind and see if it tells us why. We're also using libbtc_ex. I wonder if they collide with exqlite. Maybe I'll just try the two in a dummy project.

@warmwaffles
Copy link
Member Author

I want to check it with Valgrind and see if it tells us why.

Let me know how that goes. I have had trouble getting valgrind to play nice with erlang and asdf installs

We're also using libbtc_ex. I wonder if they collide with exqlite. Maybe I'll just try the two in a dummy project.

That should not collide with exqlite.

@laszlohegedus
Copy link

@warmwaffles Well, it was a pain indeed. I managed to build the beam with valgrind and even "hack" the Elixir executable to use cerl, but it didn't want to work properly with our application, so I gave up.

What we can tell now is that we work with exqlite master, but not these changes. I'll dig into the code to see where it fails. At a first glance with my rarely used knowledge of C, the changes look good.

@laszlohegedus
Copy link

@warmwaffles I think I found the issue. It's in the on_unload function, when the memory allocator functions are set to NULL. We need to revert to the defaults there. I couldn't find a way to access that struct from outside of sqlite.c, but we can use SQLITE_CONFIG_GETMALLOC before we set our functions and store the defaults in a global variable. I'll add some comments to the code.

@warmwaffles
Copy link
Member Author

@laszlohegedus see the latest commit, I lifted the struct method definitions to a static global context, and removed the on_unload definition. I wanted to try and be a "good citizen" and clear what was set on unload, but it probably doesn't matter in the grand scheme of things.

@warmwaffles
Copy link
Member Author

warmwaffles commented Feb 24, 2022

The SQLITE_CONFIG_MALLOC option takes a single argument which is a pointer to an instance of the sqlite3_mem_methods structure. The argument specifies alternative low-level memory allocation routines to be used in place of the memory allocation routines built into SQLite. SQLite makes its own private copy of the content of the sqlite3_mem_methods structure before the sqlite3_config() call returns.

The SQLITE_CONFIG_GETMALLOC option takes a single argument which is a pointer to an instance of the sqlite3_mem_methods structure. The sqlite3_mem_methods structure is filled with the currently defined memory allocation routines. This option can be used to overload the default memory allocation routines with a wrapper that simulations memory allocation failure or tracks memory usage, for example.

I'm unsure which one is the one we have to set.

I think SQLITE_CONFIG_MALLOC is the one we want.

Copy link

@laszlohegedus laszlohegedus left a comment

Choose a reason for hiding this comment

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

Looks great. Thank you.

@warmwaffles warmwaffles merged commit d079739 into main Feb 24, 2022
@warmwaffles warmwaffles deleted the allocator branch February 24, 2022 14:53
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.

2 participants