base.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #ifndef JEMALLOC_INTERNAL_BASE_H
  2. #define JEMALLOC_INTERNAL_BASE_H
  3. #include "jemalloc/internal/edata.h"
  4. #include "jemalloc/internal/ehooks.h"
  5. #include "jemalloc/internal/mutex.h"
  6. enum metadata_thp_mode_e {
  7. metadata_thp_disabled = 0,
  8. /*
  9. * Lazily enable hugepage for metadata. To avoid high RSS caused by THP
  10. * + low usage arena (i.e. THP becomes a significant percentage), the
  11. * "auto" option only starts using THP after a base allocator used up
  12. * the first THP region. Starting from the second hugepage (in a single
  13. * arena), "auto" behaves the same as "always", i.e. madvise hugepage
  14. * right away.
  15. */
  16. metadata_thp_auto = 1,
  17. metadata_thp_always = 2,
  18. metadata_thp_mode_limit = 3
  19. };
  20. typedef enum metadata_thp_mode_e metadata_thp_mode_t;
  21. #define METADATA_THP_DEFAULT metadata_thp_disabled
  22. extern metadata_thp_mode_t opt_metadata_thp;
  23. extern const char *metadata_thp_mode_names[];
  24. /* Embedded at the beginning of every block of base-managed virtual memory. */
  25. typedef struct base_block_s base_block_t;
  26. struct base_block_s {
  27. /* Total size of block's virtual memory mapping. */
  28. size_t size;
  29. /* Next block in list of base's blocks. */
  30. base_block_t *next;
  31. /* Tracks unused trailing space. */
  32. edata_t edata;
  33. };
  34. typedef struct base_s base_t;
  35. struct base_s {
  36. /*
  37. * User-configurable extent hook functions.
  38. */
  39. ehooks_t ehooks;
  40. /*
  41. * User-configurable extent hook functions for metadata allocations.
  42. */
  43. ehooks_t ehooks_base;
  44. /* Protects base_alloc() and base_stats_get() operations. */
  45. malloc_mutex_t mtx;
  46. /* Using THP when true (metadata_thp auto mode). */
  47. bool auto_thp_switched;
  48. /*
  49. * Most recent size class in the series of increasingly large base
  50. * extents. Logarithmic spacing between subsequent allocations ensures
  51. * that the total number of distinct mappings remains small.
  52. */
  53. pszind_t pind_last;
  54. /* Serial number generation state. */
  55. size_t extent_sn_next;
  56. /* Chain of all blocks associated with base. */
  57. base_block_t *blocks;
  58. /* Heap of extents that track unused trailing space within blocks. */
  59. edata_heap_t avail[SC_NSIZES];
  60. /* Stats, only maintained if config_stats. */
  61. size_t allocated;
  62. size_t resident;
  63. size_t mapped;
  64. /* Number of THP regions touched. */
  65. size_t n_thp;
  66. };
  67. static inline unsigned
  68. base_ind_get(const base_t *base) {
  69. return ehooks_ind_get(&base->ehooks);
  70. }
  71. static inline bool
  72. metadata_thp_enabled(void) {
  73. return (opt_metadata_thp != metadata_thp_disabled);
  74. }
  75. base_t *b0get(void);
  76. base_t *base_new(tsdn_t *tsdn, unsigned ind,
  77. const extent_hooks_t *extent_hooks, bool metadata_use_hooks);
  78. void base_delete(tsdn_t *tsdn, base_t *base);
  79. ehooks_t *base_ehooks_get(base_t *base);
  80. ehooks_t *base_ehooks_get_for_metadata(base_t *base);
  81. extent_hooks_t *base_extent_hooks_set(base_t *base,
  82. extent_hooks_t *extent_hooks);
  83. void *base_alloc(tsdn_t *tsdn, base_t *base, size_t size, size_t alignment);
  84. edata_t *base_alloc_edata(tsdn_t *tsdn, base_t *base);
  85. void base_stats_get(tsdn_t *tsdn, base_t *base, size_t *allocated,
  86. size_t *resident, size_t *mapped, size_t *n_thp);
  87. void base_prefork(tsdn_t *tsdn, base_t *base);
  88. void base_postfork_parent(tsdn_t *tsdn, base_t *base);
  89. void base_postfork_child(tsdn_t *tsdn, base_t *base);
  90. bool base_boot(tsdn_t *tsdn);
  91. #endif /* JEMALLOC_INTERNAL_BASE_H */