1 | package de.uka.ipd.sdq.measurement.strategies.activeresource.hdd; |
2 | |
3 | import java.io.File; |
4 | import java.io.FileInputStream; |
5 | import java.io.FileNotFoundException; |
6 | import java.io.FileOutputStream; |
7 | import java.io.IOException; |
8 | import java.util.Iterator; |
9 | import java.util.LinkedList; |
10 | import java.util.List; |
11 | |
12 | import org.apache.log4j.Logger; |
13 | |
14 | import de.uka.ipd.sdq.measurement.strategies.activeresource.AbstractDemandStrategy; |
15 | import de.uka.ipd.sdq.measurement.strategies.activeresource.DegreeOfAccuracyEnum; |
16 | import de.uka.ipd.sdq.measurement.strategies.activeresource.IDemandStrategy; |
17 | import de.uka.ipd.sdq.measurement.strategies.activeresource.ResourceTypeEnum; |
18 | import de.uka.ipd.sdq.measurement.strategies.system.SystemResourcesUtil; |
19 | |
20 | /** |
21 | * Reads large chunks of data up to MAX_FILE_SIZE Byte (default 8 MB) from a |
22 | * predefined hard disk drive. |
23 | * |
24 | * During initialisation, it is checked whether there is a directory |
25 | * fileDirectory. If yes, it is used to read data from the hard disk drive. If |
26 | * not, the directory is created and a number of files given by numberOfFiles |
27 | * are written. The files size is maxFileSize. |
28 | * |
29 | * It is recommended to restart the application or even the whole machine if the |
30 | * files are written during initialisation. |
31 | * |
32 | * To avoid caching effects, each consume reads from another file until all |
33 | * files of the list are read. Then, the first file is read again. |
34 | * |
35 | * TODO: Initialisation of the HD, variable file sizes, <s>variable number |
36 | * of files</s>, scattered reads, writes, variable file sizes. |
37 | * |
38 | * @author Anne, Sebastian Lehrig |
39 | * |
40 | * |
41 | */ |
42 | public class ReadLargeChunksDemand extends AbstractDemandStrategy implements |
43 | IDemandStrategy { |
44 | |
45 | /** |
46 | * Maximum size of files to be created or searched |
47 | */ |
48 | private final int maxFileSize; |
49 | private static final int DEFAULT_MAX_FILE_SIZE = 8 * 1000 * 1000; // 8 MB |
50 | |
51 | /** |
52 | * Maximum number of files to include into the calibration |
53 | */ |
54 | private final long numberOfFiles; |
55 | |
56 | /** |
57 | * Root directory from where the files will be read |
58 | */ |
59 | private final File fileDirectory; |
60 | |
61 | /** Stores some files sorted by size for fast access */ |
62 | private List<File> files = new LinkedList<File>(); |
63 | private List<File> cleanupFiles = new LinkedList<File>(); |
64 | private Iterator<File> iterator = null; |
65 | |
66 | private static final Logger logger = Logger |
67 | .getLogger(ReadLargeChunksDemand.class.getName()); |
68 | |
69 | public ReadLargeChunksDemand() { |
70 | this(SystemResourcesUtil.TEMP_DIR, DEFAULT_MAX_FILE_SIZE); |
71 | } |
72 | |
73 | public ReadLargeChunksDemand(File path, int maxFileSize) |
74 | { |
75 | super(-2,0,2,100,10); |
76 | this.fileDirectory = path; |
77 | this.maxFileSize = maxFileSize; |
78 | this.numberOfFiles = calculateDefaultNumberOfFiles(); |
79 | } |
80 | |
81 | /** |
82 | * Calculates the number of files needed for the calibration. |
83 | * |
84 | * Since the OS can use the RAM for saving time when files are |
85 | * read over and over again, this function calculates the number |
86 | * of files such that it holds: |
87 | * |
88 | * RAM size < Sum of file sizes |
89 | * |
90 | * @return System dependent number of files for calibration. |
91 | */ |
92 | private static long calculateDefaultNumberOfFiles() |
93 | { |
94 | long ramSize = SystemResourcesUtil.getTotalPhysicalMemorySize(); |
95 | long number = (ramSize/DEFAULT_MAX_FILE_SIZE); |
96 | |
97 | // increase number by 10% to assure RAM size < Sum of file sizes |
98 | number = (long)(number*1.1f); |
99 | |
100 | return number; |
101 | } |
102 | |
103 | @Override |
104 | protected void run(long load) { |
105 | logger.debug("Consume HDD demand of: " + load); |
106 | try { |
107 | long remainingLoad = load; |
108 | do { |
109 | FileInputStream fis = new FileInputStream(nextFile()); |
110 | |
111 | long consume = remainingLoad > this.maxFileSize ? maxFileSize : remainingLoad; |
112 | byte[] byteArray = new byte[(int) consume]; |
113 | int success = fis.read(byteArray); |
114 | fis.close(); |
115 | logger.trace("Adjusted demand consumed: " + success); |
116 | |
117 | remainingLoad -= success; |
118 | } while (remainingLoad > 0); |
119 | } catch (FileNotFoundException e) { |
120 | logger.error("HDD demand strategy failed", e); |
121 | System.exit(-1); |
122 | } catch (IOException e) { |
123 | logger.error("HDD demand strategy failed", e); |
124 | System.exit(-1); |
125 | } |
126 | logger.debug("Complete HDD demand consumed"); |
127 | } |
128 | |
129 | /** |
130 | * Returns the next file. The next file after the last one in the list is |
131 | * the first. Note that this method will throw a NullPointerException if the |
132 | * list is empty. For performance reasons, however, this is not checked. |
133 | * |
134 | * @return The next file. |
135 | */ |
136 | private synchronized File nextFile() { |
137 | assert this.files.size() > 0; |
138 | assert iterator != null; |
139 | |
140 | if (!iterator.hasNext()) // Reset the file iterator at the end |
141 | iterator = this.files.iterator(); |
142 | |
143 | return iterator.next(); |
144 | } |
145 | |
146 | @Override |
147 | public ResourceTypeEnum getStrategysResource() { |
148 | return ResourceTypeEnum.HDD; |
149 | } |
150 | |
151 | @Override |
152 | public void initializeStrategy(DegreeOfAccuracyEnum degreeOfAccuracy, |
153 | double processingRate, String calibrationPath) { |
154 | preInitHDDStrategy(); |
155 | super.initializeStrategy(degreeOfAccuracy,processingRate,calibrationPath); |
156 | } |
157 | |
158 | @Override |
159 | public void initializeStrategy(DegreeOfAccuracyEnum degreeOfAccuracy, |
160 | double processingRate) { |
161 | preInitHDDStrategy(); |
162 | super.initializeStrategy(degreeOfAccuracy,processingRate); |
163 | } |
164 | |
165 | private void preInitHDDStrategy() { |
166 | if (this.files.size() > 0) |
167 | return; // Already pre-init done |
168 | |
169 | logger.debug("Pre-Initialising strategy reading from " |
170 | + this.fileDirectory); |
171 | if (!fileDirectory.exists()) { |
172 | logger.info("Directory given for reading files does not exist. Trying to prepare one"); |
173 | try { |
174 | createFileDirectory(); |
175 | writeTestFiles(); |
176 | logger.info("Wrote files to be read."); |
177 | } catch (IOException e) { |
178 | logger.error("Failed creating files for HDD strategy. Maybe missing permission?", e); |
179 | System.exit(-1); |
180 | } |
181 | } else if (fileDirectory.isDirectory()) { |
182 | logger.info("Reading file list from "+fileDirectory.getAbsolutePath()); |
183 | initialiseFileList(fileDirectory); |
184 | } else { |
185 | logger.error("There already is a file at " + fileDirectory.getAbsolutePath()); |
186 | } |
187 | |
188 | // The strategy could not be initialised as there are no files to read |
189 | // -> try to create files |
190 | if (this.files.isEmpty()) { |
191 | try { |
192 | writeTestFiles(); |
193 | logger.debug("Wrote files to be read."); |
194 | } catch (IOException e) { |
195 | logger.error("Failed reading files for HDD strategy",e); |
196 | System.exit(-1); |
197 | } |
198 | } |
199 | |
200 | if (this.files.size() < 1) { |
201 | logger.error("The strategy could not be initialised as there are no files to read."); |
202 | System.exit(-1); |
203 | } else { |
204 | this.iterator = this.files.iterator(); |
205 | logger.info("HDD Strategy initialised with " + files.size() |
206 | + " files in folder "+fileDirectory.getAbsolutePath()); |
207 | } |
208 | } |
209 | |
210 | private void createFileDirectory() throws IOException { |
211 | if (!fileDirectory.mkdirs()) { |
212 | logger.error("File directory could not be created during initialisation."); |
213 | throw new IOException("Directory for files store could not be created"); |
214 | } |
215 | } |
216 | |
217 | private boolean writeTestFiles() throws IOException { |
218 | long neededSize = this.numberOfFiles*DEFAULT_MAX_FILE_SIZE; |
219 | long tmpSize = SystemResourcesUtil.getFreeTempDirectorySize(); |
220 | if(neededSize > tmpSize) |
221 | { |
222 | logger.error("The required storage space for calibration exceeds the free space in " |
223 | + SystemResourcesUtil.TEMP_DIR.getAbsolutePath()); |
224 | System.exit(-1); |
225 | } |
226 | |
227 | File childFile; |
228 | for (int i = 0; i < this.numberOfFiles; i++) { |
229 | childFile = new File(fileDirectory, "file" + i); |
230 | childFile.createNewFile(); |
231 | |
232 | FileOutputStream fos = new FileOutputStream(childFile); |
233 | byte[] bytes = new byte[maxFileSize]; |
234 | fos.write(bytes); |
235 | fos.flush(); |
236 | fos.close(); |
237 | |
238 | files.add(childFile); cleanupFiles.add(childFile); |
239 | |
240 | } |
241 | |
242 | return true; |
243 | } |
244 | |
245 | private void initialiseFileList(File files) { |
246 | File[] childFiles = files.listFiles(); |
247 | |
248 | if (childFiles != null) // childFiles may be null, if we do not have a |
249 | // permission for a directory |
250 | { |
251 | logger.debug("Found " + childFiles.length + " files in the first directory(" |
252 | + files.getAbsolutePath() + ")."); |
253 | for (File file : childFiles) { |
254 | |
255 | if (file.isDirectory()) { |
256 | initialiseFileList(file); |
257 | } else { |
258 | if (file.length() >= this.maxFileSize) { |
259 | this.files.add(file); |
260 | } else { |
261 | logger.debug("File is too small: "+file.getAbsolutePath()+". We skip it..."); |
262 | } |
263 | } |
264 | } |
265 | } |
266 | } |
267 | |
268 | public int getMaxFileSize() { |
269 | return maxFileSize; |
270 | } |
271 | |
272 | public String getName() { |
273 | return "Read Large Chunks"; |
274 | } |
275 | |
276 | public void cleanup() { |
277 | for (File file : cleanupFiles) { |
278 | logger.debug("Trying to delete file " + file.getName()); |
279 | if (!file.delete()) { |
280 | logger.error("Failed to delete file " + file.getName()); |
281 | } |
282 | } |
283 | } |
284 | |
285 | } |