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