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, ".."))

Reply via email to