sysutil.h revision 39c016f875b793296a121f41de5775b88f6fa1c9
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#ifndef ANDROID_NATIVETEST_SYSTEM_EXTRAS_TESTS_SDCARD_SYSUTIL_H_ 3039c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania#define ANDROID_NATIVETEST_SYSTEM_EXTRAS_TESTS_SDCARD_SYSUTIL_H_ 3139c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania 3239c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania#include <stdlib.h> 3339c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catanianamespace android { 3439c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania 3539c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// Collection of functions to access the system: 3639c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// .kernelVersion Retrieve the full kernel description. 3739c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// .pidOutOfMemoryAdj Get and set the OOM adj value. 3839c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// .setPidOutOfMemoryAdj 3939c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// .schedFeatures Manipulate the scheduler using debugfs. 4039c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// .newFairSleepers 4139c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// .setNewFairSleepers 4239c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// .disableCpuScaling Set cpu scaling to 'performance'. 4339c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// .forkOrExit Fork a child or exit. 4439c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// .syncAnddropCaches Call sync an drop page/dentries/inodes caches. 4539c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// .fsyncAnddropCaches Call fsync an drop page/dentries/inodes caches. 4639c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// .resetDirectory Delete (non-recursive) files in a directory. 4739c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// 4839c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// IPC function to synchonize a processes with their parent. 4939c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// .writePidAndWaitForReply To instruct the parent the child is ready. 5039c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// Blocks until the parent signals back. 5139c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// .waitForChildrenAndSignal Blocks until all the children have called 5239c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// writePidAndWaitForReply. 5339c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// Then unblock all the children. 5439c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// .waitForChildrenOrExit Wait and exit if a child exit with errors. 5539c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// 5639c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania 5739c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// Minimum size for the buffer to retrieve the kernel version. 5839c016f875b793296a121f41de5775b88f6fa1c9Nicolas Cataniastatic const size_t kMinKernelVersionBufferSize = 256; 5939c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania 6039c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// @param str points to the buffer where the kernel version should be 6139c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// added. Must be at least kMinKernelVersionBufferSize chars. 6239c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// @param size of the buffer pointed by str. 6339c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// @return If successful the number of characters inserted in the 6439c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// buffer (not including the trailing '\0' byte). -1 otherwise. 6539c016f875b793296a121f41de5775b88f6fa1c9Nicolas Cataniaint kernelVersion(char *str, size_t size); 6639c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania 6739c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania 6839c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// Return the out of memory adj for this process. /proc/<pid>/oom_adj. 6939c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// @return the oom_adj of the current process. Typically: 7039c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// 0: a regular process. Should die on OOM. 7139c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// -16: system_server level. 7239c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// -17: disable, this process will never be killed. 7339c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// -127: error. 7439c016f875b793296a121f41de5775b88f6fa1c9Nicolas Cataniaint pidOutOfMemoryAdj(); 7539c016f875b793296a121f41de5775b88f6fa1c9Nicolas Cataniavoid setPidOutOfMemoryAdj(int level); 7639c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania 7739c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// Disable cpu scaling. 7839c016f875b793296a121f41de5775b88f6fa1c9Nicolas Cataniavoid disableCpuScaling(); 7939c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania 8039c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania 8139c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// Minimum size for the buffer to retrieve the sched features. 8239c016f875b793296a121f41de5775b88f6fa1c9Nicolas Cataniastatic const size_t kMinSchedFeaturesBufferSize = 256; 8339c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania 8439c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// @param str points to the buffer where the sched features should be 8539c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// added. Must be at least kMinSchedFeaturesBufferSize chars. 8639c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// @param size of the buffer pointed by str. 8739c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// @return If successful the number of characters inserted in the 8839c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// buffer (not including the trailing '\0' byte). -1 otherwise. 8939c016f875b793296a121f41de5775b88f6fa1c9Nicolas Cataniaint schedFeatures(char *str, size_t size); 9039c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania 9139c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// @return true if NEW_FAIR_SLEEPERS is set, false if NO_NEW_FAIR_SLEEPERS is set. 9239c016f875b793296a121f41de5775b88f6fa1c9Nicolas Cataniabool newFairSleepers(); 9339c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania 9439c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// Turns NEW_FAIR_SLEEPERS on or off. 9539c016f875b793296a121f41de5775b88f6fa1c9Nicolas Cataniavoid setNewFairSleepers(bool on); 9639c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania 9739c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// @return true if NORMALIZED_SLEEPERS is set, false if NO_NORMALIZED_SLEEPERS is set. 9839c016f875b793296a121f41de5775b88f6fa1c9Nicolas Cataniabool normalizedSleepers(); 9939c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania 10039c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// Turns NORMALIZED_SLEEPERS on or off. 10139c016f875b793296a121f41de5775b88f6fa1c9Nicolas Cataniavoid setNormalizedSleepers(bool on); 10239c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania 10339c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// Filesystem 10439c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania 10539c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// Sync and drop caches. Sync is needed because dirty objects are not 10639c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// freable. 10739c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// @param code: 10839c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// * 1 To free pagecache. 10939c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// * 2 To free dentries and inodes. 11039c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// * 3 To free pagecache, dentries and inodes. 11139c016f875b793296a121f41de5775b88f6fa1c9Nicolas Cataniavoid syncAndDropCaches(int code = 3); 11239c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania 11339c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// Fsync the given fd and drop caches. Fsync is needed because dirty 11439c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// objects are not freable. 11539c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// @param code: 11639c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// * 1 To free pagecache. 11739c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// * 2 To free dentries and inodes. 11839c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// * 3 To free pagecache, dentries and inodes. 11939c016f875b793296a121f41de5775b88f6fa1c9Nicolas Cataniavoid fsyncAndDropCaches(int fd, int code = 3); 12039c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania 12139c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// Delete all the files in the given directory. If the directory does 12239c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// not exist, it is created. Use this at the beginning of a test to 12339c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// make sure you have a clean existing directory, previous run may 12439c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// have crashed and left clutter around. 12539c016f875b793296a121f41de5775b88f6fa1c9Nicolas Cataniavoid resetDirectory(const char *directory); 12639c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania 12739c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// IPC 12839c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania 12939c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// Try to fork. exit on failure. 13039c016f875b793296a121f41de5775b88f6fa1c9Nicolas Cataniapid_t forkOrExit(); 13139c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania 13239c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// Signal our parent we are alive and ready by sending our pid. 13339c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// Then do a blocking read for parent's reply. 13439c016f875b793296a121f41de5775b88f6fa1c9Nicolas Cataniabool writePidAndWaitForReply(int writefd, int readfd); 13539c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania 13639c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// Wait for all the children to report their pids. 13739c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// Then unblock them. 13839c016f875b793296a121f41de5775b88f6fa1c9Nicolas Cataniabool waitForChildrenAndSignal(int mProcessNb, int readfd, int writefd); 13939c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania 14039c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// Wait for 'num' children to complete. 14139c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania// If a child did not exit cleanly, exit. 14239c016f875b793296a121f41de5775b88f6fa1c9Nicolas Cataniavoid waitForChildrenOrExit(int num); 14339c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania 14439c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania} // namespace android 14539c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania 14639c016f875b793296a121f41de5775b88f6fa1c9Nicolas Catania#endif // ANDROID_NATIVETEST_SYSTEM_EXTRAS_TESTS_SDCARD_SYSUTIL_H_ 147