kmemtrace.txt 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. kmemtrace - Kernel Memory Tracer
  2. by Eduard - Gabriel Munteanu
  3. <eduard.munteanu@linux360.ro>
  4. I. Introduction
  5. ===============
  6. kmemtrace helps kernel developers figure out two things:
  7. 1) how different allocators (SLAB, SLUB etc.) perform
  8. 2) how kernel code allocates memory and how much
  9. To do this, we trace every allocation and export information to the userspace
  10. through the relay interface. We export things such as the number of requested
  11. bytes, the number of bytes actually allocated (i.e. including internal
  12. fragmentation), whether this is a slab allocation or a plain kmalloc() and so
  13. on.
  14. The actual analysis is performed by a userspace tool (see section III for
  15. details on where to get it from). It logs the data exported by the kernel,
  16. processes it and (as of writing this) can provide the following information:
  17. - the total amount of memory allocated and fragmentation per call-site
  18. - the amount of memory allocated and fragmentation per allocation
  19. - total memory allocated and fragmentation in the collected dataset
  20. - number of cross-CPU allocation and frees (makes sense in NUMA environments)
  21. Moreover, it can potentially find inconsistent and erroneous behavior in
  22. kernel code, such as using slab free functions on kmalloc'ed memory or
  23. allocating less memory than requested (but not truly failed allocations).
  24. kmemtrace also makes provisions for tracing on some arch and analysing the
  25. data on another.
  26. II. Design and goals
  27. ====================
  28. kmemtrace was designed to handle rather large amounts of data. Thus, it uses
  29. the relay interface to export whatever is logged to userspace, which then
  30. stores it. Analysis and reporting is done asynchronously, that is, after the
  31. data is collected and stored. By design, it allows one to log and analyse
  32. on different machines and different arches.
  33. As of writing this, the ABI is not considered stable, though it might not
  34. change much. However, no guarantees are made about compatibility yet. When
  35. deemed stable, the ABI should still allow easy extension while maintaining
  36. backward compatibility. This is described further in Documentation/ABI.
  37. Summary of design goals:
  38. - allow logging and analysis to be done across different machines
  39. - be fast and anticipate usage in high-load environments (*)
  40. - be reasonably extensible
  41. - make it possible for GNU/Linux distributions to have kmemtrace
  42. included in their repositories
  43. (*) - one of the reasons Pekka Enberg's original userspace data analysis
  44. tool's code was rewritten from Perl to C (although this is more than a
  45. simple conversion)
  46. III. Quick usage guide
  47. ======================
  48. 1) Get a kernel that supports kmemtrace and build it accordingly (i.e. enable
  49. CONFIG_KMEMTRACE).
  50. 2) Get the userspace tool and build it:
  51. $ git clone git://repo.or.cz/kmemtrace-user.git # current repository
  52. $ cd kmemtrace-user/
  53. $ ./autogen.sh
  54. $ ./configure
  55. $ make
  56. 3) Boot the kmemtrace-enabled kernel if you haven't, preferably in the
  57. 'single' runlevel (so that relay buffers don't fill up easily), and run
  58. kmemtrace:
  59. # '$' does not mean user, but root here.
  60. $ mount -t debugfs none /sys/kernel/debug
  61. $ mount -t proc none /proc
  62. $ cd path/to/kmemtrace-user/
  63. $ ./kmemtraced
  64. Wait a bit, then stop it with CTRL+C.
  65. $ cat /sys/kernel/debug/kmemtrace/total_overruns # Check if we didn't
  66. # overrun, should
  67. # be zero.
  68. $ (Optionally) [Run kmemtrace_check separately on each cpu[0-9]*.out file to
  69. check its correctness]
  70. $ ./kmemtrace-report
  71. Now you should have a nice and short summary of how the allocator performs.
  72. IV. FAQ and known issues
  73. ========================
  74. Q: 'cat /sys/kernel/debug/kmemtrace/total_overruns' is non-zero, how do I fix
  75. this? Should I worry?
  76. A: If it's non-zero, this affects kmemtrace's accuracy, depending on how
  77. large the number is. You can fix it by supplying a higher
  78. 'kmemtrace.subbufs=N' kernel parameter.
  79. ---
  80. Q: kmemtrace_check reports errors, how do I fix this? Should I worry?
  81. A: This is a bug and should be reported. It can occur for a variety of
  82. reasons:
  83. - possible bugs in relay code
  84. - possible misuse of relay by kmemtrace
  85. - timestamps being collected unorderly
  86. Or you may fix it yourself and send us a patch.
  87. ---
  88. Q: kmemtrace_report shows many errors, how do I fix this? Should I worry?
  89. A: This is a known issue and I'm working on it. These might be true errors
  90. in kernel code, which may have inconsistent behavior (e.g. allocating memory
  91. with kmem_cache_alloc() and freeing it with kfree()). Pekka Enberg pointed
  92. out this behavior may work with SLAB, but may fail with other allocators.
  93. It may also be due to lack of tracing in some unusual allocator functions.
  94. We don't want bug reports regarding this issue yet.
  95. ---
  96. V. See also
  97. ===========
  98. Documentation/kernel-parameters.txt
  99. Documentation/ABI/testing/debugfs-kmemtrace