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

v.debug: implement tracing hook feature #20818

Merged
merged 15 commits into from
Feb 17, 2024
Merged

Conversation

felipensp
Copy link
Member

@felipensp felipensp commented Feb 13, 2024

This PR adds a feature to v.debug to allow tracing fn call. Activated by -d trace V flag.

debug.add_before_call(HookFnCall) HookFnCall

Adds a new hook fn that will be called before each fn call and return its idx

debug.add_after_call(HookFnCall) HookFnCall

Adds a new hook fn that will be called after each fn call and return its idx

debug.remove_before_call(HookFnCall)

Remove a hook fn from before hook list

debug.remove_after_call(HookFnCall)

Remove a hook fn from after hook list

Example

import time
import v.debug

@[heap]
struct Profiler {
mut:
	fn_map map[string]time.StopWatch
}

fn (mut p Profiler) trace_after(fn_name string) {
	println('>>> called after ${fn_name} - ${p.fn_map[fn_name].elapsed()}')
}

fn (mut p Profiler) trace_before(fn_name string) {
	p.fn_map[fn_name] = time.new_stopwatch()
	println('>>> called before ${fn_name}')
}

fn test_call() {
	println('${@FN} got called')
	time.sleep(100)
}

fn main() {
	mut myprofiler := Profiler{}
	debug.add_after_call(myprofiler.trace_after)
	debug.add_before_call(myprofiler.trace_before)
	test_call()
}
$ v -d trace run trace.v
>>> called before main.test_call
test_call got called
>>> called before time.sleep
>>> called after time.sleep - 35.800us
>>> called after main.test_call - 137.600us

@felipensp felipensp changed the title v.debug: implement tracing fn v.debug: implement tracing hook feature Feb 13, 2024
@spytheman
Copy link
Member

Why is it still draft, do you plan to add more features/bug fixes in the same PR?

@felipensp felipensp marked this pull request as ready for review February 16, 2024 10:24
@felipensp
Copy link
Member Author

Why is it still draft, do you plan to add more features/bug fixes in the same PR?

No. I was just waiting for some feedback/tip/comment about to improved something on It.

vlib/v/debug/trace.v Outdated Show resolved Hide resolved
vlib/v/debug/trace.v Outdated Show resolved Hide resolved
@spytheman spytheman merged commit 152a977 into vlang:master Feb 17, 2024
53 of 54 checks passed
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.

None yet

2 participants