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