1*bbb1b6f9SApple OSS Distributions /*
2*bbb1b6f9SApple OSS Distributions * Copyright (c) 2012-2013 Apple Computer, Inc. All Rights Reserved.
3*bbb1b6f9SApple OSS Distributions *
4*bbb1b6f9SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*bbb1b6f9SApple OSS Distributions *
6*bbb1b6f9SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*bbb1b6f9SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*bbb1b6f9SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*bbb1b6f9SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*bbb1b6f9SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*bbb1b6f9SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*bbb1b6f9SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*bbb1b6f9SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*bbb1b6f9SApple OSS Distributions *
15*bbb1b6f9SApple OSS Distributions * Please obtain a copy of the License at
16*bbb1b6f9SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*bbb1b6f9SApple OSS Distributions *
18*bbb1b6f9SApple OSS Distributions * The Original Code and all software distributed under the License are
19*bbb1b6f9SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*bbb1b6f9SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*bbb1b6f9SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*bbb1b6f9SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*bbb1b6f9SApple OSS Distributions * Please see the License for the specific language governing rights and
24*bbb1b6f9SApple OSS Distributions * limitations under the License.
25*bbb1b6f9SApple OSS Distributions *
26*bbb1b6f9SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*bbb1b6f9SApple OSS Distributions */
28*bbb1b6f9SApple OSS Distributions
29*bbb1b6f9SApple OSS Distributions #define IOKIT_ENABLE_SHARED_PTR
30*bbb1b6f9SApple OSS Distributions
31*bbb1b6f9SApple OSS Distributions #define __STDC_LIMIT_MACROS // what are the C++ equivalents?
32*bbb1b6f9SApple OSS Distributions #include <stdint.h>
33*bbb1b6f9SApple OSS Distributions
34*bbb1b6f9SApple OSS Distributions #include <IOKit/IOKernelReportStructs.h>
35*bbb1b6f9SApple OSS Distributions #include <IOKit/IOKernelReporters.h>
36*bbb1b6f9SApple OSS Distributions #include <os/overflow.h>
37*bbb1b6f9SApple OSS Distributions #include "IOReporterDefs.h"
38*bbb1b6f9SApple OSS Distributions
39*bbb1b6f9SApple OSS Distributions
40*bbb1b6f9SApple OSS Distributions #define super IOReporter
41*bbb1b6f9SApple OSS Distributions OSDefineMetaClassAndStructors(IOHistogramReporter, IOReporter);
42*bbb1b6f9SApple OSS Distributions
43*bbb1b6f9SApple OSS Distributions /* static */
44*bbb1b6f9SApple OSS Distributions OSSharedPtr<IOHistogramReporter>
with(IOService * reportingService,IOReportCategories categories,uint64_t channelID,const char * channelName,IOReportUnit unit,int nSegments,IOHistogramSegmentConfig * config)45*bbb1b6f9SApple OSS Distributions IOHistogramReporter::with(IOService *reportingService,
46*bbb1b6f9SApple OSS Distributions IOReportCategories categories,
47*bbb1b6f9SApple OSS Distributions uint64_t channelID,
48*bbb1b6f9SApple OSS Distributions const char *channelName,
49*bbb1b6f9SApple OSS Distributions IOReportUnit unit,
50*bbb1b6f9SApple OSS Distributions int nSegments,
51*bbb1b6f9SApple OSS Distributions IOHistogramSegmentConfig *config)
52*bbb1b6f9SApple OSS Distributions {
53*bbb1b6f9SApple OSS Distributions OSSharedPtr<IOHistogramReporter> reporter = OSMakeShared<IOHistogramReporter>();
54*bbb1b6f9SApple OSS Distributions OSSharedPtr<const OSSymbol> tmpChannelName;
55*bbb1b6f9SApple OSS Distributions
56*bbb1b6f9SApple OSS Distributions if (reporter) {
57*bbb1b6f9SApple OSS Distributions if (channelName) {
58*bbb1b6f9SApple OSS Distributions tmpChannelName = OSSymbol::withCString(channelName);
59*bbb1b6f9SApple OSS Distributions }
60*bbb1b6f9SApple OSS Distributions
61*bbb1b6f9SApple OSS Distributions if (reporter->initWith(reportingService, categories,
62*bbb1b6f9SApple OSS Distributions channelID, tmpChannelName.get(),
63*bbb1b6f9SApple OSS Distributions unit, nSegments, config)) {
64*bbb1b6f9SApple OSS Distributions return reporter;
65*bbb1b6f9SApple OSS Distributions }
66*bbb1b6f9SApple OSS Distributions }
67*bbb1b6f9SApple OSS Distributions
68*bbb1b6f9SApple OSS Distributions return nullptr;
69*bbb1b6f9SApple OSS Distributions }
70*bbb1b6f9SApple OSS Distributions
71*bbb1b6f9SApple OSS Distributions
72*bbb1b6f9SApple OSS Distributions bool
initWith(IOService * reportingService,IOReportCategories categories,uint64_t channelID,const OSSymbol * channelName,IOReportUnit unit,int nSegments,IOHistogramSegmentConfig * config)73*bbb1b6f9SApple OSS Distributions IOHistogramReporter::initWith(IOService *reportingService,
74*bbb1b6f9SApple OSS Distributions IOReportCategories categories,
75*bbb1b6f9SApple OSS Distributions uint64_t channelID,
76*bbb1b6f9SApple OSS Distributions const OSSymbol *channelName,
77*bbb1b6f9SApple OSS Distributions IOReportUnit unit,
78*bbb1b6f9SApple OSS Distributions int nSegments,
79*bbb1b6f9SApple OSS Distributions IOHistogramSegmentConfig *config)
80*bbb1b6f9SApple OSS Distributions {
81*bbb1b6f9SApple OSS Distributions bool result = false;
82*bbb1b6f9SApple OSS Distributions IOReturn res; // for PREFL_MEMOP
83*bbb1b6f9SApple OSS Distributions size_t configSize, elementsSize, eCountsSize, boundsSize;
84*bbb1b6f9SApple OSS Distributions int cnt, cnt2, cnt3 = 0;
85*bbb1b6f9SApple OSS Distributions int64_t bucketBound = 0, previousBucketBound = 0;
86*bbb1b6f9SApple OSS Distributions
87*bbb1b6f9SApple OSS Distributions // analyzer appeasement
88*bbb1b6f9SApple OSS Distributions configSize = elementsSize = eCountsSize = boundsSize = 0;
89*bbb1b6f9SApple OSS Distributions
90*bbb1b6f9SApple OSS Distributions IORLOG("IOHistogramReporter::initWith");
91*bbb1b6f9SApple OSS Distributions
92*bbb1b6f9SApple OSS Distributions // For now, this reporter is currently limited to a single channel
93*bbb1b6f9SApple OSS Distributions _nChannels = 1;
94*bbb1b6f9SApple OSS Distributions
95*bbb1b6f9SApple OSS Distributions IOReportChannelType channelType = {
96*bbb1b6f9SApple OSS Distributions .categories = categories,
97*bbb1b6f9SApple OSS Distributions .report_format = kIOReportFormatHistogram,
98*bbb1b6f9SApple OSS Distributions .nelements = 0, // Initialized when Config is unpacked
99*bbb1b6f9SApple OSS Distributions .element_idx = 0
100*bbb1b6f9SApple OSS Distributions };
101*bbb1b6f9SApple OSS Distributions
102*bbb1b6f9SApple OSS Distributions if (super::init(reportingService, channelType, unit) != true) {
103*bbb1b6f9SApple OSS Distributions IORLOG("%s - ERROR: super::init failed", __func__);
104*bbb1b6f9SApple OSS Distributions result = false;
105*bbb1b6f9SApple OSS Distributions goto finish;
106*bbb1b6f9SApple OSS Distributions }
107*bbb1b6f9SApple OSS Distributions
108*bbb1b6f9SApple OSS Distributions // Make sure to call this after the commit init phase
109*bbb1b6f9SApple OSS Distributions if (channelName) {
110*bbb1b6f9SApple OSS Distributions _channelNames->setObject(channelName);
111*bbb1b6f9SApple OSS Distributions }
112*bbb1b6f9SApple OSS Distributions
113*bbb1b6f9SApple OSS Distributions _segmentCount = nSegments;
114*bbb1b6f9SApple OSS Distributions if (_segmentCount == 0) {
115*bbb1b6f9SApple OSS Distributions IORLOG("IOReportHistogram init ERROR. No configuration provided!");
116*bbb1b6f9SApple OSS Distributions result = false;
117*bbb1b6f9SApple OSS Distributions goto finish;
118*bbb1b6f9SApple OSS Distributions }
119*bbb1b6f9SApple OSS Distributions
120*bbb1b6f9SApple OSS Distributions IORLOG("%s - %u segment(s)", __func__, _segmentCount);
121*bbb1b6f9SApple OSS Distributions
122*bbb1b6f9SApple OSS Distributions PREFL_MEMOP_FAIL(_segmentCount, IOHistogramSegmentConfig);
123*bbb1b6f9SApple OSS Distributions configSize = (size_t)_segmentCount * sizeof(IOHistogramSegmentConfig);
124*bbb1b6f9SApple OSS Distributions _histogramSegmentsConfig = (IOHistogramSegmentConfig*)IOMallocData(configSize);
125*bbb1b6f9SApple OSS Distributions if (!_histogramSegmentsConfig) {
126*bbb1b6f9SApple OSS Distributions goto finish;
127*bbb1b6f9SApple OSS Distributions }
128*bbb1b6f9SApple OSS Distributions memcpy(_histogramSegmentsConfig, config, configSize);
129*bbb1b6f9SApple OSS Distributions
130*bbb1b6f9SApple OSS Distributions // Find out how many elements are need to store the histogram
131*bbb1b6f9SApple OSS Distributions for (cnt = 0; cnt < _segmentCount; cnt++) {
132*bbb1b6f9SApple OSS Distributions _nElements += _histogramSegmentsConfig[cnt].segment_bucket_count;
133*bbb1b6f9SApple OSS Distributions _channelDimension += _histogramSegmentsConfig[cnt].segment_bucket_count;
134*bbb1b6f9SApple OSS Distributions
135*bbb1b6f9SApple OSS Distributions IORLOG("\t\t bucket_base_width: %u | log_scale: %u | buckets: %u",
136*bbb1b6f9SApple OSS Distributions _histogramSegmentsConfig[cnt].base_bucket_width,
137*bbb1b6f9SApple OSS Distributions _histogramSegmentsConfig[cnt].scale_flag,
138*bbb1b6f9SApple OSS Distributions _histogramSegmentsConfig[cnt].segment_bucket_count);
139*bbb1b6f9SApple OSS Distributions
140*bbb1b6f9SApple OSS Distributions if (_histogramSegmentsConfig[cnt].scale_flag > 1
141*bbb1b6f9SApple OSS Distributions || _histogramSegmentsConfig[cnt].base_bucket_width == 0) {
142*bbb1b6f9SApple OSS Distributions result = false;
143*bbb1b6f9SApple OSS Distributions goto finish;
144*bbb1b6f9SApple OSS Distributions }
145*bbb1b6f9SApple OSS Distributions }
146*bbb1b6f9SApple OSS Distributions
147*bbb1b6f9SApple OSS Distributions // Update the channel type with discovered dimension
148*bbb1b6f9SApple OSS Distributions _channelType.nelements = _channelDimension;
149*bbb1b6f9SApple OSS Distributions
150*bbb1b6f9SApple OSS Distributions IORLOG("%s - %u channel(s) of dimension %u",
151*bbb1b6f9SApple OSS Distributions __func__, _nChannels, _channelDimension);
152*bbb1b6f9SApple OSS Distributions
153*bbb1b6f9SApple OSS Distributions IORLOG("%s %d segments for a total dimension of %d elements",
154*bbb1b6f9SApple OSS Distributions __func__, _nChannels, _nElements);
155*bbb1b6f9SApple OSS Distributions
156*bbb1b6f9SApple OSS Distributions // Allocate memory for the array of report elements
157*bbb1b6f9SApple OSS Distributions PREFL_MEMOP_FAIL(_nElements, IOReportElement);
158*bbb1b6f9SApple OSS Distributions elementsSize = (size_t)_nElements * sizeof(IOReportElement);
159*bbb1b6f9SApple OSS Distributions _elements = (IOReportElement *)IOMallocZeroData(elementsSize);
160*bbb1b6f9SApple OSS Distributions if (!_elements) {
161*bbb1b6f9SApple OSS Distributions goto finish;
162*bbb1b6f9SApple OSS Distributions }
163*bbb1b6f9SApple OSS Distributions
164*bbb1b6f9SApple OSS Distributions // Allocate memory for the array of element watch count
165*bbb1b6f9SApple OSS Distributions PREFL_MEMOP_FAIL(_nElements, int);
166*bbb1b6f9SApple OSS Distributions eCountsSize = (size_t)_nChannels * sizeof(int);
167*bbb1b6f9SApple OSS Distributions _enableCounts = (int *)IOMallocZeroData(eCountsSize);
168*bbb1b6f9SApple OSS Distributions if (!_enableCounts) {
169*bbb1b6f9SApple OSS Distributions goto finish;
170*bbb1b6f9SApple OSS Distributions }
171*bbb1b6f9SApple OSS Distributions
172*bbb1b6f9SApple OSS Distributions lockReporter();
173*bbb1b6f9SApple OSS Distributions for (cnt2 = 0; cnt2 < _channelDimension; cnt2++) {
174*bbb1b6f9SApple OSS Distributions IOHistogramReportValues hist_values;
175*bbb1b6f9SApple OSS Distributions if (copyElementValues(cnt2, (IOReportElementValues*)&hist_values)) {
176*bbb1b6f9SApple OSS Distributions goto finish;
177*bbb1b6f9SApple OSS Distributions }
178*bbb1b6f9SApple OSS Distributions hist_values.bucket_min = kIOReportInvalidIntValue;
179*bbb1b6f9SApple OSS Distributions hist_values.bucket_max = kIOReportInvalidIntValue;
180*bbb1b6f9SApple OSS Distributions hist_values.bucket_sum = kIOReportInvalidIntValue;
181*bbb1b6f9SApple OSS Distributions if (setElementValues(cnt2, (IOReportElementValues*)&hist_values)) {
182*bbb1b6f9SApple OSS Distributions goto finish;
183*bbb1b6f9SApple OSS Distributions }
184*bbb1b6f9SApple OSS Distributions
185*bbb1b6f9SApple OSS Distributions // Setup IOReporter's channel IDs
186*bbb1b6f9SApple OSS Distributions _elements[cnt2].channel_id = channelID;
187*bbb1b6f9SApple OSS Distributions
188*bbb1b6f9SApple OSS Distributions // Setup IOReporter's reporting provider service
189*bbb1b6f9SApple OSS Distributions _elements[cnt2].provider_id = _driver_id;
190*bbb1b6f9SApple OSS Distributions
191*bbb1b6f9SApple OSS Distributions // Setup IOReporter's channel type
192*bbb1b6f9SApple OSS Distributions _elements[cnt2].channel_type = _channelType;
193*bbb1b6f9SApple OSS Distributions _elements[cnt2].channel_type.element_idx = ((int16_t) cnt2);
194*bbb1b6f9SApple OSS Distributions
195*bbb1b6f9SApple OSS Distributions //IOREPORTER_DEBUG_ELEMENT(cnt2);
196*bbb1b6f9SApple OSS Distributions }
197*bbb1b6f9SApple OSS Distributions unlockReporter();
198*bbb1b6f9SApple OSS Distributions
199*bbb1b6f9SApple OSS Distributions // Allocate memory for the bucket upper bounds
200*bbb1b6f9SApple OSS Distributions PREFL_MEMOP_FAIL(_nElements, uint64_t);
201*bbb1b6f9SApple OSS Distributions boundsSize = (size_t)_nElements * sizeof(uint64_t);
202*bbb1b6f9SApple OSS Distributions _bucketBounds = (int64_t*)IOMallocZeroData(boundsSize);
203*bbb1b6f9SApple OSS Distributions if (!_bucketBounds) {
204*bbb1b6f9SApple OSS Distributions goto finish;
205*bbb1b6f9SApple OSS Distributions }
206*bbb1b6f9SApple OSS Distributions _bucketCount = _nElements;
207*bbb1b6f9SApple OSS Distributions
208*bbb1b6f9SApple OSS Distributions for (cnt = 0; cnt < _segmentCount; cnt++) {
209*bbb1b6f9SApple OSS Distributions if (_histogramSegmentsConfig[cnt].segment_bucket_count > INT_MAX
210*bbb1b6f9SApple OSS Distributions || _histogramSegmentsConfig[cnt].base_bucket_width > INT_MAX) {
211*bbb1b6f9SApple OSS Distributions goto finish;
212*bbb1b6f9SApple OSS Distributions }
213*bbb1b6f9SApple OSS Distributions for (cnt2 = 0; cnt2 < (int)_histogramSegmentsConfig[cnt].segment_bucket_count; cnt2++) {
214*bbb1b6f9SApple OSS Distributions if (cnt3 >= _nElements) {
215*bbb1b6f9SApple OSS Distributions IORLOG("ERROR: _bucketBounds init");
216*bbb1b6f9SApple OSS Distributions result = false;
217*bbb1b6f9SApple OSS Distributions goto finish;
218*bbb1b6f9SApple OSS Distributions }
219*bbb1b6f9SApple OSS Distributions
220*bbb1b6f9SApple OSS Distributions if (_histogramSegmentsConfig[cnt].scale_flag) {
221*bbb1b6f9SApple OSS Distributions int64_t power = 1;
222*bbb1b6f9SApple OSS Distributions int exponent = cnt2 + 1;
223*bbb1b6f9SApple OSS Distributions while (exponent) {
224*bbb1b6f9SApple OSS Distributions power *= _histogramSegmentsConfig[cnt].base_bucket_width;
225*bbb1b6f9SApple OSS Distributions exponent--;
226*bbb1b6f9SApple OSS Distributions }
227*bbb1b6f9SApple OSS Distributions bucketBound = power;
228*bbb1b6f9SApple OSS Distributions } else {
229*bbb1b6f9SApple OSS Distributions bucketBound = _histogramSegmentsConfig[cnt].base_bucket_width *
230*bbb1b6f9SApple OSS Distributions ((unsigned)cnt2 + 1);
231*bbb1b6f9SApple OSS Distributions }
232*bbb1b6f9SApple OSS Distributions
233*bbb1b6f9SApple OSS Distributions if (previousBucketBound >= bucketBound) {
234*bbb1b6f9SApple OSS Distributions IORLOG("Histogram ERROR: bucket bound does not increase linearly (segment %u / bucket # %u)",
235*bbb1b6f9SApple OSS Distributions cnt, cnt2);
236*bbb1b6f9SApple OSS Distributions result = false;
237*bbb1b6f9SApple OSS Distributions goto finish;
238*bbb1b6f9SApple OSS Distributions }
239*bbb1b6f9SApple OSS Distributions
240*bbb1b6f9SApple OSS Distributions _bucketBounds[cnt3] = bucketBound;
241*bbb1b6f9SApple OSS Distributions // IORLOG("_bucketBounds[%u] = %llu", cnt3, bucketBound);
242*bbb1b6f9SApple OSS Distributions previousBucketBound = _bucketBounds[cnt3];
243*bbb1b6f9SApple OSS Distributions cnt3++;
244*bbb1b6f9SApple OSS Distributions }
245*bbb1b6f9SApple OSS Distributions }
246*bbb1b6f9SApple OSS Distributions
247*bbb1b6f9SApple OSS Distributions // success
248*bbb1b6f9SApple OSS Distributions result = true;
249*bbb1b6f9SApple OSS Distributions
250*bbb1b6f9SApple OSS Distributions finish:
251*bbb1b6f9SApple OSS Distributions return result;
252*bbb1b6f9SApple OSS Distributions }
253*bbb1b6f9SApple OSS Distributions
254*bbb1b6f9SApple OSS Distributions
255*bbb1b6f9SApple OSS Distributions void
free(void)256*bbb1b6f9SApple OSS Distributions IOHistogramReporter::free(void)
257*bbb1b6f9SApple OSS Distributions {
258*bbb1b6f9SApple OSS Distributions if (_bucketBounds) {
259*bbb1b6f9SApple OSS Distributions PREFL_MEMOP_PANIC(_nElements, int64_t);
260*bbb1b6f9SApple OSS Distributions IOFreeData(_bucketBounds, (size_t)_nElements * sizeof(int64_t));
261*bbb1b6f9SApple OSS Distributions }
262*bbb1b6f9SApple OSS Distributions if (_histogramSegmentsConfig) {
263*bbb1b6f9SApple OSS Distributions PREFL_MEMOP_PANIC(_segmentCount, IOHistogramSegmentConfig);
264*bbb1b6f9SApple OSS Distributions IOFreeData(_histogramSegmentsConfig,
265*bbb1b6f9SApple OSS Distributions (size_t)_segmentCount * sizeof(IOHistogramSegmentConfig));
266*bbb1b6f9SApple OSS Distributions }
267*bbb1b6f9SApple OSS Distributions
268*bbb1b6f9SApple OSS Distributions super::free();
269*bbb1b6f9SApple OSS Distributions }
270*bbb1b6f9SApple OSS Distributions
271*bbb1b6f9SApple OSS Distributions
272*bbb1b6f9SApple OSS Distributions OSSharedPtr<IOReportLegendEntry>
handleCreateLegend(void)273*bbb1b6f9SApple OSS Distributions IOHistogramReporter::handleCreateLegend(void)
274*bbb1b6f9SApple OSS Distributions {
275*bbb1b6f9SApple OSS Distributions OSSharedPtr<IOReportLegendEntry> legendEntry;
276*bbb1b6f9SApple OSS Distributions OSSharedPtr<OSData> tmpConfigData;
277*bbb1b6f9SApple OSS Distributions OSDictionary *tmpDict; // no refcount
278*bbb1b6f9SApple OSS Distributions
279*bbb1b6f9SApple OSS Distributions legendEntry = super::handleCreateLegend();
280*bbb1b6f9SApple OSS Distributions if (!legendEntry) {
281*bbb1b6f9SApple OSS Distributions return nullptr;
282*bbb1b6f9SApple OSS Distributions }
283*bbb1b6f9SApple OSS Distributions
284*bbb1b6f9SApple OSS Distributions PREFL_MEMOP_PANIC(_segmentCount, IOHistogramSegmentConfig);
285*bbb1b6f9SApple OSS Distributions tmpConfigData = OSData::withBytes(_histogramSegmentsConfig,
286*bbb1b6f9SApple OSS Distributions (unsigned)_segmentCount *
287*bbb1b6f9SApple OSS Distributions sizeof(IOHistogramSegmentConfig));
288*bbb1b6f9SApple OSS Distributions if (!tmpConfigData) {
289*bbb1b6f9SApple OSS Distributions return nullptr;
290*bbb1b6f9SApple OSS Distributions }
291*bbb1b6f9SApple OSS Distributions
292*bbb1b6f9SApple OSS Distributions tmpDict = OSDynamicCast(OSDictionary,
293*bbb1b6f9SApple OSS Distributions legendEntry->getObject(kIOReportLegendInfoKey));
294*bbb1b6f9SApple OSS Distributions if (!tmpDict) {
295*bbb1b6f9SApple OSS Distributions return nullptr;
296*bbb1b6f9SApple OSS Distributions }
297*bbb1b6f9SApple OSS Distributions
298*bbb1b6f9SApple OSS Distributions tmpDict->setObject(kIOReportLegendConfigKey, tmpConfigData.get());
299*bbb1b6f9SApple OSS Distributions
300*bbb1b6f9SApple OSS Distributions return legendEntry;
301*bbb1b6f9SApple OSS Distributions }
302*bbb1b6f9SApple OSS Distributions
303*bbb1b6f9SApple OSS Distributions IOReturn
overrideBucketValues(unsigned int index,uint64_t bucket_hits,int64_t bucket_min,int64_t bucket_max,int64_t bucket_sum)304*bbb1b6f9SApple OSS Distributions IOHistogramReporter::overrideBucketValues(unsigned int index,
305*bbb1b6f9SApple OSS Distributions uint64_t bucket_hits,
306*bbb1b6f9SApple OSS Distributions int64_t bucket_min,
307*bbb1b6f9SApple OSS Distributions int64_t bucket_max,
308*bbb1b6f9SApple OSS Distributions int64_t bucket_sum)
309*bbb1b6f9SApple OSS Distributions {
310*bbb1b6f9SApple OSS Distributions IOReturn result;
311*bbb1b6f9SApple OSS Distributions IOHistogramReportValues bucket;
312*bbb1b6f9SApple OSS Distributions lockReporter();
313*bbb1b6f9SApple OSS Distributions
314*bbb1b6f9SApple OSS Distributions if (index >= (unsigned int)_bucketCount) {
315*bbb1b6f9SApple OSS Distributions result = kIOReturnBadArgument;
316*bbb1b6f9SApple OSS Distributions goto finish;
317*bbb1b6f9SApple OSS Distributions }
318*bbb1b6f9SApple OSS Distributions
319*bbb1b6f9SApple OSS Distributions bucket.bucket_hits = bucket_hits;
320*bbb1b6f9SApple OSS Distributions bucket.bucket_min = bucket_min;
321*bbb1b6f9SApple OSS Distributions bucket.bucket_max = bucket_max;
322*bbb1b6f9SApple OSS Distributions bucket.bucket_sum = bucket_sum;
323*bbb1b6f9SApple OSS Distributions
324*bbb1b6f9SApple OSS Distributions result = setElementValues(index, (IOReportElementValues *)&bucket);
325*bbb1b6f9SApple OSS Distributions finish:
326*bbb1b6f9SApple OSS Distributions unlockReporter();
327*bbb1b6f9SApple OSS Distributions return result;
328*bbb1b6f9SApple OSS Distributions }
329*bbb1b6f9SApple OSS Distributions
330*bbb1b6f9SApple OSS Distributions int
tallyValue(int64_t value)331*bbb1b6f9SApple OSS Distributions IOHistogramReporter::tallyValue(int64_t value)
332*bbb1b6f9SApple OSS Distributions {
333*bbb1b6f9SApple OSS Distributions int result = -1;
334*bbb1b6f9SApple OSS Distributions int cnt = 0, element_index = 0;
335*bbb1b6f9SApple OSS Distributions int64_t sum = 0;
336*bbb1b6f9SApple OSS Distributions IOHistogramReportValues hist_values;
337*bbb1b6f9SApple OSS Distributions
338*bbb1b6f9SApple OSS Distributions lockReporter();
339*bbb1b6f9SApple OSS Distributions
340*bbb1b6f9SApple OSS Distributions // Iterate over _bucketCount minus one to make last bucket of infinite width
341*bbb1b6f9SApple OSS Distributions for (cnt = 0; cnt < _bucketCount - 1; cnt++) {
342*bbb1b6f9SApple OSS Distributions if (value <= _bucketBounds[cnt]) {
343*bbb1b6f9SApple OSS Distributions break;
344*bbb1b6f9SApple OSS Distributions }
345*bbb1b6f9SApple OSS Distributions }
346*bbb1b6f9SApple OSS Distributions
347*bbb1b6f9SApple OSS Distributions element_index = cnt;
348*bbb1b6f9SApple OSS Distributions
349*bbb1b6f9SApple OSS Distributions if (copyElementValues(element_index, (IOReportElementValues *)&hist_values) != kIOReturnSuccess) {
350*bbb1b6f9SApple OSS Distributions goto finish;
351*bbb1b6f9SApple OSS Distributions }
352*bbb1b6f9SApple OSS Distributions
353*bbb1b6f9SApple OSS Distributions // init stats on first hit
354*bbb1b6f9SApple OSS Distributions if (hist_values.bucket_hits == 0) {
355*bbb1b6f9SApple OSS Distributions hist_values.bucket_min = hist_values.bucket_max = value;
356*bbb1b6f9SApple OSS Distributions hist_values.bucket_sum = 0; // += is below
357*bbb1b6f9SApple OSS Distributions }
358*bbb1b6f9SApple OSS Distributions
359*bbb1b6f9SApple OSS Distributions // update all values
360*bbb1b6f9SApple OSS Distributions if (value < hist_values.bucket_min) {
361*bbb1b6f9SApple OSS Distributions hist_values.bucket_min = value;
362*bbb1b6f9SApple OSS Distributions } else if (value > hist_values.bucket_max) {
363*bbb1b6f9SApple OSS Distributions hist_values.bucket_max = value;
364*bbb1b6f9SApple OSS Distributions }
365*bbb1b6f9SApple OSS Distributions if (os_add_overflow(hist_values.bucket_sum, value, &sum)) {
366*bbb1b6f9SApple OSS Distributions hist_values.bucket_sum = INT64_MAX;
367*bbb1b6f9SApple OSS Distributions } else {
368*bbb1b6f9SApple OSS Distributions hist_values.bucket_sum = sum;
369*bbb1b6f9SApple OSS Distributions }
370*bbb1b6f9SApple OSS Distributions hist_values.bucket_hits++;
371*bbb1b6f9SApple OSS Distributions
372*bbb1b6f9SApple OSS Distributions if (setElementValues(element_index, (IOReportElementValues *)&hist_values)
373*bbb1b6f9SApple OSS Distributions != kIOReturnSuccess) {
374*bbb1b6f9SApple OSS Distributions goto finish;
375*bbb1b6f9SApple OSS Distributions }
376*bbb1b6f9SApple OSS Distributions
377*bbb1b6f9SApple OSS Distributions // success!
378*bbb1b6f9SApple OSS Distributions result = element_index;
379*bbb1b6f9SApple OSS Distributions
380*bbb1b6f9SApple OSS Distributions finish:
381*bbb1b6f9SApple OSS Distributions unlockReporter();
382*bbb1b6f9SApple OSS Distributions return result;
383*bbb1b6f9SApple OSS Distributions }
384*bbb1b6f9SApple OSS Distributions
385*bbb1b6f9SApple OSS Distributions /* static */ OSPtr<IOReportLegendEntry>
createLegend(uint64_t channelID,const char * channelName,int segmentCount,IOHistogramSegmentConfig * config,IOReportCategories categories,IOReportUnit unit)386*bbb1b6f9SApple OSS Distributions IOHistogramReporter::createLegend(uint64_t channelID,
387*bbb1b6f9SApple OSS Distributions const char *channelName,
388*bbb1b6f9SApple OSS Distributions int segmentCount,
389*bbb1b6f9SApple OSS Distributions IOHistogramSegmentConfig *config,
390*bbb1b6f9SApple OSS Distributions IOReportCategories categories,
391*bbb1b6f9SApple OSS Distributions IOReportUnit unit)
392*bbb1b6f9SApple OSS Distributions {
393*bbb1b6f9SApple OSS Distributions OSSharedPtr<IOReportLegendEntry> legendEntry;
394*bbb1b6f9SApple OSS Distributions OSSharedPtr<OSData> tmpConfigData;
395*bbb1b6f9SApple OSS Distributions OSDictionary *tmpDict; // no refcount
396*bbb1b6f9SApple OSS Distributions int cnt;
397*bbb1b6f9SApple OSS Distributions
398*bbb1b6f9SApple OSS Distributions IOReportChannelType channelType = {
399*bbb1b6f9SApple OSS Distributions .categories = categories,
400*bbb1b6f9SApple OSS Distributions .report_format = kIOReportFormatHistogram,
401*bbb1b6f9SApple OSS Distributions .nelements = 0,
402*bbb1b6f9SApple OSS Distributions .element_idx = 0
403*bbb1b6f9SApple OSS Distributions };
404*bbb1b6f9SApple OSS Distributions
405*bbb1b6f9SApple OSS Distributions for (cnt = 0; cnt < segmentCount; cnt++) {
406*bbb1b6f9SApple OSS Distributions channelType.nelements += config[cnt].segment_bucket_count;
407*bbb1b6f9SApple OSS Distributions }
408*bbb1b6f9SApple OSS Distributions
409*bbb1b6f9SApple OSS Distributions legendEntry = IOReporter::legendWith(&channelID, &channelName, 1, channelType, unit);
410*bbb1b6f9SApple OSS Distributions if (!legendEntry) {
411*bbb1b6f9SApple OSS Distributions return nullptr;
412*bbb1b6f9SApple OSS Distributions }
413*bbb1b6f9SApple OSS Distributions
414*bbb1b6f9SApple OSS Distributions PREFL_MEMOP_PANIC(segmentCount, IOHistogramSegmentConfig);
415*bbb1b6f9SApple OSS Distributions tmpConfigData = OSData::withBytes(config,
416*bbb1b6f9SApple OSS Distributions (unsigned)segmentCount *
417*bbb1b6f9SApple OSS Distributions sizeof(IOHistogramSegmentConfig));
418*bbb1b6f9SApple OSS Distributions if (!tmpConfigData) {
419*bbb1b6f9SApple OSS Distributions return nullptr;
420*bbb1b6f9SApple OSS Distributions }
421*bbb1b6f9SApple OSS Distributions
422*bbb1b6f9SApple OSS Distributions tmpDict = OSDynamicCast(OSDictionary,
423*bbb1b6f9SApple OSS Distributions legendEntry->getObject(kIOReportLegendInfoKey));
424*bbb1b6f9SApple OSS Distributions if (!tmpDict) {
425*bbb1b6f9SApple OSS Distributions return nullptr;
426*bbb1b6f9SApple OSS Distributions }
427*bbb1b6f9SApple OSS Distributions
428*bbb1b6f9SApple OSS Distributions tmpDict->setObject(kIOReportLegendConfigKey, tmpConfigData.get());
429*bbb1b6f9SApple OSS Distributions
430*bbb1b6f9SApple OSS Distributions return legendEntry;
431*bbb1b6f9SApple OSS Distributions }
432