Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
nagworld9 committed Nov 25, 2024
1 parent 9656c78 commit 0149c5a
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 179 deletions.
35 changes: 17 additions & 18 deletions azurelinuxagent/ga/cgroupconfigurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ def _calculate_current_cpu_quota(cpu_quota_per_sec_usec):
return "{0}%".format(cpu_percentage)
except Exception as e:
log_cgroup_warning("Error parsing current CPUQuotaPerSecUSec: {0}".format(ustr(e)))
return ""
return "unknown"

def supported(self):
return self._cgroups_supported
Expand Down Expand Up @@ -845,21 +845,22 @@ def _reset_extension_cpu_quota(self, extension_name):
self._reset_cpu_quota(CGroupUtil.get_extension_slice_name(extension_name))

@staticmethod
def _is_cgroups_setup_completed(unit_name, cpu_quota=""):
def get_unit_properties_requiring_update(unit_name, cpu_quota=""):
"""
Check if the cgroups setup is completed for the unit.
Check if the cgroups setup is completed for the unit and return the properties that need an update.
"""
properties_to_update = []
cpu_accounting = systemd.get_unit_property(unit_name, "CPUAccounting")
if cpu_accounting != "yes":
return False
properties_to_update.append("CPUAccounting=yes")
memory_accounting = systemd.get_unit_property(unit_name, "MemoryAccounting")
if memory_accounting != "yes":
return False
properties_to_update.append("MemoryAccounting=yes")
cpu_quota_per_sec_usec = systemd.get_unit_property(unit_name, "CPUQuotaPerSecUSec")
current_cpu_quota = CGroupConfigurator._Impl._calculate_current_cpu_quota(cpu_quota_per_sec_usec)
if current_cpu_quota != cpu_quota:
return False
return True
properties_to_update.append("CPUQuota={0}".format(cpu_quota))
return properties_to_update

def setup_extension_slice(self, extension_name, cpu_quota):
"""
Expand Down Expand Up @@ -887,16 +888,16 @@ def setup_extension_slice(self, extension_name, cpu_quota):

cpu_quota = "{0}%".format(
cpu_quota) if cpu_quota is not None else "" # setting an empty value resets to the default (infinity)
if not self._is_cgroups_setup_completed(extension_slice, cpu_quota):
slice_properties = ["CPUAccounting=yes", "MemoryAccounting=yes"]
properties_to_update = self.get_unit_properties_requiring_update(extension_slice, cpu_quota)

if len(properties_to_update) > 0:
if cpu_quota == "":
log_cgroup_info("CPUQuota not set for {0}".format(extension_name))
else:
log_cgroup_info("Ensuring the {0}'s CPUQuota is {1}".format(extension_name, cpu_quota))
slice_properties.append("CPUQuota={0}".format(cpu_quota))

log_cgroup_info("Setting up the resource properties: {0} for {1}".format(slice_properties, extension_slice))
systemd.set_unit_properties_run_time(extension_slice, slice_properties)
log_cgroup_info("Setting up the resource properties: {0} for {1}".format(properties_to_update, extension_slice))
systemd.set_unit_properties_run_time(extension_slice, properties_to_update)

except Exception as exception:
log_cgroup_warning("Failed to set the extension {0} slice and quotas: {1}".format(extension_slice,
Expand Down Expand Up @@ -943,17 +944,15 @@ def set_extension_services_cpu_memory_quota(self, services_list):

cpu_quota = service.get('cpuQuotaPercentage')
cpu_quota = "{0}%".format(cpu_quota) if cpu_quota is not None else "" # setting an empty value resets to the default (infinity)
if systemd.is_unit_loaded(service_name) and not self._is_cgroups_setup_completed(service_name, cpu_quota):
service_properties = ["CPUAccounting=yes", "MemoryAccounting=yes"]
properties_to_update = self.get_unit_properties_requiring_update(service_name, cpu_quota)
if systemd.is_unit_loaded(service_name) and len(properties_to_update) > 0:
if cpu_quota != "":
log_cgroup_info("Ensuring the {0}'s CPUQuota is {1}".format(service_name, cpu_quota))
service_properties.append("CPUQuota={0}".format(cpu_quota))
else:
log_cgroup_info("CPUQuota not set for {0}".format(service_name))

log_cgroup_info("Setting up resource properties: {0} for {1}" .format(service_properties, service_name))
log_cgroup_info("Setting up resource properties: {0} for {1}" .format(properties_to_update, service_name))
try:
systemd.set_unit_properties_run_time(service_name, service_properties)
systemd.set_unit_properties_run_time(service_name, properties_to_update)
except Exception as exception:
log_cgroup_warning("Failed to set the quotas for {0}: {1}".format(service_name, ustr(exception)))

Expand Down
Loading

0 comments on commit 0149c5a

Please sign in to comment.