Skip to content

[native] Init system used memory cache at the beginning of PeriodicMemoryChecker callback#26152

Closed
kletkavrubashku wants to merge 1 commit intoprestodb:masterfrom
kletkavrubashku:export-D83218739
Closed

[native] Init system used memory cache at the beginning of PeriodicMemoryChecker callback#26152
kletkavrubashku wants to merge 1 commit intoprestodb:masterfrom
kletkavrubashku:export-D83218739

Conversation

@kletkavrubashku
Copy link
Copy Markdown
Contributor

@kletkavrubashku kletkavrubashku commented Sep 25, 2025

Summary:
in Currently PeriodicMemoryChecker and descendants use these 2 methods to get current system used memory:

  • systemUsedMemoryBytes:
int64_t systemUsedMemoryBytes() {
  const auto currentMemBytes = ...;
  cachedSystemUsedMemoryBytes_ = currentMemBytes;
  return currentMemBytes;
}
  • cachedSystemUsedMemoryBytes
int64_t cachedSystemUsedMemoryBytes() const {
  return cachedSystemUsedMemoryBytes_;
}

Both methods are protected and don't become public in descendants, so their usage is limited by the classes itself. We get value from cache only if there is a prior systemUsedMemoryBytes call, before that the cache is empty. All systemUsedMemoryBytes and cachedSystemUsedMemoryBytes calls only happen inside the callback, this makes it easy to maintain the invariants.

The problem

In one of the upcoming diffs I'll need an access to the value of current system used memory inside periodicCb() implementation. The straightforward approach would be to just call systemUsedMemoryBytes, but it's a syscall and I noticed, that we can do some optimization in that area. We can do systemUsedMemoryBytes() call as the very first line in the callback and then everywhere below use cached value or actual value depends on needs. As example in PeriodicMemoryChecker::pushbackMemory we can also get the value from cache, and then update it by calling systemUsedMemoryBytes() after the pushback, which we're already doing in the code.

Differential Revision: D83218739

== NO RELEASE NOTE ==

@kletkavrubashku kletkavrubashku requested review from a team as code owners September 25, 2025 19:36
@prestodb-ci prestodb-ci added the from:Meta PR from Meta label Sep 25, 2025
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai bot commented Sep 25, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

This PR seeds the system memory usage cache at the start of each PeriodicMemoryChecker callback and switches subsequent memory checks to use the cached value, also updating the test fixture to initialize the cache to maintain consistency.

Class diagram for updated PeriodicMemoryChecker memory access methods

classDiagram
    class PeriodicMemoryChecker {
        - cachedSystemUsedMemoryBytes_: int64_t
        + systemUsedMemoryBytes(): int64_t
        + cachedSystemUsedMemoryBytes() const: int64_t
        + start()
        + periodicCb()
        + maybeDumpHeap()
        + pushbackMemory()
    }
    PeriodicMemoryChecker : systemUsedMemoryBytes() updates cachedSystemUsedMemoryBytes_
    PeriodicMemoryChecker : cachedSystemUsedMemoryBytes() returns cachedSystemUsedMemoryBytes_
    PeriodicMemoryChecker : start() seeds cache before callback
    PeriodicMemoryChecker : pushbackMemory() now uses cached value
Loading

File-Level Changes

Change Details Files
Prime system memory usage cache and use cached values in PeriodicMemoryChecker
  • Add initial systemUsedMemoryBytes() call at the start of the periodic callback
  • Replace systemUsedMemoryBytes() with cachedSystemUsedMemoryBytes() in the memory limit check
  • Update pushbackMemory() to read from cachedSystemUsedMemoryBytes() instead of triggering a syscall
presto-native-execution/presto_cpp/main/PeriodicMemoryChecker.cpp
Initialize cachedSystemUsedMemoryBytes_ in TestPeriodicMemoryChecker
  • Set cachedSystemUsedMemoryBytes_ to the provided systemUsedMemoryBytes_ in the test constructor
presto-native-execution/presto_cpp/main/tests/PeriodicMemoryCheckerTest.cpp

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!

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

@facebook-github-bot
Copy link
Copy Markdown
Collaborator

@kletkavrubashku has exported this pull request. If you are a Meta employee, you can view the originating diff in D83218739.

Copy link
Copy Markdown
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 there - I've reviewed your changes - here's some feedback:

  • Consider renaming systemUsedMemoryBytes() to reflect its side effect (e.g., refreshSystemMemoryCache()) and providing a pure accessor for reading the cached value.
  • Rather than seeding cachedSystemUsedMemoryBytes_ in the test subclass, consider adding a protected initializer or setter in PeriodicMemoryChecker to simplify test setup and encapsulate implementation details.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider renaming systemUsedMemoryBytes() to reflect its side effect (e.g., refreshSystemMemoryCache()) and providing a pure accessor for reading the cached value.
- Rather than seeding cachedSystemUsedMemoryBytes_ in the test subclass, consider adding a protected initializer or setter in PeriodicMemoryChecker to simplify test setup and encapsulate implementation details.

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.

@kletkavrubashku kletkavrubashku changed the title [presto][native] Init system used memory cache at the beginning of PeriodicMemoryChecker callback [native] Init system used memory cache at the beginning of PeriodicMemoryChecker callback Sep 25, 2025
…riodicMemoryChecker callback (prestodb#26152)

Summary:

 in Currently `PeriodicMemoryChecker` and descendants use these 2 methods to get current system used memory:
- `systemUsedMemoryBytes`:
```
int64_t systemUsedMemoryBytes() {
  const auto currentMemBytes = ...;
  cachedSystemUsedMemoryBytes_ = currentMemBytes;
  return currentMemBytes;
}
```
- `cachedSystemUsedMemoryBytes`
```
int64_t cachedSystemUsedMemoryBytes() const {
  return cachedSystemUsedMemoryBytes_;
}
```
Both methods are `protected` and don't become public in descendants, so their usage is limited by the classes itself. We get value from cache only if there is a prior `systemUsedMemoryBytes` call, before that the cache is empty. All `systemUsedMemoryBytes` and `cachedSystemUsedMemoryBytes` calls only happen inside the callback, this makes it easy to maintain the invariants.

### The problem
In one of the upcoming diffs I'll need an access to the value of current system used memory inside `periodicCb()` implementation. The straightforward approach would be to just call `systemUsedMemoryBytes`, but it's a syscall and I noticed, that we can do some optimization in that area. We can do `systemUsedMemoryBytes()` call as the very first line in the callback and then everywhere below use cached value or actual value depends on needs. As example in `PeriodicMemoryChecker::pushbackMemory` we can also get the value from cache, and then update it by calling `systemUsedMemoryBytes()` after the pushback, which we're already doing in the code.

Differential Revision: D83218739
@kletkavrubashku
Copy link
Copy Markdown
Contributor Author

Merged: #26347

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