diff mbox

[lttng-tools] Enable perf PMU counters by raw ID

Message ID 1466536691-9172-1-git-send-email-jdesfossez@efficios.com
State Superseded, archived
Headers show

Commit Message

Julien Desfossez June 21, 2016, 7:18 p.m. UTC
Allow enabling perf PMU counters by raw ID in addition to the generic
list already provided. The format for kernel tracing is
"perf:cpu:raw:rNNN:<name>" and "perf:thread:raw:rNNN:<name> for
user-space. The rNNN format is the same as perf-record(1) where NNN is a
hexadecimal event descriptor in the form of umask+eventsel. The <name>
field allows the user to give a more friendly name.

Example usage on Intel i7-3520M to get the unhalted reference cycles
(eventsel: 0x13c) count at privilege level 0 (umask: 0x00):
lttng add-context -k -t perf:cpu:raw:r0013c:x86unhalted

Result in the trace:
sched_switch: { cpu_id = 3 }, {
	perf_cpu_raw_r0013c_x86unhalted = 27632578 }, [...]

Signed-off-by: Julien Desfossez <jdesfossez at efficios.com>
---
 doc/man/lttng-add-context.1.txt      |  7 +++
 include/lttng/event.h                |  2 +
 src/bin/lttng-sessiond/context.c     |  1 +
 src/bin/lttng-sessiond/trace-ust.c   |  1 +
 src/bin/lttng/commands/add_context.c | 94 +++++++++++++++++++++++++++++++++++-
 5 files changed, 104 insertions(+), 1 deletion(-)

Comments

Mathieu Desnoyers June 21, 2016, 8:20 p.m. UTC | #1
----- On Jun 21, 2016, at 3:18 PM, Julien Desfossez jdesfossez at efficios.com wrote:

> Allow enabling perf PMU counters by raw ID in addition to the generic
> list already provided. The format for kernel tracing is
> "perf:cpu:raw:rNNN:<name>" and "perf:thread:raw:rNNN:<name> for
> user-space. The rNNN format is the same as perf-record(1) where NNN is a
> hexadecimal event descriptor in the form of umask+eventsel. The <name>
> field allows the user to give a more friendly name.
> 
> Example usage on Intel i7-3520M to get the unhalted reference cycles
> (eventsel: 0x13c) count at privilege level 0 (umask: 0x00):
> lttng add-context -k -t perf:cpu:raw:r0013c:x86unhalted
> 
> Result in the trace:
> sched_switch: { cpu_id = 3 }, {
>	perf_cpu_raw_r0013c_x86unhalted = 27632578 }, [...]
> 
> Signed-off-by: Julien Desfossez <jdesfossez at efficios.com>
> ---
> doc/man/lttng-add-context.1.txt      |  7 +++
> include/lttng/event.h                |  2 +
> src/bin/lttng-sessiond/context.c     |  1 +
> src/bin/lttng-sessiond/trace-ust.c   |  1 +
> src/bin/lttng/commands/add_context.c | 94 +++++++++++++++++++++++++++++++++++-
> 5 files changed, 104 insertions(+), 1 deletion(-)
> 
> diff --git a/doc/man/lttng-add-context.1.txt b/doc/man/lttng-add-context.1.txt
> index f995a7f..4779672 100644
> --- a/doc/man/lttng-add-context.1.txt
> +++ b/doc/man/lttng-add-context.1.txt
> @@ -45,6 +45,13 @@ per-thread (`perf:thread:` prefix) counters. Currently,
> per-CPU counters
> can only be used in the Linux kernel tracing domain, while per-thread
> counters can only be used in the user space tracing domain.
> 
> +It is also possible to enable PMU counters by raw ID using the
> +`perf:cpu:raw:rNNN:<name>` or `perf:thread:raw:rNNN:<name>` format for the
> +kernel and user-space respectively. `NNN` is a hexadecimal event descriptor
> +(umask+eventsel) just like with perf-record(1), the possible values for this
> +field are processor-specific. The `<name>` field is used to give a symbolic
> +name to the counter in the trace.
> +
> Application-specific context fields can be added to a channel using the
> following syntax:
> 
> diff --git a/include/lttng/event.h b/include/lttng/event.h
> index 16b4d4f..398f94c 100644
> --- a/include/lttng/event.h
> +++ b/include/lttng/event.h
> @@ -142,6 +142,8 @@ enum lttng_event_context_type {
> 	LTTNG_EVENT_CONTEXT_PREEMPTIBLE		= 17,
> 	LTTNG_EVENT_CONTEXT_NEED_RESCHEDULE	= 18,
> 	LTTNG_EVENT_CONTEXT_MIGRATABLE		= 19,
> +	LTTNG_EVENT_CONTEXT_PERF_CPU_RAW	= 20,
> +	LTTNG_EVENT_CONTEXT_PERF_THREAD_RAW	= 21,
> };
> 
> enum lttng_event_field_type {
> diff --git a/src/bin/lttng-sessiond/context.c b/src/bin/lttng-sessiond/context.c
> index 9c3a394..81089b1 100644
> --- a/src/bin/lttng-sessiond/context.c
> +++ b/src/bin/lttng-sessiond/context.c
> @@ -227,6 +227,7 @@ int context_kernel_add(struct ltt_kernel_session *ksession,
> 		break;
> 	case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER:
> 	case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
> +	case LTTNG_EVENT_CONTEXT_PERF_CPU_RAW:
> 		kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER;
> 		break;
> 	case LTTNG_EVENT_CONTEXT_INTERRUPTIBLE:
> diff --git a/src/bin/lttng-sessiond/trace-ust.c
> b/src/bin/lttng-sessiond/trace-ust.c
> index 1c325fb..b75d11b 100644
> --- a/src/bin/lttng-sessiond/trace-ust.c
> +++ b/src/bin/lttng-sessiond/trace-ust.c
> @@ -541,6 +541,7 @@ int trace_ust_context_type_event_to_ust(
> 		utype = LTTNG_UST_CONTEXT_IP;
> 		break;
> 	case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
> +	case LTTNG_EVENT_CONTEXT_PERF_THREAD_RAW:

Do we really need to distinguish between
raw and non-raw counters in the sessiond ?
I thought the only difference would be when
parsing the command line/liblttng-ctl.

> 		if (!ustctl_has_perf_counters()) {
> 			utype = -1;
> 			WARN("Perf counters not implemented in UST");
> diff --git a/src/bin/lttng/commands/add_context.c
> b/src/bin/lttng/commands/add_context.c
> index 2f43dc7..4db9bd2 100644
> --- a/src/bin/lttng/commands/add_context.c
> +++ b/src/bin/lttng/commands/add_context.c
> @@ -78,6 +78,8 @@ enum context_type {
> 	CONTEXT_PREEMPTIBLE  = 17,
> 	CONTEXT_NEED_RESCHEDULE = 18,
> 	CONTEXT_MIGRATABLE   = 19,
> +	CONTEXT_PERF_CPU_RAW = 20,
> +	CONTEXT_PERF_THREAD_RAW = 21,
> };
> 
> /*
> @@ -87,6 +89,7 @@ enum perf_type {
> 	PERF_TYPE_HARDWARE = 0,
> 	PERF_TYPE_SOFTWARE = 1,
> 	PERF_TYPE_HW_CACHE = 3,
> +	PERF_TYPE_RAW = 4,
> };
> 
> enum perf_count_hard {
> @@ -564,6 +567,8 @@ static int add_context(char *session_name)
> 		case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
> 		case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER:
> 		case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
> +		case LTTNG_EVENT_CONTEXT_PERF_CPU_RAW:
> +		case LTTNG_EVENT_CONTEXT_PERF_THREAD_RAW:
> 			context.u.perf_counter.type = type->opt->u.perf.type;
> 			context.u.perf_counter.config = type->opt->u.perf.config;
> 			strncpy(context.u.perf_counter.name, type->opt->symbol,
> @@ -688,9 +693,88 @@ end:
> }
> 
> static
> +int find_ctx_type_perf_raw(const char *ctx, struct ctx_type *type)
> +{
> +	char *next;
> +	int ret;
> +	int field_pos = 0;
> +	char *tmp_list;
> +
> +	tmp_list = strdup(ctx);
> +	if (!tmp_list) {
> +		PERROR("strdup temp list");
> +		ret = -ENOMEM;
> +		goto error;
> +	}
> +
> +	/* Looking for "perf:[cpu|thread]:raw:<mask>:<name>". */
> +	for (;;) {
> +		next = strtok(tmp_list, ":");
> +		if (!next) {
> +			break;
> +		}
> +		tmp_list = NULL;
> +		switch (field_pos) {
> +		case 0:
> +			if (strncmp(next, "perf", 4) != 0) {
> +				ret = -1;
> +				goto end;
> +			}
> +			break;
> +		case 1:
> +			if (strncmp(next, "cpu", 3) == 0) {
> +				type->opt->ctx_type = CONTEXT_PERF_CPU_RAW;
> +			} else if (strncmp(next, "thread", 4) == 0) {
> +				type->opt->ctx_type = CONTEXT_PERF_THREAD_RAW;
> +			} else {
> +				ret = -1;
> +				goto end;
> +			}
> +			break;
> +		case 2:
> +			if (strncmp(next, "raw", 3) != 0) {
> +				ret = -1;
> +				goto end;
> +			}
> +			break;
> +		case 3:
> +			if (strlen(next) < 2 || next[0] != 'r') {
> +				ERR("Wrong perf raw mask format: rNNN");
> +				ret = -1;
> +				goto end;
> +			}
> +			type->opt->u.perf.config = strtoll(next +  1, NULL, 16);
> +			break;
> +		case 4:
> +			/* name */
> +			break;
> +		case 5:
> +			ERR("Too many ':' in perf raw format");
> +			ret = -1;
> +			goto end;
> +		};
> +		field_pos++;
> +	}
> +
> +	if (field_pos < 5) {
> +		ERR("Wrong perf raw format");
> +		ret = -1;
> +		goto end;
> +	}
> +
> +	ret = 0;
> +	goto end;
> +
> +end:
> +	free(tmp_list);
> +error:
> +	return ret;
> +}
> +
> +static
> struct ctx_type *get_context_type(const char *ctx)
> {
> -	int opt_index;
> +	int opt_index, ret;
> 	struct ctx_type *type = NULL;
> 	const char app_ctx_prefix[] = "$app.";
> 	char *provider_name = NULL, *ctx_name = NULL;
> @@ -713,6 +797,14 @@ struct ctx_type *get_context_type(const char *ctx)
> 		goto found;
> 	}
> 
> +	/* Check if ctx is a raw perf context. */
> +	ret = find_ctx_type_perf_raw(ctx, type);
> +	if (ret == 0) {
> +		type->opt->u.perf.type = PERF_TYPE_RAW;
> +		type->opt->symbol = strdup(ctx);

Missing NULL check.

Thanks,

Mathieu

> +		goto found;
> +	}
> +
> 	/*
> 	 * No match found against static contexts; check if it is an app
> 	 * context.
> --
> 1.9.1
> 
> _______________________________________________
> lttng-dev mailing list
> lttng-dev at lists.lttng.org
> https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
diff mbox

Patch

diff --git a/doc/man/lttng-add-context.1.txt b/doc/man/lttng-add-context.1.txt
index f995a7f..4779672 100644
--- a/doc/man/lttng-add-context.1.txt
+++ b/doc/man/lttng-add-context.1.txt
@@ -45,6 +45,13 @@  per-thread (`perf:thread:` prefix) counters. Currently, per-CPU counters
 can only be used in the Linux kernel tracing domain, while per-thread
 counters can only be used in the user space tracing domain.
 
+It is also possible to enable PMU counters by raw ID using the
+`perf:cpu:raw:rNNN:<name>` or `perf:thread:raw:rNNN:<name>` format for the
+kernel and user-space respectively. `NNN` is a hexadecimal event descriptor
+(umask+eventsel) just like with perf-record(1), the possible values for this
+field are processor-specific. The `<name>` field is used to give a symbolic
+name to the counter in the trace.
+
 Application-specific context fields can be added to a channel using the
 following syntax:
 
diff --git a/include/lttng/event.h b/include/lttng/event.h
index 16b4d4f..398f94c 100644
--- a/include/lttng/event.h
+++ b/include/lttng/event.h
@@ -142,6 +142,8 @@  enum lttng_event_context_type {
 	LTTNG_EVENT_CONTEXT_PREEMPTIBLE		= 17,
 	LTTNG_EVENT_CONTEXT_NEED_RESCHEDULE	= 18,
 	LTTNG_EVENT_CONTEXT_MIGRATABLE		= 19,
+	LTTNG_EVENT_CONTEXT_PERF_CPU_RAW	= 20,
+	LTTNG_EVENT_CONTEXT_PERF_THREAD_RAW	= 21,
 };
 
 enum lttng_event_field_type {
diff --git a/src/bin/lttng-sessiond/context.c b/src/bin/lttng-sessiond/context.c
index 9c3a394..81089b1 100644
--- a/src/bin/lttng-sessiond/context.c
+++ b/src/bin/lttng-sessiond/context.c
@@ -227,6 +227,7 @@  int context_kernel_add(struct ltt_kernel_session *ksession,
 		break;
 	case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER:
 	case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
+	case LTTNG_EVENT_CONTEXT_PERF_CPU_RAW:
 		kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER;
 		break;
 	case LTTNG_EVENT_CONTEXT_INTERRUPTIBLE:
diff --git a/src/bin/lttng-sessiond/trace-ust.c b/src/bin/lttng-sessiond/trace-ust.c
index 1c325fb..b75d11b 100644
--- a/src/bin/lttng-sessiond/trace-ust.c
+++ b/src/bin/lttng-sessiond/trace-ust.c
@@ -541,6 +541,7 @@  int trace_ust_context_type_event_to_ust(
 		utype = LTTNG_UST_CONTEXT_IP;
 		break;
 	case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
+	case LTTNG_EVENT_CONTEXT_PERF_THREAD_RAW:
 		if (!ustctl_has_perf_counters()) {
 			utype = -1;
 			WARN("Perf counters not implemented in UST");
diff --git a/src/bin/lttng/commands/add_context.c b/src/bin/lttng/commands/add_context.c
index 2f43dc7..4db9bd2 100644
--- a/src/bin/lttng/commands/add_context.c
+++ b/src/bin/lttng/commands/add_context.c
@@ -78,6 +78,8 @@  enum context_type {
 	CONTEXT_PREEMPTIBLE  = 17,
 	CONTEXT_NEED_RESCHEDULE = 18,
 	CONTEXT_MIGRATABLE   = 19,
+	CONTEXT_PERF_CPU_RAW = 20,
+	CONTEXT_PERF_THREAD_RAW = 21,
 };
 
 /*
@@ -87,6 +89,7 @@  enum perf_type {
 	PERF_TYPE_HARDWARE = 0,
 	PERF_TYPE_SOFTWARE = 1,
 	PERF_TYPE_HW_CACHE = 3,
+	PERF_TYPE_RAW = 4,
 };
 
 enum perf_count_hard {
@@ -564,6 +567,8 @@  static int add_context(char *session_name)
 		case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
 		case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER:
 		case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
+		case LTTNG_EVENT_CONTEXT_PERF_CPU_RAW:
+		case LTTNG_EVENT_CONTEXT_PERF_THREAD_RAW:
 			context.u.perf_counter.type = type->opt->u.perf.type;
 			context.u.perf_counter.config = type->opt->u.perf.config;
 			strncpy(context.u.perf_counter.name, type->opt->symbol,
@@ -688,9 +693,88 @@  end:
 }
 
 static
+int find_ctx_type_perf_raw(const char *ctx, struct ctx_type *type)
+{
+	char *next;
+	int ret;
+	int field_pos = 0;
+	char *tmp_list;
+
+	tmp_list = strdup(ctx);
+	if (!tmp_list) {
+		PERROR("strdup temp list");
+		ret = -ENOMEM;
+		goto error;
+	}
+
+	/* Looking for "perf:[cpu|thread]:raw:<mask>:<name>". */
+	for (;;) {
+		next = strtok(tmp_list, ":");
+		if (!next) {
+			break;
+		}
+		tmp_list = NULL;
+		switch (field_pos) {
+		case 0:
+			if (strncmp(next, "perf", 4) != 0) {
+				ret = -1;
+				goto end;
+			}
+			break;
+		case 1:
+			if (strncmp(next, "cpu", 3) == 0) {
+				type->opt->ctx_type = CONTEXT_PERF_CPU_RAW;
+			} else if (strncmp(next, "thread", 4) == 0) {
+				type->opt->ctx_type = CONTEXT_PERF_THREAD_RAW;
+			} else {
+				ret = -1;
+				goto end;
+			}
+			break;
+		case 2:
+			if (strncmp(next, "raw", 3) != 0) {
+				ret = -1;
+				goto end;
+			}
+			break;
+		case 3:
+			if (strlen(next) < 2 || next[0] != 'r') {
+				ERR("Wrong perf raw mask format: rNNN");
+				ret = -1;
+				goto end;
+			}
+			type->opt->u.perf.config = strtoll(next +  1, NULL, 16);
+			break;
+		case 4:
+			/* name */
+			break;
+		case 5:
+			ERR("Too many ':' in perf raw format");
+			ret = -1;
+			goto end;
+		};
+		field_pos++;
+	}
+
+	if (field_pos < 5) {
+		ERR("Wrong perf raw format");
+		ret = -1;
+		goto end;
+	}
+
+	ret = 0;
+	goto end;
+
+end:
+	free(tmp_list);
+error:
+	return ret;
+}
+
+static
 struct ctx_type *get_context_type(const char *ctx)
 {
-	int opt_index;
+	int opt_index, ret;
 	struct ctx_type *type = NULL;
 	const char app_ctx_prefix[] = "$app.";
 	char *provider_name = NULL, *ctx_name = NULL;
@@ -713,6 +797,14 @@  struct ctx_type *get_context_type(const char *ctx)
 		goto found;
 	}
 
+	/* Check if ctx is a raw perf context. */
+	ret = find_ctx_type_perf_raw(ctx, type);
+	if (ret == 0) {
+		type->opt->u.perf.type = PERF_TYPE_RAW;
+		type->opt->symbol = strdup(ctx);
+		goto found;
+	}
+
 	/*
 	 * No match found against static contexts; check if it is an app
 	 * context.