Skip to content

proposal: automatic function instrumentation #15483

Closed as not planned
Closed as not planned

Description

@nektro

being able to measure the performance of your application is critical, and some level of automatic instrumentation is imperative to gaining the observability necessary to inspect all layers. this is why tools like tracy are not comparable solutions to this use case as they require manual insertion of all the instrumentation calls. this would tie the instrumentation to tracy and make calls to libraries entirely opaque so its a non-starter. however tracy usage could be used on top of this api.

however, it is also necessary to not simply put instrumentation in every function call in your app because the use cases that would gain the most value from this have core loops that are scoped regions where comparison over time is also usually a common point of analysis: eg web server request handlers, game frame renderer, etc.

non-exhaustive prior art

sample code

@profileStart(start, end);
someCall();
@profileStop();
@profileStart(
  start: *const fn(thread_id: os.pid_t, func_info: std.builtin.SourceLocation) void,
    end: *const fn(thread_id: os.pid_t, func_info: std.builtin.SourceLocation) void
) void

@profileEnd() void

more details:

  • this would insert start(); defer end(); calls into the callstack of every function under someCall();
  • start and end may not error, so as to not interfere with Zig's commitment to no hidden control flow
  • a thread_id of 0, null, or otherwise invalid value would be passed when the function is called outside the bounds of a @profileStart() / @profileEnd() bounds
  • being invisible to callees is a direct goal of this proposal
  • the added runtime cost of this intervention is a direct goal of this proposal, and accepted by users of this API as the ability to perform the introspection is worth it

Activity

  1. nektro commented on Apr 27, 2023

    @nektro
    ContributorAuthor

    digging into cProfile (python's builtin) it appears to use ptrace(2) and perf_event_open(2). I need to research its capabilities more, and will report here.

  2. leecannon commented on Apr 27, 2023

    @leecannon
    Contributor

    What happens in situations like this:

    fn main() void {
        someCall();
    
        @profileStart(start, end);
        someCall();
        @profileStop();
    
        @profileStart(start2, end2);
        someCall();
        @profileStop();
    }
  3. nektro commented on Apr 27, 2023

    @nektro
    ContributorAuthor

    @profileStart could set some global int

    if (profile_global == 1) start(tid(), @src);
    defer if (profile_global == 1) end(tid(), @src);
    if (profile_global == 2) start2(tid(), @src);
    defer if (profile_global == 2) end2(tid(), @src);
    
    ...
  4. matklad commented on May 2, 2023

    @matklad
    Contributor

    some level of automatic instrumentation is imperative to gaining the observability necessary to inspect all layers.

    I think this is debatable. Often, sampling, rather than instrumentation, works best if you want to gain visibility into everything. Eg, on Linux perf is used quite successfully for this, and I think Go CPU profiler is also based on sampling? (at least https://gperftools.github.io/gperftools/cpuprofile.html is).

  5. nektro commented on May 29, 2023

    @nektro
    ContributorAuthor

    sampling could definitely get me something good and it is an avenue I'm exploring but I opened this issue because my ideal is being able to generate an exact event stream that I can then pass into something like https://github.com/colrdavidson/spall

  6. added
    proposal
    on Jul 23, 2023
  7. added this to the 0.12.0 milestone on Jul 23, 2023
  8. nektro commented on Sep 20, 2023

    @nektro
    ContributorAuthor

    its a bit tedious but i think i've changed my mind on this and prefer manual instrumentation

  9. modified the milestones: 0.14.0, 0.13.0 on Jun 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    proposal

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions