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