private void MicroThreadCallbackStart(object sender, SchedulerThreadEventArgs e) { TimeInterval timeInterval; int intervalCount; lock (threadExecutionIntervals) { List<TimeInterval> intervals = threadExecutionIntervals[e.ThreadId]; if (intervals.Count > 0 && !intervals.Last().HasEnded) throw new InvalidOperationException("Starting a new microthread on a thread still running another microthread."); timeInterval = new TimeInterval(GetTicksFromStopwatch()); intervals.Add(timeInterval); intervalCount = intervals.Count; } // Rely on intervals.Count, so must be called after intervals.Add! long jobId = GetMicrothreadJobIdFromThreadInfo(e.ThreadId, intervalCount); var jobInfo = new MicrothreadNotification(e.ThreadId, e.MicroThread.Id, jobId, timeInterval.StartTime, MicrothreadNotification.NotificationType.JobStarted); lock (microthreadNotifications) { microthreadNotifications.Add(jobInfo); } }
public void CommandEnded(CommandBuildStep command) { lock (lockObject) { TimeInterval commandInterval = commandExecutionIntervals[command]; long startTime = commandInterval.StartTime; long endTime = stopWatch.ElapsedTicks; commandInterval.End(endTime); commandExecutionIntervals.Remove(command); foreach (var outputObject in command.Result.OutputObjects) { var outputUrl = outputObject.Key; ObjectAccesses inputAccess; if (objectsAccesses.TryGetValue(outputUrl, out inputAccess)) { foreach (TimeInterval <BuildStep> input in inputAccess.Reads.Where(input => input.Object != command && input.Overlap(startTime, endTime))) { logger.Error("Command {0} is writing {1} while command {2} is reading it", command, outputUrl, input.Object); } } ObjectAccesses outputAccess; if (!objectsAccesses.TryGetValue(outputUrl, out outputAccess)) { objectsAccesses.Add(outputUrl, outputAccess = new ObjectAccesses()); } foreach (var output in outputAccess.Writes.Where(output => output.Object.Key != command && output.Overlap(startTime, endTime))) { if (outputObject.Value != output.Object.Value) { logger.Error("Commands {0} and {1} are both writing {2} at the same time, but they are different objects", command, output.Object, outputUrl); } } outputAccess.Writes.Add(new TimeInterval <KeyValuePair <BuildStep, ObjectId> >(new KeyValuePair <BuildStep, ObjectId>(command, outputObject.Value), startTime, endTime)); } foreach (ObjectUrl inputUrl in command.Result.InputDependencyVersions.Keys) { ObjectAccesses outputAccess; if (objectsAccesses.TryGetValue(inputUrl, out outputAccess)) { foreach (TimeInterval <KeyValuePair <BuildStep, ObjectId> > output in outputAccess.Writes.Where(output => output.Object.Key != command && output.Overlap(startTime, endTime))) { logger.Error("Command {0} is writing {1} while command {2} is reading it", output.Object, inputUrl, command); } } } // Notify that we're done reading input files List <ObjectUrl> inputFiles; if (commandInputFiles.TryGetValue(command, out inputFiles)) { commandInputFiles.Remove(command); foreach (ObjectUrl input in inputFiles) { objectsAccesses[input].Reads.Single(x => x.Object == command).End(endTime); } } // "Garbage collection" of accesses var newEarliestCommandAliveStartTime = commandExecutionIntervals.Count > 0 ? commandExecutionIntervals.Min(x => x.Value.StartTime) : endTime; if (newEarliestCommandAliveStartTime > earliestCommandAliveStartTime) { earliestCommandAliveStartTime = newEarliestCommandAliveStartTime; // We can remove objects whose all R/W accesses are "completed" (EndTime is set) // and happened before all the current running commands started, since they won't affect us foreach (var objectAccesses in objectsAccesses.ToList()) { if (objectAccesses.Value.Reads.All(x => x.EndTime != 0 && x.EndTime < earliestCommandAliveStartTime) && objectAccesses.Value.Writes.All(x => x.EndTime != 0 && x.EndTime < earliestCommandAliveStartTime)) { objectsAccesses.Remove(objectAccesses.Key); } } } } }