From patchwork Mon Sep 13 12:47:47 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: 12488877 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.9 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNWANTED_LANGUAGE_BODY, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E0DB9C433F5 for ; Mon, 13 Sep 2021 12:48:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C86D560FBF for ; Mon, 13 Sep 2021 12:48:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234951AbhIMMuM (ORCPT ); Mon, 13 Sep 2021 08:50:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51114 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235510AbhIMMtO (ORCPT ); Mon, 13 Sep 2021 08:49:14 -0400 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A488BC061574 for ; Mon, 13 Sep 2021 05:47:58 -0700 (PDT) Received: by mail-wr1-x431.google.com with SMTP id x6so14479596wrv.13 for ; Mon, 13 Sep 2021 05:47:58 -0700 (PDT) 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=aCECAkJ+AmB28g1g/FT1kcdtEf2WJ5s5SYF7g1An2FA=; b=jZWfTfAd+YXXB5kBV85zecUF43Idn/kPJPdHXTvRNbmkqMaDniTSpQmP5m797SfRt7 6SSWaS1jlYhmnU9VlFkWOSu/d4eixa1cb9V3X469T9uxnrmChovmINLYZ3D3p+8ICPMy A6dtAoJXxw1+zE0oiZnp9boCt8jLDY9P3P7ajU1dOEjIspwonvuf7J7hfuE8BOGJY1FN d4hAgAqFwG3+y7rWNU1t8ot4lcLM84NHH8DwcR8+0GJ41YwtFTXeUTQPLiAjf7w5Fsyb GwuTthXk7Ocpb1JFJw99yvgHv/4esmW/wtbF75DQiTg4cooretlu3K7+eg8bUR6S/pKE eaNg== 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=aCECAkJ+AmB28g1g/FT1kcdtEf2WJ5s5SYF7g1An2FA=; b=MMXQ4YVd8EWpCs9+5mMfTpdo6IaNuNP+AMk2IAGV9S4tAbvAXEGIvWFpMqdzTAPFm/ V4UgYmyvUFjPOzuEkrPzoxEGi8HSCJlmfkoAO6to+N8Ri3SvWPtO4tnhweCtkF0Ea1vo xVbReLY5gxWSyi4GDGeInAMVQmTlDtO2pPAHXFShMFrpbPGQXaFCvLHEky31k8XBHDAw pvsXTQ/9Eb603d3wFx1ggL1sP1pbo7rfEyzDuRaq9SbMKKCDMDstmZnsdN9SB9SZgOQL Npip+vKkqPxTlcdiBD+VqJaX6xQNdLbj6Rnw4jnjDfyKrQ4KdAkySc3DsxU7tuaUm1kl OvbA== X-Gm-Message-State: AOAM531q6oP7p+8byD0nSJ7jeMSsUI4O/93oud55RwmNGGAuLGYyqk2k zXvncAaecuwYBwzwPGmvTrjFH5PkzPo= X-Google-Smtp-Source: ABdhPJwl071CuUgnihG873cxOWz8p1Bz7UeF8MvWDNkbRbf4QMK/946POvyCgzKnl8TuNAJRRH1GiA== X-Received: by 2002:a5d:5258:: with SMTP id k24mr12315647wrc.390.1631537277142; Mon, 13 Sep 2021 05:47:57 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id k17sm7718077wmj.0.2021.09.13.05.47.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Sep 2021 05:47:56 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 1/8] trace-cmd library: Use output handler when copying data from input file Date: Mon, 13 Sep 2021 15:47:47 +0300 Message-Id: <20210913124754.3679916-2-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210913124754.3679916-1-tz.stoyanov@gmail.com> References: <20210913124754.3679916-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org When copying data between two trace files, use input and output handlers, instead of raw file descriptors. Handlers take care of file version and compression context. Signed-off-by: Tzvetomir Stoyanov (VMware) --- .../include/private/trace-cmd-private.h | 3 +- lib/trace-cmd/include/trace-cmd-local.h | 2 + lib/trace-cmd/trace-input.c | 145 +++++++++--------- lib/trace-cmd/trace-output.c | 6 +- 4 files changed, 82 insertions(+), 74 deletions(-) diff --git a/lib/trace-cmd/include/private/trace-cmd-private.h b/lib/trace-cmd/include/private/trace-cmd-private.h index 8f5408ff..8ff46d52 100644 --- a/lib/trace-cmd/include/private/trace-cmd-private.h +++ b/lib/trace-cmd/include/private/trace-cmd-private.h @@ -185,7 +185,8 @@ int tracecmd_get_parsing_failures(struct tracecmd_input *handle); int tracecmd_long_size(struct tracecmd_input *handle); int tracecmd_page_size(struct tracecmd_input *handle); int tracecmd_cpus(struct tracecmd_input *handle); -int tracecmd_copy_headers(struct tracecmd_input *handle, int fd, +int tracecmd_copy_headers(struct tracecmd_input *in_handle, + struct tracecmd_output *out_handle, enum tracecmd_file_states start_state, enum tracecmd_file_states end_state); void tracecmd_set_flag(struct tracecmd_input *handle, int flag); diff --git a/lib/trace-cmd/include/trace-cmd-local.h b/lib/trace-cmd/include/trace-cmd-local.h index 9ab33553..18d2d05b 100644 --- a/lib/trace-cmd/include/trace-cmd-local.h +++ b/lib/trace-cmd/include/trace-cmd-local.h @@ -57,6 +57,8 @@ out_write_section_header(struct tracecmd_output *handle, unsigned short header_i char *description, enum tracecmd_section_flags flags, bool option); int out_update_section_header(struct tracecmd_output *handle, unsigned long long offset); +long long do_write_check(struct tracecmd_output *handle, const void *data, long long size); + struct cpu_data_source { int fd; int size; diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index 6ed5edf9..5cc881a8 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -4391,44 +4391,47 @@ void tracecmd_close(struct tracecmd_input *handle) free(handle); } -static int read_copy_size8(struct tracecmd_input *handle, int fd, unsigned long long *size) +static int read_copy_size8(struct tracecmd_input *in_handle, + struct tracecmd_output *out_handle, unsigned long long *size) { /* read size */ - if (do_read_check(handle, size, 8)) + if (do_read_check(in_handle, size, 8)) return -1; - if (__do_write_check(fd, size, 8)) + if (do_write_check(out_handle, size, 8)) return -1; - *size = tep_read_number(handle->pevent, size, 8); + *size = tep_read_number(in_handle->pevent, size, 8); return 0; } -static int read_copy_size4(struct tracecmd_input *handle, int fd, unsigned int *size) +static int read_copy_size4(struct tracecmd_input *in_handle, struct tracecmd_output *out_handle, + unsigned int *size) { /* read size */ - if (do_read_check(handle, size, 4)) + if (do_read_check(in_handle, size, 4)) return -1; - if (__do_write_check(fd, size, 4)) + if (do_write_check(out_handle, size, 4)) return -1; - *size = tep_read_number(handle->pevent, size, 4); + *size = tep_read_number(in_handle->pevent, size, 4); return 0; } -static int read_copy_data(struct tracecmd_input *handle, - unsigned long long size, int fd) +static int read_copy_data(struct tracecmd_input *in_handle, + unsigned long long size, + struct tracecmd_output *out_handle) { char *buf; buf = malloc(size); if (!buf) return -1; - if (do_read_check(handle, buf, size)) + if (do_read_check(in_handle, buf, size)) goto failed_read; - if (__do_write_check(fd, buf, size)) + if (do_write_check(out_handle, buf, size)) goto failed_read; free(buf); @@ -4440,65 +4443,66 @@ static int read_copy_data(struct tracecmd_input *handle, return -1; } -static int copy_header_files(struct tracecmd_input *handle, int fd) +static int copy_header_files(struct tracecmd_input *in_handle, + struct tracecmd_output *out_handle) { unsigned long long size; - if (handle->file_state != TRACECMD_FILE_HEADERS - 1) + if (in_handle->file_state != TRACECMD_FILE_HEADERS - 1) return -1; /* "header_page" */ - if (read_copy_data(handle, 12, fd) < 0) + if (read_copy_data(in_handle, 12, out_handle) < 0) return -1; - if (read_copy_size8(handle, fd, &size) < 0) + if (read_copy_size8(in_handle, out_handle, &size) < 0) return -1; - if (read_copy_data(handle, size, fd) < 0) + if (read_copy_data(in_handle, size, out_handle) < 0) return -1; /* "header_event" */ - if (read_copy_data(handle, 13, fd) < 0) + if (read_copy_data(in_handle, 13, out_handle) < 0) return -1; - if (read_copy_size8(handle, fd, &size) < 0) + if (read_copy_size8(in_handle, out_handle, &size) < 0) return -1; - if (read_copy_data(handle, size, fd) < 0) + if (read_copy_data(in_handle, size, out_handle) < 0) return -1; - handle->file_state = TRACECMD_FILE_HEADERS; + in_handle->file_state = TRACECMD_FILE_HEADERS; return 0; } -static int copy_ftrace_files(struct tracecmd_input *handle, int fd) +static int copy_ftrace_files(struct tracecmd_input *in_handle, struct tracecmd_output *out_handle) { unsigned long long size; unsigned int count; unsigned int i; - if (handle->file_state != TRACECMD_FILE_FTRACE_EVENTS - 1) + if (in_handle->file_state != TRACECMD_FILE_FTRACE_EVENTS - 1) return -1; - if (read_copy_size4(handle, fd, &count) < 0) + if (read_copy_size4(in_handle, out_handle, &count) < 0) return -1; for (i = 0; i < count; i++) { - if (read_copy_size8(handle, fd, &size) < 0) + if (read_copy_size8(in_handle, out_handle, &size) < 0) return -1; - if (read_copy_data(handle, size, fd) < 0) + if (read_copy_data(in_handle, size, out_handle) < 0) return -1; } - handle->file_state = TRACECMD_FILE_FTRACE_EVENTS; + in_handle->file_state = TRACECMD_FILE_FTRACE_EVENTS; return 0; } -static int copy_event_files(struct tracecmd_input *handle, int fd) +static int copy_event_files(struct tracecmd_input *in_handle, struct tracecmd_output *out_handle) { unsigned long long size; char *system; @@ -4506,103 +4510,103 @@ static int copy_event_files(struct tracecmd_input *handle, int fd) unsigned int count; unsigned int i,x; - if (handle->file_state != TRACECMD_FILE_ALL_EVENTS - 1) + if (in_handle->file_state != TRACECMD_FILE_ALL_EVENTS - 1) return -1; - if (read_copy_size4(handle, fd, &systems) < 0) + if (read_copy_size4(in_handle, out_handle, &systems) < 0) return -1; for (i = 0; i < systems; i++) { - system = read_string(handle); + system = read_string(in_handle); if (!system) return -1; - if (__do_write_check(fd, system, strlen(system) + 1)) { + if (do_write_check(out_handle, system, strlen(system) + 1)) { free(system); return -1; } free(system); - if (read_copy_size4(handle, fd, &count) < 0) + if (read_copy_size4(in_handle, out_handle, &count) < 0) return -1; for (x=0; x < count; x++) { - if (read_copy_size8(handle, fd, &size) < 0) + if (read_copy_size8(in_handle, out_handle, &size) < 0) return -1; - if (read_copy_data(handle, size, fd) < 0) + if (read_copy_data(in_handle, size, out_handle) < 0) return -1; } } - handle->file_state = TRACECMD_FILE_ALL_EVENTS; + in_handle->file_state = TRACECMD_FILE_ALL_EVENTS; return 0; } -static int copy_proc_kallsyms(struct tracecmd_input *handle, int fd) +static int copy_proc_kallsyms(struct tracecmd_input *in_handle, struct tracecmd_output *out_handle) { unsigned int size; - if (handle->file_state != TRACECMD_FILE_KALLSYMS - 1) + if (in_handle->file_state != TRACECMD_FILE_KALLSYMS - 1) return -1; - if (read_copy_size4(handle, fd, &size) < 0) + if (read_copy_size4(in_handle, out_handle, &size) < 0) return -1; if (!size) return 0; /* OK? */ - if (read_copy_data(handle, size, fd) < 0) + if (read_copy_data(in_handle, size, out_handle) < 0) return -1; - handle->file_state = TRACECMD_FILE_KALLSYMS; + in_handle->file_state = TRACECMD_FILE_KALLSYMS; return 0; } -static int copy_ftrace_printk(struct tracecmd_input *handle, int fd) +static int copy_ftrace_printk(struct tracecmd_input *in_handle, struct tracecmd_output *out_handle) { unsigned int size; - if (handle->file_state != TRACECMD_FILE_PRINTK - 1) + if (in_handle->file_state != TRACECMD_FILE_PRINTK - 1) return -1; - if (read_copy_size4(handle, fd, &size) < 0) + if (read_copy_size4(in_handle, out_handle, &size) < 0) return -1; if (!size) return 0; /* OK? */ - if (read_copy_data(handle, size, fd) < 0) + if (read_copy_data(in_handle, size, out_handle) < 0) return -1; - handle->file_state = TRACECMD_FILE_PRINTK; + in_handle->file_state = TRACECMD_FILE_PRINTK; return 0; } -static int copy_command_lines(struct tracecmd_input *handle, int fd) +static int copy_command_lines(struct tracecmd_input *in_handle, struct tracecmd_output *out_handle) { unsigned long long size; - if (handle->file_state != TRACECMD_FILE_CMD_LINES - 1) + if (in_handle->file_state != TRACECMD_FILE_CMD_LINES - 1) return -1; - if (read_copy_size8(handle, fd, &size) < 0) + if (read_copy_size8(in_handle, out_handle, &size) < 0) return -1; if (!size) return 0; /* OK? */ - if (read_copy_data(handle, size, fd) < 0) + if (read_copy_data(in_handle, size, out_handle) < 0) return -1; - handle->file_state = TRACECMD_FILE_CMD_LINES; + in_handle->file_state = TRACECMD_FILE_CMD_LINES; return 0; } /** * tracecmd_copy_headers - Copy headers from a tracecmd_input handle to a file descriptor - * @handle: input handle for the trace.dat file to copy from. - * @fd: The file descriptor to copy to. + * @in_handle: input handle for the trace.dat file to copy from. + * @out_handle: output handle to the trace.dat file to copy to. * @start_state: The file state to start copying from (zero for the beginnig) * @end_state: The file state to stop at (zero for up to cmdlines) * @@ -4613,7 +4617,8 @@ static int copy_command_lines(struct tracecmd_input *handle, int fd) * NOTE: The input handle is also modified, and ends at the end * state as well. */ -int tracecmd_copy_headers(struct tracecmd_input *handle, int fd, +int tracecmd_copy_headers(struct tracecmd_input *in_handle, + struct tracecmd_output *out_handle, enum tracecmd_file_states start_state, enum tracecmd_file_states end_state) { @@ -4631,71 +4636,71 @@ int tracecmd_copy_headers(struct tracecmd_input *handle, int fd, if (end_state < TRACECMD_FILE_HEADERS) return 0; - if (handle->file_state >= start_state) { + if (in_handle->file_state >= start_state) { /* Set the handle to just before the start state */ - sec = section_open(handle, TRACECMD_OPTION_HEADER_INFO); + sec = section_open(in_handle, TRACECMD_OPTION_HEADER_INFO); if (!sec) return -1; /* Now that the file handle has moved, change its state */ - handle->file_state = TRACECMD_FILE_INIT; + in_handle->file_state = TRACECMD_FILE_INIT; } /* Try to bring the input up to the start state - 1 */ - ret = tracecmd_read_headers(handle, start_state - 1); + ret = tracecmd_read_headers(in_handle, start_state - 1); if (sec) - section_close(handle, sec); + section_close(in_handle, sec); if (ret < 0) goto out; switch (start_state) { case TRACECMD_FILE_HEADERS: - ret = copy_header_files(handle, fd); + ret = copy_header_files(in_handle, out_handle); if (ret < 0) goto out; /* fallthrough */ case TRACECMD_FILE_FTRACE_EVENTS: /* handle's state is now updating with the copies */ - if (end_state <= handle->file_state) + if (end_state <= in_handle->file_state) return 0; - ret = copy_ftrace_files(handle, fd); + ret = copy_ftrace_files(in_handle, out_handle); if (ret < 0) goto out; /* fallthrough */ case TRACECMD_FILE_ALL_EVENTS: - if (end_state <= handle->file_state) + if (end_state <= in_handle->file_state) return 0; - ret = copy_event_files(handle, fd); + ret = copy_event_files(in_handle, out_handle); if (ret < 0) goto out; /* fallthrough */ case TRACECMD_FILE_KALLSYMS: - if (end_state <= handle->file_state) + if (end_state <= in_handle->file_state) return 0; - ret = copy_proc_kallsyms(handle, fd); + ret = copy_proc_kallsyms(in_handle, out_handle); if (ret < 0) goto out; /* fallthrough */ case TRACECMD_FILE_PRINTK: - if (end_state <= handle->file_state) + if (end_state <= in_handle->file_state) return 0; - ret = copy_ftrace_printk(handle, fd); + ret = copy_ftrace_printk(in_handle, out_handle); if (ret < 0) goto out; /* fallthrough */ case TRACECMD_FILE_CMD_LINES: - if (end_state <= handle->file_state) + if (end_state <= in_handle->file_state) return 0; - ret = copy_command_lines(handle, fd); + ret = copy_command_lines(in_handle, out_handle); default: break; } diff --git a/lib/trace-cmd/trace-output.c b/lib/trace-cmd/trace-output.c index 89b502b4..2c55e95c 100644 --- a/lib/trace-cmd/trace-output.c +++ b/lib/trace-cmd/trace-output.c @@ -88,8 +88,8 @@ struct list_event_system { #define HAS_SECTIONS(H) ((H)->file_version >= FILE_VERSION_SECTIONS) -static stsize_t -do_write_check(struct tracecmd_output *handle, const void *data, tsize_t size) +__hidden long long +do_write_check(struct tracecmd_output *handle, const void *data, long long size) { if (handle->do_compress) return tracecmd_compress_write(handle->compress, data, size); @@ -2489,7 +2489,7 @@ struct tracecmd_output *tracecmd_copy(struct tracecmd_input *ihandle, goto out_free; tracecmd_output_write_init(handle); - if (tracecmd_copy_headers(ihandle, handle->fd, 0, 0) < 0) + if (tracecmd_copy_headers(ihandle, handle, 0, 0) < 0) goto out_free; handle->file_state = tracecmd_get_file_state(ihandle); From patchwork Mon Sep 13 12:47:48 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: 12488879 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D8D8CC4332F for ; Mon, 13 Sep 2021 12:48:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C24BD6101B for ; Mon, 13 Sep 2021 12:48:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232959AbhIMMuN (ORCPT ); Mon, 13 Sep 2021 08:50:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51118 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235622AbhIMMtP (ORCPT ); Mon, 13 Sep 2021 08:49:15 -0400 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 84A18C061574 for ; Mon, 13 Sep 2021 05:47:59 -0700 (PDT) Received: by mail-wm1-x334.google.com with SMTP id g19-20020a1c9d13000000b003075062d4daso1357758wme.0 for ; Mon, 13 Sep 2021 05:47:59 -0700 (PDT) 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=dWVND2TUoyNPoKgjxvxK2sC5GgTVyBk7UPC5DpBJaxE=; b=Ga8QOfaecMD3oE3GjigWPghxNPA1zSAwPTqO/NDI+GV/QIpgnsuZqj2W6yREleD5Zv jOkimYvgyB6gg21LCahglz3IAcyOuXMsgaiwYmM0/7m9BcsdGclN3/mx8DSts2n+uRFi idIxLfjhDlSOiu0gFOIy2g6vnSBke/1xuBAKPn6pcNqjjFv1nRpGkAiDKfDsPL+z5FEY Uyp083ZiwwNs9ayeGFwYvNlDpf5QnwRYD98tnonWkIr1+FnniMcSIa6IEJFHNS7dO3pJ +aRjeBci9KxCdGMTPWQdccgsV+sMl4iSYdELDvNKSYBlUsAVp6Wlyy87wVhhCJs3SVlM TZjA== 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=dWVND2TUoyNPoKgjxvxK2sC5GgTVyBk7UPC5DpBJaxE=; b=2dA6BhpxpNmcZIA12mYDYc5swGC64KNiPB/pvERxt+6nLhD4++ZzyFC0CC1rT57xxx nc3oDs7o+y3wmmrtp23Wp0l4UZx73dHSdL3ohmVnrgOua0y/V5RL/VS8TspujVgD6eYN gDh/bm42Z1Ai3RZlukTxlXRmxurN6zdc3pb4toeJcnTq8ZXC8RPNFpYHnKh6sXZCUb7u tzV1EO3vBRA161X+C7eUrD/84BtQW8vqLrm4D8yMV7u4DtmB9yr3PuRvuWF33RVJ/YXf qlYKYpvp2scPBOwWVaTcBDHV9LTQbTJr/1xhgNrnvhfenUOLK84gnl1ANWuX2LXKRT22 eIaw== X-Gm-Message-State: AOAM531iXqnuyaWKbM9VxD7Jgr/eJzsEfy6qNevFlrOvgI/Rb+4kru5a k8l3EV5tX/unec7MDkX5aE0gdhfM0bY= X-Google-Smtp-Source: ABdhPJwmHozgmsTJpntEdkuR9pOhO58yALrmv2X/uO9tbpqz43MxnRn5RdXL9bwIfgrMcjHNrAqveA== X-Received: by 2002:a05:600c:4154:: with SMTP id h20mr3623274wmm.172.1631537278122; Mon, 13 Sep 2021 05:47:58 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id k17sm7718077wmj.0.2021.09.13.05.47.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Sep 2021 05:47:57 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 2/8] trace-cmd library: Handle version 7 files when copying headers between files Date: Mon, 13 Sep 2021 15:47:48 +0300 Message-Id: <20210913124754.3679916-3-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210913124754.3679916-1-tz.stoyanov@gmail.com> References: <20210913124754.3679916-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org When copying header data between trace files, handle the file versions. Internal library functions for copying these headers are fixed to work with trace file version 7: copy_header_files copy_ftrace_files copy_event_files copy_proc_kallsyms copy_command_lines Signed-off-by: Tzvetomir Stoyanov (VMware) --- lib/trace-cmd/include/trace-cmd-local.h | 3 + lib/trace-cmd/trace-input.c | 211 ++++++++++++++++++++---- lib/trace-cmd/trace-output.c | 10 ++ lib/trace-cmd/trace-util.c | 8 +- 4 files changed, 200 insertions(+), 32 deletions(-) diff --git a/lib/trace-cmd/include/trace-cmd-local.h b/lib/trace-cmd/include/trace-cmd-local.h index 18d2d05b..92bc288a 100644 --- a/lib/trace-cmd/include/trace-cmd-local.h +++ b/lib/trace-cmd/include/trace-cmd-local.h @@ -46,6 +46,9 @@ int out_uncompress_block(struct tracecmd_output *handle); int out_compression_start(struct tracecmd_output *handle, bool compress); int out_compression_end(struct tracecmd_output *handle, bool compress); void out_compression_reset(struct tracecmd_output *handle, bool compress); +bool out_check_compression(struct tracecmd_output *handle); + +void out_set_file_state(struct tracecmd_output *handle, int new_state); unsigned long long out_copy_fd_compress(struct tracecmd_output *handle, int fd, unsigned long long max, unsigned long long *write_size); diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index 5cc881a8..e4fdc534 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -4443,164 +4443,313 @@ static int read_copy_data(struct tracecmd_input *in_handle, return -1; } + +static bool check_in_state(struct tracecmd_input *handle, int new_state) +{ + return check_file_state(handle->file_version, handle->file_state, new_state); +} + static int copy_header_files(struct tracecmd_input *in_handle, struct tracecmd_output *out_handle) { + bool compress = out_check_compression(out_handle); + struct file_section *sec; + unsigned long long offset; unsigned long long size; - if (in_handle->file_state != TRACECMD_FILE_HEADERS - 1) + if (!check_in_state(in_handle, TRACECMD_FILE_HEADERS) || + !check_out_state(out_handle, TRACECMD_FILE_HEADERS)) return -1; + sec = section_open(in_handle, TRACECMD_OPTION_HEADER_INFO); + if (!sec) + return -1; + offset = out_write_section_header(out_handle, TRACECMD_OPTION_HEADER_INFO, + "headers", TRACECMD_SEC_FL_COMPRESS, true); + out_compression_start(out_handle, compress); + /* "header_page" */ if (read_copy_data(in_handle, 12, out_handle) < 0) - return -1; + goto error; if (read_copy_size8(in_handle, out_handle, &size) < 0) - return -1; + goto error; if (read_copy_data(in_handle, size, out_handle) < 0) - return -1; + goto error; /* "header_event" */ if (read_copy_data(in_handle, 13, out_handle) < 0) - return -1; + goto error; if (read_copy_size8(in_handle, out_handle, &size) < 0) - return -1; + goto error; if (read_copy_data(in_handle, size, out_handle) < 0) - return -1; + goto error; in_handle->file_state = TRACECMD_FILE_HEADERS; + if (out_compression_end(out_handle, compress)) + goto error; + out_set_file_state(out_handle, in_handle->file_state); + section_close(in_handle, sec); + + if (out_update_section_header(out_handle, offset)) + goto error; return 0; +error: + out_compression_reset(out_handle, compress); + section_close(in_handle, sec); + return -1; } static int copy_ftrace_files(struct tracecmd_input *in_handle, struct tracecmd_output *out_handle) { + bool compress = out_check_compression(out_handle); + struct file_section *sec; + unsigned long long offset; unsigned long long size; unsigned int count; unsigned int i; - if (in_handle->file_state != TRACECMD_FILE_FTRACE_EVENTS - 1) + if (!check_in_state(in_handle, TRACECMD_FILE_FTRACE_EVENTS) || + !check_out_state(out_handle, TRACECMD_FILE_FTRACE_EVENTS)) return -1; - if (read_copy_size4(in_handle, out_handle, &count) < 0) + sec = section_open(in_handle, TRACECMD_OPTION_FTRACE_EVENTS); + if (!sec) return -1; + offset = out_write_section_header(out_handle, TRACECMD_OPTION_FTRACE_EVENTS, + "ftrace events", TRACECMD_SEC_FL_COMPRESS, true); + + out_compression_start(out_handle, compress); + + if (read_copy_size4(in_handle, out_handle, &count) < 0) + goto error; for (i = 0; i < count; i++) { if (read_copy_size8(in_handle, out_handle, &size) < 0) - return -1; + goto error; if (read_copy_data(in_handle, size, out_handle) < 0) - return -1; + goto error; } in_handle->file_state = TRACECMD_FILE_FTRACE_EVENTS; + if (out_compression_end(out_handle, compress)) + goto error; + out_set_file_state(out_handle, in_handle->file_state); + + section_close(in_handle, sec); + + if (out_update_section_header(out_handle, offset)) + goto error; return 0; +error: + out_compression_reset(out_handle, compress); + section_close(in_handle, sec); + return -1; } static int copy_event_files(struct tracecmd_input *in_handle, struct tracecmd_output *out_handle) { + bool compress = out_check_compression(out_handle); + struct file_section *sec; + unsigned long long offset; unsigned long long size; char *system; unsigned int systems; unsigned int count; unsigned int i,x; - if (in_handle->file_state != TRACECMD_FILE_ALL_EVENTS - 1) + if (!check_in_state(in_handle, TRACECMD_FILE_ALL_EVENTS) || + !check_out_state(out_handle, TRACECMD_FILE_ALL_EVENTS)) return -1; - if (read_copy_size4(in_handle, out_handle, &systems) < 0) + sec = section_open(in_handle, TRACECMD_OPTION_EVENT_FORMATS); + if (!sec) return -1; + offset = out_write_section_header(out_handle, TRACECMD_OPTION_EVENT_FORMATS, + "events format", TRACECMD_SEC_FL_COMPRESS, true); + + out_compression_start(out_handle, compress); + + if (read_copy_size4(in_handle, out_handle, &systems) < 0) + goto error; for (i = 0; i < systems; i++) { system = read_string(in_handle); if (!system) - return -1; + goto error; if (do_write_check(out_handle, system, strlen(system) + 1)) { free(system); - return -1; + goto error; } free(system); if (read_copy_size4(in_handle, out_handle, &count) < 0) - return -1; + goto error; for (x=0; x < count; x++) { if (read_copy_size8(in_handle, out_handle, &size) < 0) - return -1; + goto error; if (read_copy_data(in_handle, size, out_handle) < 0) - return -1; + goto error; } } in_handle->file_state = TRACECMD_FILE_ALL_EVENTS; + if (out_compression_end(out_handle, compress)) + goto error; + out_set_file_state(out_handle, in_handle->file_state); + + section_close(in_handle, sec); + + if (out_update_section_header(out_handle, offset)) + goto error; return 0; +error: + out_compression_reset(out_handle, compress); + section_close(in_handle, sec); + return -1; } static int copy_proc_kallsyms(struct tracecmd_input *in_handle, struct tracecmd_output *out_handle) { + bool compress = out_check_compression(out_handle); + struct file_section *sec; + unsigned long long offset; unsigned int size; - if (in_handle->file_state != TRACECMD_FILE_KALLSYMS - 1) + if (!check_in_state(in_handle, TRACECMD_FILE_KALLSYMS) || + !check_out_state(out_handle, TRACECMD_FILE_KALLSYMS)) return -1; - if (read_copy_size4(in_handle, out_handle, &size) < 0) + sec = section_open(in_handle, TRACECMD_OPTION_KALLSYMS); + if (!sec) return -1; + offset = out_write_section_header(out_handle, TRACECMD_OPTION_KALLSYMS, + "kallsyms", TRACECMD_SEC_FL_COMPRESS, true); + + out_compression_start(out_handle, compress); + if (read_copy_size4(in_handle, out_handle, &size) < 0) + goto error; if (!size) - return 0; /* OK? */ + goto out; /* OK? */ if (read_copy_data(in_handle, size, out_handle) < 0) - return -1; - + goto error; +out: in_handle->file_state = TRACECMD_FILE_KALLSYMS; + if (out_compression_end(out_handle, compress)) + goto error; + out_set_file_state(out_handle, in_handle->file_state); + + section_close(in_handle, sec); + + if (out_update_section_header(out_handle, offset)) + goto error; return 0; +error: + out_compression_reset(out_handle, compress); + section_close(in_handle, sec); + return -1; } static int copy_ftrace_printk(struct tracecmd_input *in_handle, struct tracecmd_output *out_handle) { + bool compress = out_check_compression(out_handle); + struct file_section *sec; + unsigned long long offset; unsigned int size; - if (in_handle->file_state != TRACECMD_FILE_PRINTK - 1) + if (!check_in_state(in_handle, TRACECMD_FILE_PRINTK) || + !check_out_state(out_handle, TRACECMD_FILE_PRINTK)) return -1; - if (read_copy_size4(in_handle, out_handle, &size) < 0) + sec = section_open(in_handle, TRACECMD_OPTION_PRINTK); + if (!sec) return -1; + offset = out_write_section_header(out_handle, TRACECMD_OPTION_PRINTK, + "printk", TRACECMD_SEC_FL_COMPRESS, true); + + out_compression_start(out_handle, compress); + + if (read_copy_size4(in_handle, out_handle, &size) < 0) + goto error; if (!size) - return 0; /* OK? */ + goto out; /* OK? */ if (read_copy_data(in_handle, size, out_handle) < 0) - return -1; + goto error; +out: in_handle->file_state = TRACECMD_FILE_PRINTK; + if (out_compression_end(out_handle, compress)) + goto error; + out_set_file_state(out_handle, in_handle->file_state); + + section_close(in_handle, sec); + + if (out_update_section_header(out_handle, offset)) + goto error; return 0; +error: + out_compression_reset(out_handle, compress); + section_close(in_handle, sec); + return -1; } static int copy_command_lines(struct tracecmd_input *in_handle, struct tracecmd_output *out_handle) { + bool compress = out_check_compression(out_handle); + struct file_section *sec; + unsigned long long offset; unsigned long long size; - if (in_handle->file_state != TRACECMD_FILE_CMD_LINES - 1) + if (!check_in_state(in_handle, TRACECMD_FILE_CMD_LINES) || + !check_out_state(out_handle, TRACECMD_FILE_CMD_LINES)) return -1; - if (read_copy_size8(in_handle, out_handle, &size) < 0) + sec = section_open(in_handle, TRACECMD_OPTION_CMDLINES); + if (!sec) return -1; + offset = out_write_section_header(out_handle, TRACECMD_OPTION_CMDLINES, + "command lines", TRACECMD_SEC_FL_COMPRESS, true); + + out_compression_start(out_handle, compress); + + if (read_copy_size8(in_handle, out_handle, &size) < 0) + goto error; if (!size) - return 0; /* OK? */ + goto out; /* OK? */ if (read_copy_data(in_handle, size, out_handle) < 0) - return -1; + goto error; +out: in_handle->file_state = TRACECMD_FILE_CMD_LINES; + if (out_compression_end(out_handle, compress)) + goto error; + out_set_file_state(out_handle, in_handle->file_state); + + section_close(in_handle, sec); + + if (out_update_section_header(out_handle, offset)) + goto error; return 0; +error: + out_compression_reset(out_handle, compress); + section_close(in_handle, sec); + return -1; } /** diff --git a/lib/trace-cmd/trace-output.c b/lib/trace-cmd/trace-output.c index 2c55e95c..ea5407f1 100644 --- a/lib/trace-cmd/trace-output.c +++ b/lib/trace-cmd/trace-output.c @@ -2502,11 +2502,21 @@ out_free: return NULL; } +__hidden void out_set_file_state(struct tracecmd_output *handle, int new_state) +{ + handle->file_state = new_state; +} + __hidden bool check_out_state(struct tracecmd_output *handle, int new_state) { return check_file_state(handle->file_version, handle->file_state, new_state); } +__hidden bool out_check_compression(struct tracecmd_output *handle) +{ + return (handle->compress != NULL); +} + /** * tracecmd_get_out_file_version - return the trace.dat file version * @handle: output handle for the trace.dat file diff --git a/lib/trace-cmd/trace-util.c b/lib/trace-cmd/trace-util.c index 97db6ed9..e9e444cd 100644 --- a/lib/trace-cmd/trace-util.c +++ b/lib/trace-cmd/trace-util.c @@ -637,6 +637,12 @@ static void __attribute__((destructor)) tracecmd_lib_free(void) __hidden bool check_file_state(unsigned long file_version, int current_state, int new_state) { + if (file_version >= FILE_VERSION_SECTIONS) { + if (current_state < TRACECMD_FILE_INIT) + return false; + return true; + } + switch (new_state) { case TRACECMD_FILE_HEADERS: case TRACECMD_FILE_FTRACE_EVENTS: @@ -649,7 +655,7 @@ __hidden bool check_file_state(unsigned long file_version, int current_state, in return true; break; case TRACECMD_FILE_OPTIONS: - if (current_state == (new_state - 1)) + if (file_version < FILE_VERSION_SECTIONS && current_state == TRACECMD_FILE_CPU_COUNT) return true; break; case TRACECMD_FILE_CPU_LATENCY: From patchwork Mon Sep 13 12:47:49 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: 12488881 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 870A6C433F5 for ; Mon, 13 Sep 2021 12:49:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 63C1B61051 for ; Mon, 13 Sep 2021 12:49:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235044AbhIMMuN (ORCPT ); Mon, 13 Sep 2021 08:50:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51124 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235736AbhIMMtQ (ORCPT ); Mon, 13 Sep 2021 08:49:16 -0400 Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 48B9FC061574 for ; Mon, 13 Sep 2021 05:48:00 -0700 (PDT) Received: by mail-wr1-x436.google.com with SMTP id d21so6807737wra.12 for ; Mon, 13 Sep 2021 05:48:00 -0700 (PDT) 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=nnct16Eyu0jAjQS8nc7YZ5zULEAJIbnBL0K7rY7k9m8=; b=Lz6YF01MT8MDwgMMXYwfGVCVz/3QH8YDXXSiY1GjoYauV41FJgANya4eFirpPNemZA CPGNyUhVRwnBQoYH9prnltumrTBquw/YeO5VxwVDGSl8z7cvbWXBjB6VUm4qr3dMd3jY 3pU+Dam/EGa2A6uSxXb1sxmuJsUf9ZO1QKYgfjDbcVTSxsxHEGSCS6PdBrxq6mczgDNO /I25t54CsJAiR+OS1XCF02eDeSvZ8xg/CLO/WdMGvf9Ain0apydWDtqCX3TLGxsPBMLx F46DNjKraBLRSgmTOk+4mhrmX2p3XGYMzhbfhP9Seb2nXi5+6RQB6R2e7RWSnGL9+20F 5BPw== 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=nnct16Eyu0jAjQS8nc7YZ5zULEAJIbnBL0K7rY7k9m8=; b=FIsS12WVKANVO0zuSKTMJQ8GjjxwA6xm05QiYQoQ6YePrmoE0xtgG4vxVcZFoqzZ8x GLmulAi6gAyzAFr0uso5yKYyRI413uWqU0TEhGIMOc6wCUUqUD0mSVN8yPnFDZ9tMyPt H1QUeAeLjNiA+4GJ2nDK/l6HpKRgjegzK6OiWZSB6YyGevt1ujH8WpoQeorOZ+hFqO0n VxJMnVxOo0bJ4zO2H6+lXj7tMcHU2QL3EOeephY8hBn70fDTvUxDDfvdO9rmEmkIKxTH gZ3AsAKro8piphUNnhnq2MzD9fy5uJxp133RbQXYmChvZOHIKRJbUvmYlc2nbajpdMFN vKqw== X-Gm-Message-State: AOAM531tOxoJ5T3tCRQRKbMy7GcRE0GnBEGl2P/vgqxqDDjpu3ctd7aA na6O5787vAWtzjXbqp5cqXsvuF2Bzgs= X-Google-Smtp-Source: ABdhPJzSDHwSAlmaI//L9QqUJi5A24VbnhO0vYwy+5ogwV06nNj0IDMMnpQWpUN9RM8AV8cQ0Xqskw== X-Received: by 2002:adf:f6c7:: with SMTP id y7mr596257wrp.44.1631537278949; Mon, 13 Sep 2021 05:47:58 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id k17sm7718077wmj.0.2021.09.13.05.47.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Sep 2021 05:47:58 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 3/8] trace-cmd library: Copy CPU count between trace files Date: Mon, 13 Sep 2021 15:47:49 +0300 Message-Id: <20210913124754.3679916-4-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210913124754.3679916-1-tz.stoyanov@gmail.com> References: <20210913124754.3679916-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org The tracecmd_copy_headers() API should be able to copy CPU count also, as it is part of the headers. Signed-off-by: Tzvetomir Stoyanov (VMware) --- lib/trace-cmd/trace-input.c | 40 +++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index e4fdc534..c3d7075f 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -4752,6 +4752,33 @@ error: return -1; } +static int copy_cpu_count(struct tracecmd_input *in_handle, struct tracecmd_output *out_handle) +{ + unsigned int cpus; + + if (!check_in_state(in_handle, TRACECMD_FILE_CPU_COUNT) || + !check_out_state(out_handle, TRACECMD_FILE_CPU_COUNT)) + return -1; + + if (!HAS_SECTIONS(in_handle)) { + if (read4(in_handle, &cpus)) + return -1; + } else { + cpus = in_handle->max_cpu; + } + + if (tracecmd_get_out_file_version(out_handle) < FILE_VERSION_SECTIONS) { + cpus = tep_read_number(in_handle->pevent, &cpus, 4); + if (do_write_check(out_handle, &cpus, 4)) + return -1; + } + + in_handle->file_state = TRACECMD_FILE_CPU_COUNT; + out_set_file_state(out_handle, in_handle->file_state); + + return 0; +} + /** * tracecmd_copy_headers - Copy headers from a tracecmd_input handle to a file descriptor * @in_handle: input handle for the trace.dat file to copy from. @@ -4850,6 +4877,19 @@ int tracecmd_copy_headers(struct tracecmd_input *in_handle, return 0; ret = copy_command_lines(in_handle, out_handle); + if (ret < 0) + goto out; + + /* fallthrough */ + case TRACECMD_FILE_CPU_COUNT: + if (end_state <= in_handle->file_state) + return 0; + + ret = copy_cpu_count(in_handle, out_handle); + if (ret < 0) + goto out; + + /* fallthrough */ default: break; } From patchwork Mon Sep 13 12:47:50 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: 12488865 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.9 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNWANTED_LANGUAGE_BODY, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 360BCC433EF for ; Mon, 13 Sep 2021 12:48:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 190C160FBF for ; Mon, 13 Sep 2021 12:48:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236340AbhIMMtV (ORCPT ); Mon, 13 Sep 2021 08:49:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51128 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235805AbhIMMtR (ORCPT ); Mon, 13 Sep 2021 08:49:17 -0400 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 424FEC061574 for ; Mon, 13 Sep 2021 05:48:01 -0700 (PDT) Received: by mail-wm1-x329.google.com with SMTP id g19-20020a1c9d13000000b003075062d4daso1357822wme.0 for ; Mon, 13 Sep 2021 05:48:01 -0700 (PDT) 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=tYi/OxG9q8YttiGK2z4ezEmgneh7bLVQNh0Z0ehABFo=; b=l4Ac9PkXdfLYya0VmjKZU+Xt+B+h2xb/eA1rhR2k3Ead6gLsXvyCtPTU8+pB8JhSlh qZZA3p13uwCgo/E0ZJK8m9TWpB6wIAViYcgIvrX7vkAOpmbX4L3IwX/aZKn3OKu4H01j v9T+CmuDRoinmzBvYuKhZKVq9m9wrKvbtlGIM7pIysS/MlWn3JAeY0y+GNMV5uq0jq4Z s/jcd6JwRS+5E2mp3doaFtbHKz7quKDq5CM2D0PnHooYVXg1u67lM2RErmewP83uWNoS g0eiCdtX2Bh4PHrsYz9S5t4LJOVLQg5QFQvRqhCjgPnmowfSmC2oIhLf/F7c3oWDMzhA zeBg== 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=tYi/OxG9q8YttiGK2z4ezEmgneh7bLVQNh0Z0ehABFo=; b=1f8ptDUB2+VBYteDEvg2o0MDkMYUkzPJfnDjMvBm25JusQRR8ZPKlmu+UtvZJ5VsBE uiHEVXSlQirDrXPWR1qI1Vl9yEdt6QmyWHTmuPxpEVY2iWd+Qa6mysbSowkUHsRUsSyp 7g5gkRvV1bBG/JbwC/A6VK0oT3Jg1lxYiAyk9Mey3HNbWgoceDAirssmM5n1gNalHf5S 4Pk2qCsOLIO+OErtisLDyVZntghzdHyJKmR/T3ag2f3RbMpDyB/7MrxlTy02KIWMyMv/ gFYTO/09ejMTJkDyAIptzkP0d/LPgab8+JrZIlTxxUlZTPMwZVgVBNggzisM2jlSkRYJ aMeA== X-Gm-Message-State: AOAM53063EYtHt5axe9UgSxX67D1rqsuiX2JzJf1c8kfIxi/boyXiHos bUgXIDyW7jtfExaH1RQWR+ThzwDTxt4= X-Google-Smtp-Source: ABdhPJyoK62ZMmDO3uECyglIYY/c4aooC841u9+/QO3J0JrKLvB4tpsi8HhXqccQzFvOoOJSpkDDAg== X-Received: by 2002:a1c:7d0f:: with SMTP id y15mr10341748wmc.41.1631537279862; Mon, 13 Sep 2021 05:47:59 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id k17sm7718077wmj.0.2021.09.13.05.47.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Sep 2021 05:47:59 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 4/8] trace-cmd library: New API to copy buffer description between trace files Date: Mon, 13 Sep 2021 15:47:50 +0300 Message-Id: <20210913124754.3679916-5-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210913124754.3679916-1-tz.stoyanov@gmail.com> References: <20210913124754.3679916-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org A new library API is introduced, to copy buffers description between trace files: tracecmd_copy_buffer_descr() Signed-off-by: Tzvetomir Stoyanov (VMware) --- lib/trace-cmd/include/private/trace-cmd-private.h | 2 ++ lib/trace-cmd/trace-input.c | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/lib/trace-cmd/include/private/trace-cmd-private.h b/lib/trace-cmd/include/private/trace-cmd-private.h index 8ff46d52..4930063e 100644 --- a/lib/trace-cmd/include/private/trace-cmd-private.h +++ b/lib/trace-cmd/include/private/trace-cmd-private.h @@ -189,6 +189,8 @@ int tracecmd_copy_headers(struct tracecmd_input *in_handle, struct tracecmd_output *out_handle, enum tracecmd_file_states start_state, enum tracecmd_file_states end_state); +int tracecmd_copy_buffer_descr(struct tracecmd_input *in_handle, + struct tracecmd_output *out_handle); void tracecmd_set_flag(struct tracecmd_input *handle, int flag); void tracecmd_clear_flag(struct tracecmd_input *handle, int flag); unsigned long tracecmd_get_flags(struct tracecmd_input *handle); diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index c3d7075f..86010dd5 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -4898,6 +4898,19 @@ int tracecmd_copy_headers(struct tracecmd_input *in_handle, return ret < 0 ? -1 : 0; } +int tracecmd_copy_buffer_descr(struct tracecmd_input *in_handle, + struct tracecmd_output *out_handle) +{ + int i; + + if (tracecmd_get_out_file_version(out_handle) >= FILE_VERSION_SECTIONS) + return 0; + + for (i = 0; i < in_handle->nr_buffers; i++) + tracecmd_add_buffer_info(out_handle, in_handle->buffers[i].name, 0); + return tracecmd_write_buffer_info(out_handle); +} + /** * tracecmd_record_at_buffer_start - return true if record is first on subbuffer * @handle: input handle for the trace.dat file From patchwork Mon Sep 13 12:47:51 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: 12488871 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 843BAC433FE for ; Mon, 13 Sep 2021 12:48:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 646A360FBF for ; Mon, 13 Sep 2021 12:48:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229726AbhIMMtW (ORCPT ); Mon, 13 Sep 2021 08:49:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51136 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235873AbhIMMtS (ORCPT ); Mon, 13 Sep 2021 08:49:18 -0400 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 38F8CC061574 for ; Mon, 13 Sep 2021 05:48:02 -0700 (PDT) Received: by mail-wr1-x42c.google.com with SMTP id w29so13736760wra.8 for ; Mon, 13 Sep 2021 05:48:02 -0700 (PDT) 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=matuFq6H6i0qbHJXWFaKOMMxeA0NYNSyErN/+iJFQ5o=; b=dt4ztwmdKe3a7JeGL+gzYSj9HL/mfs3JogbYnKUXI/BxdfmxbpwGhKuPsxBTcKkCYS Sfl1JevZgKOeNuJEBbT4QYt6jW+OsK5jmKKNFYkM79oSuHh8BkMr4xQjChSKPsgaOvix +Jubp1doSYIrUIJ2//fW5wiNXMcCIMRZpHkKJzLTp16j3Xz9gK+e82YEeFNLQqv142tY 7zTMX2tyqObDOh+yv2EWbHIUuq0vkkRbD3KZvhfPJ52i/avx9HXNoO4khfAlvyRtDH9f Mnshy/3VFlSJ64x9TM+YvNUDHfdVIWxAPZc6F33YNGS/+rzEzkSfRsTyrQ3L6Ugc746t l+vw== 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=matuFq6H6i0qbHJXWFaKOMMxeA0NYNSyErN/+iJFQ5o=; b=t19q4EyeSofvNznbaVO503M6OCFbRHlnScf0kqG0z5CuSLJUQWqY8crNEMW0t7bMdY GNdEJyr1KsxryiRdu+PdW5ZGJdgZIaFdWMhO3rjXZVziDSBBxCdPGJh7FMyokGXe3b9e G0WRD8HNAAglCddk0J4CBcrE2md15kMXm8GqV8Bk4y7OkAskDECU4aXKlvjtMkVGNn4b kbbdMxQJ+HW/eJa/4eOgkjmI+iUWC8Lw5Pa9US4Ey3/ZdUx+W4Rxyv/EbHtAGah5VJ2f 3J+Q6elxqwszdVlvhFaMncvQGqN73N1TAzhrk2M7iR0fVk1xZzYoUFjCyFfm1KKUIjQ4 KnzQ== X-Gm-Message-State: AOAM530RvuxGQE51v1/UOOTMKVbfRRURpri9T7bW54uyyrIUeOPlljFy Em46B2+42K1MLn3XsRZ7PGyea7ntZyU= X-Google-Smtp-Source: ABdhPJz+sZfmvw5swb/f1P4++qfC/TJF9KumVTBKEuEQ+bHwa3nbMv6NeweRAqMtNnv+hrztbTu8PQ== X-Received: by 2002:adf:d4cb:: with SMTP id w11mr12502265wrk.125.1631537280838; Mon, 13 Sep 2021 05:48:00 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id k17sm7718077wmj.0.2021.09.13.05.47.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Sep 2021 05:48:00 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 5/8] trace-cmd library: New API to copy options between trace files Date: Mon, 13 Sep 2021 15:47:51 +0300 Message-Id: <20210913124754.3679916-6-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210913124754.3679916-1-tz.stoyanov@gmail.com> References: <20210913124754.3679916-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org A new library API is introduced, to copy options between trace files: tracecmd_copy_options() Signed-off-by: Tzvetomir Stoyanov (VMware) --- .../include/private/trace-cmd-private.h | 3 + lib/trace-cmd/include/trace-cmd-local.h | 2 + lib/trace-cmd/trace-input.c | 140 ++++++++++++++++++ lib/trace-cmd/trace-output.c | 30 ++++ 4 files changed, 175 insertions(+) diff --git a/lib/trace-cmd/include/private/trace-cmd-private.h b/lib/trace-cmd/include/private/trace-cmd-private.h index 4930063e..e68f75b8 100644 --- a/lib/trace-cmd/include/private/trace-cmd-private.h +++ b/lib/trace-cmd/include/private/trace-cmd-private.h @@ -191,6 +191,8 @@ int tracecmd_copy_headers(struct tracecmd_input *in_handle, enum tracecmd_file_states end_state); int tracecmd_copy_buffer_descr(struct tracecmd_input *in_handle, struct tracecmd_output *out_handle); +int tracecmd_copy_options(struct tracecmd_input *in_handle, + struct tracecmd_output *out_handle); void tracecmd_set_flag(struct tracecmd_input *handle, int flag); void tracecmd_clear_flag(struct tracecmd_input *handle, int flag); unsigned long tracecmd_get_flags(struct tracecmd_input *handle); @@ -337,6 +339,7 @@ int tracecmd_append_buffer_cpu_data(struct tracecmd_output *handle, const char *name, int cpus, char * const *cpu_data_files); struct tracecmd_output *tracecmd_get_output_handle_fd(int fd); unsigned long tracecmd_get_out_file_version(struct tracecmd_output *handle); +unsigned long long tracecmd_get_out_file_offset(struct tracecmd_output *handle); /* --- 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 92bc288a..706ba250 100644 --- a/lib/trace-cmd/include/trace-cmd-local.h +++ b/lib/trace-cmd/include/trace-cmd-local.h @@ -49,6 +49,8 @@ void out_compression_reset(struct tracecmd_output *handle, bool compress); bool out_check_compression(struct tracecmd_output *handle); void out_set_file_state(struct tracecmd_output *handle, int new_state); +int out_save_options_offset(struct tracecmd_output *handle, + unsigned long long start); unsigned long long out_copy_fd_compress(struct tracecmd_output *handle, int fd, unsigned long long max, unsigned long long *write_size); diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index 86010dd5..2e5e9bcc 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -4911,6 +4911,146 @@ int tracecmd_copy_buffer_descr(struct tracecmd_input *in_handle, return tracecmd_write_buffer_info(out_handle); } +static int copy_options_recursive(struct tracecmd_input *in_handle, + struct tracecmd_output *out_handle) +{ + unsigned short id, flags = 0; + unsigned short option, en2; + unsigned long long next; + unsigned int size, en4; + bool skip; + + for (;;) { + if (do_read_check(in_handle, &option, 2)) + return -1; + en2 = tep_read_number(in_handle->pevent, &option, 2); + + if (en2 == TRACECMD_OPTION_DONE && !HAS_SECTIONS(in_handle)) + return 0; + + /* next 4 bytes is the size of the option */ + if (do_read_check(in_handle, &size, 4)) + return -1; + en4 = tep_read_number(in_handle->pevent, &size, 4); + if (en2 == TRACECMD_OPTION_DONE) { + /* option done v7 */ + if (en4 < 8) + return -1; + if (read8(in_handle, &next)) + return -1; + if (!next) + break; + if (do_lseek(in_handle, next, SEEK_SET) == (off64_t)-1) + return -1; + if (read_section_header(in_handle, &id, &flags, NULL, NULL)) + return -1; + if (id != TRACECMD_OPTION_DONE) + return -1; + if (flags & TRACECMD_SEC_FL_COMPRESS && in_uncompress_block(in_handle)) + return -1; + return copy_options_recursive(in_handle, out_handle); + } + /* Do not copy these, as they have file specific offsets */ + switch (en2) { + case TRACECMD_OPTION_BUFFER: + case TRACECMD_OPTION_BUFFER_TEXT: + case TRACECMD_OPTION_HEADER_INFO: + case TRACECMD_OPTION_FTRACE_EVENTS: + case TRACECMD_OPTION_EVENT_FORMATS: + case TRACECMD_OPTION_KALLSYMS: + case TRACECMD_OPTION_PRINTK: + case TRACECMD_OPTION_CMDLINES: + skip = true; + break; + default: + skip = false; + break; + } + if (skip) { + do_lseek(in_handle, en4, SEEK_CUR); + continue; + } + if (do_write_check(out_handle, &option, 2)) + return -1; + if (do_write_check(out_handle, &size, 4)) + return -1; + if (read_copy_data(in_handle, en4, out_handle)) + return -1; + } + + return 0; +} + +static int copy_options(struct tracecmd_input *in_handle, struct tracecmd_output *out_handle) +{ + unsigned long long offset, start; + unsigned short id, en2, flags = 0; + int tmp; + + if (HAS_SECTIONS(in_handle)) { + if (read_section_header(in_handle, &id, &flags, NULL, NULL)) + return -1; + if (id != TRACECMD_OPTION_DONE) + return -1; + if (flags & TRACECMD_SEC_FL_COMPRESS && in_uncompress_block(in_handle)) + return -1; + } + start = tracecmd_get_out_file_offset(out_handle); + if (tracecmd_get_out_file_version(out_handle) < FILE_VERSION_SECTIONS) { + if (do_write_check(out_handle, "options ", 10)) + return -1; + } + + offset = out_write_section_header(out_handle, TRACECMD_OPTION_DONE, "options", 0, false); + + if (copy_options_recursive(in_handle, out_handle)) + goto error; + + id = TRACECMD_OPTION_DONE; + en2 = tep_read_number(in_handle->pevent, &id, 2); + if (do_write_check(out_handle, &en2, 2)) + goto error; + if (tracecmd_get_out_file_version(out_handle) < FILE_VERSION_SECTIONS) { + out_save_options_offset(out_handle, start); + } else { + tmp = 8; + if (do_write_check(out_handle, &tmp, 4)) + goto error; + out_save_options_offset(out_handle, start); + start = 0; + if (do_write_check(out_handle, &start, 8)) + goto error; + } + out_update_section_header(out_handle, offset); + if (flags & TRACECMD_SEC_FL_COMPRESS) + in_uncompress_reset(in_handle); + in_handle->file_state = TRACECMD_FILE_OPTIONS; + out_set_file_state(out_handle, in_handle->file_state); + /* Append local options */ + return tracecmd_append_options(out_handle); + +error: + if (flags & TRACECMD_SEC_FL_COMPRESS) + in_uncompress_reset(in_handle); + return 0; +} + +int tracecmd_copy_options(struct tracecmd_input *in_handle, + struct tracecmd_output *out_handle) +{ + if (!check_in_state(in_handle, TRACECMD_FILE_OPTIONS) || + !check_out_state(out_handle, TRACECMD_FILE_OPTIONS)) + return -1; + + if (!in_handle->options_start) + return 0; + if (lseek64(in_handle->fd, in_handle->options_start, SEEK_SET) == (off64_t)-1) + return -1; + if (copy_options(in_handle, out_handle) < 0) + return -1; + return 0; +} + /** * tracecmd_record_at_buffer_start - return true if record is first on subbuffer * @handle: input handle for the trace.dat file diff --git a/lib/trace-cmd/trace-output.c b/lib/trace-cmd/trace-output.c index ea5407f1..8021ffb6 100644 --- a/lib/trace-cmd/trace-output.c +++ b/lib/trace-cmd/trace-output.c @@ -2517,6 +2517,31 @@ __hidden bool out_check_compression(struct tracecmd_output *handle) return (handle->compress != NULL); } +__hidden int out_save_options_offset(struct tracecmd_output *handle, unsigned long long start) +{ + unsigned long long new, en8; + + if (HAS_SECTIONS(handle)) { + /* Append to the previous options section, if any */ + if (!handle->options_start) + return -1; + new = do_lseek(handle, 0, SEEK_CUR); + if (do_lseek(handle, handle->options_start, SEEK_SET) == (off64_t)-1) + return -1; + + en8 = convert_endian_8(handle, start); + if (do_write_check(handle, &en8, 8)) + return -1; + handle->options_start = new; + if (do_lseek(handle, new, SEEK_SET) == (off64_t)-1) + return -1; + } else { + handle->options_start = start; + } + + return 0; +} + /** * tracecmd_get_out_file_version - return the trace.dat file version * @handle: output handle for the trace.dat file @@ -2525,3 +2550,8 @@ unsigned long tracecmd_get_out_file_version(struct tracecmd_output *handle) { return handle->file_version; } + +unsigned long long tracecmd_get_out_file_offset(struct tracecmd_output *handle) +{ + return do_lseek(handle, 0, SEEK_CUR); +} From patchwork Mon Sep 13 12:47:52 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: 12488873 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.9 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNWANTED_LANGUAGE_BODY, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DBACCC433F5 for ; Mon, 13 Sep 2021 12:48:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C33EF6101B for ; Mon, 13 Sep 2021 12:48:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229732AbhIMMtW (ORCPT ); Mon, 13 Sep 2021 08:49:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51140 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235933AbhIMMtS (ORCPT ); Mon, 13 Sep 2021 08:49:18 -0400 Received: from mail-wm1-x336.google.com (mail-wm1-x336.google.com [IPv6:2a00:1450:4864:20::336]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 16E50C061574 for ; Mon, 13 Sep 2021 05:48:03 -0700 (PDT) Received: by mail-wm1-x336.google.com with SMTP id i3so6572736wmq.3 for ; Mon, 13 Sep 2021 05:48:03 -0700 (PDT) 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=feCVGlTfCqkMNX7xuX3pLK7rOhc4wmoLC7irXhcqNaM=; b=LRY67PrrICnRuRhn98Hs6hSuQykALWvzh3nQ7xbd2aXFoAHnQah4gIedkY9h52k9Us ov8CDMFOdIRSI7d8FKME0oTKMo794I2Ou7EFzUA9U5bMsps72XiNJLLf97+jTlX4UX5p cnVaMlV2qCkWS5CrboJPYmLRxYdKBx6q3Z4AWiypadAEvzpvsEbkQTSQVOcNeKPlWWUN 3GJRzrNsjmdBgFjD5hPmoydkI+McSrvMdCBUhqzqRfSwNL7hKf4zohK5vTJooQmV7lfb xT/EkM/SGp+9cEIuw+FVD+Jrrcju9sSV8wsutd55/KiGSePFBdE/3/GAehd8v3+0qoHz E4Hg== 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=feCVGlTfCqkMNX7xuX3pLK7rOhc4wmoLC7irXhcqNaM=; b=lMtOsd56V+5wT814mlbou0POrD2O89aATPMEhG/fnhazPoRA6jdNZN1LaHr/lAgw3O CKq/oqp/qN6vg7MWDfENfJmMUcsksoR10AwQ381A6kcs4CNWDQdcDitVBWEhTgx7VyUM E1GcflAeG4Vd8GIKEOEITYvkP6OJIuq4zsgxu3X1MfnU3tXeRc7R/WYe7y8rNPieaX7q GwfvBUS231rRL/yt+f/o3nOaDMCki/Ou+FWEU2/b0r6XR6Cgp7ULK57w6e3+WiCUDTTB PP+TJLtknvcThRPWrs6BcnSDr2Ilek9W47vdLVV2drOb725xkS24yZYNhO405Bf1MZBe NTuA== X-Gm-Message-State: AOAM530kOEzNaTPrMMQUhsLr1yt5NF0q8ePZR4Wo9qitK5wwupmdvIIC wGXn19Wxn0yYTCCVVEYNzTAWAL1C4Zs= X-Google-Smtp-Source: ABdhPJzUO3UshmuON2x9/rKrWxxykgP3w0p7GWx1ufLz7cvE3A+ipYunbSz9NaSe3WV16psZSbQqZw== X-Received: by 2002:a05:600c:3790:: with SMTP id o16mr10970413wmr.157.1631537281619; Mon, 13 Sep 2021 05:48:01 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id k17sm7718077wmj.0.2021.09.13.05.48.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Sep 2021 05:48:01 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 6/8] trace-cmd library: New API to copy trace data between trace files Date: Mon, 13 Sep 2021 15:47:52 +0300 Message-Id: <20210913124754.3679916-7-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210913124754.3679916-1-tz.stoyanov@gmail.com> References: <20210913124754.3679916-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org A new library API is introduced, to copy trace data between trace files: tracecmd_copy_trace_data() Signed-off-by: Tzvetomir Stoyanov (VMware) --- .../include/private/trace-cmd-private.h | 2 + lib/trace-cmd/include/trace-cmd-local.h | 6 + lib/trace-cmd/trace-input.c | 166 ++++++++++++++++++ lib/trace-cmd/trace-output.c | 38 ++++ 4 files changed, 212 insertions(+) diff --git a/lib/trace-cmd/include/private/trace-cmd-private.h b/lib/trace-cmd/include/private/trace-cmd-private.h index e68f75b8..2c373030 100644 --- a/lib/trace-cmd/include/private/trace-cmd-private.h +++ b/lib/trace-cmd/include/private/trace-cmd-private.h @@ -193,6 +193,8 @@ int tracecmd_copy_buffer_descr(struct tracecmd_input *in_handle, struct tracecmd_output *out_handle); int tracecmd_copy_options(struct tracecmd_input *in_handle, struct tracecmd_output *out_handle); +int tracecmd_copy_trace_data(struct tracecmd_input *in_handle, + struct tracecmd_output *out_handle); void tracecmd_set_flag(struct tracecmd_input *handle, int flag); void tracecmd_clear_flag(struct tracecmd_input *handle, int flag); unsigned long tracecmd_get_flags(struct tracecmd_input *handle); diff --git a/lib/trace-cmd/include/trace-cmd-local.h b/lib/trace-cmd/include/trace-cmd-local.h index 706ba250..6c4132f2 100644 --- a/lib/trace-cmd/include/trace-cmd-local.h +++ b/lib/trace-cmd/include/trace-cmd-local.h @@ -64,6 +64,11 @@ int out_update_section_header(struct tracecmd_output *handle, unsigned long long long long do_write_check(struct tracecmd_output *handle, const void *data, long long size); +struct tracecmd_option * +out_add_buffer_option_v7(struct tracecmd_output *handle, const char *name, + unsigned short id, unsigned long long data_offset, + int cpus, struct data_file_write *cpu_data); + struct cpu_data_source { int fd; int size; @@ -72,6 +77,7 @@ struct cpu_data_source { int out_write_cpu_data(struct tracecmd_output *handle, int cpus, struct cpu_data_source *data, const char *buff_name); +int out_write_emty_cpu_data(struct tracecmd_output *handle, int cpus); off64_t msg_lseek(struct tracecmd_msg_handle *msg_handle, off_t offset, int whence); unsigned long long get_last_option_offset(struct tracecmd_input *handle); diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index 2e5e9bcc..7a0102fa 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -5051,6 +5051,172 @@ int tracecmd_copy_options(struct tracecmd_input *in_handle, return 0; } +static int copy_trace_latency(struct tracecmd_input *in_handle, + struct tracecmd_output *out_handle, const char *buf_name) +{ + unsigned long long wsize; + unsigned long long offset; + int fd; + + if (tracecmd_get_out_file_version(out_handle) < FILE_VERSION_SECTIONS && + do_write_check(out_handle, "latency ", 10)) + return -1; + + offset = tracecmd_get_out_file_offset(out_handle); + + if (tracecmd_get_out_file_version(out_handle) >= FILE_VERSION_SECTIONS && + !out_add_buffer_option_v7(out_handle, buf_name, TRACECMD_OPTION_BUFFER_TEXT, offset, 0, NULL)) + return -1; + + offset = out_write_section_header(out_handle, TRACECMD_OPTION_BUFFER_TEXT, + "buffer latency", TRACECMD_SEC_FL_COMPRESS, false); + + if (in_handle->latz.fd >= 0) + fd = in_handle->latz.fd; + else + fd = in_handle->fd; + + if (!out_copy_fd_compress(out_handle, fd, 0, &wsize)) + return -1; + if (out_update_section_header(out_handle, offset)) + return -1; + + out_set_file_state(out_handle, TRACECMD_FILE_CPU_LATENCY); + return 0; +} + +static int copy_trace_flyrecord_data(struct tracecmd_input *in_handle, + struct tracecmd_output *out_handle, const char *buff_name) +{ + struct cpu_data_source *data; + int total_size = 0; + int ret; + int i; + + data = calloc(in_handle->cpus, sizeof(struct cpu_data_source)); + if (!data) + return -1; + for (i = 0; i < in_handle->cpus; i++) { + data[i].size = in_handle->cpu_data[i].file_size; + total_size += data[i].size; + if (in_handle->cpu_data[i].compress.fd >= 0) { + data[i].fd = in_handle->cpu_data[i].compress.fd; + data[i].offset = 0; + } else { + data[i].fd = in_handle->fd; + data[i].offset = in_handle->cpu_data[i].file_offset; + } + } + if (total_size || tracecmd_get_out_file_version(out_handle) < FILE_VERSION_SECTIONS) + ret = out_write_cpu_data(out_handle, in_handle->cpus, data, buff_name); + else + ret = 0; + free(data); + + return ret; +} + +static int copy_flyrecord_buffer(struct tracecmd_input *in_handle, + struct tracecmd_output *out_handle, int index) +{ + struct tracecmd_input *instance; + const char *name; + int ret; + + name = tracecmd_buffer_instance_name(in_handle, index); + if (!name) + return -1; + + instance = tracecmd_buffer_instance_handle(in_handle, index); + if (!instance) + return -1; + if (!tracecmd_get_quiet(out_handle) && *name) + fprintf(stderr, "\nBuffer: %s\n\n", name); + + if (in_handle->buffers[index].latency) + ret = copy_trace_latency(in_handle, out_handle, name); + else + ret = copy_trace_flyrecord_data(instance, out_handle, name); + tracecmd_close(instance); + + return ret; +} + +static int copy_trace_data_from_v6(struct tracecmd_input *in_handle, + struct tracecmd_output *out_handle) +{ + char buf[10]; + int ret; + int i; + + if (do_read_check(in_handle, buf, 10)) + return -1; + + if (strncmp(buf, "latency", 7) == 0) + in_handle->file_state = TRACECMD_FILE_CPU_LATENCY; + else if (strncmp(buf, "flyrecord", 9) == 0) + in_handle->file_state = TRACECMD_FILE_CPU_FLYRECORD; + + tracecmd_init_data(in_handle); + + if (in_handle->file_state == TRACECMD_FILE_CPU_LATENCY) + return copy_trace_latency(in_handle, out_handle, ""); + + /* top instance */ + ret = copy_trace_flyrecord_data(in_handle, out_handle, ""); + if (ret) + return ret; + + for (i = 0; i < in_handle->nr_buffers; i++) + copy_flyrecord_buffer(in_handle, out_handle, i); + return 0; +} + +static int copy_trace_data_from_v7(struct tracecmd_input *in_handle, + struct tracecmd_output *out_handle) +{ + int ret; + int i; + + /* Force using temporary files for trace data decompression */ + in_handle->read_zpage = false; + tracecmd_init_data(in_handle); + + if (tracecmd_get_out_file_version(out_handle) < FILE_VERSION_SECTIONS) { + /* copy top buffer in v6 files */ + if (in_handle->top_buffer.latency) + ret = copy_trace_latency(in_handle, out_handle, in_handle->top_buffer.name); + else if (in_handle->top_buffer.cpus) + ret = copy_trace_flyrecord_data(in_handle, out_handle, + in_handle->top_buffer.name); + else + ret = out_write_emty_cpu_data(out_handle, in_handle->max_cpu); + if (ret) + return ret; + } + + for (i = 0; i < in_handle->nr_buffers; i++) + copy_flyrecord_buffer(in_handle, out_handle, i); + + return 0; +} + +__hidden int tracecmd_copy_trace_data(struct tracecmd_input *in_handle, + struct tracecmd_output *out_handle) +{ + int ret; + + if (!check_in_state(in_handle, TRACECMD_FILE_CPU_FLYRECORD) || + !check_out_state(out_handle, TRACECMD_FILE_CPU_FLYRECORD)) + return -1; + tracecmd_set_out_clock(out_handle, in_handle->trace_clock); + if (in_handle->file_version < FILE_VERSION_SECTIONS) + ret = copy_trace_data_from_v6(in_handle, out_handle); + else + ret = copy_trace_data_from_v7(in_handle, out_handle); + return ret; +} + /** * tracecmd_record_at_buffer_start - return true if record is first on subbuffer * @handle: input handle for the trace.dat file diff --git a/lib/trace-cmd/trace-output.c b/lib/trace-cmd/trace-output.c index 8021ffb6..53bd0209 100644 --- a/lib/trace-cmd/trace-output.c +++ b/lib/trace-cmd/trace-output.c @@ -2165,6 +2165,44 @@ static int update_buffer_cpu_offset_v6(struct tracecmd_output *handle, return 0; } +__hidden int out_write_emty_cpu_data(struct tracecmd_output *handle, int cpus) +{ + unsigned long long zero = 0; + char *clock; + int ret; + int i; + + if (HAS_SECTIONS(handle)) + return 0; + + ret = handle->file_state == TRACECMD_FILE_CPU_FLYRECORD ? 0 : + check_file_state(handle->file_version, + handle->file_state, + TRACECMD_FILE_CPU_FLYRECORD); + if (ret < 0) { + tracecmd_warning("Cannot write trace data into the file, unexpected state 0x%X", + handle->file_state); + return ret; + } + + if (do_write_check(handle, "flyrecord", 10)) + return -1; + + for (i = 0; i < cpus; i++) { + /* Write 0 for trace data offset and size */ + if (do_write_check(handle, &zero, 8)) + return -1; + if (do_write_check(handle, &zero, 8)) + return -1; + } + clock = get_clock(handle); + if (clock && save_clock(handle, clock)) + return -1; + handle->file_state = TRACECMD_FILE_CPU_FLYRECORD; + return 0; +} + + __hidden int out_write_cpu_data(struct tracecmd_output *handle, int cpus, struct cpu_data_source *data, const char *buff_name) { From patchwork Mon Sep 13 12:47:53 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: 12488867 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7AE63C43217 for ; Mon, 13 Sep 2021 12:48:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 652EF6103B for ; Mon, 13 Sep 2021 12:48:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230106AbhIMMtX (ORCPT ); Mon, 13 Sep 2021 08:49:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51146 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236074AbhIMMtU (ORCPT ); Mon, 13 Sep 2021 08:49:20 -0400 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1D8F9C061574 for ; Mon, 13 Sep 2021 05:48:04 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id 192-20020a1c04c9000000b002f7a4ab0a49so5193560wme.0 for ; Mon, 13 Sep 2021 05:48:04 -0700 (PDT) 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=7wQuU+VBQA+pxntlhzZ0IXTS4Xd5fD7hyi8KSCr/r9I=; b=BZjJjtoWkHjWSbJzu8xOss8A3FYeDmWAc5LFuqJjxDNvQ+pABbCX88rFu94ZJZCNfb NrfsJqggn/EkEqkAw4w4ZuuIeLNBJW84nOBPn3/hzW2RCviXCPS5gdgTcjpT7cU8wDzp 2rEIGdLxn3ccmqe+ibny8hnqfVaSBL17qe8TODpvFfSuGddAAOD11HLFWsPnAvS2NT6r GMdeYa3nNUuNN733sYNzxPk1JDPiZD6EbjH4q4LfBbJQbgnbB9ZVyhE4Af25EE2BpTgA c6bHNOgHShlT1EGnaSH4OMWAdX1FJBJLfHD2Ua7Qs8MI8VBNKz3oa3v8wu9IuLJW58Y8 If1Q== 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=7wQuU+VBQA+pxntlhzZ0IXTS4Xd5fD7hyi8KSCr/r9I=; b=x0rjRppYiVMz/Ef7NNdD8+MLX8WMV5LA/ZgnbudLfm41PTVKRxDh/e96fSymFdPTov zaDZgoIYnuaSJmQaa9eZQ+X+QvIywGhoSc5/H+t27lCsDxJ67Z+N+froDJasRoojxF74 EAFgIuKPz45aMLWdRX3W5AjRZMP4NQh9qQb7RoC4mMY8eM8mZ/YMzpyXQqRWck8SYwgh s48pVtBAOn2+RSGwzIg6ZQLj6rh0ecSaVI6C16/E9gaSawiAvA9tdUib0FcN3OVxhDHO w6TmJm7XD/Pcg42rzOLNUF2Bg6pynU/ZsRSc346AMgfzVAAquBbXfU+x/qeJZv7zFkyh 1w4Q== X-Gm-Message-State: AOAM531XlSyeXRKZpGsr3l8u1KuAy5LTWbuJ3vBSvhLeNZ3vrHr+mWBQ sNXsqJCvRRRlkm3w0RANadxYL9xiRGQ= X-Google-Smtp-Source: ABdhPJzvypIrkdlBWKpjlCtCEmCGxrpMBieXEYFww6mlSYqTlNy+HIObnJh7IiZP9na62WpQUNB1TA== X-Received: by 2002:a1c:cc16:: with SMTP id h22mr11131454wmb.117.1631537282679; Mon, 13 Sep 2021 05:48:02 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id k17sm7718077wmj.0.2021.09.13.05.48.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Sep 2021 05:48:02 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 7/8] trace-cmd library: Extend tracecmd_copy() API Date: Mon, 13 Sep 2021 15:47:53 +0300 Message-Id: <20210913124754.3679916-8-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210913124754.3679916-1-tz.stoyanov@gmail.com> References: <20210913124754.3679916-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org The tracecmd_copy() API is used to copy data between trace files. Extended it to support copying data between files from different versions. Added also functionality to copy all sections between the files. Signed-off-by: Tzvetomir Stoyanov (VMware) --- .../include/private/trace-cmd-private.h | 5 ++- lib/trace-cmd/trace-output.c | 40 +++++++++++++++---- tracecmd/trace-restore.c | 2 +- tracecmd/trace-split.c | 2 +- 4 files changed, 37 insertions(+), 12 deletions(-) diff --git a/lib/trace-cmd/include/private/trace-cmd-private.h b/lib/trace-cmd/include/private/trace-cmd-private.h index 2c373030..39ac7f6f 100644 --- a/lib/trace-cmd/include/private/trace-cmd-private.h +++ b/lib/trace-cmd/include/private/trace-cmd-private.h @@ -330,8 +330,9 @@ int tracecmd_write_options(struct tracecmd_output *handle); int tracecmd_append_options(struct tracecmd_output *handle); void tracecmd_output_close(struct tracecmd_output *handle); void tracecmd_output_free(struct tracecmd_output *handle); -struct tracecmd_output *tracecmd_copy(struct tracecmd_input *ihandle, - const char *file); +struct tracecmd_output *tracecmd_copy(struct tracecmd_input *ihandle, const char *file, + enum tracecmd_file_states state, int file_version, + const char *compression); int tracecmd_write_cpu_data(struct tracecmd_output *handle, int cpus, char * const *cpu_data_files, const char *buff_name); diff --git a/lib/trace-cmd/trace-output.c b/lib/trace-cmd/trace-output.c index 53bd0209..9bdb4547 100644 --- a/lib/trace-cmd/trace-output.c +++ b/lib/trace-cmd/trace-output.c @@ -2505,14 +2505,23 @@ struct tracecmd_output *tracecmd_create_init_file(const char *output_file) * tracecmd_copy - copy the headers of one trace.dat file for another * @ihandle: input handle of the trace.dat file to copy * @file: the trace.dat file to create + * @state: what data will be copied from the source handle + * @file_version: version of the output file + * @compression: compression of the output file, can be one of: + * NULL - inherit compression from the input file + * "any" - compress the output file with the best available algorithm + * "none" - do not compress the output file + * algorithm_name - compress the output file with specified algorithm * * Reads the header information and creates a new trace data file * with the same characteristics (events and all) and returns * tracecmd_output handle to this new file. */ -struct tracecmd_output *tracecmd_copy(struct tracecmd_input *ihandle, - const char *file) +struct tracecmd_output *tracecmd_copy(struct tracecmd_input *ihandle, const char *file, + enum tracecmd_file_states state, int file_version, + const char *compression) { + enum tracecmd_file_states fstate; struct tracecmd_output *handle; int fd; @@ -2525,18 +2534,33 @@ struct tracecmd_output *tracecmd_copy(struct tracecmd_input *ihandle, goto out_free; if (tracecmd_output_set_from_input(handle, ihandle)) goto out_free; + if (file_version >= FILE_VERSION_MIN) + tracecmd_output_set_version(handle, file_version); + if (compression && tracecmd_output_set_compression(handle, compression)) + goto out_free; tracecmd_output_write_init(handle); - - if (tracecmd_copy_headers(ihandle, handle, 0, 0) < 0) + fstate = state > TRACECMD_FILE_CPU_COUNT ? TRACECMD_FILE_CPU_COUNT : state; + if (tracecmd_copy_headers(ihandle, handle, 0, fstate) < 0) goto out_free; - - handle->file_state = tracecmd_get_file_state(ihandle); - + if (tracecmd_copy_buffer_descr(ihandle, handle) < 0) + goto out_free; + if (state >= TRACECMD_FILE_OPTIONS && + tracecmd_copy_options(ihandle, handle) < 0) + goto out_free; + if (state >= TRACECMD_FILE_CPU_LATENCY && + tracecmd_copy_trace_data(ihandle, handle) < 0) + goto out_free; + if (HAS_SECTIONS(handle)) + tracecmd_write_options(handle); /* The file is all ready to have cpu data attached */ return handle; out_free: - tracecmd_output_close(handle); + if (handle) + tracecmd_output_close(handle); + else + close(fd); + unlink(file); return NULL; } diff --git a/tracecmd/trace-restore.c b/tracecmd/trace-restore.c index a903c21a..1e416496 100644 --- a/tracecmd/trace-restore.c +++ b/tracecmd/trace-restore.c @@ -153,7 +153,7 @@ void trace_restore (int argc, char **argv) if (tracecmd_read_headers(ihandle, TRACECMD_FILE_CMD_LINES) < 0) die("error reading file %s headers", input); - handle = tracecmd_copy(ihandle, output); + handle = tracecmd_copy(ihandle, output, TRACECMD_FILE_CMD_LINES, 0, NULL); tracecmd_close(ihandle); } else handle = tracecmd_create_init_file(output); diff --git a/tracecmd/trace-split.c b/tracecmd/trace-split.c index 671d6e9f..06a1c82b 100644 --- a/tracecmd/trace-split.c +++ b/tracecmd/trace-split.c @@ -348,7 +348,7 @@ static double parse_file(struct tracecmd_input *handle, dir = dirname(output); base = basename(output); - ohandle = tracecmd_copy(handle, output_file); + ohandle = tracecmd_copy(handle, output_file, TRACECMD_FILE_CMD_LINES, 0, NULL); cpus = tracecmd_cpus(handle); cpu_data = malloc(sizeof(*cpu_data) * cpus); From patchwork Mon Sep 13 12:47:54 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: 12488869 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 36C10C4332F for ; Mon, 13 Sep 2021 12:48:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1CE7561051 for ; Mon, 13 Sep 2021 12:48:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230113AbhIMMtW (ORCPT ); Mon, 13 Sep 2021 08:49:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51150 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236217AbhIMMtU (ORCPT ); Mon, 13 Sep 2021 08:49:20 -0400 Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DFB93C061760 for ; Mon, 13 Sep 2021 05:48:04 -0700 (PDT) Received: by mail-wm1-x332.google.com with SMTP id 192-20020a1c04c9000000b002f7a4ab0a49so5193582wme.0 for ; Mon, 13 Sep 2021 05:48:04 -0700 (PDT) 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=1fqdWuvMaHgw48cIfIMXHNlhX0xndP0gWQV3+EcLyFg=; b=Ja7MFRYsECz9Bj1NioGvkhq1EJyJYeqm/Qdu026+hiiqgU10c4rMI3MSXiZkLS5uP3 3iOrvEg+kTYDKiQq3WFTDn15m63aB7j7xesWUwWufLFYcYjmCk0Qh76ehgIqDjesG+3H iAgWgF3qcY4z+GBk4LxthDX+iVKGytQG/3UAu+15/qWqjrS09SQwyCYezuj2dKNWYDSq 9GolTwD88vCmFuL7nzRRAsy94JoYnCRnB3Aomzu3x4db5dfjWH/Qw9YF9V8DnrlOF0sM GWL5Q+M4xrAN6YXzu3Lx9vxl82tRPvR2UYL1OUOIWYBJduJuFV11+v66RFjuUvN7Ug+K dIPw== 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=1fqdWuvMaHgw48cIfIMXHNlhX0xndP0gWQV3+EcLyFg=; b=haibMEINyPQ6D50Gt4W23Yu8mg5vsGXRmRnSCSz1MGXOo3toqc/XrFGKPTrowAV9Xn D4C44z631l/XTLK64hP3PfiqcKzX6EhswO08TXzRljALtxmq85ZY2xVvhiIsFGy/B9c3 S7Rr9z17uGRjrUl1JKaicVcXFw6f7ZqGplwslVbzd13jDSiHbS/PhOJnh+6H1rroZ1Go NQVqsu39klpwDFPsMuOQSX8nwkdtmx7oxpAMZtXbtGh1yzsHc35r5Yx3oHLNLU+5Hhic P/BYs3ty/EsCl7P8y5FpSAV/h2uSb3flK1raMfi2QxG/Jn8n9z6oojpeJkZ3JBeBVOE0 otuQ== X-Gm-Message-State: AOAM531yBBLTIuHEuB7qblb8PP3jUzRwNLtsTcRKszSzZt8SGlKRy63W V+F7IMXEbrZZY2ZIrqGQI9DNIJmd6ho= X-Google-Smtp-Source: ABdhPJwrVkq51cC3d06AJ7xNNvVPUamwHhrRX+j53yl2PvsEV4d7VMyrPNbQ77nsvNbu3njMmo6AJg== X-Received: by 2002:a1c:ed03:: with SMTP id l3mr11283005wmh.56.1631537283550; Mon, 13 Sep 2021 05:48:03 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id k17sm7718077wmj.0.2021.09.13.05.48.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Sep 2021 05:48:03 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 8/8] trace-cmd: Add new subcommand "convert" Date: Mon, 13 Sep 2021 15:47:54 +0300 Message-Id: <20210913124754.3679916-9-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210913124754.3679916-1-tz.stoyanov@gmail.com> References: <20210913124754.3679916-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org There is a need to convert trace files between different versions. A new "trace-cmd convert" subcommand is introduced, which reads an input trace file and copies to an output trace file. Both files can be from different versions. trace-cmd convert -i -o --file-version --compression Signed-off-by: Tzvetomir Stoyanov (VMware) --- tracecmd/Makefile | 1 + tracecmd/include/trace-local.h | 2 + tracecmd/trace-cmd.c | 1 + tracecmd/trace-convert.c | 106 +++++++++++++++++++++++++++++++++ tracecmd/trace-usage.c | 13 ++++ 5 files changed, 123 insertions(+) create mode 100644 tracecmd/trace-convert.c diff --git a/tracecmd/Makefile b/tracecmd/Makefile index b7a23dc4..56742f0a 100644 --- a/tracecmd/Makefile +++ b/tracecmd/Makefile @@ -36,6 +36,7 @@ TRACE_CMD_OBJS += trace-usage.o TRACE_CMD_OBJS += trace-dump.o TRACE_CMD_OBJS += trace-clear.o TRACE_CMD_OBJS += trace-vm.o +TRACE_CMD_OBJS += trace-convert.o ifeq ($(VSOCK_DEFINED), 1) TRACE_CMD_OBJS += trace-agent.o diff --git a/tracecmd/include/trace-local.h b/tracecmd/include/trace-local.h index e9a0aea8..13dab44c 100644 --- a/tracecmd/include/trace-local.h +++ b/tracecmd/include/trace-local.h @@ -110,6 +110,8 @@ void trace_usage(int argc, char **argv); void trace_dump(int argc, char **argv); +void trace_convert(int argc, char **argv); + int trace_record_agent(struct tracecmd_msg_handle *msg_handle, int cpus, int *fds, int argc, char **argv, bool use_fifos, diff --git a/tracecmd/trace-cmd.c b/tracecmd/trace-cmd.c index 9fc126e4..a83a8d0b 100644 --- a/tracecmd/trace-cmd.c +++ b/tracecmd/trace-cmd.c @@ -133,6 +133,7 @@ struct command commands[] = { {"list", trace_list}, {"help", trace_usage}, {"dump", trace_dump}, + {"convert", trace_convert}, {"-h", trace_usage}, }; diff --git a/tracecmd/trace-convert.c b/tracecmd/trace-convert.c new file mode 100644 index 00000000..c3b2c1d7 --- /dev/null +++ b/tracecmd/trace-convert.c @@ -0,0 +1,106 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2021, VMware, Tzvetomir Stoyanov + */ +#include +#include +#include +#include +#include + +#include "trace-local.h" +#include "trace-cmd.h" +#include "trace-cmd-private.h" + +static void convert_file(const char *in, const char *out, int file_version, char *compr) +{ + struct tracecmd_input *ihandle; + struct tracecmd_output *ohandle; + + ihandle = tracecmd_open_head(in, 0); + if (!ihandle) + die("error reading %s", in); + ohandle = tracecmd_copy(ihandle, out, TRACECMD_FILE_CPU_FLYRECORD, file_version, compr); + if (!ohandle) + die("error writing %s", out); + tracecmd_output_close(ohandle); + tracecmd_close(ihandle); +} + +enum { + OPT_file_version = 254, + OPT_compression = 255, +}; + +void trace_convert(int argc, char **argv) +{ + char *input_file = NULL; + char *output_file = NULL; + char *compression = NULL; + int file_version = FILE_VERSION_DEFAULT; + int c; + + if (argc < 2) + usage(argv); + + if (strcmp(argv[1], "convert") != 0) + usage(argv); + for (;;) { + int option_index = 0; + static struct option long_options[] = { + {"compression", required_argument, NULL, OPT_compression}, + {"file-version", required_argument, NULL, OPT_file_version}, + {"help", no_argument, NULL, '?'}, + {NULL, 0, NULL, 0} + }; + + c = getopt_long (argc-1, argv+1, "+hi:o:", long_options, &option_index); + if (c == -1) + break; + switch (c) { + case 'i': + if (input_file) + die("Only one input file is supported, %s already set", + input_file); + input_file = optarg; + break; + case 'o': + if (output_file) + die("Only one output file is supported, %s already set", + output_file); + output_file = optarg; + break; + case OPT_compression: + if (strcmp(optarg, "any") && strcmp(optarg, "none") && + !tracecmd_compress_is_supported(optarg, NULL)) + die("Compression algorithm %s is not supported", optarg); + compression = optarg; + break; + case OPT_file_version: + file_version = atoi(optarg); + if (file_version < FILE_VERSION_MIN || file_version > FILE_VERSION_MAX) + die("Unsupported file version %d, " + "supported versions are from %d to %d", + file_version, FILE_VERSION_MIN, FILE_VERSION_MAX); + + break; + case 'h': + case '?': + default: + usage(argv); + } + } + + if ((argc - optind) >= 2) { + if (output_file) + usage(argv); + output_file = argv[optind + 1]; + } + + if (!input_file) + input_file = DEFAULT_INPUT_FILE; + if (!output_file) + usage(argv); + + convert_file(input_file, output_file, file_version, compression); +} diff --git a/tracecmd/trace-usage.c b/tracecmd/trace-usage.c index 77898c1c..ec0af46f 100644 --- a/tracecmd/trace-usage.c +++ b/tracecmd/trace-usage.c @@ -412,6 +412,19 @@ static struct usage_help usage_help[] = { " -h, --help show usage information\n" " --verbose 'level' Set the desired log level\n" }, + { + "convert", + "convert trace file to different version", + " %s convert [options]\n" + " -i input file, default is trace.dat\n" + " -o output file, mandatory parameter.\n" + " The output file can be specified also as last argument of the command\n" + " --file-version set the desired trace file version\n" + " --compression compress the trace output file, one of these strings can be passed:\n" + " any - auto select the best available compression algorithm\n" + " none - do not compress the trace file\n" + " name - the name of the desired compression algorithms\n" + " available algorithms can be listed with trace-cmd list -c\n" }, { NULL, NULL, NULL }