Skip to contentSkip to conversation

tools/vm/page_owner_sort.c: Sort by stacktrace before culling

PATCH weizhenliang 2 participants 2 messages Nov 25, 2021

lore
Share & tools
lore
#0 lore raw
Quoted 5 lines · Sean Anderson

static int compare_num(const void *p1, const void *p2) @@ -121,6 +122,7 @@ static void add_list(char *buf, int len) list[list_size].page_num = get_page_num(buf); memcpy(list[list_size].txt, buf, len); list[list_size].txt[len] = 0;
+ list[list_size].stacktrace = strchr(list[list_size].txt, '\n');

When read_block gets an empty line, buf is "\n", then the stacktrace is NULL

Quoted 13 lines

list_size++;
if (list_size % 1000 == 0) {
printf("loaded %d\r", list_size);

(quoted diff) +1 -1
@@ -199,7 +201,7 @@ int main(int argc, char **argv)
 
 printf("sorting ....\n");
 
- qsort(list, list_size, sizeof(list[0]), compare_txt);
+ qsort(list, list_size, sizeof(list[0]), compare_stacktrace);
 

list2 = malloc(sizeof(*list) * list_size); if (!list2) { @@ -211,7 +213,7 @@ int main(int argc, char **argv)

Quoted 3 lines

for (i = count = 0; i < list_size; i++) { if (count == 0 ||
- strcmp(list2[count-1].txt, list[i].txt) != 0) {
+ strcmp(list2[count-1].stacktrace, list[i].stacktrace) != 0) {

And when stacktrace is NULL, a segmentation fault will be triggered here.

Quoted 3 lines

list2[count++] = list[i];
} else {
list2[count-1].num += list[i].num;

1. Maybe you can check whether the ret of read_block is 0 before add_list, or whether the len of buf is 0 in add_list
2. Is it necessary to check stacktrace==NULL before use ?

Wei.

#1 lore raw
Quoted 20 lines · weizhenliang
Quoted 5 lines · Sean Anderson

static int compare_num(const void *p1, const void *p2) @@ -121,6 +122,7 @@ static void add_list(char *buf, int len) list[list_size].page_num = get_page_num(buf); memcpy(list[list_size].txt, buf, len); list[list_size].txt[len] = 0;
+ list[list_size].stacktrace = strchr(list[list_size].txt, '\n');

When read_block gets an empty line, buf is "\n", then the stacktrace is NULL

Quoted 10 lines

list_size++;
if (list_size % 1000 == 0) {
printf("loaded %d\r", list_size);

(quoted diff) +1 -1
@@ -199,7 +201,7 @@ int main(int argc, char **argv)
 
 printf("sorting ....\n");
 
- qsort(list, list_size, sizeof(list[0]), compare_txt);
+ qsort(list, list_size, sizeof(list[0]), compare_stacktrace);
 
Quoted 8 lines
Quoted 6 lines

list2 = malloc(sizeof(*list) * list_size); if (!list2) { @@ -211,7 +213,7 @@ int main(int argc, char **argv)

for (i = count = 0; i < list_size; i++) { if (count == 0 ||
- strcmp(list2[count-1].txt, list[i].txt) != 0) {
+ strcmp(list2[count-1].stacktrace, list[i].stacktrace) != 0) {

And when stacktrace is NULL, a segmentation fault will be triggered here.

Ah, whoops. Looks like I check for this in compare_stacktrace but not here.

Quoted 6 lines
Quoted 3 lines

list2[count++] = list[i];
} else {
list2[count-1].num += list[i].num;

1. Maybe you can check whether the ret of read_block is 0 before add_list, or whether the len of buf is 0 in add_list

I think this is the best route.

Since this seems to have already been applied I've sent a follow-up patch.

--Sean