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