Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements in setup of cgroups #1896

Merged
merged 7 commits into from
May 30, 2020
Merged

Improvements in setup of cgroups #1896

merged 7 commits into from
May 30, 2020

Conversation

narrieta
Copy link
Member

Previously the cgroups code would make several assumptions as to how cgroups were setup.

These changes discover the current setup instead of making those assumptions. Also, it sends telemetry for important parts of that setup.

@codecov
Copy link

codecov bot commented May 28, 2020

Codecov Report

Merging #1896 into develop will decrease coverage by 0.19%.
The diff coverage is 77.29%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #1896      +/-   ##
===========================================
- Coverage    69.62%   69.42%   -0.20%     
===========================================
  Files           85       85              
  Lines        11723    11788      +65     
  Branches      1631     1653      +22     
===========================================
+ Hits          8162     8184      +22     
- Misses        3214     3239      +25     
- Partials       347      365      +18     
Impacted Files Coverage Δ
azurelinuxagent/common/osutil/default.py 57.89% <ø> (-1.24%) ⬇️
azurelinuxagent/common/osutil/ubuntu.py 68.49% <ø> (-0.34%) ⬇️
azurelinuxagent/daemon/main.py 72.16% <ø> (-0.57%) ⬇️
azurelinuxagent/ga/exthandlers.py 86.13% <ø> (-0.03%) ⬇️
azurelinuxagent/common/cgroupconfigurator.py 74.60% <64.38%> (-15.62%) ⬇️
azurelinuxagent/common/cgroupapi.py 81.42% <84.90%> (-3.74%) ⬇️
azurelinuxagent/common/cgroupstelemetry.py 86.81% <100.00%> (ø)
azurelinuxagent/common/event.py 85.93% <100.00%> (-0.41%) ⬇️
azurelinuxagent/ga/update.py 87.59% <100.00%> (-0.04%) ⬇️
... and 2 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ca6a53d...9c2a202. Read the comment docs.

@narrieta narrieta changed the title Improvement in setup of cgroups Improvements in setup of cgroups May 28, 2020
distro_version = FlexibleVersion(distro_info[1])
except ValueError:
return False
return distro_name.lower() == 'ubuntu' and distro_version.major >= 16
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am focusing on Ubuntu 16, 18 & 20 so this is hardcoded for now

@@ -202,6 +212,58 @@ def _add_process_to_cgroup(pid, cgroup_path):
fileutil.append_file(tasks_file, "{0}\n".format(pid))
logger.info("Added PID {0} to cgroup {1}".format(pid, cgroup_path))

@staticmethod
Copy link
Member Author

@narrieta narrieta May 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved this from osutils (though it is not being used right now, I may need it when adding support for non-systemd distros)

@@ -386,23 +538,6 @@ def _get_extensions_slice_root_name():
def _get_extension_slice_name(self, extension_name):
return "system-{0}-{1}.slice".format(EXTENSIONS_ROOT_CGROUP_NAME, self._get_extension_cgroup_name(extension_name))

def create_agent_cgroups(self):
Copy link
Member Author

@narrieta narrieta May 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

systemd creates the cgroups for the agent so this function was misleading. now, CgroupConfigurator.initialize() discovers the cgroups for the agent and starts tracking for them

@@ -149,9 +148,6 @@ def daemon(self, child_args=None):
# that require the goal state and IMDS
self._initialize_telemetry()

# Initialize the agent cgroup
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am doing all the cgroups stuff in the extension handler. Systemd does all the setup and the handler simply discovers the setup and starts tracking the cgroups. When I get to non-systemd distros I will have to do some of the setup in the daemon, but for now it is not needed.

@@ -688,7 +689,7 @@ def initialize_event_logger_vminfo_common_parameters(protocol, reporter=__event_
reporter.initialize_vminfo_common_parameters(protocol)


def add_event(name, op=WALAEventOperation.Unknown, is_success=True, duration=0, version=str(CURRENT_VERSION),
def add_event(name=AGENT_NAME, op=WALAEventOperation.Unknown, is_success=True, duration=0, version=str(CURRENT_VERSION),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I defaulted the 'name' parameter to the agent; makes the calls to add_event a little more readable



class DefaultOsUtilTestCase(AgentTestCase):

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved those to test_cgroupsapi.py/MountCgroupsTestCase

pgombar
pgombar previously approved these changes May 28, 2020
Copy link
Contributor

@pgombar pgombar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with a couple of minor comments

#
self._cgroups_supported = CGroupsApi.cgroups_supported()
if not self._cgroups_supported:
logger.info("Cgroup monitoring is not supported on {0}", get_distro())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want a telemetry event for this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The list of supported distros is hardcoded (ubuntu 16, 18, 20) so no need for telemetry

if memory_cgroup_relative_path is None:
log_cgroup_warn("The agent's process is not within a memory cgroup")
else:
cpu_accounting = self._cgroups_api.get_unit_property(agent_unit_name, "MemoryAccounting")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memory_accounting instead of cpu_accounting?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

configurator = CGroupConfigurator.get_instance()
if initialize:
with patch('azurelinuxagent.common.cgroupapi.CGroupsApi.cgroups_supported', return_value=True):
with patch('azurelinuxagent.common.cgroupapi.CGroupsApi._is_systemd', return_value=True):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before this PR, we were forcing FileSystemCgroupsApi here. Any reason for the change? I forget why filesystem was favoured before.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, with these changes FileSystemCgroupsApi is dead code. I do not want to delete it yet since we will eventually get to distros without systemd but for now all the code is going with the systemd path. I updated these tests to follow the same

message=status,
log_event=False)
except Exception as e:
message = "Error initializing cgroups: {0}".format(ustr(e))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if needed, but shouldn't we mark self._cgroups_enabled = False here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initially I was setting enabled to True at the very end of the try clause, but then I realized cgroups should be ready to use if we reach line 133 so I moved the assignment there. If for any reason we cannot track the agent cgroups we should still track extensions; if for any reason we cannot track the agent's memory cgroup we should still track its cpu cgroup

Copy link
Contributor

@larohra larohra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 small question, else everything LGTM

def mock_popen(command, *args, **kwargs):
if isinstance(command, list):
command_string = " ".join(command)
for c in _default_commands:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious to know why not just use a dict inplace of a list for _default_commands. Would definitely save you the trouble of walking the list everytime

Copy link
Member Author

@narrieta narrieta May 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initially it was a dict, but then i realized strict string comparison was not enough for I need to do and changed it to a sequential list of regular expressions so each time I do a sequential match.

now, most of those regular expressions are simple strings so at some point I considered having a dict search followed by a sequential regex match, but the tests actually run fast enough as it is, so I gave up the idea.

@narrieta narrieta merged commit c881efb into Azure:develop May 30, 2020
@narrieta narrieta deleted the cgroups branch May 30, 2020 17:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants