Java production system, the most commonly used is Linux, so when solving the production system problems, understand the Linux system, familiar with common commands, to solve problems, even from a higher level to understand Java, is very helpful.
So, how should we learn about Linux? In Friday listened to a master lectures, for learning series, cohesion, advanced, are very helpful, so share here.
This lecture, the first to go through the Linux command, so that it can assist our work. But not only to speak a few orders, but to look at some of the operating system level of things, to achieve understanding and the elevation of the realm of thought.
Understanding is the soul, Chuanjiang is the skeleton, then can self-fullness flesh and blood, this is the purpose.
This lecture, particularly suitable for "have a certain understanding, but not deep, or precisely in the advanced edge of" The situation of people, a talk, a string, clairvoyant, understanding will immediately deep layer.
1, Strace
Strace is commonly used to track system calls and received signals when a process executes.
Strace-t-t-f java Test >out 2>&1
Strace-t-t-f P PID
With this tool, we can see that when a Java program, or any program running on a Linux system, is actually decomposed into a Linux API.
The purpose of this command is to remind us that we can use a tool, such as Strace, to understand the operation of our program in a way.
Language, regardless of the platform to run, and finally to break down into the operating system API, broken down into CPU instructions. This is the advanced thinking, or the daily application system development, forget the nature of the past.
2. Process
Pstree is the process system of Linux.
PS is the process facet of a system at some point, representing how many processes are running at some point.
Because under Linux, including processes, sockets, etc., are in the form of files, in memory. We can find out the process ID through PS and then, through the runtime file system, view any information about the process.
The process information (path, jar, connection, etc.) is fully accurate from the operating system level, and is more accurate than the query from the configuration file, and does not require anyone to ask.
Ps-ef | grep Catalina
Cd/proc/60282/fd
Further information can be viewed, such as:
A, write how many logs: LL | grep log
B, start to those Jar:ll | grep jar
C. See those flashes? Those are sockets that represent who even me: ll | grep socket
D. For these socket handles, we can further see what the process is: lsof | grep 387084
3, Memory +cpu
Top, this is the Linux command that should be very familiar, is the common performance analysis tool under Linux, can show the resource occupancy of each process in real-time, it shows the same content as the task Manager of Windows.
Free to view memory usage.
We can also use JVISUALVM in Windows to graphically view the memory of Java applications, threads, or dump threads, for example, the test application runs as follows:
Java-dcom.sun.management.jmxremote.port=9998-dcom.sun.management.jmxremote.ssl=false- dcom.sun.management.jmxremote.authenticate=false-djava.rmi.server.hostname=192.168.80.128 Test
You can monitor the process locally through JVISUALVM, enter JVISUALVM at the command line, remote, connect IP address, then right-click this remote connection, new JMX connection, input port, you can monitor this remote Java process, as follows:
4. Network
Netstat-anop | The grep LIST can be viewed and all the ports are started, such as:
The sixth column "2489/mongod", process number 2489, is the mongod process, which listens on port 27017 for MONGO access.
Netstat-anop | grep EST can be viewed and the ports are connected by WHO, for example:
The 4th column, which represents its own IP and port, the 5th column, the IP and port of the peer, and the 6th column, which represents the process number and the application that initiates the process.
Other commonly used:
Strace-t-t-e Network,poll Curl www.baidu.com We can see the routing path from our machine to the Baidu server
Cat/etc/resolv.conf can see the order of the domain name resolution server
Cat/etc/hosts the static table lookup for host name (host name query static tables)
5. Threads
Top-p 59851 Shift+h, see how many threads there are in the 59851 process.
The first column of PID, thread ID, converted into 16, is the line stacks nid.
We can use Jstack-l 59851 to view the entire process information, or you can use Jstack-l 59851 | grep NID, looking at only one line of stacks information.
Many times, when looking at a problem, you need to find the thread that consumes the most resources and then view the thread stack information for that thread.
These steps, easy to operate, in fact, the most critical is that the line stacks dump down, to understand.
6. GC
Linux the threads in the next process are all shared memory, so when analyzing the memory, it depends on the process.
Jstat-gccause 59851-Jstat, the JVM Statistics monitoring Tool, collects operational data for all aspects of the hotspot virtual machine
We are not afraid of ygc,ygc very often, very quickly, the system time is very short, subtle level, for the user is not feeling;
But FGC to note, FGC, the JVM is suspended animation, only FULLGC, do not handle other requests, it may last a few seconds, FGC too much, the YGC can not be recycled, too many objects into the old age, quickly occupied, the system must have problems.
Java-xx:+printflagsinitial, you can see the configuration of the JVM parameters, for example, by default, we can see that Survior:eden = 1:8,new:old = 2:1.
Jmap-histo 59851 | More memories Map for Java, generating a memory dump snapshot of a virtual machine (heapdump file)
By Jmap, you can identify objects in your system that may have security implications (such as too many to recycle).
Through these means, without the analysis of code, without downtime, you can identify problems and solve problems.
JPS, Jstat, Jstack, Jmap, these are not Linux commands, but the JDK-written Java APP for auxiliary diagnostics in Rt.jar.
GC is the basic skill of Java programmers and should be understood very clearly.
7, Dstat and Iftop
Scenario 1: Demo uploading files via SFTP
You can see that sshd and SFTP are CPU intensive in top because SFTP is based on sshd, and you can see in Dstat that the receive traffic is very large.
Scenario 2: Demo HTTP request Baidu home page, through SS5 agent pressure measurement, in the SS5 machine, dstat display send and recv almost the same.
And we generally understand that there should be very little data emitted, and a lot of the received data are inconsistent. The reasons are:
This understanding is not right, as the SS5 agent of the machine, both from the remote received a large amount of data, while the received large amounts of data forwarded to the machine it proxies, so there is this network phenomenon.
At this time, we can clearly see the IP-to-IP send-in packets through iftop.
So, knowing that, with tools, Linux-based knowledge, Java-based knowledge, locating problems, and identifying problems, is critical.
Iftop is a third-party, real-time traffic monitoring tool:
Mainly used to display the local network traffic situation and the various machines to communicate with each other traffic sets, such as the amount of traffic between the individual machines, is very suitable for proxy server and iptables server use.
Official website: http://www.ex-parrot.com/~pdw/iftop/.
Installation steps:
Yum-y Install Flex BYACC libpcap ncurses ncurses-devel libpcap-devel
wget http://soft.kwx.gd/tools/iftop-0.17.tar.gz #获得软件包
Tar zxvf iftop-0.17.tar.gz #解压
CD iftop-0.17 #进入目录
./configure #使用默认配置
Make && make install #编译并安装
If the LIBPCAP is not packaged, you will need to download the RPM to http://pkgs.org/to install it yourself.
Dstat is Linux comes with, can install Yum.
Dstat
Iftop:
8, "|" Pipeline. Connect, filter, this is the role of the pipeline, like connecting the pipe of thousands of households
The pipe operator, by row to the data, one line, the output of the last command, into the input of the next command.
Frequently used parameter-C, similar to group by in SQL.
In Linux, to log files, as a database table, log files are also rows and columns. Log to standard, to have regular, it will be very convenient to split the search.
We can easily analyze logs with commands such as less, more, cat, sort, uniq, grep, awk, and more.
In addition, awk is a powerful text analysis tool, and awk is powerful when it comes to analyzing data and generating reports. To put it simply, awk reads the file line-by-row, using spaces as the default delimiter to slice each row, and then perform various analytical processing of the cut.
9. Other common commands
DF-H display of disk partition usage
Du-sh/root/temp show the size of a folder
Find/root/temp-type f-name "*.txt" | Xargs grep "Main" in the/root/temp directory, in the normal file, in the file with the extension txt, find the line containing the main string
Lsof-l Show All socket handles
SED file content lookup and replacement
Vim text editor, often familiar. Start by recording the most commonly used, for example, Flip screen: Ctrl+u (D), 2gg,3dd,3dw,yy,/Find,? find.
10, learn to analyze the wrong ideas
Learn to reproduce the false simulations, which can be solved by simulating the recurrence. Simulation means: You can develop some poor quality code to make mistakes.
Tools just help us locate the problem, help us to record the operating system aspect of the problem, to understand the meaning of this facet, the implied exception, but also need a deep understanding of the relevant basic knowledge. Like what:
- Using Jstat to see the GC requires a deep understanding of Java's memory management and garbage collection techniques.
- Using Netstat to look at the network, need to network knowledge, especially TCP, have in-depth understanding;
- With Jstack dump line stacks, you need to have a deep understanding of Java threading Knowledge.
Linux&java Practical Series-Advanced Guide