Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
43d2c27ecff88fb26cea316c9891c60356e299f3
[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   Action * action = (Action *)jarg1;
50   jobject res;
51   CpuAction *cpu_action = dynamic_cast<CpuAction*>(action);
52   if (cpu_action) {
53     SwigDirector_CpuAction *dir_cpu_action = dynamic_cast<SwigDirector_CpuAction*>(cpu_action);
54     if (dir_cpu_action) {
55       res = dir_cpu_action->swig_get_self(env);\
56     } else {
57       jclass clss = env->FindClass("org/simgrid/surf/CpuAction");\
58       jmethodID constru = env->GetMethodID(clss, "<init>", "()V");\
59       res = env->NewObject(clss, constru);\
60       res = env->NewGlobalRef(res);\
61     }
62   } else {
63     jclass clss = env->FindClass("org/simgrid/surf/Action");\
64     jmethodID constru = env->GetMethodID(clss, "<init>", "()V");\
65     res = env->NewObject(clss, constru);\
66     res = env->NewGlobalRef(res);\
67   }
68   return res;
69 }
70
71 #define GETDIRECTOR(NAME) \
72 JNIEXPORT jobject JNICALL Java_org_simgrid_surf_SurfJNI_get## NAME ## Director(JNIEnv *env, jclass cls, jlong jarg1)\
73 {\
74   NAME * arg1 = (NAME*)jarg1;\
75   SwigDirector_ ##NAME *director = dynamic_cast<SwigDirector_ ##NAME *>(arg1);\
76   jobject res;\
77   if (director) {\
78     res = director->swig_get_self(env);\
79   } else {\
80     jclass clss = env->FindClass("org/simgrid/surf/NAME");\
81     jmethodID constru = env->GetMethodID(clss, "<init>", "()V");\
82     res = env->NewObject(clss, constru);\
83     res = env->NewGlobalRef(res);\
84   }\
85   return res;\
86 }
87
88 GETDIRECTOR(CpuModel)
89 GETDIRECTOR(Cpu)
90 GETDIRECTOR(CpuAction)
91
92 #ifdef __cplusplus
93 }
94 #endif
95
96 %}
97
98 %typemap(freearg) char* name {
99 }
100
101 /* Handle xbt_dynar_t of Link */
102 JAVA_ARRAYSOFCLASSES(Action);
103 %apply Action[] {ActionArrayPtr};
104 %typemap(jstype) ActionArrayPtr "Action[]"
105 %typemap(javain) ActionArrayPtr "Action.cArrayUnwrap($javainput)"
106 %typemap(javaout) ActionArrayPtr {
107   long[] cArray = $jnicall;
108   Action[] arrayWrapper = new Action[cArray.length];
109   for (int i=0; i<cArray.length; i++)
110     arrayWrapper[i] = (Action) Surf.getAction(cArray[i]);
111   return arrayWrapper;
112   //   return Action.cArrayWrap($jnicall, $owner);
113 }
114 %typemap(out) ActionArrayPtr {
115   long l = 0;
116   for(ActionList::iterator it($1->begin()), itend($1->end()); it != itend ; ++it) {
117     l++;
118   }
119   $result = jenv->NewLongArray(l);
120   jlong *elts = jenv->GetLongArrayElements($result, NULL);
121   l = 0;
122   for(ActionList::iterator it($1->begin()), itend($1->end()); it != itend ; ++it) {
123     elts[l++] = (jlong)static_cast<Action*>(&*it);
124   }
125   jenv->ReleaseLongArrayElements($result, elts, 0);
126 }
127
128 class ActionList {
129 public:
130   //void push_front(Action &action);
131   //void push_back(Action &action);
132 %extend {
133   ActionArrayPtr getArray(){
134     return $self;
135   }
136 }
137 };
138
139 /* Handle xbt_dynar_t of Link */
140 JAVA_ARRAYSOFCLASSES(Link);
141 %apply Link[] {LinkDynar};
142 %typemap(jstype) LinkDynar "Link[]"
143 %typemap(javain) LinkDynar "Link.cArrayUnwrap($javainput)"
144 %typemap(javaout) LinkDynar {
145      return Link.cArrayWrap($jnicall, $owner);
146 }
147 %typemap(out) LinkDynar {
148   long l = xbt_dynar_length($1);
149   $result = jenv->NewLongArray(l);
150   unsigned i;
151   Link *link;
152   jlong *elts = jenv->GetLongArrayElements($result, NULL);
153   xbt_dynar_foreach($1, i, link) {
154     elts[i] = (jlong)link;
155   }
156   jenv->ReleaseLongArrayElements($result, elts, 0);
157   xbt_dynar_free(&$1);
158 }
159
160 %nodefault DoubleDynar;
161 %typemap(jni) DoubleDynar "jdoubleArray"
162 %rename(DoubleDynar) Double[];
163 %typemap(jtype) DoubleDynar "double[]"
164 %typemap(jstype) DoubleDynar "double[]"
165 %typemap(out) DoubleDynar {
166   long l = xbt_dynar_length($1);
167   $result = jenv->NewDoubleArray(l);
168   double *lout = (double *) xbt_dynar_get_ptr($1, 0);
169   jenv->SetDoubleArrayRegion($result, 0, l, (jdouble*)lout);
170 }
171 %typemap(javadirectorin) DoubleDynar "$jniinput"
172 %typemap(directorin,descriptor="[D") DoubleDynar %{
173   long l = xbt_dynar_length($1);
174   $input = jenv->NewDoubleArray(l);
175   double *lout = (double *) xbt_dynar_get_ptr($1, 0);
176   jenv->SetDoubleArrayRegion($input, 0, l, (jdouble*)lout);
177 %}
178 %typemap(javain) DoubleDynar "$javainput"
179 %typemap(javaout) DoubleDynar {return  $jnicall}
180
181 /* Allow to subclass Plugin and send java object to C++ code */
182 %feature("director") Plugin;
183
184 %native(getAction) jobject getAction(jlong jarg1);
185 %native(getCpuModelDirector) jobject getCpuModelDirector(jlong jarg1);
186 %typemap(javaout) CpuModel * {
187   long cPtr = $jnicall;
188   return (CpuModel)Surf.getCpuModelDirector(cPtr);
189 }
190 %native(getCpuDirector) jobject getCpuDirector(jlong jarg1);
191 %typemap(javaout) Cpu * {
192   long cPtr = $jnicall;
193   return (Cpu)Surf.getCpuDirector(cPtr);
194 }
195 %native(getCpuActionDirector) jobject getCpuActionDirector(jlong jarg1);
196 %typemap(javaout) CpuAction * {
197   long cPtr = $jnicall;
198   return (CpuAction)Surf.getCpuDirector(cPtr);
199 }
200
201 %include "src/bindings/java/surf_swig.hpp"
202
203 %rename tmgr_trace TmgrTrace;
204 %nodefaultctor tmgr_trace;
205 struct tmgr_trace {
206   //enum e_trace_type type;
207   /*union {
208     struct {
209       xbt_dynar_t event_list;
210     } s_list;
211     struct {
212       probabilist_event_generator_t event_generator[2];
213       int is_state_trace;
214       int next_event;
215     } s_probabilist;
216   };*/
217   %extend {
218   }
219 };
220
221 %rename tmgr_trace_event TmgrTraceEvent;
222 %nodefaultctor tmgr_trace_event;
223 struct tmgr_trace_event {
224   //tmgr_trace_t trace;
225   //unsigned int idx;
226   //void *model;
227   //int free_me;
228   %extend {
229     unsigned int getIdx() {return 0;}//$self->idx;}
230   }
231 };
232
233 %nodefaultctor Model;
234 class Model {
235 public:
236   Model();
237   
238   virtual double shareResources(double now);
239   virtual double shareResourcesLazy(double now);
240   virtual double shareResourcesFull(double now);
241
242   virtual void updateActionsState(double now, double delta);
243   virtual void updateActionsStateLazy(double now, double delta);
244   virtual void updateActionsStateFull(double now, double delta);
245
246   virtual ActionList *getRunningActionSet();
247
248   virtual void addTraces()=0;
249 };
250
251 %feature("director") CpuModel;
252 class CpuModel : public Model {
253 public:
254   CpuModel();
255   virtual ~CpuModel();
256   virtual Cpu *createCpu(const char *name, DoubleDynar power_peak,
257                               int pstate, double power_scale,
258                               tmgr_trace *power_trace, int core,
259                               e_surf_resource_state_t state_initial,
260                               tmgr_trace *state_trace,
261                               s_xbt_dict *cpu_properties)=0;
262 };
263
264 class Resource {
265 public:
266   Resource();
267   const char *getName();
268   virtual bool isUsed()=0;
269   Model *getModel();
270
271   virtual e_surf_resource_state_t getState();
272   lmm_constraint *getConstraint();
273   s_xbt_dict *getProperties();
274   virtual void updateState(tmgr_trace_event *event_type, double value, double date)=0;
275 };
276
277 %feature("director") Cpu;
278 class Cpu : public Resource {
279 public:
280   Cpu(Model *model, const char *name, s_xbt_dict *props,
281     lmm_constraint *constraint, int core, double powerPeak, double powerScale);
282   Cpu(Model *model, const char *name, s_xbt_dict *props,
283     int core, double powerPeak, double powerScale);
284   virtual ~Cpu();
285   virtual double getCurrentPowerPeak();
286   virtual CpuAction *execute(double size)=0;
287   virtual CpuAction *sleep(double duration)=0;
288   virtual int getCore();
289   virtual double getSpeed(double load);
290   virtual double getAvailableSpeed();
291   virtual double getPowerPeakAt(int pstate_index)=0;
292   virtual int getNbPstates()=0;
293   virtual void setPstate(int pstate_index)=0;
294   virtual int  getPstate()=0;
295   void setState(e_surf_resource_state_t state);
296 };
297
298 class Link : public Resource {
299 public:
300   Link();
301   ~Link();
302   double getBandwidth();
303   void updateBandwidth(double value, double date=surf_get_clock());
304   double getLatency();
305   void updateLatency(double value, double date=surf_get_clock());
306 };
307
308 %nodefaultctor Action;
309 class Action {
310 public:
311   Action(Model *model, double cost, bool failed);
312   virtual ~Action();
313   Model *getModel();
314   lmm_variable *getVariable();
315   e_surf_action_state_t getState();
316   bool isSuspended();
317   double getBound();
318   void setBound(double bound);
319   void updateRemains(double delta);
320   virtual double getRemains();
321   virtual void setPriority(double priority);
322   virtual void setState(e_surf_action_state_t state);
323 };
324
325 %nodefaultctor CpuAction;
326 %feature("director") CpuAction;
327 class CpuAction : public Action {
328 public:
329 CpuAction(Model *model, double cost, bool failed);
330 %extend {
331   Cpu *getCpu() {return getActionCpu($self);}
332 }
333 };
334
335 %nodefaultctor NetworkAction;
336 class NetworkAction : public Action {
337 public:
338 %extend {
339   double getLatency() {return $self->m_latency;}
340 }
341 };
342
343
344 %nodefaultctor RoutingEdge;
345 class RoutingEdge {
346 public:
347   virtual char *getName()=0;
348 };
349
350 %rename lmm_constraint LmmConstraint;
351 struct lmm_constraint {
352 %extend {
353   double getUsage() {return lmm_constraint_get_usage($self);}
354 }
355 };
356
357 %rename lmm_variable LmmVariable;
358 struct lmm_variable {
359 %extend {
360   double getValue() {return lmm_variable_getvalue($self);}
361 }
362 };
363
364 %rename s_xbt_dict XbtDict;
365 struct s_xbt_dict {
366 %extend {
367   char *getValue(char *key) {return (char*)xbt_dict_get_or_null($self, key);}
368 }
369 };
370
371 %rename e_surf_action_state_t ActionState;
372 typedef enum {
373   SURF_ACTION_READY = 0,        /**< Ready        */
374   SURF_ACTION_RUNNING,          /**< Running      */
375   SURF_ACTION_FAILED,           /**< Task Failure */
376   SURF_ACTION_DONE,             /**< Completed    */
377   SURF_ACTION_TO_FREE,          /**< Action to free in next cleanup */
378   SURF_ACTION_NOT_IN_THE_SYSTEM
379                                 /**< Not in the system anymore. Why did you ask ? */
380 } e_surf_action_state_t;
381
382 %rename e_surf_resource_state_t ResourceState;
383 typedef enum {
384   SURF_RESOURCE_ON = 1,                   /**< Up & ready        */
385   SURF_RESOURCE_OFF = 0                   /**< Down & broken     */
386 } e_surf_resource_state_t;