CPU affinity reset during save/restore
PATCH 1/1 CPU affinity reset during save/restore
Hi,
This is the same patch I sent earlier this week with Signed-off-by-line added.
Even though there is a quite old cs 14588
(http://xenbits.xensource.com/xen-unstable.hg?rev/853853686147) described as a fix to this issue, the CPU affinity is still reset after dom-U being saved and restored in current xen-unstable.
I found two reasons for this. First, although XendConfig._parse_sxp() tries to parse the affinity from 'cpus', the list is not stored in domain's checkpoint file. This is because cs 15152
(http://xenbits.xensource.com/xen-unstable.hg?rev/681ed46676a6) prevents 'cpus' list to be generated in XendConfig.to_sxp().
Second, even though 'cpus' is correctly parsed in XendConfig._parse_sxp(), it is not used for setting CPU affinity in XendDomainInfo.resume().
That said, after reverting cs 14588 and applying the attached patch, the CPU affinity is correctly restored after restoring the domain.
The attached patch just copies the code related to CPU affinity from XendDomainInfo._initDomain() into XendDomainInfo._setCPUAffinity() and calls this method from _initDomain() and resume().
What do you think about this way to fix the CPU affinity reset? If it is not the right way, what way do you prefer this issue to be fixed?
Any comments how the patch could be improved are most welcome.
tools/python/xen/xend/XendDomainInfo.py +60 -52
@@ -479,6 +479,7 @@ if state in (DOM_STATE_SUSPENDED, DOM_STATE_HALTED): try: self._constructDomain() + self._setCPUAffinity() self._storeVmDetails() self._createChannels() self._createDevices()
@@ -2166,6 +2167,64 @@ raise XendError(str(exn)) + def _setCPUAffinity(self): + """ Repin domain vcpus if a restricted cpus list is provided + """ + + def has_cpus(): + if self.info['cpus'] is not None: + for c in self.info['cpus']: + if c: + return True + return False + + if has_cpus(): + for v in range(0, self.info['VCPUs_max']): + if self.info['cpus'][v]: + xc.vcpu_setaffinity(self.domid, v, self.info['cpus'][v]) + else: + def find_relaxed_node(node_list): + import sys + nr_nodes = info['nr_nodes'] + if node_list is None: + node_list = range(0, nr_nodes) + nodeload = [0] + nodeload = nodeload * nr_nodes + from xen.xend import XendDomain + doms = XendDomain.instance().list('all') + for dom in filter (lambda d: d.domid != self.domid, doms): + cpuinfo = dom.getVCPUInfo() + for vcpu in sxp.children(cpuinfo, 'vcpu'): + if sxp.child_value(vcpu, 'online') == 0: continue + cpumap = list(sxp.child_value(vcpu,'cpumap')) + for i in range(0, nr_nodes): + node_cpumask = info['node_to_cpu'][i] + for j in node_cpumask: + if j in cpumap: + nodeload[i] += 1 + break + for i in range(0, nr_nodes): + if len(info['node_to_cpu'][i]) > 0 and i in node_list: + nodeload[i] = int(nodeload[i] * 16 / len(info['node_to_cpu'][i])) + else: + nodeload[i] = sys.maxint + index = nodeload.index( min(nodeload) ) + return index + + info = xc.physinfo() + if info['nr_nodes'] > 1: + node_memory_list = info['node_to_memory'] + needmem = self.image.getRequiredAvailableMemory(self.info['memory_dynamic_max']) / 1024 + candidate_node_list = [] + for i in range(0, info['nr_nodes']): + if node_memory_list[i] >= needmem and len(info['node_to_cpu'][i]) > 0: + candidate_node_list.append(i) + index = find_relaxed_node(candidate_node_list) + cpumask = info['node_to_cpu'][index] + for v in range(0, self.info['VCPUs_max']): + xc.vcpu_setaffinity(self.domid, v, cpumask) + + def _initDomain(self): log.debug('XendDomainInfo.initDomain: %s %s', self.domid,
@@ -2185,58 +2244,7 @@ # repin domain vcpus if a restricted cpus list is provided # this is done prior to memory allocation to aide in memory # distribution for NUMA systems. - def has_cpus(): - if self.info['cpus'] is not None: - for c in self.info['cpus']: - if c: - return True - return False - - if has_cpus(): - for v in range(0, self.info['VCPUs_max']): - if self.info['cpus'][v]: - xc.vcpu_setaffinity(self.domid, v, self.info['cpus'][v]) - else: - def find_relaxed_node(node_list): - import sys - nr_nodes = info['nr_nodes'] - if node_list is None: - node_list = range(0, nr_nodes) - nodeload = [0] - nodeload = nodeload * nr_nodes - from xen.xend import XendDomain - doms = XendDomain.instance().list('all') - for dom in filter (lambda d: d.domid != self.domid, doms): - cpuinfo = dom.getVCPUInfo() - for vcpu in sxp.children(cpuinfo, 'vcpu'): - if sxp.child_value(vcpu, 'online') == 0: continue - cpumap = list(sxp.child_value(vcpu,'cpumap')) - for i in range(0, nr_nodes): - node_cpumask = info['node_to_cpu'][i] - for j in node_cpumask: - if j in cpumap: - nodeload[i] += 1 - break - for i in range(0, nr_nodes): - if len(info['node_to_cpu'][i]) > 0 and i in node_list: - nodeload[i] = int(nodeload[i] * 16 / len(info['node_to_cpu'][i])) - else: - nodeload[i] = sys.maxint - index = nodeload.index( min(nodeload) ) - return index - - info = xc.physinfo() - if info['nr_nodes'] > 1: - node_memory_list = info['node_to_memory'] - needmem = self.image.getRequiredAvailableMemory(self.info['memory_dynamic_max']) / 1024 - candidate_node_list = [] - for i in range(0, info['nr_nodes']): - if node_memory_list[i] >= needmem and len(info['node_to_cpu'][i]) > 0: - candidate_node_list.append(i) - index = find_relaxed_node(candidate_node_list) - cpumask = info['node_to_cpu'][index] - for v in range(0, self.info['VCPUs_max']): - xc.vcpu_setaffinity(self.domid, v, cpumask) + self._setCPUAffinity() # Use architecture- and image-specific calculations to determine # the various headrooms necessary, given the raw configured
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
I looked at this problem a few months ago. Your solution looks great for save/restore on the same machine. But if the restore occurs on another machine that doesn't have the same set of CPUs, does the right thing happen? (And more importantly, what is "the right thing" for a very different set of CPUs, for example saving on a machine with no hyperthreading and restoring on one with hyper- threading?)
Even on the same machine, affinity decisions for domain X that are made between save of domain Y and restore of domain Y might imply that the same affinities for domain Y should not be applied on restore of Y.
So after thinking about it, I decided not to implement this as IMHO affinity decisions are best left to higher level management tools.
Quoted 7 lines
-----Original Message-----
From: Jiri Denemark [mailto:[email protected]]
Sent: Thursday, November 13, 2008 1:40 AM
To: [email protected]
Subject: [Xen-devel] [PATCH] CPU affinity reset during save/restore
Quoted 7 lines
Hi,
This is the same patch I sent earlier this week with Signed-off-by-line added.
Even though there is a quite old cs 14588
(http://xenbits.xensource.com/xen-unstable.hg?rev/853853686147
) described as a
fix to this issue, the CPU affinity is still reset after dom-U being saved and restored in current xen-unstable.
I found two reasons for this. First, although XendConfig._parse_sxp() tries to parse the affinity from 'cpus', the list is not stored in domain's checkpoint file. This is because cs 15152
(http://xenbits.xensource.com/xen-unstable.hg?rev/681ed46676a6) prevents 'cpus' list to be generated in XendConfig.to_sxp().
Second, even though 'cpus' is correctly parsed in XendConfig._parse_sxp(), it is not used for setting CPU affinity in XendDomainInfo.resume().
That said, after reverting cs 14588 and applying the attached patch, the CPU affinity is correctly restored after restoring the domain.
The attached patch just copies the code related to CPU affinity from XendDomainInfo._initDomain() into XendDomainInfo._setCPUAffinity() and calls this method from _initDomain() and resume().
What do you think about this way to fix the CPU affinity reset? If it is not the right way, what way do you prefer this issue to be fixed?
Any comments how the patch could be improved are most welcome.
Quoted 3 lines
So after thinking about it, I decided not to implement this as IMHO affinity decisions are best left to higher level management tools.
Well, yes, you are right affinity decisions should be made by management tools. However, I think this is more targeted into a static world, where a static set of domains is running on a host, affinity is set in each domain's configuration file and when dom0 is rebooted, domUs are automatically saved and restored. Which is when the affinity should be restored too.
Moreover, such behavior does not really interfere with any higher level management tool, as it can always repin cpus after restoring or migrating a domain, as this process would probably be under that tool's control anyway.
Of course, if domains are migrated manually, one would have to repin cpus manually, too.
Jirka
Signature · 1 line
Experience is what causes a person to make new mistakes instead of old ones.
Hi,
This is an improved patch for restoring CPU affinity on domain resume.
Quoted 3 lines
The attached patch just copies the code related to CPU affinity from
XendDomainInfo._initDomain()intoXendDomainInfo._setCPUAffinity()and calls this method from_initDomain()andresume().
I've added a try-except around self._setCPUAffinity() in resum() so that the domain can be resumed on different HW, where CPUs mentioned in the 'cpus' list are not available. In that case, the affinity is just ignored. Without this improvement, such domain would fail to resume with invalid argument.
tools/python/xen/xend/XendDomainInfo.py +67 -52
@@ -479,6 +479,14 @@ if state in (DOM_STATE_SUSPENDED, DOM_STATE_HALTED): try: self._constructDomain() + + try: + self._setCPUAffinity() + except Exception, err: + # usually a CPU we want to set affinity to does not exist + # we just ignore it so that the domain can still be restored + log.warn("cannot restore CPU affinity") + self._storeVmDetails() self._createChannels() self._createDevices()
@@ -2166,6 +2174,64 @@ raise XendError(str(exn)) + def _setCPUAffinity(self): + """ Repin domain vcpus if a restricted cpus list is provided + """ + + def has_cpus(): + if self.info['cpus'] is not None: + for c in self.info['cpus']: + if c: + return True + return False + + if has_cpus(): + for v in range(0, self.info['VCPUs_max']): + if self.info['cpus'][v]: + xc.vcpu_setaffinity(self.domid, v, self.info['cpus'][v]) + else: + def find_relaxed_node(node_list): + import sys + nr_nodes = info['nr_nodes'] + if node_list is None: + node_list = range(0, nr_nodes) + nodeload = [0] + nodeload = nodeload * nr_nodes + from xen.xend import XendDomain + doms = XendDomain.instance().list('all') + for dom in filter (lambda d: d.domid != self.domid, doms): + cpuinfo = dom.getVCPUInfo() + for vcpu in sxp.children(cpuinfo, 'vcpu'): + if sxp.child_value(vcpu, 'online') == 0: continue + cpumap = list(sxp.child_value(vcpu,'cpumap')) + for i in range(0, nr_nodes): + node_cpumask = info['node_to_cpu'][i] + for j in node_cpumask: + if j in cpumap: + nodeload[i] += 1 + break + for i in range(0, nr_nodes): + if len(info['node_to_cpu'][i]) > 0 and i in node_list: + nodeload[i] = int(nodeload[i] * 16 / len(info['node_to_cpu'][i])) + else: + nodeload[i] = sys.maxint + index = nodeload.index( min(nodeload) ) + return index + + info = xc.physinfo() + if info['nr_nodes'] > 1: + node_memory_list = info['node_to_memory'] + needmem = self.image.getRequiredAvailableMemory(self.info['memory_dynamic_max']) / 1024 + candidate_node_list = [] + for i in range(0, info['nr_nodes']): + if node_memory_list[i] >= needmem and len(info['node_to_cpu'][i]) > 0: + candidate_node_list.append(i) + index = find_relaxed_node(candidate_node_list) + cpumask = info['node_to_cpu'][index] + for v in range(0, self.info['VCPUs_max']): + xc.vcpu_setaffinity(self.domid, v, cpumask) + + def _initDomain(self): log.debug('XendDomainInfo.initDomain: %s %s', self.domid,
@@ -2185,58 +2251,7 @@ # repin domain vcpus if a restricted cpus list is provided # this is done prior to memory allocation to aide in memory # distribution for NUMA systems. - def has_cpus(): - if self.info['cpus'] is not None: - for c in self.info['cpus']: - if c: - return True - return False - - if has_cpus(): - for v in range(0, self.info['VCPUs_max']): - if self.info['cpus'][v]: - xc.vcpu_setaffinity(self.domid, v, self.info['cpus'][v]) - else: - def find_relaxed_node(node_list): - import sys - nr_nodes = info['nr_nodes'] - if node_list is None: - node_list = range(0, nr_nodes) - nodeload = [0] - nodeload = nodeload * nr_nodes - from xen.xend import XendDomain - doms = XendDomain.instance().list('all') - for dom in filter (lambda d: d.domid != self.domid, doms): - cpuinfo = dom.getVCPUInfo() - for vcpu in sxp.children(cpuinfo, 'vcpu'): - if sxp.child_value(vcpu, 'online') == 0: continue - cpumap = list(sxp.child_value(vcpu,'cpumap')) - for i in range(0, nr_nodes): - node_cpumask = info['node_to_cpu'][i] - for j in node_cpumask: - if j in cpumap: - nodeload[i] += 1 - break - for i in range(0, nr_nodes): - if len(info['node_to_cpu'][i]) > 0 and i in node_list: - nodeload[i] = int(nodeload[i] * 16 / len(info['node_to_cpu'][i])) - else: - nodeload[i] = sys.maxint - index = nodeload.index( min(nodeload) ) - return index - - info = xc.physinfo() - if info['nr_nodes'] > 1: - node_memory_list = info['node_to_memory'] - needmem = self.image.getRequiredAvailableMemory(self.info['memory_dynamic_max']) / 1024 - candidate_node_list = [] - for i in range(0, info['nr_nodes']): - if node_memory_list[i] >= needmem and len(info['node_to_cpu'][i]) > 0: - candidate_node_list.append(i) - index = find_relaxed_node(candidate_node_list) - cpumask = info['node_to_cpu'][index] - for v in range(0, self.info['VCPUs_max']): - xc.vcpu_setaffinity(self.domid, v, cpumask) + self._setCPUAffinity() # Use architecture- and image-specific calculations to determine # the various headrooms necessary, given the raw configured
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel