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("surf-java");
20 Runtime.getRuntime().addShutdownHook(
23 Thread.currentThread().setName( "Destroyer" );
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"
39 typedef struct lmm_constraint *lmm_constraint_t;
40 typedef xbt_dynar_t DoubleDynar;
48 JNIEXPORT jobject JNICALL Java_org_simgrid_surf_SurfJNI_getAction(JNIEnv *env, jclass cls, jlong jarg1) {
49 Action * action = (Action *)jarg1;
51 CpuAction *cpu_action = dynamic_cast<CpuAction*>(action);
53 SwigDirector_CpuAction *dir_cpu_action = dynamic_cast<SwigDirector_CpuAction*>(cpu_action);
55 res = dir_cpu_action->swig_get_self(env);\
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);\
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);\
71 #define GETDIRECTOR(NAME) \
72 JNIEXPORT jobject JNICALL Java_org_simgrid_surf_SurfJNI_get## NAME ## Director(JNIEnv *env, jclass cls, jlong jarg1)\
74 NAME * arg1 = (NAME*)jarg1;\
75 SwigDirector_ ##NAME *director = dynamic_cast<SwigDirector_ ##NAME *>(arg1);\
78 res = director->swig_get_self(env);\
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);\
90 GETDIRECTOR(CpuAction)
98 %typemap(freearg) char* name {
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]);
112 // return Action.cArrayWrap($jnicall, $owner);
114 %typemap(out) ActionArrayPtr {
116 for(ActionList::iterator it($1->begin()), itend($1->end()); it != itend ; ++it) {
119 $result = jenv->NewLongArray(l);
120 jlong *elts = jenv->GetLongArrayElements($result, NULL);
122 for(ActionList::iterator it($1->begin()), itend($1->end()); it != itend ; ++it) {
123 elts[l++] = (jlong)static_cast<ActionPtr>(&*it);
125 jenv->ReleaseLongArrayElements($result, elts, 0);
130 //void push_front(Action &action);
131 //void push_back(Action &action);
133 ActionArrayPtr getArray(){
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);
147 %typemap(out) NetworkLinkDynar {
148 long l = xbt_dynar_length($1);
149 $result = jenv->NewLongArray(l);
152 jlong *elts = jenv->GetLongArrayElements($result, NULL);
153 xbt_dynar_foreach($1, i, link) {
154 elts[i] = (jlong)link;
156 jenv->ReleaseLongArrayElements($result, elts, 0);
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);
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);
180 %typemap(javain) DoubleDynar "$javainput"
181 %typemap(javaout) DoubleDynar {return $jnicall}
183 /* Allow to subclass Plugin and send java object to C++ code */
184 %feature("director") Plugin;
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);
192 %native(getCpuDirector) jobject getCpuDirector(jlong jarg1);
193 %typemap(javaout) Cpu * {
194 long cPtr = $jnicall;
195 return (Cpu)Surf.getCpuDirector(cPtr);
197 %native(getCpuActionDirector) jobject getCpuActionDirector(jlong jarg1);
198 %typemap(javaout) CpuAction * {
199 long cPtr = $jnicall;
200 return (CpuAction)Surf.getCpuDirector(cPtr);
203 %include "src/bindings/java/surf_swig.hpp"
205 %rename tmgr_trace TmgrTrace;
206 %nodefaultctor tmgr_trace;
208 //enum e_trace_type type;
211 xbt_dynar_t event_list;
214 probabilist_event_generator_t event_generator[2];
223 %rename tmgr_trace_event TmgrTraceEvent;
224 %nodefaultctor tmgr_trace_event;
225 struct tmgr_trace_event {
226 //tmgr_trace_t trace;
231 unsigned int getIdx() {return 0;}//$self->idx;}
235 %nodefaultctor Model;
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);
244 virtual void updateActionsState(double now, double delta);
245 virtual void updateActionsStateLazy(double now, double delta);
246 virtual void updateActionsStateFull(double now, double delta);
248 virtual ActionList *getRunningActionSet();
251 %feature("director") CpuModel;
252 class CpuModel : public Model {
254 CpuModel(const char *name);
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;
268 const char *getName();
269 virtual bool isUsed()=0;
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;
278 %feature("director") Cpu;
279 class Cpu : public Resource {
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);
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);
298 class NetworkLink : public Resource {
302 double getBandwidth();
303 void updateBandwidth(double value, double date=surf_get_clock());
305 void updateLatency(double value, double date=surf_get_clock());
308 %nodefaultctor Action;
311 Action(Model *model, double cost, bool failed);
314 lmm_variable *getVariable();
315 e_surf_action_state_t getState();
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);
325 %nodefaultctor CpuAction;
326 %feature("director") CpuAction;
327 class CpuAction : public Action {
329 CpuAction(Model *model, double cost, bool failed);
331 Cpu *getCpu() {return getActionCpu($self);}
335 %nodefaultctor NetworkAction;
336 class NetworkAction : public Action {
339 double getLatency() {return $self->m_latency;}
344 %nodefaultctor RoutingEdge;
347 virtual char *getName()=0;
350 %rename lmm_constraint LmmConstraint;
351 struct lmm_constraint {
353 double getUsage() {return lmm_constraint_get_usage($self);}
357 %rename lmm_variable LmmVariable;
358 struct lmm_variable {
360 double getValue() {return lmm_variable_getvalue($self);}
364 %rename s_xbt_dict XbtDict;
367 char *getValue(char *key) {return (char*)xbt_dict_get_or_null($self, key);}
371 %rename e_surf_action_state_t ActionState;
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;
382 %rename e_surf_resource_state_t ResourceState;
384 SURF_RESOURCE_ON = 1, /**< Up & ready */
385 SURF_RESOURCE_OFF = 0 /**< Down & broken */
386 } e_surf_resource_state_t;