-
Notifications
You must be signed in to change notification settings - Fork 46
[DX-3763] dump inuse and memory profile #2516
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
Open
Tofel
wants to merge
2
commits into
main
Choose a base branch
from
dx-3763-dump-two-profiles
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| - Dump two memory profiles: inuse and alloc | ||
| - Dump LOOPs profiles via admin command |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| package leak | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "path" | ||
| "path/filepath" | ||
| "regexp" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/docker/docker/api/types/container" | ||
| "github.com/docker/docker/client" | ||
| f "github.com/smartcontractkit/chainlink-testing-framework/framework" | ||
| ) | ||
|
|
||
| var containerNameSanitizer = regexp.MustCompile(`[^a-zA-Z0-9._-]`) | ||
|
|
||
| const ( | ||
| DefaultAdminProfilesDir = "admin-profiles" | ||
| DefaultNodeProfileDumpTimeout = 5 * time.Minute | ||
| ) | ||
|
|
||
| // DumpNodeProfiles runs chainlink profile collection in each running container | ||
| // with a name containing namePattern and copies ./profiles content to dst/profile-<container-name>. | ||
| func DumpNodeProfiles(ctx context.Context, namePattern, dst string) error { | ||
| f.L.Info(). | ||
| Str("NamePattern", namePattern). | ||
| Str("DestinationDir", dst). | ||
| Msg("Dumping node profiles by container name pattern") | ||
|
|
||
| if strings.TrimSpace(namePattern) == "" { | ||
| return fmt.Errorf("container name pattern must not be empty") | ||
| } | ||
| if strings.TrimSpace(dst) == "" { | ||
| return fmt.Errorf("destination path must not be empty") | ||
| } | ||
|
|
||
| if err := os.MkdirAll(dst, 0o755); err != nil { | ||
| return fmt.Errorf("failed to create destination directory %q: %w", dst, err) | ||
| } | ||
|
|
||
| cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create Docker client: %w", err) | ||
| } | ||
| defer cli.Close() | ||
| dc, err := f.NewDockerClient() | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create framework docker client: %w", err) | ||
| } | ||
|
|
||
| containers, err := runningContainers(ctx, cli) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| var errs []error | ||
| for _, c := range containers { | ||
| if !strings.Contains(c.name, namePattern) { | ||
| continue | ||
| } | ||
|
|
||
| // Keep destination names safe and filesystem-friendly. | ||
| safeName := containerNameSanitizer.ReplaceAllString(c.name, "_") | ||
| targetDir := filepath.Join(dst, fmt.Sprintf("profile-%s", safeName)) | ||
| if err := os.MkdirAll(targetDir, 0o755); err != nil { | ||
| errs = append(errs, fmt.Errorf("failed to create destination directory for %s: %w", c.name, err)) | ||
| continue | ||
| } | ||
|
|
||
| f.L.Info().Str("ContainerName", c.name).Msg("Collecting node profile") | ||
|
|
||
| out, execErr := dc.ExecContainerWithContext( | ||
| ctx, | ||
| c.name, | ||
| []string{"chainlink", "admin", "profile", "-seconds", "1", "-output_dir", "./profiles"}, | ||
| ) | ||
| if execErr != nil { | ||
| errs = append(errs, fmt.Errorf("failed to execute profile command in container %s: %w, output: %s", c.name, execErr, strings.TrimSpace(out))) | ||
| continue | ||
| } | ||
|
|
||
| profilesPath := path.Clean(path.Join(c.workingDir, "profiles")) | ||
| if copyErr := dc.CopyFromContainerToHostWithContext(ctx, c.name, profilesPath, targetDir); copyErr != nil { | ||
| errs = append(errs, fmt.Errorf("failed to copy profiles from container %s to %s: %w", c.name, targetDir, copyErr)) | ||
| continue | ||
| } | ||
|
|
||
| f.L.Info().Str("ContainerName", c.name).Str("Destination", targetDir).Msg("Profiles copied") | ||
| } | ||
|
|
||
| return errors.Join(errs...) | ||
| } | ||
|
|
||
| type runningContainer struct { | ||
| name string | ||
| workingDir string | ||
| } | ||
|
|
||
| func runningContainers(ctx context.Context, cli *client.Client) ([]runningContainer, error) { | ||
| containers, err := cli.ContainerList(ctx, container.ListOptions{}) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to list running Docker containers: %w", err) | ||
| } | ||
|
|
||
| res := make([]runningContainer, 0, len(containers)) | ||
| for _, c := range containers { | ||
| name := firstContainerName(c.Names) | ||
| if name == "" { | ||
| continue | ||
| } | ||
|
|
||
| inspect, inspectErr := cli.ContainerInspect(ctx, c.ID) | ||
| if inspectErr != nil { | ||
| return nil, fmt.Errorf("failed to inspect container %s: %w", name, inspectErr) | ||
| } | ||
| workingDir := "/" | ||
| if inspect.Config != nil && inspect.Config.WorkingDir != "" { | ||
| workingDir = inspect.Config.WorkingDir | ||
| } | ||
| res = append(res, runningContainer{ | ||
| name: name, | ||
| workingDir: workingDir, | ||
| }) | ||
| } | ||
| return res, nil | ||
| } | ||
|
|
||
| func firstContainerName(names []string) string { | ||
| for _, n := range names { | ||
| if n == "" { | ||
| continue | ||
| } | ||
| return strings.TrimPrefix(n, "/") | ||
| } | ||
| return "" | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.