Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feedback-guided scheduling algorithms #791

Merged
merged 29 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
c7dd158
Add feedback strategy. (#715)
aoli-al Apr 8, 2024
c6f74dc
Improving the code with some cleanup. (#719)
ankushdesai Apr 15, 2024
3f4781a
PR cleanup (#723)
aoli-al Apr 25, 2024
9241ed4
Merge Recent Bug Fixes (#778)
ankushdesai Oct 1, 2024
9446874
Fix feedback strategy and remove experiment features.
aoli-al Oct 3, 2024
5c98bc9
Merge branch 'master' into experimental/feedback-strategy
aoli-al Oct 3, 2024
9d91a54
Removing Pattern (#786)
ChristineZh0u Oct 8, 2024
5f65c5c
Remove conflict analysis
Oct 8, 2024
6537ce6
Merge pull request #787 from p-org/experimental/feedback_cleanup
ChristineZh0u Oct 8, 2024
ccaa276
Removing compiler changes (#789)
ChristineZh0u Oct 9, 2024
61fa8d3
Cleanup.
aoli-al Oct 10, 2024
548f0f6
Revert changes to Event.
aoli-al Oct 10, 2024
e2dc756
Merge branch 'master' into experimental/feedback
ankushdesai Oct 10, 2024
52dc10a
Revert changes.
aoli-al Oct 11, 2024
cfdc88e
Merge branch 'master' into experimental/feedback
aoli-al Oct 11, 2024
8510ec4
Fix merge conflicts.
aoli-al Oct 11, 2024
6262502
Remove temp file.
aoli-al Oct 11, 2024
c183bcb
Merge branch 'master' into experimental/feedback
ChristineZh0u Oct 11, 2024
907dcd1
Rename LastSentReceiver to MessageReceiver
aoli-al Oct 12, 2024
0a371a8
Merge branch 'master' into experimental/feedback
ankushdesai Oct 14, 2024
6a4110c
Merge branch 'master' into experimental/feedback
ankushdesai Oct 17, 2024
8153cd6
Simplify scheduler implementation.
aoli-al Oct 17, 2024
d8331b8
Revert changes in QLearning strategy.
aoli-al Oct 17, 2024
b4837da
Refactor.
aoli-al Oct 17, 2024
6bd65eb
Merge branch 'master' into experimental/feedback
ankushdesai Oct 17, 2024
2eda040
Added VectorTime and BehavioralObserver class for feedback strategy (…
ChristineZh0u Oct 18, 2024
5e306b4
Revert changes in PCTStrategy.
aoli-al Oct 18, 2024
91cd717
Revert changes in Probabilistic folder.
aoli-al Oct 18, 2024
3ee77e3
revert change.
aoli-al Oct 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Src/PChecker/CheckerCore/Coverage/CoverageInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public CoverageInfo()
Machines = new HashSet<string>();
MachinesToStates = new Dictionary<string, HashSet<string>>();
RegisteredEvents = new Dictionary<string, HashSet<string>>();
EventInfo = new EventCoverage();
CoverageGraph = new Graph();
}

/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions Src/PChecker/CheckerCore/Random/IRandomValueGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;

namespace PChecker.Random
{
/// <summary>
Expand Down
5 changes: 4 additions & 1 deletion Src/PChecker/CheckerCore/Runtime/Events/EventInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ internal class EventInfo
[DataMember]
internal EventOriginInfo OriginInfo { get; private set; }

internal VectorTime VectorTime;

/// <summary>
/// Initializes a new instance of the <see cref="EventInfo"/> class.
/// </summary>
Expand All @@ -34,10 +36,11 @@ internal EventInfo(Event e)
/// <summary>
/// Initializes a new instance of the <see cref="EventInfo"/> class.
/// </summary>
internal EventInfo(Event e, EventOriginInfo originInfo)
internal EventInfo(Event e, EventOriginInfo originInfo, VectorTime v)
: this(e)
{
OriginInfo = originInfo;
VectorTime = new VectorTime(v);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public EnqueueStatus Enqueue(Event e, EventInfo info)
// A default event handler exists.
var stateName = StateMachine.CurrentState.GetType().Name;
var eventOrigin = new EventOriginInfo(StateMachine.Id, StateMachine.GetType().FullName, stateName);
return (DequeueStatus.Default, DefaultEvent.Instance, new EventInfo(DefaultEvent.Instance, eventOrigin));
return (DequeueStatus.Default, DefaultEvent.Instance, new EventInfo(DefaultEvent.Instance, eventOrigin, StateMachine.VectorTime));
}

/// <summary>
Expand Down Expand Up @@ -209,7 +209,7 @@ public void RaiseEvent(Event e)
{
var stateName = StateMachine.CurrentState.GetType().Name;
var eventOrigin = new EventOriginInfo(StateMachine.Id, StateMachine.GetType().FullName, stateName);
var info = new EventInfo(e, eventOrigin);
var info = new EventInfo(e, eventOrigin, StateMachine.VectorTime);
RaisedEvent = (e, info);
StateMachineManager.OnRaiseEvent(e, info);
}
Expand Down
13 changes: 13 additions & 0 deletions Src/PChecker/CheckerCore/Runtime/StateMachines/StateMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public abstract class StateMachine
/// </summary>
private protected IEventQueue Inbox;

/// <summary>
/// Keeps track of state machine's current vector time.
/// </summary>
public VectorTime VectorTime;

/// <summary>
/// Cache of state machine types to a map of action names to action declarations.
/// </summary>
Expand Down Expand Up @@ -295,6 +300,7 @@ internal void Configure(ControlledRuntime runtime, StateMachineId id, IStateMach
Id = id;
Manager = manager;
Inbox = inbox;
VectorTime = new VectorTime(Id);
}

/// <summary>
Expand Down Expand Up @@ -535,6 +541,9 @@ public void SendEvent(PMachineValue target, Event ev)
Assert(target.Permissions.Contains(ev.GetType().Name),
$"Event {ev.GetType().Name} is not in the permissions set of the target machine");
AnnounceInternal(ev);
// Update vector clock
VectorTime.Increment();
BehavioralObserver.AddToCurrentTimeline(ev, BehavioralObserver.EventType.SEND, VectorTime);
Runtime.SendEvent(target.Id, ev, this);
}

Expand Down Expand Up @@ -590,6 +599,10 @@ internal async Task RunEventHandlerAsync()

if (status is DequeueStatus.Success)
{
// Update state machine vector clock
VectorTime.Merge(info.VectorTime);
BehavioralObserver.AddToCurrentTimeline(e, BehavioralObserver.EventType.DEQUEUE, VectorTime);

// Notify the runtime for a new event to handle. This is only used
// during bug-finding and operation bounding, because the runtime
// has to schedule an state machine when a new operation is dequeued.
Expand Down
124 changes: 124 additions & 0 deletions Src/PChecker/CheckerCore/Runtime/VectorTime.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using PChecker.Runtime.StateMachines;
using PChecker.IO.Logging;

public class VectorTime
{
// Dictionary that uses StateMachineId as the key and stores the logical clock as the value
public Dictionary<StateMachineId, int> Clock { get; private set; }

// The ID of the current state machine

private StateMachineId stateMachineId;

public VectorTime(StateMachineId stateMachineId)
{
this.stateMachineId = stateMachineId;
Clock = new Dictionary<StateMachineId, int>();
Clock[stateMachineId] = 0; // Initialize the clock for this state machine
}

// Clone constructor (creates a snapshot of the vector clock)
public VectorTime(VectorTime other)
{
Clock = new Dictionary<StateMachineId, int>(other.Clock);
}

// Increment the logical clock for this state machine
public void Increment()
{
Clock[stateMachineId]++;
}

// Merge another vector clock into this one
public void Merge(VectorTime otherTime)
{
foreach (var entry in otherTime.Clock)
{
StateMachineId otherMachineId = entry.Key;
int otherTimestamp = entry.Value;

if (Clock.ContainsKey(otherMachineId))
{
// Take the maximum timestamp for each state machine
Clock[otherMachineId] = Math.Max(Clock[otherMachineId], otherTimestamp);
}
else
{
// Add the state machine's timestamp if it doesn't exist in this time
Clock[otherMachineId] = otherTimestamp;
}
}
}

// Compare this vector clock to another for sorting purposes
// Rturn value: -1 = This vector clock happens after the other, 1 = This vector clock happens before the other,
// 0 = Clocks are equal or concurrent
public int CompareTo(VectorTime other)
{
bool atLeastOneLess = false;
bool atLeastOneGreater = false;

foreach (var machineId in Clock.Keys)
{
int thisTime = Clock[machineId];
int otherTime = other.Clock.ContainsKey(machineId) ? other.Clock[machineId] : 0;

if (thisTime < otherTime)
{
atLeastOneLess = true;
}
else if (thisTime > otherTime)
{
atLeastOneGreater = true;
}
if (atLeastOneLess && atLeastOneGreater)
{
return 0;
}
}
if (atLeastOneLess && !atLeastOneGreater)
{
return -1;
}
if (atLeastOneGreater && !atLeastOneLess)
{
return 1;
}
return 0;
}


public override string ToString()
{
var elements = new List<string>();
foreach (var entry in Clock)
{
elements.Add($"StateMachine {entry.Key.Name}: {entry.Value}");
}
return $"[{string.Join(", ", elements)}]";
}

public override bool Equals(object obj)
{
if (obj is VectorTime other)
{
return Clock.OrderBy(x => x.Key).SequenceEqual(other.Clock.OrderBy(x => x.Key));
}
return false;
}

public override int GetHashCode()
{
int hash = 17;
foreach (var entry in Clock)
{
hash = hash * 31 + entry.Key.GetHashCode();
hash = hash * 31 + entry.Value.GetHashCode();
}
return hash;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Threading.Tasks;
using PChecker.Coverage;
using PChecker.Exceptions;
using PChecker.Feedback;
using PChecker.Random;
using PChecker.Runtime.Events;
using PChecker.Runtime.Logging;
Expand Down Expand Up @@ -150,7 +151,6 @@ private static ControlledRuntime CreateWithConfiguration(CheckerConfiguration ch
/// </summary>
public JsonWriter JsonLogger => LogWriter.JsonLogger;


/// <summary>
/// Returns the current hashed state of the monitors.
/// </summary>
Expand Down Expand Up @@ -618,6 +618,8 @@ private EnqueueStatus EnqueueEvent(StateMachineId targetId, Event e, StateMachin
"Cannot send event '{0}' to state machine id '{1}' that is not bound to an state machine instance.",
e.GetType().FullName, targetId.Value);

Scheduler.ScheduledOperation.MessageReceiver = targetId.ToString();

Scheduler.ScheduleNextEnabledOperation(AsyncOperationType.Send);
ResetProgramCounter(sender);

Expand Down Expand Up @@ -647,7 +649,7 @@ private EnqueueStatus EnqueueEvent(StateMachine stateMachine, Event e, StateMach
var originInfo = new EventOriginInfo(sender.Id, sender.GetType().FullName,
sender.CurrentState.GetType().Name);

var eventInfo = new EventInfo(e, originInfo);
var eventInfo = new EventInfo(e, originInfo, sender.VectorTime);

LogWriter.LogSendEvent(stateMachine.Id, sender.Id.Name, sender.Id.Type, sender.CurrentStateName,
e, isTargetHalted: false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ internal abstract class AsyncOperation : IAsyncOperation
/// True if the next awaiter is controlled, else false.
/// </summary>
internal bool IsAwaiterControlled;

/// <summary>
/// The receiver if the operation is Send.
/// </summary>
ankushdesai marked this conversation as resolved.
Show resolved Hide resolved
public string MessageReceiver = "";

/// <summary>
/// Initializes a new instance of the <see cref="AsyncOperation"/> class.
Expand Down
Loading
Loading