====== Profiling in Java ====== ===== Task assignment ===== Your task is to speed up [[https://en.wikipedia.org/wiki/Genetic_algorithm|Genetic algorithm]] (GA) solving [[https://en.wikipedia.org/wiki/Travelling_salesman_problem|Travelling Salesman Problem]] (TSP). Brief recap of TSP and GA can be seen in {{ :courses:b4m36esw:labs:tsp.pdf |PDF}} You probably need to do the following steps to achieve the desired speedup: - Download program from git repository: git clone https://gitlab.fel.cvut.cz/cuchymar/tsp.git - Open the source code in IDE - Run the program (cz.esw.profiling.Main class) - Do profiling (hints below) - Make changes which will improve the speed (code modifications) - Upload patch file into upload system and pass specified limit: Patch will be applied using git apply command, therefore, best way to generate patch is the git diff command:git fetch origin && git diff origin/master > tsp.diff.txt(txt file type because of upload system). You are not allowed to modify the parameters of the GA and to parallelize the algorithm! The possible speed up (with the preset parameters) is great (approx. from 120s to 4s). ===== Profiling tools ===== Oracle JDK 8 itself provides a number of monitoring tools located in 'JAVA_HOME/bin' folder. In later releases, some tools are not included and have to be installed individually. ==== Java VisualVM ==== The Java VisualVM is a baseline for profiling Java applications. It allows you to use both sampling and instrumentation approaches to profile the application. It allows to measure time spent in functions and see statistics about objects on the heap. It can also generate and analyse heap dumps, monitor garbage collector (and invoke it) and monitor threads. https://visualvm.github.io/documentation.html http://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/tooldescr010.html ==== Java Mission Control with Java Flight Recorder ==== Although, JMC is now open source, we recommend you to use Oracle JDK 8 with bundled JMC. To use it on machines in the lab, change project JDK to ''/opt/jdk1.8.0_162'' Unlike the rest, Java Mission Control is a commercial feature (free for development) of Oracle JDK. It can be used for monitoring provided also by VisualVM but its main feature is the Java Flight Recorder (JFR). The JFR can record detailed information about various aspects of the application - e.g. object statistics, compilation, detailed GC info, I/O operations, exceptions,etc. Flight Recording can be started automatically with VM arguments: -XX:+UnlockCommercialFeatures -XX:+FlightRecorder -XX:+UnlockDiagnosticVMOptions -XX:+DebugNonSafepoints -XX:StartFlightRecording=duration=10s,filename=myrecording.jfr https://docs.oracle.com/javacomponents/index.html http://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/tooldescr003.html https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/tooldescr004.html http://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/tooldescr005.html ==== JConsole ==== JConsole is a tool with a basic monitoring functionality - heap size, CPU usage, thread monitor, etc. But it also allows using [[https://en.wikipedia.org/wiki/Java_Management_Extensions|JMX]] instrumentation in order to see JVM parameters and in some cases it also allows you to change the parameters. http://docs.oracle.com/javase/8/docs/technotes/guides/management/jconsole.html ===== ===== There are also some command line [[http://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/index.html|tools]] (''jcmd, jhat, jinfo,...'') but most of their functionality is covered by the tools described above. ==== Tips ==== Try to find the code that is executed a lot and find out if the results cannot be precomputed and reused. Avoid unnecessary object creation e.g. boxing and unboxing of primitive variables (int vs. Integer).