page_owner.rst 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. .. _page_owner:
  2. ==================================================
  3. page owner: Tracking about who allocated each page
  4. ==================================================
  5. Introduction
  6. ============
  7. page owner is for the tracking about who allocated each page.
  8. It can be used to debug memory leak or to find a memory hogger.
  9. When allocation happens, information about allocation such as call stack
  10. and order of pages is stored into certain storage for each page.
  11. When we need to know about status of all pages, we can get and analyze
  12. this information.
  13. Although we already have tracepoint for tracing page allocation/free,
  14. using it for analyzing who allocate each page is rather complex. We need
  15. to enlarge the trace buffer for preventing overlapping until userspace
  16. program launched. And, launched program continually dump out the trace
  17. buffer for later analysis and it would change system behaviour with more
  18. possibility rather than just keeping it in memory, so bad for debugging.
  19. page owner can also be used for various purposes. For example, accurate
  20. fragmentation statistics can be obtained through gfp flag information of
  21. each page. It is already implemented and activated if page owner is
  22. enabled. Other usages are more than welcome.
  23. page owner is disabled by default. So, if you'd like to use it, you need
  24. to add "page_owner=on" to your boot cmdline. If the kernel is built
  25. with page owner and page owner is disabled in runtime due to not enabling
  26. boot option, runtime overhead is marginal. If disabled in runtime, it
  27. doesn't require memory to store owner information, so there is no runtime
  28. memory overhead. And, page owner inserts just two unlikely branches into
  29. the page allocator hotpath and if not enabled, then allocation is done
  30. like as the kernel without page owner. These two unlikely branches should
  31. not affect to allocation performance, especially if the static keys jump
  32. label patching functionality is available. Following is the kernel's code
  33. size change due to this facility.
  34. Although enabling page owner increases kernel size by several kilobytes,
  35. most of this code is outside page allocator and its hot path. Building
  36. the kernel with page owner and turning it on if needed would be great
  37. option to debug kernel memory problem.
  38. There is one notice that is caused by implementation detail. page owner
  39. stores information into the memory from struct page extension. This memory
  40. is initialized some time later than that page allocator starts in sparse
  41. memory system, so, until initialization, many pages can be allocated and
  42. they would have no owner information. To fix it up, these early allocated
  43. pages are investigated and marked as allocated in initialization phase.
  44. Although it doesn't mean that they have the right owner information,
  45. at least, we can tell whether the page is allocated or not,
  46. more accurately. On 2GB memory x86-64 VM box, 13343 early allocated pages
  47. are catched and marked, although they are mostly allocated from struct
  48. page extension feature. Anyway, after that, no page is left in
  49. un-tracking state.
  50. Usage
  51. =====
  52. 1) Build user-space helper::
  53. cd tools/vm
  54. make page_owner_sort
  55. 2) Enable page owner: add "page_owner=on" to boot cmdline.
  56. 3) Do the job that you want to debug.
  57. 4) Analyze information from page owner::
  58. cat /sys/kernel/debug/page_owner > page_owner_full.txt
  59. ./page_owner_sort page_owner_full.txt sorted_page_owner.txt
  60. The general output of ``page_owner_full.txt`` is as follows::
  61. Page allocated via order XXX, ...
  62. PFN XXX ...
  63. // Detailed stack
  64. Page allocated via order XXX, ...
  65. PFN XXX ...
  66. // Detailed stack
  67. By default, it will do full pfn dump, to start with a given pfn,
  68. page_owner supports fseek.
  69. FILE *fp = fopen("/sys/kernel/debug/page_owner", "r");
  70. fseek(fp, pfn_start, SEEK_SET);
  71. The ``page_owner_sort`` tool ignores ``PFN`` rows, puts the remaining rows
  72. in buf, uses regexp to extract the page order value, counts the times
  73. and pages of buf, and finally sorts them according to the parameter(s).
  74. See the result about who allocated each page
  75. in the ``sorted_page_owner.txt``. General output::
  76. XXX times, XXX pages:
  77. Page allocated via order XXX, ...
  78. // Detailed stack
  79. By default, ``page_owner_sort`` is sorted according to the times of buf.
  80. If you want to sort by the page nums of buf, use the ``-m`` parameter.
  81. The detailed parameters are:
  82. fundamental function::
  83. Sort:
  84. -a Sort by memory allocation time.
  85. -m Sort by total memory.
  86. -p Sort by pid.
  87. -P Sort by tgid.
  88. -n Sort by task command name.
  89. -r Sort by memory release time.
  90. -s Sort by stack trace.
  91. -t Sort by times (default).
  92. --sort <order> Specify sorting order. Sorting syntax is [+|-]key[,[+|-]key[,...]].
  93. Choose a key from the **STANDARD FORMAT SPECIFIERS** section. The "+" is
  94. optional since default direction is increasing numerical or lexicographic
  95. order. Mixed use of abbreviated and complete-form of keys is allowed.
  96. Examples:
  97. ./page_owner_sort <input> <output> --sort=n,+pid,-tgid
  98. ./page_owner_sort <input> <output> --sort=at
  99. additional function::
  100. Cull:
  101. --cull <rules>
  102. Specify culling rules.Culling syntax is key[,key[,...]].Choose a
  103. multi-letter key from the **STANDARD FORMAT SPECIFIERS** section.
  104. <rules> is a single argument in the form of a comma-separated list,
  105. which offers a way to specify individual culling rules. The recognized
  106. keywords are described in the **STANDARD FORMAT SPECIFIERS** section below.
  107. <rules> can be specified by the sequence of keys k1,k2, ..., as described in
  108. the STANDARD SORT KEYS section below. Mixed use of abbreviated and
  109. complete-form of keys is allowed.
  110. Examples:
  111. ./page_owner_sort <input> <output> --cull=stacktrace
  112. ./page_owner_sort <input> <output> --cull=st,pid,name
  113. ./page_owner_sort <input> <output> --cull=n,f
  114. Filter:
  115. -f Filter out the information of blocks whose memory has been released.
  116. Select:
  117. --pid <pidlist> Select by pid. This selects the blocks whose process ID
  118. numbers appear in <pidlist>.
  119. --tgid <tgidlist> Select by tgid. This selects the blocks whose thread
  120. group ID numbers appear in <tgidlist>.
  121. --name <cmdlist> Select by task command name. This selects the blocks whose
  122. task command name appear in <cmdlist>.
  123. <pidlist>, <tgidlist>, <cmdlist> are single arguments in the form of a comma-separated list,
  124. which offers a way to specify individual selecting rules.
  125. Examples:
  126. ./page_owner_sort <input> <output> --pid=1
  127. ./page_owner_sort <input> <output> --tgid=1,2,3
  128. ./page_owner_sort <input> <output> --name name1,name2
  129. STANDARD FORMAT SPECIFIERS
  130. ==========================
  131. ::
  132. For --sort option:
  133. KEY LONG DESCRIPTION
  134. p pid process ID
  135. tg tgid thread group ID
  136. n name task command name
  137. st stacktrace stack trace of the page allocation
  138. T txt full text of block
  139. ft free_ts timestamp of the page when it was released
  140. at alloc_ts timestamp of the page when it was allocated
  141. ator allocator memory allocator for pages
  142. For --curl option:
  143. KEY LONG DESCRIPTION
  144. p pid process ID
  145. tg tgid thread group ID
  146. n name task command name
  147. f free whether the page has been released or not
  148. st stacktrace stack trace of the page allocation
  149. ator allocator memory allocator for pages