diff options
Diffstat (limited to 'src/tools/heat/PerformanceCategoryHarvester.cs')
-rw-r--r-- | src/tools/heat/PerformanceCategoryHarvester.cs | 207 |
1 files changed, 207 insertions, 0 deletions
diff --git a/src/tools/heat/PerformanceCategoryHarvester.cs b/src/tools/heat/PerformanceCategoryHarvester.cs new file mode 100644 index 00000000..6be3401b --- /dev/null +++ b/src/tools/heat/PerformanceCategoryHarvester.cs | |||
@@ -0,0 +1,207 @@ | |||
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 | Wix.Directory directory = new Wix.Directory(); | ||
39 | directory.Id = "TARGETDIR"; | ||
40 | //directory.Name = directory.Id; | ||
41 | directory.AddChild(component); | ||
42 | |||
43 | Wix.Fragment fragment = new Wix.Fragment(); | ||
44 | fragment.AddChild(directory); | ||
45 | |||
46 | return new Wix.Fragment[] { fragment }; | ||
47 | } | ||
48 | |||
49 | /// <summary> | ||
50 | /// Harvest a performance category. | ||
51 | /// </summary> | ||
52 | /// <param name="category">The name of the performance category.</param> | ||
53 | /// <returns>A harvested file.</returns> | ||
54 | public Util.PerformanceCategory HarvestPerformanceCategory(string category) | ||
55 | { | ||
56 | if (null == category) | ||
57 | { | ||
58 | throw new ArgumentNullException("category"); | ||
59 | } | ||
60 | |||
61 | if (PerformanceCounterCategory.Exists(category)) | ||
62 | { | ||
63 | Util.PerformanceCategory perfCategory = new Util.PerformanceCategory(); | ||
64 | |||
65 | // Get the performance counter category and set the appropriate WiX attributes | ||
66 | PerformanceCounterCategory pcc = PerformanceCounterCategory.GetCategories().Single(c => string.Equals(c.CategoryName, category)); | ||
67 | perfCategory.Id = this.Core.CreateIdentifierFromFilename(pcc.CategoryName); | ||
68 | perfCategory.Name = pcc.CategoryName; | ||
69 | perfCategory.Help = pcc.CategoryHelp; | ||
70 | if (PerformanceCounterCategoryType.MultiInstance == pcc.CategoryType) | ||
71 | { | ||
72 | perfCategory.MultiInstance = Util.YesNoType.yes; | ||
73 | } | ||
74 | |||
75 | // If it's multi-instance, check if there are any instances and get counters from there; else we get | ||
76 | // the counters straight up. For multi-instance, GetCounters() fails if there are any instances. If there | ||
77 | // are no instances, then GetCounters(instance) can't be called since there is no instance. Instances | ||
78 | // will exist for each counter even if only one of the counters was "intialized." | ||
79 | string[] instances = pcc.GetInstanceNames(); | ||
80 | bool hasInstances = instances.Length > 0; | ||
81 | PerformanceCounter[] counters = hasInstances | ||
82 | ? pcc.GetCounters(instances.First()) | ||
83 | : pcc.GetCounters(); | ||
84 | |||
85 | foreach (PerformanceCounter counter in counters) | ||
86 | { | ||
87 | Util.PerformanceCounter perfCounter = new Util.PerformanceCounter(); | ||
88 | |||
89 | // Get the performance counter and set the appropriate WiX attributes | ||
90 | perfCounter.Name = counter.CounterName; | ||
91 | perfCounter.Type = this.CounterTypeToWix(counter.CounterType); | ||
92 | perfCounter.Help = counter.CounterHelp; | ||
93 | |||
94 | perfCategory.AddChild(perfCounter); | ||
95 | } | ||
96 | |||
97 | return perfCategory; | ||
98 | } | ||
99 | else | ||
100 | { | ||
101 | throw new WixException(HarvesterErrors.PerformanceCategoryNotFound(category)); | ||
102 | } | ||
103 | } | ||
104 | |||
105 | /// <summary> | ||
106 | /// Get the WiX performance counter type. | ||
107 | /// </summary> | ||
108 | /// <param name="pct">The performance counter value to get.</param> | ||
109 | /// <returns>The WiX performance counter type.</returns> | ||
110 | private Util.PerformanceCounterTypesType CounterTypeToWix(PerformanceCounterType pct) | ||
111 | { | ||
112 | Util.PerformanceCounterTypesType type; | ||
113 | |||
114 | switch (pct) | ||
115 | { | ||
116 | case PerformanceCounterType.AverageBase: | ||
117 | type = Util.PerformanceCounterTypesType.averageBase; | ||
118 | break; | ||
119 | case PerformanceCounterType.AverageCount64: | ||
120 | type = Util.PerformanceCounterTypesType.averageCount64; | ||
121 | break; | ||
122 | case PerformanceCounterType.AverageTimer32: | ||
123 | type = Util.PerformanceCounterTypesType.averageTimer32; | ||
124 | break; | ||
125 | case PerformanceCounterType.CounterDelta32: | ||
126 | type = Util.PerformanceCounterTypesType.counterDelta32; | ||
127 | break; | ||
128 | case PerformanceCounterType.CounterTimerInverse: | ||
129 | type = Util.PerformanceCounterTypesType.counterTimerInverse; | ||
130 | break; | ||
131 | case PerformanceCounterType.SampleFraction: | ||
132 | type = Util.PerformanceCounterTypesType.sampleFraction; | ||
133 | break; | ||
134 | case PerformanceCounterType.Timer100Ns: | ||
135 | type = Util.PerformanceCounterTypesType.timer100Ns; | ||
136 | break; | ||
137 | case PerformanceCounterType.CounterTimer: | ||
138 | type = Util.PerformanceCounterTypesType.counterTimer; | ||
139 | break; | ||
140 | case PerformanceCounterType.RawFraction: | ||
141 | type = Util.PerformanceCounterTypesType.rawFraction; | ||
142 | break; | ||
143 | case PerformanceCounterType.Timer100NsInverse: | ||
144 | type = Util.PerformanceCounterTypesType.timer100NsInverse; | ||
145 | break; | ||
146 | case PerformanceCounterType.CounterMultiTimer: | ||
147 | type = Util.PerformanceCounterTypesType.counterMultiTimer; | ||
148 | break; | ||
149 | case PerformanceCounterType.CounterMultiTimer100Ns: | ||
150 | type = Util.PerformanceCounterTypesType.counterMultiTimer100Ns; | ||
151 | break; | ||
152 | case PerformanceCounterType.CounterMultiTimerInverse: | ||
153 | type = Util.PerformanceCounterTypesType.counterMultiTimerInverse; | ||
154 | break; | ||
155 | case PerformanceCounterType.CounterMultiTimer100NsInverse: | ||
156 | type = Util.PerformanceCounterTypesType.counterMultiTimer100NsInverse; | ||
157 | break; | ||
158 | case PerformanceCounterType.ElapsedTime: | ||
159 | type = Util.PerformanceCounterTypesType.elapsedTime; | ||
160 | break; | ||
161 | case PerformanceCounterType.SampleBase: | ||
162 | type = Util.PerformanceCounterTypesType.sampleBase; | ||
163 | break; | ||
164 | case PerformanceCounterType.RawBase: | ||
165 | type = Util.PerformanceCounterTypesType.rawBase; | ||
166 | break; | ||
167 | case PerformanceCounterType.CounterMultiBase: | ||
168 | type = Util.PerformanceCounterTypesType.counterMultiBase; | ||
169 | break; | ||
170 | case PerformanceCounterType.RateOfCountsPerSecond64: | ||
171 | type = Util.PerformanceCounterTypesType.rateOfCountsPerSecond64; | ||
172 | break; | ||
173 | case PerformanceCounterType.RateOfCountsPerSecond32: | ||
174 | type = Util.PerformanceCounterTypesType.rateOfCountsPerSecond32; | ||
175 | break; | ||
176 | case PerformanceCounterType.CountPerTimeInterval64: | ||
177 | type = Util.PerformanceCounterTypesType.countPerTimeInterval64; | ||
178 | break; | ||
179 | case PerformanceCounterType.CountPerTimeInterval32: | ||
180 | type = Util.PerformanceCounterTypesType.countPerTimeInterval32; | ||
181 | break; | ||
182 | case PerformanceCounterType.SampleCounter: | ||
183 | type = Util.PerformanceCounterTypesType.sampleCounter; | ||
184 | break; | ||
185 | case PerformanceCounterType.CounterDelta64: | ||
186 | type = Util.PerformanceCounterTypesType.counterDelta64; | ||
187 | break; | ||
188 | case PerformanceCounterType.NumberOfItems64: | ||
189 | type = Util.PerformanceCounterTypesType.numberOfItems64; | ||
190 | break; | ||
191 | case PerformanceCounterType.NumberOfItems32: | ||
192 | type = Util.PerformanceCounterTypesType.numberOfItems32; | ||
193 | break; | ||
194 | case PerformanceCounterType.NumberOfItemsHEX64: | ||
195 | type = Util.PerformanceCounterTypesType.numberOfItemsHEX64; | ||
196 | break; | ||
197 | case PerformanceCounterType.NumberOfItemsHEX32: | ||
198 | type = Util.PerformanceCounterTypesType.numberOfItemsHEX32; | ||
199 | break; | ||
200 | default: | ||
201 | throw new WixException(HarvesterErrors.UnsupportedPerformanceCounterType(pct.ToString())); | ||
202 | } | ||
203 | |||
204 | return type; | ||
205 | } | ||
206 | } | ||
207 | } | ||