-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjbreak2016.xhtml
1660 lines (1388 loc) · 53 KB
/
jbreak2016.xhtml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>HotSpot Intrinsics</title>
<meta name="description" content="A short introduction to HotSpot Intrinsics" />
<meta name="author" content="Volker H. Simonis" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui" />
<link rel="stylesheet" href="reveal.js/css/reveal.css" />
<link rel="stylesheet" href="reveal.js/css/theme/jbreak2016.css" id="theme" />
<!-- Code syntax highlighting -->
<link rel="stylesheet" href="reveal.js/lib/css/monokai_sublime.css" />
<style type="text/css">
.scrollable {
bottom: 0px;
overflow-y: auto !important;
overflow-x: hidden !important;
}
.reveal .big {
font-size: .8em;
line-height: 1.3em;
}
.reveal pre.console {
background-color: black;
color: #00ff00;
}
code.terminal .hljs-title {
color: #00ff00;
}
.reveal pre.noshadow {
border-radius: 0;
box-shadow: unset;
}
.reveal pre code {
max-height: 98%;
}
.bold {
font-weight: bold;
}
table.calling_hirarchy {
// font-family: monospace, arial, helvetica, sans-serif;
// background-color: #ffffff;
font-size: smaller;
border-collapse: collapse;
margin: 0px auto;
padding: 2px;
width: auto;
overflow: auto;
border: none;
}
table.calling_hirarchy tr {
border: none;
}
table.calling_hirarchy td {
padding: 5px;
// font-family: monospace;
}
td.indent_level_0 {
border-style: solid;
border-width: 2px;
background-color: #a0a0a0;
}
td.indent_level_1 {
border-style: solid;
border-width: 2px;
background-color: #a8a8a8;
}
td.indent_level_2 {
border-style: solid;
border-width: 2px;
background-color: #b0b0b0;
}
td.indent_level_3 {
border-style: solid;
border-width: 2px;
background-color: #b8b8b8;
}
td.indent_level_4 {
border-style: solid;
border-width: 2px;
background-color: #c0c0c0;
}
td.indent_level_5 {
border-style: solid;
border-width: 2px;
background-color: #c8c8c8;
}
td.indent_level_6 {
border-style: solid;
border-width: 2px;
background-color: #d0d0d0;
}
td.indent_level_7 {
border-style: solid;
border-width: 2px;
background-color: #d8d8d8;
}
td.indent_level_8 {
border-style: solid;
border-width: 2px;
background-color: #e0e0e0;
}
td.indent_level_9 {
border-style: solid;
border-width: 2px;
background-color: #e8e8e8;
}
td.indent_level_10 {
border-style: solid;
border-width: 2px;
background-color: #f0f0f0;
}
td.indent_level_11 {
border-style: solid;
border-width: 2px;
background-color: #f8f8f8;
}
td.indent_level_12 {
border-style: solid;
border-width: 2px;
background-color: #ffffff;
}
td.functionCall {
// font-weight: bold;
color: darkblue;
}
td.javaCall {
font-weight: bold;
color: darkgreen;
}
td.comment {
font-style: italic;
color: maroon;
}
td.highlight {
border-style: solid;
border-width: 2px;
background-color: #a0ffa0;
}
table.calling_hirarchy td[rowspan] {
border-style: none;
border-width: 0;
border-left: dotted;
border-left-width: 2px;
}
</style>
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'reveal.js/css/print/pdf.css' : 'reveal.js/css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<!--[if lt IE 9]>
<script src="reveal.js/lib/js/html5shiv.js"></script>
<![endif]-->
<!--
Issue #1105: Keyboard shortcut to skip forward/back over fragments #1173
https://github.com/hakimel/reveal.js/pull/1173
-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h1>HotSpot Intrinsics<br/></h1>
<p>
<small>Volker Simonis [Фолкер Симонис], SAP / <a href="mailto:[email protected]">[email protected]</a></small>
</p>
</section>
<section>
<h2>Intrinsics</h2>
<blockquote>
<p style="text-align:justify">
“ ..an <span class="bold">intrinsic</span> function (a.k.a.
<span class="bold">builtin</span> function) is a function
available for use in a given programming language whose
implementation is handled specially by the compiler. Typically, it
substitutes a sequence of automatically generated instructions for
the original function call.. ”
</p>
</blockquote>
<blockquote>
<p style="text-align:justify">
“ ..the compiler has an intimate knowledge of the intrinsic
function and can therefore better integrate it and optimize it for
the situation.. ”
</p>
<footer>
<cite><a href="https://en.wikipedia.org/wiki/Intrinsic_function">Intrinsic function - Wikipedia</a></cite>
</footer>
</blockquote>
</section>
<section>
<h2>Intrinsics</h2>
<ul>
<li>Improve performance:<br/>
<pre class="big noshadow" data-trim="true">
<code class="java" text-trim="true">
<![CDATA[
GCC: bzero(), snprintf(), strndup(), log2(), ..
Java: Math.sqrt(), System.arraycopy(), ..
]]>
</code>
</pre>
</li>
<li>Expose functionality not expressable in the language itself:<br/>
<pre class="big noshadow" data-trim="true">
<code class="java" text-trim="true">
<![CDATA[
GCC: _xbegin(), _xend(), .. // Transactional Memory
Java: Unsafe.allocateInstance(),
Thread.onSpinWait() // JEP 285
]]>
</code>
</pre>
</li>
<li>Direct access to CPU instructions (assembler):<br/>
<pre class="big noshadow" data-trim="true">
<code class="java" text-trim="true">
<![CDATA[
MSVC: __movsq(), __mul128()
Java: Unsafe.loadFence()
]]>
</code>
</pre>
<br/>
<a href="https://software.intel.com/sites/landingpage/IntrinsicsGuide">https://software.intel.com/sites/landingpage/IntrinsicsGuide</a>
</li>
</ul>
</section>
<section>
<section>
<h2>The “<code>HelloWorld</code>” Intrinsic</h2>
<pre class="big noshadow" data-trim="true">
<code class="java" text-trim="true">
<![CDATA[
public class HelloWorld {
public static void sayHello() {
System.out.println("Hello JETConf!");
}
public static void main(String ... args) {
long start = System.nanoTime();
sayHello();
long stop = System.nanoTime();
System.out.format("%,9d%s%n", stop - start, "ns");
}
}
]]>
</code>
</pre>
</section>
<section>
<h2>The “<code>HelloWorld</code>” Intrinsic - DEMO</h2>
<pre class="big noshadow" data-trim="true">
<code class="terminal" data-trim="true" data-noescape="true">
$ java -Xint org.simonis.HelloWorld
<span class="fragment">Hello JETConf!
6,968,398ns</span>
<span class="fragment">$ java -Xint -XX:+JETConf org.simonis.HelloWorld</span>
<span class="fragment">Hello JETConf!
153,191ns</span>
</code>
</pre>
</section>
</section>
<section style="height: 100%;">
<section>
<h2>Observability - Bytecode Instrumentation</h2>
<ul>
<li>Allows dynamic changes of Java Bytecodes at runtime</li>
<li>Only addition of bytecodes to Java methods</li>
<li>Used by monitoring agents, profilers, coverage tools and event loggers</li>
<li>Implemented in <code>java.lang.instrument</code></li>
<li>Loaded into the VM with <code>-javaagent:jarpath[=options]</code></li>
<li>The agent implements "<code>premain(String args, Instrumentation inst)</code>"</li>
</ul>
</section>
<section style="height: 100%;">
<h2>The Java Instrumentation Agent</h2>
<pre class="big noshadow scrollable" style="height: 80%;" data-trim="true">
<code class="java" text-trim="true" data-noescape="true">
<![CDATA[
package org.simonis;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.lang.instrument.Instrumentation;
import java.security.ProtectionDomain;
import jdk.internal.org.objectweb.asm.ClassReader;
import jdk.internal.org.objectweb.asm.ClassVisitor;
import jdk.internal.org.objectweb.asm.ClassWriter;
import jdk.internal.org.objectweb.asm.MethodVisitor;
import jdk.internal.org.objectweb.asm.Opcodes;
public class MethodInstrumentationAgent {
static String pattern;
public static void premain(String args, Instrumentation inst) {
inst.addTransformer(new MethodInstrumentorTransformer());
pattern = args;
}
static class MethodInstrumentorTransformer implements ClassFileTransformer {
@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
MethodInstrumentorClassVisitor cv = new MethodInstrumentorClassVisitor(cw);
ClassReader cr = new ClassReader(classfileBuffer);
cr.accept(cv, 0);
return cw.toByteArray();
}
}
static class MethodInstrumentorClassVisitor extends ClassVisitor {
private String className;
public MethodInstrumentorClassVisitor(ClassVisitor cv) {
super(Opcodes.ASM5, cv);
}
@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
cv.visit(version, access, name, signature, superName, interfaces);
className = name;
}
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
MethodVisitor mv = cv.visitMethod(access, name, desc, signature, exceptions);
if (className.startsWith(pattern)) {
mv = new MethodInstrumentorMethodVisitor(mv, name + desc);
}
return mv;
}
}
static class MethodInstrumentorMethodVisitor extends MethodVisitor implements Opcodes {
private String methodName;
public MethodInstrumentorMethodVisitor(MethodVisitor mv, String name) {
super(Opcodes.ASM5, mv);
methodName = name;
}
@Override
public void visitCode() {
mv.visitCode();
mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
mv.visitLdcInsn("-> " + methodName);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
}
@Override
public void visitInsn(int opcode) {
if ((opcode >= IRETURN && opcode <= RETURN) || opcode == ATHROW) {
mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
mv.visitLdcInsn("<- " + methodName);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
}
mv.visitInsn(opcode);
}
}
}
]]>
</code>
</pre>
</section>
<section>
<p>The Manifest file:</p>
<pre class="big noshadow" data-trim="true">
<code class="java" text-trim="true">
<![CDATA[
Manifest-VErsion: 1.0
Premain-Class: org.simonis.MethodInstrumentationAgent
]]>
</code>
</pre>
<p>Compile and create with:</p>
<pre class="big noshadow" data-trim="true">
<code class="terminal" data-trim="true" data-noescape="true">
$ javac -XDignore.symbol.file=true org/simonis/MethodInstrumentationAgent.java
$ jar cvfm MethodInstrumentationAgent.jar manifest.mf \
org/simonis/MethodInstrumentationAgent*.class
</code>
</pre>
</section>
<section>
<h2>Observability - Bytecode Instrumentation</h2>
<pre class="big noshadow" data-trim="true">
<code class="terminal" data-trim="true" data-noescape="true">
$ java -javaagent:MethodInstrumentationAgent.jar=org/simonis \
org.simonis.HelloWorld
<span class="fragment">-> main([Ljava/lang/String;)V
-> sayHello()V
Hello JETConf!
<- sayHello()V
651,538ns
<- main([Ljava/lang/String;)V</span>
<span class="fragment">$ java -XX:+JETConf -javaagent:MethodInstrumentationAgent.jar=org/simonis \
org.simonis.HelloWorld</span>
<span class="fragment">-> main([Ljava/lang/String;)V
Hello JETConf!
51,750ns
<- main([Ljava/lang/String;)V</span>
</code>
</pre>
</section>
</section>
<section style="height: 100%;">
<section>
<h2>Observability - JVMTI Agents</h2>
<ul>
<li>JVMTI is the Java Virtual Machine Tool Interface</li>
<li>Allows programs to inspect the state and control the execution of the VM</li>
<li>Used by debuggers and profilers</li>
<li>A native interface to the Java VM</li>
<li>JVMTI agents are loaded during VM initialization</li>
<li>They access the VM by calling JVMTI and JNI functions</li>
</ul>
</section>
<section style="height: 100%;">
<h2>A simple tracing JVMTI Agent</h2>
<pre class="big noshadow scrollable" style="height: 80%;" data-trim="true">
<code class="cpp" text-trim="true" data-noescape="true">
<![CDATA[
// Build as follows:
// g++ -fPIC -shared -I <JDK>/include/ -I <JDK>/include/linux/ -o traceMethodAgent.so traceMethodAgent.cpp
#include <jvmti.h>
#include <stdio.h>
#include <string.h>
const char* pattern = "";
static void printMethod(jvmtiEnv* jvmti, jmethodID method, const char* prefix) {
char *name, *sig, *cl;
jclass javaClass;
jvmti->GetMethodDeclaringClass(method, &javaClass);
jvmti->GetClassSignature(javaClass, &cl, NULL);
++cl; // Ignore leading 'L'
if (strstr(cl, pattern) == cl) {
jvmti->GetMethodName(method, &name, &sig, NULL);
cl[strlen(cl) - 1] = '\0'; // Strip trailing ';'
fprintf(stdout, "%s %s::%s%s\n", prefix, cl, name, sig);
fflush (NULL);
jvmti->Deallocate((unsigned char*) name);
jvmti->Deallocate((unsigned char*) sig);
}
jvmti->Deallocate((unsigned char*) --cl);
}
void JNICALL methodEntryCallback(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread, jmethodID method) {
printMethod(jvmti, method, "->");
}
void JNICALL methodExitCallback(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread, jmethodID method, jboolean except, jvalue ret_val) {
printMethod(jvmti, method, "<-");
}
extern "C"
JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* jvm, char* options, void* reserved) {
jvmtiEnv* jvmti = NULL;
jvmtiCapabilities capa;
jvmtiError error;
if (options) pattern = strdup(options); // Options may contain the pattern
jint result = jvm->GetEnv((void**) &jvmti, JVMTI_VERSION_1_1);
if (result != JNI_OK) {
fprintf(stderr, "Can't access JVMTI!\n");
return JNI_ERR;
}
memset(&capa, 0, sizeof(jvmtiCapabilities));
capa.can_generate_method_entry_events = 1;
capa.can_generate_method_exit_events = 1;
if (jvmti->AddCapabilities(&capa) != JVMTI_ERROR_NONE) {
fprintf(stderr, "Can't set capabilities!\n");
return JNI_ERR;
}
jvmtiEventCallbacks callbacks;
memset(&callbacks, 0, sizeof(jvmtiEventCallbacks));
callbacks.MethodEntry = methodEntryCallback;
callbacks.MethodExit = methodExitCallback;
if (jvmti->SetEventCallbacks(&callbacks, sizeof(jvmtiEventCallbacks)) != JVMTI_ERROR_NONE) {
fprintf(stderr, "Can't set event callbacks!\n");
return JNI_ERR;
}
if (jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_METHOD_ENTRY, NULL) != JVMTI_ERROR_NONE) {
fprintf(stderr, "Can't enable JVMTI_EVENT_METHOD_ENTRY!\n");
return JNI_ERR;
}
if (jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_METHOD_EXIT, NULL) != JVMTI_ERROR_NONE) {
fprintf(stderr, "Can't enable JVMTI_EVENT_METHOD_EXIT!\n");
return JNI_ERR;
}
}
]]>
</code>
</pre>
</section>
<section>
<h2>Observability - JVMTI Agents</h2>
<pre class="big noshadow" data-trim="true">
<code class="terminal" data-trim="true" data-noescape="true">
$ java -XX:-JETConf -agentpath:traceMethodAgent.so=org/simonis \
org.simonis.HelloWorld
<span class="fragment">-> org/simonis/HelloWorld::main([Ljava/lang/String;)V
-> org/simonis/HelloWorld::sayHello()V
Hello JETConf!
<- org/simonis/HelloWorld::sayHello()V
24,256,161ns
<- org/simonis/HelloWorld::main([Ljava/lang/String;)V</span>
<span class="fragment">$ java -XX:+JETConf -agentpath:traceMethodAgent.so=org/simonis \
org.simonis.HelloWorld</span>
<span class="fragment">-> org/simonis/HelloWorld::main([Ljava/lang/String;)V
Hello JETConf!
171,417ns
<- org/simonis/HelloWorld::main([Ljava/lang/String;)V</span>
</code>
</pre>
</section>
</section>
<section>
<h3 style="text-transform: none;"><a href="https://bugs.openjdk.java.net/browse/JDK-8013579">https://bugs.openjdk.java.net/browse/JDK-8013579</a></h3>
<img class="plain" data-src="images/8013579_intrinsics_class_redef.png" alt="JDK-8013579: Intrinsics for Java methods don't take class redefinition into account"/>
</section>
<section style="height: 100%;">
<section>
<h2>DEMO - Math Intrinsics</h2>
<pre class="big noshadow" data-trim="true">
<code class="java" text-trim="true">
<![CDATA[
static double compute(int count, double d) {
for (int i = 0; i < count; i++) {
d += Math.pow(Math.sin(d), Math.sqrt(d)); // d += sin(d) ^ sqrt(d)
}
return d;
}
public static void main(String[] args) throws Exception {
double seed = args.length; // Avoid constant folding
int count = Integer.parseInt(args[0]); // Iteration count for compute()
for (int i = 0; i < 20_000; i++) {
compute(1, seed); // JIT-compile compute()
}
compute(count, seed); // Call compute() with huge count
}
]]>
</code>
</pre>
</section>
</section>
<section style="height: 100%;">
<section>
<h2>Math Intrinsics & Time To Safepoint (TTS)</h2>
<pre class="big noshadow" data-trim="true">
<code class="java" text-trim="true">
<![CDATA[
public static void main(String[] args) throws Exception {
... // Same as before ...
new Thread() { // Create a new thread..
{ setDaemon(true); } // ..and make it a daemon thread.
public void run() {
while (true) {
try {
Thread.sleep(1_000); // Every second..
} catch (InterruptedException e) {}
System.gc(); // ..we trigger a GC.
}
}
}.start(); // Let it run concurrently.
compute(count, seed); // Call compute() with huge count
]]>
</code>
</pre>
</section>
<section>
<h3 style="text-transform: none;"><code>-XX:+UseCountedLoopSafepoints</code><a href="https://bugs.openjdk.java.net/browse/JDK-6869327"> (JDK-6869327)</a></h3>
<img class="plain" data-src="images/6869327_counted_loop_safep.png" alt="JDK-6869327: Add new C2 flag to keep safepoints in counted loops"/>
</section>
<!--
6869327: Add new C2 flag to keep safepoints in counted loops.
-XX:+UseCountedLoopSafepoints
6869327_counted_loop_safep.png
-->
</section>
<section style="height: 100%;">
<h2>HotSpot Intrinsics</h2>
<p style="text-align:left">Defined in: <code>src/share/vm/classfile/vmSymbols.hpp</code></p>
<ul>
<li>Library intrinsics (~260)
<ul>
<li>Library methods replaced by assembly, IR or both</li>
</ul>
</li>
<li>Bytecode intrinsics (~40)
<ul>
<li>Late/always inlining candidates (e.g. <code>StringBuffer::*</code>, boxing/unboxing methods)</li>
<li>See: <code>Compile::should_delay_string_inlining()</code> in <code>doCall.cpp</code></li>
<li>See: <code>InlineTree::should_inline()</code> in <code>bytecodeInfo.cpp</code></li>
</ul>
</li>
<li><em>Only for system classes</em></li>
<li>Must be annotated with <code>jdk.internal.HotSpotIntrinsicCandidate</code> (since Java 9)
<ul>
<li>Checked by <code>-XX:+CheckIntrinsics</code></li>
</ul>
</li>
</ul>
</section>
<section style="height: 100%;">
<h2>Library Intrinsics</h2>
<ul>
<li>Interpreter:
<ul>
<li>Implements only few intrinsics (notably <code>Math</code> and <code>CRC32</code>)</li>
<li>Generates special entry points (see: <code>AbstractInterpreter::MethodKind()</code>)</li>
</ul>
</li>
<li>C1 (aka "client") JIT:
<ul>
<li>Mostly <code>Unsafe</code> and <code>Math</code> intrinsics (see <code>Compiler::is_intrinsic_supported()</code>)</li>
<li>See: <code>GraphBuilder::build_graph_for_intrinsic()</code></li>
</ul>
</li>
<li>C2 (aka "server") JIT:<ul>
<li>See <code>C2Compiler::is_intrinsic_supported()</code> for a complete list</li>
<li>See <code>LibraryCallKit::try_to_inline()</code> in <code>library_call.cpp</code></li>
<li>Create new "Intermediate Representation" (i.e. IR) nodes..<br/>
..and match these nodes with corresponding "Machine Nodes" in <code>.ad</code> file</li>
<li>Use <code>IdealKit</code>/<code>GraphKit</code> to mimic the functionality directly in IR<br/>
(see <code>PhaseStringOpts</code> methods in <code>stringopts.hpp</code></li>
</ul>
</li>
</ul>
</section>
<section style="height: 100%;">
<h2>Controlling Intrinsics</h2>
<ul>
<li><code>-XX:+PrintCompilation -XX:+PrintInlining</code><br/>
<pre class="big noshadow" data-trim="true">
<code class="cpp" text-trim="true">
<![CDATA[
613 1 b org.simonis.Loop::compute (29 bytes)
@ 9 java.lang.Math::sin (5 bytes) (intrinsic)
@ 13 java.lang.Math::sqrt (5 bytes) (intrinsic)
]]>
</code>
</pre>
</li>
<li><code>-XX:+CheckIntrinsics</code> (together with <code>@HotSpotIntrinsicCandidate</code>)><br/>
<pre class="big noshadow" data-trim="true">
<code class="cpp" text-trim="true">
<![CDATA[
Compiler intrinsic is defined for method [org.simonis.HelloWorld.sayHello()V],
but the method is not annotated with @HotSpotIntrinsicCandidate. Exiting.
]]>
</code>
</pre>
</li>
<li><code>-XX:DisableIntrinsic={_dsqrt, ...}</code> (use id from <code>vmSymbols.hpp</code>)</li>
<li><code>-XX:+PrintIntrinsics</code></li>
<li><code>-XX:+Use{AES,CRC32,GHASH,MathExact,...}Intrinsics</code></li>
<li><code>-XX:-InlineNatives</code>, <code>-XX:-Inline{Math, Class, Thread, ...}Natives</code><br/>
<code>-XX:-Inline{UnsafeOps, ArrayCopy, ObjectHash}</code></li>
</ul>
</section>
<section>
<h2>Native vs. Intrinsic methods</h2>
<ul>
<li>JVM state (safepoints)</li>
<li>Java vs. native calling conventions</li>
<li>Platform dependent (native implementation required!)</li>
<li>OK for 'expensive' calls like I/O</li>
</ul>
</section>
<section style="height: 100%;">
<section>
<h2>Implementing an Interpreter Intrinsics</h2>
<p style="text-align:left">Define in: <code>src/share/vm/classfile/vmSymbols.hpp</code></p>
<pre class="big noshadow" data-trim="true">
<code class="cpp" text-trim="true">
<![CDATA[
do_class( helloWorld, "org/simonis/HelloWorld") \
do_intrinsic(_sayHello, helloWorld, sayHello_name, sayHello_sign, F_S) \
do_name( sayHello_name, "sayHello") \
do_signature(sayHello_sign, "()V") \
]]>
</code>
</pre>
<p style="text-align:left">Define in: <code>src/share/vm/interpreter/abstractInterpreter.hpp</code></p>
<pre class="big noshadow" data-trim="true">
<code class="cpp" text-trim="true">
<![CDATA[
class AbstractInterpreter: AllStatic {
enum MethodKind {
zerolocals, // normal method (needs locals initialization)
java_lang_math_sin, // implementation of java.lang.Math.sin(x)
...
HelloWorld_sayHello, // implementation of org.simonis.HelloWorld.sayHello()
]]>
</code>
</pre>
</section>
<section>
<h2>Implementing an Interpreter Intrinsics</h2>
<p style="text-align:left">Map in: <code>src/share/vm/interpreter/abstractInterpreter.cpp</code></p>
<pre class="big noshadow" data-trim="true">
<code class="cpp" text-trim="true">
<![CDATA[
MethodKind AbstractInterpreter::method_kind(methodHandle m) {
...
if (JETConf) {
if (m->intrinsic_id() == vmIntrinsics::_sayHello) {
return HelloWorld_sayHello;
]]>
</code>
</pre>
<p style="text-align:left">Generate in: <code>src/share/vm/interpreter/templateInterpreterGenerator.cpp</code></p>
<pre class="big noshadow" data-trim="true">
<code class="cpp" text-trim="true">
<![CDATA[
address TemplateInterpreterGenerator::generate_method_entry(MethodKind kind) {
switch (kind) {
case Interpreter::HelloWorld_sayHello: entry_point = generate_sayHello();
...
return entry_point;
]]>
</code>
</pre>
</section>
<section>
<h2>Implementing an Interpreter Intrinsics</h2>
<p style="text-align:left">Implement in: <code>src/cpu/x86/vm/templateInterpreterGenerator_x86_64.cpp</code></p>
<pre class="big noshadow" data-trim="true">
<code class="cpp" text-trim="true">
<![CDATA[
#define __ _masm->
address TemplateInterpreterGenerator::generate_sayHello() {
// r13: sender sp
// stack: [ ret adr ] <-- rsp
address entry_point = __ pc();
const char *msg = "Hello JETConf!\n";
__ mov64(c_rarg1, (long)stdout);
__ mov64(c_rarg0, (long)msg);
__ call(RuntimeAddress(CAST_FROM_FN_PTR(address, fputs)));
__ pop(rax);
__ mov(rsp, r13);
__ jmp(rax);
return entry_point;
}
]]>
</code>
</pre>
</section>
<section>
<h2>Implementing an Interpreter Intrinsics</h2>
<p style="text-align:left">Enable in: <code>src/share/vm/oops/method.cpp</code></p>
<pre class="big noshadow" data-trim="true">
<code class="cpp" text-trim="true">
<![CDATA[
vmSymbols::SID Method::klass_id_for_intrinsics(const Klass* holder) {
// if loader is not the default loader (i.e., != NULL), we can't know the
// intrinsics because we are not loading from core libraries
const InstanceKlass* ik = InstanceKlass::cast(holder);
if (JETConf && ik->name()->equals("org/simonis/HelloWorld")) { // <----
// check for org.simonis.HelloWorld // <----
return vmSymbols::find_sid(ik->name()); // <----
}
if ((ik->class_loader() != NULL)) {
return vmSymbols::NO_SID; // regardless of name, no intrinsics here
}
// see if the klass name is well-known:
Symbol* klass_name = ik->name();
return vmSymbols::find_sid(klass_name);
}
]]>
</code>
</pre>
</section>
</section>
<section style="height: 100%;">
<section>
<h2>Implementing a C2-JIT Intrinsic</h2>
<p style="text-align:left">Intrinsify: <code>java.util.Random.nextInt()</code></p>
<pre class="big noshadow" data-trim="true">
<code class="java" text-trim="true">
<![CDATA[
public class Random implements java.io.Serializable {
...
@HotSpotIntrinsicCandidate
public int nextInt() {
return next(32);
}
]]>
</code>
</pre>
<p style="text-align:left">Define in: <code>src/share/vm/classfile/vmSymbols.hpp</code></p>
<pre class="big noshadow" data-trim="true">
<code class="cpp" text-trim="true">
<![CDATA[
do_class( java_util_Random, "java/util/Random") \
do_intrinsic(_nextInt, java_util_Random, nextInt_name, ni_sign, F_R)\
do_name( nextInt_name, "nextInt") \
do_signature(ni_sign, "()I") \
]]>
</code>
</pre>
</section>
<section>
<h2>Implementing a C2-JIT Intrinsic</h2>
<p style="text-align:left">Detect <code>rdrand</code> instruction in: <code>src/cpu/x86/vm/vm_version_x86.hpp</code></p>
<pre class="big noshadow" data-trim="true">
<code class="cpp" text-trim="true">
<![CDATA[
class VM_Version {
static uint64_t feature_flags() {
if (_cpuid_info.std_cpuid1_ecx.bits.rdrand != 0)
result |= CPU_RDRAND;
static bool supports_rdrand() { return (_features & CPU_RDRAND) != 0; }
]]>
</code>
</pre>
<p style="text-align:left">Implement <code>rdrand</code> in: <code>src/cpu/x86/vm/assembler_x86.hpp</code></p>
<pre class="big noshadow" data-trim="true">
<code class="cpp" text-trim="true">
<![CDATA[
void Assembler::rdrandl(Register dst) {
int encode = prefix_and_encode(dst->encoding());
emit_int8(0x0F);
emit_int8((unsigned char)0xC7);
emit_int8((unsigned char)(0xF0 | encode));