Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ftrace_log: Add override for tracefs location #126

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/tutorial.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ The category names currently supported are:

Default value is "none".

* ftracedir : String. Mountpoint of tracefs. Defaults to
"/sys/kernel/debug/tracing" if not specified. Has no effect if ftrace is not
enabled.

* gnuplot : Boolean. If True, it will create a gnu plot compatible file for
each threads (see gnuplot section for more details). Default value is False.

Expand Down
9 changes: 4 additions & 5 deletions src/rt-app.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ static pthread_mutex_t joining_mutex;
static pthread_mutex_t fork_mutex;

static ftrace_data_t ft_data = {
.debugfs = "/sys/kernel/debug",
.marker_fd = -1,
};

Expand Down Expand Up @@ -1542,8 +1541,8 @@ int main(int argc, char* argv[])
if (ftrace_level != FTRACE_NONE) {
log_notice("configuring ftrace");
// check if tracing is enabled
strcpy(tmp, ft_data.debugfs);
strcat(tmp, "/tracing/tracing_on");
strcpy(tmp, opts.ftracedir);
strcat(tmp, "/tracing_on");
int ftrace_f = open(tmp, O_RDONLY);
if (ftrace_f < 0){
log_error("Cannot open tracing_on file %s", tmp);
Expand All @@ -1557,8 +1556,8 @@ int main(int argc, char* argv[])
}
close(ftrace_f);
// set the marker
strcpy(tmp, ft_data.debugfs);
strcat(tmp, "/tracing/trace_marker");
strcpy(tmp, opts.ftracedir);
strcat(tmp, "/trace_marker");
ft_data.marker_fd = open(tmp, O_WRONLY);
if (ft_data.marker_fd < 0) {
log_error("Cannot open trace_marker file %s", tmp);
Expand Down
2 changes: 2 additions & 0 deletions src/rt-app_parse_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,7 @@ parse_global(struct json_object *global, rtapp_options_t *opts)
opts->io_device = strdup("/dev/null");
opts->mem_buffer_size = DEFAULT_MEM_BUF_SIZE;
opts->cumulative_slack = 0;
opts->ftracedir = ftrace_dir();
return;
}

Expand Down Expand Up @@ -1304,6 +1305,7 @@ parse_global(struct json_object *global, rtapp_options_t *opts)
log_critical(PFX "Invalid ftrace categories");
exit(EXIT_INV_CONFIG);
}
opts->ftracedir = get_string_value_from(global, "ftracedir", TRUE, ftrace_dir());

opts->lock_pages = get_bool_value_from(global, "lock_pages", TRUE, 1);
opts->pi_enabled = get_bool_value_from(global, "pi_enabled", TRUE, 0);
Expand Down
2 changes: 2 additions & 0 deletions src/rt-app_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ typedef struct _rtapp_options_t {
char *io_device;

int cumulative_slack;

char *ftracedir; /* should end in 'tracing' */
} rtapp_options_t;

typedef struct _timing_point_t {
Expand Down
55 changes: 55 additions & 0 deletions src/rt-app_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include <errno.h>
#include <math.h>
#include <stdarg.h>
#include <mntent.h>

#include "rt-app_utils.h"

Expand Down Expand Up @@ -267,6 +268,60 @@ resource_to_string(resource_t resource, char *resource_name)
return 0;
}

/* point directly to the tracing directory */
#define DEFAULT_FTRACE_DIR "/sys/kernel/debug/tracing"

char *mtab_by_type(const char *type, int typelen)
{
FILE *fd_mntent;

fd_mntent = setmntent("/etc/mtab", "r");
if(fd_mntent)
{
struct mntent *mntent;

while(mntent = getmntent(fd_mntent), mntent)
{
log_debug("mntent->dir=%s type=%s", mntent->mnt_dir, mntent->mnt_type);
if(!strncmp(type, mntent->mnt_type, typelen))
{
char *path = strdup(mntent->mnt_dir);
endmntent(fd_mntent);
return path;
}
}
endmntent(fd_mntent);
}
return NULL;
}

char *ftrace_dir(void)
{
char *path;

/* tracefs paths point directly to the tracing directory */
path = mtab_by_type("tracefs", sizeof("tracefs")-1);
if(path)
return path;

/* debugfs paths point one level above the tracing directory */
path = mtab_by_type("debugfs", sizeof("debugfs")-1);
if(path)
{
char *tmp;
int len = strlen(path) + sizeof("/tracing");
tmp = malloc(len);
if(!tmp)
return NULL;
strcpy(tmp, path);
free(path);
strcat(tmp, "/tracing");
return tmp;
}

return strdup(DEFAULT_FTRACE_DIR);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we really need to return a default path ? there is no chance that this can work, isn't ?

Should be better return null and escape early in the main thread

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I return NULL here, it means that if the mtab code fails we cannot use ftrace unless we provide a path in the global section. That's probably OK, I don't mind that if you dont.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wondering if there is a usecase where mtab would fail but the default path could work

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, and if that does happen you can always add ftracedir to your global options file to force it to a specific location. I'll push a new squashed rev up once I've tested it a bit.

}

int ftrace_setup(char *categories)
{
char *cat = strtok(categories, ",");
Expand Down
4 changes: 4 additions & 0 deletions src/rt-app_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ string_to_resource(const char *name, resource_t *resource);
int
resource_to_string(resource_t resource, char *name);

/* ftrace_dir always returns a string on the heap */
char *
ftrace_dir(void);

int
ftrace_setup(char *categories);

Expand Down