]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - io/mincore.c
Update copyright/license notices to match SGI legal prefered boilerplate.
[thirdparty/xfsprogs-dev.git] / io / mincore.c
1 /*
2 * Copyright (c) 2004-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
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
7 * published by the Free Software Foundation.
8 *
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.
13 *
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
17 */
18
19 #include <xfs/libxfs.h>
20 #include <xfs/command.h>
21 #include <xfs/input.h>
22 #include <sys/mman.h>
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 {
33 off64_t offset;
34 size_t length;
35 size_t blocksize, sectsize;
36 void *start;
37 void *current, *previous;
38 unsigned char *vec;
39 int i;
40
41 if (argc == 1) {
42 offset = mapping->offset;
43 length = mapping->length;
44 } else if (argc == 3) {
45 init_cvtnum(&blocksize, &sectsize);
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 }
52 length = cvtnum(blocksize, sectsize, argv[2]);
53 if (length < 0) {
54 printf(_("non-numeric length argument -- %s\n"),
55 argv[2]);
56 return 0;
57 }
58 } else {
59 return command_usage(&mincore_cmd);
60 }
61
62 start = check_mapping_range(mapping, offset, length, 1);
63 if (!start)
64 return 0;
65
66 vec = calloc(length/pagesize, sizeof(unsigned char));
67 if (!vec) {
68 perror("calloc");
69 return 0;
70 }
71
72 if (mincore(start, length, vec) < 0) {
73 perror("mincore");
74 free(vec);
75 return 0;
76 }
77
78 previous = NULL;
79 current = start;
80 for (i = 0; i < length/pagesize; i++, current += pagesize) {
81 if (vec[i]) {
82 if (!previous) { /* print start address */
83 printf("0x%lx - ", (unsigned long)current);
84 previous = start + (i * pagesize);
85 }
86 } else if (previous) { /* print end and page count */
87 printf(_("0x%lx %lu pages (%llu : %lu)\n"),
88 (unsigned long)current,
89 (unsigned long)(current - previous) / pagesize,
90 (unsigned long long)offset +
91 (unsigned long long)(previous - start),
92 (unsigned long)(current - previous));
93 previous = NULL;
94 }
95 }
96 if (previous)
97 printf(_("0x%lx %lu pages (%llu : %lu)\n"),
98 (unsigned long)current,
99 (unsigned long)(current - previous) / pagesize,
100 (unsigned long long)offset +
101 (unsigned long long)(previous - start),
102 (unsigned long)(current - previous));
103
104 free(vec);
105 return 0;
106 }
107
108 void
109 mincore_init(void)
110 {
111 mincore_cmd.name = _("mincore");
112 mincore_cmd.altname = _("mi");
113 mincore_cmd.cfunc = mincore_f;
114 mincore_cmd.argmin = 0;
115 mincore_cmd.argmax = 2;
116 mincore_cmd.flags = CMD_NOFILE_OK | CMD_FOREIGN_OK;
117 mincore_cmd.args = _("[off len]");
118 mincore_cmd.oneline = _("find mapping pages that are memory resident");
119
120 add_command(&mincore_cmd);
121 }