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 JVM config shortcuts #63

Merged
merged 9 commits into from
Nov 28, 2023
Merged

Add JVM config shortcuts #63

merged 9 commits into from
Nov 28, 2023

Conversation

ctrueden
Copy link
Member

This patch series adds functions to scyjava.config making it easier to configure commonly desired adjustments to the JVM, including initial and maximum heap sizes, headless mode, and JDWP-based remote debugging. It also adds API for querying the JVM's memory state and available processors, as well as a shortcut to Java's System.gc() function. Finally, it adds tests for the memory and headless settings, as well as improving the integration tests in general.

@ctrueden
Copy link
Member Author

@elevans I got a little carried away after we discussed these sorts of convenience functions this morning. 😅 I hope you like it!

@ctrueden
Copy link
Member Author

Ahh, it looks like we would need to run inside xvfb to truly test the not-headless-by-default assumption. I'll add a workaround for now.

Copy link

codecov bot commented Nov 21, 2023

Codecov Report

Attention: 32 lines in your changes are missing coverage. Please review.

Comparison is base (ac62992) 54.47% compared to head (2bf3319) 52.29%.

Files Patch % Lines
src/scyjava/_java.py 0.00% 17 Missing ⚠️
src/scyjava/config.py 0.00% 14 Missing ⚠️
src/scyjava/_versions.py 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #63      +/-   ##
==========================================
- Coverage   54.47%   52.29%   -2.18%     
==========================================
  Files          11       10       -1     
  Lines        1208     1218      +10     
==========================================
- Hits          658      637      -21     
- Misses        550      581      +31     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

I want to use the f"{foo=}" f-string feature.
It needs to run in an isolated Python+Java process so that
we can truly validate the before-and-after of version detection.
Otherwise, the test simply checks the same thing twice, which is
version reporting *after* the JVM is initialized.
Including:

1. heap allocation and limits
2. headless mode
3. interprocess debugging with JDWP
After the JVM has started, it is nice to be able to easily check
characteristics of the JVM from the Runtime API, including current and
maximum heap sizes, as well as number of available processors. (It is
also nice to be able to easily split infinitives in a commit message.)

Relatedly, it can be nice to be able to call the garbage collector
directly.

Note that we do not expose the Runtime.getRuntime().freeMemory() method,
because it can be confusing: one would naively expect freeMemory() to
equal maxMemory() - usedMemory(), but in actuality it represents the
amount of free memory within the bounds of the memory already reserved
by the JVM *at the moment*. So it is typically not a very useful number.
There are two classes of functions:

1. scyjava.config functions to instruct Java how much memory to use; and
2. scyjava._java functions to report back how much it is actually using.

This integration test uses both to validate that it all works properly.
@ctrueden
Copy link
Member Author

OK, this codecov check is annoying, so I'm going to quit working on this. I added integration tests exercising most of the functionality, but our codecov configuration does not appreciate it.

Copy link
Member

@elevans elevans left a comment

Choose a reason for hiding this comment

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

@ctrueden Great additions to scyjava! I tried out all the new methods without issue and your docstrings are clear and comprehensive. I especially appreciate the remote debugging!

Comment on lines +169 to +183
def set_heap_min(mb: int = None, gb: int = None):
"""
Set the initial amount of memory to allocate to the Java heap.

Either mb or gb, but not both, must be given.

Shortcut for passing -Xms###m or -Xms###g to Java.

:param mb:
The ### of megabytes of memory Java should start with.
:param gb:
The ### of gigabytes of memory Java should start with.
:raise ValueError: If exactly one of mb or gb is not given.
"""
add_option(f"-Xms{_mem_value(mb, gb)}")
Copy link
Member

Choose a reason for hiding this comment

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

This is great! ty for adding in an option for GB 👍

@ctrueden ctrueden merged commit f57d61f into main Nov 28, 2023
8 of 13 checks passed
@ctrueden ctrueden deleted the jvm-config-shortcuts branch November 28, 2023 22:26
ctrueden added a commit that referenced this pull request Dec 6, 2023
New API was added in #63.
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.

2 participants