private IEnumerable <ISliceId> ComputeCallersDependencies(ISliceId sliceId)
        {
            Contract.Requires(sliceId != null);

            lock (this.LockDependencesCache)
            {
                IEnumerable <ISliceId> cachedCallers;
                if (this.callersCache.TryGetValue(sliceId, out cachedCallers))
                {
                    return(cachedCallers);
                }

                var result = new Set <ISliceId>();

                // TODO: generalize to a set of methods?
                var methodIdForSliceId = this.sliceOfMethod.Where(pair => pair.Value.Equals(sliceId)).Select(pair => pair.Key).FirstOrDefault();

                if (methodIdForSliceId != null)
                {
                    foreach (var slice in this.slices)
                    {
                        if (slice.Value.Dependencies.Contains(methodIdForSliceId))
                        {
                            // the slice depends on the method
                            result.Add(slice.Key);
                        }
                    }
                }

                this.callersCache.Add(sliceId, result);

                return(result);
            }
        }
示例#2
0
 public WorkToDo(ISliceId sliceId, DateTime time, ISliceHash sliceHash, TPriority priority)
 {
     this.sliceId   = sliceId;
     this.time      = time;
     this.sliceHash = sliceHash;
     this.Priority  = priority;
 }
示例#3
0
        public bool AddTodo(ISliceId sliceId)
        {
            DateTime t = DateTime.Now;

            var sliceHash = this.ComputeSliceHash(sliceId, t);

            if (this.AlreadyComputed(sliceId, sliceHash))
            {
                return(false);
            }

            if (this.WorkInProgress.ContainsKey(sliceHash))
            {
                return(false);
            }

            var priority = this.ComputePriority(sliceId, t); // by default it is based on time, but we can put whatever we want

            var workToDo = new WorkToDo <TPriority>(sliceId, t, sliceHash, priority);

            lock (this.Lock)
            {
                // Notify that the queue is not empty anymore.
                // The main program ends when the queue is empty and all the workers are idle
                this.EmptyQueue.Reset();

                // Remove all the things to do which have the same slice ID
                // This is not true, as our analysis is not monotonic
                this.ToDo.Remove(workToDo);

                // Add to to the queue
                this.ToDo.Add(workToDo);

                // Untested option
                if (this.optionB1)
                {
                    foreach (var p in this.WorkInProgress.GetValueOrEmpty(sliceId).AssumeNotNull())
                    {
                        this.CancelWork(p.Key, p.Value);
                    }
                }

                // Untested option
                if (this.optionA1)
                {
                    this.WorkInProgress.Remove(sliceId);
                }
            }

            return(true);
        }
        public IEnumerable <ISliceId> SlicesForMethodsInTheSameType(ISliceId sliceId)
        {
            SliceDefinition sliceDefinition;

            if (this.slices.TryGetValue(sliceId, out sliceDefinition))
            {
                foreach (var m in sliceDefinition.MethodsInTheSameType)
                {
                    foreach (var slice in this.slices)
                    {
                        if (slice.Value.Methods.Contains(m))
                        {
                            Console.WriteLine("Scheduling slice: {0}", slice.Key);

                            yield return(slice.Key);

                            break; // done with the methodId m;
                        }
                    }
                }
            }
        }
        public IEnumerable <ISliceId> Dependences(ISliceId sliceId)
        {
            IEnumerable <ISliceId> res;

            lock (this.LockDependencesCache)
            {
                if (this.dependencesCache.TryGetValue(sliceId, out res))
                {
                    return(res);
                }
            }

            SliceDefinition sliceDef;

            if (!this.slices.TryGetValue(sliceId, out sliceDef))
            {
                return(EmptySliceIdArray); // should not happen
            }

            var      result = new Set <ISliceId>();
            ISliceId depSliceId;

            foreach (var m in sliceDef.Dependencies)
            {
                if (this.sliceOfMethod.TryGetValue(m, out depSliceId))
                {
                    result.Add(depSliceId);
                }
            }
            lock (this.LockDependencesCache)
            {
                var callers = ComputeCallersDependencies(sliceId);
                result.AddRange(callers);

                this.dependencesCache.Add(sliceId, result);
            }
            return(result);
        }
        public ISliceHash ComputeSliceHash(ISliceId sliceId, DateTime t)
        {
            // We do not consider the methods in the same type at the moment

            SliceDefinition sliceDef;

            // If we do not know the slice, we just return null
            if (!this.slices.TryGetValue(sliceId, out sliceDef))
            {
                return(null);
            }

            // TODO: wire the hashing option
            using (var tw = new HashWriter(false))
            {
                foreach (var methodId in sliceDef.Dependencies)
                {
                    tw.Write(methodId);
                    tw.Write(':');

                    var    methodHash = this.GetHashForDate(methodId, t, false);
                    Method methodModel;

                    if (methodHash == null || !this.TryGetMethodModelForHash(methodHash, out methodModel))
                    {
                        tw.WriteLine("<null>");
                    }
                    else
                    {
                        tw.WriteLine(this.GetResultHash(methodModel));
                    }
                }

                return(new SliceHash(tw.GetHash()));
            }
        }
 public void MarkAsComputed(ISliceId sliceId, ISliceHash sliceHash)
 {
     lock (this.LockComputed)
         this.computed.Add(Pair.For(sliceId, sliceHash));
 }
 public bool AlreadyComputed(ISliceId sliceId, ISliceHash sliceHash)
 {
     lock (this.LockComputed)
         return(this.computed.Contains(Pair.For(sliceId, sliceHash)));
 }
 public bool Equals(ISliceId other)
 {
     return(other != null && this.id == other.Id);
 }
示例#10
0
 protected override long ComputePriority(ISliceId sliceId, DateTime t)
 {
     return(-t.Ticks);
 }
示例#11
0
 protected abstract TPriority ComputePriority(ISliceId sliceId, DateTime t);