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=%{
20 NativeLib.nativeInit("winpthread");
21 } catch (Exception e) {}
22 NativeLib.nativeInit("simgrid");
23 NativeLib.nativeInit("surf-java");
24 Runtime.getRuntime().addShutdownHook(
27 Thread.currentThread().setName( "Destroyer" );
36 #include "src/surf/surf_interface.hpp"
37 #include "src/surf/cpu_interface.hpp"
38 #include "src/surf/network_interface.hpp"
39 #include "src/surf/trace_mgr_private.h"
40 #include "src/bindings/java/surf_swig.hpp"
41 #include "src/xbt/dict_private.h"
43 typedef struct lmm_constraint *lmm_constraint_t;
44 typedef xbt_dynar_t DoubleDynar;
52 JNIEXPORT jobject JNICALL Java_org_simgrid_surf_SurfJNI_getAction(JNIEnv *env, jclass cls, jlong jarg1) {
53 Action * action = (Action *)jarg1;
55 CpuAction *cpu_action = dynamic_cast<CpuAction*>(action);
57 SwigDirector_CpuAction *dir_cpu_action = dynamic_cast<SwigDirector_CpuAction*>(cpu_action);
59 res = dir_cpu_action->swig_get_self(env);\
61 jclass clss = env->FindClass("org/simgrid/surf/CpuAction");\
62 jmethodID constru = env->GetMethodID(clss, "<init>", "()V");\
63 res = env->NewObject(clss, constru);\
64 res = env->NewGlobalRef(res);\
67 jclass clss = env->FindClass("org/simgrid/surf/Action");\
68 jmethodID constru = env->GetMethodID(clss, "<init>", "()V");\
69 res = env->NewObject(clss, constru);\
70 res = env->NewGlobalRef(res);\
75 #define GETDIRECTOR(NAME) \
76 JNIEXPORT jobject JNICALL Java_org_simgrid_surf_SurfJNI_get## NAME ## Director(JNIEnv *env, jclass cls, jlong jarg1)\
78 NAME * arg1 = (NAME*)jarg1;\
79 SwigDirector_ ##NAME *director = dynamic_cast<SwigDirector_ ##NAME *>(arg1);\
82 res = director->swig_get_self(env);\
84 jclass clss = env->FindClass("org/simgrid/surf/NAME");\
85 jmethodID constru = env->GetMethodID(clss, "<init>", "()V");\
86 res = env->NewObject(clss, constru);\
87 res = env->NewGlobalRef(res);\
94 GETDIRECTOR(CpuAction)
102 %typemap(freearg) char* name {
105 /* Handle xbt_dynar_t of Link */
106 JAVA_ARRAYSOFCLASSES(Action);
107 %apply Action[] {ActionArrayPtr};
108 %typemap(jstype) ActionArrayPtr "Action[]"
109 %typemap(javain) ActionArrayPtr "Action.cArrayUnwrap($javainput)"
110 %typemap(javaout) ActionArrayPtr {
111 long[] cArray = $jnicall;
112 Action[] arrayWrapper = new Action[cArray.length];
113 for (int i=0; i<cArray.length; i++)
114 arrayWrapper[i] = (Action) Surf.getAction(cArray[i]);
116 // return Action.cArrayWrap($jnicall, $owner);
118 %typemap(out) ActionArrayPtr {
120 for(ActionList::iterator it($1->begin()), itend($1->end()); it != itend ; ++it) {
123 $result = jenv->NewLongArray(l);
124 jlong *elts = jenv->GetLongArrayElements($result, NULL);
126 for(ActionList::iterator it($1->begin()), itend($1->end()); it != itend ; ++it) {
127 elts[l++] = (jlong)static_cast<Action*>(&*it);
129 jenv->ReleaseLongArrayElements($result, elts, 0);
134 //void push_front(Action &action);
135 //void push_back(Action &action);
137 ActionArrayPtr getArray(){
143 /* Handle xbt_dynar_t of Link */
144 JAVA_ARRAYSOFCLASSES(Link);
145 %apply Link[] {LinkDynar};
146 %typemap(jstype) LinkDynar "Link[]"
147 %typemap(javain) LinkDynar "Link.cArrayUnwrap($javainput)"
148 %typemap(javaout) LinkDynar {
149 return Link.cArrayWrap($jnicall, $owner);
151 %typemap(out) LinkDynar {
152 long l = xbt_dynar_length($1);
153 $result = jenv->NewLongArray(l);
156 jlong *elts = jenv->GetLongArrayElements($result, NULL);
157 xbt_dynar_foreach($1, i, link) {
158 elts[i] = (jlong)link;
160 jenv->ReleaseLongArrayElements($result, elts, 0);
164 %nodefault DoubleDynar;
165 %typemap(jni) DoubleDynar "jdoubleArray"
166 %rename(DoubleDynar) Double[];
167 %typemap(jtype) DoubleDynar "double[]"
168 %typemap(jstype) DoubleDynar "double[]"
169 %typemap(out) DoubleDynar {
170 long l = xbt_dynar_length($1);
171 $result = jenv->NewDoubleArray(l);
172 double *lout = (double *)xbt_dynar_to_array($1);
173 jenv->SetDoubleArrayRegion($result, 0, l, (jdouble*)lout);
176 %typemap(javadirectorin) DoubleDynar "$jniinput"
177 %typemap(directorin,descriptor="[D") DoubleDynar %{
178 long l = xbt_dynar_length($1);
179 $input = jenv->NewDoubleArray(l);
180 double *lout = (double *)xbt_dynar_to_array($1);
181 jenv->SetDoubleArrayRegion($input, 0, l, (jdouble*)lout);
184 %typemap(javain) DoubleDynar "$javainput"
185 %typemap(javaout) DoubleDynar {return $jnicall}
187 /* Allow to subclass Plugin and send java object to C++ code */
188 %feature("director") Plugin;
190 %native(getAction) jobject getAction(jlong jarg1);
191 %native(getCpuModelDirector) jobject getCpuModelDirector(jlong jarg1);
192 %typemap(javaout) CpuModel * {
193 long cPtr = $jnicall;
194 return (CpuModel)Surf.getCpuModelDirector(cPtr);
196 %native(getCpuDirector) jobject getCpuDirector(jlong jarg1);
197 %typemap(javaout) Cpu * {
198 long cPtr = $jnicall;
199 return (Cpu)Surf.getCpuDirector(cPtr);
201 %native(getCpuActionDirector) jobject getCpuActionDirector(jlong jarg1);
202 %typemap(javaout) CpuAction * {
203 long cPtr = $jnicall;
204 return (CpuAction)Surf.getCpuDirector(cPtr);
207 %include "src/bindings/java/surf_swig.hpp"
209 %rename tmgr_trace TmgrTrace;
210 %nodefaultctor tmgr_trace;
212 //enum e_trace_type type;
215 xbt_dynar_t event_list;
218 probabilist_event_generator_t event_generator[2];
227 %rename tmgr_trace_event TmgrTraceEvent;
228 %nodefaultctor tmgr_trace_event;
229 struct tmgr_trace_event {
230 //tmgr_trace_t trace;
235 unsigned int getIdx() {return 0;}//$self->idx;}
239 %nodefaultctor Model;
244 virtual double shareResources(double now);
245 virtual double shareResourcesLazy(double now);
246 virtual double shareResourcesFull(double now);
248 virtual void updateActionsState(double now, double delta);
249 virtual void updateActionsStateLazy(double now, double delta);
250 virtual void updateActionsStateFull(double now, double delta);
252 virtual ActionList *getRunningActionSet();
254 virtual void addTraces()=0;
257 %feature("director") CpuModel;
258 class CpuModel : public Model {
262 virtual Cpu *createCpu(const char *name, DoubleDynar power_peak,
263 int pstate, double power_scale,
264 tmgr_trace *power_trace, int core,
265 e_surf_resource_state_t state_initial,
266 tmgr_trace *state_trace,
267 s_xbt_dict *cpu_properties)=0;
273 const char *getName();
274 virtual bool isUsed()=0;
277 virtual e_surf_resource_state_t getState();
278 lmm_constraint *getConstraint();
279 s_xbt_dict *getProperties();
280 virtual void updateState(tmgr_trace_event *event_type, double value, double date)=0;
283 %feature("director") Cpu;
284 class Cpu : public Resource {
286 Cpu(Model *model, const char *name, s_xbt_dict *props,
287 lmm_constraint *constraint, int core, double powerPeak, double powerScale);
288 Cpu(Model *model, const char *name, s_xbt_dict *props,
289 int core, double powerPeak, double powerScale);
291 virtual double getCurrentPowerPeak();
292 virtual CpuAction *execute(double size)=0;
293 virtual CpuAction *sleep(double duration)=0;
294 virtual int getCore();
295 virtual double getSpeed(double load);
296 virtual double getAvailableSpeed();
297 virtual double getPowerPeakAt(int pstate_index)=0;
298 virtual int getNbPstates()=0;
299 virtual void setPstate(int pstate_index)=0;
300 virtual int getPstate()=0;
301 void setState(e_surf_resource_state_t state);
304 class Link : public Resource {
308 double getBandwidth();
309 void updateBandwidth(double value, double date=surf_get_clock());
311 void updateLatency(double value, double date=surf_get_clock());
314 %nodefaultctor Action;
317 Action(Model *model, double cost, bool failed);
320 lmm_variable *getVariable();
321 e_surf_action_state_t getState();
324 void setBound(double bound);
325 void updateRemains(double delta);
326 virtual double getRemains();
327 virtual void setPriority(double priority);
328 virtual void setState(e_surf_action_state_t state);
331 %nodefaultctor CpuAction;
332 %feature("director") CpuAction;
333 class CpuAction : public Action {
335 CpuAction(Model *model, double cost, bool failed);
337 Cpu *getCpu() {return getActionCpu($self);}
341 %nodefaultctor NetworkAction;
342 class NetworkAction : public Action {
345 double getLatency() {return $self->m_latency;}
350 %nodefaultctor RoutingEdge;
353 virtual char *getName()=0;
356 %rename lmm_constraint LmmConstraint;
357 struct lmm_constraint {
359 double getUsage() {return lmm_constraint_get_usage($self);}
363 %rename lmm_variable LmmVariable;
364 struct lmm_variable {
366 double getValue() {return lmm_variable_getvalue($self);}
370 %rename s_xbt_dict XbtDict;
373 char *getValue(char *key) {return (char*)xbt_dict_get_or_null($self, key);}
377 %rename e_surf_action_state_t ActionState;
379 SURF_ACTION_READY = 0, /**< Ready */
380 SURF_ACTION_RUNNING, /**< Running */
381 SURF_ACTION_FAILED, /**< Task Failure */
382 SURF_ACTION_DONE, /**< Completed */
383 SURF_ACTION_TO_FREE, /**< Action to free in next cleanup */
384 SURF_ACTION_NOT_IN_THE_SYSTEM
385 /**< Not in the system anymore. Why did you ask ? */
386 } e_surf_action_state_t;
388 %rename e_surf_resource_state_t ResourceState;
390 SURF_RESOURCE_ON = 1, /**< Up & ready */
391 SURF_RESOURCE_OFF = 0 /**< Down & broken */
392 } e_surf_resource_state_t;