java之Compiler control

Controlling Java Virtual Machine (JVM) compilers might seem like an unnecessary task,
but for many developers, this is an important aspect of testing. This is accomplished with
method-dependent compiler flags.
In this section, we will start with a look at JVM compilation modes and then look at
the compiler that can be controlled using the Java platform.

Compilation modes


The changes in the modern Java platform include granular control of both the C1 and C2
JVM compilers. As you can see in the following illustration, the Java HotSpot JVM has two
JIT compilation modes C1 and C2:

The C1 and C2 compilation modes use different compilation techniques and, if used on
the same code base, can produce different sets of machine code.

The C1 compilation mode


The C1 compilation mode inside the Java HotSpot VM is typically used for applications
that have the following characteristics:

  • Quick startup
  • Increased optimization
  • Client-side

The C2 compilation mode


The second compilation mode, C2, is used by applications with the following
listed characteristics:

  • Long runtimes
  • Server-side

Tiered compilation


Tiered compilation allows us to use both C1 and C2 compilation modes. Starting with Java
8, tiered compilation is the default process. As illustrated here, C1 mode is used at
startup to help provide greater optimization. Then, once the app has sufficiently warmed
up, C2 mode is employed:

扫描二维码关注公众号,回复: 9356562 查看本文章

Compiler control in Java 11

Java comes with the promise of the ability to have finite control over JVM compilers and to
make changes at runtime. These additional abilities do not degrade performance.
This permits greater fidelity of testing and testing optimization as we can run small
compiler tests without having to relaunch the entire JVM.
To control compiler operations, we need to create a directives file. These files
contain compiler directives that consist of a set of options with values. Directive files
essentially use a subset of JSON:

The JavaScript Object Notation (JSON) format is used for data-interchange. The
directive files have the following formatting differences from JSON:

  • int and doubles are the only supported number formats
  • The double forward slash (//) can be used for comment lines
  • Trailing commas (,) can be used in arrays and objects
  • Escape characters are not supported
    Option names are formatted as strings and do not have to be quoted

You can learn more about JSON at http://www.json.org

We can add our directive file using the following syntax at the command line:
            -XX:CompilerDirectivesFile=<file>
Here is a shell example of a directives file:

[ // Open square bracket marks the start of the directives file
	{ // Open curly brace marks the start of a directive block
		// A directives block that applies specifically to the C1 mode
		c1: {
			// directives go here
		},
		// A directives block that applies specifically to the C2 mode
		c2: {
			// directives go here
		},
		// Here we can put a directives that do not apply to
		// a specific compiler mode
	},
	{ // can have multiple directive blocks
		c1: {
			// directives go here
		}
		c2: {
			// directives go here
		}
	}
] // Close square bracket marks the start of the directives file

发布了296 篇原创文章 · 获赞 178 · 访问量 124万+

猜你喜欢

转载自blog.csdn.net/doctor_who2004/article/details/104215298
今日推荐