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 if (System.getProperty("os.name").toLowerCase().startsWith("win"))
20 NativeLib.nativeInit("winpthread");
21 NativeLib.nativeInit("simgrid");
22 NativeLib.nativeInit("surf-java");
23 Runtime.getRuntime().addShutdownHook(
26 Thread.currentThread().setName( "Destroyer" );
35 #include "src/surf/surf_interface.hpp"
36 #include "src/surf/cpu_interface.hpp"
37 #include "src/surf/network_interface.hpp"
38 #include "src/surf/trace_mgr_private.h"
39 #include "src/bindings/java/surf_swig.hpp"
40 #include "src/xbt/dict_private.h"
42 typedef struct lmm_constraint *lmm_constraint_t;
43 typedef xbt_dynar_t DoubleDynar;
51 JNIEXPORT jobject JNICALL Java_org_simgrid_surf_SurfJNI_getAction(JNIEnv *env, jclass cls, jlong jarg1) {
52 Action * action = (Action *)jarg1;
54 CpuAction *cpu_action = dynamic_cast<CpuAction*>(action);
56 SwigDirector_CpuAction *dir_cpu_action = dynamic_cast<SwigDirector_CpuAction*>(cpu_action);
58 res = dir_cpu_action->swig_get_self(env);\
60 jclass clss = env->FindClass("org/simgrid/surf/CpuAction");\
61 jmethodID constru = env->GetMethodID(clss, "<init>", "()V");\
62 res = env->NewObject(clss, constru);\
63 res = env->NewGlobalRef(res);\
66 jclass clss = env->FindClass("org/simgrid/surf/Action");\
67 jmethodID constru = env->GetMethodID(clss, "<init>", "()V");\
68 res = env->NewObject(clss, constru);\
69 res = env->NewGlobalRef(res);\
74 #define GETDIRECTOR(NAME) \
75 JNIEXPORT jobject JNICALL Java_org_simgrid_surf_SurfJNI_get## NAME ## Director(JNIEnv *env, jclass cls, jlong jarg1)\
77 NAME * arg1 = (NAME*)jarg1;\
78 SwigDirector_ ##NAME *director = dynamic_cast<SwigDirector_ ##NAME *>(arg1);\
81 res = director->swig_get_self(env);\
83 jclass clss = env->FindClass("org/simgrid/surf/NAME");\
84 jmethodID constru = env->GetMethodID(clss, "<init>", "()V");\
85 res = env->NewObject(clss, constru);\
86 res = env->NewGlobalRef(res);\
93 GETDIRECTOR(CpuAction)
101 %typemap(freearg) char* name {
104 /* Handle xbt_dynar_t of Link */
105 JAVA_ARRAYSOFCLASSES(Action);
106 %apply Action[] {ActionArrayPtr};
107 %typemap(jstype) ActionArrayPtr "Action[]"
108 %typemap(javain) ActionArrayPtr "Action.cArrayUnwrap($javainput)"
109 %typemap(javaout) ActionArrayPtr {
110 long[] cArray = $jnicall;
111 Action[] arrayWrapper = new Action[cArray.length];
112 for (int i=0; i<cArray.length; i++)
113 arrayWrapper[i] = (Action) Surf.getAction(cArray[i]);
115 // return Action.cArrayWrap($jnicall, $owner);
117 %typemap(out) ActionArrayPtr {
119 for(ActionList::iterator it($1->begin()), itend($1->end()); it != itend ; ++it) {
122 $result = jenv->NewLongArray(l);
123 jlong *elts = jenv->GetLongArrayElements($result, NULL);
125 for(ActionList::iterator it($1->begin()), itend($1->end()); it != itend ; ++it) {
126 elts[l++] = (jlong)static_cast<Action*>(&*it);
128 jenv->ReleaseLongArrayElements($result, elts, 0);
133 //void push_front(Action &action);
134 //void push_back(Action &action);
136 ActionArrayPtr getArray(){
142 /* Handle xbt_dynar_t of Link */
143 JAVA_ARRAYSOFCLASSES(Link);
144 %apply Link[] {LinkDynar};
145 %typemap(jstype) LinkDynar "Link[]"
146 %typemap(javain) LinkDynar "Link.cArrayUnwrap($javainput)"
147 %typemap(javaout) LinkDynar {
148 return Link.cArrayWrap($jnicall, $owner);
150 %typemap(out) LinkDynar {
151 long l = xbt_dynar_length($1);
152 $result = jenv->NewLongArray(l);
155 jlong *elts = jenv->GetLongArrayElements($result, NULL);
156 xbt_dynar_foreach($1, i, link) {
157 elts[i] = (jlong)link;
159 jenv->ReleaseLongArrayElements($result, elts, 0);
163 %nodefault DoubleDynar;
164 %typemap(jni) DoubleDynar "jdoubleArray"
165 %rename(DoubleDynar) Double[];
166 %typemap(jtype) DoubleDynar "double[]"
167 %typemap(jstype) DoubleDynar "double[]"
168 %typemap(out) DoubleDynar {
169 long l = xbt_dynar_length($1);
170 $result = jenv->NewDoubleArray(l);
171 double *lout = (double *)xbt_dynar_to_array($1);
172 jenv->SetDoubleArrayRegion($result, 0, l, (jdouble*)lout);
175 %typemap(javadirectorin) DoubleDynar "$jniinput"
176 %typemap(directorin,descriptor="[D") DoubleDynar %{
177 long l = xbt_dynar_length($1);
178 $input = jenv->NewDoubleArray(l);
179 double *lout = (double *)xbt_dynar_to_array($1);
180 jenv->SetDoubleArrayRegion($input, 0, l, (jdouble*)lout);
183 %typemap(javain) DoubleDynar "$javainput"
184 %typemap(javaout) DoubleDynar {return $jnicall}
186 /* Allow to subclass Plugin and send java object to C++ code */
187 %feature("director") Plugin;
189 %native(getAction) jobject getAction(jlong jarg1);
190 %native(getCpuModelDirector) jobject getCpuModelDirector(jlong jarg1);
191 %typemap(javaout) CpuModel * {
192 long cPtr = $jnicall;
193 return (CpuModel)Surf.getCpuModelDirector(cPtr);
195 %native(getCpuDirector) jobject getCpuDirector(jlong jarg1);
196 %typemap(javaout) Cpu * {
197 long cPtr = $jnicall;
198 return (Cpu)Surf.getCpuDirector(cPtr);
200 %native(getCpuActionDirector) jobject getCpuActionDirector(jlong jarg1);
201 %typemap(javaout) CpuAction * {
202 long cPtr = $jnicall;
203 return (CpuAction)Surf.getCpuDirector(cPtr);
206 %include "src/bindings/java/surf_swig.hpp"
208 %rename tmgr_trace TmgrTrace;
209 %nodefaultctor tmgr_trace;
211 //enum e_trace_type type;
214 xbt_dynar_t event_list;
217 probabilist_event_generator_t event_generator[2];
226 %rename tmgr_trace_event TmgrTraceEvent;
227 %nodefaultctor tmgr_trace_event;
228 struct tmgr_trace_event {
229 //tmgr_trace_t trace;
234 unsigned int getIdx() {return 0;}//$self->idx;}
238 %nodefaultctor Model;
243 virtual double shareResources(double now);
244 virtual double shareResourcesLazy(double now);
245 virtual double shareResourcesFull(double now);
247 virtual void updateActionsState(double now, double delta);
248 virtual void updateActionsStateLazy(double now, double delta);
249 virtual void updateActionsStateFull(double now, double delta);
251 virtual ActionList *getRunningActionSet();
253 virtual void addTraces()=0;
256 %feature("director") CpuModel;
257 class CpuModel : public Model {
261 virtual Cpu *createCpu(const char *name, DoubleDynar power_peak,
262 int pstate, double power_scale,
263 tmgr_trace *power_trace, int core,
264 e_surf_resource_state_t state_initial,
265 tmgr_trace *state_trace,
266 s_xbt_dict *cpu_properties)=0;
272 const char *getName();
273 virtual bool isUsed()=0;
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;
282 %feature("director") Cpu;
283 class Cpu : public Resource {
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);
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 setPstate(int pstate_index)=0;
299 virtual int getPstate()=0;
300 void setState(e_surf_resource_state_t state);
303 class Link : public Resource {
307 double getBandwidth();
308 void updateBandwidth(double value, double date=surf_get_clock());
310 void updateLatency(double value, double date=surf_get_clock());
313 %nodefaultctor Action;
316 Action(Model *model, double cost, bool failed);
319 lmm_variable *getVariable();
320 e_surf_action_state_t getState();
323 void setBound(double bound);
324 void updateRemains(double delta);
325 virtual double getRemains();
326 virtual void setPriority(double priority);
327 virtual void setState(e_surf_action_state_t state);
330 %nodefaultctor CpuAction;
331 %feature("director") CpuAction;
332 class CpuAction : public Action {
334 CpuAction(Model *model, double cost, bool failed);
336 Cpu *getCpu() {return getActionCpu($self);}
340 %nodefaultctor NetworkAction;
341 class NetworkAction : public Action {
344 double getLatency() {return $self->m_latency;}
349 %nodefaultctor RoutingEdge;
352 virtual char *getName()=0;
355 %rename lmm_constraint LmmConstraint;
356 struct lmm_constraint {
358 double getUsage() {return lmm_constraint_get_usage($self);}
362 %rename lmm_variable LmmVariable;
363 struct lmm_variable {
365 double getValue() {return lmm_variable_getvalue($self);}
369 %rename s_xbt_dict XbtDict;
372 char *getValue(char *key) {return (char*)xbt_dict_get_or_null($self, key);}
376 %rename e_surf_action_state_t ActionState;
378 SURF_ACTION_READY = 0, /**< Ready */
379 SURF_ACTION_RUNNING, /**< Running */
380 SURF_ACTION_FAILED, /**< Task Failure */
381 SURF_ACTION_DONE, /**< Completed */
382 SURF_ACTION_TO_FREE, /**< Action to free in next cleanup */
383 SURF_ACTION_NOT_IN_THE_SYSTEM
384 /**< Not in the system anymore. Why did you ask ? */
385 } e_surf_action_state_t;
387 %rename e_surf_resource_state_t ResourceState;
389 SURF_RESOURCE_ON = 1, /**< Up & ready */
390 SURF_RESOURCE_OFF = 0 /**< Down & broken */
391 } e_surf_resource_state_t;