Skip to content

Commit

Permalink
trig_utils: add simple tstats_write_file helper
Browse files Browse the repository at this point in the history
Signed-off-by: Markus Klotzbuecher <mk@mkio.de>
  • Loading branch information
kmarkus committed Apr 23, 2020
1 parent 950c8ff commit e191d22
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 32 deletions.
124 changes: 92 additions & 32 deletions libubx/trig_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,58 +237,120 @@ void trig_info_tstats_log(ubx_block_t *b, struct trig_info *trig_inf)
}
}

/**
* write all stats to file
*/
int trig_info_tstats_write(ubx_block_t *b,
struct trig_info *trig_inf,
const char *profile_path)
char* tstats_build_filename(const char *blockname, const char *profile_path)
{
int ret=0, len;
char *blockname=NULL, *filename=NULL;
FILE *fp;
int len, ret;
char *bn = NULL;
char *filename = NULL;

if (profile_path == NULL)
goto out;
bn = strdup(blockname);

if (trig_inf->tstats_mode == TSTATS_DISABLED)
if (!bn)
goto out;

blockname = strdup(trig_inf->global_tstats.block_name);

if (!blockname) {
ubx_err(b, "%s: EOUTOFMEM", __func__);
ret = EOUTOFMEM;
goto out;
}

/* sanitize filename */
char_replace(blockname, '/', '-');
char_replace(bn, '/', '-');

/* the + 1 is for the '/' */
len = strlen(profile_path) + 1 + strlen(blockname) + strlen(".tstats");
len = strlen(profile_path) + 1 + strlen(bn) + strlen(".tstats");

filename = malloc(len+1);

if (!filename) {
ubx_err(b, "%s: EOUTOFMEM 2", __func__);
ret = EOUTOFMEM;
if (filename == NULL) {
goto out_free;
}

ret = snprintf(filename, len, "%s/%s.tstats",
profile_path, blockname);
ret = snprintf(filename, len, "%s/%s.tstats", profile_path, bn);

if (ret < 0) {
ubx_err(b, "%s: failed to build filepath", __func__);
if (ret < 0)
goto out_err;

/* all good */
goto out_free;

out_err:
free(filename);
filename = NULL;
out_free:
free(bn);
out:
return filename;
}

int tstat_write_file(ubx_block_t *b,
struct ubx_tstat *tstats,
const char *profile_path)
{
int ret=-1;
char *filename=NULL;
FILE *fp;

if (profile_path == NULL) {
ubx_err(b, "%s: profile_path is NULL", __func__);
goto out;
}

filename = tstats_build_filename(tstats->block_name, profile_path);

if (filename == NULL) {
ubx_err(b, "%s: failed to build filename for %s",
__func__, tstats->block_name);
goto out;
}

fp = fopen(filename, "w");

if (fp == NULL) {
ubx_err(b, "%s: opening %s failed", __func__, filename);
goto out_free;
}

fprintf(fp, FILE_HDR);

tstat_write(fp, tstats);

ubx_info(b, "wrote tstats_profile to %s", filename);
ret = 0;

out_free:
free(filename);
out:
return ret;
}

/**
* write all stats to file
*/
int trig_info_tstats_write(ubx_block_t *b,
struct trig_info *trig_inf,
const char *profile_path)
{
int ret=-1;
char *filename=NULL;
FILE *fp;

if (profile_path == NULL) {
ubx_err(b, "%s: profile_path is NULL", __func__);
goto out;
}

if (trig_inf->tstats_mode == TSTATS_DISABLED) {
ret = 0;
goto out;
}

filename = tstats_build_filename(trig_inf->global_tstats.block_name, profile_path);

if (filename == NULL) {
ubx_err(b, "%s: failed to build filename for %s",
__func__, trig_inf->global_tstats.block_name);
goto out;
}

fp = fopen(filename, "w");

if (fp == NULL) {
ubx_err(b, "%s: opening %s failed", __func__, filename);
ret = -1;
goto out_free;
}

Expand All @@ -312,9 +374,7 @@ int trig_info_tstats_write(ubx_block_t *b,
ubx_info(b, "wrote tstats_profile to %s", filename);
ret = 0;


out_free:
free(blockname);
free(filename);
out:
return ret;
Expand Down
21 changes: 21 additions & 0 deletions libubx/trig_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ int do_trigger(struct trig_info* trig_inf);
void trig_info_tstats_log(ubx_block_t *b, struct trig_info *trig_inf);


/**
* construct the tstats log file name
* @param blockname of trigger block
* @param profile path
* @return filename (must be freed by called!)
*/
char* tstats_build_filename(const char *blockname,
const char *profile_path);

/**
* write all tstats to file named "block.tstats"
* @param b parent block for logging
Expand All @@ -120,6 +129,18 @@ int trig_info_tstats_write(ubx_block_t *b,
struct trig_info *trig_inf,
const char *profile_path);


/**
* write a single tstat to a file
* @param b block for logging
* @param tstats
* @param profile_path
* @return - if sucess, <0 otherwise
*/
int tstat_write_file(ubx_block_t *b,
struct ubx_tstat *tstats,
const char *profile_path);

/*
* helpers to manager timing statistics
*/
Expand Down

0 comments on commit e191d22

Please sign in to comment.