async fn handle_set_max_power_consumption(
        &self,
        max_power: &Watts,
    ) -> Result<MessageReturn, Error> {
        // The operation completion rate over the last sample interval is
        //     num_operations / sample_interval,
        // where
        //     num_operations = last_load * last_frequency * sample_interval.
        // Hence,
        //     last_operation_rate = last_load * last_frequency.
        let last_operation_rate = {
            let last_load = self.get_load().await? as f64;
            let last_frequency = self.params.p_states[self.current_p_state_index].frequency;
            last_frequency.mul_scalar(last_load)
        };

        // Lazily get the number of CPUs from the stats handler.
        let num_cpus = {
            if self.num_cpus.get().is_none() {
                self.num_cpus.set(Some(self.get_num_cpus().await?));
            }
            self.num_cpus.get().unwrap()
        };

        // If no P-states meet the selection criterion, use the lowest-power state.
        let mut p_state_index = self.params.p_states.len() - 1;

        for (i, state) in self.params.p_states.iter().enumerate() {
            // We estimate that the operation rate over the next interval will be the min of
            // the last operation rate and the frequency of the P-state under consideration.
            //
            // Note that we don't currently account for a rise in frequency allowing for a possible
            // increase in the operation rate.
            let max_operation_rate = state.frequency.mul_scalar(num_cpus as f64);
            let estimated_operation_rate = if max_operation_rate < last_operation_rate {
                max_operation_rate
            } else {
                last_operation_rate
            };

            let estimated_power =
                get_cpu_power(self.params.capacitance, state.voltage, estimated_operation_rate);
            if estimated_power <= *max_power {
                p_state_index = i;
                break;
            }
        }

        self.cpu_device.borrow_mut().set_p_state_index(p_state_index);
        Ok(MessageReturn::SetMaxPowerConsumption)
    }
/// Sets the P-state to the highest-power state with consumption below `max_power`.
///
/// The estimated power consumption depends on the operation completion rate by the CPU.
/// We assume that the rate of operations requested over the next sample interval will be
/// the same as it was over the previous sample interval, up to CPU's max completion rate
/// for a P-state under consideration.
Sets the P-state to the highest-power state with consumption below `max_power`.
The estimated power consumption depends on the operation completion rate by the CPU.
We assume that the rate of operations requested over the next sample interval will be
the same as it was over the previous sample interval, up to CPU's max completion rate
for a P-state under consideration.
Sets the P - state to the highest - power state with consumption below ` max_power ` . The estimated power consumption depends on the operation completion rate by the CPU . We assume that the rate of operations requested over the next sample interval will be the same as it was over the previous sample interval up to CPU ' s max completion rate for a P - state under consideration .
async fn handle_set_max_power_consumption(
        &self,
        max_power: &Watts,
    ) -> Result<MessageReturn, Error> {
        // The operation completion rate over the last sample interval is
        //     num_operations / sample_interval,
        // where
        //     num_operations = last_load * last_frequency * sample_interval.
        // Hence,
        //     last_operation_rate = last_load * last_frequency.
        let last_operation_rate = {
            let last_load = self.get_load().await? as f64;
            let last_frequency = self.params.p_states[self.current_p_state_index].frequency;
            last_frequency.mul_scalar(last_load)
        };
        // Lazily get the number of CPUs from the stats handler.
        let num_cpus = {
            if self.num_cpus.get().is_none() {
                self.num_cpus.set(Some(self.get_num_cpus().await?));
            }
            self.num_cpus.get().unwrap()
        };
        // If no P-states meet the selection criterion, use the lowest-power state.
        let mut p_state_index = self.params.p_states.len() - 1;
        for (i, state) in self.params.p_states.iter().enumerate() {
            // We estimate that the operation rate over the next interval will be the min of
            // the last operation rate and the frequency of the P-state under consideration.
            //
            // Note that we don't currently account for a rise in frequency allowing for a possible
            // increase in the operation rate.
            let max_operation_rate = state.frequency.mul_scalar(num_cpus as f64);
            let estimated_operation_rate = if max_operation_rate < last_operation_rate {
                max_operation_rate
            } else {
                last_operation_rate
            };
            let estimated_power =
                get_cpu_power(self.params.capacitance, state.voltage, estimated_operation_rate);
            if estimated_power <= *max_power {
                p_state_index = i;
                break;
            }
        }
        self.cpu_device.borrow_mut().set_p_state_index(p_state_index);
        Ok(MessageReturn::SetMaxPowerConsumption)
    }
async fn handle_set_max_power_consumption ( & self , max_power : & Watts , ) -> Result < MessageReturn , Error > { // The operation completion rate over the last sample interval is //     num_operations / sample_interval, // where //     num_operations = last_load * last_frequency * sample_interval. // Hence, //     last_operation_rate = last_load * last_frequency. let last_operation_rate = { let last_load = self . get_load ( ) . await ? as f64 ; let last_frequency = self . params . p_states [ self . current_p_state_index ] . frequency ; last_frequency . mul_scalar ( last_load ) } ; // Lazily get the number of CPUs from the stats handler. let num_cpus = { if self . num_cpus . get ( ) . is_none ( ) { self . num_cpus . set ( Some ( self . get_num_cpus ( ) . await ? ) ) ; } self . num_cpus . get ( ) . unwrap ( ) } ; // If no P-states meet the selection criterion, use the lowest-power state. let mut p_state_index = self . params . p_states . len ( ) - 1 ; for ( i , state ) in self . params . p_states . iter ( ) . enumerate ( ) { // We estimate that the operation rate over the next interval will be the min of // the last operation rate and the frequency of the P-state under consideration. // // Note that we don't currently account for a rise in frequency allowing for a possible // increase in the operation rate. let max_operation_rate = state . frequency . mul_scalar ( num_cpus as f64 ) ; let estimated_operation_rate = if max_operation_rate < last_operation_rate { max_operation_rate } else { last_operation_rate } ; let estimated_power = get_cpu_power ( self . params . capacitance , state . voltage , estimated_operation_rate ) ; if estimated_power <= * max_power { p_state_index = i ; break ; } } self . cpu_device . borrow_mut ( ) . set_p_state_index ( p_state_index ) ; Ok ( MessageReturn :: SetMaxPowerConsumption ) }
Sets the P-state to the highest-power state with consumption below `max_power`.
Sets the P - state to the highest - power state with consumption below ` max_power ` .
&self
max_power
&Watts