Skip to content

Commit

Permalink
Small changes
Browse files Browse the repository at this point in the history
POM dependency update
Scheduler getTime() method
  • Loading branch information
Michał Kit committed Aug 14, 2013
1 parent ebfb3e8 commit bd53e12
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 38 deletions.
14 changes: 14 additions & 0 deletions jdeeco-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<lib>${dist}/lib</lib>
<bcelJar>bcel.jar</bcelJar>
<clonerJar>cloner.jar</clonerJar>
<objenesisJar>objenesis.jar</objenesisJar>
</properties>
<build>
<sourceDirectory>src</sourceDirectory>
Expand Down Expand Up @@ -69,6 +70,13 @@
<destFileName>${clonerJar}</destFileName>
<outputDirectory>${lib}</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>org.objenesis</groupId>
<artifactId>objenesis</artifactId>
<version>1.2</version>
<destFileName>${objenesisJar}</destFileName>
<outputDirectory>${lib}</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
Expand Down Expand Up @@ -137,6 +145,12 @@
<version>1.9.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.objenesis</groupId>
<artifactId>objenesis</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>cz.cuni.mff.d3s</groupId>
<artifactId>cz.cuni.mff.d3s.jini</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ private Object getKnowledge(boolean withdrawal, String knowledgePath,
localSession.cancel();
throw new KMAccessException(kre.getMessage());
} catch (Exception e) {
Log.e("", e);
Log.e(knowledgePath, e);
localSession.cancel();
return null;
}
Expand Down Expand Up @@ -350,9 +350,11 @@ private Object[] putStructure(String knowledgePath, Object value,
&& !Arrays.deepEquals(oldStructure, newStructure)) {
kr.take(structurePath, session);
String tempPath;
List<?> nsList = Arrays.asList(newStructure);
List<?> nsList = null;
if (newStructure != null)
nsList = Arrays.asList(newStructure);
for (Object s : oldStructure) {
if (nsList.contains(s))
if (nsList != null && nsList.contains(s))
continue;
tempPath = KnowledgePathHelper.appendToRoot(
knowledgePath, (String) s);
Expand All @@ -364,13 +366,13 @@ private Object[] putStructure(String knowledgePath, Object value,
"Knowledge repository error!");
}
}
if (store)
if (store && newStructure != null)
kr.put(structurePath, newStructure, session);
}
} else if (store)
} else if (store && newStructure != null)
kr.put(structurePath, newStructure, session);
} catch (KRExceptionUnavailableEntry uee) {
if (store)
if (store && newStructure != null)
kr.put(structurePath, newStructure, session);
}
return newStructure;
Expand Down
7 changes: 7 additions & 0 deletions jdeeco-core/src/cz/cuni/mff/d3s/deeco/runtime/IRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ public interface IRuntime extends IEnsembleComponentInformer {
*/
public void registerComponentsAndEnsembles(
DEECoObjectProvider provider);

/**
* Returns current time.
*
* @return
*/
public long getTime();

/**
* Checks whether the runtime is currently scheduling registered processes.
Expand Down
5 changes: 5 additions & 0 deletions jdeeco-core/src/cz/cuni/mff/d3s/deeco/runtime/Runtime.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ public Runtime(KnowledgeManager km, IScheduler scheduler, boolean useMXBeans) {
this.km.setRuntime(this);
this.scheduler = scheduler;
}

@Override
public long getTime() {
return scheduler.getTime();
}

/*
* (non-Javadoc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public interface IScheduler {

public List<SchedulableProcess> getPeriodicProcesses();

public long getTime();

public void clearAll();

public void start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public class MultithreadedScheduler extends Scheduler {
public MultithreadedScheduler() {
super();
}

@Override
public long getTime() {
return System.currentTimeMillis();
}

@Override
public synchronized void start() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ public void run() {
discreteSchedulerThread.start();
}
}

@Override
public long getTime() {
return virtualTime;
}

@Override
public void stop() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
/**
* Special scheduler for testing via JPF.
*
* Each task has a separate thread and is executed repeatedly.
* Timing is ignored (due to GC - see comments in the code).
* We limit the number of executed periods by 2*P_max, where P_max is the longest period.
* Each task has a separate thread and is executed repeatedly. Timing is ignored
* (due to GC - see comments in the code). We limit the number of executed
* periods by 2*P_max, where P_max is the longest period.
*/
public class MultithreadedSchedulerJPF extends Scheduler {

Expand All @@ -29,19 +29,24 @@ public class MultithreadedSchedulerJPF extends Scheduler {
long lastThreadID = -1;
int lastConsecutiveRuns = 0;

@Override
public long getTime() {
return System.currentTimeMillis();
}


public MultithreadedSchedulerJPF() {
this(0,0);
this(0, 0);
}

public MultithreadedSchedulerJPF(int maxHyperPeriodIterations, int maxConsecutiveThreadRuns) {

public MultithreadedSchedulerJPF(int maxHyperPeriodIterations,
int maxConsecutiveThreadRuns) {
super();
this.threads = new HashSet<Thread>();
this.maxHyperPeriodIterations = maxHyperPeriodIterations;
this.maxConsecutiveThreadRuns = maxConsecutiveThreadRuns;

// JPF optimization -> earlier class loading via a clinit call (in the single threaded part)
// JPF optimization -> earlier class loading via a clinit call (in the
// single threaded part)
// reduces the state space
@SuppressWarnings("unused")
ETriggerType e = ETriggerType.COORDINATOR;
Expand All @@ -50,36 +55,43 @@ public MultithreadedSchedulerJPF(int maxHyperPeriodIterations, int maxConsecutiv
@Override
public synchronized void start() {

// get the longest period (P_max)
// get the longest period (P_max)
long maxPeriod = 0;
for (SchedulableProcess sp : periodicProcesses) {
long spPeriod = ((ProcessPeriodicSchedule) sp.scheduling).interval;
if (spPeriod > maxPeriod) maxPeriod = spPeriod;
if (spPeriod > maxPeriod)
maxPeriod = spPeriod;
}

//System.out.println("[DEBUG] max period = " + maxPeriod);
// System.out.println("[DEBUG] max period = " + maxPeriod);

if (!running) {
// let every process run for the number of times its period P fits into maxPeriodIterations*P_max
// let every process run for the number of times its period P fits
// into maxPeriodIterations*P_max
for (SchedulableProcess sp : periodicProcesses) {
//long spPeriod = ((ProcessPeriodicSchedule) sp.scheduling).interval;
long repeatCount = maxHyperPeriodIterations;//(maxHyperPeriodIterations*maxPeriod) / spPeriod + 1;
// long spPeriod = ((ProcessPeriodicSchedule)
// sp.scheduling).interval;
long repeatCount = maxHyperPeriodIterations;// (maxHyperPeriodIterations*maxPeriod)
// / spPeriod + 1;

//System.out.println("[DEBUG] period = " + spPeriod + ", repeat count = " + repeatCount);
// System.out.println("[DEBUG] period = " + spPeriod +
// ", repeat count = " + repeatCount);

startPeriodicProcess(sp, repeatCount);
}
List<KnowledgeManager> kms = new LinkedList<KnowledgeManager>();
for (TriggeredSchedulableProcess tsp : triggeredProcesses) {
tsp.registerListener();
if (!kms.contains(tsp.getKnowledgeManager())) kms.add(tsp.getKnowledgeManager());
if (!kms.contains(tsp.getKnowledgeManager()))
kms.add(tsp.getKnowledgeManager());
}
for (KnowledgeManager km : kms) {
km.setListenersActive(true);
}
running = true;

Thread.yield(); // break the transition so that (periodic) processes can be scheduled and executed.
Thread.yield(); // break the transition so that (periodic) processes
// can be scheduled and executed.
}
}

Expand All @@ -91,22 +103,25 @@ public synchronized void stop() {
}
List<KnowledgeManager> kms = new LinkedList<KnowledgeManager>();
for (TriggeredSchedulableProcess tsp : triggeredProcesses) {
if (!kms.contains(tsp.getKnowledgeManager())) kms.add(tsp.getKnowledgeManager());
if (!kms.contains(tsp.getKnowledgeManager()))
kms.add(tsp.getKnowledgeManager());
}
for (KnowledgeManager km : kms) {
km.setListenersActive(false);
}
running = false;
}
}

@Override
protected void scheduleProcessForExecution(SchedulableProcess process) {
startPeriodicProcess(process, 0);
}

private void startPeriodicProcess(SchedulableProcess process, long repeatCount) {
// note that period is intentionally ignored because we just need to capture relevant thread interleavings
private void startPeriodicProcess(SchedulableProcess process,
long repeatCount) {
// note that period is intentionally ignored because we just need to
// capture relevant thread interleavings
Thread t = new Thread(new PeriodicProcessRunner(process, repeatCount));
threads.add(t);
t.start();
Expand All @@ -125,29 +140,32 @@ public PeriodicProcessRunner(SchedulableProcess process, long rc) {
@Override
public void run() {
long count = 0;
while ( repeatCount == 0 || ((count < repeatCount) && (!Thread.interrupted()))) {
while (repeatCount == 0
|| ((count < repeatCount) && (!Thread.interrupted()))) {

long curThID = Thread.currentThread().getId();

if (curThID != lastThreadID)
{
if (curThID != lastThreadID) {
lastThreadID = curThID;
lastConsecutiveRuns = 1;
}
else
{
// TODO: update it so that there is some time budget for a thread to
// execute and all threads have to spent all the potential of their
} else {
// TODO: update it so that there is some time budget for a
// thread to
// execute and all threads have to spent all the potential
// of their
// budget before scheduling threads again
// so that we don't have situations like 1,2,1,2,1,2,1,2, where 3 does not get scheduled at all
// so that we don't have situations like 1,2,1,2,1,2,1,2,
// where 3 does not get scheduled at all
lastConsecutiveRuns++;
if (lastConsecutiveRuns > maxConsecutiveThreadRuns) Verify.ignoreIf(true);
if (lastConsecutiveRuns > maxConsecutiveThreadRuns)
Verify.ignoreIf(true);
}

try {
process.invoke();
} catch (Exception e) {
System.out.println("Process scheduled exception: " + e.getMessage());
System.out.println("Process scheduled exception: "
+ e.getMessage());
e.printStackTrace();
}

Expand Down

0 comments on commit bd53e12

Please sign in to comment.