/// <summary> /// Initializes the process (necessary for constructor overload) /// </summary> /// <param name="name">Name of the rpocess</param> /// <param name="ioProbability">I/O Probability of the process</param> /// <param name="ioSwiftness">I/O Swiftness of the process</param> /// <param name="length">Length of the process</param> private void InitializeProcess(string name, double ioProbability, IO.Speed ioSwiftness, int length) { if (name.Length < 2 || name.Length > 15) { throw new ArgumentException("Parameter must be between 2 and 15 character", "Process Name"); } if (ioProbability < 0 || ioProbability > 1) { throw new ArgumentException("Parameter must be between 0 and 1", "I/O Probability"); } if (length <= 0) { throw new ArgumentException("Parameter must be greater than 0", "Process Length"); } this.name = name; this.ioSwiftness = ioSwiftness; this.ioProbability = ioProbability; this.length = length; cpuTime = 0; waitTime = 0; ioTime = 0; ioCount = 0; remainingTick = length; }
/// <summary> /// Process constructor /// </summary> /// <param name="name">Name of the process</param> /// <param name="ioProbability">I/O Probability of the process (in percent)</param> /// <param name="ioSwiftness">I/O Swiftness of the process</param> /// <param name="length">Process length</param> public Process(string name, int ioProbability, IO.Speed ioSwiftness, int length) { if (ioProbability < 0 || ioProbability > 100) { throw new ArgumentException("Parameter must be between 0 and 100", "I/O Probability"); } InitializeProcess(name, (double)ioProbability / 100, ioSwiftness, length); }
/// <summary> /// This function starts an I/O operation. /// </summary> private void AddIO() { //There is no I/O operation if (currentIO == null) { //Process has a chance to get I/O operation (normal distribution, 5% deviation, Box-Muller transform) Random random = new Random(); double u1 = 1.0 - random.NextDouble(); double u2 = 1.0 - random.NextDouble(); double randStdNormal = Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2); double randNormal = ioProbability + 0.05 * randStdNormal; double rand = random.NextDouble(); if (rand <= randNormal) { //Our process wants I/O operation ioCount++; //Determine speed of operation (80% chance to get ioSwiftness speed, 10-10% the other two) int randomSwiftness = random.Next(0, 9); IO.Speed speed = IO.Speed.FAST; if (randomSwiftness < 8) { speed = ioSwiftness; } else { // This is overkill // Cycles between enum values // -- Adds randomSwiftness - 7 to currentSwiftness // -- Wraps around enum values (e.g., If enum has 3 values, then 3 + 1 == 0) speed = (IO.Speed)((randomSwiftness - 7 + (int)ioSwiftness) % Enum.GetValues(typeof(IO.Speed)).Length); } currentIO = new IO(speed); } } }
/// <summary> /// Process constructor /// </summary> /// <param name="name">Name of the process</param> /// <param name="ioProbability">I/O Probability of the process</param> /// <param name="ioSwiftness">I/O Swiftness of the process</param> /// <param name="length">Process length</param> public Process(string name, double ioProbability, IO.Speed ioSwiftness, int length) { InitializeProcess(name, ioProbability, ioSwiftness, length); }