Skip to contentSkip to conversation

Introduce framework to forward multi context OA snapshots

RFC New [email protected] 1 participant 9 messages Aug 5, 2015

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover Introduce framework to forward multi context OA snapshots

From: Sourab Gupta <[email protected]>

This is the updated patch series(v3 - changes listed at end), which adds support for capturing OA counter snapshots for multiple contexts, by inserting MI_REPORT_PERF_COUNT commands into CS, and forwarding these snapshots to userspace using perf interface.

This work is based on Robert Bragg's perf event framework, the patches for which were floated earlier:

http://lists.freedesktop.org/archives/intel-gfx/2015-May/066102.html

Robert's perf event framework enabled capture of periodic OA counter snapshots by configuring the OA unit during perf event init. The raw OA reports generated by HW are then forwarded to userspace using perf apis.

But, there may be usecases wherein we need more than the periodic OA capture functionality which is supported by perf_event currently. Few such usecases are:
- Ability to capture system wide metrics. The reports captured should be able to be mapped back to individual contexts.
- Ability to inject tags for work, into the reports. This provides visibility into the multiple stages of work within single context.

This framework proposed here may also be seen as a way to overcome a limitation of Haswell, which doesn't write out a context ID with OA reports and handling this in the kernel makes sense when we plan for compatibility with Broadwell which doesn't include context id in reports.

This can be achieved by inserting the commands into the ring, before and after the batchbuffer, to dump the OA counter snapshots. The reports generated can have an additional footer appended for capturing the metadata information such as ctx id, pid, tags, etc. The specific issue of counter wraparound due to large batchbuffers can be subverted by using them in conjunction with periodic OA snapshots. Such per-BB data can give useful information to userspace tools to analyze performance and timing information at batchbuffer level.

An application intending to profile its own contexts can do so by submitting the MI_REPORT_PERF_COUNT commands into the CS from the userspace itself. But consider the usecase of a system wide GPU profiler tool which needs the data for all the workloads being scheduled on GPU globally. The relative complexity of doing this in kernel is significantly less than supporting such a usecase through userspace.

This framework is intended to feed into the requirement of such system wide GPU profilers, which may further utilize this data for usecases such as performance analysis (at a global level), identifying optimization scenarios for improving GPU utilization, CPU vs GPU timing analysis, etc. Again, this is made possible by presence of metadata information with individual reports, which is enabled by this framework.
One such system wide GPU profiler tool is MVP(Modular Video Profiler) tool, used by media team for profiling media workloads.

The current implementation approach is to forward these samples through the same PERF_SAMPLE_RAW sample type, as being done for periodic samples, with an additional footer appended for metadata information. The userspace can then distinguish these samples by filtering out on the basis of sample size. One of the other approaches being contemplated right now is creating separate sample types to handle these different kind of samples. There would be different fd's associated with these different sample types, though they can be a part of one event group. The userspace can listen to either or both these sample types while specifying event attributes during event init.
But right now, I'm seeing this work as a future refinement, based on acceptance of general framework as such. I'm looking, as of now, to get the feedback on these initial patches, w.r.t. the usage of perf apis and the interaction with i915.

Another feature introduced in these patches is execbuffer tagging. It is a mechanism, whereby the reports collected are marked with a tag passed by userspace during the execbuffer call. This way the userspace tool can associate the reports collected with the corresponding execbuffers. This satifies the requirement to have visibility into multiple stages (i.e. execbuffers) lying within a single context.
For e.g. for the media pipeline, CodecHAL encoding stage has a single context, and involves multiple stages such as Scaling, ME, MBEnc, PAK for which there are separate execbuffer calls. There is a need to have the granularity of these multiple stages of a context for the reports generated. The presence of a tag in report metadata fulfills this requirement.

One of the pre-requisite for this work is presence of globally unique id associated with each context. The present context id is specific to drm fd. As such, it can't uniquely be used to associate the reports generated with the corresponding context scheduled from userspace in a global way.
In absence of globally unique context id, other metadata such as pid/tags in conjunction with ctx id may be used to associate reports with the corresponding contexts.

The first patch in the series introduces a global context id, and the subsequent patches introduce the multi-context OA capture mode and the mechanism to forward these snapshots using perf.

This patch set currently supports Haswell. Gen8+ support can be added when the basic framework is agreed upon.

v2: This patch series has the following changes wrt the one floated earlier:
- Removing synchronous waits during event stop/destroy
- segregating the book-keeping data for the samples from destination buffer and collecting it into a separate list
- managing the lifetime of destination buffer with the help of gem active reference tracking
- having the scope of i915 device mutex limited to places of gem interaction and having the pmu data structures protected with a per pmu lock
- userspace can now control the metadata it wants by requesting the same during event init. The sample is sent with the requested metadata in a packed format.
- Some patches merged together and a few more introduced

v3: Changes made:
- Global id for ctx allocated from a separate cyclic idr, and not trying to overload existing drm fd specific ctx id for this purpose.
- Meeting semantics for flush (ensuring to flush samples before returning)
- spin_locks used in place of spin_lock_irqsave
- execbuffer tag now uses upper 32 bits of rsvd1 field.
- Some code restructuring/optimization, better nomenclature and error handling.

Sourab Gupta (8):
drm/i915: Introduce global id for contexts
drm/i915: Introduce mode for capture of multi ctx OA reports
synchronized with RCS
drm/i915: Add mechanism for forwarding CS based OA counter snapshots
through perf
drm/i915: Forward periodic and CS based OA reports sorted acc to
timestamps
drm/i915: Handle event stop and destroy for commands in flight drm/i915: Insert commands for capture of OA counters in the ring drm/i915: Add support for having pid output with OA report drm/i915: Add support to add execbuffer tags to OA counter reports

 drivers/gpu/drm/i915/i915_drv.h            |  53 ++-
 drivers/gpu/drm/i915/i915_gem_context.c    |  19 +
 drivers/gpu/drm/i915/i915_gem_execbuffer.c |   6 +
 drivers/gpu/drm/i915/i915_oa_perf.c        | 604 +++++++++++++++++++++++++----
 include/uapi/drm/i915_drm.h                |  25 +-
 5 files changed, 635 insertions(+), 72 deletions(-)
Signature · 6 lines
1.8.5.1

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#1 lore raw

RFC 1/8 drm/i915: Introduce global id for contexts

From: Sourab Gupta <[email protected]>

The current context user handles are specific to drm file instance. There are some usecases, which may require a global id for the contexts. For e.g. a system level GPU profiler tool may lean upon the global context ids to associate the performance snapshots with individual contexts.

This global id may also be used further in order to provide a unique context id to hw.

In this patch, the global ids are allocated from a separate cyclic idr and can be further utilized for any usecase described above.

v2: According to Chris' suggestion, implemented a separate idr for holding global ids for contexts, as opposed to overloading the file specific ctx->user_handle for this purpose. This global id can also further be used wherever hw has to be programmed with ctx unique id, though this patch just introduces the hw global id as such.

---
 drivers/gpu/drm/i915/i915_drv.h         |  3 +++
 drivers/gpu/drm/i915/i915_gem_context.c | 19 +++++++++++++++++++
 2 files changed, 22 insertions(+)
drivers/gpu/drm/i915/i915_drv.h +3 -0
index 50977f0..3436f3b 100644
@@ -861,6 +861,7 @@ struct i915_ctx_hang_stats {
 struct intel_context {
 	struct kref ref;
 	int user_handle;
+	int global_id;
 	uint8_t remap_slice;
 	int flags;
 	struct drm_i915_file_private *file_priv;
@@ -1756,6 +1757,8 @@ struct drm_i915_private {
 
 	bool preserve_bios_swizzle;
 
+	struct idr global_ctx_idr;
+
 	/* overlay */
 	struct intel_overlay *overlay;
 
drivers/gpu/drm/i915/i915_gem_context.c +19 -0
index d9ccad5..51ec420 100644
@@ -246,6 +246,18 @@ __create_hw_context(struct drm_device *dev,
 
 	ctx->file_priv = file_priv;
 	ctx->user_handle = ret;
+
+	/* TODO: If required, this global id can be used for programming the hw
+	 * fields too. In that case, we'll have take care of hw restrictions
+	 * while allocating idr. e.g. for some hw, we may not have full 32 bits
+	 * available.
+	 */
+	ret = idr_alloc_cyclic(&dev_priv->global_ctx_idr,
+				ctx, 0, 0, GFP_KERNEL);
+	if (ret < 0)
+		goto err_out;
+
+	ctx->global_id = ret;
 	/* NB: Mark all slices as needing a remap so that when the context first
 	 * loads it will restore whatever remap state already exists. If there
 	 * is no remap info, it will be a NOP. */
@@ -270,6 +282,7 @@ i915_gem_create_context(struct drm_device *dev,
 			struct drm_i915_file_private *file_priv)
 {
 	const bool is_global_default_ctx = file_priv == NULL;
+	struct drm_i915_private *dev_priv = dev->dev_private;
 	struct intel_context *ctx;
 	int ret = 0;
 
@@ -315,6 +328,7 @@ err_unpin:
 	if (is_global_default_ctx && ctx->legacy_hw_ctx.rcs_state)
 		i915_gem_context_unpin_state(dev, ctx);
 err_destroy:
+	idr_remove(&dev_priv->global_ctx_idr, ctx->global_id);
 	i915_gem_context_unreference(ctx);
 	return ERR_PTR(ret);
 }
@@ -371,6 +385,7 @@ int i915_gem_context_init(struct drm_device *dev)
 			dev_priv->hw_context_size = 0;
 		}
 	}
+	idr_init(&dev_priv->global_ctx_idr);
 
 	ctx = i915_gem_create_context(dev, NULL);
 	if (IS_ERR(ctx)) {
@@ -398,6 +413,8 @@ void i915_gem_context_fini(struct drm_device *dev)
 	struct intel_context *dctx = dev_priv->ring[RCS].default_context;
 	int i;
 
+	idr_destroy(&dev_priv->global_ctx_idr);
+
 	if (dctx->legacy_hw_ctx.rcs_state) {
 		/* The only known way to stop the gpu from accessing the hw context is
 		 * to reset it. Do this as the very last operation to avoid confusing
@@ -878,6 +895,7 @@ int i915_gem_context_destroy_ioctl(struct drm_device *dev, void *data,
 {
 	struct drm_i915_gem_context_destroy *args = data;
 	struct drm_i915_file_private *file_priv = file->driver_priv;
+	struct drm_i915_private *dev_priv = dev->dev_private;
 	struct intel_context *ctx;
 	int ret;
 
@@ -895,6 +913,7 @@ int i915_gem_context_destroy_ioctl(struct drm_device *dev, void *data,
 	}
 
 	idr_remove(&ctx->file_priv->context_idr, ctx->user_handle);
+	idr_remove(&dev_priv->global_ctx_idr, ctx->global_id);
 	i915_gem_context_unreference(ctx);
 	mutex_unlock(&dev->struct_mutex);
 
Signature · 6 lines
1.8.5.1

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#2 lore raw

RFC 2/8 drm/i915: Introduce mode for capture of multi ctx OA reports synchronized with RCS

In reply to [email protected]

From: Sourab Gupta <[email protected]>

This patch introduces a mode of capturing OA counter reports belonging to multiple contexts, which can be mapped back to individual contexts. The OA reports captured in this way are synchronized with Render command stream.

There may be usecases wherein we need more than periodic OA capture mode which is supported by perf_event currently. We may need to insert RCS synchronized commands to capture the OA counter snapshots. This mode is primarily used for two usecases:
- Ability to capture system wide metrics, alongwith the ability to map the reports back to individual contexts.
- Ability to inject tags for work, into the reports. This provides visibility into the multiple stages of work within single context.

The OA reports generated in this way will be forwarded to userspace after appending a footer, which will have this metadata information. This will enable the usecases mentioned above.

This patch introduces an additional field in the oa attr structure for supporting this capture mode. The data thus captured needs to be stored in a separate buffer, which will be different from the buffer used otherwise for periodic OA capture mode. Again this buffer address will not need to be mapped to OA unit register addresses such as OASTATUS1, OASTATUS2 and OABUFFER.

The subsequent patches introduce the mechanism for forwarding reports to userspace, handling the command synchronization and mechanism for inserting corresponding commands into the ringbuffer.

v2: Following changes:
- Save the gtt offset while pinning the buffer, to be retrieved when required.
- Use spin_lock instead of spin_lock_irqsave

---
 drivers/gpu/drm/i915/i915_drv.h     |  10 +++
 drivers/gpu/drm/i915/i915_oa_perf.c | 170 +++++++++++++++++++++++++++---------
 include/uapi/drm/i915_drm.h         |   3 +-
 3 files changed, 141 insertions(+), 42 deletions(-)
drivers/gpu/drm/i915/i915_drv.h +10 -0
index 3436f3b..050bdda 100644
@@ -1931,6 +1931,7 @@ struct drm_i915_private {
 		bool event_active;
 
 		bool periodic;
+		bool multiple_ctx_mode;
 		u32 period_exponent;
 
 		u32 metrics_set;
@@ -1945,6 +1946,15 @@ struct drm_i915_private {
 			int format_size;
 			spinlock_t flush_lock;
 		} oa_buffer;
+
+		/* Fields for multiple context capture mode */
+		struct {
+			struct drm_i915_gem_object *obj;
+			u32 gtt_offset;
+			u8 *addr;
+			int format;
+			int format_size;
+		} oa_rcs_buffer;
 	} oa_pmu;
 #endif
 
drivers/gpu/drm/i915/i915_oa_perf.c +129 -41

245 lines not on this page · raw message

include/uapi/drm/i915_drm.h +2 -1
index 9d866e8..dcf7c87 100644
@@ -92,7 +92,8 @@ typedef struct _drm_i915_oa_attr {
 	__u32 ctx_id;
 
 	__u64 single_context : 1,
-		__reserved_1 : 63;
+		multiple_context_mode:1,
+		__reserved_1:62;
 } drm_i915_oa_attr_t;
 
 /* Header for PERF_RECORD_DEVICE type events */
Signature · 6 lines
1.8.5.1

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#3 lore raw

RFC 3/8 drm/i915: Add mechanism for forwarding CS based OA counter snapshots through perf

In reply to [email protected]

From: Sourab Gupta <[email protected]>

This patch adds the mechanism for forwarding the CS based OA snapshots through the perf event interface.

The OA snapshots will be captured in a gem buffer object. The metadata information (ctx global id, as of now) pertaining to snapshot is maintained in a list, which has offsets into the gem buffer object for each snapshot captured.

Each snapshot collected is forwarded as a separate perf sample. The perf sample will have raw OA report followed by metadata information pertaining to that sample. The size of the OA report is the one specified during event init.

In order to track whether the gpu has completed processing the node, a field pertaining to corresponding gem request is added. The request is expected to be referenced whenever the gpu command is submitted.

While forwarding the samples, we check whether the gem request is completed and dereference the corresponding request. The need to dereference the request necessitates a worker here, which will be scheduled when the hrtimer triggers.

While flushing the samples, we have to wait for the requests already scheduled, before forwarding the samples. This wait is in a lockless fashion.

v2: Changes here pertaining to (as suggested by Chris):
- Forwarding functionality implemented in a separate fn. The work item (scheduled from hrtimer/event stop) would be calling that function. The event flush would directly call this forwarding fn. This meets the flush semantics.
- use spin_lock instead of spin_lock_irqsave
- Code restructuring & better nomenclature

---
 drivers/gpu/drm/i915/i915_drv.h     |  11 +++
 drivers/gpu/drm/i915/i915_oa_perf.c | 145 +++++++++++++++++++++++++++++++++++-
 include/uapi/drm/i915_drm.h         |   5 ++
 3 files changed, 160 insertions(+), 1 deletion(-)
drivers/gpu/drm/i915/i915_drv.h +11 -0
index 050bdda..87e7cf0 100644
@@ -1654,6 +1654,13 @@ struct i915_oa_reg {
 	u32 value;
 };
 
+struct i915_oa_rcs_node {
+	struct list_head head;
+	struct drm_i915_gem_request *req;
+	u32 offset;
+	u32 ctx_id;
+};
+
 extern const struct i915_oa_reg i915_oa_3d_mux_config_hsw[];
 extern const int i915_oa_3d_mux_config_hsw_len;
 extern const struct i915_oa_reg i915_oa_3d_b_counter_config_hsw[];
@@ -1954,7 +1961,11 @@ struct drm_i915_private {
 			u8 *addr;
 			int format;
 			int format_size;
+			u32 node_size;
+			u32 node_count;
 		} oa_rcs_buffer;
+		struct list_head node_list;
+		struct work_struct forward_work;
 	} oa_pmu;
 #endif
 
drivers/gpu/drm/i915/i915_oa_perf.c +144 -1

188 lines not on this page · raw message

include/uapi/drm/i915_drm.h +5 -0
index dcf7c87..e97b2fd 100644
@@ -123,6 +123,11 @@ enum drm_i915_oa_event_type {
 	I915_OA_RECORD_MAX,			/* non-ABI */
 };
 
+struct drm_i915_oa_node_ctx_id {
+	__u32 ctx_id;
+	__u32 pad;
+};
+
 /* Each region is a minimum of 16k, and there are at most 255 of them.
  */
 #define I915_NR_TEX_REGIONS 255	/* table size 2k - maximum due to use
Signature · 6 lines
1.8.5.1

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#4 lore raw

RFC 4/8 drm/i915: Forward periodic and CS based OA reports sorted acc to timestamps

In reply to [email protected]

From: Sourab Gupta <[email protected]>

The periodic reports and the RCS based reports are collected in two separate buffers. While forwarding to userspace, these have to be sent to single perf event ringbuffer. From a userspace perspective, it is good to have the reports in the single buffer in order to their timestamps. This patch addresses this problem by forwarding the periodic OA reports with a lower timestamp, whenever we are forwarding the Command streamer based report.

---
 drivers/gpu/drm/i915/i915_oa_perf.c | 40 ++++++++++++++++++++++---------------
 1 file changed, 24 insertions(+), 16 deletions(-)
drivers/gpu/drm/i915/i915_oa_perf.c +24 -16
index 3948b45..ab58c46 100644
@@ -48,8 +48,7 @@ static void forward_one_oa_snapshot_to_event(struct drm_i915_private *dev_priv,
 }
 
 static u32 forward_oa_snapshots(struct drm_i915_private *dev_priv,
-				u32 head,
-				u32 tail)
+				u32 head, u32 tail, u64 gpu_ts)
 {
 	struct perf_event *exclusive_event = dev_priv->oa_pmu.exclusive_event;
 	int snapshot_size = dev_priv->oa_pmu.oa_buffer.format_size;
@@ -58,14 +57,6 @@ static u32 forward_oa_snapshots(struct drm_i915_private *dev_priv,
 	u8 *snapshot;
 	u32 taken;
 
-	/*
-	 * Schedule a worker to forward the RCS based OA reports collected.
-	 * A worker is needed since it requires device mutex to be taken
-	 * which can't be done here because of atomic context
-	 */
-	if (dev_priv->oa_pmu.multiple_ctx_mode)
-		schedule_work(&dev_priv->oa_pmu.forward_work);
-
 	head -= dev_priv->oa_pmu.oa_buffer.gtt_offset;
 	tail -= dev_priv->oa_pmu.oa_buffer.gtt_offset;
 
@@ -75,12 +66,19 @@ static u32 forward_oa_snapshots(struct drm_i915_private *dev_priv,
 	 */
 
 	while ((taken = OA_TAKEN(tail, head))) {
+		u64 snapshot_ts;
+
 		/* The tail increases in 64 byte increments, not in
 		 * format_size steps. */
 		if (taken < snapshot_size)
 			break;
 
 		snapshot = oa_buf_base + (head & mask);
+
+		snapshot_ts = *(u64 *)(snapshot + 4);
+		if (snapshot_ts > gpu_ts)
+			break;
+
 		head += snapshot_size;
 
 		/* We currently only allow exclusive access to the counters
@@ -122,7 +120,7 @@ static void log_oa_status(struct drm_i915_private *dev_priv,
 }
 
 static void flush_oa_snapshots(struct drm_i915_private *dev_priv,
-			       bool skip_if_flushing)
+			       bool skip_if_flushing, u64 gpu_ts)
 {
 	unsigned long flags;
 	u32 oastatus2;
@@ -165,7 +163,7 @@ static void flush_oa_snapshots(struct drm_i915_private *dev_priv,
 			     GEN7_OASTATUS1_REPORT_LOST));
 	}
 
-	head = forward_oa_snapshots(dev_priv, head, tail);
+	head = forward_oa_snapshots(dev_priv, head, tail, gpu_ts);
 
 	I915_WRITE(GEN7_OASTATUS2, (head & GEN7_OASTATUS2_HEAD_MASK) |
 				    GEN7_OASTATUS2_GGTT);
@@ -213,6 +211,7 @@ static void forward_one_oa_rcs_sample(struct drm_i915_private *dev_priv,
 	u8 *snapshot;
 	struct drm_i915_oa_node_ctx_id *ctx_info;
 	struct perf_raw_record raw;
+	u64 snapshot_ts;
 
 	format_size = dev_priv->oa_pmu.oa_rcs_buffer.format_size;
 	snapshot_size = format_size + sizeof(*ctx_info);
@@ -221,6 +220,10 @@ static void forward_one_oa_rcs_sample(struct drm_i915_private *dev_priv,
 	ctx_info = (struct drm_i915_oa_node_ctx_id *)(snapshot + format_size);
 	ctx_info->ctx_id = node->ctx_id;
 
+	/* Flush the periodic snapshots till the ts of this OA report */
+	snapshot_ts = *(u64 *)(snapshot + 4);
+	flush_oa_snapshots(dev_priv, true, snapshot_ts);
+
 	perf_sample_data_init(&data, 0, event->hw.last_period);
 
 	/* Note: the raw sample consists of a u32 size member and raw data. The
@@ -516,7 +519,10 @@ static enum hrtimer_restart hrtimer_sample(struct hrtimer *hrtimer)
 	struct drm_i915_private *i915 =
 		container_of(hrtimer, typeof(*i915), oa_pmu.timer);
 
-	flush_oa_snapshots(i915, true);
+	if (i915->oa_pmu.multiple_ctx_mode)
+		schedule_work(&i915->oa_pmu.forward_work);
+	else
+		flush_oa_snapshots(i915, true, U64_MAX);
 
 	hrtimer_forward_now(hrtimer, ns_to_ktime(PERIOD));
 	return HRTIMER_RESTART;
@@ -944,7 +950,9 @@ static void i915_oa_event_stop(struct perf_event *event, int flags)
 
 	if (event->attr.sample_period) {
 		hrtimer_cancel(&dev_priv->oa_pmu.timer);
-		flush_oa_snapshots(dev_priv, false);
+		if (dev_priv->oa_pmu.multiple_ctx_mode)
+			schedule_work(&dev_priv->oa_pmu.forward_work);
+		flush_oa_snapshots(dev_priv, false, U64_MAX);
 	}
 
 	event->hw.state = PERF_HES_STOPPED;
@@ -984,8 +992,8 @@ static int i915_oa_event_flush(struct perf_event *event)
 			if (ret)
 				return ret;
 			forward_oa_rcs_snapshots(i915);
-		}
-		flush_oa_snapshots(i915, true);
+		} else
+			flush_oa_snapshots(i915, true, U64_MAX);
 	}
 
 	return 0;
Signature · 6 lines
1.8.5.1

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#5 lore raw

RFC 5/8 drm/i915: Handle event stop and destroy for commands in flight

In reply to [email protected]

From: Sourab Gupta <[email protected]>

In the periodic OA sampling mode, the event stop would stop forwarding samples to userspace, and disables OA synchronously. The buffer is destroyed eventually in event destroy callback. But when we have in flight RPC commands scheduled on GPU (like in this case), the handling of OA disabling and buffer destruction has to take those into account. This patch handles the event stop & destroy conditions after accounting for GPU commands in flight using the OA unit.

Now, the event stop would just set the event state, and stop forwarding data to userspace. From userspace perspective, for all purposes, the event sampling is stopped. This is true for periodic OA samples also. The OA unit is not disabled here, since there may be RPC commands scheduled on GPU. A subsequent event start (without event destroy) would start forwarding samples again.

The event destroy releases the local copy of the RCS buffer. But since, it is expected that the active reference of buffer is taken while inserting commands, we can rest assured that buffer is freed up only after GPU is done with it.

Still there is a need to schedule a worker from event destroy, because we need to do some further stuff listed below (in this order)
- free up request references
- disable OA unit
- dereference periodic OA buffer (as this is not managed by active ref)
- runtime_pm_put + forcewake_put

The ideal solution here would be to have a callback when the last request is finished on GPU, so that we can do this stuff there (WIP:Chris' retire-notification mechanism). Till the time, an async worker thread will do.

A subsequent event init would have to wait for previously submitted RPC commands to complete or return -EBUSY. Currently, for the sake of simplicity, we are returning -EBUSY.

---
 drivers/gpu/drm/i915/i915_drv.h     |  11 ++-
 drivers/gpu/drm/i915/i915_oa_perf.c | 162 +++++++++++++++++++++++++++++-------
 2 files changed, 143 insertions(+), 30 deletions(-)
drivers/gpu/drm/i915/i915_drv.h +10 -1
index 87e7cf0..d355691 100644
@@ -1654,10 +1654,18 @@ struct i915_oa_reg {
 	u32 value;
 };
 
+enum i915_oa_event_state {
+	I915_OA_EVENT_INIT,
+	I915_OA_EVENT_STARTED,
+	I915_OA_EVENT_STOP_IN_PROGRESS,
+	I915_OA_EVENT_STOPPED,
+};
+
 struct i915_oa_rcs_node {
 	struct list_head head;
 	struct drm_i915_gem_request *req;
 	u32 offset;
+	bool discard;
 	u32 ctx_id;
 };
 
@@ -1935,7 +1943,7 @@ struct drm_i915_private {
 
 		struct perf_event *exclusive_event;
 		struct intel_context *specific_ctx;
-		bool event_active;
+		enum i915_oa_event_state event_state;
 
 		bool periodic;
 		bool multiple_ctx_mode;
@@ -1966,6 +1974,7 @@ struct drm_i915_private {
 		} oa_rcs_buffer;
 		struct list_head node_list;
 		struct work_struct forward_work;
+		struct work_struct event_destroy_work;
 	} oa_pmu;
 #endif
 
drivers/gpu/drm/i915/i915_oa_perf.c +133 -29

253 lines not on this page · raw message

Signature · 6 lines
1.8.5.1

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#6 lore raw

RFC 6/8 drm/i915: Insert commands for capture of OA counters in the ring

In reply to [email protected]

From: Sourab Gupta <[email protected]>

This patch adds the routines which insert commands for capturing OA snapshots into the ringbuffer of RCS engine.

The command MI_REPORT_PERF_COUNT can be used to capture snapshots of OA counters, which is inserted at BB boundaries.
While inserting the commands, we keep a reference of associated request. This will be released when we are forwarding the samples to userspace (or when the event is being destroyed).
Also, an active reference of the destination buffer is taken here, so that we can be assured that the buffer is freed up only after GPU is done with it, even if the local reference of the buffer is released.

v2: Changes (as suggested by Chris):
- Passing in 'request' struct for emit report function
- Removed multiple calls to i915_gem_obj_to_ggtt(). Keeping hold of pinned vma from start and using when required.
- Better nomenclature, and error handling.

---
 drivers/gpu/drm/i915/i915_drv.h            | 13 +++++
 drivers/gpu/drm/i915/i915_gem_execbuffer.c |  4 ++
 drivers/gpu/drm/i915/i915_oa_perf.c        | 87 ++++++++++++++++++++++++++++++
 3 files changed, 104 insertions(+)
drivers/gpu/drm/i915/i915_drv.h +13 -0
index d355691..5c15e30 100644
@@ -1661,6 +1661,11 @@ enum i915_oa_event_state {
 	I915_OA_EVENT_STOPPED,
 };
 
+enum i915_profile_mode {
+	I915_PROFILE_OA = 0,
+	I915_PROFILE_MAX,
+};
+
 struct i915_oa_rcs_node {
 	struct list_head head;
 	struct drm_i915_gem_request *req;
@@ -1966,6 +1971,7 @@ struct drm_i915_private {
 		struct {
 			struct drm_i915_gem_object *obj;
 			u32 gtt_offset;
+			struct i915_vma *vma;
 			u8 *addr;
 			int format;
 			int format_size;
@@ -1976,6 +1982,9 @@ struct drm_i915_private {
 		struct work_struct forward_work;
 		struct work_struct event_destroy_work;
 	} oa_pmu;
+
+	void (*emit_profiling_data[I915_PROFILE_MAX])
+		(struct drm_i915_gem_request *req, u32 global_ctx_id);
 #endif
 
 	/* Abstract the submission mechanism (legacy ringbuffer or execlists) away */
@@ -3156,6 +3165,8 @@ void i915_oa_context_pin_notify(struct drm_i915_private *dev_priv,
 				struct intel_context *context);
 void i915_oa_context_unpin_notify(struct drm_i915_private *dev_priv,
 				  struct intel_context *context);
+void i915_emit_profiling_data(struct drm_i915_gem_request *req,
+				u32 global_ctx_id);
 #else
 static inline void
 i915_oa_context_pin_notify(struct drm_i915_private *dev_priv,
@@ -3163,6 +3174,8 @@ i915_oa_context_pin_notify(struct drm_i915_private *dev_priv,
 static inline void
 i915_oa_context_unpin_notify(struct drm_i915_private *dev_priv,
 			     struct intel_context *context) {}
+void i915_emit_profiling_data(struct drm_i915_gem_request *req,
+				u32 global_ctx_id) {};
 #endif
 
 /* i915_gem_evict.c */
drivers/gpu/drm/i915/i915_gem_execbuffer.c +4 -0
index 3336e1c..e58b10d 100644
@@ -1317,6 +1317,8 @@ i915_gem_ringbuffer_submission(struct drm_device *dev, struct drm_file *file,
 			goto error;
 	}
 
+	i915_emit_profiling_data(intel_ring_get_request(ring), ctx->global_id);
+
 	exec_len = args->batch_len;
 	if (cliprects) {
 		for (i = 0; i < args->num_cliprects; i++) {
@@ -1339,6 +1341,8 @@ i915_gem_ringbuffer_submission(struct drm_device *dev, struct drm_file *file,
 			return ret;
 	}
 
+	i915_emit_profiling_data(intel_ring_get_request(ring), ctx->global_id);
+
 	trace_i915_gem_ring_dispatch(intel_ring_get_request(ring), dispatch_flags);
 
 	i915_gem_execbuffer_move_to_active(vmas, ring);
drivers/gpu/drm/i915/i915_oa_perf.c +87 -0

117 lines not on this page · raw message

Signature · 6 lines
1.8.5.1

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#7 lore raw

RFC 7/8 drm/i915: Add support for having pid output with OA report

In reply to [email protected]

From: Sourab Gupta <[email protected]>

This patch introduces flags and adds support for having pid output with the OA reports generated through the RCS commands.

When the userspace expresses its interest in listening to the pid through an oa_attr field during event init, the OA reports generated would have an additional field appended with the pid information. The patches enables this framework, which can be expanded upon to introduce further fields in the OA report metadata.

---
 drivers/gpu/drm/i915/i915_drv.h     |  3 +++
 drivers/gpu/drm/i915/i915_oa_perf.c | 19 ++++++++++++++++++-
 include/uapi/drm/i915_drm.h         |  8 +++++++-
 3 files changed, 28 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/i915_drv.h +3 -0
index 5c15e30..6e6f1be 100644
@@ -1672,6 +1672,7 @@ struct i915_oa_rcs_node {
 	u32 offset;
 	bool discard;
 	u32 ctx_id;
+	u32 pid;
 };
 
 extern const struct i915_oa_reg i915_oa_3d_mux_config_hsw[];
@@ -1981,6 +1982,8 @@ struct drm_i915_private {
 		struct list_head node_list;
 		struct work_struct forward_work;
 		struct work_struct event_destroy_work;
+#define I915_OA_SAMPLE_PID		(1<<0)
+		int sample_info_flags;
 	} oa_pmu;
 
 	void (*emit_profiling_data[I915_PROFILE_MAX])
drivers/gpu/drm/i915/i915_oa_perf.c +18 -1
index e3bc8e0..f4b8a2b 100644
@@ -69,6 +69,8 @@ static void i915_oa_emit_perf_report(struct drm_i915_gem_request *req,
 	}
 
 	entry->ctx_id = global_ctx_id;
+	if (dev_priv->oa_pmu.sample_info_flags & I915_OA_SAMPLE_PID)
+		entry->pid = current->pid;
 	i915_gem_request_assign(&entry->req, ring->outstanding_lazy_request);
 
 	spin_lock_irqsave(&dev_priv->oa_pmu.lock, lock_flags);
@@ -303,8 +305,9 @@ static void forward_one_oa_rcs_sample(struct drm_i915_private *dev_priv,
 	struct perf_sample_data data;
 	struct perf_event *event = dev_priv->oa_pmu.exclusive_event;
 	int format_size, snapshot_size;
-	u8 *snapshot;
+	u8 *snapshot, *current_ptr;
 	struct drm_i915_oa_node_ctx_id *ctx_info;
+	struct drm_i915_oa_node_pid *pid_info;
 	struct perf_raw_record raw;
 	u64 snapshot_ts;
 
@@ -314,6 +317,14 @@ static void forward_one_oa_rcs_sample(struct drm_i915_private *dev_priv,
 
 	ctx_info = (struct drm_i915_oa_node_ctx_id *)(snapshot + format_size);
 	ctx_info->ctx_id = node->ctx_id;
+	current_ptr = snapshot + snapshot_size;
+
+	if (dev_priv->oa_pmu.sample_info_flags & I915_OA_SAMPLE_PID) {
+		pid_info = (struct drm_i915_oa_node_pid *)current_ptr;
+		pid_info->pid = node->pid;
+		snapshot_size += sizeof(*pid_info);
+		current_ptr = snapshot + snapshot_size;
+	}
 
 	/* Flush the periodic snapshots till the ts of this OA report */
 	snapshot_ts = *(u64 *)(snapshot + 4);
@@ -672,6 +683,9 @@ static int init_oa_rcs_buffer(struct perf_event *event)
 	node_size = dev_priv->oa_pmu.oa_rcs_buffer.format_size +
 			sizeof(struct drm_i915_oa_node_ctx_id);
 
+	if (dev_priv->oa_pmu.sample_info_flags & I915_OA_SAMPLE_PID)
+		node_size += sizeof(struct drm_i915_oa_node_pid);
+
 	/* node size has to be aligned to 64 bytes, since only 64 byte aligned
 	 * addresses can be given to OA unit for dumping OA reports */
 	node_size = ALIGN(node_size, 64);
@@ -824,6 +838,9 @@ static int i915_oa_event_init(struct perf_event *event)
 		if (!capable(CAP_SYS_ADMIN))
 			return -EACCES;
 		dev_priv->oa_pmu.multiple_ctx_mode = true;
+		if (oa_attr.sample_pid)
+			dev_priv->oa_pmu.sample_info_flags |=
+					I915_OA_SAMPLE_PID;
 	}
 
 	report_format = oa_attr.format;
include/uapi/drm/i915_drm.h +7 -1
index e97b2fd..65e8297 100644
@@ -93,7 +93,8 @@ typedef struct _drm_i915_oa_attr {
 
 	__u64 single_context : 1,
 		multiple_context_mode:1,
-		__reserved_1:62;
+		sample_pid:1,
+		__reserved_1:61;
 } drm_i915_oa_attr_t;
 
 /* Header for PERF_RECORD_DEVICE type events */
@@ -128,6 +129,11 @@ struct drm_i915_oa_node_ctx_id {
 	__u32 pad;
 };
 
+struct drm_i915_oa_node_pid {
+	__u32 pid;
+	__u32 pad;
+};
+
 /* Each region is a minimum of 16k, and there are at most 255 of them.
  */
 #define I915_NR_TEX_REGIONS 255	/* table size 2k - maximum due to use
Signature · 6 lines
1.8.5.1

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#8 lore raw

RFC 8/8 drm/i915: Add support to add execbuffer tags to OA counter reports

In reply to [email protected]

From: Sourab Gupta <[email protected]>

This patch enables userspace to specify tags (per workload), provided via execbuffer ioctl, which could be added to OA reports, to help associate reports with the corresponding workloads.

There may be multiple stages within a single context, from a userspace perspective. An ability is needed to individually associate the OA reports with their corresponding workloads(execbuffers), which may not be possible solely with ctx_id or pid information. This patch enables such a mechanism.

In this patch, rsvd2 field of execbuffer arguments is being utilized for passing the tag. A new bitfield in execbuffer flags is introduced in order to inform kernel of the tag being passed in execbuffer arguments.

v2: Changes (as suggested by Chris):
- Using upper 32 bits of rsvd1 field for passing tag (as it is a natural complement to ctx_id which is there in lower 32 bits of rsvd1
- Removed the flags field to inform kernel of tag being passed.
- Better ABI definition.

---
 drivers/gpu/drm/i915/i915_drv.h            |  8 +++++---
 drivers/gpu/drm/i915/i915_gem_execbuffer.c |  6 ++++--
 drivers/gpu/drm/i915/i915_oa_perf.c        | 23 ++++++++++++++++++++---
 include/uapi/drm/i915_drm.h                | 13 ++++++++++++-
 4 files changed, 41 insertions(+), 9 deletions(-)
drivers/gpu/drm/i915/i915_drv.h +5 -3
index 6e6f1be..d5d9156 100644
@@ -1673,6 +1673,7 @@ struct i915_oa_rcs_node {
 	bool discard;
 	u32 ctx_id;
 	u32 pid;
+	u32 tag;
 };
 
 extern const struct i915_oa_reg i915_oa_3d_mux_config_hsw[];
@@ -1983,11 +1984,12 @@ struct drm_i915_private {
 		struct work_struct forward_work;
 		struct work_struct event_destroy_work;
 #define I915_OA_SAMPLE_PID		(1<<0)
+#define I915_OA_SAMPLE_TAG		(1<<1)
 		int sample_info_flags;
 	} oa_pmu;
 
 	void (*emit_profiling_data[I915_PROFILE_MAX])
-		(struct drm_i915_gem_request *req, u32 global_ctx_id);
+		(struct drm_i915_gem_request *req, u32 global_ctx_id, u32 tag);
 #endif
 
 	/* Abstract the submission mechanism (legacy ringbuffer or execlists) away */
@@ -3169,7 +3171,7 @@ void i915_oa_context_pin_notify(struct drm_i915_private *dev_priv,
 void i915_oa_context_unpin_notify(struct drm_i915_private *dev_priv,
 				  struct intel_context *context);
 void i915_emit_profiling_data(struct drm_i915_gem_request *req,
-				u32 global_ctx_id);
+				u32 global_ctx_id, u32 tag);
 #else
 static inline void
 i915_oa_context_pin_notify(struct drm_i915_private *dev_priv,
@@ -3178,7 +3180,7 @@ static inline void
 i915_oa_context_unpin_notify(struct drm_i915_private *dev_priv,
 			     struct intel_context *context) {}
 void i915_emit_profiling_data(struct drm_i915_gem_request *req,
-				u32 global_ctx_id) {};
+				u32 global_ctx_id, u32 tag) {};
 #endif
 
 /* i915_gem_evict.c */
drivers/gpu/drm/i915/i915_gem_execbuffer.c +4 -2
index e58b10d..7f2246f 100644
@@ -1317,7 +1317,8 @@ i915_gem_ringbuffer_submission(struct drm_device *dev, struct drm_file *file,
 			goto error;
 	}
 
-	i915_emit_profiling_data(intel_ring_get_request(ring), ctx->global_id);
+	i915_emit_profiling_data(intel_ring_get_request(ring), ctx->global_id,
+				i915_execbuffer2_get_tag(*args));
 
 	exec_len = args->batch_len;
 	if (cliprects) {
@@ -1341,7 +1342,8 @@ i915_gem_ringbuffer_submission(struct drm_device *dev, struct drm_file *file,
 			return ret;
 	}
 
-	i915_emit_profiling_data(intel_ring_get_request(ring), ctx->global_id);
+	i915_emit_profiling_data(intel_ring_get_request(ring), ctx->global_id,
+				i915_execbuffer2_get_tag(*args));
 
 	trace_i915_gem_ring_dispatch(intel_ring_get_request(ring), dispatch_flags);
 
drivers/gpu/drm/i915/i915_oa_perf.c +20 -3
index f4b8a2b..48591fc 100644
@@ -26,7 +26,7 @@ static int hsw_perf_format_sizes[] = {
 };
 
 void i915_emit_profiling_data(struct drm_i915_gem_request *req,
-				u32 global_ctx_id)
+				u32 global_ctx_id, u32 tag)
 {
 	struct intel_engine_cs *ring = req->ring;
 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
@@ -34,7 +34,8 @@ void i915_emit_profiling_data(struct drm_i915_gem_request *req,
 
 	for (i = I915_PROFILE_OA; i < I915_PROFILE_MAX; i++) {
 		if (dev_priv->emit_profiling_data[i])
-			dev_priv->emit_profiling_data[i](req, global_ctx_id);
+			dev_priv->emit_profiling_data[i](req, global_ctx_id,
+							tag);
 	}
 }
 
@@ -42,7 +43,7 @@ void i915_emit_profiling_data(struct drm_i915_gem_request *req,
  * Emits the commands to capture OA perf report, into the Render CS
  */
 static void i915_oa_emit_perf_report(struct drm_i915_gem_request *req,
-				u32 global_ctx_id)
+				u32 global_ctx_id, u32 tag)
 {
 	struct intel_engine_cs *ring = req->ring;
 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
@@ -71,6 +72,8 @@ static void i915_oa_emit_perf_report(struct drm_i915_gem_request *req,
 	entry->ctx_id = global_ctx_id;
 	if (dev_priv->oa_pmu.sample_info_flags & I915_OA_SAMPLE_PID)
 		entry->pid = current->pid;
+	if (dev_priv->oa_pmu.sample_info_flags & I915_OA_SAMPLE_TAG)
+		entry->tag = tag;
 	i915_gem_request_assign(&entry->req, ring->outstanding_lazy_request);
 
 	spin_lock_irqsave(&dev_priv->oa_pmu.lock, lock_flags);
@@ -308,6 +311,7 @@ static void forward_one_oa_rcs_sample(struct drm_i915_private *dev_priv,
 	u8 *snapshot, *current_ptr;
 	struct drm_i915_oa_node_ctx_id *ctx_info;
 	struct drm_i915_oa_node_pid *pid_info;
+	struct drm_i915_oa_node_tag *tag_info;
 	struct perf_raw_record raw;
 	u64 snapshot_ts;
 
@@ -326,6 +330,13 @@ static void forward_one_oa_rcs_sample(struct drm_i915_private *dev_priv,
 		current_ptr = snapshot + snapshot_size;
 	}
 
+	if (dev_priv->oa_pmu.sample_info_flags & I915_OA_SAMPLE_TAG) {
+		tag_info = (struct drm_i915_oa_node_tag *)current_ptr;
+		tag_info->tag = node->tag;
+		snapshot_size += sizeof(*tag_info);
+		current_ptr = snapshot + snapshot_size;
+	}
+
 	/* Flush the periodic snapshots till the ts of this OA report */
 	snapshot_ts = *(u64 *)(snapshot + 4);
 	flush_oa_snapshots(dev_priv, true, snapshot_ts);
@@ -686,6 +697,9 @@ static int init_oa_rcs_buffer(struct perf_event *event)
 	if (dev_priv->oa_pmu.sample_info_flags & I915_OA_SAMPLE_PID)
 		node_size += sizeof(struct drm_i915_oa_node_pid);
 
+	if (dev_priv->oa_pmu.sample_info_flags & I915_OA_SAMPLE_TAG)
+		node_size += sizeof(struct drm_i915_oa_node_tag);
+
 	/* node size has to be aligned to 64 bytes, since only 64 byte aligned
 	 * addresses can be given to OA unit for dumping OA reports */
 	node_size = ALIGN(node_size, 64);
@@ -841,6 +855,9 @@ static int i915_oa_event_init(struct perf_event *event)
 		if (oa_attr.sample_pid)
 			dev_priv->oa_pmu.sample_info_flags |=
 					I915_OA_SAMPLE_PID;
+		if (oa_attr.sample_tag)
+			dev_priv->oa_pmu.sample_info_flags |=
+					I915_OA_SAMPLE_TAG;
 	}
 
 	report_format = oa_attr.format;
include/uapi/drm/i915_drm.h +12 -1
index 65e8297..abe5826 100644
@@ -94,7 +94,8 @@ typedef struct _drm_i915_oa_attr {
 	__u64 single_context : 1,
 		multiple_context_mode:1,
 		sample_pid:1,
-		__reserved_1:61;
+		sample_tag:1,
+		__reserved_1:60;
 } drm_i915_oa_attr_t;
 
 /* Header for PERF_RECORD_DEVICE type events */
@@ -134,6 +135,11 @@ struct drm_i915_oa_node_pid {
 	__u32 pad;
 };
 
+struct drm_i915_oa_node_tag {
+	__u32 tag;
+	__u32 pad;
+};
+
 /* Each region is a minimum of 16k, and there are at most 255 of them.
  */
 #define I915_NR_TEX_REGIONS 255	/* table size 2k - maximum due to use
@@ -848,6 +854,11 @@ struct drm_i915_gem_execbuffer2 {
 #define i915_execbuffer2_get_context_id(eb2) \
 	((eb2).rsvd1 & I915_EXEC_CONTEXT_ID_MASK)
 
+/* upper 32 bits of rsvd1 field contain tag */
+#define I915_EXEC_TAG_MASK		(0xffffffff00000000UL)
+#define i915_execbuffer2_get_tag(eb2) \
+	((eb2).rsvd1 & I915_EXEC_TAG_MASK)
+
 struct drm_i915_gem_pin {
 	/** Handle of the buffer to be pinned. */
 	__u32 handle;
Signature · 6 lines
1.8.5.1

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx