Commit d5901de3 authored by Jim Harris's avatar Jim Harris
Browse files

spdk_trace: default on Linux to newest trace file in /dev/shm



It is kind of a pain to have to *always* have to specify the filename
or app parameters to spdk_trace, especially when you have the app currently
running or the app just exited.

So use ntfw() to walk the files in /dev/shm when -f and -s are not specified,
and use the newest file (assuming one is found).

A few caveats:
1) This only works on Linux, mainly because FreeBSD does not have a /dev/shm
   mount for shm files.
2) Search /dev/shm is not foolproof, since we don't know for sure whether a
   file there is an SPDK trace file. But looking for the "_trace." substring
   should be enough. It should guard against trying to use other non-trace
   files.

Signed-off-by: default avatarJim Harris <jim.harris@samsung.com>
Change-Id: If130e6f5918a228b6f9d4576e16d37e05bfc4995
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/22104


Reviewed-by: default avatarKonrad Sztyber <konrad.sztyber@intel.com>
Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Community-CI: Mellanox Build Bot
Reviewed-by: default avatarBen Walker <ben@nvidia.com>
parent a5156f64
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -36,6 +36,12 @@ Added support for namespace masking using new C APIs `spdk_nvmf_ns_add_host` and
Users must pass new `--no-auto-visible` parameter to `nvmf_subsystem_add_ns` RPC to allow
namespace masking to be controlled by these new RPCs.

### spdk_trace

`spdk_trace` has learned how to use the most recent trace file in /dev/shm when
the user has specified neither `-f` nor `-s` options. This is only available on
Linux since FreeBSD does not mount have a /dev/shm mount.

## v24.01

### accel
+32 −0
Original line number Diff line number Diff line
@@ -340,9 +340,29 @@ usage(void)
	fprintf(stderr, "                       -i or -p must be specified)\n");
	fprintf(stderr, "                 '-f' to specify a tracepoint file name\n");
	fprintf(stderr, "                      (-s and -f are mutually exclusive)\n");
#if defined(__linux__)
	fprintf(stderr, "                 Without -s or -f, %s will look for\n", g_exe_name);
	fprintf(stderr, "                      newest trace file in /dev/shm\n");
#endif
	fprintf(stderr, "                 '-j' to use JSON to format the output\n");
}

#if defined(__linux__)
static time_t g_mtime = 0;
static char g_newest_file[PATH_MAX] = {};

static int
get_newest(const char *path, const struct stat *sb, int tflag, struct FTW *ftw)
{
	if (tflag == FTW_F && sb->st_mtime > g_mtime &&
	    strstr(path, SPDK_TRACE_SHM_NAME_BASE) != NULL) {
		g_mtime = sb->st_mtime;
		strncpy(g_newest_file, path, PATH_MAX - 1);
	}
	return 0;
}
#endif

int
main(int argc, char **argv)
{
@@ -400,9 +420,21 @@ main(int argc, char **argv)
	}

	if (file_name == NULL && app_name == NULL) {
#if defined(__linux__)
		nftw("/dev/shm", get_newest, 1, 0);
		if (strlen(g_newest_file) > 0) {
			file_name = g_newest_file;
			printf("Using newest trace file found: %s\n", file_name);
		} else {
			fprintf(stderr, "No shm file found and -f not specified\n");
			usage();
			exit(1);
		}
#else
		fprintf(stderr, "One of -f and -s must be specified\n");
		usage();
		exit(1);
#endif
	}

	if (json) {
+1 −0
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ extern "C" {
#include <dirent.h>
#include <fcntl.h>
#include <fnmatch.h>
#include <ftw.h>
#include <glob.h>
#include <ifaddrs.h>
#include <libgen.h>
+2 −0
Original line number Diff line number Diff line
@@ -524,6 +524,8 @@ app_setup_trace(struct spdk_app_opts *opts)
			       opts->shm_id >= 0 ? "-i" : "-p",
			       opts->shm_id >= 0 ? opts->shm_id : getpid());
#if defined(__linux__)
		SPDK_NOTICELOG("'spdk_trace' without parameters will also work if this is the only\n");
		SPDK_NOTICELOG("SPDK application currently running.\n");
		SPDK_NOTICELOG("Or copy /dev/shm%s for offline analysis/debug.\n", shm_name);
#endif
	}