Skip to content

Commit

Permalink
Merge pull request #14 from nick-wang/PM-3309-ignore-SAP.slice
Browse files Browse the repository at this point in the history
jsc#SLE-23788, Enhance WMP for a smooth handover to SAP's systemd
  • Loading branch information
nick-wang authored Feb 18, 2022
2 parents 0992962 + 9466dcf commit 6b06c5c
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 36 deletions.
76 changes: 47 additions & 29 deletions scripts/wmp-check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@
# 19.04.2021 v1.1.2 Remove sapstartsrv from process tree to capture
# Enable support for SLE15SP3
# 1.11.2021 Enable support for SLE15SP4
# 2.14.2022 v1.1.3 Adjust checker for SAP systemd integration

version="1.1.2"
version="1.1.3"

# We use these global arrays through out the program:
#
Expand Down Expand Up @@ -281,23 +282,26 @@ function get_SAP_slice_data() {
SAP_slice_data['memory.low']=${SAP_slice_data['memory.low']:=0}
SAP_slice_data['memory.current']=$(cat "/sys/fs/cgroup/${SAP_slice_data['name']}/memory.current" 2>/dev/null)
SAP_slice_data['memory.current']=${SAP_slice_data['memory.current']:=0}
SAP_slice_data['unprotect_list']=${SAP_slice_data['unprotect_list']:=''}

error=0
while read path ; do
if [ -e "${path}/memory.low" ] ; then
while read path ; do
if [ ! -e "${path}/memory.low" ] || [ "$(< ${path}/memory.low)" != 'max' ]; then
if [ ${#SAP_slice_data['unprotect_list']} -ne 0 ]; then
SAP_slice_data['unprotect_list']+=" ${path##*/}"
else
SAP_slice_data['unprotect_list']+="${path##*/}"
fi
((error++))
continue
fi
if [ "$(< ${path}/memory.low)" != 'max' ] ; then
((error++))
continue
fi
done < <(find "/sys/fs/cgroup/${SAP_slice_data['name']}/" -mindepth 1 -type d)
if [ ${error} -ne 0 ] ; then
SAP_slice_data['memory_low_children']='ok'
else

if [ ${error} -ne 0 ] ; then
SAP_slice_data['memory_low_children']='false'
else
SAP_slice_data['memory_low_children']='ok'
fi

else
SAP_slice_data['exists']='no'
fi
Expand Down Expand Up @@ -651,14 +655,23 @@ function check_wmp() {
((fails++))
;;
esac
case "${SAP_slice_data['memory_low_children']}" in
case "${SAP_slice_data['memory_low_children']}" in
false)
print_fail "Subcgroups of ${SAP_slice_data['name']} have either no MemoryLow setting or are not set to maximum!" "Check what has changed the parameters and restart the SAP instances."
((fails++))
;;
eval $(grep ^VERSION= /etc/os-release)

case "${VERSION}" in
15-SP4)
print_ok "In SLE15SP4, subcgroups of ${SAP_slice_data['name']} without MemoryLow setting is acceptable."
;;
*)
print_fail "Subcgroups (${SAP_slice_data['unprotect_list']}) of ${SAP_slice_data['name']} have either no MemoryLow setting or are not set to maximum!" "Refer to the SUSE doc and check what has changed the parameters then restart the SAP instances."
((fails++))
;;
esac

esac
;;
no)
no)
print_fail "Unit file for ${SAP_slice_data['name']} is missing!" "Reinstall the sapwmp package."
((fails++))
;;
Expand All @@ -685,21 +698,26 @@ function check_wmp() {

# Check if MemoryLow of the SAP slice is lower the memory.current and less then 3% as physical memory.
if [ -n "${SAP_slice_data['MemoryLow']}" ] ; then
if [ ${SAP_slice_data['MemoryLow']} -gt ${SAP_slice_data['memory.current']} ] ; then
print_ok "MemoryLow is larger then the current allocated memory for ${SAP_slice_data['name']}."
else
print_fail "MemoryLow (${SAP_slice_data['MemoryLow']}) is smaller then the current allocated memory (${SAP_slice_data['memory.current']}) for ${SAP_slice_data['name']}!" "Check if this is an expected situation."
((fails++))
fi
if [ ${SAP_slice_data['MemoryLow']} -lt ${memory_info['MemTotal']} ] ; then
print_ok "MemoryLow of ${SAP_slice_data['name']} is less then total memory."
memory_low_thresh=$(( ${memory_info['MemTotal']} * 97 / 100 ))
if [ ${SAP_slice_data['MemoryLow']} -gt ${memory_low_thresh} ] ; then
print_warn "MemoryLow of ${SAP_slice_data['name']} (${SAP_slice_data['MemoryLow']}) is very close to the total physical memory (${memory_info['MemTotal']})!"
((warnings++))
if grep -q '^[[:digit:]]*$' <<< "${SAP_slice_data['MemoryLow']}";then
if [ ${SAP_slice_data['MemoryLow']} -gt ${SAP_slice_data['memory.current']} ] ; then
print_ok "MemoryLow is larger then the current allocated memory for ${SAP_slice_data['name']}."
else
print_fail "MemoryLow (${SAP_slice_data['MemoryLow']}) is smaller then the current allocated memory (${SAP_slice_data['memory.current']}) for ${SAP_slice_data['name']}!" "Check if this is an expected situation."
((fails++))
fi
if [ ${SAP_slice_data['MemoryLow']} -lt ${memory_info['MemTotal']} ] ; then
print_ok "MemoryLow of ${SAP_slice_data['name']} is less then total memory."
memory_low_thresh=$(( ${memory_info['MemTotal']} * 97 / 100 ))
if [ ${SAP_slice_data['MemoryLow']} -gt ${memory_low_thresh} ] ; then
print_warn "MemoryLow of ${SAP_slice_data['name']} (${SAP_slice_data['MemoryLow']}) is very close to the total physical memory (${memory_info['MemTotal']})!"
((warnings++))
fi
else
print_fail "MemoryLow of ${SAP_slice_data['name']} (${SAP_slice_data['MemoryLow']}) is not less then the total physical memory (${memory_info['MemTotal']})!" "Reduce MemoryLow."
((fails++))
fi
else
print_fail "MemoryLow of ${SAP_slice_data['name']} (${SAP_slice_data['MemoryLow']}) is not less then the total physical memory (${memory_info['MemTotal']})!" "Reduce MemoryLow."
print_fail "MemoryLow of ${SAP_slice_data['name']} (${SAP_slice_data['MemoryLow']}) is not a numeric value!" "Configure MemoryLow to a number."
((fails++))
fi
else
Expand Down
67 changes: 60 additions & 7 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,40 @@ static inline void freep(void *p) {
free(*(void **)p);
}

int already_in_slice(pid_t pid, const char *target_slice) {
char path[PATH_MAX];
char buf[PATH_MAX];
char pattern[UNIT_NAME_LEN + 2];
FILE *f;
int r;

r = snprintf(path, PATH_MAX, "/proc/%i/cgroup", pid);
if (r < 0)
return r;

r = snprintf(pattern, UNIT_NAME_LEN + 2, "/%s/", target_slice);
if (r < 0)
return r;

f = fopen(path, "r");
if (!f)
return -errno;

r = 0;
while (fgets(buf, PATH_MAX, f)) {
if (strncmp(buf, "0:", 2) != 0)
continue;

if (strstr(buf, pattern)) {
r = 1;
break;
}
}

fclose(f);
return r;
}

int migrate(sd_bus *bus, const char *target_unit, const char *target_slice,
struct unit_config *properties,
size_t n_pids, pid_t *pids) {
Expand Down Expand Up @@ -178,8 +212,9 @@ int read_stat(pid_t pid, pid_t *ppid, char *rcomm) {
return r;
}

int collect_pids(pid_t **rpids) {
int collect_pids(pid_t **rpids, int force) {
int n_pids = 0;
int r;
pid_t pid, ppid;
char comm[TASK_COMM_LEN + 1];
pid_t *pids;
Expand All @@ -195,13 +230,27 @@ int collect_pids(pid_t **rpids) {

pid = getppid();
while (pid > 1 && n_pids < MAX_PIDS) {
if (read_stat(pid, &ppid, comm))
if (read_stat(pid, &ppid, comm)) {
r = -ESRCH;
goto err;
}

if (verbose)
log_debug("%10i: %s", pid, comm);

for (char **p = config.parent_commands.list; *p; p++) {
if(!strcmp(comm, *p)) {
if (!force) {
r = already_in_slice(pid, config.slice);

if (r < 0)
goto err;
else if (r == 1) {
log_info("Found pid(%d) already in %s", pid, config.slice);
break;
}
}

pids[n_pids++] = pid;
break;
}
Expand All @@ -216,7 +265,7 @@ int collect_pids(pid_t **rpids) {

err:
free(pids);
return -ESRCH;
return r;
}

static int make_scope_name(char *buf) {
Expand All @@ -237,25 +286,26 @@ static int make_scope_name(char *buf) {
}

static void print_help(const char *name) {
fprintf(stderr, "Usage: %s [-hv] -a\n", name);
fprintf(stderr, "Usage: %s [-hvf] -a\n", name);
fprintf(stderr, " Similar to systemd-run --scope, logs into syslog unless stderr is a TTY\n");
fprintf(stderr, "\n");
fprintf(stderr, " -h Show this help\n");
fprintf(stderr, " -v Verbose logging (for debugging)\n");
fprintf(stderr, " -f Force capture all valid processes(including systemd launched)\n");
fprintf(stderr, " -a Put chosen ancestor processes under WMP scope\n");
}

int main(int argc, char *argv[]) {
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
_cleanup_(freep) pid_t *pids = NULL;
char unit_name[UNIT_NAME_LEN];
int opt, ancestors = 0;
int opt, ancestors = 0, force = 0;
int n_pids;
int r;

log_init();

while ((opt = getopt(argc, argv, "ahv")) != -1) {
while ((opt = getopt(argc, argv, "ahvf")) != -1) {
switch (opt) {
case 'a':
ancestors = 1;
Expand All @@ -266,6 +316,9 @@ int main(int argc, char *argv[]) {
case 'v':
verbose = 1;
break;
case 'f':
force = 1;
break;
default:
log_error("Unknown option(s), use -h for help");
}
Expand All @@ -285,7 +338,7 @@ int main(int argc, char *argv[]) {
if (r < 0)
exit_error(EXIT_FAILURE, r, "Failed loading config from '%s'", CONF_FILE);

n_pids = collect_pids(&pids);
n_pids = collect_pids(&pids, force);
if (n_pids < 0)
exit_error(EXIT_FAILURE, n_pids, "Failed collecting PIDs");
else if (n_pids == 0)
Expand Down

0 comments on commit 6b06c5c

Please sign in to comment.