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

contextmanager for ftrace #285

Merged
merged 1 commit into from
Oct 13, 2021
Merged

contextmanager for ftrace #285

merged 1 commit into from
Oct 13, 2021

Conversation

ahgamut
Copy link
Collaborator

@ahgamut ahgamut commented Oct 10, 2021

Python likes to make a bunch of function calls for anything, so ftrace-ing a Python script from start to end produces a TON of info. cosmo.ftrace() + cosmo.exit1() are nice, but I have to still structure my Python script to exit right after a particular line.

As mentioned here, This commit adds a simple contextmanager wrapper to cosmo.ftrace() so instead I can do things like this:

import cosmo

with cosmo.ftrace(): # everything inside this will be logged to stderr
    a = 2 + 3

# logging disabled, do other things without having to terminate
print("a =", a)

the above snippet produces 32 lines of ftrace in stderr, which is much easier to examine:

+       sigprocmask 15481145
+ meth_dealloc 11490
+ PyFrame_BlockSetup 29
+ object_dealloc 389
+ PyObject_Free 61
+ _PyObject_Free.isra.0 14
+ PyDict_SetItem 372
+ insertdict 72
+   lookdict_unicode_nodummy 141
+   dictresize 133
+     new_keys_object 198
+       PyObject_Malloc 99
+       PyMem_Malloc 14
+       _PyObject_Alloc.isra.0 14
+         dlmalloc 54
+           dlmalloc_impl 14
+             tmalloc_large 78
+       memset 372
+       memset_avx 44
+       bzero 43
+       bzero_avx 38
+     PyObject_Free 197
+     _PyObject_Free.isra.0 10
+   find_empty_slot.isra.0 448
+ PyFrame_BlockPop 96
+ PyObject_CallFunctionObjArgs 252
+   objargs_mkstack.constprop.0 31
+   _PyObject_FastCallDict 55
+     _PyCFunction_FastCallDict 40
+       PyTuple_New 116
+       TracerObject_exit 150
a =  5

@pkulchenko
Copy link
Collaborator

I wonder if something like this can be made available for redbean as well (or maybe it already is and just need to be exposed to the Lua layer)? @jart?

@jart
Copy link
Owner

jart commented Oct 13, 2021

@pkulchenko Absolutely. Lua doesn't require parens in function calls, and it lets you pass code to a function, so we could probably define the syntax as follows:

ftrace {
  some_code()
}

Copy link
Owner

@jart jart left a comment

Choose a reason for hiding this comment

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

LGTM. Cool change! So excited to see people using the function call tracing.

third_party/python/Python/cosmomodule.c Show resolved Hide resolved
@jart jart merged commit d852640 into jart:master Oct 13, 2021
@pkulchenko
Copy link
Collaborator

pkulchenko commented Oct 13, 2021

@pkulchenko Absolutely. Lua doesn't require parens in function calls, and it lets you pass code to a function, so we could probably define the syntax as follows:

ftrace {
  some_code()
}

@jart, we may need to tweak this a bit, as it will call some_code and then pass the results. So, maybe either: ftrace(some_code) (to pass an existing function) or ftrace(function() some_code() some_other_code() end) to pass a closure).

If we're setting/removing a hook, then ftrace_start() and ftrace_stop() may be appropriate as well.

I've been also wondering if it's possible to keep the trace output internal and implement sort of a sliding window for it: ftrace_start(50) turns it on, but doesn't generate any output and always keeps the last (up to) 50 calls; I can then call ftrace_flush() and the collected info is then flushed to stderr. This way I don't need to go through the entire log and can add _flush to the places where I need it (and it may be better than start/stop, since I may come to the same place from multiple points and want to see which branch is being triggered).

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.

3 participants