/*
 * 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 "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 ouput 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'},
    {"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 [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"
           "  -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: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 '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);

        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);  // 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);
        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;
}

}  // 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;
        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);
    }
}
