Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
First series of changes to lua-bindings in order to
[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_to_array($1);
169   jenv->SetDoubleArrayRegion($result, 0, l, (jdouble*)lout);
170   free(:);
171 }
172 %typemap(javadirectorin) DoubleDynar "$jniinput"
173 %typemap(directorin,descriptor="[D") DoubleDynar %{
174   long l = xbt_dynar_length($1);
175   $input = jenv->NewDoubleArray(l);
176   double *lout = (double *)xbt_dynar_to_array($1);
177   jenv->SetDoubleArrayRegion($input, 0, l, (jdouble*)lout);
178   free(lout);
179 %}
180 %typemap(javain) DoubleDynar "$javainput"
181 %typemap(javaout) DoubleDynar {return  $jnicall}
182
183 /* Allow to subclass Plugin and send java object to C++ code */
184 %feature("director") Plugin;
185
186 %native(getAction) jobject getAction(jlong jarg1);
187 %native(getCpuModelDirector) jobject getCpuModelDirector(jlong jarg1);
188 %typemap(javaout) CpuModel * {
189   long cPtr = $jnicall;
190   return (CpuModel)Surf.getCpuModelDirector(cPtr);
191 }
192 %native(getCpuDirector) jobject getCpuDirector(jlong jarg1);
193 %typemap(javaout) Cpu * {
194   long cPtr = $jnicall;
195   return (Cpu)Surf.getCpuDirector(cPtr);
196 }
197 %native(getCpuActionDirector) jobject getCpuActionDirector(jlong jarg1);
198 %typemap(javaout) CpuAction * {
199   long cPtr = $jnicall;
200   return (CpuAction)Surf.getCpuDirector(cPtr);
201 }
202
203 %include "src/bindings/java/surf_swig.hpp"
204
205 %rename tmgr_trace TmgrTrace;
206 %nodefaultctor tmgr_trace;
207 struct tmgr_trace {
208   //enum e_trace_type type;
209   /*union {
210     struct {
211       xbt_dynar_t event_list;
212     } s_list;
213     struct {
214       probabilist_event_generator_t event_generator[2];
215       int is_state_trace;
216       int next_event;
217     } s_probabilist;
218   };*/
219   %extend {
220   }
221 };
222
223 %rename tmgr_trace_event TmgrTraceEvent;
224 %nodefaultctor tmgr_trace_event;
225 struct tmgr_trace_event {
226   //tmgr_trace_t trace;
227   //unsigned int idx;
228   //void *model;
229   //int free_me;
230   %extend {
231     unsigned int getIdx() {return 0;}//$self->idx;}
232   }
233 };
234
235 %nodefaultctor Model;
236 class Model {
237 public:
238   Model();
239   
240   virtual double shareResources(double now);
241   virtual double shareResourcesLazy(double now);
242   virtual double shareResourcesFull(double now);
243
244   virtual void updateActionsState(double now, double delta);
245   virtual void updateActionsStateLazy(double now, double delta);
246   virtual void updateActionsStateFull(double now, double delta);
247
248   virtual ActionList *getRunningActionSet();
249
250   virtual void addTraces()=0;
251 };
252
253 %feature("director") CpuModel;
254 class CpuModel : public Model {
255 public:
256   CpuModel();
257   virtual ~CpuModel();
258   virtual Cpu *createCpu(const char *name, DoubleDynar power_peak,
259                               int pstate, double power_scale,
260                               tmgr_trace *power_trace, int core,
261                               e_surf_resource_state_t state_initial,
262                               tmgr_trace *state_trace,
263                               s_xbt_dict *cpu_properties)=0;
264 };
265
266 class Resource {
267 public:
268   Resource();
269   const char *getName();
270   virtual bool isUsed()=0;
271   Model *getModel();
272
273   virtual e_surf_resource_state_t getState();
274   lmm_constraint *getConstraint();
275   s_xbt_dict *getProperties();
276   virtual void updateState(tmgr_trace_event *event_type, double value, double date)=0;
277 };
278
279 %feature("director") Cpu;
280 class Cpu : public Resource {
281 public:
282   Cpu(Model *model, const char *name, s_xbt_dict *props,
283     lmm_constraint *constraint, int core, double powerPeak, double powerScale);
284   Cpu(Model *model, const char *name, s_xbt_dict *props,
285     int core, double powerPeak, double powerScale);
286   virtual ~Cpu();
287   virtual double getCurrentPowerPeak();
288   virtual CpuAction *execute(double size)=0;
289   virtual CpuAction *sleep(double duration)=0;
290   virtual int getCore();
291   virtual double getSpeed(double load);
292   virtual double getAvailableSpeed();
293   virtual double getPowerPeakAt(int pstate_index)=0;
294   virtual int getNbPstates()=0;
295   virtual void setPstate(int pstate_index)=0;
296   virtual int  getPstate()=0;
297   void setState(e_surf_resource_state_t state);
298 };
299
300 class Link : public Resource {
301 public:
302   Link();
303   ~Link();
304   double getBandwidth();
305   void updateBandwidth(double value, double date=surf_get_clock());
306   double getLatency();
307   void updateLatency(double value, double date=surf_get_clock());
308 };
309
310 %nodefaultctor Action;
311 class Action {
312 public:
313   Action(Model *model, double cost, bool failed);
314   virtual ~Action();
315   Model *getModel();
316   lmm_variable *getVariable();
317   e_surf_action_state_t getState();
318   bool isSuspended();
319   double getBound();
320   void setBound(double bound);
321   void updateRemains(double delta);
322   virtual double getRemains();
323   virtual void setPriority(double priority);
324   virtual void setState(e_surf_action_state_t state);
325 };
326
327 %nodefaultctor CpuAction;
328 %feature("director") CpuAction;
329 class CpuAction : public Action {
330 public:
331 CpuAction(Model *model, double cost, bool failed);
332 %extend {
333   Cpu *getCpu() {return getActionCpu($self);}
334 }
335 };
336
337 %nodefaultctor NetworkAction;
338 class NetworkAction : public Action {
339 public:
340 %extend {
341   double getLatency() {return $self->m_latency;}
342 }
343 };
344
345
346 %nodefaultctor RoutingEdge;
347 class RoutingEdge {
348 public:
349   virtual char *getName()=0;
350 };
351
352 %rename lmm_constraint LmmConstraint;
353 struct lmm_constraint {
354 %extend {
355   double getUsage() {return lmm_constraint_get_usage($self);}
356 }
357 };
358
359 %rename lmm_variable LmmVariable;
360 struct lmm_variable {
361 %extend {
362   double getValue() {return lmm_variable_getvalue($self);}
363 }
364 };
365
366 %rename s_xbt_dict XbtDict;
367 struct s_xbt_dict {
368 %extend {
369   char *getValue(char *key) {return (char*)xbt_dict_get_or_null($self, key);}
370 }
371 };
372
373 %rename e_surf_action_state_t ActionState;
374 typedef enum {
375   SURF_ACTION_READY = 0,        /**< Ready        */
376   SURF_ACTION_RUNNING,          /**< Running      */
377   SURF_ACTION_FAILED,           /**< Task Failure */
378   SURF_ACTION_DONE,             /**< Completed    */
379   SURF_ACTION_TO_FREE,          /**< Action to free in next cleanup */
380   SURF_ACTION_NOT_IN_THE_SYSTEM
381                                 /**< Not in the system anymore. Why did you ask ? */
382 } e_surf_action_state_t;
383
384 %rename e_surf_resource_state_t ResourceState;
385 typedef enum {
386   SURF_RESOURCE_ON = 1,                   /**< Up & ready        */
387   SURF_RESOURCE_OFF = 0                   /**< Down & broken     */
388 } e_surf_resource_state_t;