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