Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[surf] simgrid::surf::Host, add backlink and attach() method
[simgrid.git] / examples / java / surfCpuModel / CpuConstantModel.java
1 package surfCpuModel;
2
3 import org.simgrid.surf.*;
4 import org.simgrid.msg.Msg;
5 import java.util.List;
6 import java.util.ArrayList;
7
8 public class CpuConstantModel extends CpuModel {
9
10   private List<CpuConstant> cpus = new ArrayList<CpuConstant>();
11
12   public CpuConstantModel() {
13     Msg.info("Initialize Cpu Constant Model");
14   }
15
16   public Cpu createCpu(String name, double[] power_peak, int pstate, double power_scale, TmgrTrace power_trace, int core, ResourceState state_initial, TmgrTrace state_trace, XbtDict cpu_properties) {
17     Msg.info("New Cpu("+name+", "+power_peak[pstate]+", "+power_scale+")");
18
19     CpuConstant res = new CpuConstant(this, name, cpu_properties, core, power_peak[pstate], power_scale);
20     cpus.add(res);
21     Surf.setCpu(name, res);
22     return res;
23   }
24
25   public double shareResources(double now) {
26     double res = -1;
27     for (int i=0; i<cpus.size();i++){
28       double tmp = cpus.get(i).share(now);
29       if (tmp!=-1 && (res==-1 || tmp<res))
30         res = tmp;
31     }
32     Msg.info("ShareResources at time "+res);
33     return res;
34   }
35
36   public void updateActionsState(double now, double delta) {
37     Action[] actions = getRunningActionSet().getArray();
38     Msg.info("UpdateActionState of "+actions.length+" actions");
39     for (int i=0; i<actions.length; i++) {
40       CpuConstantAction action = (CpuConstantAction)actions[i];
41       action.update(delta);
42       if (!action.sleeping()) {
43         Msg.info("action remains "+action.getRemains()+" after delta of "+delta);
44         if (action.getRemains()==0.0){
45           action.setState(ActionState.SURF_ACTION_DONE);
46           Msg.info("action DONE");
47         }
48       }
49     }
50   }
51
52   public void addTraces() {
53   }
54
55 public class CpuConstant extends Cpu {
56   private List<CpuConstantAction> actions = new ArrayList<CpuConstantAction>();
57
58   public CpuConstant(CpuConstantModel model, String name, XbtDict props,
59                      int core, double powerPeak, double powerScale) {
60     super(model, name, props, core, powerPeak, powerScale);
61   }
62
63   public void remove(CpuConstantAction action){
64     actions.remove(action);
65   }
66
67   public CpuAction execute(double size) {
68     CpuConstantAction res = new CpuConstantAction(getModel(), size, false, false, this);
69     Msg.info("Execute action of size "+size+" sleeping "+res.sleeping());
70     actions.add(res);
71     return res;
72   }
73
74   public CpuAction sleep(double duration) {
75     CpuConstantAction res = new CpuConstantAction(getModel(), duration, false, true, this);
76     Msg.info("Sleep action of duration "+duration+" sleeping "+res.sleeping());
77     return res;
78   }
79
80   public double share(double now){
81     double res = -1;
82     for (int i=0; i<actions.size();i++){
83       double tmp = actions.get(i).getRemains()/getCurrentPowerPeak();
84       Msg.info("Share action with new time "+tmp);
85       if (res==-1 || tmp<res)
86         res = tmp;
87     }
88     return res;
89   }
90
91   public ResourceState getState(){
92     return ResourceState.SURF_RESOURCE_ON;
93   }
94 }
95
96 public class CpuConstantAction extends CpuAction {
97   boolean sleep;
98   CpuConstant cpu;
99   public CpuConstantAction(Model model, double cost, boolean failed, boolean sleep, CpuConstant cpu) {
100     super(model, cost, failed);
101     this.sleep = sleep;
102     this.cpu = cpu;
103   }
104
105   public void update(double delta){
106     updateRemains(delta*cpu.getCurrentPowerPeak());
107   }
108   public synchronized void delete() {
109     cpu.remove(this);
110     super.delete();
111   }
112
113   public boolean sleeping() {return sleep;}
114   public void setPriority(double priority) {}
115 }
116 }