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" |
| 7 | #include "util/cache.h" |
| 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" |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 13 | |
| 14 | #include "util/parse-options.h" |
| 15 | #include "util/trace-event.h" |
| 16 | |
| 17 | #include "util/debug.h" |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 18 | |
| 19 | #include <linux/rbtree.h> |
| 20 | |
| 21 | struct alloc_stat; |
| 22 | typedef int (*sort_fn_t)(struct alloc_stat *, struct alloc_stat *); |
| 23 | |
Robert Richter | efad141 | 2011-12-07 10:02:54 +0100 | [diff] [blame] | 24 | static const char *input_name; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 25 | |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 26 | static int alloc_flag; |
| 27 | static int caller_flag; |
| 28 | |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 29 | static int alloc_lines = -1; |
| 30 | static int caller_lines = -1; |
| 31 | |
Li Zefan | 7707b6b | 2009-11-24 13:25:48 +0800 | [diff] [blame] | 32 | static bool raw_ip; |
| 33 | |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 34 | static char default_sort_order[] = "frag,hit,bytes"; |
| 35 | |
Li Zefan | 7d0d394 | 2009-11-24 13:26:31 +0800 | [diff] [blame] | 36 | static int *cpunode_map; |
| 37 | static int max_cpu_num; |
| 38 | |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 39 | struct alloc_stat { |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 40 | u64 call_site; |
| 41 | u64 ptr; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 42 | u64 bytes_req; |
| 43 | u64 bytes_alloc; |
| 44 | u32 hit; |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 45 | u32 pingpong; |
| 46 | |
| 47 | short alloc_cpu; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 48 | |
| 49 | struct rb_node node; |
| 50 | }; |
| 51 | |
| 52 | static struct rb_root root_alloc_stat; |
| 53 | static struct rb_root root_alloc_sorted; |
| 54 | static struct rb_root root_caller_stat; |
| 55 | static struct rb_root root_caller_sorted; |
| 56 | |
| 57 | static unsigned long total_requested, total_allocated; |
Li Zefan | 7d0d394 | 2009-11-24 13:26:31 +0800 | [diff] [blame] | 58 | static unsigned long nr_allocs, nr_cross_allocs; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 59 | |
Li Zefan | 7d0d394 | 2009-11-24 13:26:31 +0800 | [diff] [blame] | 60 | #define PATH_SYS_NODE "/sys/devices/system/node" |
| 61 | |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 62 | static int init_cpunode_map(void) |
Li Zefan | 7d0d394 | 2009-11-24 13:26:31 +0800 | [diff] [blame] | 63 | { |
| 64 | FILE *fp; |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 65 | int i, err = -1; |
Li Zefan | 7d0d394 | 2009-11-24 13:26:31 +0800 | [diff] [blame] | 66 | |
| 67 | fp = fopen("/sys/devices/system/cpu/kernel_max", "r"); |
| 68 | if (!fp) { |
| 69 | max_cpu_num = 4096; |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 70 | return 0; |
Li Zefan | 7d0d394 | 2009-11-24 13:26:31 +0800 | [diff] [blame] | 71 | } |
| 72 | |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 73 | if (fscanf(fp, "%d", &max_cpu_num) < 1) { |
| 74 | pr_err("Failed to read 'kernel_max' from sysfs"); |
| 75 | goto out_close; |
| 76 | } |
| 77 | |
Li Zefan | 7d0d394 | 2009-11-24 13:26:31 +0800 | [diff] [blame] | 78 | max_cpu_num++; |
| 79 | |
| 80 | cpunode_map = calloc(max_cpu_num, sizeof(int)); |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 81 | if (!cpunode_map) { |
| 82 | pr_err("%s: calloc failed\n", __func__); |
| 83 | goto out_close; |
| 84 | } |
| 85 | |
Li Zefan | 7d0d394 | 2009-11-24 13:26:31 +0800 | [diff] [blame] | 86 | for (i = 0; i < max_cpu_num; i++) |
| 87 | cpunode_map[i] = -1; |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 88 | |
| 89 | err = 0; |
| 90 | out_close: |
Li Zefan | 7d0d394 | 2009-11-24 13:26:31 +0800 | [diff] [blame] | 91 | fclose(fp); |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 92 | return err; |
Li Zefan | 7d0d394 | 2009-11-24 13:26:31 +0800 | [diff] [blame] | 93 | } |
| 94 | |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 95 | static int setup_cpunode_map(void) |
Li Zefan | 7d0d394 | 2009-11-24 13:26:31 +0800 | [diff] [blame] | 96 | { |
| 97 | struct dirent *dent1, *dent2; |
| 98 | DIR *dir1, *dir2; |
| 99 | unsigned int cpu, mem; |
| 100 | char buf[PATH_MAX]; |
| 101 | |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 102 | if (init_cpunode_map()) |
| 103 | return -1; |
Li Zefan | 7d0d394 | 2009-11-24 13:26:31 +0800 | [diff] [blame] | 104 | |
| 105 | dir1 = opendir(PATH_SYS_NODE); |
| 106 | if (!dir1) |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 107 | return -1; |
Li Zefan | 7d0d394 | 2009-11-24 13:26:31 +0800 | [diff] [blame] | 108 | |
Ulrich Drepper | 659d8cf | 2009-12-19 16:40:28 -0500 | [diff] [blame] | 109 | while ((dent1 = readdir(dir1)) != NULL) { |
| 110 | if (dent1->d_type != DT_DIR || |
| 111 | sscanf(dent1->d_name, "node%u", &mem) < 1) |
Li Zefan | 7d0d394 | 2009-11-24 13:26:31 +0800 | [diff] [blame] | 112 | continue; |
| 113 | |
| 114 | snprintf(buf, PATH_MAX, "%s/%s", PATH_SYS_NODE, dent1->d_name); |
| 115 | dir2 = opendir(buf); |
| 116 | if (!dir2) |
| 117 | continue; |
Ulrich Drepper | 659d8cf | 2009-12-19 16:40:28 -0500 | [diff] [blame] | 118 | while ((dent2 = readdir(dir2)) != NULL) { |
| 119 | if (dent2->d_type != DT_LNK || |
| 120 | sscanf(dent2->d_name, "cpu%u", &cpu) < 1) |
Li Zefan | 7d0d394 | 2009-11-24 13:26:31 +0800 | [diff] [blame] | 121 | continue; |
| 122 | cpunode_map[cpu] = mem; |
| 123 | } |
Namhyung Kim | 8442da1 | 2012-01-08 02:25:28 +0900 | [diff] [blame] | 124 | closedir(dir2); |
Li Zefan | 7d0d394 | 2009-11-24 13:26:31 +0800 | [diff] [blame] | 125 | } |
Namhyung Kim | 8442da1 | 2012-01-08 02:25:28 +0900 | [diff] [blame] | 126 | closedir(dir1); |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 127 | return 0; |
Li Zefan | 7d0d394 | 2009-11-24 13:26:31 +0800 | [diff] [blame] | 128 | } |
| 129 | |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 130 | static int insert_alloc_stat(unsigned long call_site, unsigned long ptr, |
| 131 | int bytes_req, int bytes_alloc, int cpu) |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 132 | { |
| 133 | struct rb_node **node = &root_alloc_stat.rb_node; |
| 134 | struct rb_node *parent = NULL; |
| 135 | struct alloc_stat *data = NULL; |
| 136 | |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 137 | while (*node) { |
| 138 | parent = *node; |
| 139 | data = rb_entry(*node, struct alloc_stat, node); |
| 140 | |
| 141 | if (ptr > data->ptr) |
| 142 | node = &(*node)->rb_right; |
| 143 | else if (ptr < data->ptr) |
| 144 | node = &(*node)->rb_left; |
| 145 | else |
| 146 | break; |
| 147 | } |
| 148 | |
| 149 | if (data && data->ptr == ptr) { |
| 150 | data->hit++; |
| 151 | data->bytes_req += bytes_req; |
Wenji Huang | 4efb529 | 2009-12-21 17:52:55 +0800 | [diff] [blame] | 152 | data->bytes_alloc += bytes_alloc; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 153 | } else { |
| 154 | data = malloc(sizeof(*data)); |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 155 | if (!data) { |
| 156 | pr_err("%s: malloc failed\n", __func__); |
| 157 | return -1; |
| 158 | } |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 159 | data->ptr = ptr; |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 160 | data->pingpong = 0; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 161 | data->hit = 1; |
| 162 | data->bytes_req = bytes_req; |
| 163 | data->bytes_alloc = bytes_alloc; |
| 164 | |
| 165 | rb_link_node(&data->node, parent, node); |
| 166 | rb_insert_color(&data->node, &root_alloc_stat); |
| 167 | } |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 168 | data->call_site = call_site; |
| 169 | data->alloc_cpu = cpu; |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 170 | return 0; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 171 | } |
| 172 | |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 173 | static int insert_caller_stat(unsigned long call_site, |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 174 | int bytes_req, int bytes_alloc) |
| 175 | { |
| 176 | struct rb_node **node = &root_caller_stat.rb_node; |
| 177 | struct rb_node *parent = NULL; |
| 178 | struct alloc_stat *data = NULL; |
| 179 | |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 180 | while (*node) { |
| 181 | parent = *node; |
| 182 | data = rb_entry(*node, struct alloc_stat, node); |
| 183 | |
| 184 | if (call_site > data->call_site) |
| 185 | node = &(*node)->rb_right; |
| 186 | else if (call_site < data->call_site) |
| 187 | node = &(*node)->rb_left; |
| 188 | else |
| 189 | break; |
| 190 | } |
| 191 | |
| 192 | if (data && data->call_site == call_site) { |
| 193 | data->hit++; |
| 194 | data->bytes_req += bytes_req; |
Wenji Huang | 4efb529 | 2009-12-21 17:52:55 +0800 | [diff] [blame] | 195 | data->bytes_alloc += bytes_alloc; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 196 | } else { |
| 197 | data = malloc(sizeof(*data)); |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 198 | if (!data) { |
| 199 | pr_err("%s: malloc failed\n", __func__); |
| 200 | return -1; |
| 201 | } |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 202 | data->call_site = call_site; |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 203 | data->pingpong = 0; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 204 | data->hit = 1; |
| 205 | data->bytes_req = bytes_req; |
| 206 | data->bytes_alloc = bytes_alloc; |
| 207 | |
| 208 | rb_link_node(&data->node, parent, node); |
| 209 | rb_insert_color(&data->node, &root_caller_stat); |
| 210 | } |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 211 | |
| 212 | return 0; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 213 | } |
| 214 | |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 215 | 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^] | 216 | struct perf_sample *sample) |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 217 | { |
Arnaldo Carvalho de Melo | 0f7d2f1 | 2012-09-24 10:46:54 -0300 | [diff] [blame^] | 218 | unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr"), |
| 219 | call_site = perf_evsel__intval(evsel, sample, "call_site"); |
| 220 | int bytes_req = perf_evsel__intval(evsel, sample, "bytes_req"), |
| 221 | bytes_alloc = perf_evsel__intval(evsel, sample, "bytes_alloc"); |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 222 | |
Arnaldo Carvalho de Melo | 0f7d2f1 | 2012-09-24 10:46:54 -0300 | [diff] [blame^] | 223 | 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] | 224 | insert_caller_stat(call_site, bytes_req, bytes_alloc)) |
| 225 | return -1; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 226 | |
| 227 | total_requested += bytes_req; |
| 228 | total_allocated += bytes_alloc; |
Li Zefan | 7d0d394 | 2009-11-24 13:26:31 +0800 | [diff] [blame] | 229 | |
Arnaldo Carvalho de Melo | 0f7d2f1 | 2012-09-24 10:46:54 -0300 | [diff] [blame^] | 230 | nr_allocs++; |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | static int perf_evsel__process_alloc_node_event(struct perf_evsel *evsel, |
| 235 | struct perf_sample *sample) |
| 236 | { |
| 237 | int ret = perf_evsel__process_alloc_event(evsel, sample); |
| 238 | |
| 239 | if (!ret) { |
| 240 | int node1 = cpunode_map[sample->cpu], |
| 241 | node2 = perf_evsel__intval(evsel, sample, "node"); |
| 242 | |
Li Zefan | 7d0d394 | 2009-11-24 13:26:31 +0800 | [diff] [blame] | 243 | if (node1 != node2) |
| 244 | nr_cross_allocs++; |
| 245 | } |
Arnaldo Carvalho de Melo | 0f7d2f1 | 2012-09-24 10:46:54 -0300 | [diff] [blame^] | 246 | |
| 247 | return ret; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 248 | } |
| 249 | |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 250 | static int ptr_cmp(struct alloc_stat *, struct alloc_stat *); |
| 251 | static int callsite_cmp(struct alloc_stat *, struct alloc_stat *); |
| 252 | |
| 253 | static struct alloc_stat *search_alloc_stat(unsigned long ptr, |
| 254 | unsigned long call_site, |
| 255 | struct rb_root *root, |
| 256 | sort_fn_t sort_fn) |
| 257 | { |
| 258 | struct rb_node *node = root->rb_node; |
| 259 | struct alloc_stat key = { .ptr = ptr, .call_site = call_site }; |
| 260 | |
| 261 | while (node) { |
| 262 | struct alloc_stat *data; |
| 263 | int cmp; |
| 264 | |
| 265 | data = rb_entry(node, struct alloc_stat, node); |
| 266 | |
| 267 | cmp = sort_fn(&key, data); |
| 268 | if (cmp < 0) |
| 269 | node = node->rb_left; |
| 270 | else if (cmp > 0) |
| 271 | node = node->rb_right; |
| 272 | else |
| 273 | return data; |
| 274 | } |
| 275 | return NULL; |
| 276 | } |
| 277 | |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 278 | static int perf_evsel__process_free_event(struct perf_evsel *evsel, |
| 279 | struct perf_sample *sample) |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 280 | { |
Arnaldo Carvalho de Melo | 0f7d2f1 | 2012-09-24 10:46:54 -0300 | [diff] [blame^] | 281 | unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr"); |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 282 | struct alloc_stat *s_alloc, *s_caller; |
| 283 | |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 284 | s_alloc = search_alloc_stat(ptr, 0, &root_alloc_stat, ptr_cmp); |
| 285 | if (!s_alloc) |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 286 | return 0; |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 287 | |
Arnaldo Carvalho de Melo | 22ad798 | 2012-08-07 10:56:43 -0300 | [diff] [blame] | 288 | if ((short)sample->cpu != s_alloc->alloc_cpu) { |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 289 | s_alloc->pingpong++; |
| 290 | |
| 291 | s_caller = search_alloc_stat(0, s_alloc->call_site, |
| 292 | &root_caller_stat, callsite_cmp); |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 293 | if (!s_caller) |
| 294 | return -1; |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 295 | s_caller->pingpong++; |
| 296 | } |
| 297 | s_alloc->alloc_cpu = -1; |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 298 | |
| 299 | return 0; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 300 | } |
| 301 | |
Arnaldo Carvalho de Melo | 0f7d2f1 | 2012-09-24 10:46:54 -0300 | [diff] [blame^] | 302 | typedef int (*tracepoint_handler)(struct perf_evsel *evsel, |
| 303 | struct perf_sample *sample); |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 304 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 305 | 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] | 306 | union perf_event *event, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 307 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | fcf65bf | 2012-08-07 09:58:03 -0300 | [diff] [blame] | 308 | struct perf_evsel *evsel, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 309 | struct machine *machine) |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 310 | { |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 311 | struct thread *thread = machine__findnew_thread(machine, event->ip.pid); |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 312 | |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 313 | if (thread == NULL) { |
| 314 | pr_debug("problem processing %d event, skipping it.\n", |
| 315 | event->header.type); |
| 316 | return -1; |
| 317 | } |
| 318 | |
| 319 | dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid); |
| 320 | |
Arnaldo Carvalho de Melo | 0f7d2f1 | 2012-09-24 10:46:54 -0300 | [diff] [blame^] | 321 | if (evsel->handler.func != NULL) { |
| 322 | tracepoint_handler f = evsel->handler.func; |
| 323 | return f(evsel, sample); |
| 324 | } |
| 325 | |
| 326 | return 0; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 327 | } |
| 328 | |
Arnaldo Carvalho de Melo | fcf65bf | 2012-08-07 09:58:03 -0300 | [diff] [blame] | 329 | static struct perf_tool perf_kmem = { |
| 330 | .sample = process_sample_event, |
| 331 | .comm = perf_event__process_comm, |
| 332 | .ordered_samples = true, |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 333 | }; |
| 334 | |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 335 | static double fragmentation(unsigned long n_req, unsigned long n_alloc) |
| 336 | { |
| 337 | if (n_alloc == 0) |
| 338 | return 0.0; |
| 339 | else |
| 340 | return 100.0 - (100.0 * n_req / n_alloc); |
| 341 | } |
| 342 | |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 343 | static void __print_result(struct rb_root *root, struct perf_session *session, |
| 344 | int n_lines, int is_caller) |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 345 | { |
| 346 | struct rb_node *next; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 347 | struct machine *machine; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 348 | |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 349 | printf("%.102s\n", graph_dotted_line); |
| 350 | printf(" %-34s |", is_caller ? "Callsite": "Alloc Ptr"); |
Pekka Enberg | 4710327 | 2010-01-19 19:23:23 +0200 | [diff] [blame] | 351 | printf(" Total_alloc/Per | Total_req/Per | Hit | Ping-pong | Frag\n"); |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 352 | printf("%.102s\n", graph_dotted_line); |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 353 | |
| 354 | next = rb_first(root); |
| 355 | |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 356 | machine = perf_session__find_host_machine(session); |
| 357 | if (!machine) { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 358 | pr_err("__print_result: couldn't find kernel information\n"); |
| 359 | return; |
| 360 | } |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 361 | while (next && n_lines--) { |
Arnaldo Carvalho de Melo | 1b145ae | 2009-11-23 17:51:09 -0200 | [diff] [blame] | 362 | struct alloc_stat *data = rb_entry(next, struct alloc_stat, |
| 363 | node); |
| 364 | struct symbol *sym = NULL; |
Arnaldo Carvalho de Melo | 71cf8b8 | 2010-04-01 21:24:38 -0300 | [diff] [blame] | 365 | struct map *map; |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 366 | char buf[BUFSIZ]; |
Arnaldo Carvalho de Melo | 1b145ae | 2009-11-23 17:51:09 -0200 | [diff] [blame] | 367 | u64 addr; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 368 | |
Arnaldo Carvalho de Melo | 1b145ae | 2009-11-23 17:51:09 -0200 | [diff] [blame] | 369 | if (is_caller) { |
| 370 | addr = data->call_site; |
Li Zefan | 7707b6b | 2009-11-24 13:25:48 +0800 | [diff] [blame] | 371 | if (!raw_ip) |
Arnaldo Carvalho de Melo | 5c0541d | 2010-04-29 15:25:23 -0300 | [diff] [blame] | 372 | sym = machine__find_kernel_function(machine, addr, &map, NULL); |
Arnaldo Carvalho de Melo | 1b145ae | 2009-11-23 17:51:09 -0200 | [diff] [blame] | 373 | } else |
| 374 | addr = data->ptr; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 375 | |
Arnaldo Carvalho de Melo | 1b145ae | 2009-11-23 17:51:09 -0200 | [diff] [blame] | 376 | if (sym != NULL) |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 377 | snprintf(buf, sizeof(buf), "%s+%" PRIx64 "", sym->name, |
Arnaldo Carvalho de Melo | 71cf8b8 | 2010-04-01 21:24:38 -0300 | [diff] [blame] | 378 | addr - map->unmap_ip(map, sym->start)); |
Arnaldo Carvalho de Melo | 1b145ae | 2009-11-23 17:51:09 -0200 | [diff] [blame] | 379 | else |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 380 | snprintf(buf, sizeof(buf), "%#" PRIx64 "", addr); |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 381 | printf(" %-34s |", buf); |
Arnaldo Carvalho de Melo | 1b145ae | 2009-11-23 17:51:09 -0200 | [diff] [blame] | 382 | |
Pekka Enberg | 4710327 | 2010-01-19 19:23:23 +0200 | [diff] [blame] | 383 | printf(" %9llu/%-5lu | %9llu/%-5lu | %8lu | %8lu | %6.3f%%\n", |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 384 | (unsigned long long)data->bytes_alloc, |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 385 | (unsigned long)data->bytes_alloc / data->hit, |
| 386 | (unsigned long long)data->bytes_req, |
| 387 | (unsigned long)data->bytes_req / data->hit, |
| 388 | (unsigned long)data->hit, |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 389 | (unsigned long)data->pingpong, |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 390 | fragmentation(data->bytes_req, data->bytes_alloc)); |
| 391 | |
| 392 | next = rb_next(next); |
| 393 | } |
| 394 | |
| 395 | if (n_lines == -1) |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 396 | printf(" ... | ... | ... | ... | ... | ... \n"); |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 397 | |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 398 | printf("%.102s\n", graph_dotted_line); |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | static void print_summary(void) |
| 402 | { |
| 403 | printf("\nSUMMARY\n=======\n"); |
| 404 | printf("Total bytes requested: %lu\n", total_requested); |
| 405 | printf("Total bytes allocated: %lu\n", total_allocated); |
| 406 | printf("Total bytes wasted on internal fragmentation: %lu\n", |
| 407 | total_allocated - total_requested); |
| 408 | printf("Internal fragmentation: %f%%\n", |
| 409 | fragmentation(total_requested, total_allocated)); |
Li Zefan | 7d0d394 | 2009-11-24 13:26:31 +0800 | [diff] [blame] | 410 | printf("Cross CPU allocations: %lu/%lu\n", nr_cross_allocs, nr_allocs); |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 411 | } |
| 412 | |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 413 | static void print_result(struct perf_session *session) |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 414 | { |
| 415 | if (caller_flag) |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 416 | __print_result(&root_caller_sorted, session, caller_lines, 1); |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 417 | if (alloc_flag) |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 418 | __print_result(&root_alloc_sorted, session, alloc_lines, 0); |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 419 | print_summary(); |
| 420 | } |
| 421 | |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [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(caller_sort); |
| 429 | static LIST_HEAD(alloc_sort); |
| 430 | |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 431 | static void sort_insert(struct rb_root *root, struct alloc_stat *data, |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 432 | struct list_head *sort_list) |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 433 | { |
| 434 | struct rb_node **new = &(root->rb_node); |
| 435 | struct rb_node *parent = NULL; |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 436 | struct sort_dimension *sort; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 437 | |
| 438 | while (*new) { |
| 439 | struct alloc_stat *this; |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 440 | int cmp = 0; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 441 | |
| 442 | this = rb_entry(*new, struct alloc_stat, node); |
| 443 | parent = *new; |
| 444 | |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 445 | list_for_each_entry(sort, sort_list, list) { |
| 446 | cmp = sort->cmp(data, this); |
| 447 | if (cmp) |
| 448 | break; |
| 449 | } |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 450 | |
| 451 | if (cmp > 0) |
| 452 | new = &((*new)->rb_left); |
| 453 | else |
| 454 | new = &((*new)->rb_right); |
| 455 | } |
| 456 | |
| 457 | rb_link_node(&data->node, parent, new); |
| 458 | rb_insert_color(&data->node, root); |
| 459 | } |
| 460 | |
| 461 | static void __sort_result(struct rb_root *root, struct rb_root *root_sorted, |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 462 | struct list_head *sort_list) |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 463 | { |
| 464 | struct rb_node *node; |
| 465 | struct alloc_stat *data; |
| 466 | |
| 467 | for (;;) { |
| 468 | node = rb_first(root); |
| 469 | if (!node) |
| 470 | break; |
| 471 | |
| 472 | rb_erase(node, root); |
| 473 | data = rb_entry(node, struct alloc_stat, node); |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 474 | sort_insert(root_sorted, data, sort_list); |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | |
| 478 | static void sort_result(void) |
| 479 | { |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 480 | __sort_result(&root_alloc_stat, &root_alloc_sorted, &alloc_sort); |
| 481 | __sort_result(&root_caller_stat, &root_caller_sorted, &caller_sort); |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | static int __cmd_kmem(void) |
| 485 | { |
Arnaldo Carvalho de Melo | d549c769 | 2009-12-27 21:37:02 -0200 | [diff] [blame] | 486 | int err = -EINVAL; |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 487 | struct perf_session *session; |
Arnaldo Carvalho de Melo | 0f7d2f1 | 2012-09-24 10:46:54 -0300 | [diff] [blame^] | 488 | const struct perf_evsel_str_handler kmem_tracepoints[] = { |
| 489 | { "kmem:kmalloc", perf_evsel__process_alloc_event, }, |
| 490 | { "kmem:kmem_cache_alloc", perf_evsel__process_alloc_event, }, |
| 491 | { "kmem:kmalloc_node", perf_evsel__process_alloc_node_event, }, |
| 492 | { "kmem:kmem_cache_alloc_node", perf_evsel__process_alloc_node_event, }, |
| 493 | { "kmem:kfree", perf_evsel__process_free_event, }, |
| 494 | { "kmem:kmem_cache_free", perf_evsel__process_free_event, }, |
| 495 | }; |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 496 | |
Arnaldo Carvalho de Melo | fcf65bf | 2012-08-07 09:58:03 -0300 | [diff] [blame] | 497 | session = perf_session__new(input_name, O_RDONLY, 0, false, &perf_kmem); |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 498 | if (session == NULL) |
| 499 | return -ENOMEM; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 500 | |
Arnaldo Carvalho de Melo | e727ca7 | 2010-04-01 19:12:13 -0300 | [diff] [blame] | 501 | if (perf_session__create_kernel_maps(session) < 0) |
| 502 | goto out_delete; |
| 503 | |
Arnaldo Carvalho de Melo | d549c769 | 2009-12-27 21:37:02 -0200 | [diff] [blame] | 504 | if (!perf_session__has_traces(session, "kmem record")) |
| 505 | goto out_delete; |
| 506 | |
Arnaldo Carvalho de Melo | 0f7d2f1 | 2012-09-24 10:46:54 -0300 | [diff] [blame^] | 507 | if (perf_session__set_tracepoints_handlers(session, kmem_tracepoints)) { |
| 508 | pr_err("Initializing perf session tracepoint handlers failed\n"); |
| 509 | return -1; |
| 510 | } |
| 511 | |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 512 | setup_pager(); |
Arnaldo Carvalho de Melo | fcf65bf | 2012-08-07 09:58:03 -0300 | [diff] [blame] | 513 | err = perf_session__process_events(session, &perf_kmem); |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 514 | if (err != 0) |
| 515 | goto out_delete; |
| 516 | sort_result(); |
| 517 | print_result(session); |
| 518 | out_delete: |
| 519 | perf_session__delete(session); |
| 520 | return err; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | static const char * const kmem_usage[] = { |
Li Zefan | 90b86a9 | 2009-12-10 15:21:57 +0800 | [diff] [blame] | 524 | "perf kmem [<options>] {record|stat}", |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 525 | NULL |
| 526 | }; |
| 527 | |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 528 | static int ptr_cmp(struct alloc_stat *l, struct alloc_stat *r) |
| 529 | { |
| 530 | if (l->ptr < r->ptr) |
| 531 | return -1; |
| 532 | else if (l->ptr > r->ptr) |
| 533 | return 1; |
| 534 | return 0; |
| 535 | } |
| 536 | |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 537 | static struct sort_dimension ptr_sort_dimension = { |
| 538 | .name = "ptr", |
| 539 | .cmp = ptr_cmp, |
| 540 | }; |
| 541 | |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 542 | static int callsite_cmp(struct alloc_stat *l, struct alloc_stat *r) |
| 543 | { |
| 544 | if (l->call_site < r->call_site) |
| 545 | return -1; |
| 546 | else if (l->call_site > r->call_site) |
| 547 | return 1; |
| 548 | return 0; |
| 549 | } |
| 550 | |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 551 | static struct sort_dimension callsite_sort_dimension = { |
| 552 | .name = "callsite", |
| 553 | .cmp = callsite_cmp, |
| 554 | }; |
| 555 | |
Pekka Enberg | f3ced7c | 2009-11-22 11:58:00 +0200 | [diff] [blame] | 556 | static int hit_cmp(struct alloc_stat *l, struct alloc_stat *r) |
| 557 | { |
| 558 | if (l->hit < r->hit) |
| 559 | return -1; |
| 560 | else if (l->hit > r->hit) |
| 561 | return 1; |
| 562 | return 0; |
| 563 | } |
| 564 | |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 565 | static struct sort_dimension hit_sort_dimension = { |
| 566 | .name = "hit", |
| 567 | .cmp = hit_cmp, |
| 568 | }; |
| 569 | |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 570 | static int bytes_cmp(struct alloc_stat *l, struct alloc_stat *r) |
| 571 | { |
| 572 | if (l->bytes_alloc < r->bytes_alloc) |
| 573 | return -1; |
| 574 | else if (l->bytes_alloc > r->bytes_alloc) |
| 575 | return 1; |
| 576 | return 0; |
| 577 | } |
| 578 | |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 579 | static struct sort_dimension bytes_sort_dimension = { |
| 580 | .name = "bytes", |
| 581 | .cmp = bytes_cmp, |
| 582 | }; |
| 583 | |
Pekka Enberg | f3ced7c | 2009-11-22 11:58:00 +0200 | [diff] [blame] | 584 | static int frag_cmp(struct alloc_stat *l, struct alloc_stat *r) |
| 585 | { |
| 586 | double x, y; |
| 587 | |
| 588 | x = fragmentation(l->bytes_req, l->bytes_alloc); |
| 589 | y = fragmentation(r->bytes_req, r->bytes_alloc); |
| 590 | |
| 591 | if (x < y) |
| 592 | return -1; |
| 593 | else if (x > y) |
| 594 | return 1; |
| 595 | return 0; |
| 596 | } |
| 597 | |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 598 | static struct sort_dimension frag_sort_dimension = { |
| 599 | .name = "frag", |
| 600 | .cmp = frag_cmp, |
| 601 | }; |
| 602 | |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 603 | static int pingpong_cmp(struct alloc_stat *l, struct alloc_stat *r) |
| 604 | { |
| 605 | if (l->pingpong < r->pingpong) |
| 606 | return -1; |
| 607 | else if (l->pingpong > r->pingpong) |
| 608 | return 1; |
| 609 | return 0; |
| 610 | } |
| 611 | |
| 612 | static struct sort_dimension pingpong_sort_dimension = { |
| 613 | .name = "pingpong", |
| 614 | .cmp = pingpong_cmp, |
| 615 | }; |
| 616 | |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 617 | static struct sort_dimension *avail_sorts[] = { |
| 618 | &ptr_sort_dimension, |
| 619 | &callsite_sort_dimension, |
| 620 | &hit_sort_dimension, |
| 621 | &bytes_sort_dimension, |
| 622 | &frag_sort_dimension, |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 623 | &pingpong_sort_dimension, |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 624 | }; |
| 625 | |
| 626 | #define NUM_AVAIL_SORTS \ |
| 627 | (int)(sizeof(avail_sorts) / sizeof(struct sort_dimension *)) |
| 628 | |
| 629 | static int sort_dimension__add(const char *tok, struct list_head *list) |
| 630 | { |
| 631 | struct sort_dimension *sort; |
| 632 | int i; |
| 633 | |
| 634 | for (i = 0; i < NUM_AVAIL_SORTS; i++) { |
| 635 | if (!strcmp(avail_sorts[i]->name, tok)) { |
| 636 | sort = malloc(sizeof(*sort)); |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 637 | if (!sort) { |
| 638 | pr_err("%s: malloc failed\n", __func__); |
| 639 | return -1; |
| 640 | } |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 641 | memcpy(sort, avail_sorts[i], sizeof(*sort)); |
| 642 | list_add_tail(&sort->list, list); |
| 643 | return 0; |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | return -1; |
| 648 | } |
| 649 | |
| 650 | static int setup_sorting(struct list_head *sort_list, const char *arg) |
| 651 | { |
| 652 | char *tok; |
| 653 | char *str = strdup(arg); |
| 654 | |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 655 | if (!str) { |
| 656 | pr_err("%s: strdup failed\n", __func__); |
| 657 | return -1; |
| 658 | } |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 659 | |
| 660 | while (true) { |
| 661 | tok = strsep(&str, ","); |
| 662 | if (!tok) |
| 663 | break; |
| 664 | if (sort_dimension__add(tok, sort_list) < 0) { |
| 665 | error("Unknown --sort key: '%s'", tok); |
Namhyung Kim | 1b22859 | 2012-01-08 02:25:29 +0900 | [diff] [blame] | 666 | free(str); |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 667 | return -1; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | free(str); |
| 672 | return 0; |
| 673 | } |
| 674 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 675 | static int parse_sort_opt(const struct option *opt __maybe_unused, |
| 676 | const char *arg, int unset __maybe_unused) |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 677 | { |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 678 | if (!arg) |
| 679 | return -1; |
| 680 | |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 681 | if (caller_flag > alloc_flag) |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 682 | return setup_sorting(&caller_sort, arg); |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 683 | else |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 684 | return setup_sorting(&alloc_sort, arg); |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 685 | |
| 686 | return 0; |
| 687 | } |
| 688 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 689 | static int parse_caller_opt(const struct option *opt __maybe_unused, |
| 690 | const char *arg __maybe_unused, |
| 691 | int unset __maybe_unused) |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 692 | { |
Li Zefan | 90b86a9 | 2009-12-10 15:21:57 +0800 | [diff] [blame] | 693 | caller_flag = (alloc_flag + 1); |
| 694 | return 0; |
| 695 | } |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 696 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 697 | static int parse_alloc_opt(const struct option *opt __maybe_unused, |
| 698 | const char *arg __maybe_unused, |
| 699 | int unset __maybe_unused) |
Li Zefan | 90b86a9 | 2009-12-10 15:21:57 +0800 | [diff] [blame] | 700 | { |
| 701 | alloc_flag = (caller_flag + 1); |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 702 | return 0; |
| 703 | } |
| 704 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 705 | static int parse_line_opt(const struct option *opt __maybe_unused, |
| 706 | const char *arg, int unset __maybe_unused) |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 707 | { |
| 708 | int lines; |
| 709 | |
| 710 | if (!arg) |
| 711 | return -1; |
| 712 | |
| 713 | lines = strtoul(arg, NULL, 10); |
| 714 | |
| 715 | if (caller_flag > alloc_flag) |
| 716 | caller_lines = lines; |
| 717 | else |
| 718 | alloc_lines = lines; |
| 719 | |
| 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | static const struct option kmem_options[] = { |
| 724 | OPT_STRING('i', "input", &input_name, "file", |
| 725 | "input file name"), |
Li Zefan | 90b86a9 | 2009-12-10 15:21:57 +0800 | [diff] [blame] | 726 | OPT_CALLBACK_NOOPT(0, "caller", NULL, NULL, |
| 727 | "show per-callsite statistics", |
| 728 | parse_caller_opt), |
| 729 | OPT_CALLBACK_NOOPT(0, "alloc", NULL, NULL, |
| 730 | "show per-allocation statistics", |
| 731 | parse_alloc_opt), |
Li Zefan | 29b3e15 | 2009-11-24 13:26:10 +0800 | [diff] [blame] | 732 | OPT_CALLBACK('s', "sort", NULL, "key[,key2...]", |
Li Zefan | 079d3f6 | 2009-11-24 13:26:55 +0800 | [diff] [blame] | 733 | "sort by keys: ptr, call_site, bytes, hit, pingpong, frag", |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 734 | parse_sort_opt), |
| 735 | OPT_CALLBACK('l', "line", NULL, "num", |
Li Zefan | 90b86a9 | 2009-12-10 15:21:57 +0800 | [diff] [blame] | 736 | "show n lines", |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 737 | parse_line_opt), |
Li Zefan | 7707b6b | 2009-11-24 13:25:48 +0800 | [diff] [blame] | 738 | OPT_BOOLEAN(0, "raw-ip", &raw_ip, "show raw ip instead of symbol"), |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 739 | OPT_END() |
| 740 | }; |
| 741 | |
| 742 | static const char *record_args[] = { |
| 743 | "record", |
| 744 | "-a", |
| 745 | "-R", |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 746 | "-f", |
| 747 | "-c", "1", |
| 748 | "-e", "kmem:kmalloc", |
| 749 | "-e", "kmem:kmalloc_node", |
| 750 | "-e", "kmem:kfree", |
| 751 | "-e", "kmem:kmem_cache_alloc", |
| 752 | "-e", "kmem:kmem_cache_alloc_node", |
| 753 | "-e", "kmem:kmem_cache_free", |
| 754 | }; |
| 755 | |
| 756 | static int __cmd_record(int argc, const char **argv) |
| 757 | { |
| 758 | unsigned int rec_argc, i, j; |
| 759 | const char **rec_argv; |
| 760 | |
| 761 | rec_argc = ARRAY_SIZE(record_args) + argc - 1; |
| 762 | rec_argv = calloc(rec_argc + 1, sizeof(char *)); |
| 763 | |
Chris Samuel | ce47dc5 | 2010-11-13 13:35:06 +1100 | [diff] [blame] | 764 | if (rec_argv == NULL) |
| 765 | return -ENOMEM; |
| 766 | |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 767 | for (i = 0; i < ARRAY_SIZE(record_args); i++) |
| 768 | rec_argv[i] = strdup(record_args[i]); |
| 769 | |
| 770 | for (j = 1; j < (unsigned int)argc; j++, i++) |
| 771 | rec_argv[i] = argv[j]; |
| 772 | |
| 773 | return cmd_record(i, rec_argv, NULL); |
| 774 | } |
| 775 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 776 | int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused) |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 777 | { |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 778 | argc = parse_options(argc, argv, kmem_options, kmem_usage, 0); |
| 779 | |
Li Zefan | 90b86a9 | 2009-12-10 15:21:57 +0800 | [diff] [blame] | 780 | if (!argc) |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 781 | usage_with_options(kmem_usage, kmem_options); |
| 782 | |
Arnaldo Carvalho de Melo | 655000e | 2009-12-15 20:04:40 -0200 | [diff] [blame] | 783 | symbol__init(); |
| 784 | |
Li Zefan | 90b86a9 | 2009-12-10 15:21:57 +0800 | [diff] [blame] | 785 | if (!strncmp(argv[0], "rec", 3)) { |
| 786 | return __cmd_record(argc, argv); |
| 787 | } else if (!strcmp(argv[0], "stat")) { |
Arnaldo Carvalho de Melo | 2814eb0 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 788 | if (setup_cpunode_map()) |
| 789 | return -1; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 790 | |
Li Zefan | 90b86a9 | 2009-12-10 15:21:57 +0800 | [diff] [blame] | 791 | if (list_empty(&caller_sort)) |
| 792 | setup_sorting(&caller_sort, default_sort_order); |
| 793 | if (list_empty(&alloc_sort)) |
| 794 | setup_sorting(&alloc_sort, default_sort_order); |
Li Zefan | 7d0d394 | 2009-11-24 13:26:31 +0800 | [diff] [blame] | 795 | |
Li Zefan | 90b86a9 | 2009-12-10 15:21:57 +0800 | [diff] [blame] | 796 | return __cmd_kmem(); |
Pekka Enberg | b00eca8 | 2010-01-19 19:26:11 +0200 | [diff] [blame] | 797 | } else |
| 798 | usage_with_options(kmem_usage, kmem_options); |
Li Zefan | 90b86a9 | 2009-12-10 15:21:57 +0800 | [diff] [blame] | 799 | |
| 800 | return 0; |
Li Zefan | ba77c9e | 2009-11-20 15:53:25 +0800 | [diff] [blame] | 801 | } |
| 802 | |