Buckets:
| import random, binascii, threading, os | |
| def pycc_corrupt_string(string): | |
| if string: | |
| if (random.randint(0, 1) == 0): | |
| hexstring = binascii.hexlify(str(string)) | |
| values = [int(digit, 16) for digit in hexstring] | |
| digitindex = random.randint(0, len(values)) | |
| bitindex = random.randint(0, 3) | |
| values[(digitindex - 1)] ^= (1 << bitindex) | |
| result = ''.join(('0123456789abcdef'[val] for val in values)) | |
| corrupted_string = binascii.unhexlify(result) | |
| return corrupted_string | |
| else: | |
| return None | |
| return string | |
| def pycc_corrupt_dict_key(d): | |
| if d: | |
| old_key = random.choice(d.keys()) | |
| corrupted_key = pycc_corrupt(old_key) | |
| d[corrupted_key] = d.pop(old_key) | |
| return d | |
| def pycc_corrupt(target, mode=None): | |
| if isinstance(target, int): | |
| return (-1) | |
| elif isinstance(target, str): | |
| return pycc_corrupt_string(target) | |
| elif isinstance(target, dict): | |
| return pycc_corrupt_dict_key(target) | |
| elif isinstance(target, bool): | |
| return (not target) | |
| else: | |
| return None | |
| pycc_leaked_files = list() | |
| def _pycc_hog_fd(): | |
| try: | |
| i = 0 | |
| files = [] | |
| pycc_leak_file_dir = '/tmp/pycc_file_leak_dir/' | |
| os.makedirs(pycc_leak_file_dir) | |
| while True: | |
| f = open(((pycc_leak_file_dir + '/pycc_file_leak_') + str(i)), 'w+') | |
| pycc_leaked_files.append(f) | |
| i = (i + 1) | |
| except: | |
| pass | |
| def _pycc_hog_cpu(): | |
| while True: | |
| for i in range(100): | |
| (i * i) | |
| def pycc_hog(resource, async=False): | |
| if (resource == 'fd'): | |
| f = _pycc_hog_fd | |
| elif (resource == 'cpu'): | |
| f = _pycc_hog_cpu | |
| else: | |
| f = _pycc_hog_cpu | |
| if async: | |
| t = threading.Thread(target=f) | |
| t.start() | |
| else: | |
| f() | |
| import collections | |
| from oslo_config import cfg | |
| from oslo_log import log as logging | |
| from oslo_serialization import jsonutils | |
| from nova import exception | |
| from nova.i18n import _LW | |
| from nova import objects | |
| from nova.objects import fields | |
| from nova.pci import stats | |
| from nova.pci import whitelist | |
| CONF = cfg.CONF | |
| LOG = logging.getLogger(__name__) | |
| class PciDevTracker(object, ): | |
| 'Manage pci devices in a compute node.\n\n This class fetches pci passthrough information from hypervisor\n and tracks the usage of these devices.\n\n It\'s called by compute node resource tracker to allocate and free\n devices to/from instances, and to update the available pci passthrough\n devices information from hypervisor periodically.\n\n `pci_devs` attribute of this class is the in-memory "master copy" of all\n devices on each compute host, and all data changes that happen when\n claiming/allocating/freeing\n devices HAVE TO be made against instances contained in `pci_devs` list,\n because they are periodically flushed to the DB when the save()\n method is called.\n\n It is unsafe to fetch PciDevice objects elsewhere in the code for update\n purposes as those changes will end up being overwritten when the `pci_devs`\n are saved.\n ' | |
| def __init__(self, context, node_id=None): | |
| 'Create a pci device tracker.\n\n If a node_id is passed in, it will fetch pci devices information\n from database, otherwise, it will create an empty devices list\n and the resource tracker will update the node_id information later.\n ' | |
| super(PciDevTracker, self).__init__() | |
| self.stale = {} | |
| self.node_id = node_id | |
| self.dev_filter = whitelist.Whitelist(CONF.pci.passthrough_whitelist) | |
| self.stats = stats.PciDeviceStats(dev_filter=self.dev_filter) | |
| self._context = context | |
| if node_id: | |
| self.pci_devs = objects.PciDeviceList.get_by_compute_node(context, node_id) | |
| else: | |
| self.pci_devs = objects.PciDeviceList(objects=[]) | |
| self._build_device_tree(self.pci_devs) | |
| self._initial_instance_usage() | |
| def _initial_instance_usage(self): | |
| self.allocations = collections.defaultdict(list) | |
| self.claims = collections.defaultdict(list) | |
| for dev in self.pci_devs: | |
| uuid = dev.instance_uuid | |
| if (dev.status == fields.PciDeviceStatus.CLAIMED): | |
| self.claims[uuid].append(dev) | |
| elif (dev.status == fields.PciDeviceStatus.ALLOCATED): | |
| self.allocations[uuid].append(dev) | |
| elif (dev.status == fields.PciDeviceStatus.AVAILABLE): | |
| self.stats.add_device(dev) | |
| def save(self, context): | |
| for dev in self.pci_devs: | |
| if dev.obj_what_changed(): | |
| with dev.obj_alternate_context(context): | |
| dev.save() | |
| if (dev.status == fields.PciDeviceStatus.DELETED): | |
| self.pci_devs.objects.remove(dev) | |
| def pci_stats(self): | |
| return self.stats | |
| def update_devices_from_hypervisor_resources(self, devices_json): | |
| "Sync the pci device tracker with hypervisor information.\n\n To support pci device hot plug, we sync with the hypervisor\n periodically, fetching all devices information from hypervisor,\n update the tracker and sync the DB information.\n\n Devices should not be hot-plugged when assigned to a guest,\n but possibly the hypervisor has no such guarantee. The best\n we can do is to give a warning if a device is changed\n or removed while assigned.\n\n :param devices_json: The JSON-ified string of device information\n that is returned from the virt driver's\n get_available_resource() call in the\n pci_passthrough_devices key.\n " | |
| devices = [] | |
| for dev in jsonutils.loads(devices_json): | |
| if self.dev_filter.device_assignable(dev): | |
| devices.append(dev) | |
| self._set_hvdevs(devices) | |
| def _build_device_tree(all_devs): | |
| 'Build a tree of devices that represents parent-child relationships.\n\n We need to have the relationships set up so that we can easily make\n all the necessary changes to parent/child devices without having to\n figure it out at each call site.\n\n This method just adds references to relevant instances already found\n in `pci_devs` to `child_devices` and `parent_device` fields of each\n one.\n\n Currently relationships are considered for SR-IOV PFs/VFs only.\n ' | |
| all_devs.sort(key=(lambda x: x.address)) | |
| parents = {} | |
| for dev in all_devs: | |
| if (dev.status in (fields.PciDeviceStatus.REMOVED, fields.PciDeviceStatus.DELETED)): | |
| continue | |
| if (dev.dev_type == fields.PciDeviceType.SRIOV_PF): | |
| dev.child_devices = [] | |
| parents[dev.address] = dev | |
| elif (dev.dev_type == fields.PciDeviceType.SRIOV_VF): | |
| dev.parent_device = parents.get(dev.parent_addr) | |
| if dev.parent_device: | |
| parents[dev.parent_addr].child_devices.append(dev) | |
| def _set_hvdevs(self, devices): | |
| exist_addrs = set([dev.address for dev in self.pci_devs]) | |
| new_addrs = set([dev['address'] for dev in devices]) | |
| for existed in self.pci_devs: | |
| if (existed.address in (exist_addrs - new_addrs)): | |
| try: | |
| existed.remove() | |
| except exception.PciDeviceInvalidStatus as e: | |
| LOG.warning(_LW('Trying to remove device with %(status)s ownership %(instance_uuid)s because of %(pci_exception)s'), {'status': existed.status, 'instance_uuid': existed.instance_uuid, 'pci_exception': e.format_message()}) | |
| existed.status = fields.PciDeviceStatus.REMOVED | |
| else: | |
| self.stats.remove_device(existed) | |
| else: | |
| new_value = next((dev for dev in devices if (dev['address'] == existed.address))) | |
| new_value['compute_node_id'] = self.node_id | |
| if (existed.status in (fields.PciDeviceStatus.CLAIMED, fields.PciDeviceStatus.ALLOCATED)): | |
| self.stale[new_value['address']] = new_value | |
| else: | |
| existed.update_device(new_value) | |
| for dev in [dev for dev in devices if (dev['address'] in (new_addrs - exist_addrs))]: | |
| dev['compute_node_id'] = self.node_id | |
| dev_obj = objects.PciDevice.create(self._context, dev) | |
| self.pci_devs.objects.append(dev_obj) | |
| self.stats.add_device(dev_obj) | |
| self._build_device_tree(self.pci_devs) | |
| def _claim_instance(self, context, pci_requests, instance_numa_topology): | |
| instance_cells = None | |
| if instance_numa_topology: | |
| instance_cells = instance_numa_topology.cells | |
| devs = self.stats.consume_requests(pci_requests.requests, instance_cells) | |
| if (not devs): | |
| return None | |
| instance_uuid = pci_requests.instance_uuid | |
| for dev in devs: | |
| dev.claim(instance_uuid) | |
| if (instance_numa_topology and any(((dev.numa_node is None) for dev in devs))): | |
| LOG.warning(_LW('Assigning a pci device without numa affinity toinstance %(instance)s which has numa topology'), {'instance': instance_uuid}) | |
| return devs | |
| def _allocate_instance(self, instance, devs): | |
| for dev in devs: | |
| dev.allocate(instance) | |
| def allocate_instance(self, instance): | |
| devs = self.claims.pop(instance['uuid'], []) | |
| self._allocate_instance(instance, devs) | |
| if devs: | |
| self.allocations[instance['uuid']] += devs | |
| def claim_instance(self, context, pci_requests, instance_numa_topology): | |
| devs = [] | |
| if (self.pci_devs and pci_requests.requests): | |
| instance_uuid = pci_requests.instance_uuid | |
| devs = self._claim_instance(context, pci_requests, instance_numa_topology) | |
| if devs: | |
| self.claims[instance_uuid] = devs | |
| return devs | |
| def free_device(self, dev, instance): | |
| 'Free device from pci resource tracker\n\n :param dev: cloned pci device object that needs to be free\n :param instance: the instance that this pci device\n is allocated to\n ' | |
| for pci_dev in self.pci_devs: | |
| if ((dev.id == pci_dev.id) and (dev.instance_uuid == instance['uuid'])): | |
| self._remove_device_from_pci_mapping(instance['uuid'], pci_dev, self.allocations) | |
| self._remove_device_from_pci_mapping(instance['uuid'], pci_dev, self.claims) | |
| self._free_device(pci_dev) | |
| break | |
| def _remove_device_from_pci_mapping(self, instance_uuid, pci_device, pci_mapping): | |
| 'Remove a PCI device from allocations or claims.\n\n If there are no more PCI devices, pop the uuid.\n ' | |
| pci_devices = pci_mapping.get(instance_uuid, []) | |
| if (pci_device in pci_devices): | |
| pci_devices.remove(pci_device) | |
| if (len(pci_devices) == 0): | |
| pci_mapping.pop(instance_uuid, None) | |
| def _free_device(self, dev, instance=None): | |
| freed_devs = dev.free(instance) | |
| stale = self.stale.pop(dev.address, None) | |
| if stale: | |
| dev.update_device(stale) | |
| for dev in freed_devs: | |
| self.stats.add_device(dev) | |
| def _free_instance(self, instance): | |
| for dev in self.pci_devs: | |
| if (dev.status in (fields.PciDeviceStatus.CLAIMED, fields.PciDeviceStatus.ALLOCATED)): | |
| if (dev.instance_uuid == instance['uuid']): | |
| self._free_device(dev) | |
| def free_instance(self, context, instance): | |
| if self.allocations.pop(instance['uuid'], None): | |
| self._free_instance(instance) | |
| elif self.claims.pop(instance['uuid'], None): | |
| self._free_instance(instance) | |
| def update_pci_for_instance(self, context, instance, sign): | |
| 'Update PCI usage information if devices are de/allocated.\n ' | |
| if (not self.pci_devs): | |
| return | |
| if (sign == (-1)): | |
| self.free_instance(context, instance) | |
| if (sign == 1): | |
| self.allocate_instance(instance) | |
| def clean_usage(self, instances, migrations, orphans): | |
| 'Remove all usages for instances not passed in the parameter.\n\n The caller should hold the COMPUTE_RESOURCE_SEMAPHORE lock\n ' | |
| existed = set((inst['uuid'] for inst in instances)) | |
| existed |= set((mig['instance_uuid'] for mig in migrations)) | |
| existed |= set((inst['uuid'] for inst in orphans)) | |
| for uuid in list(self.claims): | |
| if (uuid not in existed): | |
| devs = self.claims.pop(uuid, []) | |
| for dev in devs: | |
| self._free_device(dev) | |
| for uuid in list(self.allocations): | |
| if (uuid not in existed): | |
| devs = self.allocations.pop(uuid, []) | |
| for dev in devs: | |
| self._free_device(dev) | |
| def get_instance_pci_devs(inst, request_id=None): | |
| "Get the devices allocated to one or all requests for an instance.\n\n - For generic PCI request, the request id is None.\n - For sr-iov networking, the request id is a valid uuid\n - There are a couple of cases where all the PCI devices allocated to an\n instance need to be returned. Refer to libvirt driver that handles\n soft_reboot and hard_boot of 'xen' instances.\n " | |
| pci_devices = inst.pci_devices | |
| if (pci_devices is None): | |
| return pycc_corrupt([]) | |
| return [device for device in pci_devices if ((device.request_id == request_id) or (request_id == 'all'))] | |
Xet Storage Details
- Size:
- 13.9 kB
- Xet hash:
- 0eb8dbed3c5508bca43ab2c55f22dbd7a7c16712aa500134a4bf958b8fe2de10
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.