Finding shrink critical section refactoring opportunities for the evolution of concurrent code in trustworthy software
With the keyword synchronized, Java provides a simple but often over-used synchronization mechanism. Many programmers tend to use a big synchronized section to protect a large block of statements even if many of them are unnecessarily synchronized. Such coarse-gained synchronizations may increase lock contention and thereby decrease the performance of a program, especially in a multi-core environment. The decrease in performance may affect the quality of service provided by the software and hence make the software less trustworthy. To ensure trustworthiness, this paper proposes a source-code level refactoring approach to optimize coarse-gained synchronizations. It exploits dependence and escape information to search for shrinkable critical sections and can transform them into smaller ones automatically. The approach can be used to gradually evolve concurrent code for better quality. In the approach, to achieve more optimizations, we develop a new escape analysis, named escape state analysis, which can not only tell us whether an object finally escapes but also determine the escape states of objects at each program site. With such an analysis, more refactoring opportunities can be found. We build a tool and test our refactoring approach on several benchmark programs. The results show that the proposed approach does make effective refactoring suggestions for the users.