-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
lockstat.c
2005 lines (1710 loc) · 49.7 KB
/
lockstat.c
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
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/modctl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <dtrace.h>
#include <sys/lockstat.h>
#include <alloca.h>
#include <signal.h>
#include <assert.h>
#ifdef illumos
#define GETOPT_EOF EOF
#else
#include <sys/time.h>
#include <sys/resource.h>
#define mergesort(a, b, c, d) lsmergesort(a, b, c, d)
#define GETOPT_EOF (-1)
typedef uintptr_t pc_t;
#endif
#define LOCKSTAT_OPTSTR "x:bths:n:d:i:l:f:e:ckwWgCHEATID:RpPo:V"
#define LS_MAX_STACK_DEPTH 50
#define LS_MAX_EVENTS 64
typedef struct lsrec {
struct lsrec *ls_next; /* next in hash chain */
#ifdef illumos
uintptr_t ls_lock; /* lock address */
#else
char *ls_lock; /* lock name */
#endif
uintptr_t ls_caller; /* caller address */
uint32_t ls_count; /* cumulative event count */
uint32_t ls_event; /* type of event */
uintptr_t ls_refcnt; /* cumulative reference count */
uint64_t ls_time; /* cumulative event duration */
uint32_t ls_hist[64]; /* log2(duration) histogram */
uintptr_t ls_stack[LS_MAX_STACK_DEPTH];
} lsrec_t;
typedef struct lsdata {
struct lsrec *lsd_next; /* next available */
int lsd_count; /* number of records */
} lsdata_t;
/*
* Definitions for the types of experiments which can be run. They are
* listed in increasing order of memory cost and processing time cost.
* The numerical value of each type is the number of bytes needed per record.
*/
#define LS_BASIC offsetof(lsrec_t, ls_time)
#define LS_TIME offsetof(lsrec_t, ls_hist[0])
#define LS_HIST offsetof(lsrec_t, ls_stack[0])
#define LS_STACK(depth) offsetof(lsrec_t, ls_stack[depth])
static void report_stats(FILE *, lsrec_t **, size_t, uint64_t, uint64_t);
static void report_trace(FILE *, lsrec_t **);
extern int symtab_init(void);
extern char *addr_to_sym(uintptr_t, uintptr_t *, size_t *);
extern uintptr_t sym_to_addr(char *name);
extern size_t sym_size(char *name);
extern char *strtok_r(char *, const char *, char **);
#define DEFAULT_NRECS 10000
#define DEFAULT_HZ 97
#define MAX_HZ 1000
#define MIN_AGGSIZE (16 * 1024)
#define MAX_AGGSIZE (32 * 1024 * 1024)
static int g_stkdepth;
static int g_topn = INT_MAX;
static hrtime_t g_elapsed;
static int g_rates = 0;
static int g_pflag = 0;
static int g_Pflag = 0;
static int g_wflag = 0;
static int g_Wflag = 0;
static int g_cflag = 0;
static int g_kflag = 0;
static int g_gflag = 0;
static int g_Vflag = 0;
static int g_tracing = 0;
static size_t g_recsize;
static size_t g_nrecs;
static int g_nrecs_used;
static uchar_t g_enabled[LS_MAX_EVENTS];
static hrtime_t g_min_duration[LS_MAX_EVENTS];
static dtrace_hdl_t *g_dtp;
static char *g_predicate;
static char *g_ipredicate;
static char *g_prog;
static int g_proglen;
static int g_dropped;
typedef struct ls_event_info {
char ev_type;
char ev_lhdr[20];
char ev_desc[80];
char ev_units[10];
char ev_name[DTRACE_NAMELEN];
char *ev_predicate;
char *ev_acquire;
} ls_event_info_t;
static ls_event_info_t g_event_info[LS_MAX_EVENTS] = {
{ 'C', "Lock", "Adaptive mutex spin", "nsec",
"lockstat:::adaptive-spin" },
{ 'C', "Lock", "Adaptive mutex block", "nsec",
"lockstat:::adaptive-block" },
{ 'C', "Lock", "Spin lock spin", "nsec",
"lockstat:::spin-spin" },
{ 'C', "Lock", "Thread lock spin", "nsec",
"lockstat:::thread-spin" },
{ 'C', "Lock", "R/W writer blocked by writer", "nsec",
"lockstat:::rw-block", "arg2 == 0 && arg3 == 1" },
{ 'C', "Lock", "R/W writer blocked by readers", "nsec",
"lockstat:::rw-block", "arg2 == 0 && arg3 == 0 && arg4" },
{ 'C', "Lock", "R/W reader blocked by writer", "nsec",
"lockstat:::rw-block", "arg2 == 1 && arg3 == 1" },
{ 'C', "Lock", "R/W reader blocked by write wanted", "nsec",
"lockstat:::rw-block", "arg2 == 1 && arg3 == 0 && arg4" },
{ 'C', "Lock", "R/W writer spin on writer", "nsec",
"lockstat:::rw-spin", "arg2 == 0 && arg3 == 1" },
{ 'C', "Lock", "R/W writer spin on readers", "nsec",
"lockstat:::rw-spin", "arg2 == 0 && arg3 == 0 && arg4" },
{ 'C', "Lock", "R/W reader spin on writer", "nsec",
"lockstat:::rw-spin", "arg2 == 1 && arg3 == 1" },
{ 'C', "Lock", "R/W reader spin on write wanted", "nsec",
"lockstat:::rw-spin", "arg2 == 1 && arg3 == 0 && arg4" },
{ 'C', "Lock", "SX exclusive block", "nsec",
"lockstat:::sx-block", "arg2 == 0" },
{ 'C', "Lock", "SX shared block", "nsec",
"lockstat:::sx-block", "arg2 == 1" },
{ 'C', "Lock", "SX exclusive spin", "nsec",
"lockstat:::sx-spin", "arg2 == 0" },
{ 'C', "Lock", "SX shared spin", "nsec",
"lockstat:::sx-spin", "arg2 == 1" },
{ 'C', "Lock", "lockmgr writer blocked by writer", "nsec",
"lockstat:::lockmgr-block", "arg2 == 0 && arg3 == 1" },
{ 'C', "Lock", "lockmgr writer blocked by readers", "nsec",
"lockstat:::lockmgr-block", "arg2 == 0 && arg3 == 0 && arg4" },
{ 'C', "Lock", "lockmgr reader blocked by writer", "nsec",
"lockstat:::lockmgr-block", "arg2 == 1 && arg3 == 1" },
{ 'C', "Lock", "lockmgr reader blocked by write wanted", "nsec",
"lockstat:::lockmgr-block", "arg2 == 1 && arg3 == 0 && arg4" },
{ 'C', "Lock", "Unknown event (type 20)", "units" },
{ 'C', "Lock", "Unknown event (type 21)", "units" },
{ 'C', "Lock", "Unknown event (type 22)", "units" },
{ 'C', "Lock", "Unknown event (type 23)", "units" },
{ 'C', "Lock", "Unknown event (type 24)", "units" },
{ 'C', "Lock", "Unknown event (type 25)", "units" },
{ 'C', "Lock", "Unknown event (type 26)", "units" },
{ 'C', "Lock", "Unknown event (type 27)", "units" },
{ 'C', "Lock", "Unknown event (type 28)", "units" },
{ 'C', "Lock", "Unknown event (type 29)", "units" },
{ 'C', "Lock", "Unknown event (type 30)", "units" },
{ 'C', "Lock", "Unknown event (type 31)", "units" },
{ 'H', "Lock", "Adaptive mutex hold", "nsec",
"lockstat:::adaptive-release", NULL,
"lockstat:::adaptive-acquire" },
{ 'H', "Lock", "Spin lock hold", "nsec",
"lockstat:::spin-release", NULL,
"lockstat:::spin-acquire" },
{ 'H', "Lock", "R/W writer hold", "nsec",
"lockstat:::rw-release", "arg1 == 0",
"lockstat:::rw-acquire" },
{ 'H', "Lock", "R/W reader hold", "nsec",
"lockstat:::rw-release", "arg1 == 1",
"lockstat:::rw-acquire" },
{ 'H', "Lock", "SX shared hold", "nsec",
"lockstat:::sx-release", "arg1 == 1",
"lockstat:::sx-acquire" },
{ 'H', "Lock", "SX exclusive hold", "nsec",
"lockstat:::sx-release", "arg1 == 0",
"lockstat:::sx-acquire" },
{ 'H', "Lock", "lockmgr shared hold", "nsec",
"lockstat:::lockmgr-release", "arg1 == 1",
"lockstat:::lockmgr-acquire" },
{ 'H', "Lock", "lockmgr exclusive hold", "nsec",
"lockstat:::lockmgr-release,lockstat:::lockmgr-disown", "arg1 == 0",
"lockstat:::lockmgr-acquire" },
{ 'H', "Lock", "Unknown event (type 40)", "units" },
{ 'H', "Lock", "Unknown event (type 41)", "units" },
{ 'H', "Lock", "Unknown event (type 42)", "units" },
{ 'H', "Lock", "Unknown event (type 43)", "units" },
{ 'H', "Lock", "Unknown event (type 44)", "units" },
{ 'H', "Lock", "Unknown event (type 45)", "units" },
{ 'H', "Lock", "Unknown event (type 46)", "units" },
{ 'H', "Lock", "Unknown event (type 47)", "units" },
{ 'H', "Lock", "Unknown event (type 48)", "units" },
{ 'H', "Lock", "Unknown event (type 49)", "units" },
{ 'H', "Lock", "Unknown event (type 50)", "units" },
{ 'H', "Lock", "Unknown event (type 51)", "units" },
{ 'H', "Lock", "Unknown event (type 52)", "units" },
{ 'H', "Lock", "Unknown event (type 53)", "units" },
{ 'H', "Lock", "Unknown event (type 54)", "units" },
{ 'H', "Lock", "Unknown event (type 55)", "units" },
#ifdef illumos
{ 'I', "CPU+PIL", "Profiling interrupt", "nsec",
#else
{ 'I', "CPU+Pri_Class", "Profiling interrupt", "nsec",
#endif
"profile:::profile-97", NULL },
{ 'I', "Lock", "Unknown event (type 57)", "units" },
{ 'I', "Lock", "Unknown event (type 58)", "units" },
{ 'I', "Lock", "Unknown event (type 59)", "units" },
{ 'E', "Lock", "Recursive lock entry detected", "(N/A)",
"lockstat:::rw-release", NULL, "lockstat:::rw-acquire" },
{ 'E', "Lock", "Lockstat enter failure", "(N/A)" },
{ 'E', "Lock", "Lockstat exit failure", "nsec" },
{ 'E', "Lock", "Lockstat record failure", "(N/A)" },
};
#ifndef illumos
static char *g_pri_class[] = {
"",
"Intr",
"RealT",
"TShar",
"Idle"
};
#endif
static void
fail(int do_perror, const char *message, ...)
{
va_list args;
int save_errno = errno;
va_start(args, message);
(void) fprintf(stderr, "lockstat: ");
(void) vfprintf(stderr, message, args);
va_end(args);
if (do_perror)
(void) fprintf(stderr, ": %s", strerror(save_errno));
(void) fprintf(stderr, "\n");
exit(2);
}
static void
dfail(const char *message, ...)
{
va_list args;
va_start(args, message);
(void) fprintf(stderr, "lockstat: ");
(void) vfprintf(stderr, message, args);
va_end(args);
(void) fprintf(stderr, ": %s\n",
dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
exit(2);
}
static void
show_events(char event_type, char *desc)
{
int i, first = -1, last;
for (i = 0; i < LS_MAX_EVENTS; i++) {
ls_event_info_t *evp = &g_event_info[i];
if (evp->ev_type != event_type ||
strncmp(evp->ev_desc, "Unknown event", 13) == 0)
continue;
if (first == -1)
first = i;
last = i;
}
(void) fprintf(stderr,
"\n%s events (lockstat -%c or lockstat -e %d-%d):\n\n",
desc, event_type, first, last);
for (i = first; i <= last; i++)
(void) fprintf(stderr,
"%4d = %s\n", i, g_event_info[i].ev_desc);
}
static void
usage(void)
{
(void) fprintf(stderr,
"Usage: lockstat [options] command [args]\n"
"\nGeneral options:\n\n"
" -V print the corresponding D program\n"
"\nEvent selection options:\n\n"
" -C watch contention events [on by default]\n"
" -E watch error events [off by default]\n"
" -H watch hold events [off by default]\n"
" -I watch interrupt events [off by default]\n"
" -A watch all lock events [equivalent to -CH]\n"
" -e event_list only watch the specified events (shown below);\n"
" <event_list> is a comma-separated list of\n"
" events or ranges of events, e.g. 1,4-7,35\n"
" -i rate interrupt rate for -I [default: %d Hz]\n"
"\nData gathering options:\n\n"
" -b basic statistics (lock, caller, event count)\n"
" -t timing for all events [default]\n"
" -h histograms for event times\n"
" -s depth stack traces <depth> deep\n"
" -x opt[=val] enable or modify DTrace options\n"
"\nData filtering options:\n\n"
" -n nrecords maximum number of data records [default: %d]\n"
" -l lock[,size] only watch <lock>, which can be specified as a\n"
" symbolic name or hex address; <size> defaults\n"
" to the ELF symbol size if available, 1 if not\n"
" -f func[,size] only watch events generated by <func>\n"
" -d duration only watch events longer than <duration>\n"
" -T trace (rather than sample) events\n"
"\nData reporting options:\n\n"
#ifdef illumos
" -c coalesce lock data for arrays like pse_mutex[]\n"
#endif
" -k coalesce PCs within functions\n"
" -g show total events generated by function\n"
" -w wherever: don't distinguish events by caller\n"
" -W whichever: don't distinguish events by lock\n"
" -R display rates rather than counts\n"
" -p parsable output format (awk(1)-friendly)\n"
" -P sort lock data by (count * avg_time) product\n"
" -D n only display top <n> events of each type\n"
" -o filename send output to <filename>\n",
DEFAULT_HZ, DEFAULT_NRECS);
show_events('C', "Contention");
show_events('H', "Hold-time");
show_events('I', "Interrupt");
show_events('E', "Error");
(void) fprintf(stderr, "\n");
exit(1);
}
static int
lockcmp(lsrec_t *a, lsrec_t *b)
{
int i;
if (a->ls_event < b->ls_event)
return (-1);
if (a->ls_event > b->ls_event)
return (1);
for (i = g_stkdepth - 1; i >= 0; i--) {
if (a->ls_stack[i] < b->ls_stack[i])
return (-1);
if (a->ls_stack[i] > b->ls_stack[i])
return (1);
}
if (a->ls_caller < b->ls_caller)
return (-1);
if (a->ls_caller > b->ls_caller)
return (1);
#ifdef illumos
if (a->ls_lock < b->ls_lock)
return (-1);
if (a->ls_lock > b->ls_lock)
return (1);
return (0);
#else
return (strcmp(a->ls_lock, b->ls_lock));
#endif
}
static int
countcmp(lsrec_t *a, lsrec_t *b)
{
if (a->ls_event < b->ls_event)
return (-1);
if (a->ls_event > b->ls_event)
return (1);
return (b->ls_count - a->ls_count);
}
static int
timecmp(lsrec_t *a, lsrec_t *b)
{
if (a->ls_event < b->ls_event)
return (-1);
if (a->ls_event > b->ls_event)
return (1);
if (a->ls_time < b->ls_time)
return (1);
if (a->ls_time > b->ls_time)
return (-1);
return (0);
}
static int
lockcmp_anywhere(lsrec_t *a, lsrec_t *b)
{
if (a->ls_event < b->ls_event)
return (-1);
if (a->ls_event > b->ls_event)
return (1);
#ifdef illumos
if (a->ls_lock < b->ls_lock)
return (-1);
if (a->ls_lock > b->ls_lock)
return (1);
return (0);
#else
return (strcmp(a->ls_lock, b->ls_lock));
#endif
}
static int
lock_and_count_cmp_anywhere(lsrec_t *a, lsrec_t *b)
{
#ifndef illumos
int cmp;
#endif
if (a->ls_event < b->ls_event)
return (-1);
if (a->ls_event > b->ls_event)
return (1);
#ifdef illumos
if (a->ls_lock < b->ls_lock)
return (-1);
if (a->ls_lock > b->ls_lock)
return (1);
#else
cmp = strcmp(a->ls_lock, b->ls_lock);
if (cmp != 0)
return (cmp);
#endif
return (b->ls_count - a->ls_count);
}
static int
sitecmp_anylock(lsrec_t *a, lsrec_t *b)
{
int i;
if (a->ls_event < b->ls_event)
return (-1);
if (a->ls_event > b->ls_event)
return (1);
for (i = g_stkdepth - 1; i >= 0; i--) {
if (a->ls_stack[i] < b->ls_stack[i])
return (-1);
if (a->ls_stack[i] > b->ls_stack[i])
return (1);
}
if (a->ls_caller < b->ls_caller)
return (-1);
if (a->ls_caller > b->ls_caller)
return (1);
return (0);
}
static int
site_and_count_cmp_anylock(lsrec_t *a, lsrec_t *b)
{
int i;
if (a->ls_event < b->ls_event)
return (-1);
if (a->ls_event > b->ls_event)
return (1);
for (i = g_stkdepth - 1; i >= 0; i--) {
if (a->ls_stack[i] < b->ls_stack[i])
return (-1);
if (a->ls_stack[i] > b->ls_stack[i])
return (1);
}
if (a->ls_caller < b->ls_caller)
return (-1);
if (a->ls_caller > b->ls_caller)
return (1);
return (b->ls_count - a->ls_count);
}
static void
lsmergesort(int (*cmp)(lsrec_t *, lsrec_t *), lsrec_t **a, lsrec_t **b, int n)
{
int m = n / 2;
int i, j;
if (m > 1)
lsmergesort(cmp, a, b, m);
if (n - m > 1)
lsmergesort(cmp, a + m, b + m, n - m);
for (i = m; i > 0; i--)
b[i - 1] = a[i - 1];
for (j = m - 1; j < n - 1; j++)
b[n + m - j - 2] = a[j + 1];
while (i < j)
*a++ = cmp(b[i], b[j]) < 0 ? b[i++] : b[j--];
*a = b[i];
}
static void
coalesce(int (*cmp)(lsrec_t *, lsrec_t *), lsrec_t **lock, int n)
{
int i, j;
lsrec_t *target, *current;
target = lock[0];
for (i = 1; i < n; i++) {
current = lock[i];
if (cmp(current, target) != 0) {
target = current;
continue;
}
current->ls_event = LS_MAX_EVENTS;
target->ls_count += current->ls_count;
target->ls_refcnt += current->ls_refcnt;
if (g_recsize < LS_TIME)
continue;
target->ls_time += current->ls_time;
if (g_recsize < LS_HIST)
continue;
for (j = 0; j < 64; j++)
target->ls_hist[j] += current->ls_hist[j];
}
}
static void
coalesce_symbol(uintptr_t *addrp)
{
uintptr_t symoff;
size_t symsize;
if (addr_to_sym(*addrp, &symoff, &symsize) != NULL && symoff < symsize)
*addrp -= symoff;
}
static void
predicate_add(char **pred, char *what, char *cmp, uintptr_t value)
{
char *new;
int len, newlen;
if (what == NULL)
return;
if (*pred == NULL) {
*pred = malloc(1);
*pred[0] = '\0';
}
len = strlen(*pred);
newlen = len + strlen(what) + 32 + strlen("( && )");
new = malloc(newlen);
if (*pred[0] != '\0') {
if (cmp != NULL) {
(void) sprintf(new, "(%s) && (%s %s %p)",
*pred, what, cmp, (void *)value);
} else {
(void) sprintf(new, "(%s) && (%s)", *pred, what);
}
} else {
if (cmp != NULL) {
(void) sprintf(new, "%s %s %p",
what, cmp, (void *)value);
} else {
(void) sprintf(new, "%s", what);
}
}
free(*pred);
*pred = new;
}
static void
predicate_destroy(char **pred)
{
free(*pred);
*pred = NULL;
}
static void
filter_add(char **filt, char *what, uintptr_t base, size_t size)
{
char buf[256], *c = buf, *new;
int len, newlen;
if (*filt == NULL) {
*filt = malloc(1);
*filt[0] = '\0';
}
#ifdef illumos
(void) sprintf(c, "%s(%s >= 0x%p && %s < 0x%p)", *filt[0] != '\0' ?
" || " : "", what, (void *)base, what, (void *)(base + size));
#else
(void) sprintf(c, "%s(%s >= %p && %s < %p)", *filt[0] != '\0' ?
" || " : "", what, (void *)base, what, (void *)(base + size));
#endif
newlen = (len = strlen(*filt) + 1) + strlen(c);
new = malloc(newlen);
bcopy(*filt, new, len);
(void) strcat(new, c);
free(*filt);
*filt = new;
}
static void
filter_destroy(char **filt)
{
free(*filt);
*filt = NULL;
}
static void
dprog_add(const char *fmt, ...)
{
va_list args;
int size, offs;
char c;
va_start(args, fmt);
size = vsnprintf(&c, 1, fmt, args) + 1;
va_end(args);
if (g_proglen == 0) {
offs = 0;
} else {
offs = g_proglen - 1;
}
g_proglen = offs + size;
if ((g_prog = realloc(g_prog, g_proglen)) == NULL)
fail(1, "failed to reallocate program text");
va_start(args, fmt);
(void) vsnprintf(&g_prog[offs], size, fmt, args);
va_end(args);
}
/*
* This function may read like an open sewer, but keep in mind that programs
* that generate other programs are rarely pretty. If one has the unenviable
* task of maintaining or -- worse -- extending this code, use the -V option
* to examine the D program as generated by this function.
*/
static void
dprog_addevent(int event)
{
ls_event_info_t *info = &g_event_info[event];
char *pred = NULL;
char stack[20];
const char *arg0, *caller;
char *arg1 = "arg1";
char buf[80];
hrtime_t dur;
int depth;
if (info->ev_name[0] == '\0')
return;
if (info->ev_type == 'I') {
/*
* For interrupt events, arg0 (normally the lock pointer) is
* the CPU address plus the current pil, and arg1 (normally
* the number of nanoseconds) is the number of nanoseconds
* late -- and it's stored in arg2.
*/
#ifdef illumos
arg0 = "(uintptr_t)curthread->t_cpu + \n"
"\t curthread->t_cpu->cpu_profile_pil";
#else
arg0 = "(uintptr_t)(curthread->td_oncpu << 16) + \n"
"\t 0x01000000 + curthread->td_pri_class";
#endif
caller = "(uintptr_t)arg0";
arg1 = "arg2";
} else {
#ifdef illumos
arg0 = "(uintptr_t)arg0";
#else
arg0 = "stringof(args[0]->lock_object.lo_name)";
#endif
caller = "caller";
}
if (g_recsize > LS_HIST) {
for (depth = 0; g_recsize > LS_STACK(depth); depth++)
continue;
if (g_tracing) {
(void) sprintf(stack, "\tstack(%d);\n", depth);
} else {
(void) sprintf(stack, ", stack(%d)", depth);
}
} else {
(void) sprintf(stack, "");
}
if (info->ev_acquire != NULL) {
/*
* If this is a hold event, we need to generate an additional
* clause for the acquire; the clause for the release will be
* generated with the aggregating statement, below.
*/
dprog_add("%s\n", info->ev_acquire);
predicate_add(&pred, info->ev_predicate, NULL, 0);
predicate_add(&pred, g_predicate, NULL, 0);
if (pred != NULL)
dprog_add("/%s/\n", pred);
dprog_add("{\n");
(void) sprintf(buf, "self->ev%d[(uintptr_t)arg0]", event);
if (info->ev_type == 'H') {
dprog_add("\t%s = timestamp;\n", buf);
} else {
/*
* If this isn't a hold event, it's the recursive
* error event. For this, we simply bump the
* thread-local, per-lock count.
*/
dprog_add("\t%s++;\n", buf);
}
dprog_add("}\n\n");
predicate_destroy(&pred);
pred = NULL;
if (info->ev_type == 'E') {
/*
* If this is the recursive lock error event, we need
* to generate an additional clause to decrement the
* thread-local, per-lock count. This assures that we
* only execute the aggregating clause if we have
* recursive entry.
*/
dprog_add("%s\n", info->ev_name);
dprog_add("/%s/\n{\n\t%s--;\n}\n\n", buf, buf);
}
predicate_add(&pred, buf, NULL, 0);
if (info->ev_type == 'H') {
(void) sprintf(buf, "timestamp -\n\t "
"self->ev%d[(uintptr_t)arg0]", event);
}
arg1 = buf;
} else {
predicate_add(&pred, info->ev_predicate, NULL, 0);
if (info->ev_type != 'I')
predicate_add(&pred, g_predicate, NULL, 0);
else
predicate_add(&pred, g_ipredicate, NULL, 0);
}
if ((dur = g_min_duration[event]) != 0)
predicate_add(&pred, arg1, ">=", dur);
dprog_add("%s\n", info->ev_name);
if (pred != NULL)
dprog_add("/%s/\n", pred);
predicate_destroy(&pred);
dprog_add("{\n");
if (g_tracing) {
dprog_add("\ttrace(%dULL);\n", event);
dprog_add("\ttrace(%s);\n", arg0);
dprog_add("\ttrace(%s);\n", caller);
dprog_add(stack);
} else {
/*
* The ordering here is important: when we process the
* aggregate, we count on the fact that @avg appears before
* @hist in program order to assure that @avg is assigned the
* first aggregation variable ID and @hist assigned the
* second; see the comment in process_aggregate() for details.
*/
dprog_add("\t@avg[%dULL, %s, %s%s] = avg(%s);\n",
event, arg0, caller, stack, arg1);
if (g_recsize >= LS_HIST) {
dprog_add("\t@hist[%dULL, %s, %s%s] = quantize"
"(%s);\n", event, arg0, caller, stack, arg1);
}
}
if (info->ev_acquire != NULL)
dprog_add("\tself->ev%d[arg0] = 0;\n", event);
dprog_add("}\n\n");
}
static void
dprog_compile()
{
dtrace_prog_t *prog;
dtrace_proginfo_t info;
if (g_Vflag) {
(void) fprintf(stderr, "lockstat: vvvv D program vvvv\n");
(void) fputs(g_prog, stderr);
(void) fprintf(stderr, "lockstat: ^^^^ D program ^^^^\n");
}
if ((prog = dtrace_program_strcompile(g_dtp, g_prog,
DTRACE_PROBESPEC_NAME, 0, 0, NULL)) == NULL)
dfail("failed to compile program");
if (dtrace_program_exec(g_dtp, prog, &info) == -1)
dfail("failed to enable probes");
if (dtrace_go(g_dtp) != 0)
dfail("couldn't start tracing");
}
static void
#ifdef illumos
status_fire(void)
#else
status_fire(int i)
#endif
{}
static void
status_init(void)
{
dtrace_optval_t val, status, agg;
struct sigaction act;
struct itimerspec ts;
struct sigevent ev;
timer_t tid;
if (dtrace_getopt(g_dtp, "statusrate", &status) == -1)
dfail("failed to get 'statusrate'");
if (dtrace_getopt(g_dtp, "aggrate", &agg) == -1)
dfail("failed to get 'statusrate'");
/*
* We would want to awaken at a rate that is the GCD of the statusrate
* and the aggrate -- but that seems a bit absurd. Instead, we'll
* simply awaken at a rate that is the more frequent of the two, which
* assures that we're never later than the interval implied by the
* more frequent rate.
*/
val = status < agg ? status : agg;
(void) sigemptyset(&act.sa_mask);
act.sa_flags = 0;
act.sa_handler = status_fire;
(void) sigaction(SIGUSR1, &act, NULL);
ev.sigev_notify = SIGEV_SIGNAL;
ev.sigev_signo = SIGUSR1;
if (timer_create(CLOCK_REALTIME, &ev, &tid) == -1)
dfail("cannot create CLOCK_REALTIME timer");
ts.it_value.tv_sec = val / NANOSEC;
ts.it_value.tv_nsec = val % NANOSEC;
ts.it_interval = ts.it_value;
if (timer_settime(tid, TIMER_RELTIME, &ts, NULL) == -1)
dfail("cannot set time on CLOCK_REALTIME timer");
}
static void
status_check(void)
{
if (!g_tracing && dtrace_aggregate_snap(g_dtp) != 0)
dfail("failed to snap aggregate");
if (dtrace_status(g_dtp) == -1)
dfail("dtrace_status()");
}
static void
lsrec_fill(lsrec_t *lsrec, const dtrace_recdesc_t *rec, int nrecs, caddr_t data)
{
bzero(lsrec, g_recsize);
lsrec->ls_count = 1;
if ((g_recsize > LS_HIST && nrecs < 4) || (nrecs < 3))
fail(0, "truncated DTrace record");
if (rec->dtrd_size != sizeof (uint64_t))
fail(0, "bad event size in first record");
/* LINTED - alignment */
lsrec->ls_event = (uint32_t)*((uint64_t *)(data + rec->dtrd_offset));
rec++;
#ifdef illumos
if (rec->dtrd_size != sizeof (uintptr_t))
fail(0, "bad lock address size in second record");
/* LINTED - alignment */
lsrec->ls_lock = *((uintptr_t *)(data + rec->dtrd_offset));
rec++;
#else
lsrec->ls_lock = strdup((const char *)(data + rec->dtrd_offset));
rec++;
#endif
if (rec->dtrd_size != sizeof (uintptr_t))
fail(0, "bad caller size in third record");
/* LINTED - alignment */
lsrec->ls_caller = *((uintptr_t *)(data + rec->dtrd_offset));
rec++;
if (g_recsize > LS_HIST) {
int frames, i;
pc_t *stack;
frames = rec->dtrd_size / sizeof (pc_t);
/* LINTED - alignment */
stack = (pc_t *)(data + rec->dtrd_offset);
for (i = 1; i < frames; i++)
lsrec->ls_stack[i - 1] = stack[i];
}
}
/*ARGSUSED*/
static int
count_aggregate(const dtrace_aggdata_t *agg, void *arg)
{
*((size_t *)arg) += 1;
return (DTRACE_AGGWALK_NEXT);
}
static int
process_aggregate(const dtrace_aggdata_t *agg, void *arg)
{
const dtrace_aggdesc_t *aggdesc = agg->dtada_desc;
caddr_t data = agg->dtada_data;
lsdata_t *lsdata = arg;
lsrec_t *lsrec = lsdata->lsd_next;
const dtrace_recdesc_t *rec;
uint64_t *avg, *quantized;