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

Port an OSHI JNA-based method to OSHI-FFM #4

Open
dbwiddis opened this issue Sep 21, 2022 · 3 comments
Open

Port an OSHI JNA-based method to OSHI-FFM #4

dbwiddis opened this issue Sep 21, 2022 · 3 comments
Labels
hacktoberfest Get credit for PRs during Hacktoberfest!

Comments

@dbwiddis
Copy link
Member

dbwiddis commented Sep 21, 2022

There are hundreds of methods in OSHI leveraging JNA. Port one of them!

It's easier than you might think!

You will need to be using JDK 19. Because the FFM API is a preview API, you must compile and run the code with preview features enabled, i.e., javac --release 19 --enable-preview ... and java --enable-preview. Most IDEs will enable you to add these arguments. (In Eclipse I clicked a checkbox to enable preview features and added --enable-native-access=ooo.oshi to my VM arguments in run configurations... done!)

If you start a tutorial that has you use jlink, stop. It won't be helpful for you here. You need to use the basic method invoking and memory functions described in JEP 424. These largely have a direct correspondence to the existing JNA implementations. Some code is already committed for macOS process listings that gives a good feel for all the possibilities.

For methods and structures, check out the SystemLibrary class for macOS and usages in SysctlUtil, MacOperatingSystem and MacOSProcess. There should be enough examples there to hint you toward modeling new data structures. In summary:

  • For methods, copy the corresponding JNA interface method, and wrap the call to the MethodHandle in a try/catch. Then make a corresponding private method implementing the mapping. You'll need to tweak non-primitive arguments.
  • For structures, the GroupLayout examples at the bottom of the SystemLibrary class, and the access in the MacOSProcess class are good examples.

There's probably going to be a need for a generic IntByReference and LongByReference equivalent at some point...

Priority:

  1. Anything you can contribute. If it's your first time doing a FFM function, pick an easy one. Any help is appreciated. Truly. Don't know where to put it in the API? Don't worry! Just submit a standalone class with the implementation and we'll move it to where it's needed!

  2. Util classes covering multiple fetches (performance counters, sysctl, WMI).

  3. Hot Path: Process listing (macOS is done, other OS's need it) or CPU ticks listing

@HannesWell
Copy link

Would it be interesting for OSHI to use JExtract, which can mechanically generate the Java code that can call C functions from a given header-file using the FFM-API? https://github.com/openjdk/jextract

IIRC there is even a Maven-Plugin to generate the Java sources or binaries in a Maven build, but when I last saw it was still under development.

@surajs1n
Copy link

surajs1n commented May 6, 2023

This is a great initiative @dbwiddis. I have used the OSHI library to build one of my own projects. I am also planning to contribute to this FFM library.

Could you please quickly help me know if there are any new features already built or are work-in-progress? So that, I can focus on other unfinished tasks. Small disclaimer, I am new to FFM. Thus, I may take some time to gear up to my coding pace. 🙏

@dbwiddis
Copy link
Member Author

dbwiddis commented May 6, 2023

Hey @surajs1n thanks for stepping up!

I don't think there are any works in progress... feel free to step up wherever you'd like. My initial goal is to get most-used features like CPU usage and memory usage ported over, and I did a bit of the macOS work to support that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hacktoberfest Get credit for PRs during Hacktoberfest!
Projects
None yet
Development

No branches or pull requests

3 participants