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

btcd: Add memory profiling flag #1953

Merged
merged 1 commit into from
May 10, 2023

Conversation

kcalvinalvin
Copy link
Collaborator

Enables Go memory profiling. If the cpuprofile shows a lot of time spent on gc, it's useful to then do a memory profile to see where the memory alloctions happen.

Unlike the --profile flag, this allows for easy generation of a memory profile for the entire duration of which btcd has been running for in various readble graphs.

@coveralls
Copy link

coveralls commented Feb 23, 2023

Pull Request Test Coverage Report for Build 4475766792

  • 0 of 9 (0.0%) changed or added relevant lines in 1 file are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage decreased (-0.01%) to 55.276%

Changes Missing Coverage Covered Lines Changed/Added Lines %
btcd.go 0 9 0.0%
Files with Coverage Reduction New Missed Lines %
mempool/mempool.go 1 66.75%
Totals Coverage Status
Change from base Build 4326890213: -0.01%
Covered Lines: 26650
Relevant Lines: 48213

💛 - Coveralls

return err
}
defer f.Close()
defer pprof.WriteHeapProfile(f)
Copy link
Contributor

Choose a reason for hiding this comment

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

since the file is only ever written to on shutdown, can't this whole if statement be put in a defer? that way we also dont need to keep the file open for the lifetime of the binary

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think it'd be nice if it errors out immediately if it's not able to create a file to write to.

I'm not sure on the costs of having a file descriptor for the duration of the program but if that's a concern, how about something like this:

        // Write mem profile if requested.
        if cfg.MemoryProfile != "" {
                f, err := os.Create(cfg.MemoryProfile)
                if err != nil {
                        btcdLog.Errorf("Unable to create memory profile: %v", err)
                        return err
                }
                f.Close()

                defer func() {
                        f, err := os.Create(cfg.MemoryProfile)
                        if err != nil {
                                btcdLog.Infof("Unable to create memory profile: %v", err)
                        }
                        err = pprof.WriteHeapProfile(f)
                        if err != nil {
                                btcdLog.Infof("Unable to create memory profile: %v", err)
                        }
                        f.Close()
                }()
        }

Copy link
Contributor

Choose a reason for hiding this comment

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

good point regarding failing fast on start up 👍 in that case, if the cost of the file descriptor is low then your original solution seems good enough!

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the cost is pretty low

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok I'll keep the current implementation.

Copy link
Member

@Roasbeef Roasbeef left a comment

Choose a reason for hiding this comment

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

LGTM 🦕

@Roasbeef
Copy link
Member

cc @Crypt-iQ

Copy link
Collaborator

@Crypt-iQ Crypt-iQ left a comment

Choose a reason for hiding this comment

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

just a comment about running the gc

return err
}
defer f.Close()
defer pprof.WriteHeapProfile(f)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the cost is pretty low

}
defer f.Close()
defer pprof.WriteHeapProfile(f)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

could add a defer runtime.GC() call at the end so that the heap profile is accurate as far as what's actually lingering

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@Roasbeef any opinions on this? It'll give us better data but it may slow things down during shutdown for normal users.

Copy link
Member

Choose a reason for hiding this comment

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

IIUC, that would only be active for users already running with a flag that strictly reserved for profiling. So I think that's fine.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added a runtime.GC() so that garbage collection happens before writes

Enables Go memory profiling.  If the cpuprofile shows a lot of time
spent on gc, it's useful to then do a memory profile to see where the
memory alloctions happen.

Unlike the --profile flag, this allows for easy generation of a memory
profile for the entire duration of which btcd has been running for in
various readble graphs.
@kcalvinalvin kcalvinalvin force-pushed the add-memory-profiling branch from 8478416 to d628705 Compare March 21, 2023 05:12
Copy link
Collaborator

@halseth halseth left a comment

Choose a reason for hiding this comment

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

LGTM

@guggero guggero merged commit 1287f1e into btcsuite:master May 10, 2023
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.

7 participants