stat.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Shows data access monitoring results in simple metrics.
  4. */
  5. #define pr_fmt(fmt) "damon-stat: " fmt
  6. #include <linux/damon.h>
  7. #include <linux/init.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/sort.h>
  11. #ifdef MODULE_PARAM_PREFIX
  12. #undef MODULE_PARAM_PREFIX
  13. #endif
  14. #define MODULE_PARAM_PREFIX "damon_stat."
  15. static int damon_stat_enabled_store(
  16. const char *val, const struct kernel_param *kp);
  17. static const struct kernel_param_ops enabled_param_ops = {
  18. .set = damon_stat_enabled_store,
  19. .get = param_get_bool,
  20. };
  21. static bool enabled __read_mostly = IS_ENABLED(
  22. CONFIG_DAMON_STAT_ENABLED_DEFAULT);
  23. module_param_cb(enabled, &enabled_param_ops, &enabled, 0600);
  24. MODULE_PARM_DESC(enabled, "Enable of disable DAMON_STAT");
  25. static unsigned long estimated_memory_bandwidth __read_mostly;
  26. module_param(estimated_memory_bandwidth, ulong, 0400);
  27. MODULE_PARM_DESC(estimated_memory_bandwidth,
  28. "Estimated memory bandwidth usage in bytes per second");
  29. static long memory_idle_ms_percentiles[101] = {0,};
  30. module_param_array(memory_idle_ms_percentiles, long, NULL, 0400);
  31. MODULE_PARM_DESC(memory_idle_ms_percentiles,
  32. "Memory idle time percentiles in milliseconds");
  33. static unsigned long aggr_interval_us;
  34. module_param(aggr_interval_us, ulong, 0400);
  35. MODULE_PARM_DESC(aggr_interval_us,
  36. "Current tuned aggregation interval in microseconds");
  37. static struct damon_ctx *damon_stat_context;
  38. static unsigned long damon_stat_last_refresh_jiffies;
  39. static void damon_stat_set_estimated_memory_bandwidth(struct damon_ctx *c)
  40. {
  41. struct damon_target *t;
  42. struct damon_region *r;
  43. unsigned long access_bytes = 0;
  44. damon_for_each_target(t, c) {
  45. damon_for_each_region(r, t)
  46. access_bytes += (r->ar.end - r->ar.start) *
  47. r->nr_accesses;
  48. }
  49. estimated_memory_bandwidth = access_bytes * USEC_PER_MSEC *
  50. MSEC_PER_SEC / c->attrs.aggr_interval;
  51. }
  52. static int damon_stat_idletime(const struct damon_region *r)
  53. {
  54. if (r->nr_accesses)
  55. return -1 * (r->age + 1);
  56. return r->age + 1;
  57. }
  58. static int damon_stat_cmp_regions(const void *a, const void *b)
  59. {
  60. const struct damon_region *ra = *(const struct damon_region **)a;
  61. const struct damon_region *rb = *(const struct damon_region **)b;
  62. return damon_stat_idletime(ra) - damon_stat_idletime(rb);
  63. }
  64. static int damon_stat_sort_regions(struct damon_ctx *c,
  65. struct damon_region ***sorted_ptr, int *nr_regions_ptr,
  66. unsigned long *total_sz_ptr)
  67. {
  68. struct damon_target *t;
  69. struct damon_region *r;
  70. struct damon_region **region_pointers;
  71. unsigned int nr_regions = 0;
  72. unsigned long total_sz = 0;
  73. damon_for_each_target(t, c) {
  74. /* there is only one target */
  75. region_pointers = kmalloc_objs(*region_pointers,
  76. damon_nr_regions(t));
  77. if (!region_pointers)
  78. return -ENOMEM;
  79. damon_for_each_region(r, t) {
  80. region_pointers[nr_regions++] = r;
  81. total_sz += r->ar.end - r->ar.start;
  82. }
  83. }
  84. sort(region_pointers, nr_regions, sizeof(*region_pointers),
  85. damon_stat_cmp_regions, NULL);
  86. *sorted_ptr = region_pointers;
  87. *nr_regions_ptr = nr_regions;
  88. *total_sz_ptr = total_sz;
  89. return 0;
  90. }
  91. static void damon_stat_set_idletime_percentiles(struct damon_ctx *c)
  92. {
  93. struct damon_region **sorted_regions, *region;
  94. int nr_regions;
  95. unsigned long total_sz, accounted_bytes = 0;
  96. int err, i, next_percentile = 0;
  97. err = damon_stat_sort_regions(c, &sorted_regions, &nr_regions,
  98. &total_sz);
  99. if (err)
  100. return;
  101. for (i = 0; i < nr_regions; i++) {
  102. region = sorted_regions[i];
  103. accounted_bytes += region->ar.end - region->ar.start;
  104. while (next_percentile <= accounted_bytes * 100 / total_sz)
  105. memory_idle_ms_percentiles[next_percentile++] =
  106. damon_stat_idletime(region) *
  107. (long)c->attrs.aggr_interval / USEC_PER_MSEC;
  108. }
  109. kfree(sorted_regions);
  110. }
  111. static int damon_stat_damon_call_fn(void *data)
  112. {
  113. struct damon_ctx *c = data;
  114. /* avoid unnecessarily frequent stat update */
  115. if (time_before_eq(jiffies, damon_stat_last_refresh_jiffies +
  116. msecs_to_jiffies(5 * MSEC_PER_SEC)))
  117. return 0;
  118. damon_stat_last_refresh_jiffies = jiffies;
  119. aggr_interval_us = c->attrs.aggr_interval;
  120. damon_stat_set_estimated_memory_bandwidth(c);
  121. damon_stat_set_idletime_percentiles(c);
  122. return 0;
  123. }
  124. struct damon_stat_system_ram_range_walk_arg {
  125. bool walked;
  126. struct resource res;
  127. };
  128. static int damon_stat_system_ram_walk_fn(struct resource *res, void *arg)
  129. {
  130. struct damon_stat_system_ram_range_walk_arg *a = arg;
  131. if (!a->walked) {
  132. a->walked = true;
  133. a->res.start = res->start;
  134. }
  135. a->res.end = res->end;
  136. return 0;
  137. }
  138. static unsigned long damon_stat_res_to_core_addr(resource_size_t ra,
  139. unsigned long addr_unit)
  140. {
  141. /*
  142. * Use div_u64() for avoiding linking errors related with __udivdi3,
  143. * __aeabi_uldivmod, or similar problems. This should also improve the
  144. * performance optimization (read div_u64() comment for the detail).
  145. */
  146. if (sizeof(ra) == 8 && sizeof(addr_unit) == 4)
  147. return div_u64(ra, addr_unit);
  148. return ra / addr_unit;
  149. }
  150. static int damon_stat_set_monitoring_region(struct damon_target *t,
  151. unsigned long addr_unit, unsigned long min_region_sz)
  152. {
  153. struct damon_addr_range addr_range;
  154. struct damon_stat_system_ram_range_walk_arg arg = {};
  155. walk_system_ram_res(0, -1, &arg, damon_stat_system_ram_walk_fn);
  156. if (!arg.walked)
  157. return -EINVAL;
  158. addr_range.start = damon_stat_res_to_core_addr(
  159. arg.res.start, addr_unit);
  160. addr_range.end = damon_stat_res_to_core_addr(
  161. arg.res.end + 1, addr_unit);
  162. if (addr_range.end <= addr_range.start)
  163. return -EINVAL;
  164. return damon_set_regions(t, &addr_range, 1, min_region_sz);
  165. }
  166. static struct damon_ctx *damon_stat_build_ctx(void)
  167. {
  168. struct damon_ctx *ctx;
  169. struct damon_attrs attrs;
  170. struct damon_target *target;
  171. ctx = damon_new_ctx();
  172. if (!ctx)
  173. return NULL;
  174. attrs = (struct damon_attrs) {
  175. .sample_interval = 5 * USEC_PER_MSEC,
  176. .aggr_interval = 100 * USEC_PER_MSEC,
  177. .ops_update_interval = 60 * USEC_PER_MSEC * MSEC_PER_SEC,
  178. .min_nr_regions = 10,
  179. .max_nr_regions = 1000,
  180. };
  181. /*
  182. * auto-tune sampling and aggregation interval aiming 4% DAMON-observed
  183. * accesses ratio, keeping sampling interval in [5ms, 10s] range.
  184. */
  185. attrs.intervals_goal = (struct damon_intervals_goal) {
  186. .access_bp = 400, .aggrs = 3,
  187. .min_sample_us = 5000, .max_sample_us = 10000000,
  188. };
  189. if (damon_set_attrs(ctx, &attrs))
  190. goto free_out;
  191. if (damon_select_ops(ctx, DAMON_OPS_PADDR))
  192. goto free_out;
  193. target = damon_new_target();
  194. if (!target)
  195. goto free_out;
  196. damon_add_target(ctx, target);
  197. if (damon_stat_set_monitoring_region(target, ctx->addr_unit,
  198. ctx->min_region_sz))
  199. goto free_out;
  200. return ctx;
  201. free_out:
  202. damon_destroy_ctx(ctx);
  203. return NULL;
  204. }
  205. static struct damon_call_control call_control = {
  206. .fn = damon_stat_damon_call_fn,
  207. .repeat = true,
  208. };
  209. static int damon_stat_start(void)
  210. {
  211. int err;
  212. if (damon_stat_context) {
  213. if (damon_is_running(damon_stat_context))
  214. return -EAGAIN;
  215. damon_destroy_ctx(damon_stat_context);
  216. }
  217. damon_stat_context = damon_stat_build_ctx();
  218. if (!damon_stat_context)
  219. return -ENOMEM;
  220. err = damon_start(&damon_stat_context, 1, true);
  221. if (err)
  222. return err;
  223. damon_stat_last_refresh_jiffies = jiffies;
  224. call_control.data = damon_stat_context;
  225. return damon_call(damon_stat_context, &call_control);
  226. }
  227. static void damon_stat_stop(void)
  228. {
  229. damon_stop(&damon_stat_context, 1);
  230. damon_destroy_ctx(damon_stat_context);
  231. damon_stat_context = NULL;
  232. }
  233. static int damon_stat_enabled_store(
  234. const char *val, const struct kernel_param *kp)
  235. {
  236. bool is_enabled = enabled;
  237. int err;
  238. err = kstrtobool(val, &enabled);
  239. if (err)
  240. return err;
  241. if (is_enabled == enabled)
  242. return 0;
  243. if (!damon_initialized())
  244. /*
  245. * probably called from command line parsing (parse_args()).
  246. * Cannot call damon_new_ctx(). Let damon_stat_init() handle.
  247. */
  248. return 0;
  249. if (enabled) {
  250. err = damon_stat_start();
  251. if (err)
  252. enabled = false;
  253. return err;
  254. }
  255. damon_stat_stop();
  256. return 0;
  257. }
  258. static int __init damon_stat_init(void)
  259. {
  260. int err = 0;
  261. if (!damon_initialized()) {
  262. err = -ENOMEM;
  263. goto out;
  264. }
  265. /* probably set via command line */
  266. if (enabled)
  267. err = damon_stat_start();
  268. out:
  269. if (err && enabled)
  270. enabled = false;
  271. return err;
  272. }
  273. module_init(damon_stat_init);