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

adds gnark/profile that outputs pprof compatible circuit profiling data #352

Merged
merged 9 commits into from
Aug 3, 2022

Conversation

gbotrel
Copy link
Collaborator

@gbotrel gbotrel commented Aug 2, 2022

gnark/profile is a profiling package, which creates pprof compatible profiling files. Once the .pprof file is generated, one can visualize it using go tool pprof like standard Golang CPU or Memory profiles.

A gnark Profile measures constraint added in a circuit (for now, all constraints are equal, but we may differentiate them in the future), example output (from examples/rollup):

image

image

image

Usage:

type Circuit struct {
	A frontend.Variable
}

func (circuit *Circuit) Define(api frontend.API) error {
	api.AssertIsEqual(api.Mul(circuit.A, circuit.A), circuit.A)
	return nil
}

func Example() {
	// default options generate gnark.pprof in current dir
	// use pprof as usual (go tool pprof -http=:8080 gnark.pprof) to read the profile file
	// overlapping profiles are allowed (define profiles inside Define or subfunction to profile
	// part of the circuit only)
	p := profile.Start()
	_, _ = frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &Circuit{})
	p.Stop()

	fmt.Println(p.NbConstraints())
	fmt.Println(p.Top())
	// Output:
	// 2
	// Showing nodes accounting for 2, 100% of 2 total
	//       flat  flat%   sum%        cum   cum%
	//          1 50.00% 50.00%          2   100%  profile_test.(*Circuit).Define profile/profile_test.go:17
	//          1 50.00%   100%          1 50.00%  r1cs.(*r1cs).AssertIsEqual frontend/cs/r1cs/api_assertions.go:37
}

This should make api.Counter and api.Tag useless.

@gbotrel gbotrel added this to the v0.8.0 milestone Aug 2, 2022
@gbotrel gbotrel requested a review from ivokub August 2, 2022 19:51
@ivokub
Copy link
Collaborator

ivokub commented Aug 3, 2022

Suggested edit:

diff --git a/profile/profile.go b/profile/profile.go
index 6ac2042e..3d3778d9 100644
--- a/profile/profile.go
+++ b/profile/profile.go
@@ -41,19 +41,22 @@ type Profile struct {
 	chDone chan struct{}
 }
 
-// ProfilePath controls the profile destination file. If blank, profile is not written.
+// Option defines configuration Options for Profile.
+type Option func(*Profile)
+
+// WithPath controls the profile destination file. If blank, profile is not written.
 //
 // Defaults to ./gnark.pprof.
-func ProfilePath(path string) func(*Profile) {
+func WithPath(path string) Option {
 	return func(p *Profile) {
 		p.filePath = path
 	}
 }
 
-// ProfileNoOutput indicates that the profile is not going to be written to disk.
+// WithNoOutput indicates that the profile is not going to be written to disk.
 //
-// This is equivalent to ProfilePath("")
-func ProfileNoOutput() func(*Profile) {
+// This is equivalent to WithPath("")
+func WithNoOutput() Option {
 	return func(p *Profile) {
 		p.filePath = ""
 	}
@@ -65,7 +68,7 @@ func ProfileNoOutput() func(*Profile) {
 // All calls to profile.Start() and Stop() are meant to be executed in the same go routine (frontend.Compile).
 //
 // It is allowed to create multiple overlapping profiling sessions in one circuit.
-func Start(options ...func(*Profile)) *Profile {
+func Start(options ...Option) *Profile {
 
 	// start the worker first time a profiling session starts.
 	onceInit.Do(func() {

Copy link
Collaborator

@ivokub ivokub left a comment

Choose a reason for hiding this comment

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

Looks good. Maybe only thing which mildly caught my eye was that for Plonk we don't record if the constraint comes from Add/Mul. But I guess that in the future when custom gates arrive we have to rethink anyway.

I also changed the profile option function signature to look similar with other Option types.

@gbotrel gbotrel merged commit e8ae5d6 into develop Aug 3, 2022
@gbotrel gbotrel deleted the feat/profile branch August 3, 2022 13:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants