-
Notifications
You must be signed in to change notification settings - Fork 366
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
Conversation
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
distro_version = FlexibleVersion(distro_info[1]) | ||
except ValueError: | ||
return False | ||
return distro_name.lower() == 'ubuntu' and distro_version.major >= 16 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
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): | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this 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()) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this 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: |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
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.