-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jthreadparser_test.go
1282 lines (1149 loc) · 43.9 KB
/
jthreadparser_test.go
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
package jthreadparser
import (
"io/ioutil"
"os"
"strings"
"testing"
)
const (
threadInfo = `"Attach Listener" daemon prio=10 tid=0x00002aaab74c5000 nid=0x2ea5 waiting on condition [0x0000000000000000]`
threadInformation = `"HDScanner" prio=10 tid=0x00002aaaebf6e800 nid=0x4367 runnable [0x00000000451cf000]
java.lang.Thread.State: RUNNABLE
at java.io.UnixFileSystem.getBooleanAttributes0(Native Method)
at java.io.UnixFileSystem.getBooleanAttributes(UnixFileSystem.java:228)
at java.io.File.isHidden(File.java:804)
Locked ownable synchronizers:
- <0x00000007500b7250> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)
`
daemonThreadInformation = `"Attach Listener" #6085 daemon prio=9 os_prio=0 tid=0x00007f90d0106000 nid=0x18a1 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None`
threadInfoWithLocks = `"default task-23" #349 prio=5 os_prio=0 tid=0x00007f8fe400c800 nid=0x72fa waiting for monitor entry [0x00007f8f7228e000]
java.lang.Thread.State: BLOCKED (on object monitor)
at java.security.Provider.getService(Provider.java:1039)
- locked <0x0000000682e5f948> (a sun.security.provider.Sun)
at sun.security.jca.ProviderList.getService(ProviderList.java:332)
at sun.security.jca.GetInstance.getInstance(GetInstance.java:157)
at java.security.Security.getImpl(Security.java:695)
at java.security.MessageDigest.getInstance(MessageDigest.java:167)
at sun.security.rsa.RSASignature.<init>(RSASignature.java:79)
at sun.security.rsa.RSASignature$SHA512withRSA.<init>(RSASignature.java:305)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.security.Provider$Service.newInstance(Provider.java:1595)
at java.security.Signature$Delegate.newInstance(Signature.java:1020)
at java.security.Signature$Delegate.chooseProvider(Signature.java:1114)
- locked <0x00000007bc531138> (a java.lang.Object)
at java.security.Signature$Delegate.engineInitSign(Signature.java:1188)
at java.security.Signature.initSign(Signature.java:553)
at sun.security.ssl.HandshakeMessage$ECDH_ServerKeyExchange.<init>(HandshakeMessage.java:1031)
at sun.security.ssl.ServerHandshaker.clientHello(ServerHandshaker.java:971)
at sun.security.ssl.ServerHandshaker.processMessage(ServerHandshaker.java:228)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:1052)
at sun.security.ssl.Handshaker$1.run(Handshaker.java:992)
at sun.security.ssl.Handshaker$1.run(Handshaker.java:989)
at java.security.AccessController.doPrivileged(Native Method)
at sun.security.ssl.Handshaker$DelegatedTask.run(Handshaker.java:1467)
- locked <0x00000007bbbac500> (a sun.security.ssl.SSLEngineImpl)
at io.undertow.protocols.ssl.SslConduit$5.run(SslConduit.java:1021)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Locked ownable synchronizers:
- <0x00000006a43d5c08> (a java.util.concurrent.ThreadPoolExecutor$Worker)
`
threadDumpSample = `2017-06-02 19:02:52
Full thread dump Java HotSpot(TM) 64-Bit Server VM (20.141-b32 mixed mode):
"Attach Listener" daemon prio=10 tid=0x00002aaab74c5000 nid=0x2ea5 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"RMI TCP Connection(idle)" daemon prio=10 tid=0x00002aaac69b1800 nid=0x2dec waiting on condition [0x0000000051388000]
java.lang.Thread.State: TIMED_WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x0000000740c99708> (a java.util.concurrent.SynchronousQueue$TransferStack)
at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:196)
at java.util.concurrent.SynchronousQueue$TransferStack.awaitFulfill(SynchronousQueue.java:424)
at java.lang.Thread.run(Thread.java:682)
Locked ownable synchronizers:
- None
"RMI TCP Connection(idle)" daemon prio=10 tid=0x00002aaad5029000 nid=0x2bf8 waiting on condition [0x0000000051287000]
java.lang.Thread.State: TIMED_WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x0000000740c99708> (a java.util.concurrent.SynchronousQueue$TransferStack)
at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:196)
at java.util.concurrent.SynchronousQueue$TransferStack.awaitFulfill(SynchronousQueue.java:424)
at java.lang.Thread.run(Thread.java:682)
Locked ownable synchronizers:
- None`
)
func TestThreadInfo(t *testing.T) {
type testCase struct {
threadInfoHeadLine string
want ThreadInfo
}
tests := []testCase{
{
threadInfoHeadLine: `"http-nio-8080-BlockPoller" #18 daemon prio=5 os_prio=0 cpu=13.17ms elapsed=116.26s tid=0x00007f4948f82000 nid=0xa35f runnable [0x00007f48a7dfe000]`,
want: ThreadInfo{
Daemon: true,
Name: `http-nio-8080-BlockPoller`,
NativeID: "0xa35f",
Priority: "5",
ID: "0x00007f4948f82000",
},
},
{
threadInfoHeadLine: `"GC Thread#6" os_prio=0 cpu=12.53ms elapsed=106.94s tid=0x00007f1914009000 nid=0xb090 runnable`,
want: ThreadInfo{
Daemon: false,
Name: `GC Thread#6`,
NativeID: "0xb090",
Priority: "",
ID: `0x00007f1914009000`,
},
},
{
threadInfoHeadLine: `"GC Thread#0" os_prio=0 cpu=25.37ms elapsed=107.25s tid=0x00007f195c06a800 nid=0xb079 runnable`,
want: ThreadInfo{
Daemon: false,
Name: `GC Thread#0`,
NativeID: `0xb079`,
Priority: "",
ID: `0x00007f195c06a800`,
},
},
{
threadInfoHeadLine: `"scheduling-1" #31 prio=5 os_prio=0 cpu=23.59ms elapsed=116.23s tid=0x00007f494899a800 nid=0xa36c waiting on condition [0x00007f48a70f0000]`,
want: ThreadInfo{
Daemon: false,
Name: `scheduling-1`,
NativeID: `0xa36c`,
ID: `0x00007f494899a800`,
Priority: "5",
},
},
}
for _, tc := range tests {
got := extractThreadInfoFromLine(tc.threadInfoHeadLine)
if got.ID != tc.want.ID {
t.Errorf("got=[%s], want=[%s]", got.ID, tc.want.ID)
}
if got.Daemon != tc.want.Daemon {
t.Errorf("got=[%t], want=[%t]", got.Daemon, tc.want.Daemon)
}
if got.Name != tc.want.Name {
t.Errorf("got=[%s], want=[%s]", got.Name, tc.want.Name)
}
if got.NativeID != tc.want.NativeID {
t.Errorf("got=[%s], want=[%s]", got.NativeID, tc.want.NativeID)
}
if got.Priority != tc.want.Priority {
t.Errorf("got=[%s], want=[%s]", got.Priority, tc.want.Priority)
}
if got.ID != tc.want.ID {
t.Errorf("got=[%s], want=[%s]", got.ID, tc.want.ID)
}
}
}
func TestExtractThreadState(t *testing.T) {
t.Parallel()
type testCase struct {
threadStateLine, want string
}
tests := []testCase{
{
threadStateLine: ` java.lang.Thread.State: BLOCKED (on object monitor)`,
want: "BLOCKED",
},
{
threadStateLine: `java.lang.Thread.State: TIMED_WAITING (on object monitor)`,
want: `TIMED_WAITING`,
},
}
for _, tc := range tests {
got := extractThreadState(tc.threadStateLine)
if got != tc.want {
t.Errorf("got=[%s], want=[%s]", got, tc.want)
}
}
}
func TestShouldIdentifyDaemonThread(t *testing.T) {
t.Parallel()
threadDump := ParseFrom(strings.NewReader(daemonThreadInformation))
if len(threadDump.Threads) != 1 {
t.Error("Error parsing single daemon thread dump")
}
if !threadDump.Threads[0].Daemon {
t.Error("Thread should be daemon")
}
}
func TestShouldTagCorrectlyDaemonThread(t *testing.T) {
t.Parallel()
type testCase struct {
threadInfo ThreadInfo
want string
}
tests := []testCase{
{
threadInfo: ThreadInfo{ID: "0x00007f90d0106000", Name: "Attach Listener", State: "RUNNABLE", Daemon: true},
want: `Thread Name: 'Attach Listener' (daemon), ID: '0x00007f90d0106000', State: 'RUNNABLE'`,
},
{
threadInfo: ThreadInfo{ID: "0x00007f90d0106000", Name: "Attach Listener", State: "RUNNABLE", Daemon: false},
want: `Thread Name: 'Attach Listener', ID: '0x00007f90d0106000', State: 'RUNNABLE'`,
},
}
for _, tc := range tests {
got := tc.threadInfo.String()
if got != tc.want {
t.Errorf("got=[%s], expected=[%s]", tc.threadInfo.String(), tc.want)
}
}
}
func TestParseFromFile(t *testing.T) {
t.Parallel()
file, err := ioutil.TempFile("", "xxxx.")
if err != nil {
t.Error(err)
}
defer os.Remove(file.Name())
err = ioutil.WriteFile(file.Name(), []byte(threadDumpSample), 0600)
if err != nil {
t.Error(err)
}
expectedNumberOfThreadsInSample := 3
threadDump, _ := ParseFromFile(file.Name())
if len(threadDump.Threads) != expectedNumberOfThreadsInSample {
t.Errorf("got=[%d], expected=[%d]", len(threadDump.Threads), expectedNumberOfThreadsInSample)
}
_, err = ParseFromFile("does_not_exist...")
if err == nil {
t.Error("there should have been an error")
}
}
func TestTopMethodsInThreads(t *testing.T) {
t.Parallel()
type testCase struct {
javaMethodName string
want int
}
threadDump, err := ParseFromFile("threaddumpsamples/tdump.sample")
if err != nil {
t.Error("Unable to parse thread dump sample file")
}
tests := []testCase{
{
javaMethodName: "java.lang.Object.wait(Native Method)",
want: 82,
},
}
for _, tc := range tests {
got := MostUsedMethods(&threadDump.Threads)
if got[tc.javaMethodName] != tc.want {
t.Errorf("Should have identified %d threads, got=%d", tc.want, got[tc.javaMethodName])
}
}
}
func TestIdenticalStrackTrace(t *testing.T) {
type testCase struct {
stacktrace string
want int
}
tests := []testCase{
{
stacktrace: `at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x0000000750785368> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:156)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:1987)
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:399)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:957)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:917)
at java.lang.Thread.run(Thread.java:682)`,
want: 20,
},
}
threadDump, err := ParseFromFile("threaddumpsamples/tdump.sample")
if err != nil {
t.Error("Unable to parse thread dump sample file")
}
for _, tc := range tests {
got := IdenticalStackTrace(&threadDump.Threads)
if got[tc.stacktrace] != tc.want {
t.Errorf("Should have identified %d (got=%d) threads with the following stacktrace:\n%s\nEND",
tc.want, got[tc.stacktrace], tc.stacktrace)
}
}
}
func TestVerifyNumberOfThreadsInSamples(t *testing.T) {
t.Parallel()
type testCase struct {
sampleFileName string
want int
}
tests := []testCase{
{"threaddumpsamples/10.0.2.0.txt", 51},
{"threaddumpsamples/10.0.2.2.txt", 51},
{"threaddumpsamples/10.0.2.3.txt", 51},
{"threaddumpsamples/11.0.2.0.txt", 43},
{"threaddumpsamples/11.0.2.1.txt", 46},
{"threaddumpsamples/11.0.2.2.txt", 50},
{"threaddumpsamples/11.0.2.3.txt", 50},
{"threaddumpsamples/11.0.8.0-amazon.txt", 99},
{"threaddumpsamples/11.0.8.1-amazon.txt", 99},
{"threaddumpsamples/11.0.8.2-amazon.txt", 48},
{"threaddumpsamples/11.0.8.3-amazon.txt", 48},
{"threaddumpsamples/12.0.2.0.txt", 49},
{"threaddumpsamples/12.0.2.1.txt", 48},
{"threaddumpsamples/12.0.2.2.txt", 48},
{"threaddumpsamples/12.0.2.3.txt", 48},
{"threaddumpsamples/13.0.2.0.txt", 42},
{"threaddumpsamples/13.0.2.1.txt", 42},
{"threaddumpsamples/13.0.2.2.txt", 50},
{"threaddumpsamples/13.0.2.3.txt", 48},
{"threaddumpsamples/14.0.1.0.txt", 51},
{"threaddumpsamples/14.0.1.1.txt", 51},
{"threaddumpsamples/14.0.1.2.txt", 51},
{"threaddumpsamples/14.0.1.3.txt", 50},
{"threaddumpsamples/15.0.txt", 43},
{"threaddumpsamples/15.1.txt", 51},
{"threaddumpsamples/15.2.txt", 51},
{"threaddumpsamples/15.3.txt", 49},
{"threaddumpsamples/1.8-amazon.0.txt", 37},
{"threaddumpsamples/1.8-amazon.1.txt", 37},
{"threaddumpsamples/1.8-amazon.2.txt", 37},
{"threaddumpsamples/1.8-amazon.3.txt", 37},
{"threaddumpsamples/9.0.4.0.txt", 51},
{"threaddumpsamples/9.0.4.1.txt", 51},
{"threaddumpsamples/9.0.4.2.txt", 51},
{"threaddumpsamples/9.0.4.3.txt", 51},
{"threaddumpsamples/tdump2.sample", 58},
{"threaddumpsamples/tdump.jdk11.idea.txt", 56},
{"threaddumpsamples/tdump.sample", 203},
{"threaddumpsamples/x.txt", 0},
}
for _, tc := range tests {
threadDump, err := ParseFromFile(tc.sampleFileName)
if err != nil {
t.Error(err)
}
if len(threadDump.Threads) != tc.want {
t.Errorf("got=[%d], want=[%d]", len(threadDump.Threads), tc.want)
}
}
}
func TestNoThreadDumpFile(t *testing.T) {
t.Parallel()
type testCase struct {
sampleFileName string
want int
}
tests := []testCase{
{"threaddumpsamples/x.txt", 0},
}
for _, tc := range tests {
threadDump, err := ParseFromFile(tc.sampleFileName)
if err != nil {
t.Error(err)
}
if len(threadDump.Threads) != tc.want {
t.Errorf("got=[%d], want=[%d]", len(threadDump.Threads), tc.want)
}
}
}
func TestHasRunnableState(t *testing.T) {
t.Parallel()
type testCase struct {
threadHeaderLine string
want bool
}
tests := []testCase{
{
threadHeaderLine: `GC Thread#3" os_prio=0 cpu=21.13ms elapsed=146.91s tid=0x00007f1914004000 nid=0xb08d runnable `,
want: true,
},
{
threadHeaderLine: `"Finalizer" #3 daemon prio=8 os_prio=0 cpu=0.77ms elapsed=741.83s tid=0x00007f6f5c29e000 nid=0x6b90 in Object.wait() [0x00007f6f13ffe000]`,
want: false,
},
}
for _, tc := range tests {
got := hasRunnableState(tc.threadHeaderLine)
if got != tc.want {
t.Errorf("got=[%t], want=[%t]", got, tc.want)
}
}
}
func TestWaitingOnConditionState(t *testing.T) {
type testCase struct {
threadHeaderLine string
want bool
}
tests := []testCase{
{
threadHeaderLine: `"VM Periodic Task Thread" os_prio=0 tid=0x00007f74bc22d800 nid=0xd9c4 waiting on condition `,
want: true,
},
{
threadHeaderLine: `"Finalizer" #3 daemon prio=8 os_prio=0 cpu=0.77ms elapsed=741.83s tid=0x00007f6f5c29e000 nid=0x6b90 in Object.wait() [0x00007f6f13ffe000]`,
want: false,
},
}
for _, tc := range tests {
got := hasWaitingOnState(tc.threadHeaderLine)
if got != tc.want {
t.Errorf("got=[%t], want=[%t]", got, tc.want)
}
}
}
func TestHasThreadHeaderInformation(t *testing.T) {
type testCase struct {
threadHeader string
want bool
}
tests := []testCase{
{
threadHeader: `"VM Thread" os_prio=0 cpu=235.82ms elapsed=147.51s tid=0x00007f904819cef0 nid=0xbfd2 runnable`,
want: true,
},
{
threadHeader: `"http-nio-8080-exec-8" #27 daemon prio=5 os_prio=0 cpu=1.67ms elapsed=144.34s tid=0x00007f9049128ec0 nid=0xc03f waiting for monitor entry [0x00007f8faf0f0000]`,
want: true,
},
{
threadHeader: `"OPPORTUNITY"`,
want: false,
},
{
threadHeader: `Hello!`,
want: false,
},
}
for _, tc := range tests {
got := hasThreadHeaderInformation(tc.threadHeader)
if got != tc.want {
t.Errorf("got=[%t], want=[%t]", got, tc.want)
}
}
}
func TestCheckThreadInfo(t *testing.T) {
threadDump, err := ParseFromFile("threaddumpsamples/9.0.4.0-test.txt")
if err != nil {
t.Error(err)
}
const expectedNumberOfThreadsInSampleFile = 5
if len(threadDump.Threads) != expectedNumberOfThreadsInSampleFile {
t.Errorf("got=[%d] threads, expected=[%d]", len(threadDump.Threads), expectedNumberOfThreadsInSampleFile)
}
type testCase struct {
threadName string
isDaemon bool
threadID string
nativeID string
state string
}
tests := []testCase{
{
threadName: `Attach Listener`,
isDaemon: true,
threadID: `0x00007f321c001000`,
nativeID: `0x5ac6`,
state: `RUNNABLE`,
},
{
threadName: `DestroyJavaVM`,
isDaemon: false,
threadID: `0x00007f32b4012000`,
nativeID: `0x5934`,
state: `RUNNABLE`,
},
{
threadName: `scheduling-1`,
isDaemon: false,
threadID: `0x00007f32b556c000`,
nativeID: `0x596c`,
state: `TIMED_WAITING`,
},
{
threadName: `http-nio-8080-Acceptor`,
isDaemon: true,
threadID: `0x00007f32b53d7000`,
nativeID: `0x596b`,
state: `RUNNABLE`,
},
{
threadName: `http-nio-8080-ClientPoller`,
isDaemon: true,
threadID: `0x00007f32b53f1000`,
nativeID: `0x596a`,
state: `RUNNABLE`,
},
}
for i := 0; i < expectedNumberOfThreadsInSampleFile; i++ {
got := threadDump.Threads[i]
if got.Name != tests[i].threadName {
t.Errorf("got=[%s], expected=[%s]", got.Name, tests[i].threadName)
}
if got.Daemon != tests[i].isDaemon {
t.Errorf("got=[%t], want=[%t]", got.Daemon, tests[i].isDaemon)
}
if got.ID != tests[i].threadID {
t.Errorf("got=[%s], want=[%s]", got.ID, tests[i].threadID)
}
if got.NativeID != tests[i].nativeID {
t.Errorf("got=[%s], want=[%s]", got.NativeID, tests[i].nativeID)
}
if got.State != tests[i].state {
t.Errorf("got=[%s], want=[%s]", got.State, tests[i].state)
}
}
}
func TestCheckThreadInfo2(t *testing.T) {
threadDump, err := ParseFromFile("threaddumpsamples/14.0.1.1-together.txt")
if err != nil {
t.Error(err)
}
const expectedNumberOfThreadsInSampleFile = 9
if len(threadDump.Threads) != expectedNumberOfThreadsInSampleFile {
t.Errorf("got=[%d] threads, expected=[%d]", len(threadDump.Threads), expectedNumberOfThreadsInSampleFile)
}
type testCase struct {
threadName string
isDaemon bool
threadID string
nativeID string
state string
stackTrace string
}
test := []testCase{
{
threadName: `Reference Handler`,
isDaemon: true,
threadID: `0x00007f195c29c000`,
nativeID: `0xb07f`,
state: `RUNNABLE`,
stackTrace: `at java.lang.ref.Reference.waitForReferencePendingList([email protected]/Native Method)
at java.lang.ref.Reference.processPendingReferences([email protected]/Reference.java:241)
at java.lang.ref.Reference$ReferenceHandler.run([email protected]/Reference.java:213)
`,
},
{
threadName: `VM Thread`,
isDaemon: false,
threadID: `0x00007f195c299000`,
nativeID: `0xb07e`,
state: `runnable`,
stackTrace: ``,
},
{
threadName: `GC Thread#7`,
isDaemon: false,
threadID: `0x00007f191400a800`,
nativeID: `0xb091`,
state: `runnable`,
stackTrace: ``,
},
{
threadName: `G1 Main Marker`,
isDaemon: false,
threadID: `0x00007f195c08c000`,
nativeID: `0xb07a`,
state: `runnable`,
stackTrace: ``,
},
{
threadName: `G1 Conc#0`,
isDaemon: false,
threadID: `0x00007f195c08d800`,
nativeID: `0xb07b`,
state: `runnable`,
stackTrace: ``,
},
{
threadName: `G1 Conc#1`,
isDaemon: false,
threadID: `0x00007f1924001000`,
nativeID: `0xb097`,
state: `runnable`,
stackTrace: ``,
},
{
threadName: `G1 Refine#0`,
isDaemon: false,
threadID: `0x00007f195c20c000`,
nativeID: `0xb07c`,
state: `runnable`,
stackTrace: ``,
},
{
threadName: `G1 Young RemSet Sampling`,
isDaemon: false,
threadID: `0x00007f195c20d800`,
nativeID: `0xb07d`,
state: `runnable`,
stackTrace: ``,
},
{
threadName: `VM Periodic Task Thread`,
isDaemon: false,
threadID: `0x00007f195c30f000`,
nativeID: `0xb087`,
state: `waiting on condition`,
stackTrace: ``,
},
}
for i := 0; i < expectedNumberOfThreadsInSampleFile; i++ {
got := threadDump.Threads[i]
if got.Name != test[i].threadName {
t.Errorf("got=[%s], want=[%s]", got.Name, test[i].threadName)
}
if got.Daemon != test[i].isDaemon {
t.Errorf("got=[%t], want=[%t]", got.Daemon, test[i].isDaemon)
}
if got.ID != test[i].threadID {
t.Errorf("got=[%s], want=[%s]", got.ID, test[i].threadID)
}
if got.NativeID != test[i].nativeID {
t.Errorf("got=[%s], want=[%s]", got.NativeID, test[i].nativeID)
}
if got.State != test[i].state {
t.Errorf("got=[%s], want=[%s]", got.State, test[i].state)
}
if got.StackTrace != test[i].stackTrace {
t.Errorf("got=[\n{%s}\n], want=[\n{%s}\n]", got.StackTrace, test[i].stackTrace)
}
}
}
func TestExtractSynchronizers(t *testing.T) {
type testCase struct {
stacktrace string
want []Synchronizer
}
tests := []testCase{
{
stacktrace: `at jdk.internal.misc.Unsafe.park([email protected]/Native Method)
- parking to wait for <0x000000060dc25418> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.park([email protected]/LockSupport.java:194)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await([email protected]/AbstractQueuedSynchronizer.java:2081)
at java.util.concurrent.LinkedBlockingQueue.take([email protected]/LinkedBlockingQueue.java:433)
at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:107)
at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:33)
at java.util.concurrent.ThreadPoolExecutor.getTask([email protected]/ThreadPoolExecutor.java:1054)
at java.util.concurrent.ThreadPoolExecutor.runWorker([email protected]/ThreadPoolExecutor.java:1114)
at java.util.concurrent.ThreadPoolExecutor$Worker.run([email protected]/ThreadPoolExecutor.java:628)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run([email protected]/Thread.java:830)
Locked ownable synchronizers:
- None`,
want: []Synchronizer{
{
ID: `0x000000060dc25418`,
ObjectName: `java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject`,
State: ParkingToWaitForState,
},
},
},
{
stacktrace: ` at sun.nio.ch.EPoll.wait([email protected]/Native Method)
at sun.nio.ch.EPollSelectorImpl.doSelect([email protected]/EPollSelectorImpl.java:120)
at sun.nio.ch.SelectorImpl.lockAndDoSelect([email protected]/SelectorImpl.java:124)
- locked <0x000000060dc3e448> (a sun.nio.ch.Util$2)
- locked <0x000000060dc3e3f0> (a sun.nio.ch.EPollSelectorImpl)
at sun.nio.ch.SelectorImpl.select([email protected]/SelectorImpl.java:136)
at org.apache.tomcat.util.net.NioEndpoint$Poller.run(NioEndpoint.java:708)
at java.lang.Thread.run([email protected]/Thread.java:830)
`,
want: []Synchronizer{
{
ID: `0x000000060dc3e448`,
ObjectName: `sun.nio.ch.Util$2`,
State: LockedState,
},
{
ID: `0x000000060dc3e3f0`,
ObjectName: `sun.nio.ch.EPollSelectorImpl`,
State: LockedState,
},
},
},
{
stacktrace: ` at com.thdump.calls.Call5.hello(Call5.java:9)
- waiting to lock <0x000000060dc32098> (a java.lang.Class for com.thdump.calls.Call5)
at com.thdump.calls.Call4.hello(Call4.java:14)
- locked <0x000000062bac2040> (a java.lang.Class for com.thdump.calls.Call4)
at com.thdump.calls.Call3.hello(Call3.java:14)
- locked <0x000000062babf8a8> (a java.lang.Class for com.thdump.calls.Call3)
at com.thdump.calls.Call2.hello(Call2.java:14)
- locked <0x000000062babd110> (a java.lang.Class for com.thdump.calls.Call2)
at com.thdump.calls.Call1.hello(Call1.java:14)
- locked <0x000000062baba978> (a java.lang.Class for com.thdump.calls.Call1)
at com.thdump.web.SlowEndpoints.hello(SlowEndpoints.java:15)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0([email protected]/Native Method)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
- locked <0x000000060dc11ce0> (a org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper)
at java.util.concurrent.ThreadPoolExecutor.runWorker([email protected]/ThreadPoolExecutor.java:1128)
at java.util.concurrent.ThreadPoolExecutor$Worker.run([email protected]/ThreadPoolExecutor.java:628)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run([email protected]/Thread.java:830)`,
want: []Synchronizer{
{
ID: `0x000000060dc32098`,
ObjectName: `java.lang.Class for com.thdump.calls.Call5`,
State: WaitingToLockState,
},
{
ID: `0x000000062bac2040`,
ObjectName: `java.lang.Class for com.thdump.calls.Call4`,
State: LockedState,
},
{
ID: `0x000000062babf8a8`,
ObjectName: `java.lang.Class for com.thdump.calls.Call3`,
State: LockedState,
},
{
ID: `0x000000062babd110`,
ObjectName: `java.lang.Class for com.thdump.calls.Call2`,
State: LockedState,
},
{
ID: `0x000000062baba978`,
ObjectName: `java.lang.Class for com.thdump.calls.Call1`,
State: LockedState,
},
{
ID: `0x000000060dc11ce0`,
ObjectName: `org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper`,
State: LockedState,
},
},
},
}
for _, tc := range tests {
got := extractSynchronizers(tc.stacktrace)
if len(tc.want) != len(got) {
t.Errorf("expected=[%d] synchronizers, got=[%d]", len(tc.want), len(got))
}
if !equalSyncs(got, tc.want) {
t.Errorf("got=[%q], want=[%q]", got, tc.want)
}
}
}
func TestEqual(t *testing.T) {
type testCase struct {
a, b []Synchronizer
result bool
}
tests := []testCase{
{
a: []Synchronizer{
{
ID: "a",
ObjectName: "o1",
State: LockedState,
},
{
ID: "b",
ObjectName: "o1",
State: ParkingToWaitForState,
},
},
b: []Synchronizer{
{
ID: "a",
ObjectName: "o1",
State: LockedState,
},
{
ID: "b",
ObjectName: "o1",
State: ParkingToWaitForState,
},
},
result: true,
},
{
a: []Synchronizer{},
b: []Synchronizer{
{
ID: "a",
ObjectName: "o1",
State: LockedState,
},
{
ID: "b",
ObjectName: "o2",
State: ParkingToWaitForState,
},
},
result: false,
},
{
a: []Synchronizer{},
b: []Synchronizer{},
result: true,
},
{
a: []Synchronizer{
{
ID: `x`,
ObjectName: `y`,
State: LockedState,
},
},
b: []Synchronizer{},
result: false,
},
{
a: []Synchronizer{
{
ID: `1`,
ObjectName: `2`,
State: ParkingToWaitForState,
},
},
b: []Synchronizer{
{
ID: `2`,
ObjectName: `3`,
State: WaitingOnState,
},
},
result: false,
},
}
for _, tc := range tests {
if got := equalSyncs(tc.a, tc.b); got != tc.result {
t.Errorf("%q and %q should be equal", tc.a, tc.b)
}
}
}
func TestConvertToSynchronizerState(t *testing.T) {
type testCase struct {
textState string
want SynchronizerState
}
tests := []testCase{
{
textState: `waiting on`,
want: WaitingOnState,
},
{
textState: `parking to wait for`,
want: ParkingToWaitForState,
},
{
textState: `locked`,
want: LockedState,
},
{
textState: `abc`,
want: LockedState,
},
}
for _, tc := range tests {
got := convertToSyncState(tc.textState)
if got != tc.want {
t.Errorf("got=[%q], want=[%q]", got, tc.want)
}
}
}
func TestSynchronizersByThread(t *testing.T) {
threadDump, err := ParseFromFile("threaddumpsamples/13.0.2.0.txt")
if err != nil {
t.Error(err)
}
type testCase struct {
thread ThreadInfo
want []Synchronizer
}
tests := []testCase{
{
thread: ThreadInfo{
ID: `0x00007f4948cd6000`,
Daemon: true,
Priority: "5",
NativeID: `0xa360`,
State: "BLOCKED",
Name: `http-nio-8080-exec-1`,
StackTrace: `at com.thdump.calls.Call5.hello(Call5.java:9)
- waiting to lock <0x000000060dc32098> (a java.lang.Class for com.thdump.calls.Call5)
at com.thdump.calls.Call4.hello(Call4.java:14)
- locked <0x000000062bac2040> (a java.lang.Class for com.thdump.calls.Call4)
at com.thdump.calls.Call3.hello(Call3.java:14)
- locked <0x000000062babf8a8> (a java.lang.Class for com.thdump.calls.Call3)
at com.thdump.calls.Call2.hello(Call2.java:14)
- locked <0x000000062babd110> (a java.lang.Class for com.thdump.calls.Call2)
at com.thdump.calls.Call1.hello(Call1.java:14)
- locked <0x000000062baba978> (a java.lang.Class for com.thdump.calls.Call1)
at com.thdump.web.SlowEndpoints.hello(SlowEndpoints.java:15)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0([email protected]/Native Method)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke([email protected]/NativeMethodAccessorImpl.java:62)
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke([email protected]/DelegatingMethodAccessorImpl.java:43)