3

We want to prevent infinite loops in java plugins as well as put limits on number of statements executed. We have looked at many Java profiling tools and are not seeing any clear way to track this as the Java class is running as opposed to just getting some stats after it completes.

I should add that we are aware of jProfiler and this is not an option because we need to track potentially thousands of separate classes with minimal overhead and we need to do this all in code so we can kill a particular executing class should it exceed preset limits.

Thanks!

Maitri
  • 51
  • 1
  • It's not at all clear what you mean. Are you capping the number of live threads at once? The number of threads active at any given time? – Louis Wasserman Jun 22 '12 at 17:31

4 Answers4

3

Profilers like Yourkit and JProfiler won't help - and actually nothing will. Just like you cannot limit the amount of memory a particular Java class uses, you cannot limit the CPU. Basically all classes share the same CPU/scheduler and memory space.

You can run your plugin class in a separate, low-priority thread and wait given amount of time for the plugin to complete. Unfortunately if it does not complete (e.g. due to infinite loop), there's nothing you can do about it. Interrupting a thread must be honoured by client code and stopping it is deprecated.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • 1
    know that the `wait time` isn't going to be `wall time` it will be `wall time + N`, where `N` is non-deterministic; so you can't put a hard cap on execution time in time slices. Statements executed is the only way to put a hard cap on something like this, and as everyone has stated that isn't possible with the current JVM implementations. –  Jun 22 '12 at 17:52
  • @JarrodRoberson, +1. Anything can affect real execution time, including other threads, OS, etc. – Tomasz Nurkiewicz Jun 22 '12 at 17:56
  • Tomasz there are ways to limit consumption indirectly via call rate limiting, supervisors,... – William Louth Jun 26 '12 at 09:25
1

Short Answer

No, not with any of the existing JVM implementations.

Long Answer

Theoretically Yes, if you are willing to hack extensively at the source code for the JVM. This would be a drastic thing to do if possible.

Side Note:

Erlang's runtime does just what you are asking for in the way it manages its light weight processes. They get N number of statements executed per thread before they get switched off and something else runs. That is how the Erlang VM supports 10s of thousands of processes on moderate hardware.

0

There are few WCET analysis tools, but they assuming code is specifically annotated. See, for example Volta tools.

If you need to detect such issues in run-time, the only way is to wrap your plugin execution into a separate thread and handle execution time. Here is an example.

Community
  • 1
  • 1
Eugene Kuleshov
  • 31,461
  • 5
  • 66
  • 67
-1

You may find this helpful - http://docs.oracle.com/javase/1.5.0/docs/guide/jvmti/jvmti.html.

The JVMTI is an api that helps tools to profile JVM at runtime. You need to write code but this gives a lot of freedom as to what you want to measure and monitor.

Bhaskar
  • 7,443
  • 5
  • 39
  • 51