Skip to content

Commit

Permalink
ftrace: Autodetect location of tracing folder
Browse files Browse the repository at this point in the history
If you don't provide a tracing location in the global config key section,
rt-app usually looks in /sys/kernel/debug for the tracing folder.

As part of this, switch the internals to use the full tracing path
instead of relying upon adding /tracing/ to the path of files used.

This patch allows rt-app to examine the content of /etc/mtab, and apply
the following ordering:

* If tracefs is mounted, use that location
* If tracefs is not found anywhere, then look for debugfs
  - If we use debugfs, append "/tracing" to the path
* If we find neither, just use /sys/kernel/debug/tracing

Using the default means we couldn't find tracefs or debugfs mounted
and so is likely to fail later if you are using ftrace, but should have
no impact if you're not tracing.

All of the path selection is overridden if you provide the config key.

This should allow rt-app to work on any platform, at least from tracing
POV.

Signed-off-by: Chris Redpath <[email protected]>
  • Loading branch information
credp committed Jul 14, 2022
1 parent c2c95d2 commit 463123a
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/rt-app.c
Original file line number Diff line number Diff line change
Expand Up @@ -1542,7 +1542,7 @@ int main(int argc, char* argv[])
log_notice("configuring ftrace");
// check if tracing is enabled
strcpy(tmp, opts.ftracedir);
strcat(tmp, "/tracing/tracing_on");
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,7 +1557,7 @@ int main(int argc, char* argv[])
close(ftrace_f);
// set the marker
strcpy(tmp, opts.ftracedir);
strcat(tmp, "/tracing/trace_marker");
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
6 changes: 2 additions & 4 deletions src/rt-app_parse_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#define JSON_FILE_BUF_SIZE 4096
#define DEFAULT_MEM_BUF_SIZE (4 * 1024 * 1024)

#define DEFAULT_FTRACE_DIR "/sys/kernel/debug"

#ifndef TRUE
#define TRUE true
#define FALSE false
Expand Down Expand Up @@ -1209,7 +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 = strdup(DEFAULT_FTRACE_DIR);
opts->ftracedir = ftrace_dir();
return;
}

Expand Down Expand Up @@ -1307,7 +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, DEFAULT_FTRACE_DIR);
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: 1 addition & 1 deletion src/rt-app_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ typedef struct _rtapp_options_t {

int cumulative_slack;

char *ftracedir;
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);
}

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

0 comments on commit 463123a

Please sign in to comment.