1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
/*
* Copyright © 2009-2019 Inria. All rights reserved.
* Copyright © 2009-2012 Université Bordeaux
* Copyright © 2009-2011 Cisco Systems, Inc. All rights reserved.
* See COPYING in top-level directory.
*/
#include "private/autogen/config.h"
#include "hwloc.h"
#ifdef HWLOC_LINUX_SYS
#include "hwloc/linux.h"
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef HAVE_DIRENT_H
#include <dirent.h>
#endif
#include <fcntl.h>
#include "common-ps.h"
#include "misc.h"
int hwloc_ps_read_process(hwloc_topology_t topology, hwloc_const_bitmap_t topocpuset,
struct hwloc_ps_process *proc,
unsigned long flags, const char *pidcmd)
{
#ifdef HAVE_DIRENT_H
hwloc_pid_t realpid;
hwloc_bitmap_t cpuset;
unsigned pathlen;
char *path;
char *end;
int fd;
ssize_t n;
if (hwloc_pid_from_number(&realpid, proc->pid, 0, 0 /* ignore failures */) < 0)
return -1;
cpuset = hwloc_bitmap_alloc();
if (!cpuset)
return -1;
pathlen = 6 + 21 + 1 + 7 + 1; /* enough for /proc/%ld/cmdline /proc/%ld/comm and /proc/%ld/stat */
path = malloc(pathlen);
snprintf(path, pathlen, "/proc/%ld/cmdline", proc->pid);
fd = open(path, O_RDONLY);
if (fd < 0) {
free(path);
goto out;
}
n = read(fd, proc->name, sizeof(proc->name) - 1);
close(fd);
if (n <= 0) {
/* Ignore kernel threads and errors */
free(path);
goto out;
}
proc->name[n] = 0;
if (flags & HWLOC_PS_FLAG_SHORTNAME) {
/* try to get a small name from comm */
char comm[16] = "";
snprintf(path, pathlen, "/proc/%ld/comm", proc->pid);
fd = open(path, O_RDONLY);
if (fd >= 0) {
n = read(fd, comm, sizeof(comm) - 1);
close(fd);
if (n > 0) {
comm[n] = 0;
if (n > 1 && comm[n-1] == '\n')
comm[n-1] = 0;
}
} else {
/* Old kernel, have to look at old file */
char stats[32];
char *parenl = NULL, *parenr;
snprintf(path, pathlen, "/proc/%ld/stat", proc->pid);
fd = open(path, O_RDONLY);
if (fd >= 0) {
/* "pid (comm) ..." */
n = read(fd, stats, sizeof(stats) - 1);
close(fd);
if (n > 0) {
stats[n] = 0;
parenl = strchr(stats, '(');
parenr = strchr(stats, ')');
if (!parenr)
parenr = &stats[sizeof(stats)-1];
*parenr = 0;
if (parenl)
snprintf(comm, sizeof(comm), "%s", parenl+1);
}
}
}
if (*comm)
snprintf(proc->name, sizeof(proc->name), "%s", comm);
}
free(path);
proc->string[0] = '\0';
if (pidcmd) {
char *cmd;
FILE *file;
cmd = malloc(strlen(pidcmd)+1+5+2+1);
sprintf(cmd, "%s %u", pidcmd, (unsigned) proc->pid);
file = popen(cmd, "r");
if (file) {
if (fgets(proc->string, sizeof(proc->string), file)) {
end = strchr(proc->string, '\n');
if (end)
*end = '\0';
}
pclose(file);
}
free(cmd);
}
if (flags & HWLOC_PS_FLAG_UID) {
proc->uid = HWLOC_PS_ALL_UIDS;
#ifdef HWLOC_LINUX_SYS
pathlen = 6 + 21 + 1 + 6 + 1;
path = malloc(pathlen);
snprintf(path, pathlen, "/proc/%ld/status", proc->pid);
fd = open(path, O_RDONLY);
if (fd >= 0) {
char status[1024];
char *uid;
(void) read(fd, &status, sizeof(status));
status[1023] = '\0';
uid = strstr(status, "Uid:");
if (uid)
proc->uid = strtoul(uid+4, NULL, 0);
close(fd);
}
free(path);
#endif
/* On *BSD, parse the end of the single-line in /proc/pid/status
* (but the format is different between FreeBSD and NetBSD).
* It may be a good time to switch to a portable library for gathering this info.
*/
}
if (flags & HWLOC_PS_FLAG_THREADS) {
#ifdef HWLOC_LINUX_SYS
/* check if some threads must be displayed */
DIR *taskdir;
pathlen = 6 + 21 + 1 + 4 + 1;
path = malloc(pathlen);
snprintf(path, pathlen, "/proc/%ld/task", proc->pid);
taskdir = opendir(path);
if (taskdir) {
struct dirent *taskdirent;
long tid;
unsigned nbth = 0;
/* count threads */
while ((taskdirent = readdir(taskdir))) {
tid = strtol(taskdirent->d_name, &end, 10);
if (*end)
/* Not a number */
continue;
nbth++;
}
if (nbth > 1) {
/* if there's more than one thread, see if some are bound */
proc->threads = calloc(nbth, sizeof(*proc->threads));
if (proc->threads) {
/* reread the directory but gather info now */
rewinddir(taskdir);
unsigned i = 0;
while ((taskdirent = readdir(taskdir))) {
char *path2;
unsigned path2len;
tid = strtol(taskdirent->d_name, &end, 10);
if (*end)
/* Not a number */
continue;
proc->threads[i].tid = tid;
path2len = pathlen + 1 + 21 + 1 + 4 + 1;
path2 = malloc(path2len);
if (path2) {
int commfd;
snprintf(path2, path2len, "%s/%ld/comm", path, tid);
commfd = open(path2, O_RDWR);
if (commfd >= 0) {
n = read(commfd, proc->threads[i].name, sizeof(proc->threads[i].name));
close(commfd);
if (n <= 0)
proc->threads[i].name[0] = '\0';
else if ((size_t)n < sizeof(proc->threads[i].name))
proc->threads[i].name[n] = '\0';
proc->threads[i].name[sizeof(proc->threads[i].name)-1] = '\0';
end = strchr(proc->threads[i].name, '\n');
if (end)
*end = '\0';
}
free(path2);
}
if (flags & HWLOC_PS_FLAG_LASTCPULOCATION) {
if (hwloc_linux_get_tid_last_cpu_location(topology, tid, cpuset))
goto next;
} else {
if (hwloc_linux_get_tid_cpubind(topology, tid, cpuset))
goto next;
}
hwloc_bitmap_and(cpuset, cpuset, topocpuset);
if (hwloc_bitmap_iszero(cpuset))
goto next;
proc->threads[i].cpuset = hwloc_bitmap_dup(cpuset);
if (!hwloc_bitmap_isequal(cpuset, topocpuset)) {
proc->threads[i].bound = 1;
proc->nboundthreads++;
}
next:
i++;
proc->nthreads++;
if (i == nbth)
/* ignore the lastly created threads, I'm too lazy to reallocate */
break;
}
} else {
/* failed to alloc, behave as if there were no threads */
}
}
closedir(taskdir);
}
free(path);
#endif /* HWLOC_LINUX_SYS */
}
if (flags & HWLOC_PS_FLAG_LASTCPULOCATION) {
if (hwloc_get_proc_last_cpu_location(topology, realpid, cpuset, 0))
goto out;
} else {
if (hwloc_get_proc_cpubind(topology, realpid, cpuset, 0))
goto out;
}
hwloc_bitmap_and(cpuset, cpuset, topocpuset);
if (hwloc_bitmap_iszero(cpuset))
goto out;
proc->bound = !hwloc_bitmap_isequal(cpuset, topocpuset);
proc->cpuset = cpuset;
return 0;
out:
hwloc_bitmap_free(cpuset);
#endif /* HAVE_DIRENT_H */
return -1;
}
void hwloc_ps_free_process(struct hwloc_ps_process *proc)
{
unsigned i;
if (proc->nthreads)
for(i=0; i<proc->nthreads; i++)
if (proc->threads[i].cpuset)
hwloc_bitmap_free(proc->threads[i].cpuset);
free(proc->threads);
hwloc_bitmap_free(proc->cpuset);
}
int hwloc_ps_foreach_process(hwloc_topology_t topology, hwloc_const_bitmap_t topocpuset,
void (*callback)(hwloc_topology_t topology, struct hwloc_ps_process *proc, void *cbdata),
void *cbdata,
unsigned long flags, const char *only_name, long uid, const char *pidcmd)
{
#ifdef HAVE_DIRENT_H
DIR *dir;
struct dirent *dirent;
dir = opendir("/proc");
if (!dir)
return -1;
while ((dirent = readdir(dir))) {
struct hwloc_ps_process proc;
long pid;
char *end;
pid = strtol(dirent->d_name, &end, 10);
if (*end)
/* Not a number */
continue;
proc.pid = pid;
proc.cpuset = NULL;
proc.nthreads = 0;
proc.nboundthreads = 0;
proc.threads = NULL;
if (hwloc_ps_read_process(topology, topocpuset, &proc, flags, pidcmd) < 0)
goto next;
if (only_name && !strstr(proc.name, only_name))
goto next;
if (uid != HWLOC_PS_ALL_UIDS && proc.uid != HWLOC_PS_ALL_UIDS && proc.uid != uid)
goto next;
callback(topology, &proc, cbdata);
next:
hwloc_ps_free_process(&proc);
}
closedir(dir);
return 0;
#else /* HAVE_DIRENT_H */
return -1;
#endif /* HAVE_DIRENT_H */
}
|