Skip to content
Merged
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
2 changes: 1 addition & 1 deletion fabrics.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
#include "nvme-print.h"
#include "fabrics.h"
#include "util/cleanup.h"
#include "util/logging.h"
#include "logging.h"
#include "util/sighdl.h"

#define PATH_NVMF_DISC SYSCONFDIR "/nvme/discovery.conf"
Expand Down
65 changes: 30 additions & 35 deletions util/logging.c → logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
#include <ccan/endian/endian.h>

#include "logging.h"
#include "sighdl.h"
#include "util/sighdl.h"
#include "nvme-print.h"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I would like to avoid pulling in something from the parent directly into the util folder. Looking at what loggin.c does I suggest just move it to the parent folder.


struct submit_data {
struct timeval start;
struct timeval end;
};

int log_level;
static bool dry_run;
static struct submit_data sb;

int map_log_level(int verbose, bool quiet)
Expand Down Expand Up @@ -57,51 +57,46 @@ int map_log_level(int verbose, bool quiet)
return log_level;
}

void set_dry_run(bool enable)
{
dry_run = enable;
}

static void nvme_show_common(struct nvme_passthru_cmd *cmd)
{
printf("opcode : %02x\n", cmd->opcode);
printf("flags : %02x\n", cmd->flags);
printf("rsvd1 : %04x\n", cmd->rsvd1);
printf("nsid : %08x\n", cmd->nsid);
printf("cdw2 : %08x\n", cmd->cdw2);
printf("cdw3 : %08x\n", cmd->cdw3);
printf("data_len : %08x\n", cmd->data_len);
printf("metadata_len : %08x\n", cmd->metadata_len);
printf("addr : %"PRIx64"\n", (uint64_t)(uintptr_t)cmd->addr);
printf("metadata : %"PRIx64"\n", (uint64_t)(uintptr_t)cmd->metadata);
printf("cdw10 : %08x\n", cmd->cdw10);
printf("cdw11 : %08x\n", cmd->cdw11);
printf("cdw12 : %08x\n", cmd->cdw12);
printf("cdw13 : %08x\n", cmd->cdw13);
printf("cdw14 : %08x\n", cmd->cdw14);
printf("cdw15 : %08x\n", cmd->cdw15);
printf("timeout_ms : %08x\n", cmd->timeout_ms);
nvme_show_key_value("opcode ", "%02x", cmd->opcode);
nvme_show_key_value("flags ", "%02x", cmd->flags);
nvme_show_key_value("rsvd1 ", "%04x", cmd->rsvd1);
nvme_show_key_value("nsid ", "%08x", cmd->nsid);
nvme_show_key_value("cdw2 ", "%08x", cmd->cdw2);
nvme_show_key_value("cdw3 ", "%08x", cmd->cdw3);
nvme_show_key_value("data_len ", "%08x", cmd->data_len);
nvme_show_key_value("metadata_len ", "%08x", cmd->metadata_len);
nvme_show_key_value("addr ", "%"PRIx64"", (uint64_t)(uintptr_t)cmd->addr);
nvme_show_key_value("metadata ", "%"PRIx64"", (uint64_t)(uintptr_t)cmd->metadata);
nvme_show_key_value("cdw10 ", "%08x", cmd->cdw10);
nvme_show_key_value("cdw11 ", "%08x", cmd->cdw11);
nvme_show_key_value("cdw12 ", "%08x", cmd->cdw12);
nvme_show_key_value("cdw13 ", "%08x", cmd->cdw13);
nvme_show_key_value("cdw14 ", "%08x", cmd->cdw14);
nvme_show_key_value("cdw15 ", "%08x", cmd->cdw15);
nvme_show_key_value("timeout_ms ", "%08x", cmd->timeout_ms);
}

static void nvme_show_command(struct nvme_passthru_cmd *cmd, int err)
{
nvme_show_common(cmd);
printf("result : %08x\n", cmd->result);
printf("err : %d\n", err);
nvme_show_key_value("result ", "%08x", cmd->result);
nvme_show_key_value("err ", "%d", err);
}

static void nvme_show_command64(struct nvme_passthru_cmd64 *cmd, int err)
{
nvme_show_common((struct nvme_passthru_cmd *)cmd);
printf("result : %"PRIx64"\n", (uint64_t)(uintptr_t)cmd->result);
printf("err : %d\n", err);
nvme_show_key_value("result ", "%"PRIx64"", (uint64_t)(uintptr_t)cmd->result);
nvme_show_key_value("err ", "%d", err);
}

static void nvme_show_latency(struct timeval start, struct timeval end)
{
printf("latency : %llu us\n",
(unsigned long long)((end.tv_sec - start.tv_sec) * 1000000 +
(end.tv_usec - start.tv_usec)));
nvme_show_key_value("latency ", "%llu us",
(unsigned long long)((end.tv_sec - start.tv_sec) * 1000000 +
(end.tv_usec - start.tv_usec)));
}

static void nvme_log_retry(int errnum)
Expand All @@ -122,7 +117,7 @@ int nvme_submit_passthru(int fd, unsigned long ioctl_cmd,
if (log_level >= LOG_DEBUG)
gettimeofday(&start, NULL);

if (!dry_run) {
if (!nvme_cfg.dry_run) {
retry:
err = ioctl(fd, ioctl_cmd, cmd);
if (err && (errno == EAGAIN ||
Expand Down Expand Up @@ -155,7 +150,7 @@ int nvme_submit_passthru64(int fd, unsigned long ioctl_cmd,
if (log_level >= LOG_DEBUG)
gettimeofday(&start, NULL);

if (!dry_run) {
if (!nvme_cfg.dry_run) {
retry:
err = ioctl(fd, ioctl_cmd, cmd);
if (err && (errno == EAGAIN ||
Expand Down Expand Up @@ -197,8 +192,8 @@ static void nvme_show_req_admin(const struct nvme_mi_admin_req_hdr *hdr, size_t
};

nvme_show_common(&cmd);
printf("doff : %08x\n", le32_to_cpu(hdr->doff));
printf("dlen : %08x\n", le32_to_cpu(hdr->dlen));
nvme_show_key_value("doff ", "%08x", le32_to_cpu(hdr->doff));
nvme_show_key_value("dlen ", "%08x", le32_to_cpu(hdr->dlen));
}

static void nvme_show_req(__u8 type, const struct nvme_mi_msg_hdr *hdr, size_t hdr_len,
Expand Down
1 change: 0 additions & 1 deletion util/logging.h → logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@
extern int log_level;

int map_log_level(int verbose, bool quiet);
void set_dry_run(bool enable);

#endif // DEBUG_H_
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ sources = [
'nvme-wrap.c',
'plugin.c',
'libnvme-wrap.c',
'logging.c',
]
if json_c_dep.found()
sources += [
Expand Down
2 changes: 1 addition & 1 deletion nbft.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "nvme-print.h"

#include "util/types.h"
#include "util/logging.h"
#include "logging.h"

#define NBFT_SYSFS_FILENAME "NBFT*"

Expand Down
2 changes: 1 addition & 1 deletion nvme-print-binary.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */

#include "nvme-print.h"
#include "util/logging.h"
#include "logging.h"
#include "common.h"

static struct print_ops binary_print_ops;
Expand Down
40 changes: 37 additions & 3 deletions nvme-print-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "nvme-print.h"

#include "util/json.h"
#include "util/logging.h"
#include "logging.h"
#include "nvme.h"
#include "common.h"

Expand Down Expand Up @@ -44,6 +44,7 @@
static const uint8_t zero_uuid[16] = { 0 };
static struct print_ops json_print_ops;
static struct json_object *json_r;
static int json_init;

static void json_feature_show_fields(enum nvme_features_id fid, unsigned int result,
unsigned char *buf);
Expand Down Expand Up @@ -4900,27 +4901,60 @@ static void json_output_perror(const char *msg, va_list ap)
json_output_object(r);
}

static char *trim_white_space(char *str)
{
char *end;

if (!str)
return NULL;

/* Trim leading space */
while (isspace((unsigned char)*str))
str++;

/* All spaces */
if (!*str)
return str;

/* Trim trailing space */
end = str + strlen(str) - 1;
while (end > str && isspace((unsigned char)*end))
end--;

/* Write new null terminator character */
end[1] = '\0';

return str;
}

static void json_output_key_value(const char *key, const char *val, va_list ap)
{
struct json_object *r = json_r ? json_r : json_create_object();

_cleanup_free_ char *value = NULL;
_cleanup_free_ char *key_trim = trim_white_space(strdup(key));

if (vasprintf(&value, val, ap) < 0)
value = NULL;

obj_add_str(r, key, value ? value : "Could not allocate string");
obj_add_str(r, key_trim ? key_trim : key, value ? value : "Could not allocate string");

obj_print(r);
}

void json_show_init(void)
{
json_r = json_create_object();
json_init++;

if (!json_r)
json_r = json_create_object();
}

void json_show_finish(void)
{
if (--json_init)
return;

if (json_r)
json_output_object(json_r);

Expand Down
2 changes: 1 addition & 1 deletion nvme-print-stdout.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "nvme-models.h"
#include "util/suffix.h"
#include "util/types.h"
#include "util/logging.h"
#include "logging.h"
#include "common.h"

static const uint8_t zero_uuid[16] = { 0 };
Expand Down
12 changes: 8 additions & 4 deletions nvme.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
#include "nvme-wrap.h"
#include "util/argconfig.h"
#include "util/suffix.h"
#include "util/logging.h"
#include "logging.h"
#include "util/sighdl.h"
#include "fabrics.h"
#define CREATE_CMD
Expand Down Expand Up @@ -432,6 +432,9 @@ static int get_dev(struct nvme_dev **dev, int argc, char **argv, int flags)
else
ret = open_dev_direct(dev, devname, flags);

if (!ret && log_level >= LOG_DEBUG)
nvme_show_init();

return ret ? -errno : 0;
}

Expand All @@ -447,8 +450,6 @@ static int parse_args(int argc, char *argv[], const char *desc,
log_level = map_log_level(nvme_cfg.verbose, false);
nvme_init_default_logging(stderr, log_level, false, false);

set_dry_run(nvme_cfg.dry_run);

return 0;
}

Expand Down Expand Up @@ -515,6 +516,9 @@ bool nvme_is_output_format_json(void)

void dev_close(struct nvme_dev *dev)
{
if (log_level >= LOG_DEBUG)
nvme_show_finish();

switch (dev->type) {
case NVME_DEV_DIRECT:
close(dev_fd(dev));
Expand Down Expand Up @@ -7601,7 +7605,7 @@ static int copy_cmd(int argc, char **argv, struct command *cmd, struct plugin *p
else if (err != 0)
nvme_show_status(err);
else
printf("NVMe Copy: success\n");
nvme_show_key_value("NVMe Copy", "success");

return err;
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/nbft/nbft-plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "nvme.h"
#include "nbft.h"
#include "fabrics.h"
#include "util/logging.h"
#include "logging.h"

#define CREATE_CMD
#include "nbft-plugin.h"
Expand Down
2 changes: 1 addition & 1 deletion plugins/ocp/ocp-hardware-component-log.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include "common.h"
#include "util/types.h"
#include "util/logging.h"
#include "logging.h"
#include "nvme-print.h"
#include "ocp-hardware-component-log.h"
#include "ocp-print.h"
Expand Down
2 changes: 1 addition & 1 deletion plugins/ocp/ocp-nvme.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "plugin.h"
#include "linux/types.h"
#include "util/types.h"
#include "util/logging.h"
#include "logging.h"
#include "nvme-print.h"
#include "nvme-wrap.h"

Expand Down
1 change: 0 additions & 1 deletion util/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ sources += [
'util/argconfig.c',
'util/base64.c',
'util/crc32.c',
'util/logging.c',
'util/mem.c',
'util/sighdl.c',
'util/suffix.c',
Expand Down