]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - io/madvise.c
Merge branch 'progs-cleanup' into for-next
[thirdparty/xfsprogs-dev.git] / io / madvise.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/xfs.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 madvise_cmd;
27
28 static void
29 madvise_help(void)
30 {
31 printf(_(
32 "\n"
33 " advise the page cache about access patterns expected for a mapping\n"
34 "\n"
35 " Modifies page cache behavior when operating on the current mapping.\n"
36 " The range arguments are required by some advise commands ([*] below).\n"
37 " With no arguments, the POSIX_MADV_NORMAL advice is implied.\n"
38 " -d -- don't need these pages (POSIX_MADV_DONTNEED) [*]\n"
39 " -r -- expect random page references (POSIX_MADV_RANDOM)\n"
40 " -s -- expect sequential page references (POSIX_MADV_SEQUENTIAL)\n"
41 " -w -- will need these pages (POSIX_MADV_WILLNEED) [*]\n"
42 " Notes:\n"
43 " NORMAL sets the default readahead setting on the file.\n"
44 " RANDOM sets the readahead setting on the file to zero.\n"
45 " SEQUENTIAL sets double the default readahead setting on the file.\n"
46 " WILLNEED forces the maximum readahead.\n"
47 "\n"));
48 }
49
50 int
51 madvise_f(
52 int argc,
53 char **argv)
54 {
55 off64_t offset, llength;
56 size_t length;
57 void *start;
58 int advise = MADV_NORMAL, c;
59 size_t blocksize, sectsize;
60
61 while ((c = getopt(argc, argv, "drsw")) != EOF) {
62 switch (c) {
63 case 'd': /* Don't need these pages */
64 advise = MADV_DONTNEED;
65 break;
66 case 'r': /* Expect random page references */
67 advise = MADV_RANDOM;
68 break;
69 case 's': /* Expect sequential page references */
70 advise = MADV_SEQUENTIAL;
71 break;
72 case 'w': /* Will need these pages */
73 advise = MADV_WILLNEED;
74 break;
75 default:
76 return command_usage(&madvise_cmd);
77 }
78 }
79
80 if (optind == argc) {
81 offset = mapping->offset;
82 length = mapping->length;
83 } else if (optind == argc - 2) {
84 init_cvtnum(&blocksize, &sectsize);
85 offset = cvtnum(blocksize, sectsize, argv[optind]);
86 if (offset < 0) {
87 printf(_("non-numeric offset argument -- %s\n"),
88 argv[optind]);
89 return 0;
90 }
91 optind++;
92 llength = cvtnum(blocksize, sectsize, argv[optind]);
93 if (llength < 0) {
94 printf(_("non-numeric length argument -- %s\n"),
95 argv[optind]);
96 return 0;
97 } else if (llength > (size_t)llength) {
98 printf(_("length argument too large -- %lld\n"),
99 (long long)llength);
100 return 0;
101 } else
102 length = (size_t)llength;
103 } else {
104 return command_usage(&madvise_cmd);
105 }
106
107 start = check_mapping_range(mapping, offset, length, 1);
108 if (!start)
109 return 0;
110
111 if (madvise(start, length, advise) < 0) {
112 perror("madvise");
113 return 0;
114 }
115 return 0;
116 }
117
118 void
119 madvise_init(void)
120 {
121 madvise_cmd.name = "madvise";
122 madvise_cmd.altname = "ma";
123 madvise_cmd.cfunc = madvise_f;
124 madvise_cmd.argmin = 0;
125 madvise_cmd.argmax = -1;
126 madvise_cmd.flags = CMD_NOFILE_OK | CMD_FOREIGN_OK;
127 madvise_cmd.args = _("[-drsw] [off len]");
128 madvise_cmd.oneline = _("give advice about use of memory");
129 madvise_cmd.help = madvise_help;
130
131 add_command(&madvise_cmd);
132 }