139c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania/*
239c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania * Copyright (C) 2009 The Android Open Source Project
339c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania * All rights reserved.
439c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania *
539c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania * Redistribution and use in source and binary forms, with or without
639c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania * modification, are permitted provided that the following conditions
739c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania * are met:
839c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania *  * Redistributions of source code must retain the above copyright
939c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania *    notice, this list of conditions and the following disclaimer.
1039c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania *  * Redistributions in binary form must reproduce the above copyright
1139c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania *    notice, this list of conditions and the following disclaimer in
1239c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania *    the documentation and/or other materials provided with the
1339c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania *    distribution.
1439c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania *
1539c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1639c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1739c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
1839c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
1939c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2039c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2139c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
2239c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2339c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2439c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
2539c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2639c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania * SUCH DAMAGE.
2739c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania */
2839c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania
2939c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania
3039c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania#ifndef SYSTEM_EXTRAS_TESTS_SDCARD_TESTCASE_H_
3139c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania#define SYSTEM_EXTRAS_TESTS_SDCARD_TESTCASE_H_
3239c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania
3339c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania#include <stdlib.h>
3439c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania#include "stopwatch.h"
3539c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania#include "sysutil.h"
3639c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania
3739c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catanianamespace android_test {
3839c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania
3939c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// Class to group test parameters and implementation.
4039c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// Takes care of forking child processes and wait for them.
4139c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania
4239c016f875b793296a121f41de5775b88f6fa1c9Nicolas Cataniaclass TestCase {
4339c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania  public:
44e1b480b11c19f014a2a6ceddd28883275a4ba0e3Jeff Sharkey    enum Type {UNKNOWN_TEST, WRITE, READ, OPEN_CREATE, READ_WRITE, TRAVERSE};
4539c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    enum Pipe {READ_FROM_CHILD = 0, WRITE_TO_PARENT, READ_FROM_PARENT, WRITE_TO_CHILD};
4639c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    enum Sync {NO_SYNC, FSYNC, SYNC};
4739c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania
4839c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    // Reads takes less time than writes. This is a basic
4939c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    // approximation of how much longer the read tasks must run to
5039c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    // terminate roughly at the same time as the write tasks.
5139c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    const static int kReadWriteFactor = 5;
5239c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania
5339c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    TestCase(const char *appName);
5439c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania
5539c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    ~TestCase();
5639c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania
5739c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    size_t iter() const { return mIter; }
5839c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    void setIter(size_t iter);
5939c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania
6039c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    size_t nproc() const { return mNproc; }
6139c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    void setNproc(size_t val) { mNproc = val; }
6239c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania
6339c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    size_t dataSize() const { return mDataSize; }
6439c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    void setDataSize(size_t val) { mDataSize = val; }
6539c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania
6639c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    size_t chunkSize() const { return mChunkSize; }
6739c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    void setChunkSize(size_t val) { mChunkSize = val; }
6839c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania
69e1b480b11c19f014a2a6ceddd28883275a4ba0e3Jeff Sharkey    size_t treeDepth() const { return mTreeDepth; }
70e1b480b11c19f014a2a6ceddd28883275a4ba0e3Jeff Sharkey    void setTreeDepth(size_t val) { mTreeDepth = val; }
71e1b480b11c19f014a2a6ceddd28883275a4ba0e3Jeff Sharkey
7239c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    bool newFairSleepers() const { return mNewFairSleepers; }
7339c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    void setNewFairSleepers(bool val) {
7439c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania        mNewFairSleepers = val;
7539c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania        android::setNewFairSleepers(val);
7639c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    }
7739c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania
7839c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    bool normalizedSleepers() const { return mNormalizedSleepers; }
7939c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    void setNormalizedSleepers(bool val) {
8039c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania        mNormalizedSleepers = val;
8139c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania        android::setNormalizedSleepers(val);
8239c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    }
8339c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania
8439c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    Sync sync() const { return mSync; }
8539c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    void setSync(Sync s);
8639c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    const char *syncAsStr() const;
8739c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania
8839c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    bool cpuScaling() const { return mCpuScaling; }
8939c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    void setCpuScaling() { mCpuScaling = true; }
9039c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania
9139c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    bool truncateToSize() const { return mTruncateToSize; }
9239c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    void setTruncateToSize() { mTruncateToSize = true; }
9339c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania
9439c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    int fadvise() { return mFadvice; }
9539c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    void setFadvise(const char *advice);
9639c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    const char *fadviseAsStr() const;
9739c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania
9839c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    // Print the samples.
9939c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    void setDump() { StopWatch::setPrintRawMode(true); }
10039c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania
10139c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    StopWatch *testTimer() { return mTestTimer; }
10239c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    StopWatch *openTimer() { return mOpenTimer; }
10339c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    StopWatch *readTimer() { return mReadTimer; }
10439c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    StopWatch *writeTimer() { return mWriteTimer; }
10539c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    StopWatch *syncTimer() { return mSyncTimer; }
10639c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    StopWatch *truncateTimer() { return mTruncateTimer; }
107e1b480b11c19f014a2a6ceddd28883275a4ba0e3Jeff Sharkey    StopWatch *traverseTimer() { return mTraverseTimer; }
10839c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania
10939c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    // Fork the children, run the test and wait for them to complete.
11039c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    bool runTest();
11139c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania
11239c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    void signalParentAndWait() {
11339c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania        if (!android::writePidAndWaitForReply(mIpc[WRITE_TO_PARENT], mIpc[READ_FROM_PARENT])) {
11439c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania            exit(1);
11539c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania        }
11639c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    }
11739c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania
11839c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    void createTimers();
11939c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    bool setTypeFromName(const char *test_name);
12039c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    Type type() const { return mType; }
12139c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    pid_t pid() const { return mPid; }
12239c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    const char *name() const { return mName; }
12339c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania
12439c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    // This is set to the function that will actually do the test when
12539c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    // the command line arguments have been parsed. The function will
12639c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    // be run in one or more child(ren) process(es).
12739c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    bool (*mTestBody)(TestCase *);
12839c016f875b793296a121f41de5775b88f6fa1c9Nicolas Cataniaprivate:
12939c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    const char *mAppName;
13039c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    size_t mDataSize;
13139c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    size_t mChunkSize;
132e1b480b11c19f014a2a6ceddd28883275a4ba0e3Jeff Sharkey    size_t mTreeDepth;
13339c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    size_t mIter;
13439c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    size_t mNproc;
13539c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    pid_t mPid;
13639c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    char mName[80];
13739c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    Type mType;
13839c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania
13939c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    bool mDump;  // print the raw values instead of a human friendly report.
14039c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    bool mCpuScaling;  // true, do not turn off cpu scaling.
14139c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    Sync mSync;
14239c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    int mFadvice;
14339c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    // When new files are created, truncate them to the final size.
14439c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    bool mTruncateToSize;
14539c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania
14639c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    bool mNewFairSleepers;
14739c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    bool mNormalizedSleepers;
14839c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania
14939c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    // IPC
15039c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    //        Parent               Child(ren)
15139c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    // ---------------------------------------
15239c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    // 0: read from child          closed
15339c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    // 1: closed                   write to parent
15439c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    // 2: closed                   read from parent
15539c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    // 3: write to child           closed
15639c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    int mIpc[4];
15739c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania
15839c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    StopWatch *mTestTimer;  // Used to time the test overall.
15939c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    StopWatch *mOpenTimer;  // Used to time the open calls.
16039c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    StopWatch *mReadTimer;  // Used to time the read calls.
16139c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    StopWatch *mWriteTimer;  // Used to time the write calls.
16239c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    StopWatch *mSyncTimer;  // Used to time the sync/fsync calls.
16339c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania    StopWatch *mTruncateTimer;  // Used to time the ftruncate calls.
164e1b480b11c19f014a2a6ceddd28883275a4ba0e3Jeff Sharkey    StopWatch *mTraverseTimer;  // Used to time each traversal.
16539c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania};
16639c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania
16739c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania}  // namespace android_test
16839c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania
16939c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania#endif  // SYSTEM_EXTRAS_TESTS_SDCARD_TESTCASE_H_
170