| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | namespace WixToolset.Harvesters |
| 4 | { |
| 5 | using System; |
| 6 | using System.Linq; |
| 7 | using System.Diagnostics; |
| 8 | using WixToolset.Data; |
| 9 | using WixToolset.Harvesters.Data; |
| 10 | using WixToolset.Harvesters.Extensibility; |
| 11 | using Util = WixToolset.Harvesters.Serialize.Util; |
| 12 | using Wix = WixToolset.Harvesters.Serialize; |
| 13 | |
| 14 | /// <summary> |
| 15 | /// Harvest WiX authoring for a file from the file system. |
| 16 | /// </summary> |
| 17 | public sealed class PerformanceCategoryHarvester : BaseHarvesterExtension |
| 18 | { |
| 19 | /// <summary> |
| 20 | /// Harvest a performance category. |
| 21 | /// </summary> |
| 22 | /// <param name="argument">The name of the performance category.</param> |
| 23 | /// <returns>A harvested performance category.</returns> |
| 24 | public override Wix.Fragment[] Harvest(string argument) |
| 25 | { |
| 26 | if (null == argument) |
| 27 | { |
| 28 | throw new ArgumentNullException("argument"); |
| 29 | } |
| 30 | |
| 31 | Util.PerformanceCategory perf = this.HarvestPerformanceCategory(argument); |
| 32 | |
| 33 | Wix.Component component = new Wix.Component(); |
| 34 | component.Id = this.Core.CreateIdentifierFromFilename(argument); |
| 35 | component.KeyPath = Wix.YesNoType.yes; |
| 36 | component.AddChild(perf); |
| 37 | |
| 38 | var directory = DirectoryHelper.CreateDirectory("TARGETDIR"); |
| 39 | directory.AddChild(component); |
| 40 | |
| 41 | Wix.Fragment fragment = new Wix.Fragment(); |
| 42 | fragment.AddChild(directory); |
| 43 | |
| 44 | return new Wix.Fragment[] { fragment }; |
| 45 | } |
| 46 | |
| 47 | /// <summary> |
| 48 | /// Harvest a performance category. |
| 49 | /// </summary> |
| 50 | /// <param name="category">The name of the performance category.</param> |
| 51 | /// <returns>A harvested file.</returns> |
| 52 | public Util.PerformanceCategory HarvestPerformanceCategory(string category) |
| 53 | { |
| 54 | if (null == category) |
| 55 | { |
| 56 | throw new ArgumentNullException("category"); |
| 57 | } |
| 58 | |
| 59 | if (PerformanceCounterCategory.Exists(category)) |
| 60 | { |
| 61 | Util.PerformanceCategory perfCategory = new Util.PerformanceCategory(); |
| 62 | |
| 63 | // Get the performance counter category and set the appropriate WiX attributes |
| 64 | PerformanceCounterCategory pcc = PerformanceCounterCategory.GetCategories().Single(c => string.Equals(c.CategoryName, category)); |
| 65 | perfCategory.Id = this.Core.CreateIdentifierFromFilename(pcc.CategoryName); |
| 66 | perfCategory.Name = pcc.CategoryName; |
| 67 | perfCategory.Help = pcc.CategoryHelp; |
| 68 | if (PerformanceCounterCategoryType.MultiInstance == pcc.CategoryType) |
| 69 | { |
| 70 | perfCategory.MultiInstance = Util.YesNoType.yes; |
| 71 | } |
| 72 | |
| 73 | // If it's multi-instance, check if there are any instances and get counters from there; else we get |
| 74 | // the counters straight up. For multi-instance, GetCounters() fails if there are any instances. If there |
| 75 | // are no instances, then GetCounters(instance) can't be called since there is no instance. Instances |
| 76 | // will exist for each counter even if only one of the counters was "intialized." |
| 77 | string[] instances = pcc.GetInstanceNames(); |
| 78 | bool hasInstances = instances.Length > 0; |
| 79 | PerformanceCounter[] counters = hasInstances |
| 80 | ? pcc.GetCounters(instances.First()) |
| 81 | : pcc.GetCounters(); |
| 82 | |
| 83 | foreach (PerformanceCounter counter in counters) |
| 84 | { |
| 85 | Util.PerformanceCounter perfCounter = new Util.PerformanceCounter(); |
| 86 | |
| 87 | // Get the performance counter and set the appropriate WiX attributes |
| 88 | perfCounter.Name = counter.CounterName; |
| 89 | perfCounter.Type = this.CounterTypeToWix(counter.CounterType); |
| 90 | perfCounter.Help = counter.CounterHelp; |
| 91 | |
| 92 | perfCategory.AddChild(perfCounter); |
| 93 | } |
| 94 | |
| 95 | return perfCategory; |
| 96 | } |
| 97 | else |
| 98 | { |
| 99 | throw new WixException(HarvesterErrors.PerformanceCategoryNotFound(category)); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /// <summary> |
| 104 | /// Get the WiX performance counter type. |
| 105 | /// </summary> |
| 106 | /// <param name="pct">The performance counter value to get.</param> |
| 107 | /// <returns>The WiX performance counter type.</returns> |
| 108 | private Util.PerformanceCounterTypesType CounterTypeToWix(PerformanceCounterType pct) |
| 109 | { |
| 110 | Util.PerformanceCounterTypesType type; |
| 111 | |
| 112 | switch (pct) |
| 113 | { |
| 114 | case PerformanceCounterType.AverageBase: |
| 115 | type = Util.PerformanceCounterTypesType.averageBase; |
| 116 | break; |
| 117 | case PerformanceCounterType.AverageCount64: |
| 118 | type = Util.PerformanceCounterTypesType.averageCount64; |
| 119 | break; |
| 120 | case PerformanceCounterType.AverageTimer32: |
| 121 | type = Util.PerformanceCounterTypesType.averageTimer32; |
| 122 | break; |
| 123 | case PerformanceCounterType.CounterDelta32: |
| 124 | type = Util.PerformanceCounterTypesType.counterDelta32; |
| 125 | break; |
| 126 | case PerformanceCounterType.CounterTimerInverse: |
| 127 | type = Util.PerformanceCounterTypesType.counterTimerInverse; |
| 128 | break; |
| 129 | case PerformanceCounterType.SampleFraction: |
| 130 | type = Util.PerformanceCounterTypesType.sampleFraction; |
| 131 | break; |
| 132 | case PerformanceCounterType.Timer100Ns: |
| 133 | type = Util.PerformanceCounterTypesType.timer100Ns; |
| 134 | break; |
| 135 | case PerformanceCounterType.CounterTimer: |
| 136 | type = Util.PerformanceCounterTypesType.counterTimer; |
| 137 | break; |
| 138 | case PerformanceCounterType.RawFraction: |
| 139 | type = Util.PerformanceCounterTypesType.rawFraction; |
| 140 | break; |
| 141 | case PerformanceCounterType.Timer100NsInverse: |
| 142 | type = Util.PerformanceCounterTypesType.timer100NsInverse; |
| 143 | break; |
| 144 | case PerformanceCounterType.CounterMultiTimer: |
| 145 | type = Util.PerformanceCounterTypesType.counterMultiTimer; |
| 146 | break; |
| 147 | case PerformanceCounterType.CounterMultiTimer100Ns: |
| 148 | type = Util.PerformanceCounterTypesType.counterMultiTimer100Ns; |
| 149 | break; |
| 150 | case PerformanceCounterType.CounterMultiTimerInverse: |
| 151 | type = Util.PerformanceCounterTypesType.counterMultiTimerInverse; |
| 152 | break; |
| 153 | case PerformanceCounterType.CounterMultiTimer100NsInverse: |
| 154 | type = Util.PerformanceCounterTypesType.counterMultiTimer100NsInverse; |
| 155 | break; |
| 156 | case PerformanceCounterType.ElapsedTime: |
| 157 | type = Util.PerformanceCounterTypesType.elapsedTime; |
| 158 | break; |
| 159 | case PerformanceCounterType.SampleBase: |
| 160 | type = Util.PerformanceCounterTypesType.sampleBase; |
| 161 | break; |
| 162 | case PerformanceCounterType.RawBase: |
| 163 | type = Util.PerformanceCounterTypesType.rawBase; |
| 164 | break; |
| 165 | case PerformanceCounterType.CounterMultiBase: |
| 166 | type = Util.PerformanceCounterTypesType.counterMultiBase; |
| 167 | break; |
| 168 | case PerformanceCounterType.RateOfCountsPerSecond64: |
| 169 | type = Util.PerformanceCounterTypesType.rateOfCountsPerSecond64; |
| 170 | break; |
| 171 | case PerformanceCounterType.RateOfCountsPerSecond32: |
| 172 | type = Util.PerformanceCounterTypesType.rateOfCountsPerSecond32; |
| 173 | break; |
| 174 | case PerformanceCounterType.CountPerTimeInterval64: |
| 175 | type = Util.PerformanceCounterTypesType.countPerTimeInterval64; |
| 176 | break; |
| 177 | case PerformanceCounterType.CountPerTimeInterval32: |
| 178 | type = Util.PerformanceCounterTypesType.countPerTimeInterval32; |
| 179 | break; |
| 180 | case PerformanceCounterType.SampleCounter: |
| 181 | type = Util.PerformanceCounterTypesType.sampleCounter; |
| 182 | break; |
| 183 | case PerformanceCounterType.CounterDelta64: |
| 184 | type = Util.PerformanceCounterTypesType.counterDelta64; |
| 185 | break; |
| 186 | case PerformanceCounterType.NumberOfItems64: |
| 187 | type = Util.PerformanceCounterTypesType.numberOfItems64; |
| 188 | break; |
| 189 | case PerformanceCounterType.NumberOfItems32: |
| 190 | type = Util.PerformanceCounterTypesType.numberOfItems32; |
| 191 | break; |
| 192 | case PerformanceCounterType.NumberOfItemsHEX64: |
| 193 | type = Util.PerformanceCounterTypesType.numberOfItemsHEX64; |
| 194 | break; |
| 195 | case PerformanceCounterType.NumberOfItemsHEX32: |
| 196 | type = Util.PerformanceCounterTypesType.numberOfItemsHEX32; |
| 197 | break; |
| 198 | default: |
| 199 | throw new WixException(HarvesterErrors.UnsupportedPerformanceCounterType(pct.ToString())); |
| 200 | } |
| 201 | |
| 202 | return type; |
| 203 | } |
| 204 | } |
| 205 | } |