1 /*- 2 * Copyright (c) 2003 Poul-Henning Kamp 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. The names of the authors may not be used to endorse or promote 14 * products derived from this software without specific prior written 15 * permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD$ 30 */ 31 32 #include <stdio.h> 33 #include <stdint.h> 34 #include <stdlib.h> 35 #include <unistd.h> 36 #include <errno.h> 37 #include <fcntl.h> 38 #include <libutil.h> 39 #include <paths.h> 40 #include <err.h> 41 #include <sys/disk.h> 42 #include <sys/time.h> 43 44 static void 45 usage(void) 46 { 47 fprintf(stderr, "usage: diskinfo [-ctv] disk ...\n"); 48 exit (1); 49 } 50 51 static int opt_c, opt_t, opt_v; 52 53 static void speeddisk(int fd, off_t mediasize, u_int sectorsize); 54 static void commandtime(int fd, off_t mediasize, u_int sectorsize); 55 56 int 57 main(int argc, char **argv) 58 { 59 int i, ch, fd, error; 60 char buf[BUFSIZ], ident[DISK_IDENT_SIZE]; 61 off_t mediasize; 62 u_int sectorsize, fwsectors, fwheads; 63 64 while ((ch = getopt(argc, argv, "ctv")) != -1) { 65 switch (ch) { 66 case 'c': 67 opt_c = 1; 68 opt_v = 1; 69 break; 70 case 't': 71 opt_t = 1; 72 opt_v = 1; 73 break; 74 case 'v': 75 opt_v = 1; 76 break; 77 default: 78 usage(); 79 } 80 } 81 argc -= optind; 82 argv += optind; 83 84 if (argc < 1) 85 usage(); 86 87 for (i = 0; i < argc; i++) { 88 fd = open(argv[i], O_RDONLY); 89 if (fd < 0 && errno == ENOENT && *argv[i] != '/') { 90 sprintf(buf, "%s%s", _PATH_DEV, argv[i]); 91 fd = open(buf, O_RDONLY); 92 } 93 if (fd < 0) 94 err(1, argv[i]); 95 error = ioctl(fd, DIOCGMEDIASIZE, &mediasize); 96 if (error) 97 err(1, "%s: ioctl(DIOCGMEDIASIZE) failed, probably not a disk.", argv[i]); 98 error = ioctl(fd, DIOCGSECTORSIZE, §orsize); 99 if (error) 100 err(1, "%s: DIOCGSECTORSIZE failed, probably not a disk.", argv[i]); 101 error = ioctl(fd, DIOCGFWSECTORS, &fwsectors); 102 if (error) 103 fwsectors = 0; 104 error = ioctl(fd, DIOCGFWHEADS, &fwheads); 105 if (error) 106 fwheads = 0; 107 if (!opt_v) { 108 printf("%s", argv[i]); 109 printf("\t%u", sectorsize); 110 printf("\t%jd", (intmax_t)mediasize); 111 printf("\t%jd", (intmax_t)mediasize/sectorsize); 112 if (fwsectors != 0 && fwheads != 0) { 113 printf("\t%jd", (intmax_t)mediasize / 114 (fwsectors * fwheads * sectorsize)); 115 printf("\t%u", fwheads); 116 printf("\t%u", fwsectors); 117 } 118 } else { 119 humanize_number(buf, 5, (int64_t)mediasize, "", 120 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); 121 printf("%s\n", argv[i]); 122 printf("\t%-12u\t# sectorsize\n", sectorsize); 123 printf("\t%-12jd\t# mediasize in bytes (%s)\n", 124 (intmax_t)mediasize, buf); 125 printf("\t%-12jd\t# mediasize in sectors\n", 126 (intmax_t)mediasize/sectorsize); 127 if (fwsectors != 0 && fwheads != 0) { 128 printf("\t%-12jd\t# Cylinders according to firmware.\n", (intmax_t)mediasize / 129 (fwsectors * fwheads * sectorsize)); 130 printf("\t%-12u\t# Heads according to firmware.\n", fwheads); 131 printf("\t%-12u\t# Sectors according to firmware.\n", fwsectors); 132 } 133 if (ioctl(fd, DIOCGIDENT, ident) == 0) 134 printf("\t%-12s\t# Disk ident.\n", ident); 135 } 136 printf("\n"); 137 if (opt_c) 138 commandtime(fd, mediasize, sectorsize); 139 if (opt_t) 140 speeddisk(fd, mediasize, sectorsize); 141 close(fd); 142 } 143 exit (0); 144 } 145 146 147 static char sector[65536]; 148 static char mega[1024 * 1024]; 149 150 static void 151 rdsect(int fd, u_int blockno, u_int sectorsize) 152 { 153 int error; 154 155 lseek(fd, (off_t)blockno * sectorsize, SEEK_SET); 156 error = read(fd, sector, sectorsize); 157 if (error != (int)sectorsize) 158 err(1, "read error or disk too small for test."); 159 } 160 161 static void 162 rdmega(int fd) 163 { 164 int error; 165 166 error = read(fd, mega, sizeof(mega)); 167 if (error != sizeof(mega)) 168 err(1, "read error or disk too small for test."); 169 } 170 171 static struct timeval tv1, tv2; 172 173 static void 174 T0(void) 175 { 176 177 fflush(stdout); 178 sync(); 179 sleep(1); 180 sync(); 181 sync(); 182 gettimeofday(&tv1, NULL); 183 } 184 185 static void 186 TN(int count) 187 { 188 double dt; 189 190 gettimeofday(&tv2, NULL); 191 dt = (tv2.tv_usec - tv1.tv_usec) / 1e6; 192 dt += (tv2.tv_sec - tv1.tv_sec); 193 printf("%5d iter in %10.6f sec = %8.3f msec\n", 194 count, dt, dt * 1000.0 / count); 195 } 196 197 static void 198 TR(double count) 199 { 200 double dt; 201 202 gettimeofday(&tv2, NULL); 203 dt = (tv2.tv_usec - tv1.tv_usec) / 1e6; 204 dt += (tv2.tv_sec - tv1.tv_sec); 205 printf("%8.0f kbytes in %10.6f sec = %8.0f kbytes/sec\n", 206 count, dt, count / dt); 207 } 208 209 static void 210 speeddisk(int fd, off_t mediasize, u_int sectorsize) 211 { 212 int i; 213 uint b0, b1, sectorcount; 214 215 sectorcount = mediasize / sectorsize; 216 217 printf("Seek times:\n"); 218 printf("\tFull stroke:\t"); 219 b0 = 0; 220 b1 = sectorcount - 1 - 16384; 221 T0(); 222 for (i = 0; i < 125; i++) { 223 rdsect(fd, b0, sectorsize); 224 b0 += 16384; 225 rdsect(fd, b1, sectorsize); 226 b1 -= 16384; 227 } 228 TN(250); 229 230 printf("\tHalf stroke:\t"); 231 b0 = sectorcount / 4; 232 b1 = b0 + sectorcount / 2; 233 T0(); 234 for (i = 0; i < 125; i++) { 235 rdsect(fd, b0, sectorsize); 236 b0 += 16384; 237 rdsect(fd, b1, sectorsize); 238 b1 += 16384; 239 } 240 TN(250); 241 printf("\tQuarter stroke:\t"); 242 b0 = sectorcount / 4; 243 b1 = b0 + sectorcount / 4; 244 T0(); 245 for (i = 0; i < 250; i++) { 246 rdsect(fd, b0, sectorsize); 247 b0 += 16384; 248 rdsect(fd, b1, sectorsize); 249 b1 += 16384; 250 } 251 TN(500); 252 253 printf("\tShort forward:\t"); 254 b0 = sectorcount / 2; 255 T0(); 256 for (i = 0; i < 400; i++) { 257 rdsect(fd, b0, sectorsize); 258 b0 += 16384; 259 } 260 TN(400); 261 262 printf("\tShort backward:\t"); 263 b0 = sectorcount / 2; 264 T0(); 265 for (i = 0; i < 400; i++) { 266 rdsect(fd, b0, sectorsize); 267 b0 -= 16384; 268 } 269 TN(400); 270 271 printf("\tSeq outer:\t"); 272 b0 = 0; 273 T0(); 274 for (i = 0; i < 2048; i++) { 275 rdsect(fd, b0, sectorsize); 276 b0++; 277 } 278 TN(2048); 279 280 printf("\tSeq inner:\t"); 281 b0 = sectorcount - 2048 - 1; 282 T0(); 283 for (i = 0; i < 2048; i++) { 284 rdsect(fd, b0, sectorsize); 285 b0++; 286 } 287 TN(2048); 288 289 printf("Transfer rates:\n"); 290 printf("\toutside: "); 291 rdsect(fd, 0, sectorsize); 292 T0(); 293 for (i = 0; i < 100; i++) { 294 rdmega(fd); 295 } 296 TR(100 * 1024); 297 298 printf("\tmiddle: "); 299 b0 = sectorcount / 2; 300 rdsect(fd, b0, sectorsize); 301 T0(); 302 for (i = 0; i < 100; i++) { 303 rdmega(fd); 304 } 305 TR(100 * 1024); 306 307 printf("\tinside: "); 308 b0 = sectorcount - 100 * (1024*1024 / sectorsize) - 1;; 309 rdsect(fd, b0, sectorsize); 310 T0(); 311 for (i = 0; i < 100; i++) { 312 rdmega(fd); 313 } 314 TR(100 * 1024); 315 316 printf("\n"); 317 return; 318 } 319 320 static void 321 commandtime(int fd, off_t mediasize, u_int sectorsize) 322 { 323 double dtmega, dtsector; 324 int i; 325 326 printf("I/O command overhead:\n"); 327 i = mediasize; 328 rdsect(fd, 0, sectorsize); 329 T0(); 330 for (i = 0; i < 10; i++) 331 rdmega(fd); 332 gettimeofday(&tv2, NULL); 333 dtmega = (tv2.tv_usec - tv1.tv_usec) / 1e6; 334 dtmega += (tv2.tv_sec - tv1.tv_sec); 335 336 printf("\ttime to read 10MB block %10.6f sec\t= %8.3f msec/sector\n", 337 dtmega, dtmega*100/2048); 338 339 rdsect(fd, 0, sectorsize); 340 T0(); 341 for (i = 0; i < 20480; i++) 342 rdsect(fd, 0, sectorsize); 343 gettimeofday(&tv2, NULL); 344 dtsector = (tv2.tv_usec - tv1.tv_usec) / 1e6; 345 dtsector += (tv2.tv_sec - tv1.tv_sec); 346 347 printf("\ttime to read 20480 sectors %10.6f sec\t= %8.3f msec/sector\n", 348 dtsector, dtsector*100/2048); 349 printf("\tcalculated command overhead\t\t\t= %8.3f msec/sector\n", 350 (dtsector - dtmega)*100/2048); 351 352 printf("\n"); 353 return; 354 } 355