Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <dirent.h>
0003 #include <errno.h>
0004 #include <limits.h>
0005 #include <stdbool.h>
0006 #include <stdlib.h>
0007 #include <stdio.h>
0008 #include <sys/types.h>
0009 #include <sys/stat.h>
0010 #include <unistd.h>
0011 #include "string2.h"
0012 #include "strlist.h"
0013 #include <string.h>
0014 #include <api/fs/fs.h>
0015 #include <linux/string.h>
0016 #include <linux/zalloc.h>
0017 #include "asm/bug.h"
0018 #include "thread_map.h"
0019 #include "debug.h"
0020 #include "event.h"
0021 
0022 /* Skip "." and ".." directories */
0023 static int filter(const struct dirent *dir)
0024 {
0025     if (dir->d_name[0] == '.')
0026         return 0;
0027     else
0028         return 1;
0029 }
0030 
0031 #define thread_map__alloc(__nr) perf_thread_map__realloc(NULL, __nr)
0032 
0033 struct perf_thread_map *thread_map__new_by_pid(pid_t pid)
0034 {
0035     struct perf_thread_map *threads;
0036     char name[256];
0037     int items;
0038     struct dirent **namelist = NULL;
0039     int i;
0040 
0041     sprintf(name, "/proc/%d/task", pid);
0042     items = scandir(name, &namelist, filter, NULL);
0043     if (items <= 0)
0044         return NULL;
0045 
0046     threads = thread_map__alloc(items);
0047     if (threads != NULL) {
0048         for (i = 0; i < items; i++)
0049             perf_thread_map__set_pid(threads, i, atoi(namelist[i]->d_name));
0050         threads->nr = items;
0051         refcount_set(&threads->refcnt, 1);
0052     }
0053 
0054     for (i=0; i<items; i++)
0055         zfree(&namelist[i]);
0056     free(namelist);
0057 
0058     return threads;
0059 }
0060 
0061 struct perf_thread_map *thread_map__new_by_tid(pid_t tid)
0062 {
0063     struct perf_thread_map *threads = thread_map__alloc(1);
0064 
0065     if (threads != NULL) {
0066         perf_thread_map__set_pid(threads, 0, tid);
0067         threads->nr = 1;
0068         refcount_set(&threads->refcnt, 1);
0069     }
0070 
0071     return threads;
0072 }
0073 
0074 static struct perf_thread_map *__thread_map__new_all_cpus(uid_t uid)
0075 {
0076     DIR *proc;
0077     int max_threads = 32, items, i;
0078     char path[NAME_MAX + 1 + 6];
0079     struct dirent *dirent, **namelist = NULL;
0080     struct perf_thread_map *threads = thread_map__alloc(max_threads);
0081 
0082     if (threads == NULL)
0083         goto out;
0084 
0085     proc = opendir("/proc");
0086     if (proc == NULL)
0087         goto out_free_threads;
0088 
0089     threads->nr = 0;
0090     refcount_set(&threads->refcnt, 1);
0091 
0092     while ((dirent = readdir(proc)) != NULL) {
0093         char *end;
0094         bool grow = false;
0095         pid_t pid = strtol(dirent->d_name, &end, 10);
0096 
0097         if (*end) /* only interested in proper numerical dirents */
0098             continue;
0099 
0100         snprintf(path, sizeof(path), "/proc/%s", dirent->d_name);
0101 
0102         if (uid != UINT_MAX) {
0103             struct stat st;
0104 
0105             if (stat(path, &st) != 0 || st.st_uid != uid)
0106                 continue;
0107         }
0108 
0109         snprintf(path, sizeof(path), "/proc/%d/task", pid);
0110         items = scandir(path, &namelist, filter, NULL);
0111         if (items <= 0)
0112             goto out_free_closedir;
0113 
0114         while (threads->nr + items >= max_threads) {
0115             max_threads *= 2;
0116             grow = true;
0117         }
0118 
0119         if (grow) {
0120             struct perf_thread_map *tmp;
0121 
0122             tmp = perf_thread_map__realloc(threads, max_threads);
0123             if (tmp == NULL)
0124                 goto out_free_namelist;
0125 
0126             threads = tmp;
0127         }
0128 
0129         for (i = 0; i < items; i++) {
0130             perf_thread_map__set_pid(threads, threads->nr + i,
0131                             atoi(namelist[i]->d_name));
0132         }
0133 
0134         for (i = 0; i < items; i++)
0135             zfree(&namelist[i]);
0136         free(namelist);
0137 
0138         threads->nr += items;
0139     }
0140 
0141 out_closedir:
0142     closedir(proc);
0143 out:
0144     return threads;
0145 
0146 out_free_threads:
0147     free(threads);
0148     return NULL;
0149 
0150 out_free_namelist:
0151     for (i = 0; i < items; i++)
0152         zfree(&namelist[i]);
0153     free(namelist);
0154 
0155 out_free_closedir:
0156     zfree(&threads);
0157     goto out_closedir;
0158 }
0159 
0160 struct perf_thread_map *thread_map__new_all_cpus(void)
0161 {
0162     return __thread_map__new_all_cpus(UINT_MAX);
0163 }
0164 
0165 struct perf_thread_map *thread_map__new_by_uid(uid_t uid)
0166 {
0167     return __thread_map__new_all_cpus(uid);
0168 }
0169 
0170 struct perf_thread_map *thread_map__new(pid_t pid, pid_t tid, uid_t uid)
0171 {
0172     if (pid != -1)
0173         return thread_map__new_by_pid(pid);
0174 
0175     if (tid == -1 && uid != UINT_MAX)
0176         return thread_map__new_by_uid(uid);
0177 
0178     return thread_map__new_by_tid(tid);
0179 }
0180 
0181 static struct perf_thread_map *thread_map__new_by_pid_str(const char *pid_str)
0182 {
0183     struct perf_thread_map *threads = NULL, *nt;
0184     char name[256];
0185     int items, total_tasks = 0;
0186     struct dirent **namelist = NULL;
0187     int i, j = 0;
0188     pid_t pid, prev_pid = INT_MAX;
0189     char *end_ptr;
0190     struct str_node *pos;
0191     struct strlist_config slist_config = { .dont_dupstr = true, };
0192     struct strlist *slist = strlist__new(pid_str, &slist_config);
0193 
0194     if (!slist)
0195         return NULL;
0196 
0197     strlist__for_each_entry(pos, slist) {
0198         pid = strtol(pos->s, &end_ptr, 10);
0199 
0200         if (pid == INT_MIN || pid == INT_MAX ||
0201             (*end_ptr != '\0' && *end_ptr != ','))
0202             goto out_free_threads;
0203 
0204         if (pid == prev_pid)
0205             continue;
0206 
0207         sprintf(name, "/proc/%d/task", pid);
0208         items = scandir(name, &namelist, filter, NULL);
0209         if (items <= 0)
0210             goto out_free_threads;
0211 
0212         total_tasks += items;
0213         nt = perf_thread_map__realloc(threads, total_tasks);
0214         if (nt == NULL)
0215             goto out_free_namelist;
0216 
0217         threads = nt;
0218 
0219         for (i = 0; i < items; i++) {
0220             perf_thread_map__set_pid(threads, j++, atoi(namelist[i]->d_name));
0221             zfree(&namelist[i]);
0222         }
0223         threads->nr = total_tasks;
0224         free(namelist);
0225     }
0226 
0227 out:
0228     strlist__delete(slist);
0229     if (threads)
0230         refcount_set(&threads->refcnt, 1);
0231     return threads;
0232 
0233 out_free_namelist:
0234     for (i = 0; i < items; i++)
0235         zfree(&namelist[i]);
0236     free(namelist);
0237 
0238 out_free_threads:
0239     zfree(&threads);
0240     goto out;
0241 }
0242 
0243 struct perf_thread_map *thread_map__new_by_tid_str(const char *tid_str)
0244 {
0245     struct perf_thread_map *threads = NULL, *nt;
0246     int ntasks = 0;
0247     pid_t tid, prev_tid = INT_MAX;
0248     char *end_ptr;
0249     struct str_node *pos;
0250     struct strlist_config slist_config = { .dont_dupstr = true, };
0251     struct strlist *slist;
0252 
0253     /* perf-stat expects threads to be generated even if tid not given */
0254     if (!tid_str)
0255         return perf_thread_map__new_dummy();
0256 
0257     slist = strlist__new(tid_str, &slist_config);
0258     if (!slist)
0259         return NULL;
0260 
0261     strlist__for_each_entry(pos, slist) {
0262         tid = strtol(pos->s, &end_ptr, 10);
0263 
0264         if (tid == INT_MIN || tid == INT_MAX ||
0265             (*end_ptr != '\0' && *end_ptr != ','))
0266             goto out_free_threads;
0267 
0268         if (tid == prev_tid)
0269             continue;
0270 
0271         ntasks++;
0272         nt = perf_thread_map__realloc(threads, ntasks);
0273 
0274         if (nt == NULL)
0275             goto out_free_threads;
0276 
0277         threads = nt;
0278         perf_thread_map__set_pid(threads, ntasks - 1, tid);
0279         threads->nr = ntasks;
0280     }
0281 out:
0282     if (threads)
0283         refcount_set(&threads->refcnt, 1);
0284     return threads;
0285 
0286 out_free_threads:
0287     zfree(&threads);
0288     strlist__delete(slist);
0289     goto out;
0290 }
0291 
0292 struct perf_thread_map *thread_map__new_str(const char *pid, const char *tid,
0293                        uid_t uid, bool all_threads)
0294 {
0295     if (pid)
0296         return thread_map__new_by_pid_str(pid);
0297 
0298     if (!tid && uid != UINT_MAX)
0299         return thread_map__new_by_uid(uid);
0300 
0301     if (all_threads)
0302         return thread_map__new_all_cpus();
0303 
0304     return thread_map__new_by_tid_str(tid);
0305 }
0306 
0307 size_t thread_map__fprintf(struct perf_thread_map *threads, FILE *fp)
0308 {
0309     int i;
0310     size_t printed = fprintf(fp, "%d thread%s: ",
0311                  threads->nr, threads->nr > 1 ? "s" : "");
0312     for (i = 0; i < threads->nr; ++i)
0313         printed += fprintf(fp, "%s%d", i ? ", " : "", perf_thread_map__pid(threads, i));
0314 
0315     return printed + fprintf(fp, "\n");
0316 }
0317 
0318 static int get_comm(char **comm, pid_t pid)
0319 {
0320     char *path;
0321     size_t size;
0322     int err;
0323 
0324     if (asprintf(&path, "%s/%d/comm", procfs__mountpoint(), pid) == -1)
0325         return -ENOMEM;
0326 
0327     err = filename__read_str(path, comm, &size);
0328     if (!err) {
0329         /*
0330          * We're reading 16 bytes, while filename__read_str
0331          * allocates data per BUFSIZ bytes, so we can safely
0332          * mark the end of the string.
0333          */
0334         (*comm)[size] = 0;
0335         strim(*comm);
0336     }
0337 
0338     free(path);
0339     return err;
0340 }
0341 
0342 static void comm_init(struct perf_thread_map *map, int i)
0343 {
0344     pid_t pid = perf_thread_map__pid(map, i);
0345     char *comm = NULL;
0346 
0347     /* dummy pid comm initialization */
0348     if (pid == -1) {
0349         map->map[i].comm = strdup("dummy");
0350         return;
0351     }
0352 
0353     /*
0354      * The comm name is like extra bonus ;-),
0355      * so just warn if we fail for any reason.
0356      */
0357     if (get_comm(&comm, pid))
0358         pr_warning("Couldn't resolve comm name for pid %d\n", pid);
0359 
0360     map->map[i].comm = comm;
0361 }
0362 
0363 void thread_map__read_comms(struct perf_thread_map *threads)
0364 {
0365     int i;
0366 
0367     for (i = 0; i < threads->nr; ++i)
0368         comm_init(threads, i);
0369 }
0370 
0371 static void thread_map__copy_event(struct perf_thread_map *threads,
0372                    struct perf_record_thread_map *event)
0373 {
0374     unsigned i;
0375 
0376     threads->nr = (int) event->nr;
0377 
0378     for (i = 0; i < event->nr; i++) {
0379         perf_thread_map__set_pid(threads, i, (pid_t) event->entries[i].pid);
0380         threads->map[i].comm = strndup(event->entries[i].comm, 16);
0381     }
0382 
0383     refcount_set(&threads->refcnt, 1);
0384 }
0385 
0386 struct perf_thread_map *thread_map__new_event(struct perf_record_thread_map *event)
0387 {
0388     struct perf_thread_map *threads;
0389 
0390     threads = thread_map__alloc(event->nr);
0391     if (threads)
0392         thread_map__copy_event(threads, event);
0393 
0394     return threads;
0395 }
0396 
0397 bool thread_map__has(struct perf_thread_map *threads, pid_t pid)
0398 {
0399     int i;
0400 
0401     for (i = 0; i < threads->nr; ++i) {
0402         if (threads->map[i].pid == pid)
0403             return true;
0404     }
0405 
0406     return false;
0407 }
0408 
0409 int thread_map__remove(struct perf_thread_map *threads, int idx)
0410 {
0411     int i;
0412 
0413     if (threads->nr < 1)
0414         return -EINVAL;
0415 
0416     if (idx >= threads->nr)
0417         return -EINVAL;
0418 
0419     /*
0420      * Free the 'idx' item and shift the rest up.
0421      */
0422     zfree(&threads->map[idx].comm);
0423 
0424     for (i = idx; i < threads->nr - 1; i++)
0425         threads->map[i] = threads->map[i + 1];
0426 
0427     threads->nr--;
0428     return 0;
0429 }