]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - io/mincore.c
xfsprogs: don't install platform_defs.h
[thirdparty/xfsprogs-dev.git] / io / mincore.c
CommitLineData
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
6b803e5a
CH
19#include "command.h"
20#include "input.h"
0bba1a49 21#include <sys/mman.h>
0bba1a49
NS
22#include "init.h"
23#include "io.h"
24
25static cmdinfo_t mincore_cmd;
26
27int
28mincore_f(
29 int argc,
30 char **argv)
31{
3b1cdc9f 32 off64_t offset, llength;
0bba1a49 33 size_t length;
2c2f6d79 34 size_t blocksize, sectsize;
0bba1a49
NS
35 void *start;
36 void *current, *previous;
37 unsigned char *vec;
cb7ba2b0 38 int i;
0bba1a49
NS
39
40 if (argc == 1) {
41 offset = mapping->offset;
42 length = mapping->length;
43 } else if (argc == 3) {
44 init_cvtnum(&blocksize, &sectsize);
45 offset = cvtnum(blocksize, sectsize, argv[1]);
46 if (offset < 0) {
47 printf(_("non-numeric offset argument -- %s\n"),
48 argv[1]);
49 return 0;
50 }
3b1cdc9f
ES
51 llength = cvtnum(blocksize, sectsize, argv[2]);
52 if (llength < 0) {
0bba1a49
NS
53 printf(_("non-numeric length argument -- %s\n"),
54 argv[2]);
55 return 0;
3b1cdc9f
ES
56 } else if (llength > (size_t)llength) {
57 printf(_("length argument too large -- %lld\n"),
1accf98c 58 (long long)llength);
3b1cdc9f
ES
59 return 0;
60 } else
61 length = (size_t)llength;
0bba1a49
NS
62 } else {
63 return command_usage(&mincore_cmd);
64 }
65
66 start = check_mapping_range(mapping, offset, length, 1);
67 if (!start)
68 return 0;
69
70 vec = calloc(length/pagesize, sizeof(unsigned char));
71 if (!vec) {
72 perror("calloc");
73 return 0;
74 }
75
76 if (mincore(start, length, vec) < 0) {
77 perror("mincore");
78 free(vec);
79 return 0;
80 }
81
82 previous = NULL;
83 current = start;
84 for (i = 0; i < length/pagesize; i++, current += pagesize) {
85 if (vec[i]) {
86 if (!previous) { /* print start address */
87 printf("0x%lx - ", (unsigned long)current);
88 previous = start + (i * pagesize);
89 }
90 } else if (previous) { /* print end and page count */
91 printf(_("0x%lx %lu pages (%llu : %lu)\n"),
92 (unsigned long)current,
93 (unsigned long)(current - previous) / pagesize,
94 (unsigned long long)offset +
95 (unsigned long long)(previous - start),
96 (unsigned long)(current - previous));
97 previous = NULL;
98 }
99 }
100 if (previous)
101 printf(_("0x%lx %lu pages (%llu : %lu)\n"),
102 (unsigned long)current,
103 (unsigned long)(current - previous) / pagesize,
104 (unsigned long long)offset +
105 (unsigned long long)(previous - start),
106 (unsigned long)(current - previous));
107
108 free(vec);
109 return 0;
110}
111
112void
113mincore_init(void)
114{
ad765595
AM
115 mincore_cmd.name = "mincore";
116 mincore_cmd.altname = "mi";
0bba1a49
NS
117 mincore_cmd.cfunc = mincore_f;
118 mincore_cmd.argmin = 0;
119 mincore_cmd.argmax = 2;
120 mincore_cmd.flags = CMD_NOFILE_OK | CMD_FOREIGN_OK;
121 mincore_cmd.args = _("[off len]");
122 mincore_cmd.oneline = _("find mapping pages that are memory resident");
123
124 add_command(&mincore_cmd);
125}