[Cbe-oss-dev] [PATCH] spu-tools: Enhance PID retrieval

Andre Detsch adetsch at br.ibm.com
Wed Sep 3 05:55:47 EST 2008


Subject: spu-tools: Enhance PID retrieval

This patch makes the PID retrieval smarter, by optimizing the
processing of libspe spufs entries, and by using the PID given
in the entry name when we can't find the thread ID in procfs.

Signed-off-by: Andre Detsch <adetsch at br.ibm.com>

Index: libspe2/tools/spu-tools/spu-tools.spec
===================================================================
--- libspe2.orig/tools/spu-tools/spu-tools.spec
+++ libspe2/tools/spu-tools/spu-tools.spec
@@ -1,7 +1,7 @@
 Summary: user space tools for Cell/B.E.
 Name: spu-tools
 Version: 1.1
-Release: 5
+Release: 6
 License: GPL
 Group: Applications/System
 Source0: spu-tools.tar.bz2
@@ -42,6 +42,9 @@ rm -rf $RPM_BUILD_ROOT
 %dir /%{_prefix}/share/man/man1/spu-ps.1.gz
 
 %changelog
+* Tue Sep 02 2008  Andre Detsch <adetsch at br.ibm.com> 1.1-6
+- Enhanced PID retrieval.
+
 * Fri Jul 04 2008  Andre Detsch <adetsch at br.ibm.com> 1.1-5
 - Support for spu threads with arbitrary spufs entry names.
 
Index: libspe2/tools/spu-tools/src/ChangeLog
===================================================================
--- libspe2.orig/tools/spu-tools/src/ChangeLog
+++ libspe2/tools/spu-tools/src/ChangeLog
@@ -1,3 +1,6 @@
+2008-09-02 Andre Detsch <adetsch at br.ibm.com>
+	* ctx-info.c: Enhanced PID retrieval.
+
 2008-07-04 Andre Detsch <adetsch at br.ibm.com>
 	* ctx-info.c: Support for spu threads with arbitrary spufs entry names.
 
Index: libspe2/tools/spu-tools/src/ctx-info.c
===================================================================
--- libspe2.orig/tools/spu-tools/src/ctx-info.c
+++ libspe2/tools/spu-tools/src/ctx-info.c
@@ -291,13 +291,23 @@ static struct ctx *ctxs_get_ctx(int thre
 	return NULL;
 }
 
-static int find_thread_pid(int thread_id)
+static int get_thread_pid(int thread_id, const char* entry_name)
 {
 	DIR* proc_dir;
 	struct dirent *entry;
 	static char buf[PATH_MAX];
-	int pid = -1;
+	unsigned long long ctx_id;
+	int pid = -1, pid_entry = -1;
+
+	/* Try to get PID from the spufs entry name */
+	if (sscanf(entry_name, "spethread-%d-%llu", &pid_entry, &ctx_id) == 2) {
+		/* Check if the TID belongs to the given PID in procfs */
+		sprintf(buf, "%s/%d/task/%d", PROCFS_PATH, pid_entry, thread_id);
+		if (access(buf, F_OK) == 0)
+			return pid_entry;
+	}
 
+	/* If PID was not found yet, try to find it in procfs */
 	proc_dir = opendir(PROCFS_PATH);
 	if (proc_dir) {
 		while ((entry = readdir(proc_dir)) != NULL) {
@@ -309,7 +319,13 @@ static int find_thread_pid(int thread_id
 		}
 		closedir(proc_dir);
 	}
-	return pid;
+	if (pid != -1)
+		return pid;
+
+	/* If no PID was found, use the one given by the entry name, even
+	 * if it can't be verified in procfs
+	 */
+	return pid_entry;
 }
 
 static int
@@ -340,9 +356,8 @@ process_ctx_entry(struct dirent *entry, 
 		ctx = alloc_ctx();
 		ctxs[ctxs_n] = ctx;
 		ctxs_n++;
-
 		ctx->thread_id = thread_id;
-		ctx->ppu_pid = find_thread_pid(thread_id);
+		ctx->ppu_pid = get_thread_pid(thread_id, entry->d_name);
 	}
 
 	sprintf(buf, "%s/%d/stat", PROCFS_PATH, ctx->ppu_pid);
@@ -352,8 +367,6 @@ process_ctx_entry(struct dirent *entry, 
 		buf[strlen(buf)-1] = '\0';         /* Remove trailing ')'*/
 		ctx->binary_name = strdup(buf+1);  /* Skip initial '('   */
 		fclose(fp);
-	} else {
-		return 0;
 	}
 
 	sprintf(buf, "%s/%d/status", PROCFS_PATH, ctx->ppu_pid);



More information about the cbe-oss-dev mailing list