Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
70006b39365cf9811cb6d4b6937d5c4243a57c64
[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("surf-java");
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 NetworkLink */
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<ActionPtr>(&*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 NetworkLink */
140 JAVA_ARRAYSOFCLASSES(NetworkLink);
141 %apply NetworkLink[] {NetworkLinkDynar};
142 %typemap(jstype) NetworkLinkDynar "NetworkLink[]"
143 %typemap(javain) NetworkLinkDynar "NetworkLink.cArrayUnwrap($javainput)"
144 %typemap(javaout) NetworkLinkDynar {
145      return NetworkLink.cArrayWrap($jnicall, $owner);
146 }
147 %typemap(out) NetworkLinkDynar {
148   long l = xbt_dynar_length($1);
149   $result = jenv->NewLongArray(l);
150   unsigned i;
151   NetworkLink *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, (const jdouble*)lout);
170   free(lout);
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, (const 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(const char *name);
239   const char *getName();
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
251 %feature("director") CpuModel;
252 class CpuModel : public Model {
253 public:
254   CpuModel(const char *name);
255   virtual ~CpuModel();
256   virtual Cpu *createResource(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   virtual void addTraces()=0;
263 };
264
265 class Resource {
266 public:
267   Resource();
268   const char *getName();
269   virtual bool isUsed()=0;
270   Model *getModel();
271
272   virtual e_surf_resource_state_t getState();
273   lmm_constraint *getConstraint();
274   s_xbt_dict *getProperties();
275   virtual void updateState(tmgr_trace_event *event_type, double value, double date)=0;
276 };
277
278 %feature("director") Cpu;
279 class Cpu : public Resource {
280 public:
281   Cpu(Model *model, const char *name, s_xbt_dict *props,
282     lmm_constraint *constraint, int core, double powerPeak, double powerScale);
283   Cpu(Model *model, const char *name, s_xbt_dict *props,
284     int core, double powerPeak, double powerScale);
285   virtual ~Cpu();
286   virtual double getCurrentPowerPeak();
287   virtual CpuAction *execute(double size)=0;
288   virtual CpuAction *sleep(double duration)=0;
289   virtual int getCore();
290   virtual double getSpeed(double load);
291   virtual double getAvailableSpeed();
292   virtual double getPowerPeakAt(int pstate_index)=0;
293   virtual int getNbPstates()=0;
294   virtual void setPowerPeakAt(int pstate_index)=0;
295   void setState(e_surf_resource_state_t state);
296 };
297
298 class NetworkLink : public Resource {
299 public:
300   NetworkLink();
301   ~NetworkLink();
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;