Allocate only the needed looms

This commit is contained in:
Rodrigo Arias 2021-11-18 14:05:19 +01:00
parent d54c287ec8
commit 25cf3a8585
3 changed files with 76 additions and 28 deletions

2
emu.h
View File

@ -395,7 +395,7 @@ struct ovni_loom {
struct ovni_trace { struct ovni_trace {
size_t nlooms; size_t nlooms;
struct ovni_loom loom[OVNI_MAX_LOOM]; struct ovni_loom *loom;
size_t nstreams; size_t nstreams;
struct ovni_stream *stream; struct ovni_stream *stream;

1
ovni.h
View File

@ -43,7 +43,6 @@ typedef struct json_value_t JSON_Value;
#define OVNI_MAX_CPU 256 #define OVNI_MAX_CPU 256
#define OVNI_MAX_PROC 256 #define OVNI_MAX_PROC 256
#define OVNI_MAX_THR 256 #define OVNI_MAX_THR 256
#define OVNI_MAX_LOOM 4
#define OVNI_TRACEDIR "ovni" #define OVNI_TRACEDIR "ovni"
#define OVNI_MAX_HOSTNAME 512 #define OVNI_MAX_HOSTNAME 512

103
trace.c
View File

@ -208,14 +208,14 @@ load_loom(struct ovni_loom *loom, int loomid, char *loomdir)
static int static int
compare_alph(const void *a, const void *b) compare_alph(const void *a, const void *b)
{ {
return strcmp(*(const char**)a, *(const char**)b); return strcmp((const char *)a, (const char *)b);
} }
int int
ovni_load_trace(struct ovni_trace *trace, char *tracedir) ovni_load_trace(struct ovni_trace *trace, char *tracedir)
{ {
char *looms[OVNI_MAX_LOOM]; char (*looms)[PATH_MAX];
char *hosts[OVNI_MAX_LOOM]; char (*hosts)[PATH_MAX];
const char *loom_name; const char *loom_name;
DIR *dir; DIR *dir;
struct dirent *dirent; struct dirent *dirent;
@ -225,17 +225,60 @@ ovni_load_trace(struct ovni_trace *trace, char *tracedir)
if((dir = opendir(tracedir)) == NULL) if((dir = opendir(tracedir)) == NULL)
{ {
fprintf(stderr, "opendir %s failed: %s\n", err("opendir %s failed: %s\n", tracedir, strerror(errno));
tracedir, strerror(errno));
return -1; return -1;
} }
for(l=0; l<OVNI_MAX_LOOM; l++) /* Find how many looms we have */
while((dirent = readdir(dir)) != NULL)
{ {
looms[l] = malloc(PATH_MAX); if(find_dir_prefix_str(dirent, "loom", &loom_name) != 0)
hosts[l] = malloc(PATH_MAX); {
/* Ignore other files in tracedir */
continue;
} }
trace->nlooms++;
}
closedir(dir);
if(trace->nlooms == 0)
{
err("cannot find any loom in %s\n", tracedir);
return -1;
}
/* Then allocate the loom array */
trace->loom = calloc(trace->nlooms, sizeof(struct ovni_loom));
if(trace->loom == NULL)
{
perror("calloc failed\n");
return -1;
}
if((looms = calloc(trace->nlooms, PATH_MAX)) == NULL)
{
perror("calloc failed\n");
return -1;
}
if((hosts = calloc(trace->nlooms, PATH_MAX)) == NULL)
{
perror("calloc failed\n");
return -1;
}
/* Read again the directory */
if((dir = opendir(tracedir)) == NULL)
{
err("opendir %s failed: %s\n", tracedir, strerror(errno));
return -1;
}
l = 0;
while((dirent = readdir(dir)) != NULL) while((dirent = readdir(dir)) != NULL)
{ {
if(find_dir_prefix_str(dirent, "loom", &loom_name) != 0) if(find_dir_prefix_str(dirent, "loom", &loom_name) != 0)
@ -244,29 +287,40 @@ ovni_load_trace(struct ovni_trace *trace, char *tracedir)
continue; continue;
} }
if(trace->nlooms >= OVNI_MAX_LOOM) if(l >= trace->nlooms)
{ {
err("too many looms for trace %s\n", err("extra loom detected\n");
tracedir); return -1;
abort();
} }
sprintf(hosts[trace->nlooms], "%s", loom_name); if(snprintf(hosts[l], PATH_MAX, "%s",
loom_name) >= PATH_MAX)
sprintf(looms[trace->nlooms], "%s/%s", tracedir, dirent->d_name); {
err("error: hostname %s too long\n", loom_name);
trace->nlooms++; return -1;
} }
qsort((const char **) hosts, trace->nlooms, sizeof(const char*), compare_alph); if(snprintf(looms[l], PATH_MAX, "%s/%s",
qsort((const char **) looms, trace->nlooms, sizeof(const char*), compare_alph); tracedir, dirent->d_name) >= PATH_MAX)
{
err("error: loom name %s too long\n", loom_name);
return -1;
}
l++;
}
closedir(dir);
qsort(hosts, trace->nlooms, PATH_MAX, compare_alph);
qsort(looms, trace->nlooms, PATH_MAX, compare_alph);
for(l=0; l<trace->nlooms; l++) for(l=0; l<trace->nlooms; l++)
{ {
if(strlen(hosts[l]) >= PATH_MAX) if(strlen(hosts[l]) >= PATH_MAX)
{ {
err("error hostname too long: %s\n", hosts[l]); err("error hostname too long: %s\n", hosts[l]);
exit(EXIT_FAILURE); return -1;
} }
/* Safe */ /* Safe */
@ -276,13 +330,8 @@ ovni_load_trace(struct ovni_trace *trace, char *tracedir)
return -1; return -1;
} }
closedir(dir); free(looms);
free(hosts);
for(l=0; l<OVNI_MAX_LOOM; l++)
{
free(looms[l]);
free(hosts[l]);
}
return 0; return 0;
} }