def set_governors(c, governor, cpus=None): try: c = cpuFreq() c.set_governors(gov=governor, rg=cpus) print("Governors set successfully.") except CPUFreqErrorInit as err: print("{}".format(err))
def main(): """ Main function executed from console run. """ try: c = cpuFreq() except CPUFreqErrorInit as err: print("{}".format(err)) exit() args = argsparsevalidation(c.available_governors) if args.info is True: info(c) elif args.reset is True: c.reset() print("Governors, maximum and minimum frequencies reset successfully.") elif hasattr(args, "governor"): if args.all == True: rg = None else: avail_cpus = c.get_online_cpus() if not set(args.cpus).issubset(set(avail_cpus)): print( "ERROR: cpu list has cpu number(s) that not in online cpus list." ) exit(1) rg = args.cpus c.set_governors(gov=args.governor, rg=rg) print("Governor set successfully to cpus.") elif hasattr(args, "frequency"): if not args.frequency in c.available_frequencies: print( "ERROR: frequency should be a value in list availabe frequencies: " ) print(" ", c.available_frequencies) exit(1) if args.all == True: rg = None else: avail_cpus = c.get_online_cpus() if not set(args.cpus).issubset(set(avail_cpus)): print( "ERROR: cpu list has cpu number(s) that not in online cpus list." ) exit(1) rg = args.cpus c.set_frequencies(freq=args.frequency, rg=rg) print("Frequency set successfully to cpus.")
def raise_frequency(freq, rg): """ Sets the specified cores to the given frequency, supposed that their actual frequencies are lower than the given one. Parameters ---------- freq : int The frequency, in KHz, to which cores will be set. rg : list The cores whose frequency will be raised. """ cpu = cpufreq.cpuFreq() cpu.set_max_frequencies(freq, rg) cpu.set_min_frequencies(freq, rg) cpu.set_frequencies(freq, rg)
GPIO.setup(PIN_BIT2, GPIO.IN) GPIO.setup(PIN_CLK_BIT, GPIO.IN) def send_clk(): GPIO.output(PIN_CLK, True) GPIO.output(PIN_CLK, False) def send_clr(): GPIO.output(PIN_CLR, True) GPIO.output(PIN_CLR, False) SAMPLE_RATE = 8000 cpu = cpuFreq() freqs = cpu.get_frequencies() DELAY_TIME = freqs[0] / SAMPLE_RATE # END OF RPI ZONE JSON = open('song_list.json', ) filename = json.load(JSON)['song'] # print(filename) JSON.close() # constant variable CHUNK = 1024
def main(): cpu = cpufreq.cpuFreq() while(1): print(cpu.get_frequencies()[0]) time.sleep(0.001)
def __init__(self, **config): ### CPUEnv constant values. # SOCKET socket to get pyRAPL measures # CORES CPU cores assigned to SOCKET # LIMIT power limit for environment functionality # MAXSTEPS maximum iterations for environment # TIME time spent in each rapl measurement self.LIMIT = config.get('power', DEF_POWERLIMIT) self.SOCKET = config.get('socket', DEF_SOCKET) self.CORES = config.get('cores', DEF_CORES) self.MAXSTEPS = config.get('maxsteps', DEF_MAXSTEPS) self.SEED = config.get('seed', DEF_SEED) self.DECISION_TIME = config.get('decision_time', DEF_DECISION) self.MEASURE_TIME = config.get('measure_time', self.DECISION_TIME) self.SLEEP_TIME = self.DECISION_TIME - self.MEASURE_TIME assert(self.SLEEP_TIME >= 0) ### Default metadata. self.metadata = { 'render.modes': ['human'] } ### Frequency control. # _cpu: cpufreq class control # _frequencies: list of available frequencies (<= order) self._cpu = cpufreq.cpuFreq() self._frequencies = sorted( self._cpu.available_frequencies ) # Set scheme of used cores to 'userspace' to allow frequency control. self._cpu.set_governors('userspace', self.CORES) ### Action space [0,1] where: # '0' lower frequency # '1' raise frequency self.action_space = gym.spaces.Discrete(2) self.LOWER_FREQ = 0 self.RAISE_FREQ = 1 ### Action rewards: # REWARD_LOWER_BELOW lower frequency while below power limit # REWARD_RAISE_BELOW raise frequency while below power limit # REWARD_LOWER_ABOVE lower frequency while above power limit # REWARD_RAISE_ABOVE raise frequency while above power limit self.REWARD_LOWER_BELOW = -1 self.REWARD_RAISE_BELOW = 1 self.REWARD_LOWER_ABOVE = 5 self.REWARD_RAISE_ABOVE = -5 ### Observation space: # Positions of CPU frequencies list. self.observation_space = gym.spaces.Discrete(len(self._frequencies) + 1) self._state = -1 ### CPUEnv: random number generator. # RNG random number generator self.RNG = None self.seed( self.SEED ) ### CPUEnv: general environment variables. # _reward: accumulated environment reward # _done: boolean value to indicate if goal or max steps were reached # _info: dict for auxiliary debug values # _count: counts the number of steps taken during environment action # _power: current power consumption self._reward = None self._done = None self._info = None self._count = None self._power = None self.reset()
import time from filelock import FileLock import powerutil.numpypower as npp import timeutil.numpytime as npt # MODIFY ACCORDING TO YOUR MACHINE CPU CONFIGURATION. CPU_LIST = list(range(16)) SOCKET_LIST = [0, 1] SOCKET_DICT = { 0: CPU_LIST[0:8], 1: CPU_LIST[8:16] } # cpufreq variables. AVAILABLE_FREQS = sorted(cpufreq.cpuFreq().available_frequencies) ## TIME TEST TIME_CPU = 0 TIME_REPS = 10 # ENERGY MEASURE DEF_POWERTIME = 2.0 # MAX FREQUENCY NOMINAL_MAXFREQ = 2601000 REAL_MAXFREQ = 3000000 # DIMENSION DEF_DIM = 1000
def __init__(self, *args, **kwargs): super(TestMethods, self).__init__(*args, **kwargs) self.cpu = cpufreq.cpuFreq() self.cpu.reset() self.online = self.cpu.get_online_cpus()
def __init__(self, **config): ### CPUEnv constant values. # POWER power cap to reach self.POWER = config.get('power', self.DEF_POWER) # SOCKET socket to get pyRAPL measures # CORES CPU cores assigned to SOCKET self.SOCKET = config.get('socket', self.DEF_SOCKET) self.CORES = config.get('cores', self.DEF_CORES) # MAXSTEPS maximum iterations for environment # SEED seed for RNG reporducibility self.MAXSTEPS = config.get('maxsteps', self.DEF_MAXSTEPS) self.SEED = config.get('seed', self.DEF_SEED) # MINPOWER minimum in power bandwidth # MAXPOWER maximum in power bandwidth self.MINPOWER = config.get('minpower', self.DEF_MINPOWER) self.MAXPOWER = config.get('maxpower', self.DEF_MAXPOWER) assert (self.MINPOWER < self.MAXPOWER) # DECISION_TIME time spent between actions (frequency change and power measure) # MEASURE_TIME time spent measuring energy data # SLEEP_TIME* waiting time after frequency change self.DECISION_TIME = config.get('decision_time', self.DEF_DECISION) self.MEASURE_TIME = config.get('measure_time', self.DECISION_TIME) self.SLEEP_TIME = self.DECISION_TIME - self.MEASURE_TIME # POWERSTEP size of intervals of observation space # POWERPOINTS extrema of power intervals # INTERVALS list power intervals self.POWERSTEP = config.get('powstep', self.DEF_POWERSTEP) self.POWERPOINTS = self.get_powerpoints(self.POWERSTEP) self.INTERVALS = self.get_intervals(self.POWERPOINTS) ### Default metadata. self.metadata = {'render.modes': ['human']} ### Frequency control. # _cpu cpufreq class control # _frequencies list of available frequencies (<= order) # _freqpos position of current frequency self._cpu = cpufreq.cpuFreq() self._frequencies = sorted(self._cpu.available_frequencies)[:-1] self._freqpos = -1 # Set used cores to 'userspace' scheme for frequency modification. self._cpu.set_governors('userspace', self.CORES) ### Power measure. pyRAPL.setup(devices=[pyRAPL.Device.PKG], socket_ids=[self.SOCKET]) ### Action space. # 0: hold frequency # 1: lower frequency # 2: raise frequency self.action_space = gym.spaces.Discrete(3) self.HOLD_FREQ = 0 self.LOWER_FREQ = 1 self.RAISE_FREQ = 2 ### Action rewards: # See 'get_reward()' # REWARD_CLOSER given when action approaches goal # REWARD_FARTHER given when action gets farther from goal # REWARD_GOAL given when action gets to goal state self.REWARD_CLOSER = +1 self.REWARD_FARTHER = -1 self.REWARD_GOAL = +2 ### Observation space: # Interval partition of power range of CPU. # Shape of intervals: (power_i, power_i+1] self.observation_space = gym.spaces.Discrete(len(self.INTERVALS) + 1) # _power: current power consumption # _state: interval of current power consumption # _goal: interval of self.LIMIT self._power = 0.0 self._state = 0 self._goal = self.get_state(self.POWER) ### CPUEnv: random number generator. # RNG random number generator self.RNG = None self.seed(self.SEED) ### CPUEnv: general environment variables. # _reward: accumulated environment reward # _done: boolean value to indicate if goal or max steps were reached # _info: dict for auxiliary debug values # _count: counts the number of steps taken during environment action self._reward = None self._acc_reward = None self._done = None self._info = None self._count = None self.reset()
def __init__(self, **config): ### CPUEnv constant values. # SOCKET socket to get pyRAPL measures # CORES CPU cores assigned to SOCKET # LIMIT power limit for environment functionality # MAXSTEPS maximum iterations for environment # TIME time spent in each rapl measurement # POWERSTEP size of intervals of observation space self.LIMIT = config.get('power', self.DEF_POWERLIMIT) self.SOCKET = config.get('socket', self.DEF_SOCKET) self.CORES = config.get('cores', self.DEF_CORES) self.MAXSTEPS = config.get('maxsteps', self.DEF_MAXSTEPS) self.SEED = config.get('seed', self.DEF_SEED) self.MINPOWER = config.get('minpower', self.DEF_MINPOWER) self.MAXPOWER = config.get('maxpower', self.DEF_MAXPOWER) assert (self.MINPOWER < self.MAXPOWER) self.DECISION_TIME = config.get('decision_time', self.DEF_DECISION) self.MEASURE_TIME = config.get('measure_time', self.DECISION_TIME) self.SLEEP_TIME = self.DECISION_TIME - self.MEASURE_TIME assert (self.SLEEP_TIME >= 0) self.POWERPOINTS = self.get_powerpoints(**config) self.INTERVALS = len(self.POWERPOINTS) + 1 ### Default metadata. self.metadata = {'render.modes': ['human']} ### Frequency control. # _cpu: cpufreq class control # _frequencies: list of available frequencies (<= order) # _freqpos: position of current frequency self._cpu = cpufreq.cpuFreq() self._frequencies = sorted(self._cpu.available_frequencies) self._freqpos = -1 # Set used cores to 'userspace' scheme for frequency modification. self._cpu.set_governors('userspace', self.CORES) ### Action space. # 0: lower frequency # 1: raise frequency self.action_space = gym.spaces.Discrete(2) self.LOWER_FREQ = 0 self.RAISE_FREQ = 1 ### Action rewards: # REWARD_LOWER_BELOW lower frequency while below limit interval # REWARD_RAISE_BELOW raise frequency while below limit interval # REWARD_LOWER_ABOVE lower frequency while above limit interval # REWARD_RAISE_ABOVE raise frequency while above limit interval # REWARD_GOAL interval goal reached self.REWARD_LOWER_BELOW = -1 self.REWARD_RAISE_BELOW = 1 self.REWARD_LOWER_ABOVE = 5 self.REWARD_RAISE_ABOVE = -5 self.REWARD_GOAL = 50 ### Observation space: # Interval partition of power range of CPU. # Shape of intervals: (power_i, power_i+1] self.observation_space = gym.spaces.Discrete(self.INTERVALS + 1) # _power: current power consumption # _state: interval of current power consumption # _goal: interval of self.LIMIT self._power = 0.0 self._state = 0 self._goal = self.get_state(self.LIMIT) ### CPUEnv: random number generator. # RNG random number generator self.RNG = None self.seed(self.SEED) ### CPUEnv: general environment variables. # _reward: accumulated environment reward # _done: boolean value to indicate if goal or max steps were reached # _info: dict for auxiliary debug values # _count: counts the number of steps taken during environment action self._reward = None self._done = None self._info = None self._count = None self.reset()
def main(): # 'cpufreq' module object to modify CPU states. global _cpu _cpu = cpufreq.cpuFreq() # List of available CPU governor schemes. global _available_govs _available_govs = [ 'conservative', 'ondemand', 'userspace', 'powersave', 'performance', 'schedutil' ] # List of CPU available frequencies. global _available_freqs _available_freqs = sorted(_cpu.available_frequencies) # Verbose option. global _verbose ## Command parsing. parser = get_parser() args = parser.parse_args() rg = args.cpu _verbose = True if args.verbose else False if 'governor' in args: gov = args.governor if gov == 'show': get_governors(rg) else: set_governors(gov, rg) if 'online' in args: online = args.online if online == 'show': get_online() if online == 'on': set_online(rg) if online == 'off': set_offline(rg) if 'min' in args: minf = closest_frequency(args.min * 1000) set_minimum(minf, rg) if 'max' in args: maxf = closest_frequency(args.max * 1000) set_maximum(maxf, rg) if 'frequency' in args: freq = closest_frequency(args.frequency * 1000) set_frequency(freq, rg) if 'nolimits' in args: freq = closest_frequency(args.nolimits * 1000) set_frequency(freq, rg, False) if 'list' in args: show_list(args.list) if args.show: get_frequency(rg) if args.reset: _cpu.reset()