Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use simgrid::Host instead of xbt_dictelt_t for root main object
[simgrid.git] / src / bindings / java / surf.i
1 /* Copyright (c) 2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 /* File : example.i */
8 %module(directors="1") Surf
9
10 %include "arrays_java.i"
11 %include "std_string.i"
12 %include "surfdoc.i"
13
14 %pragma(java) jniclassimports=%{
15 import org.simgrid.NativeLib;
16 %}
17 %pragma(java) jniclasscode=%{
18   static {
19     org.simgrid.NativeLib.nativeInit();    
20     Runtime.getRuntime().addShutdownHook(
21       new Thread() {
22         public void run() {
23           Thread.currentThread().setName( "Destroyer" );
24           Surf.clean();
25         }
26       }
27     );
28   }
29 %}
30
31 %{
32 #include "src/surf/surf_interface.hpp"
33 #include "src/surf/cpu_interface.hpp"
34 #include "src/surf/network_interface.hpp"
35 #include "src/surf/trace_mgr_private.h"
36 #include "src/bindings/java/surf_swig.hpp"
37 #include "src/xbt/dict_private.h"
38
39 typedef struct lmm_constraint *lmm_constraint_t;
40 typedef xbt_dynar_t DoubleDynar;
41 %}
42
43 %wrapper %{
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47
48 JNIEXPORT jobject JNICALL Java_org_simgrid_surf_SurfJNI_getAction(JNIEnv *env, jclass cls, jlong jarg1) {
49   simgrid::surf::Action * action = (simgrid::surf::Action *)jarg1;
50   jobject res;
51   simgrid::surf::CpuAction *cpu_action =
52     dynamic_cast<simgrid::surf::CpuAction*>(action);
53   if (cpu_action) {
54     SwigDirector_CpuAction *dir_cpu_action =
55       dynamic_cast<SwigDirector_CpuAction*>(cpu_action);
56     if (dir_cpu_action) {
57       res = dir_cpu_action->swig_get_self(env);\
58     } else {
59       jclass clss = env->FindClass("org/simgrid/surf/CpuAction");\
60       jmethodID constru = env->GetMethodID(clss, "<init>", "()V");\
61       res = env->NewObject(clss, constru);\
62       res = env->NewGlobalRef(res);\
63     }
64   } else {
65     jclass clss = env->FindClass("org/simgrid/surf/Action");\
66     jmethodID constru = env->GetMethodID(clss, "<init>", "()V");\
67     res = env->NewObject(clss, constru);\
68     res = env->NewGlobalRef(res);\
69   }
70   return res;
71 }
72
73 #define GETDIRECTOR(NAME) \
74 JNIEXPORT jobject JNICALL Java_org_simgrid_surf_SurfJNI_get## NAME ## Director(JNIEnv *env, jclass cls, jlong jarg1)\
75 {\
76   simgrid::surf::NAME * arg1 = (simgrid::surf::NAME*)jarg1;\
77   SwigDirector_ ##NAME *director = dynamic_cast<SwigDirector_ ##NAME *>(arg1);\
78   jobject res;\
79   if (director) {\
80     res = director->swig_get_self(env);\
81   } else {\
82     jclass clss = env->FindClass("org/simgrid/surf/NAME");\
83     jmethodID constru = env->GetMethodID(clss, "<init>", "()V");\
84     res = env->NewObject(clss, constru);\
85     res = env->NewGlobalRef(res);\
86   }\
87   return res;\
88 }
89
90 GETDIRECTOR(CpuModel)
91 GETDIRECTOR(Cpu)
92 GETDIRECTOR(CpuAction)
93
94 #ifdef __cplusplus
95 }
96 #endif
97
98 %}
99
100 %typemap(freearg) char* name {
101 }
102
103 /* Handle xbt_dynar_t of Link */
104 JAVA_ARRAYSOFCLASSES(Action);
105 %apply Action[] {ActionArrayPtr};
106 %typemap(jstype) ActionArrayPtr "Action[]"
107 %typemap(javain) ActionArrayPtr "Action.cArrayUnwrap($javainput)"
108 %typemap(javaout) ActionArrayPtr {
109   long[] cArray = $jnicall;
110   Action[] arrayWrapper = new Action[cArray.length];
111   for (int i=0; i<cArray.length; i++)
112     arrayWrapper[i] = (Action) Surf.getAction(cArray[i]);
113   return arrayWrapper;
114   //   return Action.cArrayWrap($jnicall, $owner);
115 }
116 %typemap(out) ActionArrayPtr {
117   long l = 0;
118   for(simgrid::surf::ActionList::iterator it($1->begin()), itend($1->end()); it != itend ; ++it) {
119     l++;
120   }
121   $result = jenv->NewLongArray(l);
122   jlong *elts = jenv->GetLongArrayElements($result, NULL);
123   l = 0;
124   for(simgrid::surf::ActionList::iterator it($1->begin()), itend($1->end()); it != itend ; ++it) {
125     elts[l++] = (jlong)static_cast<simgrid::surf::Action*>(&*it);
126   }
127   jenv->ReleaseLongArrayElements($result, elts, 0);
128 }
129
130 namespace simgrid {
131 namespace surf {
132
133 class ActionList {
134 public:
135   //void push_front(Action &action);
136   //void push_back(Action &action);
137 %extend {
138   ActionArrayPtr getArray(){
139     return $self;
140   }
141 }
142 };
143
144 }
145 }
146
147 /* Handle xbt_dynar_t of Link */
148 JAVA_ARRAYSOFCLASSES(simgrid::surf::Link);
149 %apply simgrid::surf::Link[] {LinkDynar};
150 %typemap(jstype) LinkDynar "Link[]"
151 %typemap(javain) LinkDynar "Link.cArrayUnwrap($javainput)"
152 %typemap(javaout) LinkDynar {
153      return Link.cArrayWrap($jnicall, $owner);
154 }
155 %typemap(out) LinkDynar {
156   long l = xbt_dynar_length($1);
157   $result = jenv->NewLongArray(l);
158   unsigned i;
159   Link *link;
160   jlong *elts = jenv->GetLongArrayElements($result, NULL);
161   xbt_dynar_foreach($1, i, link) {
162     elts[i] = (jlong)link;
163   }
164   jenv->ReleaseLongArrayElements($result, elts, 0);
165   xbt_dynar_free(&$1);
166 }
167
168 %nodefault DoubleDynar;
169 %typemap(jni) DoubleDynar "jdoubleArray"
170 %rename(DoubleDynar) Double[];
171 %typemap(jtype) DoubleDynar "double[]"
172 %typemap(jstype) DoubleDynar "double[]"
173 %typemap(out) DoubleDynar {
174   long l = xbt_dynar_length($1);
175   $result = jenv->NewDoubleArray(l);
176   double *lout = (double *) xbt_dynar_get_ptr($1, 0);
177   jenv->SetDoubleArrayRegion($result, 0, l, (jdouble*)lout);
178 }
179 %typemap(javadirectorin) DoubleDynar "$jniinput"
180 %typemap(directorin,descriptor="[D") DoubleDynar %{
181   long l = xbt_dynar_length($1);
182   $input = jenv->NewDoubleArray(l);
183   double *lout = (double *) xbt_dynar_get_ptr($1, 0);
184   jenv->SetDoubleArrayRegion($input, 0, l, (jdouble*)lout);
185 %}
186 %typemap(javain) DoubleDynar "$javainput"
187 %typemap(javaout) DoubleDynar {return  $jnicall}
188
189 /* Allow to subclass Plugin and send java object to C++ code */
190 %feature("director") Plugin;
191
192 %native(getAction) jobject getAction(jlong jarg1);
193 %native(getCpuModelDirector) jobject getCpuModelDirector(jlong jarg1);
194 %typemap(javaout) CpuModel * {
195   long cPtr = $jnicall;
196   return (CpuModel)Surf.getCpuModelDirector(cPtr);
197 }
198 %native(getCpuDirector) jobject getCpuDirector(jlong jarg1);
199 %typemap(javaout) Cpu * {
200   long cPtr = $jnicall;
201   return (Cpu)Surf.getCpuDirector(cPtr);
202 }
203 %native(getCpuActionDirector) jobject getCpuActionDirector(jlong jarg1);
204 %typemap(javaout) CpuAction * {
205   long cPtr = $jnicall;
206   return (CpuAction)Surf.getCpuDirector(cPtr);
207 }
208
209 %include "src/bindings/java/surf_swig.hpp"
210
211 namespace simgrid {
212 namespace surf {
213
214 class Model;
215 class CpuModel;
216 class HostModel;
217 class VMModel;
218 class NetworkModel;
219 class StorageModel;
220 class Resource;
221 class ResourceLmm;
222 class Host;
223 class HostCLM03;
224 class NetworkCm02Link;
225 class Action;
226 class ActionLmm;
227 class StorageActionLmm;
228 class As;
229 class RoutingPlatf;
230
231 }
232 }
233
234 %rename tmgr_trace TmgrTrace;
235 %nodefaultctor tmgr_trace;
236 struct tmgr_trace {
237   //enum e_trace_type type;
238   /*union {
239     struct {
240       xbt_dynar_t event_list;
241     } s_list;
242     struct {
243       probabilist_event_generator_t event_generator[2];
244       int is_state_trace;
245       int next_event;
246     } s_probabilist;
247   };*/
248   %extend {
249   }
250 };
251
252 %rename tmgr_trace_event TmgrTraceEvent;
253 %nodefaultctor tmgr_trace_event;
254 struct tmgr_trace_event {
255   //tmgr_trace_t trace;
256   //unsigned int idx;
257   //void *model;
258   //int free_me;
259   %extend {
260     unsigned int getIdx() {return 0;}//$self->idx;}
261   }
262 };
263
264 namespace simgrid {
265 namespace surf {
266
267 %nodefaultctor Model;
268 class Model {
269 public:
270   Model();
271   
272   virtual double shareResources(double now);
273   virtual double shareResourcesLazy(double now);
274   virtual double shareResourcesFull(double now);
275
276   virtual void updateActionsState(double now, double delta);
277   virtual void updateActionsStateLazy(double now, double delta);
278   virtual void updateActionsStateFull(double now, double delta);
279
280   virtual simgrid::surf::ActionList *getRunningActionSet();
281
282   virtual void addTraces()=0;
283 };
284
285 %feature("director") CpuModel;
286 class CpuModel : public Model {
287 public:
288   CpuModel();
289   virtual ~CpuModel();
290   virtual simgrid::surf::Cpu *createCpu(const char *name,
291                               DoubleDynar power_peak,
292                               int pstate, double power_scale,
293                               tmgr_trace *power_trace, int core,
294                               e_surf_resource_state_t state_initial,
295                               tmgr_trace *state_trace,
296                               s_xbt_dict *cpu_properties)=0;
297 };
298
299 class Resource {
300 public:
301   Resource();
302   const char *getName();
303   virtual bool isUsed()=0;
304   simgrid::surf::Model *getModel();
305
306   virtual e_surf_resource_state_t getState();
307   lmm_constraint *getConstraint();
308   s_xbt_dict *getProperties();
309   virtual void updateState(tmgr_trace_event *event_type, double value, double date)=0;
310 };
311
312 %feature("director") Cpu;
313 class Cpu : public Resource {
314 public:
315   Cpu(simgrid::surf::Model *model, const char *name, s_xbt_dict *props,
316     lmm_constraint *constraint, int core, double powerPeak, double powerScale);
317   Cpu(simgrid::surf::Model *model, const char *name, s_xbt_dict *props,
318     int core, double powerPeak, double powerScale);
319   virtual ~Cpu();
320   virtual double getCurrentPowerPeak();
321   virtual simgrid::surf::CpuAction *execute(double size)=0;
322   virtual simgrid::surf::CpuAction *sleep(double duration)=0;
323   virtual int getCore();
324   virtual double getSpeed(double load);
325   virtual double getAvailableSpeed();
326   virtual double getPowerPeakAt(int pstate_index)=0;
327   virtual int getNbPstates()=0;
328   virtual void setPstate(int pstate_index)=0;
329   virtual int  getPstate()=0;
330   void setState(e_surf_resource_state_t state);
331 };
332
333 class Link : public simgrid::surf::Resource {
334 public:
335   Link();
336   ~Link();
337   double getBandwidth();
338   void updateBandwidth(double value, double date=surf_get_clock());
339   double getLatency();
340   void updateLatency(double value, double date=surf_get_clock());
341 };
342
343 %nodefaultctor Action;
344 class Action {
345 public:
346   Action(Model *model, double cost, bool failed);
347   virtual ~Action();
348   Model *getModel();
349   lmm_variable *getVariable();
350   e_surf_action_state_t getState();
351   bool isSuspended();
352   double getBound();
353   void setBound(double bound);
354   void updateRemains(double delta);
355   virtual double getRemains();
356   virtual void setPriority(double priority);
357   virtual void setState(e_surf_action_state_t state);
358 };
359
360 %nodefaultctor CpuAction;
361 %feature("director") CpuAction;
362 class CpuAction : public Action {
363 public:
364 CpuAction(Model *model, double cost, bool failed);
365 %extend {
366   simgrid::surf::Cpu *getCpu() {return getActionCpu($self);}
367 }
368 };
369
370 %nodefaultctor NetworkAction;
371 class NetworkAction : public Action {
372 public:
373 %extend {
374   double getLatency() {return $self->m_latency;}
375 }
376 };
377
378
379 %nodefaultctor RoutingEdge;
380 class RoutingEdge {
381 public:
382   virtual char *getName()=0;
383 };
384
385 }
386 }
387
388 %rename lmm_constraint LmmConstraint;
389 struct lmm_constraint {
390 %extend {
391   double getUsage() {return lmm_constraint_get_usage($self);}
392 }
393 };
394
395 %rename lmm_variable LmmVariable;
396 struct lmm_variable {
397 %extend {
398   double getValue() {return lmm_variable_getvalue($self);}
399 }
400 };
401
402 %rename s_xbt_dict XbtDict;
403 struct s_xbt_dict {
404 %extend {
405   char *getValue(char *key) {return (char*)xbt_dict_get_or_null($self, key);}
406 }
407 };
408
409 %rename e_surf_action_state_t ActionState;
410 typedef enum {
411   SURF_ACTION_READY = 0,        /**< Ready        */
412   SURF_ACTION_RUNNING,          /**< Running      */
413   SURF_ACTION_FAILED,           /**< Task Failure */
414   SURF_ACTION_DONE,             /**< Completed    */
415   SURF_ACTION_TO_FREE,          /**< Action to free in next cleanup */
416   SURF_ACTION_NOT_IN_THE_SYSTEM
417                                 /**< Not in the system anymore. Why did you ask ? */
418 } e_surf_action_state_t;
419
420 %rename e_surf_resource_state_t ResourceState;
421 typedef enum {
422   SURF_RESOURCE_ON = 1,                   /**< Up & ready        */
423   SURF_RESOURCE_OFF = 0                   /**< Down & broken     */
424 } e_surf_resource_state_t;