testcase.h revision 39c016f875b793296a121f41de5775b88f6fa1c9
1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *  * Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 *  * Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in
12 *    the documentation and/or other materials provided with the
13 *    distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29
30#ifndef SYSTEM_EXTRAS_TESTS_SDCARD_TESTCASE_H_
31#define SYSTEM_EXTRAS_TESTS_SDCARD_TESTCASE_H_
32
33#include <stdlib.h>
34#include "stopwatch.h"
35#include "sysutil.h"
36
37namespace android_test {
38
39// Class to group test parameters and implementation.
40// Takes care of forking child processes and wait for them.
41
42class TestCase {
43  public:
44    enum Type {UNKNOWN_TEST, WRITE, READ, OPEN_CREATE, READ_WRITE};
45    enum Pipe {READ_FROM_CHILD = 0, WRITE_TO_PARENT, READ_FROM_PARENT, WRITE_TO_CHILD};
46    enum Sync {NO_SYNC, FSYNC, SYNC};
47
48    // Reads takes less time than writes. This is a basic
49    // approximation of how much longer the read tasks must run to
50    // terminate roughly at the same time as the write tasks.
51    const static int kReadWriteFactor = 5;
52
53    TestCase(const char *appName);
54
55    ~TestCase();
56
57    size_t iter() const { return mIter; }
58    void setIter(size_t iter);
59
60    size_t nproc() const { return mNproc; }
61    void setNproc(size_t val) { mNproc = val; }
62
63    size_t dataSize() const { return mDataSize; }
64    void setDataSize(size_t val) { mDataSize = val; }
65
66    size_t chunkSize() const { return mChunkSize; }
67    void setChunkSize(size_t val) { mChunkSize = val; }
68
69    bool newFairSleepers() const { return mNewFairSleepers; }
70    void setNewFairSleepers(bool val) {
71        mNewFairSleepers = val;
72        android::setNewFairSleepers(val);
73    }
74
75    bool normalizedSleepers() const { return mNormalizedSleepers; }
76    void setNormalizedSleepers(bool val) {
77        mNormalizedSleepers = val;
78        android::setNormalizedSleepers(val);
79    }
80
81    Sync sync() const { return mSync; }
82    void setSync(Sync s);
83    const char *syncAsStr() const;
84
85    bool cpuScaling() const { return mCpuScaling; }
86    void setCpuScaling() { mCpuScaling = true; }
87
88    bool truncateToSize() const { return mTruncateToSize; }
89    void setTruncateToSize() { mTruncateToSize = true; }
90
91    int fadvise() { return mFadvice; }
92    void setFadvise(const char *advice);
93    const char *fadviseAsStr() const;
94
95    // Print the samples.
96    void setDump() { StopWatch::setPrintRawMode(true); }
97
98    StopWatch *testTimer() { return mTestTimer; }
99    StopWatch *openTimer() { return mOpenTimer; }
100    StopWatch *readTimer() { return mReadTimer; }
101    StopWatch *writeTimer() { return mWriteTimer; }
102    StopWatch *syncTimer() { return mSyncTimer; }
103    StopWatch *truncateTimer() { return mTruncateTimer; }
104
105    // Fork the children, run the test and wait for them to complete.
106    bool runTest();
107
108    void signalParentAndWait() {
109        if (!android::writePidAndWaitForReply(mIpc[WRITE_TO_PARENT], mIpc[READ_FROM_PARENT])) {
110            exit(1);
111        }
112    }
113
114    void createTimers();
115    bool setTypeFromName(const char *test_name);
116    Type type() const { return mType; }
117    pid_t pid() const { return mPid; }
118    const char *name() const { return mName; }
119
120    // This is set to the function that will actually do the test when
121    // the command line arguments have been parsed. The function will
122    // be run in one or more child(ren) process(es).
123    bool (*mTestBody)(TestCase *);
124private:
125    const char *mAppName;
126    size_t mDataSize;
127    size_t mChunkSize;
128    size_t mIter;
129    size_t mNproc;
130    pid_t mPid;
131    char mName[80];
132    Type mType;
133
134    bool mDump;  // print the raw values instead of a human friendly report.
135    bool mCpuScaling;  // true, do not turn off cpu scaling.
136    Sync mSync;
137    int mFadvice;
138    // When new files are created, truncate them to the final size.
139    bool mTruncateToSize;
140
141    bool mNewFairSleepers;
142    bool mNormalizedSleepers;
143
144    // IPC
145    //        Parent               Child(ren)
146    // ---------------------------------------
147    // 0: read from child          closed
148    // 1: closed                   write to parent
149    // 2: closed                   read from parent
150    // 3: write to child           closed
151    int mIpc[4];
152
153    StopWatch *mTestTimer;  // Used to time the test overall.
154    StopWatch *mOpenTimer;  // Used to time the open calls.
155    StopWatch *mReadTimer;  // Used to time the read calls.
156    StopWatch *mWriteTimer;  // Used to time the write calls.
157    StopWatch *mSyncTimer;  // Used to time the sync/fsync calls.
158    StopWatch *mTruncateTimer;  // Used to time the ftruncate calls.
159};
160
161}  // namespace android_test
162
163#endif  // SYSTEM_EXTRAS_TESTS_SDCARD_TESTCASE_H_
164