oshi / oshi-ffm

Operating System and Hardware Information using Java's Foreign Function and Memory API
Apache License 2.0
29 stars 7 forks source link

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

Open dbwiddis opened 1 year ago

dbwiddis commented 1 year ago

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:

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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.