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
|
/*
* Copyright (C) 2009 The Android Open Source Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <limits.h>
#include <string.h>
#include <sys/stat.h>
#include <linux/fadvise.h>
#include <unistd.h>
#include <fts.h>
#include "stopwatch.h"
#include "sysutil.h"
#include "testcase.h"
// Stress test for the sdcard. Use this to generate some load on the
// sdcard and collect performance statistics. The output is either a
// human readable report or the raw timing samples that can be
// processed using another tool.
//
// Needs debugfs:
// adb root;
// adb shell mount -t debugfs none /sys/kernel/debug
//
// The following tests are defined (value of the --test flag):
// write: Open a file write some random data and close.
// read: Open a file read it and close.
// read_write: Combine readers and writers.
// open_create: Open|create an non existing file.
//
// For each run you can control how many processes will run the test in
// parallel to simulate a real load (--procnb flag)
//
// For each process, the test selected will be executed many time to
// get a meaningful average/min/max (--iterations flag)
//
// Use --dump to also get the raw data.
//
// For read/write tests, size is the number of Kbytes to use.
//
// To build: mmm system/extras/tests/sdcard/Android.mk SDCARD_TESTS=1
//
// Examples:
// adb shell /system/bin/sdcard_perf_test --test=read --size=1000 --chunk-size=100 --procnb=1 --iterations=10 --dump > /tmp/data.txt
// adb shell /system/bin/sdcard_perf_test --test=write --size=1000 --chunk-size=100 --procnb=1 --iterations=100 --dump > /tmp/data.txt
//
// To watch the memory: cat /proc/meminfo
// If the phone crashes, look at /proc/last_kmsg on reboot.
//
// TODO: It would be cool if we could play with various fadvise()
// strategies in here to see how tweaking read-ahead changes things.
//
extern char *optarg;
extern int optind, opterr, optopt;
// TODO: No clue where fadvise is. Disabled for now.
#define FADVISE(fd, off, len, advice) (void)0
#ifndef min
#define min(a,b) (((a)>(b))?(b):(a))
#endif
namespace {
using android::kernelVersion;
using android::kMinKernelVersionBufferSize;
using android::schedFeatures;
using android::kMinSchedFeaturesBufferSize;
using android_test::StopWatch;
using android::writePidAndWaitForReply;
using android::waitForChildrenAndSignal;
using android::waitForChildrenOrExit;
using android_test::TestCase;
const char *kAppName = "sdcard_perf_test";
const char *kTestDir = "/sdcard/perf";
const bool kVerbose = false;
// Used by getopt to parse the command line.
struct option long_options[] = {
{"size", required_argument, 0, 's'},
{"chunk-size", required_argument, 0, 'S'},
{"depth", required_argument, 0, 'D'},
{"iterations", required_argument, 0, 'i'},
{"procnb", required_argument, 0, 'p'},
{"test", required_argument, 0, 't'},
{"dump", no_argument, 0, 'd'},
{"cpu-scaling", no_argument, 0, 'c'},
{"sync", required_argument, 0, 'f'},
{"truncate", no_argument, 0, 'e'},
{"no-new-fair-sleepers", no_argument, 0, 'z'},
{"no-normalized-sleepers", no_argument, 0, 'Z'},
{"fadvise", required_argument, 0, 'a'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0},
};
void usage()
{
printf("sdcard_perf_test --test=write|read|read_write|open_create|traverse [options]\n\n"
" -t --test: Select the test.\n"
" -s --size: Size in kbytes of the data.\n"
" -S --chunk-size: Size of a chunk. Default to size ie 1 chunk.\n"
" Data will be written/read using that chunk size.\n"
" -D --depth: Depth of directory tree to create for traversal.\n",
" -i --iterations: Number of time a process should carry its task.\n"
" -p --procnb: Number of processes to use.\n"
" -d --dump: Print the raw timing on stdout.\n"
" -c --cpu-scaling: Enable cpu scaling.\n"
" -s --sync: fsync|sync Use fsync or sync in write test. Default: no sync call.\n"
" -e --truncate: Truncate to size the file on creation.\n"
" -z --no-new-fair-sleepers: Turn them off. You need to mount debugfs.\n"
" -Z --no-normalized-sleepers: Turn them off. You need to mount debugfs.\n"
" -a --fadvise: Specify an fadvise policy (not supported).\n"
);
}
// Print command line, pid, kernel version, OOM adj and scheduler.
void printHeader(int argc, char **argv, const TestCase& testCase)
{
printf("# Command: ");
for (int i = 0; i < argc; ++i)
{
printf("%s ", argv[i]);
}
printf("\n");
printf("# Pid: %d\n", getpid());
{
char buffer[kMinKernelVersionBufferSize] = {0, };
if (kernelVersion(buffer, sizeof(buffer)) > 0)
{
printf("# Kernel: %s", buffer);
}
}
// Earlier on, running this test was crashing the phone. It turned
// out that it was using too much memory but its oom_adj value was
// -17 which means disabled. -16 is the system_server and 0 is
// typically what applications run at. The issue is that adb runs
// at -17 and so is this test. We force oom_adj to 0 unless the
// oom_adj option has been used.
// TODO: We talked about adding an option to control oom_adj, not
// sure if we still need that.
int oomAdj = android::pidOutOfMemoryAdj();
printf("# Oom_adj: %d ", oomAdj);
if (oomAdj < 0)
{
android::setPidOutOfMemoryAdj(0);
printf("adjuted to %d", android::pidOutOfMemoryAdj());
}
printf("\n");
{
char buffer[kMinSchedFeaturesBufferSize] = {0, };
if (schedFeatures(buffer, sizeof(buffer)) > 0)
{
printf("# Sched features: %s", buffer);
}
}
printf("# Fadvise: %s\n", testCase.fadviseAsStr());
}
// Remove all the files under kTestDir and clear the caches.
void cleanup() {
android::resetDirectory(kTestDir);
android::syncAndDropCaches(); // don't pollute runs.
}
// @param argc, argv have a wild guess.
// @param[out] testCase Structure built from the cmd line args.
void parseCmdLine(int argc, char **argv, TestCase *testCase)\
{
int c;
while(true)
{
// getopt_long stores the option index here.
int option_index = 0;
c = getopt_long (argc, argv,
"hS:s:D:i:p:t:dcf:ezZa:",
long_options,
&option_index);
// Detect the end of the options.
if (c == -1) break;
switch (c)
{
case 's':
testCase->setDataSize(atoi(optarg) * 1024);
break;
case 'S':
testCase->setChunkSize(atoi(optarg) * 1024);
break;
case 'D': // tree depth
testCase->setTreeDepth(atoi(optarg));
break;
case 'i':
testCase->setIter(atoi(optarg));
printf("# Iterations: %d\n", testCase->iter());
break;
case 'p':
testCase->setNproc(atoi(optarg));
printf("# Proc nb: %d\n", testCase->nproc());
break;
case 't':
testCase->setTypeFromName(optarg);
printf("# Test name %s\n", testCase->name());
break;
case 'd':
testCase->setDump();
break;
case 'c':
printf("# Cpu scaling is on\n");
testCase->setCpuScaling();
break;
case 'f':
if (strcmp("sync", optarg) == 0) {
testCase->setSync(TestCase::SYNC);
} else if (strcmp("fsync", optarg) == 0) {
testCase->setSync(TestCase::FSYNC);
}
break;
case 'e': // e for empty
printf("# Will truncate to size\n");
testCase->setTruncateToSize();
break;
case 'z': // no new fair sleepers
testCase->setNewFairSleepers(false);
break;
case 'Z': // no normalized sleepers
testCase->setNormalizedSleepers(false);
break;
case 'a': // fadvise
testCase->setFadvise(optarg);
break;
case 'h':
usage();
exit(0);
default:
fprintf(stderr, "Unknown option %s\n", optarg);
exit(EXIT_FAILURE);
}
}
}
// ----------------------------------------------------------------------
// READ TEST
// Read a file. We use a new file each time to avoid any caching
// effect that would happen if we were reading the same file each
// time.
// @param chunk buffer large enough where the chunk read are written.
// @param idx iteration number.
// @param testCase has all the timers and paramters to run the test.
bool readData(char *const chunk, const int idx, TestCase *testCase)
{
char filename[80] = {'\0',};
sprintf(filename, "%s/file-%d-%d", kTestDir, idx, getpid());
testCase->openTimer()->start();
int fd = open(filename, O_RDONLY);
testCase->openTimer()->stop();
if (fd < 0)
{
fprintf(stderr, "Open read only failed.");
return false;
}
FADVISE(fd, 0, 0, testCase->fadvise());
size_t left = testCase->dataSize();
pid_t *pid = (pid_t*)chunk;
while (left > 0)
{
char *dest = chunk;
size_t chunk_size = testCase->chunkSize();
if (chunk_size > left)
{
chunk_size = left;
left = 0;
}
else
{
left -= chunk_size;
}
testCase->readTimer()->start();
while (chunk_size > 0)
{
ssize_t s = read(fd, dest, chunk_size);
if (s < 0)
{
fprintf(stderr, "Failed to read.\n");
close(fd);
return false;
}
chunk_size -= s;
dest += s;
}
testCase->readTimer()->stop();
}
close(fd);
if (testCase->pid() != *pid)
{
fprintf(stderr, "Wrong pid found @ read block %x != %x\n", testCase->pid(), *pid);
return false;
}
else
{
return true;
}
}
bool testRead(TestCase *testCase) {
// Setup the testcase by writting some dummy files.
const size_t size = testCase->dataSize();
size_t chunk_size = testCase->chunkSize();
char *const chunk = new char[chunk_size];
memset(chunk, 0xaa, chunk_size);
*((pid_t *)chunk) = testCase->pid(); // write our pid at the beginning of each chunk
size_t iter = testCase->iter();
// since readers are much faster we increase the number of
// iteration to last longer and have concurrent read/write
// thoughout the whole test.
if (testCase->type() == TestCase::READ_WRITE)
{
iter *= TestCase::kReadWriteFactor;
}
for (size_t i = 0; i < iter; ++i)
{
char filename[80] = {'\0',};
sprintf(filename, "%s/file-%d-%d", kTestDir, i, testCase->pid());
int fd = open(filename, O_RDWR | O_CREAT, S_IRWXU);
size_t left = size;
while (left > 0)
{
if (chunk_size > left)
{
chunk_size = left;
}
ssize_t written = write(fd, chunk, chunk_size);
if (written < 0)
{
fprintf(stderr, "Write failed %d %s.", errno, strerror(errno));
return false;
}
left -= written;
}
close(fd);
}
if (kVerbose) printf("Child %d all chunk written\n", testCase->pid());
android::syncAndDropCaches();
testCase->signalParentAndWait();
// Start the read test.
testCase->testTimer()->start();
for (size_t i = 0; i < iter; ++i)
{
if (!readData(chunk, i, testCase))
{
return false;
}
}
testCase->testTimer()->stop();
delete [] chunk;
return true;
}
// ----------------------------------------------------------------------
// WRITE TEST
bool writeData(const char *const chunk, const int idx, TestCase *testCase) {
char filename[80] = {'\0',};
sprintf(filename, "%s/file-%d-%d", kTestDir, idx, getpid());
testCase->openTimer()->start();
int fd = open(filename, O_RDWR | O_CREAT, S_IRWXU); // no O_TRUNC, see header comment
testCase->openTimer()->stop();
if (fd < 0)
{
fprintf(stderr, "Open write failed.");
return false;
}
FADVISE(fd, 0, 0, testCase->fadvise());
if (testCase->truncateToSize())
{
testCase->truncateTimer()->start();
ftruncate(fd, testCase->dataSize());
testCase->truncateTimer()->stop();
}
size_t left = testCase->dataSize();
while (left > 0)
{
const char *dest = chunk;
size_t chunk_size = testCase->chunkSize();
if (chunk_size > left)
{
chunk_size = left;
left = 0;
}
else
{
left -= chunk_size;
}
testCase->writeTimer()->start();
while (chunk_size > 0)
{
ssize_t s = write(fd, dest, chunk_size);
if (s < 0)
{
fprintf(stderr, "Failed to write.\n");
close(fd);
return false;
}
chunk_size -= s;
dest += s;
}
testCase->writeTimer()->stop();
}
if (TestCase::FSYNC == testCase->sync())
{
testCase->syncTimer()->start();
fsync(fd);
testCase->syncTimer()->stop();
}
else if (TestCase::SYNC == testCase->sync())
{
testCase->syncTimer()->start();
sync();
testCase->syncTimer()->stop();
}
close(fd);
return true;
}
bool testWrite(TestCase *testCase)
{
const size_t size = testCase->dataSize();
size_t chunk_size = testCase->chunkSize();
char *data = new char[chunk_size];
memset(data, 0xaa, chunk_size);
// TODO: write the pid at the beginning like in the write
// test. Have a mode where we check the write was correct.
testCase->signalParentAndWait();
testCase->testTimer()->start();
for (size_t i = 0; i < testCase->iter(); ++i)
{
if (!writeData(data, i, testCase))
{
return false;
}
}
testCase->testTimer()->stop();
delete [] data;
return true;
}
// ----------------------------------------------------------------------
// READ WRITE
// Mix of read and write test. Even PID run the write test. Odd PID
// the read test. Not fool proof but work most of the time.
bool testReadWrite(TestCase *testCase)
{
if (getpid() & 0x1) {
return testRead(testCase);
} else {
return testWrite(testCase);
}
}
// ----------------------------------------------------------------------
// OPEN CREATE TEST
bool testOpenCreate(TestCase *testCase)
{
char filename[80] = {'\0',};
testCase->signalParentAndWait();
testCase->testTimer()->start();
for (size_t i = 0; i < testCase->iter(); ++i)
{
sprintf(filename, "%s/file-%d-%d", kTestDir, i, testCase->pid());
int fd = open(filename, O_RDWR | O_CREAT, S_IRWXU);
FADVISE(fd, 0, 0, testCase->fadvise());
if (testCase->truncateToSize())
{
ftruncate(fd, testCase->dataSize());
}
if (fd < 0)
{
return false;
}
close(fd);
}
testCase->testTimer()->stop();
return true;
}
bool writeTestFile(TestCase *testCase, const char* filename) {
int fd = open(filename, O_RDWR | O_CREAT, S_IRWXU);
if (fd < 0) {
fprintf(stderr, "open() failed: %s\n", strerror(errno));
return false;
}
bool res = false;
char * const chunk = new char[testCase->chunkSize()];
memset(chunk, 0xaa, testCase->chunkSize());
size_t left = testCase->dataSize();
while (left > 0) {
char *dest = chunk;
size_t chunk_size = testCase->chunkSize();
if (chunk_size > left) {
chunk_size = left;
left = 0;
} else {
left -= chunk_size;
}
while (chunk_size > 0) {
ssize_t s = write(fd, dest, chunk_size);
if (s < 0) {
fprintf(stderr, "write() failed: %s\n", strerror(errno));
goto fail;
}
chunk_size -= s;
dest += s;
}
}
res = true;
fail:
close(fd);
delete[] chunk;
return res;
}
// ----------------------------------------------------------------------
// TRAVERSE
#define MAX_PATH 512
// Creates a directory tree that is both deep and wide, and times
// traversal using fts_open().
bool testTraverse(TestCase *testCase) {
char path[MAX_PATH];
char filepath[MAX_PATH];
strcpy(path, kTestDir);
// Generate a deep directory hierarchy
size_t depth = testCase->treeDepth();
for (size_t i = 0; i < depth; i++) {
// Go deeper by appending onto current path
snprintf(path + strlen(path), MAX_PATH - strlen(path), "/dir%d", i);
mkdir(path, S_IRWXU);
// Create some files at this depth
strcpy(filepath, path);
int pathlen = strlen(path);
char* nameStart = filepath + pathlen;
for (size_t j = 0; j < depth; j++) {
snprintf(nameStart, MAX_PATH - pathlen, "/file%d", j);
writeTestFile(testCase, filepath);
}
}
testCase->signalParentAndWait();
testCase->testTimer()->start();
// Now traverse structure
size_t iter = testCase->iter();
for (size_t i = 0; i < iter; i++) {
testCase->traverseTimer()->start();
FTS *ftsp;
if ((ftsp = fts_open((char **) &kTestDir, FTS_LOGICAL | FTS_XDEV, NULL)) == NULL) {
fprintf(stderr, "fts_open() failed: %s\n", strerror(errno));
return false;
}
// Count discovered files
int dirs = 0, files = 0;
FTSENT *curr;
while ((curr = fts_read(ftsp)) != NULL) {
switch (curr->fts_info) {
case FTS_D:
dirs++;
break;
case FTS_F:
files++;
break;
}
}
fts_close(ftsp);
testCase->traverseTimer()->stop();
int expectedDirs = depth + 1;
if (expectedDirs != dirs) {
fprintf(stderr, "expected %d dirs, but found %d\n", expectedDirs, dirs);
return false;
}
int expectedFiles = depth * depth;
if (expectedFiles != files) {
fprintf(stderr, "expected %d files, but found %d\n", expectedFiles, files);
return false;
}
}
testCase->testTimer()->stop();
return true;
}
} // anonymous namespace
int main(int argc, char **argv)
{
android_test::TestCase testCase(kAppName);
cleanup();
parseCmdLine(argc, argv, &testCase);
printHeader(argc, argv, testCase);
printf("# File size %d kbytes\n", testCase.dataSize() / 1024);
printf("# Chunk size %d kbytes\n", testCase.chunkSize() / 1024);
printf("# Sync: %s\n", testCase.syncAsStr());
if (!testCase.cpuScaling())
{
android::disableCpuScaling();
}
switch(testCase.type()) {
case TestCase::WRITE:
testCase.mTestBody = testWrite;
break;
case TestCase::READ:
testCase.mTestBody = testRead;
break;
case TestCase::OPEN_CREATE:
testCase.mTestBody = testOpenCreate;
break;
case TestCase::READ_WRITE:
testCase.mTestBody = testReadWrite;
break;
case TestCase::TRAVERSE:
testCase.mTestBody = testTraverse;
break;
default:
fprintf(stderr, "Unknown test type %s", testCase.name());
exit(EXIT_FAILURE);
}
testCase.createTimers();
bool ok;
ok = testCase.runTest();
cleanup();
if (!ok)
{
printf("error %d %s", errno, strerror(errno));
exit(EXIT_FAILURE);
}
else
{
exit(EXIT_SUCCESS);
}
}
|