Commit ef1faf0e authored by Jianlin Lv's avatar Jianlin Lv Committed by Andrew Morton

tools/vm/page_owner_sort: free memory before exit

Although when a process terminates, the kernel will removes memory
associated with that process, It's neither good style nor proper design to
leave it to kernel.  This patch free allocated memory before process exit.

Link: https://lkml.kernel.org/r/20221219164917.14132-1-iecedge@gmail.comSigned-off-by: default avatarJianlin Lv <iecedge@gmail.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
parent 44383cef
...@@ -246,15 +246,16 @@ static int search_pattern(regex_t *pattern, char *pattern_str, char *buf) ...@@ -246,15 +246,16 @@ static int search_pattern(regex_t *pattern, char *pattern_str, char *buf)
return 0; return 0;
} }
static void check_regcomp(regex_t *pattern, const char *regex) static bool check_regcomp(regex_t *pattern, const char *regex)
{ {
int err; int err;
err = regcomp(pattern, regex, REG_EXTENDED | REG_NEWLINE); err = regcomp(pattern, regex, REG_EXTENDED | REG_NEWLINE);
if (err != 0 || pattern->re_nsub != 1) { if (err != 0 || pattern->re_nsub != 1) {
fprintf(stderr, "Invalid pattern %s code %d\n", regex, err); fprintf(stderr, "Invalid pattern %s code %d\n", regex, err);
exit(1); return false;
} }
return true;
} }
static char **explode(char sep, const char *str, int *size) static char **explode(char sep, const char *str, int *size)
...@@ -494,28 +495,28 @@ static bool is_need(char *buf) ...@@ -494,28 +495,28 @@ static bool is_need(char *buf)
return true; return true;
} }
static void add_list(char *buf, int len, char *ext_buf) static bool add_list(char *buf, int len, char *ext_buf)
{ {
if (list_size != 0 && if (list_size != 0 &&
len == list[list_size-1].len && len == list[list_size-1].len &&
memcmp(buf, list[list_size-1].txt, len) == 0) { memcmp(buf, list[list_size-1].txt, len) == 0) {
list[list_size-1].num++; list[list_size-1].num++;
list[list_size-1].page_num += get_page_num(buf); list[list_size-1].page_num += get_page_num(buf);
return; return true;
} }
if (list_size == max_size) { if (list_size == max_size) {
fprintf(stderr, "max_size too small??\n"); fprintf(stderr, "max_size too small??\n");
exit(1); return false;
} }
if (!is_need(buf)) if (!is_need(buf))
return; return true;
list[list_size].pid = get_pid(buf); list[list_size].pid = get_pid(buf);
list[list_size].tgid = get_tgid(buf); list[list_size].tgid = get_tgid(buf);
list[list_size].comm = get_comm(buf); list[list_size].comm = get_comm(buf);
list[list_size].txt = malloc(len+1); list[list_size].txt = malloc(len+1);
if (!list[list_size].txt) { if (!list[list_size].txt) {
fprintf(stderr, "Out of memory\n"); fprintf(stderr, "Out of memory\n");
exit(1); return false;
} }
memcpy(list[list_size].txt, buf, len); memcpy(list[list_size].txt, buf, len);
list[list_size].txt[len] = 0; list[list_size].txt[len] = 0;
...@@ -534,6 +535,7 @@ static void add_list(char *buf, int len, char *ext_buf) ...@@ -534,6 +535,7 @@ static void add_list(char *buf, int len, char *ext_buf)
printf("loaded %d\r", list_size); printf("loaded %d\r", list_size);
fflush(stdout); fflush(stdout);
} }
return true;
} }
static bool parse_cull_args(const char *arg_str) static bool parse_cull_args(const char *arg_str)
...@@ -790,12 +792,19 @@ int main(int argc, char **argv) ...@@ -790,12 +792,19 @@ int main(int argc, char **argv)
exit(1); exit(1);
} }
check_regcomp(&order_pattern, "order\\s*([0-9]*),"); if (!check_regcomp(&order_pattern, "order\\s*([0-9]*),"))
check_regcomp(&pid_pattern, "pid\\s*([0-9]*),"); goto out_order;
check_regcomp(&tgid_pattern, "tgid\\s*([0-9]*) "); if (!check_regcomp(&pid_pattern, "pid\\s*([0-9]*),"))
check_regcomp(&comm_pattern, "tgid\\s*[0-9]*\\s*\\((.*)\\),\\s*ts"); goto out_pid;
check_regcomp(&ts_nsec_pattern, "ts\\s*([0-9]*)\\s*ns,"); if (!check_regcomp(&tgid_pattern, "tgid\\s*([0-9]*) "))
check_regcomp(&free_ts_nsec_pattern, "free_ts\\s*([0-9]*)\\s*ns"); goto out_tgid;
if (!check_regcomp(&comm_pattern, "tgid\\s*[0-9]*\\s*\\((.*)\\),\\s*ts"))
goto out_comm;
if (!check_regcomp(&ts_nsec_pattern, "ts\\s*([0-9]*)\\s*ns,"))
goto out_ts;
if (!check_regcomp(&free_ts_nsec_pattern, "free_ts\\s*([0-9]*)\\s*ns"))
goto out_free_ts;
fstat(fileno(fin), &st); fstat(fileno(fin), &st);
max_size = st.st_size / 100; /* hack ... */ max_size = st.st_size / 100; /* hack ... */
...@@ -804,7 +813,7 @@ int main(int argc, char **argv) ...@@ -804,7 +813,7 @@ int main(int argc, char **argv)
ext_buf = malloc(BUF_SIZE); ext_buf = malloc(BUF_SIZE);
if (!list || !buf || !ext_buf) { if (!list || !buf || !ext_buf) {
fprintf(stderr, "Out of memory\n"); fprintf(stderr, "Out of memory\n");
exit(1); goto out_free;
} }
for ( ; ; ) { for ( ; ; ) {
...@@ -812,7 +821,8 @@ int main(int argc, char **argv) ...@@ -812,7 +821,8 @@ int main(int argc, char **argv)
if (buf_len < 0) if (buf_len < 0)
break; break;
add_list(buf, buf_len, ext_buf); if (!add_list(buf, buf_len, ext_buf))
goto out_free;
} }
printf("loaded %d\n", list_size); printf("loaded %d\n", list_size);
...@@ -862,11 +872,26 @@ int main(int argc, char **argv) ...@@ -862,11 +872,26 @@ int main(int argc, char **argv)
fprintf(fout, "\n"); fprintf(fout, "\n");
} }
} }
regfree(&order_pattern);
regfree(&pid_pattern); out_free:
regfree(&tgid_pattern); if (ext_buf)
regfree(&comm_pattern); free(ext_buf);
regfree(&ts_nsec_pattern); if (buf)
free(buf);
if (list)
free(list);
out_free_ts:
regfree(&free_ts_nsec_pattern); regfree(&free_ts_nsec_pattern);
out_ts:
regfree(&ts_nsec_pattern);
out_comm:
regfree(&comm_pattern);
out_tgid:
regfree(&tgid_pattern);
out_pid:
regfree(&pid_pattern);
out_order:
regfree(&order_pattern);
return 0; return 0;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment