Skip to content

8337217: Port VirtualMemoryTracker to use VMATree #20425

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

Closed

Conversation

afshin-zafari
Copy link
Contributor

@afshin-zafari afshin-zafari commented Aug 1, 2024

  • VMATree is used instead of SortedLinkList in new class VirtualMemoryTracker.
  • A wrapper/helper RegionTree is made around VMATree to make some calls easier.
  • find_reserved_region() is used in 4 cases, it will be removed in further PRs.
  • All tier1 tests pass except this https://bugs.openjdk.org/browse/JDK-8335167.

Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8337217: Port VirtualMemoryTracker to use VMATree (Enhancement - P4)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/20425/head:pull/20425
$ git checkout pull/20425

Update a local copy of the PR:
$ git checkout pull/20425
$ git pull https://git.openjdk.org/jdk.git pull/20425/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 20425

View PR using the GUI difftool:
$ git pr show -t 20425

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/20425.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Aug 1, 2024

👋 Welcome back azafari! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Aug 1, 2024

@afshin-zafari This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8337217: Port VirtualMemoryTracker to use VMATree

Reviewed-by: jsjolen, gziemski

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 1 new commit pushed to the master branch:

  • 5726606: 8359709: java.net.HttpURLConnection sends unexpected "Host" request header in some cases after JDK-8344190

Please see this link for an up-to-date comparison between the source branch of this pull request and the master branch.
As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk
Copy link

openjdk bot commented Aug 1, 2024

@afshin-zafari The following labels will be automatically applied to this pull request:

  • core-libs
  • hotspot

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing lists. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added hotspot hotspot-dev@openjdk.org core-libs core-libs-dev@openjdk.org labels Aug 1, 2024
@afshin-zafari
Copy link
Contributor Author

/label hotspot-runtime

@openjdk openjdk bot added the hotspot-runtime hotspot-runtime-dev@openjdk.org label Aug 1, 2024
@openjdk
Copy link

openjdk bot commented Aug 1, 2024

@afshin-zafari
The hotspot-runtime label was successfully added.

Copy link
Contributor

@jdksjolen jdksjolen left a comment

Choose a reason for hiding this comment

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

Hi,

Great work! I'm slowly working through the code, let's take our time with this. My PR got too stressful for everyone involved :-/.

I've reviewed some of the code. Could you go a bit more in-depth on what the results of this work was? Did it improve performance, for example?

All tier1 tests pass except one that expects a 50% increase in committed memory but it does not happen.

I don't understand what this means, could you expand on this? Edit: Re-read it again now and I get it, which test is this?

Thanks!

@@ -37,8 +37,9 @@
// For example, the state may go from released memory to committed memory,
// or from committed memory of a certain MEMFLAGS to committed memory of a different MEMFLAGS.
// The set of points is stored in a balanced binary tree for efficient querying and updating.
class VMATree {
class VMATree : public AnyObj {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is it AnyObj now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mainly to be able to use new operator. Changed to CheapObj<mtNMT>.

});
}

void VirtualMemoryTrackerWithTree::apply_summary_diff(VMATree::SummaryDiff diff) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Applying a summary diff in the MemoryFileTracker is this:

  for (int i = 0; i < mt_number_of_types; i++) {
    VirtualMemory* summary = file->_summary.by_type(NMTUtil::index_to_flag(i));
    summary->reserve_memory(diff.flag[i].commit);
    summary->commit_memory(diff.flag[i].commit);
  }

This is much simpler and doesn't require looking at signs and so on.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In MemoryFileTracker, the changes in commit/reserve are applied to a local VirtualMemorySnapshot. Here we have to apply them to the global VirtualMemorySummary.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, that doesn't seem like a problem.

  for (int i = 0; i < mt_number_of_types; i++) {
    r = diff.flag[i].reserve;
    c = diff.flag[i].commit;
    flag = NMTUtil::index_to_flag(i);
    VirtualMemory* mem = VirtualMemorySummary::as_snapshot()->by_type(flag);
    reserved = mem->reserved();
    committed = mem->committed();
    mem->reserve_memory(r);
    mem->commit_memory(c);
    if ((size_t)-r > reserved) {
      print_err("release");
    }
    if ((size_t)-c > reserved || (size_t)c > committed) {
      print_err("uncommit");
    }
  }

Copy link
Contributor

Choose a reason for hiding this comment

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

This applies the reserve/commit regardless of outcome, so slightly different.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The main purpose of the if (...) cases is to find if the request to apply the delta is valid or not. There are related assertions in VirtualMemory but not so informative. Also, using log_debug lets the build proceed and just show the messages.
These messages help a lot when something goes wrong in terms of commit/uncommit/release failure.

Copy link
Contributor

Choose a reason for hiding this comment

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

How does my example code not account for this? You can still get rid of the sign checking.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Case of 'commit' error is missing from your suggestion. At commit time, c > reserved is invalid too.
(size_t)-r for positive r is a large number and is greater than reserved.
We have to find out the intent of the call first (by checking the sign of r or c) and then check if it is valid or not.

Copy link
Contributor Author

@afshin-zafari afshin-zafari left a comment

Choose a reason for hiding this comment

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

Thanks for your comments. They are replied or applied.

@openjdk
Copy link

openjdk bot commented Aug 4, 2024

⚠️ @afshin-zafari This pull request contains merges that bring in commits not present in the target repository. Since this is not a "merge style" pull request, these changes will be squashed when this pull request in integrated. If this is your intention, then please ignore this message. If you want to preserve the commit structure, you must change the title of this pull request to Merge <project>:<branch> where <project> is the name of another project in the OpenJDK organization (for example Merge jdk:master).

});
}

void VirtualMemoryTrackerWithTree::apply_summary_diff(VMATree::SummaryDiff diff) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, that doesn't seem like a problem.

  for (int i = 0; i < mt_number_of_types; i++) {
    r = diff.flag[i].reserve;
    c = diff.flag[i].commit;
    flag = NMTUtil::index_to_flag(i);
    VirtualMemory* mem = VirtualMemorySummary::as_snapshot()->by_type(flag);
    reserved = mem->reserved();
    committed = mem->committed();
    mem->reserve_memory(r);
    mem->commit_memory(c);
    if ((size_t)-r > reserved) {
      print_err("release");
    }
    if ((size_t)-c > reserved || (size_t)c > committed) {
      print_err("uncommit");
    }
  }

});
}

void VirtualMemoryTrackerWithTree::apply_summary_diff(VMATree::SummaryDiff diff) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This applies the reserve/commit regardless of outcome, so slightly different.

static RegionsTree* tree() { return _tree; }

private:
static RegionsTree* _tree;
Copy link
Contributor

Choose a reason for hiding this comment

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

If you do the instance/static pattern then you can switch this into a regular member, no pointer necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The _tree ctor to be called in initialize with a bool depending the NMT_Level. Existing VMT also uses a pointer to a SortedLinkList.
I am not sure yet: What is the advantage of not-a-pointer member? How the instance/static helps this?

Copy link
Contributor Author

@afshin-zafari afshin-zafari left a comment

Choose a reason for hiding this comment

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

More fixes and questions.

@afshin-zafari
Copy link
Contributor Author

Performance test runs insert/remove operations for SortedLinkList (SLL in the code) and Treap and expects that Treap be faster. Both tests pass with a factor of 70+ faster for 10000 elements.

@gerard-ziemski
Copy link

Are we keeping both the linked list based and VMATree based implementations just for the review process, and we will drop the linked list one, once we are done with the review, or are they both going to be checked in?

@gerard-ziemski
Copy link

Is there a concrete advantage here for using lambda expression when iterating committed regions? I'm asking because personally I find

    while ((committed_rgn = itr.next()) != nullptr) {
      print_committed_rgn(committed_rgn);
    }

simpler and more compact, hence easier to understand, than

    CommittedMemoryRegion cmr;
    VirtualMemoryTrackerWithTree::tree()->visit_committed_regions(reserved_rgn, &cmr, [&](CommittedMemoryRegion* crgn) {
      print_committed_rgn(crgn);
      return true;
    });

@gerard-ziemski
Copy link

Same here:

    if (MemTracker::is_using_tree()) {
      CommittedMemoryRegion cmr;
      bool reserved_and_committed = false;
      VirtualMemoryTrackerWithTree::tree()->visit_committed_regions(reserved_rgn,
                                                                    &cmr,
                                                                    [&](CommittedMemoryRegion* committed_rgn) {
        if (committed_rgn->size() == reserved_rgn->size() && committed_rgn->call_stack()->equals(*stack)) {
          // One region spanning the entire reserved region, with the same stack trace.
          // Don't print this regions because the "reserved and committed" line above
          // already indicates that the region is committed.
          reserved_and_committed = true;
          return false;
        }
        return true;
      });

      if (reserved_and_committed)
        return;
    }
  }

We are setting reserved_and_committed inside lambda, then we need to check it outside. It's messy and harder to follow which return belongs to which context.

@openjdk openjdk bot removed the merge-conflict Pull request has merge conflict with target branch label Mar 7, 2025
@bridgekeeper
Copy link

bridgekeeper bot commented Apr 4, 2025

@afshin-zafari This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

@bridgekeeper
Copy link

bridgekeeper bot commented May 2, 2025

@afshin-zafari This pull request has been inactive for more than 8 weeks and will now be automatically closed. If you would like to continue working on this pull request in the future, feel free to reopen it! This can be done using the /open pull request command.

@bridgekeeper bridgekeeper bot closed this May 2, 2025
@afshin-zafari
Copy link
Contributor Author

/open

@openjdk openjdk bot reopened this Jun 16, 2025
@openjdk
Copy link

openjdk bot commented Jun 16, 2025

@afshin-zafari This pull request is now open

Copy link
Contributor

@jdksjolen jdksjolen left a comment

Choose a reason for hiding this comment

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

LGTM!

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jun 17, 2025
Copy link

@gerard-ziemski gerard-ziemski left a comment

Choose a reason for hiding this comment

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

Small changes (copyright years) and one question, otherwise LGTM.

Nice!

Comment on lines +258 to +259
RegionsTree* rtree = VirtualMemoryTracker::Instance::tree();
rtree->tree().remove_all();

Choose a reason for hiding this comment

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

Why are we calling remove_all() right after we create the tree?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We are not creating the tree here. We just retrieve it from the Instance of VMT. Since the tree is also visible to other tests (it is static and not created per test), any changes in other tests will be visible here by this test. This is not as expected in the design of the tests (tests assume that tree is empty at the beginning of the test).

@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Jun 18, 2025
@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jun 18, 2025
@afshin-zafari
Copy link
Contributor Author

Thanks for reviews @gerard-ziemski and @jdksjolen.
/integrate

@openjdk
Copy link

openjdk bot commented Jun 18, 2025

Going to push as commit 547ce03.
Since your change was applied there have been 3 commits pushed to the master branch:

  • f07f5ce: 8359067: Fix typo in DelayScheduler.java
  • cabd7c1: 8356897: Update NSS library to 3.111
  • 5726606: 8359709: java.net.HttpURLConnection sends unexpected "Host" request header in some cases after JDK-8344190

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Jun 18, 2025
@openjdk openjdk bot closed this Jun 18, 2025
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Jun 18, 2025
@openjdk
Copy link

openjdk bot commented Jun 18, 2025

@afshin-zafari Pushed as commit 547ce03.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hotspot hotspot-dev@openjdk.org hotspot-runtime hotspot-runtime-dev@openjdk.org integrated Pull request has been integrated
Development

Successfully merging this pull request may close these issues.

7 participants