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

Fix high CPU usage during ramalama pull #685

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

mkesper
Copy link

@mkesper mkesper commented Jan 31, 2025

Feel free to adjust block size. But 1k blocks are ridiculously low for downloading GiB of LLM data.

Related: #684

Summary by Sourcery

New Features:

  • Added a --quiet flag to suppress the progress bar during downloads.

Showing update progress in 1 kiB blocks adds unnecessary overhead.
100 kiB or even 1 MiB should be sufficient.

Related: containers#684
Copy link
Contributor

sourcery-ai bot commented Jan 31, 2025

Reviewer's Guide by Sourcery

This pull request introduces a new --quiet flag to suppress progress bars during downloads, and increases the download block size to 100KB to improve download speeds and reduce CPU usage.

Sequence diagram for the modified download process

sequenceDiagram
    participant Client
    participant DownloadManager
    participant HTTPClient

    Client->>DownloadManager: download_file(url, show_progress)
    DownloadManager->>HTTPClient: init(url, show_progress)
    activate HTTPClient
    loop Until EOF
        HTTPClient->>HTTPClient: read(100KB blocks)
        Note right of HTTPClient: Increased from 1KB to 100KB blocks
        alt show_progress is true
            HTTPClient->>Client: Update progress bar
        end
    end
    HTTPClient-->>DownloadManager: Download complete
    deactivate HTTPClient
    DownloadManager-->>Client: File downloaded
Loading

Class diagram showing modified download components

classDiagram
    class HTTPClient {
        -response
        -now_downloaded: int
        -start_time: float
        +init(url, headers, output_file, show_progress)
        +perform_download(file, progress)
    }

    class DownloadManager {
        +download_file(url, dest_path, headers, show_progress)
    }

    class PullCommand {
        +pull(args)
    }

    PullCommand ..> DownloadManager : uses
    DownloadManager ..> HTTPClient : uses

    note for HTTPClient "Block size increased to 100KB"
Loading

File-Level Changes

Change Details Files
Added a --quiet flag to suppress progress bars during downloads.
  • Added a --quiet argument to the pull_parser in ramalama/cli.py.
  • Modified ramalama/url.py to pass the quiet argument to the download_file function.
  • Modified ramalama/oci.py to pass the quiet argument to the container engine.
ramalama/cli.py
ramalama/url.py
ramalama/oci.py
Increased the download block size to 100KB.
  • Changed the read size in perform_download from 1024 bytes to 100 * 1024 bytes.
ramalama/http_client.py
Modified download_file to accept a show_progress argument.
  • Modified download_file in ramalama/common.py to accept a show_progress argument.
  • Modified init_pull and pull_blob in ramalama/ollama.py to pass the show_progress argument to download_file.
ramalama/common.py
ramalama/ollama.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @mkesper - I've reviewed your changes - here's some feedback:

Overall Comments:

  • The --quiet flag should use action="store_true" instead of action="store" with default=False to be consistent with other boolean flags in the codebase
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@@ -45,9 +45,9 @@ def pull_blob(repos, layer_digest, accept, registry_head, models, model_name, mo
run_cmd(["ln", "-sf", relative_target_path, model_path])


def init_pull(repos, accept, registry_head, model_name, model_tag, models, model_path, model):
def init_pull(repos, accept, registry_head, model_name, model_tag, models, model_path, model, show_progress):
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (bug_risk): Function signature mismatch: pull_config_blob is called with show_progress parameter but doesn't accept it

@ericcurtin
Copy link
Collaborator

Change looks fine to me @mkesper you just need to sign your commits with something like:

git commit -a -s --amend

to satisfy the DCO bot.

@ericcurtin
Copy link
Collaborator

ericcurtin commented Feb 1, 2025

Lots of tests failed too, we need to get this build green somehow, at least some of the failures are flakes, not sure how many.

This will all be rebuilt anyway when you the commits are signed.

@@ -177,7 +177,7 @@ def download_file(url, dest_path, headers=None, show_progress=True):
show_progress = False

try:
http_client.init(url=url, headers=headers, output_file=dest_path, progress=show_progress)
http_client.init(url=url, headers=headers, output_file=dest_path, show_progress=show_progress)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This rename needs to be made to HttpClient.init as well.

@ericcurtin
Copy link
Collaborator

ericcurtin commented Feb 1, 2025

Anybody know what "podman pull" or "podman artifact pull" block size is? @baude @rhatdan

We could just standardise on that, it's likely a sane value.

I probably care about reliability over cpu usage. I know in practice at the TCP level of the stack a max packet size is 1500 bytes, but that's probably not so important at the http level.

@ericcurtin
Copy link
Collaborator

It could be the progress bar causing the CPU usage issue also, maybe we could change it so that it doesn't update every single block iteration.

@ericcurtin
Copy link
Collaborator

ericcurtin commented Feb 1, 2025

Does quiet alone lower the CPU usage significantly @mkesper ?

@jhjaggars
Copy link
Collaborator

@ericcurtin Best I can tell, the image copier used in podman updates once per second by default:
https://github.com/containers/podman/blob/main/vendor/github.com/containers/common/libimage/copier.go#L315

@ericcurtin
Copy link
Collaborator

ericcurtin commented Feb 3, 2025

If you guys could check if this makes a difference I'd appreciate it, not much CPU usage issues on my machine (and maybe the time calculation is as expensive that printing a progress bar update, I've no idea):

#717

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.

3 participants