Linux Audio

Check our new training course

Loading...
v4.17
  1// SPDX-License-Identifier: GPL-2.0
  2#include <dirent.h>
  3#include <errno.h>
  4#include <limits.h>
  5#include <stdbool.h>
  6#include <stdlib.h>
  7#include <stdio.h>
  8#include <sys/types.h>
  9#include <sys/stat.h>
 10#include <unistd.h>
 11#include "string2.h"
 12#include "strlist.h"
 13#include <string.h>
 14#include <api/fs/fs.h>
 15#include "asm/bug.h"
 16#include "thread_map.h"
 17#include "util.h"
 18#include "debug.h"
 19#include "event.h"
 20
 21/* Skip "." and ".." directories */
 22static int filter(const struct dirent *dir)
 23{
 24	if (dir->d_name[0] == '.')
 25		return 0;
 26	else
 27		return 1;
 28}
 29
 30static void thread_map__reset(struct thread_map *map, int start, int nr)
 31{
 32	size_t size = (nr - start) * sizeof(map->map[0]);
 33
 34	memset(&map->map[start], 0, size);
 35	map->err_thread = -1;
 36}
 37
 38static struct thread_map *thread_map__realloc(struct thread_map *map, int nr)
 39{
 40	size_t size = sizeof(*map) + sizeof(map->map[0]) * nr;
 41	int start = map ? map->nr : 0;
 42
 43	map = realloc(map, size);
 44	/*
 45	 * We only realloc to add more items, let's reset new items.
 46	 */
 47	if (map)
 48		thread_map__reset(map, start, nr);
 49
 50	return map;
 51}
 52
 53#define thread_map__alloc(__nr) thread_map__realloc(NULL, __nr)
 54
 55struct thread_map *thread_map__new_by_pid(pid_t pid)
 56{
 57	struct thread_map *threads;
 58	char name[256];
 59	int items;
 60	struct dirent **namelist = NULL;
 61	int i;
 62
 63	sprintf(name, "/proc/%d/task", pid);
 64	items = scandir(name, &namelist, filter, NULL);
 65	if (items <= 0)
 66		return NULL;
 67
 68	threads = thread_map__alloc(items);
 69	if (threads != NULL) {
 70		for (i = 0; i < items; i++)
 71			thread_map__set_pid(threads, i, atoi(namelist[i]->d_name));
 72		threads->nr = items;
 73		refcount_set(&threads->refcnt, 1);
 74	}
 75
 76	for (i=0; i<items; i++)
 77		zfree(&namelist[i]);
 78	free(namelist);
 79
 80	return threads;
 81}
 82
 83struct thread_map *thread_map__new_by_tid(pid_t tid)
 84{
 85	struct thread_map *threads = thread_map__alloc(1);
 86
 87	if (threads != NULL) {
 88		thread_map__set_pid(threads, 0, tid);
 89		threads->nr = 1;
 90		refcount_set(&threads->refcnt, 1);
 91	}
 92
 93	return threads;
 94}
 95
 96static struct thread_map *__thread_map__new_all_cpus(uid_t uid)
 97{
 98	DIR *proc;
 99	int max_threads = 32, items, i;
100	char path[NAME_MAX + 1 + 6];
101	struct dirent *dirent, **namelist = NULL;
102	struct thread_map *threads = thread_map__alloc(max_threads);
103
104	if (threads == NULL)
105		goto out;
106
107	proc = opendir("/proc");
108	if (proc == NULL)
109		goto out_free_threads;
110
111	threads->nr = 0;
112	refcount_set(&threads->refcnt, 1);
113
114	while ((dirent = readdir(proc)) != NULL) {
115		char *end;
116		bool grow = false;
117		pid_t pid = strtol(dirent->d_name, &end, 10);
 
118
119		if (*end) /* only interested in proper numerical dirents */
120			continue;
121
122		snprintf(path, sizeof(path), "/proc/%s", dirent->d_name);
123
124		if (uid != UINT_MAX) {
125			struct stat st;
126
127			if (stat(path, &st) != 0 || st.st_uid != uid)
128				continue;
129		}
130
131		snprintf(path, sizeof(path), "/proc/%d/task", pid);
132		items = scandir(path, &namelist, filter, NULL);
133		if (items <= 0)
134			goto out_free_closedir;
135
136		while (threads->nr + items >= max_threads) {
137			max_threads *= 2;
138			grow = true;
139		}
140
141		if (grow) {
142			struct thread_map *tmp;
143
144			tmp = thread_map__realloc(threads, max_threads);
 
145			if (tmp == NULL)
146				goto out_free_namelist;
147
148			threads = tmp;
149		}
150
151		for (i = 0; i < items; i++) {
152			thread_map__set_pid(threads, threads->nr + i,
153					    atoi(namelist[i]->d_name));
154		}
155
156		for (i = 0; i < items; i++)
157			zfree(&namelist[i]);
158		free(namelist);
159
160		threads->nr += items;
161	}
162
163out_closedir:
164	closedir(proc);
165out:
166	return threads;
167
168out_free_threads:
169	free(threads);
170	return NULL;
171
172out_free_namelist:
173	for (i = 0; i < items; i++)
174		zfree(&namelist[i]);
175	free(namelist);
176
177out_free_closedir:
178	zfree(&threads);
 
179	goto out_closedir;
180}
181
182struct thread_map *thread_map__new_all_cpus(void)
183{
184	return __thread_map__new_all_cpus(UINT_MAX);
185}
186
187struct thread_map *thread_map__new_by_uid(uid_t uid)
188{
189	return __thread_map__new_all_cpus(uid);
190}
191
192struct thread_map *thread_map__new(pid_t pid, pid_t tid, uid_t uid)
193{
194	if (pid != -1)
195		return thread_map__new_by_pid(pid);
196
197	if (tid == -1 && uid != UINT_MAX)
198		return thread_map__new_by_uid(uid);
199
200	return thread_map__new_by_tid(tid);
201}
202
203static struct thread_map *thread_map__new_by_pid_str(const char *pid_str)
204{
205	struct thread_map *threads = NULL, *nt;
206	char name[256];
207	int items, total_tasks = 0;
208	struct dirent **namelist = NULL;
209	int i, j = 0;
210	pid_t pid, prev_pid = INT_MAX;
211	char *end_ptr;
212	struct str_node *pos;
213	struct strlist_config slist_config = { .dont_dupstr = true, };
214	struct strlist *slist = strlist__new(pid_str, &slist_config);
215
216	if (!slist)
217		return NULL;
218
219	strlist__for_each_entry(pos, slist) {
220		pid = strtol(pos->s, &end_ptr, 10);
221
222		if (pid == INT_MIN || pid == INT_MAX ||
223		    (*end_ptr != '\0' && *end_ptr != ','))
224			goto out_free_threads;
225
226		if (pid == prev_pid)
227			continue;
228
229		sprintf(name, "/proc/%d/task", pid);
230		items = scandir(name, &namelist, filter, NULL);
231		if (items <= 0)
232			goto out_free_threads;
233
234		total_tasks += items;
235		nt = thread_map__realloc(threads, total_tasks);
 
236		if (nt == NULL)
237			goto out_free_namelist;
238
239		threads = nt;
240
241		for (i = 0; i < items; i++) {
242			thread_map__set_pid(threads, j++, atoi(namelist[i]->d_name));
243			zfree(&namelist[i]);
244		}
245		threads->nr = total_tasks;
246		free(namelist);
247	}
248
249out:
250	strlist__delete(slist);
251	if (threads)
252		refcount_set(&threads->refcnt, 1);
253	return threads;
254
255out_free_namelist:
256	for (i = 0; i < items; i++)
257		zfree(&namelist[i]);
258	free(namelist);
259
260out_free_threads:
261	zfree(&threads);
 
262	goto out;
263}
264
265struct thread_map *thread_map__new_dummy(void)
266{
267	struct thread_map *threads = thread_map__alloc(1);
268
269	if (threads != NULL) {
270		thread_map__set_pid(threads, 0, -1);
271		threads->nr = 1;
272		refcount_set(&threads->refcnt, 1);
273	}
274	return threads;
275}
276
277struct thread_map *thread_map__new_by_tid_str(const char *tid_str)
278{
279	struct thread_map *threads = NULL, *nt;
280	int ntasks = 0;
281	pid_t tid, prev_tid = INT_MAX;
282	char *end_ptr;
283	struct str_node *pos;
284	struct strlist_config slist_config = { .dont_dupstr = true, };
285	struct strlist *slist;
286
287	/* perf-stat expects threads to be generated even if tid not given */
288	if (!tid_str)
289		return thread_map__new_dummy();
 
 
 
 
 
 
290
291	slist = strlist__new(tid_str, &slist_config);
292	if (!slist)
293		return NULL;
294
295	strlist__for_each_entry(pos, slist) {
296		tid = strtol(pos->s, &end_ptr, 10);
297
298		if (tid == INT_MIN || tid == INT_MAX ||
299		    (*end_ptr != '\0' && *end_ptr != ','))
300			goto out_free_threads;
301
302		if (tid == prev_tid)
303			continue;
304
305		ntasks++;
306		nt = thread_map__realloc(threads, ntasks);
307
308		if (nt == NULL)
309			goto out_free_threads;
310
311		threads = nt;
312		thread_map__set_pid(threads, ntasks - 1, tid);
313		threads->nr = ntasks;
314	}
315out:
316	if (threads)
317		refcount_set(&threads->refcnt, 1);
318	return threads;
319
320out_free_threads:
321	zfree(&threads);
322	strlist__delete(slist);
323	goto out;
324}
325
326struct thread_map *thread_map__new_str(const char *pid, const char *tid,
327				       uid_t uid, bool all_threads)
328{
329	if (pid)
330		return thread_map__new_by_pid_str(pid);
331
332	if (!tid && uid != UINT_MAX)
333		return thread_map__new_by_uid(uid);
334
335	if (all_threads)
336		return thread_map__new_all_cpus();
337
338	return thread_map__new_by_tid_str(tid);
339}
340
341static void thread_map__delete(struct thread_map *threads)
342{
343	if (threads) {
344		int i;
345
346		WARN_ONCE(refcount_read(&threads->refcnt) != 0,
347			  "thread map refcnt unbalanced\n");
348		for (i = 0; i < threads->nr; i++)
349			free(thread_map__comm(threads, i));
350		free(threads);
351	}
352}
353
354struct thread_map *thread_map__get(struct thread_map *map)
355{
356	if (map)
357		refcount_inc(&map->refcnt);
358	return map;
359}
360
361void thread_map__put(struct thread_map *map)
362{
363	if (map && refcount_dec_and_test(&map->refcnt))
364		thread_map__delete(map);
365}
366
367size_t thread_map__fprintf(struct thread_map *threads, FILE *fp)
368{
369	int i;
370	size_t printed = fprintf(fp, "%d thread%s: ",
371				 threads->nr, threads->nr > 1 ? "s" : "");
372	for (i = 0; i < threads->nr; ++i)
373		printed += fprintf(fp, "%s%d", i ? ", " : "", thread_map__pid(threads, i));
374
375	return printed + fprintf(fp, "\n");
376}
377
378static int get_comm(char **comm, pid_t pid)
379{
380	char *path;
381	size_t size;
382	int err;
383
384	if (asprintf(&path, "%s/%d/comm", procfs__mountpoint(), pid) == -1)
385		return -ENOMEM;
386
387	err = filename__read_str(path, comm, &size);
388	if (!err) {
389		/*
390		 * We're reading 16 bytes, while filename__read_str
391		 * allocates data per BUFSIZ bytes, so we can safely
392		 * mark the end of the string.
393		 */
394		(*comm)[size] = 0;
395		rtrim(*comm);
396	}
397
398	free(path);
399	return err;
400}
401
402static void comm_init(struct thread_map *map, int i)
403{
404	pid_t pid = thread_map__pid(map, i);
405	char *comm = NULL;
406
407	/* dummy pid comm initialization */
408	if (pid == -1) {
409		map->map[i].comm = strdup("dummy");
410		return;
411	}
412
413	/*
414	 * The comm name is like extra bonus ;-),
415	 * so just warn if we fail for any reason.
416	 */
417	if (get_comm(&comm, pid))
418		pr_warning("Couldn't resolve comm name for pid %d\n", pid);
419
420	map->map[i].comm = comm;
421}
422
423void thread_map__read_comms(struct thread_map *threads)
424{
425	int i;
426
427	for (i = 0; i < threads->nr; ++i)
428		comm_init(threads, i);
429}
430
431static void thread_map__copy_event(struct thread_map *threads,
432				   struct thread_map_event *event)
433{
434	unsigned i;
435
436	threads->nr = (int) event->nr;
437
438	for (i = 0; i < event->nr; i++) {
439		thread_map__set_pid(threads, i, (pid_t) event->entries[i].pid);
440		threads->map[i].comm = strndup(event->entries[i].comm, 16);
441	}
442
443	refcount_set(&threads->refcnt, 1);
444}
445
446struct thread_map *thread_map__new_event(struct thread_map_event *event)
447{
448	struct thread_map *threads;
449
450	threads = thread_map__alloc(event->nr);
451	if (threads)
452		thread_map__copy_event(threads, event);
453
454	return threads;
455}
456
457bool thread_map__has(struct thread_map *threads, pid_t pid)
458{
459	int i;
460
461	for (i = 0; i < threads->nr; ++i) {
462		if (threads->map[i].pid == pid)
463			return true;
464	}
465
466	return false;
467}
468
469int thread_map__remove(struct thread_map *threads, int idx)
470{
471	int i;
472
473	if (threads->nr < 1)
474		return -EINVAL;
475
476	if (idx >= threads->nr)
477		return -EINVAL;
478
479	/*
480	 * Free the 'idx' item and shift the rest up.
481	 */
482	free(threads->map[idx].comm);
483
484	for (i = idx; i < threads->nr - 1; i++)
485		threads->map[i] = threads->map[i + 1];
486
487	threads->nr--;
488	return 0;
489}
v3.5.6
 
  1#include <dirent.h>
 
  2#include <limits.h>
  3#include <stdbool.h>
  4#include <stdlib.h>
  5#include <stdio.h>
  6#include <sys/types.h>
  7#include <sys/stat.h>
  8#include <unistd.h>
 
  9#include "strlist.h"
 10#include <string.h>
 
 
 11#include "thread_map.h"
 
 
 
 12
 13/* Skip "." and ".." directories */
 14static int filter(const struct dirent *dir)
 15{
 16	if (dir->d_name[0] == '.')
 17		return 0;
 18	else
 19		return 1;
 20}
 21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 22struct thread_map *thread_map__new_by_pid(pid_t pid)
 23{
 24	struct thread_map *threads;
 25	char name[256];
 26	int items;
 27	struct dirent **namelist = NULL;
 28	int i;
 29
 30	sprintf(name, "/proc/%d/task", pid);
 31	items = scandir(name, &namelist, filter, NULL);
 32	if (items <= 0)
 33		return NULL;
 34
 35	threads = malloc(sizeof(*threads) + sizeof(pid_t) * items);
 36	if (threads != NULL) {
 37		for (i = 0; i < items; i++)
 38			threads->map[i] = atoi(namelist[i]->d_name);
 39		threads->nr = items;
 
 40	}
 41
 42	for (i=0; i<items; i++)
 43		free(namelist[i]);
 44	free(namelist);
 45
 46	return threads;
 47}
 48
 49struct thread_map *thread_map__new_by_tid(pid_t tid)
 50{
 51	struct thread_map *threads = malloc(sizeof(*threads) + sizeof(pid_t));
 52
 53	if (threads != NULL) {
 54		threads->map[0] = tid;
 55		threads->nr	= 1;
 
 56	}
 57
 58	return threads;
 59}
 60
 61struct thread_map *thread_map__new_by_uid(uid_t uid)
 62{
 63	DIR *proc;
 64	int max_threads = 32, items, i;
 65	char path[256];
 66	struct dirent dirent, *next, **namelist = NULL;
 67	struct thread_map *threads = malloc(sizeof(*threads) +
 68					    max_threads * sizeof(pid_t));
 69	if (threads == NULL)
 70		goto out;
 71
 72	proc = opendir("/proc");
 73	if (proc == NULL)
 74		goto out_free_threads;
 75
 76	threads->nr = 0;
 
 77
 78	while (!readdir_r(proc, &dirent, &next) && next) {
 79		char *end;
 80		bool grow = false;
 81		struct stat st;
 82		pid_t pid = strtol(dirent.d_name, &end, 10);
 83
 84		if (*end) /* only interested in proper numerical dirents */
 85			continue;
 86
 87		snprintf(path, sizeof(path), "/proc/%s", dirent.d_name);
 88
 89		if (stat(path, &st) != 0)
 90			continue;
 91
 92		if (st.st_uid != uid)
 93			continue;
 
 94
 95		snprintf(path, sizeof(path), "/proc/%d/task", pid);
 96		items = scandir(path, &namelist, filter, NULL);
 97		if (items <= 0)
 98			goto out_free_closedir;
 99
100		while (threads->nr + items >= max_threads) {
101			max_threads *= 2;
102			grow = true;
103		}
104
105		if (grow) {
106			struct thread_map *tmp;
107
108			tmp = realloc(threads, (sizeof(*threads) +
109						max_threads * sizeof(pid_t)));
110			if (tmp == NULL)
111				goto out_free_namelist;
112
113			threads = tmp;
114		}
115
116		for (i = 0; i < items; i++)
117			threads->map[threads->nr + i] = atoi(namelist[i]->d_name);
 
 
118
119		for (i = 0; i < items; i++)
120			free(namelist[i]);
121		free(namelist);
122
123		threads->nr += items;
124	}
125
126out_closedir:
127	closedir(proc);
128out:
129	return threads;
130
131out_free_threads:
132	free(threads);
133	return NULL;
134
135out_free_namelist:
136	for (i = 0; i < items; i++)
137		free(namelist[i]);
138	free(namelist);
139
140out_free_closedir:
141	free(threads);
142	threads = NULL;
143	goto out_closedir;
144}
145
 
 
 
 
 
 
 
 
 
 
146struct thread_map *thread_map__new(pid_t pid, pid_t tid, uid_t uid)
147{
148	if (pid != -1)
149		return thread_map__new_by_pid(pid);
150
151	if (tid == -1 && uid != UINT_MAX)
152		return thread_map__new_by_uid(uid);
153
154	return thread_map__new_by_tid(tid);
155}
156
157static struct thread_map *thread_map__new_by_pid_str(const char *pid_str)
158{
159	struct thread_map *threads = NULL, *nt;
160	char name[256];
161	int items, total_tasks = 0;
162	struct dirent **namelist = NULL;
163	int i, j = 0;
164	pid_t pid, prev_pid = INT_MAX;
165	char *end_ptr;
166	struct str_node *pos;
167	struct strlist *slist = strlist__new(false, pid_str);
 
168
169	if (!slist)
170		return NULL;
171
172	strlist__for_each(pos, slist) {
173		pid = strtol(pos->s, &end_ptr, 10);
174
175		if (pid == INT_MIN || pid == INT_MAX ||
176		    (*end_ptr != '\0' && *end_ptr != ','))
177			goto out_free_threads;
178
179		if (pid == prev_pid)
180			continue;
181
182		sprintf(name, "/proc/%d/task", pid);
183		items = scandir(name, &namelist, filter, NULL);
184		if (items <= 0)
185			goto out_free_threads;
186
187		total_tasks += items;
188		nt = realloc(threads, (sizeof(*threads) +
189				       sizeof(pid_t) * total_tasks));
190		if (nt == NULL)
191			goto out_free_namelist;
192
193		threads = nt;
194
195		for (i = 0; i < items; i++) {
196			threads->map[j++] = atoi(namelist[i]->d_name);
197			free(namelist[i]);
198		}
199		threads->nr = total_tasks;
200		free(namelist);
201	}
202
203out:
204	strlist__delete(slist);
 
 
205	return threads;
206
207out_free_namelist:
208	for (i = 0; i < items; i++)
209		free(namelist[i]);
210	free(namelist);
211
212out_free_threads:
213	free(threads);
214	threads = NULL;
215	goto out;
216}
217
218static struct thread_map *thread_map__new_by_tid_str(const char *tid_str)
 
 
 
 
 
 
 
 
 
 
 
 
219{
220	struct thread_map *threads = NULL, *nt;
221	int ntasks = 0;
222	pid_t tid, prev_tid = INT_MAX;
223	char *end_ptr;
224	struct str_node *pos;
 
225	struct strlist *slist;
226
227	/* perf-stat expects threads to be generated even if tid not given */
228	if (!tid_str) {
229		threads = malloc(sizeof(*threads) + sizeof(pid_t));
230		if (threads != NULL) {
231			threads->map[0] = -1;
232			threads->nr	= 1;
233		}
234		return threads;
235	}
236
237	slist = strlist__new(false, tid_str);
238	if (!slist)
239		return NULL;
240
241	strlist__for_each(pos, slist) {
242		tid = strtol(pos->s, &end_ptr, 10);
243
244		if (tid == INT_MIN || tid == INT_MAX ||
245		    (*end_ptr != '\0' && *end_ptr != ','))
246			goto out_free_threads;
247
248		if (tid == prev_tid)
249			continue;
250
251		ntasks++;
252		nt = realloc(threads, sizeof(*threads) + sizeof(pid_t) * ntasks);
253
254		if (nt == NULL)
255			goto out_free_threads;
256
257		threads = nt;
258		threads->map[ntasks - 1] = tid;
259		threads->nr		 = ntasks;
260	}
261out:
 
 
262	return threads;
263
264out_free_threads:
265	free(threads);
266	threads = NULL;
267	goto out;
268}
269
270struct thread_map *thread_map__new_str(const char *pid, const char *tid,
271				       uid_t uid)
272{
273	if (pid)
274		return thread_map__new_by_pid_str(pid);
275
276	if (!tid && uid != UINT_MAX)
277		return thread_map__new_by_uid(uid);
278
 
 
 
279	return thread_map__new_by_tid_str(tid);
280}
281
282void thread_map__delete(struct thread_map *threads)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
283{
284	free(threads);
 
285}
286
287size_t thread_map__fprintf(struct thread_map *threads, FILE *fp)
288{
289	int i;
290	size_t printed = fprintf(fp, "%d thread%s: ",
291				 threads->nr, threads->nr > 1 ? "s" : "");
292	for (i = 0; i < threads->nr; ++i)
293		printed += fprintf(fp, "%s%d", i ? ", " : "", threads->map[i]);
294
295	return printed + fprintf(fp, "\n");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
296}