page_owner_sort.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * User-space helper to sort the output of /sys/kernel/debug/page_owner
  4. *
  5. * Example use:
  6. * cat /sys/kernel/debug/page_owner > page_owner_full.txt
  7. * grep -v ^PFN page_owner_full.txt > page_owner.txt
  8. * ./sort page_owner.txt sorted_page_owner.txt
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #include <fcntl.h>
  15. #include <unistd.h>
  16. #include <string.h>
  17. struct block_list {
  18. char *txt;
  19. int len;
  20. int num;
  21. };
  22. static struct block_list *list;
  23. static int list_size;
  24. static int max_size;
  25. struct block_list *block_head;
  26. int read_block(char *buf, int buf_size, FILE *fin)
  27. {
  28. char *curr = buf, *const buf_end = buf + buf_size;
  29. while (buf_end - curr > 1 && fgets(curr, buf_end - curr, fin)) {
  30. if (*curr == '\n') /* empty line */
  31. return curr - buf;
  32. curr += strlen(curr);
  33. }
  34. return -1; /* EOF or no space left in buf. */
  35. }
  36. static int compare_txt(const void *p1, const void *p2)
  37. {
  38. const struct block_list *l1 = p1, *l2 = p2;
  39. return strcmp(l1->txt, l2->txt);
  40. }
  41. static int compare_num(const void *p1, const void *p2)
  42. {
  43. const struct block_list *l1 = p1, *l2 = p2;
  44. return l2->num - l1->num;
  45. }
  46. static void add_list(char *buf, int len)
  47. {
  48. if (list_size != 0 &&
  49. len == list[list_size-1].len &&
  50. memcmp(buf, list[list_size-1].txt, len) == 0) {
  51. list[list_size-1].num++;
  52. return;
  53. }
  54. if (list_size == max_size) {
  55. printf("max_size too small??\n");
  56. exit(1);
  57. }
  58. list[list_size].txt = malloc(len+1);
  59. list[list_size].len = len;
  60. list[list_size].num = 1;
  61. memcpy(list[list_size].txt, buf, len);
  62. list[list_size].txt[len] = 0;
  63. list_size++;
  64. if (list_size % 1000 == 0) {
  65. printf("loaded %d\r", list_size);
  66. fflush(stdout);
  67. }
  68. }
  69. #define BUF_SIZE (128 * 1024)
  70. int main(int argc, char **argv)
  71. {
  72. FILE *fin, *fout;
  73. char *buf;
  74. int ret, i, count;
  75. struct block_list *list2;
  76. struct stat st;
  77. if (argc < 3) {
  78. printf("Usage: ./program <input> <output>\n");
  79. perror("open: ");
  80. exit(1);
  81. }
  82. fin = fopen(argv[1], "r");
  83. fout = fopen(argv[2], "w");
  84. if (!fin || !fout) {
  85. printf("Usage: ./program <input> <output>\n");
  86. perror("open: ");
  87. exit(1);
  88. }
  89. fstat(fileno(fin), &st);
  90. max_size = st.st_size / 100; /* hack ... */
  91. list = malloc(max_size * sizeof(*list));
  92. buf = malloc(BUF_SIZE);
  93. if (!list || !buf) {
  94. printf("Out of memory\n");
  95. exit(1);
  96. }
  97. for ( ; ; ) {
  98. ret = read_block(buf, BUF_SIZE, fin);
  99. if (ret < 0)
  100. break;
  101. add_list(buf, ret);
  102. }
  103. printf("loaded %d\n", list_size);
  104. printf("sorting ....\n");
  105. qsort(list, list_size, sizeof(list[0]), compare_txt);
  106. list2 = malloc(sizeof(*list) * list_size);
  107. printf("culling\n");
  108. for (i = count = 0; i < list_size; i++) {
  109. if (count == 0 ||
  110. strcmp(list2[count-1].txt, list[i].txt) != 0) {
  111. list2[count++] = list[i];
  112. } else {
  113. list2[count-1].num += list[i].num;
  114. }
  115. }
  116. qsort(list2, count, sizeof(list[0]), compare_num);
  117. for (i = 0; i < count; i++)
  118. fprintf(fout, "%d times:\n%s\n", list2[i].num, list2[i].txt);
  119. return 0;
  120. }