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