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"
13 %pragma(java) jniclassimports=%{
14 import org.simgrid.NativeLib;
16 %pragma(java) jniclasscode=%{
18 NativeLib.nativeInit("surf-java");
19 Runtime.getRuntime().addShutdownHook(
22 Thread.currentThread().setName( "Destroyer" );
31 #include "src/surf/surf_interface.hpp"
32 #include "src/surf/cpu_interface.hpp"
33 #include "src/surf/network_interface.hpp"
34 #include "src/surf/trace_mgr_private.h"
35 #include "src/bindings/java/surf_swig.hpp"
36 #include "src/xbt/dict_private.h"
38 typedef struct lmm_constraint *lmm_constraint_t;
39 typedef xbt_dynar_t DoubleDynar;
47 JNIEXPORT jobject JNICALL Java_org_simgrid_surf_SurfJNI_getAction(JNIEnv *env, jclass cls, jlong jarg1) {
48 Action * action = (Action *)jarg1;
50 CpuAction *cpu_action = dynamic_cast<CpuAction*>(action);
52 SwigDirector_CpuAction *dir_cpu_action = dynamic_cast<SwigDirector_CpuAction*>(cpu_action);
54 res = dir_cpu_action->swig_get_self(env);\
56 jclass clss = env->FindClass("org/simgrid/surf/CpuAction");\
57 jmethodID constru = env->GetMethodID(clss, "<init>", "()V");\
58 res = env->NewObject(clss, constru);\
59 res = env->NewGlobalRef(res);\
62 jclass clss = env->FindClass("org/simgrid/surf/Action");\
63 jmethodID constru = env->GetMethodID(clss, "<init>", "()V");\
64 res = env->NewObject(clss, constru);\
65 res = env->NewGlobalRef(res);\
70 #define GETDIRECTOR(NAME) \
71 JNIEXPORT jobject JNICALL Java_org_simgrid_surf_SurfJNI_get## NAME ## Director(JNIEnv *env, jclass cls, jlong jarg1)\
73 NAME * arg1 = (NAME*)jarg1;\
74 SwigDirector_ ##NAME *director = dynamic_cast<SwigDirector_ ##NAME *>(arg1);\
77 res = director->swig_get_self(env);\
79 jclass clss = env->FindClass("org/simgrid/surf/NAME");\
80 jmethodID constru = env->GetMethodID(clss, "<init>", "()V");\
81 res = env->NewObject(clss, constru);\
82 res = env->NewGlobalRef(res);\
89 GETDIRECTOR(CpuAction)
97 %typemap(freearg) char* name {
100 /* Handle xbt_dynar_t of NetworkLink */
101 JAVA_ARRAYSOFCLASSES(Action);
102 %apply Action[] {ActionArrayPtr};
103 %typemap(jstype) ActionArrayPtr "Action[]"
104 %typemap(javain) ActionArrayPtr "Action.cArrayUnwrap($javainput)"
105 %typemap(javaout) ActionArrayPtr {
106 long[] cArray = $jnicall;
107 Action[] arrayWrapper = new Action[cArray.length];
108 for (int i=0; i<cArray.length; i++)
109 arrayWrapper[i] = (Action) Surf.getAction(cArray[i]);
111 // return Action.cArrayWrap($jnicall, $owner);
113 %typemap(out) ActionArrayPtr {
115 for(ActionList::iterator it($1->begin()), itend($1->end()); it != itend ; ++it) {
118 $result = jenv->NewLongArray(l);
119 jlong *elts = jenv->GetLongArrayElements($result, NULL);
121 for(ActionList::iterator it($1->begin()), itend($1->end()); it != itend ; ++it) {
122 elts[l++] = (jlong)static_cast<ActionPtr>(&*it);
124 jenv->ReleaseLongArrayElements($result, elts, 0);
129 //void push_front(Action &action);
130 //void push_back(Action &action);
132 ActionArrayPtr getArray(){
138 /* Handle xbt_dynar_t of NetworkLink */
139 JAVA_ARRAYSOFCLASSES(NetworkLink);
140 %apply NetworkLink[] {NetworkLinkDynar};
141 %typemap(jstype) NetworkLinkDynar "NetworkLink[]"
142 %typemap(javain) NetworkLinkDynar "NetworkLink.cArrayUnwrap($javainput)"
143 %typemap(javaout) NetworkLinkDynar {
144 return NetworkLink.cArrayWrap($jnicall, $owner);
146 %typemap(out) NetworkLinkDynar {
147 long l = xbt_dynar_length($1);
148 $result = jenv->NewLongArray(l);
151 jlong *elts = jenv->GetLongArrayElements($result, NULL);
152 xbt_dynar_foreach($1, i, link) {
153 elts[i] = (jlong)link;
155 jenv->ReleaseLongArrayElements($result, elts, 0);
159 %nodefault DoubleDynar;
160 %typemap(jni) DoubleDynar "jdoubleArray"
161 %rename(DoubleDynar) Double[];
162 %typemap(jtype) DoubleDynar "double[]"
163 %typemap(jstype) DoubleDynar "double[]"
164 %typemap(out) DoubleDynar {
165 long l = xbt_dynar_length($1);
166 $result = jenv->NewDoubleArray(l);
167 double *lout = (double *)xbt_dynar_to_array($1);
168 jenv->SetDoubleArrayRegion($result, 0, l, (const 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_to_array($1);
176 jenv->SetDoubleArrayRegion($input, 0, l, (const jdouble*)lout);
179 %typemap(javain) DoubleDynar "$javainput"
180 %typemap(javaout) DoubleDynar {return $jnicall}
182 /* Allow to subclass Plugin and send java object to C++ code */
183 %feature("director") Plugin;
185 %native(getAction) jobject getAction(jlong jarg1);
186 %native(getCpuModelDirector) jobject getCpuModelDirector(jlong jarg1);
187 %typemap(javaout) CpuModel * {
188 long cPtr = $jnicall;
189 return (CpuModel)Surf.getCpuModelDirector(cPtr);
191 %native(getCpuDirector) jobject getCpuDirector(jlong jarg1);
192 %typemap(javaout) Cpu * {
193 long cPtr = $jnicall;
194 return (Cpu)Surf.getCpuDirector(cPtr);
196 %native(getCpuActionDirector) jobject getCpuActionDirector(jlong jarg1);
197 %typemap(javaout) CpuAction * {
198 long cPtr = $jnicall;
199 return (CpuAction)Surf.getCpuDirector(cPtr);
202 %include "src/bindings/java/surf_swig.hpp"
204 %rename tmgr_trace TmgrTrace;
205 %nodefaultctor tmgr_trace;
207 //enum e_trace_type type;
210 xbt_dynar_t event_list;
213 probabilist_event_generator_t event_generator[2];
222 %rename tmgr_trace_event TmgrTraceEvent;
223 %nodefaultctor tmgr_trace_event;
224 struct tmgr_trace_event {
225 //tmgr_trace_t trace;
230 unsigned int getIdx() {return 0;}//$self->idx;}
234 %nodefaultctor Model;
237 Model(const char *name);
238 const char *getName();
239 virtual double shareResources(double now);
240 virtual double shareResourcesLazy(double now);
241 virtual double shareResourcesFull(double now);
243 virtual void updateActionsState(double now, double delta);
244 virtual void updateActionsStateLazy(double now, double delta);
245 virtual void updateActionsStateFull(double now, double delta);
247 virtual ActionList *getRunningActionSet();
249 Action *getRunningActionS() {
250 return (Action*)(&($self->getRunningActionSet()->front()));
255 %feature("director") CpuModel;
256 class CpuModel : public Model {
258 CpuModel(const char *name);
260 virtual Cpu *createResource(const char *name, DoubleDynar power_peak,
261 int pstate, double power_scale,
262 tmgr_trace *power_trace, int core,
263 e_surf_resource_state_t state_initial,
264 tmgr_trace *state_trace,
265 s_xbt_dict *cpu_properties)=0;
266 virtual void addTraces()=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 setPowerPeakAt(int pstate_index)=0;
299 void setState(e_surf_resource_state_t state);
302 class NetworkLink : public Resource {
306 double getBandwidth();
307 void updateBandwidth(double value, double date=surf_get_clock());
309 void updateLatency(double value, double date=surf_get_clock());
312 %nodefaultctor Action;
315 Action(Model *model, double cost, bool failed);
318 lmm_variable *getVariable();
319 e_surf_action_state_t getState();
322 void setBound(double bound);
323 void updateRemains(double delta);
324 virtual double getRemains();
325 virtual void setPriority(double priority);
326 virtual void setState(e_surf_action_state_t state);
329 %nodefaultctor CpuAction;
330 %feature("director") CpuAction;
331 class CpuAction : public Action {
333 CpuAction(Model *model, double cost, bool failed);
335 Cpu *getCpu() {return getActionCpu($self);}
339 %nodefaultctor NetworkAction;
340 class NetworkAction : public Action {
343 double getLatency() {return $self->m_latency;}
348 %nodefaultctor RoutingEdge;
351 virtual char *getName()=0;
354 %rename lmm_constraint LmmConstraint;
355 struct lmm_constraint {
357 double getUsage() {return lmm_constraint_get_usage($self);}
361 %rename lmm_variable LmmVariable;
362 struct lmm_variable {
364 double getValue() {return lmm_variable_getvalue($self);}
368 %rename s_xbt_dict XbtDict;
371 char *getValue(char *key) {return (char*)xbt_dict_get_or_null($self, key);}
375 %rename e_surf_action_state_t ActionState;
377 SURF_ACTION_READY = 0, /**< Ready */
378 SURF_ACTION_RUNNING, /**< Running */
379 SURF_ACTION_FAILED, /**< Task Failure */
380 SURF_ACTION_DONE, /**< Completed */
381 SURF_ACTION_TO_FREE, /**< Action to free in next cleanup */
382 SURF_ACTION_NOT_IN_THE_SYSTEM
383 /**< Not in the system anymore. Why did you ask ? */
384 } e_surf_action_state_t;
386 %rename e_surf_resource_state_t ResourceState;
388 SURF_RESOURCE_ON = 1, /**< Up & ready */
389 SURF_RESOURCE_OFF = 0 /**< Down & broken */
390 } e_surf_resource_state_t;