]> git.ipfire.org Git - thirdparty/util-linux.git/blame - disk-utils/elvtune.c
build-sys: enable mesg(1) by default
[thirdparty/util-linux.git] / disk-utils / elvtune.c
CommitLineData
22853e4a
KZ
1/*
2 * elvtune.c - I/O elevator tuner
3 *
4 * Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
7cebf0bb
SK
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22853e4a
KZ
19 */
20
ba0c9030
SK
21/*
22 * This command is deprecated. The utility is in maintenance mode,
23 * meaning we keep them in source tree for backward compatibility
24 * only. Do not waste time making this command better, unless the
25 * fix is about security or other very critical issue.
26 *
27 * See Documentation/deprecated.txt for more information.
28 */
29
22853e4a 30#include <fcntl.h>
d26aa358 31#include <errno.h>
22853e4a 32#include <stdio.h>
d26aa358 33#include <getopt.h>
22853e4a
KZ
34#include <unistd.h>
35#include <stdlib.h>
d26aa358
KZ
36#include <string.h>
37#include <sys/ioctl.h>
38#include <sys/utsname.h>
66ee8158 39#include "nls.h"
8505ff35 40#include "blkdev.h"
45ca68ec 41#include "closestream.h"
8505ff35 42#include "linux_version.h"
22853e4a
KZ
43
44/* this has to match with the kernel structure */
66ee8158 45/* current version for ac19 and 2.2.16 */
22853e4a 46typedef struct blkelv_ioctl_arg_s {
66ee8158 47 int queue_ID;
22853e4a
KZ
48 int read_latency;
49 int write_latency;
50 int max_bomb_segments;
51} blkelv_ioctl_arg_t;
52
53static void
54usage(void) {
8e522bb7 55 fprintf(stderr, "elvtune (%s)\n", PACKAGE_STRING);
66ee8158
KZ
56 fprintf(stderr, _("usage:\n"));
57 fprintf(stderr, "\telvtune [-r r_lat] [-w w_lat] [-b b_lat]"
58 " /dev/blkdev1 [/dev/blkdev2...]\n");
22853e4a
KZ
59 fprintf(stderr, "\telvtune -h\n");
60 fprintf(stderr, "\telvtune -v\n");
90d43eca 61 fprintf(stderr, _("\tNOTE: elvtune only works with 2.4 kernels\n"));
d26aa358 62 /* (ioctls exist in 2.2.16 - 2.5.57) */
22853e4a
KZ
63}
64
65static void
66version(void) {
e421313d 67 fprintf(stderr, UTIL_LINUX_VERSION);
22853e4a
KZ
68}
69
70int
71main(int argc, char * argv[]) {
72 int read_value = 0xbeefbeef, write_value = 0xbeefbeef, bomb_value = 0xbeefbeef;
73 int read_set, write_set, bomb_set, set;
74 char * devname;
75 int fd;
76 blkelv_ioctl_arg_t elevator;
77
78 read_set = write_set = bomb_set = set = 0;
79
90d43eca
PR
80 setlocale(LC_MESSAGES, "");
81 bindtextdomain(PACKAGE, LOCALEDIR);
82 textdomain(PACKAGE);
45ca68ec 83 atexit(close_stdout);
90d43eca 84
22853e4a
KZ
85 for (;;) {
86 int opt;
87
88 opt = getopt(argc, argv, "r:w:b:hv");
ffc43748 89 if (opt == -1)
22853e4a
KZ
90 break;
91 switch (opt) {
92 case 'r':
93 read_value = atoi(optarg);
94 read_set = set = 1;
95 break;
96 case 'w':
97 write_value = atoi(optarg);
98 write_set = set = 1;
99 break;
100 case 'b':
101 bomb_value = atoi(optarg);
102 bomb_set = set = 1;
103 break;
104
105 case 'h':
106 usage(), exit(0);
107 case 'v':
108 version(), exit(0);
109
22853e4a 110 default:
9404cc7f 111 usage(), exit(1);
22853e4a
KZ
112 }
113 }
114
115 if (optind >= argc)
90d43eca 116 fprintf(stderr, _("missing blockdevice, use -h for help\n")), exit(1);
8505ff35 117
22853e4a
KZ
118 while (optind < argc) {
119 devname = argv[optind++];
120
121 fd = open(devname, O_RDONLY|O_NONBLOCK);
122 if (fd < 0) {
123 perror("open");
124 break;
125 }
126
d26aa358
KZ
127 /* mmj: If we get EINVAL it's not a 2.4 kernel, so warn about
128 that and exit. It should return ENOTTY however, so check for
129 that as well in case it gets corrected in the future */
130
22853e4a 131 if (ioctl(fd, BLKELVGET, &elevator) < 0) {
d26aa358 132 int errsv = errno;
22853e4a 133 perror("ioctl get");
d26aa358 134 if ((errsv == EINVAL || errsv == ENOTTY) &&
8505ff35 135 get_linux_version() >= KERNEL_VERSION(2,5,58)) {
d26aa358 136 fprintf(stderr,
90d43eca 137 _("\nelvtune is only useful on older "
d26aa358 138 "kernels;\nfor 2.6 use IO scheduler "
90d43eca 139 "sysfs tunables instead..\n"));
d26aa358 140 }
22853e4a
KZ
141 break;
142 }
143
144 if (set) {
145 if (read_set)
146 elevator.read_latency = read_value;
147 if (write_set)
148 elevator.write_latency = write_value;
149 if (bomb_set)
150 elevator.max_bomb_segments = bomb_value;
151
152 if (ioctl(fd, BLKELVSET, &elevator) < 0) {
153 perror("ioctl set");
154 break;
155 }
156 if (ioctl(fd, BLKELVGET, &elevator) < 0) {
157 perror("ioctl reget");
158 break;
159 }
160 }
161
66ee8158 162 printf("\n%s elevator ID\t\t%d\n", devname, elevator.queue_ID);
22853e4a
KZ
163 printf("\tread_latency:\t\t%d\n", elevator.read_latency);
164 printf("\twrite_latency:\t\t%d\n", elevator.write_latency);
165 printf("\tmax_bomb_segments:\t%d\n\n", elevator.max_bomb_segments);
166
167 if (close(fd) < 0) {
168 perror("close");
169 break;
170 }
171 }
172
173 return 0;
174}