Skip to content

Improve performance using parallel streams for processes and threads#2204

Merged
dbwiddis merged 12 commits into
oshi:masterfrom
adrian-kong:adrian/parallel
Oct 2, 2022
Merged

Improve performance using parallel streams for processes and threads#2204
dbwiddis merged 12 commits into
oshi:masterfrom
adrian-kong:adrian/parallel

Conversation

@adrian-kong

@adrian-kong adrian-kong commented Oct 1, 2022

Copy link
Copy Markdown
Collaborator

Not sure if this was done,

Adds parallel on every stream call
Fixes #2196

@dbwiddis I'm not actually sure when parallel should be used according to
https://www.baeldung.com/java-when-to-use-parallel-stream
so I have never actually used this method, is it necessary to use .parallel on every stream call?

@adrian-kong adrian-kong requested a review from dbwiddis October 1, 2022 14:52
@sonatype-lift

sonatype-lift Bot commented Oct 1, 2022

Copy link
Copy Markdown
Contributor

⚠️ 25 God Classes were detected by Lift in this project. Visit the Lift web console for more details.

@dbwiddis dbwiddis left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not sure if this was done,

Adds parallel on every stream call #2196

@dbwiddis I'm not actually sure when parallel should be used according to https://www.baeldung.com/java-when-to-use-parallel-stream so I have never actually used this method, is it necessary to use .parallel on every stream call?

No, it's not necessary, or even desired to use it on every stream. From a performance perspective (the main reason for #2196) we want to balance the additional overhead of spawning/managing the streams, vs. the faster time from the parallel processing. And we don't want to use it in any place that order maters.

I've looked at every change and the vast majority aren't needed. Some are good. But you missed the primary reason for #2196 by only looking at existing streams.

For the below:

  • keep the one for network local interface check
  • (probably) keep the one for thread details, but there should probably also be ones for the other OS implementations

For things not included:

  • please focus on methods that instantiate (a lot of) new OSProcess or new OSThread objects. Look at every getProcess() implementation on every OS. Some of them are in lists which could be turned into streams and parallelized.
    • also look into the getThreadDetails() implementations for the same reason. You found one... look at the others.

Comment thread oshi-core/src/main/java/oshi/driver/mac/ThreadInfo.java Outdated
Comment thread oshi-core/src/main/java/oshi/driver/unix/freebsd/disk/GeomPartList.java Outdated
Comment thread oshi-core/src/main/java/oshi/driver/windows/DeviceTree.java Outdated
Comment thread oshi-core/src/main/java/oshi/driver/windows/wmi/Win32Process.java Outdated
Comment thread oshi-core/src/main/java/oshi/driver/windows/wmi/Win32Process.java Outdated
Comment thread oshi-core/src/main/java/oshi/software/os/unix/openbsd/OpenBsdOperatingSystem.java Outdated
Comment thread oshi-core/src/main/java/oshi/software/os/unix/openbsd/OpenBsdOperatingSystem.java Outdated
Comment thread oshi-core/src/main/java/oshi/software/os/unix/solaris/SolarisOperatingSystem.java Outdated
Comment thread oshi-core/src/main/java/oshi/software/os/unix/solaris/SolarisOperatingSystem.java Outdated
Comment thread oshi-core/src/main/java/oshi/software/os/windows/WindowsFileSystem.java Outdated
@adrian-kong adrian-kong reopened this Oct 2, 2022
@adrian-kong adrian-kong requested a review from dbwiddis October 2, 2022 02:35

@dbwiddis dbwiddis left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Great job, I love the improved readability and the performance will be good too.

Will approve with removal of star imports, but consider making the repeated valid thread check a constnat.

Comment thread oshi-core/src/main/java/oshi/software/os/unix/aix/AixOSProcess.java Outdated
Comment thread oshi-core/src/main/java/oshi/software/os/unix/aix/AixOSProcess.java Outdated
Comment thread oshi-core/src/main/java/oshi/software/os/unix/freebsd/FreeBsdOSProcess.java Outdated
Comment thread oshi-core/src/main/java/oshi/software/os/unix/solaris/SolarisOSProcess.java Outdated
@dbwiddis

dbwiddis commented Oct 2, 2022

Copy link
Copy Markdown
Member

failing license check CI is due to copyright change date. Run mvn license:format to fix.

@adrian-kong adrian-kong requested a review from dbwiddis October 2, 2022 04:10

@dbwiddis dbwiddis left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks really good, thanks for the updates! I've made a few more suggestions if you're willing to iterate one last time, otherwise this is probably GTG.

Comment thread oshi-core/src/main/java/oshi/software/os/linux/LinuxOSProcess.java Outdated
Comment thread oshi-core/src/main/java/oshi/software/os/OSThread.java Outdated
: getAllNetworkInterfaces().stream().filter(networkInterface1 -> !isLocalInterface(networkInterface1))
.collect(Collectors.toList());
: getAllNetworkInterfaces().stream().parallel()
.filter(networkInterface1 -> !isLocalInterface(networkInterface1)).collect(Collectors.toList());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Huh, that networkInterface1 snuck in at some point, probably due to a refactoring. I like shorter names in lambdas, though, why not ni?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

hmmm how about just .filter(Predicate.not(AbstractNetworkIF::isLocalInterface))

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

nevermind it seems has compilation errors

Error:  /home/runner/work/oshi/oshi/oshi-core/src/main/java/oshi/hardware/common/AbstractNetworkIF.java:[159,42] cannot find symbol

Comment thread oshi-core/src/main/java/oshi/software/os/unix/freebsd/FreeBsdOSProcess.java Outdated
}
}
return threads;
Predicate<Map<PsThreadColumns, String>> columnPRI = threadMap -> threadMap.containsKey(PsThreadColumns.PRI);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

(Preference) let's name this for the reason it exists, e.g., hasAllPSColumns

@adrian-kong adrian-kong Oct 2, 2022

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

would hasColumnsPri suffice? im not sure what AllPS means i.e. the other ones would be hasKeywordsArgs etc...
not actually sure what PRI means

Comment thread oshi-core/src/main/java/oshi/software/os/unix/freebsd/FreeBsdOperatingSystem.java Outdated
Comment thread oshi-core/src/main/java/oshi/software/os/unix/openbsd/OpenBsdOSProcess.java Outdated
Comment thread oshi-core/src/main/java/oshi/software/os/windows/WindowsOperatingSystem.java Outdated
Comment thread oshi-core/src/main/java/oshi/software/os/windows/WindowsOperatingSystem.java Outdated
@dbwiddis dbwiddis changed the title Added .parallel() Improve performance using parallel streams for processes and threads Oct 2, 2022
@dbwiddis

dbwiddis commented Oct 2, 2022

Copy link
Copy Markdown
Member

Oh, and also a changelog entry would be nice!

@dbwiddis dbwiddis added hacktoberfest Issues we're happy for new #Hacktoberfest2020 participants to do hacktoberfest-accepted Accepted during Hacktoberfest labels Oct 2, 2022
@dbwiddis

dbwiddis commented Oct 2, 2022

Copy link
Copy Markdown
Member

Spotless should help with the unused imports :)

@adrian-kong

adrian-kong commented Oct 2, 2022

Copy link
Copy Markdown
Collaborator Author

oops
EDIT: spotless didnt catch the unused import 😢

@dbwiddis

dbwiddis commented Oct 2, 2022

Copy link
Copy Markdown
Member

oops EDIT: spotless didnt catch the unused import 😢

Sounds like a PR to fix spotless config is in order ;)

@dbwiddis dbwiddis left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. Let me know if you're ready to merge this or still working.

@adrian-kong

Copy link
Copy Markdown
Collaborator Author

LGTM. Let me know if you're ready to merge this or still working.

yep its all good to go!

@dbwiddis dbwiddis merged commit 5bbe49b into oshi:master Oct 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hacktoberfest Issues we're happy for new #Hacktoberfest2020 participants to do hacktoberfest-accepted Accepted during Hacktoberfest

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve processing speed with parallel streams

2 participants