summaryrefslogtreecommitdiff
path: root/tests/sdcard/sysutil.h
blob: 3df79c124a788913e58c87b8714462dc2029f43a (plain)
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
/*
 * 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.
 */

#ifndef ANDROID_NATIVETEST_SYSTEM_EXTRAS_TESTS_SDCARD_SYSUTIL_H_
#define ANDROID_NATIVETEST_SYSTEM_EXTRAS_TESTS_SDCARD_SYSUTIL_H_

#include <stdlib.h>
namespace android {

// Collection of functions to access the system:
// .kernelVersion         Retrieve the full kernel description.
// .pidOutOfMemoryAdj     Get and set the OOM adj value.
// .setPidOutOfMemoryAdj
// .schedFeatures         Manipulate the scheduler using debugfs.
// .newFairSleepers
// .setNewFairSleepers
// .disableCpuScaling     Set cpu scaling to 'performance'.
// .forkOrExit            Fork a child or exit.
// .syncAnddropCaches     Call sync an drop page/dentries/inodes caches.
// .fsyncAnddropCaches    Call fsync an drop page/dentries/inodes caches.
// .resetDirectory        Delete (non-recursive) files in a directory.
//
// IPC function to synchonize a processes with their parent.
// .writePidAndWaitForReply To instruct the parent the child is ready.
//                          Blocks until the parent signals back.
// .waitForChildrenAndSignal Blocks until all the children have called
//                           writePidAndWaitForReply.
//                           Then unblock all the children.
// .waitForChildrenOrExit Wait and exit if a child exit with errors.
//

// Minimum size for the buffer to retrieve the kernel version.
static const size_t kMinKernelVersionBufferSize = 256;

// @param str points to the buffer where the kernel version should be
//            added. Must be at least kMinKernelVersionBufferSize chars.
// @param size of the buffer pointed by str.
// @return If successful the number of characters inserted in the
//         buffer (not including the trailing '\0' byte). -1 otherwise.
int kernelVersion(char *str, size_t size);


// Return the out of memory adj for this process. /proc/<pid>/oom_adj.
// @return the oom_adj of the current process. Typically:
//           0: a regular process. Should die on OOM.
//         -16: system_server level.
//         -17: disable, this process  will never be killed.
//        -127: error.
int pidOutOfMemoryAdj();
void setPidOutOfMemoryAdj(int level);

// Disable cpu scaling.
void disableCpuScaling();


// Minimum size for the buffer to retrieve the sched features.
static const size_t kMinSchedFeaturesBufferSize = 256;

// @param str points to the buffer where the sched features should be
//            added. Must be at least kMinSchedFeaturesBufferSize chars.
// @param size of the buffer pointed by str.
// @return If successful the number of characters inserted in the
//         buffer (not including the trailing '\0' byte). -1 otherwise.
int schedFeatures(char *str, size_t size);

// @return true if NEW_FAIR_SLEEPERS is set, false if NO_NEW_FAIR_SLEEPERS is set.
bool newFairSleepers();

// Turns NEW_FAIR_SLEEPERS on or off.
void setNewFairSleepers(bool on);

// @return true if NORMALIZED_SLEEPERS is set, false if NO_NORMALIZED_SLEEPERS is set.
bool normalizedSleepers();

// Turns NORMALIZED_SLEEPERS on or off.
void setNormalizedSleepers(bool on);

// Filesystem

// Sync and drop caches. Sync is needed because dirty objects are not
// freable.
// @param code:
//        * 1 To free pagecache.
//        * 2 To free dentries and inodes.
//        * 3 To free pagecache, dentries and inodes.
void syncAndDropCaches(int code = 3);

// Fsync the given fd and drop caches. Fsync is needed because dirty
// objects are not freable.
// @param code:
//        * 1 To free pagecache.
//        * 2 To free dentries and inodes.
//        * 3 To free pagecache, dentries and inodes.
void fsyncAndDropCaches(int fd, int code = 3);

// Delete all the files in the given directory.  If the directory does
// not exist, it is created.  Use this at the beginning of a test to
// make sure you have a clean existing directory, previous run may
// have crashed and left clutter around.
void resetDirectory(const char *directory);

// IPC

// Try to fork. exit on failure.
pid_t forkOrExit();

// Signal our parent we are alive and ready by sending our pid.
// Then do a blocking read for parent's reply.
bool writePidAndWaitForReply(int writefd, int readfd);

// Wait for all the children to report their pids.
// Then unblock them.
bool waitForChildrenAndSignal(int mProcessNb, int readfd, int writefd);

// Wait for 'num' children to complete.
// If a child did not exit cleanly, exit.
void waitForChildrenOrExit(int num);

}  // namespace android

#endif  // ANDROID_NATIVETEST_SYSTEM_EXTRAS_TESTS_SDCARD_SYSUTIL_H_