-
Notifications
You must be signed in to change notification settings - Fork 742
optimize nerdctl ps #4732
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
base: main
Are you sure you want to change the base?
optimize nerdctl ps #4732
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,7 +47,7 @@ func List(ctx context.Context, client *containerd.Client, options types.Containe | |
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return prepareContainers(ctx, client, containers, cMap, options) | ||
| return prepareContainers(ctx, client, &containers, cMap, options) | ||
| } | ||
|
|
||
| // filterContainers returns containers matching the filters. | ||
|
|
@@ -134,50 +134,47 @@ func (x *ListItem) Label(s string) string { | |
| return x.LabelsMap[s] | ||
| } | ||
|
|
||
| func prepareContainers(ctx context.Context, client *containerd.Client, containers []containerd.Container, statusPerContainer map[string]string, options types.ContainerListOptions) ([]ListItem, error) { | ||
| listItems := make([]ListItem, len(containers)) | ||
| func prepareContainers(ctx context.Context, client *containerd.Client, containers *[]containerd.Container, statusPerContainer map[string]string, options types.ContainerListOptions) ([]ListItem, error) { | ||
| listItems := make([]ListItem, len(*containers)) | ||
| snapshottersCache := map[string]snapshots.Snapshotter{} | ||
| for i, c := range containers { | ||
| info, err := c.Info(ctx, containerd.WithoutRefreshedMetadata) | ||
| for i := range *containers { | ||
| info, err := (*containers)[i].Info(ctx, containerd.WithoutRefreshedMetadata) | ||
| if err != nil { | ||
| if errdefs.IsNotFound(err) { | ||
| log.G(ctx).Warn(err) | ||
| continue | ||
| } | ||
| return nil, err | ||
| } | ||
| spec, err := c.Spec(ctx) | ||
| if err != nil { | ||
| if errdefs.IsNotFound(err) { | ||
| log.G(ctx).Warn(err) | ||
| continue | ||
| } | ||
| return nil, err | ||
| } | ||
| id := c.ID() | ||
| id := (*containers)[i].ID() | ||
| if options.Truncate && len(id) > 12 { | ||
| id = id[:12] | ||
| } | ||
| var status string | ||
| if s, ok := statusPerContainer[c.ID()]; ok { | ||
| if s, ok := statusPerContainer[(*containers)[i].ID()]; ok { | ||
| status = s | ||
| } else { | ||
| return nil, fmt.Errorf("can't get container %s status", c.ID()) | ||
| return nil, fmt.Errorf("can't get container %s status", (*containers)[i].ID()) | ||
| } | ||
| dataStore, err := clientutil.DataStore(options.GOptions.DataRoot, options.GOptions.Address) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| containerLabels, err := c.Labels(ctx) | ||
| containerLabels, err := (*containers)[i].Labels(ctx) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| ports, err := portutil.LoadPortMappings(dataStore, options.GOptions.Namespace, c.ID(), containerLabels) | ||
| ports, err := portutil.LoadPortMappings(dataStore, options.GOptions.Namespace, (*containers)[i].ID(), containerLabels) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| cmd, err := formatter.GetCommandFromSpec(info.Spec, options.Truncate, true) | ||
| if err != nil { | ||
| continue | ||
| } | ||
| li := ListItem{ | ||
| Command: formatter.InspectContainerCommand(spec, options.Truncate, true), | ||
| Command: cmd, | ||
| CreatedAt: info.CreatedAt, | ||
| ID: id, | ||
| Image: info.Image, | ||
|
|
@@ -201,6 +198,7 @@ func prepareContainers(ctx context.Context, client *containerd.Client, container | |
| } | ||
| li.Size = containerSize | ||
| } | ||
| (*containers)[i] = nil | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here I want to let gc recycle the memory quickly
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That behavior has to be explained in the code comments in the function declaration |
||
| listItems[i] = li | ||
| } | ||
| return listItems, nil | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,7 @@ import ( | |
| "github.com/containerd/containerd/v2/pkg/oci" | ||
| "github.com/containerd/errdefs" | ||
| "github.com/containerd/go-cni" | ||
| "github.com/containerd/typeurl/v2" | ||
| ) | ||
|
|
||
| func ContainerStatus(ctx context.Context, c containerd.Container) string { | ||
|
|
@@ -73,6 +74,14 @@ func ContainerStatus(ctx context.Context, c containerd.Container) string { | |
| } | ||
| } | ||
|
|
||
| func GetCommandFromSpec(spec typeurl.Any, trunc, quote bool) (string, error) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code does not need to be a separate function, as it is called only once
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since spec have much data, If I use it into a function, it may cause quickly gc (gc may happen after the function return)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can just create a scope with Also please add the code comments about GC |
||
| var s oci.Spec | ||
| if err := json.Unmarshal(spec.GetValue(), &s); err != nil { | ||
| return "", err | ||
| } | ||
| return InspectContainerCommand(&s, trunc, quote), nil | ||
| } | ||
|
|
||
| func InspectContainerCommand(spec *oci.Spec, trunc, quote bool) string { | ||
| if spec == nil || spec.Process == nil { | ||
| return "" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why?