-
-
Notifications
You must be signed in to change notification settings - Fork 12k
ENH: expose PyDataMem_NEW/FREE/RENEW as numpy API functions with an event hook. #309
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
Conversation
…vent hook.
Moves PyDataMem_NEW/FREE/RENEW to the external API.
Fixes PyDataMem_NEW/RENEW to return void* instead of char*.
Replaces PyDataMem_NEW/FREE with NpySortArray_malloc/free in sort.c.src
(should be reverted if npysort is moved to be part of multiarraymodule).
Adds PyDataMem_SetEventHook which takes a (PyDataMem_EventHookFunc *) as an argument,
with signature:
void hook(void *old, void *new, size_t size).
When not NULL, hook will be called at the end of each PyDataMem_NEW/FREE/RENEW:
result = PyDataMem_NEW(size) -> (*hook(NULL, result, size)
PyDataMem_FREE(ptr) -> (*hook(ptr, NULL, 0)
result = PyDataMem_RENEW(ptr, size) -> (*hook)(ptr, result, size)
Adds tests in multiarray_tests.c.src, driven by tests/test_multiarray.py.
|
EventHookFunc and SetEventHook need to take a void *user_data argument.
|
|
...Now that I think about it, there's also a concurrency problem here, since PyDataMem_NEW/FREE/RENEW can be called without the GIL. So PyDataMem_SetHookFunc could race with them, and we need a mutex of some kind to prevent calling into the wrong function pointer or with the wrong user_data. To avoid overhead on the no-hook path though I think it'd be okay to do something like The GIL could be used for the mutex, or else the PyThread_*_lock API could be used directly. Sorry I keep coming up with extra issues here! You might want to run the API by the list first just to make sure that there's consensus on it before working on it more. (Most people don't see pull requests.) |
|
No problem, I appreciate the feedback. I'll put in a fix for locking, and post to the mailing list. |
Wraps the SetHook and calls to the hook with the GIL, to prevent races. Adds an example of using the interface for callbacks into python code.
|
Limited response on the mailing list (Dag seems to like the idea and possible extensions). @charris @cournape @teoliphant have all expressed opinions on the other pull requests, so I'll ping them here. |
|
This seems like quite a useful feature that could help debug many programs. I am supportive of its inclusion. |
|
Any more comments on this PR? Mailing list was similarly quiet when asked. |
|
Looks good, I guess! Only thought I've had is from this message http://www.mail-archive.com/numpy-discussion@scipy.org/msg37843.html, maybe an even nicer name would be PyDataMem_SetTraceHook? But that's pretty trivial. I think github's saying there's a merge conflict ("This pull request cannot be automatically merged"). Can you merge from master or rebase? |
|
I've toyed with different names (Trace, Log, Event), and decided to go with Event because it seemed the most generic. I'll rebase, and also update the numpy api hash (I had held off on that part, because I knew the NA removal changes were probably coming soon). |
|
I merged master to this branch. If you prefer a cleaner history, I also pushed a malloc_hooks_rebase branch to my repo. Any chance of this making it into 1.7? |
|
Not sure what's going on with 1.7, but probably? Sorry for all the delays and back and forth here. |
ENH: expose PyDataMem_NEW/FREE/RENEW as numpy API functions with an event hook.
|
Holy cow, this tool is really useful! Is there any documentation for it on http://docs.scipy.org? For that matter, is there any reason it can't be moved out of this |
[BUGFIX] Fix neon2rvv failing tests
This is the simplest intersection of two previous pull requests:
#301
#284
Changes:
Moves PyDataMem_NEW/FREE/RENEW to the external API.
Fixes PyDataMem_NEW/RENEW to return void* instead of char_.
Replaces PyDataMem_NEW/FREE with NpySortArray_malloc/free in sort.c.src
(should be reverted if npysort is moved to be part of multiarraymodule).
Adds PyDataMem_SetEventHook which takes a (PyDataMem_EventHookFunc *) as an argument,
with signature:
void hook(void *old, void *new, size_t size).
When not NULL, hook will be called at the end of each PyDataMem_NEW/FREE/RENEW:
result = PyDataMem_NEW(size) -> (_hook(NULL, result, size)
PyDataMem_FREE(ptr) -> (_hook(ptr, NULL, 0)
result = PyDataMem_RENEW(ptr, size) -> (_hook)(ptr, result, size)
Adds tests in multiarray_tests.c.src, driven by tests/test_multiarray.py.