damon.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * DAMON api
  4. *
  5. * Author: SeongJae Park <sjpark@amazon.de>
  6. */
  7. #ifndef _DAMON_H_
  8. #define _DAMON_H_
  9. #include <linux/mutex.h>
  10. #include <linux/time64.h>
  11. #include <linux/types.h>
  12. #include <linux/random.h>
  13. /* Minimal region size. Every damon_region is aligned by this. */
  14. #define DAMON_MIN_REGION PAGE_SIZE
  15. /* Max priority score for DAMON-based operation schemes */
  16. #define DAMOS_MAX_SCORE (99)
  17. /* Get a random number in [l, r) */
  18. static inline unsigned long damon_rand(unsigned long l, unsigned long r)
  19. {
  20. return l + prandom_u32_max(r - l);
  21. }
  22. /**
  23. * struct damon_addr_range - Represents an address region of [@start, @end).
  24. * @start: Start address of the region (inclusive).
  25. * @end: End address of the region (exclusive).
  26. */
  27. struct damon_addr_range {
  28. unsigned long start;
  29. unsigned long end;
  30. };
  31. /**
  32. * struct damon_region - Represents a monitoring target region.
  33. * @ar: The address range of the region.
  34. * @sampling_addr: Address of the sample for the next access check.
  35. * @nr_accesses: Access frequency of this region.
  36. * @list: List head for siblings.
  37. * @age: Age of this region.
  38. *
  39. * @age is initially zero, increased for each aggregation interval, and reset
  40. * to zero again if the access frequency is significantly changed. If two
  41. * regions are merged into a new region, both @nr_accesses and @age of the new
  42. * region are set as region size-weighted average of those of the two regions.
  43. */
  44. struct damon_region {
  45. struct damon_addr_range ar;
  46. unsigned long sampling_addr;
  47. unsigned int nr_accesses;
  48. struct list_head list;
  49. unsigned int age;
  50. /* private: Internal value for age calculation. */
  51. unsigned int last_nr_accesses;
  52. };
  53. /**
  54. * struct damon_target - Represents a monitoring target.
  55. * @id: Unique identifier for this target.
  56. * @nr_regions: Number of monitoring target regions of this target.
  57. * @regions_list: Head of the monitoring target regions of this target.
  58. * @list: List head for siblings.
  59. *
  60. * Each monitoring context could have multiple targets. For example, a context
  61. * for virtual memory address spaces could have multiple target processes. The
  62. * @id of each target should be unique among the targets of the context. For
  63. * example, in the virtual address monitoring context, it could be a pidfd or
  64. * an address of an mm_struct.
  65. */
  66. struct damon_target {
  67. unsigned long id;
  68. unsigned int nr_regions;
  69. struct list_head regions_list;
  70. struct list_head list;
  71. };
  72. /**
  73. * enum damos_action - Represents an action of a Data Access Monitoring-based
  74. * Operation Scheme.
  75. *
  76. * @DAMOS_WILLNEED: Call ``madvise()`` for the region with MADV_WILLNEED.
  77. * @DAMOS_COLD: Call ``madvise()`` for the region with MADV_COLD.
  78. * @DAMOS_PAGEOUT: Call ``madvise()`` for the region with MADV_PAGEOUT.
  79. * @DAMOS_HUGEPAGE: Call ``madvise()`` for the region with MADV_HUGEPAGE.
  80. * @DAMOS_NOHUGEPAGE: Call ``madvise()`` for the region with MADV_NOHUGEPAGE.
  81. * @DAMOS_STAT: Do nothing but count the stat.
  82. */
  83. enum damos_action {
  84. DAMOS_WILLNEED,
  85. DAMOS_COLD,
  86. DAMOS_PAGEOUT,
  87. DAMOS_HUGEPAGE,
  88. DAMOS_NOHUGEPAGE,
  89. DAMOS_STAT, /* Do nothing but only record the stat */
  90. };
  91. /**
  92. * struct damos_quota - Controls the aggressiveness of the given scheme.
  93. * @ms: Maximum milliseconds that the scheme can use.
  94. * @sz: Maximum bytes of memory that the action can be applied.
  95. * @reset_interval: Charge reset interval in milliseconds.
  96. *
  97. * @weight_sz: Weight of the region's size for prioritization.
  98. * @weight_nr_accesses: Weight of the region's nr_accesses for prioritization.
  99. * @weight_age: Weight of the region's age for prioritization.
  100. *
  101. * To avoid consuming too much CPU time or IO resources for applying the
  102. * &struct damos->action to large memory, DAMON allows users to set time and/or
  103. * size quotas. The quotas can be set by writing non-zero values to &ms and
  104. * &sz, respectively. If the time quota is set, DAMON tries to use only up to
  105. * &ms milliseconds within &reset_interval for applying the action. If the
  106. * size quota is set, DAMON tries to apply the action only up to &sz bytes
  107. * within &reset_interval.
  108. *
  109. * Internally, the time quota is transformed to a size quota using estimated
  110. * throughput of the scheme's action. DAMON then compares it against &sz and
  111. * uses smaller one as the effective quota.
  112. *
  113. * For selecting regions within the quota, DAMON prioritizes current scheme's
  114. * target memory regions using the &struct damon_primitive->get_scheme_score.
  115. * You could customize the prioritization logic by setting &weight_sz,
  116. * &weight_nr_accesses, and &weight_age, because monitoring primitives are
  117. * encouraged to respect those.
  118. */
  119. struct damos_quota {
  120. unsigned long ms;
  121. unsigned long sz;
  122. unsigned long reset_interval;
  123. unsigned int weight_sz;
  124. unsigned int weight_nr_accesses;
  125. unsigned int weight_age;
  126. /* private: */
  127. /* For throughput estimation */
  128. unsigned long total_charged_sz;
  129. unsigned long total_charged_ns;
  130. unsigned long esz; /* Effective size quota in bytes */
  131. /* For charging the quota */
  132. unsigned long charged_sz;
  133. unsigned long charged_from;
  134. struct damon_target *charge_target_from;
  135. unsigned long charge_addr_from;
  136. /* For prioritization */
  137. unsigned long histogram[DAMOS_MAX_SCORE + 1];
  138. unsigned int min_score;
  139. };
  140. /**
  141. * enum damos_wmark_metric - Represents the watermark metric.
  142. *
  143. * @DAMOS_WMARK_NONE: Ignore the watermarks of the given scheme.
  144. * @DAMOS_WMARK_FREE_MEM_RATE: Free memory rate of the system in [0,1000].
  145. */
  146. enum damos_wmark_metric {
  147. DAMOS_WMARK_NONE,
  148. DAMOS_WMARK_FREE_MEM_RATE,
  149. };
  150. /**
  151. * struct damos_watermarks - Controls when a given scheme should be activated.
  152. * @metric: Metric for the watermarks.
  153. * @interval: Watermarks check time interval in microseconds.
  154. * @high: High watermark.
  155. * @mid: Middle watermark.
  156. * @low: Low watermark.
  157. *
  158. * If &metric is &DAMOS_WMARK_NONE, the scheme is always active. Being active
  159. * means DAMON does monitoring and applying the action of the scheme to
  160. * appropriate memory regions. Else, DAMON checks &metric of the system for at
  161. * least every &interval microseconds and works as below.
  162. *
  163. * If &metric is higher than &high, the scheme is inactivated. If &metric is
  164. * between &mid and &low, the scheme is activated. If &metric is lower than
  165. * &low, the scheme is inactivated.
  166. */
  167. struct damos_watermarks {
  168. enum damos_wmark_metric metric;
  169. unsigned long interval;
  170. unsigned long high;
  171. unsigned long mid;
  172. unsigned long low;
  173. /* private: */
  174. bool activated;
  175. };
  176. /**
  177. * struct damos_stat - Statistics on a given scheme.
  178. * @nr_tried: Total number of regions that the scheme is tried to be applied.
  179. * @sz_tried: Total size of regions that the scheme is tried to be applied.
  180. * @nr_applied: Total number of regions that the scheme is applied.
  181. * @sz_applied: Total size of regions that the scheme is applied.
  182. * @qt_exceeds: Total number of times the quota of the scheme has exceeded.
  183. */
  184. struct damos_stat {
  185. unsigned long nr_tried;
  186. unsigned long sz_tried;
  187. unsigned long nr_applied;
  188. unsigned long sz_applied;
  189. unsigned long qt_exceeds;
  190. };
  191. /**
  192. * struct damos - Represents a Data Access Monitoring-based Operation Scheme.
  193. * @min_sz_region: Minimum size of target regions.
  194. * @max_sz_region: Maximum size of target regions.
  195. * @min_nr_accesses: Minimum ``->nr_accesses`` of target regions.
  196. * @max_nr_accesses: Maximum ``->nr_accesses`` of target regions.
  197. * @min_age_region: Minimum age of target regions.
  198. * @max_age_region: Maximum age of target regions.
  199. * @action: &damo_action to be applied to the target regions.
  200. * @quota: Control the aggressiveness of this scheme.
  201. * @wmarks: Watermarks for automated (in)activation of this scheme.
  202. * @stat: Statistics of this scheme.
  203. * @list: List head for siblings.
  204. *
  205. * For each aggregation interval, DAMON finds regions which fit in the
  206. * condition (&min_sz_region, &max_sz_region, &min_nr_accesses,
  207. * &max_nr_accesses, &min_age_region, &max_age_region) and applies &action to
  208. * those. To avoid consuming too much CPU time or IO resources for the
  209. * &action, &quota is used.
  210. *
  211. * To do the work only when needed, schemes can be activated for specific
  212. * system situations using &wmarks. If all schemes that registered to the
  213. * monitoring context are inactive, DAMON stops monitoring either, and just
  214. * repeatedly checks the watermarks.
  215. *
  216. * If all schemes that registered to a &struct damon_ctx are inactive, DAMON
  217. * stops monitoring and just repeatedly checks the watermarks.
  218. *
  219. * After applying the &action to each region, &stat_count and &stat_sz is
  220. * updated to reflect the number of regions and total size of regions that the
  221. * &action is applied.
  222. */
  223. struct damos {
  224. unsigned long min_sz_region;
  225. unsigned long max_sz_region;
  226. unsigned int min_nr_accesses;
  227. unsigned int max_nr_accesses;
  228. unsigned int min_age_region;
  229. unsigned int max_age_region;
  230. enum damos_action action;
  231. struct damos_quota quota;
  232. struct damos_watermarks wmarks;
  233. struct damos_stat stat;
  234. struct list_head list;
  235. };
  236. struct damon_ctx;
  237. /**
  238. * struct damon_primitive - Monitoring primitives for given use cases.
  239. *
  240. * @init: Initialize primitive-internal data structures.
  241. * @update: Update primitive-internal data structures.
  242. * @prepare_access_checks: Prepare next access check of target regions.
  243. * @check_accesses: Check the accesses to target regions.
  244. * @reset_aggregated: Reset aggregated accesses monitoring results.
  245. * @get_scheme_score: Get the score of a region for a scheme.
  246. * @apply_scheme: Apply a DAMON-based operation scheme.
  247. * @target_valid: Determine if the target is valid.
  248. * @cleanup: Clean up the context.
  249. *
  250. * DAMON can be extended for various address spaces and usages. For this,
  251. * users should register the low level primitives for their target address
  252. * space and usecase via the &damon_ctx.primitive. Then, the monitoring thread
  253. * (&damon_ctx.kdamond) calls @init and @prepare_access_checks before starting
  254. * the monitoring, @update after each &damon_ctx.primitive_update_interval, and
  255. * @check_accesses, @target_valid and @prepare_access_checks after each
  256. * &damon_ctx.sample_interval. Finally, @reset_aggregated is called after each
  257. * &damon_ctx.aggr_interval.
  258. *
  259. * @init should initialize primitive-internal data structures. For example,
  260. * this could be used to construct proper monitoring target regions and link
  261. * those to @damon_ctx.adaptive_targets.
  262. * @update should update the primitive-internal data structures. For example,
  263. * this could be used to update monitoring target regions for current status.
  264. * @prepare_access_checks should manipulate the monitoring regions to be
  265. * prepared for the next access check.
  266. * @check_accesses should check the accesses to each region that made after the
  267. * last preparation and update the number of observed accesses of each region.
  268. * It should also return max number of observed accesses that made as a result
  269. * of its update. The value will be used for regions adjustment threshold.
  270. * @reset_aggregated should reset the access monitoring results that aggregated
  271. * by @check_accesses.
  272. * @get_scheme_score should return the priority score of a region for a scheme
  273. * as an integer in [0, &DAMOS_MAX_SCORE].
  274. * @apply_scheme is called from @kdamond when a region for user provided
  275. * DAMON-based operation scheme is found. It should apply the scheme's action
  276. * to the region and return bytes of the region that the action is successfully
  277. * applied.
  278. * @target_valid should check whether the target is still valid for the
  279. * monitoring.
  280. * @cleanup is called from @kdamond just before its termination.
  281. */
  282. struct damon_primitive {
  283. void (*init)(struct damon_ctx *context);
  284. void (*update)(struct damon_ctx *context);
  285. void (*prepare_access_checks)(struct damon_ctx *context);
  286. unsigned int (*check_accesses)(struct damon_ctx *context);
  287. void (*reset_aggregated)(struct damon_ctx *context);
  288. int (*get_scheme_score)(struct damon_ctx *context,
  289. struct damon_target *t, struct damon_region *r,
  290. struct damos *scheme);
  291. unsigned long (*apply_scheme)(struct damon_ctx *context,
  292. struct damon_target *t, struct damon_region *r,
  293. struct damos *scheme);
  294. bool (*target_valid)(void *target);
  295. void (*cleanup)(struct damon_ctx *context);
  296. };
  297. /**
  298. * struct damon_callback - Monitoring events notification callbacks.
  299. *
  300. * @before_start: Called before starting the monitoring.
  301. * @after_sampling: Called after each sampling.
  302. * @after_aggregation: Called after each aggregation.
  303. * @before_terminate: Called before terminating the monitoring.
  304. * @private: User private data.
  305. *
  306. * The monitoring thread (&damon_ctx.kdamond) calls @before_start and
  307. * @before_terminate just before starting and finishing the monitoring,
  308. * respectively. Therefore, those are good places for installing and cleaning
  309. * @private.
  310. *
  311. * The monitoring thread calls @after_sampling and @after_aggregation for each
  312. * of the sampling intervals and aggregation intervals, respectively.
  313. * Therefore, users can safely access the monitoring results without additional
  314. * protection. For the reason, users are recommended to use these callback for
  315. * the accesses to the results.
  316. *
  317. * If any callback returns non-zero, monitoring stops.
  318. */
  319. struct damon_callback {
  320. void *private;
  321. int (*before_start)(struct damon_ctx *context);
  322. int (*after_sampling)(struct damon_ctx *context);
  323. int (*after_aggregation)(struct damon_ctx *context);
  324. void (*before_terminate)(struct damon_ctx *context);
  325. };
  326. /**
  327. * struct damon_ctx - Represents a context for each monitoring. This is the
  328. * main interface that allows users to set the attributes and get the results
  329. * of the monitoring.
  330. *
  331. * @sample_interval: The time between access samplings.
  332. * @aggr_interval: The time between monitor results aggregations.
  333. * @primitive_update_interval: The time between monitoring primitive updates.
  334. *
  335. * For each @sample_interval, DAMON checks whether each region is accessed or
  336. * not. It aggregates and keeps the access information (number of accesses to
  337. * each region) for @aggr_interval time. DAMON also checks whether the target
  338. * memory regions need update (e.g., by ``mmap()`` calls from the application,
  339. * in case of virtual memory monitoring) and applies the changes for each
  340. * @primitive_update_interval. All time intervals are in micro-seconds.
  341. * Please refer to &struct damon_primitive and &struct damon_callback for more
  342. * detail.
  343. *
  344. * @kdamond: Kernel thread who does the monitoring.
  345. * @kdamond_stop: Notifies whether kdamond should stop.
  346. * @kdamond_lock: Mutex for the synchronizations with @kdamond.
  347. *
  348. * For each monitoring context, one kernel thread for the monitoring is
  349. * created. The pointer to the thread is stored in @kdamond.
  350. *
  351. * Once started, the monitoring thread runs until explicitly required to be
  352. * terminated or every monitoring target is invalid. The validity of the
  353. * targets is checked via the &damon_primitive.target_valid of @primitive. The
  354. * termination can also be explicitly requested by writing non-zero to
  355. * @kdamond_stop. The thread sets @kdamond to NULL when it terminates.
  356. * Therefore, users can know whether the monitoring is ongoing or terminated by
  357. * reading @kdamond. Reads and writes to @kdamond and @kdamond_stop from
  358. * outside of the monitoring thread must be protected by @kdamond_lock.
  359. *
  360. * Note that the monitoring thread protects only @kdamond and @kdamond_stop via
  361. * @kdamond_lock. Accesses to other fields must be protected by themselves.
  362. *
  363. * @primitive: Set of monitoring primitives for given use cases.
  364. * @callback: Set of callbacks for monitoring events notifications.
  365. *
  366. * @min_nr_regions: The minimum number of adaptive monitoring regions.
  367. * @max_nr_regions: The maximum number of adaptive monitoring regions.
  368. * @adaptive_targets: Head of monitoring targets (&damon_target) list.
  369. * @schemes: Head of schemes (&damos) list.
  370. */
  371. struct damon_ctx {
  372. unsigned long sample_interval;
  373. unsigned long aggr_interval;
  374. unsigned long primitive_update_interval;
  375. /* private: internal use only */
  376. struct timespec64 last_aggregation;
  377. struct timespec64 last_primitive_update;
  378. /* public: */
  379. struct task_struct *kdamond;
  380. struct mutex kdamond_lock;
  381. struct damon_primitive primitive;
  382. struct damon_callback callback;
  383. unsigned long min_nr_regions;
  384. unsigned long max_nr_regions;
  385. struct list_head adaptive_targets;
  386. struct list_head schemes;
  387. };
  388. static inline struct damon_region *damon_next_region(struct damon_region *r)
  389. {
  390. return container_of(r->list.next, struct damon_region, list);
  391. }
  392. static inline struct damon_region *damon_prev_region(struct damon_region *r)
  393. {
  394. return container_of(r->list.prev, struct damon_region, list);
  395. }
  396. static inline struct damon_region *damon_last_region(struct damon_target *t)
  397. {
  398. return list_last_entry(&t->regions_list, struct damon_region, list);
  399. }
  400. #define damon_for_each_region(r, t) \
  401. list_for_each_entry(r, &t->regions_list, list)
  402. #define damon_for_each_region_safe(r, next, t) \
  403. list_for_each_entry_safe(r, next, &t->regions_list, list)
  404. #define damon_for_each_target(t, ctx) \
  405. list_for_each_entry(t, &(ctx)->adaptive_targets, list)
  406. #define damon_for_each_target_safe(t, next, ctx) \
  407. list_for_each_entry_safe(t, next, &(ctx)->adaptive_targets, list)
  408. #define damon_for_each_scheme(s, ctx) \
  409. list_for_each_entry(s, &(ctx)->schemes, list)
  410. #define damon_for_each_scheme_safe(s, next, ctx) \
  411. list_for_each_entry_safe(s, next, &(ctx)->schemes, list)
  412. #ifdef CONFIG_DAMON
  413. struct damon_region *damon_new_region(unsigned long start, unsigned long end);
  414. /*
  415. * Add a region between two other regions
  416. */
  417. static inline void damon_insert_region(struct damon_region *r,
  418. struct damon_region *prev, struct damon_region *next,
  419. struct damon_target *t)
  420. {
  421. __list_add(&r->list, &prev->list, &next->list);
  422. t->nr_regions++;
  423. }
  424. void damon_add_region(struct damon_region *r, struct damon_target *t);
  425. void damon_destroy_region(struct damon_region *r, struct damon_target *t);
  426. struct damos *damon_new_scheme(
  427. unsigned long min_sz_region, unsigned long max_sz_region,
  428. unsigned int min_nr_accesses, unsigned int max_nr_accesses,
  429. unsigned int min_age_region, unsigned int max_age_region,
  430. enum damos_action action, struct damos_quota *quota,
  431. struct damos_watermarks *wmarks);
  432. void damon_add_scheme(struct damon_ctx *ctx, struct damos *s);
  433. void damon_destroy_scheme(struct damos *s);
  434. struct damon_target *damon_new_target(unsigned long id);
  435. void damon_add_target(struct damon_ctx *ctx, struct damon_target *t);
  436. bool damon_targets_empty(struct damon_ctx *ctx);
  437. void damon_free_target(struct damon_target *t);
  438. void damon_destroy_target(struct damon_target *t);
  439. unsigned int damon_nr_regions(struct damon_target *t);
  440. struct damon_ctx *damon_new_ctx(void);
  441. void damon_destroy_ctx(struct damon_ctx *ctx);
  442. int damon_set_targets(struct damon_ctx *ctx,
  443. unsigned long *ids, ssize_t nr_ids);
  444. int damon_set_attrs(struct damon_ctx *ctx, unsigned long sample_int,
  445. unsigned long aggr_int, unsigned long primitive_upd_int,
  446. unsigned long min_nr_reg, unsigned long max_nr_reg);
  447. int damon_set_schemes(struct damon_ctx *ctx,
  448. struct damos **schemes, ssize_t nr_schemes);
  449. int damon_nr_running_ctxs(void);
  450. int damon_start(struct damon_ctx **ctxs, int nr_ctxs);
  451. int damon_stop(struct damon_ctx **ctxs, int nr_ctxs);
  452. #endif /* CONFIG_DAMON */
  453. #ifdef CONFIG_DAMON_VADDR
  454. bool damon_va_target_valid(void *t);
  455. void damon_va_set_primitives(struct damon_ctx *ctx);
  456. #endif /* CONFIG_DAMON_VADDR */
  457. #ifdef CONFIG_DAMON_PADDR
  458. bool damon_pa_target_valid(void *t);
  459. void damon_pa_set_primitives(struct damon_ctx *ctx);
  460. #endif /* CONFIG_DAMON_PADDR */
  461. #endif /* _DAMON_H */