From patchwork Wed Jan 26 09:51:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12724845 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8104BC28CF5 for ; Wed, 26 Jan 2022 09:52:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232736AbiAZJwB (ORCPT ); Wed, 26 Jan 2022 04:52:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229663AbiAZJwA (ORCPT ); Wed, 26 Jan 2022 04:52:00 -0500 Received: from mail-ed1-x534.google.com (mail-ed1-x534.google.com [IPv6:2a00:1450:4864:20::534]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7A79CC06161C for ; Wed, 26 Jan 2022 01:52:00 -0800 (PST) Received: by mail-ed1-x534.google.com with SMTP id b13so68368189edn.0 for ; Wed, 26 Jan 2022 01:52:00 -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=x/HHJ9LBTPwrluu/ZS6RG44E5q1mYBedqI52JNJRRzg=; b=SR8x3jie8iRqKLz0qs2LwNlDKhd5dKEgWNqr0KrNMEDprp/r/7i1H9WK2OmA7Om352 1d0kklDaX1u3Nomgmrw7vrnJwXKHI3qaIubbDqjJPBuG5XRYkYP5Gxb6JHg69S07JUSe /hmcCJKvPjoTS+C7XuvY4o5Bs52PZcCHNm1HrJx8i01jiGla/kfE7U1Pf0hXcbWVuQll Z/++gPW+Cvo71PALlwCWbeGLjY1VQyyam11n9pwbVrbMIdM+lQrhSFOCXAJhVLPJTdOX TWGOTZrIkDgChmioOIlFs4BAGZZrmVKf8XNhOQVy2Bqc5RQd+umGXWuhpoPQSsFv3keW ndqw== 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=x/HHJ9LBTPwrluu/ZS6RG44E5q1mYBedqI52JNJRRzg=; b=ilDT3hYBxQ7Uw17PvZVCts4tv9+c7130ycwIQqLMjyPpPMuEob89V77vYc25P4TvRP I08jdJIJcRCp7XI+W70RosyDWFUMHkb69JCZiWzoFvEp4FDZY47xcY5ag65KDNYa52wU tvJ0hzMRZsEyJDQ6kWhS/WPtGLRSaxYOMK0t1kwqMWakMEhXCRo3fXlv1/Kkx+mMEArU 9Fhoo3FkmTN9AQjvZTAfLVjCCoUDbpV8ozl+FE54EGSiCviGCvSjS1xFeXW2f+Vc4wJM AtBzIyCfXoPlCFnOGdX1oU5jPjyL6BSplMDw6pbPNjzGe7SAPeXLPkeAc0ROZSCpI0Bj HaBw== X-Gm-Message-State: AOAM5303lrSjXI/IQX71B+T9kXiid4AlF0IHHuCTAVk1gUlG5lMW/wXp DLEnp+Jo/2whiLQqwajYPn3HZH5cX3I= X-Google-Smtp-Source: ABdhPJxDdUjhrmbxSHe1uHP6hORe6iragx4Yo/w778/ucpyxt0HXHMvlXWBI5i1DZpW8B6rPNvl3zw== X-Received: by 2002:a05:6402:4cf:: with SMTP id n15mr17698828edw.165.1643190719031; Wed, 26 Jan 2022 01:51:59 -0800 (PST) Received: from oberon.zico.biz ([151.251.254.11]) by smtp.gmail.com with ESMTPSA id v15sm9529823edq.35.2022.01.26.01.51.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jan 2022 01:51:58 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v8 1/9] trace-cmd library: Use output handler when copying data from input file Date: Wed, 26 Jan 2022 11:51:46 +0200 Message-Id: <20220126095154.570700-2-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220126095154.570700-1-tz.stoyanov@gmail.com> References: <20220126095154.570700-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 b5dbf56d..5bbe5898 100644 --- a/lib/trace-cmd/include/private/trace-cmd-private.h +++ b/lib/trace-cmd/include/private/trace-cmd-private.h @@ -191,7 +191,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 3d75bd63..5ce640a6 100644 --- a/lib/trace-cmd/include/trace-cmd-local.h +++ b/lib/trace-cmd/include/trace-cmd-local.h @@ -61,6 +61,8 @@ out_write_section_header(struct tracecmd_output *handle, unsigned short header_i char *description, int 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 3f7971c4..5c415e94 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -4489,44 +4489,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); @@ -4538,65 +4541,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; @@ -4604,103 +4608,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) * @@ -4711,7 +4715,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) { @@ -4729,71 +4734,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 780fb481..9b31e264 100644 --- a/lib/trace-cmd/trace-output.c +++ b/lib/trace-cmd/trace-output.c @@ -99,8 +99,8 @@ struct list_event_system { static int write_options(struct tracecmd_output *handle); static int save_string_section(struct tracecmd_output *handle, bool compress); -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_buffer_write(handle->compress, data, size); @@ -2618,7 +2618,7 @@ struct tracecmd_output *tracecmd_copy(struct tracecmd_input *ihandle, goto out_free; 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 Wed Jan 26 09:51:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12724846 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 65078C2BA4C for ; Wed, 26 Jan 2022 09:52:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232742AbiAZJwD (ORCPT ); Wed, 26 Jan 2022 04:52:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49426 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229663AbiAZJwC (ORCPT ); Wed, 26 Jan 2022 04:52:02 -0500 Received: from mail-ed1-x535.google.com (mail-ed1-x535.google.com [IPv6:2a00:1450:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0D5FBC06161C for ; Wed, 26 Jan 2022 01:52:02 -0800 (PST) Received: by mail-ed1-x535.google.com with SMTP id m11so68066085edi.13 for ; Wed, 26 Jan 2022 01:52:01 -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=+RKJnpmBuWeisjhf9Q4FZedicWYXlYDW+3lYs5wRt9M=; b=gjTGrP5vCIgbDXSPKDtbKIHfMCW9r05+TFt5+UhoMhFuY1bUYIw8Sc+pf1olhY9So9 op6O4Rk1reQoF5utJX4ySyNV9I42pojMdDtLOnb1aqhZnj2oXpB9UchG86wMtv/nAPpt woS0eIUETyKn/NQr0GOK2z1BmSJGOJMbvi9G/VxKNvhCUdC+9VfNbgihaag1ezWnOW7C TYk1U24FfSg+b18E4dtshJyTWWtwIJeOp/e+iBlNNeYph/eL3c+6+YQOG3MTSluPblWP ZiA19gOouwMSI5YBNq78GOoIojnCcj5F+lnR3XFgXEbwlowwihDaATicUPVseA6H23f6 Jv9w== 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=+RKJnpmBuWeisjhf9Q4FZedicWYXlYDW+3lYs5wRt9M=; b=ETcuDwaG4OtEHf6O/SzZyAGS0nTy1RUSlyB/iQJGll2zcGHAJC4l2yl+9lAVnfYQ52 Zi0S/UoPrHGMSk1m83slbTce/8qUBfU8BOEq0Wr8y5iasOt3fgd3QDFnVPw9zwPQfL8H 2YIo4b+sTkiWmVUZnDzh91nLwzM2hx6991rDqwUSS1UqZ6AYjsTKz4HunTb2oD89HhPA X6yE/vGcYgmLsY9d1fT9FKgk7axwh5W8sLXRoHcF3pZeSUmuCdpuYdZHillxS3OxQ2WT mm8dYgcDvwwX30dTUoTg31cZ5WTOarqvwwjIHQl+XXfWR46i+J7QJzmjHj8qXdHnkxBw F9AA== X-Gm-Message-State: AOAM531dyMGa7Ef74jZYlMyMpFJQg43B15p7OYywbAGvG/aM6IJUHHqa sbVcNRIaJzm5FGMi1jur9f65nNZcdHs= X-Google-Smtp-Source: ABdhPJzvHwr7ZWNTda4L3kfgqIIp7KLWsDuMsUNP3IQzinsedLcADvZUSN0dVdVtT+OfvDC2Qwdohg== X-Received: by 2002:a05:6402:3484:: with SMTP id v4mr23860919edc.34.1643190720583; Wed, 26 Jan 2022 01:52:00 -0800 (PST) Received: from oberon.zico.biz ([151.251.254.11]) by smtp.gmail.com with ESMTPSA id v15sm9529823edq.35.2022.01.26.01.51.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jan 2022 01:52:00 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v8 2/9] trace-cmd library: Handle version 7 files when copying headers between files Date: Wed, 26 Jan 2022 11:51:47 +0200 Message-Id: <20220126095154.570700-3-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220126095154.570700-1-tz.stoyanov@gmail.com> References: <20220126095154.570700-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 | 222 ++++++++++++++++++++---- lib/trace-cmd/trace-output.c | 10 ++ lib/trace-cmd/trace-util.c | 9 +- 4 files changed, 212 insertions(+), 32 deletions(-) diff --git a/lib/trace-cmd/include/trace-cmd-local.h b/lib/trace-cmd/include/trace-cmd-local.h index 5ce640a6..4154e76e 100644 --- a/lib/trace-cmd/include/trace-cmd-local.h +++ b/lib/trace-cmd/include/trace-cmd-local.h @@ -50,6 +50,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 5c415e94..09888b35 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -4541,164 +4541,324 @@ 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 9b31e264..164c66a8 100644 --- a/lib/trace-cmd/trace-output.c +++ b/lib/trace-cmd/trace-output.c @@ -2631,11 +2631,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 06071f6c..f5c77e91 100644 --- a/lib/trace-cmd/trace-util.c +++ b/lib/trace-cmd/trace-util.c @@ -647,6 +647,13 @@ 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: @@ -659,7 +666,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 Wed Jan 26 09:51:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12724847 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 59A93C63682 for ; Wed, 26 Jan 2022 09:52:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232828AbiAZJwE (ORCPT ); Wed, 26 Jan 2022 04:52:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49434 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229663AbiAZJwE (ORCPT ); Wed, 26 Jan 2022 04:52:04 -0500 Received: from mail-ed1-x534.google.com (mail-ed1-x534.google.com [IPv6:2a00:1450:4864:20::534]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5850CC06161C for ; Wed, 26 Jan 2022 01:52:03 -0800 (PST) Received: by mail-ed1-x534.google.com with SMTP id j23so63028341edp.5 for ; Wed, 26 Jan 2022 01:52:03 -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=gpBCiaTu4bIKCouWw2H9YKfJ0QpTsb3puj4jgwBl9HE=; b=mv9TkacVg6N5QEQLxbp3ENeqMcHxqvRsCKic1/2oXDKw04ieD9yn5gqR0gnuXG+Jfu HbFjwBSAQCDafi0nU/kBBWIwmz0VTsaFqhod20zIe+NUuACth9iX8P4jdpkTmEgf5d1g MNoDexZ4Fk4SacqkU6DdZwonqxTttN7iof/W6Vb4N34MuHWHI1noON66AC36l2sBeC33 OrrfX/heN/Z6TLSxc3zDCnXRrhbgmlC+y0MyuOoY1y4byXICZTRQH+oGjNO9D8ebZeM1 Sspm3MqT6buGnD3TYqxf+RkDtqZI0WMLrJ4V3qBotfZ+qr8uI0LWirgfHGLcQba+wt+O m/hA== 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=gpBCiaTu4bIKCouWw2H9YKfJ0QpTsb3puj4jgwBl9HE=; b=Ko/Rk+M1MzVFn5aYqopshwppBThxIbJAHDTC2clVgclg5h+9ResqTAwRBboRzYM60V NGTvtlYoLuvmBmMVtWM4JCcmLTCs/vU7KgwSVmAN5ZaXZokcD6Fmfu6oYmVlpZEwtQsg oGJURr+V+0qJ3CHhpI6rZHXcf/i9v6h2insca3+FO6HHUOXCReW5LJbs6pr7rYoqKhB2 eeQ6ZlXDsaOVa8FnegD87W+ffkZTX2JqNcUzLy95/sVS/vPvzKjc/fPhxQcODf3i3A6J OAo/286Ge3eQwgEuv/1BJZ/tZbcCulo3OHt//ugxGAWxIYQyKTrRFgXAuCTK52DrZ46k tzZA== X-Gm-Message-State: AOAM530LO2NaXJk4lkGDVYuHzqTwtqXl03bR8bzUdjgB3Cu28jVDbS7k GvLz/P4d8Yqtu9ogucuimK7ZKJSnQwo= X-Google-Smtp-Source: ABdhPJxHsE2NXfHo7ZgeU2jOpNxcO8GrwZ4lPYNqUTUZu8VV3oqfljjvIxN5RrLSRVvyMfRfNbTQSg== X-Received: by 2002:a05:6402:1d4a:: with SMTP id dz10mr24151813edb.206.1643190721930; Wed, 26 Jan 2022 01:52:01 -0800 (PST) Received: from oberon.zico.biz ([151.251.254.11]) by smtp.gmail.com with ESMTPSA id v15sm9529823edq.35.2022.01.26.01.52.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jan 2022 01:52:01 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v8 3/9] trace-cmd library: Copy CPU count between trace files Date: Wed, 26 Jan 2022 11:51:48 +0200 Message-Id: <20220126095154.570700-4-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220126095154.570700-1-tz.stoyanov@gmail.com> References: <20220126095154.570700-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 | 42 +++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index 09888b35..9afc6559 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -4861,6 +4861,35 @@ 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; + } else { + tracecmd_add_option(out_handle, TRACECMD_OPTION_CPUCOUNT, sizeof(int), &cpus); + } + + 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. @@ -4959,6 +4988,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 Wed Jan 26 09:51:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12724848 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 809FFC2BA4C for ; Wed, 26 Jan 2022 09:52:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229663AbiAZJwF (ORCPT ); Wed, 26 Jan 2022 04:52:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49438 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232830AbiAZJwF (ORCPT ); Wed, 26 Jan 2022 04:52:05 -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 9EC67C06161C for ; Wed, 26 Jan 2022 01:52:04 -0800 (PST) Received: by mail-ed1-x536.google.com with SMTP id j23so63028410edp.5 for ; Wed, 26 Jan 2022 01:52:04 -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=1r3q6UI3ETEfXlpiKGuOGPYwXnfQP0DjegJSqd7Veys=; b=XKPVgdAhEettpMMhC52qvVZYROUS6g09ZZvVuW3/C6dy09gsZMRd88BQ6Lz/k18mmH OnbBp8UHyyOtlM2z9F9BwOARqSXTrjpIUd3rozeihmm6o0GlyVe5rAXWrWgjmGAzUJvC zo9X/ny+d92/OO9xhx2qA8Kuvsng0Bf3rhqfa/wSjhGk5UZmARQSRlUSiGvQaTqo4WOI EqzX+aQx7speD60pY6vqpDYPxrX20YZsb5N7T18VBikqTl4wuQYlGLwpQt2czWM8ozV7 DvhczyHq7SiFUnG8sW2OqywlZooShuwUb8mvFz3XCdchRBOtaYJZFKHlmAx+XCuHasCf x7yQ== 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=1r3q6UI3ETEfXlpiKGuOGPYwXnfQP0DjegJSqd7Veys=; b=Dw3+dnNbZ8E5uL4DKXcl48BJv9EF4Ae5W3hpg2PEW5rq6kpytLVyQTnorRJDH6cv7j RJoE1+MYAnfzG362sDlAWK9dyPoo/NCSyxqnNbTB+poHulY1uFfNTW9GoNS2F9JPuhBM ynWBkMSiMnB0RuUMQRiFtag3WoW5odyksv5nSf7njX+5ouf3oHrvm4G0stW51BEKKVgV XQnZq0BOZCLh7juc6jN+pvBFqLiuvj7iBl+AOKD6/LD8WCHlVhwnMhZ+1K8TdR/JFvtg /yuKga7b0I8dQTKhX7KAdZD9yQU6eJ2est4/SABPDTfRYIGtwsVupKYW6l6PhBhB53HI zj0Q== X-Gm-Message-State: AOAM533TB1Xtiv6h88rLIqzACEpNMnaJECRr7jsVtI8b96tApboHs5Gb 6AkzQHYQG42vceri8C3fcX83SUg4bMs= X-Google-Smtp-Source: ABdhPJzGS7+0L1vfcAzW++W1MWSOpdwKA51ogBwhFY528YGtabV1MMlMhJpileXyCtc2CXH629tjoQ== X-Received: by 2002:a05:6402:26cc:: with SMTP id x12mr14573767edd.153.1643190723240; Wed, 26 Jan 2022 01:52:03 -0800 (PST) Received: from oberon.zico.biz ([151.251.254.11]) by smtp.gmail.com with ESMTPSA id v15sm9529823edq.35.2022.01.26.01.52.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jan 2022 01:52:02 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v8 4/9] trace-cmd library: New API to copy buffer description between trace files Date: Wed, 26 Jan 2022 11:51:49 +0200 Message-Id: <20220126095154.570700-5-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220126095154.570700-1-tz.stoyanov@gmail.com> References: <20220126095154.570700-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 | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/trace-cmd/include/private/trace-cmd-private.h b/lib/trace-cmd/include/private/trace-cmd-private.h index 5bbe5898..07430d37 100644 --- a/lib/trace-cmd/include/private/trace-cmd-private.h +++ b/lib/trace-cmd/include/private/trace-cmd-private.h @@ -195,6 +195,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 9afc6559..5f2b9d85 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -5009,6 +5009,20 @@ 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 Wed Jan 26 09:51:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12724849 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 700A9C28CF5 for ; Wed, 26 Jan 2022 09:52:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232849AbiAZJwH (ORCPT ); Wed, 26 Jan 2022 04:52:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49446 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232830AbiAZJwG (ORCPT ); Wed, 26 Jan 2022 04:52:06 -0500 Received: from mail-ed1-x52a.google.com (mail-ed1-x52a.google.com [IPv6:2a00:1450:4864:20::52a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F011CC06161C for ; Wed, 26 Jan 2022 01:52:05 -0800 (PST) Received: by mail-ed1-x52a.google.com with SMTP id p7so2708516edc.12 for ; Wed, 26 Jan 2022 01:52:05 -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=/WVeK5dUptswPcuIyqXNbzB0yKJX8gcjkxbih/4K/ls=; b=H9yq1TZkb2SdhvXx06fiEUlwDGPNkCN8FRwMpHXzU5UR/X+HStSdN8usoz/VnRItod GymI+efEd6VBOjzqtr/20CAKaTWFAiT93PJ4zCNrjJUPfTGIWw9e1slZxKludS9YahwZ UxNc+Iw6rU2maXdKgCJzcNLudu3CzudNnoy4Eazwf4H+JcYjBuN3UU5pM3an4YAAWtYp kLHr7PlzbsMM/3UfOGNAUJullaM/DMUYDHxdAzv5/sKclQujAKAEMyClWSV8J+cOc9LP 0k7z1KKA6DaIJwgTBPbpLcbnTFA6oTIauS6OKJx7MpnFMqqqD5FYSNZ3aol0fYO9/HUH oKuQ== 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=/WVeK5dUptswPcuIyqXNbzB0yKJX8gcjkxbih/4K/ls=; b=afv0Y12MQUIrONIssS/9HOBTjLkmDaCyZEevOfyFWLWuBFybGBihXx318y1z+z4/IG UpFIxxSDXmFAqVsYPNFIKZIZH4PitpfoWqTB8gzmEI7z1/6H12oHNG9crIb2FVqTMmKQ 4cch78/+1AvaUKpQ/CPw4AdX6nhKJ9k/7zr/L7wSwhx65aSuZ8PKidBliKup5Chz/Wdf fz1MADYHIGJA4y6Knf9/liNBXntJ2z9WQgE4PztG4gq4C38DsB5MnFhbh7ALhvW5TAxy NIaZdKqmnc29OhXGjiQKYrJl6aJO5GZeQUjWnINy1AIqaaQPvd72bdzAE2450SYYkiSA Ac8Q== X-Gm-Message-State: AOAM532PeU4swEzgr78IOy1mKMJqFbYocmYqfsqzym0NW9BnVV14kG8c 4VTA4cjc8xqEmUdp1e8xorrEr8yWLxo= X-Google-Smtp-Source: ABdhPJxxQ1eF2eK/K6Y2lmleGGRqorkaPtplGmnF1bpHLrF0NL93qlQNqUTPbyJEtZGIvaHtBuujvA== X-Received: by 2002:a50:ec09:: with SMTP id g9mr23949082edr.198.1643190724562; Wed, 26 Jan 2022 01:52:04 -0800 (PST) Received: from oberon.zico.biz ([151.251.254.11]) by smtp.gmail.com with ESMTPSA id v15sm9529823edq.35.2022.01.26.01.52.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jan 2022 01:52:04 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v8 5/9] trace-cmd library: New API to copy options between trace files Date: Wed, 26 Jan 2022 11:51:50 +0200 Message-Id: <20220126095154.570700-6-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220126095154.570700-1-tz.stoyanov@gmail.com> References: <20220126095154.570700-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 | 158 ++++++++++++++++++ lib/trace-cmd/trace-output.c | 32 ++++ 4 files changed, 195 insertions(+) diff --git a/lib/trace-cmd/include/private/trace-cmd-private.h b/lib/trace-cmd/include/private/trace-cmd-private.h index 07430d37..98b1864e 100644 --- a/lib/trace-cmd/include/private/trace-cmd-private.h +++ b/lib/trace-cmd/include/private/trace-cmd-private.h @@ -197,6 +197,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); @@ -342,6 +344,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 4154e76e..85420fb1 100644 --- a/lib/trace-cmd/include/trace-cmd-local.h +++ b/lib/trace-cmd/include/trace-cmd-local.h @@ -53,6 +53,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 5f2b9d85..2af3ecc0 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -5023,6 +5023,164 @@ 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 164c66a8..a8c4d7d9 100644 --- a/lib/trace-cmd/trace-output.c +++ b/lib/trace-cmd/trace-output.c @@ -2646,6 +2646,33 @@ __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 @@ -2654,3 +2681,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 Wed Jan 26 09:51:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12724851 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CF5C2C2BA4C for ; Wed, 26 Jan 2022 09:52:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232850AbiAZJwJ (ORCPT ); Wed, 26 Jan 2022 04:52:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49454 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232830AbiAZJwH (ORCPT ); Wed, 26 Jan 2022 04:52:07 -0500 Received: from mail-ej1-x62c.google.com (mail-ej1-x62c.google.com [IPv6:2a00:1450:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3BAAEC06161C for ; Wed, 26 Jan 2022 01:52:07 -0800 (PST) Received: by mail-ej1-x62c.google.com with SMTP id a8so37603886ejc.8 for ; Wed, 26 Jan 2022 01:52:07 -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=VosBxo+OxxFR63GMrkmMMYecn0KMYB0Tn+qzuQ54A8o=; b=qVauOTur4kBgFZoDVLQxM84puNyD20g3CwEI5izkeGFSJHDHMWEYUdBhafKKzEDiIp SmufBjuA1wU3tL0B3oVbdPKyEsW/lBY7lKeizLQ8xxb02h1+vMf+jFQFha3+vGZwya2I nQU0EDmLWe/FV1ONrrsJZpEXS6CRBYx8MNxwNsT/WfCkkwUZpRg2DbdMz8JJHQ3G34nT zJHACgLtDKLgU9fzKLrB9u31pSnQxvJFFKR0YvzYu5eUVN0xQy7GyYKEehBpks+Sj3aL dhqjPc1AtzL+jdZnoXdsPT5JyOcQ8iHeDyX6tP7hVUxEyV7Qqi2kRbSIFz2Qz03PdHGV 2euQ== 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=VosBxo+OxxFR63GMrkmMMYecn0KMYB0Tn+qzuQ54A8o=; b=lsYUnGYilRfRpy11+V1hGaOUzqbL0NF+bbAO+ELfYyviQYFtgfflvGaSTD7gpyFzho 2F5DAGKw54ZRA/W4SRDwgdIQAkZY6QFDhKNHQRf4eo3HxqZXX/BQ4ceiYFyWYYhN7Bme btMMBzbVKkV1DVQrSf0a5wVGY/6i86pawVM3sHcg6oMz3lYqb9Oqg15xei7xFF40220Q EhjXtgB1K2QJsEngupYmnTA5u6XkFGo4DIxYC2NA8R0ET9KUcKn/KalYcV/0tRvDLnx5 wIHfoaxvjq5ms3UdlRvHMTyVXRE3otwwCRrqR+bkfU6s0q1OvNmzBKpDBuggkDfrm2T2 GLjA== X-Gm-Message-State: AOAM533NhtwmaEnDtE3CiKifzVRdttZrYvT5AsjenyCRBwPjr7Cj257c HSBUXqm41mk4PGvf+z0xHW0sFXF7rTM= X-Google-Smtp-Source: ABdhPJwOmC7IIq4dmTqdmOmYRE10HashdSuS+tNxrXgNEOpKG20JcZ9TlEma3BclVMclzX1ISQlqJQ== X-Received: by 2002:a17:907:948b:: with SMTP id dm11mr18792327ejc.687.1643190725753; Wed, 26 Jan 2022 01:52:05 -0800 (PST) Received: from oberon.zico.biz ([151.251.254.11]) by smtp.gmail.com with ESMTPSA id v15sm9529823edq.35.2022.01.26.01.52.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jan 2022 01:52:05 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v8 6/9] trace-cmd library: New API to copy trace data between trace files Date: Wed, 26 Jan 2022 11:51:51 +0200 Message-Id: <20220126095154.570700-7-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220126095154.570700-1-tz.stoyanov@gmail.com> References: <20220126095154.570700-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 | 178 ++++++++++++++++++ lib/trace-cmd/trace-output.c | 40 ++++ 4 files changed, 226 insertions(+) diff --git a/lib/trace-cmd/include/private/trace-cmd-private.h b/lib/trace-cmd/include/private/trace-cmd-private.h index 98b1864e..32957305 100644 --- a/lib/trace-cmd/include/private/trace-cmd-private.h +++ b/lib/trace-cmd/include/private/trace-cmd-private.h @@ -199,6 +199,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 85420fb1..48f179d6 100644 --- a/lib/trace-cmd/include/trace-cmd-local.h +++ b/lib/trace-cmd/include/trace-cmd-local.h @@ -68,6 +68,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(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; @@ -76,6 +81,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); unsigned int get_meta_strings_size(struct tracecmd_input *handle); diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index 2af3ecc0..60ac839c 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -5181,6 +5181,184 @@ 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(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 cpus; + int ret; + int i, j; + + if (tracecmd_get_out_file_version(out_handle) < FILE_VERSION_SECTIONS) + cpus = in_handle->max_cpu; + else + cpus = in_handle->cpus; + + data = calloc(cpus, sizeof(struct cpu_data_source)); + if (!data) + return -1; + + for (i = 0; i < in_handle->cpus; i++) { + j = in_handle->cpu_data[i].cpu; + data[j].size = in_handle->cpu_data[i].file_size; + total_size += data[j].size; + if (in_handle->cpu_data[i].compress.fd >= 0) { + data[j].fd = in_handle->cpu_data[i].compress.fd; + data[j].offset = 0; + } else { + data[j].fd = in_handle->fd; + data[j].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, 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); + tracecmd_set_out_clock(out_handle, in_handle->trace_clock); + + 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 = 0; + int i; + + /* Force using temporary files for trace data decompression */ + in_handle->read_zpage = false; + tracecmd_init_data(in_handle); + tracecmd_set_out_clock(out_handle, in_handle->trace_clock); + + /* copy top buffer */ + 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 if (tracecmd_get_out_file_version(out_handle) < FILE_VERSION_SECTIONS) + 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; + + 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 a8c4d7d9..b54c68fe 100644 --- a/lib/trace-cmd/trace-output.c +++ b/lib/trace-cmd/trace-output.c @@ -2298,6 +2298,46 @@ 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 Wed Jan 26 09:51:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12724850 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9E6E3C63682 for ; Wed, 26 Jan 2022 09:52:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232853AbiAZJwJ (ORCPT ); Wed, 26 Jan 2022 04:52:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49456 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232850AbiAZJwI (ORCPT ); Wed, 26 Jan 2022 04:52:08 -0500 Received: from mail-ej1-x634.google.com (mail-ej1-x634.google.com [IPv6:2a00:1450:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2CF9CC061744 for ; Wed, 26 Jan 2022 01:52:08 -0800 (PST) Received: by mail-ej1-x634.google.com with SMTP id ka4so37706609ejc.11 for ; Wed, 26 Jan 2022 01:52:08 -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=FtkvWDntvaQsbS0VyHeLi1rv+YVJUzbhCKEwMcOq4Ao=; b=avn7EnilLdBB9B4bTV44i2SeTu0+Td6cn+AcsasGjQorgawxHFcsLUWsY3wViItokg Bp4U7DXdAnr//DpZVNyRleEpDkpCl3wTN95GrLZsVNX9EIZSAGsNpy7NMIkiB2ZH3fl8 7/VojKjOGnRYKh3HGnheVzzVHQy4buf3eETHT+PYatOI9hpSutu0kJmIuLRgYObkQdM1 CZfRXxI4zqV1JsIefCJWkJQvXpCP/Yp7TgJsgnGfPgMoobu3+0sXhKGMqeigXRLzPwkZ 6nl+D+xlckm79jCDp/GqsI1s78MERRpBSkA1XrAC8U0amczzhZTElGtwKYl42XtcKPrn 2hHA== 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=FtkvWDntvaQsbS0VyHeLi1rv+YVJUzbhCKEwMcOq4Ao=; b=0pDwLlnnVtWvErWgNBIxDa/0oFAJTunjRwrbavVYhtPi5SyPZzqgYeTV+d/uFKVRcb Aj1DNQNYp56kZCs5yLaoDFuKbj1s6OVfaar5+SQ9h4E9oN/NZgJY8S6a8iaApEj9GLvj YtZJSTqAbZHDk5huwcrozNnhLgrETG/MdroXMAz/0cvtp3S9hvvBlX6FLtO1XNzlfx6J KW4ax0ZhJbfZhWkD8mfd2kiVCdmAghB0L0vFMv3Vm8og3muognl+uRLdKCKn8lIfVLbi C80MSu4r7O91ZYDP28kgT3Us6BLs0GmU0vvW6ePmoC/2dqzAvEiOIeYxbNwB4vOOHeUb aa4A== X-Gm-Message-State: AOAM5338g1lIzCBKczEi1+9UZlYReH4frNgwoIj9ePFbdsgt2DVWMyAK ukd3XmqAmb2Hcj+S5HRZwnI= X-Google-Smtp-Source: ABdhPJwgm/7ltU0w00OyZKPkxS9BiHBnaMUjT3bsQzqxLh0bjjJmV5ZF4Bw3VAHJnKaHwj5cPLyNsg== X-Received: by 2002:a17:907:3ea4:: with SMTP id hs36mr1252202ejc.507.1643190726802; Wed, 26 Jan 2022 01:52:06 -0800 (PST) Received: from oberon.zico.biz ([151.251.254.11]) by smtp.gmail.com with ESMTPSA id v15sm9529823edq.35.2022.01.26.01.52.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jan 2022 01:52:06 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v8 7/9] trace-cmd library: Avoid memory leak when setting trace clock Date: Wed, 26 Jan 2022 11:51:52 +0200 Message-Id: <20220126095154.570700-8-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220126095154.570700-1-tz.stoyanov@gmail.com> References: <20220126095154.570700-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org The API for setting a trace clock to a input handle did not check if the clock is already set: tracecmd_set_out_clock() This could cause a memory leak, if the clock is already set - the old clock string is overwritten. The old clock should be freed before setting the new one. Signed-off-by: Tzvetomir Stoyanov (VMware) --- lib/trace-cmd/trace-output.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/trace-cmd/trace-output.c b/lib/trace-cmd/trace-output.c index b54c68fe..c1ba8a52 100644 --- a/lib/trace-cmd/trace-output.c +++ b/lib/trace-cmd/trace-output.c @@ -227,8 +227,10 @@ void tracecmd_set_quiet(struct tracecmd_output *handle, bool set_quiet) void tracecmd_set_out_clock(struct tracecmd_output *handle, const char *clock) { - if (handle && clock) + if (handle && clock) { + free(handle->trace_clock); handle->trace_clock = strdup(clock); + } } /** From patchwork Wed Jan 26 09:51:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12724852 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D716DC28CF5 for ; Wed, 26 Jan 2022 09:52:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232867AbiAZJwK (ORCPT ); Wed, 26 Jan 2022 04:52:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49466 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232830AbiAZJwK (ORCPT ); Wed, 26 Jan 2022 04:52:10 -0500 Received: from mail-ej1-x634.google.com (mail-ej1-x634.google.com [IPv6:2a00:1450:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B0763C06161C for ; Wed, 26 Jan 2022 01:52:09 -0800 (PST) Received: by mail-ej1-x634.google.com with SMTP id h7so37742966ejf.1 for ; Wed, 26 Jan 2022 01:52: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=tCAmBfN+hSY4foKzWF2G25qnQqbe3j5CHe/tPygwmno=; b=cUuxSaXFuiL3W+3KTLERBcIdyBFL+Ky5SydlKKG86bColuIo16OdD/KxZHZGEU5ZFc tAsOuoJcjAuZSo47nIPCtBGBRPwEkBH8TFsxO0X9bQBbFbNVGCOD+JTPZzeaGlpNgTxS QnS4k6T36dGsXuLPnyr1uqviHk/eXxK3YxBFD7KMPM87WRxW8qOzAHTXNeJvSKhyxhWX UkS3hGPDYEmWysUz4Xv9Ba7224U6K2vZ5W1BbbuU/F/BU/l4ocJM52nDCqoLRYAUPqjf AeXpgDu2WhBecWQXVFDUZj+s56OrlJLwnVprtub95SneJ4yAqqFjUSZl6vS+sFR8phQd K/kQ== 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=tCAmBfN+hSY4foKzWF2G25qnQqbe3j5CHe/tPygwmno=; b=rnMJ1/WMzo1rTUJN+rGssh1jG6YtJ4OpaAJOr1VdbjBBI8dxyYJDJmCH7vc6jjsh5h iGoxxN1gzce1Hnug3uZXv9Q6bcWXmMn2TXVCNyyKms4f5HrnpVR2eMM2hCe66uUJi+op fM/dmBVuDD795D7E74JDMRVGrIGsjAV7k4R5QSiN167rwpsNvVMaowgqV/0cgnzuqnxY Fd+b21bjuEyv9+i4bv4XXA55WwDU8v9XXPCSHOR3XJxBFpUI33N87rc1m1m9NBKOtDoS 0ji01zz8fCrv9JY49ofL6+OAQQWpXABlCqSCzTdSy2UQBFjenZqLQN3m/r3SP9yQXmIZ FvIA== X-Gm-Message-State: AOAM533pPNLs0UrhSVNom0kRDmRTo8ekcqFc2DOTgmLwX6QrYKJuQq+/ 9xCzWFpgo9WGMdWr5CpRq3/59dOHjEs= X-Google-Smtp-Source: ABdhPJxfLRrHBvxK23DYGqlAGs4hJ5sMMXwP8XM0vyuAnWnvBTiv8Rsqlheusf+nBgRqArM69O9cXw== X-Received: by 2002:a17:906:38ce:: with SMTP id r14mr19241525ejd.468.1643190728222; Wed, 26 Jan 2022 01:52:08 -0800 (PST) Received: from oberon.zico.biz ([151.251.254.11]) by smtp.gmail.com with ESMTPSA id v15sm9529823edq.35.2022.01.26.01.52.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jan 2022 01:52:07 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v8 8/9] trace-cmd library: Extend tracecmd_copy() API Date: Wed, 26 Jan 2022 11:51:53 +0200 Message-Id: <20220126095154.570700-9-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220126095154.570700-1-tz.stoyanov@gmail.com> References: <20220126095154.570700-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 | 41 ++++++++++++++++--- tracecmd/trace-restore.c | 2 +- tracecmd/trace-split.c | 2 +- 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/lib/trace-cmd/include/private/trace-cmd-private.h b/lib/trace-cmd/include/private/trace-cmd-private.h index 32957305..d229b264 100644 --- a/lib/trace-cmd/include/private/trace-cmd-private.h +++ b/lib/trace-cmd/include/private/trace-cmd-private.h @@ -335,8 +335,9 @@ int tracecmd_write_meta_strings(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 c1ba8a52..e7915aa6 100644 --- a/lib/trace-cmd/trace-output.c +++ b/lib/trace-cmd/trace-output.c @@ -2642,14 +2642,23 @@ struct tracecmd_output *tracecmd_output_create(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; handle = tracecmd_output_create(file); @@ -2658,18 +2667,40 @@ struct tracecmd_output *tracecmd_copy(struct tracecmd_input *ihandle, 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; + output_write_init(handle); + fstate = state > TRACECMD_FILE_CPU_COUNT ? TRACECMD_FILE_CPU_COUNT : state; + if (tracecmd_copy_headers(ihandle, handle, 0, fstate) < 0) + goto out_free; - if (tracecmd_copy_headers(ihandle, handle, 0, 0) < 0) + if (tracecmd_copy_buffer_descr(ihandle, handle) < 0) goto out_free; - handle->file_state = tracecmd_get_file_state(ihandle); + 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); + + unlink(file); return NULL; } diff --git a/tracecmd/trace-restore.c b/tracecmd/trace-restore.c index 23a7f4af..5bf29c52 100644 --- a/tracecmd/trace-restore.c +++ b/tracecmd/trace-restore.c @@ -147,7 +147,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_output_create(output); diff --git a/tracecmd/trace-split.c b/tracecmd/trace-split.c index e4a0c3b3..83c5402c 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 Wed Jan 26 09:51:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12724853 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 557ABC2BA4C for ; Wed, 26 Jan 2022 09:52:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232868AbiAZJwL (ORCPT ); Wed, 26 Jan 2022 04:52:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49468 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232830AbiAZJwL (ORCPT ); Wed, 26 Jan 2022 04:52:11 -0500 Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [IPv6:2a00:1450:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 15B65C06161C for ; Wed, 26 Jan 2022 01:52:11 -0800 (PST) Received: by mail-ed1-x532.google.com with SMTP id u18so53813503edt.6 for ; Wed, 26 Jan 2022 01:52: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=wCFizrQF12aKlQ5tI08zVD4cWaKfpMz8UdmvlPAl+mc=; b=iC/EY1TzJhly9G8KT2ueJLQgROcgNvFKC33wqMIpE3H6gaKwGXqFwcp782PejX4m2g aZdoYXZ6I4W4JYlAMeH8bhC5ZhB2Gk6Z9+AIr/KSL36A29h9o8FLX4s7YGJUK5MLQdiP iuFhySy9c8BlaUQr3lDzBUBXWzFVruuDYJmRkxYqB/SD0/xBc0O560qLGywtONPtbgNw 6X8k018WVTjWfvHP+C5zroIHF4zLKsLObvj5PWDY7hBXeUKWbfFyPKsD3BxpiblIOoM8 +xFw/LD1jGzJZELj1XH1AnkdxVrdTSjj+2h7BBESe8h2bnt1/uM7NAgFIBeGkuFd1Mtj zLoA== 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=wCFizrQF12aKlQ5tI08zVD4cWaKfpMz8UdmvlPAl+mc=; b=kf27XKDDV1zI3YRSIHveHANVFDSwKeiY9XetDPLBqAoMITEEwwBFQayUzCfSYeFwXL JmKr1VWiivoMvMwb0MnurDE+27KlV/4qnTLZsDfrTlMGtVG8+zucEUFvi3m/YT/QWP6N elmDQreTOzA+bvI/EML4kBQ24XzqE29NctLT+uEYJh8lu8u4xwvKxCjY1JiwRDQHIOv6 TlEp2ikWq545Pu3beDnAd4B91AAFCucm46ZM9CgwMNOj6aHWoLhwfpY4ZWb/mfdFeswg EucnoJQIw5+pEpdCwkiZtktx+VXUXXJnUS8T7ZMEcqq2ne1nhMqPJbXhTYEGTyGsNms+ VdUA== X-Gm-Message-State: AOAM532IRRJjtgjQIwqzt3qKW+SYwyIRwPh0hNmZkmikHU0g3R7D6jKs 6ZLhyc2wNh1oeptnZLFuuXQ8O1imazw= X-Google-Smtp-Source: ABdhPJz4FQGjgNuuSqAN22EB9lhYo6aTRwD10SGtttqMxkiGBPbpX7JBYi2krQjZ+Eg0SvQjeSsjJw== X-Received: by 2002:a05:6402:8ca:: with SMTP id d10mr18968263edz.55.1643190729669; Wed, 26 Jan 2022 01:52:09 -0800 (PST) Received: from oberon.zico.biz ([151.251.254.11]) by smtp.gmail.com with ESMTPSA id v15sm9529823edq.35.2022.01.26.01.52.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jan 2022 01:52:09 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v8 9/9] trace-cmd: Add new subcommand "convert" Date: Wed, 26 Jan 2022 11:51:54 +0200 Message-Id: <20220126095154.570700-10-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220126095154.570700-1-tz.stoyanov@gmail.com> References: <20220126095154.570700-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 | 109 +++++++++++++++++++++++++++++++++ tracecmd/trace-usage.c | 13 ++++ 5 files changed, 126 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..88935dc7 --- /dev/null +++ b/tracecmd/trace-convert.c @@ -0,0 +1,109 @@ +// 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 = tracecmd_default_file_version(); + 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 }