Home | History | Annotate | Download | only in util
      1 #include "../perf.h"
      2 #include <stdlib.h>
      3 #include <stdio.h>
      4 #include <string.h>
      5 #include "session.h"
      6 #include "thread.h"
      7 #include "util.h"
      8 #include "debug.h"
      9 
     10 struct thread *thread__new(pid_t pid, pid_t tid)
     11 {
     12 	struct thread *self = zalloc(sizeof(*self));
     13 
     14 	if (self != NULL) {
     15 		map_groups__init(&self->mg);
     16 		self->pid_ = pid;
     17 		self->tid = tid;
     18 		self->ppid = -1;
     19 		self->comm = malloc(32);
     20 		if (self->comm)
     21 			snprintf(self->comm, 32, ":%d", self->tid);
     22 	}
     23 
     24 	return self;
     25 }
     26 
     27 void thread__delete(struct thread *self)
     28 {
     29 	map_groups__exit(&self->mg);
     30 	free(self->comm);
     31 	free(self);
     32 }
     33 
     34 int thread__set_comm(struct thread *self, const char *comm)
     35 {
     36 	int err;
     37 
     38 	if (self->comm)
     39 		free(self->comm);
     40 	self->comm = strdup(comm);
     41 	err = self->comm == NULL ? -ENOMEM : 0;
     42 	if (!err) {
     43 		self->comm_set = true;
     44 	}
     45 	return err;
     46 }
     47 
     48 int thread__comm_len(struct thread *self)
     49 {
     50 	if (!self->comm_len) {
     51 		if (!self->comm)
     52 			return 0;
     53 		self->comm_len = strlen(self->comm);
     54 	}
     55 
     56 	return self->comm_len;
     57 }
     58 
     59 size_t thread__fprintf(struct thread *thread, FILE *fp)
     60 {
     61 	return fprintf(fp, "Thread %d %s\n", thread->tid, thread->comm) +
     62 	       map_groups__fprintf(&thread->mg, verbose, fp);
     63 }
     64 
     65 void thread__insert_map(struct thread *self, struct map *map)
     66 {
     67 	map_groups__fixup_overlappings(&self->mg, map, verbose, stderr);
     68 	map_groups__insert(&self->mg, map);
     69 }
     70 
     71 int thread__fork(struct thread *self, struct thread *parent)
     72 {
     73 	int i;
     74 
     75 	if (parent->comm_set) {
     76 		if (self->comm)
     77 			free(self->comm);
     78 		self->comm = strdup(parent->comm);
     79 		if (!self->comm)
     80 			return -ENOMEM;
     81 		self->comm_set = true;
     82 	}
     83 
     84 	for (i = 0; i < MAP__NR_TYPES; ++i)
     85 		if (map_groups__clone(&self->mg, &parent->mg, i) < 0)
     86 			return -ENOMEM;
     87 
     88 	self->ppid = parent->tid;
     89 
     90 	return 0;
     91 }
     92