From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id ED970A0560; Tue, 18 Oct 2022 15:28:14 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 3B7254282F; Tue, 18 Oct 2022 15:27:49 +0200 (CEST) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mails.dpdk.org (Postfix) with ESMTP id 31D214282F for ; Tue, 18 Oct 2022 15:27:46 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666099665; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=X5KLhqXv8gZhoFg28Qbw1pIOMpKFEJP80YBRww2FGDU=; b=OeM781TGquPq9PtP6fZenFVUL8FnuCV7pQVpEMfvekGPPwgNBs4TOttL5fhWCayjo79qbR P0cg8CuV1CvUN0lD7qTGG0D51yc3wTzKv8Nog9DMuaVNYWq9wRdBmzcMVPB3qIpcvmE8KD YYmLbi7FeDbHC1ldWDkA8LapZRdzeEY= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-480-zEGTqSL2OgqI6Q3N3FXXBw-1; Tue, 18 Oct 2022 09:27:42 -0400 X-MC-Unique: zEGTqSL2OgqI6Q3N3FXXBw-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 503B2882820; Tue, 18 Oct 2022 13:27:42 +0000 (UTC) Received: from localhost.localdomain (ovpn-192-244.brq.redhat.com [10.40.192.244]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7EA35492B0A; Tue, 18 Oct 2022 13:27:41 +0000 (UTC) From: David Marchand To: dev@dpdk.org Cc: jerinj@marvell.com, skori@marvell.com Subject: [PATCH v4 10/11] trace: create new directory for each trace dump Date: Tue, 18 Oct 2022 15:26:53 +0200 Message-Id: <20221018132654.3760561-11-david.marchand@redhat.com> In-Reply-To: <20221018132654.3760561-1-david.marchand@redhat.com> References: <20220921120359.2201131-1-david.marchand@redhat.com> <20221018132654.3760561-1-david.marchand@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Rather than always overwrite the content of the trace directory, create new directories for each call to rte_trace_save(). In the unlikely event that multiple rte_trace_save() gets called many times in a single second, try to suffix the name with a digit. Signed-off-by: David Marchand --- lib/eal/common/eal_common_trace.c | 7 ++ lib/eal/common/eal_common_trace_utils.c | 101 ++++++++++-------------- lib/eal/common/eal_trace.h | 2 + 3 files changed, 50 insertions(+), 60 deletions(-) diff --git a/lib/eal/common/eal_common_trace.c b/lib/eal/common/eal_common_trace.c index 5caaac8e59..42f2c28c23 100644 --- a/lib/eal/common/eal_common_trace.c +++ b/lib/eal/common/eal_common_trace.c @@ -66,6 +66,9 @@ eal_trace_init(void) if (trace_metadata_create() < 0) goto fail; + if (trace.rootdir == NULL && trace_dir_default_path_get() < 0) + goto free_meta; + /* Save current epoch timestamp for future use */ if (trace_epoch_time_save() < 0) goto free_meta; @@ -91,6 +94,10 @@ eal_trace_fini(void) trace_mem_free(); trace_metadata_destroy(); eal_trace_args_free(); + free(trace.rootdir); + trace.rootdir = NULL; + free(trace.dir); + trace.dir = NULL; } bool diff --git a/lib/eal/common/eal_common_trace_utils.c b/lib/eal/common/eal_common_trace_utils.c index 8561a0e198..c226313d61 100644 --- a/lib/eal/common/eal_common_trace_utils.c +++ b/lib/eal/common/eal_common_trace_utils.c @@ -87,9 +87,10 @@ trace_uuid_generate(void) } static int -trace_session_name_generate(char **trace_dir) +trace_dir_name_generate(char **trace_dir) { char date[sizeof("YYYY-mm-dd-AM-HH-MM-SS")]; + struct trace *trace = trace_obj_get(); struct tm *tm_result; time_t tm; @@ -106,7 +107,8 @@ trace_session_name_generate(char **trace_dir) goto fail; } - if (asprintf(trace_dir, "%s-%s", eal_get_hugefile_prefix(), date) == -1) + if (asprintf(trace_dir, "%s/%s-%s", trace->rootdir, + eal_get_hugefile_prefix(), date) == -1) goto fail; return 0; @@ -115,21 +117,6 @@ trace_session_name_generate(char **trace_dir) return -1; } -static int -trace_dir_update(const char *str) -{ - struct trace *trace = trace_obj_get(); - char *dir; - int rc; - - rc = asprintf(&dir, "%s%s", trace->dir != NULL ? trace->dir : "", str); - if (rc != -1) { - free(trace->dir); - trace->dir = dir; - } - return rc; -} - int eal_trace_args_save(const char *val) { @@ -240,17 +227,16 @@ eal_trace_mode_args_save(const char *val) int eal_trace_dir_args_save(char const *val) { - char *dir_path; - int rc; + struct trace *trace = trace_obj_get(); - if (asprintf(&dir_path, "%s/", val) == -1) { + free(trace->rootdir); + trace->rootdir = strdup(val); + if (trace->rootdir == NULL) { trace_err("failed to copy directory: %s", strerror(errno)); return -ENOMEM; } - rc = trace_dir_update(dir_path); - free(dir_path); - return rc; + return 0; } int @@ -275,9 +261,10 @@ trace_epoch_time_save(void) return 0; } -static int -trace_dir_default_path_get(char **dir_path) +int +trace_dir_default_path_get(void) { + struct trace *trace = trace_obj_get(); struct passwd *pwd; char *home_dir; @@ -293,7 +280,7 @@ trace_dir_default_path_get(char **dir_path) } /* Append dpdk-traces to directory */ - if (asprintf(dir_path, "%s/dpdk-traces/", home_dir) == -1) + if (asprintf(&trace->rootdir, "%s/dpdk-traces", home_dir) == -1) return -ENOMEM; return 0; @@ -303,53 +290,47 @@ static int trace_mkdir(void) { struct trace *trace = trace_obj_get(); - static bool already_done; - char *session; + unsigned int try; + char *trace_dir; int rc; - if (already_done) - return 0; - - if (trace->dir == NULL) { - char *dir_path; - - rc = trace_dir_default_path_get(&dir_path); - if (rc < 0) { - trace_err("fail to get default path"); - return rc; - } - - rc = trace_dir_update(dir_path); - free(dir_path); - if (rc < 0) - return rc; - } - /* Create the path if it t exist, no "mkdir -p" available here */ - rc = mkdir(trace->dir, 0700); + rc = mkdir(trace->rootdir, 0700); if (rc < 0 && errno != EEXIST) { - trace_err("mkdir %s failed [%s]", trace->dir, strerror(errno)); + trace_err("mkdir %s failed [%s]", trace->rootdir, strerror(errno)); rte_errno = errno; return -rte_errno; } - rc = trace_session_name_generate(&session); + rc = trace_dir_name_generate(&trace_dir); if (rc < 0) return rc; - rc = trace_dir_update(session); - free(session); - if (rc < 0) - return rc; - - rc = mkdir(trace->dir, 0700); - if (rc < 0) { - trace_err("mkdir %s failed [%s]", trace->dir, strerror(errno)); - rte_errno = errno; - return -rte_errno; + rc = mkdir(trace_dir, 0700); + if (rc == 0) + goto out; + + /* 1000 tries are definitely enough :-) */ + for (try = 1; try < 1000; try++) { + char *trace_dir_suffix; + + if (asprintf(&trace_dir_suffix, "%s.%u", trace_dir, try) == -1) + continue; + rc = mkdir(trace_dir_suffix, 0700); + if (rc == 0) { + free(trace_dir); + trace_dir = trace_dir_suffix; + goto out; + } + free(trace_dir_suffix); } + trace_err("mkdir %s failed [%s]", trace->dir, strerror(errno)); + rte_errno = errno; + return -rte_errno; +out: + free(trace->dir); + trace->dir = trace_dir; RTE_LOG(INFO, EAL, "Trace dir: %s\n", trace->dir); - already_done = true; return 0; } diff --git a/lib/eal/common/eal_trace.h b/lib/eal/common/eal_trace.h index d66bcfe198..be9e1645a3 100644 --- a/lib/eal/common/eal_trace.h +++ b/lib/eal/common/eal_trace.h @@ -48,6 +48,7 @@ struct trace_arg { }; struct trace { + char *rootdir; char *dir; int register_errno; uint32_t status; @@ -100,6 +101,7 @@ void trace_uuid_generate(void); int trace_metadata_create(void); void trace_metadata_destroy(void); char *trace_metadata_fixup_field(const char *field); +int trace_dir_default_path_get(void); int trace_epoch_time_save(void); void trace_mem_free(void); void trace_mem_per_thread_free(void); -- 2.37.3