1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798
|
#ifndef lint
static char sccsid[] = "@(#)nhfsstone.c 1.22 90/05/08 Copyright (c) 1990, Legato Systems Inc";
#endif
/*
* Copyright (c) 1990 Legato Systems Inc.
*
* See DISCLAIMER file for restrictions
*
* Ported to Linux by Olaf Kirch <okir@monad.swb.de>
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/vfs.h>
#include <sys/stat.h>
#include <sys/wait.h>
#ifdef BSD
#include <sys/dir.h>
#define dirent direct
#else
#include <dirent.h>
#endif
#include <signal.h>
#ifndef NULL
#define NULL 0
#endif
/*
* Usage: nhfsstone [-v] [[-t secs] | [-c calls]] [-l load] [-p nprocs]
* [-m mixfile] [dir]...
*
* Generates an artifical NFS client load based on a given mix of
* operations.
*
* Strategy: loop for some number of NFS calls doing a random sleep
* followed by a call to one of the op generator routines. The routines
* are called based on a weighting factor determined by the difference
* between the current ops percentages (derived from kernel NFS stats)
* and a set of default percentages or a mix supplied by the caller.
*
* The generator routines try very hard to guess how many NFS operations
* they are generating so that the calling routine can keep a running
* estimate of the number of calls and the mix to avoid having to get
* the NFS statistics from the kernel too often.
*
* The operations are done in a directory that has a set of file names
* that are long enough that they won't be cached by the name cache
* in the kernel. The "lookup" operation steps through the names and
* creates a file if that name does not exist, or closes and reopens it
* if it does. This generates a table of open file descriptors. Most of the
* other operations are done on random descriptors in the table. The "getattr"
* operation tries to avoid the kernel attribute cache by doing "fstat"
* system calls on random descriptors in the table. There must be enough
* files in the directory so that, on average, the getattr operation hits
* any file less often than once each 6 seconds (the default timeout for
* the attributes cache).
*
* The parent process starts children to do the real work of generating load.
* The parent coordinates them so that they all start at the same time, and
* collects statistics from them when they are done. To coordinate the
* start up, the parent waits for each child to write one byte into
* a common log file (opened in append mode to avoid overwriting).
* After they write a byte the children pause, and the parent send SIGUSR1
* when it has heard from all of the kids. The children write their statistics
* into the same common log file and the parent reads and accumulates the
* statics and prints them out.
*
* This code will only compile and run on 4.X BSD based systems.
*/
#define DEFAULT_LOAD 30 /* default calls per sec */
#define DEFAULT_CALLS 5000 /* default number of calls */
#define NFILES 40 /* number of test files/dir */
#define BUFSIZE 8192 /* block size for read and write */
#define MAXFILESIZE 32 /* size, in blocks, of large file */
#define SAMPLETIME 5 /* secs between samples of NFS stats */
#define NPROCS 7 /* number of children to run */
/*
* The names of NFS operations
*/
char *Opnames[] = {
"null", "getattr", "setattr", "root", "lookup", "readlink", "read",
"wrcache", "write", "create", "remove", "rename", "link", "symlink",
"mkdir", "rmdir", "readdir", "fsstat",
};
/*
* NFS operation numbers
*
* Used to index the Opnames, Mix and statistics arrays.
*/
#define NOPS 18 /* number of NFS ops */
#define NULLCALL 0
#define GETATTR 1
#define SETATTR 2
#define ROOT 3
#define LOOKUP 4
#define READLINK 5
#define READ 6
#define WRCACHE 7
#define WRITE 8
#define CREATE 9
#define REMOVE 10
#define RENAME 11
#define LINK 12
#define SYMLINK 13
#define MKDIR 14
#define RMDIR 15
#define READDIR 16
#define FSSTAT 17
/*
* Operations counts
*/
struct count {
int total;
int calls[NOPS];
};
/*
* Software development mix for server with about 50/50 mix of
* diskless and diskful clients running SunOS 4.0.
*/
int Mix[NOPS] = {
0, /* null */
13, /* getattr */
1, /* setattr */
0, /* root */
34, /* lookup */
8, /* readlink */
22, /* read */
0, /* wrcache */
15, /* write */
2, /* create */
1, /* remove */
0, /* rename */
0, /* link */
0, /* symlink */
0, /* mkdir */
0, /* rmdir */
3, /* readdir */
1, /* fsstat */
};
/* Prototype decls */
int setmix(FILE *fp);
void usage(void);
void init_logfile(void);
void init_counters(void);
void get_delta(struct count *start, struct count *cur);
void init_testdir(int dirnum, char *parentdir);
void do_op(int rpct);
void op(int opnum);
void nextfile(void);
int createfile(void);
int openfile(void);
int writefile(void);
void collect_counters(void);
int check_counters(void);
void print(void);
void msec_sleep(int msecs);
void get_opct(struct count *count);
int substr(char *sp, char *subsp);
int check_access(struct stat statb);
void error(char *str);
/*
* NFS operations generator routines
*/
int op_null();
int op_getattr();
int op_setattr();
int op_root();
int op_lookup();
int op_readlink();
int op_read();
int op_wrcache();
int op_write();
int op_create();
int op_remove();
int op_rename();
int op_link();
int op_symlink();
int op_mkdir();
int op_rmdir();
int op_readdir();
int op_fsstat();
/*
* Operations generator vector
*/
struct op_vect {
int (*funct)(); /* op */
} Op_vect[NOPS] = {
{ op_null },
{ op_getattr },
{ op_setattr },
{ op_root },
{ op_lookup },
{ op_readlink },
{ op_read },
{ op_wrcache },
{ op_write },
{ op_create },
{ op_remove },
{ op_rename },
{ op_link },
{ op_symlink },
{ op_mkdir },
{ op_rmdir },
{ op_readdir },
{ op_fsstat },
};
/*
* Name sub-strings
*/
#define DIRSTR "dir" /* directory */
#define SYMSTR "sym" /* symbolic link */
#define LINSTR "lin" /* hard link */
struct timeval Optime[NOPS+1]; /* cumulative running time for ops */
struct count Curct; /* total number ops called */
int Openfd[NFILES]; /* open file descriptors */
int Curnum; /* current file number */
int Symnum; /* current symlink file number */
int Linknum; /* current link file number */
int Dirnum; /* current directory number */
DIR *Testdir; /* my test directory */
char Testdirname[MAXNAMLEN*2]; /* my test directory name */
char Curname[MAXNAMLEN]; /* current file name */
char Dirname[MAXNAMLEN]; /* current directory name */
char Symname[MAXNAMLEN]; /* symlink file name */
char Linkname[MAXNAMLEN]; /* link file name */
char *Otherspec = "%s/%03d"; /* sprintf spec for other names */
char *Rename1 = "rename1"; /* first name of rename pair */
char *Rename2 = "rename2"; /* second name of rename pair */
char *Symlinkpath = "./symlinknamelongstuff";
/* symlink file data */
char *Myname; /* name program invoked under */
char Namebuf[MAXNAMLEN]; /* unique name for this program */
int Log; /* synchronization log */
char Logname[MAXNAMLEN]; /* synchronization log name */
int Kmem; /* /dev/kmem file descriptor */
off_t Statoffset; /* offset to op count in NFS stats */
int Nprocs; /* sub-processes started */
int Verbose; /* print more info */
int Testop = -1; /* operation to test */
int Saveerrno; /* place to save errno */
#define subtime(t1, t2) {if ((t1.tv_usec -= t2.tv_usec) >= 1000000) {\
t1.tv_sec += (t1.tv_usec / 1000000); \
t1.tv_usec %= 1000000; \
} else if (t1.tv_usec < 0) { \
t1.tv_usec += 1000000; \
t1.tv_sec--; \
} \
t1.tv_sec -= t2.tv_sec; \
}
#define addtime(t1, t2) {if ((t1.tv_usec += t2.tv_usec) >= 1000000) {\
t1.tv_sec += (t1.tv_usec / 1000000); \
t1.tv_usec %= 1000000; \
} else if (t1.tv_usec < 0) { \
t1.tv_usec += 1000000; \
t1.tv_sec--; \
} \
t1.tv_sec += t2.tv_sec; \
}
/*
* Used to catch the parent's "start" signal
*/
void
startup()
{
return;
}
/*
* Clean up and exit
*/
void
cleanup()
{
(void) unlink(Logname);
exit(1);
}
int
main(int argc, char **argv)
{
int runtime; /* length of run, in seconds */
int load; /* load factor, in client loads */
int ncalls; /* total number of calls to make */
int avgmspc; /* average millisec per call */
int mspc; /* millisec per call */
int wantcalls; /* ncalls that should have happend by now */
int pid; /* process id */
int delay; /* msecs since last checked current time */
int randnum; /* a random number */
int oldmask; /* saved signal mask */
int sampletime; /* secs between reading kernel stats */
char *opts; /* option parsing */
int pct;
int procnum;
FILE *fp;
struct timeval curtime;
struct timeval starttime;
struct count startct;
struct stat statb;
char workdir[MAXPATHLEN];
char *getwd();
Myname = argv[0];
argc--;
argv++;
load = DEFAULT_LOAD;
ncalls = 0;
runtime = 0;
Nprocs = NPROCS;
pid = 0;
(void) umask(0);
/*
* Parse options
*/
while (argc && **argv == '-') {
opts = &argv[0][1];
while (*opts) {
switch (*opts) {
case 'c':
/*
* Set number of calls
*/
if (!isdigit(argv[1][0])) {
(void) fprintf(stderr,
"%s: illegal calls value %s\n",
Myname, argv[1]);
exit(1);
}
ncalls = atoi(argv[1]);
argv++;
argc--;
break;
case 'l':
/*
* Set load
*/
if (!isdigit(argv[1][0])) {
(void) fprintf(stderr,
"%s: illegal load value %s\n",
Myname, argv[1]);
exit(1);
}
load = atoi(argv[1]);
argv++;
argc--;
break;
case 'm':
/*
* Set mix from a file
*/
if ((fp = fopen(argv[1], "r")) == NULL) {
Saveerrno = errno;
(void) fprintf(stderr,
"%s: bad mix file", Myname);
errno = Saveerrno;
perror("");
exit(1);
}
if (setmix(fp) < 0) {
exit(1);
}
(void) fclose(fp);
argv++;
argc--;
break;
case 'p':
/*
* Set number of child processes
*/
if (!isdigit(argv[1][0])) {
(void) fprintf(stderr,
"%s: illegal procs value %s\n",
Myname, argv[1]);
exit(1);
}
Nprocs = atoi(argv[1]);
argv++;
argc--;
break;
case 'T':
/*
* Set test mode, number following is opnum
*/
if (!isdigit(argv[1][0])) {
(void) fprintf(stderr,
"%s: illegal test value %s\n",
Myname, argv[1]);
exit(1);
}
Testop = atoi(argv[1]);
if (Testop >= NOPS) {
(void) fprintf(stderr,
"%s: illegal test value %d\n",
Myname, Testop);
exit(1);
}
argv++;
argc--;
break;
case 't':
/*
* Set running time
*/
if (!isdigit(argv[1][0])) {
(void) fprintf(stderr,
"%s: illegal time value %s\n",
Myname, argv[1]);
exit(1);
}
runtime = atoi(argv[1]);
argv++;
argc--;
break;
case 'v':
/*
* Set verbose mode
*/
Verbose++;
break;
default:
usage();
exit(1);
}
opts++;
}
argv++;
argc--;
}
init_logfile(); /* Set up synchronizatin log file */
if (getcwd(workdir, sizeof(workdir)) == (char *) 0) {
Saveerrno = errno;
(void) fprintf(stderr,
"%s: can't find current directory ", Myname);
errno = Saveerrno;
perror("");
exit(1);
}
(void) signal(SIGINT, cleanup);
(void) signal(SIGUSR1, startup);
oldmask = sigblock(sigmask(SIGUSR1));
if (ncalls == 0) {
if (runtime == 0) {
ncalls = DEFAULT_CALLS;
} else {
ncalls = runtime * load;
}
}
avgmspc = Nprocs * 1000 / load;
/*
* Fork kids
*/
for (procnum = 0; procnum < Nprocs; procnum++) {
if ((pid = fork()) == -1) {
Saveerrno = errno;
(void) fprintf(stderr, "%s: can't fork ", Myname);
errno = Saveerrno;
perror("");
(void) kill(0, SIGINT);
exit(1);
}
/*
* Kids go initialize
*/
if (pid == 0) {
break;
}
}
/*
* Parent: wait for kids to get ready, start them, wait for them to
* finish, read and accumulate results.
*/
if (pid != 0) {
/*
* wait for kids to initialize
*/
do {
sleep(1);
if (fstat(Log, &statb) == -1) {
(void) fprintf(stderr, "%s: can't stat log %s",
Myname, Logname);
(void) kill(0, SIGINT);
exit(1);
}
} while (statb.st_size != Nprocs);
if (ftruncate(Log, 0L) == -1) {
(void) fprintf(stderr, "%s: can't truncate log %s",
Myname, Logname);
(void) kill(0, SIGINT);
exit(1);
}
sync();
sleep(3);
/*
* Be sure there isn't something else going on
*/
get_opct(&startct);
msec_sleep(2000);
get_delta(&startct, &Curct);
if (Curct.total > 20) {
(void) fprintf(stderr,
"%s: too much background activity (%d calls/sec)\n",
Myname, Curct.total);
(void) kill(0, SIGINT);
exit(1);
}
/*
* get starting stats
*/
get_opct(&startct);
/*
* Start kids
*/
(void) kill(0, SIGUSR1);
/*
* Kids started, wait for first one to finish, signal the
* rest and wait for them to finish.
*/
if (wait((union wait *) 0) != -1) {
(void) kill(0, SIGUSR1);
while (wait((union wait *) 0) != -1)
/* nothing */;
}
/*
* Initialize and sum up counters
*/
init_counters();
get_delta(&startct, &Curct);
collect_counters();
if (check_counters() == -1) {
Verbose = 1;
}
print();
(void) close(Log);
(void) unlink(Logname);
exit(0);
}
/*
* Children: initialize, then notify parent through log file,
* wait to get signal, beat the snot out of the server, write
* stats to the log file, and exit.
*/
/*
* Change my name for error logging
*/
(void) sprintf(Namebuf, "%s%d", Myname, procnum);
Myname = Namebuf;
/*
* Initialize and cd to test directory
*/
if (argc != 0) {
init_testdir(procnum, argv[procnum % argc]);
} else {
init_testdir(procnum, ".");
}
if ((Testdir = opendir(".")) == NULL) {
Saveerrno = errno;
(void) fprintf(stderr,
"%s: can't open test directory ", Myname);
errno = Saveerrno;
perror(Testdirname);
exit(1);
}
init_counters();
srandom(procnum+1);
/*
* Tell parent I'm ready then wait for go ahead
*/
if (write(Log, " ", 1) != 1) {
(void) fprintf(stderr, "%s: can't write sync file %s",
Myname, Logname);
(void) kill(0, SIGINT);
exit(1);
}
sigpause(oldmask);
/*
* Initialize counters
*/
get_opct(&startct);
(void) gettimeofday(&starttime, (struct timezone *)NULL);
sampletime = starttime.tv_sec + ((int) random()) % (2 * SAMPLETIME);
curtime = starttime;
/*
* Do pseudo NFS operations and adapt to dynamic changes in load
* by adjusting the sleep time between operations based on the
* number of calls that should have occured since starttime and
* the number that have actually occured. A delay is used to avoid
* doing gettimeofday calls too often, and a sampletime is
* used to avoid reading kernel NFS stats too often.
* If parent interrupts, get out and clean up.
*/
delay = 0;
mspc = avgmspc;
for (;;) {
randnum = (int) random();
if (mspc > 0) {
msec_sleep(randnum % (mspc << 1));
}
/*
* Do the NFS operation
* We use a random number from 0-199 to avoid starvation
* of the operations at the end of the mix.
*/
do_op(randnum % 200);
/*
* Do a gettimeofday call only once per second
*/
delay += mspc;
if (delay > 1000 || Curct.total >= ncalls) {
delay = 0;
(void) gettimeofday(&curtime, (struct timezone *)NULL);
/*
* If sample time is up, check the kernel stats
* and adjust our parameters to either catch up or
* slow down.
*/
if (curtime.tv_sec > sampletime ||
Curct.total >= ncalls) {
sampletime = curtime.tv_sec + SAMPLETIME;
get_delta(&startct, &Curct);
if (Curct.total >= ncalls) {
break;
}
wantcalls =
((curtime.tv_sec - starttime.tv_sec) * 1000
+(curtime.tv_usec-starttime.tv_usec) / 1000)
* Nprocs / avgmspc;
pct = 1000 * (Curct.total - wantcalls) / ncalls;
mspc = avgmspc + avgmspc * pct / 20;
if (mspc <= 0) {
/*
* mspc must be positive or we will
* never advance time.
*/
mspc = 10;
}
}
}
}
/*
* Store total time in last slot of counts array
*/
Optime[NOPS].tv_sec = curtime.tv_sec - starttime.tv_sec;
Optime[NOPS].tv_usec = curtime.tv_usec - starttime.tv_usec;
/*
* write stats to log file (append mode)
*/
if (write(Log, (char *)Optime, sizeof (Optime)) == -1) {
Saveerrno = errno;
(void) fprintf(stderr, "%s: can't write log ", Myname);
errno = Saveerrno;
perror("");
(void) kill(0, SIGINT);
exit(1);
}
(void) close(Log);
exit(0);
}
/*
* Initialize test directory
*
* If the directory already exists, check to see that all of the
* files exist and we can write them. If directory doesn't exist
* create it and fill it using the LOOKUP and WRITE ops.
* Chdir to the directory.
*/
void
init_testdir(int dirnum, char *parentdir)
{
int i;
int fd;
char cmd[256];
struct stat statb;
(void) sprintf(Testdirname, "%s/testdir%d", parentdir, dirnum);
if (stat(Testdirname, &statb) == -1) {
if (mkdir(Testdirname, 0777) == -1) {
Saveerrno = errno;
(void) fprintf(stderr,
"%s: can't create test directory ", Myname);
errno = Saveerrno;
perror(Testdirname);
(void) kill(0, SIGINT);
exit(1);
}
if (chdir(Testdirname) == -1) {
Saveerrno = errno;
(void) fprintf(stderr,
"%s: can't cd to test directory ", Myname);
errno = Saveerrno;
perror(Testdirname);
(void) kill(0, SIGINT);
exit(1);
}
/*
* create some files with long names and average size
*/
for (i = 0; i < NFILES; i++) {
nextfile();
(void) createfile();
if (Openfd[Curnum] == 0 || writefile() == 0) {
Saveerrno = errno;
(void) fprintf(stderr,
"%s: can't create test file '%s'\n",
Myname, Curname);
errno = Saveerrno;
perror(Testdirname);
(void) kill(0, SIGINT);
exit(1);
}
}
} else {
if (chdir(Testdirname) == -1) {
Saveerrno = errno;
(void) fprintf(stderr,
"%s: can't cd to test directory ", Myname);
errno = Saveerrno;
perror(Testdirname);
(void) kill(0, SIGINT);
exit(1);
}
/*
* Verify that we can read and write the test dir
*/
if (check_access(statb) == -1) {
(void) fprintf(stderr,
"%s: wrong permissions on test dir %s\n",
Myname, Testdirname);
(void) kill(0, SIGINT);
exit(1);
}
/*
* Verify that we can read and write all the files
*/
for (i = 0; i < NFILES; i++) {
nextfile();
if (stat(Curname, &statb) == -1 || statb.st_size == 0) {
/*
* File doesn't exist or is 0 size
*/
(void) createfile();
if (Openfd[Curnum] == 0 || writefile() == 0) {
(void) kill(0, SIGINT);
exit(1);
}
} else if (check_access(statb) == -1) {
/*
* should try to remove and recreate it
*/
(void) fprintf(stderr,
"%s: wrong permissions on testfile %s\n",
Myname, Curname);
(void) kill(0, SIGINT);
exit(1);
} else if (Openfd[Curnum] == 0) {
(void) openfile();
if (Openfd[Curnum] == 0) {
(void) kill(0, SIGINT);
exit(1);
}
}
}
}
/*
* Start with Rename1 and no Rename2 so the
* rename op can ping pong back and forth.
*/
(void) unlink(Rename2);
if ((fd = open(Rename1, O_CREAT|O_TRUNC|O_RDWR, 0666)) == -1) {
Saveerrno = errno;
(void) fprintf(stderr, "%s: can't create rename file ", Myname);
errno = Saveerrno;
perror(Rename1);
(void) kill(0, SIGINT);
exit(1);
}
/*
* Remove and recreate the test sub-directories
* for mkdir symlink and hard link.
*/
(void) sprintf(cmd, "rm -rf %s %s %s", DIRSTR, SYMSTR, LINSTR);
if (system(cmd) != 0) {
(void) fprintf(stderr, "%s: can't %s\n", Myname, cmd);
(void) kill(0, SIGINT);
exit(1);
}
if (mkdir(DIRSTR, 0777) == -1) {
(void) fprintf(stderr,
"%s: can't create subdir %s\n", Myname, DIRSTR);
(void) kill(0, SIGINT);
exit(1);
}
if (mkdir(SYMSTR, 0777) == -1) {
(void) fprintf(stderr,
"%s: can't create subdir %s\n", Myname, SYMSTR);
(void) kill(0, SIGINT);
exit(1);
}
op(SYMLINK);
if (mkdir(LINSTR, 0777) == -1) {
(void) fprintf(stderr, "%s: can't create subdir %s\n", Myname,
LINSTR);
(void) kill(0, SIGINT);
exit(1);
}
(void) close(fd);
}
/*
* The routines below attempt to do over-the-wire operations.
* Each op tries to cause one or more of a particular
* NFS operation to go over the wire. OPs return the number
* of OTW calls they think they have generated.
*
* An array of open file descriptors is kept for the files in each
* test directory. The open fd's are used to get access to the files
* without generating lookups. An fd value of 0 mean the corresponding
* file name is closed. Ops that need a name use Curname.
*/
/*
* Call an op based on a random number and the current
* op calling weights. Op weights are derived from the
* mix percentage and the current NFS stats mix percentage.
*/
void
do_op(int rpct)
{
int opnum;
int weight;
int oppct;
if (Testop != -1) {
nextfile();
op(Testop);
return;
}
for (opnum = rpct % NOPS; rpct >= 0; opnum = (opnum + 1) % NOPS) {
if (Curct.total) {
oppct = (Curct.calls[opnum] * 100) / Curct.total;
} else {
oppct = 0;
}
/*
* Weight is mix percent - (how far off we are * fudge)
* fudge factor is required because some ops (read, write)
* generate many NFS calls for a single op call
*/
weight = Mix[opnum] - ((oppct - Mix[opnum]) << 4);
if (weight <= 0) {
continue;
}
rpct -= weight;
if (rpct < 0) {
if (opnum == RMDIR && Dirnum == 0) {
op(MKDIR);
} else if (opnum != CREATE && opnum != LOOKUP &&
opnum != REMOVE) {
nextfile();
}
op(opnum);
if (Openfd[Curnum] == 0) {
op(CREATE);
#ifdef XXX
op(WRITE);
#endif /* XXX */
}
return;
}
}
}
/*
* Call an op generator and keep track of its running time
*/
void
op(int opnum)
{
struct timeval start;
struct timeval stop;
int nops;
(void) gettimeofday(&start, (struct timezone *)NULL);
nops = (*Op_vect[opnum].funct)();
(void) gettimeofday(&stop, (struct timezone *)NULL);
stop.tv_sec -= start.tv_sec;
stop.tv_usec -= start.tv_usec;
#ifdef SUNOS4
/*
* SunOS 4.0 does a lookup and a getattr on each open
* so we have to account for that in the getattr op
*/
if (opnum == GETATTR && nops == 2) {
nops = 1;
stop.tv_sec /= 2;
stop.tv_usec /= 2;
Curct.total += Nprocs;
Curct.calls[LOOKUP] += Nprocs;
addtime(Optime[LOOKUP], stop);
}
#endif
nops *= Nprocs;
Curct.total += nops;
Curct.calls[opnum] += nops;
addtime(Optime[opnum], stop);
}
/*
* Advance file number (Curnum) and name (Curname)
*/
void
nextfile(void)
{
static char *numpart = NULL;
int num;
Curnum = (Curnum + 1) % NFILES;
if (numpart == NULL) {
(void) sprintf(Curname, "%03dabcdefghijklmn", Curnum);
numpart = Curname;
} else {
num = Curnum;
numpart[0] = '0' + num / 100;
num %= 100;
numpart[1] = '0' + num / 10;
num %= 10;
numpart[2] = '0' + num;
}
}
int
createfile(void)
{
int ret;
int fd;
ret = 0;
fd = Openfd[Curnum];
if ((fd && close(fd) == -1) ||
(fd = open(Curname, O_CREAT|O_RDWR|O_TRUNC, 0666)) == -1) {
fd = 0;
ret = -1;
error("create");
}
Openfd[Curnum] = fd;
return (ret);
}
int
openfile(void)
{
int ret;
int fd;
ret = 0;
fd = Openfd[Curnum];
if (fd == 0 && (fd = open(Curname, O_RDWR, 0666)) == -1) {
fd = 0;
ret = -1;
error("open");
}
Openfd[Curnum] = fd;
return (ret);
}
int
writefile(void)
{
int fd;
int wrote;
int bufs;
int size;
int randnum;
char buf[BUFSIZE];
fd = Openfd[Curnum];
if (lseek(fd, 0L, 0) == (off_t) -1) {
error("write: lseek");
return (-1);
}
randnum = (int) random();
bufs = randnum % 100; /* using this for distribution desired */
/*
* Attempt to create a distribution of file sizes
* to reflect reality. Most files are small,
* but there are a few files that are very large.
*
* The sprite paper (USENIX 198?) claims :
* 50% of all files are < 2.5K
* 80% of all file accesses are to files < 10K
* 40% of all file I/O is to files > 25K
*
* static examination of the files in our file system
* seems to support the claim that 50% of all files are
* smaller than 2.5K
*/
if (bufs < 50) {
bufs = (randnum % 3) + 1;
size = 1024;
} else if (bufs < 97) {
bufs = (randnum % 6) + 1;
size = BUFSIZE;
} else {
bufs = MAXFILESIZE;
size = BUFSIZE;
}
for (wrote = 0; wrote < bufs; wrote++) {
if (write(fd, buf, size) == -1) {
error("write");
break;
}
}
return (wrote);
}
int
op_null(void)
{
return (1);
}
/*
* Generate a getattr call by fstat'ing the current file
* or by closing and re-opening it. This helps to keep the
* attribute cache cold.
*/
int
op_getattr(void)
{
struct stat statb;
if ((random() % 2) == 0) {
(void) close(Openfd[Curnum]);
Openfd[Curnum] = 0;
if (openfile() == -1) {
return (0);
}
return (2);
}
if (fstat(Openfd[Curnum], &statb) == -1) {
error("getattr");
}
return (1);
}
int op_setattr(void)
{
if (fchmod(Openfd[Curnum], 0666) == -1) {
error("setattr");
}
return (1);
}
int op_root(void)
{
error("root");
return (0);
}
/*
* Generate a lookup by stat'ing the current name.
*/
int op_lookup(void)
{
struct stat statb;
if (stat(Curname, &statb) == -1) {
error("lookup");
}
return (1);
}
int op_read(void)
{
int got;
int bufs;
int fd;
char buf[BUFSIZE];
bufs = 0;
fd = Openfd[Curnum];
if (lseek(fd, 0L, 0) == (off_t) -1) {
error("read: lseek");
return (0);
}
while ((got = read(fd, buf, sizeof (buf))) > 0) {
bufs++;
}
if (got == -1) {
error("read");
} else {
bufs++; /* did one extra read to find EOF */
}
return (bufs);
}
int op_wrcache(void)
{
error("wrcache");
return 0;
}
int op_write(void)
{
int bufs;
bufs = writefile();
if (bufs == 0) {
return (0);
}
(void) fsync(Openfd[Curnum]);
return (bufs + 2);
}
int op_create(void)
{
if (createfile() == -1) {
return (0);
}
return (1);
}
int op_remove(void)
{
int fd;
int got;
if (Linknum > 0) {
got = unlink(Linkname);
Linknum--;
(void) sprintf(Linkname, Otherspec, LINSTR, Linknum);
} else if (Symnum > 1) {
got = unlink(Symname);
Symnum--;
(void) sprintf(Symname, Otherspec, SYMSTR, Symnum);
} else {
fd = Openfd[Curnum];
if (fd && (close(fd) == -1)) {
error("remove: close");
}
Openfd[Curnum] = 0;
got = unlink(Curname);
}
if (got == -1) {
error("remove");
}
return (1);
}
int toggle = 0;
int op_rename(void)
{
int got;
if (toggle++ & 01) {
got = rename(Rename2, Rename1);
} else {
got = rename(Rename1, Rename2);
}
if (got == -1) {
error("rename");
}
return (1);
}
int op_link(void)
{
Linknum++;
(void) sprintf(Linkname, Otherspec, LINSTR, Linknum);
if (link(Curname, Linkname) == -1) {
error("link");
}
return (1);
}
int op_readlink(void)
{
char buf[MAXPATHLEN];
if (Symnum == 0) {
error("readlink");
return (0);
}
if (readlink(Symname, buf, sizeof (buf)) == -1) {
error("readlink");
}
return (1);
}
int op_symlink(void)
{
Symnum++;
(void) sprintf(Symname, Otherspec, SYMSTR, Symnum);
if (symlink(Symlinkpath, Symname) == -1) {
error("symlink");
}
return (1);
}
int op_mkdir(void)
{
Dirnum++;
(void) sprintf(Dirname, Otherspec, DIRSTR, Dirnum);
if (mkdir(Dirname, 0777) == -1) {
error("mkdir");
}
return (1);
}
int op_rmdir(void)
{
if (Dirnum == 0) {
error("rmdir");
return (0);
}
if (rmdir(Dirname) == -1) {
error("rmdir");
}
Dirnum--;
(void) sprintf(Dirname, Otherspec, DIRSTR, Dirnum);
return (1);
}
int op_readdir(void)
{
rewinddir(Testdir);
while (readdir(Testdir) != (struct dirent *)NULL)
/* nothing */;
return (1);
}
int op_fsstat(void)
{
struct statfs statfsb;
if (statfs(".", &statfsb) == -1) {
error("statfs");
}
return (1);
}
/*
* Utility routines
*/
/*
* Read counter arrays out of log file and accumulate them in "Optime"
*/
void
collect_counters(void)
{
int i;
int j;
(void) lseek(Log, 0L, 0);
for (i = 0; i < Nprocs; i++) {
struct timeval buf[NOPS+1];
if (read(Log, (char *)buf, sizeof (buf)) == -1) {
Saveerrno = errno;
(void) fprintf(stderr, "%s: can't read log ", Myname);
errno = Saveerrno;
perror("");
(void) kill(0, SIGINT);
exit(1);
}
for (j = 0; j < NOPS+1; j++) {
addtime(Optime[j], buf[j]);
}
}
}
/*
* Check consistance of results
*/
int
check_counters(void)
{
int i;
int mixdiff;
int got;
int want;
mixdiff = 0;
for (i = 0; i < NOPS; i++) {
got = Curct.calls[i] * 10000 / Curct.total;
want = Mix[i] * 100;
if (got > want) {
mixdiff += got - want;
} else {
mixdiff += want - got;
}
}
if (mixdiff > 1000) {
(void) fprintf(stdout,
"%s: INVALID RUN, mix generated is off by %d.%02d%%\n",
Myname, mixdiff / 100, mixdiff % 100);
return (-1);
}
return (0);
}
/*
* Print results
*/
void
print(void)
{
int totalmsec;
int runtime;
int msec;
int i;
totalmsec = 0;
for (i = 0; i < NOPS; i++) {
totalmsec += Optime[i].tv_sec * 1000;
totalmsec += Optime[i].tv_usec / 1000;
}
if (Verbose) {
const char *format = sizeof (Optime[0].tv_sec) == sizeof (long)
? "%-10s%3d%% %2d.%02d%% %6d %4ld.%02ld %4d.%02d %2d.%02d%%\n"
: "%-10s%3d%% %2d.%02d%% %6d %4d.%02d %4d.%02d %2d.%02d%%\n";
(void) fprintf(stdout,
"op want got calls secs msec/call time %%\n");
for (i = 0; i < NOPS; i++) {
msec = Optime[i].tv_sec * 1000
+ Optime[i].tv_usec / 1000;
(void) fprintf(stdout, format,
Opnames[i], Mix[i],
Curct.calls[i] * 100 / Curct.total,
(Curct.calls[i] * 100 % Curct.total)
* 100 / Curct.total,
Curct.calls[i],
Optime[i].tv_sec, Optime[i].tv_usec / 10000,
Curct.calls[i]
? msec / Curct.calls[i]
: 0,
Curct.calls[i]
? (msec % Curct.calls[i]) * 100 / Curct.calls[i]
: 0,
msec * 100 / totalmsec,
(msec * 100 % totalmsec) * 100 / totalmsec);
}
}
runtime = Optime[NOPS].tv_sec / Nprocs;
(void) fprintf(stdout,
"%d sec %d calls %d.%02d calls/sec %d.%02d msec/call\n",
runtime, Curct.total,
Curct.total / runtime,
((Curct.total % runtime) * 100) / runtime,
totalmsec / Curct.total,
((totalmsec % Curct.total) * 100) / Curct.total);
}
/*
* Use select to sleep for some number of milliseconds
* granularity is 20 msec
*/
void
msec_sleep(int msecs)
{
struct timeval sleeptime;
if (msecs < 20) {
return;
}
sleeptime.tv_sec = msecs / 1000;
sleeptime.tv_usec = (msecs % 1000) * 1000;
if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &sleeptime) == -1){
Saveerrno = errno;
(void) fprintf(stderr, "%s: select failed ", Myname);
errno = Saveerrno;
perror("");
(void) kill(0, SIGINT);
exit(1);
}
}
/*
* Open the synchronization file with append mode
*/
void
init_logfile(void)
{
(void) sprintf(Logname, "/tmp/nhfsstone%d", getpid());
if ((Log = open(Logname, O_RDWR|O_CREAT|O_TRUNC|O_APPEND, 0666)) == -1){
Saveerrno = errno;
(void) fprintf(stderr,
"%s: can't open log file %s ", Myname, Logname);
errno = Saveerrno;
perror("");
exit(1);
}
}
/*
* Zero counters
*/
void
init_counters(void)
{
int i;
Curct.total = 0;
for (i = 0; i < NOPS; i++) {
Curct.calls[i] = 0;
Optime[i].tv_sec = 0;
Optime[i].tv_usec = 0;
}
Optime[NOPS].tv_sec = 0;
Optime[NOPS].tv_usec = 0;
}
/*
* Set cur = cur - start
*/
void
get_delta(struct count *start, struct count *cur)
{
int i;
get_opct(cur);
cur->total -= start->total;
for (i = 0; i < NOPS; i++) {
cur->calls[i] -= start->calls[i];
}
}
/*
* Read kernel stats
*/
void
get_opct(struct count *count)
{
static FILE *fp = NULL;
char buffer[256];
int i;
if (fp == NULL && !(fp = fopen("/proc/net/rpc/nfs", "r"))) {
perror("/proc/net/rpc/nfs");
(void) kill(0, SIGINT);
exit(1);
} else {
fflush(fp);
rewind(fp);
}
while (fgets(buffer, sizeof(buffer), fp) != NULL) {
char *sp, *line = buffer;
if ((sp = strchr(line, '\n')) != NULL)
*sp = '\0';
if (!(sp = strtok(line, " \t")) || strcmp(line, "proc2"))
continue;
if (!(sp = strtok(NULL, " \t")))
goto bummer;
count->total = 0;
for (i = 0; i < 18; i++) {
if (!(sp = strtok(NULL, " \t")))
goto bummer;
/* printf("call %d -> %s\n", i, sp); */
count->calls[i] = atoi(sp);
count->total += count->calls[i];
}
/* printf("total calls %d\n", count->total); */
break;
}
return;
bummer:
fprintf(stderr, "parse error in /proc/net/rpc/nfs!\n");
kill(0, SIGINT);
exit(1);
}
#define LINELEN 128 /* max bytes/line in mix file */
#define MIX_START 0
#define MIX_DATALINE 1
#define MIX_DONE 2
#define MIX_FIRSTLINE 3
/*
* Mix file parser.
* Assumes that the input file is in the same format as
* the output of the nfsstat(8) command.
*
* Uses a simple state transition to keep track of what to expect.
* Parsing is done a line at a time.
*
* State Input action New state
* MIX_START ".*nfs:.*" skip one line MIX_FIRSTLINE
* MIX_FIRSTLINE ".*[0-9]*.*" get ncalls MIX_DATALINE
* MIX_DATALINE "[0-9]* [0-9]*%"X6 get op counts MIX_DATALINE
* MIX_DATALINE "[0-9]* [0-9]*%"X4 get op counts MIX_DONE
* MIX_DONE EOF return
*/
int
setmix(FILE *fp)
{
int state;
int got;
int opnum;
int calls;
int len;
char line[LINELEN];
state = MIX_START;
opnum = 0;
while (state != MIX_DONE && fgets(line, LINELEN, fp)) {
switch (state) {
case MIX_START:
len = strlen(line);
if (len >= 4 && substr(line, "nfs:")) {
if (fgets(line, LINELEN, fp) == NULL) {
(void) fprintf(stderr,
"%s: bad mix format: unexpected EOF after 'nfs:'\n", Myname);
return (-1);
}
state = MIX_FIRSTLINE;
}
break;
case MIX_FIRSTLINE:
got = sscanf(line, "%d", &calls);
if (got != 1) {
(void) fprintf(stderr,
"%s: bad mix format: can't find 'calls' value %d\n", Myname, got);
return (-1);
}
if (fgets(line, LINELEN, fp) == NULL) {
(void) fprintf(stderr,
"%s: bad mix format: unexpected EOF after 'calls'\n", Myname);
return (-1);
}
state = MIX_DATALINE;
break;
case MIX_DATALINE:
got = sscanf(line,
"%d %*d%% %d %*d%% %d %*d%% %d %*d%% %d %*d%% %d %*d%% %d %*d%%",
&Mix[opnum], &Mix[opnum+1], &Mix[opnum+2], &Mix[opnum+3],
&Mix[opnum+4], &Mix[opnum+5], &Mix[opnum+6]);
if (got == 4 && opnum == 14) {
/*
* looks like the last line
*/
state = MIX_DONE;
} else if (got == 7) {
opnum += 7;
if (fgets(line, LINELEN, fp) == NULL) {
(void) fprintf(stderr,
"%s: bad mix format: unexpected EOF after 'calls'\n", Myname);
return (-1);
}
} else {
(void) fprintf(stderr,
"%s: bad mix format: can't find %d op values\n", Myname, got);
return (-1);
}
break;
default:
(void) fprintf(stderr,
"%s: unknown state %d\n", Myname, state);
return (-1);
}
}
if (state != MIX_DONE) {
(void) fprintf(stderr,
"%s: bad mix format: unexpected EOF\n", Myname);
return (-1);
}
for (opnum = 0; opnum < NOPS; opnum++) {
Mix[opnum] = Mix[opnum] * 100 / calls
+ ((Mix[opnum] * 1000 / calls % 10) >= 5);
}
return (0);
}
/*
* return true if sp contains the substring subsp, false otherwise
*/
int
substr(char *sp, char *subsp)
{
int found;
int want;
char *s2;
if (sp == NULL || subsp == NULL) {
return (0);
}
want = strlen(subsp);
while (*sp != '\0') {
while (*sp != *subsp && *sp != '\0') {
sp++;
}
found = 0;
s2 = subsp;
while (*sp == *s2) {
sp++;
s2++;
found++;
}
if (found == want) {
return (1);
}
}
return (0);
}
/*
* check to make sure that we have
* both read and write permissions
* for this file or directory.
*/
int
check_access(struct stat statb)
{
int gidsetlen;
gid_t gidset[NGROUPS];
int i;
if (statb.st_uid == getuid()) {
if ((statb.st_mode & 0200) && (statb.st_mode & 0400)) {
return 1;
} else {
return -1;
}
}
gidsetlen = NGROUPS;
if (getgroups(gidsetlen, gidset) == -1) {
perror("getgroups");
return -1;
}
for (i = 0; i < NGROUPS; i++) {
if (statb.st_gid == gidset[i]) {
if ((statb.st_mode & 020) && (statb.st_mode & 040)) {
return 1;
} else {
return -1;
}
}
}
if ((statb.st_mode & 02) && (statb.st_mode & 04)) {
return 1;
} else {
return -1;
}
}
void
usage(void)
{
(void) fprintf(stderr, "usage: %s [-v] [[-t secs] | [-c calls]] [-l load] [-p nprocs] [-m mixfile] [dir]...\n", Myname);
}
void
error(char *str)
{
Saveerrno = errno;
(void) fprintf(stderr, "%s: op failed: %s ", Myname, str);
errno = Saveerrno;
perror("");
}
|