From 122dc53aa8adc4f2a3bb800056396734e80d27b4 Mon Sep 17 00:00:00 2001 From: Aman Goel Date: Thu, 13 Jun 2024 18:50:37 +0000 Subject: [PATCH 01/31] [PEx] Change schedule choice from PMachine to PMachineId --- .../pexplicit/runtime/PExplicitGlobal.java | 12 ++++----- .../runtime/logger/PExplicitLogger.java | 3 ++- .../pexplicit/runtime/machine/PMachine.java | 4 +-- .../pexplicit/runtime/machine/PMachineId.java | 14 +++++++++++ .../pexplicit/runtime/scheduler/Schedule.java | 7 +++--- .../runtime/scheduler/Scheduler.java | 5 ++-- .../scheduler/choice/ScheduleChoice.java | 6 ++--- .../explicit/ExplicitSearchScheduler.java | 23 +++++++++-------- .../runtime/scheduler/explicit/StepState.java | 25 ------------------- .../scheduler/replay/ReplayScheduler.java | 6 +++-- 10 files changed, 51 insertions(+), 54 deletions(-) create mode 100644 Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/PMachineId.java diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java index f30f2c005..d6e71fe60 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java @@ -4,6 +4,7 @@ import lombok.Setter; import pexplicit.commandline.PExplicitConfig; import pexplicit.runtime.machine.PMachine; +import pexplicit.runtime.machine.PMachineId; import pexplicit.runtime.scheduler.Scheduler; import pexplicit.runtime.scheduler.explicit.strategy.SearchStrategyMode; @@ -60,19 +61,18 @@ public class PExplicitGlobal { /** * Get a machine of a given type and index if exists, else return null. * - * @param type Machine type - * @param idx Machine index + * @param pid Machine pid * @return Machine */ - public static PMachine getGlobalMachine(Class type, int idx) { - List machinesOfType = machineListByType.get(type); + public static PMachine getGlobalMachine(PMachineId pid) { + List machinesOfType = machineListByType.get(pid.getType()); if (machinesOfType == null) { return null; } - if (idx >= machinesOfType.size()) { + if (pid.getTypeId() >= machinesOfType.size()) { return null; } - PMachine result = machineListByType.get(type).get(idx); + PMachine result = machineListByType.get(pid.getType()).get(pid.getTypeId()); assert (machineSet.contains(result)); return result; } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java index a5137ab72..7782909d6 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java @@ -9,6 +9,7 @@ import pexplicit.runtime.PExplicitGlobal; import pexplicit.runtime.STATUS; import pexplicit.runtime.machine.PMachine; +import pexplicit.runtime.machine.PMachineId; import pexplicit.runtime.machine.PMonitor; import pexplicit.runtime.machine.State; import pexplicit.runtime.machine.events.PContinuation; @@ -217,7 +218,7 @@ public static void logBacktrack(Choice choice) { } } - public static void logNewScheduleChoice(List choices, int step, int idx) { + public static void logNewScheduleChoice(List choices, int step, int idx) { if (verbosity > 1) { log.info(String.format(" @%d::%d new schedule choice: %s", step, idx, choices)); } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/PMachine.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/PMachine.java index 7b1b25b67..0263f5d2e 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/PMachine.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/PMachine.java @@ -28,7 +28,7 @@ public abstract class PMachine implements Serializable, Comparable { private static final Map nameToMachine = new HashMap<>(); protected static int globalMachineId = 1; @Getter - protected final int typeId; + protected final PMachineId pid; @Getter protected final String name; private final Set states; @@ -72,7 +72,7 @@ public PMachine(String name, int id, State startState, State... states) { // initialize name, ids this.name = name; this.instanceId = ++globalMachineId; - this.typeId = id; + this.pid = new PMachineId(this.getClass(), id); nameToMachine.put(toString(), this); // initialize states diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/PMachineId.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/PMachineId.java new file mode 100644 index 000000000..569da384e --- /dev/null +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/PMachineId.java @@ -0,0 +1,14 @@ +package pexplicit.runtime.machine; + +import lombok.Getter; + +@Getter +public class PMachineId { + Class type; + int typeId; + + public PMachineId(Class t, int tid) { + type = t; + typeId = tid; + } +} diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Schedule.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Schedule.java index 93e191093..1b4cf8633 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Schedule.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Schedule.java @@ -4,6 +4,7 @@ import lombok.Setter; import pexplicit.runtime.PExplicitGlobal; import pexplicit.runtime.machine.PMachine; +import pexplicit.runtime.machine.PMachineId; import pexplicit.runtime.scheduler.choice.Choice; import pexplicit.runtime.scheduler.choice.DataChoice; import pexplicit.runtime.scheduler.choice.ScheduleChoice; @@ -103,7 +104,7 @@ public int getNumUnexploredDataChoices() { * @param current Machine to set as current schedule choice * @param unexplored List of machine to set as unexplored schedule choices */ - public void setScheduleChoice(int stepNum, int choiceNum, PMachine current, List unexplored) { + public void setScheduleChoice(int stepNum, int choiceNum, PMachineId current, List unexplored) { if (choiceNum == choices.size()) { choices.add(null); } @@ -139,7 +140,7 @@ public void setDataChoice(int stepNum, int choiceNum, PValue current, List getCurrentDataChoice(int idx) { * @param idx Choice depth * @return List of machines, or null if index is invalid */ - public List getUnexploredScheduleChoices(int idx) { + public List getUnexploredScheduleChoices(int idx) { if (idx < size()) { assert (choices.get(idx) instanceof ScheduleChoice); return ((ScheduleChoice) choices.get(idx)).getUnexplored(); diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Scheduler.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Scheduler.java index ad2865e5c..163b0ef86 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Scheduler.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Scheduler.java @@ -5,6 +5,7 @@ import pexplicit.runtime.PExplicitGlobal; import pexplicit.runtime.logger.PExplicitLogger; import pexplicit.runtime.machine.PMachine; +import pexplicit.runtime.machine.PMachineId; import pexplicit.runtime.machine.PMonitor; import pexplicit.runtime.scheduler.explicit.StepState; import pexplicit.utils.exceptions.BugFoundException; @@ -271,8 +272,8 @@ public PMachine allocateMachine( Function constructor) { // get machine count for given type from schedule int machineCount = stepState.getMachineCount(machineType); - - PMachine machine = PExplicitGlobal.getGlobalMachine(machineType, machineCount); + PMachineId pid = new PMachineId(machineType, machineCount); + PMachine machine = PExplicitGlobal.getGlobalMachine(pid); if (machine == null) { // create a new machine machine = constructor.apply(machineCount); diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleChoice.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleChoice.java index c7f67815d..0bbc9b40c 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleChoice.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleChoice.java @@ -2,7 +2,7 @@ import lombok.Getter; import lombok.Setter; -import pexplicit.runtime.machine.PMachine; +import pexplicit.runtime.machine.PMachineId; import pexplicit.runtime.scheduler.explicit.StepState; import java.util.ArrayList; @@ -10,13 +10,13 @@ @Getter @Setter -public class ScheduleChoice extends Choice { +public class ScheduleChoice extends Choice { private StepState choiceState = null; /** * Constructor */ - public ScheduleChoice(int stepNum, int choiceNum, PMachine c, List u, StepState s) { + public ScheduleChoice(int stepNum, int choiceNum, PMachineId c, List u, StepState s) { super(c, u, stepNum, choiceNum); this.choiceState = s; } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java index 3bbf10de9..72118b76c 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java @@ -10,6 +10,7 @@ import pexplicit.runtime.logger.ScratchLogger; import pexplicit.runtime.logger.StatWriter; import pexplicit.runtime.machine.PMachine; +import pexplicit.runtime.machine.PMachineId; import pexplicit.runtime.scheduler.Scheduler; import pexplicit.runtime.scheduler.choice.Choice; import pexplicit.runtime.scheduler.choice.ScheduleChoice; @@ -301,7 +302,8 @@ public PMachine getNextScheduleChoice() { if (choiceNumber < backtrackChoiceNumber) { // pick the current schedule choice - result = schedule.getCurrentScheduleChoice(choiceNumber); + PMachineId pid = schedule.getCurrentScheduleChoice(choiceNumber); + result = PExplicitGlobal.getGlobalMachine(pid); PExplicitLogger.logRepeatScheduleChoice(result, stepNumber, choiceNumber); // increment choice number @@ -310,7 +312,7 @@ public PMachine getNextScheduleChoice() { } // get existing unexplored choices, if any - List choices = schedule.getUnexploredScheduleChoices(choiceNumber); + List choices = schedule.getUnexploredScheduleChoices(choiceNumber); if (choices.isEmpty()) { // no existing unexplored choices, so try generating new choices @@ -330,14 +332,14 @@ public PMachine getNextScheduleChoice() { } // pick the first choice - result = choices.get(0); + result = PExplicitGlobal.getGlobalMachine(choices.get(0)); PExplicitLogger.logCurrentScheduleChoice(result, stepNumber, choiceNumber); // remove the first choice from unexplored choices choices.remove(0); // add choice to schedule - schedule.setScheduleChoice(stepNumber, choiceNumber, result, choices); + schedule.setScheduleChoice(stepNumber, choiceNumber, result.getPid(), choices); // increment choice number choiceNumber++; @@ -521,8 +523,9 @@ private void postIterationCleanup() { choiceNumber = scheduleChoice.getChoiceNumber(); stepState.setTo(scheduleChoice.getChoiceState()); - assert (!scheduleChoice.getCurrent().getSendBuffer().isEmpty()); - for (PMachine machine : scheduleChoice.getUnexplored()) { + assert (!PExplicitGlobal.getGlobalMachine(scheduleChoice.getCurrent()).getSendBuffer().isEmpty()); + for (PMachineId pid : scheduleChoice.getUnexplored()) { + PMachine machine = PExplicitGlobal.getGlobalMachine(pid); assert (!machine.getSendBuffer().isEmpty()); } } @@ -535,20 +538,20 @@ private void postIterationCleanup() { isDoneIterating = true; } - private List getNewScheduleChoices() { + private List getNewScheduleChoices() { // prioritize create machine events for (PMachine machine : stepState.getMachineSet()) { if (machine.getSendBuffer().nextIsCreateMachineMsg()) { - return new ArrayList<>(Collections.singletonList(machine)); + return new ArrayList<>(Collections.singletonList(machine.getPid())); } } // now there are no create machine events remaining - List choices = new ArrayList<>(); + List choices = new ArrayList<>(); for (PMachine machine : stepState.getMachineSet()) { if (machine.getSendBuffer().nextHasTargetRunning()) { - choices.add(machine); + choices.add(machine.getPid()); } } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/StepState.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/StepState.java index f706c1e6d..9af4836be 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/StepState.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/StepState.java @@ -87,31 +87,6 @@ public void makeMachine(PMachine machine) { machineSet.add(machine); } - /** - * Check if a machine of a given type and index exists in the schedule. - * - * @param type Machine type - * @param idx Machine index - * @return true if machine is in this schedule, false otherwise - */ - public boolean hasMachine(Class type, int idx) { - if (!machineListByType.containsKey(type)) - return false; - return idx < machineListByType.get(type).size(); - } - - /** - * Get a machine of a given type and index. - * - * @param type Machine type - * @param idx Machine index - * @return Machine - */ - public PMachine getMachine(Class type, int idx) { - assert (hasMachine(type, idx)); - return machineListByType.get(type).get(idx); - } - /** * Get the number of machines of a given type in the schedule. * diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/replay/ReplayScheduler.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/replay/ReplayScheduler.java index fc9776bf5..2b5b48764 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/replay/ReplayScheduler.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/replay/ReplayScheduler.java @@ -4,6 +4,7 @@ import pexplicit.runtime.logger.PExplicitLogger; import pexplicit.runtime.logger.ScheduleWriter; import pexplicit.runtime.machine.PMachine; +import pexplicit.runtime.machine.PMachineId; import pexplicit.runtime.scheduler.Schedule; import pexplicit.runtime.scheduler.Scheduler; import pexplicit.utils.misc.Assert; @@ -87,11 +88,12 @@ protected PMachine getNextScheduleChoice() { } // pick the current schedule choice - PMachine result = schedule.getCurrentScheduleChoice(choiceNumber); - if (result == null) { + PMachineId pid = schedule.getCurrentScheduleChoice(choiceNumber); + if (pid == null) { return null; } + PMachine result = PExplicitGlobal.getGlobalMachine(pid); ScheduleWriter.logScheduleChoice(result); PExplicitLogger.logRepeatScheduleChoice(result, stepNumber, choiceNumber); From 53c5d15e94f9bf42cb0f12e348de34a184744e9a Mon Sep 17 00:00:00 2001 From: Aman Goel Date: Thu, 13 Jun 2024 19:50:02 +0000 Subject: [PATCH 02/31] [PEx] Major revamping of Choice: 1/n Old schedule/data choice changed to schedule/data SearchUnit Added new class for schedule/data Choice, which is just a wrapper around PMachineId/PValue TODO: clean up Schedule with new class structure --- .../runtime/logger/PExplicitLogger.java | 27 +++--- .../runtime/logger/ScheduleWriter.java | 12 +-- .../pexplicit/runtime/machine/PMachineId.java | 17 ++++ .../pexplicit/runtime/scheduler/Schedule.java | 86 +++++++++---------- .../runtime/scheduler/Scheduler.java | 13 +-- .../runtime/scheduler/choice/Choice.java | 72 ++-------------- .../runtime/scheduler/choice/DataChoice.java | 46 +++------- .../scheduler/choice/DataSearchUnit.java | 48 +++++++++++ .../scheduler/choice/ScheduleChoice.java | 50 +++-------- .../scheduler/choice/ScheduleSearchUnit.java | 52 +++++++++++ .../runtime/scheduler/choice/SearchUnit.java | 74 ++++++++++++++++ .../explicit/ExplicitSearchScheduler.java | 75 ++++++++-------- .../explicit/strategy/SearchTask.java | 36 ++++---- .../scheduler/replay/ReplayScheduler.java | 15 ++-- .../java/pexplicit/values/PMachineValue.java | 24 ++++-- 15 files changed, 373 insertions(+), 274 deletions(-) create mode 100644 Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/DataSearchUnit.java create mode 100644 Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleSearchUnit.java create mode 100644 Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/SearchUnit.java diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java index 7782909d6..9e4492119 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java @@ -13,8 +13,11 @@ import pexplicit.runtime.machine.PMonitor; import pexplicit.runtime.machine.State; import pexplicit.runtime.machine.events.PContinuation; -import pexplicit.runtime.scheduler.choice.Choice; +import pexplicit.runtime.scheduler.Schedule; +import pexplicit.runtime.scheduler.choice.DataChoice; import pexplicit.runtime.scheduler.choice.ScheduleChoice; +import pexplicit.runtime.scheduler.choice.ScheduleSearchUnit; +import pexplicit.runtime.scheduler.choice.SearchUnit; import pexplicit.runtime.scheduler.explicit.ExplicitSearchScheduler; import pexplicit.runtime.scheduler.explicit.SearchStatistics; import pexplicit.runtime.scheduler.explicit.StateCachingMode; @@ -207,48 +210,48 @@ public static void logFinishedIteration(int step) { /** * Log when backtracking to a new choice * - * @param choice Choice to which backtracking to + * @param searchUnit Choice to which backtracking to */ - public static void logBacktrack(Choice choice) { + public static void logBacktrack(SearchUnit searchUnit) { if (verbosity > 1) { log.info(String.format(" Backtracking to %s choice @%d::%d", - ((choice instanceof ScheduleChoice) ? "schedule" : "data"), - choice.getStepNumber(), - choice.getChoiceNumber())); + ((searchUnit instanceof ScheduleSearchUnit) ? "schedule" : "data"), + searchUnit.getStepNumber(), + searchUnit.getChoiceNumber())); } } - public static void logNewScheduleChoice(List choices, int step, int idx) { + public static void logNewScheduleChoice(List choices, int step, int idx) { if (verbosity > 1) { log.info(String.format(" @%d::%d new schedule choice: %s", step, idx, choices)); } } - public static void logNewDataChoice(List> choices, int step, int idx) { + public static void logNewDataChoice(List choices, int step, int idx) { if (verbosity > 1) { log.info(String.format(" @%d::%d new data choice: %s", step, idx, choices)); } } - public static void logRepeatScheduleChoice(PMachine choice, int step, int idx) { + public static void logRepeatScheduleChoice(ScheduleChoice choice, int step, int idx) { if (verbosity > 2) { log.info(String.format(" @%d::%d %s (repeat)", step, idx, choice)); } } - public static void logCurrentScheduleChoice(PMachine choice, int step, int idx) { + public static void logCurrentScheduleChoice(ScheduleChoice choice, int step, int idx) { if (verbosity > 2) { log.info(String.format(" @%d::%d %s", step, idx, choice)); } } - public static void logRepeatDataChoice(PValue choice, int step, int idx) { + public static void logRepeatDataChoice(DataChoice choice, int step, int idx) { if (verbosity > 2) { log.info(String.format(" @%d::%d %s (repeat)", step, idx, choice)); } } - public static void logCurrentDataChoice(PValue choice, int step, int idx) { + public static void logCurrentDataChoice(DataChoice choice, int step, int idx) { if (verbosity > 2) { log.info(String.format(" @%d::%d %s", step, idx, choice)); } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/ScheduleWriter.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/ScheduleWriter.java index 548486ac5..f252cc5d1 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/ScheduleWriter.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/ScheduleWriter.java @@ -3,6 +3,8 @@ import lombok.Getter; import pexplicit.runtime.PExplicitGlobal; import pexplicit.runtime.machine.PMachine; +import pexplicit.runtime.scheduler.choice.DataChoice; +import pexplicit.runtime.scheduler.choice.ScheduleChoice; import pexplicit.utils.misc.Assert; import pexplicit.values.PBool; import pexplicit.values.PInt; @@ -42,12 +44,12 @@ private static void logComment(String message) { logIdx++; } - public static void logDataChoice(PValue choice) { + public static void logDataChoice(DataChoice choice) { Class type = choice.getClass(); - if (choice instanceof PBool boolChoice) { + if (choice.getValue() instanceof PBool boolChoice) { logComment("boolean choice"); log(boolChoice.getValue() ? "True" : "False"); - } else if (choice instanceof PInt intChoice) { + } else if (choice.getValue() instanceof PInt intChoice) { logComment("integer choice"); log(intChoice.toString()); } else { @@ -55,9 +57,9 @@ public static void logDataChoice(PValue choice) { } } - public static void logScheduleChoice(PMachine choice) { + public static void logScheduleChoice(ScheduleChoice choice) { logComment("schedule choice"); - log(String.format("(%d)", choice.getInstanceId())); + log(String.format("(%d)", PExplicitGlobal.getGlobalMachine(choice.getValue()).getInstanceId())); } public static void logHeader() { diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/PMachineId.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/PMachineId.java index 569da384e..baf9d3d95 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/PMachineId.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/PMachineId.java @@ -1,6 +1,7 @@ package pexplicit.runtime.machine; import lombok.Getter; +import pexplicit.runtime.scheduler.choice.ScheduleChoice; @Getter public class PMachineId { @@ -11,4 +12,20 @@ public PMachineId(Class t, int tid) { type = t; typeId = tid; } + + @Override + public String toString() { + return String.format("%s<%d>", type.getSimpleName(), typeId); + } + + @Override + public boolean equals(Object obj) { + if (obj == this) + return true; + else if (!(obj instanceof PMachineId)) { + return false; + } + PMachineId rhs = (PMachineId) obj; + return this.type == rhs.type && this.typeId == rhs.typeId; + } } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Schedule.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Schedule.java index 1b4cf8633..b9433ce39 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Schedule.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Schedule.java @@ -1,13 +1,11 @@ package pexplicit.runtime.scheduler; +import lombok.Data; import lombok.Getter; import lombok.Setter; import pexplicit.runtime.PExplicitGlobal; -import pexplicit.runtime.machine.PMachine; import pexplicit.runtime.machine.PMachineId; -import pexplicit.runtime.scheduler.choice.Choice; -import pexplicit.runtime.scheduler.choice.DataChoice; -import pexplicit.runtime.scheduler.choice.ScheduleChoice; +import pexplicit.runtime.scheduler.choice.*; import pexplicit.runtime.scheduler.explicit.StepState; import pexplicit.values.PValue; @@ -20,11 +18,11 @@ */ public class Schedule implements Serializable { /** - * List of choices + * List of current choices */ @Getter @Setter - private List choices = new ArrayList<>(); + private List searchUnits = new ArrayList<>(); /** * Step state at the start of a scheduler step. @@ -45,8 +43,8 @@ public Schedule() { * @param idx Choice depth * @return Choice at depth idx */ - public Choice getChoice(int idx) { - return choices.get(idx); + public SearchUnit getChoice(int idx) { + return searchUnits.get(idx); } /** @@ -55,8 +53,8 @@ public Choice getChoice(int idx) { * @param idx Choice depth */ public void clearChoice(int idx) { - choices.get(idx).clearCurrent(); - choices.get(idx).clearUnexplored(); + searchUnits.get(idx).clearCurrent(); + searchUnits.get(idx).clearUnexplored(); } /** @@ -65,7 +63,7 @@ public void clearChoice(int idx) { * @param choiceNum Choice depth */ public void removeChoicesAfter(int choiceNum) { - choices.subList(choiceNum + 1, choices.size()).clear(); + searchUnits.subList(choiceNum + 1, searchUnits.size()).clear(); } /** @@ -75,7 +73,7 @@ public void removeChoicesAfter(int choiceNum) { */ public int getNumUnexploredChoices() { int numUnexplored = 0; - for (Choice c : choices) { + for (SearchUnit c : searchUnits) { numUnexplored += c.getUnexplored().size(); } return numUnexplored; @@ -88,8 +86,8 @@ public int getNumUnexploredChoices() { */ public int getNumUnexploredDataChoices() { int numUnexplored = 0; - for (Choice c : choices) { - if (c instanceof DataChoice) { + for (SearchUnit c : searchUnits) { + if (c instanceof DataSearchUnit) { numUnexplored += c.getUnexplored().size(); } } @@ -104,17 +102,17 @@ public int getNumUnexploredDataChoices() { * @param current Machine to set as current schedule choice * @param unexplored List of machine to set as unexplored schedule choices */ - public void setScheduleChoice(int stepNum, int choiceNum, PMachineId current, List unexplored) { - if (choiceNum == choices.size()) { - choices.add(null); + public void setScheduleChoice(int stepNum, int choiceNum, ScheduleChoice current, List unexplored) { + if (choiceNum == searchUnits.size()) { + searchUnits.add(null); } - assert (choiceNum < choices.size()); + assert (choiceNum < searchUnits.size()); if (PExplicitGlobal.getConfig().isStatefulBacktrackEnabled() && stepNum != 0) { assert (stepBeginState != null); - choices.set(choiceNum, new ScheduleChoice(stepNum, choiceNum, current, unexplored, stepBeginState)); + searchUnits.set(choiceNum, new ScheduleSearchUnit(stepNum, choiceNum, current, unexplored, stepBeginState)); } else { - choices.set(choiceNum, new ScheduleChoice(stepNum, choiceNum, current, unexplored, null)); + searchUnits.set(choiceNum, new ScheduleSearchUnit(stepNum, choiceNum, current, unexplored, null)); } } @@ -126,12 +124,12 @@ public void setScheduleChoice(int stepNum, int choiceNum, PMachineId current, Li * @param current PValue to set as current schedule choice * @param unexplored List of PValue to set as unexplored schedule choices */ - public void setDataChoice(int stepNum, int choiceNum, PValue current, List> unexplored) { - if (choiceNum == choices.size()) { - choices.add(null); + public void setDataChoice(int stepNum, int choiceNum, DataChoice current, List unexplored) { + if (choiceNum == searchUnits.size()) { + searchUnits.add(null); } - assert (choiceNum < choices.size()); - choices.set(choiceNum, new DataChoice(stepNum, choiceNum, current, unexplored)); + assert (choiceNum < searchUnits.size()); + searchUnits.set(choiceNum, new DataSearchUnit(stepNum, choiceNum, current, unexplored)); } /** @@ -140,9 +138,9 @@ public void setDataChoice(int stepNum, int choiceNum, PValue current, List getCurrentDataChoice(int idx) { - assert (choices.get(idx) instanceof DataChoice); - return ((DataChoice) choices.get(idx)).getCurrent(); + public DataChoice getCurrentDataChoice(int idx) { + assert (searchUnits.get(idx) instanceof DataSearchUnit); + return ((DataSearchUnit) searchUnits.get(idx)).getCurrent(); } /** @@ -162,10 +160,10 @@ public PValue getCurrentDataChoice(int idx) { * @param idx Choice depth * @return List of machines, or null if index is invalid */ - public List getUnexploredScheduleChoices(int idx) { + public List getUnexploredScheduleChoices(int idx) { if (idx < size()) { - assert (choices.get(idx) instanceof ScheduleChoice); - return ((ScheduleChoice) choices.get(idx)).getUnexplored(); + assert (searchUnits.get(idx) instanceof ScheduleSearchUnit); + return ((ScheduleSearchUnit) searchUnits.get(idx)).getUnexplored(); } else { return new ArrayList<>(); } @@ -177,22 +175,22 @@ public List getUnexploredScheduleChoices(int idx) { * @param idx Choice depth * @return List of PValue, or null if index is invalid */ - public List> getUnexploredDataChoices(int idx) { + public List getUnexploredDataChoices(int idx) { if (idx < size()) { - assert (choices.get(idx) instanceof DataChoice); - return ((DataChoice) choices.get(idx)).getUnexplored(); + assert (searchUnits.get(idx) instanceof DataSearchUnit); + return ((DataSearchUnit) searchUnits.get(idx)).getUnexplored(); } else { return new ArrayList<>(); } } - public ScheduleChoice getScheduleChoiceAt(Choice choice) { - if (choice instanceof ScheduleChoice scheduleChoice) { + public ScheduleSearchUnit getScheduleChoiceAt(SearchUnit searchUnit) { + if (searchUnit instanceof ScheduleSearchUnit scheduleChoice) { return scheduleChoice; } else { - for (int i = choice.getChoiceNumber() - 1; i >= 0; i--) { - Choice c = choices.get(i); - if (c instanceof ScheduleChoice scheduleChoice) { + for (int i = searchUnit.getChoiceNumber() - 1; i >= 0; i--) { + SearchUnit c = searchUnits.get(i); + if (c instanceof ScheduleSearchUnit scheduleChoice) { return scheduleChoice; } } @@ -206,7 +204,7 @@ public ScheduleChoice getScheduleChoiceAt(Choice choice) { * @param idx Choice depth */ public void clearCurrent(int idx) { - choices.get(idx).clearCurrent(); + searchUnits.get(idx).clearCurrent(); } /** @@ -215,6 +213,6 @@ public void clearCurrent(int idx) { * @return Number of choices in the schedule */ public int size() { - return choices.size(); + return searchUnits.size(); } } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Scheduler.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Scheduler.java index 163b0ef86..9418088dd 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Scheduler.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Scheduler.java @@ -7,6 +7,7 @@ import pexplicit.runtime.machine.PMachine; import pexplicit.runtime.machine.PMachineId; import pexplicit.runtime.machine.PMonitor; +import pexplicit.runtime.scheduler.choice.DataChoice; import pexplicit.runtime.scheduler.explicit.StepState; import pexplicit.utils.exceptions.BugFoundException; import pexplicit.utils.exceptions.NotImplementedException; @@ -118,7 +119,7 @@ protected void reset() { * * @return PValue as data choice */ - protected abstract PValue getNextDataChoice(List> input_choices); + protected abstract PValue getNextDataChoice(List input_choices); public void updateLogNumber() { stepNumLogs += 1; @@ -133,9 +134,9 @@ public void updateLogNumber() { * @return boolean data choice */ public PBool getRandomBool() { - List> choices = new ArrayList<>(); - choices.add(PBool.PTRUE); - choices.add(PBool.PFALSE); + List choices = new ArrayList<>(); + choices.add(new DataChoice(PBool.PTRUE)); + choices.add(new DataChoice(PBool.PFALSE)); return (PBool) getNextDataChoice(choices); } @@ -146,7 +147,7 @@ public PBool getRandomBool() { * @return integer data choice */ public PInt getRandomInt(PInt bound) { - List> choices = new ArrayList<>(); + List choices = new ArrayList<>(); int boundInt = bound.getValue(); if (boundInt > 10000) { throw new BugFoundException(String.format("choose expects a parameter with at most 10,000 choices, got %d choices instead.", boundInt)); @@ -155,7 +156,7 @@ public PInt getRandomInt(PInt bound) { boundInt = 1; } for (int i = 0; i < boundInt; i++) { - choices.add(new PInt(i)); + choices.add(new DataChoice(new PInt(i))); } return (PInt) getNextDataChoice(choices); } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/Choice.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/Choice.java index 486fa7d9d..71ac475fb 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/Choice.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/Choice.java @@ -1,74 +1,14 @@ package pexplicit.runtime.scheduler.choice; +import lombok.AllArgsConstructor; import lombok.Getter; import lombok.Setter; import java.io.Serializable; -import java.util.List; -/** - * Represents a schedule or data choice - */ +@Getter +@Setter +@AllArgsConstructor public abstract class Choice implements Serializable { - @Getter - @Setter - protected T current; - @Getter - @Setter - protected List unexplored; - - /** - * Step number - */ - @Getter - protected int stepNumber = 0; - /** - * Choice number - */ - @Getter - protected int choiceNumber = 0; - - protected Choice(T c, List u, int stepNum, int choiceNum) { - this.current = c; - this.unexplored = u; - this.stepNumber = stepNum; - this.choiceNumber = choiceNum; - } - - /** - * Check if this choice has an unexplored choice remaining. - * - * @return true if this choice has an unexplored choice, false otherwise - */ - public boolean isUnexploredNonEmpty() { - return !unexplored.isEmpty(); - } - - /** - * Clear current choices - */ - public void clearCurrent() { - this.current = null; - } - - /** - * Clean unexplored choices - */ - abstract public void clearUnexplored(); - - /** - * Copy current choice as a new Choice object - * - * @return Choice object with the copied current choice - */ - abstract public Choice copyCurrent(); - - /** - * Copy this choice to a new choice and clear any unexplored choices. - * - * @return New choice same as original choice - */ - abstract public Choice transferChoice(); - - abstract public String toString(); -} \ No newline at end of file + protected T value; +} diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/DataChoice.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/DataChoice.java index 97b88519c..684d89eb2 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/DataChoice.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/DataChoice.java @@ -1,48 +1,24 @@ package pexplicit.runtime.scheduler.choice; -import lombok.Getter; -import lombok.Setter; import pexplicit.values.PValue; -import java.util.ArrayList; -import java.util.List; - -@Getter -@Setter public class DataChoice extends Choice> { - /** - * Constructor - */ - public DataChoice(int stepNum, int choiceNum, PValue c, List> u) { - super(c, u, stepNum, choiceNum); - } - - /** - * Clean unexplored choices - */ - public void clearUnexplored() { - unexplored.clear(); - } - - public Choice copyCurrent() { - return new DataChoice(this.stepNumber, this.choiceNumber, this.current, new ArrayList<>()); + public DataChoice(PValue value) { + super(value); } - public Choice transferChoice() { - DataChoice newChoice = new DataChoice(this.stepNumber, this.choiceNumber, this.current, this.unexplored); - this.unexplored = new ArrayList<>(); - return newChoice; + @Override + public String toString() { + return value.toString(); } @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - if (current != null) { - sb.append(String.format("curr:%s", current)); - } - if (unexplored != null && !unexplored.isEmpty()) { - sb.append(String.format(" rem:%s", unexplored)); + public boolean equals(Object obj) { + if (obj == this) + return true; + else if (!(obj instanceof DataChoice)) { + return false; } - return sb.toString(); + return this.value == ((DataChoice) obj).value; } } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/DataSearchUnit.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/DataSearchUnit.java new file mode 100644 index 000000000..66e253714 --- /dev/null +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/DataSearchUnit.java @@ -0,0 +1,48 @@ +package pexplicit.runtime.scheduler.choice; + +import lombok.Getter; +import lombok.Setter; +import pexplicit.values.PValue; + +import java.util.ArrayList; +import java.util.List; + +@Getter +@Setter +public class DataSearchUnit extends SearchUnit { + /** + * Constructor + */ + public DataSearchUnit(int stepNum, int choiceNum, DataChoice c, List u) { + super(c, u, stepNum, choiceNum); + } + + /** + * Clean unexplored choices + */ + public void clearUnexplored() { + unexplored.clear(); + } + + public SearchUnit copyCurrent() { + return new DataSearchUnit(this.stepNumber, this.choiceNumber, this.current, new ArrayList<>()); + } + + public SearchUnit transferChoice() { + DataSearchUnit newChoice = new DataSearchUnit(this.stepNumber, this.choiceNumber, this.current, this.unexplored); + this.unexplored = new ArrayList<>(); + return newChoice; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + if (current != null) { + sb.append(String.format("curr:%s", current)); + } + if (unexplored != null && !unexplored.isEmpty()) { + sb.append(String.format(" rem:%s", unexplored)); + } + return sb.toString(); + } +} diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleChoice.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleChoice.java index 0bbc9b40c..1741c5457 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleChoice.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleChoice.java @@ -1,52 +1,24 @@ package pexplicit.runtime.scheduler.choice; -import lombok.Getter; -import lombok.Setter; import pexplicit.runtime.machine.PMachineId; -import pexplicit.runtime.scheduler.explicit.StepState; -import java.util.ArrayList; -import java.util.List; - -@Getter -@Setter public class ScheduleChoice extends Choice { - private StepState choiceState = null; - - /** - * Constructor - */ - public ScheduleChoice(int stepNum, int choiceNum, PMachineId c, List u, StepState s) { - super(c, u, stepNum, choiceNum); - this.choiceState = s; - } - - /** - * Clean unexplored choices - */ - public void clearUnexplored() { - unexplored.clear(); - } - - public Choice copyCurrent() { - return new ScheduleChoice(this.stepNumber, this.choiceNumber, this.current, new ArrayList<>(), this.choiceState); + public ScheduleChoice(PMachineId value) { + super(value); } - public Choice transferChoice() { - ScheduleChoice newChoice = new ScheduleChoice(this.stepNumber, this.choiceNumber, this.current, this.unexplored, this.choiceState); - this.unexplored = new ArrayList<>(); - return newChoice; + @Override + public String toString() { + return value.toString(); } @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - if (current != null) { - sb.append(String.format("curr@%s", current)); - } - if (unexplored != null && !unexplored.isEmpty()) { - sb.append(String.format(" rem@%s", unexplored)); + public boolean equals(Object obj) { + if (obj == this) + return true; + else if (!(obj instanceof ScheduleChoice)) { + return false; } - return sb.toString(); + return this.value == ((ScheduleChoice) obj).value; } } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleSearchUnit.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleSearchUnit.java new file mode 100644 index 000000000..1d9dd5be9 --- /dev/null +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleSearchUnit.java @@ -0,0 +1,52 @@ +package pexplicit.runtime.scheduler.choice; + +import lombok.Getter; +import lombok.Setter; +import pexplicit.runtime.machine.PMachineId; +import pexplicit.runtime.scheduler.explicit.StepState; + +import java.util.ArrayList; +import java.util.List; + +@Getter +@Setter +public class ScheduleSearchUnit extends SearchUnit { + private StepState choiceState = null; + + /** + * Constructor + */ + public ScheduleSearchUnit(int stepNum, int choiceNum, ScheduleChoice c, List u, StepState s) { + super(c, u, stepNum, choiceNum); + this.choiceState = s; + } + + /** + * Clean unexplored choices + */ + public void clearUnexplored() { + unexplored.clear(); + } + + public SearchUnit copyCurrent() { + return new ScheduleSearchUnit(this.stepNumber, this.choiceNumber, this.current, new ArrayList<>(), this.choiceState); + } + + public SearchUnit transferChoice() { + ScheduleSearchUnit newChoice = new ScheduleSearchUnit(this.stepNumber, this.choiceNumber, this.current, this.unexplored, this.choiceState); + this.unexplored = new ArrayList<>(); + return newChoice; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + if (current != null) { + sb.append(String.format("curr@%s", current)); + } + if (unexplored != null && !unexplored.isEmpty()) { + sb.append(String.format(" rem@%s", unexplored)); + } + return sb.toString(); + } +} diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/SearchUnit.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/SearchUnit.java new file mode 100644 index 000000000..1241af8eb --- /dev/null +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/SearchUnit.java @@ -0,0 +1,74 @@ +package pexplicit.runtime.scheduler.choice; + +import lombok.Getter; +import lombok.Setter; + +import java.io.Serializable; +import java.util.List; + +/** + * Represents a schedule or data choice + */ +public abstract class SearchUnit implements Serializable { + @Getter + @Setter + protected T current; + @Getter + @Setter + protected List unexplored; + + /** + * Step number + */ + @Getter + protected int stepNumber = 0; + /** + * Choice number + */ + @Getter + protected int choiceNumber = 0; + + protected SearchUnit(T c, List u, int stepNum, int choiceNum) { + this.current = c; + this.unexplored = u; + this.stepNumber = stepNum; + this.choiceNumber = choiceNum; + } + + /** + * Check if this choice has an unexplored choice remaining. + * + * @return true if this choice has an unexplored choice, false otherwise + */ + public boolean isUnexploredNonEmpty() { + return !unexplored.isEmpty(); + } + + /** + * Clear current choices + */ + public void clearCurrent() { + this.current = null; + } + + /** + * Clean unexplored choices + */ + abstract public void clearUnexplored(); + + /** + * Copy current choice as a new Choice object + * + * @return Choice object with the copied current choice + */ + abstract public SearchUnit copyCurrent(); + + /** + * Copy this choice to a new choice and clear any unexplored choices. + * + * @return New choice same as original choice + */ + abstract public SearchUnit transferChoice(); + + abstract public String toString(); +} \ No newline at end of file diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java index 72118b76c..71fec4e98 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java @@ -12,8 +12,10 @@ import pexplicit.runtime.machine.PMachine; import pexplicit.runtime.machine.PMachineId; import pexplicit.runtime.scheduler.Scheduler; -import pexplicit.runtime.scheduler.choice.Choice; +import pexplicit.runtime.scheduler.choice.DataChoice; import pexplicit.runtime.scheduler.choice.ScheduleChoice; +import pexplicit.runtime.scheduler.choice.ScheduleSearchUnit; +import pexplicit.runtime.scheduler.choice.SearchUnit; import pexplicit.runtime.scheduler.explicit.strategy.*; import pexplicit.utils.exceptions.PExplicitRuntimeException; import pexplicit.utils.misc.Assert; @@ -298,21 +300,20 @@ protected void reset() { */ @Override public PMachine getNextScheduleChoice() { - PMachine result; + ScheduleChoice result; if (choiceNumber < backtrackChoiceNumber) { // pick the current schedule choice - PMachineId pid = schedule.getCurrentScheduleChoice(choiceNumber); - result = PExplicitGlobal.getGlobalMachine(pid); + result = schedule.getCurrentScheduleChoice(choiceNumber); PExplicitLogger.logRepeatScheduleChoice(result, stepNumber, choiceNumber); // increment choice number choiceNumber++; - return result; + return PExplicitGlobal.getGlobalMachine(result.getValue()); } // get existing unexplored choices, if any - List choices = schedule.getUnexploredScheduleChoices(choiceNumber); + List choices = schedule.getUnexploredScheduleChoices(choiceNumber); if (choices.isEmpty()) { // no existing unexplored choices, so try generating new choices @@ -332,18 +333,18 @@ public PMachine getNextScheduleChoice() { } // pick the first choice - result = PExplicitGlobal.getGlobalMachine(choices.get(0)); + result = choices.get(0); PExplicitLogger.logCurrentScheduleChoice(result, stepNumber, choiceNumber); // remove the first choice from unexplored choices choices.remove(0); // add choice to schedule - schedule.setScheduleChoice(stepNumber, choiceNumber, result.getPid(), choices); + schedule.setScheduleChoice(stepNumber, choiceNumber, result, choices); // increment choice number choiceNumber++; - return result; + return PExplicitGlobal.getGlobalMachine(result.getValue()); } /** @@ -352,8 +353,8 @@ public PMachine getNextScheduleChoice() { * @return PValue as data choice */ @Override - public PValue getNextDataChoice(List> input_choices) { - PValue result; + public PValue getNextDataChoice(List input_choices) { + DataChoice result; if (choiceNumber < backtrackChoiceNumber) { // pick the current data choice @@ -363,11 +364,11 @@ public PValue getNextDataChoice(List> input_choices) { // increment choice number choiceNumber++; - return result; + return result.getValue(); } // get existing unexplored choices, if any - List> choices = schedule.getUnexploredDataChoices(choiceNumber); + List choices = schedule.getUnexploredDataChoices(choiceNumber); assert (input_choices.containsAll(choices)); if (choices.isEmpty()) { @@ -399,7 +400,7 @@ public PValue getNextDataChoice(List> input_choices) { // increment choice number choiceNumber++; - return result; + return result.getValue(); } private void postProcessIteration() { @@ -417,15 +418,15 @@ private void addRemainingChoicesAsChildrenTasks() { SearchTask parentTask = searchStrategy.getCurrTask(); int numChildrenAdded = 0; for (int i = 0; i < schedule.size(); i++) { - Choice choice = schedule.getChoice(i); + SearchUnit searchUnit = schedule.getChoice(i); // if choice at this depth is non-empty - if (choice.isUnexploredNonEmpty()) { + if (searchUnit.isUnexploredNonEmpty()) { if (PExplicitGlobal.getConfig().getMaxChildrenPerTask() > 0 && numChildrenAdded == (PExplicitGlobal.getConfig().getMaxChildrenPerTask() - 1)) { - setChildTask(choice, i, parentTask, false); + setChildTask(searchUnit, i, parentTask, false); break; } // top search task should be always exact - setChildTask(choice, i, parentTask, true); + setChildTask(searchUnit, i, parentTask, true); numChildrenAdded++; } } @@ -439,14 +440,14 @@ private void endCurrTask() { searchStrategy.getFinishedTasks().add(currTask.getId()); } - private void setChildTask(Choice choice, int choiceNum, SearchTask parentTask, boolean isExact) { + private void setChildTask(SearchUnit searchUnit, int choiceNum, SearchTask parentTask, boolean isExact) { SearchTask newTask = searchStrategy.createTask(choiceNum, parentTask); for (int i = 0; i < choiceNum; i++) { newTask.addPrefixChoice(schedule.getChoice(i)); } - newTask.addSuffixChoice(choice); + newTask.addSuffixChoice(searchUnit); if (!isExact) { for (int i = choiceNum + 1; i < schedule.size(); i++) { @@ -467,7 +468,7 @@ public SearchTask setNextTask() { SearchTask nextTask = searchStrategy.setNextTask(); if (nextTask != null) { PExplicitLogger.logNextTask(nextTask); - schedule.setChoices(nextTask.getAllChoices()); + schedule.setSearchUnits(nextTask.getAllChoices()); postIterationCleanup(); } return nextTask; @@ -498,21 +499,21 @@ public double getUnexploredDataChoicesPercent() { private void postIterationCleanup() { for (int cIdx = schedule.size() - 1; cIdx >= 0; cIdx--) { - Choice choice = schedule.getChoice(cIdx); - if (choice.isUnexploredNonEmpty()) { - PExplicitLogger.logBacktrack(choice); + SearchUnit searchUnit = schedule.getChoice(cIdx); + if (searchUnit.isUnexploredNonEmpty()) { + PExplicitLogger.logBacktrack(searchUnit); backtrackChoiceNumber = cIdx; int newStepNumber = 0; - ScheduleChoice scheduleChoice = null; + ScheduleSearchUnit scheduleChoice = null; if (PExplicitGlobal.getConfig().isStatefulBacktrackEnabled()) { - scheduleChoice = schedule.getScheduleChoiceAt(choice); + scheduleChoice = schedule.getScheduleChoiceAt(searchUnit); if (scheduleChoice != null && scheduleChoice.getChoiceState() != null) { - assert ((scheduleChoice == choice) || - (scheduleChoice.getStepNumber() == (choice.getStepNumber() - 1)) || - (scheduleChoice.getStepNumber() == choice.getStepNumber())); + assert ((scheduleChoice == searchUnit) || + (scheduleChoice.getStepNumber() == (searchUnit.getStepNumber() - 1)) || + (scheduleChoice.getStepNumber() == searchUnit.getStepNumber())); newStepNumber = scheduleChoice.getStepNumber(); } else { - assert (choice.getStepNumber() <= 1); + assert (searchUnit.getStepNumber() <= 1); } } if (newStepNumber == 0) { @@ -523,9 +524,9 @@ private void postIterationCleanup() { choiceNumber = scheduleChoice.getChoiceNumber(); stepState.setTo(scheduleChoice.getChoiceState()); - assert (!PExplicitGlobal.getGlobalMachine(scheduleChoice.getCurrent()).getSendBuffer().isEmpty()); - for (PMachineId pid : scheduleChoice.getUnexplored()) { - PMachine machine = PExplicitGlobal.getGlobalMachine(pid); + assert (!PExplicitGlobal.getGlobalMachine(scheduleChoice.getCurrent().getValue()).getSendBuffer().isEmpty()); + for (ScheduleChoice sc : scheduleChoice.getUnexplored()) { + PMachine machine = PExplicitGlobal.getGlobalMachine(sc.getValue()); assert (!machine.getSendBuffer().isEmpty()); } } @@ -538,20 +539,20 @@ private void postIterationCleanup() { isDoneIterating = true; } - private List getNewScheduleChoices() { + private List getNewScheduleChoices() { // prioritize create machine events for (PMachine machine : stepState.getMachineSet()) { if (machine.getSendBuffer().nextIsCreateMachineMsg()) { - return new ArrayList<>(Collections.singletonList(machine.getPid())); + return new ArrayList<>(Collections.singletonList(new ScheduleChoice(machine.getPid()))); } } // now there are no create machine events remaining - List choices = new ArrayList<>(); + List choices = new ArrayList<>(); for (PMachine machine : stepState.getMachineSet()) { if (machine.getSendBuffer().nextHasTargetRunning()) { - choices.add(machine.getPid()); + choices.add(new ScheduleChoice(machine.getPid())); } } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchTask.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchTask.java index 14c25960a..4ebe6f43b 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchTask.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchTask.java @@ -1,9 +1,9 @@ package pexplicit.runtime.scheduler.explicit.strategy; import lombok.Getter; -import pexplicit.runtime.scheduler.choice.Choice; -import pexplicit.runtime.scheduler.choice.DataChoice; -import pexplicit.runtime.scheduler.choice.ScheduleChoice; +import pexplicit.runtime.scheduler.choice.DataSearchUnit; +import pexplicit.runtime.scheduler.choice.SearchUnit; +import pexplicit.runtime.scheduler.choice.ScheduleSearchUnit; import java.io.Serializable; import java.util.ArrayList; @@ -18,8 +18,8 @@ public class SearchTask implements Serializable { private final List children = new ArrayList<>(); @Getter private final int currChoiceNumber; - private final List prefixChoices = new ArrayList<>(); - private final List suffixChoices = new ArrayList<>(); + private final List prefixSearchUnits = new ArrayList<>(); + private final List suffixSearchUnits = new ArrayList<>(); @Getter private int numUnexploredScheduleChoices = 0; @Getter @@ -40,27 +40,27 @@ public void addChild(SearchTask task) { } public void cleanup() { - prefixChoices.clear(); - suffixChoices.clear(); + prefixSearchUnits.clear(); + suffixSearchUnits.clear(); } - public void addPrefixChoice(Choice choice) { - prefixChoices.add(choice.copyCurrent()); + public void addPrefixChoice(SearchUnit searchUnit) { + prefixSearchUnits.add(searchUnit.copyCurrent()); } - public void addSuffixChoice(Choice choice) { - if (choice instanceof ScheduleChoice scheduleChoice) { + public void addSuffixChoice(SearchUnit searchUnit) { + if (searchUnit instanceof ScheduleSearchUnit scheduleChoice) { numUnexploredScheduleChoices += scheduleChoice.getUnexplored().size(); } else { - numUnexploredDataChoices += ((DataChoice) choice).getUnexplored().size(); + numUnexploredDataChoices += ((DataSearchUnit) searchUnit).getUnexplored().size(); } - suffixChoices.add(choice.transferChoice()); + suffixSearchUnits.add(searchUnit.transferChoice()); } - public List getAllChoices() { - List result = new ArrayList<>(prefixChoices); - result.addAll(suffixChoices); - assert (result.size() == (currChoiceNumber + suffixChoices.size())); + public List getAllChoices() { + List result = new ArrayList<>(prefixSearchUnits); + result.addAll(suffixSearchUnits); + assert (result.size() == (currChoiceNumber + suffixSearchUnits.size())); return result; } @@ -89,7 +89,7 @@ public String toStringDetailed() { } return String.format("%s @%d::%d (parent: %s)", this, - suffixChoices.get(0).getStepNumber(), + suffixSearchUnits.get(0).getStepNumber(), currChoiceNumber, parentTask); } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/replay/ReplayScheduler.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/replay/ReplayScheduler.java index 2b5b48764..82e5887a7 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/replay/ReplayScheduler.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/replay/ReplayScheduler.java @@ -7,6 +7,8 @@ import pexplicit.runtime.machine.PMachineId; import pexplicit.runtime.scheduler.Schedule; import pexplicit.runtime.scheduler.Scheduler; +import pexplicit.runtime.scheduler.choice.DataChoice; +import pexplicit.runtime.scheduler.choice.ScheduleChoice; import pexplicit.utils.misc.Assert; import pexplicit.values.PValue; @@ -88,32 +90,31 @@ protected PMachine getNextScheduleChoice() { } // pick the current schedule choice - PMachineId pid = schedule.getCurrentScheduleChoice(choiceNumber); - if (pid == null) { + ScheduleChoice result = schedule.getCurrentScheduleChoice(choiceNumber); + if (result == null) { return null; } - PMachine result = PExplicitGlobal.getGlobalMachine(pid); ScheduleWriter.logScheduleChoice(result); PExplicitLogger.logRepeatScheduleChoice(result, stepNumber, choiceNumber); choiceNumber++; - return result; + return PExplicitGlobal.getGlobalMachine(result.getValue()); } @Override - protected PValue getNextDataChoice(List> input_choices) { + protected PValue getNextDataChoice(List input_choices) { if (choiceNumber >= schedule.size()) { return null; } // pick the current data choice - PValue result = schedule.getCurrentDataChoice(choiceNumber); + DataChoice result = schedule.getCurrentDataChoice(choiceNumber); assert (input_choices.contains(result)); ScheduleWriter.logDataChoice(result); PExplicitLogger.logRepeatDataChoice(result, stepNumber, choiceNumber); choiceNumber++; - return result; + return result.getValue(); } } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/values/PMachineValue.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/values/PMachineValue.java index 8b39af167..ba172f741 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/values/PMachineValue.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/values/PMachineValue.java @@ -1,7 +1,9 @@ package pexplicit.values; import lombok.Getter; +import pexplicit.runtime.PExplicitGlobal; import pexplicit.runtime.machine.PMachine; +import pexplicit.runtime.machine.PMachineId; import pexplicit.runtime.machine.PMonitor; import pexplicit.utils.exceptions.BugFoundException; @@ -10,7 +12,8 @@ */ @Getter public class PMachineValue extends PValue { - private final PMachine value; + private final PMachineId pid; + private final String name; /** * Constructor @@ -18,16 +21,27 @@ public class PMachineValue extends PValue { * @param val machine value to set to */ public PMachineValue(PMachine val) { - value = val; + pid = val.getPid(); + name = val.toString(); initialize(); } + private PMachineValue(PMachineId inp_pid, String inp_name) { + pid = inp_pid; + name = inp_name; + } + + public PMachine getValue() { + return PExplicitGlobal.getGlobalMachine(pid); + } + /** * Get the unique machine identifier * * @return unique machine instance id */ public int getId() { + PMachine value = getValue(); if (value instanceof PMonitor) { throw new BugFoundException(String.format("Cannot fetch id from a PMonitor: %s", value)); } @@ -36,12 +50,12 @@ public int getId() { @Override public PMachineValue clone() { - return new PMachineValue(value); + return new PMachineValue(pid, name); } @Override protected String _asString() { - return value.toString(); + return name; } @Override @@ -50,6 +64,6 @@ public boolean equals(Object obj) { else if (!(obj instanceof PMachineValue)) { return false; } - return this.value.equals(((PMachineValue) obj).value); + return this.pid.equals(((PMachineValue) obj).pid); } } From c6d87ede98c5f0bb393984535efd6118c20d715b Mon Sep 17 00:00:00 2001 From: Aman Goel Date: Thu, 13 Jun 2024 21:14:11 +0000 Subject: [PATCH 03/31] Revert "[PEx] Major revamping of Choice: 1/n" This reverts commit 53c5d15e94f9bf42cb0f12e348de34a184744e9a. --- .../runtime/logger/PExplicitLogger.java | 27 +++--- .../runtime/logger/ScheduleWriter.java | 12 ++- .../pexplicit/runtime/machine/PMachineId.java | 17 ---- .../pexplicit/runtime/scheduler/Schedule.java | 86 ++++++++++--------- .../runtime/scheduler/Scheduler.java | 13 ++- .../runtime/scheduler/choice/Choice.java | 72 ++++++++++++++-- .../runtime/scheduler/choice/DataChoice.java | 46 +++++++--- .../scheduler/choice/DataSearchUnit.java | 48 ----------- .../scheduler/choice/ScheduleChoice.java | 50 ++++++++--- .../scheduler/choice/ScheduleSearchUnit.java | 52 ----------- .../runtime/scheduler/choice/SearchUnit.java | 74 ---------------- .../explicit/ExplicitSearchScheduler.java | 75 ++++++++-------- .../explicit/strategy/SearchTask.java | 36 ++++---- .../scheduler/replay/ReplayScheduler.java | 15 ++-- .../java/pexplicit/values/PMachineValue.java | 24 ++---- 15 files changed, 274 insertions(+), 373 deletions(-) delete mode 100644 Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/DataSearchUnit.java delete mode 100644 Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleSearchUnit.java delete mode 100644 Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/SearchUnit.java diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java index 9e4492119..7782909d6 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java @@ -13,11 +13,8 @@ import pexplicit.runtime.machine.PMonitor; import pexplicit.runtime.machine.State; import pexplicit.runtime.machine.events.PContinuation; -import pexplicit.runtime.scheduler.Schedule; -import pexplicit.runtime.scheduler.choice.DataChoice; +import pexplicit.runtime.scheduler.choice.Choice; import pexplicit.runtime.scheduler.choice.ScheduleChoice; -import pexplicit.runtime.scheduler.choice.ScheduleSearchUnit; -import pexplicit.runtime.scheduler.choice.SearchUnit; import pexplicit.runtime.scheduler.explicit.ExplicitSearchScheduler; import pexplicit.runtime.scheduler.explicit.SearchStatistics; import pexplicit.runtime.scheduler.explicit.StateCachingMode; @@ -210,48 +207,48 @@ public static void logFinishedIteration(int step) { /** * Log when backtracking to a new choice * - * @param searchUnit Choice to which backtracking to + * @param choice Choice to which backtracking to */ - public static void logBacktrack(SearchUnit searchUnit) { + public static void logBacktrack(Choice choice) { if (verbosity > 1) { log.info(String.format(" Backtracking to %s choice @%d::%d", - ((searchUnit instanceof ScheduleSearchUnit) ? "schedule" : "data"), - searchUnit.getStepNumber(), - searchUnit.getChoiceNumber())); + ((choice instanceof ScheduleChoice) ? "schedule" : "data"), + choice.getStepNumber(), + choice.getChoiceNumber())); } } - public static void logNewScheduleChoice(List choices, int step, int idx) { + public static void logNewScheduleChoice(List choices, int step, int idx) { if (verbosity > 1) { log.info(String.format(" @%d::%d new schedule choice: %s", step, idx, choices)); } } - public static void logNewDataChoice(List choices, int step, int idx) { + public static void logNewDataChoice(List> choices, int step, int idx) { if (verbosity > 1) { log.info(String.format(" @%d::%d new data choice: %s", step, idx, choices)); } } - public static void logRepeatScheduleChoice(ScheduleChoice choice, int step, int idx) { + public static void logRepeatScheduleChoice(PMachine choice, int step, int idx) { if (verbosity > 2) { log.info(String.format(" @%d::%d %s (repeat)", step, idx, choice)); } } - public static void logCurrentScheduleChoice(ScheduleChoice choice, int step, int idx) { + public static void logCurrentScheduleChoice(PMachine choice, int step, int idx) { if (verbosity > 2) { log.info(String.format(" @%d::%d %s", step, idx, choice)); } } - public static void logRepeatDataChoice(DataChoice choice, int step, int idx) { + public static void logRepeatDataChoice(PValue choice, int step, int idx) { if (verbosity > 2) { log.info(String.format(" @%d::%d %s (repeat)", step, idx, choice)); } } - public static void logCurrentDataChoice(DataChoice choice, int step, int idx) { + public static void logCurrentDataChoice(PValue choice, int step, int idx) { if (verbosity > 2) { log.info(String.format(" @%d::%d %s", step, idx, choice)); } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/ScheduleWriter.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/ScheduleWriter.java index f252cc5d1..548486ac5 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/ScheduleWriter.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/ScheduleWriter.java @@ -3,8 +3,6 @@ import lombok.Getter; import pexplicit.runtime.PExplicitGlobal; import pexplicit.runtime.machine.PMachine; -import pexplicit.runtime.scheduler.choice.DataChoice; -import pexplicit.runtime.scheduler.choice.ScheduleChoice; import pexplicit.utils.misc.Assert; import pexplicit.values.PBool; import pexplicit.values.PInt; @@ -44,12 +42,12 @@ private static void logComment(String message) { logIdx++; } - public static void logDataChoice(DataChoice choice) { + public static void logDataChoice(PValue choice) { Class type = choice.getClass(); - if (choice.getValue() instanceof PBool boolChoice) { + if (choice instanceof PBool boolChoice) { logComment("boolean choice"); log(boolChoice.getValue() ? "True" : "False"); - } else if (choice.getValue() instanceof PInt intChoice) { + } else if (choice instanceof PInt intChoice) { logComment("integer choice"); log(intChoice.toString()); } else { @@ -57,9 +55,9 @@ public static void logDataChoice(DataChoice choice) { } } - public static void logScheduleChoice(ScheduleChoice choice) { + public static void logScheduleChoice(PMachine choice) { logComment("schedule choice"); - log(String.format("(%d)", PExplicitGlobal.getGlobalMachine(choice.getValue()).getInstanceId())); + log(String.format("(%d)", choice.getInstanceId())); } public static void logHeader() { diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/PMachineId.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/PMachineId.java index baf9d3d95..569da384e 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/PMachineId.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/PMachineId.java @@ -1,7 +1,6 @@ package pexplicit.runtime.machine; import lombok.Getter; -import pexplicit.runtime.scheduler.choice.ScheduleChoice; @Getter public class PMachineId { @@ -12,20 +11,4 @@ public PMachineId(Class t, int tid) { type = t; typeId = tid; } - - @Override - public String toString() { - return String.format("%s<%d>", type.getSimpleName(), typeId); - } - - @Override - public boolean equals(Object obj) { - if (obj == this) - return true; - else if (!(obj instanceof PMachineId)) { - return false; - } - PMachineId rhs = (PMachineId) obj; - return this.type == rhs.type && this.typeId == rhs.typeId; - } } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Schedule.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Schedule.java index b9433ce39..1b4cf8633 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Schedule.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Schedule.java @@ -1,11 +1,13 @@ package pexplicit.runtime.scheduler; -import lombok.Data; import lombok.Getter; import lombok.Setter; import pexplicit.runtime.PExplicitGlobal; +import pexplicit.runtime.machine.PMachine; import pexplicit.runtime.machine.PMachineId; -import pexplicit.runtime.scheduler.choice.*; +import pexplicit.runtime.scheduler.choice.Choice; +import pexplicit.runtime.scheduler.choice.DataChoice; +import pexplicit.runtime.scheduler.choice.ScheduleChoice; import pexplicit.runtime.scheduler.explicit.StepState; import pexplicit.values.PValue; @@ -18,11 +20,11 @@ */ public class Schedule implements Serializable { /** - * List of current choices + * List of choices */ @Getter @Setter - private List searchUnits = new ArrayList<>(); + private List choices = new ArrayList<>(); /** * Step state at the start of a scheduler step. @@ -43,8 +45,8 @@ public Schedule() { * @param idx Choice depth * @return Choice at depth idx */ - public SearchUnit getChoice(int idx) { - return searchUnits.get(idx); + public Choice getChoice(int idx) { + return choices.get(idx); } /** @@ -53,8 +55,8 @@ public SearchUnit getChoice(int idx) { * @param idx Choice depth */ public void clearChoice(int idx) { - searchUnits.get(idx).clearCurrent(); - searchUnits.get(idx).clearUnexplored(); + choices.get(idx).clearCurrent(); + choices.get(idx).clearUnexplored(); } /** @@ -63,7 +65,7 @@ public void clearChoice(int idx) { * @param choiceNum Choice depth */ public void removeChoicesAfter(int choiceNum) { - searchUnits.subList(choiceNum + 1, searchUnits.size()).clear(); + choices.subList(choiceNum + 1, choices.size()).clear(); } /** @@ -73,7 +75,7 @@ public void removeChoicesAfter(int choiceNum) { */ public int getNumUnexploredChoices() { int numUnexplored = 0; - for (SearchUnit c : searchUnits) { + for (Choice c : choices) { numUnexplored += c.getUnexplored().size(); } return numUnexplored; @@ -86,8 +88,8 @@ public int getNumUnexploredChoices() { */ public int getNumUnexploredDataChoices() { int numUnexplored = 0; - for (SearchUnit c : searchUnits) { - if (c instanceof DataSearchUnit) { + for (Choice c : choices) { + if (c instanceof DataChoice) { numUnexplored += c.getUnexplored().size(); } } @@ -102,17 +104,17 @@ public int getNumUnexploredDataChoices() { * @param current Machine to set as current schedule choice * @param unexplored List of machine to set as unexplored schedule choices */ - public void setScheduleChoice(int stepNum, int choiceNum, ScheduleChoice current, List unexplored) { - if (choiceNum == searchUnits.size()) { - searchUnits.add(null); + public void setScheduleChoice(int stepNum, int choiceNum, PMachineId current, List unexplored) { + if (choiceNum == choices.size()) { + choices.add(null); } - assert (choiceNum < searchUnits.size()); + assert (choiceNum < choices.size()); if (PExplicitGlobal.getConfig().isStatefulBacktrackEnabled() && stepNum != 0) { assert (stepBeginState != null); - searchUnits.set(choiceNum, new ScheduleSearchUnit(stepNum, choiceNum, current, unexplored, stepBeginState)); + choices.set(choiceNum, new ScheduleChoice(stepNum, choiceNum, current, unexplored, stepBeginState)); } else { - searchUnits.set(choiceNum, new ScheduleSearchUnit(stepNum, choiceNum, current, unexplored, null)); + choices.set(choiceNum, new ScheduleChoice(stepNum, choiceNum, current, unexplored, null)); } } @@ -124,12 +126,12 @@ public void setScheduleChoice(int stepNum, int choiceNum, ScheduleChoice current * @param current PValue to set as current schedule choice * @param unexplored List of PValue to set as unexplored schedule choices */ - public void setDataChoice(int stepNum, int choiceNum, DataChoice current, List unexplored) { - if (choiceNum == searchUnits.size()) { - searchUnits.add(null); + public void setDataChoice(int stepNum, int choiceNum, PValue current, List> unexplored) { + if (choiceNum == choices.size()) { + choices.add(null); } - assert (choiceNum < searchUnits.size()); - searchUnits.set(choiceNum, new DataSearchUnit(stepNum, choiceNum, current, unexplored)); + assert (choiceNum < choices.size()); + choices.set(choiceNum, new DataChoice(stepNum, choiceNum, current, unexplored)); } /** @@ -138,9 +140,9 @@ public void setDataChoice(int stepNum, int choiceNum, DataChoice current, List getCurrentDataChoice(int idx) { + assert (choices.get(idx) instanceof DataChoice); + return ((DataChoice) choices.get(idx)).getCurrent(); } /** @@ -160,10 +162,10 @@ public DataChoice getCurrentDataChoice(int idx) { * @param idx Choice depth * @return List of machines, or null if index is invalid */ - public List getUnexploredScheduleChoices(int idx) { + public List getUnexploredScheduleChoices(int idx) { if (idx < size()) { - assert (searchUnits.get(idx) instanceof ScheduleSearchUnit); - return ((ScheduleSearchUnit) searchUnits.get(idx)).getUnexplored(); + assert (choices.get(idx) instanceof ScheduleChoice); + return ((ScheduleChoice) choices.get(idx)).getUnexplored(); } else { return new ArrayList<>(); } @@ -175,22 +177,22 @@ public List getUnexploredScheduleChoices(int idx) { * @param idx Choice depth * @return List of PValue, or null if index is invalid */ - public List getUnexploredDataChoices(int idx) { + public List> getUnexploredDataChoices(int idx) { if (idx < size()) { - assert (searchUnits.get(idx) instanceof DataSearchUnit); - return ((DataSearchUnit) searchUnits.get(idx)).getUnexplored(); + assert (choices.get(idx) instanceof DataChoice); + return ((DataChoice) choices.get(idx)).getUnexplored(); } else { return new ArrayList<>(); } } - public ScheduleSearchUnit getScheduleChoiceAt(SearchUnit searchUnit) { - if (searchUnit instanceof ScheduleSearchUnit scheduleChoice) { + public ScheduleChoice getScheduleChoiceAt(Choice choice) { + if (choice instanceof ScheduleChoice scheduleChoice) { return scheduleChoice; } else { - for (int i = searchUnit.getChoiceNumber() - 1; i >= 0; i--) { - SearchUnit c = searchUnits.get(i); - if (c instanceof ScheduleSearchUnit scheduleChoice) { + for (int i = choice.getChoiceNumber() - 1; i >= 0; i--) { + Choice c = choices.get(i); + if (c instanceof ScheduleChoice scheduleChoice) { return scheduleChoice; } } @@ -204,7 +206,7 @@ public ScheduleSearchUnit getScheduleChoiceAt(SearchUnit searchUnit) { * @param idx Choice depth */ public void clearCurrent(int idx) { - searchUnits.get(idx).clearCurrent(); + choices.get(idx).clearCurrent(); } /** @@ -213,6 +215,6 @@ public void clearCurrent(int idx) { * @return Number of choices in the schedule */ public int size() { - return searchUnits.size(); + return choices.size(); } } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Scheduler.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Scheduler.java index 9418088dd..163b0ef86 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Scheduler.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Scheduler.java @@ -7,7 +7,6 @@ import pexplicit.runtime.machine.PMachine; import pexplicit.runtime.machine.PMachineId; import pexplicit.runtime.machine.PMonitor; -import pexplicit.runtime.scheduler.choice.DataChoice; import pexplicit.runtime.scheduler.explicit.StepState; import pexplicit.utils.exceptions.BugFoundException; import pexplicit.utils.exceptions.NotImplementedException; @@ -119,7 +118,7 @@ protected void reset() { * * @return PValue as data choice */ - protected abstract PValue getNextDataChoice(List input_choices); + protected abstract PValue getNextDataChoice(List> input_choices); public void updateLogNumber() { stepNumLogs += 1; @@ -134,9 +133,9 @@ public void updateLogNumber() { * @return boolean data choice */ public PBool getRandomBool() { - List choices = new ArrayList<>(); - choices.add(new DataChoice(PBool.PTRUE)); - choices.add(new DataChoice(PBool.PFALSE)); + List> choices = new ArrayList<>(); + choices.add(PBool.PTRUE); + choices.add(PBool.PFALSE); return (PBool) getNextDataChoice(choices); } @@ -147,7 +146,7 @@ public PBool getRandomBool() { * @return integer data choice */ public PInt getRandomInt(PInt bound) { - List choices = new ArrayList<>(); + List> choices = new ArrayList<>(); int boundInt = bound.getValue(); if (boundInt > 10000) { throw new BugFoundException(String.format("choose expects a parameter with at most 10,000 choices, got %d choices instead.", boundInt)); @@ -156,7 +155,7 @@ public PInt getRandomInt(PInt bound) { boundInt = 1; } for (int i = 0; i < boundInt; i++) { - choices.add(new DataChoice(new PInt(i))); + choices.add(new PInt(i)); } return (PInt) getNextDataChoice(choices); } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/Choice.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/Choice.java index 71ac475fb..486fa7d9d 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/Choice.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/Choice.java @@ -1,14 +1,74 @@ package pexplicit.runtime.scheduler.choice; -import lombok.AllArgsConstructor; import lombok.Getter; import lombok.Setter; import java.io.Serializable; +import java.util.List; -@Getter -@Setter -@AllArgsConstructor +/** + * Represents a schedule or data choice + */ public abstract class Choice implements Serializable { - protected T value; -} + @Getter + @Setter + protected T current; + @Getter + @Setter + protected List unexplored; + + /** + * Step number + */ + @Getter + protected int stepNumber = 0; + /** + * Choice number + */ + @Getter + protected int choiceNumber = 0; + + protected Choice(T c, List u, int stepNum, int choiceNum) { + this.current = c; + this.unexplored = u; + this.stepNumber = stepNum; + this.choiceNumber = choiceNum; + } + + /** + * Check if this choice has an unexplored choice remaining. + * + * @return true if this choice has an unexplored choice, false otherwise + */ + public boolean isUnexploredNonEmpty() { + return !unexplored.isEmpty(); + } + + /** + * Clear current choices + */ + public void clearCurrent() { + this.current = null; + } + + /** + * Clean unexplored choices + */ + abstract public void clearUnexplored(); + + /** + * Copy current choice as a new Choice object + * + * @return Choice object with the copied current choice + */ + abstract public Choice copyCurrent(); + + /** + * Copy this choice to a new choice and clear any unexplored choices. + * + * @return New choice same as original choice + */ + abstract public Choice transferChoice(); + + abstract public String toString(); +} \ No newline at end of file diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/DataChoice.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/DataChoice.java index 684d89eb2..97b88519c 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/DataChoice.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/DataChoice.java @@ -1,24 +1,48 @@ package pexplicit.runtime.scheduler.choice; +import lombok.Getter; +import lombok.Setter; import pexplicit.values.PValue; +import java.util.ArrayList; +import java.util.List; + +@Getter +@Setter public class DataChoice extends Choice> { - public DataChoice(PValue value) { - super(value); + /** + * Constructor + */ + public DataChoice(int stepNum, int choiceNum, PValue c, List> u) { + super(c, u, stepNum, choiceNum); } - @Override - public String toString() { - return value.toString(); + /** + * Clean unexplored choices + */ + public void clearUnexplored() { + unexplored.clear(); + } + + public Choice copyCurrent() { + return new DataChoice(this.stepNumber, this.choiceNumber, this.current, new ArrayList<>()); + } + + public Choice transferChoice() { + DataChoice newChoice = new DataChoice(this.stepNumber, this.choiceNumber, this.current, this.unexplored); + this.unexplored = new ArrayList<>(); + return newChoice; } @Override - public boolean equals(Object obj) { - if (obj == this) - return true; - else if (!(obj instanceof DataChoice)) { - return false; + public String toString() { + StringBuilder sb = new StringBuilder(); + if (current != null) { + sb.append(String.format("curr:%s", current)); + } + if (unexplored != null && !unexplored.isEmpty()) { + sb.append(String.format(" rem:%s", unexplored)); } - return this.value == ((DataChoice) obj).value; + return sb.toString(); } } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/DataSearchUnit.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/DataSearchUnit.java deleted file mode 100644 index 66e253714..000000000 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/DataSearchUnit.java +++ /dev/null @@ -1,48 +0,0 @@ -package pexplicit.runtime.scheduler.choice; - -import lombok.Getter; -import lombok.Setter; -import pexplicit.values.PValue; - -import java.util.ArrayList; -import java.util.List; - -@Getter -@Setter -public class DataSearchUnit extends SearchUnit { - /** - * Constructor - */ - public DataSearchUnit(int stepNum, int choiceNum, DataChoice c, List u) { - super(c, u, stepNum, choiceNum); - } - - /** - * Clean unexplored choices - */ - public void clearUnexplored() { - unexplored.clear(); - } - - public SearchUnit copyCurrent() { - return new DataSearchUnit(this.stepNumber, this.choiceNumber, this.current, new ArrayList<>()); - } - - public SearchUnit transferChoice() { - DataSearchUnit newChoice = new DataSearchUnit(this.stepNumber, this.choiceNumber, this.current, this.unexplored); - this.unexplored = new ArrayList<>(); - return newChoice; - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - if (current != null) { - sb.append(String.format("curr:%s", current)); - } - if (unexplored != null && !unexplored.isEmpty()) { - sb.append(String.format(" rem:%s", unexplored)); - } - return sb.toString(); - } -} diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleChoice.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleChoice.java index 1741c5457..0bbc9b40c 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleChoice.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleChoice.java @@ -1,24 +1,52 @@ package pexplicit.runtime.scheduler.choice; +import lombok.Getter; +import lombok.Setter; import pexplicit.runtime.machine.PMachineId; +import pexplicit.runtime.scheduler.explicit.StepState; +import java.util.ArrayList; +import java.util.List; + +@Getter +@Setter public class ScheduleChoice extends Choice { - public ScheduleChoice(PMachineId value) { - super(value); + private StepState choiceState = null; + + /** + * Constructor + */ + public ScheduleChoice(int stepNum, int choiceNum, PMachineId c, List u, StepState s) { + super(c, u, stepNum, choiceNum); + this.choiceState = s; } - @Override - public String toString() { - return value.toString(); + /** + * Clean unexplored choices + */ + public void clearUnexplored() { + unexplored.clear(); + } + + public Choice copyCurrent() { + return new ScheduleChoice(this.stepNumber, this.choiceNumber, this.current, new ArrayList<>(), this.choiceState); + } + + public Choice transferChoice() { + ScheduleChoice newChoice = new ScheduleChoice(this.stepNumber, this.choiceNumber, this.current, this.unexplored, this.choiceState); + this.unexplored = new ArrayList<>(); + return newChoice; } @Override - public boolean equals(Object obj) { - if (obj == this) - return true; - else if (!(obj instanceof ScheduleChoice)) { - return false; + public String toString() { + StringBuilder sb = new StringBuilder(); + if (current != null) { + sb.append(String.format("curr@%s", current)); + } + if (unexplored != null && !unexplored.isEmpty()) { + sb.append(String.format(" rem@%s", unexplored)); } - return this.value == ((ScheduleChoice) obj).value; + return sb.toString(); } } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleSearchUnit.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleSearchUnit.java deleted file mode 100644 index 1d9dd5be9..000000000 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleSearchUnit.java +++ /dev/null @@ -1,52 +0,0 @@ -package pexplicit.runtime.scheduler.choice; - -import lombok.Getter; -import lombok.Setter; -import pexplicit.runtime.machine.PMachineId; -import pexplicit.runtime.scheduler.explicit.StepState; - -import java.util.ArrayList; -import java.util.List; - -@Getter -@Setter -public class ScheduleSearchUnit extends SearchUnit { - private StepState choiceState = null; - - /** - * Constructor - */ - public ScheduleSearchUnit(int stepNum, int choiceNum, ScheduleChoice c, List u, StepState s) { - super(c, u, stepNum, choiceNum); - this.choiceState = s; - } - - /** - * Clean unexplored choices - */ - public void clearUnexplored() { - unexplored.clear(); - } - - public SearchUnit copyCurrent() { - return new ScheduleSearchUnit(this.stepNumber, this.choiceNumber, this.current, new ArrayList<>(), this.choiceState); - } - - public SearchUnit transferChoice() { - ScheduleSearchUnit newChoice = new ScheduleSearchUnit(this.stepNumber, this.choiceNumber, this.current, this.unexplored, this.choiceState); - this.unexplored = new ArrayList<>(); - return newChoice; - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - if (current != null) { - sb.append(String.format("curr@%s", current)); - } - if (unexplored != null && !unexplored.isEmpty()) { - sb.append(String.format(" rem@%s", unexplored)); - } - return sb.toString(); - } -} diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/SearchUnit.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/SearchUnit.java deleted file mode 100644 index 1241af8eb..000000000 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/SearchUnit.java +++ /dev/null @@ -1,74 +0,0 @@ -package pexplicit.runtime.scheduler.choice; - -import lombok.Getter; -import lombok.Setter; - -import java.io.Serializable; -import java.util.List; - -/** - * Represents a schedule or data choice - */ -public abstract class SearchUnit implements Serializable { - @Getter - @Setter - protected T current; - @Getter - @Setter - protected List unexplored; - - /** - * Step number - */ - @Getter - protected int stepNumber = 0; - /** - * Choice number - */ - @Getter - protected int choiceNumber = 0; - - protected SearchUnit(T c, List u, int stepNum, int choiceNum) { - this.current = c; - this.unexplored = u; - this.stepNumber = stepNum; - this.choiceNumber = choiceNum; - } - - /** - * Check if this choice has an unexplored choice remaining. - * - * @return true if this choice has an unexplored choice, false otherwise - */ - public boolean isUnexploredNonEmpty() { - return !unexplored.isEmpty(); - } - - /** - * Clear current choices - */ - public void clearCurrent() { - this.current = null; - } - - /** - * Clean unexplored choices - */ - abstract public void clearUnexplored(); - - /** - * Copy current choice as a new Choice object - * - * @return Choice object with the copied current choice - */ - abstract public SearchUnit copyCurrent(); - - /** - * Copy this choice to a new choice and clear any unexplored choices. - * - * @return New choice same as original choice - */ - abstract public SearchUnit transferChoice(); - - abstract public String toString(); -} \ No newline at end of file diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java index 71fec4e98..72118b76c 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java @@ -12,10 +12,8 @@ import pexplicit.runtime.machine.PMachine; import pexplicit.runtime.machine.PMachineId; import pexplicit.runtime.scheduler.Scheduler; -import pexplicit.runtime.scheduler.choice.DataChoice; +import pexplicit.runtime.scheduler.choice.Choice; import pexplicit.runtime.scheduler.choice.ScheduleChoice; -import pexplicit.runtime.scheduler.choice.ScheduleSearchUnit; -import pexplicit.runtime.scheduler.choice.SearchUnit; import pexplicit.runtime.scheduler.explicit.strategy.*; import pexplicit.utils.exceptions.PExplicitRuntimeException; import pexplicit.utils.misc.Assert; @@ -300,20 +298,21 @@ protected void reset() { */ @Override public PMachine getNextScheduleChoice() { - ScheduleChoice result; + PMachine result; if (choiceNumber < backtrackChoiceNumber) { // pick the current schedule choice - result = schedule.getCurrentScheduleChoice(choiceNumber); + PMachineId pid = schedule.getCurrentScheduleChoice(choiceNumber); + result = PExplicitGlobal.getGlobalMachine(pid); PExplicitLogger.logRepeatScheduleChoice(result, stepNumber, choiceNumber); // increment choice number choiceNumber++; - return PExplicitGlobal.getGlobalMachine(result.getValue()); + return result; } // get existing unexplored choices, if any - List choices = schedule.getUnexploredScheduleChoices(choiceNumber); + List choices = schedule.getUnexploredScheduleChoices(choiceNumber); if (choices.isEmpty()) { // no existing unexplored choices, so try generating new choices @@ -333,18 +332,18 @@ public PMachine getNextScheduleChoice() { } // pick the first choice - result = choices.get(0); + result = PExplicitGlobal.getGlobalMachine(choices.get(0)); PExplicitLogger.logCurrentScheduleChoice(result, stepNumber, choiceNumber); // remove the first choice from unexplored choices choices.remove(0); // add choice to schedule - schedule.setScheduleChoice(stepNumber, choiceNumber, result, choices); + schedule.setScheduleChoice(stepNumber, choiceNumber, result.getPid(), choices); // increment choice number choiceNumber++; - return PExplicitGlobal.getGlobalMachine(result.getValue()); + return result; } /** @@ -353,8 +352,8 @@ public PMachine getNextScheduleChoice() { * @return PValue as data choice */ @Override - public PValue getNextDataChoice(List input_choices) { - DataChoice result; + public PValue getNextDataChoice(List> input_choices) { + PValue result; if (choiceNumber < backtrackChoiceNumber) { // pick the current data choice @@ -364,11 +363,11 @@ public PValue getNextDataChoice(List input_choices) { // increment choice number choiceNumber++; - return result.getValue(); + return result; } // get existing unexplored choices, if any - List choices = schedule.getUnexploredDataChoices(choiceNumber); + List> choices = schedule.getUnexploredDataChoices(choiceNumber); assert (input_choices.containsAll(choices)); if (choices.isEmpty()) { @@ -400,7 +399,7 @@ public PValue getNextDataChoice(List input_choices) { // increment choice number choiceNumber++; - return result.getValue(); + return result; } private void postProcessIteration() { @@ -418,15 +417,15 @@ private void addRemainingChoicesAsChildrenTasks() { SearchTask parentTask = searchStrategy.getCurrTask(); int numChildrenAdded = 0; for (int i = 0; i < schedule.size(); i++) { - SearchUnit searchUnit = schedule.getChoice(i); + Choice choice = schedule.getChoice(i); // if choice at this depth is non-empty - if (searchUnit.isUnexploredNonEmpty()) { + if (choice.isUnexploredNonEmpty()) { if (PExplicitGlobal.getConfig().getMaxChildrenPerTask() > 0 && numChildrenAdded == (PExplicitGlobal.getConfig().getMaxChildrenPerTask() - 1)) { - setChildTask(searchUnit, i, parentTask, false); + setChildTask(choice, i, parentTask, false); break; } // top search task should be always exact - setChildTask(searchUnit, i, parentTask, true); + setChildTask(choice, i, parentTask, true); numChildrenAdded++; } } @@ -440,14 +439,14 @@ private void endCurrTask() { searchStrategy.getFinishedTasks().add(currTask.getId()); } - private void setChildTask(SearchUnit searchUnit, int choiceNum, SearchTask parentTask, boolean isExact) { + private void setChildTask(Choice choice, int choiceNum, SearchTask parentTask, boolean isExact) { SearchTask newTask = searchStrategy.createTask(choiceNum, parentTask); for (int i = 0; i < choiceNum; i++) { newTask.addPrefixChoice(schedule.getChoice(i)); } - newTask.addSuffixChoice(searchUnit); + newTask.addSuffixChoice(choice); if (!isExact) { for (int i = choiceNum + 1; i < schedule.size(); i++) { @@ -468,7 +467,7 @@ public SearchTask setNextTask() { SearchTask nextTask = searchStrategy.setNextTask(); if (nextTask != null) { PExplicitLogger.logNextTask(nextTask); - schedule.setSearchUnits(nextTask.getAllChoices()); + schedule.setChoices(nextTask.getAllChoices()); postIterationCleanup(); } return nextTask; @@ -499,21 +498,21 @@ public double getUnexploredDataChoicesPercent() { private void postIterationCleanup() { for (int cIdx = schedule.size() - 1; cIdx >= 0; cIdx--) { - SearchUnit searchUnit = schedule.getChoice(cIdx); - if (searchUnit.isUnexploredNonEmpty()) { - PExplicitLogger.logBacktrack(searchUnit); + Choice choice = schedule.getChoice(cIdx); + if (choice.isUnexploredNonEmpty()) { + PExplicitLogger.logBacktrack(choice); backtrackChoiceNumber = cIdx; int newStepNumber = 0; - ScheduleSearchUnit scheduleChoice = null; + ScheduleChoice scheduleChoice = null; if (PExplicitGlobal.getConfig().isStatefulBacktrackEnabled()) { - scheduleChoice = schedule.getScheduleChoiceAt(searchUnit); + scheduleChoice = schedule.getScheduleChoiceAt(choice); if (scheduleChoice != null && scheduleChoice.getChoiceState() != null) { - assert ((scheduleChoice == searchUnit) || - (scheduleChoice.getStepNumber() == (searchUnit.getStepNumber() - 1)) || - (scheduleChoice.getStepNumber() == searchUnit.getStepNumber())); + assert ((scheduleChoice == choice) || + (scheduleChoice.getStepNumber() == (choice.getStepNumber() - 1)) || + (scheduleChoice.getStepNumber() == choice.getStepNumber())); newStepNumber = scheduleChoice.getStepNumber(); } else { - assert (searchUnit.getStepNumber() <= 1); + assert (choice.getStepNumber() <= 1); } } if (newStepNumber == 0) { @@ -524,9 +523,9 @@ private void postIterationCleanup() { choiceNumber = scheduleChoice.getChoiceNumber(); stepState.setTo(scheduleChoice.getChoiceState()); - assert (!PExplicitGlobal.getGlobalMachine(scheduleChoice.getCurrent().getValue()).getSendBuffer().isEmpty()); - for (ScheduleChoice sc : scheduleChoice.getUnexplored()) { - PMachine machine = PExplicitGlobal.getGlobalMachine(sc.getValue()); + assert (!PExplicitGlobal.getGlobalMachine(scheduleChoice.getCurrent()).getSendBuffer().isEmpty()); + for (PMachineId pid : scheduleChoice.getUnexplored()) { + PMachine machine = PExplicitGlobal.getGlobalMachine(pid); assert (!machine.getSendBuffer().isEmpty()); } } @@ -539,20 +538,20 @@ private void postIterationCleanup() { isDoneIterating = true; } - private List getNewScheduleChoices() { + private List getNewScheduleChoices() { // prioritize create machine events for (PMachine machine : stepState.getMachineSet()) { if (machine.getSendBuffer().nextIsCreateMachineMsg()) { - return new ArrayList<>(Collections.singletonList(new ScheduleChoice(machine.getPid()))); + return new ArrayList<>(Collections.singletonList(machine.getPid())); } } // now there are no create machine events remaining - List choices = new ArrayList<>(); + List choices = new ArrayList<>(); for (PMachine machine : stepState.getMachineSet()) { if (machine.getSendBuffer().nextHasTargetRunning()) { - choices.add(new ScheduleChoice(machine.getPid())); + choices.add(machine.getPid()); } } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchTask.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchTask.java index 4ebe6f43b..14c25960a 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchTask.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchTask.java @@ -1,9 +1,9 @@ package pexplicit.runtime.scheduler.explicit.strategy; import lombok.Getter; -import pexplicit.runtime.scheduler.choice.DataSearchUnit; -import pexplicit.runtime.scheduler.choice.SearchUnit; -import pexplicit.runtime.scheduler.choice.ScheduleSearchUnit; +import pexplicit.runtime.scheduler.choice.Choice; +import pexplicit.runtime.scheduler.choice.DataChoice; +import pexplicit.runtime.scheduler.choice.ScheduleChoice; import java.io.Serializable; import java.util.ArrayList; @@ -18,8 +18,8 @@ public class SearchTask implements Serializable { private final List children = new ArrayList<>(); @Getter private final int currChoiceNumber; - private final List prefixSearchUnits = new ArrayList<>(); - private final List suffixSearchUnits = new ArrayList<>(); + private final List prefixChoices = new ArrayList<>(); + private final List suffixChoices = new ArrayList<>(); @Getter private int numUnexploredScheduleChoices = 0; @Getter @@ -40,27 +40,27 @@ public void addChild(SearchTask task) { } public void cleanup() { - prefixSearchUnits.clear(); - suffixSearchUnits.clear(); + prefixChoices.clear(); + suffixChoices.clear(); } - public void addPrefixChoice(SearchUnit searchUnit) { - prefixSearchUnits.add(searchUnit.copyCurrent()); + public void addPrefixChoice(Choice choice) { + prefixChoices.add(choice.copyCurrent()); } - public void addSuffixChoice(SearchUnit searchUnit) { - if (searchUnit instanceof ScheduleSearchUnit scheduleChoice) { + public void addSuffixChoice(Choice choice) { + if (choice instanceof ScheduleChoice scheduleChoice) { numUnexploredScheduleChoices += scheduleChoice.getUnexplored().size(); } else { - numUnexploredDataChoices += ((DataSearchUnit) searchUnit).getUnexplored().size(); + numUnexploredDataChoices += ((DataChoice) choice).getUnexplored().size(); } - suffixSearchUnits.add(searchUnit.transferChoice()); + suffixChoices.add(choice.transferChoice()); } - public List getAllChoices() { - List result = new ArrayList<>(prefixSearchUnits); - result.addAll(suffixSearchUnits); - assert (result.size() == (currChoiceNumber + suffixSearchUnits.size())); + public List getAllChoices() { + List result = new ArrayList<>(prefixChoices); + result.addAll(suffixChoices); + assert (result.size() == (currChoiceNumber + suffixChoices.size())); return result; } @@ -89,7 +89,7 @@ public String toStringDetailed() { } return String.format("%s @%d::%d (parent: %s)", this, - suffixSearchUnits.get(0).getStepNumber(), + suffixChoices.get(0).getStepNumber(), currChoiceNumber, parentTask); } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/replay/ReplayScheduler.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/replay/ReplayScheduler.java index 82e5887a7..2b5b48764 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/replay/ReplayScheduler.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/replay/ReplayScheduler.java @@ -7,8 +7,6 @@ import pexplicit.runtime.machine.PMachineId; import pexplicit.runtime.scheduler.Schedule; import pexplicit.runtime.scheduler.Scheduler; -import pexplicit.runtime.scheduler.choice.DataChoice; -import pexplicit.runtime.scheduler.choice.ScheduleChoice; import pexplicit.utils.misc.Assert; import pexplicit.values.PValue; @@ -90,31 +88,32 @@ protected PMachine getNextScheduleChoice() { } // pick the current schedule choice - ScheduleChoice result = schedule.getCurrentScheduleChoice(choiceNumber); - if (result == null) { + PMachineId pid = schedule.getCurrentScheduleChoice(choiceNumber); + if (pid == null) { return null; } + PMachine result = PExplicitGlobal.getGlobalMachine(pid); ScheduleWriter.logScheduleChoice(result); PExplicitLogger.logRepeatScheduleChoice(result, stepNumber, choiceNumber); choiceNumber++; - return PExplicitGlobal.getGlobalMachine(result.getValue()); + return result; } @Override - protected PValue getNextDataChoice(List input_choices) { + protected PValue getNextDataChoice(List> input_choices) { if (choiceNumber >= schedule.size()) { return null; } // pick the current data choice - DataChoice result = schedule.getCurrentDataChoice(choiceNumber); + PValue result = schedule.getCurrentDataChoice(choiceNumber); assert (input_choices.contains(result)); ScheduleWriter.logDataChoice(result); PExplicitLogger.logRepeatDataChoice(result, stepNumber, choiceNumber); choiceNumber++; - return result.getValue(); + return result; } } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/values/PMachineValue.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/values/PMachineValue.java index ba172f741..8b39af167 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/values/PMachineValue.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/values/PMachineValue.java @@ -1,9 +1,7 @@ package pexplicit.values; import lombok.Getter; -import pexplicit.runtime.PExplicitGlobal; import pexplicit.runtime.machine.PMachine; -import pexplicit.runtime.machine.PMachineId; import pexplicit.runtime.machine.PMonitor; import pexplicit.utils.exceptions.BugFoundException; @@ -12,8 +10,7 @@ */ @Getter public class PMachineValue extends PValue { - private final PMachineId pid; - private final String name; + private final PMachine value; /** * Constructor @@ -21,27 +18,16 @@ public class PMachineValue extends PValue { * @param val machine value to set to */ public PMachineValue(PMachine val) { - pid = val.getPid(); - name = val.toString(); + value = val; initialize(); } - private PMachineValue(PMachineId inp_pid, String inp_name) { - pid = inp_pid; - name = inp_name; - } - - public PMachine getValue() { - return PExplicitGlobal.getGlobalMachine(pid); - } - /** * Get the unique machine identifier * * @return unique machine instance id */ public int getId() { - PMachine value = getValue(); if (value instanceof PMonitor) { throw new BugFoundException(String.format("Cannot fetch id from a PMonitor: %s", value)); } @@ -50,12 +36,12 @@ public int getId() { @Override public PMachineValue clone() { - return new PMachineValue(pid, name); + return new PMachineValue(value); } @Override protected String _asString() { - return name; + return value.toString(); } @Override @@ -64,6 +50,6 @@ public boolean equals(Object obj) { else if (!(obj instanceof PMachineValue)) { return false; } - return this.pid.equals(((PMachineValue) obj).pid); + return this.value.equals(((PMachineValue) obj).value); } } From 20f02163b23ed5f8e3777224f77a90848fe27292 Mon Sep 17 00:00:00 2001 From: Aman Goel Date: Thu, 13 Jun 2024 22:53:56 +0000 Subject: [PATCH 04/31] [PEx] Separates unexplored choices from schedule --- .../runtime/logger/PExplicitLogger.java | 15 +- .../pexplicit/runtime/machine/PMachine.java | 1 + .../pexplicit/runtime/machine/PMachineId.java | 22 +++ .../pexplicit/runtime/scheduler/Schedule.java | 86 ++-------- .../runtime/scheduler/choice/Choice.java | 28 +--- .../runtime/scheduler/choice/DataChoice.java | 25 +-- .../scheduler/choice/DataSearchUnit.java | 30 ++++ .../scheduler/choice/ScheduleChoice.java | 22 +-- .../scheduler/choice/ScheduleSearchUnit.java | 31 ++++ .../runtime/scheduler/choice/SearchUnit.java | 30 ++++ .../explicit/ExplicitSearchScheduler.java | 108 ++++++------ .../explicit/strategy/SearchTask.java | 155 +++++++++++++++--- 12 files changed, 331 insertions(+), 222 deletions(-) create mode 100644 Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/DataSearchUnit.java create mode 100644 Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleSearchUnit.java create mode 100644 Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/SearchUnit.java diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java index 7782909d6..f0dbf439c 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java @@ -15,6 +15,8 @@ import pexplicit.runtime.machine.events.PContinuation; import pexplicit.runtime.scheduler.choice.Choice; import pexplicit.runtime.scheduler.choice.ScheduleChoice; +import pexplicit.runtime.scheduler.choice.ScheduleSearchUnit; +import pexplicit.runtime.scheduler.choice.SearchUnit; import pexplicit.runtime.scheduler.explicit.ExplicitSearchScheduler; import pexplicit.runtime.scheduler.explicit.SearchStatistics; import pexplicit.runtime.scheduler.explicit.StateCachingMode; @@ -205,16 +207,17 @@ public static void logFinishedIteration(int step) { } /** - * Log when backtracking to a new choice + * Log when backtracking to a search unit * - * @param choice Choice to which backtracking to + * @param stepNum Step number + * @param choiceNum Choice number + * @param unit Search unit to which backtracking to */ - public static void logBacktrack(Choice choice) { + public static void logBacktrack(int stepNum, int choiceNum, SearchUnit unit) { if (verbosity > 1) { log.info(String.format(" Backtracking to %s choice @%d::%d", - ((choice instanceof ScheduleChoice) ? "schedule" : "data"), - choice.getStepNumber(), - choice.getChoiceNumber())); + ((unit instanceof ScheduleSearchUnit) ? "schedule" : "data"), + stepNum, choiceNum)); } } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/PMachine.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/PMachine.java index 0263f5d2e..dff212214 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/PMachine.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/PMachine.java @@ -73,6 +73,7 @@ public PMachine(String name, int id, State startState, State... states) { this.name = name; this.instanceId = ++globalMachineId; this.pid = new PMachineId(this.getClass(), id); + this.pid.setName(this.toString()); nameToMachine.put(toString(), this); // initialize states diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/PMachineId.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/PMachineId.java index 569da384e..d2005736d 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/PMachineId.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/PMachineId.java @@ -1,14 +1,36 @@ package pexplicit.runtime.machine; import lombok.Getter; +import lombok.Setter; @Getter public class PMachineId { Class type; int typeId; + @Setter + String name; public PMachineId(Class t, int tid) { type = t; typeId = tid; + name = null; } + + + @Override + public String toString() { + return name; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) + return true; + else if (!(obj instanceof PMachineId)) { + return false; + } + PMachineId rhs = (PMachineId) obj; + return this.type == rhs.type && this.typeId == rhs.typeId; + } + } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Schedule.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Schedule.java index 1b4cf8633..174acdd6d 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Schedule.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Schedule.java @@ -3,7 +3,6 @@ import lombok.Getter; import lombok.Setter; import pexplicit.runtime.PExplicitGlobal; -import pexplicit.runtime.machine.PMachine; import pexplicit.runtime.machine.PMachineId; import pexplicit.runtime.scheduler.choice.Choice; import pexplicit.runtime.scheduler.choice.DataChoice; @@ -56,7 +55,6 @@ public Choice getChoice(int idx) { */ public void clearChoice(int idx) { choices.get(idx).clearCurrent(); - choices.get(idx).clearUnexplored(); } /** @@ -68,43 +66,14 @@ public void removeChoicesAfter(int choiceNum) { choices.subList(choiceNum + 1, choices.size()).clear(); } - /** - * Get the number of unexplored choices in this schedule - * - * @return Number of unexplored choices - */ - public int getNumUnexploredChoices() { - int numUnexplored = 0; - for (Choice c : choices) { - numUnexplored += c.getUnexplored().size(); - } - return numUnexplored; - } - - /** - * Get the number of unexplored data choices in this schedule - * - * @return Number of unexplored data choices - */ - public int getNumUnexploredDataChoices() { - int numUnexplored = 0; - for (Choice c : choices) { - if (c instanceof DataChoice) { - numUnexplored += c.getUnexplored().size(); - } - } - return numUnexplored; - } - /** * Set the schedule choice at a choice depth. * * @param stepNum Step number * @param choiceNum Choice number * @param current Machine to set as current schedule choice - * @param unexplored List of machine to set as unexplored schedule choices */ - public void setScheduleChoice(int stepNum, int choiceNum, PMachineId current, List unexplored) { + public void setScheduleChoice(int stepNum, int choiceNum, PMachineId current) { if (choiceNum == choices.size()) { choices.add(null); } @@ -112,9 +81,9 @@ public void setScheduleChoice(int stepNum, int choiceNum, PMachineId current, Li if (PExplicitGlobal.getConfig().isStatefulBacktrackEnabled() && stepNum != 0) { assert (stepBeginState != null); - choices.set(choiceNum, new ScheduleChoice(stepNum, choiceNum, current, unexplored, stepBeginState)); + choices.set(choiceNum, new ScheduleChoice(stepNum, choiceNum, current, stepBeginState)); } else { - choices.set(choiceNum, new ScheduleChoice(stepNum, choiceNum, current, unexplored, null)); + choices.set(choiceNum, new ScheduleChoice(stepNum, choiceNum, current, null)); } } @@ -124,14 +93,13 @@ public void setScheduleChoice(int stepNum, int choiceNum, PMachineId current, Li * @param stepNum Step number * @param choiceNum Choice number * @param current PValue to set as current schedule choice - * @param unexplored List of PValue to set as unexplored schedule choices */ - public void setDataChoice(int stepNum, int choiceNum, PValue current, List> unexplored) { + public void setDataChoice(int stepNum, int choiceNum, PValue current) { if (choiceNum == choices.size()) { choices.add(null); } assert (choiceNum < choices.size()); - choices.set(choiceNum, new DataChoice(stepNum, choiceNum, current, unexplored)); + choices.set(choiceNum, new DataChoice(stepNum, choiceNum, current)); } /** @@ -156,45 +124,11 @@ public PValue getCurrentDataChoice(int idx) { return ((DataChoice) choices.get(idx)).getCurrent(); } - /** - * Get unexplored schedule choices at a choice depth. - * - * @param idx Choice depth - * @return List of machines, or null if index is invalid - */ - public List getUnexploredScheduleChoices(int idx) { - if (idx < size()) { - assert (choices.get(idx) instanceof ScheduleChoice); - return ((ScheduleChoice) choices.get(idx)).getUnexplored(); - } else { - return new ArrayList<>(); - } - } - - /** - * Get unexplored data choices at a choice depth. - * - * @param idx Choice depth - * @return List of PValue, or null if index is invalid - */ - public List> getUnexploredDataChoices(int idx) { - if (idx < size()) { - assert (choices.get(idx) instanceof DataChoice); - return ((DataChoice) choices.get(idx)).getUnexplored(); - } else { - return new ArrayList<>(); - } - } - - public ScheduleChoice getScheduleChoiceAt(Choice choice) { - if (choice instanceof ScheduleChoice scheduleChoice) { - return scheduleChoice; - } else { - for (int i = choice.getChoiceNumber() - 1; i >= 0; i--) { - Choice c = choices.get(i); - if (c instanceof ScheduleChoice scheduleChoice) { - return scheduleChoice; - } + public ScheduleChoice getScheduleChoiceAt(int choiceNum) { + for (int i = choiceNum; i >= 0; i--) { + Choice c = choices.get(i); + if (c instanceof ScheduleChoice scheduleChoice) { + return scheduleChoice; } } return null; diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/Choice.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/Choice.java index 486fa7d9d..e1770aae8 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/Choice.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/Choice.java @@ -4,7 +4,6 @@ import lombok.Setter; import java.io.Serializable; -import java.util.List; /** * Represents a schedule or data choice @@ -13,9 +12,6 @@ public abstract class Choice implements Serializable { @Getter @Setter protected T current; - @Getter - @Setter - protected List unexplored; /** * Step number @@ -28,22 +24,12 @@ public abstract class Choice implements Serializable { @Getter protected int choiceNumber = 0; - protected Choice(T c, List u, int stepNum, int choiceNum) { + protected Choice(T c, int stepNum, int choiceNum) { this.current = c; - this.unexplored = u; this.stepNumber = stepNum; this.choiceNumber = choiceNum; } - /** - * Check if this choice has an unexplored choice remaining. - * - * @return true if this choice has an unexplored choice, false otherwise - */ - public boolean isUnexploredNonEmpty() { - return !unexplored.isEmpty(); - } - /** * Clear current choices */ @@ -51,11 +37,6 @@ public void clearCurrent() { this.current = null; } - /** - * Clean unexplored choices - */ - abstract public void clearUnexplored(); - /** * Copy current choice as a new Choice object * @@ -63,12 +44,5 @@ public void clearCurrent() { */ abstract public Choice copyCurrent(); - /** - * Copy this choice to a new choice and clear any unexplored choices. - * - * @return New choice same as original choice - */ - abstract public Choice transferChoice(); - abstract public String toString(); } \ No newline at end of file diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/DataChoice.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/DataChoice.java index 97b88519c..71f74a97e 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/DataChoice.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/DataChoice.java @@ -4,34 +4,18 @@ import lombok.Setter; import pexplicit.values.PValue; -import java.util.ArrayList; -import java.util.List; - @Getter @Setter public class DataChoice extends Choice> { /** * Constructor */ - public DataChoice(int stepNum, int choiceNum, PValue c, List> u) { - super(c, u, stepNum, choiceNum); - } - - /** - * Clean unexplored choices - */ - public void clearUnexplored() { - unexplored.clear(); + public DataChoice(int stepNum, int choiceNum, PValue c) { + super(c, stepNum, choiceNum); } public Choice copyCurrent() { - return new DataChoice(this.stepNumber, this.choiceNumber, this.current, new ArrayList<>()); - } - - public Choice transferChoice() { - DataChoice newChoice = new DataChoice(this.stepNumber, this.choiceNumber, this.current, this.unexplored); - this.unexplored = new ArrayList<>(); - return newChoice; + return new DataChoice(this.stepNumber, this.choiceNumber, this.current); } @Override @@ -40,9 +24,6 @@ public String toString() { if (current != null) { sb.append(String.format("curr:%s", current)); } - if (unexplored != null && !unexplored.isEmpty()) { - sb.append(String.format(" rem:%s", unexplored)); - } return sb.toString(); } } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/DataSearchUnit.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/DataSearchUnit.java new file mode 100644 index 000000000..fa40a57a4 --- /dev/null +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/DataSearchUnit.java @@ -0,0 +1,30 @@ +package pexplicit.runtime.scheduler.choice; + +import pexplicit.values.PValue; + +import java.util.ArrayList; +import java.util.List; + +public class DataSearchUnit extends SearchUnit>{ + /** + * Constructor + */ + public DataSearchUnit(List> u) { + super(u); + } + + public SearchUnit transferUnit() { + SearchUnit newUnit = new DataSearchUnit(this.unexplored); + this.unexplored = new ArrayList<>(); + return newUnit; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + if (unexplored != null && !unexplored.isEmpty()) { + sb.append(String.format(" rem:%s", unexplored)); + } + return sb.toString(); + } +} diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleChoice.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleChoice.java index 0bbc9b40c..6d0f2ca74 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleChoice.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleChoice.java @@ -16,26 +16,13 @@ public class ScheduleChoice extends Choice { /** * Constructor */ - public ScheduleChoice(int stepNum, int choiceNum, PMachineId c, List u, StepState s) { - super(c, u, stepNum, choiceNum); + public ScheduleChoice(int stepNum, int choiceNum, PMachineId c, StepState s) { + super(c, stepNum, choiceNum); this.choiceState = s; } - /** - * Clean unexplored choices - */ - public void clearUnexplored() { - unexplored.clear(); - } - public Choice copyCurrent() { - return new ScheduleChoice(this.stepNumber, this.choiceNumber, this.current, new ArrayList<>(), this.choiceState); - } - - public Choice transferChoice() { - ScheduleChoice newChoice = new ScheduleChoice(this.stepNumber, this.choiceNumber, this.current, this.unexplored, this.choiceState); - this.unexplored = new ArrayList<>(); - return newChoice; + return new ScheduleChoice(this.stepNumber, this.choiceNumber, this.current, this.choiceState); } @Override @@ -44,9 +31,6 @@ public String toString() { if (current != null) { sb.append(String.format("curr@%s", current)); } - if (unexplored != null && !unexplored.isEmpty()) { - sb.append(String.format(" rem@%s", unexplored)); - } return sb.toString(); } } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleSearchUnit.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleSearchUnit.java new file mode 100644 index 000000000..69398f413 --- /dev/null +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleSearchUnit.java @@ -0,0 +1,31 @@ +package pexplicit.runtime.scheduler.choice; + +import pexplicit.runtime.machine.PMachineId; +import pexplicit.runtime.scheduler.explicit.StepState; + +import java.util.ArrayList; +import java.util.List; + +public class ScheduleSearchUnit extends SearchUnit { + /** + * Constructor + */ + public ScheduleSearchUnit(List u) { + super(u); + } + + public SearchUnit transferUnit() { + SearchUnit newUnit = new ScheduleSearchUnit(this.unexplored); + this.unexplored = new ArrayList<>(); + return newUnit; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + if (unexplored != null && !unexplored.isEmpty()) { + sb.append(String.format(" rem@%s", unexplored)); + } + return sb.toString(); + } +} diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/SearchUnit.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/SearchUnit.java new file mode 100644 index 000000000..713c1747e --- /dev/null +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/SearchUnit.java @@ -0,0 +1,30 @@ +package pexplicit.runtime.scheduler.choice; + +import lombok.Getter; +import lombok.Setter; + +import java.io.Serializable; +import java.util.List; + +/** + * Represents a schedule or data search unit + */ +public abstract class SearchUnit implements Serializable { + @Getter + @Setter + protected List unexplored; + + protected SearchUnit(List u) { + this.unexplored = u; + } + + /** + * Clean unexplored choices + */ + public void clearUnexplored() { + unexplored.clear(); + } + + public abstract SearchUnit transferUnit(); +} + diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java index 72118b76c..a92755884 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java @@ -14,6 +14,7 @@ import pexplicit.runtime.scheduler.Scheduler; import pexplicit.runtime.scheduler.choice.Choice; import pexplicit.runtime.scheduler.choice.ScheduleChoice; +import pexplicit.runtime.scheduler.choice.SearchUnit; import pexplicit.runtime.scheduler.explicit.strategy.*; import pexplicit.utils.exceptions.PExplicitRuntimeException; import pexplicit.utils.misc.Assert; @@ -312,7 +313,7 @@ public PMachine getNextScheduleChoice() { } // get existing unexplored choices, if any - List choices = schedule.getUnexploredScheduleChoices(choiceNumber); + List choices = searchStrategy.getCurrTask().getScheduleSearchUnit(choiceNumber); if (choices.isEmpty()) { // no existing unexplored choices, so try generating new choices @@ -339,7 +340,11 @@ public PMachine getNextScheduleChoice() { choices.remove(0); // add choice to schedule - schedule.setScheduleChoice(stepNumber, choiceNumber, result.getPid(), choices); + schedule.setScheduleChoice(stepNumber, choiceNumber, result.getPid()); + + // update search unit in search task + if (!choices.isEmpty()) + searchStrategy.getCurrTask().setScheduleSearchUnit(choiceNumber, choices); // increment choice number choiceNumber++; @@ -367,7 +372,7 @@ public PValue getNextDataChoice(List> input_choices) { } // get existing unexplored choices, if any - List> choices = schedule.getUnexploredDataChoices(choiceNumber); + List> choices = searchStrategy.getCurrTask().getDataSearchUnit(choiceNumber); assert (input_choices.containsAll(choices)); if (choices.isEmpty()) { @@ -395,7 +400,11 @@ public PValue getNextDataChoice(List> input_choices) { choices.remove(0); // add choice to schedule - schedule.setDataChoice(stepNumber, choiceNumber, result, choices); + schedule.setDataChoice(stepNumber, choiceNumber, result); + + // update search unit in search task + if (!choices.isEmpty()) + searchStrategy.getCurrTask().setDataSearchUnit(choiceNumber, choices); // increment choice number choiceNumber++; @@ -416,16 +425,16 @@ private void postProcessIteration() { private void addRemainingChoicesAsChildrenTasks() { SearchTask parentTask = searchStrategy.getCurrTask(); int numChildrenAdded = 0; - for (int i = 0; i < schedule.size(); i++) { - Choice choice = schedule.getChoice(i); - // if choice at this depth is non-empty - if (choice.isUnexploredNonEmpty()) { + for (int i: parentTask.getSearchUnitKeys(false)) { + SearchUnit unit = parentTask.getSearchUnit(i); + // if search unit at this depth is non-empty + if (!unit.getUnexplored().isEmpty()) { if (PExplicitGlobal.getConfig().getMaxChildrenPerTask() > 0 && numChildrenAdded == (PExplicitGlobal.getConfig().getMaxChildrenPerTask() - 1)) { - setChildTask(choice, i, parentTask, false); + setChildTask(unit, i, parentTask, false); break; } // top search task should be always exact - setChildTask(choice, i, parentTask, true); + setChildTask(unit, i, parentTask, true); numChildrenAdded++; } } @@ -439,18 +448,20 @@ private void endCurrTask() { searchStrategy.getFinishedTasks().add(currTask.getId()); } - private void setChildTask(Choice choice, int choiceNum, SearchTask parentTask, boolean isExact) { + private void setChildTask(SearchUnit unit, int choiceNum, SearchTask parentTask, boolean isExact) { SearchTask newTask = searchStrategy.createTask(choiceNum, parentTask); - for (int i = 0; i < choiceNum; i++) { + for (int i = 0; i <= choiceNum; i++) { newTask.addPrefixChoice(schedule.getChoice(i)); } - newTask.addSuffixChoice(choice); + newTask.addSuffixSearchUnit(choiceNum, unit); if (!isExact) { - for (int i = choiceNum + 1; i < schedule.size(); i++) { - newTask.addSuffixChoice(schedule.getChoice(i)); + for (int i: parentTask.getSearchUnitKeys(false)) { + if (i > choiceNum) { + newTask.addSuffixSearchUnit(i, parentTask.getSearchUnit(i)); + } } } @@ -467,18 +478,18 @@ public SearchTask setNextTask() { SearchTask nextTask = searchStrategy.setNextTask(); if (nextTask != null) { PExplicitLogger.logNextTask(nextTask); - schedule.setChoices(nextTask.getAllChoices()); + schedule.setChoices(nextTask.getPrefixChoices()); postIterationCleanup(); } return nextTask; } public int getNumUnexploredChoices() { - return schedule.getNumUnexploredChoices() + searchStrategy.getNumPendingChoices(); + return searchStrategy.getCurrTask().getNumUnexploredChoices() + searchStrategy.getNumPendingChoices(); } public int getNumUnexploredDataChoices() { - return schedule.getNumUnexploredDataChoices() + searchStrategy.getNumPendingDataChoices(); + return searchStrategy.getCurrTask().getNumUnexploredDataChoices() + searchStrategy.getNumPendingDataChoices(); } /** @@ -497,44 +508,37 @@ public double getUnexploredDataChoicesPercent() { } private void postIterationCleanup() { - for (int cIdx = schedule.size() - 1; cIdx >= 0; cIdx--) { - Choice choice = schedule.getChoice(cIdx); - if (choice.isUnexploredNonEmpty()) { - PExplicitLogger.logBacktrack(choice); - backtrackChoiceNumber = cIdx; - int newStepNumber = 0; - ScheduleChoice scheduleChoice = null; - if (PExplicitGlobal.getConfig().isStatefulBacktrackEnabled()) { - scheduleChoice = schedule.getScheduleChoiceAt(choice); - if (scheduleChoice != null && scheduleChoice.getChoiceState() != null) { - assert ((scheduleChoice == choice) || - (scheduleChoice.getStepNumber() == (choice.getStepNumber() - 1)) || - (scheduleChoice.getStepNumber() == choice.getStepNumber())); - newStepNumber = scheduleChoice.getStepNumber(); - } else { - assert (choice.getStepNumber() <= 1); - } - } - if (newStepNumber == 0) { - reset(); - stepState.resetToZero(); - } else { - stepNumber = newStepNumber; - choiceNumber = scheduleChoice.getChoiceNumber(); - stepState.setTo(scheduleChoice.getChoiceState()); - - assert (!PExplicitGlobal.getGlobalMachine(scheduleChoice.getCurrent()).getSendBuffer().isEmpty()); - for (PMachineId pid : scheduleChoice.getUnexplored()) { - PMachine machine = PExplicitGlobal.getGlobalMachine(pid); - assert (!machine.getSendBuffer().isEmpty()); - } + SearchTask task = searchStrategy.getCurrTask(); + for (int cIdx: task.getSearchUnitKeys(true)) { + SearchUnit unit = task.getSearchUnit(cIdx); + if (unit.getUnexplored().isEmpty()) { + task.clearSearchUnit(cIdx); + continue; + } + + backtrackChoiceNumber = cIdx; + int newStepNumber = 0; + ScheduleChoice scheduleChoice = null; + if (PExplicitGlobal.getConfig().isStatefulBacktrackEnabled()) { + scheduleChoice = schedule.getScheduleChoiceAt(cIdx); + if (scheduleChoice != null && scheduleChoice.getChoiceState() != null) { + newStepNumber = scheduleChoice.getStepNumber(); } - schedule.removeChoicesAfter(backtrackChoiceNumber); - return; + } + if (newStepNumber == 0) { + reset(); + stepState.resetToZero(); } else { - schedule.clearChoice(cIdx); + stepNumber = newStepNumber; + choiceNumber = scheduleChoice.getChoiceNumber(); + stepState.setTo(scheduleChoice.getChoiceState()); + assert (!PExplicitGlobal.getGlobalMachine(scheduleChoice.getCurrent()).getSendBuffer().isEmpty()); } + schedule.removeChoicesAfter(backtrackChoiceNumber); + PExplicitLogger.logBacktrack(newStepNumber, cIdx, unit); + return; } + schedule.removeChoicesAfter(0); isDoneIterating = true; } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchTask.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchTask.java index 14c25960a..ad81716de 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchTask.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchTask.java @@ -1,13 +1,13 @@ package pexplicit.runtime.scheduler.explicit.strategy; import lombok.Getter; -import pexplicit.runtime.scheduler.choice.Choice; -import pexplicit.runtime.scheduler.choice.DataChoice; -import pexplicit.runtime.scheduler.choice.ScheduleChoice; +import pexplicit.runtime.PExplicitGlobal; +import pexplicit.runtime.machine.PMachineId; +import pexplicit.runtime.scheduler.choice.*; +import pexplicit.values.PValue; import java.io.Serializable; -import java.util.ArrayList; -import java.util.List; +import java.util.*; public class SearchTask implements Serializable { @Getter @@ -18,8 +18,10 @@ public class SearchTask implements Serializable { private final List children = new ArrayList<>(); @Getter private final int currChoiceNumber; + @Getter private final List prefixChoices = new ArrayList<>(); - private final List suffixChoices = new ArrayList<>(); + @Getter + private final Map searchUnits = new HashMap<>(); @Getter private int numUnexploredScheduleChoices = 0; @Getter @@ -41,27 +43,20 @@ public void addChild(SearchTask task) { public void cleanup() { prefixChoices.clear(); - suffixChoices.clear(); + searchUnits.clear(); } public void addPrefixChoice(Choice choice) { prefixChoices.add(choice.copyCurrent()); } - public void addSuffixChoice(Choice choice) { - if (choice instanceof ScheduleChoice scheduleChoice) { - numUnexploredScheduleChoices += scheduleChoice.getUnexplored().size(); + public void addSuffixSearchUnit(int choiceNum, SearchUnit unit) { + if (unit instanceof ScheduleSearchUnit scheduleSearchUnit) { + numUnexploredScheduleChoices += scheduleSearchUnit.getUnexplored().size(); } else { - numUnexploredDataChoices += ((DataChoice) choice).getUnexplored().size(); + numUnexploredDataChoices += ((DataSearchUnit) unit).getUnexplored().size(); } - suffixChoices.add(choice.transferChoice()); - } - - public List getAllChoices() { - List result = new ArrayList<>(prefixChoices); - result.addAll(suffixChoices); - assert (result.size() == (currChoiceNumber + suffixChoices.size())); - return result; + searchUnits.put(choiceNum, unit.transferUnit()); } @Override @@ -89,8 +84,128 @@ public String toStringDetailed() { } return String.format("%s @%d::%d (parent: %s)", this, - suffixChoices.get(0).getStepNumber(), + prefixChoices.get(currChoiceNumber).getStepNumber(), currChoiceNumber, parentTask); } + + public List getSearchUnitKeys(boolean reversed) { + List keys = new ArrayList<>(searchUnits.keySet()); + if (reversed) + Collections.sort(keys, Collections.reverseOrder()); + else + Collections.sort(keys); + return keys; + } + + + /** + * Get the number of search units in the task + * + * @return Number of search units in the task + */ + public int size() { + return searchUnits.size(); + } + + /** + * Get the search unit at a choice depth + * + * @param idx Choice depth + * @return Search unit at depth idx + */ + public SearchUnit getSearchUnit(int idx) { + return searchUnits.get(idx); + } + + /** + * Get unexplored schedule choices at a choice depth. + * + * @param idx Choice depth + * @return List of machines, or null if index is invalid + */ + public List getScheduleSearchUnit(int idx) { + SearchUnit searchUnit = searchUnits.get(idx); + if (searchUnit != null) { + assert (searchUnit instanceof ScheduleSearchUnit); + return ((ScheduleSearchUnit) searchUnit).getUnexplored(); + } else { + return new ArrayList<>(); + } + } + + /** + * Get unexplored data choices at a choice depth. + * + * @param idx Choice depth + * @return List of PValue, or null if index is invalid + */ + public List> getDataSearchUnit(int idx) { + SearchUnit searchUnit = searchUnits.get(idx); + if (searchUnit != null) { + assert (searchUnit instanceof DataSearchUnit); + return ((DataSearchUnit) searchUnit).getUnexplored(); + } else { + return new ArrayList<>(); + } + } + + /** + * Set the schedule search unit at a choice depth. + * + * @param choiceNum Choice number + * @param unexplored List of machine to set as unexplored schedule choices + */ + public void setScheduleSearchUnit(int choiceNum, List unexplored) { + searchUnits.put(choiceNum, new ScheduleSearchUnit(unexplored)); + } + + /** + * Set the data search unit at a choice depth. + * + * @param choiceNum Choice number + * @param unexplored List of PValue to set as unexplored schedule choices + */ + public void setDataSearchUnit(int choiceNum, List> unexplored) { + searchUnits.put(choiceNum, new DataSearchUnit(unexplored)); + } + + /** + * Get the number of unexplored choices in this task + * + * @return Number of unexplored choices + */ + public int getNumUnexploredChoices() { + int numUnexplored = 0; + for (SearchUnit c : searchUnits.values()) { + numUnexplored += c.getUnexplored().size(); + } + return numUnexplored; + } + + /** + * Get the number of unexplored data choices in this task + * + * @return Number of unexplored data choices + */ + public int getNumUnexploredDataChoices() { + int numUnexplored = 0; + for (SearchUnit c : searchUnits.values()) { + if (c instanceof DataSearchUnit) { + numUnexplored += c.getUnexplored().size(); + } + } + return numUnexplored; + } + + + /** + * Clear search unit at a choice depth + * + * @param idx Choice depth + */ + public void clearSearchUnit(int idx) { + searchUnits.remove(idx); + } + } From 9c9e7d0b9a6dd17b44dfd8198ca518828a4be547 Mon Sep 17 00:00:00 2001 From: Aman Goel Date: Thu, 13 Jun 2024 23:39:04 +0000 Subject: [PATCH 05/31] [PEx] Cleanup and minor corrections to recent changes to SearchTask --- .../pexplicit/runtime/scheduler/Schedule.java | 11 +----- .../runtime/scheduler/choice/Choice.java | 15 +------- .../runtime/scheduler/choice/DataChoice.java | 6 ++-- .../scheduler/choice/ScheduleChoice.java | 19 ++++++++--- .../explicit/ExplicitSearchScheduler.java | 10 ++++-- .../explicit/strategy/SearchStrategy.java | 10 ++++-- .../explicit/strategy/SearchTask.java | 34 +++++++++---------- 7 files changed, 51 insertions(+), 54 deletions(-) diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Schedule.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Schedule.java index 174acdd6d..f9279e385 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Schedule.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Schedule.java @@ -48,15 +48,6 @@ public Choice getChoice(int idx) { return choices.get(idx); } - /** - * Clear choice at a choice depth - * - * @param idx Choice depth - */ - public void clearChoice(int idx) { - choices.get(idx).clearCurrent(); - } - /** * Remove choices after a choice depth * @@ -99,7 +90,7 @@ public void setDataChoice(int stepNum, int choiceNum, PValue current) { choices.add(null); } assert (choiceNum < choices.size()); - choices.set(choiceNum, new DataChoice(stepNum, choiceNum, current)); + choices.set(choiceNum, new DataChoice(current)); } /** diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/Choice.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/Choice.java index e1770aae8..00fd47431 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/Choice.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/Choice.java @@ -13,21 +13,8 @@ public abstract class Choice implements Serializable { @Setter protected T current; - /** - * Step number - */ - @Getter - protected int stepNumber = 0; - /** - * Choice number - */ - @Getter - protected int choiceNumber = 0; - - protected Choice(T c, int stepNum, int choiceNum) { + protected Choice(T c) { this.current = c; - this.stepNumber = stepNum; - this.choiceNumber = choiceNum; } /** diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/DataChoice.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/DataChoice.java index 71f74a97e..4f2dbe754 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/DataChoice.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/DataChoice.java @@ -10,12 +10,12 @@ public class DataChoice extends Choice> { /** * Constructor */ - public DataChoice(int stepNum, int choiceNum, PValue c) { - super(c, stepNum, choiceNum); + public DataChoice(PValue c) { + super(c); } public Choice copyCurrent() { - return new DataChoice(this.stepNumber, this.choiceNumber, this.current); + return new DataChoice(this.current); } @Override diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleChoice.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleChoice.java index 6d0f2ca74..8ad0a0147 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleChoice.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleChoice.java @@ -5,19 +5,30 @@ import pexplicit.runtime.machine.PMachineId; import pexplicit.runtime.scheduler.explicit.StepState; -import java.util.ArrayList; -import java.util.List; - @Getter @Setter public class ScheduleChoice extends Choice { + /** + * Step number + */ + private int stepNumber = 0; + /** + * Choice number + */ + private int choiceNumber = 0; + + /** + * Protocol state at the schedule step + */ private StepState choiceState = null; /** * Constructor */ public ScheduleChoice(int stepNum, int choiceNum, PMachineId c, StepState s) { - super(c, stepNum, choiceNum); + super(c); + this.stepNumber = stepNum; + this.choiceNumber = choiceNum; this.choiceState = s; } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java index a92755884..b8b5a22a6 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java @@ -343,8 +343,11 @@ public PMachine getNextScheduleChoice() { schedule.setScheduleChoice(stepNumber, choiceNumber, result.getPid()); // update search unit in search task - if (!choices.isEmpty()) + if (choices.isEmpty()) { + searchStrategy.getCurrTask().clearSearchUnit(choiceNumber); + } else { searchStrategy.getCurrTask().setScheduleSearchUnit(choiceNumber, choices); + } // increment choice number choiceNumber++; @@ -403,8 +406,11 @@ public PValue getNextDataChoice(List> input_choices) { schedule.setDataChoice(stepNumber, choiceNumber, result); // update search unit in search task - if (!choices.isEmpty()) + if (choices.isEmpty()) { + searchStrategy.getCurrTask().clearSearchUnit(choiceNumber); + } else { searchStrategy.getCurrTask().setDataSearchUnit(choiceNumber, choices); + } // increment choice number choiceNumber++; diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java index d49b39f04..614dc2b00 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java @@ -88,9 +88,11 @@ public SearchTask setNextTask() { */ public int getNumPendingChoices() { int numUnexplored = 0; + SearchTask task = getCurrTask(); + numUnexplored += task.getNumUnexploredChoices(); for (Integer tid : pendingTasks) { - SearchTask task = getTask(tid); - numUnexplored += task.getNumUnexploredScheduleChoices() + task.getNumUnexploredDataChoices(); + task = getTask(tid); + numUnexplored += task.getNumUnexploredChoices(); } return numUnexplored; } @@ -102,8 +104,10 @@ public int getNumPendingChoices() { */ public int getNumPendingDataChoices() { int numUnexplored = 0; + SearchTask task = getCurrTask(); + numUnexplored += task.getNumUnexploredDataChoices(); for (Integer tid : pendingTasks) { - SearchTask task = getTask(tid); + task = getTask(tid); numUnexplored += task.getNumUnexploredDataChoices(); } return numUnexplored; diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchTask.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchTask.java index ad81716de..9afb90e6c 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchTask.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchTask.java @@ -1,7 +1,6 @@ package pexplicit.runtime.scheduler.explicit.strategy; import lombok.Getter; -import pexplicit.runtime.PExplicitGlobal; import pexplicit.runtime.machine.PMachineId; import pexplicit.runtime.scheduler.choice.*; import pexplicit.values.PValue; @@ -22,10 +21,6 @@ public class SearchTask implements Serializable { private final List prefixChoices = new ArrayList<>(); @Getter private final Map searchUnits = new HashMap<>(); - @Getter - private int numUnexploredScheduleChoices = 0; - @Getter - private int numUnexploredDataChoices = 0; public SearchTask(int id, int choiceNum, SearchTask parentTask) { this.id = id; @@ -51,11 +46,6 @@ public void addPrefixChoice(Choice choice) { } public void addSuffixSearchUnit(int choiceNum, SearchUnit unit) { - if (unit instanceof ScheduleSearchUnit scheduleSearchUnit) { - numUnexploredScheduleChoices += scheduleSearchUnit.getUnexplored().size(); - } else { - numUnexploredDataChoices += ((DataSearchUnit) unit).getUnexplored().size(); - } searchUnits.put(choiceNum, unit.transferUnit()); } @@ -82,11 +72,19 @@ public String toStringDetailed() { if (isInitialTask()) { return String.format("%s @0::0 (parent: null)", this); } - return String.format("%s @%d::%d (parent: %s)", - this, - prefixChoices.get(currChoiceNumber).getStepNumber(), - currChoiceNumber, - parentTask); + Choice c = prefixChoices.get(currChoiceNumber); + if (c instanceof ScheduleChoice scheduleChoice) { + return String.format("%s @%d::%d (parent: %s)", + this, + scheduleChoice.getStepNumber(), + currChoiceNumber, + parentTask); + } else { + return String.format("%s -::%d (parent: %s)", + this, + currChoiceNumber, + parentTask); + } } public List getSearchUnitKeys(boolean reversed) { @@ -202,10 +200,10 @@ public int getNumUnexploredDataChoices() { /** * Clear search unit at a choice depth * - * @param idx Choice depth + * @param choiceNum Choice depth */ - public void clearSearchUnit(int idx) { - searchUnits.remove(idx); + public void clearSearchUnit(int choiceNum) { + searchUnits.remove(choiceNum); } } From d8d9e7ef94a47d01b88fd802b83465e45678ec46 Mon Sep 17 00:00:00 2001 From: Aman Goel Date: Thu, 13 Jun 2024 23:48:41 +0000 Subject: [PATCH 06/31] [PEx] Minor correction --- .../main/java/pexplicit/runtime/scheduler/Schedule.java | 8 +++----- .../scheduler/explicit/ExplicitSearchScheduler.java | 5 +++-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Schedule.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Schedule.java index f9279e385..76275c54c 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Schedule.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Schedule.java @@ -126,12 +126,10 @@ public ScheduleChoice getScheduleChoiceAt(int choiceNum) { } /** - * Clear current choices at a choice depth - * - * @param idx Choice depth + * Clear current choices */ - public void clearCurrent(int idx) { - choices.get(idx).clearCurrent(); + public void clear() { + choices.clear(); } /** diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java index b8b5a22a6..0067ffaea 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java @@ -534,17 +534,18 @@ private void postIterationCleanup() { if (newStepNumber == 0) { reset(); stepState.resetToZero(); + schedule.clear(); } else { stepNumber = newStepNumber; choiceNumber = scheduleChoice.getChoiceNumber(); stepState.setTo(scheduleChoice.getChoiceState()); assert (!PExplicitGlobal.getGlobalMachine(scheduleChoice.getCurrent()).getSendBuffer().isEmpty()); + schedule.removeChoicesAfter(backtrackChoiceNumber); } - schedule.removeChoicesAfter(backtrackChoiceNumber); PExplicitLogger.logBacktrack(newStepNumber, cIdx, unit); return; } - schedule.removeChoicesAfter(0); + schedule.clear(); isDoneIterating = true; } From 33c6ff381ff36a31435f74464d81181d1d76c88f Mon Sep 17 00:00:00 2001 From: Aman Goel Date: Fri, 14 Jun 2024 16:56:43 +0000 Subject: [PATCH 07/31] [PEx] Corrections to new backtracking logic --- .../pexplicit/runtime/scheduler/Schedule.java | 7 +++++- .../explicit/ExplicitSearchScheduler.java | 18 +++++++------ .../explicit/strategy/SearchStrategy.java | 6 ++--- .../explicit/strategy/SearchTask.java | 25 +++++++------------ 4 files changed, 28 insertions(+), 28 deletions(-) diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Schedule.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Schedule.java index 76275c54c..3591e725f 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Schedule.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Schedule.java @@ -54,7 +54,9 @@ public Choice getChoice(int idx) { * @param choiceNum Choice depth */ public void removeChoicesAfter(int choiceNum) { - choices.subList(choiceNum + 1, choices.size()).clear(); + if ((choiceNum + 1) < choices.size()) { + choices.subList(choiceNum + 1, choices.size()).clear(); + } } /** @@ -117,6 +119,9 @@ public PValue getCurrentDataChoice(int idx) { public ScheduleChoice getScheduleChoiceAt(int choiceNum) { for (int i = choiceNum; i >= 0; i--) { + if (choiceNum >= choices.size()) { + continue; + } Choice c = choices.get(i); if (c instanceof ScheduleChoice scheduleChoice) { return scheduleChoice; diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java index 0067ffaea..c233ca8d8 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java @@ -455,26 +455,29 @@ private void endCurrTask() { } private void setChildTask(SearchUnit unit, int choiceNum, SearchTask parentTask, boolean isExact) { - SearchTask newTask = searchStrategy.createTask(choiceNum, parentTask); + SearchTask newTask = searchStrategy.createTask(parentTask); - for (int i = 0; i <= choiceNum; i++) { - newTask.addPrefixChoice(schedule.getChoice(i)); - } + int maxChoiceNum = choiceNum; newTask.addSuffixSearchUnit(choiceNum, unit); if (!isExact) { for (int i: parentTask.getSearchUnitKeys(false)) { if (i > choiceNum) { + if (i > maxChoiceNum) { + maxChoiceNum = i; + } newTask.addSuffixSearchUnit(i, parentTask.getSearchUnit(i)); } } } + for (int i = 0; i <= maxChoiceNum; i++) { + newTask.addPrefixChoice(schedule.getChoice(i)); + } + parentTask.addChild(newTask); searchStrategy.addNewTask(newTask); - - assert (choiceNum >= parentTask.getCurrChoiceNumber()); } /** @@ -534,14 +537,13 @@ private void postIterationCleanup() { if (newStepNumber == 0) { reset(); stepState.resetToZero(); - schedule.clear(); } else { stepNumber = newStepNumber; choiceNumber = scheduleChoice.getChoiceNumber(); stepState.setTo(scheduleChoice.getChoiceState()); assert (!PExplicitGlobal.getGlobalMachine(scheduleChoice.getCurrent()).getSendBuffer().isEmpty()); - schedule.removeChoicesAfter(backtrackChoiceNumber); } + schedule.removeChoicesAfter(backtrackChoiceNumber); PExplicitLogger.logBacktrack(newStepNumber, cIdx, unit); return; } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java index 614dc2b00..c4f6cf407 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java @@ -32,8 +32,8 @@ public abstract class SearchStrategy implements Serializable { */ int currTaskStartIteration = 0; - public SearchTask createTask(int choiceNum, SearchTask parentTask) { - SearchTask newTask = new SearchTask(allTasks.size(), choiceNum, parentTask); + public SearchTask createTask(SearchTask parentTask) { + SearchTask newTask = new SearchTask(allTasks.size(), parentTask); allTasks.add(newTask); pendingTasks.add(newTask.getId()); return newTask; @@ -41,7 +41,7 @@ public SearchTask createTask(int choiceNum, SearchTask parentTask) { public void createFirstTask() { assert (allTasks.size() == 0); - SearchTask firstTask = createTask(0, null); + SearchTask firstTask = createTask(null); setCurrTask(firstTask); } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchTask.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchTask.java index 9afb90e6c..597b116bc 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchTask.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchTask.java @@ -16,15 +16,14 @@ public class SearchTask implements Serializable { @Getter private final List children = new ArrayList<>(); @Getter - private final int currChoiceNumber; + private int currChoiceNumber = 0; @Getter private final List prefixChoices = new ArrayList<>(); @Getter private final Map searchUnits = new HashMap<>(); - public SearchTask(int id, int choiceNum, SearchTask parentTask) { + public SearchTask(int id, SearchTask parentTask) { this.id = id; - this.currChoiceNumber = choiceNum; this.parentTask = parentTask; } @@ -47,6 +46,9 @@ public void addPrefixChoice(Choice choice) { public void addSuffixSearchUnit(int choiceNum, SearchUnit unit) { searchUnits.put(choiceNum, unit.transferUnit()); + if (choiceNum > currChoiceNumber) { + currChoiceNumber = choiceNum; + } } @Override @@ -72,19 +74,10 @@ public String toStringDetailed() { if (isInitialTask()) { return String.format("%s @0::0 (parent: null)", this); } - Choice c = prefixChoices.get(currChoiceNumber); - if (c instanceof ScheduleChoice scheduleChoice) { - return String.format("%s @%d::%d (parent: %s)", - this, - scheduleChoice.getStepNumber(), - currChoiceNumber, - parentTask); - } else { - return String.format("%s -::%d (parent: %s)", - this, - currChoiceNumber, - parentTask); - } + return String.format("%s ?::%d (parent: %s)", + this, + currChoiceNumber, + parentTask); } public List getSearchUnitKeys(boolean reversed) { From ef2f17c1bd60155e39f17ce17931aabffd979613 Mon Sep 17 00:00:00 2001 From: Ashish Kumar Date: Thu, 20 Jun 2024 16:09:17 -0700 Subject: [PATCH 08/31] Ongoing Changes --- .../main/java/pexplicit/RuntimeExecutor.java | 78 ++++++++++++++----- .../pexplicit/runtime/PExplicitGlobal.java | 44 +++++++++-- .../runtime/logger/PExplicitLogger.java | 30 ++++--- .../pexplicit/runtime/machine/PMachine.java | 10 +-- .../explicit/strategy/SearchStrategy.java | 2 +- .../pexplicit/utils/monitor/TimedCall.java | 14 +++- benchmarksRuns.sh | 8 ++ 7 files changed, 143 insertions(+), 43 deletions(-) create mode 100755 benchmarksRuns.sh diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java index 3c5e3ecdb..a356a18b5 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java @@ -11,18 +11,22 @@ import pexplicit.utils.monitor.MemoryMonitor; import pexplicit.utils.monitor.TimeMonitor; import pexplicit.utils.monitor.TimedCall; +import pexplicit.runtime.scheduler.Scheduler; import java.time.Duration; import java.time.Instant; +import java.util.ArrayList; import java.util.concurrent.*; +import pexplicit.commandline.PExplicitConfig; + /** * Represents the runtime executor that executes the analysis engine */ public class RuntimeExecutor { private static ExecutorService executor; - private static Future future; - private static ExplicitSearchScheduler scheduler; + private static ArrayList> futures = new ArrayList<>(); + private static ArrayList schedulers = new ArrayList<>(); private static void runWithTimeout(long timeLimit) throws TimeoutException, @@ -30,9 +34,15 @@ private static void runWithTimeout(long timeLimit) RuntimeException { try { if (timeLimit > 0) { - future.get(timeLimit, TimeUnit.SECONDS); + for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) { + Future future = futures.get(i); + future.get(timeLimit, TimeUnit.SECONDS); + } } else { - future.get(); + for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) { + Future future = futures.get(i); + future.get(); + } } } catch (TimeoutException | BugFoundException e) { throw e; @@ -55,7 +65,11 @@ private static void runWithTimeout(long timeLimit) private static void printStats() { double searchTime = TimeMonitor.stopInterval(); - scheduler.recordStats(); + for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++){ + ExplicitSearchScheduler scheduler = schedulers.get(i); + scheduler.recordStats(); + } + if (PExplicitGlobal.getResult().equals("correct for any depth")) { PExplicitGlobal.setStatus(STATUS.VERIFIED); } else if (PExplicitGlobal.getResult().startsWith("correct up to step")) { @@ -69,9 +83,9 @@ private static void preprocess() { PExplicitLogger.logInfo(String.format("... Checker is using '%s' strategy (seed:%s)", PExplicitGlobal.getConfig().getSearchStrategyMode(), PExplicitGlobal.getConfig().getRandomSeed())); - executor = Executors.newSingleThreadExecutor(); + executor = Executors.newFixedThreadPool(PExplicitGlobal.getMaxThreads()); - PExplicitGlobal.setResult("error"); + // PExplicitGlobal.setResult("error"); double preSearchTime = TimeMonitor.findInterval(TimeMonitor.getStart()); @@ -84,8 +98,12 @@ private static void preprocess() { private static void process(boolean resume) throws Exception { try { - TimedCall timedCall = new TimedCall(scheduler, resume); - future = executor.submit(timedCall); + ArrayList timedCalls = new ArrayList<>(); + for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) { + timedCalls.add( new TimedCall(schedulers.get(i), resume, i)); + futures.add(executor.submit(timedCalls.get(i))); + } + TimeMonitor.startInterval(); runWithTimeout((long) PExplicitGlobal.getConfig().getTimeLimit()); } catch (TimeoutException e) { @@ -96,13 +114,26 @@ private static void process(boolean resume) throws Exception { throw new Exception("MEMOUT", e); } catch (BugFoundException e) { PExplicitGlobal.setStatus(STATUS.BUG_FOUND); - PExplicitGlobal.setResult(String.format("found cex of length %d", scheduler.getStepNumber())); + + + + // for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) + // PExplicitGlobal.setResult(String.format("found cex of length %d", (schedulers.get(i)).getStepNumber())); + // Terminate all schedulers at this point, and that scheduler which found this exception stores result. + PExplicitLogger.logStackTrace(e); - ReplayScheduler replayer = new ReplayScheduler(scheduler.schedule); - PExplicitGlobal.setScheduler(replayer); + ArrayList replayers = new ArrayList<>(); + for (int i = 0; i < PExplicitGlobal.getMaxThreads() ; i++) + replayers.add(new ReplayScheduler((schedulers.get(i)).schedule)); + + ArrayList localSchedulers = PExplicitGlobal.getSchedulers(); + for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) + localSchedulers.set(i,replayers.get(i)); + try { - replayer.run(); + for (int i = 0; i < PExplicitGlobal.getMaxThreads() ; i++) + (replayers.get(i)).run(); } catch (NullPointerException | StackOverflowError | ClassCastException replayException) { PExplicitLogger.logStackTrace((Exception) replayException); throw new BugFoundException(replayException.getMessage(), replayException); @@ -121,17 +152,28 @@ private static void process(boolean resume) throws Exception { PExplicitGlobal.setStatus(STATUS.ERROR); throw new Exception("ERROR", e); } finally { - future.cancel(true); + for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) { + Future future = futures.get(i); + future.cancel(true); + } executor.shutdownNow(); - scheduler.updateResult(); + for (int i = 0; i < PExplicitGlobal.getMaxThreads() ; i++) + (schedulers.get(i)).updateResult(); printStats(); - PExplicitLogger.logEndOfRun(scheduler, Duration.between(TimeMonitor.getStart(), Instant.now()).getSeconds()); + for (int i = 0; i < PExplicitGlobal.getMaxThreads() ; i++) + PExplicitLogger.logEndOfRun(schedulers.get(i), Duration.between(TimeMonitor.getStart(), Instant.now()).getSeconds()); } } public static void run() throws Exception { - scheduler = new ExplicitSearchScheduler(); - PExplicitGlobal.setScheduler(scheduler); + ArrayList localSchedulers = PExplicitGlobal.getSchedulers(); + for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) { + ExplicitSearchScheduler localCopy = new ExplicitSearchScheduler(); + schedulers.add(localCopy); + localSchedulers.add(localCopy); + } + + // PExplicitLogger.logInfo("Hello, World!"); preprocess(); process(false); diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java index d6e71fe60..ea43ca05a 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java @@ -9,15 +9,31 @@ import pexplicit.runtime.scheduler.explicit.strategy.SearchStrategyMode; import java.util.*; - /** * Represents global data structures represented with a singleton class */ public class PExplicitGlobal { + + // @Getter + // @Setter + // private static Map< > + + @Getter + private static final int maxThreads = 2; + + @Getter + private static Map tID_to_localtID = new HashMap<>(); + + + // Method to add to tID_to_localtID + public static void addTotIDtolocaltID(long tID, Integer localtID) { + tID_to_localtID.put(tID, localtID); + } + /** * Mapping from machine type to list of all machine instances - */ - @Getter + */ + @Getter private static final Map, List> machineListByType = new HashMap<>(); /** * Set of machines @@ -39,9 +55,20 @@ public class PExplicitGlobal { /** * Scheduler **/ + // @Getter + // @Setter + // private static Scheduler scheduler = null; // Remove this! + @Getter @Setter - private static Scheduler scheduler = null; + private static ArrayList schedulers = new ArrayList<>(); + + + public static Scheduler getScheduler() { + int localtID = tID_to_localtID.get(Thread.currentThread().getId()); + return schedulers.get(localtID); + } + @Getter @Setter private static SearchStrategyMode searchStrategyMode; @@ -50,13 +77,14 @@ public class PExplicitGlobal { **/ @Getter @Setter - private static STATUS status = STATUS.INCOMPLETE; + private static STATUS status = STATUS.INCOMPLETE; + /** - * Result of the run + * Results of the run **/ @Getter - @Setter - private static String result = "error"; + @Setter + private static String result = null; /** * Get a machine of a given type and index if exists, else return null. diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java index f0dbf439c..a4ebc52e8 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java @@ -267,7 +267,8 @@ public static void logNewState(int step, int idx, Object stateKey, SortedSet machineType, - PValue payload, - Function constructor) { - PMachine machine = PExplicitGlobal.getScheduler().allocateMachine(machineType, constructor); + Class machineType, PValue payload, Function constructor) { + int localtID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); + PMachine machine = ((PExplicitGlobal.getSchedulers()).get(localtID)).allocateMachine(machineType, constructor); PMessage msg = new PMessage(PEvent.createMachine, machine, payload); sendBuffer.add(msg); return new PMachineValue(machine); @@ -288,7 +287,8 @@ public void sendEvent(PMachineValue target, PEvent event, PValue payload) { PExplicitLogger.logSendEvent(this, msg); sendBuffer.add(msg); - PExplicitGlobal.getScheduler().runMonitors(msg); + int localtID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); + ((PExplicitGlobal.getSchedulers()).get(localtID)).runMonitors(msg); } /** diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java index c4f6cf407..d3d7806c6 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java @@ -13,7 +13,7 @@ public abstract class SearchStrategy implements Serializable { /** * List of all search tasks - */ + */ // Make 3 static final List allTasks = new ArrayList<>(); /** * Set of all search tasks that are pending diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/monitor/TimedCall.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/monitor/TimedCall.java index 53133fe89..a2202548f 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/monitor/TimedCall.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/monitor/TimedCall.java @@ -1,5 +1,6 @@ package pexplicit.utils.monitor; +import pexplicit.runtime.PExplicitGlobal; import pexplicit.runtime.scheduler.Scheduler; import pexplicit.utils.exceptions.BugFoundException; import pexplicit.utils.exceptions.MemoutException; @@ -7,11 +8,22 @@ import java.util.concurrent.Callable; import java.util.concurrent.TimeoutException; +import javax.annotation.concurrent.ThreadSafe; + +import lombok.Getter; +import lombok.Setter; + public class TimedCall implements Callable { private final Scheduler scheduler; - public TimedCall(Scheduler scheduler, boolean resume) { + @Getter + @Setter + private long threadId; + + public TimedCall(Scheduler scheduler, boolean resume, int localtID) { this.scheduler = scheduler; + this.threadId = Thread.currentThread().getId(); + PExplicitGlobal.addTotIDtolocaltID(this.threadId, localtID); } @Override diff --git a/benchmarksRuns.sh b/benchmarksRuns.sh new file mode 100755 index 000000000..a42923d26 --- /dev/null +++ b/benchmarksRuns.sh @@ -0,0 +1,8 @@ +# Go to /Users/xashisk/ashish-ws/SyncedForkedRepo/P +cd ./Src/PRuntimes/PExplicitRuntime/ +./scripts/build.sh +echo "-------------------Build Over---------------------------" +cd - +cd ../../scriptsRepo/src/P-Evaluation-Tests/ +./scripts/run_pexplicitzshrc.sh ../../../SyncedForkedRepo/P/Tutorial/1_ClientServer test -tc tcMultipleClients --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 +cd - \ No newline at end of file From 673df136973114516f1a9959e02eb75536955141 Mon Sep 17 00:00:00 2001 From: Ashish Kumar Date: Mon, 24 Jun 2024 11:24:48 -0700 Subject: [PATCH 09/31] Week 6 tasks --- .../src/main/java/pexplicit/RuntimeExecutor.java | 1 - 1 file changed, 1 deletion(-) diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java index a356a18b5..1621f698c 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java @@ -103,7 +103,6 @@ private static void process(boolean resume) throws Exception { timedCalls.add( new TimedCall(schedulers.get(i), resume, i)); futures.add(executor.submit(timedCalls.get(i))); } - TimeMonitor.startInterval(); runWithTimeout((long) PExplicitGlobal.getConfig().getTimeLimit()); } catch (TimeoutException e) { From b66d1f68e56ba934bb6d7fdb6bfa142b829cbac7 Mon Sep 17 00:00:00 2001 From: Ashish Kumar Date: Wed, 26 Jun 2024 10:39:16 -0700 Subject: [PATCH 10/31] discussed changes --- .../main/java/pexplicit/RuntimeExecutor.java | 53 ++++++++++++++--- .../pexplicit/runtime/PExplicitGlobal.java | 53 ++++++++++++++--- .../runtime/scheduler/Scheduler.java | 9 +++ .../explicit/ExplicitSearchScheduler.java | 57 +++++++++++++------ .../explicit/strategy/SearchStrategy.java | 34 ++++++++--- .../strategy/SearchStrategyRandom.java | 8 +++ .../scheduler/replay/ReplayScheduler.java | 6 ++ .../pexplicit/utils/monitor/TimedCall.java | 5 +- benchmarksRuns.sh | 1 + 9 files changed, 187 insertions(+), 39 deletions(-) diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java index 1621f698c..85328bea4 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java @@ -5,6 +5,7 @@ import pexplicit.runtime.logger.PExplicitLogger; import pexplicit.runtime.logger.StatWriter; import pexplicit.runtime.scheduler.explicit.ExplicitSearchScheduler; +import pexplicit.runtime.scheduler.explicit.strategy.SearchStrategy; import pexplicit.runtime.scheduler.replay.ReplayScheduler; import pexplicit.utils.exceptions.BugFoundException; import pexplicit.utils.exceptions.MemoutException; @@ -24,7 +25,7 @@ * Represents the runtime executor that executes the analysis engine */ public class RuntimeExecutor { - private static ExecutorService executor; + private static ThreadPoolExecutor executor; private static ArrayList> futures = new ArrayList<>(); private static ArrayList schedulers = new ArrayList<>(); @@ -32,13 +33,21 @@ private static void runWithTimeout(long timeLimit) throws TimeoutException, InterruptedException, RuntimeException { - try { + try { // PIN: If thread gets exception, need to kill the other threads. if (timeLimit > 0) { - for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) { - Future future = futures.get(i); + // PExplicitLogger.logInfo("Check0.1.3.1"); + + for (Future future: futures) { + // Future future = futures.get(i); future.get(timeLimit, TimeUnit.SECONDS); } + + // PExplicitLogger.logInfo("Check0.1.3.2"); + + + } else { + for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) { Future future = futures.get(i); future.get(); @@ -83,7 +92,7 @@ private static void preprocess() { PExplicitLogger.logInfo(String.format("... Checker is using '%s' strategy (seed:%s)", PExplicitGlobal.getConfig().getSearchStrategyMode(), PExplicitGlobal.getConfig().getRandomSeed())); - executor = Executors.newFixedThreadPool(PExplicitGlobal.getMaxThreads()); + executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(PExplicitGlobal.getMaxThreads()); // PExplicitGlobal.setResult("error"); @@ -99,12 +108,36 @@ private static void preprocess() { private static void process(boolean resume) throws Exception { try { ArrayList timedCalls = new ArrayList<>(); + + // PExplicitLogger.logInfo("Check0.1.1"); + + SearchStrategy.createFirstTask(); + for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) { timedCalls.add( new TimedCall(schedulers.get(i), resume, i)); - futures.add(executor.submit(timedCalls.get(i))); } + + for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) { + Future future = executor.submit(timedCalls.get(i)); + futures.add(future); + } + + // Thread.sleep(1000); + + // Get the number of pending tasks + int pendingTasks = executor.getQueue().size(); + PExplicitLogger.logInfo("Number of pending tasks: " + pendingTasks); + + + // PExplicitLogger.logInfo("Check0.1.2"); + TimeMonitor.startInterval(); + + // PExplicitLogger.logInfo("Check0.1.3"); + runWithTimeout((long) PExplicitGlobal.getConfig().getTimeLimit()); + + // PExplicitLogger.logInfo("Check0.1.4"); } catch (TimeoutException e) { PExplicitGlobal.setStatus(STATUS.TIMEOUT); throw new Exception("TIMEOUT", e); @@ -172,10 +205,16 @@ public static void run() throws Exception { localSchedulers.add(localCopy); } - // PExplicitLogger.logInfo("Hello, World!"); + // PExplicitLogger.logInfo("Check0.0"); preprocess(); + + // PExplicitLogger.logInfo("Check0.1"); + process(false); + + // PExplicitLogger.logInfo("Check0.2"); + } } \ No newline at end of file diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java index ea43ca05a..e4ff8c16d 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java @@ -9,18 +9,38 @@ import pexplicit.runtime.scheduler.explicit.strategy.SearchStrategyMode; import java.util.*; +import java.util.concurrent.atomic.AtomicLong; /** * Represents global data structures represented with a singleton class */ public class PExplicitGlobal { - // @Getter - // @Setter - // private static Map< > + // Need a bunch of semaphores? + @Getter private static final int maxThreads = 2; - + + + private static AtomicLong threadsBlocking = new AtomicLong(0); + + // Method to get the current value of threadSafeLong + public static long getThreadsBlocking() { + return threadsBlocking.get(); + } + + // Method to increment threadSafeLong + public static void incrementThreadsBlocking() { + threadsBlocking.incrementAndGet(); + } + + public static void decrementThreadsBlocking() { + threadsBlocking.decrementAndGet(); + } + + // @Getter + // @Setter + // private static Map< > @Getter private static Map tID_to_localtID = new HashMap<>(); @@ -33,8 +53,24 @@ public static void addTotIDtolocaltID(long tID, Integer localtID) { /** * Mapping from machine type to list of all machine instances */ - @Getter - private static final Map, List> machineListByType = new HashMap<>(); + // @Getter + // private static final Map, List> machineListByType = new HashMap<>(); // This is per thread; so make this map of tiD to same Map + /** + * Mapping from machine type to list of all machine instances + */ + @Getter + private static final Map< Integer, Map, List>> machineListByTypePerThread = new HashMap<>(); // This is per thread; so make this map of tiD to same Map + + public static Map, List> getMachineListByType() { + int localtID = tID_to_localtID.get(Thread.currentThread().getId()); + return machineListByTypePerThread.get(localtID); + } + + public static void putMachineListByType( Map, List> machineListByType ) { + int localtID = tID_to_localtID.get(Thread.currentThread().getId()); + machineListByTypePerThread.put(localtID, machineListByType); + } + /** * Set of machines */ @@ -93,6 +129,7 @@ public static Scheduler getScheduler() { * @return Machine */ public static PMachine getGlobalMachine(PMachineId pid) { + Map, List> machineListByType = getMachineListByType(); List machinesOfType = machineListByType.get(pid.getType()); if (machinesOfType == null) { return null; @@ -112,8 +149,10 @@ public static PMachine getGlobalMachine(PMachineId pid) { * @param machineCount Machine type count */ public static void addGlobalMachine(PMachine machine, int machineCount) { + Map, List> machineListByType = getMachineListByType(); if (!machineListByType.containsKey(machine.getClass())) { - machineListByType.put(machine.getClass(), new ArrayList<>()); + machineListByType.put(machine.getClass(), new ArrayList<>()); + putMachineListByType(machineListByType); // PIN: Need lock and key somewhere here! Also, is this local copy ok for future use in this function? Need lock and key for future use? } assert (machineCount == machineListByType.get(machine.getClass()).size()); machineListByType.get(machine.getClass()).add(machine); diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Scheduler.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Scheduler.java index 163b0ef86..05ca14994 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Scheduler.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Scheduler.java @@ -83,6 +83,15 @@ protected Scheduler() { */ public abstract void run() throws TimeoutException, InterruptedException; + + /** + * Run the scheduler. + * + * @throws TimeoutException Throws timeout exception if timeout is reached + * @throws InterruptedException Throws interrupt exception if interrupted + */ + public abstract void runParallel() throws TimeoutException, InterruptedException; + /** * Run an iteration. * diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java index c233ca8d8..2b170c396 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java @@ -1,9 +1,12 @@ package pexplicit.runtime.scheduler.explicit; import com.google.common.hash.Hashing; + import lombok.Getter; import lombok.Setter; + import org.apache.commons.lang3.StringUtils; + import pexplicit.runtime.PExplicitGlobal; import pexplicit.runtime.STATUS; import pexplicit.runtime.logger.PExplicitLogger; @@ -28,15 +31,18 @@ import java.util.*; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; - +import java.util.Collections; +import java.util.concurrent.ConcurrentHashMap; /** * Represents the scheduler for performing explicit-state model checking */ public class ExplicitSearchScheduler extends Scheduler { + /** * Map from state hash to iteration when first visited */ - private final transient Map stateCache = new HashMap<>(); + private static final transient Map stateCache = new ConcurrentHashMap<>(); + /** * Search strategy orchestrator */ @@ -84,28 +90,47 @@ public ExplicitSearchScheduler() { } } + @Override + public void run() throws TimeoutException { + return; + } + + + /** * Run the scheduler to perform explicit-state search. * * @throws TimeoutException Throws timeout exception if timeout is reached */ + // @Override + // public void run() throws TimeoutException { + + // PExplicitLogger.logInfo("Hello my dear friend" + PExplicitGlobal.getTID_to_localtID().get(Thread.currentThread().getId())); + + // } @Override - public void run() throws TimeoutException { - // log run test - PExplicitLogger.logRunTest(); + public void runParallel() throws TimeoutException { + + // PExplicitLogger.logInfo("Check1.0"); + // PExplicitLogger.logRunTest(); + + // PExplicitLogger.logInfo("Check1.1"); + - PExplicitGlobal.setResult("incomplete"); - if (PExplicitGlobal.getConfig().getVerbosity() == 0) { - printProgressHeader(true); - } - searchStrategy.createFirstTask(); + // PExplicitGlobal.setResult("incomplete"); // PIN: Result object + + + // if (PExplicitGlobal.getConfig().getVerbosity() == 0) { + // printProgressHeader(true); + // } + while (true) { - PExplicitLogger.logStartTask(searchStrategy.getCurrTask()); + // PExplicitLogger.logStartTask(searchStrategy.getCurrTask()); isDoneIterating = false; while (!isDoneIterating) { - SearchStatistics.iteration++; - PExplicitLogger.logStartIteration(searchStrategy.getCurrTask(), SearchStatistics.iteration, stepNumber); + // SearchStatistics.iteration++; + // PExplicitLogger.logStartIteration(searchStrategy.getCurrTask(), SearchStatistics.iteration, stepNumber); if (stepNumber == 0) { start(); } @@ -114,7 +139,7 @@ public void run() throws TimeoutException { } addRemainingChoicesAsChildrenTasks(); endCurrTask(); - PExplicitLogger.logEndTask(searchStrategy.getCurrTask(), searchStrategy.getNumSchedulesInCurrTask()); + // PExplicitLogger.logEndTask(searchStrategy.getCurrTask(), searchStrategy.getNumSchedulesInCurrTask()); if (searchStrategy.getPendingTasks().isEmpty() || PExplicitGlobal.getStatus() == STATUS.SCHEDULEOUT) { // all tasks completed or schedule limit reached @@ -445,7 +470,7 @@ private void addRemainingChoicesAsChildrenTasks() { } } - PExplicitLogger.logNewTasks(parentTask.getChildren()); + // PExplicitLogger.logNewTasks(parentTask.getChildren()); } private void endCurrTask() { @@ -486,7 +511,7 @@ private void setChildTask(SearchUnit unit, int choiceNum, SearchTask parentTask, public SearchTask setNextTask() { SearchTask nextTask = searchStrategy.setNextTask(); if (nextTask != null) { - PExplicitLogger.logNextTask(nextTask); + // PExplicitLogger.logNextTask(nextTask); schedule.setChoices(nextTask.getPrefixChoices()); postIterationCleanup(); } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java index d3d7806c6..b27d92ff4 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java @@ -8,21 +8,26 @@ import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.Collections; + +import pexplicit.runtime.PExplicitGlobal; @Getter public abstract class SearchStrategy implements Serializable { /** * List of all search tasks - */ // Make 3 static - final List allTasks = new ArrayList<>(); + */ + final static List allTasks = Collections.synchronizedList(new ArrayList<>()); /** * Set of all search tasks that are pending */ - final Set pendingTasks = new HashSet<>(); + @Getter + final static Set pendingTasks = Collections.synchronizedSet(new HashSet<>()); // Is synchornized hash set /** * List of all search tasks that finished */ - final List finishedTasks = new ArrayList<>(); + @Getter + final static List finishedTasks = Collections.synchronizedList(new ArrayList<>()); /** * Task id of the latest search task */ @@ -39,10 +44,17 @@ public SearchTask createTask(SearchTask parentTask) { return newTask; } - public void createFirstTask() { + + + + public static void createFirstTask() { assert (allTasks.size() == 0); - SearchTask firstTask = createTask(null); - setCurrTask(firstTask); + // SearchTask firstTask = createTask(null); // Need a static version of createTask here, so just put createTask implementation here for null argument + SearchTask newTask = new SearchTask(allTasks.size(), null); + allTasks.add(newTask); + pendingTasks.add(newTask.getId()); + + // setCurrTask(firstTask); // Add it to pending Task List instead of setting it to set current task; like in pendingTasks.add(newTask.getId()); } public SearchTask getCurrTask() { @@ -72,7 +84,13 @@ protected SearchTask getTask(int id) { public SearchTask setNextTask() { if (pendingTasks.isEmpty()) { - return null; + PExplicitGlobal.incrementThreadsBlocking(); + while(pendingTasks.isEmpty()) { + if (PExplicitGlobal.getThreadsBlocking() == PExplicitGlobal.getMaxThreads()) + return null; + Thread.sleep(50000); + } + PExplicitGlobal.decrementThreadsBlocking(); } SearchTask nextTask = popNextTask(); diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategyRandom.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategyRandom.java index 8d3441428..1f19e156d 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategyRandom.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategyRandom.java @@ -28,4 +28,12 @@ public SearchTask popNextTask() { elementSet.remove(result); return result; } + + public SearchTask popNextTaskAndCheckEmpty() { + if (isEmpty) + return false; + else popNextTask(); + } + + } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/replay/ReplayScheduler.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/replay/ReplayScheduler.java index 2b5b48764..773872b9a 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/replay/ReplayScheduler.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/replay/ReplayScheduler.java @@ -31,6 +31,12 @@ public void run() throws TimeoutException, InterruptedException { runIteration(); } + @Override + public void runParallel() throws TimeoutException, InterruptedException { + run(); + } + + @Override protected void runIteration() throws TimeoutException { isDoneStepping = false; diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/monitor/TimedCall.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/monitor/TimedCall.java index a2202548f..d911da7f1 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/monitor/TimedCall.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/monitor/TimedCall.java @@ -1,6 +1,7 @@ package pexplicit.utils.monitor; import pexplicit.runtime.PExplicitGlobal; +import pexplicit.runtime.logger.PExplicitLogger; import pexplicit.runtime.scheduler.Scheduler; import pexplicit.utils.exceptions.BugFoundException; import pexplicit.utils.exceptions.MemoutException; @@ -30,7 +31,9 @@ public TimedCall(Scheduler scheduler, boolean resume, int localtID) { public Integer call() throws MemoutException, BugFoundException, TimeoutException, InterruptedException { try { - this.scheduler.run(); + // PExplicitLogger.logInfo("Check: Job submitted"); + this.scheduler.runParallel(); + // PExplicitLogger.logInfo("-Check: Job completed-"); } catch (OutOfMemoryError e) { throw new MemoutException(e.getMessage(), MemoryMonitor.getMemSpent(), e); } catch (NullPointerException | StackOverflowError | ClassCastException e) { diff --git a/benchmarksRuns.sh b/benchmarksRuns.sh index a42923d26..c76db4bdc 100755 --- a/benchmarksRuns.sh +++ b/benchmarksRuns.sh @@ -4,5 +4,6 @@ cd ./Src/PRuntimes/PExplicitRuntime/ echo "-------------------Build Over---------------------------" cd - cd ../../scriptsRepo/src/P-Evaluation-Tests/ +echo "Running script ..." ./scripts/run_pexplicitzshrc.sh ../../../SyncedForkedRepo/P/Tutorial/1_ClientServer test -tc tcMultipleClients --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 cd - \ No newline at end of file From 81815b46e87f0ae7ad0d4f0f3a2560bebf859aa2 Mon Sep 17 00:00:00 2001 From: Ashish Kumar Date: Wed, 26 Jun 2024 11:05:23 -0700 Subject: [PATCH 11/31] discussed changes --- .../commandline/PExplicitOptions.java | 1 + .../pexplicit/runtime/PExplicitGlobal.java | 11 +++++----- .../explicit/ExplicitSearchScheduler.java | 21 +++++++++++-------- .../explicit/strategy/SearchStrategy.java | 1 + .../strategy/SearchStrategyRandom.java | 19 ++++++++++------- 5 files changed, 31 insertions(+), 22 deletions(-) diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitOptions.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitOptions.java index 3aa4da04f..2a8ace05b 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitOptions.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitOptions.java @@ -148,6 +148,7 @@ public class PExplicitOptions { .build(); addOption(randomSeed); + // Add here numThreads /* * Invisible/expert options diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java index e4ff8c16d..21e77b017 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java @@ -9,6 +9,7 @@ import pexplicit.runtime.scheduler.explicit.strategy.SearchStrategyMode; import java.util.*; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicLong; /** * Represents global data structures represented with a singleton class @@ -42,7 +43,7 @@ public static void decrementThreadsBlocking() { // @Setter // private static Map< > @Getter - private static Map tID_to_localtID = new HashMap<>(); + private static Map tID_to_localtID = new ConcurrentHashMap<>(); // Method to add to tID_to_localtID @@ -59,11 +60,11 @@ public static void addTotIDtolocaltID(long tID, Integer localtID) { * Mapping from machine type to list of all machine instances */ @Getter - private static final Map< Integer, Map, List>> machineListByTypePerThread = new HashMap<>(); // This is per thread; so make this map of tiD to same Map + private static final Map< Integer, Map, List>> machineListByTypePerThread = new ConcurrentHashMap<>(); // This is per thread; so make this map of tiD to same Map public static Map, List> getMachineListByType() { int localtID = tID_to_localtID.get(Thread.currentThread().getId()); - return machineListByTypePerThread.get(localtID); + return machineListByTypePerThread.get(localtID); // If this key is not there; initialize it to an empty Hashmap and then return that there! } public static void putMachineListByType( Map, List> machineListByType ) { @@ -105,9 +106,7 @@ public static Scheduler getScheduler() { return schedulers.get(localtID); } - @Getter - @Setter - private static SearchStrategyMode searchStrategyMode; + /** * Status of the run **/ diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java index 2b170c396..8c04c517f 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java @@ -92,7 +92,8 @@ public ExplicitSearchScheduler() { @Override public void run() throws TimeoutException { - return; + + } @@ -126,6 +127,13 @@ public void runParallel() throws TimeoutException { while (true) { + // schedule limit not reached and there are pending tasks + // set the next task + SearchTask nextTask = setNextTask(); + if (nextTask == null ) { // || TODO: PExplicitGlobal.getStatus() == STATUS.SCHEDULEOUT + // all tasks completed or schedule limit reached + break; + } // PExplicitLogger.logStartTask(searchStrategy.getCurrTask()); isDoneIterating = false; while (!isDoneIterating) { @@ -141,15 +149,10 @@ public void runParallel() throws TimeoutException { endCurrTask(); // PExplicitLogger.logEndTask(searchStrategy.getCurrTask(), searchStrategy.getNumSchedulesInCurrTask()); - if (searchStrategy.getPendingTasks().isEmpty() || PExplicitGlobal.getStatus() == STATUS.SCHEDULEOUT) { - // all tasks completed or schedule limit reached - break; - } - // schedule limit not reached and there are pending tasks - // set the next task - SearchTask nextTask = setNextTask(); - assert (nextTask != null); + + + } } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java index b27d92ff4..b5e203fbc 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java @@ -82,6 +82,7 @@ protected SearchTask getTask(int id) { return allTasks.get(id); } + @throws InterruptedException public SearchTask setNextTask() { if (pendingTasks.isEmpty()) { PExplicitGlobal.incrementThreadsBlocking(); diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategyRandom.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategyRandom.java index 1f19e156d..01c24989a 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategyRandom.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategyRandom.java @@ -1,8 +1,13 @@ package pexplicit.runtime.scheduler.explicit.strategy; -import pexplicit.utils.random.RandomNumberGenerator; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Random; +import java.util.Set; -import java.util.*; +import pexplicit.utils.random.RandomNumberGenerator; public class SearchStrategyRandom extends SearchStrategy { private final List elementList; @@ -29,11 +34,11 @@ public SearchTask popNextTask() { return result; } - public SearchTask popNextTaskAndCheckEmpty() { - if (isEmpty) - return false; - else popNextTask(); - } + // public SearchTask popNextTaskAndCheckEmpty() { + // if (isEmpty) + // return false; + // else popNextTask(); + // } } From 7c400c92a9d57bf1f19c38eb7276620e27d0a01c Mon Sep 17 00:00:00 2001 From: Ashish Kumar Date: Wed, 26 Jun 2024 13:20:20 -0700 Subject: [PATCH 12/31] Recent Changes --- .../main/java/pexplicit/RuntimeExecutor.java | 24 +----- .../commandline/PExplicitConfig.java | 5 ++ .../commandline/PExplicitOptions.java | 15 +++- .../pexplicit/runtime/PExplicitGlobal.java | 25 ++++-- .../runtime/logger/PExplicitLogger.java | 80 +++++++++---------- .../explicit/ExplicitSearchScheduler.java | 30 +++---- .../explicit/strategy/SearchStrategy.java | 8 +- .../pexplicit/utils/monitor/TimedCall.java | 2 - 8 files changed, 92 insertions(+), 97 deletions(-) diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java index 85328bea4..76b360a23 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java @@ -35,17 +35,11 @@ private static void runWithTimeout(long timeLimit) RuntimeException { try { // PIN: If thread gets exception, need to kill the other threads. if (timeLimit > 0) { - // PExplicitLogger.logInfo("Check0.1.3.1"); - for (Future future: futures) { // Future future = futures.get(i); future.get(timeLimit, TimeUnit.SECONDS); } - - // PExplicitLogger.logInfo("Check0.1.3.2"); - - } else { for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) { @@ -94,7 +88,7 @@ private static void preprocess() { executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(PExplicitGlobal.getMaxThreads()); - // PExplicitGlobal.setResult("error"); + // PExplicitGlobal.setResult("error"); // TODO: Set Results, need to take care of. double preSearchTime = TimeMonitor.findInterval(TimeMonitor.getStart()); @@ -109,8 +103,6 @@ private static void process(boolean resume) throws Exception { try { ArrayList timedCalls = new ArrayList<>(); - // PExplicitLogger.logInfo("Check0.1.1"); - SearchStrategy.createFirstTask(); for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) { @@ -122,22 +114,16 @@ private static void process(boolean resume) throws Exception { futures.add(future); } - // Thread.sleep(1000); + Thread.sleep(1000); // Sleep for 1 second, so that threads can pick up the task from the executor object // Get the number of pending tasks int pendingTasks = executor.getQueue().size(); PExplicitLogger.logInfo("Number of pending tasks: " + pendingTasks); - - // PExplicitLogger.logInfo("Check0.1.2"); - TimeMonitor.startInterval(); - // PExplicitLogger.logInfo("Check0.1.3"); - runWithTimeout((long) PExplicitGlobal.getConfig().getTimeLimit()); - // PExplicitLogger.logInfo("Check0.1.4"); } catch (TimeoutException e) { PExplicitGlobal.setStatus(STATUS.TIMEOUT); throw new Exception("TIMEOUT", e); @@ -149,9 +135,6 @@ private static void process(boolean resume) throws Exception { - // for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) - // PExplicitGlobal.setResult(String.format("found cex of length %d", (schedulers.get(i)).getStepNumber())); - // Terminate all schedulers at this point, and that scheduler which found this exception stores result. PExplicitLogger.logStackTrace(e); @@ -205,15 +188,12 @@ public static void run() throws Exception { localSchedulers.add(localCopy); } - // PExplicitLogger.logInfo("Check0.0"); preprocess(); - // PExplicitLogger.logInfo("Check0.1"); process(false); - // PExplicitLogger.logInfo("Check0.2"); } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java index ce28c7f38..a00e23cfd 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java @@ -64,4 +64,9 @@ public class PExplicitConfig { //max number of children search tasks @Setter int maxChildrenPerTask = 2; + + @Getter + @Setter + private static int numThreads = 1; + } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitOptions.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitOptions.java index 2a8ace05b..55bcb5231 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitOptions.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitOptions.java @@ -148,8 +148,16 @@ public class PExplicitOptions { .build(); addOption(randomSeed); - // Add here numThreads - + // Number of threads + Option numThreads = + Option.builder() + .longOpt("nproc") + .desc("Specify the number of threads to use") + .numberOfArgs(1) + .hasArg() + .argName("No. of threads (integer)") + .build(); + addOption(numThreads); /* * Invisible/expert options */ @@ -337,6 +345,9 @@ public static PExplicitConfig ParseCommandlineArgs(String[] args) { option, String.format("Expected an integer value, got %s", option.getValue())); } break; + case "nproc": + PExplicitConfig.setNumThreads(Integer.parseInt(option.getValue())); + break; // invisible expert options case "state-caching": switch (option.getValue()) { diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java index 21e77b017..b1d310614 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java @@ -11,16 +11,16 @@ import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicLong; + +import org.checkerframework.checker.units.qual.A; /** * Represents global data structures represented with a singleton class */ public class PExplicitGlobal { - - // Need a bunch of semaphores? - + @Getter - private static final int maxThreads = 2; + private static final int maxThreads = PExplicitConfig.getNumThreads(); private static AtomicLong threadsBlocking = new AtomicLong(0); @@ -64,7 +64,10 @@ public static void addTotIDtolocaltID(long tID, Integer localtID) { public static Map, List> getMachineListByType() { int localtID = tID_to_localtID.get(Thread.currentThread().getId()); - return machineListByTypePerThread.get(localtID); // If this key is not there; initialize it to an empty Hashmap and then return that there! + if (!machineListByTypePerThread.containsKey(localtID)) { + machineListByTypePerThread.put(localtID, new HashMap<>()); // Initialize with an empty HashMap if key doesn't exist + } + return machineListByTypePerThread.get(localtID); } public static void putMachineListByType( Map, List> machineListByType ) { @@ -76,7 +79,17 @@ public static void putMachineListByType( Map, List machineSet = new TreeSet<>(); + private static final Map< Integer, SortedSet> machineSetPerThread = new ConcurrentHashMap<>(); + + + public static SortedSet getMachineSet () { + int localtID = tID_to_localtID.get(Thread.currentThread().getId()); + if (!machineSetPerThread.containsKey(localtID)) { + machineSetPerThread.put(localtID, new TreeSet<>()); + } + return machineSetPerThread.get(localtID); + } + /** * PModel **/ diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java index a4ebc52e8..be6fa181f 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java @@ -78,7 +78,7 @@ public static void logInfo(String message) { */ public static void logVerbose(String message) { if (verbosity > 3) { - log.info(message); + logInfo(message); } } @@ -90,27 +90,27 @@ public static void logVerbose(String message) { */ public static void logEndOfRun(ExplicitSearchScheduler scheduler, long timeSpent) { if (verbosity == 0) { - log.info(""); + logInfo(""); } - log.info("--------------------"); - log.info("... Checking statistics:"); + logInfo("--------------------"); + logInfo("... Checking statistics:"); if (PExplicitGlobal.getStatus() == STATUS.BUG_FOUND) { - log.info("..... Found 1 bug."); + logInfo("..... Found 1 bug."); } else { - log.info("..... Found 0 bugs."); + logInfo("..... Found 0 bugs."); } - log.info("... Scheduling statistics:"); + logInfo("... Scheduling statistics:"); if (PExplicitGlobal.getConfig().getStateCachingMode() != StateCachingMode.None) { - log.info(String.format("..... Explored %d distinct states", SearchStatistics.totalDistinctStates)); + logInfo(String.format("..... Explored %d distinct states", SearchStatistics.totalDistinctStates)); } - log.info(String.format("..... Explored %d distinct schedules", SearchStatistics.iteration)); - log.info(String.format("..... Finished %d search tasks (%d pending)", + logInfo(String.format("..... Explored %d distinct schedules", SearchStatistics.iteration)); + logInfo(String.format("..... Finished %d search tasks (%d pending)", scheduler.getSearchStrategy().getFinishedTasks().size(), scheduler.getSearchStrategy().getPendingTasks().size())); - log.info(String.format("..... Number of steps explored: %d (min), %d (avg), %d (max).", + logInfo(String.format("..... Number of steps explored: %d (min), %d (avg), %d (max).", SearchStatistics.minSteps, (SearchStatistics.totalSteps / SearchStatistics.iteration), SearchStatistics.maxSteps)); - log.info(String.format("... Elapsed %d seconds and used %.1f GB", timeSpent, MemoryMonitor.getMaxMemSpent() / 1000.0)); - log.info(String.format(".. Result: " + PExplicitGlobal.getResult())); - log.info(". Done"); + logInfo(String.format("... Elapsed %d seconds and used %.1f GB", timeSpent, MemoryMonitor.getMaxMemSpent() / 1000.0)); + logInfo(String.format(".. Result: " + PExplicitGlobal.getResult())); + logInfo(". Done"); } /** @@ -122,8 +122,8 @@ public static void logStackTrace(Exception e) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); - log.info("--------------------"); - log.info(sw.toString()); + logInfo("--------------------"); + logInfo(sw.toString()); } /** @@ -133,8 +133,8 @@ public static void logStackTrace(Exception e) { */ public static void logStartTask(SearchTask task) { if (verbosity > 0) { - log.info("====================="); - log.info(String.format("Starting %s", task.toStringDetailed())); + logInfo("====================="); + logInfo(String.format("Starting %s", task.toStringDetailed())); } } @@ -145,7 +145,7 @@ public static void logStartTask(SearchTask task) { */ public static void logEndTask(SearchTask task, int numSchedules) { if (verbosity > 0) { - log.info(String.format(" Finished %s after exploring %d schedules", task, numSchedules)); + logInfo(String.format(" Finished %s after exploring %d schedules", task, numSchedules)); } } @@ -156,17 +156,17 @@ public static void logEndTask(SearchTask task, int numSchedules) { */ public static void logNextTask(SearchTask task) { if (verbosity > 1) { - log.info(String.format(" Next task: %s", task.toStringDetailed())); + logInfo(String.format(" Next task: %s", task.toStringDetailed())); } } public static void logNewTasks(List tasks) { if (verbosity > 0) { - log.info(String.format(" Added %d new tasks", tasks.size())); + logInfo(String.format(" Added %d new tasks", tasks.size())); } if (verbosity > 1) { for (SearchTask task : tasks) { - log.info(String.format(" %s", task.toStringDetailed())); + logInfo(String.format(" %s", task.toStringDetailed())); } } } @@ -179,18 +179,18 @@ public static void logNewTasks(List tasks) { */ public static void logStartIteration(SearchTask task, int iter, int step) { if (verbosity > 0) { - log.info("--------------------"); - log.info(String.format("[%s] Starting schedule %s from step %s", task, iter, step)); + logInfo("--------------------"); + logInfo(String.format("[%s] Starting schedule %s from step %s", task, iter, step)); } } public static void logStartStep(int step, PMachine sender, PMessage msg) { if (verbosity > 0) { - log.info(String.format( + logInfo(String.format( " Step %d: %s sent %s to %s", step, sender, msg.getEvent(), msg.getTarget())); if (verbosity > 5) { - log.info(String.format(" payload: %s", msg.getPayload())); + logInfo(String.format(" payload: %s", msg.getPayload())); } } } @@ -202,7 +202,7 @@ public static void logStartStep(int step, PMachine sender, PMessage msg) { */ public static void logFinishedIteration(int step) { if (verbosity > 0) { - log.info(String.format(" Schedule finished at step %d", step)); + logInfo(String.format(" Schedule finished at step %d", step)); } } @@ -215,7 +215,7 @@ public static void logFinishedIteration(int step) { */ public static void logBacktrack(int stepNum, int choiceNum, SearchUnit unit) { if (verbosity > 1) { - log.info(String.format(" Backtracking to %s choice @%d::%d", + logInfo(String.format(" Backtracking to %s choice @%d::%d", ((unit instanceof ScheduleSearchUnit) ? "schedule" : "data"), stepNum, choiceNum)); } @@ -223,45 +223,45 @@ public static void logBacktrack(int stepNum, int choiceNum, SearchUnit unit) { public static void logNewScheduleChoice(List choices, int step, int idx) { if (verbosity > 1) { - log.info(String.format(" @%d::%d new schedule choice: %s", step, idx, choices)); + logInfo(String.format(" @%d::%d new schedule choice: %s", step, idx, choices)); } } public static void logNewDataChoice(List> choices, int step, int idx) { if (verbosity > 1) { - log.info(String.format(" @%d::%d new data choice: %s", step, idx, choices)); + logInfo(String.format(" @%d::%d new data choice: %s", step, idx, choices)); } } public static void logRepeatScheduleChoice(PMachine choice, int step, int idx) { if (verbosity > 2) { - log.info(String.format(" @%d::%d %s (repeat)", step, idx, choice)); + logInfo(String.format(" @%d::%d %s (repeat)", step, idx, choice)); } } public static void logCurrentScheduleChoice(PMachine choice, int step, int idx) { if (verbosity > 2) { - log.info(String.format(" @%d::%d %s", step, idx, choice)); + logInfo(String.format(" @%d::%d %s", step, idx, choice)); } } public static void logRepeatDataChoice(PValue choice, int step, int idx) { if (verbosity > 2) { - log.info(String.format(" @%d::%d %s (repeat)", step, idx, choice)); + logInfo(String.format(" @%d::%d %s (repeat)", step, idx, choice)); } } public static void logCurrentDataChoice(PValue choice, int step, int idx) { if (verbosity > 2) { - log.info(String.format(" @%d::%d %s", step, idx, choice)); + logInfo(String.format(" @%d::%d %s", step, idx, choice)); } } public static void logNewState(int step, int idx, Object stateKey, SortedSet machines) { if (verbosity > 3) { - log.info(String.format(" @%d::%d new state with key %s", step, idx, stateKey)); + logInfo(String.format(" @%d::%d new state with key %s", step, idx, stateKey)); if (verbosity > 6) { - log.info(String.format(" %s", ComputeHash.getExactString(machines))); + logInfo(String.format(" %s", ComputeHash.getExactString(machines))); } } } @@ -279,7 +279,7 @@ private static void typedLog(LogType type, String message) { if (isReplaying()) { TextWriter.typedLog(type, message); } else { - log.info(String.format(" <%s> %s", type, message)); + logInfo(String.format(" <%s> %s", type, message)); } } @@ -292,7 +292,7 @@ public static void logRunTest() { public static void logModel(String message) { if (verbosity > 0) { - log.info(message); + logInfo(message); } if (typedLogEnabled()) { typedLog(LogType.PrintLog, message); @@ -410,8 +410,8 @@ public static void logDequeueEvent(PMachine machine, PMessage message) { public static void logStartReplay() { if (verbosity > 0) { - log.info("--------------------"); - log.info("Replaying schedule"); + logInfo("--------------------"); + logInfo("Replaying schedule"); } } } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java index 8c04c517f..2372294b3 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java @@ -103,25 +103,17 @@ public void run() throws TimeoutException { * * @throws TimeoutException Throws timeout exception if timeout is reached */ - // @Override - // public void run() throws TimeoutException { - - // PExplicitLogger.logInfo("Hello my dear friend" + PExplicitGlobal.getTID_to_localtID().get(Thread.currentThread().getId())); - - // } @Override public void runParallel() throws TimeoutException { - // PExplicitLogger.logInfo("Check1.0"); - // PExplicitLogger.logRunTest(); + // PExplicitLogger.logRunTest(); // TODO : Add log Info feature - // PExplicitLogger.logInfo("Check1.1"); - // PExplicitGlobal.setResult("incomplete"); // PIN: Result object + // PExplicitGlobal.setResult("incomplete"); // TODO: Set Result object - // if (PExplicitGlobal.getConfig().getVerbosity() == 0) { + // if (PExplicitGlobal.getConfig().getVerbosity() == 0) { // TODO : Add log Info feature // printProgressHeader(true); // } @@ -130,15 +122,15 @@ public void runParallel() throws TimeoutException { // schedule limit not reached and there are pending tasks // set the next task SearchTask nextTask = setNextTask(); - if (nextTask == null ) { // || TODO: PExplicitGlobal.getStatus() == STATUS.SCHEDULEOUT + if (nextTask == null ) { // || PIN: PExplicitGlobal.getStatus() == STATUS.SCHEDULEOUT // all tasks completed or schedule limit reached break; } - // PExplicitLogger.logStartTask(searchStrategy.getCurrTask()); + // PExplicitLogger.logStartTask(searchStrategy.getCurrTask()); // TODO : Add log Info feature isDoneIterating = false; while (!isDoneIterating) { - // SearchStatistics.iteration++; - // PExplicitLogger.logStartIteration(searchStrategy.getCurrTask(), SearchStatistics.iteration, stepNumber); + // SearchStatistics.iteration++; // TODO : Add log Info feature + // PExplicitLogger.logStartIteration(searchStrategy.getCurrTask(), SearchStatistics.iteration, stepNumber); // TODO : Add log Info feature if (stepNumber == 0) { start(); } @@ -147,7 +139,7 @@ public void runParallel() throws TimeoutException { } addRemainingChoicesAsChildrenTasks(); endCurrTask(); - // PExplicitLogger.logEndTask(searchStrategy.getCurrTask(), searchStrategy.getNumSchedulesInCurrTask()); + // PExplicitLogger.logEndTask(searchStrategy.getCurrTask(), searchStrategy.getNumSchedulesInCurrTask()); // TODO : Add log Info feature @@ -473,7 +465,7 @@ private void addRemainingChoicesAsChildrenTasks() { } } - // PExplicitLogger.logNewTasks(parentTask.getChildren()); + // PExplicitLogger.logNewTasks(parentTask.getChildren()); // TODO : Add log Info feature } private void endCurrTask() { @@ -483,7 +475,7 @@ private void endCurrTask() { } private void setChildTask(SearchUnit unit, int choiceNum, SearchTask parentTask, boolean isExact) { - SearchTask newTask = searchStrategy.createTask(parentTask); + SearchTask newTask = SearchStrategy.createTask(parentTask); int maxChoiceNum = choiceNum; @@ -514,7 +506,7 @@ private void setChildTask(SearchUnit unit, int choiceNum, SearchTask parentTask, public SearchTask setNextTask() { SearchTask nextTask = searchStrategy.setNextTask(); if (nextTask != null) { - // PExplicitLogger.logNextTask(nextTask); + // PExplicitLogger.logNextTask(nextTask); // TODO : Add log Info feature schedule.setChoices(nextTask.getPrefixChoices()); postIterationCleanup(); } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java index b5e203fbc..ea58b6deb 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java @@ -37,7 +37,7 @@ public abstract class SearchStrategy implements Serializable { */ int currTaskStartIteration = 0; - public SearchTask createTask(SearchTask parentTask) { + public static SearchTask createTask(SearchTask parentTask) { SearchTask newTask = new SearchTask(allTasks.size(), parentTask); allTasks.add(newTask); pendingTasks.add(newTask.getId()); @@ -49,12 +49,8 @@ public SearchTask createTask(SearchTask parentTask) { public static void createFirstTask() { assert (allTasks.size() == 0); - // SearchTask firstTask = createTask(null); // Need a static version of createTask here, so just put createTask implementation here for null argument - SearchTask newTask = new SearchTask(allTasks.size(), null); - allTasks.add(newTask); - pendingTasks.add(newTask.getId()); + createTask(null); - // setCurrTask(firstTask); // Add it to pending Task List instead of setting it to set current task; like in pendingTasks.add(newTask.getId()); } public SearchTask getCurrTask() { diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/monitor/TimedCall.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/monitor/TimedCall.java index d911da7f1..fe7668917 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/monitor/TimedCall.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/monitor/TimedCall.java @@ -31,9 +31,7 @@ public TimedCall(Scheduler scheduler, boolean resume, int localtID) { public Integer call() throws MemoutException, BugFoundException, TimeoutException, InterruptedException { try { - // PExplicitLogger.logInfo("Check: Job submitted"); this.scheduler.runParallel(); - // PExplicitLogger.logInfo("-Check: Job completed-"); } catch (OutOfMemoryError e) { throw new MemoutException(e.getMessage(), MemoryMonitor.getMemSpent(), e); } catch (NullPointerException | StackOverflowError | ClassCastException e) { From fdff5984ca1b969041759a9ad648b53c91f7bcca Mon Sep 17 00:00:00 2001 From: Aman Goel Date: Wed, 26 Jun 2024 21:35:19 +0000 Subject: [PATCH 13/31] [PEx] Add thread-safe code for search strategies, minor cleanup --- .../main/java/pexplicit/RuntimeExecutor.java | 6 +- .../pexplicit/runtime/PExplicitGlobal.java | 4 +- .../runtime/logger/PExplicitLogger.java | 18 +++--- .../pexplicit/runtime/machine/PMachine.java | 2 +- .../explicit/ExplicitSearchScheduler.java | 12 ++-- .../explicit/strategy/SearchStrategy.java | 57 ++++++++++++------- .../strategy/SearchStrategyAStar.java | 21 +++---- .../strategy/SearchStrategyRandom.java | 37 ++++++------ 8 files changed, 86 insertions(+), 71 deletions(-) diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java index 76b360a23..9c47007e5 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java @@ -101,10 +101,10 @@ private static void preprocess() { private static void process(boolean resume) throws Exception { try { + // create and add first task through scheduler 0 + schedulers.get(0).getSearchStrategy().createFirstTask(); + ArrayList timedCalls = new ArrayList<>(); - - SearchStrategy.createFirstTask(); - for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) { timedCalls.add( new TimedCall(schedulers.get(i), resume, i)); } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java index b1d310614..2b9232692 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java @@ -150,7 +150,7 @@ public static PMachine getGlobalMachine(PMachineId pid) { return null; } PMachine result = machineListByType.get(pid.getType()).get(pid.getTypeId()); - assert (machineSet.contains(result)); + assert (getMachineSet().contains(result)); return result; } @@ -168,7 +168,7 @@ public static void addGlobalMachine(PMachine machine, int machineCount) { } assert (machineCount == machineListByType.get(machine.getClass()).size()); machineListByType.get(machine.getClass()).add(machine); - machineSet.add(machine); + getMachineSet().add(machine); assert (machineListByType.get(machine.getClass()).get(machineCount) == machine); } } \ No newline at end of file diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java index be6fa181f..f101773ee 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java @@ -315,7 +315,7 @@ public static void logBugFound(String message) { * @param creator Machine that created this machine */ public static void logCreateMachine(PMachine machine, PMachine creator) { - int localtID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); + int localtID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); ((PExplicitGlobal.getSchedulers()).get(localtID)).updateLogNumber(); if (typedLogEnabled()) { typedLog(LogType.CreateLog, String.format("%s was created by %s.", machine, creator)); @@ -323,7 +323,7 @@ public static void logCreateMachine(PMachine machine, PMachine creator) { } public static void logSendEvent(PMachine sender, PMessage message) { - int localtID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); + int localtID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); ((PExplicitGlobal.getSchedulers()).get(localtID)).updateLogNumber(); if (typedLogEnabled()) { String payloadMsg = ""; @@ -341,7 +341,7 @@ public static void logSendEvent(PMachine sender, PMessage message) { * @param machine Machine that is entering the state */ public static void logStateEntry(PMachine machine) { - int localtID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); + int localtID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); ((PExplicitGlobal.getSchedulers()).get(localtID)).updateLogNumber(); if (typedLogEnabled()) { typedLog(LogType.StateLog, String.format("%s enters state %s.", machine, machine.getCurrentState())); @@ -354,7 +354,7 @@ public static void logStateEntry(PMachine machine) { * @param machine Machine that is exiting the state */ public static void logStateExit(PMachine machine) { - int localtID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); + int localtID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); ((PExplicitGlobal.getSchedulers()).get(localtID)).updateLogNumber(); if (typedLogEnabled()) { typedLog(LogType.StateLog, String.format("%s exits state %s.", machine, machine.getCurrentState())); @@ -362,7 +362,7 @@ public static void logStateExit(PMachine machine) { } public static void logRaiseEvent(PMachine machine, PEvent event) { - int localtID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); + int localtID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); ((PExplicitGlobal.getSchedulers()).get(localtID)).updateLogNumber(); if (typedLogEnabled()) { typedLog(LogType.RaiseLog, String.format("%s raised event %s in state %s.", machine, event, machine.getCurrentState())); @@ -370,7 +370,7 @@ public static void logRaiseEvent(PMachine machine, PEvent event) { } public static void logStateTransition(PMachine machine, State newState) { - int localtID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); + int localtID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); ((PExplicitGlobal.getSchedulers()).get(localtID)).updateLogNumber(); if (typedLogEnabled()) { typedLog(LogType.GotoLog, String.format("%s is transitioning from state %s to state %s.", machine, machine.getCurrentState(), newState)); @@ -378,7 +378,7 @@ public static void logStateTransition(PMachine machine, State newState) { } public static void logReceive(PMachine machine, PContinuation continuation) { - int localtID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); + int localtID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); ((PExplicitGlobal.getSchedulers()).get(localtID)).updateLogNumber(); if (typedLogEnabled()) { typedLog(LogType.ReceiveLog, String.format("%s is waiting to dequeue an event of type %s or %s in state %s.", @@ -387,7 +387,7 @@ public static void logReceive(PMachine machine, PContinuation continuation) { } public static void logMonitorProcessEvent(PMonitor monitor, PMessage message) { - int localtID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); + int localtID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); ((PExplicitGlobal.getSchedulers()).get(localtID)).updateLogNumber(); if (typedLogEnabled()) { typedLog(LogType.MonitorLog, String.format("%s is processing event %s in state %s.", @@ -396,7 +396,7 @@ public static void logMonitorProcessEvent(PMonitor monitor, PMessage message) { } public static void logDequeueEvent(PMachine machine, PMessage message) { - int localtID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); + int localtID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); ((PExplicitGlobal.getSchedulers()).get(localtID)).updateLogNumber(); if (typedLogEnabled()) { String payloadMsg = ""; diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/PMachine.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/PMachine.java index 0c1263a85..0638d98fb 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/PMachine.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/PMachine.java @@ -248,7 +248,7 @@ public void setMachineState(MachineLocalState input) { * @return New machine as a PMachineValue */ public PMachineValue create( - Class machineType, PValue payload, Function constructor) { + Class machineType, PValue payload, Function constructor) { int localtID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); PMachine machine = ((PExplicitGlobal.getSchedulers()).get(localtID)).allocateMachine(machineType, constructor); PMessage msg = new PMessage(PEvent.createMachine, machine, payload); diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java index 2372294b3..7781ea36d 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java @@ -47,7 +47,6 @@ public class ExplicitSearchScheduler extends Scheduler { * Search strategy orchestrator */ @Getter - @Setter private final transient SearchStrategy searchStrategy; /** * Backtrack choice number @@ -104,7 +103,7 @@ public void run() throws TimeoutException { * @throws TimeoutException Throws timeout exception if timeout is reached */ @Override - public void runParallel() throws TimeoutException { + public void runParallel() throws TimeoutException, InterruptedException { // PExplicitLogger.logRunTest(); // TODO : Add log Info feature @@ -129,7 +128,8 @@ public void runParallel() throws TimeoutException { // PExplicitLogger.logStartTask(searchStrategy.getCurrTask()); // TODO : Add log Info feature isDoneIterating = false; while (!isDoneIterating) { - // SearchStatistics.iteration++; // TODO : Add log Info feature + searchStrategy.incrementIteration(); + // TODO : Add log Info feature // PExplicitLogger.logStartIteration(searchStrategy.getCurrTask(), SearchStatistics.iteration, stepNumber); // TODO : Add log Info feature if (stepNumber == 0) { start(); @@ -448,7 +448,7 @@ private void postProcessIteration() { } } - private void addRemainingChoicesAsChildrenTasks() { + private void addRemainingChoicesAsChildrenTasks() throws InterruptedException { SearchTask parentTask = searchStrategy.getCurrTask(); int numChildrenAdded = 0; for (int i: parentTask.getSearchUnitKeys(false)) { @@ -474,7 +474,7 @@ private void endCurrTask() { searchStrategy.getFinishedTasks().add(currTask.getId()); } - private void setChildTask(SearchUnit unit, int choiceNum, SearchTask parentTask, boolean isExact) { + private void setChildTask(SearchUnit unit, int choiceNum, SearchTask parentTask, boolean isExact) throws InterruptedException { SearchTask newTask = SearchStrategy.createTask(parentTask); int maxChoiceNum = choiceNum; @@ -503,7 +503,7 @@ private void setChildTask(SearchUnit unit, int choiceNum, SearchTask parentTask, /** * Set next backtrack task with given orchestration mode */ - public SearchTask setNextTask() { + public SearchTask setNextTask() throws InterruptedException { SearchTask nextTask = searchStrategy.setNextTask(); if (nextTask != null) { // PExplicitLogger.logNextTask(nextTask); // TODO : Add log Info feature diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java index ea58b6deb..1387a61ca 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java @@ -16,7 +16,7 @@ public abstract class SearchStrategy implements Serializable { /** * List of all search tasks - */ + */ final static List allTasks = Collections.synchronizedList(new ArrayList<>()); /** * Set of all search tasks that are pending @@ -36,6 +36,10 @@ public abstract class SearchStrategy implements Serializable { * Starting iteration number for the current task */ int currTaskStartIteration = 0; + /** + * Number of schedulers explored for the current task + */ + int numSchedulesExplored = 0; public static SearchTask createTask(SearchTask parentTask) { SearchTask newTask = new SearchTask(allTasks.size(), parentTask); @@ -44,13 +48,10 @@ public static SearchTask createTask(SearchTask parentTask) { return newTask; } - - - - public static void createFirstTask() { + public void createFirstTask() throws InterruptedException { assert (allTasks.size() == 0); - createTask(null); - + SearchTask task = createTask(null); + addNewTask(task); } public SearchTask getCurrTask() { @@ -62,10 +63,16 @@ private void setCurrTask(SearchTask task) { pendingTasks.remove(task.getId()); currTaskId = task.getId(); currTaskStartIteration = SearchStatistics.iteration; + numSchedulesExplored = 0; } public int getNumSchedulesInCurrTask() { - return SearchStatistics.iteration - currTaskStartIteration; + return numSchedulesExplored; + } + + public void incrementIteration() { + numSchedulesExplored++; + SearchStatistics.iteration++; } @@ -78,22 +85,30 @@ protected SearchTask getTask(int id) { return allTasks.get(id); } - @throws InterruptedException - public SearchTask setNextTask() { - if (pendingTasks.isEmpty()) { + public SearchTask setNextTask() throws InterruptedException { + SearchTask task = popNextTask(); + if (task == null) { PExplicitGlobal.incrementThreadsBlocking(); - while(pendingTasks.isEmpty()) { - if (PExplicitGlobal.getThreadsBlocking() == PExplicitGlobal.getMaxThreads()) - return null; - Thread.sleep(50000); + if (PExplicitGlobal.getThreadsBlocking() == PExplicitGlobal.getMaxThreads()) { + // all threads blocked, no task remains + return null; } + + // other threads still working and can add new tasks + // try popping in every 5 seconds + do { + // sleep for 5 seconds + Thread.sleep(5000); + // try popping again + task = popNextTask(); + } while (task == null); + + // got a non-null task PExplicitGlobal.decrementThreadsBlocking(); } - SearchTask nextTask = popNextTask(); - setCurrTask(nextTask); - - return nextTask; + setCurrTask(task); + return task; } /** @@ -128,7 +143,7 @@ public int getNumPendingDataChoices() { return numUnexplored; } - public abstract void addNewTask(SearchTask task); + public abstract void addNewTask(SearchTask task) throws InterruptedException; - abstract SearchTask popNextTask(); + abstract SearchTask popNextTask() throws InterruptedException; } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategyAStar.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategyAStar.java index 53126b7a5..04d3a100b 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategyAStar.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategyAStar.java @@ -4,25 +4,22 @@ import java.util.concurrent.PriorityBlockingQueue; public class SearchStrategyAStar extends SearchStrategy { - private final PriorityBlockingQueue elements; + private static final PriorityBlockingQueue elements = + new PriorityBlockingQueue( + 100, + new Comparator() { + public int compare(SearchTask a, SearchTask b) { + return Integer.compare(a.getCurrChoiceNumber(), b.getCurrChoiceNumber()); + } + }); - public SearchStrategyAStar() { - elements = - new PriorityBlockingQueue( - 100, - new Comparator() { - public int compare(SearchTask a, SearchTask b) { - return Integer.compare(a.getCurrChoiceNumber(), b.getCurrChoiceNumber()); - } - }); - } + public SearchStrategyAStar() {} public void addNewTask(SearchTask task) { elements.offer(task); } public SearchTask popNextTask() { - assert (!elements.isEmpty()); return elements.poll(); } } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategyRandom.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategyRandom.java index 01c24989a..81b4b76c7 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategyRandom.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategyRandom.java @@ -6,39 +6,42 @@ import java.util.List; import java.util.Random; import java.util.Set; +import java.util.concurrent.Semaphore; import pexplicit.utils.random.RandomNumberGenerator; public class SearchStrategyRandom extends SearchStrategy { - private final List elementList; - private final Set elementSet; + private static final Semaphore lock = new Semaphore(1); + private static final List elementList = new ArrayList<>(); + private static final Set elementSet = new HashSet<>(); - public SearchStrategyRandom() { - elementList = new ArrayList<>(); - elementSet = new HashSet<>(); - } + public SearchStrategyRandom() {} + + public void addNewTask(SearchTask task) throws InterruptedException { + lock.acquire(); - public void addNewTask(SearchTask task) { assert (!elementSet.contains(task)); elementList.add(task); elementSet.add(task); + + lock.release(); } - public SearchTask popNextTask() { - assert (!elementList.isEmpty()); + public SearchTask popNextTask() throws InterruptedException { + lock.acquire(); + + if (elementList.isEmpty()) { + lock.release(); + return null; + } + Collections.shuffle( elementList, new Random(RandomNumberGenerator.getInstance().getRandomLong())); SearchTask result = elementList.get(0); elementList.remove(0); elementSet.remove(result); + + lock.release(); return result; } - - // public SearchTask popNextTaskAndCheckEmpty() { - // if (isEmpty) - // return false; - // else popNextTask(); - // } - - } From f4c779ce992e0cad781116ff5a679d42734d2cab Mon Sep 17 00:00:00 2001 From: Aman Goel Date: Wed, 26 Jun 2024 21:37:52 +0000 Subject: [PATCH 14/31] [PEx] Refactoring and cleanup --- .../main/java/pexplicit/RuntimeExecutor.java | 35 ++--- .../commandline/PExplicitConfig.java | 7 +- .../runtime/ForeignFunctionInterface.java | 38 ++--- .../pexplicit/runtime/PExplicitGlobal.java | 137 ++++++++---------- .../runtime/logger/PExplicitLogger.java | 6 +- .../pexplicit/runtime/machine/PMachine.java | 4 +- .../runtime/machine/buffer/MessageQueue.java | 1 - .../pexplicit/runtime/scheduler/Schedule.java | 12 +- .../runtime/scheduler/Scheduler.java | 2 +- .../scheduler/choice/DataSearchUnit.java | 2 +- .../scheduler/choice/ScheduleSearchUnit.java | 1 - .../explicit/ExplicitSearchScheduler.java | 42 +++--- .../explicit/strategy/SearchStrategy.java | 13 +- .../strategy/SearchStrategyAStar.java | 3 +- .../strategy/SearchStrategyRandom.java | 14 +- .../explicit/strategy/SearchTask.java | 9 +- .../pexplicit/utils/monitor/TimedCall.java | 8 +- 17 files changed, 148 insertions(+), 186 deletions(-) diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java index 9c47007e5..8e2c70ac1 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java @@ -4,23 +4,20 @@ import pexplicit.runtime.STATUS; import pexplicit.runtime.logger.PExplicitLogger; import pexplicit.runtime.logger.StatWriter; +import pexplicit.runtime.scheduler.Scheduler; import pexplicit.runtime.scheduler.explicit.ExplicitSearchScheduler; -import pexplicit.runtime.scheduler.explicit.strategy.SearchStrategy; import pexplicit.runtime.scheduler.replay.ReplayScheduler; import pexplicit.utils.exceptions.BugFoundException; import pexplicit.utils.exceptions.MemoutException; import pexplicit.utils.monitor.MemoryMonitor; import pexplicit.utils.monitor.TimeMonitor; import pexplicit.utils.monitor.TimedCall; -import pexplicit.runtime.scheduler.Scheduler; import java.time.Duration; import java.time.Instant; import java.util.ArrayList; import java.util.concurrent.*; -import pexplicit.commandline.PExplicitConfig; - /** * Represents the runtime executor that executes the analysis engine */ @@ -35,13 +32,13 @@ private static void runWithTimeout(long timeLimit) RuntimeException { try { // PIN: If thread gets exception, need to kill the other threads. if (timeLimit > 0) { - for (Future future: futures) { + for (Future future : futures) { // Future future = futures.get(i); future.get(timeLimit, TimeUnit.SECONDS); } - + } else { - + for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) { Future future = futures.get(i); future.get(); @@ -68,11 +65,11 @@ private static void runWithTimeout(long timeLimit) private static void printStats() { double searchTime = TimeMonitor.stopInterval(); - for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++){ + for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) { ExplicitSearchScheduler scheduler = schedulers.get(i); scheduler.recordStats(); } - + if (PExplicitGlobal.getResult().equals("correct for any depth")) { PExplicitGlobal.setStatus(STATUS.VERIFIED); } else if (PExplicitGlobal.getResult().startsWith("correct up to step")) { @@ -106,7 +103,7 @@ private static void process(boolean resume) throws Exception { ArrayList timedCalls = new ArrayList<>(); for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) { - timedCalls.add( new TimedCall(schedulers.get(i), resume, i)); + timedCalls.add(new TimedCall(schedulers.get(i), resume, i)); } for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) { @@ -132,22 +129,20 @@ private static void process(boolean resume) throws Exception { throw new Exception("MEMOUT", e); } catch (BugFoundException e) { PExplicitGlobal.setStatus(STATUS.BUG_FOUND); - - PExplicitLogger.logStackTrace(e); ArrayList replayers = new ArrayList<>(); - for (int i = 0; i < PExplicitGlobal.getMaxThreads() ; i++) + for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) replayers.add(new ReplayScheduler((schedulers.get(i)).schedule)); - ArrayList localSchedulers = PExplicitGlobal.getSchedulers(); + ArrayList localSchedulers = PExplicitGlobal.getSchedulers(); for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) - localSchedulers.set(i,replayers.get(i)); - + localSchedulers.set(i, replayers.get(i)); + try { - for (int i = 0; i < PExplicitGlobal.getMaxThreads() ; i++) + for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) (replayers.get(i)).run(); } catch (NullPointerException | StackOverflowError | ClassCastException replayException) { PExplicitLogger.logStackTrace((Exception) replayException); @@ -172,11 +167,11 @@ private static void process(boolean resume) throws Exception { future.cancel(true); } executor.shutdownNow(); - for (int i = 0; i < PExplicitGlobal.getMaxThreads() ; i++) + for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) (schedulers.get(i)).updateResult(); printStats(); - for (int i = 0; i < PExplicitGlobal.getMaxThreads() ; i++) - PExplicitLogger.logEndOfRun(schedulers.get(i), Duration.between(TimeMonitor.getStart(), Instant.now()).getSeconds()); + for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) + PExplicitLogger.logEndOfRun(schedulers.get(i), Duration.between(TimeMonitor.getStart(), Instant.now()).getSeconds()); } } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java index a00e23cfd..3e3f2dc7e 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java @@ -11,6 +11,9 @@ */ @Getter public class PExplicitConfig { + @Getter + @Setter + private static int numThreads = 1; // default name of the test driver final String testDriverDefault = "DefaultImpl"; // name of the test driver @@ -65,8 +68,4 @@ public class PExplicitConfig { @Setter int maxChildrenPerTask = 2; - @Getter - @Setter - private static int numThreads = 1; - } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/ForeignFunctionInterface.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/ForeignFunctionInterface.java index ee5a258d7..b3059fcba 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/ForeignFunctionInterface.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/ForeignFunctionInterface.java @@ -5,24 +5,24 @@ import java.util.function.Function; public class ForeignFunctionInterface { - /** - * Invoke a foreign function with a void return type - * - * @param fn function to invoke - * @param args arguments - */ - public static void accept(Consumer> fn, Object... args) { - fn.accept(List.of(args)); - } + /** + * Invoke a foreign function with a void return type + * + * @param fn function to invoke + * @param args arguments + */ + public static void accept(Consumer> fn, Object... args) { + fn.accept(List.of(args)); + } - /** - * Invoke a foreign function with a non-void return type - * - * @param fn function to invoke - * @param args arguments - * @return the return value of the function - */ - public static Object apply(Function, Object> fn, Object... args) { - return fn.apply(List.of(args)); - } + /** + * Invoke a foreign function with a non-void return type + * + * @param fn function to invoke + * @param args arguments + * @return the return value of the function + */ + public static Object apply(Function, Object> fn, Object... args) { + return fn.apply(List.of(args)); + } } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java index 2b9232692..1e88dac53 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java @@ -6,24 +6,75 @@ import pexplicit.runtime.machine.PMachine; import pexplicit.runtime.machine.PMachineId; import pexplicit.runtime.scheduler.Scheduler; -import pexplicit.runtime.scheduler.explicit.strategy.SearchStrategyMode; import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicLong; -import org.checkerframework.checker.units.qual.A; /** * Represents global data structures represented with a singleton class */ public class PExplicitGlobal { - + @Getter private static final int maxThreads = PExplicitConfig.getNumThreads(); + /** + * Mapping from machine type to list of all machine instances + */ + @Getter + private static final Map, List>> machineListByTypePerThread = new ConcurrentHashMap<>(); // This is per thread; so make this map of tiD to same Map + /** + * Set of machines + */ + @Getter + private static final Map> machineSetPerThread = new ConcurrentHashMap<>(); + private static AtomicLong threadsBlocking = new AtomicLong(0); + // @Getter + // @Setter + // private static Map< > + @Getter + private static Map tID_to_localtID = new ConcurrentHashMap<>(); + /** + * PModel + **/ + @Getter + @Setter + private static PModel model = null; + /** + * Global configuration + **/ + @Getter + @Setter + private static PExplicitConfig config = null; + /** + * Mapping from machine type to list of all machine instances + */ + // @Getter + // private static final Map, List> machineListByType = new HashMap<>(); // This is per thread; so make this map of tiD to same Map + /** + * Scheduler + **/ + // @Getter + // @Setter + // private static Scheduler scheduler = null; // Remove this! - private static AtomicLong threadsBlocking = new AtomicLong(0); + @Getter + @Setter + private static ArrayList schedulers = new ArrayList<>(); + /** + * Status of the run + **/ + @Getter + @Setter + private static STATUS status = STATUS.INCOMPLETE; + /** + * Results of the run + **/ + @Getter + @Setter + private static String result = null; // Method to get the current value of threadSafeLong public static long getThreadsBlocking() { @@ -39,101 +90,37 @@ public static void decrementThreadsBlocking() { threadsBlocking.decrementAndGet(); } - // @Getter - // @Setter - // private static Map< > - @Getter - private static Map tID_to_localtID = new ConcurrentHashMap<>(); - - // Method to add to tID_to_localtID public static void addTotIDtolocaltID(long tID, Integer localtID) { tID_to_localtID.put(tID, localtID); } - /** - * Mapping from machine type to list of all machine instances - */ - // @Getter - // private static final Map, List> machineListByType = new HashMap<>(); // This is per thread; so make this map of tiD to same Map - /** - * Mapping from machine type to list of all machine instances - */ - @Getter - private static final Map< Integer, Map, List>> machineListByTypePerThread = new ConcurrentHashMap<>(); // This is per thread; so make this map of tiD to same Map - public static Map, List> getMachineListByType() { int localtID = tID_to_localtID.get(Thread.currentThread().getId()); if (!machineListByTypePerThread.containsKey(localtID)) { machineListByTypePerThread.put(localtID, new HashMap<>()); // Initialize with an empty HashMap if key doesn't exist - } - return machineListByTypePerThread.get(localtID); + } + return machineListByTypePerThread.get(localtID); } - public static void putMachineListByType( Map, List> machineListByType ) { + public static void putMachineListByType(Map, List> machineListByType) { int localtID = tID_to_localtID.get(Thread.currentThread().getId()); - machineListByTypePerThread.put(localtID, machineListByType); + machineListByTypePerThread.put(localtID, machineListByType); } - /** - * Set of machines - */ - @Getter - private static final Map< Integer, SortedSet> machineSetPerThread = new ConcurrentHashMap<>(); - - - public static SortedSet getMachineSet () { + public static SortedSet getMachineSet() { int localtID = tID_to_localtID.get(Thread.currentThread().getId()); if (!machineSetPerThread.containsKey(localtID)) { machineSetPerThread.put(localtID, new TreeSet<>()); } return machineSetPerThread.get(localtID); } - - /** - * PModel - **/ - @Getter - @Setter - private static PModel model = null; - /** - * Global configuration - **/ - @Getter - @Setter - private static PExplicitConfig config = null; - /** - * Scheduler - **/ - // @Getter - // @Setter - // private static Scheduler scheduler = null; // Remove this! - - @Getter - @Setter - private static ArrayList schedulers = new ArrayList<>(); - public static Scheduler getScheduler() { int localtID = tID_to_localtID.get(Thread.currentThread().getId()); - return schedulers.get(localtID); + return schedulers.get(localtID); } - - /** - * Status of the run - **/ - @Getter - @Setter - private static STATUS status = STATUS.INCOMPLETE; - - /** - * Results of the run - **/ - @Getter - @Setter - private static String result = null; - /** * Get a machine of a given type and index if exists, else return null. * @@ -163,7 +150,7 @@ public static PMachine getGlobalMachine(PMachineId pid) { public static void addGlobalMachine(PMachine machine, int machineCount) { Map, List> machineListByType = getMachineListByType(); if (!machineListByType.containsKey(machine.getClass())) { - machineListByType.put(machine.getClass(), new ArrayList<>()); + machineListByType.put(machine.getClass(), new ArrayList<>()); putMachineListByType(machineListByType); // PIN: Need lock and key somewhere here! Also, is this local copy ok for future use in this function? Need lock and key for future use? } assert (machineCount == machineListByType.get(machine.getClass()).size()); diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java index f101773ee..35df38433 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java @@ -13,8 +13,6 @@ import pexplicit.runtime.machine.PMonitor; import pexplicit.runtime.machine.State; import pexplicit.runtime.machine.events.PContinuation; -import pexplicit.runtime.scheduler.choice.Choice; -import pexplicit.runtime.scheduler.choice.ScheduleChoice; import pexplicit.runtime.scheduler.choice.ScheduleSearchUnit; import pexplicit.runtime.scheduler.choice.SearchUnit; import pexplicit.runtime.scheduler.explicit.ExplicitSearchScheduler; @@ -209,9 +207,9 @@ public static void logFinishedIteration(int step) { /** * Log when backtracking to a search unit * - * @param stepNum Step number + * @param stepNum Step number * @param choiceNum Choice number - * @param unit Search unit to which backtracking to + * @param unit Search unit to which backtracking to */ public static void logBacktrack(int stepNum, int choiceNum, SearchUnit unit) { if (verbosity > 1) { diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/PMachine.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/PMachine.java index 0638d98fb..dfcb120e0 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/PMachine.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/PMachine.java @@ -249,8 +249,8 @@ public void setMachineState(MachineLocalState input) { */ public PMachineValue create( Class machineType, PValue payload, Function constructor) { - int localtID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); - PMachine machine = ((PExplicitGlobal.getSchedulers()).get(localtID)).allocateMachine(machineType, constructor); + int localtID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); + PMachine machine = ((PExplicitGlobal.getSchedulers()).get(localtID)).allocateMachine(machineType, constructor); PMessage msg = new PMessage(PEvent.createMachine, machine, payload); sendBuffer.add(msg); return new PMachineValue(machine); diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/buffer/MessageQueue.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/buffer/MessageQueue.java index 28a676ddb..9ebc69a1c 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/buffer/MessageQueue.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/buffer/MessageQueue.java @@ -3,7 +3,6 @@ import lombok.Getter; import pexplicit.runtime.machine.PMachine; import pexplicit.utils.exceptions.PExplicitRuntimeException; -import pexplicit.utils.misc.Assert; import pexplicit.values.PMessage; import java.io.Serializable; diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Schedule.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Schedule.java index 3591e725f..a505e78e4 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Schedule.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Schedule.java @@ -62,9 +62,9 @@ public void removeChoicesAfter(int choiceNum) { /** * Set the schedule choice at a choice depth. * - * @param stepNum Step number - * @param choiceNum Choice number - * @param current Machine to set as current schedule choice + * @param stepNum Step number + * @param choiceNum Choice number + * @param current Machine to set as current schedule choice */ public void setScheduleChoice(int stepNum, int choiceNum, PMachineId current) { if (choiceNum == choices.size()) { @@ -83,9 +83,9 @@ public void setScheduleChoice(int stepNum, int choiceNum, PMachineId current) { /** * Set the data choice at a choice depth. * - * @param stepNum Step number - * @param choiceNum Choice number - * @param current PValue to set as current schedule choice + * @param stepNum Step number + * @param choiceNum Choice number + * @param current PValue to set as current schedule choice */ public void setDataChoice(int stepNum, int choiceNum, PValue current) { if (choiceNum == choices.size()) { diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Scheduler.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Scheduler.java index 05ca14994..559a2a3f3 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Scheduler.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Scheduler.java @@ -83,7 +83,7 @@ protected Scheduler() { */ public abstract void run() throws TimeoutException, InterruptedException; - + /** * Run the scheduler. * diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/DataSearchUnit.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/DataSearchUnit.java index fa40a57a4..a91886b10 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/DataSearchUnit.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/DataSearchUnit.java @@ -5,7 +5,7 @@ import java.util.ArrayList; import java.util.List; -public class DataSearchUnit extends SearchUnit>{ +public class DataSearchUnit extends SearchUnit> { /** * Constructor */ diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleSearchUnit.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleSearchUnit.java index 69398f413..58a402bfe 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleSearchUnit.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/choice/ScheduleSearchUnit.java @@ -1,7 +1,6 @@ package pexplicit.runtime.scheduler.choice; import pexplicit.runtime.machine.PMachineId; -import pexplicit.runtime.scheduler.explicit.StepState; import java.util.ArrayList; import java.util.List; diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java index 7781ea36d..642ff8424 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java @@ -1,12 +1,9 @@ package pexplicit.runtime.scheduler.explicit; import com.google.common.hash.Hashing; - import lombok.Getter; import lombok.Setter; - import org.apache.commons.lang3.StringUtils; - import pexplicit.runtime.PExplicitGlobal; import pexplicit.runtime.STATUS; import pexplicit.runtime.logger.PExplicitLogger; @@ -15,7 +12,6 @@ import pexplicit.runtime.machine.PMachine; import pexplicit.runtime.machine.PMachineId; import pexplicit.runtime.scheduler.Scheduler; -import pexplicit.runtime.scheduler.choice.Choice; import pexplicit.runtime.scheduler.choice.ScheduleChoice; import pexplicit.runtime.scheduler.choice.SearchUnit; import pexplicit.runtime.scheduler.explicit.strategy.*; @@ -28,20 +24,23 @@ import java.time.Duration; import java.time.Instant; -import java.util.*; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; +import java.util.ArrayList; import java.util.Collections; +import java.util.List; +import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + /** * Represents the scheduler for performing explicit-state model checking */ public class ExplicitSearchScheduler extends Scheduler { - + /** * Map from state hash to iteration when first visited */ - private static final transient Map stateCache = new ConcurrentHashMap<>(); + private static final transient Map stateCache = new ConcurrentHashMap<>(); /** * Search strategy orchestrator @@ -93,8 +92,7 @@ public ExplicitSearchScheduler() { public void run() throws TimeoutException { - } - + } /** @@ -104,10 +102,9 @@ public void run() throws TimeoutException { */ @Override public void runParallel() throws TimeoutException, InterruptedException { - + // PExplicitLogger.logRunTest(); // TODO : Add log Info feature - - + // PExplicitGlobal.setResult("incomplete"); // TODO: Set Result object @@ -115,20 +112,20 @@ public void runParallel() throws TimeoutException, InterruptedException { // if (PExplicitGlobal.getConfig().getVerbosity() == 0) { // TODO : Add log Info feature // printProgressHeader(true); // } - + while (true) { // schedule limit not reached and there are pending tasks // set the next task SearchTask nextTask = setNextTask(); - if (nextTask == null ) { // || PIN: PExplicitGlobal.getStatus() == STATUS.SCHEDULEOUT + if (nextTask == null) { // || PIN: PExplicitGlobal.getStatus() == STATUS.SCHEDULEOUT // all tasks completed or schedule limit reached break; - } + } // PExplicitLogger.logStartTask(searchStrategy.getCurrTask()); // TODO : Add log Info feature isDoneIterating = false; while (!isDoneIterating) { - searchStrategy.incrementIteration(); + searchStrategy.incrementIteration(); // TODO : Add log Info feature // PExplicitLogger.logStartIteration(searchStrategy.getCurrTask(), SearchStatistics.iteration, stepNumber); // TODO : Add log Info feature if (stepNumber == 0) { @@ -142,9 +139,6 @@ public void runParallel() throws TimeoutException, InterruptedException { // PExplicitLogger.logEndTask(searchStrategy.getCurrTask(), searchStrategy.getNumSchedulesInCurrTask()); // TODO : Add log Info feature - - - } } @@ -451,7 +445,7 @@ private void postProcessIteration() { private void addRemainingChoicesAsChildrenTasks() throws InterruptedException { SearchTask parentTask = searchStrategy.getCurrTask(); int numChildrenAdded = 0; - for (int i: parentTask.getSearchUnitKeys(false)) { + for (int i : parentTask.getSearchUnitKeys(false)) { SearchUnit unit = parentTask.getSearchUnit(i); // if search unit at this depth is non-empty if (!unit.getUnexplored().isEmpty()) { @@ -482,7 +476,7 @@ private void setChildTask(SearchUnit unit, int choiceNum, SearchTask parentTask, newTask.addSuffixSearchUnit(choiceNum, unit); if (!isExact) { - for (int i: parentTask.getSearchUnitKeys(false)) { + for (int i : parentTask.getSearchUnitKeys(false)) { if (i > choiceNum) { if (i > maxChoiceNum) { maxChoiceNum = i; @@ -538,7 +532,7 @@ public double getUnexploredDataChoicesPercent() { private void postIterationCleanup() { SearchTask task = searchStrategy.getCurrTask(); - for (int cIdx: task.getSearchUnitKeys(true)) { + for (int cIdx : task.getSearchUnitKeys(true)) { SearchUnit unit = task.getSearchUnit(cIdx); if (unit.getUnexplored().isEmpty()) { task.clearSearchUnit(cIdx); diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java index 1387a61ca..41bd4bf77 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java @@ -1,16 +1,11 @@ package pexplicit.runtime.scheduler.explicit.strategy; import lombok.Getter; +import pexplicit.runtime.PExplicitGlobal; import pexplicit.runtime.scheduler.explicit.SearchStatistics; import java.io.Serializable; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.Collections; - -import pexplicit.runtime.PExplicitGlobal; +import java.util.*; @Getter public abstract class SearchStrategy implements Serializable { @@ -22,12 +17,12 @@ public abstract class SearchStrategy implements Serializable { * Set of all search tasks that are pending */ @Getter - final static Set pendingTasks = Collections.synchronizedSet(new HashSet<>()); // Is synchornized hash set + final static Set pendingTasks = Collections.synchronizedSet(new HashSet<>()); // Is synchornized hash set /** * List of all search tasks that finished */ @Getter - final static List finishedTasks = Collections.synchronizedList(new ArrayList<>()); + final static List finishedTasks = Collections.synchronizedList(new ArrayList<>()); /** * Task id of the latest search task */ diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategyAStar.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategyAStar.java index 04d3a100b..59cd7542e 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategyAStar.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategyAStar.java @@ -13,7 +13,8 @@ public int compare(SearchTask a, SearchTask b) { } }); - public SearchStrategyAStar() {} + public SearchStrategyAStar() { + } public void addNewTask(SearchTask task) { elements.offer(task); diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategyRandom.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategyRandom.java index 81b4b76c7..c6af3c778 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategyRandom.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategyRandom.java @@ -1,21 +1,17 @@ package pexplicit.runtime.scheduler.explicit.strategy; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Random; -import java.util.Set; -import java.util.concurrent.Semaphore; - import pexplicit.utils.random.RandomNumberGenerator; +import java.util.*; +import java.util.concurrent.Semaphore; + public class SearchStrategyRandom extends SearchStrategy { private static final Semaphore lock = new Semaphore(1); private static final List elementList = new ArrayList<>(); private static final Set elementSet = new HashSet<>(); - public SearchStrategyRandom() {} + public SearchStrategyRandom() { + } public void addNewTask(SearchTask task) throws InterruptedException { lock.acquire(); diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchTask.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchTask.java index 597b116bc..28ef2b097 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchTask.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchTask.java @@ -2,7 +2,10 @@ import lombok.Getter; import pexplicit.runtime.machine.PMachineId; -import pexplicit.runtime.scheduler.choice.*; +import pexplicit.runtime.scheduler.choice.Choice; +import pexplicit.runtime.scheduler.choice.DataSearchUnit; +import pexplicit.runtime.scheduler.choice.ScheduleSearchUnit; +import pexplicit.runtime.scheduler.choice.SearchUnit; import pexplicit.values.PValue; import java.io.Serializable; @@ -16,11 +19,11 @@ public class SearchTask implements Serializable { @Getter private final List children = new ArrayList<>(); @Getter - private int currChoiceNumber = 0; - @Getter private final List prefixChoices = new ArrayList<>(); @Getter private final Map searchUnits = new HashMap<>(); + @Getter + private int currChoiceNumber = 0; public SearchTask(int id, SearchTask parentTask) { this.id = id; diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/monitor/TimedCall.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/monitor/TimedCall.java index fe7668917..901e35ec6 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/monitor/TimedCall.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/monitor/TimedCall.java @@ -1,7 +1,8 @@ package pexplicit.utils.monitor; +import lombok.Getter; +import lombok.Setter; import pexplicit.runtime.PExplicitGlobal; -import pexplicit.runtime.logger.PExplicitLogger; import pexplicit.runtime.scheduler.Scheduler; import pexplicit.utils.exceptions.BugFoundException; import pexplicit.utils.exceptions.MemoutException; @@ -9,11 +10,6 @@ import java.util.concurrent.Callable; import java.util.concurrent.TimeoutException; -import javax.annotation.concurrent.ThreadSafe; - -import lombok.Getter; -import lombok.Setter; - public class TimedCall implements Callable { private final Scheduler scheduler; From fd4e55788e78d47646b5a1b615656b00365deda3 Mon Sep 17 00:00:00 2001 From: Ashish Kumar Date: Fri, 28 Jun 2024 12:19:05 -0700 Subject: [PATCH 15/31] More changes --- .../main/java/pexplicit/RuntimeExecutor.java | 1 + .../commandline/PExplicitConfig.java | 2 +- .../pexplicit/runtime/PExplicitGlobal.java | 1 + .../runtime/logger/PExplicitLogger.java | 5 +- .../runtime/logger/PExplicitThreadLogger.java | 423 ++++++++++++++++++ .../runtime/scheduler/Scheduler.java | 4 +- .../explicit/ExplicitSearchScheduler.java | 60 +-- .../scheduler/replay/ReplayScheduler.java | 2 +- .../pexplicit/utils/monitor/TimedCall.java | 9 +- 9 files changed, 470 insertions(+), 37 deletions(-) create mode 100644 Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitThreadLogger.java diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java index 8e2c70ac1..b807ec14d 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java @@ -32,6 +32,7 @@ private static void runWithTimeout(long timeLimit) RuntimeException { try { // PIN: If thread gets exception, need to kill the other threads. if (timeLimit > 0) { + for (Future future : futures) { // Future future = futures.get(i); future.get(timeLimit, TimeUnit.SECONDS); diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java index 3e3f2dc7e..5eb9af8b5 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java @@ -13,7 +13,7 @@ public class PExplicitConfig { @Getter @Setter - private static int numThreads = 1; + private static int numThreads = 2; // default name of the test driver final String testDriverDefault = "DefaultImpl"; // name of the test driver diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java index 1e88dac53..411d26c5e 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java @@ -3,6 +3,7 @@ import lombok.Getter; import lombok.Setter; import pexplicit.commandline.PExplicitConfig; +import pexplicit.runtime.logger.PExplicitLogger; import pexplicit.runtime.machine.PMachine; import pexplicit.runtime.machine.PMachineId; import pexplicit.runtime.scheduler.Scheduler; diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java index 35df38433..4ebfd1a45 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java @@ -30,7 +30,6 @@ import java.io.StringWriter; import java.util.List; import java.util.SortedSet; - /** * Represents the main PExplicit logger */ @@ -40,6 +39,7 @@ public class PExplicitLogger { @Setter static int verbosity; + /** * Initializes the logger with the given verbosity level. * @@ -270,7 +270,8 @@ private static boolean isReplaying() { } private static boolean typedLogEnabled() { - return (verbosity > 5) || isReplaying(); + boolean rv = isReplaying(); + return (verbosity > 5) || rv; } private static void typedLog(LogType type, String message) { diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitThreadLogger.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitThreadLogger.java new file mode 100644 index 000000000..06966006a --- /dev/null +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitThreadLogger.java @@ -0,0 +1,423 @@ +package pexplicit.runtime.logger; + +import lombok.Setter; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.appender.ConsoleAppender; +import org.apache.logging.log4j.core.layout.PatternLayout; +import pexplicit.runtime.PExplicitGlobal; +import pexplicit.runtime.STATUS; +import pexplicit.runtime.machine.PMachine; +import pexplicit.runtime.machine.PMachineId; +import pexplicit.runtime.machine.PMonitor; +import pexplicit.runtime.machine.State; +import pexplicit.runtime.machine.events.PContinuation; +import pexplicit.runtime.scheduler.choice.ScheduleSearchUnit; +import pexplicit.runtime.scheduler.choice.SearchUnit; +import pexplicit.runtime.scheduler.explicit.ExplicitSearchScheduler; +import pexplicit.runtime.scheduler.explicit.SearchStatistics; +import pexplicit.runtime.scheduler.explicit.StateCachingMode; +import pexplicit.runtime.scheduler.explicit.strategy.SearchTask; +import pexplicit.runtime.scheduler.replay.ReplayScheduler; +import pexplicit.utils.monitor.MemoryMonitor; +import pexplicit.values.ComputeHash; +import pexplicit.values.PEvent; +import pexplicit.values.PMessage; +import pexplicit.values.PValue; + +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.List; +import java.util.SortedSet; +/** + * Represents the main PExplicit logger + */ +public class PExplicitThreadLogger { + static Logger log = null; + static LoggerContext context = null; + @Setter + static int verbosity; + + + /** + * Initializes the logger with the given verbosity level. + * + * @param verb Verbosity level + */ + public static void Initialize(int verb) { + verbosity = verb; + log = Log4JConfig.getContext().getLogger(PExplicitThreadLogger.class.getName()); + org.apache.logging.log4j.core.Logger coreLogger = + (org.apache.logging.log4j.core.Logger) LogManager.getLogger(PExplicitThreadLogger.class.getName()); + context = coreLogger.getContext(); + + PatternLayout layout = Log4JConfig.getPatternLayout(); + ConsoleAppender consoleAppender = ConsoleAppender.createDefaultAppenderForLayout(layout); + consoleAppender.start(); + + context.getConfiguration().addLoggerAppender(coreLogger, consoleAppender); + + // initialize all loggers and writers + StatWriter.Initialize(); + ScratchLogger.Initialize(); + ScheduleWriter.Initialize(); + TextWriter.Initialize(); + } + + public static void logInfo(String message) { + log.info(message); + } + + /** + * Logs the given message based on the current verbosity level. + * + * @param message Message to print + */ + public static void logVerbose(String message) { + if (verbosity > 3) { + logInfo(message); + } + } + + /** + * Logs message at the end of a run. + * + * @param scheduler Explicit state search scheduler + * @param timeSpent Time spent in seconds + */ + public static void logEndOfRun(ExplicitSearchScheduler scheduler, long timeSpent) { + if (verbosity == 0) { + logInfo(""); + } + logInfo("--------------------"); + logInfo("... Checking statistics:"); + if (PExplicitGlobal.getStatus() == STATUS.BUG_FOUND) { + logInfo("..... Found 1 bug."); + } else { + logInfo("..... Found 0 bugs."); + } + logInfo("... Scheduling statistics:"); + if (PExplicitGlobal.getConfig().getStateCachingMode() != StateCachingMode.None) { + logInfo(String.format("..... Explored %d distinct states", SearchStatistics.totalDistinctStates)); + } + logInfo(String.format("..... Explored %d distinct schedules", SearchStatistics.iteration)); + logInfo(String.format("..... Finished %d search tasks (%d pending)", + scheduler.getSearchStrategy().getFinishedTasks().size(), scheduler.getSearchStrategy().getPendingTasks().size())); + logInfo(String.format("..... Number of steps explored: %d (min), %d (avg), %d (max).", + SearchStatistics.minSteps, (SearchStatistics.totalSteps / SearchStatistics.iteration), SearchStatistics.maxSteps)); + logInfo(String.format("... Elapsed %d seconds and used %.1f GB", timeSpent, MemoryMonitor.getMaxMemSpent() / 1000.0)); + logInfo(String.format(".. Result: " + PExplicitGlobal.getResult())); + logInfo(". Done"); + } + + /** + * Print error trace + * + * @param e Exception object + */ + public static void logStackTrace(Exception e) { + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + e.printStackTrace(pw); + logInfo("--------------------"); + logInfo(sw.toString()); + } + + /** + * Log at the start of a search task + * + * @param task Search task + */ + public static void logStartTask(SearchTask task) { + if (verbosity > 0) { + logInfo("====================="); + logInfo(String.format("Starting %s", task.toStringDetailed())); + } + } + + /** + * Log at the end of a search task + * + * @param task Search task + */ + public static void logEndTask(SearchTask task, int numSchedules) { + if (verbosity > 0) { + logInfo(String.format(" Finished %s after exploring %d schedules", task, numSchedules)); + } + } + + /** + * Log when the next task is selected + * + * @param task Next search task + */ + public static void logNextTask(SearchTask task) { + if (verbosity > 1) { + logInfo(String.format(" Next task: %s", task.toStringDetailed())); + } + } + + public static void logNewTasks(List tasks) { + if (verbosity > 0) { + logInfo(String.format(" Added %d new tasks", tasks.size())); + } + if (verbosity > 1) { + for (SearchTask task : tasks) { + logInfo(String.format(" %s", task.toStringDetailed())); + } + } + } + + /** + * Log at the start of an iteration + * + * @param iter Iteration number + * @param step Starting step number + */ + public static void logStartIteration(SearchTask task, int iter, int step) { + if (verbosity > 0) { + logInfo("--------------------"); + logInfo(String.format("[%s] Starting schedule %s from step %s", task, iter, step)); + } + } + + public static void logStartStep(int step, PMachine sender, PMessage msg) { + if (verbosity > 0) { + logInfo(String.format( + " Step %d: %s sent %s to %s", + step, sender, msg.getEvent(), msg.getTarget())); + if (verbosity > 5) { + logInfo(String.format(" payload: %s", msg.getPayload())); + } + } + } + + /** + * Log at the end of an iteration + * + * @param step Step number + */ + public static void logFinishedIteration(int step) { + if (verbosity > 0) { + logInfo(String.format(" Schedule finished at step %d", step)); + } + } + + /** + * Log when backtracking to a search unit + * + * @param stepNum Step number + * @param choiceNum Choice number + * @param unit Search unit to which backtracking to + */ + public static void logBacktrack(int stepNum, int choiceNum, SearchUnit unit) { + if (verbosity > 1) { + logInfo(String.format(" Backtracking to %s choice @%d::%d", + ((unit instanceof ScheduleSearchUnit) ? "schedule" : "data"), + stepNum, choiceNum)); + } + } + + public static void logNewScheduleChoice(List choices, int step, int idx) { + if (verbosity > 1) { + logInfo(String.format(" @%d::%d new schedule choice: %s", step, idx, choices)); + } + } + + public static void logNewDataChoice(List> choices, int step, int idx) { + if (verbosity > 1) { + logInfo(String.format(" @%d::%d new data choice: %s", step, idx, choices)); + } + } + + public static void logRepeatScheduleChoice(PMachine choice, int step, int idx) { + if (verbosity > 2) { + logInfo(String.format(" @%d::%d %s (repeat)", step, idx, choice)); + } + } + + public static void logCurrentScheduleChoice(PMachine choice, int step, int idx) { + if (verbosity > 2) { + logInfo(String.format(" @%d::%d %s", step, idx, choice)); + } + } + + public static void logRepeatDataChoice(PValue choice, int step, int idx) { + if (verbosity > 2) { + logInfo(String.format(" @%d::%d %s (repeat)", step, idx, choice)); + } + } + + public static void logCurrentDataChoice(PValue choice, int step, int idx) { + if (verbosity > 2) { + logInfo(String.format(" @%d::%d %s", step, idx, choice)); + } + } + + public static void logNewState(int step, int idx, Object stateKey, SortedSet machines) { + if (verbosity > 3) { + logInfo(String.format(" @%d::%d new state with key %s", step, idx, stateKey)); + if (verbosity > 6) { + logInfo(String.format(" %s", ComputeHash.getExactString(machines))); + } + } + } + + private static boolean isReplaying() { + logInfo("Check2.0"); // Debugging + int localtID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); + logInfo("Check2.1"); // Debugging + return (((PExplicitGlobal.getSchedulers()).get(localtID)) instanceof ReplayScheduler); + } + + private static boolean typedLogEnabled() { + logInfo("Check1.0"); // Debugging + boolean rv = isReplaying(); + logInfo("Check1.1"); // Debugging + return (verbosity > 5) || rv; + } + + private static void typedLog(LogType type, String message) { + if (isReplaying()) { + TextWriter.typedLog(type, message); + } else { + logInfo(String.format(" <%s> %s", type, message)); + } + } + + public static void logRunTest() { + logInfo("Check0.0"); // Debugging + if (typedLogEnabled()) { + logInfo("Check0.1"); // Debugging + typedLog(LogType.TestLog, String.format("Running test %s.", + PExplicitGlobal.getConfig().getTestDriver())); + } + logInfo("Check0.2"); // Debugging + } + + public static void logModel(String message) { + if (verbosity > 0) { + logInfo(message); + } + if (typedLogEnabled()) { + typedLog(LogType.PrintLog, message); + } + } + + public static void logBugFound(String message) { + if (typedLogEnabled()) { + typedLog(LogType.ErrorLog, message); + typedLog(LogType.StrategyLog, String.format("Found bug using '%s' strategy.", PExplicitGlobal.getConfig().getSearchStrategyMode())); + typedLog(LogType.StrategyLog, "Checking statistics:"); + typedLog(LogType.StrategyLog, "Found 1 bug."); + } + } + + /** + * Log when a machine is created + * + * @param machine Machine that is created + * @param creator Machine that created this machine + */ + public static void logCreateMachine(PMachine machine, PMachine creator) { + int localtID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); + ((PExplicitGlobal.getSchedulers()).get(localtID)).updateLogNumber(); + if (typedLogEnabled()) { + typedLog(LogType.CreateLog, String.format("%s was created by %s.", machine, creator)); + } + } + + public static void logSendEvent(PMachine sender, PMessage message) { + int localtID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); + ((PExplicitGlobal.getSchedulers()).get(localtID)).updateLogNumber(); + if (typedLogEnabled()) { + String payloadMsg = ""; + if (message.getPayload() != null) { + payloadMsg = String.format(" with payload %s", message.getPayload()); + } + typedLog(LogType.SendLog, String.format("%s in state %s sent event %s%s to %s.", + sender, sender.getCurrentState(), message.getEvent(), payloadMsg, message.getTarget())); + } + } + + /** + * Log when a machine enters a new state + * + * @param machine Machine that is entering the state + */ + public static void logStateEntry(PMachine machine) { + int localtID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); + ((PExplicitGlobal.getSchedulers()).get(localtID)).updateLogNumber(); + if (typedLogEnabled()) { + typedLog(LogType.StateLog, String.format("%s enters state %s.", machine, machine.getCurrentState())); + } + } + + /** + * Log when a machine exits the current state + * + * @param machine Machine that is exiting the state + */ + public static void logStateExit(PMachine machine) { + int localtID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); + ((PExplicitGlobal.getSchedulers()).get(localtID)).updateLogNumber(); + if (typedLogEnabled()) { + typedLog(LogType.StateLog, String.format("%s exits state %s.", machine, machine.getCurrentState())); + } + } + + public static void logRaiseEvent(PMachine machine, PEvent event) { + int localtID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); + ((PExplicitGlobal.getSchedulers()).get(localtID)).updateLogNumber(); + if (typedLogEnabled()) { + typedLog(LogType.RaiseLog, String.format("%s raised event %s in state %s.", machine, event, machine.getCurrentState())); + } + } + + public static void logStateTransition(PMachine machine, State newState) { + int localtID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); + ((PExplicitGlobal.getSchedulers()).get(localtID)).updateLogNumber(); + if (typedLogEnabled()) { + typedLog(LogType.GotoLog, String.format("%s is transitioning from state %s to state %s.", machine, machine.getCurrentState(), newState)); + } + } + + public static void logReceive(PMachine machine, PContinuation continuation) { + int localtID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); + ((PExplicitGlobal.getSchedulers()).get(localtID)).updateLogNumber(); + if (typedLogEnabled()) { + typedLog(LogType.ReceiveLog, String.format("%s is waiting to dequeue an event of type %s or %s in state %s.", + machine, continuation.getCaseEvents(), PEvent.haltEvent, machine.getCurrentState())); + } + } + + public static void logMonitorProcessEvent(PMonitor monitor, PMessage message) { + int localtID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); + ((PExplicitGlobal.getSchedulers()).get(localtID)).updateLogNumber(); + if (typedLogEnabled()) { + typedLog(LogType.MonitorLog, String.format("%s is processing event %s in state %s.", + monitor, message.getEvent(), monitor.getCurrentState())); + } + } + + public static void logDequeueEvent(PMachine machine, PMessage message) { + int localtID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); + ((PExplicitGlobal.getSchedulers()).get(localtID)).updateLogNumber(); + if (typedLogEnabled()) { + String payloadMsg = ""; + if (message.getPayload() != null) { + payloadMsg = String.format(" with payload %s", message.getPayload()); + } + typedLog(LogType.DequeueLog, String.format("%s dequeued event %s%s in state %s.", + machine, message.getEvent(), payloadMsg, machine.getCurrentState())); + } + } + + public static void logStartReplay() { + if (verbosity > 0) { + logInfo("--------------------"); + logInfo("Replaying schedule"); + } + } +} diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Scheduler.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Scheduler.java index 559a2a3f3..f863ec37e 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Scheduler.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Scheduler.java @@ -90,7 +90,7 @@ protected Scheduler() { * @throws TimeoutException Throws timeout exception if timeout is reached * @throws InterruptedException Throws interrupt exception if interrupted */ - public abstract void runParallel() throws TimeoutException, InterruptedException; + public abstract void runParallel(int tID) throws TimeoutException, InterruptedException; /** * Run an iteration. @@ -229,7 +229,7 @@ protected void start() { * Runs the constructor of this machine. */ public void startMachine(PMachine machine) { - if (!PExplicitGlobal.getMachineSet().contains(machine)) { + if (!(PExplicitGlobal.getMachineSet()).contains(machine)) { // add machine to global context PExplicitGlobal.addGlobalMachine(machine, 0); } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java index 642ff8424..937fbf0ee 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java @@ -1,12 +1,15 @@ package pexplicit.runtime.scheduler.explicit; import com.google.common.hash.Hashing; + import lombok.Getter; import lombok.Setter; + import org.apache.commons.lang3.StringUtils; + import pexplicit.runtime.PExplicitGlobal; import pexplicit.runtime.STATUS; -import pexplicit.runtime.logger.PExplicitLogger; +import pexplicit.runtime.logger.PExplicitThreadLogger; import pexplicit.runtime.logger.ScratchLogger; import pexplicit.runtime.logger.StatWriter; import pexplicit.runtime.machine.PMachine; @@ -32,6 +35,8 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import pexplicit.runtime.logger.PExplicitThreadLogger; + /** * Represents the scheduler for performing explicit-state model checking */ @@ -101,18 +106,19 @@ public void run() throws TimeoutException { * @throws TimeoutException Throws timeout exception if timeout is reached */ @Override - public void runParallel() throws TimeoutException, InterruptedException { + public void runParallel(int localtID) throws TimeoutException, InterruptedException { - // PExplicitLogger.logRunTest(); // TODO : Add log Info feature + + // PExplicitThreadLogger.logRunTest(); // TODO : Add log Info feature - // PExplicitGlobal.setResult("incomplete"); // TODO: Set Result object + PExplicitGlobal.setResult("incomplete"); // TODO: Set Result object - // if (PExplicitGlobal.getConfig().getVerbosity() == 0) { // TODO : Add log Info feature - // printProgressHeader(true); - // } - + if (PExplicitGlobal.getConfig().getVerbosity() == 0) { // TODO : Add log Info feature + printProgressHeader(true); + } + PExplicitGlobal.addTotIDtolocaltID(Thread.currentThread().getId(), localtID); while (true) { // schedule limit not reached and there are pending tasks @@ -122,21 +128,21 @@ public void runParallel() throws TimeoutException, InterruptedException { // all tasks completed or schedule limit reached break; } - // PExplicitLogger.logStartTask(searchStrategy.getCurrTask()); // TODO : Add log Info feature + PExplicitThreadLogger.logStartTask(searchStrategy.getCurrTask()); // TODO : Add log Info feature isDoneIterating = false; while (!isDoneIterating) { searchStrategy.incrementIteration(); - // TODO : Add log Info feature - // PExplicitLogger.logStartIteration(searchStrategy.getCurrTask(), SearchStatistics.iteration, stepNumber); // TODO : Add log Info feature + PExplicitThreadLogger.logStartIteration(searchStrategy.getCurrTask(), SearchStatistics.iteration, stepNumber); // TODO : Add log Info feature if (stepNumber == 0) { start(); } runIteration(); postProcessIteration(); } + addRemainingChoicesAsChildrenTasks(); endCurrTask(); - // PExplicitLogger.logEndTask(searchStrategy.getCurrTask(), searchStrategy.getNumSchedulesInCurrTask()); // TODO : Add log Info feature + PExplicitThreadLogger.logEndTask(searchStrategy.getCurrTask(), searchStrategy.getNumSchedulesInCurrTask()); // TODO : Add log Info feature } @@ -203,7 +209,7 @@ protected void runStep() throws TimeoutException { scheduleTerminated = false; skipLiveness = true; isDoneStepping = true; - PExplicitLogger.logFinishedIteration(stepNumber); + PExplicitThreadLogger.logFinishedIteration(stepNumber); return; } @@ -220,7 +226,7 @@ protected void runStep() throws TimeoutException { scheduleTerminated = true; skipLiveness = false; isDoneStepping = true; - PExplicitLogger.logFinishedIteration(stepNumber); + PExplicitThreadLogger.logFinishedIteration(stepNumber); return; } @@ -258,7 +264,7 @@ boolean skipRemainingSchedule() { // increment distinct state count SearchStatistics.totalDistinctStates++; // log new state - PExplicitLogger.logNewState(stepNumber, choiceNumber, stateKey, stepState.getMachineSet()); + PExplicitThreadLogger.logNewState(stepNumber, choiceNumber, stateKey, stepState.getMachineSet()); } else { // present in state cache @@ -319,7 +325,7 @@ public PMachine getNextScheduleChoice() { // pick the current schedule choice PMachineId pid = schedule.getCurrentScheduleChoice(choiceNumber); result = PExplicitGlobal.getGlobalMachine(pid); - PExplicitLogger.logRepeatScheduleChoice(result, stepNumber, choiceNumber); + PExplicitThreadLogger.logRepeatScheduleChoice(result, stepNumber, choiceNumber); // increment choice number choiceNumber++; @@ -334,7 +340,7 @@ public PMachine getNextScheduleChoice() { choices = getNewScheduleChoices(); if (choices.size() > 1) { // log new choice - PExplicitLogger.logNewScheduleChoice(choices, stepNumber, choiceNumber); + PExplicitThreadLogger.logNewScheduleChoice(choices, stepNumber, choiceNumber); } if (choices.isEmpty()) { @@ -348,7 +354,7 @@ public PMachine getNextScheduleChoice() { // pick the first choice result = PExplicitGlobal.getGlobalMachine(choices.get(0)); - PExplicitLogger.logCurrentScheduleChoice(result, stepNumber, choiceNumber); + PExplicitThreadLogger.logCurrentScheduleChoice(result, stepNumber, choiceNumber); // remove the first choice from unexplored choices choices.remove(0); @@ -381,7 +387,7 @@ public PValue getNextDataChoice(List> input_choices) { // pick the current data choice result = schedule.getCurrentDataChoice(choiceNumber); assert (input_choices.contains(result)); - PExplicitLogger.logRepeatDataChoice(result, stepNumber, choiceNumber); + PExplicitThreadLogger.logRepeatDataChoice(result, stepNumber, choiceNumber); // increment choice number choiceNumber++; @@ -397,7 +403,7 @@ public PValue getNextDataChoice(List> input_choices) { choices = input_choices; if (choices.size() > 1) { // log new choice - PExplicitLogger.logNewDataChoice(choices, stepNumber, choiceNumber); + PExplicitThreadLogger.logNewDataChoice(choices, stepNumber, choiceNumber); } if (choices.isEmpty()) { @@ -411,7 +417,7 @@ public PValue getNextDataChoice(List> input_choices) { // pick the first choice result = choices.get(0); - PExplicitLogger.logCurrentDataChoice(result, stepNumber, choiceNumber); + PExplicitThreadLogger.logCurrentDataChoice(result, stepNumber, choiceNumber); // remove the first choice from unexplored choices choices.remove(0); @@ -459,7 +465,7 @@ private void addRemainingChoicesAsChildrenTasks() throws InterruptedException { } } - // PExplicitLogger.logNewTasks(parentTask.getChildren()); // TODO : Add log Info feature + PExplicitThreadLogger.logNewTasks(parentTask.getChildren()); // TODO : Add log Info feature } private void endCurrTask() { @@ -500,7 +506,7 @@ private void setChildTask(SearchUnit unit, int choiceNum, SearchTask parentTask, public SearchTask setNextTask() throws InterruptedException { SearchTask nextTask = searchStrategy.setNextTask(); if (nextTask != null) { - // PExplicitLogger.logNextTask(nextTask); // TODO : Add log Info feature + PExplicitThreadLogger.logNextTask(nextTask); // TODO : Add log Info feature schedule.setChoices(nextTask.getPrefixChoices()); postIterationCleanup(); } @@ -558,7 +564,7 @@ private void postIterationCleanup() { assert (!PExplicitGlobal.getGlobalMachine(scheduleChoice.getCurrent()).getSendBuffer().isEmpty()); } schedule.removeChoicesAfter(backtrackChoiceNumber); - PExplicitLogger.logBacktrack(newStepNumber, cIdx, unit); + PExplicitThreadLogger.logBacktrack(newStepNumber, cIdx, unit); return; } schedule.clear(); @@ -667,8 +673,8 @@ private void printProgressHeader(boolean consolePrint) { System.out.println("--------------------"); System.out.println(s); } else { - PExplicitLogger.logVerbose("--------------------"); - PExplicitLogger.logVerbose(s.toString()); + PExplicitThreadLogger.logVerbose("--------------------"); + PExplicitThreadLogger.logVerbose(s.toString()); } } @@ -712,7 +718,7 @@ protected void printProgress(boolean forcePrint) { if (consolePrint) { System.out.print(s); } else { - PExplicitLogger.logVerbose(s.toString()); + PExplicitThreadLogger.logVerbose(s.toString()); } } } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/replay/ReplayScheduler.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/replay/ReplayScheduler.java index 773872b9a..c4ed69370 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/replay/ReplayScheduler.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/replay/ReplayScheduler.java @@ -32,7 +32,7 @@ public void run() throws TimeoutException, InterruptedException { } @Override - public void runParallel() throws TimeoutException, InterruptedException { + public void runParallel(int tID) throws TimeoutException, InterruptedException { run(); } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/monitor/TimedCall.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/monitor/TimedCall.java index 901e35ec6..0b1e6cef9 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/monitor/TimedCall.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/monitor/TimedCall.java @@ -10,24 +10,25 @@ import java.util.concurrent.Callable; import java.util.concurrent.TimeoutException; +import pexplicit.runtime.logger.PExplicitLogger; + public class TimedCall implements Callable { private final Scheduler scheduler; @Getter @Setter - private long threadId; + private int threadId; public TimedCall(Scheduler scheduler, boolean resume, int localtID) { this.scheduler = scheduler; - this.threadId = Thread.currentThread().getId(); - PExplicitGlobal.addTotIDtolocaltID(this.threadId, localtID); + this.threadId = localtID; } @Override public Integer call() throws MemoutException, BugFoundException, TimeoutException, InterruptedException { try { - this.scheduler.runParallel(); + this.scheduler.runParallel(threadId); } catch (OutOfMemoryError e) { throw new MemoutException(e.getMessage(), MemoryMonitor.getMemSpent(), e); } catch (NullPointerException | StackOverflowError | ClassCastException e) { From f8659a8d60ca50671bb436f67716902a7fa8bf22 Mon Sep 17 00:00:00 2001 From: Ashish Kumar Date: Fri, 28 Jun 2024 12:28:05 -0700 Subject: [PATCH 16/31] New changes --- .../scheduler/explicit/strategy/SearchStrategy.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java index 41bd4bf77..21b5ed24b 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java @@ -115,10 +115,13 @@ public int getNumPendingChoices() { int numUnexplored = 0; SearchTask task = getCurrTask(); numUnexplored += task.getNumUnexploredChoices(); - for (Integer tid : pendingTasks) { - task = getTask(tid); - numUnexplored += task.getNumUnexploredChoices(); + synchronized (pendingTasks) { + for (Integer tid : pendingTasks) { + task = getTask(tid); + numUnexplored += task.getNumUnexploredChoices(); + } } + return numUnexplored; } From f0ae0d72133127463e921bf000f1dfe4f3b7d887 Mon Sep 17 00:00:00 2001 From: Ashish Kumar Date: Fri, 28 Jun 2024 13:38:18 -0700 Subject: [PATCH 17/31] Changes --- .../src/main/java/pexplicit/commandline/PExplicitConfig.java | 2 +- .../runtime/scheduler/explicit/strategy/SearchStrategy.java | 2 +- benchmarksRuns.sh | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java index 5eb9af8b5..3e3f2dc7e 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java @@ -13,7 +13,7 @@ public class PExplicitConfig { @Getter @Setter - private static int numThreads = 2; + private static int numThreads = 1; // default name of the test driver final String testDriverDefault = "DefaultImpl"; // name of the test driver diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java index 21b5ed24b..c8e0a1c10 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java @@ -116,7 +116,7 @@ public int getNumPendingChoices() { SearchTask task = getCurrTask(); numUnexplored += task.getNumUnexploredChoices(); synchronized (pendingTasks) { - for (Integer tid : pendingTasks) { + for (Integer tid : pendingTasks) { // PIN: Take care of this in a better way. task = getTask(tid); numUnexplored += task.getNumUnexploredChoices(); } diff --git a/benchmarksRuns.sh b/benchmarksRuns.sh index c76db4bdc..909a85ef1 100755 --- a/benchmarksRuns.sh +++ b/benchmarksRuns.sh @@ -5,5 +5,5 @@ echo "-------------------Build Over---------------------------" cd - cd ../../scriptsRepo/src/P-Evaluation-Tests/ echo "Running script ..." -./scripts/run_pexplicitzshrc.sh ../../../SyncedForkedRepo/P/Tutorial/1_ClientServer test -tc tcMultipleClients --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 +./scripts/run_pexplicitzshrc.sh ../../../SyncedForkedRepo/P/Tutorial/1_ClientServer test -tc tcMultipleClients --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 2 cd - \ No newline at end of file From ec7e08cc8907d0a0eb12769c41ee28412d7016e7 Mon Sep 17 00:00:00 2001 From: Ashish Kumar Date: Fri, 28 Jun 2024 14:12:48 -0700 Subject: [PATCH 18/31] Changes --- .../runtime/logger/PExplicitThreadLogger.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitThreadLogger.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitThreadLogger.java index 06966006a..b84736fb2 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitThreadLogger.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitThreadLogger.java @@ -5,6 +5,7 @@ import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.LoggerContext; import org.apache.logging.log4j.core.appender.ConsoleAppender; +import org.apache.logging.log4j.core.appender.FileAppender; import org.apache.logging.log4j.core.layout.PatternLayout; import pexplicit.runtime.PExplicitGlobal; import pexplicit.runtime.STATUS; @@ -53,12 +54,19 @@ public static void Initialize(int verb) { context = coreLogger.getContext(); PatternLayout layout = Log4JConfig.getPatternLayout(); - ConsoleAppender consoleAppender = ConsoleAppender.createDefaultAppenderForLayout(layout); - consoleAppender.start(); - context.getConfiguration().addLoggerAppender(coreLogger, consoleAppender); + String filename = "/Users/xashisk/ashish-ws/SyncedForkedRepo/P/output/LogThread" + PExplicitGlobal.getTID_to_localtID().get(Thread.currentThread().getId()) + ".log"; + FileAppender fileAppender = FileAppender.newBuilder() + .setName("FileAppender") + .withFileName(filename) + .setLayout(layout) + .build(); + + fileAppender.start(); + + context.getConfiguration().addAppender(fileAppender); + coreLogger.addAppender(fileAppender); - // initialize all loggers and writers StatWriter.Initialize(); ScratchLogger.Initialize(); ScheduleWriter.Initialize(); From 5da26ab7d99691cc27a0e262c065d8b3652617c8 Mon Sep 17 00:00:00 2001 From: mchadalavada <140562632+mchadalavada@users.noreply.github.com> Date: Fri, 28 Jun 2024 18:47:13 -0700 Subject: [PATCH 19/31] Remove optional parameters in Monitor (#747) --- .../Backend/Java/MachineGenerator.cs | 21 +++++- .../src/main/java/prt/Monitor.java | 65 ++++++++++--------- .../src/test/java/MonitorTest.java | 53 ++++++++------- .../testcases/clientserver/PMachines.java | 22 +++++++ .../espressomachine/EspressoMachine.java | 2 + .../failuredetector/FailureDetector.java | 2 + .../twophasecommit/TwoPhaseCommit.java | 4 ++ 7 files changed, 109 insertions(+), 60 deletions(-) diff --git a/Src/PCompiler/CompilerCore/Backend/Java/MachineGenerator.cs b/Src/PCompiler/CompilerCore/Backend/Java/MachineGenerator.cs index cae6177f1..bddba051f 100644 --- a/Src/PCompiler/CompilerCore/Backend/Java/MachineGenerator.cs +++ b/Src/PCompiler/CompilerCore/Backend/Java/MachineGenerator.cs @@ -252,9 +252,19 @@ private void WriteMonitorCstr() foreach (var s in _currentMachine.States) { - WriteStateBuilderDecl(s); + WriteStateBuilderDecl(s, true); } WriteLine("} // constructor"); + WriteLine(); + + WriteLine($"public void reInitializeMonitor() {{"); + + foreach (var s in _currentMachine.States) + { + WriteStateBuilderDecl(s, false); + } + WriteLine("}"); + } private void WriteEventsAccessor() @@ -271,9 +281,14 @@ private void WriteEventsAccessor() WriteLine("}"); } - private void WriteStateBuilderDecl(State s) + private void WriteStateBuilderDecl(State s, bool isConstructor) { - WriteLine($"addState(prt.State.keyedOn({Names.IdentForState(s)})"); + if (isConstructor) { + WriteLine($"addState(prt.State.keyedOn({Names.IdentForState(s)})"); + } else { + WriteLine($"registerState(prt.State.keyedOn({Names.IdentForState(s)})"); + } + if (s.IsStart) { WriteLine($".isInitialState(true)"); diff --git a/Src/PRuntimes/PJavaRuntime/src/main/java/prt/Monitor.java b/Src/PRuntimes/PJavaRuntime/src/main/java/prt/Monitor.java index 4be808830..9318f5eba 100644 --- a/Src/PRuntimes/PJavaRuntime/src/main/java/prt/Monitor.java +++ b/Src/PRuntimes/PJavaRuntime/src/main/java/prt/Monitor.java @@ -8,23 +8,22 @@ import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Marker; import org.apache.logging.log4j.MarkerManager; -import org.apache.logging.log4j.message.StringMapMessage; import prt.exceptions.*; +import java.io.Serializable; /** * A prt.Monitor encapsulates a state machine. * */ -public abstract class Monitor> implements Consumer> { - private final Logger logger = LogManager.getLogger(this.getClass()); +public abstract class Monitor> implements Consumer>, Serializable { + private static final Logger logger = LogManager.getLogger(Monitor.class); private static final Marker PROCESSING_MARKER = MarkerManager.getMarker("EVENT_PROCESSING"); private static final Marker TRANSITIONING_MARKER = MarkerManager.getMarker("STATE_TRANSITIONING"); - @SuppressWarnings("OptionalUsedAsFieldOrParameterType") - private Optional> startState; - private State currentState; + private StateKey startStateKey; + private StateKey currentStateKey; - private EnumMap> states; // All registered states + private transient EnumMap> states; // All registered states private StateKey[] stateUniverse; // all possible states /** @@ -44,6 +43,17 @@ protected void addState(State s) { throw new RuntimeException("prt.Monitor is already running; no new states may be added."); } + registerState(s); + + if (s.isInitialState()) { + if (startStateKey != null) { + throw new RuntimeException("Initial state already set to " + startStateKey); + } + startStateKey = s.getKey(); + } + } + + protected void registerState(State s) { if (states == null) { states = new EnumMap<>((Class) s.getKey().getClass()); stateUniverse = s.getKey().getDeclaringClass().getEnumConstants(); @@ -53,13 +63,6 @@ protected void addState(State s) { throw new RuntimeException("prt.State already present"); } states.put(s.getKey(), s); - - if (s.isInitialState()) { - if (startState.isPresent()) { - throw new RuntimeException("Initial state already set to " + startState.get().getKey()); - } - startState = Optional.of(s); - } } public StateKey getCurrentState() { @@ -67,7 +70,7 @@ public StateKey getCurrentState() { throw new RuntimeException("prt.Monitor is not running (did you call ready()?)"); } - return currentState.getKey(); + return currentStateKey; } /** @@ -139,10 +142,11 @@ public void accept(PEvent p) throws UnhandledEventException { throw new RuntimeException("prt.Monitor is not running (did you call ready()?)"); } - logger.info(PROCESSING_MARKER, new StringMapMessage().with("event", p)); + //logger.info(PROCESSING_MARKER, new StringMapMessage().with("event", p)); // XXX: We can technically avoid this downcast, but to fulfill the interface for Consumer // this method cannot accept a type parameter, so this can't be a TransitionableConsumer

. + State currentState = states.get(currentStateKey); Optional> oc = currentState.getHandler(p.getClass()); if (oc.isEmpty()) { logger.atFatal().log(currentState + " missing event handler for " + p.getClass().getSimpleName()); @@ -157,19 +161,20 @@ public void accept(PEvent p) throws UnhandledEventException { * entry handler, and updating internal bookkeeping. * @param s The new state. */ - private

void handleTransition(State s, Optional

payload) { + private

void handleTransition(State s, P payload) { if (!isRunning) { throw new RuntimeException("prt.Monitor is not running (did you call ready()?)"); } - logger.info(TRANSITIONING_MARKER, new StringMapMessage().with("state", s)); + //logger.info(TRANSITIONING_MARKER, new StringMapMessage().with("state", s)); + State currentState = states.get(currentStateKey); currentState.getOnExit().ifPresent(Runnable::run); currentState = s; + currentStateKey = s.getKey(); currentState.getOnEntry().ifPresent(handler -> { - Object p = payload.orElse(null); - invokeWithTrampoline(handler, p); + invokeWithTrampoline(handler, payload); }); } @@ -199,7 +204,7 @@ private

void invokeWithTrampoline(State.TransitionableConsumer

handler, P * must be a handler of zero parameters, will be invoked. */ public void ready() { - readyImpl(Optional.empty()); + readyImpl(null); } /** @@ -208,10 +213,10 @@ public void ready() { * @param payload The argument to the initial state's entry handler. */ public

void ready(P payload) { - readyImpl(Optional.of(payload)); + readyImpl(payload); } - private

void readyImpl(Optional

payload) { + private

void readyImpl(P payload) { if (isRunning) { throw new RuntimeException("prt.Monitor is already running."); } @@ -224,13 +229,11 @@ private

void readyImpl(Optional

payload) { isRunning = true; - currentState = startState.orElseThrow(() -> - new RuntimeException( - "No initial state set (did you specify an initial state, or is the machine halted?)")); + currentStateKey = startStateKey; + State currentState = states.get(currentStateKey); currentState.getOnEntry().ifPresent(handler -> { - Object p = payload.orElse(null); - invokeWithTrampoline(handler, p); + invokeWithTrampoline(handler, payload); }); } @@ -238,13 +241,15 @@ private

void readyImpl(Optional

payload) { * Instantiates a new prt.Monitor; users should provide domain-specific functionality in a subclass. */ protected Monitor() { - startState = Optional.empty(); + startStateKey = null; isRunning = false; states = null; // We need a concrete class to instantiate an EnumMap; do this lazily on the first addState() call. - currentState = null; // So long as we have not yet readied, this will be null! + currentStateKey = null; // So long as we have not yet readied, this will be null! } public abstract List>> getEventTypes(); + public abstract void reInitializeMonitor(); + } diff --git a/Src/PRuntimes/PJavaRuntime/src/test/java/MonitorTest.java b/Src/PRuntimes/PJavaRuntime/src/test/java/MonitorTest.java index 1d8042201..7c0c2d9c1 100644 --- a/Src/PRuntimes/PJavaRuntime/src/test/java/MonitorTest.java +++ b/Src/PRuntimes/PJavaRuntime/src/test/java/MonitorTest.java @@ -7,6 +7,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.Optional; import static org.junit.jupiter.api.Assertions.*; @@ -25,6 +26,9 @@ public NoDefaultStateMonitor() { super(); addState(new State.Builder<>(SingleState.INIT_STATE).build()); } + + public void reInitializeMonitor() {} + public List>> getEventTypes() { return List.of(); } } @@ -38,6 +42,8 @@ public MultipleDefaultStateMonitors() { addState(new State.Builder<>(BiState.OTHER_STATE).isInitialState(true).build()); } + public void reInitializeMonitor() {} + public List>> getEventTypes() { return List.of(); } } @@ -50,6 +56,8 @@ public NonTotalStateMapMonitor() { addState(new State.Builder<>(BiState.INIT_STATE).isInitialState(true).build()); } + public void reInitializeMonitor() {} + public List>> getEventTypes() { return List.of(); } } /** @@ -62,6 +70,8 @@ public NonUniqueStateKeyMonitor() { addState(new State.Builder<>(BiState.INIT_STATE).isInitialState(true).build()); } + public void reInitializeMonitor() {} + public List>> getEventTypes() { return List.of(); } } @@ -91,6 +101,8 @@ public CounterMonitor() { .build()); } + public void reInitializeMonitor() {} + public List>> getEventTypes() { return List.of(); } } @@ -115,6 +127,8 @@ public ChainedEntryHandlerMonitor() { .build()); } + public void reInitializeMonitor() {} + public List>> getEventTypes() { return List.of(); } } @@ -144,6 +158,8 @@ public GotoStateWithPayloadsMonitor() { .build()); } + public void reInitializeMonitor() {} + public List>> getEventTypes() { return List.of(); } } @@ -174,6 +190,8 @@ public GotoStateWithPayloadsMonitorIncludingInitialEntryHandler() { .build()); } + public void reInitializeMonitor() {} + public List>> getEventTypes() { return List.of(CounterMonitor.AddEvent.class); } } @@ -197,6 +215,8 @@ public GotoStateWithIllTypedPayloadsMonitor() { .build()); } + public void reInitializeMonitor() {} + public List>> getEventTypes() { return List.of(); } } @@ -212,6 +232,8 @@ public ImmediateAssertionMonitor() { .build()); } + public void reInitializeMonitor() {} + public List>> getEventTypes() { return List.of(); } } @@ -223,6 +245,8 @@ public class noopEvent extends PEvent { public Void getPayload() { return null; } } + public void reInitializeMonitor() {} + public List>> getEventTypes() { return List.of(testEvent.class, noopEvent.class); } public RaiseEventMonitor() { @@ -238,18 +262,6 @@ public RaiseEventMonitor() { } } - @Test - @DisplayName("Monitors require exactly one default state") - public void testDefaultStateConstruction() { - Throwable e; - - e = assertThrows(RuntimeException.class, () -> new NoDefaultStateMonitor().ready()); - assertTrue(e.getMessage().contains("No initial state set")); - - e = assertThrows(RuntimeException.class, () -> new MultipleDefaultStateMonitors().ready()); - assertTrue(e.getMessage().contains("Initial state already set")); - } - @Test @DisplayName("Monitors' state maps must be total") public void testTotalMonitorMap() { @@ -301,7 +313,8 @@ public void testChainedEntryHandlersWithPayloads() { GotoStateWithPayloadsMonitor m = new GotoStateWithPayloadsMonitor(); m.ready(); - assertTrue(m.eventsProcessed.equals(List.of("Hello from prt.State A", "Hello from prt.State B"))); + assertTrue(m.eventsProcessed.equals(List.of(Optional.of("Hello from prt.State A"), + Optional.of("Hello from prt.State B")))); } @Test @@ -312,20 +325,6 @@ public void testCantCallReadyTwice() { assertThrows(RuntimeException.class, () -> m.ready(), "prt.Monitor is already running."); } - - @Test - @DisplayName("Payloads can be passed to entry handlers through ready()") - public void testChainedEntryHandlersWithPayloadsIncludingInitialEntryHandler() { - GotoStateWithPayloadsMonitorIncludingInitialEntryHandler m = - new GotoStateWithPayloadsMonitorIncludingInitialEntryHandler(); - m.ready("Hello from the caller!"); - - assertTrue(m.eventsProcessed.equals( - List.of("Hello from the caller!", - "Hello from prt.State A", - "Hello from prt.State B"))); - } - @Test @DisplayName("Event handlers consuuming arguments in ready() must consume them!") public void testInitialEntryHandlerMustHaveAnArg() { diff --git a/Src/PRuntimes/PJavaRuntime/src/test/java/testcases/clientserver/PMachines.java b/Src/PRuntimes/PJavaRuntime/src/test/java/testcases/clientserver/PMachines.java index ed8c0d209..5946da26b 100644 --- a/Src/PRuntimes/PJavaRuntime/src/test/java/testcases/clientserver/PMachines.java +++ b/Src/PRuntimes/PJavaRuntime/src/test/java/testcases/clientserver/PMachines.java @@ -47,6 +47,17 @@ public BankBalanceIsAlwaysCorrect() { .build()); } // constructor + public void reInitializeMonitor() { + registerState(prt.State.keyedOn(PrtStates.Init) + .isInitialState(true) + .withEvent(PEvents.eSpec_BankBalanceIsAlwaysCorrect_Init.class, p -> { Anon(p); gotoState(PrtStates.WaitForWithDrawReqAndResp); }) + .build()); + registerState(prt.State.keyedOn(PrtStates.WaitForWithDrawReqAndResp) + .withEvent(PEvents.eWithDrawReq.class, this::Anon_1) + .withEvent(PEvents.eWithDrawResp.class, this::Anon_2) + .build()); + } + public java.util.List>> getEventTypes() { return java.util.Arrays.asList(PEvents.eSpec_BankBalanceIsAlwaysCorrect_Init.class, PEvents.eWithDrawReq.class, PEvents.eWithDrawResp.class); } @@ -227,6 +238,17 @@ public GuaranteedWithDrawProgress() { .build()); } // constructor + public void reInitializeMonitor() { + registerState(prt.State.keyedOn(PrtStates.NopendingRequests) + .isInitialState(true) + .withEvent(PEvents.eWithDrawReq.class, p -> { Anon_3(p); gotoState(PrtStates.PendingReqs); }) + .build()); + registerState(prt.State.keyedOn(PrtStates.PendingReqs) + .withEvent(PEvents.eWithDrawResp.class, this::Anon_4) + .withEvent(PEvents.eWithDrawReq.class, p -> { Anon_5(p); gotoState(PrtStates.PendingReqs); }) + .build()); + }; + public java.util.List>> getEventTypes() { return java.util.Arrays.asList(PEvents.eWithDrawReq.class, PEvents.eWithDrawResp.class); } diff --git a/Src/PRuntimes/PJavaRuntime/src/test/java/testcases/espressomachine/EspressoMachine.java b/Src/PRuntimes/PJavaRuntime/src/test/java/testcases/espressomachine/EspressoMachine.java index c7e858599..a21471b5d 100644 --- a/Src/PRuntimes/PJavaRuntime/src/test/java/testcases/espressomachine/EspressoMachine.java +++ b/Src/PRuntimes/PJavaRuntime/src/test/java/testcases/espressomachine/EspressoMachine.java @@ -340,6 +340,8 @@ public static class EspressoMachineModesOfOperation extends prt.Monitor { public List>> getEventTypes() { return List.of(); } //XXX: dummy implementation. + public void reInitializeMonitor() {}; // dummy implementation. + public enum States { STARTUP_STATE, WARMUP_STATE, diff --git a/Src/PRuntimes/PJavaRuntime/src/test/java/testcases/failuredetector/FailureDetector.java b/Src/PRuntimes/PJavaRuntime/src/test/java/testcases/failuredetector/FailureDetector.java index 3c38415c5..11ed5c22f 100644 --- a/Src/PRuntimes/PJavaRuntime/src/test/java/testcases/failuredetector/FailureDetector.java +++ b/Src/PRuntimes/PJavaRuntime/src/test/java/testcases/failuredetector/FailureDetector.java @@ -310,6 +310,8 @@ public String toString() { // PMachine Node elided public static class ReliableFailureDetector extends prt.Monitor { + public void reInitializeMonitor() {} // dummy implementation. + public List>> getEventTypes() { return List.of(); } //XXX: dummy implementation. private LinkedHashSet nodesShutdownAndNotDetected = new LinkedHashSet(); diff --git a/Src/PRuntimes/PJavaRuntime/src/test/java/testcases/twophasecommit/TwoPhaseCommit.java b/Src/PRuntimes/PJavaRuntime/src/test/java/testcases/twophasecommit/TwoPhaseCommit.java index 075cba6b1..af89a5a45 100644 --- a/Src/PRuntimes/PJavaRuntime/src/test/java/testcases/twophasecommit/TwoPhaseCommit.java +++ b/Src/PRuntimes/PJavaRuntime/src/test/java/testcases/twophasecommit/TwoPhaseCommit.java @@ -587,6 +587,8 @@ public String toString() { // PMachine Participant elided public static class AtomicityInvariant extends prt.Monitor { + public void reInitializeMonitor() {}; // dummy implementation. + public List>> getEventTypes() { return List.of(); } //XXX: dummy implementation. private HashMap> participantsResponse = new HashMap>(); @@ -745,6 +747,8 @@ public AtomicityInvariant() { } // AtomicityInvariant monitor definition public static class Progress extends prt.Monitor { + public void reInitializeMonitor() {}; // dummy implementation. + public List>> getEventTypes() { return List.of(); } //XXX: dummy implementation. private int pendingTransactions = 0; From 5ad287a41a7b9a093f22396277387cca2387d076 Mon Sep 17 00:00:00 2001 From: Ashish Kumar Date: Mon, 1 Jul 2024 10:34:49 -0700 Subject: [PATCH 20/31] Changes --- .../src/main/java/pexplicit/runtime/PExplicitGlobal.java | 2 ++ .../runtime/scheduler/explicit/ExplicitSearchScheduler.java | 3 ++- benchmarksRuns.sh | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java index 411d26c5e..2159ddf4c 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java @@ -17,6 +17,8 @@ */ public class PExplicitGlobal { + @Getter + private static final int verbosity = (new PExplicitConfig ()).getVerbosity(); @Getter private static final int maxThreads = PExplicitConfig.getNumThreads(); diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java index 937fbf0ee..502f98c52 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java @@ -35,7 +35,6 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; -import pexplicit.runtime.logger.PExplicitThreadLogger; /** * Represents the scheduler for performing explicit-state model checking @@ -120,6 +119,8 @@ public void runParallel(int localtID) throws TimeoutException, InterruptedExcept } PExplicitGlobal.addTotIDtolocaltID(Thread.currentThread().getId(), localtID); + PExplicitThreadLogger.Initialize( PExplicitGlobal.getVerbosity()); + while (true) { // schedule limit not reached and there are pending tasks // set the next task diff --git a/benchmarksRuns.sh b/benchmarksRuns.sh index 909a85ef1..999be12a3 100755 --- a/benchmarksRuns.sh +++ b/benchmarksRuns.sh @@ -5,5 +5,5 @@ echo "-------------------Build Over---------------------------" cd - cd ../../scriptsRepo/src/P-Evaluation-Tests/ echo "Running script ..." -./scripts/run_pexplicitzshrc.sh ../../../SyncedForkedRepo/P/Tutorial/1_ClientServer test -tc tcMultipleClients --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 2 +./scripts/run_pexplicitzshrc.sh ../../../SyncedForkedRepo/P/Tutorial/1_ClientServer test -tc tcMultipleClients --seed 0 -t 10 -s 0 -v 0 --schedules-per-task 2 --nproc 2 cd - \ No newline at end of file From 611ea9c736d009f14aa17caf62552477abc13d7f Mon Sep 17 00:00:00 2001 From: Ashish Kumar Date: Mon, 1 Jul 2024 13:53:59 -0700 Subject: [PATCH 21/31] Changes --- .../java/pexplicit/commandline/PExplicitConfig.java | 2 +- .../main/java/pexplicit/runtime/PExplicitGlobal.java | 2 +- .../pexplicit/runtime/logger/PExplicitLogger.java | 9 +++++++-- .../runtime/logger/PExplicitThreadLogger.java | 12 +++--------- .../scheduler/explicit/ExplicitSearchScheduler.java | 5 ++++- .../scheduler/explicit/strategy/SearchStrategy.java | 8 +++++--- benchmarksRuns.sh | 2 +- 7 files changed, 22 insertions(+), 18 deletions(-) diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java index 3e3f2dc7e..808670fc1 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java @@ -24,7 +24,7 @@ public class PExplicitConfig { String projectName = "default"; // name of the output folder @Setter - String outputFolder = "output"; + String outputFolder = "/Users/xashisk/ashish-ws/SyncedForkedRepo/P/output"; // time limit in seconds (0 means infinite) @Setter double timeLimit = 0; diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java index 2159ddf4c..652c2bb3e 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java @@ -154,7 +154,7 @@ public static void addGlobalMachine(PMachine machine, int machineCount) { Map, List> machineListByType = getMachineListByType(); if (!machineListByType.containsKey(machine.getClass())) { machineListByType.put(machine.getClass(), new ArrayList<>()); - putMachineListByType(machineListByType); // PIN: Need lock and key somewhere here! Also, is this local copy ok for future use in this function? Need lock and key for future use? + putMachineListByType(machineListByType); } assert (machineCount == machineListByType.get(machine.getClass()).size()); machineListByType.get(machine.getClass()).add(machine); diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java index 4ebfd1a45..36fd24d27 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java @@ -104,9 +104,14 @@ public static void logEndOfRun(ExplicitSearchScheduler scheduler, long timeSpent logInfo(String.format("..... Explored %d distinct schedules", SearchStatistics.iteration)); logInfo(String.format("..... Finished %d search tasks (%d pending)", scheduler.getSearchStrategy().getFinishedTasks().size(), scheduler.getSearchStrategy().getPendingTasks().size())); - logInfo(String.format("..... Number of steps explored: %d (min), %d (avg), %d (max).", + if (SearchStatistics.iteration != 0) // PIN: Find better way to do this + logInfo(String.format("..... Number of steps explored: %d (min), %d (avg), %d (max).", SearchStatistics.minSteps, (SearchStatistics.totalSteps / SearchStatistics.iteration), SearchStatistics.maxSteps)); - logInfo(String.format("... Elapsed %d seconds and used %.1f GB", timeSpent, MemoryMonitor.getMaxMemSpent() / 1000.0)); + else + logInfo(String.format("..... Number of steps explored: %d (min), inf (avg), %d (max).", + SearchStatistics.minSteps, SearchStatistics.maxSteps)); + + logInfo(String.format("... Elapsed %d seconds and used %.1f GB", timeSpent, MemoryMonitor.getMaxMemSpent() / 1000.0)); logInfo(String.format(".. Result: " + PExplicitGlobal.getResult())); logInfo(". Done"); } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitThreadLogger.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitThreadLogger.java index b84736fb2..556df93db 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitThreadLogger.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitThreadLogger.java @@ -4,7 +4,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.LoggerContext; -import org.apache.logging.log4j.core.appender.ConsoleAppender; +import pexplicit.commandline.PExplicitConfig; import org.apache.logging.log4j.core.appender.FileAppender; import org.apache.logging.log4j.core.layout.PatternLayout; import pexplicit.runtime.PExplicitGlobal; @@ -55,7 +55,8 @@ public static void Initialize(int verb) { PatternLayout layout = Log4JConfig.getPatternLayout(); - String filename = "/Users/xashisk/ashish-ws/SyncedForkedRepo/P/output/LogThread" + PExplicitGlobal.getTID_to_localtID().get(Thread.currentThread().getId()) + ".log"; + String filename = (new PExplicitConfig()).getOutputFolder() + "/outputTID" + PExplicitGlobal.getTID_to_localtID().get(Thread.currentThread().getId()) + ".log"; + // String filename = "/Users/xashisk/ashish-ws/scriptsRepo/src/P-Evaluation-Tests/runs/pexplicit/test/output" + PExplicitGlobal.getTID_to_localtID().get(Thread.currentThread().getId()) + ".log"; FileAppender fileAppender = FileAppender.newBuilder() .setName("FileAppender") .withFileName(filename) @@ -273,16 +274,12 @@ public static void logNewState(int step, int idx, Object stateKey, SortedSet 5) || rv; } @@ -295,13 +292,10 @@ private static void typedLog(LogType type, String message) { } public static void logRunTest() { - logInfo("Check0.0"); // Debugging if (typedLogEnabled()) { - logInfo("Check0.1"); // Debugging typedLog(LogType.TestLog, String.format("Running test %s.", PExplicitGlobal.getConfig().getTestDriver())); } - logInfo("Check0.2"); // Debugging } public static void logModel(String message) { diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java index 502f98c52..d06051ee2 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java @@ -634,7 +634,10 @@ public void recordStats() { } StatWriter.log("steps-min", String.format("%d", SearchStatistics.minSteps)); StatWriter.log("max-depth-explored", String.format("%d", SearchStatistics.maxSteps)); - StatWriter.log("steps-avg", String.format("%d", SearchStatistics.totalSteps / SearchStatistics.iteration)); + if (SearchStatistics.iteration != 0) // PIN: Do in a better way? + StatWriter.log("steps-avg", String.format("%d", SearchStatistics.totalSteps / SearchStatistics.iteration)); + else + StatWriter.log("steps-avg", "inf"); StatWriter.log("#-choices-unexplored", String.format("%d", getNumUnexploredChoices())); StatWriter.log("%-choices-unexplored-data", String.format("%.1f", getUnexploredDataChoicesPercent())); StatWriter.log("#-tasks-finished", String.format("%d", searchStrategy.getFinishedTasks().size())); diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java index c8e0a1c10..5bd34a060 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/strategy/SearchStrategy.java @@ -134,9 +134,11 @@ public int getNumPendingDataChoices() { int numUnexplored = 0; SearchTask task = getCurrTask(); numUnexplored += task.getNumUnexploredDataChoices(); - for (Integer tid : pendingTasks) { - task = getTask(tid); - numUnexplored += task.getNumUnexploredDataChoices(); + synchronized (pendingTasks) { + for (Integer tid : pendingTasks) { // PIN: Take care of this in a better way. + task = getTask(tid); + numUnexplored += task.getNumUnexploredDataChoices(); + } } return numUnexplored; } diff --git a/benchmarksRuns.sh b/benchmarksRuns.sh index 999be12a3..a8839aa3e 100755 --- a/benchmarksRuns.sh +++ b/benchmarksRuns.sh @@ -5,5 +5,5 @@ echo "-------------------Build Over---------------------------" cd - cd ../../scriptsRepo/src/P-Evaluation-Tests/ echo "Running script ..." -./scripts/run_pexplicitzshrc.sh ../../../SyncedForkedRepo/P/Tutorial/1_ClientServer test -tc tcMultipleClients --seed 0 -t 10 -s 0 -v 0 --schedules-per-task 2 --nproc 2 +./scripts/run_pexplicitzshrc.sh ../../../SyncedForkedRepo/P/Tutorial/1_ClientServer test -tc tcMultipleClients --seed 0 -t 10 -s 0 -v 0 --schedules-per-task 2 --nproc 2 --no-backtrack cd - \ No newline at end of file From 37d329dce452eb965129e1ba380afcd27193c8ae Mon Sep 17 00:00:00 2001 From: Ashish Kumar Date: Tue, 2 Jul 2024 15:09:01 -0700 Subject: [PATCH 22/31] Changes --- .../main/java/pexplicit/commandline/PExplicitConfig.java | 2 +- .../pexplicit/runtime/logger/PExplicitThreadLogger.java | 4 ++-- benchmarksRuns.sh | 6 ++++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java index 808670fc1..3e3f2dc7e 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java @@ -24,7 +24,7 @@ public class PExplicitConfig { String projectName = "default"; // name of the output folder @Setter - String outputFolder = "/Users/xashisk/ashish-ws/SyncedForkedRepo/P/output"; + String outputFolder = "output"; // time limit in seconds (0 means infinite) @Setter double timeLimit = 0; diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitThreadLogger.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitThreadLogger.java index 556df93db..f93f99aa0 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitThreadLogger.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitThreadLogger.java @@ -55,8 +55,8 @@ public static void Initialize(int verb) { PatternLayout layout = Log4JConfig.getPatternLayout(); - String filename = (new PExplicitConfig()).getOutputFolder() + "/outputTID" + PExplicitGlobal.getTID_to_localtID().get(Thread.currentThread().getId()) + ".log"; - // String filename = "/Users/xashisk/ashish-ws/scriptsRepo/src/P-Evaluation-Tests/runs/pexplicit/test/output" + PExplicitGlobal.getTID_to_localtID().get(Thread.currentThread().getId()) + ".log"; + String filename = "/Users/xashisk/ashish-ws/SyncedForkedRepo/P/output/LogThread" + PExplicitGlobal.getTID_to_localtID().get(Thread.currentThread().getId()) + ".log"; // PIN: Configure output folder with CLI options. + // String filename = (new PExplicitConfig()).getOutputFolder() + "/LogThread" + PExplicitGlobal.getTID_to_localtID().get(Thread.currentThread().getId()) + ".log"; // This way gives error FileAppender fileAppender = FileAppender.newBuilder() .setName("FileAppender") .withFileName(filename) diff --git a/benchmarksRuns.sh b/benchmarksRuns.sh index a8839aa3e..24e883e87 100755 --- a/benchmarksRuns.sh +++ b/benchmarksRuns.sh @@ -5,5 +5,7 @@ echo "-------------------Build Over---------------------------" cd - cd ../../scriptsRepo/src/P-Evaluation-Tests/ echo "Running script ..." -./scripts/run_pexplicitzshrc.sh ../../../SyncedForkedRepo/P/Tutorial/1_ClientServer test -tc tcMultipleClients --seed 0 -t 10 -s 0 -v 0 --schedules-per-task 2 --nproc 2 --no-backtrack -cd - \ No newline at end of file +./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tutorial/1_ClientServer test -tc tcMultipleClients --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 2 --no-backtrack +# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/scriptsRepo/src/P-Evaluation-Tests/sample/PingPong/pingPongNew test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 2 --no-backtrack +cd - + From b2fb5177352f2b071c0c5172c822f58b01d0caea Mon Sep 17 00:00:00 2001 From: Ashish Kumar Date: Fri, 12 Jul 2024 11:20:22 -0700 Subject: [PATCH 23/31] Local CHanges --- Src/PRuntimes/PExplicitRuntime/regressionTestScript.sh | 3 +++ .../src/main/java/pexplicit/commandline/PExplicitConfig.java | 4 ++-- benchmarksRuns.sh | 4 ++-- 3 files changed, 7 insertions(+), 4 deletions(-) create mode 100755 Src/PRuntimes/PExplicitRuntime/regressionTestScript.sh diff --git a/Src/PRuntimes/PExplicitRuntime/regressionTestScript.sh b/Src/PRuntimes/PExplicitRuntime/regressionTestScript.sh new file mode 100755 index 000000000..dbbb72ae6 --- /dev/null +++ b/Src/PRuntimes/PExplicitRuntime/regressionTestScript.sh @@ -0,0 +1,3 @@ +./scripts/build.sh +mvn test +# --checker-args :-nproc:4:--schedules-per-task:2:-t:60 \ No newline at end of file diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java index 3e3f2dc7e..59204da14 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java @@ -13,7 +13,7 @@ public class PExplicitConfig { @Getter @Setter - private static int numThreads = 1; + private static int numThreads = 2; // default name of the test driver final String testDriverDefault = "DefaultImpl"; // name of the test driver @@ -36,7 +36,7 @@ public class PExplicitConfig { int verbosity = 0; // max number of schedules bound provided by the user @Setter - int maxSchedules = 1; + int maxSchedules = 4; // max steps/depth bound provided by the user @Setter int maxStepBound = 10000; diff --git a/benchmarksRuns.sh b/benchmarksRuns.sh index 24e883e87..eea00973f 100755 --- a/benchmarksRuns.sh +++ b/benchmarksRuns.sh @@ -5,7 +5,7 @@ echo "-------------------Build Over---------------------------" cd - cd ../../scriptsRepo/src/P-Evaluation-Tests/ echo "Running script ..." -./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tutorial/1_ClientServer test -tc tcMultipleClients --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 2 --no-backtrack -# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/scriptsRepo/src/P-Evaluation-Tests/sample/PingPong/pingPongNew test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 2 --no-backtrack +# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tutorial/1_ClientServer test -tc tcMultipleClients --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 2 --no-backtrack +./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/scriptsRepo/src/P-Evaluation-Tests/sample/PingPong/pingPongNew test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 2 --no-backtrack cd - From ab8fa27a60291fd115bfd11eda081dcf3a99990f Mon Sep 17 00:00:00 2001 From: Ashish Kumar Date: Mon, 22 Jul 2024 13:22:15 -0700 Subject: [PATCH 24/31] All changes --- .../main/java/pexplicit/RuntimeExecutor.java | 37 ++++--- .../commandline/PExplicitConfig.java | 2 +- .../pexplicit/runtime/PExplicitGlobal.java | 99 +++++++------------ .../runtime/logger/PExplicitLogger.java | 40 ++++---- .../runtime/scheduler/Scheduler.java | 96 +++++++++++++++++- .../explicit/ExplicitSearchScheduler.java | 8 +- .../runtime/scheduler/explicit/StepState.java | 4 +- .../scheduler/replay/ReplayScheduler.java | 6 +- .../utils/exceptions/BugFoundException.java | 18 ++++ benchmarksRuns.sh | 6 +- 10 files changed, 206 insertions(+), 110 deletions(-) diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java index b807ec14d..84d0064a6 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java @@ -16,7 +16,9 @@ import java.time.Duration; import java.time.Instant; import java.util.ArrayList; +import java.util.HashMap; import java.util.concurrent.*; +import java.util.*; /** * Represents the runtime executor that executes the analysis engine @@ -129,23 +131,36 @@ private static void process(boolean resume) throws Exception { PExplicitGlobal.setStatus(STATUS.MEMOUT); throw new Exception("MEMOUT", e); } catch (BugFoundException e) { + + + PExplicitGlobal.setStatus(STATUS.BUG_FOUND); PExplicitLogger.logStackTrace(e); - ArrayList replayers = new ArrayList<>(); - for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) - replayers.add(new ReplayScheduler((schedulers.get(i)).schedule)); + // ArrayList replayers = new ArrayList<>(); + - ArrayList localSchedulers = PExplicitGlobal.getSchedulers(); - for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) - localSchedulers.set(i, replayers.get(i)); + // PExplicitGlobal.setBuggytID(e.getBuggyLocalTID()); + // for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) + // if( e.getBuggyLocalTID() == i ) + // replayers.add(new ReplayScheduler((schedulers.get(i)).schedule)); + + + ReplayScheduler replayer = new ReplayScheduler( e.getBuggySchedule() ); + + PExplicitGlobal.setRepScheduler(replayer); + + + // ArrayList localSchedulers = PExplicitGlobal.getSchedulers(); + // for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) + // if( BugFoundException.getBuggyLocalTID() == i ) + // localSchedulers.set(i, replayers.get(i)); try { - for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) - (replayers.get(i)).run(); - } catch (NullPointerException | StackOverflowError | ClassCastException replayException) { + replayer.run(); + } catch (NullPointerException | StackOverflowError | ClassCastException replayException) { PExplicitLogger.logStackTrace((Exception) replayException); throw new BugFoundException(replayException.getMessage(), replayException); } catch (BugFoundException replayException) { @@ -177,11 +192,11 @@ private static void process(boolean resume) throws Exception { } public static void run() throws Exception { - ArrayList localSchedulers = PExplicitGlobal.getSchedulers(); + // ArrayList localSchedulers = PExplicitGlobal.getSchedulers(); for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) { ExplicitSearchScheduler localCopy = new ExplicitSearchScheduler(); schedulers.add(localCopy); - localSchedulers.add(localCopy); + // localSchedulers.add(localCopy); } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java index 59204da14..821e51ce0 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java @@ -13,7 +13,7 @@ public class PExplicitConfig { @Getter @Setter - private static int numThreads = 2; + private static int numThreads = 1; // default name of the test driver final String testDriverDefault = "DefaultImpl"; // name of the test driver diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java index 652c2bb3e..e153d9134 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java @@ -6,12 +6,17 @@ import pexplicit.runtime.logger.PExplicitLogger; import pexplicit.runtime.machine.PMachine; import pexplicit.runtime.machine.PMachineId; +import pexplicit.runtime.scheduler.Schedule; import pexplicit.runtime.scheduler.Scheduler; import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicLong; +import org.jetbrains.annotations.Async; + +import pexplicit.runtime.scheduler.replay.ReplayScheduler; + /** * Represents global data structures represented with a singleton class */ @@ -22,16 +27,10 @@ public class PExplicitGlobal { @Getter private static final int maxThreads = PExplicitConfig.getNumThreads(); - /** - * Mapping from machine type to list of all machine instances - */ - @Getter - private static final Map, List>> machineListByTypePerThread = new ConcurrentHashMap<>(); // This is per thread; so make this map of tiD to same Map - /** - * Set of machines - */ - @Getter - private static final Map> machineSetPerThread = new ConcurrentHashMap<>(); + + + + private static AtomicLong threadsBlocking = new AtomicLong(0); // @Getter // @Setter @@ -63,9 +62,23 @@ public class PExplicitGlobal { // @Setter // private static Scheduler scheduler = null; // Remove this! + // @Getter + // @Setter + // private static int buggytID = -1; + @Getter + @Setter + private static Scheduler buggyScheduler = null; + + + @Getter @Setter - private static ArrayList schedulers = new ArrayList<>(); + private static Map schedulers = new ConcurrentHashMap<>(); + + @Getter + @Setter + private static ReplayScheduler repScheduler = null; + /** * Status of the run **/ @@ -98,67 +111,21 @@ public static void addTotIDtolocaltID(long tID, Integer localtID) { tID_to_localtID.put(tID, localtID); } - public static Map, List> getMachineListByType() { - int localtID = tID_to_localtID.get(Thread.currentThread().getId()); - if (!machineListByTypePerThread.containsKey(localtID)) { - machineListByTypePerThread.put(localtID, new HashMap<>()); // Initialize with an empty HashMap if key doesn't exist - } - return machineListByTypePerThread.get(localtID); - } - public static void putMachineListByType(Map, List> machineListByType) { - int localtID = tID_to_localtID.get(Thread.currentThread().getId()); - machineListByTypePerThread.put(localtID, machineListByType); - } - - public static SortedSet getMachineSet() { - int localtID = tID_to_localtID.get(Thread.currentThread().getId()); - if (!machineSetPerThread.containsKey(localtID)) { - machineSetPerThread.put(localtID, new TreeSet<>()); - } - return machineSetPerThread.get(localtID); - } public static Scheduler getScheduler() { + if (repScheduler != null) + return repScheduler; int localtID = tID_to_localtID.get(Thread.currentThread().getId()); return schedulers.get(localtID); } - /** - * Get a machine of a given type and index if exists, else return null. - * - * @param pid Machine pid - * @return Machine - */ - public static PMachine getGlobalMachine(PMachineId pid) { - Map, List> machineListByType = getMachineListByType(); - List machinesOfType = machineListByType.get(pid.getType()); - if (machinesOfType == null) { - return null; - } - if (pid.getTypeId() >= machinesOfType.size()) { - return null; - } - PMachine result = machineListByType.get(pid.getType()).get(pid.getTypeId()); - assert (getMachineSet().contains(result)); - return result; + public static void putSchedulers( Integer ltID, Scheduler sch ) { + schedulers.put(ltID, sch); } - /** - * Add a machine. - * - * @param machine Machine to add - * @param machineCount Machine type count - */ - public static void addGlobalMachine(PMachine machine, int machineCount) { - Map, List> machineListByType = getMachineListByType(); - if (!machineListByType.containsKey(machine.getClass())) { - machineListByType.put(machine.getClass(), new ArrayList<>()); - putMachineListByType(machineListByType); - } - assert (machineCount == machineListByType.get(machine.getClass()).size()); - machineListByType.get(machine.getClass()).add(machine); - getMachineSet().add(machine); - assert (machineListByType.get(machine.getClass()).get(machineCount) == machine); - } -} \ No newline at end of file + + + +} + diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java index 36fd24d27..db1b6ce36 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java @@ -270,8 +270,9 @@ public static void logNewState(int step, int idx, Object stateKey, SortedSet, List>> machineListByTypePerThread = new ConcurrentHashMap<>(); // This is per thread; so make this map of tiD to same Map + /** + * Set of machines + */ + @Getter + private final Map> machineSetPerThread = new ConcurrentHashMap<>(); + + public Map, List> getMachineListByType() { + int localtID = PExplicitGlobal.getTID_to_localtID().get(Thread.currentThread().getId()); + if (!machineListByTypePerThread.containsKey(localtID)) { + machineListByTypePerThread.put(localtID, new HashMap<>()); // Initialize with an empty HashMap if key doesn't exist + } + return machineListByTypePerThread.get(localtID); + } + + public void putMachineListByType(Map, List> machineListByType) { + int localtID = PExplicitGlobal.getTID_to_localtID().get(Thread.currentThread().getId()); + machineListByTypePerThread.put(localtID, machineListByType); + } + + public SortedSet getMachineSet() { + int localtID = PExplicitGlobal.getTID_to_localtID().get(Thread.currentThread().getId()); + if (!machineSetPerThread.containsKey(localtID)) { + machineSetPerThread.put(localtID, new TreeSet<>()); + } + return machineSetPerThread.get(localtID); + } + + public void putMachineSet( SortedSet machineSet) { + int localtID = PExplicitGlobal.getTID_to_localtID().get(Thread.currentThread().getId()); + machineSetPerThread.put(localtID, machineSet); + } + + /** + * Get a machine of a given type and index if exists, else return null. + * + * @param pid Machine pid + * @return Machine + */ + public PMachine getGlobalMachine(PMachineId pid) { + Map, List> machineListByType = getMachineListByType(); + List machinesOfType = machineListByType.get(pid.getType()); + if (machinesOfType == null) { + return null; + } + if (pid.getTypeId() >= machinesOfType.size()) { + return null; + } + PMachine result = machineListByType.get(pid.getType()).get(pid.getTypeId()); + assert (getMachineSet().contains(result)); + return result; + } + + + /** + * Add a machine. + * + * @param machine Machine to add + * @param machineCount Machine type count + */ + public void addGlobalMachine(PMachine machine, int machineCount) { + Map, List> machineListByType = getMachineListByType(); + if (!machineListByType.containsKey(machine.getClass())) { + machineListByType.put(machine.getClass(), new ArrayList<>()); + putMachineListByType(machineListByType); + } + assert (machineCount == machineListByType.get(machine.getClass()).size()); + machineListByType.get(machine.getClass()).add(machine); + getMachineSet().add(machine); + assert (machineListByType.get(machine.getClass()).get(machineCount) == machine); + } + + + + + + /** * Current schedule */ @@ -229,9 +315,9 @@ protected void start() { * Runs the constructor of this machine. */ public void startMachine(PMachine machine) { - if (!(PExplicitGlobal.getMachineSet()).contains(machine)) { + if (!(getMachineSet()).contains(machine)) { // add machine to global context - PExplicitGlobal.addGlobalMachine(machine, 0); + addGlobalMachine(machine, 0); } // add machine to schedule @@ -282,11 +368,11 @@ public PMachine allocateMachine( // get machine count for given type from schedule int machineCount = stepState.getMachineCount(machineType); PMachineId pid = new PMachineId(machineType, machineCount); - PMachine machine = PExplicitGlobal.getGlobalMachine(pid); + PMachine machine = getGlobalMachine(pid); if (machine == null) { // create a new machine machine = constructor.apply(machineCount); - PExplicitGlobal.addGlobalMachine(machine, machineCount); + addGlobalMachine(machine, machineCount); } // add machine to schedule diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java index d06051ee2..34fa9f37c 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java @@ -118,6 +118,8 @@ public void runParallel(int localtID) throws TimeoutException, InterruptedExcept printProgressHeader(true); } PExplicitGlobal.addTotIDtolocaltID(Thread.currentThread().getId(), localtID); + PExplicitGlobal.putSchedulers(localtID, this); + PExplicitThreadLogger.Initialize( PExplicitGlobal.getVerbosity()); @@ -325,7 +327,7 @@ public PMachine getNextScheduleChoice() { if (choiceNumber < backtrackChoiceNumber) { // pick the current schedule choice PMachineId pid = schedule.getCurrentScheduleChoice(choiceNumber); - result = PExplicitGlobal.getGlobalMachine(pid); + result = PExplicitGlobal.getScheduler().getGlobalMachine(pid); PExplicitThreadLogger.logRepeatScheduleChoice(result, stepNumber, choiceNumber); // increment choice number @@ -354,7 +356,7 @@ public PMachine getNextScheduleChoice() { } // pick the first choice - result = PExplicitGlobal.getGlobalMachine(choices.get(0)); + result = PExplicitGlobal.getScheduler().getGlobalMachine(choices.get(0)); PExplicitThreadLogger.logCurrentScheduleChoice(result, stepNumber, choiceNumber); // remove the first choice from unexplored choices @@ -562,7 +564,7 @@ private void postIterationCleanup() { stepNumber = newStepNumber; choiceNumber = scheduleChoice.getChoiceNumber(); stepState.setTo(scheduleChoice.getChoiceState()); - assert (!PExplicitGlobal.getGlobalMachine(scheduleChoice.getCurrent()).getSendBuffer().isEmpty()); + assert (!PExplicitGlobal.getScheduler().getGlobalMachine(scheduleChoice.getCurrent()).getSendBuffer().isEmpty()); } schedule.removeChoicesAfter(backtrackChoiceNumber); PExplicitThreadLogger.logBacktrack(newStepNumber, cIdx, unit); diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/StepState.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/StepState.java index 9af4836be..7d433d618 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/StepState.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/StepState.java @@ -51,7 +51,7 @@ public void clear() { public void resetToZero() { - for (PMachine machine : PExplicitGlobal.getMachineSet()) { + for (PMachine machine : PExplicitGlobal.getScheduler().getMachineSet()) { machine.reset(); } machineListByType.clear(); @@ -64,7 +64,7 @@ public void setTo(StepState input) { machineLocalStates = new HashMap<>(input.machineLocalStates); assert (machineSet.size() == machineLocalStates.size()); - for (PMachine machine : PExplicitGlobal.getMachineSet()) { + for (PMachine machine : PExplicitGlobal.getScheduler().getMachineSet()) { MachineLocalState ms = machineLocalStates.get(machine); if (ms == null) { machine.reset(); diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/replay/ReplayScheduler.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/replay/ReplayScheduler.java index c4ed69370..43f96eb66 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/replay/ReplayScheduler.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/replay/ReplayScheduler.java @@ -23,10 +23,10 @@ public void run() throws TimeoutException, InterruptedException { PExplicitLogger.logStartReplay(); ScheduleWriter.logHeader(); - // log run test + // log run test // Add PexplicitGobal.tIDtoLocalTID here PExplicitLogger.logRunTest(); - stepState.resetToZero(); + // stepState.resetToZero(); start(); runIteration(); } @@ -99,7 +99,7 @@ protected PMachine getNextScheduleChoice() { return null; } - PMachine result = PExplicitGlobal.getGlobalMachine(pid); + PMachine result = PExplicitGlobal.getScheduler().getGlobalMachine(pid); ScheduleWriter.logScheduleChoice(result); PExplicitLogger.logRepeatScheduleChoice(result, stepNumber, choiceNumber); diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/exceptions/BugFoundException.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/exceptions/BugFoundException.java index e73a7f83f..b8941d0a3 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/exceptions/BugFoundException.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/exceptions/BugFoundException.java @@ -1,15 +1,33 @@ package pexplicit.utils.exceptions; +import org.jetbrains.annotations.Async; + +import lombok.Getter; +import lombok.Setter; +import pexplicit.runtime.PExplicitGlobal; import pexplicit.runtime.logger.PExplicitLogger; +import pexplicit.runtime.scheduler.Schedule; +import pexplicit.runtime.PExplicitGlobal; public class BugFoundException extends RuntimeException { + + // @Getter + // @Setter + // private int buggyLocalTID = -1; + + + @Getter + private Schedule buggySchedule = null; + public BugFoundException(String message) { super(message); + buggySchedule = (PExplicitGlobal.getSchedulers().get( (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()) )).schedule; PExplicitLogger.logBugFound(message); } public BugFoundException(String message, Throwable cause) { super(message, cause); + buggySchedule = (PExplicitGlobal.getSchedulers().get( (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()) )).schedule; PExplicitLogger.logBugFound(message); } } diff --git a/benchmarksRuns.sh b/benchmarksRuns.sh index eea00973f..1fa775f22 100755 --- a/benchmarksRuns.sh +++ b/benchmarksRuns.sh @@ -6,6 +6,10 @@ cd - cd ../../scriptsRepo/src/P-Evaluation-Tests/ echo "Running script ..." # ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tutorial/1_ClientServer test -tc tcMultipleClients --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 2 --no-backtrack -./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/scriptsRepo/src/P-Evaluation-Tests/sample/PingPong/pingPongNew test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 2 --no-backtrack +# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/scriptsRepo/src/P-Evaluation-Tests/sample/PingPong/pingPongNew test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 2 --no-backtrack +./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature1SMLevelDecls/DynamicError/AlonBug test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack +# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature1SMLevelDecls/Correct/bug1 test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack + + cd - From 5c0a9bff0cf418ef4934dd454bb5b59b973318ea Mon Sep 17 00:00:00 2001 From: Ashish Kumar Date: Tue, 23 Jul 2024 10:29:12 -0700 Subject: [PATCH 25/31] Changes --- Src/PRuntimes/PExplicitRuntime/regressionTestScript.sh | 1 - .../src/main/java/pexplicit/RuntimeExecutor.java | 2 +- .../pexplicit/utils/exceptions/BugFoundException.java | 8 +++++--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Src/PRuntimes/PExplicitRuntime/regressionTestScript.sh b/Src/PRuntimes/PExplicitRuntime/regressionTestScript.sh index dbbb72ae6..06949fcad 100755 --- a/Src/PRuntimes/PExplicitRuntime/regressionTestScript.sh +++ b/Src/PRuntimes/PExplicitRuntime/regressionTestScript.sh @@ -1,3 +1,2 @@ ./scripts/build.sh mvn test -# --checker-args :-nproc:4:--schedules-per-task:2:-t:60 \ No newline at end of file diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java index 84d0064a6..f6a0e1c15 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java @@ -151,7 +151,7 @@ private static void process(boolean resume) throws Exception { ReplayScheduler replayer = new ReplayScheduler( e.getBuggySchedule() ); PExplicitGlobal.setRepScheduler(replayer); - + PExplicitGlobal.addTotIDtolocaltID(Thread.currentThread().getId(), e.getBuggyLocalTID()); // ArrayList localSchedulers = PExplicitGlobal.getSchedulers(); // for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/exceptions/BugFoundException.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/exceptions/BugFoundException.java index b8941d0a3..1892740d6 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/exceptions/BugFoundException.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/exceptions/BugFoundException.java @@ -11,9 +11,9 @@ public class BugFoundException extends RuntimeException { - // @Getter - // @Setter - // private int buggyLocalTID = -1; + @Getter + @Setter + private int buggyLocalTID = -1; @Getter @@ -21,12 +21,14 @@ public class BugFoundException extends RuntimeException { public BugFoundException(String message) { super(message); + buggyLocalTID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); buggySchedule = (PExplicitGlobal.getSchedulers().get( (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()) )).schedule; PExplicitLogger.logBugFound(message); } public BugFoundException(String message, Throwable cause) { super(message, cause); + buggyLocalTID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); buggySchedule = (PExplicitGlobal.getSchedulers().get( (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()) )).schedule; PExplicitLogger.logBugFound(message); } From fb9b5914fa8c4eafd1ec050b33638900189d6936 Mon Sep 17 00:00:00 2001 From: Ashish Kumar Date: Tue, 23 Jul 2024 11:15:27 -0700 Subject: [PATCH 26/31] Required changes --- .../Backend/Java/MachineGenerator.cs | 21 +++---------------- .../PExplicitRuntime/regressionTestScript.sh | 2 -- benchmarksRuns.sh | 7 +++---- 3 files changed, 6 insertions(+), 24 deletions(-) delete mode 100755 Src/PRuntimes/PExplicitRuntime/regressionTestScript.sh diff --git a/Src/PCompiler/CompilerCore/Backend/Java/MachineGenerator.cs b/Src/PCompiler/CompilerCore/Backend/Java/MachineGenerator.cs index bddba051f..cae6177f1 100644 --- a/Src/PCompiler/CompilerCore/Backend/Java/MachineGenerator.cs +++ b/Src/PCompiler/CompilerCore/Backend/Java/MachineGenerator.cs @@ -252,19 +252,9 @@ private void WriteMonitorCstr() foreach (var s in _currentMachine.States) { - WriteStateBuilderDecl(s, true); + WriteStateBuilderDecl(s); } WriteLine("} // constructor"); - WriteLine(); - - WriteLine($"public void reInitializeMonitor() {{"); - - foreach (var s in _currentMachine.States) - { - WriteStateBuilderDecl(s, false); - } - WriteLine("}"); - } private void WriteEventsAccessor() @@ -281,14 +271,9 @@ private void WriteEventsAccessor() WriteLine("}"); } - private void WriteStateBuilderDecl(State s, bool isConstructor) + private void WriteStateBuilderDecl(State s) { - if (isConstructor) { - WriteLine($"addState(prt.State.keyedOn({Names.IdentForState(s)})"); - } else { - WriteLine($"registerState(prt.State.keyedOn({Names.IdentForState(s)})"); - } - + WriteLine($"addState(prt.State.keyedOn({Names.IdentForState(s)})"); if (s.IsStart) { WriteLine($".isInitialState(true)"); diff --git a/Src/PRuntimes/PExplicitRuntime/regressionTestScript.sh b/Src/PRuntimes/PExplicitRuntime/regressionTestScript.sh deleted file mode 100755 index 06949fcad..000000000 --- a/Src/PRuntimes/PExplicitRuntime/regressionTestScript.sh +++ /dev/null @@ -1,2 +0,0 @@ -./scripts/build.sh -mvn test diff --git a/benchmarksRuns.sh b/benchmarksRuns.sh index 1fa775f22..e48e25de4 100755 --- a/benchmarksRuns.sh +++ b/benchmarksRuns.sh @@ -5,10 +5,9 @@ echo "-------------------Build Over---------------------------" cd - cd ../../scriptsRepo/src/P-Evaluation-Tests/ echo "Running script ..." -# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tutorial/1_ClientServer test -tc tcMultipleClients --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 2 --no-backtrack -# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/scriptsRepo/src/P-Evaluation-Tests/sample/PingPong/pingPongNew test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 2 --no-backtrack -./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature1SMLevelDecls/DynamicError/AlonBug test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack -# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature1SMLevelDecls/Correct/bug1 test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack +./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Integration/DynamicError/SEM_OneMachine_8 test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack +# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature2Stmts/DynamicError/GotoStmt1 test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack +# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature2Stmts/DynamicError/receive6 test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack cd - From 96f061f29e9bc0c11ffd5b030afb2e7afd6cbf2e Mon Sep 17 00:00:00 2001 From: Ashish Kumar Date: Wed, 24 Jul 2024 13:35:37 -0700 Subject: [PATCH 27/31] 1. Changes made. 2. For hardcoded log path, need to know where scripts are placed are inside the P folder (else can then change) 3. For nproc 4, added code for killing other threads when bugfoundException is raised --- .../main/java/pexplicit/RuntimeExecutor.java | 3 +- .../commandline/PExplicitConfig.java | 2 +- .../pexplicit/runtime/PExplicitGlobal.java | 2 +- .../runtime/logger/PExplicitLogger.java | 18 ++--- .../runtime/logger/PExplicitThreadLogger.java | 24 +++--- .../pexplicit/runtime/machine/PMachine.java | 4 +- .../runtime/scheduler/Scheduler.java | 36 ++------- .../utils/exceptions/BugFoundException.java | 4 +- .../src/main/java/prt/Monitor.java | 81 ++++++++++--------- .../src/test/java/MonitorTest.java | 72 +++++++++-------- benchmarksRuns.sh | 6 +- 11 files changed, 123 insertions(+), 129 deletions(-) diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java index f6a0e1c15..1893325f0 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java @@ -133,7 +133,8 @@ private static void process(boolean resume) throws Exception { } catch (BugFoundException e) { - + executor.shutdownNow(); // This shutsdown all the threads. TODO / PIN: Need for other exceptions? + PExplicitGlobal.setStatus(STATUS.BUG_FOUND); diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java index 821e51ce0..7f4edd2b7 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java @@ -13,7 +13,7 @@ public class PExplicitConfig { @Getter @Setter - private static int numThreads = 1; + private static int numThreads = 4; // default name of the test driver final String testDriverDefault = "DefaultImpl"; // name of the test driver diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java index e153d9134..85b85a733 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/PExplicitGlobal.java @@ -71,7 +71,7 @@ public class PExplicitGlobal { - @Getter + @Setter private static Map schedulers = new ConcurrentHashMap<>(); diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java index db1b6ce36..f058140ff 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitLogger.java @@ -270,8 +270,6 @@ public static void logNewState(int step, int idx, Object stateKey, SortedSet machineType, PValue payload, Function constructor) { int localtID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); - PMachine machine = ((PExplicitGlobal.getSchedulers()).get(localtID)).allocateMachine(machineType, constructor); + PMachine machine = (PExplicitGlobal.getScheduler()).allocateMachine(machineType, constructor); PMessage msg = new PMessage(PEvent.createMachine, machine, payload); sendBuffer.add(msg); return new PMachineValue(machine); @@ -288,7 +288,7 @@ public void sendEvent(PMachineValue target, PEvent event, PValue payload) { sendBuffer.add(msg); int localtID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); - ((PExplicitGlobal.getSchedulers()).get(localtID)).runMonitors(msg); + (PExplicitGlobal.getScheduler()).runMonitors(msg); } /** diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Scheduler.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Scheduler.java index 06da7bc87..100daf49f 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Scheduler.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/Scheduler.java @@ -13,11 +13,6 @@ import pexplicit.utils.misc.Assert; import pexplicit.values.*; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.SortedSet; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeoutException; import java.util.function.Function; @@ -31,37 +26,22 @@ public abstract class Scheduler implements SchedulerInterface { * Mapping from machine type to list of all machine instances */ @Getter - private final Map, List>> machineListByTypePerThread = new ConcurrentHashMap<>(); // This is per thread; so make this map of tiD to same Map + @Setter + private Map,List> machineListByType = new ConcurrentHashMap<>(); /** * Set of machines */ @Getter - private final Map> machineSetPerThread = new ConcurrentHashMap<>(); - - public Map, List> getMachineListByType() { - int localtID = PExplicitGlobal.getTID_to_localtID().get(Thread.currentThread().getId()); - if (!machineListByTypePerThread.containsKey(localtID)) { - machineListByTypePerThread.put(localtID, new HashMap<>()); // Initialize with an empty HashMap if key doesn't exist - } - return machineListByTypePerThread.get(localtID); - } + @Setter + private SortedSet machineSet = Collections.synchronizedSortedSet( new TreeSet<>()); - public void putMachineListByType(Map, List> machineListByType) { - int localtID = PExplicitGlobal.getTID_to_localtID().get(Thread.currentThread().getId()); - machineListByTypePerThread.put(localtID, machineListByType); - } - public SortedSet getMachineSet() { - int localtID = PExplicitGlobal.getTID_to_localtID().get(Thread.currentThread().getId()); - if (!machineSetPerThread.containsKey(localtID)) { - machineSetPerThread.put(localtID, new TreeSet<>()); - } - return machineSetPerThread.get(localtID); + public void putMachineListByType(Map, List> machinelistbytype) { + machineListByType = machinelistbytype; } - public void putMachineSet( SortedSet machineSet) { - int localtID = PExplicitGlobal.getTID_to_localtID().get(Thread.currentThread().getId()); - machineSetPerThread.put(localtID, machineSet); + public void putMachineSet( SortedSet machineset) { + machineSet = machineset; } /** diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/exceptions/BugFoundException.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/exceptions/BugFoundException.java index 1892740d6..f9048032d 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/exceptions/BugFoundException.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/exceptions/BugFoundException.java @@ -22,14 +22,14 @@ public class BugFoundException extends RuntimeException { public BugFoundException(String message) { super(message); buggyLocalTID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); - buggySchedule = (PExplicitGlobal.getSchedulers().get( (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()) )).schedule; + buggySchedule = (PExplicitGlobal.getScheduler()).schedule; PExplicitLogger.logBugFound(message); } public BugFoundException(String message, Throwable cause) { super(message, cause); buggyLocalTID = (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()); - buggySchedule = (PExplicitGlobal.getSchedulers().get( (PExplicitGlobal.getTID_to_localtID()).get(Thread.currentThread().getId()) )).schedule; + buggySchedule = (PExplicitGlobal.getScheduler()).schedule; PExplicitLogger.logBugFound(message); } } diff --git a/Src/PRuntimes/PJavaRuntime/src/main/java/prt/Monitor.java b/Src/PRuntimes/PJavaRuntime/src/main/java/prt/Monitor.java index 9318f5eba..ef73be60d 100644 --- a/Src/PRuntimes/PJavaRuntime/src/main/java/prt/Monitor.java +++ b/Src/PRuntimes/PJavaRuntime/src/main/java/prt/Monitor.java @@ -1,29 +1,38 @@ package prt; -import java.util.*; +import java.util.EnumMap; +import java.util.List; +import java.util.Objects; +import java.util.Optional; import java.util.function.Consumer; -import prt.events.PEvent; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Marker; import org.apache.logging.log4j.MarkerManager; -import prt.exceptions.*; -import java.io.Serializable; +import org.apache.logging.log4j.message.StringMapMessage; + +import prt.events.PEvent; +import prt.exceptions.NonTotalStateMapException; +import prt.exceptions.PAssertionFailureException; +import prt.exceptions.RaiseEventException; +import prt.exceptions.TransitionException; +import prt.exceptions.UnhandledEventException; /** * A prt.Monitor encapsulates a state machine. * */ -public abstract class Monitor> implements Consumer>, Serializable { - private static final Logger logger = LogManager.getLogger(Monitor.class); +public abstract class Monitor> implements Consumer> { + private final Logger logger = LogManager.getLogger(this.getClass()); private static final Marker PROCESSING_MARKER = MarkerManager.getMarker("EVENT_PROCESSING"); private static final Marker TRANSITIONING_MARKER = MarkerManager.getMarker("STATE_TRANSITIONING"); - private StateKey startStateKey; - private StateKey currentStateKey; + @SuppressWarnings("OptionalUsedAsFieldOrParameterType") + private Optional> startState; + private State currentState; - private transient EnumMap> states; // All registered states + private EnumMap> states; // All registered states private StateKey[] stateUniverse; // all possible states /** @@ -43,17 +52,6 @@ protected void addState(State s) { throw new RuntimeException("prt.Monitor is already running; no new states may be added."); } - registerState(s); - - if (s.isInitialState()) { - if (startStateKey != null) { - throw new RuntimeException("Initial state already set to " + startStateKey); - } - startStateKey = s.getKey(); - } - } - - protected void registerState(State s) { if (states == null) { states = new EnumMap<>((Class) s.getKey().getClass()); stateUniverse = s.getKey().getDeclaringClass().getEnumConstants(); @@ -63,6 +61,13 @@ protected void registerState(State s) { throw new RuntimeException("prt.State already present"); } states.put(s.getKey(), s); + + if (s.isInitialState()) { + if (startState.isPresent()) { + throw new RuntimeException("Initial state already set to " + startState.get().getKey()); + } + startState = Optional.of(s); + } } public StateKey getCurrentState() { @@ -70,7 +75,7 @@ public StateKey getCurrentState() { throw new RuntimeException("prt.Monitor is not running (did you call ready()?)"); } - return currentStateKey; + return currentState.getKey(); } /** @@ -142,11 +147,10 @@ public void accept(PEvent p) throws UnhandledEventException { throw new RuntimeException("prt.Monitor is not running (did you call ready()?)"); } - //logger.info(PROCESSING_MARKER, new StringMapMessage().with("event", p)); + logger.info(PROCESSING_MARKER, new StringMapMessage().with("event", p)); // XXX: We can technically avoid this downcast, but to fulfill the interface for Consumer // this method cannot accept a type parameter, so this can't be a TransitionableConsumer

. - State currentState = states.get(currentStateKey); Optional> oc = currentState.getHandler(p.getClass()); if (oc.isEmpty()) { logger.atFatal().log(currentState + " missing event handler for " + p.getClass().getSimpleName()); @@ -161,20 +165,19 @@ public void accept(PEvent p) throws UnhandledEventException { * entry handler, and updating internal bookkeeping. * @param s The new state. */ - private

void handleTransition(State s, P payload) { + private

void handleTransition(State s, Optional

payload) { if (!isRunning) { throw new RuntimeException("prt.Monitor is not running (did you call ready()?)"); } - //logger.info(TRANSITIONING_MARKER, new StringMapMessage().with("state", s)); + logger.info(TRANSITIONING_MARKER, new StringMapMessage().with("state", s)); - State currentState = states.get(currentStateKey); currentState.getOnExit().ifPresent(Runnable::run); currentState = s; - currentStateKey = s.getKey(); currentState.getOnEntry().ifPresent(handler -> { - invokeWithTrampoline(handler, payload); + Object p = payload.orElse(null); + invokeWithTrampoline(handler, p); }); } @@ -204,7 +207,7 @@ private

void invokeWithTrampoline(State.TransitionableConsumer

handler, P * must be a handler of zero parameters, will be invoked. */ public void ready() { - readyImpl(null); + readyImpl(Optional.empty()); } /** @@ -213,10 +216,10 @@ public void ready() { * @param payload The argument to the initial state's entry handler. */ public

void ready(P payload) { - readyImpl(payload); + readyImpl(Optional.of(payload)); } - private

void readyImpl(P payload) { + private

void readyImpl(Optional

payload) { if (isRunning) { throw new RuntimeException("prt.Monitor is already running."); } @@ -229,11 +232,13 @@ private

void readyImpl(P payload) { isRunning = true; - currentStateKey = startStateKey; - State currentState = states.get(currentStateKey); + currentState = startState.orElseThrow(() -> + new RuntimeException( + "No initial state set (did you specify an initial state, or is the machine halted?)")); currentState.getOnEntry().ifPresent(handler -> { - invokeWithTrampoline(handler, payload); + Object p = payload.orElse(null); + invokeWithTrampoline(handler, p); }); } @@ -241,15 +246,13 @@ private

void readyImpl(P payload) { * Instantiates a new prt.Monitor; users should provide domain-specific functionality in a subclass. */ protected Monitor() { - startStateKey = null; + startState = Optional.empty(); isRunning = false; states = null; // We need a concrete class to instantiate an EnumMap; do this lazily on the first addState() call. - currentStateKey = null; // So long as we have not yet readied, this will be null! + currentState = null; // So long as we have not yet readied, this will be null! } public abstract List>> getEventTypes(); - public abstract void reInitializeMonitor(); - -} +} \ No newline at end of file diff --git a/Src/PRuntimes/PJavaRuntime/src/test/java/MonitorTest.java b/Src/PRuntimes/PJavaRuntime/src/test/java/MonitorTest.java index 7c0c2d9c1..698ab3b17 100644 --- a/Src/PRuntimes/PJavaRuntime/src/test/java/MonitorTest.java +++ b/Src/PRuntimes/PJavaRuntime/src/test/java/MonitorTest.java @@ -1,16 +1,18 @@ -import prt.events.PEvent; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import prt.*; + +import prt.Monitor; +import prt.State; +import prt.events.PEvent; import prt.exceptions.NonTotalStateMapException; import prt.exceptions.PAssertionFailureException; -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; - -import static org.junit.jupiter.api.Assertions.*; - public class MonitorTest { private enum SingleState { INIT_STATE } @@ -26,9 +28,6 @@ public NoDefaultStateMonitor() { super(); addState(new State.Builder<>(SingleState.INIT_STATE).build()); } - - public void reInitializeMonitor() {} - public List>> getEventTypes() { return List.of(); } } @@ -42,8 +41,6 @@ public MultipleDefaultStateMonitors() { addState(new State.Builder<>(BiState.OTHER_STATE).isInitialState(true).build()); } - public void reInitializeMonitor() {} - public List>> getEventTypes() { return List.of(); } } @@ -56,8 +53,6 @@ public NonTotalStateMapMonitor() { addState(new State.Builder<>(BiState.INIT_STATE).isInitialState(true).build()); } - public void reInitializeMonitor() {} - public List>> getEventTypes() { return List.of(); } } /** @@ -70,8 +65,6 @@ public NonUniqueStateKeyMonitor() { addState(new State.Builder<>(BiState.INIT_STATE).isInitialState(true).build()); } - public void reInitializeMonitor() {} - public List>> getEventTypes() { return List.of(); } } @@ -101,8 +94,6 @@ public CounterMonitor() { .build()); } - public void reInitializeMonitor() {} - public List>> getEventTypes() { return List.of(); } } @@ -127,8 +118,6 @@ public ChainedEntryHandlerMonitor() { .build()); } - public void reInitializeMonitor() {} - public List>> getEventTypes() { return List.of(); } } @@ -158,8 +147,6 @@ public GotoStateWithPayloadsMonitor() { .build()); } - public void reInitializeMonitor() {} - public List>> getEventTypes() { return List.of(); } } @@ -190,8 +177,6 @@ public GotoStateWithPayloadsMonitorIncludingInitialEntryHandler() { .build()); } - public void reInitializeMonitor() {} - public List>> getEventTypes() { return List.of(CounterMonitor.AddEvent.class); } } @@ -215,8 +200,6 @@ public GotoStateWithIllTypedPayloadsMonitor() { .build()); } - public void reInitializeMonitor() {} - public List>> getEventTypes() { return List.of(); } } @@ -232,8 +215,6 @@ public ImmediateAssertionMonitor() { .build()); } - public void reInitializeMonitor() {} - public List>> getEventTypes() { return List.of(); } } @@ -245,8 +226,6 @@ public class noopEvent extends PEvent { public Void getPayload() { return null; } } - public void reInitializeMonitor() {} - public List>> getEventTypes() { return List.of(testEvent.class, noopEvent.class); } public RaiseEventMonitor() { @@ -262,6 +241,18 @@ public RaiseEventMonitor() { } } + @Test + @DisplayName("Monitors require exactly one default state") + public void testDefaultStateConstruction() { + Throwable e; + + e = assertThrows(RuntimeException.class, () -> new NoDefaultStateMonitor().ready()); + assertTrue(e.getMessage().contains("No initial state set")); + + e = assertThrows(RuntimeException.class, () -> new MultipleDefaultStateMonitors().ready()); + assertTrue(e.getMessage().contains("Initial state already set")); + } + @Test @DisplayName("Monitors' state maps must be total") public void testTotalMonitorMap() { @@ -313,8 +304,7 @@ public void testChainedEntryHandlersWithPayloads() { GotoStateWithPayloadsMonitor m = new GotoStateWithPayloadsMonitor(); m.ready(); - assertTrue(m.eventsProcessed.equals(List.of(Optional.of("Hello from prt.State A"), - Optional.of("Hello from prt.State B")))); + assertTrue(m.eventsProcessed.equals(List.of("Hello from prt.State A", "Hello from prt.State B"))); } @Test @@ -325,6 +315,20 @@ public void testCantCallReadyTwice() { assertThrows(RuntimeException.class, () -> m.ready(), "prt.Monitor is already running."); } + + @Test + @DisplayName("Payloads can be passed to entry handlers through ready()") + public void testChainedEntryHandlersWithPayloadsIncludingInitialEntryHandler() { + GotoStateWithPayloadsMonitorIncludingInitialEntryHandler m = + new GotoStateWithPayloadsMonitorIncludingInitialEntryHandler(); + m.ready("Hello from the caller!"); + + assertTrue(m.eventsProcessed.equals( + List.of("Hello from the caller!", + "Hello from prt.State A", + "Hello from prt.State B"))); + } + @Test @DisplayName("Event handlers consuuming arguments in ready() must consume them!") public void testInitialEntryHandlerMustHaveAnArg() { @@ -365,4 +369,4 @@ public void testTryRaiseEvent() { m.accept(m.new testEvent()); } -} +} \ No newline at end of file diff --git a/benchmarksRuns.sh b/benchmarksRuns.sh index e48e25de4..c51c3f347 100755 --- a/benchmarksRuns.sh +++ b/benchmarksRuns.sh @@ -5,7 +5,11 @@ echo "-------------------Build Over---------------------------" cd - cd ../../scriptsRepo/src/P-Evaluation-Tests/ echo "Running script ..." -./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Integration/DynamicError/SEM_OneMachine_8 test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack +# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Integration/DynamicError/SEM_OneMachine_8 test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack + +./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature1SMLevelDecls/DynamicError/AlonBug test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack + + # ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature2Stmts/DynamicError/GotoStmt1 test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack # ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature2Stmts/DynamicError/receive6 test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack From 1a50e85a210e66f8f8cd6d60f8ddac682ec7410f Mon Sep 17 00:00:00 2001 From: Ashish Kumar Date: Thu, 25 Jul 2024 14:04:00 -0700 Subject: [PATCH 28/31] Hardcoded path changed --- .../regressionTestScript 11.14.59\342\200\257AM.sh" | 2 ++ .../java/pexplicit/runtime/logger/PExplicitThreadLogger.java | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) create mode 100755 "Src/PRuntimes/PExplicitRuntime/regressionTestScript 11.14.59\342\200\257AM.sh" diff --git "a/Src/PRuntimes/PExplicitRuntime/regressionTestScript 11.14.59\342\200\257AM.sh" "b/Src/PRuntimes/PExplicitRuntime/regressionTestScript 11.14.59\342\200\257AM.sh" new file mode 100755 index 000000000..06949fcad --- /dev/null +++ "b/Src/PRuntimes/PExplicitRuntime/regressionTestScript 11.14.59\342\200\257AM.sh" @@ -0,0 +1,2 @@ +./scripts/build.sh +mvn test diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitThreadLogger.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitThreadLogger.java index 6671ee0c2..edbbd5af2 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitThreadLogger.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitThreadLogger.java @@ -59,8 +59,8 @@ public static void Initialize(int verb) { // My working dir: /Users/xashisk/ashish-ws/scriptsRepo/src/P-Evaluation-Tests/runs/pexplicit/test // Required dir: "/Users/xashisk/ashish-ws/SyncedForkedRepo/P/output/LogThread" - String filename = "/Users/xashisk/ashish-ws/SyncedForkedRepo/P/output/LogThread" + PExplicitGlobal.getTID_to_localtID().get(Thread.currentThread().getId()) + ".log"; // PIN: Configure output folder with CLI options. - // String filename = (new PExplicitConfig()).getOutputFolder() + "/LogThread" + PExplicitGlobal.getTID_to_localtID().get(Thread.currentThread().getId()) + ".log"; // This way gives error + // System.out.println("What this variable stores:" + PExplicitGlobal.getConfig().getOutputFolder()); + String filename = "PExplicitGlobal.getConfig().getOutputFolder()" + "/LogThread" + PExplicitGlobal.getTID_to_localtID().get(Thread.currentThread().getId()) + ".log"; // PIN: Configure output folder with CLI options. FileAppender fileAppender = FileAppender.newBuilder() .setName("FileAppender") .withFileName(filename) From 4fc66837d93c98af008b0fd42cd23873768c0c15 Mon Sep 17 00:00:00 2001 From: Ashish Kumar Date: Fri, 26 Jul 2024 11:22:27 -0700 Subject: [PATCH 29/31] Added code for gracefully interrupting other threads, but confusing error: comment and uncomment L151 and L152, and run for both --- .../main/java/pexplicit/RuntimeExecutor.java | 28 +++++++++++++++---- .../commandline/PExplicitConfig.java | 2 +- .../runtime/logger/PExplicitThreadLogger.java | 2 +- benchmarksRuns.sh | 2 +- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java index 1893325f0..67e426540 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java @@ -15,8 +15,6 @@ import java.time.Duration; import java.time.Instant; -import java.util.ArrayList; -import java.util.HashMap; import java.util.concurrent.*; import java.util.*; @@ -27,6 +25,7 @@ public class RuntimeExecutor { private static ThreadPoolExecutor executor; private static ArrayList> futures = new ArrayList<>(); private static ArrayList schedulers = new ArrayList<>(); + private static List threads = new ArrayList<>(); private static void runWithTimeout(long timeLimit) throws TimeoutException, @@ -86,7 +85,16 @@ private static void preprocess() { PExplicitLogger.logInfo(String.format("... Checker is using '%s' strategy (seed:%s)", PExplicitGlobal.getConfig().getSearchStrategyMode(), PExplicitGlobal.getConfig().getRandomSeed())); - executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(PExplicitGlobal.getMaxThreads()); + ThreadFactory threadFactory = new ThreadFactory() { + @Override + public Thread newThread(Runnable r) { + Thread thread = new Thread(r); + threads.add(thread); + return thread; + } + }; + + executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(PExplicitGlobal.getMaxThreads(), threadFactory); // PExplicitGlobal.setResult("error"); // TODO: Set Results, need to take care of. @@ -109,6 +117,7 @@ private static void process(boolean resume) throws Exception { timedCalls.add(new TimedCall(schedulers.get(i), resume, i)); } + for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) { Future future = executor.submit(timedCalls.get(i)); futures.add(future); @@ -131,13 +140,22 @@ private static void process(boolean resume) throws Exception { PExplicitGlobal.setStatus(STATUS.MEMOUT); throw new Exception("MEMOUT", e); } catch (BugFoundException e) { - - executor.shutdownNow(); // This shutsdown all the threads. TODO / PIN: Need for other exceptions? + for (Thread thread : threads) { + if (thread != Thread.currentThread()) { + thread.interrupt(); + } + } + + // Useful for debugging purposes + // List notExecutedTasks = executor.shutdownNow(); + // System.out.println("Executor has been shut down. " + notExecutedTasks.size() + " tasks were not executed."); + PExplicitGlobal.setStatus(STATUS.BUG_FOUND); + PExplicitLogger.logStackTrace(e); // ArrayList replayers = new ArrayList<>(); diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java index 7f4edd2b7..821e51ce0 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java @@ -13,7 +13,7 @@ public class PExplicitConfig { @Getter @Setter - private static int numThreads = 4; + private static int numThreads = 1; // default name of the test driver final String testDriverDefault = "DefaultImpl"; // name of the test driver diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitThreadLogger.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitThreadLogger.java index edbbd5af2..69e4aedcd 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitThreadLogger.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/logger/PExplicitThreadLogger.java @@ -60,7 +60,7 @@ public static void Initialize(int verb) { // My working dir: /Users/xashisk/ashish-ws/scriptsRepo/src/P-Evaluation-Tests/runs/pexplicit/test // Required dir: "/Users/xashisk/ashish-ws/SyncedForkedRepo/P/output/LogThread" // System.out.println("What this variable stores:" + PExplicitGlobal.getConfig().getOutputFolder()); - String filename = "PExplicitGlobal.getConfig().getOutputFolder()" + "/LogThread" + PExplicitGlobal.getTID_to_localtID().get(Thread.currentThread().getId()) + ".log"; // PIN: Configure output folder with CLI options. + String filename = PExplicitGlobal.getConfig().getOutputFolder() + "/LogThread" + PExplicitGlobal.getTID_to_localtID().get(Thread.currentThread().getId()) + ".log"; // PIN: Configure output folder with CLI options. FileAppender fileAppender = FileAppender.newBuilder() .setName("FileAppender") .withFileName(filename) diff --git a/benchmarksRuns.sh b/benchmarksRuns.sh index c51c3f347..183d75b5f 100755 --- a/benchmarksRuns.sh +++ b/benchmarksRuns.sh @@ -7,7 +7,7 @@ cd ../../scriptsRepo/src/P-Evaluation-Tests/ echo "Running script ..." # ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Integration/DynamicError/SEM_OneMachine_8 test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack -./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature1SMLevelDecls/DynamicError/AlonBug test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack +./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature1SMLevelDecls/DynamicError/AlonBug test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 4 --no-backtrack # ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature2Stmts/DynamicError/GotoStmt1 test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack From 955b17890ce26e2284a5cee5149915c2223c79db Mon Sep 17 00:00:00 2001 From: Ashish Kumar Date: Mon, 29 Jul 2024 14:58:44 -0700 Subject: [PATCH 30/31] nproc: 4, bugs < 86 --- .../main/java/pexplicit/RuntimeExecutor.java | 162 +++++++++++------- .../commandline/PExplicitConfig.java | 2 +- .../pexplicit/utils/monitor/TimedCall.java | 21 ++- benchmarksRuns.sh | 14 +- 4 files changed, 133 insertions(+), 66 deletions(-) diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java index 67e426540..1487ac747 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java @@ -31,18 +31,13 @@ private static void runWithTimeout(long timeLimit) throws TimeoutException, InterruptedException, RuntimeException { - try { // PIN: If thread gets exception, need to kill the other threads. + try { if (timeLimit > 0) { - for (Future future : futures) { - // Future future = futures.get(i); future.get(timeLimit, TimeUnit.SECONDS); } - } else { - - for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) { - Future future = futures.get(i); + for (Future future : futures) { future.get(); } } @@ -96,8 +91,6 @@ public Thread newThread(Runnable r) { executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(PExplicitGlobal.getMaxThreads(), threadFactory); - // PExplicitGlobal.setResult("error"); // TODO: Set Results, need to take care of. - double preSearchTime = TimeMonitor.findInterval(TimeMonitor.getStart()); StatWriter.log("project-name", String.format("%s", PExplicitGlobal.getConfig().getProjectName())); @@ -108,11 +101,13 @@ public Thread newThread(Runnable r) { } private static void process(boolean resume) throws Exception { + + ArrayList timedCalls = new ArrayList<>(); try { // create and add first task through scheduler 0 schedulers.get(0).getSearchStrategy().createFirstTask(); - ArrayList timedCalls = new ArrayList<>(); + for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) { timedCalls.add(new TimedCall(schedulers.get(i), resume, i)); } @@ -133,53 +128,47 @@ private static void process(boolean resume) throws Exception { runWithTimeout((long) PExplicitGlobal.getConfig().getTimeLimit()); - } catch (TimeoutException e) { - PExplicitGlobal.setStatus(STATUS.TIMEOUT); - throw new Exception("TIMEOUT", e); - } catch (MemoutException | OutOfMemoryError e) { - PExplicitGlobal.setStatus(STATUS.MEMOUT); - throw new Exception("MEMOUT", e); - } catch (BugFoundException e) { - + } + catch (MemoutException | OutOfMemoryError e) { for (Thread thread : threads) { if (thread != Thread.currentThread()) { thread.interrupt(); } } - - // Useful for debugging purposes - // List notExecutedTasks = executor.shutdownNow(); - // System.out.println("Executor has been shut down. " + notExecutedTasks.size() + " tasks were not executed."); + } + // catch (TimeoutException e) { + // PExplicitGlobal.setStatus(STATUS.TIMEOUT); + // throw new Exception("TIMEOUT", e); + // } + // catch (MemoutException | OutOfMemoryError e) { + // PExplicitGlobal.setStatus(STATUS.MEMOUT); + // throw new Exception("MEMOUT", e); + // } + catch (BugFoundException e) { + for (Thread thread : threads) { + if (thread != Thread.currentThread()) { + thread.interrupt(); + } + } - PExplicitGlobal.setStatus(STATUS.BUG_FOUND); + // (schedulers.get( PExplicitGlobal.getTID_to_localtID().get( Thread.currentThread().getId() ))).updateResult(); // Update result field before setting status + PExplicitGlobal.setStatus(STATUS.BUG_FOUND); + PExplicitGlobal.setResult("cex"); // TODO: Why and where after setting status and result here are they changed before line 237? PExplicitLogger.logStackTrace(e); - // ArrayList replayers = new ArrayList<>(); - - - // PExplicitGlobal.setBuggytID(e.getBuggyLocalTID()); - // for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) - // if( e.getBuggyLocalTID() == i ) - // replayers.add(new ReplayScheduler((schedulers.get(i)).schedule)); - ReplayScheduler replayer = new ReplayScheduler( e.getBuggySchedule() ); PExplicitGlobal.setRepScheduler(replayer); PExplicitGlobal.addTotIDtolocaltID(Thread.currentThread().getId(), e.getBuggyLocalTID()); - // ArrayList localSchedulers = PExplicitGlobal.getSchedulers(); - // for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) - // if( BugFoundException.getBuggyLocalTID() == i ) - // localSchedulers.set(i, replayers.get(i)); - try { replayer.run(); - } catch (NullPointerException | StackOverflowError | ClassCastException replayException) { + } catch (NullPointerException | StackOverflowError | ClassCastException replayException) { PExplicitLogger.logStackTrace((Exception) replayException); throw new BugFoundException(replayException.getMessage(), replayException); } catch (BugFoundException replayException) { @@ -190,41 +179,96 @@ private static void process(boolean resume) throws Exception { throw new Exception("Error when replaying the bug", replayException); } throw new Exception("Failed to replay bug", e); - } catch (InterruptedException e) { - PExplicitGlobal.setStatus(STATUS.INTERRUPTED); - throw new Exception("INTERRUPTED", e); - } catch (RuntimeException e) { - PExplicitGlobal.setStatus(STATUS.ERROR); - throw new Exception("ERROR", e); - } finally { - for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) { - Future future = futures.get(i); - future.cancel(true); + } + // catch (InterruptedException e) { + // PExplicitGlobal.setStatus(STATUS.INTERRUPTED); + // throw new Exception("INTERRUPTED", e); + // } + // catch (RuntimeException e) { + // PExplicitGlobal.setStatus(STATUS.ERROR); + // throw new Exception("ERROR", e); + // } + finally { + // for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) { + // Future future = futures.get(i); + // future.cancel(true); + // } // If we waiting for threads by future.get(), we dont need to cancel threads by future.cancel; especially if we have executor.shutdownNow as next command. + + executor.shutdownNow(); // forcibly shutdown EVERY thread + + Boolean isMemOutException = false; + Boolean NoException = true; + Boolean isBugFoundException = false; + Boolean AllTimeOutException = true; + int buggyScheduleIndex = -1; + int memOutScheduleIndex = -1; + + for (int i = 0; i < timedCalls.size() ; i++) { + Throwable ePerThread = (timedCalls.get(i)).getExceptionThrown(); + if (ePerThread != null && !(ePerThread instanceof InterruptedException)) + /* Interrupted exception is thrown because 'sleep' of other threads is interrupted somewhere - possibly by timeout feature + or blocking on no current jobs available, so dont want this to go into `etc error case' */ + NoException = false; + if (!(ePerThread instanceof TimeoutException)) + AllTimeOutException = false; + if (ePerThread instanceof MemoutException || ePerThread instanceof OutOfMemoryError) { + isMemOutException = true; + memOutScheduleIndex = i; + } + else if ( ePerThread instanceof BugFoundException) { + isBugFoundException = true; + buggyScheduleIndex = i; + } + } + + if (NoException) { // Correct Case (all threads report correct) + PExplicitGlobal.setStatus(STATUS.VERIFIED_UPTO_MAX_STEPS); + PExplicitGlobal.setResult("Correct"); + printStats(); + PExplicitLogger.logEndOfRun(schedulers.get(0), Duration.between(TimeMonitor.getStart(), Instant.now()).getSeconds()); + } + else if (isMemOutException) { // Memory Error (atleast one thread throws MemOut Exception) + PExplicitGlobal.setStatus(STATUS.MEMOUT); + PExplicitGlobal.setResult("MemOut"); + printStats(); + PExplicitLogger.logEndOfRun(schedulers.get(memOutScheduleIndex), Duration.between(TimeMonitor.getStart(), Instant.now()).getSeconds()); + } + else if (isBugFoundException) { // Bug Found Exception (atleast one thread throws BugFound Exception AND no thread throws MemOut Exception) + PExplicitGlobal.setStatus(STATUS.BUG_FOUND); + PExplicitGlobal.setResult("cex"); + printStats(); + PExplicitLogger.logEndOfRun(schedulers.get(buggyScheduleIndex), Duration.between(TimeMonitor.getStart(), Instant.now()).getSeconds()); } - executor.shutdownNow(); - for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) - (schedulers.get(i)).updateResult(); - printStats(); - for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) - PExplicitLogger.logEndOfRun(schedulers.get(i), Duration.between(TimeMonitor.getStart(), Instant.now()).getSeconds()); + else if (AllTimeOutException) { // All threads timeout + PExplicitGlobal.setStatus(STATUS.TIMEOUT); + PExplicitGlobal.setResult("TimeOut"); + printStats(); + PExplicitLogger.logEndOfRun(schedulers.get(0), Duration.between(TimeMonitor.getStart(), Instant.now()).getSeconds()); + } + else { // Some other case + PExplicitGlobal.setStatus(STATUS.ERROR); + PExplicitGlobal.setResult("error"); + printStats(); + throw new Exception("ERROR"); + } + + // for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) + // (schedulers.get(i)).updateResult(); + + // for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) + // PExplicitLogger.logEndOfRun(schedulers.get(i), Duration.between(TimeMonitor.getStart(), Instant.now()).getSeconds()); } } public static void run() throws Exception { - // ArrayList localSchedulers = PExplicitGlobal.getSchedulers(); for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) { ExplicitSearchScheduler localCopy = new ExplicitSearchScheduler(); schedulers.add(localCopy); - // localSchedulers.add(localCopy); } - preprocess(); - process(false); - } - } \ No newline at end of file diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java index 821e51ce0..7f4edd2b7 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/commandline/PExplicitConfig.java @@ -13,7 +13,7 @@ public class PExplicitConfig { @Getter @Setter - private static int numThreads = 1; + private static int numThreads = 4; // default name of the test driver final String testDriverDefault = "DefaultImpl"; // name of the test driver diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/monitor/TimedCall.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/monitor/TimedCall.java index 0b1e6cef9..7892c657d 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/monitor/TimedCall.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/monitor/TimedCall.java @@ -19,6 +19,11 @@ public class TimedCall implements Callable { @Setter private int threadId; + @Getter + @Setter + private Throwable exceptionThrown; + + public TimedCall(Scheduler scheduler, boolean resume, int localtID) { this.scheduler = scheduler; this.threadId = localtID; @@ -29,13 +34,21 @@ public Integer call() throws MemoutException, BugFoundException, TimeoutException, InterruptedException { try { this.scheduler.runParallel(threadId); - } catch (OutOfMemoryError e) { + } catch ( NullPointerException | StackOverflowError | ClassCastException | TimeoutException | InterruptedException e ) { + exceptionThrown = e; + } + catch (OutOfMemoryError e) { + exceptionThrown = e; throw new MemoutException(e.getMessage(), MemoryMonitor.getMemSpent(), e); - } catch (NullPointerException | StackOverflowError | ClassCastException e) { - throw new BugFoundException(e.getMessage(), e); - } catch (MemoutException | BugFoundException | TimeoutException | InterruptedException e) { + } catch (MemoutException | BugFoundException e) { + exceptionThrown = e; throw e; } + // catch (NullPointerException | StackOverflowError | ClassCastException e) { + // throw new BugFoundException(e.getMessage(), e); + // } catch (MemoutException | BugFoundException | TimeoutException | InterruptedException e) { + // throw e; + // } return 0; } } diff --git a/benchmarksRuns.sh b/benchmarksRuns.sh index 183d75b5f..4274e5120 100755 --- a/benchmarksRuns.sh +++ b/benchmarksRuns.sh @@ -5,13 +5,23 @@ echo "-------------------Build Over---------------------------" cd - cd ../../scriptsRepo/src/P-Evaluation-Tests/ echo "Running script ..." + +#nproc 1 errors: # ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Integration/DynamicError/SEM_OneMachine_8 test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack +# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature4DataTypes/DynamicError/CastInExprs3 test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack +# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature4DataTypes/DynamicError/CastInExprs4 test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack +# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature4DataTypes/DynamicError/CastInExprs5 test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack +# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature4DataTypes/DynamicError/CastInExprs6 test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack +# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature4DataTypes/DynamicError/EnumType1 test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack +# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature2Stmts/DynamicError/GotoStmt1 test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack +#nproc 4 dynamic errors (86 failures (all in Dynamic Error)): +# expected: 2, got: 0 type errors (82/86): ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature1SMLevelDecls/DynamicError/AlonBug test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 4 --no-backtrack +# expected: 2, got: 5 type errors: +# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature4DataTypes/DynamicError/CastInExprs3 test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 4 --no-backtrack -# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature2Stmts/DynamicError/GotoStmt1 test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack -# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature2Stmts/DynamicError/receive6 test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack cd - From a87cbef50ec1d0454fa8007366aec9ae1da1ae99 Mon Sep 17 00:00:00 2001 From: Ashish Kumar Date: Thu, 1 Aug 2024 15:01:29 -0700 Subject: [PATCH 31/31] Changes --- .../main/java/pexplicit/RuntimeExecutor.java | 30 ++++++++----------- .../explicit/ExplicitSearchScheduler.java | 10 +++---- .../scheduler/replay/ReplayScheduler.java | 5 +++- .../utils/exceptions/BugFoundException.java | 6 ++++ benchmarksRuns.sh | 24 +++++++-------- 5 files changed, 40 insertions(+), 35 deletions(-) diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java index 1487ac747..5f9cd4b33 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java @@ -41,7 +41,9 @@ private static void runWithTimeout(long timeLimit) future.get(); } } - } catch (TimeoutException | BugFoundException e) { + } catch (TimeoutException e) { + throw e; + } catch (BugFoundException e) { // Dont merge with TimeoutException catch block, easier for seeing race conditions throw e; } catch (OutOfMemoryError e) { throw new MemoutException(e.getMessage(), MemoryMonitor.getMemSpent(), e); @@ -145,7 +147,8 @@ private static void process(boolean resume) throws Exception { // throw new Exception("MEMOUT", e); // } catch (BugFoundException e) { - + + for (Thread thread : threads) { if (thread != Thread.currentThread()) { thread.interrupt(); @@ -154,8 +157,8 @@ private static void process(boolean resume) throws Exception { // (schedulers.get( PExplicitGlobal.getTID_to_localtID().get( Thread.currentThread().getId() ))).updateResult(); // Update result field before setting status - PExplicitGlobal.setStatus(STATUS.BUG_FOUND); - PExplicitGlobal.setResult("cex"); // TODO: Why and where after setting status and result here are they changed before line 237? + // PExplicitGlobal.setStatus(STATUS.BUG_FOUND); + // PExplicitGlobal.setResult("cex"); PExplicitLogger.logStackTrace(e); @@ -172,7 +175,7 @@ private static void process(boolean resume) throws Exception { PExplicitLogger.logStackTrace((Exception) replayException); throw new BugFoundException(replayException.getMessage(), replayException); } catch (BugFoundException replayException) { - PExplicitLogger.logStackTrace(replayException); + PExplicitLogger.logStackTrace(replayException); // This should be throw exception again! throw replayException; } catch (Exception replayException) { PExplicitLogger.logStackTrace(replayException); @@ -188,12 +191,7 @@ private static void process(boolean resume) throws Exception { // PExplicitGlobal.setStatus(STATUS.ERROR); // throw new Exception("ERROR", e); // } - finally { - // for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) { - // Future future = futures.get(i); - // future.cancel(true); - // } // If we waiting for threads by future.get(), we dont need to cancel threads by future.cancel; especially if we have executor.shutdownNow as next command. - + finally { executor.shutdownNow(); // forcibly shutdown EVERY thread Boolean isMemOutException = false; @@ -225,25 +223,28 @@ else if ( ePerThread instanceof BugFoundException) { PExplicitGlobal.setStatus(STATUS.VERIFIED_UPTO_MAX_STEPS); PExplicitGlobal.setResult("Correct"); printStats(); - PExplicitLogger.logEndOfRun(schedulers.get(0), Duration.between(TimeMonitor.getStart(), Instant.now()).getSeconds()); + PExplicitLogger.logEndOfRun(schedulers.get(0), Duration.between(TimeMonitor.getStart(), Instant.now()).getSeconds()); // PIN: } else if (isMemOutException) { // Memory Error (atleast one thread throws MemOut Exception) PExplicitGlobal.setStatus(STATUS.MEMOUT); PExplicitGlobal.setResult("MemOut"); printStats(); PExplicitLogger.logEndOfRun(schedulers.get(memOutScheduleIndex), Duration.between(TimeMonitor.getStart(), Instant.now()).getSeconds()); + throw new Exception("MEMOUT"); } else if (isBugFoundException) { // Bug Found Exception (atleast one thread throws BugFound Exception AND no thread throws MemOut Exception) PExplicitGlobal.setStatus(STATUS.BUG_FOUND); PExplicitGlobal.setResult("cex"); printStats(); PExplicitLogger.logEndOfRun(schedulers.get(buggyScheduleIndex), Duration.between(TimeMonitor.getStart(), Instant.now()).getSeconds()); + throw new BugFoundException(); } else if (AllTimeOutException) { // All threads timeout PExplicitGlobal.setStatus(STATUS.TIMEOUT); PExplicitGlobal.setResult("TimeOut"); printStats(); PExplicitLogger.logEndOfRun(schedulers.get(0), Duration.between(TimeMonitor.getStart(), Instant.now()).getSeconds()); + throw new Exception("TIMEOUT"); } else { // Some other case PExplicitGlobal.setStatus(STATUS.ERROR); @@ -252,11 +253,6 @@ else if (AllTimeOutException) { // All threads timeout throw new Exception("ERROR"); } - // for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) - // (schedulers.get(i)).updateResult(); - - // for (int i = 0; i < PExplicitGlobal.getMaxThreads(); i++) - // PExplicitLogger.logEndOfRun(schedulers.get(i), Duration.between(TimeMonitor.getStart(), Instant.now()).getSeconds()); } } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java index 34fa9f37c..f14f15fde 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/explicit/ExplicitSearchScheduler.java @@ -111,10 +111,10 @@ public void runParallel(int localtID) throws TimeoutException, InterruptedExcept // PExplicitThreadLogger.logRunTest(); // TODO : Add log Info feature - PExplicitGlobal.setResult("incomplete"); // TODO: Set Result object + PExplicitGlobal.setResult("incomplete"); - if (PExplicitGlobal.getConfig().getVerbosity() == 0) { // TODO : Add log Info feature + if (PExplicitGlobal.getConfig().getVerbosity() == 0) { printProgressHeader(true); } PExplicitGlobal.addTotIDtolocaltID(Thread.currentThread().getId(), localtID); @@ -131,11 +131,11 @@ public void runParallel(int localtID) throws TimeoutException, InterruptedExcept // all tasks completed or schedule limit reached break; } - PExplicitThreadLogger.logStartTask(searchStrategy.getCurrTask()); // TODO : Add log Info feature + PExplicitThreadLogger.logStartTask(searchStrategy.getCurrTask()); isDoneIterating = false; while (!isDoneIterating) { searchStrategy.incrementIteration(); - PExplicitThreadLogger.logStartIteration(searchStrategy.getCurrTask(), SearchStatistics.iteration, stepNumber); // TODO : Add log Info feature + PExplicitThreadLogger.logStartIteration(searchStrategy.getCurrTask(), SearchStatistics.iteration, stepNumber); if (stepNumber == 0) { start(); } @@ -145,7 +145,7 @@ public void runParallel(int localtID) throws TimeoutException, InterruptedExcept addRemainingChoicesAsChildrenTasks(); endCurrTask(); - PExplicitThreadLogger.logEndTask(searchStrategy.getCurrTask(), searchStrategy.getNumSchedulesInCurrTask()); // TODO : Add log Info feature + PExplicitThreadLogger.logEndTask(searchStrategy.getCurrTask(), searchStrategy.getNumSchedulesInCurrTask()); } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/replay/ReplayScheduler.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/replay/ReplayScheduler.java index 43f96eb66..91c0c82d2 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/replay/ReplayScheduler.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/scheduler/replay/ReplayScheduler.java @@ -2,11 +2,14 @@ import pexplicit.runtime.PExplicitGlobal; import pexplicit.runtime.logger.PExplicitLogger; +import pexplicit.runtime.logger.PExplicitThreadLogger; import pexplicit.runtime.logger.ScheduleWriter; import pexplicit.runtime.machine.PMachine; import pexplicit.runtime.machine.PMachineId; import pexplicit.runtime.scheduler.Schedule; import pexplicit.runtime.scheduler.Scheduler; +import pexplicit.runtime.scheduler.explicit.SearchStatistics; +import pexplicit.runtime.scheduler.explicit.strategy.SearchTask; import pexplicit.utils.misc.Assert; import pexplicit.values.PValue; @@ -33,7 +36,7 @@ public void run() throws TimeoutException, InterruptedException { @Override public void runParallel(int tID) throws TimeoutException, InterruptedException { - run(); + } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/exceptions/BugFoundException.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/exceptions/BugFoundException.java index f9048032d..b77963503 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/exceptions/BugFoundException.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/utils/exceptions/BugFoundException.java @@ -32,4 +32,10 @@ public BugFoundException(String message, Throwable cause) { buggySchedule = (PExplicitGlobal.getScheduler()).schedule; PExplicitLogger.logBugFound(message); } + + public BugFoundException() { /* this constructor is only needed at the end of the code in RuntimeExecutor to throw some bugFoundException, so that PExplicit sets + the value of exit_code correctly */ + + } + } diff --git a/benchmarksRuns.sh b/benchmarksRuns.sh index 4274e5120..6dfb1100e 100755 --- a/benchmarksRuns.sh +++ b/benchmarksRuns.sh @@ -7,20 +7,20 @@ cd ../../scriptsRepo/src/P-Evaluation-Tests/ echo "Running script ..." #nproc 1 errors: -# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Integration/DynamicError/SEM_OneMachine_8 test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack -# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature4DataTypes/DynamicError/CastInExprs3 test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack -# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature4DataTypes/DynamicError/CastInExprs4 test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack -# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature4DataTypes/DynamicError/CastInExprs5 test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack -# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature4DataTypes/DynamicError/CastInExprs6 test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack -# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature4DataTypes/DynamicError/EnumType1 test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack -# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature2Stmts/DynamicError/GotoStmt1 test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack +./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Integration/DynamicError/SEM_OneMachine_8 test --seed 0 -t 0 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack +# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature4DataTypes/DynamicError/CastInExprs3 test --seed 0 -t 0 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack +# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature4DataTypes/DynamicError/CastInExprs4 test --seed 0 -t 0 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack +# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature4DataTypes/DynamicError/CastInExprs5 test --seed 0 -t 0 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack +# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature4DataTypes/DynamicError/CastInExprs6 test --seed 0 -t 0 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack +# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature4DataTypes/DynamicError/EnumType1 test --seed 0 -t 0 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack +# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature2Stmts/DynamicError/GotoStmt1 test --seed 0 -t 0 -s 0 -v 3 --schedules-per-task 2 --nproc 1 --no-backtrack -#nproc 4 dynamic errors (86 failures (all in Dynamic Error)): -# expected: 2, got: 0 type errors (82/86): -./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature1SMLevelDecls/DynamicError/AlonBug test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 4 --no-backtrack +#nproc 4 dynamic errors (80 failures (all in Dynamic Error folder)): +# expected: 2, got: 0 type errors (76/80): +# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature1SMLevelDecls/DynamicError/AlonBug test --seed 0 -t 0 -s 0 -v 3 --schedules-per-task 2 --nproc 4 --no-backtrack -# expected: 2, got: 5 type errors: -# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature4DataTypes/DynamicError/CastInExprs3 test --seed 0 -t 10 -s 0 -v 3 --schedules-per-task 2 --nproc 4 --no-backtrack +# expected: 2, got: 5 type errors (4/80): +# ./scripts/run_pexplicitzshrc.sh /Users/xashisk/ashish-ws/SyncedForkedRepo/P/Tst/RegressionTests/Feature4DataTypes/DynamicError/CastInExprs3 test --seed 0 -t 0 -s 0 -v 3 --schedules-per-task 2 --nproc 4 --no-backtrack