From 57a4b0f17961c655d7c1c9bfc27a0ff087ab138d Mon Sep 17 00:00:00 2001 From: Shuhei Matsumoto Date: Mon, 28 Sep 2020 10:48:34 +0900 Subject: [PATCH] test/nvme_overhead&startup: Replace next pointer by TAILQ This will make the object relationship cleaner and the asynchronous detach operation easier to implement. Signed-off-by: Shuhei Matsumoto Change-Id: I3095d59b632ea2fc29fc44f8da330cb98c50bed1 Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/4434 Tested-by: SPDK CI Jenkins Reviewed-by: Aleksey Marchuk Reviewed-by: Jim Harris Reviewed-by: Ben Walker --- test/nvme/overhead/overhead.c | 15 ++++++--------- test/nvme/startup/startup.c | 32 ++++++++++++++------------------ 2 files changed, 20 insertions(+), 27 deletions(-) diff --git a/test/nvme/overhead/overhead.c b/test/nvme/overhead/overhead.c index 553f1a545..20d833775 100644 --- a/test/nvme/overhead/overhead.c +++ b/test/nvme/overhead/overhead.c @@ -47,7 +47,7 @@ struct ctrlr_entry { struct spdk_nvme_ctrlr *ctrlr; - struct ctrlr_entry *next; + TAILQ_ENTRY(ctrlr_entry) link; char name[1024]; }; @@ -95,7 +95,7 @@ struct perf_task { static bool g_enable_histogram = false; -static struct ctrlr_entry *g_ctrlr = NULL; +static TAILQ_HEAD(, ctrlr_entry) g_ctrlr = TAILQ_HEAD_INITIALIZER(g_ctrlr); static struct ns_entry *g_ns = NULL; static uint64_t g_tsc_rate; @@ -176,8 +176,7 @@ register_ctrlr(struct spdk_nvme_ctrlr *ctrlr) entry->ctrlr = ctrlr; - entry->next = g_ctrlr; - g_ctrlr = entry; + TAILQ_INSERT_TAIL(&g_ctrlr, entry, link); num_ns = spdk_nvme_ctrlr_get_num_ns(ctrlr); /* Only register the first namespace. */ @@ -645,7 +644,7 @@ static void cleanup(void) { struct ns_entry *ns_entry = g_ns; - struct ctrlr_entry *ctrlr_entry = g_ctrlr; + struct ctrlr_entry *ctrlr_entry, *tmp_ctrlr_entry; while (ns_entry) { struct ns_entry *next = ns_entry->next; @@ -656,12 +655,10 @@ cleanup(void) ns_entry = next; } - while (ctrlr_entry) { - struct ctrlr_entry *next = ctrlr_entry->next; - + TAILQ_FOREACH_SAFE(ctrlr_entry, &g_ctrlr, link, tmp_ctrlr_entry) { + TAILQ_REMOVE(&g_ctrlr, ctrlr_entry, link); spdk_nvme_detach(ctrlr_entry->ctrlr); free(ctrlr_entry); - ctrlr_entry = next; } } diff --git a/test/nvme/startup/startup.c b/test/nvme/startup/startup.c index 2d99803d3..e77655e17 100644 --- a/test/nvme/startup/startup.c +++ b/test/nvme/startup/startup.c @@ -38,20 +38,20 @@ #include "spdk/string.h" struct ctrlr_entry { - struct spdk_nvme_ctrlr *ctrlr; - struct ctrlr_entry *next; - char name[1024]; + struct spdk_nvme_ctrlr *ctrlr; + TAILQ_ENTRY(ctrlr_entry) link; + char name[1024]; }; struct ns_entry { struct spdk_nvme_ctrlr *ctrlr; struct spdk_nvme_ns *ns; - struct ns_entry *next; + TAILQ_ENTRY(ns_entry) link; struct spdk_nvme_qpair *qpair; }; -static struct ctrlr_entry *g_controllers = NULL; -static struct ns_entry *g_namespaces = NULL; +static TAILQ_HEAD(, ctrlr_entry) g_controllers = TAILQ_HEAD_INITIALIZER(g_controllers); +static TAILQ_HEAD(, ns_entry) g_namespaces = TAILQ_HEAD_INITIALIZER(g_namespaces); static int g_startup_time = 0; static bool @@ -92,28 +92,24 @@ attach_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid, snprintf(entry->name, sizeof(entry->name), "%-20.20s (%-20.20s)", cdata->mn, cdata->sn); entry->ctrlr = ctrlr; - entry->next = g_controllers; - g_controllers = entry; + TAILQ_INSERT_TAIL(&g_controllers, entry, link); } static void cleanup(void) { - struct ns_entry *ns_entry = g_namespaces; - struct ctrlr_entry *ctrlr_entry = g_controllers; + struct ns_entry *ns_entry, *tmp_ns_entry; + struct ctrlr_entry *ctrlr_entry, *tmp_ctrlr_entry; - while (ns_entry) { - struct ns_entry *next = ns_entry->next; + TAILQ_FOREACH_SAFE(ns_entry, &g_namespaces, link, tmp_ns_entry) { + TAILQ_REMOVE(&g_namespaces, ns_entry, link); free(ns_entry); - ns_entry = next; } - while (ctrlr_entry) { - struct ctrlr_entry *next = ctrlr_entry->next; - + TAILQ_FOREACH_SAFE(ctrlr_entry, &g_controllers, link, tmp_ctrlr_entry) { + TAILQ_REMOVE(&g_controllers, ctrlr_entry, link); spdk_nvme_detach(ctrlr_entry->ctrlr); free(ctrlr_entry); - ctrlr_entry = next; } } @@ -198,7 +194,7 @@ int main(int argc, char **argv) return 1; } - if (g_controllers == NULL) { + if (TAILQ_EMPTY(&g_controllers)) { fprintf(stderr, "no NVMe controllers found\n"); return 0; }