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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
|
/*
* 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.
* Copyright (c) 2014, Joyent, Inc. All rights reserved.
*/
#include <stdio.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <fcntl.h>
#include <string.h>
#include <dirent.h>
#include <limits.h>
#include <link.h>
#include <libelf.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/mkdev.h>
#include <sys/mman.h>
#include <sys/lgrp_user.h>
#include <libproc.h>
#include "ptools_common.h"
#include "pmap_common.h"
#define KILOBYTE 1024
#define MEGABYTE (KILOBYTE * KILOBYTE)
#define GIGABYTE (KILOBYTE * KILOBYTE * KILOBYTE)
/*
* Round up the value to the nearest kilobyte
*/
#define ROUNDUP_KB(x) (((x) + (KILOBYTE - 1)) / KILOBYTE)
/*
* The alignment should be a power of 2.
*/
#define P2ALIGN(x, align) ((x) & -(align))
#define INVALID_ADDRESS (uintptr_t)(-1)
struct totals {
ulong_t total_size;
ulong_t total_swap;
ulong_t total_rss;
ulong_t total_anon;
ulong_t total_locked;
};
/*
* -L option requires per-page information. The information is presented in an
* array of page_descr structures.
*/
typedef struct page_descr {
uintptr_t pd_start; /* start address of a page */
size_t pd_pagesize; /* page size in bytes */
lgrp_id_t pd_lgrp; /* lgroup of memory backing the page */
int pd_valid; /* valid page description if non-zero */
} page_descr_t;
/*
* Per-page information for a memory chunk.
* The meminfo(2) system call accepts up to MAX_MEMINFO_CNT pages at once.
* When we need to scan larger ranges we divide them in MAX_MEMINFO_CNT sized
* chunks. The chunk information is stored in the memory_chunk structure.
*/
typedef struct memory_chunk {
page_descr_t page_info[MAX_MEMINFO_CNT];
uintptr_t end_addr;
uintptr_t chunk_start; /* Starting address */
uintptr_t chunk_end; /* chunk_end is always <= end_addr */
size_t page_size;
int page_index; /* Current page */
int page_count; /* Number of pages */
} memory_chunk_t;
static volatile int interrupt;
typedef int proc_xmap_f(void *, const prxmap_t *, const char *, int, int);
static int xmapping_iter(struct ps_prochandle *, proc_xmap_f *, void *,
int);
static int rmapping_iter(struct ps_prochandle *, proc_map_f *, void *);
static int look_map(void *, const prmap_t *, const char *);
static int look_smap(void *, const prxmap_t *, const char *, int, int);
static int look_xmap(void *, const prxmap_t *, const char *, int, int);
static int look_xmap_nopgsz(void *, const prxmap_t *, const char *,
int, int);
static int gather_map(void *, const prmap_t *, const char *);
static int gather_xmap(void *, const prxmap_t *, const char *, int, int);
static int iter_map(proc_map_f *, void *);
static int iter_xmap(proc_xmap_f *, void *);
static int parse_addr_range(char *, uintptr_t *, uintptr_t *);
static void mem_chunk_init(memory_chunk_t *, uintptr_t, size_t);
static int perr(char *);
static void printK(long, int);
static char *mflags(uint_t);
static size_t get_contiguous_region(memory_chunk_t *, uintptr_t,
uintptr_t, size_t, lgrp_id_t *);
static void mem_chunk_get(memory_chunk_t *, uintptr_t);
static lgrp_id_t addr_to_lgrp(memory_chunk_t *, uintptr_t, size_t *);
static char *lgrp2str(lgrp_id_t);
static int address_in_range(uintptr_t, uintptr_t, size_t);
static size_t adjust_addr_range(uintptr_t, uintptr_t, size_t,
uintptr_t *, uintptr_t *);
static int lflag = 0;
static int Lflag = 0;
static int aflag = 0;
/*
* The -A address range is represented as a pair of addresses
* <start_addr, end_addr>. Either one of these may be unspecified (set to
* INVALID_ADDRESS). If both are unspecified, no address range restrictions are
* in place.
*/
static uintptr_t start_addr = INVALID_ADDRESS;
static uintptr_t end_addr = INVALID_ADDRESS;
static int addr_width, size_width;
static char *command;
static char *procname;
static struct ps_prochandle *Pr;
static void intr(int);
typedef struct {
prxmap_t md_xmap;
prmap_t md_map;
char *md_objname;
boolean_t md_last;
int md_doswap;
} mapdata_t;
static mapdata_t *maps;
static int map_count;
static int map_alloc;
static lwpstack_t *stacks = NULL;
static uint_t nstacks = 0;
#define MAX_TRIES 5
static int
getstack(void *data, const lwpstatus_t *lsp)
{
int *np = (int *)data;
if (Plwp_alt_stack(Pr, lsp->pr_lwpid, &stacks[*np].lwps_stack) == 0) {
stacks[*np].lwps_stack.ss_flags |= SS_ONSTACK;
stacks[*np].lwps_lwpid = lsp->pr_lwpid;
(*np)++;
}
if (Plwp_main_stack(Pr, lsp->pr_lwpid, &stacks[*np].lwps_stack) == 0) {
stacks[*np].lwps_lwpid = lsp->pr_lwpid;
(*np)++;
}
return (0);
}
int
main(int argc, char **argv)
{
int rflag = 0, sflag = 0, xflag = 0, Fflag = 0;
int errflg = 0, Sflag = 0;
int rc = 0;
int opt;
const char *bar8 = "-------";
const char *bar16 = "----------";
const char *bar;
struct rlimit rlim;
struct stat64 statbuf;
char buf[PATH_MAX];
int mapfd;
int prg_gflags = PGRAB_RDONLY;
int prr_flags = 0;
boolean_t use_agent_lwp = B_FALSE;
if ((command = strrchr(argv[0], '/')) != NULL)
command++;
else
command = argv[0];
while ((opt = getopt(argc, argv, "arsxSlLFA:")) != EOF) {
switch (opt) {
case 'a': /* include shared mappings in -[xS] */
aflag = 1;
break;
case 'r': /* show reserved mappings */
rflag = 1;
break;
case 's': /* show hardware page sizes */
sflag = 1;
break;
case 'S': /* show swap reservations */
Sflag = 1;
break;
case 'x': /* show extended mappings */
xflag = 1;
break;
case 'l': /* show unresolved link map names */
lflag = 1;
break;
case 'L': /* show lgroup information */
Lflag = 1;
use_agent_lwp = B_TRUE;
break;
case 'F': /* force grabbing (no O_EXCL) */
Fflag = PGRAB_FORCE;
break;
case 'A':
if (parse_addr_range(optarg, &start_addr, &end_addr)
!= 0)
errflg++;
break;
default:
errflg = 1;
break;
}
}
argc -= optind;
argv += optind;
if ((Sflag && (xflag || rflag || sflag)) || (xflag && rflag) ||
(aflag && (!xflag && !Sflag)) ||
(Lflag && (xflag || Sflag))) {
errflg = 1;
}
if (errflg || argc <= 0) {
(void) fprintf(stderr,
"usage:\t%s [-rslF] [-A start[,end]] { pid | core } ...\n",
command);
(void) fprintf(stderr,
"\t\t(report process address maps)\n");
(void) fprintf(stderr,
"\t%s -L [-rslF] [-A start[,end]] pid ...\n", command);
(void) fprintf(stderr,
"\t\t(report process address maps lgroups mappings)\n");
(void) fprintf(stderr,
"\t%s -x [-aslF] [-A start[,end]] pid ...\n", command);
(void) fprintf(stderr,
"\t\t(show resident/anon/locked mapping details)\n");
(void) fprintf(stderr,
"\t%s -S [-alF] [-A start[,end]] { pid | core } ...\n",
command);
(void) fprintf(stderr,
"\t\t(show swap reservations)\n\n");
(void) fprintf(stderr,
"\t-a: include shared mappings in -[xS] summary\n");
(void) fprintf(stderr,
"\t-r: show reserved address maps\n");
(void) fprintf(stderr,
"\t-s: show hardware page sizes\n");
(void) fprintf(stderr,
"\t-l: show unresolved dynamic linker map names\n");
(void) fprintf(stderr,
"\t-F: force grabbing of the target process\n");
(void) fprintf(stderr,
"\t-L: show lgroup mappings\n");
(void) fprintf(stderr,
"\t-A start,end: limit output to the specified range\n");
return (2);
}
/*
* Make sure we'll have enough file descriptors to handle a target
* that has many many mappings.
*/
if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
rlim.rlim_cur = rlim.rlim_max;
(void) setrlimit(RLIMIT_NOFILE, &rlim);
(void) enable_extended_FILE_stdio(-1, -1);
}
/*
* The implementation of -L option creates an agent LWP in the target
* process address space. The agent LWP issues meminfo(2) system calls
* on behalf of the target process. If we are interrupted prematurely,
* the target process remains in the stopped state with the agent still
* attached to it. To prevent such situation we catch signals from
* terminal and terminate gracefully.
*/
if (use_agent_lwp) {
/*
* Buffer output to stdout, stderr while process is grabbed.
* Prevents infamous deadlocks due to pmap `pgrep xterm` and
* other variants.
*/
(void) proc_initstdio();
prg_gflags = PGRAB_RETAIN | Fflag;
prr_flags = PRELEASE_RETAIN;
if (sigset(SIGHUP, SIG_IGN) == SIG_DFL)
(void) sigset(SIGHUP, intr);
if (sigset(SIGINT, SIG_IGN) == SIG_DFL)
(void) sigset(SIGINT, intr);
if (sigset(SIGQUIT, SIG_IGN) == SIG_DFL)
(void) sigset(SIGQUIT, intr);
(void) sigset(SIGPIPE, intr);
(void) sigset(SIGTERM, intr);
}
while (argc-- > 0) {
char *arg;
int gcode;
psinfo_t psinfo;
int tries = 0;
if (use_agent_lwp)
(void) proc_flushstdio();
if ((Pr = proc_arg_grab(arg = *argv++, PR_ARG_ANY,
prg_gflags, &gcode)) == NULL) {
(void) fprintf(stderr, "%s: cannot examine %s: %s\n",
command, arg, Pgrab_error(gcode));
rc++;
continue;
}
procname = arg; /* for perr() */
addr_width = (Pstatus(Pr)->pr_dmodel == PR_MODEL_LP64) ? 16 : 8;
size_width = (Pstatus(Pr)->pr_dmodel == PR_MODEL_LP64) ? 11 : 8;
bar = addr_width == 8 ? bar8 : bar16;
(void) memcpy(&psinfo, Ppsinfo(Pr), sizeof (psinfo_t));
proc_unctrl_psinfo(&psinfo);
if (Pstate(Pr) != PS_DEAD) {
(void) proc_snprintf(buf, sizeof (buf),
"/proc/%d/map", (int)psinfo.pr_pid);
if ((mapfd = open(buf, O_RDONLY)) < 0) {
(void) fprintf(stderr, "%s: cannot "
"examine %s: lost control of "
"process\n", command, arg);
rc++;
Prelease(Pr, prr_flags);
continue;
}
} else {
mapfd = -1;
}
again:
map_count = 0;
if (Pstate(Pr) == PS_DEAD) {
(void) printf("core '%s' of %d:\t%.70s\n",
arg, (int)psinfo.pr_pid, psinfo.pr_psargs);
if (rflag || sflag || xflag || Sflag || Lflag) {
(void) printf(" -%c option is not compatible "
"with core files\n", xflag ? 'x' :
sflag ? 's' : rflag ? 'r' :
Lflag ? 'L' : 'S');
Prelease(Pr, prr_flags);
rc++;
continue;
}
} else {
(void) printf("%d:\t%.70s\n",
(int)psinfo.pr_pid, psinfo.pr_psargs);
}
if (!(Pstatus(Pr)->pr_flags & PR_ISSYS)) {
struct totals t;
/*
* Since we're grabbing the process readonly, we need
* to make sure the address space doesn't change during
* execution.
*/
if (Pstate(Pr) != PS_DEAD) {
if (tries++ == MAX_TRIES) {
Prelease(Pr, prr_flags);
(void) close(mapfd);
(void) fprintf(stderr, "%s: cannot "
"examine %s: address space is "
"changing\n", command, arg);
continue;
}
if (fstat64(mapfd, &statbuf) != 0) {
Prelease(Pr, prr_flags);
(void) close(mapfd);
(void) fprintf(stderr, "%s: cannot "
"examine %s: lost control of "
"process\n", command, arg);
continue;
}
}
nstacks = psinfo.pr_nlwp * 2;
stacks = calloc(nstacks, sizeof (stacks[0]));
if (stacks != NULL) {
int n = 0;
(void) Plwp_iter(Pr, getstack, &n);
qsort(stacks, nstacks, sizeof (stacks[0]),
cmpstacks);
}
(void) memset(&t, 0, sizeof (t));
if (Pgetauxval(Pr, AT_BASE) != -1L &&
Prd_agent(Pr) == NULL) {
(void) fprintf(stderr, "%s: warning: "
"librtld_db failed to initialize; "
"shared library information will not be "
"available\n", command);
}
/*
* Gather data
*/
if (xflag)
rc += xmapping_iter(Pr, gather_xmap, NULL, 0);
else if (Sflag)
rc += xmapping_iter(Pr, gather_xmap, NULL, 1);
else {
if (rflag)
rc += rmapping_iter(Pr, gather_map,
NULL);
else if (sflag)
rc += xmapping_iter(Pr, gather_xmap,
NULL, 0);
else if (lflag)
rc += Pmapping_iter(Pr,
gather_map, NULL);
else
rc += Pmapping_iter_resolved(Pr,
gather_map, NULL);
}
/*
* Ensure mappings are consistent.
*/
if (Pstate(Pr) != PS_DEAD) {
struct stat64 newbuf;
if (fstat64(mapfd, &newbuf) != 0 ||
memcmp(&newbuf.st_mtim, &statbuf.st_mtim,
sizeof (newbuf.st_mtim)) != 0) {
if (stacks != NULL) {
free(stacks);
stacks = NULL;
}
goto again;
}
}
/*
* Display data.
*/
if (xflag) {
(void) printf("%*s%*s%*s%*s%*s "
"%sMode Mapped File\n",
addr_width, "Address",
size_width, "Kbytes",
size_width, "RSS",
size_width, "Anon",
size_width, "Locked",
sflag ? "Pgsz " : "");
rc += iter_xmap(sflag ? look_xmap :
look_xmap_nopgsz, &t);
(void) printf("%s%s %s %s %s %s\n",
addr_width == 8 ? "-" : "------",
bar, bar, bar, bar, bar);
(void) printf("%stotal Kb", addr_width == 16 ?
" " : "");
printK(t.total_size, size_width);
printK(t.total_rss, size_width);
printK(t.total_anon, size_width);
printK(t.total_locked, size_width);
(void) printf("\n");
} else if (Sflag) {
(void) printf("%*s%*s%*s Mode"
" Mapped File\n",
addr_width, "Address",
size_width, "Kbytes",
size_width, "Swap");
rc += iter_xmap(look_xmap_nopgsz, &t);
(void) printf("%s%s %s %s\n",
addr_width == 8 ? "-" : "------",
bar, bar, bar);
(void) printf("%stotal Kb", addr_width == 16 ?
" " : "");
printK(t.total_size, size_width);
printK(t.total_swap, size_width);
(void) printf("\n");
} else {
if (rflag) {
rc += iter_map(look_map, &t);
} else if (sflag) {
if (Lflag) {
(void) printf("%*s %*s %4s"
" %-6s %s %s\n",
addr_width, "Address",
size_width,
"Bytes", "Pgsz", "Mode ",
"Lgrp", "Mapped File");
rc += iter_xmap(look_smap, &t);
} else {
(void) printf("%*s %*s %4s"
" %-6s %s\n",
addr_width, "Address",
size_width,
"Bytes", "Pgsz", "Mode ",
"Mapped File");
rc += iter_xmap(look_smap, &t);
}
} else {
rc += iter_map(look_map, &t);
}
(void) printf(" %stotal %*luK\n",
addr_width == 16 ?
" " : "",
size_width, t.total_size);
}
if (stacks != NULL) {
free(stacks);
stacks = NULL;
}
}
Prelease(Pr, prr_flags);
if (mapfd != -1)
(void) close(mapfd);
}
if (use_agent_lwp)
(void) proc_finistdio();
return (rc);
}
static int
rmapping_iter(struct ps_prochandle *Pr, proc_map_f *func, void *cd)
{
char mapname[PATH_MAX];
int mapfd, nmap, i, rc;
struct stat st;
prmap_t *prmapp, *pmp;
ssize_t n;
(void) proc_snprintf(mapname, sizeof (mapname),
"/proc/%d/rmap", (int)Pstatus(Pr)->pr_pid);
if ((mapfd = open(mapname, O_RDONLY)) < 0 || fstat(mapfd, &st) != 0) {
if (mapfd >= 0)
(void) close(mapfd);
return (perr(mapname));
}
nmap = st.st_size / sizeof (prmap_t);
prmapp = malloc((nmap + 1) * sizeof (prmap_t));
if ((n = pread(mapfd, prmapp, (nmap + 1) * sizeof (prmap_t), 0L)) < 0) {
(void) close(mapfd);
free(prmapp);
return (perr("read rmap"));
}
(void) close(mapfd);
nmap = n / sizeof (prmap_t);
for (i = 0, pmp = prmapp; i < nmap; i++, pmp++) {
if ((rc = func(cd, pmp, NULL)) != 0) {
free(prmapp);
return (rc);
}
}
free(prmapp);
return (0);
}
static int
xmapping_iter(struct ps_prochandle *Pr, proc_xmap_f *func, void *cd, int doswap)
{
char mapname[PATH_MAX];
int mapfd, nmap, i, rc;
struct stat st;
prxmap_t *prmapp, *pmp;
ssize_t n;
(void) proc_snprintf(mapname, sizeof (mapname),
"/proc/%d/xmap", (int)Pstatus(Pr)->pr_pid);
if ((mapfd = open(mapname, O_RDONLY)) < 0 || fstat(mapfd, &st) != 0) {
if (mapfd >= 0)
(void) close(mapfd);
return (perr(mapname));
}
nmap = st.st_size / sizeof (prxmap_t);
nmap *= 2;
again:
prmapp = malloc((nmap + 1) * sizeof (prxmap_t));
if ((n = pread(mapfd, prmapp, (nmap + 1) * sizeof (prxmap_t), 0)) < 0) {
(void) close(mapfd);
free(prmapp);
return (perr("read xmap"));
}
if (nmap < n / sizeof (prxmap_t)) {
free(prmapp);
nmap *= 2;
goto again;
}
(void) close(mapfd);
nmap = n / sizeof (prxmap_t);
for (i = 0, pmp = prmapp; i < nmap; i++, pmp++) {
if ((rc = func(cd, pmp, NULL, i == nmap - 1, doswap)) != 0) {
free(prmapp);
return (rc);
}
}
/*
* Mark the last element.
*/
if (map_count > 0)
maps[map_count - 1].md_last = B_TRUE;
free(prmapp);
return (0);
}
/*ARGSUSED*/
static int
look_map(void *data, const prmap_t *pmp, const char *object_name)
{
struct totals *t = data;
const pstatus_t *Psp = Pstatus(Pr);
size_t size;
char mname[PATH_MAX];
char *lname = NULL;
size_t psz = pmp->pr_pagesize;
uintptr_t vaddr = pmp->pr_vaddr;
uintptr_t segment_end = vaddr + pmp->pr_size;
lgrp_id_t lgrp;
memory_chunk_t mchunk;
/*
* If the mapping is not anon or not part of the heap, make a name
* for it. We don't want to report the heap as a.out's data.
*/
if (!(pmp->pr_mflags & MA_ANON) ||
segment_end <= Psp->pr_brkbase ||
pmp->pr_vaddr >= Psp->pr_brkbase + Psp->pr_brksize) {
lname = make_name(Pr, lflag, pmp->pr_vaddr, pmp->pr_mapname,
mname, sizeof (mname));
}
if (lname == NULL &&
((pmp->pr_mflags & MA_ANON) || Pstate(Pr) == PS_DEAD)) {
lname = anon_name(mname, Psp, stacks, nstacks, pmp->pr_vaddr,
pmp->pr_size, pmp->pr_mflags, pmp->pr_shmid, NULL);
}
/*
* Adjust the address range if -A is specified.
*/
size = adjust_addr_range(pmp->pr_vaddr, segment_end, psz,
&vaddr, &segment_end);
if (size == 0)
return (0);
if (!Lflag) {
/*
* Display the whole mapping
*/
size = ROUNDUP_KB(size);
(void) printf(lname ?
"%.*lX %*luK %-6s %s\n" :
"%.*lX %*luK %s\n",
addr_width, vaddr,
size_width - 1, size, mflags(pmp->pr_mflags), lname);
t->total_size += size;
return (0);
}
/*
* We need to display lgroups backing physical memory, so we break the
* segment into individual pages and coalesce pages with the same lgroup
* into one "segment".
*/
/*
* Initialize address descriptions for the mapping.
*/
mem_chunk_init(&mchunk, segment_end, psz);
size = 0;
/*
* Walk mapping (page by page) and display contiguous ranges of memory
* allocated to same lgroup.
*/
do {
size_t size_contig;
/*
* Get contiguous region of memory starting from vaddr allocated
* from the same lgroup.
*/
size_contig = get_contiguous_region(&mchunk, vaddr,
segment_end, pmp->pr_pagesize, &lgrp);
(void) printf(lname ? "%.*lX %*luK %-6s%s %s\n" :
"%.*lX %*luK %s %s\n",
addr_width, vaddr,
size_width - 1, size_contig / KILOBYTE,
mflags(pmp->pr_mflags),
lgrp2str(lgrp), lname);
vaddr += size_contig;
size += size_contig;
} while (vaddr < segment_end && !interrupt);
/* Update the total size */
t->total_size += ROUNDUP_KB(size);
return (0);
}
static void
printK(long value, int width)
{
if (value == 0)
(void) printf(width == 8 ? " -" : " -");
else
(void) printf(" %*lu", width - 1, value);
}
static const char *
pagesize(const prxmap_t *pmp)
{
int pagesize = pmp->pr_hatpagesize;
static char buf[32];
if (pagesize == 0) {
return ("-"); /* no underlying HAT mapping */
}
if (pagesize >= KILOBYTE && (pagesize % KILOBYTE) == 0) {
if ((pagesize % GIGABYTE) == 0)
(void) snprintf(buf, sizeof (buf), "%dG",
pagesize / GIGABYTE);
else if ((pagesize % MEGABYTE) == 0)
(void) snprintf(buf, sizeof (buf), "%dM",
pagesize / MEGABYTE);
else
(void) snprintf(buf, sizeof (buf), "%dK",
pagesize / KILOBYTE);
} else
(void) snprintf(buf, sizeof (buf), "%db", pagesize);
return (buf);
}
/*ARGSUSED*/
static int
look_smap(void *data,
const prxmap_t *pmp,
const char *object_name,
int last, int doswap)
{
struct totals *t = data;
const pstatus_t *Psp = Pstatus(Pr);
size_t size;
char mname[PATH_MAX];
char *lname = NULL;
const char *format;
size_t psz = pmp->pr_pagesize;
uintptr_t vaddr = pmp->pr_vaddr;
uintptr_t segment_end = vaddr + pmp->pr_size;
lgrp_id_t lgrp;
memory_chunk_t mchunk;
/*
* If the mapping is not anon or not part of the heap, make a name
* for it. We don't want to report the heap as a.out's data.
*/
if (!(pmp->pr_mflags & MA_ANON) ||
pmp->pr_vaddr + pmp->pr_size <= Psp->pr_brkbase ||
pmp->pr_vaddr >= Psp->pr_brkbase + Psp->pr_brksize) {
lname = make_name(Pr, lflag, pmp->pr_vaddr, pmp->pr_mapname,
mname, sizeof (mname));
}
if (lname == NULL &&
((pmp->pr_mflags & MA_ANON) || Pstate(Pr) == PS_DEAD)) {
lname = anon_name(mname, Psp, stacks, nstacks, pmp->pr_vaddr,
pmp->pr_size, pmp->pr_mflags, pmp->pr_shmid, NULL);
}
/*
* Adjust the address range if -A is specified.
*/
size = adjust_addr_range(pmp->pr_vaddr, segment_end, psz,
&vaddr, &segment_end);
if (size == 0)
return (0);
if (!Lflag) {
/*
* Display the whole mapping
*/
if (lname != NULL)
format = "%.*lX %*luK %4s %-6s %s\n";
else
format = "%.*lX %*luK %4s %s\n";
size = ROUNDUP_KB(size);
(void) printf(format, addr_width, vaddr, size_width - 1, size,
pagesize(pmp), mflags(pmp->pr_mflags), lname);
t->total_size += size;
return (0);
}
if (lname != NULL)
format = "%.*lX %*luK %4s %-6s%s %s\n";
else
format = "%.*lX %*luK %4s%s %s\n";
/*
* We need to display lgroups backing physical memory, so we break the
* segment into individual pages and coalesce pages with the same lgroup
* into one "segment".
*/
/*
* Initialize address descriptions for the mapping.
*/
mem_chunk_init(&mchunk, segment_end, psz);
size = 0;
/*
* Walk mapping (page by page) and display contiguous ranges of memory
* allocated to same lgroup.
*/
do {
size_t size_contig;
/*
* Get contiguous region of memory starting from vaddr allocated
* from the same lgroup.
*/
size_contig = get_contiguous_region(&mchunk, vaddr,
segment_end, pmp->pr_pagesize, &lgrp);
(void) printf(format, addr_width, vaddr,
size_width - 1, size_contig / KILOBYTE,
pagesize(pmp), mflags(pmp->pr_mflags),
lgrp2str(lgrp), lname);
vaddr += size_contig;
size += size_contig;
} while (vaddr < segment_end && !interrupt);
t->total_size += ROUNDUP_KB(size);
return (0);
}
#define ANON(x) ((aflag || (((x)->pr_mflags & MA_SHARED) == 0)) ? \
((x)->pr_anon) : 0)
/*ARGSUSED*/
static int
look_xmap(void *data,
const prxmap_t *pmp,
const char *object_name,
int last, int doswap)
{
struct totals *t = data;
const pstatus_t *Psp = Pstatus(Pr);
char mname[PATH_MAX];
char *lname = NULL;
char *ln;
/*
* If the mapping is not anon or not part of the heap, make a name
* for it. We don't want to report the heap as a.out's data.
*/
if (!(pmp->pr_mflags & MA_ANON) ||
pmp->pr_vaddr + pmp->pr_size <= Psp->pr_brkbase ||
pmp->pr_vaddr >= Psp->pr_brkbase + Psp->pr_brksize) {
lname = make_name(Pr, lflag, pmp->pr_vaddr, pmp->pr_mapname,
mname, sizeof (mname));
}
if (lname != NULL) {
if ((ln = strrchr(lname, '/')) != NULL)
lname = ln + 1;
} else if ((pmp->pr_mflags & MA_ANON) || Pstate(Pr) == PS_DEAD) {
lname = anon_name(mname, Psp, stacks, nstacks, pmp->pr_vaddr,
pmp->pr_size, pmp->pr_mflags, pmp->pr_shmid, NULL);
}
(void) printf("%.*lX", addr_width, (ulong_t)pmp->pr_vaddr);
printK(ROUNDUP_KB(pmp->pr_size), size_width);
printK(pmp->pr_rss * (pmp->pr_pagesize / KILOBYTE), size_width);
printK(ANON(pmp) * (pmp->pr_pagesize / KILOBYTE), size_width);
printK(pmp->pr_locked * (pmp->pr_pagesize / KILOBYTE), size_width);
(void) printf(lname ? " %4s %-6s %s\n" : " %4s %s\n",
pagesize(pmp), mflags(pmp->pr_mflags), lname);
t->total_size += ROUNDUP_KB(pmp->pr_size);
t->total_rss += pmp->pr_rss * (pmp->pr_pagesize / KILOBYTE);
t->total_anon += ANON(pmp) * (pmp->pr_pagesize / KILOBYTE);
t->total_locked += (pmp->pr_locked * (pmp->pr_pagesize / KILOBYTE));
return (0);
}
/*ARGSUSED*/
static int
look_xmap_nopgsz(void *data,
const prxmap_t *pmp,
const char *object_name,
int last, int doswap)
{
struct totals *t = data;
const pstatus_t *Psp = Pstatus(Pr);
char mname[PATH_MAX];
char *lname = NULL;
char *ln;
static uintptr_t prev_vaddr;
static size_t prev_size;
static offset_t prev_offset;
static int prev_mflags;
static char *prev_lname;
static char prev_mname[PATH_MAX];
static ulong_t prev_rss;
static ulong_t prev_anon;
static ulong_t prev_locked;
static ulong_t prev_swap;
int merged = 0;
static int first = 1;
ulong_t swap = 0;
int kperpage;
/*
* Calculate swap reservations
*/
if (pmp->pr_mflags & MA_SHARED) {
if (aflag && (pmp->pr_mflags & MA_NORESERVE) == 0) {
/* Swap reserved for entire non-ism SHM */
swap = pmp->pr_size / pmp->pr_pagesize;
}
} else if (pmp->pr_mflags & MA_NORESERVE) {
/* Swap reserved on fault for each anon page */
swap = pmp->pr_anon;
} else if (pmp->pr_mflags & MA_WRITE) {
/* Swap reserve for entire writable segment */
swap = pmp->pr_size / pmp->pr_pagesize;
}
/*
* If the mapping is not anon or not part of the heap, make a name
* for it. We don't want to report the heap as a.out's data.
*/
if (!(pmp->pr_mflags & MA_ANON) ||
pmp->pr_vaddr + pmp->pr_size <= Psp->pr_brkbase ||
pmp->pr_vaddr >= Psp->pr_brkbase + Psp->pr_brksize) {
lname = make_name(Pr, lflag, pmp->pr_vaddr, pmp->pr_mapname,
mname, sizeof (mname));
}
if (lname != NULL) {
if ((ln = strrchr(lname, '/')) != NULL)
lname = ln + 1;
} else if ((pmp->pr_mflags & MA_ANON) || Pstate(Pr) == PS_DEAD) {
lname = anon_name(mname, Psp, stacks, nstacks, pmp->pr_vaddr,
pmp->pr_size, pmp->pr_mflags, pmp->pr_shmid, NULL);
}
kperpage = pmp->pr_pagesize / KILOBYTE;
t->total_size += ROUNDUP_KB(pmp->pr_size);
t->total_rss += pmp->pr_rss * kperpage;
t->total_anon += ANON(pmp) * kperpage;
t->total_locked += pmp->pr_locked * kperpage;
t->total_swap += swap * kperpage;
if (first == 1) {
first = 0;
prev_vaddr = pmp->pr_vaddr;
prev_size = pmp->pr_size;
prev_offset = pmp->pr_offset;
prev_mflags = pmp->pr_mflags;
if (lname == NULL) {
prev_lname = NULL;
} else {
(void) strcpy(prev_mname, lname);
prev_lname = prev_mname;
}
prev_rss = pmp->pr_rss * kperpage;
prev_anon = ANON(pmp) * kperpage;
prev_locked = pmp->pr_locked * kperpage;
prev_swap = swap * kperpage;
if (last == 0) {
return (0);
}
merged = 1;
} else if (prev_vaddr + prev_size == pmp->pr_vaddr &&
prev_mflags == pmp->pr_mflags &&
((prev_mflags & MA_ISM) ||
prev_offset + prev_size == pmp->pr_offset) &&
((lname == NULL && prev_lname == NULL) ||
(lname != NULL && prev_lname != NULL &&
strcmp(lname, prev_lname) == 0))) {
prev_size += pmp->pr_size;
prev_rss += pmp->pr_rss * kperpage;
prev_anon += ANON(pmp) * kperpage;
prev_locked += pmp->pr_locked * kperpage;
prev_swap += swap * kperpage;
if (last == 0) {
return (0);
}
merged = 1;
}
(void) printf("%.*lX", addr_width, (ulong_t)prev_vaddr);
printK(ROUNDUP_KB(prev_size), size_width);
if (doswap)
printK(prev_swap, size_width);
else {
printK(prev_rss, size_width);
printK(prev_anon, size_width);
printK(prev_locked, size_width);
}
(void) printf(prev_lname ? " %-6s %s\n" : "%s\n",
mflags(prev_mflags), prev_lname);
if (last == 0) {
prev_vaddr = pmp->pr_vaddr;
prev_size = pmp->pr_size;
prev_offset = pmp->pr_offset;
prev_mflags = pmp->pr_mflags;
if (lname == NULL) {
prev_lname = NULL;
} else {
(void) strcpy(prev_mname, lname);
prev_lname = prev_mname;
}
prev_rss = pmp->pr_rss * kperpage;
prev_anon = ANON(pmp) * kperpage;
prev_locked = pmp->pr_locked * kperpage;
prev_swap = swap * kperpage;
} else if (merged == 0) {
(void) printf("%.*lX", addr_width, (ulong_t)pmp->pr_vaddr);
printK(ROUNDUP_KB(pmp->pr_size), size_width);
if (doswap)
printK(swap * kperpage, size_width);
else {
printK(pmp->pr_rss * kperpage, size_width);
printK(ANON(pmp) * kperpage, size_width);
printK(pmp->pr_locked * kperpage, size_width);
}
(void) printf(lname ? " %-6s %s\n" : " %s\n",
mflags(pmp->pr_mflags), lname);
}
if (last != 0)
first = 1;
return (0);
}
static int
perr(char *s)
{
if (s)
(void) fprintf(stderr, "%s: ", procname);
else
s = procname;
perror(s);
return (1);
}
static char *
mflags(uint_t arg)
{
static char code_buf[80];
char *str = code_buf;
/*
* rwxsR
*
* r - segment is readable
* w - segment is writable
* x - segment is executable
* s - segment is shared
* R - segment is mapped MAP_NORESERVE
*
*/
(void) sprintf(str, "%c%c%c%c%c%c",
arg & MA_READ ? 'r' : '-',
arg & MA_WRITE ? 'w' : '-',
arg & MA_EXEC ? 'x' : '-',
arg & MA_SHARED ? 's' : '-',
arg & MA_NORESERVE ? 'R' : '-',
arg & MA_RESERVED1 ? '*' : ' ');
return (str);
}
static mapdata_t *
nextmap(void)
{
mapdata_t *newmaps;
int next;
if (map_count == map_alloc) {
if (map_alloc == 0)
next = 16;
else
next = map_alloc * 2;
newmaps = realloc(maps, next * sizeof (mapdata_t));
if (newmaps == NULL) {
(void) perr("failed to allocate maps");
exit(1);
}
(void) memset(newmaps + map_alloc, '\0',
(next - map_alloc) * sizeof (mapdata_t));
map_alloc = next;
maps = newmaps;
}
return (&maps[map_count++]);
}
/*ARGSUSED*/
static int
gather_map(void *ignored, const prmap_t *map, const char *objname)
{
mapdata_t *data;
/* Skip mappings which are outside the range specified by -A */
if (!address_in_range(map->pr_vaddr,
map->pr_vaddr + map->pr_size, map->pr_pagesize))
return (0);
data = nextmap();
data->md_map = *map;
if (data->md_objname != NULL)
free(data->md_objname);
data->md_objname = objname ? strdup(objname) : NULL;
return (0);
}
/*ARGSUSED*/
static int
gather_xmap(void *ignored, const prxmap_t *xmap, const char *objname,
int last, int doswap)
{
mapdata_t *data;
/* Skip mappings which are outside the range specified by -A */
if (!address_in_range(xmap->pr_vaddr,
xmap->pr_vaddr + xmap->pr_size, xmap->pr_pagesize))
return (0);
data = nextmap();
data->md_xmap = *xmap;
if (data->md_objname != NULL)
free(data->md_objname);
data->md_objname = objname ? strdup(objname) : NULL;
data->md_last = last;
data->md_doswap = doswap;
return (0);
}
static int
iter_map(proc_map_f *func, void *data)
{
int i;
int ret;
for (i = 0; i < map_count; i++) {
if (interrupt)
break;
if ((ret = func(data, &maps[i].md_map,
maps[i].md_objname)) != 0)
return (ret);
}
return (0);
}
static int
iter_xmap(proc_xmap_f *func, void *data)
{
int i;
int ret;
for (i = 0; i < map_count; i++) {
if (interrupt)
break;
if ((ret = func(data, &maps[i].md_xmap, maps[i].md_objname,
maps[i].md_last, maps[i].md_doswap)) != 0)
return (ret);
}
return (0);
}
/*
* Convert lgroup ID to string.
* returns dash when lgroup ID is invalid.
*/
static char *
lgrp2str(lgrp_id_t lgrp)
{
static char lgrp_buf[20];
char *str = lgrp_buf;
(void) sprintf(str, lgrp == LGRP_NONE ? " -" : "%4d", lgrp);
return (str);
}
/*
* Parse address range specification for -A option.
* The address range may have the following forms:
*
* address
* start and end is set to address
* address,
* start is set to address, end is set to INVALID_ADDRESS
* ,address
* start is set to 0, end is set to address
* address1,address2
* start is set to address1, end is set to address2
*
*/
static int
parse_addr_range(char *input_str, uintptr_t *start, uintptr_t *end)
{
char *startp = input_str;
char *endp = strchr(input_str, ',');
ulong_t s = (ulong_t)INVALID_ADDRESS;
ulong_t e = (ulong_t)INVALID_ADDRESS;
if (endp != NULL) {
/*
* Comma is present. If there is nothing after comma, the end
* remains set at INVALID_ADDRESS. Otherwise it is set to the
* value after comma.
*/
*endp = '\0';
endp++;
if ((*endp != '\0') && sscanf(endp, "%lx", &e) != 1)
return (1);
}
if (startp != NULL) {
/*
* Read the start address, if it is specified. If the address is
* missing, start will be set to INVALID_ADDRESS.
*/
if ((*startp != '\0') && sscanf(startp, "%lx", &s) != 1)
return (1);
}
/* If there is no comma, end becomes equal to start */
if (endp == NULL)
e = s;
/*
* ,end implies 0..end range
*/
if (e != INVALID_ADDRESS && s == INVALID_ADDRESS)
s = 0;
*start = (uintptr_t)s;
*end = (uintptr_t)e;
/* Return error if neither start nor end address were specified */
return (! (s != INVALID_ADDRESS || e != INVALID_ADDRESS));
}
/*
* Check whether any portion of [start, end] segment is within the
* [start_addr, end_addr] range.
*
* Return values:
* 0 - address is outside the range
* 1 - address is within the range
*/
static int
address_in_range(uintptr_t start, uintptr_t end, size_t psz)
{
int rc = 1;
/*
* Nothing to do if there is no address range specified with -A
*/
if (start_addr != INVALID_ADDRESS || end_addr != INVALID_ADDRESS) {
/* The segment end is below the range start */
if ((start_addr != INVALID_ADDRESS) &&
(end < P2ALIGN(start_addr, psz)))
rc = 0;
/* The segment start is above the range end */
if ((end_addr != INVALID_ADDRESS) &&
(start > P2ALIGN(end_addr + psz, psz)))
rc = 0;
}
return (rc);
}
/*
* Returns an intersection of the [start, end] interval and the range specified
* by -A flag [start_addr, end_addr]. Unspecified parts of the address range
* have value INVALID_ADDRESS.
*
* The start_addr address is rounded down to the beginning of page and end_addr
* is rounded up to the end of page.
*
* Returns the size of the resulting interval or zero if the interval is empty
* or invalid.
*/
static size_t
adjust_addr_range(uintptr_t start, uintptr_t end, size_t psz,
uintptr_t *new_start, uintptr_t *new_end)
{
uintptr_t from; /* start_addr rounded down */
uintptr_t to; /* end_addr rounded up */
/*
* Round down the lower address of the range to the beginning of page.
*/
if (start_addr == INVALID_ADDRESS) {
/*
* No start_addr specified by -A, the lower part of the interval
* does not change.
*/
*new_start = start;
} else {
from = P2ALIGN(start_addr, psz);
/*
* If end address is outside the range, return an empty
* interval
*/
if (end < from) {
*new_start = *new_end = 0;
return (0);
}
/*
* The adjusted start address is the maximum of requested start
* and the aligned start_addr of the -A range.
*/
*new_start = start < from ? from : start;
}
/*
* Round up the higher address of the range to the end of page.
*/
if (end_addr == INVALID_ADDRESS) {
/*
* No end_addr specified by -A, the upper part of the interval
* does not change.
*/
*new_end = end;
} else {
/*
* If only one address is specified and it is the beginning of a
* segment, get information about the whole segment. This
* function is called once per segment and the 'end' argument is
* always the end of a segment, so just use the 'end' value.
*/
to = (end_addr == start_addr && start == start_addr) ?
end :
P2ALIGN(end_addr + psz, psz);
/*
* If start address is outside the range, return an empty
* interval
*/
if (start > to) {
*new_start = *new_end = 0;
return (0);
}
/*
* The adjusted end address is the minimum of requested end
* and the aligned end_addr of the -A range.
*/
*new_end = end > to ? to : end;
}
/*
* Make sure that the resulting interval is legal.
*/
if (*new_end < *new_start)
*new_start = *new_end = 0;
/* Return the size of the interval */
return (*new_end - *new_start);
}
/*
* Initialize memory_info data structure with information about a new segment.
*/
static void
mem_chunk_init(memory_chunk_t *chunk, uintptr_t end, size_t psz)
{
chunk->end_addr = end;
chunk->page_size = psz;
chunk->page_index = 0;
chunk->chunk_start = chunk->chunk_end = 0;
}
/*
* Create a new chunk of addresses starting from vaddr.
* Pass the whole chunk to pr_meminfo to collect lgroup and page size
* information for each page in the chunk.
*/
static void
mem_chunk_get(memory_chunk_t *chunk, uintptr_t vaddr)
{
page_descr_t *pdp = chunk->page_info;
size_t psz = chunk->page_size;
uintptr_t addr = vaddr;
uint64_t inaddr[MAX_MEMINFO_CNT];
uint64_t outdata[2 * MAX_MEMINFO_CNT];
uint_t info[2] = { MEMINFO_VLGRP, MEMINFO_VPAGESIZE };
uint_t validity[MAX_MEMINFO_CNT];
uint64_t *dataptr = inaddr;
uint64_t *outptr = outdata;
uint_t *valptr = validity;
int i, j, rc;
chunk->chunk_start = vaddr;
chunk->page_index = 0; /* reset index for the new chunk */
/*
* Fill in MAX_MEMINFO_CNT wotrh of pages starting from vaddr. Also,
* copy starting address of each page to inaddr array for pr_meminfo.
*/
for (i = 0, pdp = chunk->page_info;
(i < MAX_MEMINFO_CNT) && (addr <= chunk->end_addr);
i++, pdp++, dataptr++, addr += psz) {
*dataptr = (uint64_t)addr;
pdp->pd_start = addr;
pdp->pd_lgrp = LGRP_NONE;
pdp->pd_valid = 0;
pdp->pd_pagesize = 0;
}
/* Mark the number of entries in the chunk and the last address */
chunk->page_count = i;
chunk->chunk_end = addr - psz;
if (interrupt)
return;
/* Call meminfo for all collected addresses */
rc = pr_meminfo(Pr, inaddr, i, info, 2, outdata, validity);
if (rc < 0) {
(void) perr("can not get memory information");
return;
}
/* Verify validity of each result and fill in the addrs array */
pdp = chunk->page_info;
for (j = 0; j < i; j++, pdp++, valptr++, outptr += 2) {
/* Skip invalid address pointers */
if ((*valptr & 1) == 0) {
continue;
}
/* Is lgroup information available? */
if ((*valptr & 2) != 0) {
pdp->pd_lgrp = (lgrp_id_t)*outptr;
pdp->pd_valid = 1;
}
/* Is page size informaion available? */
if ((*valptr & 4) != 0) {
pdp->pd_pagesize = *(outptr + 1);
}
}
}
/*
* Starting from address 'vaddr' find the region with pages allocated from the
* same lgroup.
*
* Arguments:
* mchunk Initialized memory chunk structure
* vaddr Starting address of the region
* maxaddr Upper bound of the region
* pagesize Default page size to use
* ret_lgrp On exit contains the lgroup ID of all pages in the
* region.
*
* Returns:
* Size of the contiguous region in bytes
* The lgroup ID of all pages in the region in ret_lgrp argument.
*/
static size_t
get_contiguous_region(memory_chunk_t *mchunk, uintptr_t vaddr,
uintptr_t maxaddr, size_t pagesize, lgrp_id_t *ret_lgrp)
{
size_t size_contig = 0;
lgrp_id_t lgrp; /* Lgroup of the region start */
lgrp_id_t curr_lgrp; /* Lgroup of the current page */
size_t psz = pagesize; /* Pagesize to use */
/* Set both lgroup IDs to the lgroup of the first page */
curr_lgrp = lgrp = addr_to_lgrp(mchunk, vaddr, &psz);
/*
* Starting from vaddr, walk page by page until either the end
* of the segment is reached or a page is allocated from a different
* lgroup. Also stop if interrupted from keyboard.
*/
while ((vaddr < maxaddr) && (curr_lgrp == lgrp) && !interrupt) {
/*
* Get lgroup ID and the page size of the current page.
*/
curr_lgrp = addr_to_lgrp(mchunk, vaddr, &psz);
/* If there is no page size information, use the default */
if (psz == 0)
psz = pagesize;
if (curr_lgrp == lgrp) {
/*
* This page belongs to the contiguous region.
* Increase the region size and advance to the new page.
*/
size_contig += psz;
vaddr += psz;
}
}
/* Return the region lgroup ID and the size */
*ret_lgrp = lgrp;
return (size_contig);
}
/*
* Given a virtual address, return its lgroup and page size. If there is meminfo
* information for an address, use it, otherwise shift the chunk window to the
* vaddr and create a new chunk with known meminfo information.
*/
static lgrp_id_t
addr_to_lgrp(memory_chunk_t *chunk, uintptr_t vaddr, size_t *psz)
{
page_descr_t *pdp;
lgrp_id_t lgrp = LGRP_NONE;
int i;
*psz = chunk->page_size;
if (interrupt)
return (0);
/*
* Is there information about this address? If not, create a new chunk
* starting from vaddr and apply pr_meminfo() to the whole chunk.
*/
if (vaddr < chunk->chunk_start || vaddr > chunk->chunk_end) {
/*
* This address is outside the chunk, get the new chunk and
* collect meminfo information for it.
*/
mem_chunk_get(chunk, vaddr);
}
/*
* Find information about the address.
*/
pdp = &chunk->page_info[chunk->page_index];
for (i = chunk->page_index; i < chunk->page_count; i++, pdp++) {
if (pdp->pd_start == vaddr) {
if (pdp->pd_valid) {
lgrp = pdp->pd_lgrp;
/*
* Override page size information if it is
* present.
*/
if (pdp->pd_pagesize > 0)
*psz = pdp->pd_pagesize;
}
break;
}
}
/*
* Remember where we ended - the next search will start here.
* We can query for the lgrp for the same address again, so do not
* advance index past the current value.
*/
chunk->page_index = i;
return (lgrp);
}
/* ARGSUSED */
static void
intr(int sig)
{
interrupt = 1;
}
|