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#ifndef ANDROID_NATIVETEST_SYSTEM_EXTRAS_TESTS_SDCARD_SYSUTIL_H_
30#define ANDROID_NATIVETEST_SYSTEM_EXTRAS_TESTS_SDCARD_SYSUTIL_H_
31
32#include <stdlib.h>
33namespace android {
34
35// Collection of functions to access the system:
36// .kernelVersion         Retrieve the full kernel description.
37// .pidOutOfMemoryAdj     Get and set the OOM adj value.
38// .setPidOutOfMemoryAdj
39// .schedFeatures         Manipulate the scheduler using debugfs.
40// .newFairSleepers
41// .setNewFairSleepers
42// .disableCpuScaling     Set cpu scaling to 'performance'.
43// .forkOrExit            Fork a child or exit.
44// .syncAnddropCaches     Call sync an drop page/dentries/inodes caches.
45// .fsyncAnddropCaches    Call fsync an drop page/dentries/inodes caches.
46// .resetDirectory        Delete (non-recursive) files in a directory.
47//
48// IPC function to synchonize a processes with their parent.
49// .writePidAndWaitForReply To instruct the parent the child is ready.
50//                          Blocks until the parent signals back.
51// .waitForChildrenAndSignal Blocks until all the children have called
52//                           writePidAndWaitForReply.
53//                           Then unblock all the children.
54// .waitForChildrenOrExit Wait and exit if a child exit with errors.
55//
56
57// Minimum size for the buffer to retrieve the kernel version.
58static const size_t kMinKernelVersionBufferSize = 256;
59
60// @param str points to the buffer where the kernel version should be
61//            added. Must be at least kMinKernelVersionBufferSize chars.
62// @param size of the buffer pointed by str.
63// @return If successful the number of characters inserted in the
64//         buffer (not including the trailing '\0' byte). -1 otherwise.
65int kernelVersion(char *str, size_t size);
66
67
68// Return the out of memory adj for this process. /proc/<pid>/oom_adj.
69// @return the oom_adj of the current process. Typically:
70//           0: a regular process. Should die on OOM.
71//         -16: system_server level.
72//         -17: disable, this process  will never be killed.
73//        -127: error.
74int pidOutOfMemoryAdj();
75void setPidOutOfMemoryAdj(int level);
76
77// Disable cpu scaling.
78void disableCpuScaling();
79
80
81// Minimum size for the buffer to retrieve the sched features.
82static const size_t kMinSchedFeaturesBufferSize = 256;
83
84// @param str points to the buffer where the sched features should be
85//            added. Must be at least kMinSchedFeaturesBufferSize chars.
86// @param size of the buffer pointed by str.
87// @return If successful the number of characters inserted in the
88//         buffer (not including the trailing '\0' byte). -1 otherwise.
89int schedFeatures(char *str, size_t size);
90
91// @return true if NEW_FAIR_SLEEPERS is set, false if NO_NEW_FAIR_SLEEPERS is set.
92bool newFairSleepers();
93
94// Turns NEW_FAIR_SLEEPERS on or off.
95void setNewFairSleepers(bool on);
96
97// @return true if NORMALIZED_SLEEPERS is set, false if NO_NORMALIZED_SLEEPERS is set.
98bool normalizedSleepers();
99
100// Turns NORMALIZED_SLEEPERS on or off.
101void setNormalizedSleepers(bool on);
102
103// Filesystem
104
105// Sync and drop caches. Sync is needed because dirty objects are not
106// freable.
107// @param code:
108//        * 1 To free pagecache.
109//        * 2 To free dentries and inodes.
110//        * 3 To free pagecache, dentries and inodes.
111void syncAndDropCaches(int code = 3);
112
113// Fsync the given fd and drop caches. Fsync is needed because dirty
114// objects are not freable.
115// @param code:
116//        * 1 To free pagecache.
117//        * 2 To free dentries and inodes.
118//        * 3 To free pagecache, dentries and inodes.
119void fsyncAndDropCaches(int fd, int code = 3);
120
121// Delete all the files in the given directory.  If the directory does
122// not exist, it is created.  Use this at the beginning of a test to
123// make sure you have a clean existing directory, previous run may
124// have crashed and left clutter around.
125void resetDirectory(const char *directory);
126
127// IPC
128
129// Try to fork. exit on failure.
130pid_t forkOrExit();
131
132// Signal our parent we are alive and ready by sending our pid.
133// Then do a blocking read for parent's reply.
134bool writePidAndWaitForReply(int writefd, int readfd);
135
136// Wait for all the children to report their pids.
137// Then unblock them.
138bool waitForChildrenAndSignal(int mProcessNb, int readfd, int writefd);
139
140// Wait for 'num' children to complete.
141// If a child did not exit cleanly, exit.
142void waitForChildrenOrExit(int num);
143
144}  // namespace android
145
146#endif  // ANDROID_NATIVETEST_SYSTEM_EXTRAS_TESTS_SDCARD_SYSUTIL_H_
147