Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1 | #include "builtin.h" |
| 2 | #include "perf.h" |
| 3 | |
Arnaldo Carvalho de Melo | 0f7d2f1 | 2012-09-24 10:46:54 -0300 | [diff] [blame] | 4 | #include "util/evlist.h" |
Arnaldo Carvalho de Melo | fcf65bf | 2012-08-07 09:58:03 -0300 | [diff] [blame] | 5 | #include "util/evsel.h" |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 6 | #include "util/util.h" |
Taeung Song | 41840d2 | 2016-06-23 17:55:17 +0900 | [diff] [blame] | 7 | #include "util/config.h" |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 8 | #include "util/symbol.h" |
| 9 | #include "util/thread.h" |
| 10 | #include "util/header.h" |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 11 | #include "util/session.h" |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 12 | #include "util/tool.h" |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 13 | #include "util/callchain.h" |
David Ahern | 2a865bd | 2016-11-29 10:15:45 -0700 | [diff] [blame] | 14 | #include "util/time-utils.h" |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 15 | |
Josh Poimboeuf | 4b6ab94 | 2015-12-15 09:39:39 -0600 | [diff] [blame] | 16 | #include <subcmd/parse-options.h> |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 17 | #include "util/trace-event.h" |
Jiri Olsa | f5fc1412 | 2013-10-15 16:27:32 +0200 | [diff] [blame] | 18 | #include "util/data.h" |
Don Zickus | 4b62795 | 2014-04-07 14:55:23 -0400 | [diff] [blame] | 19 | #include "util/cpumap.h" |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 20 | |
| 21 | #include "util/debug.h" |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 22 | |
Arnaldo Carvalho de Melo | 877a7a1 | 2017-04-17 11:39:06 -0300 | [diff] [blame^] | 23 | #include <linux/kernel.h> |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 24 | #include <linux/rbtree.h> |
Arnaldo Carvalho de Melo | 8d9233f | 2013-01-24 22:24:57 -0300 | [diff] [blame] | 25 | #include <linux/string.h> |
Namhyung Kim | 77cfe38 | 2015-03-23 15:30:40 +0900 | [diff] [blame] | 26 | #include <locale.h> |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 27 | #include <regex.h> |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 28 | |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 29 | static int kmem_slab; |
| 30 | static int kmem_page; |
| 31 | |
| 32 | static long kmem_page_size; |
Namhyung Kim | 0c160d4 | 2015-04-21 13:55:06 +0900 | [diff] [blame] | 33 | static enum { |
| 34 | KMEM_SLAB, |
| 35 | KMEM_PAGE, |
| 36 | } kmem_default = KMEM_SLAB; /* for backward compatibility */ |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 37 | |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 38 | struct alloc_stat; |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 39 | typedef int (*sort_fn_t)(void *, void *); |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 40 | |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 41 | static int alloc_flag; |
| 42 | static int caller_flag; |
| 43 | |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 44 | static int alloc_lines = -1; |
| 45 | static int caller_lines = -1; |
| 46 | |
Li Zefan | 7707b6b | 2009-11-24 13:25:48 +0800 | [diff] [blame] | 47 | static bool raw_ip; |
| 48 | |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 49 | struct alloc_stat { |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 50 | u64 call_site; |
| 51 | u64 ptr; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 52 | u64 bytes_req; |
| 53 | u64 bytes_alloc; |
David Ahern | aa58e9a | 2016-11-25 14:42:13 -0700 | [diff] [blame] | 54 | u64 last_alloc; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 55 | u32 hit; |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 56 | u32 pingpong; |
| 57 | |
| 58 | short alloc_cpu; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 59 | |
| 60 | struct rb_node node; |
| 61 | }; |
| 62 | |
| 63 | static struct rb_root root_alloc_stat; |
| 64 | static struct rb_root root_alloc_sorted; |
| 65 | static struct rb_root root_caller_stat; |
| 66 | static struct rb_root root_caller_sorted; |
| 67 | |
David Ahern | aa58e9a | 2016-11-25 14:42:13 -0700 | [diff] [blame] | 68 | static unsigned long total_requested, total_allocated, total_freed; |
Li Zefan | 7d0d394 | 2009-11-24 13:26:31 +0800 | [diff] [blame] | 69 | static unsigned long nr_allocs, nr_cross_allocs; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 70 | |
David Ahern | 2a865bd | 2016-11-29 10:15:45 -0700 | [diff] [blame] | 71 | /* filters for controlling start and stop of time of analysis */ |
| 72 | static struct perf_time_interval ptime; |
| 73 | const char *time_str; |
| 74 | |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 75 | static int insert_alloc_stat(unsigned long call_site, unsigned long ptr, |
| 76 | int bytes_req, int bytes_alloc, int cpu) |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 77 | { |
| 78 | struct rb_node **node = &root_alloc_stat.rb_node; |
| 79 | struct rb_node *parent = NULL; |
| 80 | struct alloc_stat *data = NULL; |
| 81 | |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 82 | while (*node) { |
| 83 | parent = *node; |
| 84 | data = rb_entry(*node, struct alloc_stat, node); |
| 85 | |
| 86 | if (ptr > data->ptr) |
| 87 | node = &(*node)->rb_right; |
| 88 | else if (ptr < data->ptr) |
| 89 | node = &(*node)->rb_left; |
| 90 | else |
| 91 | break; |
| 92 | } |
| 93 | |
| 94 | if (data && data->ptr == ptr) { |
| 95 | data->hit++; |
| 96 | data->bytes_req += bytes_req; |
Wenji Huang | 4efb529 | 2009-12-21 17:52:55 +0800 | [diff] [blame] | 97 | data->bytes_alloc += bytes_alloc; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 98 | } else { |
| 99 | data = malloc(sizeof(*data)); |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 100 | if (!data) { |
| 101 | pr_err("%s: malloc failed\n", __func__); |
| 102 | return -1; |
| 103 | } |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 104 | data->ptr = ptr; |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 105 | data->pingpong = 0; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 106 | data->hit = 1; |
| 107 | data->bytes_req = bytes_req; |
| 108 | data->bytes_alloc = bytes_alloc; |
| 109 | |
| 110 | rb_link_node(&data->node, parent, node); |
| 111 | rb_insert_color(&data->node, &root_alloc_stat); |
| 112 | } |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 113 | data->call_site = call_site; |
| 114 | data->alloc_cpu = cpu; |
David Ahern | aa58e9a | 2016-11-25 14:42:13 -0700 | [diff] [blame] | 115 | data->last_alloc = bytes_alloc; |
| 116 | |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 117 | return 0; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 118 | } |
| 119 | |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 120 | static int insert_caller_stat(unsigned long call_site, |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 121 | int bytes_req, int bytes_alloc) |
| 122 | { |
| 123 | struct rb_node **node = &root_caller_stat.rb_node; |
| 124 | struct rb_node *parent = NULL; |
| 125 | struct alloc_stat *data = NULL; |
| 126 | |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 127 | while (*node) { |
| 128 | parent = *node; |
| 129 | data = rb_entry(*node, struct alloc_stat, node); |
| 130 | |
| 131 | if (call_site > data->call_site) |
| 132 | node = &(*node)->rb_right; |
| 133 | else if (call_site < data->call_site) |
| 134 | node = &(*node)->rb_left; |
| 135 | else |
| 136 | break; |
| 137 | } |
| 138 | |
| 139 | if (data && data->call_site == call_site) { |
| 140 | data->hit++; |
| 141 | data->bytes_req += bytes_req; |
Wenji Huang | 4efb529 | 2009-12-21 17:52:55 +0800 | [diff] [blame] | 142 | data->bytes_alloc += bytes_alloc; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 143 | } else { |
| 144 | data = malloc(sizeof(*data)); |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 145 | if (!data) { |
| 146 | pr_err("%s: malloc failed\n", __func__); |
| 147 | return -1; |
| 148 | } |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 149 | data->call_site = call_site; |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 150 | data->pingpong = 0; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 151 | data->hit = 1; |
| 152 | data->bytes_req = bytes_req; |
| 153 | data->bytes_alloc = bytes_alloc; |
| 154 | |
| 155 | rb_link_node(&data->node, parent, node); |
| 156 | rb_insert_color(&data->node, &root_caller_stat); |
| 157 | } |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 158 | |
| 159 | return 0; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 160 | } |
| 161 | |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 162 | static int perf_evsel__process_alloc_event(struct perf_evsel *evsel, |
Arnaldo Carvalho de Melo | 0f7d2f1 | 2012-09-24 10:46:54 -0300 | [diff] [blame] | 163 | struct perf_sample *sample) |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 164 | { |
Arnaldo Carvalho de Melo | 0f7d2f1 | 2012-09-24 10:46:54 -0300 | [diff] [blame] | 165 | unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr"), |
| 166 | call_site = perf_evsel__intval(evsel, sample, "call_site"); |
| 167 | int bytes_req = perf_evsel__intval(evsel, sample, "bytes_req"), |
| 168 | bytes_alloc = perf_evsel__intval(evsel, sample, "bytes_alloc"); |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 169 | |
Arnaldo Carvalho de Melo | 0f7d2f1 | 2012-09-24 10:46:54 -0300 | [diff] [blame] | 170 | if (insert_alloc_stat(call_site, ptr, bytes_req, bytes_alloc, sample->cpu) || |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 171 | insert_caller_stat(call_site, bytes_req, bytes_alloc)) |
| 172 | return -1; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 173 | |
| 174 | total_requested += bytes_req; |
| 175 | total_allocated += bytes_alloc; |
Li Zefan | 7d0d394 | 2009-11-24 13:26:31 +0800 | [diff] [blame] | 176 | |
Arnaldo Carvalho de Melo | 0f7d2f1 | 2012-09-24 10:46:54 -0300 | [diff] [blame] | 177 | nr_allocs++; |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | static int perf_evsel__process_alloc_node_event(struct perf_evsel *evsel, |
| 182 | struct perf_sample *sample) |
| 183 | { |
| 184 | int ret = perf_evsel__process_alloc_event(evsel, sample); |
| 185 | |
| 186 | if (!ret) { |
Don Zickus | 4b62795 | 2014-04-07 14:55:23 -0400 | [diff] [blame] | 187 | int node1 = cpu__get_node(sample->cpu), |
Arnaldo Carvalho de Melo | 0f7d2f1 | 2012-09-24 10:46:54 -0300 | [diff] [blame] | 188 | node2 = perf_evsel__intval(evsel, sample, "node"); |
| 189 | |
Li Zefan | 7d0d394 | 2009-11-24 13:26:31 +0800 | [diff] [blame] | 190 | if (node1 != node2) |
| 191 | nr_cross_allocs++; |
| 192 | } |
Arnaldo Carvalho de Melo | 0f7d2f1 | 2012-09-24 10:46:54 -0300 | [diff] [blame] | 193 | |
| 194 | return ret; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 195 | } |
| 196 | |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 197 | static int ptr_cmp(void *, void *); |
| 198 | static int slab_callsite_cmp(void *, void *); |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 199 | |
| 200 | static struct alloc_stat *search_alloc_stat(unsigned long ptr, |
| 201 | unsigned long call_site, |
| 202 | struct rb_root *root, |
| 203 | sort_fn_t sort_fn) |
| 204 | { |
| 205 | struct rb_node *node = root->rb_node; |
| 206 | struct alloc_stat key = { .ptr = ptr, .call_site = call_site }; |
| 207 | |
| 208 | while (node) { |
| 209 | struct alloc_stat *data; |
| 210 | int cmp; |
| 211 | |
| 212 | data = rb_entry(node, struct alloc_stat, node); |
| 213 | |
| 214 | cmp = sort_fn(&key, data); |
| 215 | if (cmp < 0) |
| 216 | node = node->rb_left; |
| 217 | else if (cmp > 0) |
| 218 | node = node->rb_right; |
| 219 | else |
| 220 | return data; |
| 221 | } |
| 222 | return NULL; |
| 223 | } |
| 224 | |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 225 | static int perf_evsel__process_free_event(struct perf_evsel *evsel, |
| 226 | struct perf_sample *sample) |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 227 | { |
Arnaldo Carvalho de Melo | 0f7d2f1 | 2012-09-24 10:46:54 -0300 | [diff] [blame] | 228 | unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr"); |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 229 | struct alloc_stat *s_alloc, *s_caller; |
| 230 | |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 231 | s_alloc = search_alloc_stat(ptr, 0, &root_alloc_stat, ptr_cmp); |
| 232 | if (!s_alloc) |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 233 | return 0; |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 234 | |
David Ahern | aa58e9a | 2016-11-25 14:42:13 -0700 | [diff] [blame] | 235 | total_freed += s_alloc->last_alloc; |
| 236 | |
Arnaldo Carvalho de Melo | 22ad798 | 2012-08-07 10:56:43 -0300 | [diff] [blame] | 237 | if ((short)sample->cpu != s_alloc->alloc_cpu) { |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 238 | s_alloc->pingpong++; |
| 239 | |
| 240 | s_caller = search_alloc_stat(0, s_alloc->call_site, |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 241 | &root_caller_stat, |
| 242 | slab_callsite_cmp); |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 243 | if (!s_caller) |
| 244 | return -1; |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 245 | s_caller->pingpong++; |
| 246 | } |
| 247 | s_alloc->alloc_cpu = -1; |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 248 | |
| 249 | return 0; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 250 | } |
| 251 | |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 252 | static u64 total_page_alloc_bytes; |
| 253 | static u64 total_page_free_bytes; |
| 254 | static u64 total_page_nomatch_bytes; |
| 255 | static u64 total_page_fail_bytes; |
| 256 | static unsigned long nr_page_allocs; |
| 257 | static unsigned long nr_page_frees; |
| 258 | static unsigned long nr_page_fails; |
| 259 | static unsigned long nr_page_nomatch; |
| 260 | |
| 261 | static bool use_pfn; |
Namhyung Kim | 2a7ef02 | 2015-04-21 13:55:04 +0900 | [diff] [blame] | 262 | static bool live_page; |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 263 | static struct perf_session *kmem_session; |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 264 | |
| 265 | #define MAX_MIGRATE_TYPES 6 |
| 266 | #define MAX_PAGE_ORDER 11 |
| 267 | |
| 268 | static int order_stats[MAX_PAGE_ORDER][MAX_MIGRATE_TYPES]; |
| 269 | |
| 270 | struct page_stat { |
| 271 | struct rb_node node; |
| 272 | u64 page; |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 273 | u64 callsite; |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 274 | int order; |
| 275 | unsigned gfp_flags; |
| 276 | unsigned migrate_type; |
| 277 | u64 alloc_bytes; |
| 278 | u64 free_bytes; |
| 279 | int nr_alloc; |
| 280 | int nr_free; |
| 281 | }; |
| 282 | |
Namhyung Kim | 2a7ef02 | 2015-04-21 13:55:04 +0900 | [diff] [blame] | 283 | static struct rb_root page_live_tree; |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 284 | static struct rb_root page_alloc_tree; |
| 285 | static struct rb_root page_alloc_sorted; |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 286 | static struct rb_root page_caller_tree; |
| 287 | static struct rb_root page_caller_sorted; |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 288 | |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 289 | struct alloc_func { |
| 290 | u64 start; |
| 291 | u64 end; |
| 292 | char *name; |
| 293 | }; |
| 294 | |
| 295 | static int nr_alloc_funcs; |
| 296 | static struct alloc_func *alloc_func_list; |
| 297 | |
| 298 | static int funcmp(const void *a, const void *b) |
| 299 | { |
| 300 | const struct alloc_func *fa = a; |
| 301 | const struct alloc_func *fb = b; |
| 302 | |
| 303 | if (fa->start > fb->start) |
| 304 | return 1; |
| 305 | else |
| 306 | return -1; |
| 307 | } |
| 308 | |
| 309 | static int callcmp(const void *a, const void *b) |
| 310 | { |
| 311 | const struct alloc_func *fa = a; |
| 312 | const struct alloc_func *fb = b; |
| 313 | |
| 314 | if (fb->start <= fa->start && fa->end < fb->end) |
| 315 | return 0; |
| 316 | |
| 317 | if (fa->start > fb->start) |
| 318 | return 1; |
| 319 | else |
| 320 | return -1; |
| 321 | } |
| 322 | |
| 323 | static int build_alloc_func_list(void) |
| 324 | { |
| 325 | int ret; |
| 326 | struct map *kernel_map; |
| 327 | struct symbol *sym; |
| 328 | struct rb_node *node; |
| 329 | struct alloc_func *func; |
| 330 | struct machine *machine = &kmem_session->machines.host; |
| 331 | regex_t alloc_func_regex; |
| 332 | const char pattern[] = "^_?_?(alloc|get_free|get_zeroed)_pages?"; |
| 333 | |
| 334 | ret = regcomp(&alloc_func_regex, pattern, REG_EXTENDED); |
| 335 | if (ret) { |
| 336 | char err[BUFSIZ]; |
| 337 | |
| 338 | regerror(ret, &alloc_func_regex, err, sizeof(err)); |
| 339 | pr_err("Invalid regex: %s\n%s", pattern, err); |
| 340 | return -EINVAL; |
| 341 | } |
| 342 | |
Arnaldo Carvalho de Melo | a5e813c | 2015-09-30 11:54:04 -0300 | [diff] [blame] | 343 | kernel_map = machine__kernel_map(machine); |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 344 | if (map__load(kernel_map) < 0) { |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 345 | pr_err("cannot load kernel map\n"); |
| 346 | return -ENOENT; |
| 347 | } |
| 348 | |
| 349 | map__for_each_symbol(kernel_map, sym, node) { |
| 350 | if (regexec(&alloc_func_regex, sym->name, 0, NULL, 0)) |
| 351 | continue; |
| 352 | |
| 353 | func = realloc(alloc_func_list, |
| 354 | (nr_alloc_funcs + 1) * sizeof(*func)); |
| 355 | if (func == NULL) |
| 356 | return -ENOMEM; |
| 357 | |
| 358 | pr_debug("alloc func: %s\n", sym->name); |
| 359 | func[nr_alloc_funcs].start = sym->start; |
| 360 | func[nr_alloc_funcs].end = sym->end; |
| 361 | func[nr_alloc_funcs].name = sym->name; |
| 362 | |
| 363 | alloc_func_list = func; |
| 364 | nr_alloc_funcs++; |
| 365 | } |
| 366 | |
| 367 | qsort(alloc_func_list, nr_alloc_funcs, sizeof(*func), funcmp); |
| 368 | |
| 369 | regfree(&alloc_func_regex); |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | /* |
| 374 | * Find first non-memory allocation function from callchain. |
| 375 | * The allocation functions are in the 'alloc_func_list'. |
| 376 | */ |
| 377 | static u64 find_callsite(struct perf_evsel *evsel, struct perf_sample *sample) |
| 378 | { |
| 379 | struct addr_location al; |
| 380 | struct machine *machine = &kmem_session->machines.host; |
| 381 | struct callchain_cursor_node *node; |
| 382 | |
| 383 | if (alloc_func_list == NULL) { |
| 384 | if (build_alloc_func_list() < 0) |
| 385 | goto out; |
| 386 | } |
| 387 | |
| 388 | al.thread = machine__findnew_thread(machine, sample->pid, sample->tid); |
Arnaldo Carvalho de Melo | 91d7b2d | 2016-04-14 14:48:07 -0300 | [diff] [blame] | 389 | sample__resolve_callchain(sample, &callchain_cursor, NULL, evsel, &al, 16); |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 390 | |
| 391 | callchain_cursor_commit(&callchain_cursor); |
| 392 | while (true) { |
| 393 | struct alloc_func key, *caller; |
| 394 | u64 addr; |
| 395 | |
| 396 | node = callchain_cursor_current(&callchain_cursor); |
| 397 | if (node == NULL) |
| 398 | break; |
| 399 | |
| 400 | key.start = key.end = node->ip; |
| 401 | caller = bsearch(&key, alloc_func_list, nr_alloc_funcs, |
| 402 | sizeof(key), callcmp); |
| 403 | if (!caller) { |
| 404 | /* found */ |
| 405 | if (node->map) |
| 406 | addr = map__unmap_ip(node->map, node->ip); |
| 407 | else |
| 408 | addr = node->ip; |
| 409 | |
| 410 | return addr; |
| 411 | } else |
| 412 | pr_debug3("skipping alloc function: %s\n", caller->name); |
| 413 | |
| 414 | callchain_cursor_advance(&callchain_cursor); |
| 415 | } |
| 416 | |
| 417 | out: |
| 418 | pr_debug2("unknown callsite: %"PRIx64 "\n", sample->ip); |
| 419 | return sample->ip; |
| 420 | } |
| 421 | |
Namhyung Kim | 2a7ef02 | 2015-04-21 13:55:04 +0900 | [diff] [blame] | 422 | struct sort_dimension { |
| 423 | const char name[20]; |
| 424 | sort_fn_t cmp; |
| 425 | struct list_head list; |
| 426 | }; |
| 427 | |
| 428 | static LIST_HEAD(page_alloc_sort_input); |
| 429 | static LIST_HEAD(page_caller_sort_input); |
| 430 | |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 431 | static struct page_stat * |
Namhyung Kim | 2a7ef02 | 2015-04-21 13:55:04 +0900 | [diff] [blame] | 432 | __page_stat__findnew_page(struct page_stat *pstat, bool create) |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 433 | { |
Namhyung Kim | 2a7ef02 | 2015-04-21 13:55:04 +0900 | [diff] [blame] | 434 | struct rb_node **node = &page_live_tree.rb_node; |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 435 | struct rb_node *parent = NULL; |
| 436 | struct page_stat *data; |
| 437 | |
| 438 | while (*node) { |
| 439 | s64 cmp; |
| 440 | |
| 441 | parent = *node; |
| 442 | data = rb_entry(*node, struct page_stat, node); |
| 443 | |
Namhyung Kim | 2a7ef02 | 2015-04-21 13:55:04 +0900 | [diff] [blame] | 444 | cmp = data->page - pstat->page; |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 445 | if (cmp < 0) |
| 446 | node = &parent->rb_left; |
| 447 | else if (cmp > 0) |
| 448 | node = &parent->rb_right; |
| 449 | else |
| 450 | return data; |
| 451 | } |
| 452 | |
| 453 | if (!create) |
| 454 | return NULL; |
| 455 | |
| 456 | data = zalloc(sizeof(*data)); |
| 457 | if (data != NULL) { |
Namhyung Kim | 2a7ef02 | 2015-04-21 13:55:04 +0900 | [diff] [blame] | 458 | data->page = pstat->page; |
| 459 | data->order = pstat->order; |
| 460 | data->gfp_flags = pstat->gfp_flags; |
| 461 | data->migrate_type = pstat->migrate_type; |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 462 | |
| 463 | rb_link_node(&data->node, parent, node); |
Namhyung Kim | 2a7ef02 | 2015-04-21 13:55:04 +0900 | [diff] [blame] | 464 | rb_insert_color(&data->node, &page_live_tree); |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | return data; |
| 468 | } |
| 469 | |
Namhyung Kim | 2a7ef02 | 2015-04-21 13:55:04 +0900 | [diff] [blame] | 470 | static struct page_stat *page_stat__find_page(struct page_stat *pstat) |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 471 | { |
Namhyung Kim | 2a7ef02 | 2015-04-21 13:55:04 +0900 | [diff] [blame] | 472 | return __page_stat__findnew_page(pstat, false); |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 473 | } |
| 474 | |
Namhyung Kim | 2a7ef02 | 2015-04-21 13:55:04 +0900 | [diff] [blame] | 475 | static struct page_stat *page_stat__findnew_page(struct page_stat *pstat) |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 476 | { |
Namhyung Kim | 2a7ef02 | 2015-04-21 13:55:04 +0900 | [diff] [blame] | 477 | return __page_stat__findnew_page(pstat, true); |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 478 | } |
| 479 | |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 480 | static struct page_stat * |
| 481 | __page_stat__findnew_alloc(struct page_stat *pstat, bool create) |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 482 | { |
| 483 | struct rb_node **node = &page_alloc_tree.rb_node; |
| 484 | struct rb_node *parent = NULL; |
| 485 | struct page_stat *data; |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 486 | struct sort_dimension *sort; |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 487 | |
| 488 | while (*node) { |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 489 | int cmp = 0; |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 490 | |
| 491 | parent = *node; |
| 492 | data = rb_entry(*node, struct page_stat, node); |
| 493 | |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 494 | list_for_each_entry(sort, &page_alloc_sort_input, list) { |
| 495 | cmp = sort->cmp(pstat, data); |
| 496 | if (cmp) |
| 497 | break; |
| 498 | } |
| 499 | |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 500 | if (cmp < 0) |
| 501 | node = &parent->rb_left; |
| 502 | else if (cmp > 0) |
| 503 | node = &parent->rb_right; |
| 504 | else |
| 505 | return data; |
| 506 | } |
| 507 | |
| 508 | if (!create) |
| 509 | return NULL; |
| 510 | |
| 511 | data = zalloc(sizeof(*data)); |
| 512 | if (data != NULL) { |
David Ahern | 6b1a275 | 2015-04-14 13:49:33 -0400 | [diff] [blame] | 513 | data->page = pstat->page; |
| 514 | data->order = pstat->order; |
| 515 | data->gfp_flags = pstat->gfp_flags; |
| 516 | data->migrate_type = pstat->migrate_type; |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 517 | |
| 518 | rb_link_node(&data->node, parent, node); |
| 519 | rb_insert_color(&data->node, &page_alloc_tree); |
| 520 | } |
| 521 | |
| 522 | return data; |
| 523 | } |
| 524 | |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 525 | static struct page_stat *page_stat__find_alloc(struct page_stat *pstat) |
| 526 | { |
| 527 | return __page_stat__findnew_alloc(pstat, false); |
| 528 | } |
| 529 | |
| 530 | static struct page_stat *page_stat__findnew_alloc(struct page_stat *pstat) |
| 531 | { |
| 532 | return __page_stat__findnew_alloc(pstat, true); |
| 533 | } |
| 534 | |
| 535 | static struct page_stat * |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 536 | __page_stat__findnew_caller(struct page_stat *pstat, bool create) |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 537 | { |
| 538 | struct rb_node **node = &page_caller_tree.rb_node; |
| 539 | struct rb_node *parent = NULL; |
| 540 | struct page_stat *data; |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 541 | struct sort_dimension *sort; |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 542 | |
| 543 | while (*node) { |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 544 | int cmp = 0; |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 545 | |
| 546 | parent = *node; |
| 547 | data = rb_entry(*node, struct page_stat, node); |
| 548 | |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 549 | list_for_each_entry(sort, &page_caller_sort_input, list) { |
| 550 | cmp = sort->cmp(pstat, data); |
| 551 | if (cmp) |
| 552 | break; |
| 553 | } |
| 554 | |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 555 | if (cmp < 0) |
| 556 | node = &parent->rb_left; |
| 557 | else if (cmp > 0) |
| 558 | node = &parent->rb_right; |
| 559 | else |
| 560 | return data; |
| 561 | } |
| 562 | |
| 563 | if (!create) |
| 564 | return NULL; |
| 565 | |
| 566 | data = zalloc(sizeof(*data)); |
| 567 | if (data != NULL) { |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 568 | data->callsite = pstat->callsite; |
| 569 | data->order = pstat->order; |
| 570 | data->gfp_flags = pstat->gfp_flags; |
| 571 | data->migrate_type = pstat->migrate_type; |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 572 | |
| 573 | rb_link_node(&data->node, parent, node); |
| 574 | rb_insert_color(&data->node, &page_caller_tree); |
| 575 | } |
| 576 | |
| 577 | return data; |
| 578 | } |
| 579 | |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 580 | static struct page_stat *page_stat__find_caller(struct page_stat *pstat) |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 581 | { |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 582 | return __page_stat__findnew_caller(pstat, false); |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 583 | } |
| 584 | |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 585 | static struct page_stat *page_stat__findnew_caller(struct page_stat *pstat) |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 586 | { |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 587 | return __page_stat__findnew_caller(pstat, true); |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 588 | } |
| 589 | |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 590 | static bool valid_page(u64 pfn_or_page) |
| 591 | { |
| 592 | if (use_pfn && pfn_or_page == -1UL) |
| 593 | return false; |
| 594 | if (!use_pfn && pfn_or_page == 0) |
| 595 | return false; |
| 596 | return true; |
| 597 | } |
| 598 | |
Namhyung Kim | 0e11115 | 2015-04-21 13:55:05 +0900 | [diff] [blame] | 599 | struct gfp_flag { |
| 600 | unsigned int flags; |
| 601 | char *compact_str; |
| 602 | char *human_readable; |
| 603 | }; |
| 604 | |
| 605 | static struct gfp_flag *gfps; |
| 606 | static int nr_gfps; |
| 607 | |
| 608 | static int gfpcmp(const void *a, const void *b) |
| 609 | { |
| 610 | const struct gfp_flag *fa = a; |
| 611 | const struct gfp_flag *fb = b; |
| 612 | |
| 613 | return fa->flags - fb->flags; |
| 614 | } |
| 615 | |
Vlastimil Babka | 420adbe9 | 2016-03-15 14:55:52 -0700 | [diff] [blame] | 616 | /* see include/trace/events/mmflags.h */ |
Namhyung Kim | 0e11115 | 2015-04-21 13:55:05 +0900 | [diff] [blame] | 617 | static const struct { |
| 618 | const char *original; |
| 619 | const char *compact; |
| 620 | } gfp_compact_table[] = { |
| 621 | { "GFP_TRANSHUGE", "THP" }, |
Vlastimil Babka | 2516035 | 2016-07-28 15:49:25 -0700 | [diff] [blame] | 622 | { "GFP_TRANSHUGE_LIGHT", "THL" }, |
Namhyung Kim | 0e11115 | 2015-04-21 13:55:05 +0900 | [diff] [blame] | 623 | { "GFP_HIGHUSER_MOVABLE", "HUM" }, |
| 624 | { "GFP_HIGHUSER", "HU" }, |
| 625 | { "GFP_USER", "U" }, |
| 626 | { "GFP_TEMPORARY", "TMP" }, |
Vlastimil Babka | 14e0a21 | 2016-03-15 14:55:49 -0700 | [diff] [blame] | 627 | { "GFP_KERNEL_ACCOUNT", "KAC" }, |
Namhyung Kim | 0e11115 | 2015-04-21 13:55:05 +0900 | [diff] [blame] | 628 | { "GFP_KERNEL", "K" }, |
| 629 | { "GFP_NOFS", "NF" }, |
| 630 | { "GFP_ATOMIC", "A" }, |
| 631 | { "GFP_NOIO", "NI" }, |
Namhyung Kim | 0e11115 | 2015-04-21 13:55:05 +0900 | [diff] [blame] | 632 | { "GFP_NOWAIT", "NW" }, |
Vlastimil Babka | 14e0a21 | 2016-03-15 14:55:49 -0700 | [diff] [blame] | 633 | { "GFP_DMA", "D" }, |
| 634 | { "__GFP_HIGHMEM", "HM" }, |
| 635 | { "GFP_DMA32", "D32" }, |
| 636 | { "__GFP_HIGH", "H" }, |
| 637 | { "__GFP_ATOMIC", "_A" }, |
| 638 | { "__GFP_IO", "I" }, |
| 639 | { "__GFP_FS", "F" }, |
| 640 | { "__GFP_COLD", "CO" }, |
| 641 | { "__GFP_NOWARN", "NWR" }, |
| 642 | { "__GFP_REPEAT", "R" }, |
| 643 | { "__GFP_NOFAIL", "NF" }, |
| 644 | { "__GFP_NORETRY", "NR" }, |
| 645 | { "__GFP_COMP", "C" }, |
| 646 | { "__GFP_ZERO", "Z" }, |
| 647 | { "__GFP_NOMEMALLOC", "NMA" }, |
| 648 | { "__GFP_MEMALLOC", "MA" }, |
| 649 | { "__GFP_HARDWALL", "HW" }, |
| 650 | { "__GFP_THISNODE", "TN" }, |
| 651 | { "__GFP_RECLAIMABLE", "RC" }, |
| 652 | { "__GFP_MOVABLE", "M" }, |
| 653 | { "__GFP_ACCOUNT", "AC" }, |
| 654 | { "__GFP_NOTRACK", "NT" }, |
| 655 | { "__GFP_WRITE", "WR" }, |
| 656 | { "__GFP_RECLAIM", "R" }, |
| 657 | { "__GFP_DIRECT_RECLAIM", "DR" }, |
| 658 | { "__GFP_KSWAPD_RECLAIM", "KR" }, |
Namhyung Kim | 0e11115 | 2015-04-21 13:55:05 +0900 | [diff] [blame] | 659 | }; |
| 660 | |
| 661 | static size_t max_gfp_len; |
| 662 | |
| 663 | static char *compact_gfp_flags(char *gfp_flags) |
| 664 | { |
| 665 | char *orig_flags = strdup(gfp_flags); |
| 666 | char *new_flags = NULL; |
Arnaldo Carvalho de Melo | b236512 | 2015-05-29 09:48:13 -0300 | [diff] [blame] | 667 | char *str, *pos = NULL; |
Namhyung Kim | 0e11115 | 2015-04-21 13:55:05 +0900 | [diff] [blame] | 668 | size_t len = 0; |
| 669 | |
| 670 | if (orig_flags == NULL) |
| 671 | return NULL; |
| 672 | |
| 673 | str = strtok_r(orig_flags, "|", &pos); |
| 674 | while (str) { |
| 675 | size_t i; |
| 676 | char *new; |
| 677 | const char *cpt; |
| 678 | |
| 679 | for (i = 0; i < ARRAY_SIZE(gfp_compact_table); i++) { |
| 680 | if (strcmp(gfp_compact_table[i].original, str)) |
| 681 | continue; |
| 682 | |
| 683 | cpt = gfp_compact_table[i].compact; |
| 684 | new = realloc(new_flags, len + strlen(cpt) + 2); |
| 685 | if (new == NULL) { |
| 686 | free(new_flags); |
| 687 | return NULL; |
| 688 | } |
| 689 | |
| 690 | new_flags = new; |
| 691 | |
| 692 | if (!len) { |
| 693 | strcpy(new_flags, cpt); |
| 694 | } else { |
| 695 | strcat(new_flags, "|"); |
| 696 | strcat(new_flags, cpt); |
| 697 | len++; |
| 698 | } |
| 699 | |
| 700 | len += strlen(cpt); |
| 701 | } |
| 702 | |
| 703 | str = strtok_r(NULL, "|", &pos); |
| 704 | } |
| 705 | |
| 706 | if (max_gfp_len < len) |
| 707 | max_gfp_len = len; |
| 708 | |
| 709 | free(orig_flags); |
| 710 | return new_flags; |
| 711 | } |
| 712 | |
| 713 | static char *compact_gfp_string(unsigned long gfp_flags) |
| 714 | { |
| 715 | struct gfp_flag key = { |
| 716 | .flags = gfp_flags, |
| 717 | }; |
| 718 | struct gfp_flag *gfp; |
| 719 | |
| 720 | gfp = bsearch(&key, gfps, nr_gfps, sizeof(*gfps), gfpcmp); |
| 721 | if (gfp) |
| 722 | return gfp->compact_str; |
| 723 | |
| 724 | return NULL; |
| 725 | } |
| 726 | |
| 727 | static int parse_gfp_flags(struct perf_evsel *evsel, struct perf_sample *sample, |
| 728 | unsigned int gfp_flags) |
| 729 | { |
| 730 | struct pevent_record record = { |
| 731 | .cpu = sample->cpu, |
| 732 | .data = sample->raw_data, |
| 733 | .size = sample->raw_size, |
| 734 | }; |
| 735 | struct trace_seq seq; |
Arnaldo Carvalho de Melo | 08a9b98 | 2015-05-11 11:41:17 -0300 | [diff] [blame] | 736 | char *str, *pos = NULL; |
Namhyung Kim | 0e11115 | 2015-04-21 13:55:05 +0900 | [diff] [blame] | 737 | |
| 738 | if (nr_gfps) { |
| 739 | struct gfp_flag key = { |
| 740 | .flags = gfp_flags, |
| 741 | }; |
| 742 | |
| 743 | if (bsearch(&key, gfps, nr_gfps, sizeof(*gfps), gfpcmp)) |
| 744 | return 0; |
| 745 | } |
| 746 | |
| 747 | trace_seq_init(&seq); |
| 748 | pevent_event_info(&seq, evsel->tp_format, &record); |
| 749 | |
| 750 | str = strtok_r(seq.buffer, " ", &pos); |
| 751 | while (str) { |
| 752 | if (!strncmp(str, "gfp_flags=", 10)) { |
| 753 | struct gfp_flag *new; |
| 754 | |
| 755 | new = realloc(gfps, (nr_gfps + 1) * sizeof(*gfps)); |
| 756 | if (new == NULL) |
| 757 | return -ENOMEM; |
| 758 | |
| 759 | gfps = new; |
| 760 | new += nr_gfps++; |
| 761 | |
| 762 | new->flags = gfp_flags; |
| 763 | new->human_readable = strdup(str + 10); |
| 764 | new->compact_str = compact_gfp_flags(str + 10); |
| 765 | if (!new->human_readable || !new->compact_str) |
| 766 | return -ENOMEM; |
| 767 | |
| 768 | qsort(gfps, nr_gfps, sizeof(*gfps), gfpcmp); |
| 769 | } |
| 770 | |
| 771 | str = strtok_r(NULL, " ", &pos); |
| 772 | } |
| 773 | |
| 774 | trace_seq_destroy(&seq); |
| 775 | return 0; |
| 776 | } |
| 777 | |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 778 | static int perf_evsel__process_page_alloc_event(struct perf_evsel *evsel, |
| 779 | struct perf_sample *sample) |
| 780 | { |
| 781 | u64 page; |
| 782 | unsigned int order = perf_evsel__intval(evsel, sample, "order"); |
| 783 | unsigned int gfp_flags = perf_evsel__intval(evsel, sample, "gfp_flags"); |
| 784 | unsigned int migrate_type = perf_evsel__intval(evsel, sample, |
| 785 | "migratetype"); |
| 786 | u64 bytes = kmem_page_size << order; |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 787 | u64 callsite; |
David Ahern | 6b1a275 | 2015-04-14 13:49:33 -0400 | [diff] [blame] | 788 | struct page_stat *pstat; |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 789 | struct page_stat this = { |
| 790 | .order = order, |
| 791 | .gfp_flags = gfp_flags, |
| 792 | .migrate_type = migrate_type, |
| 793 | }; |
| 794 | |
| 795 | if (use_pfn) |
| 796 | page = perf_evsel__intval(evsel, sample, "pfn"); |
| 797 | else |
| 798 | page = perf_evsel__intval(evsel, sample, "page"); |
| 799 | |
| 800 | nr_page_allocs++; |
| 801 | total_page_alloc_bytes += bytes; |
| 802 | |
| 803 | if (!valid_page(page)) { |
| 804 | nr_page_fails++; |
| 805 | total_page_fail_bytes += bytes; |
| 806 | |
| 807 | return 0; |
| 808 | } |
| 809 | |
Namhyung Kim | 0e11115 | 2015-04-21 13:55:05 +0900 | [diff] [blame] | 810 | if (parse_gfp_flags(evsel, sample, gfp_flags) < 0) |
| 811 | return -1; |
| 812 | |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 813 | callsite = find_callsite(evsel, sample); |
| 814 | |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 815 | /* |
| 816 | * This is to find the current page (with correct gfp flags and |
| 817 | * migrate type) at free event. |
| 818 | */ |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 819 | this.page = page; |
Namhyung Kim | 2a7ef02 | 2015-04-21 13:55:04 +0900 | [diff] [blame] | 820 | pstat = page_stat__findnew_page(&this); |
David Ahern | 6b1a275 | 2015-04-14 13:49:33 -0400 | [diff] [blame] | 821 | if (pstat == NULL) |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 822 | return -ENOMEM; |
| 823 | |
David Ahern | 6b1a275 | 2015-04-14 13:49:33 -0400 | [diff] [blame] | 824 | pstat->nr_alloc++; |
| 825 | pstat->alloc_bytes += bytes; |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 826 | pstat->callsite = callsite; |
| 827 | |
Namhyung Kim | 2a7ef02 | 2015-04-21 13:55:04 +0900 | [diff] [blame] | 828 | if (!live_page) { |
| 829 | pstat = page_stat__findnew_alloc(&this); |
| 830 | if (pstat == NULL) |
| 831 | return -ENOMEM; |
| 832 | |
| 833 | pstat->nr_alloc++; |
| 834 | pstat->alloc_bytes += bytes; |
| 835 | pstat->callsite = callsite; |
| 836 | } |
| 837 | |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 838 | this.callsite = callsite; |
| 839 | pstat = page_stat__findnew_caller(&this); |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 840 | if (pstat == NULL) |
| 841 | return -ENOMEM; |
| 842 | |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 843 | pstat->nr_alloc++; |
| 844 | pstat->alloc_bytes += bytes; |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 845 | |
| 846 | order_stats[order][migrate_type]++; |
| 847 | |
| 848 | return 0; |
| 849 | } |
| 850 | |
| 851 | static int perf_evsel__process_page_free_event(struct perf_evsel *evsel, |
| 852 | struct perf_sample *sample) |
| 853 | { |
| 854 | u64 page; |
| 855 | unsigned int order = perf_evsel__intval(evsel, sample, "order"); |
| 856 | u64 bytes = kmem_page_size << order; |
David Ahern | 6b1a275 | 2015-04-14 13:49:33 -0400 | [diff] [blame] | 857 | struct page_stat *pstat; |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 858 | struct page_stat this = { |
| 859 | .order = order, |
| 860 | }; |
| 861 | |
| 862 | if (use_pfn) |
| 863 | page = perf_evsel__intval(evsel, sample, "pfn"); |
| 864 | else |
| 865 | page = perf_evsel__intval(evsel, sample, "page"); |
| 866 | |
| 867 | nr_page_frees++; |
| 868 | total_page_free_bytes += bytes; |
| 869 | |
Namhyung Kim | 2a7ef02 | 2015-04-21 13:55:04 +0900 | [diff] [blame] | 870 | this.page = page; |
| 871 | pstat = page_stat__find_page(&this); |
David Ahern | 6b1a275 | 2015-04-14 13:49:33 -0400 | [diff] [blame] | 872 | if (pstat == NULL) { |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 873 | pr_debug2("missing free at page %"PRIx64" (order: %d)\n", |
| 874 | page, order); |
| 875 | |
| 876 | nr_page_nomatch++; |
| 877 | total_page_nomatch_bytes += bytes; |
| 878 | |
| 879 | return 0; |
| 880 | } |
| 881 | |
David Ahern | 6b1a275 | 2015-04-14 13:49:33 -0400 | [diff] [blame] | 882 | this.gfp_flags = pstat->gfp_flags; |
| 883 | this.migrate_type = pstat->migrate_type; |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 884 | this.callsite = pstat->callsite; |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 885 | |
Namhyung Kim | 2a7ef02 | 2015-04-21 13:55:04 +0900 | [diff] [blame] | 886 | rb_erase(&pstat->node, &page_live_tree); |
David Ahern | 6b1a275 | 2015-04-14 13:49:33 -0400 | [diff] [blame] | 887 | free(pstat); |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 888 | |
Namhyung Kim | 2a7ef02 | 2015-04-21 13:55:04 +0900 | [diff] [blame] | 889 | if (live_page) { |
| 890 | order_stats[this.order][this.migrate_type]--; |
| 891 | } else { |
| 892 | pstat = page_stat__find_alloc(&this); |
| 893 | if (pstat == NULL) |
| 894 | return -ENOMEM; |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 895 | |
Namhyung Kim | 2a7ef02 | 2015-04-21 13:55:04 +0900 | [diff] [blame] | 896 | pstat->nr_free++; |
| 897 | pstat->free_bytes += bytes; |
| 898 | } |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 899 | |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 900 | pstat = page_stat__find_caller(&this); |
David Ahern | 6b1a275 | 2015-04-14 13:49:33 -0400 | [diff] [blame] | 901 | if (pstat == NULL) |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 902 | return -ENOENT; |
| 903 | |
David Ahern | 6b1a275 | 2015-04-14 13:49:33 -0400 | [diff] [blame] | 904 | pstat->nr_free++; |
| 905 | pstat->free_bytes += bytes; |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 906 | |
Namhyung Kim | 2a7ef02 | 2015-04-21 13:55:04 +0900 | [diff] [blame] | 907 | if (live_page) { |
| 908 | pstat->nr_alloc--; |
| 909 | pstat->alloc_bytes -= bytes; |
| 910 | |
| 911 | if (pstat->nr_alloc == 0) { |
| 912 | rb_erase(&pstat->node, &page_caller_tree); |
| 913 | free(pstat); |
| 914 | } |
| 915 | } |
| 916 | |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 917 | return 0; |
| 918 | } |
| 919 | |
David Ahern | 2a865bd | 2016-11-29 10:15:45 -0700 | [diff] [blame] | 920 | static bool perf_kmem__skip_sample(struct perf_sample *sample) |
| 921 | { |
| 922 | /* skip sample based on time? */ |
| 923 | if (perf_time__skip_sample(&ptime, sample->time)) |
| 924 | return true; |
| 925 | |
| 926 | return false; |
| 927 | } |
| 928 | |
Arnaldo Carvalho de Melo | 0f7d2f1 | 2012-09-24 10:46:54 -0300 | [diff] [blame] | 929 | typedef int (*tracepoint_handler)(struct perf_evsel *evsel, |
| 930 | struct perf_sample *sample); |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 931 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 932 | static int process_sample_event(struct perf_tool *tool __maybe_unused, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 933 | union perf_event *event, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 934 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | fcf65bf | 2012-08-07 09:58:03 -0300 | [diff] [blame] | 935 | struct perf_evsel *evsel, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 936 | struct machine *machine) |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 937 | { |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 938 | int err = 0; |
Adrian Hunter | ef89325 | 2013-08-27 11:23:06 +0300 | [diff] [blame] | 939 | struct thread *thread = machine__findnew_thread(machine, sample->pid, |
Namhyung Kim | 13ce34d | 2014-05-12 09:56:42 +0900 | [diff] [blame] | 940 | sample->tid); |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 941 | |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 942 | if (thread == NULL) { |
| 943 | pr_debug("problem processing %d event, skipping it.\n", |
| 944 | event->header.type); |
| 945 | return -1; |
| 946 | } |
| 947 | |
David Ahern | 2a865bd | 2016-11-29 10:15:45 -0700 | [diff] [blame] | 948 | if (perf_kmem__skip_sample(sample)) |
| 949 | return 0; |
| 950 | |
Frederic Weisbecker | b9c5143 | 2013-09-11 14:46:56 +0200 | [diff] [blame] | 951 | dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid); |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 952 | |
Arnaldo Carvalho de Melo | 744a971 | 2013-11-06 10:17:38 -0300 | [diff] [blame] | 953 | if (evsel->handler != NULL) { |
| 954 | tracepoint_handler f = evsel->handler; |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 955 | err = f(evsel, sample); |
Arnaldo Carvalho de Melo | 0f7d2f1 | 2012-09-24 10:46:54 -0300 | [diff] [blame] | 956 | } |
| 957 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 958 | thread__put(thread); |
| 959 | |
| 960 | return err; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 961 | } |
| 962 | |
Arnaldo Carvalho de Melo | fcf65bf | 2012-08-07 09:58:03 -0300 | [diff] [blame] | 963 | static struct perf_tool perf_kmem = { |
| 964 | .sample = process_sample_event, |
| 965 | .comm = perf_event__process_comm, |
Namhyung Kim | 64c4090 | 2014-08-01 14:59:31 +0900 | [diff] [blame] | 966 | .mmap = perf_event__process_mmap, |
| 967 | .mmap2 = perf_event__process_mmap2, |
Hari Bathini | f3b3614 | 2017-03-08 02:11:43 +0530 | [diff] [blame] | 968 | .namespaces = perf_event__process_namespaces, |
Jiri Olsa | 0a8cb85 | 2014-07-06 14:18:21 +0200 | [diff] [blame] | 969 | .ordered_events = true, |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 970 | }; |
| 971 | |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 972 | static double fragmentation(unsigned long n_req, unsigned long n_alloc) |
| 973 | { |
| 974 | if (n_alloc == 0) |
| 975 | return 0.0; |
| 976 | else |
| 977 | return 100.0 - (100.0 * n_req / n_alloc); |
| 978 | } |
| 979 | |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 980 | static void __print_slab_result(struct rb_root *root, |
| 981 | struct perf_session *session, |
| 982 | int n_lines, int is_caller) |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 983 | { |
| 984 | struct rb_node *next; |
Arnaldo Carvalho de Melo | 34ba512 | 2012-12-19 09:04:24 -0300 | [diff] [blame] | 985 | struct machine *machine = &session->machines.host; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 986 | |
Namhyung Kim | 65f46e0 | 2015-03-12 16:32:48 +0900 | [diff] [blame] | 987 | printf("%.105s\n", graph_dotted_line); |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 988 | printf(" %-34s |", is_caller ? "Callsite": "Alloc Ptr"); |
Pekka Enberg | 4710327 | 2010-01-19 19:23:23 +0200 | [diff] [blame] | 989 | printf(" Total_alloc/Per | Total_req/Per | Hit | Ping-pong | Frag\n"); |
Namhyung Kim | 65f46e0 | 2015-03-12 16:32:48 +0900 | [diff] [blame] | 990 | printf("%.105s\n", graph_dotted_line); |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 991 | |
| 992 | next = rb_first(root); |
| 993 | |
| 994 | while (next && n_lines--) { |
Arnaldo Carvalho de Melo | 1b145ae | 2009-11-23 17:51:09 -0200 | [diff] [blame] | 995 | struct alloc_stat *data = rb_entry(next, struct alloc_stat, |
| 996 | node); |
| 997 | struct symbol *sym = NULL; |
Arnaldo Carvalho de Melo | 71cf8b8 | 2010-04-01 21:24:38 -0300 | [diff] [blame] | 998 | struct map *map; |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 999 | char buf[BUFSIZ]; |
Arnaldo Carvalho de Melo | 1b145ae | 2009-11-23 17:51:09 -0200 | [diff] [blame] | 1000 | u64 addr; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1001 | |
Arnaldo Carvalho de Melo | 1b145ae | 2009-11-23 17:51:09 -0200 | [diff] [blame] | 1002 | if (is_caller) { |
| 1003 | addr = data->call_site; |
Li Zefan | 7707b6b | 2009-11-24 13:25:48 +0800 | [diff] [blame] | 1004 | if (!raw_ip) |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 1005 | sym = machine__find_kernel_function(machine, addr, &map); |
Arnaldo Carvalho de Melo | 1b145ae | 2009-11-23 17:51:09 -0200 | [diff] [blame] | 1006 | } else |
| 1007 | addr = data->ptr; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1008 | |
Arnaldo Carvalho de Melo | 1b145ae | 2009-11-23 17:51:09 -0200 | [diff] [blame] | 1009 | if (sym != NULL) |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 1010 | snprintf(buf, sizeof(buf), "%s+%" PRIx64 "", sym->name, |
Arnaldo Carvalho de Melo | 71cf8b8 | 2010-04-01 21:24:38 -0300 | [diff] [blame] | 1011 | addr - map->unmap_ip(map, sym->start)); |
Arnaldo Carvalho de Melo | 1b145ae | 2009-11-23 17:51:09 -0200 | [diff] [blame] | 1012 | else |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 1013 | snprintf(buf, sizeof(buf), "%#" PRIx64 "", addr); |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 1014 | printf(" %-34s |", buf); |
Arnaldo Carvalho de Melo | 1b145ae | 2009-11-23 17:51:09 -0200 | [diff] [blame] | 1015 | |
Namhyung Kim | 65f46e0 | 2015-03-12 16:32:48 +0900 | [diff] [blame] | 1016 | printf(" %9llu/%-5lu | %9llu/%-5lu | %8lu | %9lu | %6.3f%%\n", |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 1017 | (unsigned long long)data->bytes_alloc, |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1018 | (unsigned long)data->bytes_alloc / data->hit, |
| 1019 | (unsigned long long)data->bytes_req, |
| 1020 | (unsigned long)data->bytes_req / data->hit, |
| 1021 | (unsigned long)data->hit, |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 1022 | (unsigned long)data->pingpong, |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1023 | fragmentation(data->bytes_req, data->bytes_alloc)); |
| 1024 | |
| 1025 | next = rb_next(next); |
| 1026 | } |
| 1027 | |
| 1028 | if (n_lines == -1) |
Namhyung Kim | 65f46e0 | 2015-03-12 16:32:48 +0900 | [diff] [blame] | 1029 | printf(" ... | ... | ... | ... | ... | ... \n"); |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1030 | |
Namhyung Kim | 65f46e0 | 2015-03-12 16:32:48 +0900 | [diff] [blame] | 1031 | printf("%.105s\n", graph_dotted_line); |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1032 | } |
| 1033 | |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1034 | static const char * const migrate_type_str[] = { |
| 1035 | "UNMOVABL", |
| 1036 | "RECLAIM", |
| 1037 | "MOVABLE", |
| 1038 | "RESERVED", |
| 1039 | "CMA/ISLT", |
| 1040 | "UNKNOWN", |
| 1041 | }; |
| 1042 | |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 1043 | static void __print_page_alloc_result(struct perf_session *session, int n_lines) |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1044 | { |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 1045 | struct rb_node *next = rb_first(&page_alloc_sorted); |
| 1046 | struct machine *machine = &session->machines.host; |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1047 | const char *format; |
Namhyung Kim | 0e11115 | 2015-04-21 13:55:05 +0900 | [diff] [blame] | 1048 | int gfp_len = max(strlen("GFP flags"), max_gfp_len); |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1049 | |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 1050 | printf("\n%.105s\n", graph_dotted_line); |
Namhyung Kim | 0e11115 | 2015-04-21 13:55:05 +0900 | [diff] [blame] | 1051 | printf(" %-16s | %5s alloc (KB) | Hits | Order | Mig.type | %-*s | Callsite\n", |
| 1052 | use_pfn ? "PFN" : "Page", live_page ? "Live" : "Total", |
| 1053 | gfp_len, "GFP flags"); |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 1054 | printf("%.105s\n", graph_dotted_line); |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1055 | |
| 1056 | if (use_pfn) |
Namhyung Kim | 0e11115 | 2015-04-21 13:55:05 +0900 | [diff] [blame] | 1057 | format = " %16llu | %'16llu | %'9d | %5d | %8s | %-*s | %s\n"; |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1058 | else |
Namhyung Kim | 0e11115 | 2015-04-21 13:55:05 +0900 | [diff] [blame] | 1059 | format = " %016llx | %'16llu | %'9d | %5d | %8s | %-*s | %s\n"; |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1060 | |
| 1061 | while (next && n_lines--) { |
| 1062 | struct page_stat *data; |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 1063 | struct symbol *sym; |
| 1064 | struct map *map; |
| 1065 | char buf[32]; |
| 1066 | char *caller = buf; |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1067 | |
| 1068 | data = rb_entry(next, struct page_stat, node); |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 1069 | sym = machine__find_kernel_function(machine, data->callsite, &map); |
Arnaldo Carvalho de Melo | a7c3899 | 2017-02-13 16:52:15 -0300 | [diff] [blame] | 1070 | if (sym) |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 1071 | caller = sym->name; |
| 1072 | else |
| 1073 | scnprintf(buf, sizeof(buf), "%"PRIx64, data->callsite); |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1074 | |
| 1075 | printf(format, (unsigned long long)data->page, |
| 1076 | (unsigned long long)data->alloc_bytes / 1024, |
| 1077 | data->nr_alloc, data->order, |
| 1078 | migrate_type_str[data->migrate_type], |
Namhyung Kim | 0e11115 | 2015-04-21 13:55:05 +0900 | [diff] [blame] | 1079 | gfp_len, compact_gfp_string(data->gfp_flags), caller); |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1080 | |
| 1081 | next = rb_next(next); |
| 1082 | } |
| 1083 | |
Namhyung Kim | 0e11115 | 2015-04-21 13:55:05 +0900 | [diff] [blame] | 1084 | if (n_lines == -1) { |
| 1085 | printf(" ... | ... | ... | ... | ... | %-*s | ...\n", |
| 1086 | gfp_len, "..."); |
| 1087 | } |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1088 | |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 1089 | printf("%.105s\n", graph_dotted_line); |
| 1090 | } |
| 1091 | |
| 1092 | static void __print_page_caller_result(struct perf_session *session, int n_lines) |
| 1093 | { |
| 1094 | struct rb_node *next = rb_first(&page_caller_sorted); |
| 1095 | struct machine *machine = &session->machines.host; |
Namhyung Kim | 0e11115 | 2015-04-21 13:55:05 +0900 | [diff] [blame] | 1096 | int gfp_len = max(strlen("GFP flags"), max_gfp_len); |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 1097 | |
| 1098 | printf("\n%.105s\n", graph_dotted_line); |
Namhyung Kim | 0e11115 | 2015-04-21 13:55:05 +0900 | [diff] [blame] | 1099 | printf(" %5s alloc (KB) | Hits | Order | Mig.type | %-*s | Callsite\n", |
| 1100 | live_page ? "Live" : "Total", gfp_len, "GFP flags"); |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 1101 | printf("%.105s\n", graph_dotted_line); |
| 1102 | |
| 1103 | while (next && n_lines--) { |
| 1104 | struct page_stat *data; |
| 1105 | struct symbol *sym; |
| 1106 | struct map *map; |
| 1107 | char buf[32]; |
| 1108 | char *caller = buf; |
| 1109 | |
| 1110 | data = rb_entry(next, struct page_stat, node); |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 1111 | sym = machine__find_kernel_function(machine, data->callsite, &map); |
Arnaldo Carvalho de Melo | a7c3899 | 2017-02-13 16:52:15 -0300 | [diff] [blame] | 1112 | if (sym) |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 1113 | caller = sym->name; |
| 1114 | else |
| 1115 | scnprintf(buf, sizeof(buf), "%"PRIx64, data->callsite); |
| 1116 | |
Namhyung Kim | 0e11115 | 2015-04-21 13:55:05 +0900 | [diff] [blame] | 1117 | printf(" %'16llu | %'9d | %5d | %8s | %-*s | %s\n", |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 1118 | (unsigned long long)data->alloc_bytes / 1024, |
| 1119 | data->nr_alloc, data->order, |
| 1120 | migrate_type_str[data->migrate_type], |
Namhyung Kim | 0e11115 | 2015-04-21 13:55:05 +0900 | [diff] [blame] | 1121 | gfp_len, compact_gfp_string(data->gfp_flags), caller); |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 1122 | |
| 1123 | next = rb_next(next); |
| 1124 | } |
| 1125 | |
Namhyung Kim | 0e11115 | 2015-04-21 13:55:05 +0900 | [diff] [blame] | 1126 | if (n_lines == -1) { |
| 1127 | printf(" ... | ... | ... | ... | %-*s | ...\n", |
| 1128 | gfp_len, "..."); |
| 1129 | } |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 1130 | |
| 1131 | printf("%.105s\n", graph_dotted_line); |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1132 | } |
| 1133 | |
Namhyung Kim | 0e11115 | 2015-04-21 13:55:05 +0900 | [diff] [blame] | 1134 | static void print_gfp_flags(void) |
| 1135 | { |
| 1136 | int i; |
| 1137 | |
| 1138 | printf("#\n"); |
| 1139 | printf("# GFP flags\n"); |
| 1140 | printf("# ---------\n"); |
| 1141 | for (i = 0; i < nr_gfps; i++) { |
| 1142 | printf("# %08x: %*s: %s\n", gfps[i].flags, |
| 1143 | (int) max_gfp_len, gfps[i].compact_str, |
| 1144 | gfps[i].human_readable); |
| 1145 | } |
| 1146 | } |
| 1147 | |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1148 | static void print_slab_summary(void) |
| 1149 | { |
| 1150 | printf("\nSUMMARY (SLAB allocator)"); |
| 1151 | printf("\n========================\n"); |
Namhyung Kim | 77cfe38 | 2015-03-23 15:30:40 +0900 | [diff] [blame] | 1152 | printf("Total bytes requested: %'lu\n", total_requested); |
| 1153 | printf("Total bytes allocated: %'lu\n", total_allocated); |
David Ahern | aa58e9a | 2016-11-25 14:42:13 -0700 | [diff] [blame] | 1154 | printf("Total bytes freed: %'lu\n", total_freed); |
| 1155 | if (total_allocated > total_freed) { |
| 1156 | printf("Net total bytes allocated: %'lu\n", |
| 1157 | total_allocated - total_freed); |
| 1158 | } |
Namhyung Kim | 77cfe38 | 2015-03-23 15:30:40 +0900 | [diff] [blame] | 1159 | printf("Total bytes wasted on internal fragmentation: %'lu\n", |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1160 | total_allocated - total_requested); |
| 1161 | printf("Internal fragmentation: %f%%\n", |
| 1162 | fragmentation(total_requested, total_allocated)); |
Namhyung Kim | 77cfe38 | 2015-03-23 15:30:40 +0900 | [diff] [blame] | 1163 | printf("Cross CPU allocations: %'lu/%'lu\n", nr_cross_allocs, nr_allocs); |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1164 | } |
| 1165 | |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1166 | static void print_page_summary(void) |
| 1167 | { |
| 1168 | int o, m; |
| 1169 | u64 nr_alloc_freed = nr_page_frees - nr_page_nomatch; |
| 1170 | u64 total_alloc_freed_bytes = total_page_free_bytes - total_page_nomatch_bytes; |
| 1171 | |
| 1172 | printf("\nSUMMARY (page allocator)"); |
| 1173 | printf("\n========================\n"); |
| 1174 | printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total allocation requests", |
| 1175 | nr_page_allocs, total_page_alloc_bytes / 1024); |
| 1176 | printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total free requests", |
| 1177 | nr_page_frees, total_page_free_bytes / 1024); |
| 1178 | printf("\n"); |
| 1179 | |
Will Deacon | 6145c25 | 2015-04-23 14:40:37 +0100 | [diff] [blame] | 1180 | printf("%-30s: %'16"PRIu64" [ %'16"PRIu64" KB ]\n", "Total alloc+freed requests", |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1181 | nr_alloc_freed, (total_alloc_freed_bytes) / 1024); |
Will Deacon | 6145c25 | 2015-04-23 14:40:37 +0100 | [diff] [blame] | 1182 | printf("%-30s: %'16"PRIu64" [ %'16"PRIu64" KB ]\n", "Total alloc-only requests", |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1183 | nr_page_allocs - nr_alloc_freed, |
| 1184 | (total_page_alloc_bytes - total_alloc_freed_bytes) / 1024); |
| 1185 | printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total free-only requests", |
| 1186 | nr_page_nomatch, total_page_nomatch_bytes / 1024); |
| 1187 | printf("\n"); |
| 1188 | |
| 1189 | printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total allocation failures", |
| 1190 | nr_page_fails, total_page_fail_bytes / 1024); |
| 1191 | printf("\n"); |
| 1192 | |
| 1193 | printf("%5s %12s %12s %12s %12s %12s\n", "Order", "Unmovable", |
| 1194 | "Reclaimable", "Movable", "Reserved", "CMA/Isolated"); |
| 1195 | printf("%.5s %.12s %.12s %.12s %.12s %.12s\n", graph_dotted_line, |
| 1196 | graph_dotted_line, graph_dotted_line, graph_dotted_line, |
| 1197 | graph_dotted_line, graph_dotted_line); |
| 1198 | |
| 1199 | for (o = 0; o < MAX_PAGE_ORDER; o++) { |
| 1200 | printf("%5d", o); |
| 1201 | for (m = 0; m < MAX_MIGRATE_TYPES - 1; m++) { |
| 1202 | if (order_stats[o][m]) |
| 1203 | printf(" %'12d", order_stats[o][m]); |
| 1204 | else |
| 1205 | printf(" %12c", '.'); |
| 1206 | } |
| 1207 | printf("\n"); |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | static void print_slab_result(struct perf_session *session) |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1212 | { |
| 1213 | if (caller_flag) |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1214 | __print_slab_result(&root_caller_sorted, session, caller_lines, 1); |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1215 | if (alloc_flag) |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1216 | __print_slab_result(&root_alloc_sorted, session, alloc_lines, 0); |
| 1217 | print_slab_summary(); |
| 1218 | } |
| 1219 | |
| 1220 | static void print_page_result(struct perf_session *session) |
| 1221 | { |
Namhyung Kim | 0e11115 | 2015-04-21 13:55:05 +0900 | [diff] [blame] | 1222 | if (caller_flag || alloc_flag) |
| 1223 | print_gfp_flags(); |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 1224 | if (caller_flag) |
| 1225 | __print_page_caller_result(session, caller_lines); |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1226 | if (alloc_flag) |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 1227 | __print_page_alloc_result(session, alloc_lines); |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1228 | print_page_summary(); |
| 1229 | } |
| 1230 | |
| 1231 | static void print_result(struct perf_session *session) |
| 1232 | { |
| 1233 | if (kmem_slab) |
| 1234 | print_slab_result(session); |
| 1235 | if (kmem_page) |
| 1236 | print_page_result(session); |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1237 | } |
| 1238 | |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 1239 | static LIST_HEAD(slab_caller_sort); |
| 1240 | static LIST_HEAD(slab_alloc_sort); |
| 1241 | static LIST_HEAD(page_caller_sort); |
| 1242 | static LIST_HEAD(page_alloc_sort); |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 1243 | |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1244 | static void sort_slab_insert(struct rb_root *root, struct alloc_stat *data, |
| 1245 | struct list_head *sort_list) |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1246 | { |
| 1247 | struct rb_node **new = &(root->rb_node); |
| 1248 | struct rb_node *parent = NULL; |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 1249 | struct sort_dimension *sort; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1250 | |
| 1251 | while (*new) { |
| 1252 | struct alloc_stat *this; |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 1253 | int cmp = 0; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1254 | |
| 1255 | this = rb_entry(*new, struct alloc_stat, node); |
| 1256 | parent = *new; |
| 1257 | |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 1258 | list_for_each_entry(sort, sort_list, list) { |
| 1259 | cmp = sort->cmp(data, this); |
| 1260 | if (cmp) |
| 1261 | break; |
| 1262 | } |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1263 | |
| 1264 | if (cmp > 0) |
| 1265 | new = &((*new)->rb_left); |
| 1266 | else |
| 1267 | new = &((*new)->rb_right); |
| 1268 | } |
| 1269 | |
| 1270 | rb_link_node(&data->node, parent, new); |
| 1271 | rb_insert_color(&data->node, root); |
| 1272 | } |
| 1273 | |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1274 | static void __sort_slab_result(struct rb_root *root, struct rb_root *root_sorted, |
| 1275 | struct list_head *sort_list) |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1276 | { |
| 1277 | struct rb_node *node; |
| 1278 | struct alloc_stat *data; |
| 1279 | |
| 1280 | for (;;) { |
| 1281 | node = rb_first(root); |
| 1282 | if (!node) |
| 1283 | break; |
| 1284 | |
| 1285 | rb_erase(node, root); |
| 1286 | data = rb_entry(node, struct alloc_stat, node); |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1287 | sort_slab_insert(root_sorted, data, sort_list); |
| 1288 | } |
| 1289 | } |
| 1290 | |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 1291 | static void sort_page_insert(struct rb_root *root, struct page_stat *data, |
| 1292 | struct list_head *sort_list) |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1293 | { |
| 1294 | struct rb_node **new = &root->rb_node; |
| 1295 | struct rb_node *parent = NULL; |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 1296 | struct sort_dimension *sort; |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1297 | |
| 1298 | while (*new) { |
| 1299 | struct page_stat *this; |
| 1300 | int cmp = 0; |
| 1301 | |
| 1302 | this = rb_entry(*new, struct page_stat, node); |
| 1303 | parent = *new; |
| 1304 | |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 1305 | list_for_each_entry(sort, sort_list, list) { |
| 1306 | cmp = sort->cmp(data, this); |
| 1307 | if (cmp) |
| 1308 | break; |
| 1309 | } |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1310 | |
| 1311 | if (cmp > 0) |
| 1312 | new = &parent->rb_left; |
| 1313 | else |
| 1314 | new = &parent->rb_right; |
| 1315 | } |
| 1316 | |
| 1317 | rb_link_node(&data->node, parent, new); |
| 1318 | rb_insert_color(&data->node, root); |
| 1319 | } |
| 1320 | |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 1321 | static void __sort_page_result(struct rb_root *root, struct rb_root *root_sorted, |
| 1322 | struct list_head *sort_list) |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1323 | { |
| 1324 | struct rb_node *node; |
| 1325 | struct page_stat *data; |
| 1326 | |
| 1327 | for (;;) { |
| 1328 | node = rb_first(root); |
| 1329 | if (!node) |
| 1330 | break; |
| 1331 | |
| 1332 | rb_erase(node, root); |
| 1333 | data = rb_entry(node, struct page_stat, node); |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 1334 | sort_page_insert(root_sorted, data, sort_list); |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1335 | } |
| 1336 | } |
| 1337 | |
| 1338 | static void sort_result(void) |
| 1339 | { |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1340 | if (kmem_slab) { |
| 1341 | __sort_slab_result(&root_alloc_stat, &root_alloc_sorted, |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 1342 | &slab_alloc_sort); |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1343 | __sort_slab_result(&root_caller_stat, &root_caller_sorted, |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 1344 | &slab_caller_sort); |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1345 | } |
| 1346 | if (kmem_page) { |
Namhyung Kim | 2a7ef02 | 2015-04-21 13:55:04 +0900 | [diff] [blame] | 1347 | if (live_page) |
| 1348 | __sort_page_result(&page_live_tree, &page_alloc_sorted, |
| 1349 | &page_alloc_sort); |
| 1350 | else |
| 1351 | __sort_page_result(&page_alloc_tree, &page_alloc_sorted, |
| 1352 | &page_alloc_sort); |
| 1353 | |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 1354 | __sort_page_result(&page_caller_tree, &page_caller_sorted, |
| 1355 | &page_caller_sort); |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1356 | } |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1357 | } |
| 1358 | |
Namhyung Kim | 2b2b2c6 | 2014-08-12 15:40:38 +0900 | [diff] [blame] | 1359 | static int __cmd_kmem(struct perf_session *session) |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1360 | { |
Arnaldo Carvalho de Melo | d549c769 | 2009-12-27 21:37:02 -0200 | [diff] [blame] | 1361 | int err = -EINVAL; |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1362 | struct perf_evsel *evsel; |
Arnaldo Carvalho de Melo | 0f7d2f1 | 2012-09-24 10:46:54 -0300 | [diff] [blame] | 1363 | const struct perf_evsel_str_handler kmem_tracepoints[] = { |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1364 | /* slab allocator */ |
Arnaldo Carvalho de Melo | 0f7d2f1 | 2012-09-24 10:46:54 -0300 | [diff] [blame] | 1365 | { "kmem:kmalloc", perf_evsel__process_alloc_event, }, |
| 1366 | { "kmem:kmem_cache_alloc", perf_evsel__process_alloc_event, }, |
| 1367 | { "kmem:kmalloc_node", perf_evsel__process_alloc_node_event, }, |
| 1368 | { "kmem:kmem_cache_alloc_node", perf_evsel__process_alloc_node_event, }, |
| 1369 | { "kmem:kfree", perf_evsel__process_free_event, }, |
| 1370 | { "kmem:kmem_cache_free", perf_evsel__process_free_event, }, |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1371 | /* page allocator */ |
| 1372 | { "kmem:mm_page_alloc", perf_evsel__process_page_alloc_event, }, |
| 1373 | { "kmem:mm_page_free", perf_evsel__process_page_free_event, }, |
Arnaldo Carvalho de Melo | 0f7d2f1 | 2012-09-24 10:46:54 -0300 | [diff] [blame] | 1374 | }; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1375 | |
Arnaldo Carvalho de Melo | d549c769 | 2009-12-27 21:37:02 -0200 | [diff] [blame] | 1376 | if (!perf_session__has_traces(session, "kmem record")) |
Namhyung Kim | 2b2b2c6 | 2014-08-12 15:40:38 +0900 | [diff] [blame] | 1377 | goto out; |
Arnaldo Carvalho de Melo | d549c769 | 2009-12-27 21:37:02 -0200 | [diff] [blame] | 1378 | |
Arnaldo Carvalho de Melo | 0f7d2f1 | 2012-09-24 10:46:54 -0300 | [diff] [blame] | 1379 | if (perf_session__set_tracepoints_handlers(session, kmem_tracepoints)) { |
| 1380 | pr_err("Initializing perf session tracepoint handlers failed\n"); |
Namhyung Kim | 2b2b2c6 | 2014-08-12 15:40:38 +0900 | [diff] [blame] | 1381 | goto out; |
Arnaldo Carvalho de Melo | 0f7d2f1 | 2012-09-24 10:46:54 -0300 | [diff] [blame] | 1382 | } |
| 1383 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 1384 | evlist__for_each_entry(session->evlist, evsel) { |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1385 | if (!strcmp(perf_evsel__name(evsel), "kmem:mm_page_alloc") && |
| 1386 | perf_evsel__field(evsel, "pfn")) { |
| 1387 | use_pfn = true; |
| 1388 | break; |
| 1389 | } |
| 1390 | } |
| 1391 | |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 1392 | setup_pager(); |
Arnaldo Carvalho de Melo | b7b61cb | 2015-03-03 11:58:45 -0300 | [diff] [blame] | 1393 | err = perf_session__process_events(session); |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1394 | if (err != 0) { |
| 1395 | pr_err("error during process events: %d\n", err); |
Namhyung Kim | 2b2b2c6 | 2014-08-12 15:40:38 +0900 | [diff] [blame] | 1396 | goto out; |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1397 | } |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 1398 | sort_result(); |
| 1399 | print_result(session); |
Namhyung Kim | 2b2b2c6 | 2014-08-12 15:40:38 +0900 | [diff] [blame] | 1400 | out: |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 1401 | return err; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1402 | } |
| 1403 | |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 1404 | /* slab sort keys */ |
| 1405 | static int ptr_cmp(void *a, void *b) |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1406 | { |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 1407 | struct alloc_stat *l = a; |
| 1408 | struct alloc_stat *r = b; |
| 1409 | |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1410 | if (l->ptr < r->ptr) |
| 1411 | return -1; |
| 1412 | else if (l->ptr > r->ptr) |
| 1413 | return 1; |
| 1414 | return 0; |
| 1415 | } |
| 1416 | |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 1417 | static struct sort_dimension ptr_sort_dimension = { |
| 1418 | .name = "ptr", |
| 1419 | .cmp = ptr_cmp, |
| 1420 | }; |
| 1421 | |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 1422 | static int slab_callsite_cmp(void *a, void *b) |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1423 | { |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 1424 | struct alloc_stat *l = a; |
| 1425 | struct alloc_stat *r = b; |
| 1426 | |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1427 | if (l->call_site < r->call_site) |
| 1428 | return -1; |
| 1429 | else if (l->call_site > r->call_site) |
| 1430 | return 1; |
| 1431 | return 0; |
| 1432 | } |
| 1433 | |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 1434 | static struct sort_dimension callsite_sort_dimension = { |
| 1435 | .name = "callsite", |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 1436 | .cmp = slab_callsite_cmp, |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 1437 | }; |
| 1438 | |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 1439 | static int hit_cmp(void *a, void *b) |
Pekka Enberg | f3ced7c | 2009-11-22 11:58:00 +0200 | [diff] [blame] | 1440 | { |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 1441 | struct alloc_stat *l = a; |
| 1442 | struct alloc_stat *r = b; |
| 1443 | |
Pekka Enberg | f3ced7c | 2009-11-22 11:58:00 +0200 | [diff] [blame] | 1444 | if (l->hit < r->hit) |
| 1445 | return -1; |
| 1446 | else if (l->hit > r->hit) |
| 1447 | return 1; |
| 1448 | return 0; |
| 1449 | } |
| 1450 | |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 1451 | static struct sort_dimension hit_sort_dimension = { |
| 1452 | .name = "hit", |
| 1453 | .cmp = hit_cmp, |
| 1454 | }; |
| 1455 | |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 1456 | static int bytes_cmp(void *a, void *b) |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1457 | { |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 1458 | struct alloc_stat *l = a; |
| 1459 | struct alloc_stat *r = b; |
| 1460 | |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1461 | if (l->bytes_alloc < r->bytes_alloc) |
| 1462 | return -1; |
| 1463 | else if (l->bytes_alloc > r->bytes_alloc) |
| 1464 | return 1; |
| 1465 | return 0; |
| 1466 | } |
| 1467 | |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 1468 | static struct sort_dimension bytes_sort_dimension = { |
| 1469 | .name = "bytes", |
| 1470 | .cmp = bytes_cmp, |
| 1471 | }; |
| 1472 | |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 1473 | static int frag_cmp(void *a, void *b) |
Pekka Enberg | f3ced7c | 2009-11-22 11:58:00 +0200 | [diff] [blame] | 1474 | { |
| 1475 | double x, y; |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 1476 | struct alloc_stat *l = a; |
| 1477 | struct alloc_stat *r = b; |
Pekka Enberg | f3ced7c | 2009-11-22 11:58:00 +0200 | [diff] [blame] | 1478 | |
| 1479 | x = fragmentation(l->bytes_req, l->bytes_alloc); |
| 1480 | y = fragmentation(r->bytes_req, r->bytes_alloc); |
| 1481 | |
| 1482 | if (x < y) |
| 1483 | return -1; |
| 1484 | else if (x > y) |
| 1485 | return 1; |
| 1486 | return 0; |
| 1487 | } |
| 1488 | |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 1489 | static struct sort_dimension frag_sort_dimension = { |
| 1490 | .name = "frag", |
| 1491 | .cmp = frag_cmp, |
| 1492 | }; |
| 1493 | |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 1494 | static int pingpong_cmp(void *a, void *b) |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 1495 | { |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 1496 | struct alloc_stat *l = a; |
| 1497 | struct alloc_stat *r = b; |
| 1498 | |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 1499 | if (l->pingpong < r->pingpong) |
| 1500 | return -1; |
| 1501 | else if (l->pingpong > r->pingpong) |
| 1502 | return 1; |
| 1503 | return 0; |
| 1504 | } |
| 1505 | |
| 1506 | static struct sort_dimension pingpong_sort_dimension = { |
| 1507 | .name = "pingpong", |
| 1508 | .cmp = pingpong_cmp, |
| 1509 | }; |
| 1510 | |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 1511 | /* page sort keys */ |
| 1512 | static int page_cmp(void *a, void *b) |
| 1513 | { |
| 1514 | struct page_stat *l = a; |
| 1515 | struct page_stat *r = b; |
| 1516 | |
| 1517 | if (l->page < r->page) |
| 1518 | return -1; |
| 1519 | else if (l->page > r->page) |
| 1520 | return 1; |
| 1521 | return 0; |
| 1522 | } |
| 1523 | |
| 1524 | static struct sort_dimension page_sort_dimension = { |
| 1525 | .name = "page", |
| 1526 | .cmp = page_cmp, |
| 1527 | }; |
| 1528 | |
| 1529 | static int page_callsite_cmp(void *a, void *b) |
| 1530 | { |
| 1531 | struct page_stat *l = a; |
| 1532 | struct page_stat *r = b; |
| 1533 | |
| 1534 | if (l->callsite < r->callsite) |
| 1535 | return -1; |
| 1536 | else if (l->callsite > r->callsite) |
| 1537 | return 1; |
| 1538 | return 0; |
| 1539 | } |
| 1540 | |
| 1541 | static struct sort_dimension page_callsite_sort_dimension = { |
| 1542 | .name = "callsite", |
| 1543 | .cmp = page_callsite_cmp, |
| 1544 | }; |
| 1545 | |
| 1546 | static int page_hit_cmp(void *a, void *b) |
| 1547 | { |
| 1548 | struct page_stat *l = a; |
| 1549 | struct page_stat *r = b; |
| 1550 | |
| 1551 | if (l->nr_alloc < r->nr_alloc) |
| 1552 | return -1; |
| 1553 | else if (l->nr_alloc > r->nr_alloc) |
| 1554 | return 1; |
| 1555 | return 0; |
| 1556 | } |
| 1557 | |
| 1558 | static struct sort_dimension page_hit_sort_dimension = { |
| 1559 | .name = "hit", |
| 1560 | .cmp = page_hit_cmp, |
| 1561 | }; |
| 1562 | |
| 1563 | static int page_bytes_cmp(void *a, void *b) |
| 1564 | { |
| 1565 | struct page_stat *l = a; |
| 1566 | struct page_stat *r = b; |
| 1567 | |
| 1568 | if (l->alloc_bytes < r->alloc_bytes) |
| 1569 | return -1; |
| 1570 | else if (l->alloc_bytes > r->alloc_bytes) |
| 1571 | return 1; |
| 1572 | return 0; |
| 1573 | } |
| 1574 | |
| 1575 | static struct sort_dimension page_bytes_sort_dimension = { |
| 1576 | .name = "bytes", |
| 1577 | .cmp = page_bytes_cmp, |
| 1578 | }; |
| 1579 | |
| 1580 | static int page_order_cmp(void *a, void *b) |
| 1581 | { |
| 1582 | struct page_stat *l = a; |
| 1583 | struct page_stat *r = b; |
| 1584 | |
| 1585 | if (l->order < r->order) |
| 1586 | return -1; |
| 1587 | else if (l->order > r->order) |
| 1588 | return 1; |
| 1589 | return 0; |
| 1590 | } |
| 1591 | |
| 1592 | static struct sort_dimension page_order_sort_dimension = { |
| 1593 | .name = "order", |
| 1594 | .cmp = page_order_cmp, |
| 1595 | }; |
| 1596 | |
| 1597 | static int migrate_type_cmp(void *a, void *b) |
| 1598 | { |
| 1599 | struct page_stat *l = a; |
| 1600 | struct page_stat *r = b; |
| 1601 | |
| 1602 | /* for internal use to find free'd page */ |
| 1603 | if (l->migrate_type == -1U) |
| 1604 | return 0; |
| 1605 | |
| 1606 | if (l->migrate_type < r->migrate_type) |
| 1607 | return -1; |
| 1608 | else if (l->migrate_type > r->migrate_type) |
| 1609 | return 1; |
| 1610 | return 0; |
| 1611 | } |
| 1612 | |
| 1613 | static struct sort_dimension migrate_type_sort_dimension = { |
| 1614 | .name = "migtype", |
| 1615 | .cmp = migrate_type_cmp, |
| 1616 | }; |
| 1617 | |
| 1618 | static int gfp_flags_cmp(void *a, void *b) |
| 1619 | { |
| 1620 | struct page_stat *l = a; |
| 1621 | struct page_stat *r = b; |
| 1622 | |
| 1623 | /* for internal use to find free'd page */ |
| 1624 | if (l->gfp_flags == -1U) |
| 1625 | return 0; |
| 1626 | |
| 1627 | if (l->gfp_flags < r->gfp_flags) |
| 1628 | return -1; |
| 1629 | else if (l->gfp_flags > r->gfp_flags) |
| 1630 | return 1; |
| 1631 | return 0; |
| 1632 | } |
| 1633 | |
| 1634 | static struct sort_dimension gfp_flags_sort_dimension = { |
| 1635 | .name = "gfp", |
| 1636 | .cmp = gfp_flags_cmp, |
| 1637 | }; |
| 1638 | |
| 1639 | static struct sort_dimension *slab_sorts[] = { |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 1640 | &ptr_sort_dimension, |
| 1641 | &callsite_sort_dimension, |
| 1642 | &hit_sort_dimension, |
| 1643 | &bytes_sort_dimension, |
| 1644 | &frag_sort_dimension, |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 1645 | &pingpong_sort_dimension, |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 1646 | }; |
| 1647 | |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 1648 | static struct sort_dimension *page_sorts[] = { |
| 1649 | &page_sort_dimension, |
| 1650 | &page_callsite_sort_dimension, |
| 1651 | &page_hit_sort_dimension, |
| 1652 | &page_bytes_sort_dimension, |
| 1653 | &page_order_sort_dimension, |
| 1654 | &migrate_type_sort_dimension, |
| 1655 | &gfp_flags_sort_dimension, |
| 1656 | }; |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 1657 | |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 1658 | static int slab_sort_dimension__add(const char *tok, struct list_head *list) |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 1659 | { |
| 1660 | struct sort_dimension *sort; |
| 1661 | int i; |
| 1662 | |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 1663 | for (i = 0; i < (int)ARRAY_SIZE(slab_sorts); i++) { |
| 1664 | if (!strcmp(slab_sorts[i]->name, tok)) { |
| 1665 | sort = memdup(slab_sorts[i], sizeof(*slab_sorts[i])); |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1666 | if (!sort) { |
Arnaldo Carvalho de Melo | 8d9233f | 2013-01-24 22:24:57 -0300 | [diff] [blame] | 1667 | pr_err("%s: memdup failed\n", __func__); |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1668 | return -1; |
| 1669 | } |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 1670 | list_add_tail(&sort->list, list); |
| 1671 | return 0; |
| 1672 | } |
| 1673 | } |
| 1674 | |
| 1675 | return -1; |
| 1676 | } |
| 1677 | |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 1678 | static int page_sort_dimension__add(const char *tok, struct list_head *list) |
| 1679 | { |
| 1680 | struct sort_dimension *sort; |
| 1681 | int i; |
| 1682 | |
| 1683 | for (i = 0; i < (int)ARRAY_SIZE(page_sorts); i++) { |
| 1684 | if (!strcmp(page_sorts[i]->name, tok)) { |
| 1685 | sort = memdup(page_sorts[i], sizeof(*page_sorts[i])); |
| 1686 | if (!sort) { |
| 1687 | pr_err("%s: memdup failed\n", __func__); |
| 1688 | return -1; |
| 1689 | } |
| 1690 | list_add_tail(&sort->list, list); |
| 1691 | return 0; |
| 1692 | } |
| 1693 | } |
| 1694 | |
| 1695 | return -1; |
| 1696 | } |
| 1697 | |
| 1698 | static int setup_slab_sorting(struct list_head *sort_list, const char *arg) |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 1699 | { |
| 1700 | char *tok; |
| 1701 | char *str = strdup(arg); |
Namhyung Kim | 405f875 | 2015-03-12 16:32:46 +0900 | [diff] [blame] | 1702 | char *pos = str; |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 1703 | |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1704 | if (!str) { |
| 1705 | pr_err("%s: strdup failed\n", __func__); |
| 1706 | return -1; |
| 1707 | } |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 1708 | |
| 1709 | while (true) { |
Namhyung Kim | 405f875 | 2015-03-12 16:32:46 +0900 | [diff] [blame] | 1710 | tok = strsep(&pos, ","); |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 1711 | if (!tok) |
| 1712 | break; |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 1713 | if (slab_sort_dimension__add(tok, sort_list) < 0) { |
| 1714 | error("Unknown slab --sort key: '%s'", tok); |
| 1715 | free(str); |
| 1716 | return -1; |
| 1717 | } |
| 1718 | } |
| 1719 | |
| 1720 | free(str); |
| 1721 | return 0; |
| 1722 | } |
| 1723 | |
| 1724 | static int setup_page_sorting(struct list_head *sort_list, const char *arg) |
| 1725 | { |
| 1726 | char *tok; |
| 1727 | char *str = strdup(arg); |
| 1728 | char *pos = str; |
| 1729 | |
| 1730 | if (!str) { |
| 1731 | pr_err("%s: strdup failed\n", __func__); |
| 1732 | return -1; |
| 1733 | } |
| 1734 | |
| 1735 | while (true) { |
| 1736 | tok = strsep(&pos, ","); |
| 1737 | if (!tok) |
| 1738 | break; |
| 1739 | if (page_sort_dimension__add(tok, sort_list) < 0) { |
| 1740 | error("Unknown page --sort key: '%s'", tok); |
Namhyung Kim | 1b22859 | 2012-01-08 02:25:29 +0900 | [diff] [blame] | 1741 | free(str); |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 1742 | return -1; |
| 1743 | } |
| 1744 | } |
| 1745 | |
| 1746 | free(str); |
| 1747 | return 0; |
| 1748 | } |
| 1749 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1750 | static int parse_sort_opt(const struct option *opt __maybe_unused, |
| 1751 | const char *arg, int unset __maybe_unused) |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1752 | { |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1753 | if (!arg) |
| 1754 | return -1; |
| 1755 | |
Namhyung Kim | 0c160d4 | 2015-04-21 13:55:06 +0900 | [diff] [blame] | 1756 | if (kmem_page > kmem_slab || |
| 1757 | (kmem_page == 0 && kmem_slab == 0 && kmem_default == KMEM_PAGE)) { |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 1758 | if (caller_flag > alloc_flag) |
| 1759 | return setup_page_sorting(&page_caller_sort, arg); |
| 1760 | else |
| 1761 | return setup_page_sorting(&page_alloc_sort, arg); |
| 1762 | } else { |
| 1763 | if (caller_flag > alloc_flag) |
| 1764 | return setup_slab_sorting(&slab_caller_sort, arg); |
| 1765 | else |
| 1766 | return setup_slab_sorting(&slab_alloc_sort, arg); |
| 1767 | } |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1768 | |
| 1769 | return 0; |
| 1770 | } |
| 1771 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1772 | static int parse_caller_opt(const struct option *opt __maybe_unused, |
| 1773 | const char *arg __maybe_unused, |
| 1774 | int unset __maybe_unused) |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1775 | { |
Li Zefan | 90b86a9 | 2009-12-10 15:21:57 +0800 | [diff] [blame] | 1776 | caller_flag = (alloc_flag + 1); |
| 1777 | return 0; |
| 1778 | } |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1779 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1780 | static int parse_alloc_opt(const struct option *opt __maybe_unused, |
| 1781 | const char *arg __maybe_unused, |
| 1782 | int unset __maybe_unused) |
Li Zefan | 90b86a9 | 2009-12-10 15:21:57 +0800 | [diff] [blame] | 1783 | { |
| 1784 | alloc_flag = (caller_flag + 1); |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1785 | return 0; |
| 1786 | } |
| 1787 | |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1788 | static int parse_slab_opt(const struct option *opt __maybe_unused, |
| 1789 | const char *arg __maybe_unused, |
| 1790 | int unset __maybe_unused) |
| 1791 | { |
| 1792 | kmem_slab = (kmem_page + 1); |
| 1793 | return 0; |
| 1794 | } |
| 1795 | |
| 1796 | static int parse_page_opt(const struct option *opt __maybe_unused, |
| 1797 | const char *arg __maybe_unused, |
| 1798 | int unset __maybe_unused) |
| 1799 | { |
| 1800 | kmem_page = (kmem_slab + 1); |
| 1801 | return 0; |
| 1802 | } |
| 1803 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1804 | static int parse_line_opt(const struct option *opt __maybe_unused, |
| 1805 | const char *arg, int unset __maybe_unused) |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1806 | { |
| 1807 | int lines; |
| 1808 | |
| 1809 | if (!arg) |
| 1810 | return -1; |
| 1811 | |
| 1812 | lines = strtoul(arg, NULL, 10); |
| 1813 | |
| 1814 | if (caller_flag > alloc_flag) |
| 1815 | caller_lines = lines; |
| 1816 | else |
| 1817 | alloc_lines = lines; |
| 1818 | |
| 1819 | return 0; |
| 1820 | } |
| 1821 | |
Arnaldo Carvalho de Melo | 0433ffb | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 1822 | static int __cmd_record(int argc, const char **argv) |
| 1823 | { |
| 1824 | const char * const record_args[] = { |
Jiri Olsa | 4a4d371 | 2013-06-05 13:37:21 +0200 | [diff] [blame] | 1825 | "record", "-a", "-R", "-c", "1", |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1826 | }; |
| 1827 | const char * const slab_events[] = { |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1828 | "-e", "kmem:kmalloc", |
| 1829 | "-e", "kmem:kmalloc_node", |
| 1830 | "-e", "kmem:kfree", |
| 1831 | "-e", "kmem:kmem_cache_alloc", |
| 1832 | "-e", "kmem:kmem_cache_alloc_node", |
| 1833 | "-e", "kmem:kmem_cache_free", |
Arnaldo Carvalho de Melo | 0433ffb | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 1834 | }; |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1835 | const char * const page_events[] = { |
| 1836 | "-e", "kmem:mm_page_alloc", |
| 1837 | "-e", "kmem:mm_page_free", |
| 1838 | }; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1839 | unsigned int rec_argc, i, j; |
| 1840 | const char **rec_argv; |
| 1841 | |
| 1842 | rec_argc = ARRAY_SIZE(record_args) + argc - 1; |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1843 | if (kmem_slab) |
| 1844 | rec_argc += ARRAY_SIZE(slab_events); |
| 1845 | if (kmem_page) |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 1846 | rec_argc += ARRAY_SIZE(page_events) + 1; /* for -g */ |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1847 | |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1848 | rec_argv = calloc(rec_argc + 1, sizeof(char *)); |
| 1849 | |
Chris Samuel | ce47dc5 | 2010-11-13 13:35:06 +1100 | [diff] [blame] | 1850 | if (rec_argv == NULL) |
| 1851 | return -ENOMEM; |
| 1852 | |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1853 | for (i = 0; i < ARRAY_SIZE(record_args); i++) |
| 1854 | rec_argv[i] = strdup(record_args[i]); |
| 1855 | |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1856 | if (kmem_slab) { |
| 1857 | for (j = 0; j < ARRAY_SIZE(slab_events); j++, i++) |
| 1858 | rec_argv[i] = strdup(slab_events[j]); |
| 1859 | } |
| 1860 | if (kmem_page) { |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 1861 | rec_argv[i++] = strdup("-g"); |
| 1862 | |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1863 | for (j = 0; j < ARRAY_SIZE(page_events); j++, i++) |
| 1864 | rec_argv[i] = strdup(page_events[j]); |
| 1865 | } |
| 1866 | |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1867 | for (j = 1; j < (unsigned int)argc; j++, i++) |
| 1868 | rec_argv[i] = argv[j]; |
| 1869 | |
Arnaldo Carvalho de Melo | b0ad8ea | 2017-03-27 11:47:20 -0300 | [diff] [blame] | 1870 | return cmd_record(i, rec_argv); |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1871 | } |
| 1872 | |
Wang Nan | b8cbb34 | 2016-02-26 09:31:51 +0000 | [diff] [blame] | 1873 | static int kmem_config(const char *var, const char *value, void *cb __maybe_unused) |
Namhyung Kim | 0c160d4 | 2015-04-21 13:55:06 +0900 | [diff] [blame] | 1874 | { |
| 1875 | if (!strcmp(var, "kmem.default")) { |
| 1876 | if (!strcmp(value, "slab")) |
| 1877 | kmem_default = KMEM_SLAB; |
| 1878 | else if (!strcmp(value, "page")) |
| 1879 | kmem_default = KMEM_PAGE; |
| 1880 | else |
| 1881 | pr_err("invalid default value ('slab' or 'page' required): %s\n", |
| 1882 | value); |
| 1883 | return 0; |
| 1884 | } |
| 1885 | |
Wang Nan | b8cbb34 | 2016-02-26 09:31:51 +0000 | [diff] [blame] | 1886 | return 0; |
Namhyung Kim | 0c160d4 | 2015-04-21 13:55:06 +0900 | [diff] [blame] | 1887 | } |
| 1888 | |
Arnaldo Carvalho de Melo | b0ad8ea | 2017-03-27 11:47:20 -0300 | [diff] [blame] | 1889 | int cmd_kmem(int argc, const char **argv) |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1890 | { |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 1891 | const char * const default_slab_sort = "frag,hit,bytes"; |
| 1892 | const char * const default_page_sort = "bytes,hit"; |
Yunlong Song | d1eeb77 | 2015-04-02 21:47:12 +0800 | [diff] [blame] | 1893 | struct perf_data_file file = { |
Yunlong Song | d1eeb77 | 2015-04-02 21:47:12 +0800 | [diff] [blame] | 1894 | .mode = PERF_DATA_MODE_READ, |
| 1895 | }; |
Arnaldo Carvalho de Melo | 0433ffb | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 1896 | const struct option kmem_options[] = { |
| 1897 | OPT_STRING('i', "input", &input_name, "file", "input file name"), |
Namhyung Kim | bd72a33 | 2015-03-12 16:32:47 +0900 | [diff] [blame] | 1898 | OPT_INCR('v', "verbose", &verbose, |
| 1899 | "be more verbose (show symbol address, etc)"), |
Arnaldo Carvalho de Melo | 0433ffb | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 1900 | OPT_CALLBACK_NOOPT(0, "caller", NULL, NULL, |
| 1901 | "show per-callsite statistics", parse_caller_opt), |
| 1902 | OPT_CALLBACK_NOOPT(0, "alloc", NULL, NULL, |
| 1903 | "show per-allocation statistics", parse_alloc_opt), |
| 1904 | OPT_CALLBACK('s', "sort", NULL, "key[,key2...]", |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 1905 | "sort by keys: ptr, callsite, bytes, hit, pingpong, frag, " |
| 1906 | "page, order, migtype, gfp", parse_sort_opt), |
Arnaldo Carvalho de Melo | 0433ffb | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 1907 | OPT_CALLBACK('l', "line", NULL, "num", "show n lines", parse_line_opt), |
| 1908 | OPT_BOOLEAN(0, "raw-ip", &raw_ip, "show raw ip instead of symbol"), |
Yunlong Song | d1eeb77 | 2015-04-02 21:47:12 +0800 | [diff] [blame] | 1909 | OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"), |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1910 | OPT_CALLBACK_NOOPT(0, "slab", NULL, NULL, "Analyze slab allocator", |
| 1911 | parse_slab_opt), |
| 1912 | OPT_CALLBACK_NOOPT(0, "page", NULL, NULL, "Analyze page allocator", |
| 1913 | parse_page_opt), |
Namhyung Kim | 2a7ef02 | 2015-04-21 13:55:04 +0900 | [diff] [blame] | 1914 | OPT_BOOLEAN(0, "live", &live_page, "Show live page stat"), |
David Ahern | 2a865bd | 2016-11-29 10:15:45 -0700 | [diff] [blame] | 1915 | OPT_STRING(0, "time", &time_str, "str", |
| 1916 | "Time span of interest (start,stop)"), |
Arnaldo Carvalho de Melo | 0433ffb | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 1917 | OPT_END() |
| 1918 | }; |
Ramkumar Ramachandra | 3bca235 | 2014-03-14 23:17:51 -0400 | [diff] [blame] | 1919 | const char *const kmem_subcommands[] = { "record", "stat", NULL }; |
| 1920 | const char *kmem_usage[] = { |
| 1921 | NULL, |
Arnaldo Carvalho de Melo | 0433ffb | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 1922 | NULL |
| 1923 | }; |
Namhyung Kim | 2b2b2c6 | 2014-08-12 15:40:38 +0900 | [diff] [blame] | 1924 | struct perf_session *session; |
Namhyung Kim | a923e2c | 2015-05-05 23:52:52 +0900 | [diff] [blame] | 1925 | const char errmsg[] = "No %s allocation events found. Have you run 'perf kmem record --%s'?\n"; |
Arnaldo Carvalho de Melo | ecc4c56 | 2017-01-24 13:44:10 -0300 | [diff] [blame] | 1926 | int ret = perf_config(kmem_config, NULL); |
Namhyung Kim | 2b2b2c6 | 2014-08-12 15:40:38 +0900 | [diff] [blame] | 1927 | |
Arnaldo Carvalho de Melo | ecc4c56 | 2017-01-24 13:44:10 -0300 | [diff] [blame] | 1928 | if (ret) |
| 1929 | return ret; |
| 1930 | |
Ramkumar Ramachandra | 3bca235 | 2014-03-14 23:17:51 -0400 | [diff] [blame] | 1931 | argc = parse_options_subcommand(argc, argv, kmem_options, |
| 1932 | kmem_subcommands, kmem_usage, 0); |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1933 | |
Li Zefan | 90b86a9 | 2009-12-10 15:21:57 +0800 | [diff] [blame] | 1934 | if (!argc) |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1935 | usage_with_options(kmem_usage, kmem_options); |
| 1936 | |
Namhyung Kim | 0c160d4 | 2015-04-21 13:55:06 +0900 | [diff] [blame] | 1937 | if (kmem_slab == 0 && kmem_page == 0) { |
| 1938 | if (kmem_default == KMEM_SLAB) |
| 1939 | kmem_slab = 1; |
| 1940 | else |
| 1941 | kmem_page = 1; |
| 1942 | } |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1943 | |
Namhyung Kim | 2b2b2c6 | 2014-08-12 15:40:38 +0900 | [diff] [blame] | 1944 | if (!strncmp(argv[0], "rec", 3)) { |
Namhyung Kim | 0a7e6d1 | 2014-08-12 15:40:45 +0900 | [diff] [blame] | 1945 | symbol__init(NULL); |
Namhyung Kim | 2b2b2c6 | 2014-08-12 15:40:38 +0900 | [diff] [blame] | 1946 | return __cmd_record(argc, argv); |
| 1947 | } |
| 1948 | |
Jiri Olsa | 28939e1 | 2015-04-06 14:36:08 +0900 | [diff] [blame] | 1949 | file.path = input_name; |
| 1950 | |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 1951 | kmem_session = session = perf_session__new(&file, false, &perf_kmem); |
Namhyung Kim | 2b2b2c6 | 2014-08-12 15:40:38 +0900 | [diff] [blame] | 1952 | if (session == NULL) |
Taeung Song | 52e02834 | 2014-09-24 10:33:37 +0900 | [diff] [blame] | 1953 | return -1; |
Namhyung Kim | 2b2b2c6 | 2014-08-12 15:40:38 +0900 | [diff] [blame] | 1954 | |
Arnaldo Carvalho de Melo | ecc4c56 | 2017-01-24 13:44:10 -0300 | [diff] [blame] | 1955 | ret = -1; |
| 1956 | |
Namhyung Kim | a923e2c | 2015-05-05 23:52:52 +0900 | [diff] [blame] | 1957 | if (kmem_slab) { |
| 1958 | if (!perf_evlist__find_tracepoint_by_name(session->evlist, |
| 1959 | "kmem:kmalloc")) { |
| 1960 | pr_err(errmsg, "slab", "slab"); |
Taeung Song | 249ca1a | 2015-06-30 17:15:21 +0900 | [diff] [blame] | 1961 | goto out_delete; |
Namhyung Kim | a923e2c | 2015-05-05 23:52:52 +0900 | [diff] [blame] | 1962 | } |
| 1963 | } |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1964 | |
Namhyung Kim | a923e2c | 2015-05-05 23:52:52 +0900 | [diff] [blame] | 1965 | if (kmem_page) { |
| 1966 | struct perf_evsel *evsel; |
| 1967 | |
| 1968 | evsel = perf_evlist__find_tracepoint_by_name(session->evlist, |
| 1969 | "kmem:mm_page_alloc"); |
| 1970 | if (evsel == NULL) { |
| 1971 | pr_err(errmsg, "page", "page"); |
Taeung Song | 249ca1a | 2015-06-30 17:15:21 +0900 | [diff] [blame] | 1972 | goto out_delete; |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1973 | } |
| 1974 | |
| 1975 | kmem_page_size = pevent_get_page_size(evsel->tp_format->pevent); |
Namhyung Kim | c9758cc | 2015-04-21 13:55:02 +0900 | [diff] [blame] | 1976 | symbol_conf.use_callchain = true; |
Namhyung Kim | 0d68bc9 | 2015-04-06 14:36:10 +0900 | [diff] [blame] | 1977 | } |
| 1978 | |
Namhyung Kim | 0a7e6d1 | 2014-08-12 15:40:45 +0900 | [diff] [blame] | 1979 | symbol__init(&session->header.env); |
Arnaldo Carvalho de Melo | 655000e | 2009-12-15 20:04:40 -0200 | [diff] [blame] | 1980 | |
David Ahern | 2a865bd | 2016-11-29 10:15:45 -0700 | [diff] [blame] | 1981 | if (perf_time__parse_str(&ptime, time_str) != 0) { |
| 1982 | pr_err("Invalid time string\n"); |
| 1983 | return -EINVAL; |
| 1984 | } |
| 1985 | |
Namhyung Kim | 2b2b2c6 | 2014-08-12 15:40:38 +0900 | [diff] [blame] | 1986 | if (!strcmp(argv[0], "stat")) { |
Namhyung Kim | 77cfe38 | 2015-03-23 15:30:40 +0900 | [diff] [blame] | 1987 | setlocale(LC_ALL, ""); |
| 1988 | |
Don Zickus | 4b62795 | 2014-04-07 14:55:23 -0400 | [diff] [blame] | 1989 | if (cpu__setup_cpunode_map()) |
Namhyung Kim | 2b2b2c6 | 2014-08-12 15:40:38 +0900 | [diff] [blame] | 1990 | goto out_delete; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 1991 | |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 1992 | if (list_empty(&slab_caller_sort)) |
| 1993 | setup_slab_sorting(&slab_caller_sort, default_slab_sort); |
| 1994 | if (list_empty(&slab_alloc_sort)) |
| 1995 | setup_slab_sorting(&slab_alloc_sort, default_slab_sort); |
| 1996 | if (list_empty(&page_caller_sort)) |
| 1997 | setup_page_sorting(&page_caller_sort, default_page_sort); |
| 1998 | if (list_empty(&page_alloc_sort)) |
| 1999 | setup_page_sorting(&page_alloc_sort, default_page_sort); |
Li Zefan | 7d0d394 | 2009-11-24 13:26:31 +0800 | [diff] [blame] | 2000 | |
Namhyung Kim | fb4f313 | 2015-04-21 13:55:03 +0900 | [diff] [blame] | 2001 | if (kmem_page) { |
| 2002 | setup_page_sorting(&page_alloc_sort_input, |
| 2003 | "page,order,migtype,gfp"); |
| 2004 | setup_page_sorting(&page_caller_sort_input, |
| 2005 | "callsite,order,migtype,gfp"); |
| 2006 | } |
Namhyung Kim | 2b2b2c6 | 2014-08-12 15:40:38 +0900 | [diff] [blame] | 2007 | ret = __cmd_kmem(session); |
Pekka Enberg | b00eca8 | 2010-01-19 19:26:11 +0200 | [diff] [blame] | 2008 | } else |
| 2009 | usage_with_options(kmem_usage, kmem_options); |
Li Zefan | 90b86a9 | 2009-12-10 15:21:57 +0800 | [diff] [blame] | 2010 | |
Namhyung Kim | 2b2b2c6 | 2014-08-12 15:40:38 +0900 | [diff] [blame] | 2011 | out_delete: |
| 2012 | perf_session__delete(session); |
| 2013 | |
| 2014 | return ret; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 2015 | } |
| 2016 | |