Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[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     NativeLib.nativeInit("simgrid");
20     NativeLib.nativeInit("surf-java");
21     Runtime.getRuntime().addShutdownHook(
22       new Thread() {
23         public void run() {
24           Thread.currentThread().setName( "Destroyer" );
25           Surf.clean();
26         }
27       }
28     );
29   }
30 %}
31
32 %{
33 #include "src/surf/surf_interface.hpp"
34 #include "src/surf/cpu_interface.hpp"
35 #include "src/surf/network_interface.hpp"
36 #include "src/surf/trace_mgr_private.h"
37 #include "src/bindings/java/surf_swig.hpp"
38 #include "src/xbt/dict_private.h"
39
40 typedef struct lmm_constraint *lmm_constraint_t;
41 typedef xbt_dynar_t DoubleDynar;
42 %}
43
44 %wrapper %{
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48
49 JNIEXPORT jobject JNICALL Java_org_simgrid_surf_SurfJNI_getAction(JNIEnv *env, jclass cls, jlong jarg1) {
50   Action * action = (Action *)jarg1;
51   jobject res;
52   CpuAction *cpu_action = dynamic_cast<CpuAction*>(action);
53   if (cpu_action) {
54     SwigDirector_CpuAction *dir_cpu_action = dynamic_cast<SwigDirector_CpuAction*>(cpu_action);
55     if (dir_cpu_action) {
56       res = dir_cpu_action->swig_get_self(env);\
57     } else {
58       jclass clss = env->FindClass("org/simgrid/surf/CpuAction");\
59       jmethodID constru = env->GetMethodID(clss, "<init>", "()V");\
60       res = env->NewObject(clss, constru);\
61       res = env->NewGlobalRef(res);\
62     }
63   } else {
64     jclass clss = env->FindClass("org/simgrid/surf/Action");\
65     jmethodID constru = env->GetMethodID(clss, "<init>", "()V");\
66     res = env->NewObject(clss, constru);\
67     res = env->NewGlobalRef(res);\
68   }
69   return res;
70 }
71
72 #define GETDIRECTOR(NAME) \
73 JNIEXPORT jobject JNICALL Java_org_simgrid_surf_SurfJNI_get## NAME ## Director(JNIEnv *env, jclass cls, jlong jarg1)\
74 {\
75   NAME * arg1 = (NAME*)jarg1;\
76   SwigDirector_ ##NAME *director = dynamic_cast<SwigDirector_ ##NAME *>(arg1);\
77   jobject res;\
78   if (director) {\
79     res = director->swig_get_self(env);\
80   } else {\
81     jclass clss = env->FindClass("org/simgrid/surf/NAME");\
82     jmethodID constru = env->GetMethodID(clss, "<init>", "()V");\
83     res = env->NewObject(clss, constru);\
84     res = env->NewGlobalRef(res);\
85   }\
86   return res;\
87 }
88
89 GETDIRECTOR(CpuModel)
90 GETDIRECTOR(Cpu)
91 GETDIRECTOR(CpuAction)
92
93 #ifdef __cplusplus
94 }
95 #endif
96
97 %}
98
99 %typemap(freearg) char* name {
100 }
101
102 /* Handle xbt_dynar_t of NetworkLink */
103 JAVA_ARRAYSOFCLASSES(Action);
104 %apply Action[] {ActionArrayPtr};
105 %typemap(jstype) ActionArrayPtr "Action[]"
106 %typemap(javain) ActionArrayPtr "Action.cArrayUnwrap($javainput)"
107 %typemap(javaout) ActionArrayPtr {
108   long[] cArray = $jnicall;
109   Action[] arrayWrapper = new Action[cArray.length];
110   for (int i=0; i<cArray.length; i++)
111     arrayWrapper[i] = (Action) Surf.getAction(cArray[i]);
112   return arrayWrapper;
113   //   return Action.cArrayWrap($jnicall, $owner);
114 }
115 %typemap(out) ActionArrayPtr {
116   long l = 0;
117   for(ActionList::iterator it($1->begin()), itend($1->end()); it != itend ; ++it) {
118     l++;
119   }
120   $result = jenv->NewLongArray(l);
121   jlong *elts = jenv->GetLongArrayElements($result, NULL);
122   l = 0;
123   for(ActionList::iterator it($1->begin()), itend($1->end()); it != itend ; ++it) {
124     elts[l++] = (jlong)static_cast<ActionPtr>(&*it);
125   }
126   jenv->ReleaseLongArrayElements($result, elts, 0);
127 }
128
129 class ActionList {
130 public:
131   //void push_front(Action &action);
132   //void push_back(Action &action);
133 %extend {
134   ActionArrayPtr getArray(){
135     return $self;
136   }
137 }
138 };
139
140 /* Handle xbt_dynar_t of NetworkLink */
141 JAVA_ARRAYSOFCLASSES(NetworkLink);
142 %apply NetworkLink[] {NetworkLinkDynar};
143 %typemap(jstype) NetworkLinkDynar "NetworkLink[]"
144 %typemap(javain) NetworkLinkDynar "NetworkLink.cArrayUnwrap($javainput)"
145 %typemap(javaout) NetworkLinkDynar {
146      return NetworkLink.cArrayWrap($jnicall, $owner);
147 }
148 %typemap(out) NetworkLinkDynar {
149   long l = xbt_dynar_length($1);
150   $result = jenv->NewLongArray(l);
151   unsigned i;
152   NetworkLink *link;
153   jlong *elts = jenv->GetLongArrayElements($result, NULL);
154   xbt_dynar_foreach($1, i, link) {
155     elts[i] = (jlong)link;
156   }
157   jenv->ReleaseLongArrayElements($result, elts, 0);
158   xbt_dynar_free(&$1);
159 }
160
161 %nodefault DoubleDynar;
162 %typemap(jni) DoubleDynar "jdoubleArray"
163 %rename(DoubleDynar) Double[];
164 %typemap(jtype) DoubleDynar "double[]"
165 %typemap(jstype) DoubleDynar "double[]"
166 %typemap(out) DoubleDynar {
167   long l = xbt_dynar_length($1);
168   $result = jenv->NewDoubleArray(l);
169   double *lout = (double *)xbt_dynar_to_array($1);
170   jenv->SetDoubleArrayRegion($result, 0, l, (jdouble*)lout);
171   free(:);
172 }
173 %typemap(javadirectorin) DoubleDynar "$jniinput"
174 %typemap(directorin,descriptor="[D") DoubleDynar %{
175   long l = xbt_dynar_length($1);
176   $input = jenv->NewDoubleArray(l);
177   double *lout = (double *)xbt_dynar_to_array($1);
178   jenv->SetDoubleArrayRegion($input, 0, l, (jdouble*)lout);
179   free(lout);
180 %}
181 %typemap(javain) DoubleDynar "$javainput"
182 %typemap(javaout) DoubleDynar {return  $jnicall}
183
184 /* Allow to subclass Plugin and send java object to C++ code */
185 %feature("director") Plugin;
186
187 %native(getAction) jobject getAction(jlong jarg1);
188 %native(getCpuModelDirector) jobject getCpuModelDirector(jlong jarg1);
189 %typemap(javaout) CpuModel * {
190   long cPtr = $jnicall;
191   return (CpuModel)Surf.getCpuModelDirector(cPtr);
192 }
193 %native(getCpuDirector) jobject getCpuDirector(jlong jarg1);
194 %typemap(javaout) Cpu * {
195   long cPtr = $jnicall;
196   return (Cpu)Surf.getCpuDirector(cPtr);
197 }
198 %native(getCpuActionDirector) jobject getCpuActionDirector(jlong jarg1);
199 %typemap(javaout) CpuAction * {
200   long cPtr = $jnicall;
201   return (CpuAction)Surf.getCpuDirector(cPtr);
202 }
203
204 %include "src/bindings/java/surf_swig.hpp"
205
206 %rename tmgr_trace TmgrTrace;
207 %nodefaultctor tmgr_trace;
208 struct tmgr_trace {
209   //enum e_trace_type type;
210   /*union {
211     struct {
212       xbt_dynar_t event_list;
213     } s_list;
214     struct {
215       probabilist_event_generator_t event_generator[2];
216       int is_state_trace;
217       int next_event;
218     } s_probabilist;
219   };*/
220   %extend {
221   }
222 };
223
224 %rename tmgr_trace_event TmgrTraceEvent;
225 %nodefaultctor tmgr_trace_event;
226 struct tmgr_trace_event {
227   //tmgr_trace_t trace;
228   //unsigned int idx;
229   //void *model;
230   //int free_me;
231   %extend {
232     unsigned int getIdx() {return 0;}//$self->idx;}
233   }
234 };
235
236 %nodefaultctor Model;
237 class Model {
238 public:
239   Model(const char *name);
240   const char *getName();
241   virtual double shareResources(double now);
242   virtual double shareResourcesLazy(double now);
243   virtual double shareResourcesFull(double now);
244
245   virtual void updateActionsState(double now, double delta);
246   virtual void updateActionsStateLazy(double now, double delta);
247   virtual void updateActionsStateFull(double now, double delta);
248
249   virtual ActionList *getRunningActionSet();
250
251   virtual void addTraces()=0;
252 };
253
254 %feature("director") CpuModel;
255 class CpuModel : public Model {
256 public:
257   CpuModel(const char *name);
258   virtual ~CpuModel();
259   virtual Cpu *createCpu(const char *name, DoubleDynar power_peak,
260                               int pstate, double power_scale,
261                               tmgr_trace *power_trace, int core,
262                               e_surf_resource_state_t state_initial,
263                               tmgr_trace *state_trace,
264                               s_xbt_dict *cpu_properties)=0;
265 };
266
267 class Resource {
268 public:
269   Resource();
270   const char *getName();
271   virtual bool isUsed()=0;
272   Model *getModel();
273
274   virtual e_surf_resource_state_t getState();
275   lmm_constraint *getConstraint();
276   s_xbt_dict *getProperties();
277   virtual void updateState(tmgr_trace_event *event_type, double value, double date)=0;
278 };
279
280 %feature("director") Cpu;
281 class Cpu : public Resource {
282 public:
283   Cpu(Model *model, const char *name, s_xbt_dict *props,
284     lmm_constraint *constraint, int core, double powerPeak, double powerScale);
285   Cpu(Model *model, const char *name, s_xbt_dict *props,
286     int core, double powerPeak, double powerScale);
287   virtual ~Cpu();
288   virtual double getCurrentPowerPeak();
289   virtual CpuAction *execute(double size)=0;
290   virtual CpuAction *sleep(double duration)=0;
291   virtual int getCore();
292   virtual double getSpeed(double load);
293   virtual double getAvailableSpeed();
294   virtual double getPowerPeakAt(int pstate_index)=0;
295   virtual int getNbPstates()=0;
296   virtual void setPstate(int pstate_index)=0;
297   void setState(e_surf_resource_state_t state);
298 };
299
300 class NetworkLink : public Resource {
301 public:
302   NetworkLink();
303   ~NetworkLink();
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;