page_owner_sort.c 2.8 KB

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