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

Add support for calculating virtual image size as well. #1918

Closed
wants to merge 1 commit into from

Conversation

manugupt1
Copy link
Contributor

@manugupt1 manugupt1 commented Jan 24, 2023

Testing details

➜  nerdctl git:(virtual-size) ✗ nerdctl image list --format 'table ID:{{.ID}};Repo:{{.Repository}};Tag:{{.Tag}};Size:{{.Size}};VirtSize:{{.VirtualSize}}'

 table ID:f271e74b17ce;Repo:alpine;Tag:latest;Size:8.3 MiB;VirtSize:8.3 MiB table ID:3487c98481d1;Repo:fedora;Tag:latest;Size:195.1 MiB;VirtSize:195.1 MiB

@AkihiroSuda
Copy link
Member

Size:8.3 MiB;VirtSize:8.3 MiB

This shouldn't be same?

// https://github.com/moby/moby/blob/65c371ed6e48d0014a4826a6933f9231f688327a/daemon/containerd/image_list.go#L184-L194
// What is VirtualSize: https://github.com/docker/docs/issues/1520#issuecomment-305179362
func computeVirtualSize(ctx context.Context, s snapshots.Snapshotter, img containerd.Image) (int64, error) {
chainIDs, err := img.RootFS(ctx)
Copy link
Member

Choose a reason for hiding this comment

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

}
var virtualSize int64
for _, chainID := range chainIDs {
usage, err := s.Usage(ctx, chainID.String())
Copy link
Member

Choose a reason for hiding this comment

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

Can we cache this to avoid calculating the usage twice

@manugupt1
Copy link
Contributor Author

manugupt1 commented Jan 24, 2023

docker/docs#1520 (comment)

See: docker/docs#16315
I think for images it is the same. For containers when we have a writeable layer; it will change.

Also this: moby/moby#43862 :/

@manugupt1 manugupt1 force-pushed the virtual-size branch 2 times, most recently from 28c774c to 2464f7f Compare January 25, 2023 06:02
@manugupt1
Copy link
Contributor Author

@AkihiroSuda I made the change equivalent to moby's containerd implementation. Should we keep blob size now or remove it?
This is sort of a breaking change; will you like to keep the previous behavior or maintain compatibility with moby.

@manugupt1
Copy link
Contributor Author

@fahedouch Can you please have a look? Let me know what do you think?

//add ChainID's parent usage to the total usage
if err := snapshotKey(info.Parent).add(ctx, s, &usage); err != nil {
return 0, err
virtualSize += usage.Size
Copy link
Member

Choose a reason for hiding this comment

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

is the container writable layer counted here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For images; it is zero; so it really does not matter.
For containers; the writable layer size will be added.

Copy link
Member

Choose a reason for hiding this comment

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

@manugupt1

My understanding is that VirtualSize = blob size + writable layer size

but here you are not counting the writable layer size (before your modification it was counted by adding the parent of most top DiffId size to the total size)

What I did in the past is

  1. calculate the chainID of the readonly layer
  2. add chainID size to the total size
  3. calculate the chainID parent size ( writable layer)
  4. add the chainID parent size to the total size
info, err := s.Stat(ctx, chainID)
if err != nil {
   return 0, err
}
//add ChainID's parent usage to the total usage
	if err := snapshotKey(info.Parent).add(ctx, s, &usage); err != nil {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@fahedouch Sorry for the late response. I have updated this PR; the previous behavior was correct and when I read through the moby issue; this is the behavior that we want.
However, now containerd provides withSnapshotUsage so we can re-use it. I have refactored the code and also added the field for VirtualSize.
Please let me know if this sounds good to you. I can write tests if this sounds good to you.

@@ -304,7 +303,8 @@ func (x *imagePrinter) printImageSinglePlatform(ctx context.Context, img images.
Tag: tag,
Name: img.Name,
Size: progress.Bytes(size).String(),
BlobSize: progress.Bytes(blobSize).String(),
VirtualSize: progress.Bytes(virtualSize).String(),
BlobSize: progress.Bytes(size).String(), // Was introduced since docker had a different behavior earlier.
Copy link
Member

Choose a reason for hiding this comment

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

do we really need BlobSize here ? I think we should keep either size or BlobSize

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we should keep Size (needed your opinion on it).
This will make tests docker incompatible until docker moves to containerd.

Copy link
Member

Choose a reason for hiding this comment

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

SGTM

@@ -177,7 +175,8 @@ type imagePrintable struct {
Name string // image name
Size string // the size of the unpacked snapshots.
Copy link
Member

@fahedouch fahedouch Feb 7, 2023

Choose a reason for hiding this comment

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

since your modification // the size of the unpacked snapshots. => wrong

@fahedouch fahedouch self-requested a review February 10, 2023 23:22
Copy link
Member

@fahedouch fahedouch left a comment

Choose a reason for hiding this comment

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

Could you please check my comment above.

@manugupt1 manugupt1 force-pushed the virtual-size branch 3 times, most recently from 6866ce5 to 07a9347 Compare March 8, 2023 07:30
Signed-off-by: Manu Gupta <manugupt1@gmail.com>
@manugupt1 manugupt1 requested a review from fahedouch March 8, 2023 07:48
@manugupt1 manugupt1 marked this pull request as ready for review March 10, 2023 03:09
@manugupt1 manugupt1 added this to the v1.4.0 (tentative) milestone Mar 10, 2023
@@ -245,6 +246,7 @@ func (x *imagePrinter) printImageSinglePlatform(ctx context.Context, img images.
Tag: tag,
Name: img.Name,
Size: progress.Bytes(size).String(),
VirtualSize: progress.Bytes(size).String(),
Copy link
Member

Choose a reason for hiding this comment

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

Needs comment lines to explain why these are same

Copy link
Member

Choose a reason for hiding this comment

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

Also needs integration tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep! Wanted to get an initial look. Will add by the end of this week.
Thanks!

@@ -119,6 +119,7 @@ type imagePrintable struct {
Name string // image name
Size string // the size of the unpacked snapshots.
BlobSize string // the size of the blobs in the content store (nerdctl extension)
VirtualSize string
Copy link
Member

Choose a reason for hiding this comment

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

Needs a comment line

}
return usage.Size, nil
func UnpackedImageSize(ctx context.Context, img containerd.Image) (int64, error) {
return img.Usage(ctx, containerd.WithSnapshotUsage())
Copy link
Member

Choose a reason for hiding this comment

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

Is this just a refactoring, or does this change the computed size?
Could you consider making this a separate commit/PR ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It does not change the computed size at all. I verified locally.

@manugupt1
Copy link
Contributor Author

@fahedouch Ping

@manugupt1
Copy link
Contributor Author

Even the sizes are inconistent now with docker and nerdctl.
See: moby/moby#45180
Closing this.

@manugupt1 manugupt1 closed this Mar 19, 2023
@manugupt1 manugupt1 deleted the virtual-size branch March 19, 2023 20:28
@fahedouch
Copy link
Member

@manugupt1 why closing this ?

@manugupt1
Copy link
Contributor Author

manugupt1 commented Mar 20, 2023

I mostly closed this because of moby/moby#45180
Also: As per moby: https://github.com/moby/moby/blob/7489b51f610104ab5acc43f4e77142927e7b522e/api/types/types.go#L128 this can be deprecated and no longer relevant.

@AkihiroSuda AkihiroSuda removed this from the v1.3.1 milestone Apr 13, 2023
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.

3 participants