Logo AND Algorithmique Numérique Distribuée

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