]>
Commit | Line | Data |
---|---|---|
0bba1a49 | 1 | /* |
da23017d NS |
2 | * Copyright (c) 2004-2005 Silicon Graphics, Inc. |
3 | * All Rights Reserved. | |
0bba1a49 | 4 | * |
da23017d NS |
5 | * This program is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU General Public License as | |
0bba1a49 NS |
7 | * published by the Free Software Foundation. |
8 | * | |
da23017d NS |
9 | * This program is distributed in the hope that it would be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
0bba1a49 | 13 | * |
da23017d NS |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write the Free Software Foundation, | |
16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
0bba1a49 NS |
17 | */ |
18 | ||
2a1888c5 | 19 | #include <xfs/xfs.h> |
0717a7db NS |
20 | #include <xfs/command.h> |
21 | #include <xfs/input.h> | |
0bba1a49 | 22 | #include <sys/mman.h> |
0bba1a49 NS |
23 | #include "init.h" |
24 | #include "io.h" | |
25 | ||
26 | static cmdinfo_t mincore_cmd; | |
27 | ||
28 | int | |
29 | mincore_f( | |
30 | int argc, | |
31 | char **argv) | |
32 | { | |
3b1cdc9f | 33 | off64_t offset, llength; |
0bba1a49 | 34 | size_t length; |
2c2f6d79 | 35 | size_t blocksize, sectsize; |
0bba1a49 NS |
36 | void *start; |
37 | void *current, *previous; | |
38 | unsigned char *vec; | |
cb7ba2b0 | 39 | int i; |
0bba1a49 NS |
40 | |
41 | if (argc == 1) { | |
42 | offset = mapping->offset; | |
43 | length = mapping->length; | |
44 | } else if (argc == 3) { | |
45 | init_cvtnum(&blocksize, §size); | |
46 | offset = cvtnum(blocksize, sectsize, argv[1]); | |
47 | if (offset < 0) { | |
48 | printf(_("non-numeric offset argument -- %s\n"), | |
49 | argv[1]); | |
50 | return 0; | |
51 | } | |
3b1cdc9f ES |
52 | llength = cvtnum(blocksize, sectsize, argv[2]); |
53 | if (llength < 0) { | |
0bba1a49 NS |
54 | printf(_("non-numeric length argument -- %s\n"), |
55 | argv[2]); | |
56 | return 0; | |
3b1cdc9f ES |
57 | } else if (llength > (size_t)llength) { |
58 | printf(_("length argument too large -- %lld\n"), | |
1accf98c | 59 | (long long)llength); |
3b1cdc9f ES |
60 | return 0; |
61 | } else | |
62 | length = (size_t)llength; | |
0bba1a49 NS |
63 | } else { |
64 | return command_usage(&mincore_cmd); | |
65 | } | |
66 | ||
67 | start = check_mapping_range(mapping, offset, length, 1); | |
68 | if (!start) | |
69 | return 0; | |
70 | ||
71 | vec = calloc(length/pagesize, sizeof(unsigned char)); | |
72 | if (!vec) { | |
73 | perror("calloc"); | |
74 | return 0; | |
75 | } | |
76 | ||
77 | if (mincore(start, length, vec) < 0) { | |
78 | perror("mincore"); | |
79 | free(vec); | |
80 | return 0; | |
81 | } | |
82 | ||
83 | previous = NULL; | |
84 | current = start; | |
85 | for (i = 0; i < length/pagesize; i++, current += pagesize) { | |
86 | if (vec[i]) { | |
87 | if (!previous) { /* print start address */ | |
88 | printf("0x%lx - ", (unsigned long)current); | |
89 | previous = start + (i * pagesize); | |
90 | } | |
91 | } else if (previous) { /* print end and page count */ | |
92 | printf(_("0x%lx %lu pages (%llu : %lu)\n"), | |
93 | (unsigned long)current, | |
94 | (unsigned long)(current - previous) / pagesize, | |
95 | (unsigned long long)offset + | |
96 | (unsigned long long)(previous - start), | |
97 | (unsigned long)(current - previous)); | |
98 | previous = NULL; | |
99 | } | |
100 | } | |
101 | if (previous) | |
102 | printf(_("0x%lx %lu pages (%llu : %lu)\n"), | |
103 | (unsigned long)current, | |
104 | (unsigned long)(current - previous) / pagesize, | |
105 | (unsigned long long)offset + | |
106 | (unsigned long long)(previous - start), | |
107 | (unsigned long)(current - previous)); | |
108 | ||
109 | free(vec); | |
110 | return 0; | |
111 | } | |
112 | ||
113 | void | |
114 | mincore_init(void) | |
115 | { | |
116 | mincore_cmd.name = _("mincore"); | |
117 | mincore_cmd.altname = _("mi"); | |
118 | mincore_cmd.cfunc = mincore_f; | |
119 | mincore_cmd.argmin = 0; | |
120 | mincore_cmd.argmax = 2; | |
121 | mincore_cmd.flags = CMD_NOFILE_OK | CMD_FOREIGN_OK; | |
122 | mincore_cmd.args = _("[off len]"); | |
123 | mincore_cmd.oneline = _("find mapping pages that are memory resident"); | |
124 | ||
125 | add_command(&mincore_cmd); | |
126 | } |