1 /* Copyright (c) 2014. The SimGrid Team.
2 * All rights reserved. */
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. */
8 %module(directors="1") Surf
10 %include "arrays_java.i"
11 %include "std_string.i"
14 %pragma(java) jniclassimports=%{
15 import org.simgrid.NativeLib;
17 %pragma(java) jniclasscode=%{
19 NativeLib.nativeInit("simgrid");
20 NativeLib.nativeInit("surf-java");
21 Runtime.getRuntime().addShutdownHook(
24 Thread.currentThread().setName( "Destroyer" );
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"
40 typedef struct lmm_constraint *lmm_constraint_t;
41 typedef xbt_dynar_t DoubleDynar;
49 JNIEXPORT jobject JNICALL Java_org_simgrid_surf_SurfJNI_getAction(JNIEnv *env, jclass cls, jlong jarg1) {
50 Action * action = (Action *)jarg1;
52 CpuAction *cpu_action = dynamic_cast<CpuAction*>(action);
54 SwigDirector_CpuAction *dir_cpu_action = dynamic_cast<SwigDirector_CpuAction*>(cpu_action);
56 res = dir_cpu_action->swig_get_self(env);\
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);\
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);\
72 #define GETDIRECTOR(NAME) \
73 JNIEXPORT jobject JNICALL Java_org_simgrid_surf_SurfJNI_get## NAME ## Director(JNIEnv *env, jclass cls, jlong jarg1)\
75 NAME * arg1 = (NAME*)jarg1;\
76 SwigDirector_ ##NAME *director = dynamic_cast<SwigDirector_ ##NAME *>(arg1);\
79 res = director->swig_get_self(env);\
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);\
91 GETDIRECTOR(CpuAction)
99 %typemap(freearg) char* name {
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]);
113 // return Action.cArrayWrap($jnicall, $owner);
115 %typemap(out) ActionArrayPtr {
117 for(ActionList::iterator it($1->begin()), itend($1->end()); it != itend ; ++it) {
120 $result = jenv->NewLongArray(l);
121 jlong *elts = jenv->GetLongArrayElements($result, NULL);
123 for(ActionList::iterator it($1->begin()), itend($1->end()); it != itend ; ++it) {
124 elts[l++] = (jlong)static_cast<ActionPtr>(&*it);
126 jenv->ReleaseLongArrayElements($result, elts, 0);
131 //void push_front(Action &action);
132 //void push_back(Action &action);
134 ActionArrayPtr getArray(){
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);
148 %typemap(out) NetworkLinkDynar {
149 long l = xbt_dynar_length($1);
150 $result = jenv->NewLongArray(l);
153 jlong *elts = jenv->GetLongArrayElements($result, NULL);
154 xbt_dynar_foreach($1, i, link) {
155 elts[i] = (jlong)link;
157 jenv->ReleaseLongArrayElements($result, elts, 0);
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);
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);
181 %typemap(javain) DoubleDynar "$javainput"
182 %typemap(javaout) DoubleDynar {return $jnicall}
184 /* Allow to subclass Plugin and send java object to C++ code */
185 %feature("director") Plugin;
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);
193 %native(getCpuDirector) jobject getCpuDirector(jlong jarg1);
194 %typemap(javaout) Cpu * {
195 long cPtr = $jnicall;
196 return (Cpu)Surf.getCpuDirector(cPtr);
198 %native(getCpuActionDirector) jobject getCpuActionDirector(jlong jarg1);
199 %typemap(javaout) CpuAction * {
200 long cPtr = $jnicall;
201 return (CpuAction)Surf.getCpuDirector(cPtr);
204 %include "src/bindings/java/surf_swig.hpp"
206 %rename tmgr_trace TmgrTrace;
207 %nodefaultctor tmgr_trace;
209 //enum e_trace_type type;
212 xbt_dynar_t event_list;
215 probabilist_event_generator_t event_generator[2];
224 %rename tmgr_trace_event TmgrTraceEvent;
225 %nodefaultctor tmgr_trace_event;
226 struct tmgr_trace_event {
227 //tmgr_trace_t trace;
232 unsigned int getIdx() {return 0;}//$self->idx;}
236 %nodefaultctor Model;
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);
245 virtual void updateActionsState(double now, double delta);
246 virtual void updateActionsStateLazy(double now, double delta);
247 virtual void updateActionsStateFull(double now, double delta);
249 virtual ActionList *getRunningActionSet();
251 virtual void addTraces()=0;
254 %feature("director") CpuModel;
255 class CpuModel : public Model {
257 CpuModel(const char *name);
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;
270 const char *getName();
271 virtual bool isUsed()=0;
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;
280 %feature("director") Cpu;
281 class Cpu : public Resource {
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);
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 virtual int getPstate()=0;
298 void setState(e_surf_resource_state_t state);
301 class NetworkLink : public Resource {
305 double getBandwidth();
306 void updateBandwidth(double value, double date=surf_get_clock());
308 void updateLatency(double value, double date=surf_get_clock());
311 %nodefaultctor Action;
314 Action(Model *model, double cost, bool failed);
317 lmm_variable *getVariable();
318 e_surf_action_state_t getState();
321 void setBound(double bound);
322 void updateRemains(double delta);
323 virtual double getRemains();
324 virtual void setPriority(double priority);
325 virtual void setState(e_surf_action_state_t state);
328 %nodefaultctor CpuAction;
329 %feature("director") CpuAction;
330 class CpuAction : public Action {
332 CpuAction(Model *model, double cost, bool failed);
334 Cpu *getCpu() {return getActionCpu($self);}
338 %nodefaultctor NetworkAction;
339 class NetworkAction : public Action {
342 double getLatency() {return $self->m_latency;}
347 %nodefaultctor RoutingEdge;
350 virtual char *getName()=0;
353 %rename lmm_constraint LmmConstraint;
354 struct lmm_constraint {
356 double getUsage() {return lmm_constraint_get_usage($self);}
360 %rename lmm_variable LmmVariable;
361 struct lmm_variable {
363 double getValue() {return lmm_variable_getvalue($self);}
367 %rename s_xbt_dict XbtDict;
370 char *getValue(char *key) {return (char*)xbt_dict_get_or_null($self, key);}
374 %rename e_surf_action_state_t ActionState;
376 SURF_ACTION_READY = 0, /**< Ready */
377 SURF_ACTION_RUNNING, /**< Running */
378 SURF_ACTION_FAILED, /**< Task Failure */
379 SURF_ACTION_DONE, /**< Completed */
380 SURF_ACTION_TO_FREE, /**< Action to free in next cleanup */
381 SURF_ACTION_NOT_IN_THE_SYSTEM
382 /**< Not in the system anymore. Why did you ask ? */
383 } e_surf_action_state_t;
385 %rename e_surf_resource_state_t ResourceState;
387 SURF_RESOURCE_ON = 1, /**< Up & ready */
388 SURF_RESOURCE_OFF = 0 /**< Down & broken */
389 } e_surf_resource_state_t;