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