[PATCH 03/66] lib/xe_eudebug: Allow client to wait for debugger

Christoph Manszewski christoph.manszewski at intel.com
Mon Jul 29 16:00:56 UTC 2024


From: Mika Kuoppala <mika.kuoppala at linux.intel.com>

In some tests, we need to pause the client into a certain
stage it has setup so that debugger can then examine that
setup and crosscheck.

Add a possibility to pause client and debugger to signal
continuation.

We need to take into account that the debugger might
disappear before signaling continuation so a timeout
mechanism for all pipe token reads needs to be added.

Signed-off-by: Mika Kuoppala <mika.kuoppala at linux.intel.com>
Cc: Dominik Grzegorzek <dominik.grzegorzek at intel.com>
---
 lib/xe/xe_eudebug.c | 159 +++++++++++++++++++++++++++++++++-----------
 lib/xe/xe_eudebug.h |   8 ++-
 2 files changed, 129 insertions(+), 38 deletions(-)

diff --git a/lib/xe/xe_eudebug.c b/lib/xe/xe_eudebug.c
index 59cd33990..70769e532 100644
--- a/lib/xe/xe_eudebug.c
+++ b/lib/xe/xe_eudebug.c
@@ -24,6 +24,7 @@ struct event_trigger {
 #define CLIENT_RUN  2
 #define CLIENT_FINI 3
 #define CLIENT_STOP 4
+#define CLIENT_STAGE 5
 
 #define DEBUGGER_WORKER_INACTIVE  0
 #define DEBUGGER_WORKER_ACTIVE  1
@@ -129,9 +130,10 @@ static void catch_child_failure(void)
 	igt_assert_f(WEXITSTATUS(status) == 0, "Client failed!\n");
 }
 
-static int safe_pipe_read(int pipe[2], void *buf, int nbytes)
+static int safe_pipe_read(int pipe[2], void *buf, int nbytes, int timeout_ms)
 {
 	int ret;
+	int t = 0;
 	struct pollfd fd = {
 		.fd = pipe[0],
 		.events = POLLIN,
@@ -142,36 +144,33 @@ static int safe_pipe_read(int pipe[2], void *buf, int nbytes)
 	 * the child process ended with an error.
 	 */
 	do {
-		ret = poll(&fd, 1, 1000);
+		const int interval_ms = 1000;
 
-		if (!ret)
+		ret = poll(&fd, 1, interval_ms);
+
+		if (!ret) {
 			catch_child_failure();
-	} while (!ret);
+			t += interval_ms;
+		}
+	} while (!ret && t < timeout_ms);
 
-	return read(pipe[0], buf, nbytes);
+	if (ret > 0)
+		return read(pipe[0], buf, nbytes);
+
+	return 0;
 }
 
-static uint64_t pipe_wait_u64(int pipe[2])
+static uint64_t pipe_read(int pipe[2], int timeout_ms)
 {
 	uint64_t in;
 	uint64_t ret;
 
-	ret = safe_pipe_read(pipe, &in, sizeof(in));
+	ret = safe_pipe_read(pipe, &in, sizeof(in), timeout_ms);
 	igt_assert(ret == sizeof(in));
 
 	return in;
 }
 
-#define pipe_wait pipe_wait_u64
-
-static void pipe_wait_token(int pipe[2], uint64_t token)
-{
-	uint64_t in;
-
-	in = pipe_wait_u64(pipe);
-	igt_assert_eq(token, in);
-}
-
 static void pipe_signal(int pipe[2], uint64_t token)
 {
 	igt_assert(write(pipe[1], &token, sizeof(token)) == sizeof(token));
@@ -186,6 +185,42 @@ static void pipe_close(int pipe[2])
 		close(pipe[1]);
 }
 
+static uint64_t __wait_token(int p[2], const uint64_t token, int timeout_ms)
+{
+	uint64_t in;
+
+	in = pipe_read(p, timeout_ms);
+
+	igt_assert_eq(in, token);
+
+	return pipe_read(p, timeout_ms);
+}
+
+static uint64_t client_wait_token(struct xe_eudebug_client *c,
+				 const uint64_t token)
+{
+	return __wait_token(c->p_in, token, c->timeout_ms);
+}
+
+static uint64_t wait_from_client(struct xe_eudebug_client *c,
+				 const uint64_t token)
+{
+	return __wait_token(c->p_out, token, c->timeout_ms);
+}
+
+static void token_signal(int p[2], const uint64_t token, const uint64_t value)
+{
+	pipe_signal(p, token);
+	pipe_signal(p, value);
+}
+
+static void client_signal(struct xe_eudebug_client *c,
+			  const uint64_t token,
+			  const uint64_t value)
+{
+	token_signal(c->p_out, token, value);
+}
+
 static int __xe_eudebug_connect(int fd, pid_t pid, uint32_t flags, uint64_t events)
 {
 	struct drm_xe_eudebug_connect param = {
@@ -735,24 +770,28 @@ void xe_eudebug_debugger_destroy(struct xe_eudebug_debugger *d)
 /**
  * xe_eudebug_debugger_attach:
  * @d: pointer to the debugger
- * @target: pid of the process to attach debugger
+ * @c: pointer to the client
  *
- * Opens the xe eu debugger connection to the @target proccess.
+ * Opens the xe eu debugger connection to the process described by @c (c->pid)
  *
  * Returns: 0 if the debugger was successfully attached, -errno otherwise.
  */
-int xe_eudebug_debugger_attach(struct xe_eudebug_debugger *d, pid_t target)
+int xe_eudebug_debugger_attach(struct xe_eudebug_debugger *d,
+			       struct xe_eudebug_client *c)
 {
 	int ret;
 
 	igt_assert_eq(d->fd, -1);
-	ret = xe_eudebug_connect(d->master_fd, target, 0);
+	igt_assert_neq(c->pid, 0);
+	ret = xe_eudebug_connect(d->master_fd, c->pid, 0);
 
 	if (ret < 0)
 		return ret;
 
 	d->fd = ret;
-	d->target_pid = target;
+	d->target_pid = c->pid;
+	d->p_client[0] = c->p_in[0];
+	d->p_client[1] = c->p_in[1];
 
 	igt_debug("debugger connected to %lu\n", d->target_pid);
 
@@ -848,6 +887,19 @@ void xe_eudebug_debugger_stop_worker(struct xe_eudebug_debugger *d,
 	event_log_sort(d->log);
 }
 
+/**
+ * xe_eudebug_debugger_signal_stage:
+ * @d: pointer to the debugger
+ * @stage: stage to signal
+ *
+ * Signals to client, waiting in xe_eudebug_client_wait_stage(),
+ * releasing it to proceed.
+ */
+void xe_eudebug_debugger_signal_stage(struct xe_eudebug_debugger *d, uint64_t stage)
+{
+	token_signal(d->p_client, CLIENT_STAGE, stage);
+}
+
 /**
  * xe_eudebug_client_create:
  * @master_fd: xe client used to open the debugger connection
@@ -879,8 +931,11 @@ struct xe_eudebug_client *xe_eudebug_client_create(int master_fd, xe_eudebug_cli
 	c->done = 0;
 	c->ptr = data;
 	c->master_fd = master_fd;
+	c->timeout_ms = XE_EUDEBUG_DEFAULT_TIMEOUT_MS;
 
 	igt_fork(child, 1) {
+		int mypid;
+
 		igt_assert_eq(c->pid, 0);
 
 		close(c->p_out[0]);
@@ -888,17 +943,20 @@ struct xe_eudebug_client *xe_eudebug_client_create(int master_fd, xe_eudebug_cli
 		close(c->p_in[1]);
 		c->p_in[1] = -1;
 
-		pipe_signal(c->p_out, CLIENT_PID);
-		pipe_signal(c->p_out, getpid());
+		mypid = getpid();
+		client_signal(c, CLIENT_PID, mypid);
 
-		pipe_wait_token(c->p_in, CLIENT_RUN);
-		work(c);
-		pipe_signal(c->p_out, CLIENT_FINI);
+		c->pid = client_wait_token(c, CLIENT_RUN);
+		igt_assert_eq(c->pid, mypid);
+		if (work)
+			work(c);
+
+		client_signal(c, CLIENT_FINI, c->seqno);
 
-		igt_assert_eq(c->pid, 0);
 		event_log_write_to_fd(c->log, c->p_out[1]);
-		pipe_signal(c->p_out, c->seqno);
-		pipe_wait_token(c->p_in, CLIENT_STOP);
+
+		c->pid = client_wait_token(c, CLIENT_STOP);
+		igt_assert_eq(c->pid, mypid);
 	}
 
 	close(c->p_out[1]);
@@ -906,8 +964,7 @@ struct xe_eudebug_client *xe_eudebug_client_create(int master_fd, xe_eudebug_cli
 	close(c->p_in[0]);
 	c->p_in[0] = -1;
 
-	pipe_wait_token(c->p_out, CLIENT_PID);
-	c->pid = pipe_wait(c->p_out);
+	c->pid = wait_from_client(c, CLIENT_PID);
 
 	igt_info("client running with pid %d\n", c->pid);
 
@@ -927,7 +984,7 @@ void xe_eudebug_client_stop(struct xe_eudebug_client *c)
 
 		xe_eudebug_client_wait_done(c);
 
-		pipe_signal(c->p_in, CLIENT_STOP);
+		token_signal(c->p_in, CLIENT_STOP, c->pid);
 		igt_assert_eq(waitpid(c->pid, &waitstatus, 0),
 			      c->pid);
 		c->pid = 0;
@@ -971,7 +1028,7 @@ uint64_t xe_eudebug_client_get_seqno(struct xe_eudebug_client *c)
  */
 void xe_eudebug_client_start(struct xe_eudebug_client *c)
 {
-	pipe_signal(c->p_in, CLIENT_RUN);
+	token_signal(c->p_in, CLIENT_RUN, c->pid);
 }
 
 /**
@@ -985,12 +1042,40 @@ void xe_eudebug_client_wait_done(struct xe_eudebug_client *c)
 {
 	if (!c->done) {
 		c->done = 1;
-		pipe_wait_token(c->p_out, CLIENT_FINI);
+		c->seqno = wait_from_client(c, CLIENT_FINI);
 		event_log_read_from_fd(c->log, c->p_out[0]);
-		c->seqno = pipe_wait(c->p_out);
 	}
 }
 
+/**
+ * xe_eudebug_client_wait_stage:
+ * @c: pointer to xe_eudebug_client structure
+ * @stage: stage to wait on
+ *
+ * Pauses client until the debugger has signalled the corresponding stage with
+ * xe_eudebug_debugger_signal_stage. This is only for situations where the
+ * actual event flow is not enough to coordinate between client/debugger and extra
+ * sync mechanism is needed.
+ *
+ */
+void xe_eudebug_client_wait_stage(struct xe_eudebug_client *c, uint64_t stage)
+{
+	u64 stage_in;
+
+	if (c->done) {
+		igt_warn("client: %d already done before %lu\n", c->pid, stage);
+		return;
+	}
+
+	igt_debug("client: %d pausing for stage %lu\n", c->pid, stage);
+
+	stage_in = client_wait_token(c, CLIENT_STAGE);
+	igt_debug("client: %d stage %lu, expected %lu, stage\n", c->pid, stage_in, stage);
+
+	igt_assert_eq(stage_in, stage);
+}
+
+
 /**
  * xe_eudebug_session_create:
  * @fd: XE file descriptor
@@ -1031,7 +1116,7 @@ void xe_eudebug_session_run(struct xe_eudebug_session *s)
 	struct xe_eudebug_debugger *debugger = s->d;
 	struct xe_eudebug_client *client = s->c;
 
-	igt_assert_eq(xe_eudebug_debugger_attach(debugger, client->pid), 0);
+	igt_assert_eq(xe_eudebug_debugger_attach(debugger, client), 0);
 
 	xe_eudebug_debugger_start_worker(debugger);
 
diff --git a/lib/xe/xe_eudebug.h b/lib/xe/xe_eudebug.h
index a0cae245d..5ec63e46b 100644
--- a/lib/xe/xe_eudebug.h
+++ b/lib/xe/xe_eudebug.h
@@ -36,6 +36,7 @@ struct xe_eudebug_debugger {
 	pthread_t worker_thread;
 	int worker_state;
 
+	int p_client[2];
 };
 
 struct xe_eudebug_client {
@@ -54,6 +55,8 @@ struct xe_eudebug_client {
 
 	/* Used to pickup right device (the one used in debugger) */
 	int master_fd;
+
+	int timeout_ms;
 };
 
 struct xe_eudebug_session {
@@ -110,13 +113,14 @@ void xe_eudebug_event_log_match_opposite(struct xe_eudebug_event_log *l);
 struct xe_eudebug_debugger *
 xe_eudebug_debugger_create(int xe, uint64_t flags, void *data);
 void xe_eudebug_debugger_destroy(struct xe_eudebug_debugger *d);
-int xe_eudebug_debugger_attach(struct xe_eudebug_debugger *d, pid_t pid);
+int xe_eudebug_debugger_attach(struct xe_eudebug_debugger *d, struct xe_eudebug_client *c);
 void xe_eudebug_debugger_start_worker(struct xe_eudebug_debugger *d);
 void xe_eudebug_debugger_stop_worker(struct xe_eudebug_debugger *d, int timeout_s);
 void xe_eudebug_debugger_dettach(struct xe_eudebug_debugger *d);
 void xe_eudebug_debugger_set_data(struct xe_eudebug_debugger *c, void *ptr);
 void xe_eudebug_debugger_add_trigger(struct xe_eudebug_debugger *d, int type,
 				     xe_eudebug_trigger_fn fn);
+void xe_eudebug_debugger_signal_stage(struct xe_eudebug_debugger *d, uint64_t stage);
 
 struct xe_eudebug_client *
 xe_eudebug_client_create(int xe, xe_eudebug_client_work_fn work, uint64_t flags, void *data);
@@ -124,6 +128,8 @@ void xe_eudebug_client_destroy(struct xe_eudebug_client *c);
 void xe_eudebug_client_start(struct xe_eudebug_client *c);
 void xe_eudebug_client_stop(struct xe_eudebug_client *c);
 void xe_eudebug_client_wait_done(struct xe_eudebug_client *c);
+void xe_eudebug_client_wait_stage(struct xe_eudebug_client *c, uint64_t stage);
+
 uint64_t xe_eudebug_client_get_seqno(struct xe_eudebug_client *c);
 void xe_eudebug_client_set_data(struct xe_eudebug_client *c, void *ptr);
 
-- 
2.34.1



More information about the igt-dev mailing list