intel_pmu: address PR comments
[collectd.git] / src / intel_pmu.c
1 /**
2  * collectd - src/intel_pmu.c
3  *
4  * Copyright(c) 2017 Intel Corporation. All rights reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  * Authors:
25  *   Serhiy Pshyk <serhiyx.pshyk@intel.com>
26  **/
27
28 #include "collectd.h"
29 #include "common.h"
30
31 #include <jevents.h>
32 #include <jsession.h>
33
34 #define PMU_PLUGIN "intel_pmu"
35
36 #define HW_CACHE_READ_ACCESS                                                   \
37   (((PERF_COUNT_HW_CACHE_OP_READ) << 8) |                                      \
38    ((PERF_COUNT_HW_CACHE_RESULT_ACCESS) << 16))
39
40 #define HW_CACHE_WRITE_ACCESS                                                  \
41   (((PERF_COUNT_HW_CACHE_OP_WRITE) << 8) |                                     \
42    ((PERF_COUNT_HW_CACHE_RESULT_ACCESS) << 16))
43
44 #define HW_CACHE_PREFETCH_ACCESS                                               \
45   (((PERF_COUNT_HW_CACHE_OP_PREFETCH) << 8) |                                  \
46    ((PERF_COUNT_HW_CACHE_RESULT_ACCESS) << 16))
47
48 #define HW_CACHE_READ_MISS                                                     \
49   (((PERF_COUNT_HW_CACHE_OP_READ) << 8) |                                      \
50    ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))
51
52 #define HW_CACHE_WRITE_MISS                                                    \
53   (((PERF_COUNT_HW_CACHE_OP_WRITE) << 8) |                                     \
54    ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))
55
56 #define HW_CACHE_PREFETCH_MISS                                                 \
57   (((PERF_COUNT_HW_CACHE_OP_PREFETCH) << 8) |                                  \
58    ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))
59
60 struct event_info {
61   char *name;
62   uint64_t config;
63 };
64 typedef struct event_info event_info_t;
65
66 struct intel_pmu_ctx_s {
67   _Bool hw_cache_events;
68   _Bool kernel_pmu_events;
69   _Bool sw_events;
70   char  event_list_fn[PATH_MAX];
71   char **hw_events;
72   size_t hw_events_count;
73   struct eventlist *event_list;
74 };
75 typedef struct intel_pmu_ctx_s intel_pmu_ctx_t;
76
77 event_info_t g_kernel_pmu_events[] = {
78     {.name = "cpu-cycles", .config = PERF_COUNT_HW_CPU_CYCLES},
79     {.name = "instructions", .config = PERF_COUNT_HW_INSTRUCTIONS},
80     {.name = "cache-references", .config = PERF_COUNT_HW_CACHE_REFERENCES},
81     {.name = "cache-misses", .config = PERF_COUNT_HW_CACHE_MISSES},
82     {.name = "branches", .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS},
83     {.name = "branch-misses", .config = PERF_COUNT_HW_BRANCH_MISSES},
84     {.name = "bus-cycles", .config = PERF_COUNT_HW_BUS_CYCLES},
85 };
86
87 event_info_t g_hw_cache_events[] = {
88
89     {.name = "L1-dcache-loads",
90      .config = (PERF_COUNT_HW_CACHE_L1D | HW_CACHE_READ_ACCESS)},
91     {.name = "L1-dcache-load-misses",
92      .config = (PERF_COUNT_HW_CACHE_L1D | HW_CACHE_READ_MISS)},
93     {.name = "L1-dcache-stores",
94      .config = (PERF_COUNT_HW_CACHE_L1D | HW_CACHE_WRITE_ACCESS)},
95     {.name = "L1-dcache-store-misses",
96      .config = (PERF_COUNT_HW_CACHE_L1D | HW_CACHE_WRITE_MISS)},
97     {.name = "L1-dcache-prefetches",
98      .config = (PERF_COUNT_HW_CACHE_L1D | HW_CACHE_PREFETCH_ACCESS)},
99     {.name = "L1-dcache-prefetch-misses",
100      .config = (PERF_COUNT_HW_CACHE_L1D | HW_CACHE_PREFETCH_MISS)},
101
102     {.name = "L1-icache-loads",
103      .config = (PERF_COUNT_HW_CACHE_L1I | HW_CACHE_READ_ACCESS)},
104     {.name = "L1-icache-load-misses",
105      .config = (PERF_COUNT_HW_CACHE_L1I | HW_CACHE_READ_MISS)},
106     {.name = "L1-icache-prefetches",
107      .config = (PERF_COUNT_HW_CACHE_L1I | HW_CACHE_PREFETCH_ACCESS)},
108     {.name = "L1-icache-prefetch-misses",
109      .config = (PERF_COUNT_HW_CACHE_L1I | HW_CACHE_PREFETCH_MISS)},
110
111     {.name = "LLC-loads",
112      .config = (PERF_COUNT_HW_CACHE_LL | HW_CACHE_READ_ACCESS)},
113     {.name = "LLC-load-misses",
114      .config = (PERF_COUNT_HW_CACHE_LL | HW_CACHE_READ_MISS)},
115     {.name = "LLC-stores",
116      .config = (PERF_COUNT_HW_CACHE_LL | HW_CACHE_WRITE_ACCESS)},
117     {.name = "LLC-store-misses",
118      .config = (PERF_COUNT_HW_CACHE_LL | HW_CACHE_WRITE_MISS)},
119     {.name = "LLC-prefetches",
120      .config = (PERF_COUNT_HW_CACHE_LL | HW_CACHE_PREFETCH_ACCESS)},
121     {.name = "LLC-prefetch-misses",
122      .config = (PERF_COUNT_HW_CACHE_LL | HW_CACHE_PREFETCH_MISS)},
123
124     {.name = "dTLB-loads",
125      .config = (PERF_COUNT_HW_CACHE_DTLB | HW_CACHE_READ_ACCESS)},
126     {.name = "dTLB-load-misses",
127      .config = (PERF_COUNT_HW_CACHE_DTLB | HW_CACHE_READ_MISS)},
128     {.name = "dTLB-stores",
129      .config = (PERF_COUNT_HW_CACHE_DTLB | HW_CACHE_WRITE_ACCESS)},
130     {.name = "dTLB-store-misses",
131      .config = (PERF_COUNT_HW_CACHE_DTLB | HW_CACHE_WRITE_MISS)},
132     {.name = "dTLB-prefetches",
133      .config = (PERF_COUNT_HW_CACHE_DTLB | HW_CACHE_PREFETCH_ACCESS)},
134     {.name = "dTLB-prefetch-misses",
135      .config = (PERF_COUNT_HW_CACHE_DTLB | HW_CACHE_PREFETCH_MISS)},
136
137     {.name = "iTLB-loads",
138      .config = (PERF_COUNT_HW_CACHE_ITLB | HW_CACHE_READ_ACCESS)},
139     {.name = "iTLB-load-misses",
140      .config = (PERF_COUNT_HW_CACHE_ITLB | HW_CACHE_READ_MISS)},
141
142     {.name = "branch-loads",
143      .config = (PERF_COUNT_HW_CACHE_BPU | HW_CACHE_READ_ACCESS)},
144     {.name = "branch-load-misses",
145      .config = (PERF_COUNT_HW_CACHE_BPU | HW_CACHE_READ_MISS)},
146 };
147
148 event_info_t g_sw_events[] = {
149     {.name = "cpu-clock", .config = PERF_COUNT_SW_CPU_CLOCK},
150
151     {.name = "task-clock", .config = PERF_COUNT_SW_TASK_CLOCK},
152
153     {.name = "context-switches", .config = PERF_COUNT_SW_CONTEXT_SWITCHES},
154
155     {.name = "cpu-migrations", .config = PERF_COUNT_SW_CPU_MIGRATIONS},
156
157     {.name = "page-faults", .config = PERF_COUNT_SW_PAGE_FAULTS},
158
159     {.name = "minor-faults", .config = PERF_COUNT_SW_PAGE_FAULTS_MIN},
160
161     {.name = "major-faults", .config = PERF_COUNT_SW_PAGE_FAULTS_MAJ},
162
163     {.name = "alignment-faults", .config = PERF_COUNT_SW_ALIGNMENT_FAULTS},
164
165     {.name = "emulation-faults", .config = PERF_COUNT_SW_EMULATION_FAULTS},
166 };
167
168 static intel_pmu_ctx_t g_ctx;
169
170 #if COLLECT_DEBUG
171 static void pmu_dump_events() {
172
173   DEBUG(PMU_PLUGIN ": Events:");
174
175   struct event *e;
176
177   for (e = g_ctx.event_list->eventlist; e; e = e->next) {
178     DEBUG(PMU_PLUGIN ":   event       : %s", e->event);
179     DEBUG(PMU_PLUGIN ":     group_lead: %d", e->group_leader);
180     DEBUG(PMU_PLUGIN ":     end_group : %d", e->end_group);
181     DEBUG(PMU_PLUGIN ":     type      : %#x", e->attr.type);
182     DEBUG(PMU_PLUGIN ":     config    : %#x", (unsigned)e->attr.config);
183     DEBUG(PMU_PLUGIN ":     size      : %d", e->attr.size);
184   }
185
186   return;
187 }
188
189 static void pmu_dump_config(void) {
190
191   DEBUG(PMU_PLUGIN ": Config:");
192   DEBUG(PMU_PLUGIN ":   hw_cache_events   : %d", g_ctx.hw_cache_events);
193   DEBUG(PMU_PLUGIN ":   kernel_pmu_events : %d", g_ctx.kernel_pmu_events);
194   DEBUG(PMU_PLUGIN ":   software_events   : %d", g_ctx.sw_events);
195
196   for (size_t i = 0; i < g_ctx.hw_events_count; i++) {
197     DEBUG(PMU_PLUGIN ":   hardware_events[%zu]: %s", i, g_ctx.hw_events[i]);
198   }
199
200   return;
201 }
202
203 #endif /* COLLECT_DEBUG */
204
205 static int pmu_config_hw_events(oconfig_item_t *ci) {
206
207   if (strcasecmp("HardwareEvents", ci->key) != 0) {
208     return -EINVAL;
209   }
210
211   g_ctx.hw_events = calloc(ci->values_num, sizeof(char *));
212   if (g_ctx.hw_events == NULL) {
213     ERROR(PMU_PLUGIN ": Failed to allocate hw events.");
214     return -ENOMEM;
215   }
216
217   for (int i = 0; i < ci->values_num; i++) {
218     if (ci->values[i].type != OCONFIG_TYPE_STRING) {
219       WARNING(PMU_PLUGIN ": The %s option requires string arguments.", ci->key);
220       continue;
221     }
222
223     g_ctx.hw_events[g_ctx.hw_events_count] = strdup(ci->values[i].value.string);
224     if (g_ctx.hw_events[g_ctx.hw_events_count] == NULL) {
225       ERROR(PMU_PLUGIN ": Failed to allocate hw events entry.");
226       return -ENOMEM;
227     }
228
229     g_ctx.hw_events_count++;
230   }
231
232   return 0;
233 }
234
235 static int pmu_config(oconfig_item_t *ci) {
236   int ret = 0;
237
238   DEBUG(PMU_PLUGIN ": %s:%d", __FUNCTION__, __LINE__);
239
240   for (int i = 0; i < ci->children_num; i++) {
241     oconfig_item_t *child = ci->children + i;
242
243     if (strcasecmp("ReportHardwareCacheEvents", child->key) == 0) {
244       ret = cf_util_get_boolean(child, &g_ctx.hw_cache_events);
245     } else if (strcasecmp("ReportKernelPMUEvents", child->key) == 0) {
246       ret = cf_util_get_boolean(child, &g_ctx.kernel_pmu_events);
247     } else if (strcasecmp("EventList", child->key) == 0) {
248       ret = cf_util_get_string_buffer(child, g_ctx.event_list_fn,
249                                       sizeof(g_ctx.event_list_fn));
250     } else if (strcasecmp("HardwareEvents", child->key) == 0) {
251       ret = pmu_config_hw_events(child);
252     } else if (strcasecmp("ReportSoftwareEvents", child->key) == 0) {
253       ret = cf_util_get_boolean(child, &g_ctx.sw_events);
254     } else {
255       ERROR(PMU_PLUGIN ": Unknown configuration parameter \"%s\".", child->key);
256       ret = (-1);
257     }
258
259     if (ret != 0) {
260       DEBUG(PMU_PLUGIN ": %s:%d ret=%d", __FUNCTION__, __LINE__, ret);
261       return ret;
262     }
263   }
264
265 #if COLLECT_DEBUG
266   pmu_dump_config();
267 #endif
268
269   return 0;
270 }
271
272 static void pmu_submit_counter(int cpu, char *event, counter_t value) {
273   value_list_t vl = VALUE_LIST_INIT;
274
275   vl.values = &(value_t){.counter = value};
276   vl.values_len = 1;
277
278   sstrncpy(vl.plugin, PMU_PLUGIN, sizeof(vl.plugin));
279   if (cpu == -1) {
280     ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "all");
281   } else {
282     ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%d", cpu);
283   }
284   sstrncpy(vl.type, "counter", sizeof(vl.type));
285   sstrncpy(vl.type_instance, event, sizeof(vl.type_instance));
286
287   plugin_dispatch_values(&vl);
288 }
289
290 static int pmu_dispatch_data(void) {
291
292   struct event *e;
293
294   for (e = g_ctx.event_list->eventlist; e; e = e->next) {
295     uint64_t all_value = 0;
296     int event_enabled = 0;
297     for (int i = 0; i < g_ctx.event_list->num_cpus; i++) {
298
299       if (e->efd[i].fd < 0)
300         continue;
301
302       event_enabled++;
303
304       uint64_t value = event_scaled_value(e, i);
305       all_value += value;
306
307       /* dispatch per CPU value */
308       pmu_submit_counter(i, e->event, value);
309     }
310
311     if (event_enabled > 0) {
312       DEBUG(PMU_PLUGIN ": %-20s %'10lu", e->event, all_value);
313       /* dispatch all CPU value */
314       pmu_submit_counter(-1, e->event, all_value);
315     }
316   }
317
318   return 0;
319 }
320
321 static int pmu_read(__attribute__((unused)) user_data_t *ud) {
322   int ret;
323
324   DEBUG(PMU_PLUGIN ": %s:%d", __FUNCTION__, __LINE__);
325
326   ret = read_all_events(g_ctx.event_list);
327   if (ret != 0) {
328     ERROR(PMU_PLUGIN ": Failed to read values of all events.");
329     return 0;
330   }
331
332   ret = pmu_dispatch_data();
333   if (ret != 0) {
334     ERROR(PMU_PLUGIN ": Failed to dispatch event values.");
335     return 0;
336   }
337
338   return 0;
339 }
340
341 static int pmu_add_events(struct eventlist *el, uint32_t type,
342                           event_info_t *events, int count) {
343
344   for (int i = 0; i < count; i++) {
345     /* Allocate memory for event struct that contains array of efd structs
346        for all cores */
347     struct event *e =
348         calloc(sizeof(struct event) + sizeof(struct efd) * el->num_cpus, 1);
349     if (e == NULL) {
350       ERROR(PMU_PLUGIN ": Failed to allocate event structure");
351       return -ENOMEM;
352     }
353
354     e->attr.type = type;
355     e->attr.config = events[i].config;
356     e->attr.size = PERF_ATTR_SIZE_VER0;
357     e->next = NULL;
358     if (!el->eventlist)
359       el->eventlist = e;
360     if (el->eventlist_last)
361       el->eventlist_last->next = e;
362     el->eventlist_last = e;
363     e->event = strdup(events[i].name);
364   }
365
366   return 0;
367 }
368
369 static int pmu_add_hw_events(struct eventlist *el, char **e, size_t count) {
370
371   for (size_t i = 0; i < count; i++) {
372
373     size_t group_events_count = 0;
374
375     char *events = strdup(e[i]);
376     if (!events)
377       return -1;
378
379     char *s, *tmp;
380     for (s = strtok_r(events, ",", &tmp); s; s = strtok_r(NULL, ",", &tmp)) {
381
382       /* Multiple events parsed in one entry */
383       if (group_events_count == 1) {
384         /* Mark previously added event as group leader */
385         el->eventlist_last->group_leader = 1;
386       }
387
388       /* Allocate memory for event struct that contains array of efd structs
389          for all cores */
390       struct event *e =
391           calloc(sizeof(struct event) + sizeof(struct efd) * el->num_cpus, 1);
392       if (e == NULL) {
393         free(events);
394         return -ENOMEM;
395       }
396
397       if (resolve_event(s, &e->attr) == 0) {
398         e->next = NULL;
399         if (!el->eventlist)
400           el->eventlist = e;
401         if (el->eventlist_last)
402           el->eventlist_last->next = e;
403         el->eventlist_last = e;
404         e->event = strdup(s);
405       } else {
406         DEBUG(PMU_PLUGIN ": Cannot resolve %s", s);
407         sfree(e);
408       }
409
410       group_events_count++;
411     }
412
413     /* Multiple events parsed in one entry */
414     if (group_events_count > 1) {
415       /* Mark last added event as group end */
416       el->eventlist_last->end_group = 1;
417     }
418
419     free(events);
420   }
421
422   return 0;
423 }
424
425 static void pmu_free_events(struct eventlist *el) {
426
427   if (el == NULL)
428     return;
429
430   struct event *e = el->eventlist;
431
432   while (e) {
433     struct event *next = e->next;
434     sfree(e);
435     e = next;
436   }
437
438   el->eventlist = NULL;
439 }
440
441 static int pmu_setup_events(struct eventlist *el, bool measure_all,
442                             int measure_pid) {
443   struct event *e, *leader = NULL;
444   int ret = -1;
445
446   for (e = el->eventlist; e; e = e->next) {
447
448     for (int i = 0; i < el->num_cpus; i++) {
449       if (setup_event(e, i, leader, measure_all, measure_pid) < 0) {
450         WARNING(PMU_PLUGIN ": perf event '%s' is not available (cpu=%d).",
451                 e->event, i);
452       } else {
453         /* success if at least one event was set */
454         ret = 0;
455       }
456     }
457
458     if (e->group_leader)
459       leader = e;
460     if (e->end_group)
461       leader = NULL;
462   }
463
464   return ret;
465 }
466
467 static int pmu_init(void) {
468   int ret;
469
470   DEBUG(PMU_PLUGIN ": %s:%d", __FUNCTION__, __LINE__);
471
472   g_ctx.event_list = alloc_eventlist();
473   if (g_ctx.event_list == NULL) {
474     ERROR(PMU_PLUGIN ": Failed to allocate event list.");
475     return -ENOMEM;
476   }
477
478   if (g_ctx.hw_cache_events) {
479     ret =
480         pmu_add_events(g_ctx.event_list, PERF_TYPE_HW_CACHE, g_hw_cache_events,
481                        STATIC_ARRAY_SIZE(g_hw_cache_events));
482     if (ret != 0) {
483       ERROR(PMU_PLUGIN ": Failed to add hw cache events.");
484       goto init_error;
485     }
486   }
487
488   if (g_ctx.kernel_pmu_events) {
489     ret = pmu_add_events(g_ctx.event_list, PERF_TYPE_HARDWARE,
490                          g_kernel_pmu_events,
491                          STATIC_ARRAY_SIZE(g_kernel_pmu_events));
492     if (ret != 0) {
493       ERROR(PMU_PLUGIN ": Failed to add kernel PMU events.");
494       goto init_error;
495     }
496   }
497
498   /* parse events names if config option is present and is not empty */
499   if (g_ctx.hw_events_count) {
500
501     ret = read_events(g_ctx.event_list_fn);
502     if (ret != 0) {
503       ERROR(PMU_PLUGIN ": Failed to read event list file '%s'.",
504             g_ctx.event_list_fn);
505       return ret;
506     }
507
508     ret = pmu_add_hw_events(g_ctx.event_list, g_ctx.hw_events,
509                             g_ctx.hw_events_count);
510     if (ret != 0) {
511       ERROR(PMU_PLUGIN ": Failed to add hardware events.");
512       goto init_error;
513     }
514   }
515
516   if (g_ctx.sw_events) {
517     ret = pmu_add_events(g_ctx.event_list, PERF_TYPE_SOFTWARE, g_sw_events,
518                          STATIC_ARRAY_SIZE(g_sw_events));
519     if (ret != 0) {
520       ERROR(PMU_PLUGIN ": Failed to add software events.");
521       goto init_error;
522     }
523   }
524
525 #if COLLECT_DEBUG
526   pmu_dump_events();
527 #endif
528
529   if (g_ctx.event_list->eventlist != NULL) {
530     /* measure all processes */
531     ret = pmu_setup_events(g_ctx.event_list, true, -1);
532     if (ret != 0) {
533       ERROR(PMU_PLUGIN ": Failed to setup perf events for the event list.");
534       goto init_error;
535     }
536   } else {
537     WARNING(PMU_PLUGIN
538             ": Events list is empty. No events were setup for monitoring.");
539   }
540
541   return 0;
542
543 init_error:
544
545   pmu_free_events(g_ctx.event_list);
546   sfree(g_ctx.event_list);
547   for (size_t i = 0; i < g_ctx.hw_events_count; i++) {
548     sfree(g_ctx.hw_events[i]);
549   }
550   sfree(g_ctx.hw_events);
551   g_ctx.hw_events_count = 0;
552
553
554   return ret;
555 }
556
557 static int pmu_shutdown(void) {
558
559   DEBUG(PMU_PLUGIN ": %s:%d", __FUNCTION__, __LINE__);
560
561   pmu_free_events(g_ctx.event_list);
562   sfree(g_ctx.event_list);
563   for (size_t i = 0; i < g_ctx.hw_events_count; i++) {
564     sfree(g_ctx.hw_events[i]);
565   }
566   sfree(g_ctx.hw_events);
567   g_ctx.hw_events_count = 0;
568
569   return 0;
570 }
571
572 void module_register(void) {
573   plugin_register_init(PMU_PLUGIN, pmu_init);
574   plugin_register_complex_config(PMU_PLUGIN, pmu_config);
575   plugin_register_complex_read(NULL, PMU_PLUGIN, pmu_read, 0, NULL);
576   plugin_register_shutdown(PMU_PLUGIN, pmu_shutdown);
577 }