Logo AND Algorithmique Numérique Distribuée

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