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 error = ioctl(fd, DIOCGIDENT, ident); 108 if (error) 109 ident[0] = '\0'; 110 if (!opt_v) { 111 printf("%s", argv[i]); 112 printf("\t%u", sectorsize); 113 printf("\t%jd", (intmax_t)mediasize); 114 printf("\t%jd", (intmax_t)mediasize/sectorsize); 115 if (fwsectors != 0 && fwheads != 0) { 116 printf("\t%jd", (intmax_t)mediasize / 117 (fwsectors * fwheads * sectorsize)); 118 printf("\t%u", fwheads); 119 printf("\t%u", fwsectors); 120 } 121 } else { 122 humanize_number(buf, 5, (int64_t)mediasize, "", 123 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); 124 printf("%s\n", argv[i]); 125 printf("\t%-12u\t# sectorsize\n", sectorsize); 126 printf("\t%-12jd\t# mediasize in bytes (%s)\n", 127 (intmax_t)mediasize, buf); 128 printf("\t%-12jd\t# mediasize in sectors\n", 129 (intmax_t)mediasize/sectorsize); 130 if (fwsectors != 0 && fwheads != 0) { 131 printf("\t%-12jd\t# Cylinders according to firmware.\n", (intmax_t)mediasize / 132 (fwsectors * fwheads * sectorsize)); 133 printf("\t%-12u\t# Heads according to firmware.\n", fwheads); 134 printf("\t%-12u\t# Sectors according to firmware.\n", fwsectors); 135 } 136 if (ident[0] != '\0') 137 printf("\t%-12s\t# Disk ident.\n", ident); 138 } 139 printf("\n"); 140 if (opt_c) 141 commandtime(fd, mediasize, sectorsize); 142 if (opt_t) 143 speeddisk(fd, mediasize, sectorsize); 144 close(fd); 145 } 146 exit (0); 147 } 148 149 150 static char sector[65536]; 151 static char mega[1024 * 1024]; 152 153 static void 154 rdsect(int fd, u_int blockno, u_int sectorsize) 155 { 156 int error; 157 158 lseek(fd, (off_t)blockno * sectorsize, SEEK_SET); 159 error = read(fd, sector, sectorsize); 160 if (error != (int)sectorsize) 161 err(1, "read error or disk too small for test."); 162 } 163 164 static void 165 rdmega(int fd) 166 { 167 int error; 168 169 error = read(fd, mega, sizeof(mega)); 170 if (error != sizeof(mega)) 171 err(1, "read error or disk too small for test."); 172 } 173 174 static struct timeval tv1, tv2; 175 176 static void 177 T0(void) 178 { 179 180 fflush(stdout); 181 sync(); 182 sleep(1); 183 sync(); 184 sync(); 185 gettimeofday(&tv1, NULL); 186 } 187 188 static void 189 TN(int count) 190 { 191 double dt; 192 193 gettimeofday(&tv2, NULL); 194 dt = (tv2.tv_usec - tv1.tv_usec) / 1e6; 195 dt += (tv2.tv_sec - tv1.tv_sec); 196 printf("%5d iter in %10.6f sec = %8.3f msec\n", 197 count, dt, dt * 1000.0 / count); 198 } 199 200 static void 201 TR(double count) 202 { 203 double dt; 204 205 gettimeofday(&tv2, NULL); 206 dt = (tv2.tv_usec - tv1.tv_usec) / 1e6; 207 dt += (tv2.tv_sec - tv1.tv_sec); 208 printf("%8.0f kbytes in %10.6f sec = %8.0f kbytes/sec\n", 209 count, dt, count / dt); 210 } 211 212 static void 213 speeddisk(int fd, off_t mediasize, u_int sectorsize) 214 { 215 int i; 216 uint b0, b1, sectorcount; 217 218 sectorcount = mediasize / sectorsize; 219 220 printf("Seek times:\n"); 221 printf("\tFull stroke:\t"); 222 b0 = 0; 223 b1 = sectorcount - 1 - 16384; 224 T0(); 225 for (i = 0; i < 125; i++) { 226 rdsect(fd, b0, sectorsize); 227 b0 += 16384; 228 rdsect(fd, b1, sectorsize); 229 b1 -= 16384; 230 } 231 TN(250); 232 233 printf("\tHalf stroke:\t"); 234 b0 = sectorcount / 4; 235 b1 = b0 + sectorcount / 2; 236 T0(); 237 for (i = 0; i < 125; i++) { 238 rdsect(fd, b0, sectorsize); 239 b0 += 16384; 240 rdsect(fd, b1, sectorsize); 241 b1 += 16384; 242 } 243 TN(250); 244 printf("\tQuarter stroke:\t"); 245 b0 = sectorcount / 4; 246 b1 = b0 + sectorcount / 4; 247 T0(); 248 for (i = 0; i < 250; i++) { 249 rdsect(fd, b0, sectorsize); 250 b0 += 16384; 251 rdsect(fd, b1, sectorsize); 252 b1 += 16384; 253 } 254 TN(500); 255 256 printf("\tShort forward:\t"); 257 b0 = sectorcount / 2; 258 T0(); 259 for (i = 0; i < 400; i++) { 260 rdsect(fd, b0, sectorsize); 261 b0 += 16384; 262 } 263 TN(400); 264 265 printf("\tShort backward:\t"); 266 b0 = sectorcount / 2; 267 T0(); 268 for (i = 0; i < 400; i++) { 269 rdsect(fd, b0, sectorsize); 270 b0 -= 16384; 271 } 272 TN(400); 273 274 printf("\tSeq outer:\t"); 275 b0 = 0; 276 T0(); 277 for (i = 0; i < 2048; i++) { 278 rdsect(fd, b0, sectorsize); 279 b0++; 280 } 281 TN(2048); 282 283 printf("\tSeq inner:\t"); 284 b0 = sectorcount - 2048 - 1; 285 T0(); 286 for (i = 0; i < 2048; i++) { 287 rdsect(fd, b0, sectorsize); 288 b0++; 289 } 290 TN(2048); 291 292 printf("Transfer rates:\n"); 293 printf("\toutside: "); 294 rdsect(fd, 0, sectorsize); 295 T0(); 296 for (i = 0; i < 100; i++) { 297 rdmega(fd); 298 } 299 TR(100 * 1024); 300 301 printf("\tmiddle: "); 302 b0 = sectorcount / 2; 303 rdsect(fd, b0, sectorsize); 304 T0(); 305 for (i = 0; i < 100; i++) { 306 rdmega(fd); 307 } 308 TR(100 * 1024); 309 310 printf("\tinside: "); 311 b0 = sectorcount - 100 * (1024*1024 / sectorsize) - 1;; 312 rdsect(fd, b0, sectorsize); 313 T0(); 314 for (i = 0; i < 100; i++) { 315 rdmega(fd); 316 } 317 TR(100 * 1024); 318 319 printf("\n"); 320 return; 321 } 322 323 static void 324 commandtime(int fd, off_t mediasize, u_int sectorsize) 325 { 326 double dtmega, dtsector; 327 int i; 328 329 printf("I/O command overhead:\n"); 330 i = mediasize; 331 rdsect(fd, 0, sectorsize); 332 T0(); 333 for (i = 0; i < 10; i++) 334 rdmega(fd); 335 gettimeofday(&tv2, NULL); 336 dtmega = (tv2.tv_usec - tv1.tv_usec) / 1e6; 337 dtmega += (tv2.tv_sec - tv1.tv_sec); 338 339 printf("\ttime to read 10MB block %10.6f sec\t= %8.3f msec/sector\n", 340 dtmega, dtmega*100/2048); 341 342 rdsect(fd, 0, sectorsize); 343 T0(); 344 for (i = 0; i < 20480; i++) 345 rdsect(fd, 0, sectorsize); 346 gettimeofday(&tv2, NULL); 347 dtsector = (tv2.tv_usec - tv1.tv_usec) / 1e6; 348 dtsector += (tv2.tv_sec - tv1.tv_sec); 349 350 printf("\ttime to read 20480 sectors %10.6f sec\t= %8.3f msec/sector\n", 351 dtsector, dtsector*100/2048); 352 printf("\tcalculated command overhead\t\t\t= %8.3f msec/sector\n", 353 (dtsector - dtmega)*100/2048); 354 355 printf("\n"); 356 return; 357 } 358