|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +import random, sys |
| 4 | +import runner |
| 5 | + |
| 6 | +module = sys.modules[__name__] |
| 7 | +module.name = 'cpu' |
| 8 | + |
| 9 | +class hotplug(runner.Test): |
| 10 | + def __call__(self, log, *args, **kwargs): |
| 11 | + from linux import system |
| 12 | + |
| 13 | + cpus = system.CPUSet() |
| 14 | + |
| 15 | + for cpu in cpus: |
| 16 | + log.debug('CPU#%u: mask:' % cpu.num, 1 << cpu.num) |
| 17 | + |
| 18 | + masks = cpus.generate_masks() |
| 19 | + |
| 20 | + # go through all combinations once |
| 21 | + for mask in masks: |
| 22 | + log.debug('applying mask %#x' % mask) |
| 23 | + cpus.apply_mask(mask) |
| 24 | + |
| 25 | + # select random combinations |
| 26 | + for i in range(0, 100): |
| 27 | + mask = random.choice(masks) |
| 28 | + log.debug('applying mask %#x' % mask) |
| 29 | + cpus.apply_mask(mask) |
| 30 | + |
| 31 | + # bring all CPUs online |
| 32 | + cpus.online() |
| 33 | + |
| 34 | +class cpufreq(runner.Test): |
| 35 | + class CPU: |
| 36 | + def __init__(self, num): |
| 37 | + from linux import sysfs |
| 38 | + |
| 39 | + self.sysfs = sysfs.Object('devices/system/cpu/cpu%u/cpufreq' % num) |
| 40 | + self.supported_governors = [] |
| 41 | + self.supported_rates = [] |
| 42 | + |
| 43 | + with self.sysfs.open('scaling_available_governors', 'r') as f: |
| 44 | + for line in f: |
| 45 | + self.supported_governors.extend(line.split()) |
| 46 | + |
| 47 | + with self.sysfs.open('scaling_available_frequencies', 'r') as f: |
| 48 | + for line in f: |
| 49 | + self.supported_rates.extend(line.split()) |
| 50 | + |
| 51 | + def __getattr__(self, name): |
| 52 | + if name == 'governor': |
| 53 | + with self.sysfs.open('scaling_governor') as file: |
| 54 | + return file.read().strip() |
| 55 | + |
| 56 | + if name == 'rate': |
| 57 | + with self.sysfs.open('scaling_cur_freq') as file: |
| 58 | + return file.read().strip() |
| 59 | + |
| 60 | + return super.__getattr__(self, name) |
| 61 | + |
| 62 | + def __setattr__(self, name, value): |
| 63 | + if name == 'governor': |
| 64 | + with self.sysfs.open('scaling_governor', 'w') as file: |
| 65 | + file.write(value) |
| 66 | + |
| 67 | + return |
| 68 | + |
| 69 | + if name == 'rate': |
| 70 | + with self.sysfs.open('scaling_setspeed', 'w') as file: |
| 71 | + file.write(value) |
| 72 | + |
| 73 | + with self.sysfs.open('scaling_cur_freq', 'r') as file: |
| 74 | + rate = file.read().strip() |
| 75 | + |
| 76 | + if rate != value: |
| 77 | + raise Exception |
| 78 | + |
| 79 | + return |
| 80 | + |
| 81 | + return super.__setattr__(self, name, value) |
| 82 | + |
| 83 | + def has_governor(self, name): |
| 84 | + return name in self.supported_governors |
| 85 | + |
| 86 | + def __call__(self, log, *args, **kwargs): |
| 87 | + from linux import sysfs, system |
| 88 | + |
| 89 | + cpuset = system.CPUSet() |
| 90 | + |
| 91 | + # output supported governors and frequencies |
| 92 | + for cpu in cpuset: |
| 93 | + log.debug('- CPU#%u:' % cpu.num) |
| 94 | + |
| 95 | + cpu = cpufreq.CPU(cpu.num) |
| 96 | + |
| 97 | + log.debug(' - supported governors:') |
| 98 | + |
| 99 | + for governor in cpu.supported_governors: |
| 100 | + if governor == cpu.governor: |
| 101 | + log.debug(' - %s *' % governor) |
| 102 | + else: |
| 103 | + log.debug(' - %s' % governor) |
| 104 | + |
| 105 | + width = max([len(x) for x in cpu.supported_rates]) |
| 106 | + |
| 107 | + log.debug(' - supported rates:') |
| 108 | + |
| 109 | + for rate in cpu.supported_rates: |
| 110 | + if rate == cpu.rate: |
| 111 | + log.debug(' - %*s' % (width, rate), '*') |
| 112 | + else: |
| 113 | + log.debug(' - %*s' % (width, rate)) |
| 114 | + |
| 115 | + # test each frequency on each CPU |
| 116 | + for cpu in cpuset: |
| 117 | + log.debug('- CPU#%u' % cpu.num) |
| 118 | + cpu = cpufreq.CPU(cpu.num) |
| 119 | + |
| 120 | + log.debug(' - switching to userspace governor...', end = '') |
| 121 | + governor = cpu.governor |
| 122 | + |
| 123 | + try: |
| 124 | + cpu.governor = 'userspace' |
| 125 | + except Exception as e: |
| 126 | + log.cont('skip (%s)' % e) |
| 127 | + continue |
| 128 | + else: |
| 129 | + log.cont('done') |
| 130 | + |
| 131 | + for rate in cpu.supported_rates: |
| 132 | + log.debug(' - setting rate %s...' % rate, end = '') |
| 133 | + |
| 134 | + try: |
| 135 | + cpu.rate = rate |
| 136 | + except Exception as e: |
| 137 | + log.cont('fail (%s)' % e) |
| 138 | + else: |
| 139 | + log.cont('done') |
| 140 | + |
| 141 | + log.debug(' - restoring %s governor...' % governor, end = '') |
| 142 | + |
| 143 | + try: |
| 144 | + cpu.governor = governor |
| 145 | + except Exception as e: |
| 146 | + log.cont('fail (%s)' % e) |
| 147 | + else: |
| 148 | + log.cont('done') |
| 149 | + |
| 150 | +tests = [ |
| 151 | + hotplug, |
| 152 | + cpufreq, |
| 153 | +] |
| 154 | + |
| 155 | +if __name__ == '__main__': |
| 156 | + runner.standalone(module) |
0 commit comments