Java System Tools JPS principle analysis

Source: Internet
Author: User
Tags gtk

Introduction to Java System Tools JPS principle analysis

When we need to get the currently running Java process, we can filter by the operating system's own tools, such as PS and Netstat. However, Java also provides a common tool to implement this functionality, and can provide more detailed information. JPS is the abbreviation for Java Virtual machine process Status too, which can be used to obtain Java processes in the current user system.

Use

The command format for JPS is as jps [ options ] [ hostid ] follows:

usagejps[-help]       jps[-q][-mlvV][Definitions:    <hostid>:      <hostname>[:<port>]

The parameters are explained as follows (where MLVV can be used in combination):

    • Q: Displays only the Java process's lvmid, does not display the class name, the jar file name, and the parameters passed to the main method, etc.
    • M: output passed to the main method parameter
    • L: The full package name of the output application entry function or the full path name of the application's jar file
    • V: Output parameters passed to the JVM
    • V: Outputs parameters passed to the JVM (i.e.. hotspotrc files or files determined by the parameter-xx:flags=) through the identity file
    • HostID: The system that needs to view the Java process, determined by the hostname and port number

Note: Viewing the local Java process does not require the HostID parameter, and if you want to view the remote process, you need to provide the JSTATD service and RMI bindings on the remote host, which is described later JSTATD

The output of the JPS is in the form of a lvmid [ [ classname | JARfilename | "Unknown"] [ arg* ] [ jvmarg* ] ] test example in Ubuntu:

[Email protected]:~$ JPS3540Org. Eclipse. Equinox. Launcher_1. 3. 0. V20140415-.Jar3669Main4535Jps[email protected]:~$ jps-m4548Jps-m3540Org. Eclipse. Equinox. Launcher_1. 3. 0. V20140415-.Jar-os Linux-ws Gtk-arch x86_64-showsplash/usr/share/eclipse//plugins/org. Eclipse. Platform_4. 4. 2. V20150204-1700/splash. bmp-launcher/usr/share/eclipse/eclipse-name Eclipse--launcher. Library/usr/share/eclipse//plugins/org. Eclipse. Equinox. Launcher. GTK. Linux. x86_64_1. 1.. V20150204-1316/eclipse_1607. so-startup/usr/share/eclipse//plugins/org. Eclipse. Equinox. Launcher_1. 3. 0. V20140415-.Jar--launcher. Appendvmargs-exitdata220011-product org. Eclipse. Epp. Package. Java. Product-vm/usr/bin/java-vmargs-dosgi. Requiredjavaversion=1.6-xx:maxpermsize= them-xms40m-xmx512m-jar/usr/share/eclipse//plugins/org. Eclipse. Equinox. Launcher_1. 3. 0. V20140415-.Jar3669Main[email protected]:~$ Jps-l4738Sun. Tools. JPS. Jps3540/usr/share/eclipse//plugins/org. Eclipse. Equinox. Launcher_1. 3. 0. V20140415-.Jar3669Local. Main[Email protected]:~$ jps-v3540Org. Eclipse. Equinox. Launcher_1. 3. 0. V20140415-.Jar-dosgi. Requiredjavaversion=1.6-xx:maxpermsize= them-xms40m-xmx512m3669Main-dfile. Encoding=utf-84907Jps-dapplication. Home=/usr/lib/jvm/jdk1. 8. 0_45-xms8m
Principle

The following analysis of how JPS implementation, see the code from OPENJDK7. We see from the entrance class Jps.java, in this class is a main method, the core code as shown, first get the running Java process from a specific host, and then output the process, if there are parameters, additional output parameters need to output the information. There are two main ways to get a Java process from a host, one is local and the other is remotely invoked via RMI.

Hostidentifier hostId =arguments. HostId (); Monitoredhost monitoredhost = Monitoredhost.getmonitoredhost (hostId);//Get Java process from a specific hostSet JVMs = Monitoredhost.activevms (); for(Iterator j = jvms.iterator (); J.hasnext ();/ * Empty * /) {StringBuilder output =NewStringBuilder (); Throwable LastError =NULL;                int lvmid = ((Integer) J.next ()). Intvalue (); Output.append (String. ValueOf (Lvmid));if(arguments. Isquiet ()) {System.out.println (output);Continue; } MONITOREDVM VM =NULL;StringVmidstring ="//"+ Lvmid +"? Mode=r";StringErrorString =NULL;//Processing other parameters}

Let's take a look at the code that gets the Java process locally, and the Monitoredhost.activevms () method is finally implemented by Activevms () in Localvmmanager. By looking at the following code, it is easy to see that the elements in the last returned Lvmid collection Jvmset are added in a similar PerfDataFile.getLocalVmId(files[j]) way, and that the files folders are derived from functions in class Perfdatafile PerfDataFile.getTempDirectory(userName) . Careful classmates should find that the structure of the folder will change depending on the user name, and the code in the Perfdatafile can find that the file name under the folder also has two different standards. Next we continue to look at the class Perfdatafile.

 Public synchronizedSet<integer>Activevms() {/* * This method was synchronized because the Matcher object used by * FileFilter are not safe for concu Rrent use, and this method are * called by multiple threads.         Before this method is synchronized, * we ' d see Strange file names being matched by the matcher. */Set<integer> Jvmset =NewHashset<integer> ();if(! tmpdir.isdirectory ()) {returnJvmset; }if(UserName = =NULL) {/* Get a list of all of the user temporary directories and * Iterate over the list to find a             NY files within those directories. */file[] dirs = Tmpdir.listfiles (Userfilter); for(inti =0; i < dirs.length; i + +) {if(!dirs[i].isdirectory ()) {Continue; }//Get a list of files from the directoryfile[] files = dirs[i].listfiles (FileFilter);if(Files! =NULL) { for(intj =0; J < Files.length; J + +) {if(Files[j].isfile () && files[j].canread ()) {Jvmset.add (NewInteger (Perfdatafile.getlocalvmid (FILES[J))); }                    }                }            }        }Else{/ * * Check If the user directory can be accessed. Any of these * conditions is asynchronously changed between subsequent * calls to this Metho d. * /            //Get the list of files from the specified user directoryfile[] files = tmpdir.listfiles (FileFilter);if(Files! =NULL) { for(intj =0; J < Files.length; J + +) {if(Files[j].isfile () && files[j].canread ()) {Jvmset.add (NewInteger (Perfdatafile.getlocalvmid (FILES[J))); }                }            }        }// look for any 1.4.1 filesfile[] files = tmpdir.listfiles (Tmpfilefilter);if(Files! =NULL) { for(intj =0; J < Files.length; J + +) {if(Files[j].isfile () && files[j].canread ()) {Jvmset.add (NewInteger (Perfdatafile.getlocalvmid (FILES[J))); }            }        }returnJvmset; }}

Class Perfdatafile provides a way for the Java process to convert files to and from one another. From this class can find the corresponding location of these files under each system, as shown in method Gettempdirectory, the path to the temporary directory is as follows: tmpDirName + dirNamePrefix + user + File.separator , where Tmpdirname is a temporary file for Java IO, dirnameprefix fixed to " Hsperfdata_ ", user is the system user name and File.separator is the file delimiter. Under Windows system, the default path is C:\Users\username\AppData\Local\Temp\hsperfdata_username , and under Linux, the default path is /tmp/hsperfdata_username . Files under these folders are purged at the end of the corresponding Java process. Method Getlocalvmid is used to get lvmid from the process file, it can be seen that 1.4.2 and later hotspot virtual machines are directly named Lvmid, with delimiters in previous versions.

Note: Temporary files in Windows may be hidden files

     Public StaticStringgettempdirectory(String user) {returnTmpdirname + dirnameprefix + user + File.separator; } Public Static int Getlocalvmid(File file) {intLvmid =0;Try{//Try 1.4.2 and later format first            returnInteger.parseint (File.getname ()); }Catch(NumberFormatException e) { }//Now try the 1.4.1 formatString name = File.getname ();if(Name.startswith (Dirnameprefix)) {intFirst = Name.indexof (' _ ');intLast = Name.lastindexof (' _ ');Try{if(first = = last) {returnInteger.parseint (name.substring (First +1)); }Else{returnInteger.parseint (name.substring (First +1, last)); }            }Catch(NumberFormatException e) { }        }Throw NewIllegalArgumentException ("file name does not match pattern"); }
Summarize

This article mainly explains the use of JPS and its realization principle is what. Here are some further questions to consider:

    1. When did the files for these processes be written to the corresponding folder?
    2. How does the JPS parameter parse the contents of a file?

Interested students can continue to read the source to learn more deeply.

    • This article by Drfish (http://www.drfish.me/) Original, reproduced please indicate the original link, thank you.

Java System Tools JPS principle analysis

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.