Allocate only the required threads

This commit is contained in:
Rodrigo Arias 2021-11-18 16:20:20 +01:00
parent bed8c35980
commit ffd492e922
2 changed files with 47 additions and 14 deletions

2
emu.h
View File

@ -305,7 +305,7 @@ struct ovni_eproc {
/* Threads */ /* Threads */
size_t nthreads; size_t nthreads;
struct ovni_ethread thread[OVNI_MAX_THR]; struct ovni_ethread *thread;
JSON_Value *meta; JSON_Value *meta;

59
trace.c
View File

@ -117,6 +117,7 @@ load_proc(struct ovni_eproc *proc, struct ovni_loom *loom, int index, int pid, c
char path[PATH_MAX]; char path[PATH_MAX];
struct ovni_ethread *thread; struct ovni_ethread *thread;
int tid; int tid;
size_t i;
proc->pid = pid; proc->pid = pid;
proc->index = index; proc->index = index;
@ -147,6 +148,26 @@ load_proc(struct ovni_eproc *proc, struct ovni_loom *loom, int index, int pid, c
return -1; return -1;
} }
proc->nthreads = count_dir_prefix(dir, "thread");
if(proc->nthreads <= 0)
{
err("cannot find any thread for process %d\n",
proc->pid);
return -1;
}
proc->thread = calloc(proc->nthreads, sizeof(struct ovni_ethread));
if(proc->thread == NULL)
{
perror("calloc failed");
return -1;
}
rewinddir(dir);
i = 0;
while((dirent = readdir(dir)) != NULL) while((dirent = readdir(dir)) != NULL)
{ {
if(find_dir_prefix_int(dirent, "thread", &tid) != 0) if(find_dir_prefix_int(dirent, "thread", &tid) != 0)
@ -159,31 +180,24 @@ load_proc(struct ovni_eproc *proc, struct ovni_loom *loom, int index, int pid, c
abort(); abort();
} }
if(proc->nthreads >= OVNI_MAX_THR) thread = &proc->thread[i];
{
err("too many thread streams for process %d\n", pid);
abort();
}
thread = &proc->thread[proc->nthreads]; if(load_thread(thread, proc, i, tid, path) != 0)
if(load_thread(thread, proc, proc->nthreads, tid, path) != 0)
return -1; return -1;
proc->nthreads++; i++;
} }
closedir(dir); closedir(dir);
return 0; return 0;
} }
static int static int
load_loom(struct ovni_loom *loom, char *loomdir) load_loom(struct ovni_loom *loom, char *loomdir)
{ {
int pid, i; int pid;
size_t i;
char path[PATH_MAX]; char path[PATH_MAX];
DIR *dir; DIR *dir;
struct dirent *dirent; struct dirent *dirent;
@ -222,6 +236,12 @@ load_loom(struct ovni_loom *loom, char *loomdir)
sprintf(path, "%s/%s", loomdir, dirent->d_name); sprintf(path, "%s/%s", loomdir, dirent->d_name);
if(i >= loom->nprocs)
{
err("more process than expected\n");
abort();
}
if(load_proc(&loom->proc[i], loom, i, pid, path) != 0) if(load_proc(&loom->proc[i], loom, i, pid, path) != 0)
return -1; return -1;
@ -229,6 +249,12 @@ load_loom(struct ovni_loom *loom, char *loomdir)
} }
if(i != loom->nprocs)
{
err("unexpected number of processes\n");
abort();
}
closedir(dir); closedir(dir);
return 0; return 0;
@ -470,10 +496,17 @@ ovni_free_streams(struct ovni_trace *trace)
void void
ovni_free_trace(struct ovni_trace *trace) ovni_free_trace(struct ovni_trace *trace)
{ {
size_t i; size_t i, j;
for(i=0; i<trace->nlooms; i++) for(i=0; i<trace->nlooms; i++)
{
for(j=0; j<trace->loom[i].nprocs; j++)
{
free(trace->loom[i].proc[j].thread);
}
free(trace->loom[i].proc); free(trace->loom[i].proc);
}
free(trace->loom); free(trace->loom);
} }