Automatically detect errors in parallel Java programs

Source: Internet
Author: User
Document options

Print this page

Sample Code

Level: Intermediate

Qi Yao (qiyaoj@cn.ibm.com), software engineer, IBM China Software Development Center
Gan Zhi (ganzhi@cn.ibm.com), Senior Software Engineer, IBM China Software Development Center
Luo Zhida (luozd@cn.ibm.com), software engineer, IBM China Software Development Center
Dai Xiaojun (daixiaoj@cn.ibm.com), software engineer, IBM China Software Development Center

October 17, 2008

The uncertainty of parallel programs makes it difficult to find, reproduce, and debug parallel program errors. Mtrat can collect program runtime information and analyze all possible parallel program errors in the program online.

When the CPU enters the multi-core era, parallel programming will become more popular, but writing parallel programs is more prone to errors. During the development process, engineers can notice that the same program runs correctly in a single thread, but it may cause errors when multithreading occurs. The causes of parallel errors are usually very obscure, and they appear randomly in a test. Since multiple threads in a program may be staggered in any way, even if a parallel program runs hundreds or thousands of times correctly, a new error may occur in the next operation.

Multi-thread run-time analysis tool is a runtime analysis tool developed by IBM for multi-threaded Java programs, it can be used to analyze and find some hidden parallel program errors in Java code, such as data race and deadlock, to improve the Code Quality of parallel programs. This article will introduce a new tool (http://alphaworks.ibm.com/tech/mtrat) for detecting random parallel errors in Java programs, check the potential parallel program errors in Java code, so as to improve the security and stability of the Code, it also demonstrates its ability to explore potential and non-occurring errors.

Overview

The Java programming language provides powerful language support for compiling multi-threaded applications. However, it is still difficult to write useful, error-free multi-threaded programs. Threads in programming languages face many challenges. Among these challenges, the most important thing is the improvement of programming complexity. These programming complexities are caused by the Access to synchronized shared variables, potentially dependent on timing errors and the complexity of debugging and optimization of parallel programs.

Mtrat only integrates different technologies into a single development tool to conceal the complexity of the tool and make mtrat easy to use. Mtrat consists of the following parts,

  • Simple command line interface and Eclipse plug-in. The parallel errors returned by mtrat are output.
  • Dynamic Java bytecode modification engine. You can modify the Java class when a Java class file is loaded by a Java virtual machine.
  • Information Collector when the program is running. Collect dynamic program information, such as memory access, thread synchronization, creation, and termination.
  • Efficient runtime analysis engine. The collected runtime information is analyzed online. If a potential parallel error is found, it is reported to the user through the interface.



Back to Top

Detection data competition

In a parallel program, when two parallel threads access a domain of a shared variable or shared object without any constraints, at least one operation is a write operation, a Data competition error occurs. The most powerful function of mtrat is to discover potential data competition errors in parallel programs. The following Java program hides a potential data competition error.

 package sample;                        class Value                        {                        private int x;                        public Value()                        {                        x = 0;                        }                        public synchronized void add (Value v)                        {                        x = x + v.get();                        }                        public int get() {return x;}                        }                        class Task extends Thread                        {                        Value v1, v2;                        public Task (Value v1, Value v2)                        {                        this.v1 = v1;                        this.v2 = v2;                        }                        public void run() {v1.add(v2);}                        }                        public class DataRace                        {                        public static void main (String[] args) throws InterruptedException                        {                        Value v1 = new Value ();                        Value v2 = new Value ();                        Thread t1 = new Task(v1, v2);                        Thread t2 = new Task (v2, v1);                        t1.start();                        t2.start();                        t1.join();                        t2.join();                        }                        }

ClassValueDeclare an integer domainx, A Synchronization MethodaddModify this domain and a methodgetReturned domainx. ClassTaskTake two classesValue.

Run classes with mtratsample.DataRaceTo check potential data contention errors in the program at runtime.

 $ mtrat -cp . sample.DataRace                        Data Race 1 : 3 : sample/Value : x                        Thread Thread-3 id: 7 : WRITE                        sample.Value : get : 15                        sample.Value : add : 15                        sample.Task : run : 32                        Thread Thread-4 id: 8 : READ                        sample.Value : get : 18                        sample.Value : add : 15                        sample.Task : run : 32                        Data Race 2 : 4 : sample/Value : x                        Thread Thread-3 id: 7 : READ                        sample.Value : get : 15                        sample.Value : add : 15                        sample.Task : run : 32                        Thread Thread-4 id: 8 : WRITE                        sample.Value : get : 15                        sample.Value : add : 15                        sample.Task : run : 32

On the GUIEclipseAnd the following results are obtained:

Figure 1.

Two data contention errors were reported by mtrat because of the classTaskThe two instances inValueBut the shared object is not protected by a common lock.

For example, a thread execution method may exist in parallel program execution at such a time.getRead domainxAnd the other thread executes the execution method.addWrite domainx.

Based on the check results, mtrat finds two data competition errors in the classsample/ValueDomainx. After obtaining these two data competition errors, programmers can easily find that two threads in the program can access the same object domain concurrently. If the two threads can access this object domain sequentially, the competition between the two data can be eliminated.



Back to Top

Deadlock Detection

Deadlock is also a common problem in parallel Java programs. A deadlock occurs in a Java program becausesynchronizedKeyword causes the running thread to wait for the lock associated with an object. Because the thread may have obtained another lock, the two threads may wait for the other thread to release the lock. In this case, the two threads will always wait.

The Java program below hides a potential deadlock problem,

 class T3 extends Thread                        {                        StringBuffer L1;                        StringBuffer L2;                        public T3(StringBuffer L1, StringBuffer L2)                        {                        this.L1 = L1;                        this.L2 = L2;                        }                        public void run()                        {                        synchronized (L1)                        {                        synchronized (L2)                        {                        }                        }                        }                        }                        public class Deadlock                        {                        void harness2() throws InterruptedException                        {                        StringBuffer L1 = new StringBuffer("L1");                        StringBuffer L2 = new StringBuffer("L2");                        Thread t1 = new T3(L1, L2);                        Thread t2 = new T3(L2, L1);                        t1.start();                        t2.start();                        t1.join();                        t2.join();                        }                        public static void main(String[] args) throws InterruptedException                        {                        Deadlock dlt = new Deadlock();                        dlt.harness2();                        }                        }

InDeadlockOfharness2Method, ClassDeadlockThe two instances are created and passed as parameters to the class.T3. InT3OfrunIn the method, the thread will obtain the locks of the two objects in turn, and then release the two locks in the reverse order. Because twoStringBufferThe instance is passed to the class in different order.T3The two threads obtain the two locks in different order. In this way, the deadlock occurs.

Run classes with mtratsample.Deadlock, You can check the potential deadlock errors in the program at runtime:

 $ mtrat -Dcom.ibm.mtrat.deadlock=true  -cp . sample.Deadlock                        Thread 7 : Acquire L1 L2                        Dead Lock 1                        Thread 7, acquired lock1 -> try lock2  sample/T3  line 109                        Thread 8, acquired lock2 -> try lock1  sample/T3  line 109                        Thread 8 : Acquire L2 L1

On the GUIEclipseAnd the following results are obtained:

Figure 2.

In the mtrat deadlock check report, we can find that the threadThread 7The lock has been obtained.lock1, Attempts to obtain the lock in the first line of the programlock2. However, threadsThread 8The lock has been obtained.lock2, Attempts to obtain the lock in the first line of the programlock1.

According to the mtrat deadlock check report, programmers can easily know that the deadlock problem is caused by locking two threads in the opposite order. One way to avoid this problem is to let the code get the lock in a fixed global order. If the two threads are locked in the same order, the deadlock error can be eliminated.

 void harness2() throws InterruptedException                        {                        StringBuffer L1 = new StringBuffer("L1");                        StringBuffer L2 = new StringBuffer("L2");                        Thread t1 = new T3(L1, L2);                        Thread t2 = new T3(L1, L2);                        t1.start();                        t2.start();                        t1.join();                        t2.join();                        }


Back to Top

Conclusion

In this article, we show how to check potential errors in parallel Java programs, such as data competition and deadlock. By using mtrat, you can discover parallel program errors that are hard to be detected with the naked eye during the program development stage. This tool makes it easier to develop correct and high-quality parallel programs.



Back to Top

Download

Description Name Size Download Method
Sample Code Src.zip 12 KB HTTP
Information about the Download Method

References

  • Download mtrat from alphaWorks.

  • Peter Haggar's article on developerworks acquires multiple locks in a globally fixed order to avoid deadlocks
  • Neel v. Kumar writes efficient thread security classes in developerworks articles.

Author Profile

Qi Yao, a member of the IBM China Software Lab (csdl bj) China Emerging Technology Institute, is mainly engaged in dynamic program analysis and static program analysis. He earned a master's degree from Beijing Institute of Technology. You can contact him via qiyao@cn.ibm.com.

Gan Zhi, a member of the SOA Design Center of the IBM China Software Lab (csdl bj), is mainly engaged in SOA and security. He is also interested in badminton. He earned a doctorate degree in network security in the Computer Science Department of Shanghai Jiao Tong University, during which he published many papers and technical books. You can contact him via ganzhi@cn.ibm.com.

Luo Zhida, a member of the IBM China Software Lab (csdl bj) China Emerging Technology Institute, specializes in dynamic program analysis and static program analysis. He earned a master's degree in software engineering from Peking University. You can contact him via luozd@cn.ibm.com.

Dai Xiaojun, a member of the IBM China Software Lab (csdl bj) China Emerging Technology Institute, focuses on parallel programming and agile software development methods. He obtained a computer from the Institute of software, Chinese Emy of sciences.

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.