Logo AND Algorithmique Numérique Distribuée

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