📄 harddrivestorage.java
字号:
/* * Title: GridSim Toolkit * Description: GridSim (Grid Simulation) Toolkit for Modeling and Simulation * of Parallel and Distributed Systems such as Clusters and Grids * Licence: GPL - http://www.gnu.org/copyleft/gpl.html */package gridsim.datagrid.storage;import gridsim.datagrid.File;import eduni.simjava.distributions.ContinuousGenerator;import gridsim.ParameterException;import java.util.*;/** * An implementation of a storage system. It simulates the behaviour of a * typical harddrive storage. The default values for this storage are those of * a Maxtor DiamonMax 10 ATA harddisk with the following parameters: * <ul> * <li> latency = 4.17 ms * <li> avg seek time = 9 ms * <li> max transfer rate = 133 MB/sec * </ul> * * @author Uros Cibej and Anthony Sulistio * @since GridSim Toolkit 4.0 * @see gridsim.datagrid.storage.Storage */public class HarddriveStorage implements Storage { /** a list storing the names of all the files on the harddrive */ private ArrayList nameList_; /** a list storing all the files stored on the harddrive */ private ArrayList fileList_; /** the name of the harddrive*/ private String name_; /** a generator required to randomize the seek time */ private ContinuousGenerator gen_; /** the current size of files on the harddrive */ private double currentSize_; /** the total capacity of the harddrive in MB */ private double capacity_; /** the maximum transfer rate in MB/sec */ private double maxTransferRate_; /** the latency of the harddrive in seconds */ private double latency_; /** the average seek time in seconds */ private double avgSeekTime_; /** * Creates a new harddrive storage with a given name and capacity. * @param name the name of the new harddrive storage * @param capacity the capacity in MByte * @throws ParameterException when the name and the capacity are not valid */ public HarddriveStorage(String name, double capacity) throws ParameterException { if (name == null || name.length() == 0) { throw new ParameterException( "HarddriveStorage(): Error - invalid storage name."); } if (capacity <= 0) { throw new ParameterException( "HarddriveStorage(): Error - capacity <= 0."); } name_ = name; capacity_ = capacity; init(); } /** * Creates a new harddrive storage with a given capacity. * In this case the name of the storage is a default name. * @param capacity the capacity in MByte * @throws ParameterException when the capacity is not valid */ public HarddriveStorage(double capacity) throws ParameterException { if (capacity <= 0) { throw new ParameterException( "HarddriveStorage(): Error - capacity <= 0."); } name_ = "HarddriveStorage"; capacity_ = capacity; init(); } /** * The initialization of the harddrive is done in this method. The most * common parameters, such as latency, average seek time and maximum * transfer rate are set. The default values are set to simulate the Maxtor * DiamonMax 10 ATA harddisk. Furthermore, the necessary lists are created. */ private void init() { fileList_ = new ArrayList(); nameList_ = new ArrayList(); gen_ = null; currentSize_ = 0; latency_ = 0.00417; // 4.17 ms in seconds avgSeekTime_ = 0.009; // 9 ms maxTransferRate_ = 133; // in MB/sec } /** * Gets the available space on this storage in MB. * @return the available space in MB */ public double getAvailableSpace() { return capacity_ - currentSize_; } /** * Checks if the storage is full or not. * @return <tt>true</tt> if the storage is full, <tt>false</tt> * otherwise */ public boolean isFull() { if (currentSize_ == capacity_) { return true; } return false; } /** * Gets the number of files stored on this storage. * @return the number of stored files */ public int getNumStoredFile() { return fileList_.size(); } /** * Makes a reservation of the space on the storage to store a file. * @param fileSize the size to be reserved in MB * @return <tt>true</tt> if reservation succeeded, <tt>false</tt> * otherwise */ public boolean reserveSpace(int fileSize) { if (fileSize <= 0) { return false; } if (currentSize_ + fileSize >= capacity_) { return false; } currentSize_ += fileSize; return true; } /** * Adds a file for which the space has already been reserved. * The time taken (in seconds) for adding the file can also be * found using {@link gridsim.datagrid.File#getTransactionTime()}. * * @param file the file to be added * @return the time (in seconds) required to add the file */ public double addReservedFile(File file) { if (file == null) { return 0; } currentSize_ -= file.getSize(); double result = addFile(file); // if add file fails, then set the current size back to its old value if (result == 0.0) { currentSize_ += file.getSize(); } return result; } /** * Checks whether there is enough space on the storage for a certain file. * @param fileSize a FileAttribute object to compare to * @return <tt>true</tt> if enough space available, <tt>false</tt> * otherwise */ public boolean hasPotentialAvailableSpace(int fileSize) { if (fileSize <= 0) { return false; } // check if enough space left if (getAvailableSpace() > fileSize) { return true; } Iterator it = fileList_.iterator(); File file = null; int deletedFileSize = 0; // if not enough space, then if want to clear/delete some files // then check whether it still have space or not boolean result = false; while (it.hasNext()) { file = (File) it.next(); if (file.isReadOnly() == false) { deletedFileSize += file.getSize(); } if (deletedFileSize > fileSize) { result = true; break; } } return result; } /** * Gets the total capacity of the storage in MB. * @return the capacity of the storage in MB */ public double getCapacity() { return capacity_; } /** * Gets the current size of the stored files in MB. * @return the current size of the stored files in MB */ public double getCurrentSize() { return currentSize_; } /** * Gets the name of the storage. * @return the name of this storage */ public String getName() { return name_; } /** * Sets the latency of this harddrive in seconds. * @param latency the new latency in seconds * @return <tt>true</tt> if the setting succeeded, <tt>false</tt> * otherwise */ public boolean setLatency(double latency) { if (latency < 0) { return false; } latency_ = latency; return true; } /** * Gets the latency of this harddrive in seconds. * @return the latency in seconds */ public double getLatency() { return latency_; } /** * Sets the maximum transfer rate of this storage system in MB/sec. * @param rate the maximum transfer rate in MB/sec * @return <tt>true</tt> if the setting succeeded, <tt>false</tt> * otherwise */ public boolean setMaxTransferRate(int rate) { if (rate <= 0) { return false; } maxTransferRate_ = rate; return true; } /** * Gets the maximum transfer rate of the storage in MB/sec. * @return the maximum transfer rate in MB/sec */ public double getMaxTransferRate() { return maxTransferRate_; } /** * Sets the average seek time of the storage in seconds. * @param seekTime the average seek time in seconds * @return <tt>true</tt> if the setting succeeded, <tt>false</tt> * otherwise */ public boolean setAvgSeekTime(double seekTime) { return setAvgSeekTime(seekTime, null); } /** * Sets the average seek time and a new generator of seek times in seconds. * The generator determines a randomized seek time. * @param seekTime the average seek time in seconds * @param gen the ContinuousGenerator which generates seek times * @return <tt>true</tt> if the setting succeeded, <tt>false</tt> * otherwise */ public boolean setAvgSeekTime(double seekTime, ContinuousGenerator gen) { if (seekTime <= 0.0) { return false; } avgSeekTime_ = seekTime; gen_ = gen; return true; } /** * Gets the average seek time of the harddrive in seconds. * @return the average seek time in seconds */ public double getAvgSeekTime() { return avgSeekTime_; } /** * Gets the file with the specified name. * The time taken (in seconds) for getting the file can also be * found using {@link gridsim.datagrid.File#getTransactionTime()}. * * @param fileName the name of the needed file * @return the file with the specified filename */ public File getFile(String fileName) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -