Open MPI logo

Hardware Locality Development Mailing List Archives

  |   Home   |   Support   |   FAQ   |   all Hardware Locality Development mailing list

Subject: [hwloc-devel] [PATCH] Use the #links as an estimate for the #tids in a tasks directory
From: Bert Wesarg (bert.wesarg_at_[hidden])
Date: 2010-03-10 18:30:57


When reading all tids from a process in
topology-linux.c::hwloc_linux_get_proc_tids(), it used a
exponential realloc algorithm to increase the storage size for the tids.

Now it uses the number of links (.st_nlinks) from a stat() call to the
directory (actually a fstat() call to the dirfd() of the opened
directory) as a good estimate for the initial size of the storage
vector and than a small linear expansion rule.

Regards,
Bert

Index: src/topology-linux.c
===================================================================
--- src/topology-linux.c (revision 1821)
+++ src/topology-linux.c (working copy)
@@ -18,6 +18,7 @@
 #include <dirent.h>
 #include <unistd.h>
 #include <sys/types.h>
+#include <sys/stat.h>
 #include <sched.h>
 #include <pthread.h>

@@ -312,13 +313,20 @@
   struct dirent *dirent;
   unsigned nr_tids = 0;
   unsigned max_tids = 32;
- pid_t *tids = malloc(max_tids*sizeof(pid_t));
+ pid_t *tids;
+ struct stat sb;

+ /* take the number of links as a good estimate for the number of tids */
+ if (fstat(dirfd(taskdir), &sb) == 0)
+ max_tids = sb.st_nlink;
+
+ tids = malloc(max_tids*sizeof(pid_t));
+
   rewinddir(taskdir);

   while ((dirent = readdir(taskdir)) != NULL) {
     if (nr_tids == max_tids) {
- max_tids *= 2;
+ max_tids += 8;
       tids = realloc(tids, max_tids*sizeof(pid_t));
     }
     if (!strcmp(dirent->d_name, ".") || !strcmp(dirent->d_name, ".."))