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 org.simgrid.NativeLib.nativeInit();
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 Link */
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<Action*>(&*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 Link */
140 JAVA_ARRAYSOFCLASSES(Link);
141 %apply Link[] {LinkDynar};
142 %typemap(jstype) LinkDynar "Link[]"
143 %typemap(javain) LinkDynar "Link.cArrayUnwrap($javainput)"
144 %typemap(javaout) LinkDynar {
145 return Link.cArrayWrap($jnicall, $owner);
147 %typemap(out) LinkDynar {
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_get_ptr($1, 0);
169 jenv->SetDoubleArrayRegion($result, 0, l, (jdouble*)lout);
171 %typemap(javadirectorin) DoubleDynar "$jniinput"
172 %typemap(directorin,descriptor="[D") DoubleDynar %{
173 long l = xbt_dynar_length($1);
174 $input = jenv->NewDoubleArray(l);
175 double *lout = (double *) xbt_dynar_get_ptr($1, 0);
176 jenv->SetDoubleArrayRegion($input, 0, l, (jdouble*)lout);
178 %typemap(javain) DoubleDynar "$javainput"
179 %typemap(javaout) DoubleDynar {return $jnicall}
181 /* Allow to subclass Plugin and send java object to C++ code */
182 %feature("director") Plugin;
184 %native(getAction) jobject getAction(jlong jarg1);
185 %native(getCpuModelDirector) jobject getCpuModelDirector(jlong jarg1);
186 %typemap(javaout) CpuModel * {
187 long cPtr = $jnicall;
188 return (CpuModel)Surf.getCpuModelDirector(cPtr);
190 %native(getCpuDirector) jobject getCpuDirector(jlong jarg1);
191 %typemap(javaout) Cpu * {
192 long cPtr = $jnicall;
193 return (Cpu)Surf.getCpuDirector(cPtr);
195 %native(getCpuActionDirector) jobject getCpuActionDirector(jlong jarg1);
196 %typemap(javaout) CpuAction * {
197 long cPtr = $jnicall;
198 return (CpuAction)Surf.getCpuDirector(cPtr);
201 %include "src/bindings/java/surf_swig.hpp"
203 %rename tmgr_trace TmgrTrace;
204 %nodefaultctor tmgr_trace;
206 //enum e_trace_type type;
209 xbt_dynar_t event_list;
212 probabilist_event_generator_t event_generator[2];
221 %rename tmgr_trace_event TmgrTraceEvent;
222 %nodefaultctor tmgr_trace_event;
223 struct tmgr_trace_event {
224 //tmgr_trace_t trace;
229 unsigned int getIdx() {return 0;}//$self->idx;}
233 %nodefaultctor Model;
238 virtual double shareResources(double now);
239 virtual double shareResourcesLazy(double now);
240 virtual double shareResourcesFull(double now);
242 virtual void updateActionsState(double now, double delta);
243 virtual void updateActionsStateLazy(double now, double delta);
244 virtual void updateActionsStateFull(double now, double delta);
246 virtual ActionList *getRunningActionSet();
248 virtual void addTraces()=0;
251 %feature("director") CpuModel;
252 class CpuModel : public Model {
256 virtual Cpu *createCpu(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;
267 const char *getName();
268 virtual bool isUsed()=0;
271 virtual e_surf_resource_state_t getState();
272 lmm_constraint *getConstraint();
273 s_xbt_dict *getProperties();
274 virtual void updateState(tmgr_trace_event *event_type, double value, double date)=0;
277 %feature("director") Cpu;
278 class Cpu : public Resource {
280 Cpu(Model *model, const char *name, s_xbt_dict *props,
281 lmm_constraint *constraint, int core, double powerPeak, double powerScale);
282 Cpu(Model *model, const char *name, s_xbt_dict *props,
283 int core, double powerPeak, double powerScale);
285 virtual double getCurrentPowerPeak();
286 virtual CpuAction *execute(double size)=0;
287 virtual CpuAction *sleep(double duration)=0;
288 virtual int getCore();
289 virtual double getSpeed(double load);
290 virtual double getAvailableSpeed();
291 virtual double getPowerPeakAt(int pstate_index)=0;
292 virtual int getNbPstates()=0;
293 virtual void setPstate(int pstate_index)=0;
294 virtual int getPstate()=0;
295 void setState(e_surf_resource_state_t state);
298 class Link : 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;