- IOReturn res = kIOReturnError;
-
- IOStateReportValues curr_state_values, new_state_values;
- int curr_state_index = 0;
- int curr_element_index, new_element_index;
- uint64_t last_ch_update_time = 0;
- uint64_t recordTime = mach_absolute_time();
-
- IOREPORTER_CHECK_LOCK();
-
- if (channel_index < 0 || channel_index >= _nChannels) {
- res = kIOReturnBadArgument; goto finish;
- }
-
- // if no timestamp provided, last_intransition = time of recording (now)
- if (last_intransition == 0) {
- last_intransition = recordTime;
- }
-
- // First update target state if different than the current state
- // _currentStates[] initialized to -1 to detect first state transition
- curr_state_index = _currentStates[channel_index];
- if (new_state_index != curr_state_index) {
- // fetch element data
- if (channel_index < 0 || channel_index > (_nElements-new_state_index)
- / _channelDimension) {
- res = kIOReturnOverrun; goto finish;
- }
- new_element_index = channel_index*_channelDimension + new_state_index;
- if (copyElementValues(new_element_index,
- (IOReportElementValues *)&new_state_values)) {
- res = kIOReturnBadArgument;
- goto finish;
- }
-
- // Update new state's transition info
- new_state_values.intransitions += 1;
- new_state_values.last_intransition = last_intransition;
-
- // and store the values
- res = setElementValues(new_element_index,
- (IOReportElementValues *)&new_state_values,
- recordTime);
-
- if (res != kIOReturnSuccess) {
- goto finish;
- }
-
- _currentStates[channel_index] = new_state_index;
- }
-
- /* Now update time spent in any previous state
- If new_state_index = curr_state_index, this updates time in the
- current state. If this is the channel's first state transition,
- the last update time will be zero.
-
- Note: While setState() should never be called on a channel being
- updated with increment/overrideChannelState(), that's another way
- that the last update time might not exist. Regardless, if there
- is no basis for determining time spent in previous state, there's
- nothing to update!
- */
- last_ch_update_time = _lastUpdateTimes[channel_index];
- if (last_ch_update_time != 0) {
- if (channel_index < 0 || channel_index > (_nElements-curr_state_index)
- / _channelDimension) {
- res = kIOReturnOverrun; goto finish;
- }
- curr_element_index = channel_index*_channelDimension + curr_state_index;
- if (copyElementValues(curr_element_index,
- (IOReportElementValues *)&curr_state_values)) {
- res = kIOReturnBadArgument;
- goto finish;
- }
- // compute the time spent in previous state, unless provided
- if (prev_state_residency == 0) {
- prev_state_residency = last_intransition - last_ch_update_time;
- }
-
- curr_state_values.upticks += prev_state_residency;
-
- res = setElementValues(curr_element_index,
- (IOReportElementValues*)&curr_state_values,
- recordTime);
-
- if (res != kIOReturnSuccess) {
- goto finish;
- }
- }
-
- // record basis for next "time in prior state" calculation
- // (also arms a panic in override/incrementChannelState())
- _lastUpdateTimes[channel_index] = last_intransition;
-
+ IOReturn res = kIOReturnError;
+
+ IOStateReportValues curr_state_values, new_state_values;
+ int curr_state_index = 0;
+ int curr_element_index, new_element_index;
+ uint64_t last_ch_update_time = 0;
+ uint64_t recordTime = mach_absolute_time();
+
+ IOREPORTER_CHECK_LOCK();
+
+ if (channel_index < 0 || channel_index >= _nChannels) {
+ res = kIOReturnBadArgument; goto finish;
+ }
+
+ // if no timestamp provided, last_intransition = time of recording (now)
+ if (last_intransition == 0) {
+ last_intransition = recordTime;
+ }
+
+ // First update target state if different than the current state
+ // _currentStates[] initialized to -1 to detect first state transition
+ curr_state_index = _currentStates[channel_index];
+ if (new_state_index != curr_state_index) {
+ // fetch element data
+ if (channel_index < 0 || channel_index > (_nElements - new_state_index)
+ / _channelDimension) {
+ res = kIOReturnOverrun; goto finish;
+ }
+ new_element_index = channel_index * _channelDimension + new_state_index;
+ if (copyElementValues(new_element_index,
+ (IOReportElementValues *)&new_state_values)) {
+ res = kIOReturnBadArgument;
+ goto finish;
+ }
+
+ // Update new state's transition info
+ new_state_values.intransitions += 1;
+ new_state_values.last_intransition = last_intransition;
+
+ // and store the values
+ res = setElementValues(new_element_index,
+ (IOReportElementValues *)&new_state_values,
+ recordTime);
+
+ if (res != kIOReturnSuccess) {
+ goto finish;
+ }
+
+ _currentStates[channel_index] = new_state_index;
+ }
+
+ /* Now update time spent in any previous state
+ * If new_state_index = curr_state_index, this updates time in the
+ * current state. If this is the channel's first state transition,
+ * the last update time will be zero.
+ *
+ * Note: While setState() should never be called on a channel being
+ * updated with increment/overrideChannelState(), that's another way
+ * that the last update time might not exist. Regardless, if there
+ * is no basis for determining time spent in previous state, there's
+ * nothing to update!
+ */
+ last_ch_update_time = _lastUpdateTimes[channel_index];
+ if (last_ch_update_time != 0) {
+ if (channel_index < 0 || channel_index > (_nElements - curr_state_index)
+ / _channelDimension) {
+ res = kIOReturnOverrun; goto finish;
+ }
+ curr_element_index = channel_index * _channelDimension + curr_state_index;
+ if (copyElementValues(curr_element_index,
+ (IOReportElementValues *)&curr_state_values)) {
+ res = kIOReturnBadArgument;
+ goto finish;
+ }
+ // compute the time spent in previous state, unless provided
+ if (prev_state_residency == 0) {
+ prev_state_residency = last_intransition - last_ch_update_time;
+ }
+
+ curr_state_values.upticks += prev_state_residency;
+
+ res = setElementValues(curr_element_index,
+ (IOReportElementValues*)&curr_state_values,
+ recordTime);
+
+ if (res != kIOReturnSuccess) {
+ goto finish;
+ }
+ }
+
+ // record basis for next "time in prior state" calculation
+ // (also arms a panic in override/incrementChannelState())
+ _lastUpdateTimes[channel_index] = last_intransition;
+