-
-
Notifications
You must be signed in to change notification settings - Fork 598
Compress cgroup names based on some heuristics #849
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9114939
Ensure maximum width for CGROUP column
BenBE b540e95
Filter leading colons in CGROUP name
BenBE 83a73f5
Compress cgroup names based on some heuristics
BenBE 12fd2b1
Extract string writing/buffer handling into some callback
BenBE fc49796
Document CCGROUP column
BenBE 5c3cf78
Use helpers for parsing of cgroup labels
BenBE 10fef93
Include support for legacy LXC cgroup naming
BenBE File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,317 @@ | ||
| /* | ||
| htop - CGroupUtils.h | ||
| (C) 2021 htop dev team | ||
| Released under the GNU GPLv2+, see the COPYING file | ||
| in the source distribution for its full text. | ||
| */ | ||
|
|
||
| #include "linux/CGroupUtils.h" | ||
|
|
||
| #include "XUtils.h" | ||
|
|
||
|
|
||
| typedef struct StrBuf_state { | ||
| char *buf; | ||
| size_t size; | ||
| size_t pos; | ||
| } StrBuf_state; | ||
|
|
||
| typedef bool (*StrBuf_putc_t)(StrBuf_state* p, char c); | ||
|
|
||
| static bool StrBuf_putc_count(StrBuf_state* p, ATTR_UNUSED char c) { | ||
| p->pos++; | ||
| return true; | ||
| } | ||
|
|
||
| static bool StrBuf_putc_write(StrBuf_state* p, char c) { | ||
| if (p->pos >= p->size) | ||
| return false; | ||
|
|
||
| p->buf[p->pos] = c; | ||
| p->pos++; | ||
| return true; | ||
| } | ||
|
|
||
| static bool StrBuf_putsn(StrBuf_state* p, StrBuf_putc_t w, const char* s, size_t count) { | ||
| while (count--) | ||
| if (!w(p, *s++)) | ||
| return false; | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| static bool StrBuf_putsz(StrBuf_state* p, StrBuf_putc_t w, const char* s) { | ||
| while (*s) | ||
| if (!w(p, *s++)) | ||
| return false; | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| static bool Label_checkEqual(const char* labelStart, size_t labelLen, const char* expected) { | ||
| return labelLen == strlen(expected) && String_startsWith(labelStart, expected); | ||
| } | ||
|
|
||
| static bool Label_checkPrefix(const char* labelStart, size_t labelLen, const char* expected) { | ||
| return labelLen > strlen(expected) && String_startsWith(labelStart, expected); | ||
| } | ||
|
|
||
| static bool Label_checkSuffix(const char* labelStart, size_t labelLen, const char* expected) { | ||
| return labelLen > strlen(expected) && String_startsWith(labelStart + labelLen - strlen(expected), expected); | ||
| } | ||
|
|
||
| static bool CGroup_filterName_internal(const char *cgroup, StrBuf_state* s, StrBuf_putc_t w) { | ||
| const char* str_slice_suffix = ".slice"; | ||
| const char* str_system_slice = "system.slice"; | ||
| const char* str_user_slice = "user.slice"; | ||
| const char* str_machine_slice = "machine.slice"; | ||
| const char* str_user_slice_prefix = "/user-"; | ||
|
|
||
| const char* str_lxc_monitor_legacy = "lxc.monitor"; | ||
| const char* str_lxc_payload_legacy = "lxc.payload"; | ||
| const char* str_lxc_monitor_prefix = "lxc.monitor."; | ||
| const char* str_lxc_payload_prefix = "lxc.payload."; | ||
|
|
||
| const char* str_nspawn_scope_prefix = "machine-"; | ||
| const char* str_nspawn_monitor_label = "/supervisor"; | ||
| const char* str_nspawn_payload_label = "/payload"; | ||
|
|
||
| const char* str_service_suffix = ".service"; | ||
| const char* str_scope_suffix = ".scope"; | ||
|
|
||
| while (*cgroup) { | ||
| if ('/' == *cgroup) { | ||
| while ('/' == *cgroup) | ||
| cgroup++; | ||
|
|
||
| if (!w(s, '/')) | ||
| return false; | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| const char* labelStart = cgroup; | ||
| const char* nextSlash = strchrnul(labelStart, '/'); | ||
| const size_t labelLen = nextSlash - labelStart; | ||
|
|
||
| if (Label_checkEqual(labelStart, labelLen, str_system_slice)) { | ||
| cgroup = nextSlash; | ||
|
|
||
| if (!StrBuf_putsz(s, w, "[S]")) | ||
| return false; | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| if (Label_checkEqual(labelStart, labelLen, str_machine_slice)) { | ||
| cgroup = nextSlash; | ||
|
|
||
| if (!StrBuf_putsz(s, w, "[M]")) | ||
| return false; | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| if (Label_checkEqual(labelStart, labelLen, str_user_slice)) { | ||
| cgroup = nextSlash; | ||
|
|
||
| if (!StrBuf_putsz(s, w, "[U]")) | ||
| return false; | ||
|
|
||
| if (!String_startsWith(cgroup, str_user_slice_prefix)) | ||
| continue; | ||
|
|
||
| const char* userSliceSlash = strchrnul(cgroup + strlen(str_user_slice_prefix), '/'); | ||
| const char* sliceSpec = userSliceSlash - strlen(str_slice_suffix); | ||
|
|
||
| if (!String_startsWith(sliceSpec, str_slice_suffix)) | ||
| continue; | ||
|
|
||
| const size_t sliceNameLen = sliceSpec - (cgroup + strlen(str_user_slice_prefix)); | ||
|
|
||
| s->pos--; | ||
| if (!w(s, ':')) | ||
| return false; | ||
|
|
||
| if (!StrBuf_putsn(s, w, cgroup + strlen(str_user_slice_prefix), sliceNameLen)) | ||
| return false; | ||
|
|
||
| if (!w(s, ']')) | ||
| return false; | ||
|
|
||
| cgroup = userSliceSlash; | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| if (Label_checkSuffix(labelStart, labelLen, str_slice_suffix)) { | ||
| const size_t sliceNameLen = labelLen - strlen(str_slice_suffix); | ||
|
|
||
| if (!w(s, '[')) | ||
| return false; | ||
|
|
||
| if (!StrBuf_putsn(s, w, cgroup, sliceNameLen)) | ||
| return false; | ||
|
|
||
| if (!w(s, ']')) | ||
| return false; | ||
|
|
||
| cgroup = nextSlash; | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| if (Label_checkPrefix(labelStart, labelLen, str_lxc_payload_prefix)) { | ||
| const size_t cgroupNameLen = labelLen - strlen(str_lxc_payload_prefix); | ||
|
|
||
| if (!StrBuf_putsz(s, w, "[lxc:")) | ||
| return false; | ||
|
|
||
| if (!StrBuf_putsn(s, w, cgroup + strlen(str_lxc_payload_prefix), cgroupNameLen)) | ||
| return false; | ||
|
|
||
| if (!w(s, ']')) | ||
| return false; | ||
|
|
||
| cgroup = nextSlash; | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| if (Label_checkPrefix(labelStart, labelLen, str_lxc_monitor_prefix)) { | ||
| const size_t cgroupNameLen = labelLen - strlen(str_lxc_monitor_prefix); | ||
|
|
||
| if (!StrBuf_putsz(s, w, "[LXC:")) | ||
| return false; | ||
|
|
||
| if (!StrBuf_putsn(s, w, cgroup + strlen(str_lxc_monitor_prefix), cgroupNameLen)) | ||
| return false; | ||
|
|
||
| if (!w(s, ']')) | ||
| return false; | ||
|
|
||
| cgroup = nextSlash; | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| // LXC legacy cgroup naming | ||
| if (Label_checkEqual(labelStart, labelLen, str_lxc_monitor_legacy) || | ||
| Label_checkEqual(labelStart, labelLen, str_lxc_payload_legacy)) { | ||
| bool isMonitor = Label_checkEqual(labelStart, labelLen, str_lxc_monitor_legacy); | ||
|
|
||
| labelStart = nextSlash; | ||
| while (*labelStart == '/') | ||
| labelStart++; | ||
|
|
||
| nextSlash = strchrnul(labelStart, '/'); | ||
| if (nextSlash - labelStart > 0) { | ||
| if (!StrBuf_putsz(s, w, isMonitor ? "[LXC:" : "[lxc:")) | ||
| return false; | ||
|
|
||
| if (!StrBuf_putsn(s, w, labelStart, nextSlash - labelStart)) | ||
| return false; | ||
|
|
||
| if (!w(s, ']')) | ||
| return false; | ||
|
|
||
| cgroup = nextSlash; | ||
| continue; | ||
| } | ||
|
|
||
| labelStart = cgroup; | ||
| nextSlash = labelStart + labelLen; | ||
| } | ||
|
|
||
| if (Label_checkSuffix(labelStart, labelLen, str_service_suffix)) { | ||
| const size_t serviceNameLen = labelLen - strlen(str_service_suffix); | ||
|
|
||
| if (String_startsWith(cgroup, "user@")) { | ||
| cgroup = nextSlash; | ||
|
|
||
| while(*cgroup == '/') | ||
| cgroup++; | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| if (!StrBuf_putsn(s, w, cgroup, serviceNameLen)) | ||
| return false; | ||
|
|
||
| cgroup = nextSlash; | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| if (Label_checkSuffix(labelStart, labelLen, str_scope_suffix)) { | ||
| const size_t scopeNameLen = labelLen - strlen(str_scope_suffix); | ||
|
|
||
| if (Label_checkPrefix(labelStart, scopeNameLen, str_nspawn_scope_prefix)) { | ||
| const size_t machineScopeNameLen = scopeNameLen - strlen(str_nspawn_scope_prefix); | ||
|
|
||
| const bool is_monitor = String_startsWith(nextSlash, str_nspawn_monitor_label); | ||
|
|
||
| if (!StrBuf_putsz(s, w, is_monitor ? "[SNC:" : "[snc:")) | ||
| return false; | ||
|
|
||
| if (!StrBuf_putsn(s, w, cgroup + strlen(str_nspawn_scope_prefix), machineScopeNameLen)) | ||
| return false; | ||
|
|
||
| if (!w(s, ']')) | ||
| return false; | ||
|
|
||
| cgroup = nextSlash; | ||
| if (String_startsWith(nextSlash, str_nspawn_monitor_label)) | ||
| cgroup += strlen(str_nspawn_monitor_label); | ||
| else if (String_startsWith(nextSlash, str_nspawn_payload_label)) | ||
| cgroup += strlen(str_nspawn_payload_label); | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| if (!w(s, '!')) | ||
| return false; | ||
|
|
||
| if (!StrBuf_putsn(s, w, cgroup, scopeNameLen)) | ||
| return false; | ||
|
|
||
| cgroup = nextSlash; | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| // Default behavior: Copy the full label | ||
| cgroup = labelStart; | ||
|
|
||
| if (!StrBuf_putsn(s, w, cgroup, labelLen)) | ||
| return false; | ||
|
|
||
| cgroup = nextSlash; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| char* CGroup_filterName(const char *cgroup) { | ||
| StrBuf_state s = { | ||
| .buf = NULL, | ||
| .size = 0, | ||
| .pos = 0, | ||
| }; | ||
|
|
||
| if (!CGroup_filterName_internal(cgroup, &s, StrBuf_putc_count)) { | ||
| return NULL; | ||
| } | ||
|
|
||
| s.buf = xCalloc(s.pos + 1, sizeof(char)); | ||
| s.size = s.pos; | ||
| s.pos = 0; | ||
|
|
||
| if (!CGroup_filterName_internal(cgroup, &s, StrBuf_putc_write)) { | ||
| free(s.buf); | ||
| return NULL; | ||
| } | ||
|
|
||
| s.buf[s.size] = '\0'; | ||
| return s.buf; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| #ifndef HEADER_CGroupUtils | ||
| #define HEADER_CGroupUtils | ||
| /* | ||
| htop - CGroupUtils.h | ||
| (C) 2021 htop dev team | ||
| Released under the GNU GPLv2+, see the COPYING file | ||
| in the source distribution for its full text. | ||
| */ | ||
|
|
||
| #include <stdbool.h> | ||
| #include <stddef.h> | ||
|
|
||
|
|
||
| char* CGroup_filterName(const char *cgroup); | ||
|
|
||
| #endif /* HEADER_CGroupUtils */ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.