1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.filesystem.cts; 18 19 import android.util.Log; 20 21 import android.cts.util.CtsAndroidTestCase; 22 import android.cts.util.SystemUtil; 23 24 import com.android.compatibility.common.util.DeviceReportLog; 25 26 import java.util.concurrent.atomic.AtomicBoolean; 27 import java.util.concurrent.atomic.AtomicInteger; 28 29 public class AlmostFullTest extends CtsAndroidTestCase { 30 31 private static final String DIR_INITIAL_FILL = "INITIAL_FILL"; 32 private static final String DIR_SEQ_UPDATE = "SEQ_UPDATE"; 33 private static final String DIR_RANDOM_WR = "RANDOM_WR"; 34 private static final String DIR_RANDOM_RD = "RANDOM_RD"; 35 private static final String TAG = "AlmostFullTest"; 36 private static final String REPORT_LOG_NAME = "CtsFileSystemTestCases"; 37 38 private static final long FREE_SPACE_FINAL = 1000L * 1024 * 1024L; 39 40 // test runner creates multiple instances at the begging. 41 // use that to fill disk only once. 42 // set as final to initialize it only once 43 private static final AtomicInteger mRefCounter = new AtomicInteger(0); 44 private static final AtomicBoolean mDiskFilled = new AtomicBoolean(false); 45 AlmostFullTest()46 public AlmostFullTest() { 47 int currentCounter = mRefCounter.incrementAndGet(); 48 Log.i(TAG, "++currentCounter: " + currentCounter); 49 } 50 51 @Override setUp()52 protected void setUp() throws Exception { 53 super.setUp(); 54 if (mDiskFilled.compareAndSet(false, true)) { 55 Log.i(TAG, "Filling disk"); 56 // initial fill done in two stage as disk can be filled by other 57 // components 58 long freeDisk = SystemUtil.getFreeDiskSize(getContext()); 59 long diskToFill = freeDisk - FREE_SPACE_FINAL; 60 if (diskToFill >= 0) { 61 Log.i(TAG, "free disk " + freeDisk + ", to fill " + diskToFill); 62 } else { 63 Log.i(TAG, "free disk " + freeDisk + " too small, needs " + FREE_SPACE_FINAL); 64 return; 65 } 66 final long MAX_FILE_SIZE_TO_FILL = 1024L * 1024L * 1024L; 67 long filled = 0; 68 while (filled < diskToFill) { 69 long toFill = diskToFill - filled; 70 if (toFill > MAX_FILE_SIZE_TO_FILL) { 71 toFill = MAX_FILE_SIZE_TO_FILL; 72 } 73 Log.i(TAG, "Generating file " + toFill); 74 FileUtil.createNewFilledFile(getContext(), 75 DIR_INITIAL_FILL, toFill); 76 filled += toFill; 77 } 78 } 79 Log.i(TAG, "free disk " + SystemUtil.getFreeDiskSize(getContext())); 80 } 81 82 @Override tearDown()83 protected void tearDown() throws Exception { 84 Log.i(TAG, "tearDown free disk " + SystemUtil.getFreeDiskSize(getContext())); 85 int currentCounter = mRefCounter.decrementAndGet(); 86 Log.i(TAG, "--currentCounter: " + currentCounter); 87 if (currentCounter == 0) { 88 FileUtil.removeFileOrDir(getContext(), DIR_INITIAL_FILL); 89 } 90 FileUtil.removeFileOrDir(getContext(), DIR_SEQ_UPDATE); 91 FileUtil.removeFileOrDir(getContext(), DIR_RANDOM_WR); 92 FileUtil.removeFileOrDir(getContext(), DIR_RANDOM_RD); 93 Log.i(TAG, "tearDown free disk " + SystemUtil.getFreeDiskSize(getContext())); 94 super.tearDown(); 95 } 96 testSequentialUpdate()97 public void testSequentialUpdate() throws Exception { 98 // now about freeSpaceToLeave should be left 99 // and try updating exceeding the free space size 100 final long FILE_SIZE = 400L * 1024L * 1024L; 101 long freeDisk = SystemUtil.getFreeDiskSize(getContext()); 102 Log.i(TAG, "Now free space is " + freeDisk); 103 if (freeDisk < FILE_SIZE) { 104 Log.w(TAG, "too little space: " + freeDisk); 105 return; 106 } 107 final int BUFFER_SIZE = 10 * 1024 * 1024; 108 final int NUMBER_REPETITION = 10; 109 String streamName = "test_sequential_update"; 110 FileUtil.doSequentialUpdateTest(getContext(), DIR_SEQ_UPDATE, FILE_SIZE, BUFFER_SIZE, 111 NUMBER_REPETITION, REPORT_LOG_NAME, streamName); 112 } 113 114 // TODO: file size too small and caching will give wrong better result. 115 // needs to flush cache by reading big files per each read. testRandomRead()116 public void testRandomRead() throws Exception { 117 final int BUFFER_SIZE = 4 * 1024; 118 final long fileSize = 400L * 1024L * 1024L; 119 long freeDisk = SystemUtil.getFreeDiskSize(getContext()); 120 if (freeDisk < fileSize) { 121 Log.w(TAG, "too little space: " + freeDisk); 122 return; 123 } 124 String streamName = "test_random_read"; 125 DeviceReportLog report = new DeviceReportLog(REPORT_LOG_NAME, streamName); 126 FileUtil.doRandomReadTest(getContext(), DIR_RANDOM_RD, report, fileSize, BUFFER_SIZE); 127 report.submit(getInstrumentation()); 128 } 129 testRandomUpdate()130 public void testRandomUpdate() throws Exception { 131 final int BUFFER_SIZE = 4 * 1024; 132 final long fileSize = 256L * 1024L * 1024L; 133 long freeDisk = SystemUtil.getFreeDiskSize(getContext()); 134 if (freeDisk < fileSize) { 135 Log.w(TAG, "too little space: " + freeDisk); 136 return; 137 } 138 String streamName = "test_random_update"; 139 DeviceReportLog report = new DeviceReportLog(REPORT_LOG_NAME, streamName); 140 FileUtil.doRandomWriteTest(getContext(), DIR_RANDOM_WR, report, fileSize, BUFFER_SIZE); 141 report.submit(getInstrumentation()); 142 } 143 } 144