Linux Audio

Check our new training course

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