[477] | 1 | /* |
---|
| 2 | * Title: GridSim Toolkit |
---|
| 3 | * Description: GridSim (Grid Simulation) Toolkit for Modeling and Simulation |
---|
| 4 | * of Parallel and Distributed Systems such as Clusters and Grids |
---|
| 5 | * Licence: GPL - http://www.gnu.org/copyleft/gpl.html |
---|
| 6 | * |
---|
| 7 | * $Id: GridSimShutdown.java,v 1.4 2009/01/07 14:31:54 mkrystek Exp $ |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | package gridsim; |
---|
| 11 | |
---|
| 12 | import java.util.Calendar; |
---|
| 13 | |
---|
| 14 | import org.apache.commons.logging.Log; |
---|
| 15 | import org.apache.commons.logging.LogFactory; |
---|
| 16 | |
---|
| 17 | import eduni.simjava.Sim_event; |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | /** |
---|
| 21 | * GridimShutdown waits for termination of all GridSim user entities to |
---|
| 22 | * determine the end of simulation. |
---|
| 23 | * <p> |
---|
| 24 | * This class will be created by GridSim upon initialization of the simulation, |
---|
| 25 | * i.e. done via <tt>GridSim.init()</tt> method. Hence, do not need to worry |
---|
| 26 | * about creating an object of this class. |
---|
| 27 | * <p> |
---|
| 28 | * This class signals the user-defined report-writer |
---|
| 29 | * entity to interact with the GridStatistics entity to generate a report. |
---|
| 30 | * Finally, it signals the end of simulation to GridInformationService (GIS) |
---|
| 31 | * entity. |
---|
| 32 | * |
---|
| 33 | * @author Manzur Murshed and Rajkumar Buyya |
---|
| 34 | * @since GridSim Toolkit 1.0 |
---|
| 35 | * @invariant $none |
---|
| 36 | * @see gridsim.GridSim#init(int, Calendar, boolean) |
---|
| 37 | * @see gridsim.GridSim#init(int, Calendar, boolean, String[], String[], String) |
---|
| 38 | */ |
---|
| 39 | public class GridSimShutdown extends GridSimCore |
---|
| 40 | { |
---|
| 41 | protected int numUser_; |
---|
| 42 | protected String reportWriterName_; |
---|
| 43 | private Log log = LogFactory.getLog(GridSimShutdown.class); |
---|
| 44 | |
---|
| 45 | /** |
---|
| 46 | * Allocates a new GridSimShutdown object. |
---|
| 47 | * <p> |
---|
| 48 | * The total number of |
---|
| 49 | * grid user entity plays an important role to determine whether all |
---|
| 50 | * resources should be shut down or not. |
---|
| 51 | * If one or more users are still not finish, then the resources will not |
---|
| 52 | * be shut down. Therefore, it is important to give a correct number of |
---|
| 53 | * total grid user entity. Otherwise, GridSim program will hang or encounter |
---|
| 54 | * a weird behaviour. |
---|
| 55 | * |
---|
| 56 | * @param name the name to be associated with this entity (as |
---|
| 57 | * required by Sim_entity class from simjava package) |
---|
| 58 | * @param numUser total number of grid user entity |
---|
| 59 | * @param reportWriterName a <tt>ReportWriter</tt> entity name. This entity |
---|
| 60 | * can be found inside a gridbroker package. |
---|
| 61 | * @throws Exception This happens when creating this entity before |
---|
| 62 | * initializing GridSim package or this entity name is |
---|
| 63 | * <tt>null</tt> or empty |
---|
| 64 | * @see gridsim.GridSim#init(int, Calendar, boolean, String[], String[], |
---|
| 65 | * String) |
---|
| 66 | * @see gridsim.GridSim#init(int, Calendar, boolean) |
---|
| 67 | * @see eduni.simjava.Sim_entity |
---|
| 68 | * @pre name != null |
---|
| 69 | * @pre numUser >= 0 |
---|
| 70 | * @post $none |
---|
| 71 | */ |
---|
| 72 | public GridSimShutdown(String name, int numUser, |
---|
| 73 | String reportWriterName) throws Exception |
---|
| 74 | { |
---|
| 75 | // NOTE: This entity doesn't use any I/O port. |
---|
| 76 | //super(name, GridSimTags.DEFAULT_BAUD_RATE); |
---|
| 77 | super(name); |
---|
| 78 | this.numUser_ = numUser; |
---|
| 79 | this.reportWriterName_ = reportWriterName; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | /** |
---|
| 83 | * Allocates a new GridSimShutdown object. |
---|
| 84 | * <p> |
---|
| 85 | * The total number of |
---|
| 86 | * grid user entity plays an important role to determine whether all |
---|
| 87 | * resources should be shut down or not. |
---|
| 88 | * If one or more users are still not finish, then the resources will not |
---|
| 89 | * be shut down. Therefore, it is important to give a correct number of |
---|
| 90 | * total grid user entity. Otherwise, GridSim program will hang or encounter |
---|
| 91 | * a weird behaviour. |
---|
| 92 | * |
---|
| 93 | * @param name the name to be associated with this entity (as |
---|
| 94 | * required by Sim_entity class from simjava package) |
---|
| 95 | * @param numUser total number of grid user entity |
---|
| 96 | * @throws Exception This happens when creating this entity before |
---|
| 97 | * initializing GridSim package or this entity name is |
---|
| 98 | * <tt>null</tt> or empty |
---|
| 99 | * @see gridsim.GridSim#init(int, Calendar, boolean, String[], String[], |
---|
| 100 | * String) |
---|
| 101 | * @see gridsim.GridSim#init(int, Calendar, boolean) |
---|
| 102 | * @see eduni.simjava.Sim_entity |
---|
| 103 | * @pre name != null |
---|
| 104 | * @pre numUser >= 0 |
---|
| 105 | * @post $none |
---|
| 106 | */ |
---|
| 107 | public GridSimShutdown(String name, int numUser) throws Exception { |
---|
| 108 | this(name, numUser, null); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | /** |
---|
| 112 | * The main method that shuts down resources and Grid Information |
---|
| 113 | * Service (GIS). In addition, this method writes down a report at the |
---|
| 114 | * end of a simulation based on <tt>reportWriterName</tt> defined in |
---|
| 115 | * the Constructor. |
---|
| 116 | * <br> |
---|
| 117 | * <b>NOTE:</b> This method shuts down grid resources and GIS entities |
---|
| 118 | * <tt>AFTER</tt> all grid users have been shut down. |
---|
| 119 | * Therefore, the number of grid users given in the |
---|
| 120 | * Constructor <tt>must</tt> be correct. Otherwise, GridSim |
---|
| 121 | * package hangs forever or it does not terminate properly. |
---|
| 122 | * @pre $none |
---|
| 123 | * @post $none |
---|
| 124 | */ |
---|
| 125 | public void body() |
---|
| 126 | { |
---|
| 127 | Sim_event ev = new Sim_event(); |
---|
| 128 | |
---|
| 129 | // wait for shutdown message from all users. |
---|
| 130 | // NOTE: this can cause GridSim to be hanged if numUser_ doesn't match |
---|
| 131 | // with number of user entities given during GridSim.init(). |
---|
| 132 | for (int i = 0; i < numUser_; i++) { |
---|
| 133 | super.sim_get_next(ev); |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | // Shutdown GIS - now GIS is responsible for informing end of simulation |
---|
| 137 | // This is to simplify design and coding, without introducing any static |
---|
| 138 | // methods for GridSim and GIS. |
---|
| 139 | super.send(GridSim.getGridInfoServiceEntityId(), |
---|
| 140 | GridSimTags.SCHEDULE_NOW, GridSimTags.END_OF_SIMULATION); |
---|
| 141 | |
---|
| 142 | //super.sim_pause(100); //FIXME we don't want this line at all |
---|
| 143 | |
---|
| 144 | // Invoke report Writer and shutdown |
---|
| 145 | if (reportWriterName_ != null) |
---|
| 146 | { |
---|
| 147 | int repWriterID = GridSim.getEntityId(reportWriterName_); |
---|
| 148 | if (repWriterID != -1) |
---|
| 149 | { |
---|
| 150 | super.send(repWriterID, GridSimTags.SCHEDULE_NOW, |
---|
| 151 | GridSimTags.END_OF_SIMULATION); |
---|
| 152 | } |
---|
| 153 | else |
---|
| 154 | { |
---|
| 155 | if(log.isErrorEnabled()) |
---|
| 156 | log.error(super.get_name() + |
---|
| 157 | ": User defined Report Writer entity " + |
---|
| 158 | reportWriterName_ + " does not exist."); |
---|
| 159 | } |
---|
| 160 | } |
---|
| 161 | else |
---|
| 162 | { |
---|
| 163 | // check whether GridStatistics entity has been created or not |
---|
| 164 | int id = GridSim.getGridStatisticsEntityId(); |
---|
| 165 | if (id != -1) { |
---|
| 166 | super.send(id, 0.0, GridSimTags.END_OF_SIMULATION); |
---|
| 167 | } |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | super.terminateIOEntities(); |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | |
---|
| 174 | /////////////////////// PRIVATE METHODS ////////////////////////// |
---|
| 175 | |
---|
| 176 | } // end class |
---|