From patchwork Thu Nov 11 15:08:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12615017 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D2BB2C433F5 for ; Thu, 11 Nov 2021 15:08:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B560461252 for ; Thu, 11 Nov 2021 15:08:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233820AbhKKPK7 (ORCPT ); Thu, 11 Nov 2021 10:10:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37080 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233191AbhKKPK7 (ORCPT ); Thu, 11 Nov 2021 10:10:59 -0500 Received: from mail-ed1-x52e.google.com (mail-ed1-x52e.google.com [IPv6:2a00:1450:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D64E2C061766 for ; Thu, 11 Nov 2021 07:08:09 -0800 (PST) Received: by mail-ed1-x52e.google.com with SMTP id c8so25025309ede.13 for ; Thu, 11 Nov 2021 07:08:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=bOvASMiHnoTgV/RV4y7rZXaI1AIEHyKAUsVohjEHmGw=; b=a8Did8/8ZvDFzbIV33/Oi6MpbbPdDRJZS7ISQopndYQ1kpPAeAicyUfScg0yL8GE29 3J8YpdiKbtpO/1cyOwdYFrUO4Ay/GzJPtIcYkNpxQ74Y0ciCKJf0aBttc4RLc8yRGbsN EvcpoD5fyxIpv5yspZ43R1axqboWaqoPe/FBA1D1uXWMH4VBdpf76Fc8RnJEOIPjajUo AeQL/NCpKBxwFh4lk+0J0FWsHR/Zvd2oIXnd/+7vw0Pa9cobbrMWoL2cS05UWvXNWmHc zJrOBdTBwD5Ajy2wAPwiXAXJJxvMvk6F3JOkUdlBoZmfwkHi6cs6nlFCEttRaOFQBVat NEew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=bOvASMiHnoTgV/RV4y7rZXaI1AIEHyKAUsVohjEHmGw=; b=oo+ny9Vf6DxntV9YtoWSczv/3VH0+T0mA9eiP1oQrULYU9PhtmvBU4lKo5f1m4GRl/ icQN6vKCttN8/ZC29Rdpb0BrmtVk5c2TW9YX1x99kf2+xxUIUYyNP1HasubkxneMKqKe lRH4tH3vBul7FaC07ALRZTCK/QBGnKlYUmjAmJmx16ou/NzGKyHs79KAklSkLePIA+/T RsUJBTY/usOI00wmhgjO+TTrXOB2ELC07N9JmnNSHPjzztJOc1H2uR7bgX80xt7pwzR0 AqsDvJO9OFyZhZ9DXDrp52IvJpcC2U4K8hizqhJl7LnTEvdPQlMwMsAq8jnGvn2Pk7wz AIZw== X-Gm-Message-State: AOAM531MrXA/wybjjw2IAZf/OK41VpDnDSKb+JzhfVG2/BT+FGWXtuVI vlutYa8vIBf4V3bQ2j6L3kQ71sBdW4Os+w== X-Google-Smtp-Source: ABdhPJyrWAlt6LCuGN06AAOXIaLTLsBhAhusxzHjmjfpwPtMQeP1L+MWpmpPo3oz1iVLsCcKk+0G8A== X-Received: by 2002:a17:906:4452:: with SMTP id i18mr10017303ejp.374.1636643288430; Thu, 11 Nov 2021 07:08:08 -0800 (PST) Received: from oberon.zico.biz.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id y15sm1869027eda.35.2021.11.11.07.08.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Nov 2021 07:08:07 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v3 1/4] trace-cmd library: Add internal database for trace buffers Date: Thu, 11 Nov 2021 17:08:02 +0200 Message-Id: <20211111150805.86419-2-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211111150805.86419-1-tz.stoyanov@gmail.com> References: <20211111150805.86419-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org The new list with trace buffers information is added in the output handler, and internal APIs to work with that list. Storing various trace buffers information in the handler hides the complexity of this logic inside the library and simplifies possible extensions in the future. Signed-off-by: Tzvetomir Stoyanov (VMware) --- .../include/private/trace-cmd-private.h | 2 + lib/trace-cmd/trace-output.c | 58 ++++++++++++++++++- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/lib/trace-cmd/include/private/trace-cmd-private.h b/lib/trace-cmd/include/private/trace-cmd-private.h index fb050671..c0a07045 100644 --- a/lib/trace-cmd/include/private/trace-cmd-private.h +++ b/lib/trace-cmd/include/private/trace-cmd-private.h @@ -292,6 +292,8 @@ tracecmd_add_option_v(struct tracecmd_output *handle, struct tracecmd_option *tracecmd_add_buffer_option(struct tracecmd_output *handle, const char *name, int cpus); +int tracecmd_add_buffer_info(struct tracecmd_output *handle, const char *name, int cpus); +int tracecmd_write_buffer_info(struct tracecmd_output *handle); int tracecmd_write_cpus(struct tracecmd_output *handle, int cpus); int tracecmd_write_cmdlines(struct tracecmd_output *handle); diff --git a/lib/trace-cmd/trace-output.c b/lib/trace-cmd/trace-output.c index 27f296dd..f0f6aeed 100644 --- a/lib/trace-cmd/trace-output.c +++ b/lib/trace-cmd/trace-output.c @@ -39,6 +39,14 @@ struct tracecmd_option { struct list_head list; }; +struct tracecmd_buffer { + int cpus; + void *name; + tsize_t offset; + struct tracecmd_option *option; + struct list_head list; +}; + enum { OUTPUT_FL_SEND_META = (1 << 0), }; @@ -58,6 +66,7 @@ struct tracecmd_output { bool big_endian; struct list_head options; + struct list_head buffers; struct tracecmd_msg_handle *msg_handle; char *trace_clock; }; @@ -140,6 +149,7 @@ bool tracecmd_get_quiet(struct tracecmd_output *handle) void tracecmd_output_free(struct tracecmd_output *handle) { struct tracecmd_option *option; + struct tracecmd_buffer *buffer; if (!handle) return; @@ -150,6 +160,13 @@ void tracecmd_output_free(struct tracecmd_output *handle) if (handle->pevent) tep_unref(handle->pevent); + while (!list_empty(&handle->buffers)) { + buffer = container_of(handle->buffers.next, + struct tracecmd_buffer, list); + list_del(&buffer->list); + free(buffer->name); + free(buffer); + } while (!list_empty(&handle->options)) { option = container_of(handle->options.next, struct tracecmd_option, list); @@ -157,6 +174,7 @@ void tracecmd_output_free(struct tracecmd_output *handle) free(option->data); free(option); } + free(handle->trace_clock); free(handle); } @@ -907,6 +925,7 @@ struct tracecmd_output *tracecmd_output_allocate(int fd) handle->big_endian = tracecmd_host_bigendian(); list_head_init(&handle->options); + list_head_init(&handle->buffers); handle->file_state = TRACECMD_FILE_ALLOCATED; @@ -1323,7 +1342,7 @@ tracecmd_add_buffer_option(struct tracecmd_output *handle, const char *name, char *buf; int size = 8 + strlen(name) + 1; - buf = malloc(size); + buf = calloc(1, size); if (!buf) { tracecmd_warning("Failed to malloc buffer"); return NULL; @@ -1345,6 +1364,38 @@ tracecmd_add_buffer_option(struct tracecmd_output *handle, const char *name, return option; } +int tracecmd_add_buffer_info(struct tracecmd_output *handle, const char *name, int cpus) +{ + struct tracecmd_buffer *buf; + + buf = calloc(1, sizeof(struct tracecmd_buffer)); + if (!buf) + return -1; + buf->name = strdup(name); + buf->cpus = cpus; + if (!buf->name) { + free(buf); + return -1; + } + list_add_tail(&buf->list, &handle->buffers); + return 0; +} + +int tracecmd_write_buffer_info(struct tracecmd_output *handle) +{ + struct tracecmd_option *option; + struct tracecmd_buffer *buf; + + list_for_each_entry(buf, &handle->buffers, list) { + option = tracecmd_add_buffer_option(handle, buf->name, buf->cpus); + if (!option) + return -1; + buf->option = option; + } + + return 0; +} + int tracecmd_write_cmdlines(struct tracecmd_output *handle) { int ret; @@ -1580,7 +1631,9 @@ int tracecmd_append_cpu_data(struct tracecmd_output *handle, ret = tracecmd_write_cpus(handle, cpus); if (ret) return ret; - + ret = tracecmd_write_buffer_info(handle); + if (ret) + return ret; ret = tracecmd_write_options(handle); if (ret) return ret; @@ -1658,6 +1711,7 @@ struct tracecmd_output *tracecmd_get_output_handle_fd(int fd) handle->file_version = tracecmd_get_in_file_version(ihandle); handle->options_start = tracecmd_get_options_offset(ihandle); list_head_init(&handle->options); + list_head_init(&handle->buffers); tracecmd_close(ihandle); From patchwork Thu Nov 11 15:08:03 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12615019 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 74485C433EF for ; Thu, 11 Nov 2021 15:08:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 572AF61268 for ; Thu, 11 Nov 2021 15:08:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233191AbhKKPLA (ORCPT ); Thu, 11 Nov 2021 10:11:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37084 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233828AbhKKPLA (ORCPT ); Thu, 11 Nov 2021 10:11:00 -0500 Received: from mail-ed1-x529.google.com (mail-ed1-x529.google.com [IPv6:2a00:1450:4864:20::529]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 06D1CC061766 for ; Thu, 11 Nov 2021 07:08:11 -0800 (PST) Received: by mail-ed1-x529.google.com with SMTP id f8so25601006edy.4 for ; Thu, 11 Nov 2021 07:08:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=dJ4ENCfOd6TCRPScHl/BkdWhdsno+tVEOizcPCPbEP0=; b=CKv1tB4foNgiqpbgQfl8glcJCmlI66d6THuBw9fUgjg8l3xTueOfdHsTQIZ+hijk55 kmQr4Joof4MIH5G2K7Hrh4cGaF5mq8NBJ+PB3KZzCmWe+dfzxNfF7PdXKKeXtEjQTs+j 2XiFpa48hTL0t/gfJ7WqjTp4NAQv92OXg1BOuvAS/J3iz9/pJF6CPGziN4ZtyYV+jfqd jAZI5t2x0XXAqYB6tIlTEXH7d68KmXU4S+FuuP6VCIvDvaCa1ACd5R5KUFINL1Hx12W/ QStlArqm7Bp6KHA0obSNZR0LPhuwbxjVlZbbovpkunZiFILuvHlwg9yNeZAmFOy+ZN8f jakA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=dJ4ENCfOd6TCRPScHl/BkdWhdsno+tVEOizcPCPbEP0=; b=SrUmAx3Lfi+H0PV1nEssKhxYVOEG4eBZUpptL33YZt0Y4Jhi2kF+wtoDKx+ThK+AIT xpAc1HaVtOx2ggemeGjSMmD2QTmCYwXCKrXKqZQo8sxWAzWkmfrQAX73o/s0FWwRHgrj cuc/DZsESOAN6R2wkRR3dWqWvn9lRCTp4/IOFHVqSQBzkT95s6F2VdfLfL+LgMYdSfbX 61oW+l+ji851D15QNU4dZxOAN7uE2jz/qkqK5L7JzhFwB603lUiT7kv0Vt/8QLZZzOhE azQjgYXZxKMNOlejfJvxD6lQ9kx7HMVmRNwkeEwNXslBDE68MKoudeZym8NTA5qS7nai RFSw== X-Gm-Message-State: AOAM530cAX5OGHYlwYB8UtoYgMYYfj2IZL//6mRdDTtv1pwQpop3wjWL ppzt204cIyUeUUWMiJXKF8UiHZo5/UCMlQ== X-Google-Smtp-Source: ABdhPJx8oEBKSe7XjczYAESq0pq8cxxq1sll+0gnUO6LLSg9ttBkYkLcynYB/35c/C+ThkSJwDw2lw== X-Received: by 2002:a05:6402:14c3:: with SMTP id f3mr10549221edx.67.1636643289624; Thu, 11 Nov 2021 07:08:09 -0800 (PST) Received: from oberon.zico.biz.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id y15sm1869027eda.35.2021.11.11.07.08.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Nov 2021 07:08:09 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v3 2/4] trace-cmd: Use the new trace buffers API Date: Thu, 11 Nov 2021 17:08:03 +0200 Message-Id: <20211111150805.86419-3-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211111150805.86419-1-tz.stoyanov@gmail.com> References: <20211111150805.86419-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org When recording trace buffers, use the newly introduced library API for describing the buffers. Signed-off-by: Tzvetomir Stoyanov (VMware) --- .../include/private/trace-cmd-private.h | 2 -- lib/trace-cmd/trace-output.c | 7 +++---- tracecmd/trace-record.c | 16 +++++----------- 3 files changed, 8 insertions(+), 17 deletions(-) diff --git a/lib/trace-cmd/include/private/trace-cmd-private.h b/lib/trace-cmd/include/private/trace-cmd-private.h index c0a07045..47358c4b 100644 --- a/lib/trace-cmd/include/private/trace-cmd-private.h +++ b/lib/trace-cmd/include/private/trace-cmd-private.h @@ -290,8 +290,6 @@ struct tracecmd_option * tracecmd_add_option_v(struct tracecmd_output *handle, unsigned short id, const struct iovec *vector, int count); -struct tracecmd_option *tracecmd_add_buffer_option(struct tracecmd_output *handle, - const char *name, int cpus); int tracecmd_add_buffer_info(struct tracecmd_output *handle, const char *name, int cpus); int tracecmd_write_buffer_info(struct tracecmd_output *handle); diff --git a/lib/trace-cmd/trace-output.c b/lib/trace-cmd/trace-output.c index f0f6aeed..1f144d28 100644 --- a/lib/trace-cmd/trace-output.c +++ b/lib/trace-cmd/trace-output.c @@ -1334,9 +1334,8 @@ int tracecmd_append_options(struct tracecmd_output *handle) return 0; } -struct tracecmd_option * -tracecmd_add_buffer_option(struct tracecmd_output *handle, const char *name, - int cpus) +static struct tracecmd_option * +add_buffer_option(struct tracecmd_output *handle, const char *name, int cpus) { struct tracecmd_option *option; char *buf; @@ -1387,7 +1386,7 @@ int tracecmd_write_buffer_info(struct tracecmd_output *handle) struct tracecmd_buffer *buf; list_for_each_entry(buf, &handle->buffers, list) { - option = tracecmd_add_buffer_option(handle, buf->name, buf->cpus); + option = add_buffer_option(handle, buf->name, buf->cpus); if (!option) return -1; buf->option = option; diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c index 15e07cf0..c96dcda0 100644 --- a/tracecmd/trace-record.c +++ b/tracecmd/trace-record.c @@ -4169,7 +4169,6 @@ static void touch_file(const char *file) } static void append_buffer(struct tracecmd_output *handle, - struct tracecmd_option *buffer_option, struct buffer_instance *instance, char **temp_files) { @@ -4197,7 +4196,7 @@ static void append_buffer(struct tracecmd_output *handle, touch_file(temp_files[i]); } - tracecmd_append_buffer_cpu_data(handle, buffer_option, + tracecmd_append_buffer_cpu_data(handle, NULL, cpu_count, temp_files); for (i = 0; i < instance->cpu_count; i++) { @@ -4482,7 +4481,6 @@ error: static void record_data(struct common_record_context *ctx) { - struct tracecmd_option **buffer_options; struct tracecmd_output *handle; struct buffer_instance *instance; bool local = false; @@ -4551,9 +4549,6 @@ static void record_data(struct common_record_context *ctx) } if (buffers) { - buffer_options = malloc(sizeof(*buffer_options) * buffers); - if (!buffer_options) - die("Failed to allocate buffer options"); i = 0; for_each_instance(instance) { int cpus = instance->cpu_count != local_cpu_count ? @@ -4561,10 +4556,9 @@ static void record_data(struct common_record_context *ctx) if (instance->msg_handle) continue; - - buffer_options[i++] = tracecmd_add_buffer_option(handle, - tracefs_instance_get_name(instance->tracefs), - cpus); + tracecmd_add_buffer_info(handle, + tracefs_instance_get_name(instance->tracefs), + cpus); add_buffer_stat(handle, instance); } } @@ -4599,7 +4593,7 @@ static void record_data(struct common_record_context *ctx) if (instance->msg_handle) continue; print_stat(instance); - append_buffer(handle, buffer_options[i++], instance, temp_files); + append_buffer(handle, instance, temp_files); } } From patchwork Thu Nov 11 15:08:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12615021 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 45A64C433FE for ; Thu, 11 Nov 2021 15:08:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2D3FC61108 for ; Thu, 11 Nov 2021 15:08:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233460AbhKKPLB (ORCPT ); Thu, 11 Nov 2021 10:11:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37090 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233828AbhKKPLB (ORCPT ); Thu, 11 Nov 2021 10:11:01 -0500 Received: from mail-ed1-x531.google.com (mail-ed1-x531.google.com [IPv6:2a00:1450:4864:20::531]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 10C2CC061766 for ; Thu, 11 Nov 2021 07:08:12 -0800 (PST) Received: by mail-ed1-x531.google.com with SMTP id c8so25025794ede.13 for ; Thu, 11 Nov 2021 07:08:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=RAGSfu5oLO8nXabFlXdMGNLb/vGke1IHWcJ2BZgo0sc=; b=Uqkrx3DfjowWudkYcPvzCcUsHEAGjNtwyQ6sJwU4FtzMq6XhFhJx1QKHfKT/ytYOOa /OeIif2q+moEeIgHJpgfAhcVsm4eoLh02eUPA1Xg998q80vVBs0MK2DtchHZv7Q5GMtk avDGvNxX6mv3hkN4/nKr7UVgSlJZ2TUHPi7DJ/Qhb0+FnsOyZB2Y/+gUWGachXpF5a7c Q/GJgdsyZWvYIFIBwHQGTTEx6Xe0ggSTxmdno4oVkiuXcXyULWfDA5saFU/6Kg6bIhDM NYNezgTPYtOzs0VEyydZHTkqg16dWf0OpKlM7/9ZHNEfUyRxqCkb6oi5ighiU+a/3wcH nhzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=RAGSfu5oLO8nXabFlXdMGNLb/vGke1IHWcJ2BZgo0sc=; b=uGN8la+hgUy95qv6EvRGcGHDhvWKgMA1+ZThH/1vnTmLjU/S9zj5QArg1L6KAQLo97 bCqRIVQX8030sZxWq5qe0YDabfRTgKYQ6i6ySFkt/kWB2iF1T/UyfP4EGJJ8G9vc+1vS HhjQB3z9yWoZcScT2i2kIvalontMtyR09LOMBnzymRVnoY6vNpl/oGEl54vc2zSLXNKC 6lmA96JbvXNuBwIhPiMMMkg8VOIMoZM36eFWRJPTUjSfvslE4I3JVBOTHr3x2DTphFPZ SMy7TpKGcMgkpDYQLI+eXgN732t3I3mSeNLCES8HYrFp6MatoHtFLvfO02RXNN1zB9yd qnpQ== X-Gm-Message-State: AOAM532Is1+KDV9plobVlV/jY3+Rjtzdw5ADdEp91L3ldYpuXg4uQFoa CAUVkoP06YZ0SnKrHUT0jNrQINRq94R2yg== X-Google-Smtp-Source: ABdhPJy20qzQ/Cm8rvbOMHwg+YrUvHV+MPY2Yes+uvkIBd+rG6BTdckSBmAfcwrPAKDx///MPC+RPw== X-Received: by 2002:a05:6402:1c1c:: with SMTP id ck28mr10871332edb.300.1636643290638; Thu, 11 Nov 2021 07:08:10 -0800 (PST) Received: from oberon.zico.biz.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id y15sm1869027eda.35.2021.11.11.07.08.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Nov 2021 07:08:10 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v3 3/4] trace-cmd library: Helper logic to update the trace buffer offset Date: Thu, 11 Nov 2021 17:08:04 +0200 Message-Id: <20211111150805.86419-4-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211111150805.86419-1-tz.stoyanov@gmail.com> References: <20211111150805.86419-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org The new helper functions can be used the update the trace buffer metadata with the real offset within the trace file, where the buffer data are stored. Signed-off-by: Tzvetomir Stoyanov (VMware) --- lib/trace-cmd/trace-output.c | 49 ++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/lib/trace-cmd/trace-output.c b/lib/trace-cmd/trace-output.c index 1f144d28..17607b93 100644 --- a/lib/trace-cmd/trace-output.c +++ b/lib/trace-cmd/trace-output.c @@ -1395,6 +1395,20 @@ int tracecmd_write_buffer_info(struct tracecmd_output *handle) return 0; } +static tsize_t get_buffer_file_offset(struct tracecmd_output *handle, const char *name) +{ + struct tracecmd_buffer *buf; + + list_for_each_entry(buf, &handle->buffers, list) { + if (!strcmp(name, buf->name)) { + if (!buf->option) + break; + return buf->option->offset; + } + } + return 0; +} + int tracecmd_write_cmdlines(struct tracecmd_output *handle) { int ret; @@ -1487,6 +1501,41 @@ out: return ret; } +static int update_buffer_cpu_offset(struct tracecmd_output *handle, + const char *name, tsize_t offset) +{ + tsize_t b_offset; + tsize_t current; + + if (!name) + name = ""; + + b_offset = get_buffer_file_offset(handle, name); + if (!b_offset) { + tracecmd_warning("Cannot find description for buffer %s\n", name); + return -1; + } + + current = lseek64(handle->fd, 0, SEEK_CUR); + + /* Go to the option data, where will write the offest */ + if (lseek64(handle->fd, b_offset, SEEK_SET) == (off64_t)-1) { + tracecmd_warning("could not seek to %lld\n", b_offset); + return -1; + } + + if (do_write_check(handle, &offset, 8)) + return -1; + + /* Go back to end of file */ + if (lseek64(handle->fd, current, SEEK_SET) == (off64_t)-1) { + tracecmd_warning("could not seek to %lld\n", offset); + return -1; + } + return 0; +} + + static char *get_clock(struct tracecmd_output *handle) { struct tracefs_instance *inst; From patchwork Thu Nov 11 15:08:05 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12615023 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B9F4BC433F5 for ; Thu, 11 Nov 2021 15:08:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9CAE861252 for ; Thu, 11 Nov 2021 15:08:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233828AbhKKPLD (ORCPT ); Thu, 11 Nov 2021 10:11:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37096 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233824AbhKKPLC (ORCPT ); Thu, 11 Nov 2021 10:11:02 -0500 Received: from mail-ed1-x536.google.com (mail-ed1-x536.google.com [IPv6:2a00:1450:4864:20::536]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 48179C061766 for ; Thu, 11 Nov 2021 07:08:13 -0800 (PST) Received: by mail-ed1-x536.google.com with SMTP id r12so25385126edt.6 for ; Thu, 11 Nov 2021 07:08:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=4vXoNlfOg9utFABowyChlHAzm6DTD/Nao3nQ3VUYBWI=; b=S0DTIdr9XVf9WrHjnEjTm7zhhlHlJi2WEK84tISz+GeQHbFc3mbmw8MM/IbRHxcyqd cj4HgNqzi/jBqjexNtMoWxqnsKDtZkEtT7dsMHkV0re3gHdK/x6d+mec/0UZ1ItDmmiv DQ6nUQq2dktkh1JSInZk3PhKhnWS8aSV9S6zO3PmTcGX00VSC02Vhi/ejKz7tGSReARl EKLQKwtQazNenP9vb15dHuTyD64NZh1TyibjXB1QGbyXuG5DeiHemxBN3y1EgitiXC9E MBMQvxy6VFEtBRCoHDi0Z4g0ueE1UsZ5OggzQD2Hdjq4n3iESnESczq0zWyh0Kp2/yqf bAKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=4vXoNlfOg9utFABowyChlHAzm6DTD/Nao3nQ3VUYBWI=; b=Rn49kt0Y/CGLRvzfu6ZjK1/eSabFkiQaQLSf4HAoulqd55bxSJVM6CJODts8zFMGOw bOEZUsfeMa66vxmy7WmG2Yt+llkXakkljO/ZZ8xaYK+4LVpB4Le4Di0DLl+uCiomZk1i ii0z3DqgIlfRzD5d6zoum5O+muNS4CYa37mXRNdsKNU459YzEV6XNAeaxXT/4WQV9LRq +SD0IXRkrM1Hi02SzYzP73un7wZ4+eW39Y8kOPAUtnxQR+PdSrbrdFsEx1gBuflz+S+T N1FyLDje+tpC+Ul0okRs08dbOHcbxEOfZgH0B/0HtaWXdkelEH2/a0OyPDF18Zx2Zjci bFEg== X-Gm-Message-State: AOAM531wqsLH+i+SLG2C4YqNKLqDvHNrFXlmDADkY5RHL6nwscLAjTqa kuukusydBBhCNz5FQC8YrQA/oUI8DhlGOQ== X-Google-Smtp-Source: ABdhPJzZqJ4zhNfocxIfPbXitsuQE1gzjplqZ3Bx7Gll0WtWtkIeq7Y2xp9q/cfAgz8DsfMSkcdVug== X-Received: by 2002:a05:6402:4246:: with SMTP id g6mr10936000edb.112.1636643291634; Thu, 11 Nov 2021 07:08:11 -0800 (PST) Received: from oberon.zico.biz.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id y15sm1869027eda.35.2021.11.11.07.08.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Nov 2021 07:08:11 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v3 4/4] trace-cmd library: Refactor the logic for writing trace data in the file Date: Thu, 11 Nov 2021 17:08:05 +0200 Message-Id: <20211111150805.86419-5-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211111150805.86419-1-tz.stoyanov@gmail.com> References: <20211111150805.86419-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org Refactored the internal logic of tracecmd_write_cpu_data() API to be suitable for upcoming trace file format changes and data compression. The size and the offset of the trace data is saved in the file right after the data is written. The old logic calculates the size and offset in advance, but when the trace data is compressed it is hard to use that approach. Signed-off-by: Tzvetomir Stoyanov (VMware) --- .../include/private/trace-cmd-private.h | 6 +- lib/trace-cmd/include/trace-cmd-local.h | 19 ++ lib/trace-cmd/trace-output.c | 210 ++++++++++-------- tracecmd/trace-listen.c | 2 +- tracecmd/trace-record.c | 4 +- 5 files changed, 140 insertions(+), 101 deletions(-) diff --git a/lib/trace-cmd/include/private/trace-cmd-private.h b/lib/trace-cmd/include/private/trace-cmd-private.h index 47358c4b..767a63ab 100644 --- a/lib/trace-cmd/include/private/trace-cmd-private.h +++ b/lib/trace-cmd/include/private/trace-cmd-private.h @@ -303,13 +303,11 @@ struct tracecmd_output *tracecmd_copy(struct tracecmd_input *ihandle, const char *file); int tracecmd_write_cpu_data(struct tracecmd_output *handle, - int cpus, char * const *cpu_data_files); + int cpus, char * const *cpu_data_files, const char *buff_name); int tracecmd_append_cpu_data(struct tracecmd_output *handle, int cpus, char * const *cpu_data_files); int tracecmd_append_buffer_cpu_data(struct tracecmd_output *handle, - struct tracecmd_option *option, - int cpus, char * const *cpu_data_files); - + const char *name, int cpus, char * const *cpu_data_files); struct tracecmd_output *tracecmd_get_output_handle_fd(int fd); /* --- Reading the Fly Recorder Trace --- */ diff --git a/lib/trace-cmd/include/trace-cmd-local.h b/lib/trace-cmd/include/trace-cmd-local.h index 7f3b45a8..a0d6f0a6 100644 --- a/lib/trace-cmd/include/trace-cmd-local.h +++ b/lib/trace-cmd/include/trace-cmd-local.h @@ -31,7 +31,26 @@ void tracecmd_info(const char *fmt, ...); #endif #endif +struct data_file_write { + unsigned long long file_size; + unsigned long long write_size; + /* offset in the trace file, where write_size is stored */ + unsigned long long file_write_size; + unsigned long long data_offset; + /* offset in the trace file, where data_offset is stored */ + unsigned long long file_data_offset; +}; + bool check_file_state(unsigned long file_version, int current_state, int new_state); bool check_out_state(struct tracecmd_output *handle, int new_state); +struct cpu_data_source { + int fd; + int size; + off64_t offset; +}; + +int out_write_cpu_data(struct tracecmd_output *handle, int cpus, + struct cpu_data_source *data, const char *buff_name); + #endif /* _TRACE_CMD_LOCAL_H */ diff --git a/lib/trace-cmd/trace-output.c b/lib/trace-cmd/trace-output.c index 17607b93..6272c0f8 100644 --- a/lib/trace-cmd/trace-output.c +++ b/lib/trace-cmd/trace-output.c @@ -1555,17 +1555,14 @@ static char *get_clock(struct tracecmd_output *handle) return handle->trace_clock; } -int tracecmd_write_cpu_data(struct tracecmd_output *handle, - int cpus, char * const *cpu_data_files) +__hidden int out_write_cpu_data(struct tracecmd_output *handle, + int cpus, struct cpu_data_source *data, const char *buff_name) { - off64_t *offsets = NULL; - unsigned long long *sizes = NULL; - off64_t offset; + struct data_file_write *data_files = NULL; + tsize_t data_offs, offset; unsigned long long endian8; - char *clock = NULL; - off64_t check_size; - char *file; - struct stat st; + unsigned long long read_size; + char *clock; int ret; int i; @@ -1580,97 +1577,145 @@ int tracecmd_write_cpu_data(struct tracecmd_output *handle, goto out_free; } + data_offs = lseek64(handle->fd, 0, SEEK_CUR); if (do_write_check(handle, "flyrecord", 10)) goto out_free; - offsets = malloc(sizeof(*offsets) * cpus); - if (!offsets) - goto out_free; - sizes = malloc(sizeof(*sizes) * cpus); - if (!sizes) + data_files = calloc(cpus, sizeof(*data_files)); + if (!data_files) goto out_free; - offset = lseek64(handle->fd, 0, SEEK_CUR); - - /* hold any extra data for data */ - offset += cpus * (16); - - /* - * Unfortunately, the trace_clock data was placed after the - * cpu data, and wasn't accounted for with the offsets. - * We need to save room for the trace_clock file. This means - * we need to find the size of it before we define the final - * offsets. - */ - clock = get_clock(handle); - if (!clock) - goto out_free; - /* Save room for storing the size */ - offset += 8; - offset += strlen(clock); - /* 2 bytes for [] around the clock */ - offset += 2; - - /* Page align offset */ - offset = (offset + (handle->page_size - 1)) & ~(handle->page_size - 1); - for (i = 0; i < cpus; i++) { - file = cpu_data_files[i]; - ret = stat(file, &st); - if (ret < 0) { - tracecmd_warning("can not stat '%s'", file); - goto out_free; - } - offsets[i] = offset; - sizes[i] = st.st_size; - offset += st.st_size; - offset = (offset + (handle->page_size - 1)) & ~(handle->page_size - 1); - - endian8 = convert_endian_8(handle, offsets[i]); + data_files[i].file_size = data[i].size; + /* + * Place 0 for the data offset and size, and save the offsets to + * updated them with the correct data later. + */ + endian8 = 0; + data_files[i].file_data_offset = lseek64(handle->fd, 0, SEEK_CUR); if (do_write_check(handle, &endian8, 8)) goto out_free; - endian8 = convert_endian_8(handle, sizes[i]); + data_files[i].file_write_size = lseek64(handle->fd, 0, SEEK_CUR); if (do_write_check(handle, &endian8, 8)) goto out_free; } - if (save_clock(handle, clock)) + update_buffer_cpu_offset(handle, buff_name, data_offs); + clock = get_clock(handle); + if (clock && save_clock(handle, clock)) goto out_free; for (i = 0; i < cpus; i++) { + data_files[i].data_offset = lseek64(handle->fd, 0, SEEK_CUR); + /* Page align offset */ + data_files[i].data_offset += handle->page_size - 1; + data_files[i].data_offset &= ~(handle->page_size - 1); + + ret = lseek64(handle->fd, data_files[i].data_offset, SEEK_SET); + if (ret == (off64_t)-1) + goto out_free; + if (!tracecmd_get_quiet(handle)) fprintf(stderr, "CPU%d data recorded at offset=0x%llx\n", - i, (unsigned long long) offsets[i]); - offset = lseek64(handle->fd, offsets[i], SEEK_SET); - if (offset == (off64_t)-1) { - tracecmd_warning("could not seek to %lld\n", offsets[i]); - goto out_free; + i, (unsigned long long)data_files[i].data_offset); + + if (data[i].size) { + if (lseek64(data[i].fd, data[i].offset, SEEK_SET) == (off64_t)-1) + goto out_free; + read_size = copy_file_fd(handle, data[i].fd); + if (read_size != data_files[i].file_size) { + errno = EINVAL; + tracecmd_warning("did not match size of %lld to %lld", + read_size, data_files[i].file_size); + goto out_free; + } + data_files[i].write_size = read_size; + } else { + data_files[i].write_size = 0; } - check_size = copy_file(handle, cpu_data_files[i]); - if (check_size != sizes[i]) { - errno = EINVAL; - tracecmd_warning("did not match size of %lld to %lld", - check_size, sizes[i]); + + /* Write the real CPU data offset in the file */ + if (lseek64(handle->fd, data_files[i].file_data_offset, SEEK_SET) == (off64_t)-1) goto out_free; - } + endian8 = convert_endian_8(handle, data_files[i].data_offset); + if (do_write_check(handle, &endian8, 8)) + goto out_free; + + /* Write the real CPU data size in the file */ + if (lseek64(handle->fd, data_files[i].file_write_size, SEEK_SET) == (off64_t)-1) + goto out_free; + endian8 = convert_endian_8(handle, data_files[i].write_size); + if (do_write_check(handle, &endian8, 8)) + goto out_free; + + offset = data_files[i].data_offset + data_files[i].write_size; + if (lseek64(handle->fd, offset, SEEK_SET) == (off64_t)-1) + goto out_free; + if (!tracecmd_get_quiet(handle)) fprintf(stderr, " %llu bytes in size\n", - (unsigned long long)check_size); + (unsigned long long)data_files[i].write_size); } - free(offsets); - free(sizes); + free(data_files); + if (lseek64(handle->fd, 0, SEEK_END) == (off64_t)-1) + return -1; handle->file_state = TRACECMD_FILE_CPU_FLYRECORD; return 0; out_free: - free(offsets); - free(sizes); + lseek64(handle->fd, 0, SEEK_END); + free(data_files); return -1; } +int tracecmd_write_cpu_data(struct tracecmd_output *handle, + int cpus, char * const *cpu_data_files, const char *buff_name) +{ + struct cpu_data_source *data; + struct stat st; + int size = 0; + int ret; + int i; + + if (!buff_name) + buff_name = ""; + + data = calloc(cpus, sizeof(struct cpu_data_source)); + if (!data) + return -1; + + for (i = 0; i < cpus; i++) { + ret = stat(cpu_data_files[i], &st); + if (ret < 0) { + tracecmd_warning("can not stat '%s'", cpu_data_files[i]); + break; + } + data[i].fd = open(cpu_data_files[i], O_RDONLY); + if (data[i].fd < 0) { + tracecmd_warning("Can't read '%s'", data[i].fd); + break; + } + + data[i].size = st.st_size; + data[i].offset = 0; + size += st.st_size; + } + + if (i < cpus) + ret = -1; + else + ret = out_write_cpu_data(handle, cpus, data, buff_name); + + for (i--; i >= 0; i--) + close(data[i].fd); + + free(data); + return ret; +} + int tracecmd_append_cpu_data(struct tracecmd_output *handle, int cpus, char * const *cpu_data_files) { @@ -1686,36 +1731,13 @@ int tracecmd_append_cpu_data(struct tracecmd_output *handle, if (ret) return ret; - return tracecmd_write_cpu_data(handle, cpus, cpu_data_files); + return tracecmd_write_cpu_data(handle, cpus, cpu_data_files, NULL); } int tracecmd_append_buffer_cpu_data(struct tracecmd_output *handle, - struct tracecmd_option *option, - int cpus, char * const *cpu_data_files) + const char *name, int cpus, char * const *cpu_data_files) { - tsize_t offset; - stsize_t ret; - - offset = lseek64(handle->fd, 0, SEEK_CUR); - - /* Go to the option data, where will write the offest */ - ret = lseek64(handle->fd, option->offset, SEEK_SET); - if (ret == (off64_t)-1) { - tracecmd_warning("could not seek to %lld\n", option->offset); - return -1; - } - - if (do_write_check(handle, &offset, 8)) - return -1; - - /* Go back to end of file */ - ret = lseek64(handle->fd, offset, SEEK_SET); - if (ret == (off64_t)-1) { - tracecmd_warning("could not seek to %lld\n", offset); - return -1; - } - - return tracecmd_write_cpu_data(handle, cpus, cpu_data_files); + return tracecmd_write_cpu_data(handle, cpus, cpu_data_files, name); } struct tracecmd_output *tracecmd_get_output_handle_fd(int fd) diff --git a/tracecmd/trace-listen.c b/tracecmd/trace-listen.c index 0cb70b7d..45ba1211 100644 --- a/tracecmd/trace-listen.c +++ b/tracecmd/trace-listen.c @@ -610,7 +610,7 @@ static int put_together_file(int cpus, int ofd, const char *node, if (ret) goto out; } - ret = tracecmd_write_cpu_data(handle, cpus, temp_files); + ret = tracecmd_write_cpu_data(handle, cpus, temp_files, NULL); out: tracecmd_output_close(handle); diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c index c96dcda0..3a84e116 100644 --- a/tracecmd/trace-record.c +++ b/tracecmd/trace-record.c @@ -4196,7 +4196,7 @@ static void append_buffer(struct tracecmd_output *handle, touch_file(temp_files[i]); } - tracecmd_append_buffer_cpu_data(handle, NULL, + tracecmd_append_buffer_cpu_data(handle, tracefs_instance_get_name(instance->tracefs), cpu_count, temp_files); for (i = 0; i < instance->cpu_count; i++) { @@ -4446,7 +4446,7 @@ static void write_guest_file(struct buffer_instance *instance) die("failed to allocate memory"); } - if (tracecmd_write_cpu_data(handle, cpu_count, temp_files) < 0) + if (tracecmd_write_cpu_data(handle, cpu_count, temp_files, NULL) < 0) die("failed to write CPU data"); tracecmd_output_close(handle);