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

Profiler for Lua in Hammerspoon #2317

Open
asmagill opened this issue Mar 11, 2020 · 3 comments
Open

Profiler for Lua in Hammerspoon #2317

asmagill opened this issue Mar 11, 2020 · 3 comments

Comments

@asmagill
Copy link
Member

It's extremely crude, and I'm not yet 100% convinced its accurate enough to actually be useful, but if anyone is interested, I've put together a really simplistic profiler for looking at Hammerspoon's executing lua code...

https://github.com/asmagill/hammerspoon-config-take2/blob/master/_scratch/profile.lua

It hooks into the debug library with debug.sethook and tracks calls and returns... Looking online this appears to be the most common approach, but most I saw were aimed at 5.1... one (that I found after starting this) also checks line-by-line, but again looks like it focuses on 5.1 and LuaJit, but does say LuaJit is optional, so it may work with a little tweaking... it's at https://github.com/geoffleyland/luatrace if anyone wants to take a crack at it.

At any rate, I put this up for review, ridicule, feedback, whatever...

And if anyone knows of a better tool, I'm all ears -- like I said, a quick (but definitely not thorough) googling showed mostly similar scripts or things aimed at earlier lua versions.

@randomeizer
Copy link
Contributor

This requires your new coroutine support by the looks?

@asmagill
Copy link
Member Author

No, but it will work with it. If you're running stock (or a dev build without the coroutine branch) then you can ignore the notes about coroutines... it will just wrap functions you aren't using anyways.

@asmagill
Copy link
Member Author

Ok, if you downloaded the file earlier, trash it and get the latest version... the original mishandled tail calls completely. The comments also give some more details on using it.

I also changed the report format (I think this is easier to understand). By default, it doesn't include C functions in the report, though you can add those by passing true as an argument to the dump function.


The other project I linked to looks like it produces a line-by-line trace with a time given for each line... this would be extremely slow as it would mean using the line debug hook and then saving/writing data for each and every line... right now, I'm more interested in identifying slow functions so I can look closer at them and see if there is a better way, so I don't care about an actual trace... if there is interest, I may see about adding that later, but its not my priority...

Or someone else can do it sooner (or see about porting their code... the main things I see that would have to be addressed is that lua 5.1 didn't report tail call but instead reported tail return... not sure how/if that would change the logic, and I'm not sure how they're getting the debug hook invoked on coroutine threads (maybe 5.1 just used one rather than a separate one per thread?)

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

No branches or pull requests

2 participants