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 com.android.cts.filesystemperf;
18 
19 import android.util.Log;
20 
21 import android.cts.util.CtsAndroidTestCase;
22 import android.cts.util.SystemUtil;
23 
24 import com.android.cts.util.TimeoutReq;
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 
37     private static final long FREE_SPACE_FINAL = 1000L * 1024 * 1024L;
38 
39     // test runner creates multiple instances at the begging.
40     // use that to fill disk only once.
41     // set as final to initialize it only once
42     private static final AtomicInteger mRefCounter = new AtomicInteger(0);
43     private static final AtomicBoolean mDiskFilled = new AtomicBoolean(false);
44 
AlmostFullTest()45     public AlmostFullTest() {
46         int currentCounter = mRefCounter.incrementAndGet();
47         Log.i(TAG, "++currentCounter: " + currentCounter);
48     }
49 
50     @Override
setUp()51     protected void setUp() throws Exception {
52         super.setUp();
53         if (mDiskFilled.compareAndSet(false, true)) {
54             Log.i(TAG, "Filling disk");
55             // initial fill done in two stage as disk can be filled by other
56             // components
57             long freeDisk = SystemUtil.getFreeDiskSize(getContext());
58             long diskToFill = freeDisk - FREE_SPACE_FINAL;
59             if (diskToFill >= 0) {
60                 Log.i(TAG, "free disk " + freeDisk + ", to fill " + diskToFill);
61             } else {
62                 Log.i(TAG, "free disk " + freeDisk + " too small, needs " + FREE_SPACE_FINAL);
63                 return;
64             }
65             final long MAX_FILE_SIZE_TO_FILL = 1024L * 1024L * 1024L;
66             long filled = 0;
67             while (filled < diskToFill) {
68                 long toFill = diskToFill - filled;
69                 if (toFill > MAX_FILE_SIZE_TO_FILL) {
70                     toFill = MAX_FILE_SIZE_TO_FILL;
71                 }
72                 Log.i(TAG, "Generating file " + toFill);
73                 FileUtil.createNewFilledFile(getContext(),
74                         DIR_INITIAL_FILL, toFill);
75                 filled += toFill;
76             }
77         }
78         Log.i(TAG, "free disk " + SystemUtil.getFreeDiskSize(getContext()));
79     }
80 
81     @Override
tearDown()82     protected void tearDown() throws Exception {
83         Log.i(TAG, "tearDown free disk " + SystemUtil.getFreeDiskSize(getContext()));
84         int currentCounter = mRefCounter.decrementAndGet();
85         Log.i(TAG, "--currentCounter: " + currentCounter);
86         if (currentCounter == 0) {
87             FileUtil.removeFileOrDir(getContext(), DIR_INITIAL_FILL);
88         }
89         FileUtil.removeFileOrDir(getContext(), DIR_SEQ_UPDATE);
90         FileUtil.removeFileOrDir(getContext(), DIR_RANDOM_WR);
91         FileUtil.removeFileOrDir(getContext(), DIR_RANDOM_RD);
92         Log.i(TAG, "tearDown free disk " + SystemUtil.getFreeDiskSize(getContext()));
93         super.tearDown();
94     }
95 
96     @TimeoutReq(minutes = 30)
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         FileUtil.doSequentialUpdateTest(getContext(), DIR_SEQ_UPDATE, getReportLog(), FILE_SIZE,
110                 BUFFER_SIZE, NUMBER_REPETITION);
111     }
112 
113     // TODO: file size too small and caching will give wrong better result.
114     // needs to flush cache by reading big files per each read.
115     @TimeoutReq(minutes = 60)
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         FileUtil.doRandomReadTest(getContext(), DIR_RANDOM_RD, getReportLog(), fileSize,
125                 BUFFER_SIZE);
126     }
127 
128     @TimeoutReq(minutes = 60)
testRandomUpdate()129     public void testRandomUpdate() throws Exception {
130         final int BUFFER_SIZE = 4 * 1024;
131         final long fileSize = 256L * 1024L * 1024L;
132         long freeDisk = SystemUtil.getFreeDiskSize(getContext());
133         if (freeDisk < fileSize) {
134             Log.w(TAG, "too little space: " + freeDisk);
135             return;
136         }
137         FileUtil.doRandomWriteTest(getContext(), DIR_RANDOM_WR, getReportLog(), fileSize,
138                 BUFFER_SIZE);
139     }
140 }
141