+IOReturn IOService::addInterruptStatistics(IOInterruptAccountingData * statistics, int source)
+{
+ IOReportLegend * legend = NULL;
+ IOInterruptAccountingData * oldValue = NULL;
+ IOInterruptAccountingReporter * newArray = NULL;
+ char subgroupName[64];
+ int newArraySize = 0;
+ int i = 0;
+
+ if (source < 0) {
+ return kIOReturnBadArgument;
+ }
+
+ /*
+ * We support statistics on a maximum of 256 interrupts per nub; if a nub
+ * has more than 256 interrupt specifiers associated with it, and tries
+ * to register a high interrupt index with interrupt accounting, panic.
+ * Having more than 256 interrupts associated with a single nub is
+ * probably a sign that something fishy is going on.
+ */
+ if (source > IA_INDEX_MAX) {
+ panic("addInterruptStatistics called for an excessively large index (%d)", source);
+ }
+
+ /*
+ * TODO: This is ugly (wrapping a lock around an allocation). I'm only
+ * leaving it as is because the likelihood of contention where we are
+ * actually growing the array is minimal (we would realistically need
+ * to be starting a driver for the first time, with an IOReporting
+ * client already in place). Nonetheless, cleanup that can be done
+ * to adhere to best practices; it'll make the code more complicated,
+ * unfortunately.
+ */
+ IOLockLock(reserved->interruptStatisticsLock);
+
+ /*
+ * Lazily allocate the statistics array.
+ */
+ if (!reserved->interruptStatisticsArray) {
+ reserved->interruptStatisticsArray = IONew(IOInterruptAccountingReporter, 1);
+ assert(reserved->interruptStatisticsArray);
+ reserved->interruptStatisticsArrayCount = 1;
+ bzero(reserved->interruptStatisticsArray, sizeof(*reserved->interruptStatisticsArray));
+ }
+
+ if (source >= reserved->interruptStatisticsArrayCount) {
+ /*
+ * We're still within the range of supported indices, but we are out
+ * of space in the current array. Do a nasty realloc (because
+ * IORealloc isn't a thing) here. We'll double the size with each
+ * reallocation.
+ *
+ * Yes, the "next power of 2" could be more efficient; but this will
+ * be invoked incredibly rarely. Who cares.
+ */
+ newArraySize = (reserved->interruptStatisticsArrayCount << 1);
+
+ while (newArraySize <= source)
+ newArraySize = (newArraySize << 1);
+ newArray = IONew(IOInterruptAccountingReporter, newArraySize);
+
+ assert(newArray);
+
+ /*
+ * TODO: This even zeroes the memory it is about to overwrite.
+ * Shameful; fix it. Not particularly high impact, however.
+ */
+ bzero(newArray, newArraySize * sizeof(*newArray));
+ memcpy(newArray, reserved->interruptStatisticsArray, reserved->interruptStatisticsArrayCount * sizeof(*newArray));
+ IODelete(reserved->interruptStatisticsArray, IOInterruptAccountingReporter, reserved->interruptStatisticsArrayCount);
+ reserved->interruptStatisticsArray = newArray;
+ reserved->interruptStatisticsArrayCount = newArraySize;
+ }
+
+ if (!reserved->interruptStatisticsArray[source].reporter) {
+ /*
+ * We don't have a reporter associated with this index yet, so we
+ * need to create one.
+ */
+ /*
+ * TODO: Some statistics do in fact have common units (time); should this be
+ * split into separate reporters to communicate this?
+ */
+ reserved->interruptStatisticsArray[source].reporter = IOSimpleReporter::with(this, kIOReportCategoryPower, kIOReportUnitNone);
+
+ /*
+ * Each statistic is given an identifier based on the interrupt index (which
+ * should be unique relative to any single nub) and the statistic involved.
+ * We should now have a sane (small and positive) index, so start
+ * constructing the channels for statistics.
+ */
+ for (i = 0; i < IA_NUM_INTERRUPT_ACCOUNTING_STATISTICS; i++) {
+ /*
+ * TODO: Currently, this does not add channels for disabled statistics.
+ * Will this be confusing for clients? If so, we should just add the
+ * channels; we can avoid updating the channels even if they exist.
+ */
+ if (IA_GET_STATISTIC_ENABLED(i))
+ reserved->interruptStatisticsArray[source].reporter->addChannel(IA_GET_CHANNEL_ID(source, i), kInterruptAccountingStatisticNameArray[i]);
+ }
+
+ /*
+ * We now need to add the legend for this reporter to the registry.
+ */
+ OSObject * prop = copyProperty(kIOReportLegendKey);
+ legend = IOReportLegend::with(OSDynamicCast(OSArray, prop));
+ OSSafeReleaseNULL(prop);
+
+ /*
+ * Note that while we compose the subgroup name, we do not need to
+ * manage its lifecycle (the reporter will handle this).
+ */
+ snprintf(subgroupName, sizeof(subgroupName), "%s %d", getName(), source);
+ subgroupName[sizeof(subgroupName) - 1] = 0;
+ legend->addReporterLegend(reserved->interruptStatisticsArray[source].reporter, kInterruptAccountingGroupName, subgroupName);
+ setProperty(kIOReportLegendKey, legend->getLegend());
+ legend->release();
+
+ /*
+ * TODO: Is this a good idea? Probably not; my assumption is it opts
+ * all entities who register interrupts into public disclosure of all
+ * IOReporting channels. Unfortunately, this appears to be as fine
+ * grain as it gets.
+ */
+ setProperty(kIOReportLegendPublicKey, true);
+ }
+
+ /*
+ * Don't stomp existing entries. If we are about to, panic; this
+ * probably means we failed to tear down our old interrupt source
+ * correctly.
+ */
+ oldValue = reserved->interruptStatisticsArray[source].statistics;
+
+ if (oldValue) {
+ panic("addInterruptStatistics call for index %d would have clobbered existing statistics", source);
+ }
+
+ reserved->interruptStatisticsArray[source].statistics = statistics;
+
+ /*
+ * Inherit the reporter values for each statistic. The target may
+ * be torn down as part of the runtime of the service (especially
+ * for sleep/wake), so we inherit in order to avoid having values
+ * reset for no apparent reason. Our statistics are ultimately
+ * tied to the index and the sevice, not to an individual target,
+ * so we should maintain them accordingly.
+ */
+ interruptAccountingDataInheritChannels(reserved->interruptStatisticsArray[source].statistics, reserved->interruptStatisticsArray[source].reporter);
+
+ IOLockUnlock(reserved->interruptStatisticsLock);
+
+ return kIOReturnSuccess;
+}
+
+IOReturn IOService::removeInterruptStatistics(int source)
+{
+ IOInterruptAccountingData * value = NULL;
+
+ if (source < 0) {
+ return kIOReturnBadArgument;
+ }
+
+ IOLockLock(reserved->interruptStatisticsLock);
+
+ /*
+ * We dynamically grow the statistics array, so an excessively
+ * large index value has NEVER been registered. This either
+ * means our cap on the array size is too small (unlikely), or
+ * that we have been passed a corrupt index (this must be passed
+ * the plain index into the interrupt specifier list).
+ */
+ if (source >= reserved->interruptStatisticsArrayCount) {
+ panic("removeInterruptStatistics called for index %d, which was never registered", source);
+ }
+
+ assert(reserved->interruptStatisticsArray);
+
+ /*
+ * If there is no existing entry, we are most likely trying to
+ * free an interrupt owner twice, or we have corrupted the
+ * index value.
+ */
+ value = reserved->interruptStatisticsArray[source].statistics;
+
+ if (!value) {
+ panic("removeInterruptStatistics called for empty index %d", source);
+ }
+
+ /*
+ * We update the statistics, so that any delta with the reporter
+ * state is not lost.
+ */
+ interruptAccountingDataUpdateChannels(reserved->interruptStatisticsArray[source].statistics, reserved->interruptStatisticsArray[source].reporter);
+ reserved->interruptStatisticsArray[source].statistics = NULL;
+ IOLockUnlock(reserved->interruptStatisticsLock);
+
+ return kIOReturnSuccess;
+}
+