+/*
+ * Context options
+ */
+#define PERF_HW(opt, name) \
+ { \
+ "perf:" #opt, CONTEXT_PERF_COUNTER, \
+ .u.perf = { PERF_TYPE_HARDWARE, PERF_COUNT_HW_##name, },\
+ }
+
+#define PERF_SW(opt, name) \
+ { \
+ "perf:" #opt, CONTEXT_PERF_COUNTER, \
+ .u.perf = { PERF_TYPE_SOFTWARE, PERF_COUNT_SW_##name, },\
+ }
+
+#define _PERF_HW_CACHE(optstr, name, op, result) \
+ { \
+ "perf:" optstr, CONTEXT_PERF_COUNTER, \
+ .u.perf = { \
+ PERF_TYPE_HW_CACHE, \
+ (uint64_t) PERF_COUNT_HW_CACHE_##name \
+ | ((uint64_t) PERF_COUNT_HW_CACHE_OP_##op << 8) \
+ | ((uint64_t) PERF_COUNT_HW_CACHE_RESULT_##result << 16), \
+ }, \
+ }
+
+#define PERF_HW_CACHE(opt, name) \
+ _PERF_HW_CACHE(#opt "-loads", name, READ, ACCESS), \
+ _PERF_HW_CACHE(#opt "-load-misses", name, READ, MISS), \
+ _PERF_HW_CACHE(#opt "-stores", name, WRITE, ACCESS), \
+ _PERF_HW_CACHE(#opt "-store-misses", name, WRITE, MISS), \
+ _PERF_HW_CACHE(#opt "-prefetches", name, PREFETCH, ACCESS), \
+ _PERF_HW_CACHE(#opt "-prefetch-misses", name, PREFETCH, MISS) \
+
+static
+const struct ctx_opts {
+ char *symbol;
+ enum context_type ctx_type;
+ union {
+ struct {
+ uint32_t type;
+ uint64_t config;
+ } perf;
+ } u;
+} ctx_opts[] = {
+ { "pid", CONTEXT_PID },
+ { "comm", CONTEXT_COMM },
+ { "prio", CONTEXT_PRIO },
+ { "nice", CONTEXT_NICE },
+ { "vpid", CONTEXT_VPID },
+ { "tid", CONTEXT_TID },
+ { "vtid", CONTEXT_VTID },
+ { "ppid", CONTEXT_PPID },
+ { "vppid", CONTEXT_VPPID },
+ /* Perf options */
+ PERF_HW(cpu-cycles, CPU_CYCLES),
+ PERF_HW(cycles, CPU_CYCLES),
+ PERF_HW(stalled-cycles-frontend, STALLED_CYCLES_FRONTEND),
+ PERF_HW(idle-cycles-frontend, STALLED_CYCLES_FRONTEND),
+ PERF_HW(stalled-cycles-backend, STALLED_CYCLES_BACKEND),
+ PERF_HW(idle-cycles-backend, STALLED_CYCLES_BACKEND),
+ PERF_HW(instructions, INSTRUCTIONS),
+ PERF_HW(cache-references, CACHE_REFERENCES),
+ PERF_HW(cache-misses, CACHE_MISSES),
+ PERF_HW(branch-instructions, BRANCH_INSTRUCTIONS),
+ PERF_HW(branches, BRANCH_INSTRUCTIONS),
+ PERF_HW(branch-misses, BRANCH_MISSES),
+ PERF_HW(bus-cycles, BUS_CYCLES),
+
+ PERF_HW_CACHE(L1-dcache, L1D),
+ PERF_HW_CACHE(L1-icache, L1I),
+ PERF_HW_CACHE(LLC, LL),
+ PERF_HW_CACHE(dTLB, DTLB),
+ _PERF_HW_CACHE("iTLB-loads", ITLB, READ, ACCESS),
+ _PERF_HW_CACHE("iTLB-load-misses", ITLB, READ, MISS),
+ _PERF_HW_CACHE("branch-loads", BPU, READ, ACCESS),
+ _PERF_HW_CACHE("branch-load-misses", BPU, READ, MISS),
+
+
+ PERF_SW(cpu-clock, CPU_CLOCK),
+ PERF_SW(task-clock, TASK_CLOCK),
+ PERF_SW(page-fault, PAGE_FAULTS),
+ PERF_SW(faults, PAGE_FAULTS),
+ PERF_SW(major-faults, PAGE_FAULTS_MAJ),
+ PERF_SW(minor-faults, PAGE_FAULTS_MIN),
+ PERF_SW(context-switches, CONTEXT_SWITCHES),
+ PERF_SW(cs, CONTEXT_SWITCHES),
+ PERF_SW(cpu-migrations, CPU_MIGRATIONS),
+ PERF_SW(migrations, CPU_MIGRATIONS),
+ PERF_SW(alignment-faults, ALIGNMENT_FAULTS),
+ PERF_SW(emulation-faults, EMULATION_FAULTS),
+ { NULL, -1 }, /* Closure */
+};
+
+#undef PERF_SW
+#undef PERF_HW
+
+/*
+ * Context type for command line option parsing.
+ */
+struct ctx_type {
+ const struct ctx_opts *opt;
+ struct cds_list_head list;
+};
+
+/*
+ * List of context type. Use to enable multiple context on a single command
+ * line entry.
+ */
+struct ctx_type_list {
+ struct cds_list_head head;
+} ctx_type_list = {
+ .head = CDS_LIST_HEAD_INIT(ctx_type_list.head),
+};
+
+/*
+ * Pretty print context type.
+ */
+static void print_ctx_type(FILE *ofp)
+{
+ const char *indent = " ";
+ int indent_len = strlen(indent);
+ int len, i = 0;
+
+ fprintf(ofp, "%s", indent);
+ len = indent_len;
+ while (ctx_opts[i].symbol != NULL) {
+ if (len > indent_len) {
+ if (len + strlen(ctx_opts[i].symbol) + 2
+ >= PRINT_LINE_LEN) {
+ fprintf(ofp, ",\n");
+ fprintf(ofp, "%s", indent);
+ len = indent_len;
+ } else {
+ len += fprintf(ofp, ", ");
+ }
+ }
+ len += fprintf(ofp, "%s", ctx_opts[i].symbol);
+ i++;
+ }
+}
+