Skip to content

Commit

Permalink
tools/vm/page_owner_sort: fix three trivival places
Browse files Browse the repository at this point in the history
The following adjustments are made:

1. Instead of using another array to cull the blocks after sorting,
   reuse the old array.  So there is no need to malloc a new array.

2. When enabling '-f' option to filter out the blocks which have been
   released, only add those have not been released in the list, rather
   than add all of blocks in the list and then do the filtering when
   printing the result.

3. When enabling '-c' option to cull the blocks by comparing
   stacktrace, print the stacetrace rather than the total block.

Link: https://lkml.kernel.org/r/[email protected]
Signed-off-by: Jiajian Ye <[email protected]>
Cc: <[email protected]>
Cc: Sean Anderson <[email protected]>
Cc: Stephen Rothwell <[email protected]>
Cc: Yixuan Cao <[email protected]>
Cc: <[email protected]>
Cc: Zhenliang Wei <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Leo2022Leo authored and torvalds committed Mar 25, 2022
1 parent cf3c2c8 commit 578d8f2
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions tools/vm/page_owner_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ static regex_t free_ts_nsec_pattern;
static struct block_list *list;
static int list_size;
static int max_size;
static int cull_st;
static int filter;

int read_block(char *buf, int buf_size, FILE *fin)
{
Expand Down Expand Up @@ -245,6 +247,9 @@ static void add_list(char *buf, int len)
exit(1);
}

list[list_size].free_ts_nsec = get_free_ts_nsec(buf);
if (filter == 1 && list[list_size].free_ts_nsec != 0)
return;
list[list_size].txt = malloc(len+1);
if (!list[list_size].txt) {
printf("Out of memory\n");
Expand All @@ -257,10 +262,11 @@ static void add_list(char *buf, int len)
memcpy(list[list_size].txt, buf, len);
list[list_size].txt[len] = 0;
list[list_size].stacktrace = strchr(list[list_size].txt, '\n') ?: "";
if (*list[list_size].stacktrace == '\n')
list[list_size].stacktrace++;
list[list_size].pid = get_pid(buf);
list[list_size].tgid = get_tgid(buf);
list[list_size].ts_nsec = get_ts_nsec(buf);
list[list_size].free_ts_nsec = get_free_ts_nsec(buf);
list_size++;
if (list_size % 1000 == 0) {
printf("loaded %d\r", list_size);
Expand Down Expand Up @@ -288,12 +294,9 @@ static void usage(void)
int main(int argc, char **argv)
{
int (*cmp)(const void *, const void *) = compare_num;
int cull_st = 0;
int filter = 0;
FILE *fin, *fout;
char *buf;
int ret, i, count;
struct block_list *list2;
struct stat st;
int opt;

Expand Down Expand Up @@ -376,33 +379,31 @@ int main(int argc, char **argv)
else
qsort(list, list_size, sizeof(list[0]), compare_txt);

list2 = malloc(sizeof(*list) * list_size);
if (!list2) {
printf("Out of memory\n");
exit(1);
}


printf("culling\n");

long offset = cull_st ? &list[0].stacktrace - &list[0].txt : 0;

for (i = count = 0; i < list_size; i++) {
if (count == 0 ||
strcmp(*(&list2[count-1].txt+offset), *(&list[i].txt+offset)) != 0) {
list2[count++] = list[i];
strcmp(*(&list[count-1].txt+offset), *(&list[i].txt+offset)) != 0) {
list[count++] = list[i];
} else {
list2[count-1].num += list[i].num;
list2[count-1].page_num += list[i].page_num;
list[count-1].num += list[i].num;
list[count-1].page_num += list[i].page_num;
}
}

qsort(list2, count, sizeof(list[0]), cmp);
qsort(list, count, sizeof(list[0]), cmp);

for (i = 0; i < count; i++) {
if (filter == 1 && list2[i].free_ts_nsec != 0)
continue;
fprintf(fout, "%d times, %d pages:\n%s\n",
list2[i].num, list2[i].page_num, list2[i].txt);
if (cull_st == 0)
fprintf(fout, "%d times, %d pages:\n%s\n",
list[i].num, list[i].page_num, list[i].txt);
else
fprintf(fout, "%d times, %d pages:\n%s\n",
list[i].num, list[i].page_num, list[i].stacktrace);
}
regfree(&order_pattern);
regfree(&pid_pattern);
Expand Down

0 comments on commit 578d8f2

Please sign in to comment.