private static XElement GenerateElementForPerformanceCounter(
            PerformanceData counter,
            PerformanceData parent)
        {
            XElement result = new XElement("counter");

            result.Add(new XAttribute("name", counter.Name));
            double percentage;

            if (parent == null)
            {
                percentage = 100;
            }
            else
            {
                percentage = GetPercentage(parent.AccumulatedSpan, counter.AccumulatedSpan);
            }
            result.Add(new XAttribute("Percentage", percentage));
            result.Add(new XAttribute("TotalTime", counter.AccumulatedSpan));
            foreach (PerformanceData child in GetChildren(counter))
            {
                result.Add(GenerateElementForPerformanceCounter(child, counter));
            }
            return(result);
        }
 public static void Start(string name, string parentCounterName)
 {
     if (String.IsNullOrEmpty(parentCounterName)) // it's root
     {
         lock (Map)
         {
             if (Map.Count > 0)
             {
                 throw new NotSupportedException("Root element already set.");
             }
             root      = new PerformanceData(name, parentCounterName);
             Map[name] = root;
         }
     }
     else
     {
         if (Map.ContainsKey(name))
         {
             PerformanceData ctr = Map[name];
             if (!ctr.Closed)
             {
                 throw new NotSupportedException("Cannot reopen counter which is not yet closed.");
             }
             ctr.Reopen();
         }
         else
         {
             Map[name] = new PerformanceData(name, parentCounterName);
         }
     }
 }
 private static IEnumerable <PerformanceData> GetChildren(PerformanceData parent)
 {
     return(Map.Where(v =>
                      v.Value.ParentName.Equals(parent.Name, StringComparison.OrdinalIgnoreCase))
            .Select(v => v.Value));
 }