From patchwork Fri Mar 8 13:44:15 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tzvetomir Stoyanov X-Patchwork-Id: 10844869 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D587F1803 for ; Fri, 8 Mar 2019 13:44:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C15AF2DF5B for ; Fri, 8 Mar 2019 13:44:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B4AFD2DF50; Fri, 8 Mar 2019 13:44:29 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 36CE62DF5B for ; Fri, 8 Mar 2019 13:44:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726508AbfCHNo3 (ORCPT ); Fri, 8 Mar 2019 08:44:29 -0500 Received: from mail-wm1-f67.google.com ([209.85.128.67]:37314 "EHLO mail-wm1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726565AbfCHNo2 (ORCPT ); Fri, 8 Mar 2019 08:44:28 -0500 Received: by mail-wm1-f67.google.com with SMTP id x10so12366230wmg.2 for ; Fri, 08 Mar 2019 05:44:27 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=iGC7rh6ccnovJ9Vj2x/cclblPNlik72qrfXGJ/5N6b4=; b=jfmO3QXE0GogOQgoke6448wA8rum+NZjFrn3bDqB0VjvqkiPO37Z36f7NLaesmCGlZ pgE2BCnTkWFOGcYf7wjmsehzpfwflJ9dsEanind628Ccflutiv83HPwpPaa+m5iDfXd5 dMu9RruAh0DgcSz//3NEPpq3phFNLLKGc+VLS8E9uLC37fMPpDQbUlX+D96EMgHhYppA YVJC9E5rti2m08lz91xNOizRsRb8qnD8Wzr4CGrgk3BGxLlBNyy0HXK5kCYTy7VRlunQ 5RHEOwcrwxGvCAxy/BimSACwKndI78Hi2ZsVtSuQ/h5Lp8j+BtVZYy+AHQnonl1QU7Be L1Vw== X-Gm-Message-State: APjAAAUAFh43FhF5nhBrmx2hO3wg9dxhq0sAeEKuOphun36glPUF2LIj 85DILGlMBk04UE44/fKYHJB/AnpF X-Google-Smtp-Source: APXvYqwMO/eJbHF+hoB6L4YZOVWSeJKakhy+nwamHY9A0DJeDr0oSMvGtXrcZppEveUgx+6jvNwpIg== X-Received: by 2002:a7b:c34c:: with SMTP id l12mr3059520wmj.126.1552052666384; Fri, 08 Mar 2019 05:44:26 -0800 (PST) Received: from oberon.eng.vmware.com ([146.247.46.5]) by smtp.gmail.com with ESMTPSA id w4sm11631872wmg.8.2019.03.08.05.44.25 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 08 Mar 2019 05:44:25 -0800 (PST) From: Tzvetomir Stoyanov To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v5 1/9] trace-cmd: Implemented new lib API: tracecmd_local_events_system() Date: Fri, 8 Mar 2019 15:44:15 +0200 Message-Id: <20190308134423.22008-2-tstoyanov@vmware.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190308134423.22008-1-tstoyanov@vmware.com> References: <20190308134423.22008-1-tstoyanov@vmware.com> MIME-Version: 1.0 Sender: linux-trace-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The new tracecmd lib API tracecmd_local_events_system() creates a tep handler and initializes it with the events of the specified subsystems. Signed-off-by: Tzvetomir Stoyanov --- include/trace-cmd/trace-cmd.h | 2 + lib/trace-cmd/trace-util.c | 99 +++++++++++++++++++++++------------ 2 files changed, 68 insertions(+), 33 deletions(-) diff --git a/include/trace-cmd/trace-cmd.h b/include/trace-cmd/trace-cmd.h index 27ba89d..0067882 100644 --- a/include/trace-cmd/trace-cmd.h +++ b/include/trace-cmd/trace-cmd.h @@ -32,6 +32,8 @@ void tracecmd_unload_plugins(struct tep_plugin_list *list, struct tep_handle *pe char **tracecmd_event_systems(const char *tracing_dir); char **tracecmd_system_events(const char *tracing_dir, const char *system); struct tep_handle *tracecmd_local_events(const char *tracing_dir); +struct tep_handle *tracecmd_local_events_system(const char *tracing_dir, + char **sys_names); int tracecmd_fill_local_events(const char *tracing_dir, struct tep_handle *pevent); char **tracecmd_local_plugins(const char *tracing_dir); diff --git a/lib/trace-cmd/trace-util.c b/lib/trace-cmd/trace-util.c index b5aea39..0e925ed 100644 --- a/lib/trace-cmd/trace-util.c +++ b/lib/trace-cmd/trace-util.c @@ -1119,43 +1119,15 @@ static int read_header(struct tep_handle *pevent, const char *events_dir) return ret; } -/** - * tracecmd_local_events - create a pevent from the events on system - * @tracing_dir: The directory that contains the events. - * - * Returns a pevent structure that contains the pevents local to - * the system. - */ -struct tep_handle *tracecmd_local_events(const char *tracing_dir) -{ - struct tep_handle *pevent = NULL; - - pevent = tep_alloc(); - if (!pevent) - return NULL; - - if (tracecmd_fill_local_events(tracing_dir, pevent)) { - tep_free(pevent); - pevent = NULL; - } - - return pevent; -} - -/** - * tracecmd_fill_local_events - Fill a pevent with the events on system - * @tracing_dir: The directory that contains the events. - * @pevent: Allocated pevent which will be filled - * - * Returns whether the operation succeeded - */ -int tracecmd_fill_local_events(const char *tracing_dir, struct tep_handle *pevent) +static int tracecmd_fill_local_events_system(const char *tracing_dir, + struct tep_handle *pevent, + char **sys_names) { + int ret, i, failure = 0; struct dirent *dent; char *events_dir; struct stat st; DIR *dir; - int ret, failure = 0; if (!tracing_dir) return -1; @@ -1189,7 +1161,16 @@ int tracecmd_fill_local_events(const char *tracing_dir, struct tep_handle *peven if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) continue; - + if (sys_names) { + i = 0; + while (sys_names[i]) { + if (strcmp(name, sys_names[i]) == 0) + break; + i++; + } + if (sys_names[i] == NULL) + continue; + } sys = append_file(events_dir, name); ret = stat(sys, &st); if (ret < 0 || !S_ISDIR(st.st_mode)) { @@ -1217,6 +1198,58 @@ int tracecmd_fill_local_events(const char *tracing_dir, struct tep_handle *peven return ret; } +/** + * tracecmd_local_events_system - create a tep from the events of the specified subsystem. + * + * @tracing_dir: The directory that contains the events. + * @sys_name: Array of system names, to load the events from. + * The last element from the array must be NULL + * + * Returns a tep structure that contains the tep local to + * the system. + */ +struct tep_handle *tracecmd_local_events_system(const char *tracing_dir, + char **sys_names) +{ + struct tep_handle *tep = NULL; + + tep = tep_alloc(); + if (!tep) + return NULL; + + if (tracecmd_fill_local_events_system(tracing_dir, tep, sys_names)) { + tep_free(tep); + tep = NULL; + } + + return tep; +} + +/** + * tracecmd_local_events - create a pevent from the events on system + * @tracing_dir: The directory that contains the events. + * + * Returns a pevent structure that contains the pevents local to + * the system. + */ +struct tep_handle *tracecmd_local_events(const char *tracing_dir) +{ + return tracecmd_local_events_system(tracing_dir, NULL); +} + +/** + * tracecmd_fill_local_events - Fill a pevent with the events on system + * @tracing_dir: The directory that contains the events. + * @pevent: Allocated pevent which will be filled + * + * Returns whether the operation succeeded + */ +int tracecmd_fill_local_events(const char *tracing_dir, + struct tep_handle *pevent) +{ + return tracecmd_fill_local_events_system(tracing_dir, pevent, NULL); +} + /** * tracecmd_local_plugins - returns an array of available tracer plugins * @tracing_dir: The directory that contains the tracing directory From patchwork Fri Mar 8 13:44:16 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tzvetomir Stoyanov X-Patchwork-Id: 10844871 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9B9A51869 for ; Fri, 8 Mar 2019 13:44:30 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 86FE42DF4F for ; Fri, 8 Mar 2019 13:44:30 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7B62E2DF76; Fri, 8 Mar 2019 13:44:30 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1E2FC2DF4F for ; Fri, 8 Mar 2019 13:44:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726565AbfCHNo3 (ORCPT ); Fri, 8 Mar 2019 08:44:29 -0500 Received: from mail-wm1-f67.google.com ([209.85.128.67]:55927 "EHLO mail-wm1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726420AbfCHNo3 (ORCPT ); Fri, 8 Mar 2019 08:44:29 -0500 Received: by mail-wm1-f67.google.com with SMTP id q187so12703602wme.5 for ; Fri, 08 Mar 2019 05:44:28 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=0Akwbj/kBryCNHRJuSYOiQOEQ7urlKUyQpryXh2sI2c=; b=si/L+CGFbMGA11+khtqqw+3csfAwoyCWkVldgo1rgtgHC1EVhasfjviwnEslzTIMpc 21B6vJMzhvF8WxXpEIkFKmgaS2QoZ3UB+EwwzxpefKVyPSbMA9wKyqniEn4h436/yx2H WMnePALEmpdA4fgQeyr+pmk7gfuYsDm7wW9w4OS4is0dvT8uPCJsTxNEk+sSNZfTkw4l gSWZnzB+PRcpWVQThXvuFriLULf7Z3Vk3B9IN56dBFWge6Ik72FUuM7ayvVvQr0wrIV1 fVwVlh6EZJsfO8/QQi7Mm35sDI4AYs+HnrG4Tqgqe74TBTyw/xfZ8GP0ILQ/3TVs40q7 dyDQ== X-Gm-Message-State: APjAAAVXl3oy4hiCQCWKh7kQZmaIlPdwhHr7Tu5paH8Ny6GrqCvHHnF/ /YxnmUbRYCmlQMaCyTM1vDM= X-Google-Smtp-Source: APXvYqy+j/uVxCfCOvmbo64x9/7LFaUSKGU860HAKRC4zEQQAz/o/RgsPtXh0X2dj3GNh6a6WVSetw== X-Received: by 2002:a1c:1cf:: with SMTP id 198mr8815678wmb.52.1552052667412; Fri, 08 Mar 2019 05:44:27 -0800 (PST) Received: from oberon.eng.vmware.com ([146.247.46.5]) by smtp.gmail.com with ESMTPSA id w4sm11631872wmg.8.2019.03.08.05.44.26 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 08 Mar 2019 05:44:26 -0800 (PST) From: Tzvetomir Stoyanov To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v5 2/9] trace-cmd: Added support for negative time offsets in trace.dat file Date: Fri, 8 Mar 2019 15:44:16 +0200 Message-Id: <20190308134423.22008-3-tstoyanov@vmware.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190308134423.22008-1-tstoyanov@vmware.com> References: <20190308134423.22008-1-tstoyanov@vmware.com> MIME-Version: 1.0 Sender: linux-trace-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When synchronizing timestamps between different machines, there are cases when the time offset is negative. This patch changes the way time offset is written and read from trace.dat file - as signed decimal, instead of hex. Signed-off-by: Tzvetomir Stoyanov --- include/trace-cmd/trace-cmd.h | 2 +- lib/trace-cmd/trace-input.c | 6 +++--- tracecmd/trace-read.c | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/trace-cmd/trace-cmd.h b/include/trace-cmd/trace-cmd.h index 0067882..3f0ab4f 100644 --- a/include/trace-cmd/trace-cmd.h +++ b/include/trace-cmd/trace-cmd.h @@ -126,7 +126,7 @@ int tracecmd_is_buffer_instance(struct tracecmd_input *handle); void tracecmd_create_top_instance(char *name); void tracecmd_remove_instances(void); -void tracecmd_set_ts_offset(struct tracecmd_input *handle, unsigned long long offset); +void tracecmd_set_ts_offset(struct tracecmd_input *handle, long long offset); void tracecmd_set_ts2secs(struct tracecmd_input *handle, unsigned long long hz); void tracecmd_print_events(struct tracecmd_input *handle, const char *regex); diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index 34a8301..be6720c 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -91,7 +91,7 @@ struct tracecmd_input { bool read_page; bool use_pipe; struct cpu_data *cpu_data; - unsigned long long ts_offset; + long long ts_offset; double ts2secs; char * cpustats; char * uname; @@ -2082,7 +2082,7 @@ static int init_cpu(struct tracecmd_input *handle, int cpu) } void tracecmd_set_ts_offset(struct tracecmd_input *handle, - unsigned long long offset) + long long offset) { handle->ts_offset = offset; } @@ -2099,7 +2099,7 @@ void tracecmd_set_ts2secs(struct tracecmd_input *handle, static int handle_options(struct tracecmd_input *handle) { - unsigned long long offset; + long long offset; unsigned short option; unsigned int size; char *cpustats = NULL; diff --git a/tracecmd/trace-read.c b/tracecmd/trace-read.c index 804022a..e309672 100644 --- a/tracecmd/trace-read.c +++ b/tracecmd/trace-read.c @@ -58,7 +58,7 @@ static struct list_head handle_list; struct input_files { struct list_head list; const char *file; - unsigned long long tsoffset; + long long tsoffset; unsigned long long ts2secs; }; static struct list_head input_files; @@ -1413,7 +1413,7 @@ void trace_report (int argc, char **argv) struct input_files *inputs; struct handle_list *handles; enum output_type otype; - unsigned long long tsoffset = 0; + long long tsoffset = 0; unsigned long long ts2secs = 0; unsigned long long ts2sc; int show_stat = 0; From patchwork Fri Mar 8 13:44:17 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tzvetomir Stoyanov X-Patchwork-Id: 10844873 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id DEDEA17E9 for ; Fri, 8 Mar 2019 13:44:30 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CC87C2DF4F for ; Fri, 8 Mar 2019 13:44:30 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C0C912DF5B; Fri, 8 Mar 2019 13:44:30 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 764452DF50 for ; Fri, 8 Mar 2019 13:44:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726579AbfCHNoa (ORCPT ); Fri, 8 Mar 2019 08:44:30 -0500 Received: from mail-wm1-f66.google.com ([209.85.128.66]:54470 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726613AbfCHNoa (ORCPT ); Fri, 8 Mar 2019 08:44:30 -0500 Received: by mail-wm1-f66.google.com with SMTP id f3so12692916wmj.4 for ; Fri, 08 Mar 2019 05:44:29 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=uk3prq7PM5NWRxFeku/sMfCiWVMc5DcibNxNMdyfE4M=; b=QrtLXFPuxcnJxv0Ugitvw7ienRs+L/VynXCQiycUy38hw67i3O9HwHBHx63+nu31sE XwemZeqIj5WPv/hGQFS4SLjawt/AKlUlwD1lK+KSFoqn5AWszaDZ7Wx7NWeKn0IILPQJ 6BquTxRu7KqTDoHdmG00+Ud0R9xPpngkBv9htyyJVcrWYnPogL+g/x4934ofolDk9DpO 5TNc78WZK34dtW0N2CrJFDFRybKCUt7wVvS5pI+kWcdUQjfAiFel0umRS4yVF9PT4Yfv zMfIzdA+wOBcy/z25KnTX4F5ewpbpb8SnDRQ4qaYffoPksWVM38Sm86GgFYmw/l6dODd Vf6g== X-Gm-Message-State: APjAAAW28QOU7acHis4yW67Bn9dUHtnWGut2BOh3WnReTCyikjINj8pL WgN5Jc4Fng4oFC8YX45vyp2/KWSj X-Google-Smtp-Source: APXvYqxRxZp/pQFhk1KViG7+htlBxeyEv9IWAAXyZx/8JTbIevjFlA8slWj7eDNr5zDq7qCip5a7xg== X-Received: by 2002:a7b:c352:: with SMTP id l18mr8920739wmj.127.1552052668375; Fri, 08 Mar 2019 05:44:28 -0800 (PST) Received: from oberon.eng.vmware.com ([146.247.46.5]) by smtp.gmail.com with ESMTPSA id w4sm11631872wmg.8.2019.03.08.05.44.27 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 08 Mar 2019 05:44:27 -0800 (PST) From: Tzvetomir Stoyanov To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v5 3/9] trace-cmd: Fix tracecmd_read_page_record() to read more than one event Date: Fri, 8 Mar 2019 15:44:17 +0200 Message-Id: <20190308134423.22008-4-tstoyanov@vmware.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190308134423.22008-1-tstoyanov@vmware.com> References: <20190308134423.22008-1-tstoyanov@vmware.com> MIME-Version: 1.0 Sender: linux-trace-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The kbuffer_next_event() will return the next event on the sub buffer. If we pass in the last_record to tracecmd_read_page_record(), it initializes the sub buffer, and by calling kbuffer_next_event() (instead of kbuffer_read_event()), the second event on the sub buffer is returned. This causes the match of the last_record not to match if the last_record happens to be the first event on the sub buffer. Signed-off-by: Tzvetomir Stoyanov --- lib/trace-cmd/trace-input.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index be6720c..0a6e820 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -1679,18 +1679,22 @@ tracecmd_read_page_record(struct tep_handle *pevent, void *page, int size, goto out_free; } - do { + ptr = kbuffer_read_event(kbuf, &ts); + while (ptr < last_record->data) { ptr = kbuffer_next_event(kbuf, NULL); if (!ptr) break; - } while (ptr < last_record->data); + if (ptr == last_record->data) + break; + } if (ptr != last_record->data) { warning("tracecmd_read_page_record: could not find last_record"); goto out_free; } - } + ptr = kbuffer_next_event(kbuf, &ts); + } else + ptr = kbuffer_read_event(kbuf, &ts); - ptr = kbuffer_read_event(kbuf, &ts); if (!ptr) goto out_free; From patchwork Fri Mar 8 13:44:18 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tzvetomir Stoyanov X-Patchwork-Id: 10844875 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 462A918A7 for ; Fri, 8 Mar 2019 13:44:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 319002DF4F for ; Fri, 8 Mar 2019 13:44:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2623F2DF5B; Fri, 8 Mar 2019 13:44:32 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CE95C2DF4F for ; Fri, 8 Mar 2019 13:44:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726622AbfCHNob (ORCPT ); Fri, 8 Mar 2019 08:44:31 -0500 Received: from mail-wr1-f65.google.com ([209.85.221.65]:44586 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726420AbfCHNob (ORCPT ); Fri, 8 Mar 2019 08:44:31 -0500 Received: by mail-wr1-f65.google.com with SMTP id w2so21413689wrt.11 for ; Fri, 08 Mar 2019 05:44:30 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=KZrqIpIWaDsmqIPJyX90jfu7nox1DuIvSLjj1esD/k8=; b=eYHZW7fksGxOz3giiEQURtbtPxmCImoCT22/m7/vjUr5XvlLavpJux9dXqhFbiGBIK huEeVv33sT7zt1IDVSbjHN2avFc0QbHZNk8vyNVvlycGX4itj6WF6IbYi2uMCOaPatFW yBm9e2XbtmDX+pJA7i7Nn+5JPnYsve/s7ODytjvhgVkln/WAzE2Vl6y2QLqUl1qXMXyt 3YWux6D01DiWG7u/8RtDD1BzJE8jz4owjVPmwGjStgqgVZX1wTZ4KD7Drw2ecoVVcp3I v0fpTKgs4Btxva1T+X0Mz+CKq4ojqxMeJEg0wwa+kN3Hz0tNV0LwUds2We4fP5sigQpd o8aQ== X-Gm-Message-State: APjAAAVwwP6Z5oGtbGhU9h+uwrkA4EuDlvAV0bjCxfzarmAMmW8T8RaN G1aR7m00jKVsUmB0XGjr9CIgmsbO X-Google-Smtp-Source: APXvYqy9KgOcvoD4F7YJLW3hJEeQ8odWKZ9Ve8IOrJ7w41mhfKXnJ3yYRsXEy19w3yWf9pvk5Rq27A== X-Received: by 2002:adf:a382:: with SMTP id l2mr2284647wrb.79.1552052669318; Fri, 08 Mar 2019 05:44:29 -0800 (PST) Received: from oberon.eng.vmware.com ([146.247.46.5]) by smtp.gmail.com with ESMTPSA id w4sm11631872wmg.8.2019.03.08.05.44.28 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 08 Mar 2019 05:44:28 -0800 (PST) From: Tzvetomir Stoyanov To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v5 4/9] trace-cmd: Added implementation of htonll() and ntohll() Date: Fri, 8 Mar 2019 15:44:18 +0200 Message-Id: <20190308134423.22008-5-tstoyanov@vmware.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190308134423.22008-1-tstoyanov@vmware.com> References: <20190308134423.22008-1-tstoyanov@vmware.com> MIME-Version: 1.0 Sender: linux-trace-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Added implementation of htonll() and ntohll() as macros, if they are not already defined. Signed-off-by: Tzvetomir Stoyanov --- tracecmd/include/trace-msg.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tracecmd/include/trace-msg.h b/tracecmd/include/trace-msg.h index b7fe10b..445f799 100644 --- a/tracecmd/include/trace-msg.h +++ b/tracecmd/include/trace-msg.h @@ -15,4 +15,14 @@ extern unsigned int page_size; void plog(const char *fmt, ...); void pdie(const char *fmt, ...); +#ifndef htonll +# if __BYTE_ORDER == __LITTLE_ENDIAN +#define htonll(x) __bswap_64(x) +#define ntohll(x) __bswap_64(x) +#else +#define htonll(x) (x) +#define ntohll(x) (x) +#endif +#endif + #endif /* _TRACE_MSG_H_ */ From patchwork Fri Mar 8 13:44:19 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tzvetomir Stoyanov X-Patchwork-Id: 10844879 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2706217E9 for ; Fri, 8 Mar 2019 13:44:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 120E72DF5B for ; Fri, 8 Mar 2019 13:44:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 06A712DF76; Fri, 8 Mar 2019 13:44:34 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6090F2DF4F for ; Fri, 8 Mar 2019 13:44:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726733AbfCHNod (ORCPT ); Fri, 8 Mar 2019 08:44:33 -0500 Received: from mail-wm1-f65.google.com ([209.85.128.65]:52131 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726613AbfCHNod (ORCPT ); Fri, 8 Mar 2019 08:44:33 -0500 Received: by mail-wm1-f65.google.com with SMTP id n19so12577966wmi.1 for ; Fri, 08 Mar 2019 05:44:31 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=1mKApxI7kawm9QL1wqZ1c9itYK2/mW362scd4gL+P/k=; b=bndfbt72EX570BCMf7+/n9sOQMSVVYr7WZjqSG9Ng9HFAKOf/UDPSFXRhAVDtNvhwy UdRgBinnbbwY39ZJYWEvcNTeduFCR5MITt03PYaX9/6zCFceCmzZHb0KCOnYKI9u76li hTC5EARalOw3SUoo67lfEeqJjQKlAvNEpZRS6ndB7ddMQh+FjirSj8ChNsTFhVIE8RR4 BsxwwS2wW81+54zR8OikhVfuRhj7IyB5B67phGV2aJq/9EMjMLj+bhAkEe7TEMOt8vlO 0edlF2B2GfjD/UJHzkZ7F0fvnmQMYpXbJHtdDLS6SjIQECec+7Lkof+IbzgTWJm6JjPs Hlmg== X-Gm-Message-State: APjAAAVB9Wx8u4JYJMOPDWz1DqwQK1pYm9iZhgtqG2nEQPywbq+CmKOY ieDT83cGmGzoYfbkGYCRvgQgVMOm X-Google-Smtp-Source: APXvYqxauHDptOZE0qkUY5zWBOraNr37O/GUGaNrXOd1ihPfYzrAtu6wzOPHfZ915xJgE+q78kgIXw== X-Received: by 2002:a1c:1b8a:: with SMTP id b132mr9188669wmb.138.1552052670239; Fri, 08 Mar 2019 05:44:30 -0800 (PST) Received: from oberon.eng.vmware.com ([146.247.46.5]) by smtp.gmail.com with ESMTPSA id w4sm11631872wmg.8.2019.03.08.05.44.29 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 08 Mar 2019 05:44:29 -0800 (PST) From: Tzvetomir Stoyanov To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v5 5/9] trace-cmd: Refactored few functions in trace-record.c Date: Fri, 8 Mar 2019 15:44:19 +0200 Message-Id: <20190308134423.22008-6-tstoyanov@vmware.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190308134423.22008-1-tstoyanov@vmware.com> References: <20190308134423.22008-1-tstoyanov@vmware.com> MIME-Version: 1.0 Sender: linux-trace-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP In order to reuse the code inside the trace-cmd application, few functions from trace-record.c are refactored: - make_instances() and tracecmd_remove_instances() are splited. New ones are created: tracecmd_make_instance() and tracecmd_remove_instance(), which are visible outside the trace-record.c - Following functions are made non-static: tracecmd_init_instance() get_instance_dir(), write_instance_file(), write_tracing_on(), tracecmd_set_clock() - New function is implemented: tracecmd_local_cpu_count(), an internal API to get local_cpu_count. Signed-off-by: Tzvetomir Stoyanov --- tracecmd/include/trace-local.h | 9 ++++ tracecmd/trace-record.c | 87 +++++++++++++++++++--------------- 2 files changed, 58 insertions(+), 38 deletions(-) diff --git a/tracecmd/include/trace-local.h b/tracecmd/include/trace-local.h index d7bdb1f..8413054 100644 --- a/tracecmd/include/trace-local.h +++ b/tracecmd/include/trace-local.h @@ -235,6 +235,15 @@ void update_first_instance(struct buffer_instance *instance, int topt); void show_instance_file(struct buffer_instance *instance, const char *name); int count_cpus(void); +void write_tracing_on(struct buffer_instance *instance, int on); +char *get_instance_dir(struct buffer_instance *instance); +int write_instance_file(struct buffer_instance *instance, + const char *file, const char *str, const char *type); +void tracecmd_init_instance(struct buffer_instance *instance); +void tracecmd_make_instance(struct buffer_instance *instance); +int tracecmd_local_cpu_count(void); +void tracecmd_set_clock(struct buffer_instance *instance); +void tracecmd_remove_instance(struct buffer_instance *instance); /* No longer in event-utils.h */ void __noreturn die(const char *fmt, ...); /* Can be overriden */ diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c index 177060d..3950242 100644 --- a/tracecmd/trace-record.c +++ b/tracecmd/trace-record.c @@ -184,7 +184,7 @@ static inline int no_top_instance(void) return first_instance != &top_instance; } -static void init_instance(struct buffer_instance *instance) +void tracecmd_init_instance(struct buffer_instance *instance) { instance->event_next = &instance->events; } @@ -308,7 +308,7 @@ static void reset_save_file_cond(const char *file, int prio, */ void add_instance(struct buffer_instance *instance, int cpu_count) { - init_instance(instance); + tracecmd_init_instance(instance); instance->next = buffer_instances; if (first_instance == buffer_instances) first_instance = instance; @@ -495,7 +495,7 @@ static void add_event(struct buffer_instance *instance, struct event_list *event static void reset_event_list(struct buffer_instance *instance) { instance->events = NULL; - init_instance(instance); + tracecmd_init_instance(instance); } static char *get_temp_file(struct buffer_instance *instance, int cpu) @@ -791,8 +791,7 @@ get_instance_file(struct buffer_instance *instance, const char *file) return path; } -static char * -get_instance_dir(struct buffer_instance *instance) +char *get_instance_dir(struct buffer_instance *instance) { char *buf; char *path; @@ -1668,9 +1667,8 @@ static int write_file(const char *file, const char *str, const char *type) return ret; } -static int -write_instance_file(struct buffer_instance *instance, - const char *file, const char *str, const char *type) +int write_instance_file(struct buffer_instance *instance, + const char *file, const char *str, const char *type) { char *path; int ret; @@ -1946,7 +1944,7 @@ static int open_tracing_on(struct buffer_instance *instance) return fd; } -static void write_tracing_on(struct buffer_instance *instance, int on) +void write_tracing_on(struct buffer_instance *instance, int on) { int ret; int fd; @@ -2262,7 +2260,7 @@ void tracecmd_enable_events(void) enable_events(first_instance); } -static void set_clock(struct buffer_instance *instance) +void tracecmd_set_clock(struct buffer_instance *instance) { char *path; char *content; @@ -4316,49 +4314,57 @@ static void clear_func_filters(void) } } -static void make_instances(void) +void tracecmd_make_instance(struct buffer_instance *instance) { - struct buffer_instance *instance; struct stat st; char *path; int ret; + path = get_instance_dir(instance); + ret = stat(path, &st); + if (ret < 0) { + ret = mkdir(path, 0777); + if (ret < 0) + die("mkdir %s", path); + } else + /* Don't delete instances that already exist */ + instance->flags |= BUFFER_FL_KEEP; + tracecmd_put_tracing_file(path); + +} + +static void make_instances(void) +{ + struct buffer_instance *instance; + for_each_instance(instance) { if (is_guest(instance)) continue; + tracecmd_make_instance(instance); + } +} - path = get_instance_dir(instance); - ret = stat(path, &st); - if (ret < 0) { - ret = mkdir(path, 0777); - if (ret < 0) - die("mkdir %s", path); - } else - /* Don't delete instances that already exist */ - instance->flags |= BUFFER_FL_KEEP; - tracecmd_put_tracing_file(path); +void tracecmd_remove_instance(struct buffer_instance *instance) +{ + char *path; + + if (instance->tracing_on_fd > 0) { + close(instance->tracing_on_fd); + instance->tracing_on_fd = 0; } + path = get_instance_dir(instance); + tracecmd_put_tracing_file(path); } void tracecmd_remove_instances(void) { struct buffer_instance *instance; - char *path; - int ret; for_each_instance(instance) { /* Only delete what we created */ if (is_guest(instance) || (instance->flags & BUFFER_FL_KEEP)) continue; - if (instance->tracing_on_fd > 0) { - close(instance->tracing_on_fd); - instance->tracing_on_fd = 0; - } - path = get_instance_dir(instance); - ret = rmdir(path); - if (ret < 0) - die("rmdir %s", path); - tracecmd_put_tracing_file(path); + tracecmd_remove_instance(instance); } } @@ -4853,7 +4859,7 @@ void trace_stop(int argc, char **argv) int topt = 0; struct buffer_instance *instance = &top_instance; - init_instance(instance); + tracecmd_init_instance(instance); for (;;) { int c; @@ -4894,7 +4900,7 @@ void trace_restart(int argc, char **argv) int topt = 0; struct buffer_instance *instance = &top_instance; - init_instance(instance); + tracecmd_init_instance(instance); for (;;) { int c; @@ -4936,7 +4942,7 @@ void trace_reset(int argc, char **argv) int topt = 0; struct buffer_instance *instance = &top_instance; - init_instance(instance); + tracecmd_init_instance(instance); /* if last arg is -a, then -b and -d apply to all instances */ int last_specified_all = 0; @@ -5014,11 +5020,16 @@ static void init_common_record_context(struct common_record_context *ctx, memset(ctx, 0, sizeof(*ctx)); ctx->instance = &top_instance; ctx->curr_cmd = curr_cmd; - init_instance(ctx->instance); + tracecmd_init_instance(ctx->instance); local_cpu_count = count_cpus(); ctx->instance->cpu_count = local_cpu_count; } +int tracecmd_local_cpu_count(void) +{ + return local_cpu_count; +} + #define IS_EXTRACT(ctx) ((ctx)->curr_cmd == CMD_extract) #define IS_START(ctx) ((ctx)->curr_cmd == CMD_start) #define IS_STREAM(ctx) ((ctx)->curr_cmd == CMD_stream) @@ -5589,7 +5600,7 @@ static void record_trace(int argc, char **argv, tracecmd_disable_all_tracing(1); for_all_instances(instance) - set_clock(instance); + tracecmd_set_clock(instance); /* Record records the date first */ if (ctx->date && From patchwork Fri Mar 8 13:44:20 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tzvetomir Stoyanov X-Patchwork-Id: 10844881 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 41EBA1803 for ; Fri, 8 Mar 2019 13:44:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2E2672DF4F for ; Fri, 8 Mar 2019 13:44:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 22AB12DF83; Fri, 8 Mar 2019 13:44:34 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AEF862DF50 for ; Fri, 8 Mar 2019 13:44:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726748AbfCHNod (ORCPT ); Fri, 8 Mar 2019 08:44:33 -0500 Received: from mail-wr1-f68.google.com ([209.85.221.68]:33030 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726420AbfCHNod (ORCPT ); Fri, 8 Mar 2019 08:44:33 -0500 Received: by mail-wr1-f68.google.com with SMTP id i12so21418438wrw.0 for ; Fri, 08 Mar 2019 05:44:32 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=c07RBxy3r3sgBU5lzu6Hll8axSgYZtDit0u/zFUdcfM=; b=qchbohGAk7P0JJr0zFo9nmsg9qK4IEhP6Twry1RCyqHC+cMBAFrPWx8+DkYIUTVKaD KoYNSrSipFJsS3Wifc/Ri4m3IAub4QSNPTUfrtKKfyPazefu7Jqi1v4QODeGo4YBINOu XSdkf67PvYjKGuFkMJ4K36XoI0Glvu6cI2XyvOjtu5j8YzIuKX/vo23Rh4LQ3jFhtwwm 3/l2bQgJpcKZQLOqIYTeR/JkH20jy1cbsaTyEhdUJQ8WDqQNjxmZIOdQdq7de/pwZyS9 JxdCyurxUBH31TgvTVAr5CITaOhf/fXi59pdoXYbPGlNuYv/HVh88Hi4GZVczOWSZ0cC AwtQ== X-Gm-Message-State: APjAAAUsQtlOzhctqoxSJiboOuNUs53Hvm4b+GEUtnXFDYCNco/wrgyO G0XX2Jj/WxjNvkk7NpFcM84= X-Google-Smtp-Source: APXvYqyBBWmvN0nubcRbQdxNwWBfGmnIzqbuogXFBsTs3g6otKoP5Yhf2BxXq5MuAhu6Cg8DkyuI8w== X-Received: by 2002:a5d:44c3:: with SMTP id z3mr10702488wrr.329.1552052671273; Fri, 08 Mar 2019 05:44:31 -0800 (PST) Received: from oberon.eng.vmware.com ([146.247.46.5]) by smtp.gmail.com with ESMTPSA id w4sm11631872wmg.8.2019.03.08.05.44.30 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 08 Mar 2019 05:44:30 -0800 (PST) From: Tzvetomir Stoyanov To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v5 6/9] trace-cmd: Find and store pids of tasks, which run virtual CPUs of given VM Date: Fri, 8 Mar 2019 15:44:20 +0200 Message-Id: <20190308134423.22008-7-tstoyanov@vmware.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190308134423.22008-1-tstoyanov@vmware.com> References: <20190308134423.22008-1-tstoyanov@vmware.com> MIME-Version: 1.0 Sender: linux-trace-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP In order to match host and guest events, a mapping between guest VCPU and the host task, running this VCPU is needed. Extended existing struct guest to hold such mapping and added logic in read_qemu_guests() function to initialize it. Implemented a new internal API, get_guest_vcpu_pid(), to retrieve VCPU-task mapping for given VM. Signed-off-by: Tzvetomir Stoyanov --- tracecmd/include/trace-local.h | 1 + tracecmd/trace-record.c | 68 ++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/tracecmd/include/trace-local.h b/tracecmd/include/trace-local.h index 8413054..62f5e47 100644 --- a/tracecmd/include/trace-local.h +++ b/tracecmd/include/trace-local.h @@ -245,6 +245,7 @@ int tracecmd_local_cpu_count(void); void tracecmd_set_clock(struct buffer_instance *instance); void tracecmd_remove_instance(struct buffer_instance *instance); +int get_guest_vcpu_pid(unsigned int guest_cid, unsigned int guest_vcpu); /* No longer in event-utils.h */ void __noreturn die(const char *fmt, ...); /* Can be overriden */ void *malloc_or_die(unsigned int size); /* Can be overridden */ diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c index 3950242..d9e9c69 100644 --- a/tracecmd/trace-record.c +++ b/tracecmd/trace-record.c @@ -2746,10 +2746,12 @@ static bool is_digits(const char *s) return true; } +#define VCPUS_MAX 256 struct guest { char *name; int cid; int pid; + int cpu_pid[VCPUS_MAX]; }; static struct guest *guests; @@ -2767,6 +2769,57 @@ static char *get_qemu_guest_name(char *arg) return arg; } +static void read_qemu_guests_pids(char *guest_task, struct guest *guest) +{ + struct dirent *entry_t; + char path[PATH_MAX]; + unsigned int vcpu; + char *buf = NULL; + char *cpu_str; + DIR *dir; + FILE *ft; + size_t n; + int j; + + + snprintf(path, sizeof(path), "/proc/%s/task", guest_task); + dir = opendir(path); + if (!dir) + return; + + for (entry_t = readdir(dir); entry_t; entry_t = readdir(dir)) { + if (!(entry_t->d_type == DT_DIR && + is_digits(entry_t->d_name))) + continue; + snprintf(path, sizeof(path), + "/proc/%s/task/%s/comm", + guest_task, entry_t->d_name); + ft = fopen(path, "r"); + if (!ft) + continue; + getline(&buf, &n, ft); + if (buf && strncmp(buf, "CPU ", 4) == 0) { + cpu_str = buf; + while (*cpu_str != '\0' && + isdigit(*cpu_str) == 0) + cpu_str++; + if (*cpu_str != '\0') { + j = 0; + while (cpu_str[j] != '\0' && + isdigit(cpu_str[j]) != 0) + j++; + cpu_str[j] = '\0'; + vcpu = atoi(cpu_str); + if (vcpu < VCPUS_MAX) + guest->cpu_pid[vcpu] = atoi(entry_t->d_name); + } + } + free(buf); + fclose(ft); + buf = NULL; + } +} + static void read_qemu_guests(void) { static bool initialized; @@ -2828,6 +2881,8 @@ static void read_qemu_guests(void) if (!is_qemu) goto next; + read_qemu_guests_pids(entry->d_name, &guest); + guests = realloc(guests, (guests_len + 1) * sizeof(*guests)); if (!guests) die("Can not allocate guest buffer"); @@ -2873,6 +2928,19 @@ static char *parse_guest_name(char *guest, int *cid, int *port) return guest; } +int get_guest_vcpu_pid(unsigned int guest_cid, unsigned int guest_vcpu) +{ + int i; + + if (!guests || guest_vcpu >= VCPUS_MAX) + return -1; + + for (i = 0; i < guests_len; i++) + if (guest_cid == guests[i].cid) + return guests[i].cpu_pid[guest_vcpu]; + return -1; +} + static void set_prio(int prio) { struct sched_param sp; From patchwork Fri Mar 8 13:44:21 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tzvetomir Stoyanov X-Patchwork-Id: 10844883 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 05FC51390 for ; Fri, 8 Mar 2019 13:44:35 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E57C02DF4F for ; Fri, 8 Mar 2019 13:44:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D9CBE2DF5B; Fri, 8 Mar 2019 13:44:34 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 53FA32DF4F for ; Fri, 8 Mar 2019 13:44:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726420AbfCHNoe (ORCPT ); Fri, 8 Mar 2019 08:44:34 -0500 Received: from mail-wr1-f65.google.com ([209.85.221.65]:40165 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726706AbfCHNoe (ORCPT ); Fri, 8 Mar 2019 08:44:34 -0500 Received: by mail-wr1-f65.google.com with SMTP id t6so2213744wrw.7 for ; Fri, 08 Mar 2019 05:44:32 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=r35nAWnDGLi7XaFd52nE7EtPM/JcoTSF2Yuqxw5WQIo=; b=a+xro8cmACHSRRzjazInFkMljNXy8uRJ9lppowH8kc78yUNg0hHy2vPWoDFv9bBkYL 6/3ebm+58wrn89t7JNYJ4WepiNf+vccvSIzgStwpzB9j8ubcHBtk2wjOUV90hUlSxu3D QcyNHD7XvE5exJXbCr6GNRs5+FCjqeaL1/bmSKIX3YlMhGZWwUT45u1L6iqWQf2znTSo Yi8hdm3QQjmy2+CRgBlV4OsEa6EzzYOvwIDTReajbRwWxdSlkztMWy1k4FI5d/bNlxtN xpkjpRr4brk4pvcTTVEf7EB8t0StovD33/f/M+wR27GcwFiKZInkMlFA9GMO3+WxUOTg IY2Q== X-Gm-Message-State: APjAAAXbChoML/b0H1F4wWkvIZGTweSeFjZxXaUm3OfXEr5nIFgUuiW8 f6eU/eGaQY0HwUZJ+lnnaw4= X-Google-Smtp-Source: APXvYqxkaeR3fX9zHa2tatpg0WpUJi27LV0xCINAgP7EO8KtRIdKhV1swhd462pMcurunXsYmB+bOg== X-Received: by 2002:a5d:62cc:: with SMTP id o12mr10818054wrv.242.1552052671991; Fri, 08 Mar 2019 05:44:31 -0800 (PST) Received: from oberon.eng.vmware.com ([146.247.46.5]) by smtp.gmail.com with ESMTPSA id w4sm11631872wmg.8.2019.03.08.05.44.31 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 08 Mar 2019 05:44:31 -0800 (PST) From: Tzvetomir Stoyanov To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v5 7/9] trace-cmd: Implemented new API tracecmd_add_option_v() Date: Fri, 8 Mar 2019 15:44:21 +0200 Message-Id: <20190308134423.22008-8-tstoyanov@vmware.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190308134423.22008-1-tstoyanov@vmware.com> References: <20190308134423.22008-1-tstoyanov@vmware.com> MIME-Version: 1.0 Sender: linux-trace-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This patch implements a new tracecmd API, tracecmd_add_option_v() It adds new option in trace.dat, similar to tracecmd_add_option(), but the option's data is passed as list of buffers. The standard struct iovec is used as input parameter, containing the option's data buffers. Signed-off-by: Tzvetomir Stoyanov --- include/trace-cmd/trace-cmd.h | 5 ++ include/traceevent/event-parse.h | 1 + tracecmd/trace-output.c | 117 ++++++++++++++++++++++++++----- 3 files changed, 106 insertions(+), 17 deletions(-) diff --git a/include/trace-cmd/trace-cmd.h b/include/trace-cmd/trace-cmd.h index 3f0ab4f..023b701 100644 --- a/include/trace-cmd/trace-cmd.h +++ b/include/trace-cmd/trace-cmd.h @@ -245,11 +245,16 @@ struct tracecmd_output *tracecmd_create_init_file_override(const char *output_fi struct tracecmd_option *tracecmd_add_option(struct tracecmd_output *handle, unsigned short id, int size, const void *data); +struct tracecmd_option * +tracecmd_add_option_v(struct tracecmd_output *handle, + unsigned short id, const struct iovec *vector, int count); + struct tracecmd_option *tracecmd_add_buffer_option(struct tracecmd_output *handle, const char *name, int cpus); int tracecmd_write_cpus(struct tracecmd_output *handle, int cpus); int tracecmd_write_options(struct tracecmd_output *handle); +int tracecmd_append_options(struct tracecmd_output *handle); int tracecmd_update_option(struct tracecmd_output *handle, struct tracecmd_option *option, int size, const void *data); diff --git a/include/traceevent/event-parse.h b/include/traceevent/event-parse.h index 3f2ae80..9705b51 100644 --- a/include/traceevent/event-parse.h +++ b/include/traceevent/event-parse.h @@ -11,6 +11,7 @@ #include #include #include +#include #include "trace-seq.h" diff --git a/tracecmd/trace-output.c b/tracecmd/trace-output.c index 33d6ce3..d57539c 100644 --- a/tracecmd/trace-output.c +++ b/tracecmd/trace-output.c @@ -883,21 +883,23 @@ static struct tracecmd_output *create_file(const char *output_file, } /** - * tracecmd_add_option - add options to the file + * tracecmd_add_option_v - add options to the file * @handle: the output file handle name * @id: the id of the option - * @size: the size of the option data - * @data: the data to write to the file. + * @vector: array of vectors, pointing to the data to write in the file + * @count: number of items in the vector array * * Returns handle to update option if needed. * Just the content can be updated, with smaller or equal to * content than the specified size. */ struct tracecmd_option * -tracecmd_add_option(struct tracecmd_output *handle, - unsigned short id, int size, const void *data) +tracecmd_add_option_v(struct tracecmd_output *handle, + unsigned short id, const struct iovec *vector, int count) { struct tracecmd_option *option; + char *data = NULL; + int i, size = 0; /* * We can only add options before they were written. @@ -906,32 +908,63 @@ tracecmd_add_option(struct tracecmd_output *handle, if (handle->options_written) return NULL; - handle->nr_options++; + for (i = 0; i < count; i++) + size += vector[i].iov_len; + + /* Some IDs (like TRACECMD_OPTION_TRACECLOCK) pass vector with 0 / NULL data */ + if (size) { + data = malloc(size); + if (!data) { + warning("Insufficient memory"); + return NULL; + } + } option = malloc(sizeof(*option)); if (!option) { warning("Could not allocate space for option"); + free(data); return NULL; } - option->id = id; - option->size = size; - option->data = malloc(size); - if (!option->data) { - warning("Insufficient memory"); - free(option); - return NULL; + handle->nr_options++; + option->data = data; + for (i = 0; i < count; i++) { + if (vector[i].iov_base && vector[i].iov_len) { + memcpy(data, vector[i].iov_base, vector[i].iov_len); + data += vector[i].iov_len; + } } - - /* Some IDs (like TRACECMD_OPTION_TRACECLOCK) pass 0 / NULL data */ - if (size) - memcpy(option->data, data, size); + option->size = size; + option->id = id; list_add_tail(&option->list, &handle->options); return option; } +/** + * tracecmd_add_option - add options to the file + * @handle: the output file handle name + * @id: the id of the option + * @size: the size of the option data + * @data: the data to write to the file. + * + * Returns handle to update option if needed. + * Just the content can be updated, with smaller or equal to + * content than the specified size. + */ +struct tracecmd_option * +tracecmd_add_option(struct tracecmd_output *handle, + unsigned short id, int size, const void *data) +{ + struct iovec vect; + + vect.iov_base = data; + vect.iov_len = size; + return tracecmd_add_option_v(handle, id, &vect, 1); +} + int tracecmd_write_cpus(struct tracecmd_output *handle, int cpus) { cpus = convert_endian_4(handle, cpus); @@ -979,6 +1012,56 @@ int tracecmd_write_options(struct tracecmd_output *handle) return 0; } +int tracecmd_append_options(struct tracecmd_output *handle) +{ + struct tracecmd_option *options; + unsigned short option; + unsigned short endian2; + unsigned int endian4; + off_t offset; + int r; + + /* If already written, ignore */ + if (handle->options_written) + return 0; + + if (lseek64(handle->fd, 0, SEEK_END) == (off_t)-1) + return -1; + offset = lseek64(handle->fd, -2, SEEK_CUR); + if (offset == (off_t)-1) + return -1; + + r = pread(handle->fd, &option, 2, offset); + if (r != 2 || option != TRACECMD_OPTION_DONE) + return -1; + + list_for_each_entry(options, &handle->options, list) { + endian2 = convert_endian_2(handle, options->id); + if (do_write_check(handle, &endian2, 2)) + return -1; + + endian4 = convert_endian_4(handle, options->size); + if (do_write_check(handle, &endian4, 4)) + return -1; + + /* Save the data location in case it needs to be updated */ + options->offset = lseek64(handle->fd, 0, SEEK_CUR); + + if (do_write_check(handle, options->data, + options->size)) + return -1; + } + + option = TRACECMD_OPTION_DONE; + + if (do_write_check(handle, &option, 2)) + return -1; + + handle->options_written = 1; + + return 0; +} + int tracecmd_update_option(struct tracecmd_output *handle, struct tracecmd_option *option, int size, const void *data) From patchwork Fri Mar 8 13:44:22 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tzvetomir Stoyanov X-Patchwork-Id: 10844885 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 105A61390 for ; Fri, 8 Mar 2019 13:44:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EFDC62DF4F for ; Fri, 8 Mar 2019 13:44:35 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E4BF12DF5B; Fri, 8 Mar 2019 13:44:35 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5EE972DF4F for ; Fri, 8 Mar 2019 13:44:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726737AbfCHNof (ORCPT ); Fri, 8 Mar 2019 08:44:35 -0500 Received: from mail-wm1-f65.google.com ([209.85.128.65]:50585 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726613AbfCHNoe (ORCPT ); Fri, 8 Mar 2019 08:44:34 -0500 Received: by mail-wm1-f65.google.com with SMTP id x7so12609801wmj.0 for ; Fri, 08 Mar 2019 05:44:33 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=gacbcF6cWlnSIZ3FEjiMO8yBwpK1r7Ava8qBIF1rGe4=; b=UHLbKwBFzFNidZdbTPbrWTBFgu6sY5+AOkFjXPGdiSBYE5CTL9ipJkaMK+7ff1rz9k aq2I8Yr1epuHVVi94tRuRaaCheNjrBcSOOnric6DwWtKWtvXxv8M1R5MEmwgj8jjbjee domG1n+N6LrksxgLgPiF5X/u/h24KIDBVJbm9Yauagz2jYblZoBL9/tKLUNyXOcVgH1H ig1x7F/+xmW0De2Dec/lieect/8yOCTWKt9UxFv760kPUPJaVkTvxd4YuBV7WMCIXsw0 a8Cn+DXqqRhyWgpQGGyGFoDDXTJiniudvx+SBxpz6MoZMBG9pYxA9y3kMERysgjVw3Sp r4PA== X-Gm-Message-State: APjAAAUt8ZWHIbx4i50QojaF7nTZrX7ztMg+5JPTjU5UUs67MsF0J08d QIBYFftLUjN16dpBazTHrSuiS6ar X-Google-Smtp-Source: APXvYqwG5U+RWvvK04QP+fTF/jnI87lSZzPwow+r4GNiX/zk5PFbsKWsl8G+jy3zcd5WKm33Ma8bww== X-Received: by 2002:a1c:6555:: with SMTP id z82mr8872254wmb.125.1552052672690; Fri, 08 Mar 2019 05:44:32 -0800 (PST) Received: from oberon.eng.vmware.com ([146.247.46.5]) by smtp.gmail.com with ESMTPSA id w4sm11631872wmg.8.2019.03.08.05.44.32 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 08 Mar 2019 05:44:32 -0800 (PST) From: Tzvetomir Stoyanov To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v5 8/9] trace-cmd: Implemented new option in trace.dat file: TRACECMD_OPTION_TIME_SHIFT Date: Fri, 8 Mar 2019 15:44:22 +0200 Message-Id: <20190308134423.22008-9-tstoyanov@vmware.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190308134423.22008-1-tstoyanov@vmware.com> References: <20190308134423.22008-1-tstoyanov@vmware.com> MIME-Version: 1.0 Sender: linux-trace-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The TRACECMD_OPTION_TIME_SHIFT is used when synchronizing trace time stamps between two trace.dat files. It contains multiple long long (time, offset) pairs, describing time stamps _offset_, measured in the given local _time_. The content of the option buffer is: first 4 bytes - integer, count of timestamp offsets long long array of size _count_, local time in which the offset is measured long long array of size _count_, offset of the time stamps Signed-off-by: Tzvetomir Stoyanov --- include/trace-cmd/trace-cmd.h | 1 + lib/trace-cmd/trace-input.c | 119 +++++++++++++++++++++++++++++++++- 2 files changed, 118 insertions(+), 2 deletions(-) diff --git a/include/trace-cmd/trace-cmd.h b/include/trace-cmd/trace-cmd.h index 023b701..cea6bb9 100644 --- a/include/trace-cmd/trace-cmd.h +++ b/include/trace-cmd/trace-cmd.h @@ -82,6 +82,7 @@ enum { TRACECMD_OPTION_HOOK, TRACECMD_OPTION_OFFSET, TRACECMD_OPTION_CPUCOUNT, + TRACECMD_OPTION_TIME_SHIFT, }; enum { diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index 0a6e820..bfda4c2 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -75,6 +75,11 @@ struct input_buffer_instance { size_t offset; }; +struct ts_offset_corr { + long long time; + long long ofsset; +}; + struct tracecmd_input { struct tep_handle *pevent; struct tep_plugin_list *plugin_list; @@ -92,6 +97,8 @@ struct tracecmd_input { bool use_pipe; struct cpu_data *cpu_data; long long ts_offset; + int ts_corr_count; + struct ts_offset_corr *ts_corr; double ts2secs; char * cpustats; char * uname; @@ -1028,6 +1035,68 @@ static void free_next(struct tracecmd_input *handle, int cpu) free_record(record); } +static inline unsigned long long +timestamp_correction_calc(unsigned long long ts, struct ts_offset_corr *min, + struct ts_offset_corr *max) +{ + long long tscor = min->ofsset + + (((((long long)ts) - min->time)* + (max->ofsset-min->ofsset))/(max->time-min->time)); + + if (tscor < 0) + return ts - llabs(tscor); + + return ts + tscor; + +} + +static unsigned long long timestamp_correct(unsigned long long ts, + struct tracecmd_input *handle) +{ + static int min = -1; + static int max = -1; + int i; + + if (handle->ts_offset) + return ts + handle->ts_offset; + if (!handle->ts_corr_count || !handle->ts_corr) + return ts; + + /* We have only one offset, nothing to calc here */ + if (handle->ts_corr_count == 1) + return ts + handle->ts_corr[0].ofsset; + + /* We have two offsets, nothing to search here */ + if (handle->ts_corr_count == 2) + return timestamp_correction_calc(ts, &handle->ts_corr[0], + &handle->ts_corr[1]); + + /* We have more than two offsets */ + /* check if current ts fits in the last found time frame */ + if (min >= 0 && max >= 0 && + ts >= handle->ts_corr[min].time && + ts <= handle->ts_corr[max].time) { + return timestamp_correction_calc(ts, &handle->ts_corr[min], + &handle->ts_corr[max]); + } + min = -1; + max = -1; + for (i = 0; i < handle->ts_corr_count; i++) { + if (ts > handle->ts_corr[i].time) + min = i; + else if (ts <= handle->ts_corr[i].time) { + max = i; + break; + } + } + if (min >= 0 && max >= 0 && min != max && + min < handle->ts_corr_count && max < handle->ts_corr_count) + return timestamp_correction_calc(ts, &handle->ts_corr[min], + &handle->ts_corr[max]); + + return ts; +} + /* * Page is mapped, now read in the page header info. */ @@ -1049,7 +1118,7 @@ static int update_page_info(struct tracecmd_input *handle, int cpu) kbuffer_subbuffer_size(kbuf)); return -1; } - handle->cpu_data[cpu].timestamp = kbuffer_timestamp(kbuf) + handle->ts_offset; + handle->cpu_data[cpu].timestamp = timestamp_correct(kbuffer_timestamp(kbuf), handle); if (handle->ts2secs) handle->cpu_data[cpu].timestamp *= handle->ts2secs; @@ -1776,7 +1845,7 @@ read_again: goto read_again; } - handle->cpu_data[cpu].timestamp = ts + handle->ts_offset; + handle->cpu_data[cpu].timestamp = timestamp_correct(ts, handle); if (handle->ts2secs) { handle->cpu_data[cpu].timestamp *= handle->ts2secs; @@ -2101,6 +2170,34 @@ void tracecmd_set_ts2secs(struct tracecmd_input *handle, handle->use_trace_clock = false; } +static int tsync_offset_cmp(const void *a, const void *b) +{ + struct ts_offset_corr *ts_a = (struct ts_offset_corr *)a; + struct ts_offset_corr *ts_b = (struct ts_offset_corr *)b; + + if (ts_a->time > ts_b->time) + return 1; + if (ts_a->time < ts_b->time) + return -1; + return 0; +} + +static void tsync_offset_load(struct tracecmd_input *handle, char *buf) +{ + int i; + long long *buf8 = (long long *)buf; + + for (i = 0; i < handle->ts_corr_count; i++) { + handle->ts_corr[i].time = tep_read_number(handle->pevent, + buf8+i, 8); + handle->ts_corr[i].ofsset = tep_read_number(handle->pevent, + buf8+handle->ts_corr_count+i, 8); + } + qsort(handle->ts_corr, + handle->ts_corr_count, sizeof(struct ts_offset_corr), + tsync_offset_cmp); +} + static int handle_options(struct tracecmd_input *handle) { long long offset; @@ -2111,6 +2208,7 @@ static int handle_options(struct tracecmd_input *handle) struct input_buffer_instance *buffer; struct hook_list *hook; char *buf; + int tsync; int cpus; for (;;) { @@ -2155,6 +2253,23 @@ static int handle_options(struct tracecmd_input *handle) offset = strtoll(buf, NULL, 0); handle->ts_offset += offset; break; + case TRACECMD_OPTION_TIME_SHIFT: + /* + * int (4 bytes) count of timestamp offsets. + * long long array of size [count] of times, + * when the offsets were calculated. + * long long array of size [count] of timestamp offsets. + */ + handle->ts_corr_count = tep_read_number(handle->pevent, + buf, 4); + tsync = (sizeof(long long)*handle->ts_corr_count); + if (size != (4+(2*tsync))) + break; + handle->ts_corr = malloc(2*tsync); + if (!handle->ts_corr) + return -ENOMEM; + tsync_offset_load(handle, buf+4); + break; case TRACECMD_OPTION_CPUSTAT: buf[size-1] = '\n'; cpustats = realloc(cpustats, cpustats_size + size + 1); From patchwork Fri Mar 8 13:44:23 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tzvetomir Stoyanov X-Patchwork-Id: 10844887 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 83AC117E9 for ; Fri, 8 Mar 2019 13:44:40 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 67C2B2DF4F for ; Fri, 8 Mar 2019 13:44:40 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5C1AF2DF76; Fri, 8 Mar 2019 13:44:40 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6885E2DF4F for ; Fri, 8 Mar 2019 13:44:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726613AbfCHNoi (ORCPT ); Fri, 8 Mar 2019 08:44:38 -0500 Received: from mail-wr1-f66.google.com ([209.85.221.66]:41404 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726706AbfCHNoi (ORCPT ); Fri, 8 Mar 2019 08:44:38 -0500 Received: by mail-wr1-f66.google.com with SMTP id n2so21416393wrw.8 for ; Fri, 08 Mar 2019 05:44:34 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=9J0pLuIwycnbheXqWs2CljoyxQT/CDliz18qPVqA9Ho=; b=PNQXhH7uQFg+T24/7Vv44FTf0D5EOw8YUFsT6NrWDEdl2pS3OQzjAAkTemMfyRwH38 X+Ek4J74XKrzb2dzQNcJ1n9RcD8cPechfLHpDMOAtCw+pOomTJm8rUj/u6oohpMO4dgO sd/BuvKc72PwcPFnA58SMSUKkY53DRvVfq19PjP78h3mfdf0RBT/7lYVZM05XvU8/upj fXZ/OwKvhvAQ3qo7Ql+NUAZn5rG35Y0I9FxDHbQT2GSXqzwZ8T6R1dqpUQ+taiPlky5O dBV/qv7YmpYaOxz+CBi3bkq9JLVGAJOy+N+OaSAqxxJRpfRPwQX1P3yaz7lxDdzddviv SjkQ== X-Gm-Message-State: APjAAAUdQHhgyOzA+wI03pR61JmtSsjGcJ0/rCuOy7Pp498KWSGPoH78 U33704G2omt2iwkz2yh9qoM98lRc X-Google-Smtp-Source: APXvYqzurkldzqf1ZwFmagtsfYZXtT4gXoSp2T7FR5XJT5J+xOGH14FxCfqm/AX48wTALTY13nI+YA== X-Received: by 2002:adf:b784:: with SMTP id s4mr10922231wre.155.1552052673606; Fri, 08 Mar 2019 05:44:33 -0800 (PST) Received: from oberon.eng.vmware.com ([146.247.46.5]) by smtp.gmail.com with ESMTPSA id w4sm11631872wmg.8.2019.03.08.05.44.32 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 08 Mar 2019 05:44:33 -0800 (PST) From: Tzvetomir Stoyanov To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v5 9/9] trace-cmd [POC]: Implemented timestamps synch PTP-like algorithm, using vsock events. Date: Fri, 8 Mar 2019 15:44:23 +0200 Message-Id: <20190308134423.22008-10-tstoyanov@vmware.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190308134423.22008-1-tstoyanov@vmware.com> References: <20190308134423.22008-1-tstoyanov@vmware.com> MIME-Version: 1.0 Sender: linux-trace-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This is a POC patch, implementing a PTP-like algorithm for syncing timestamps between host and guest machines, using vsock trace events to catch the host / guest time. Signed-off-by: Tzvetomir Stoyanov --- include/trace-cmd/trace-cmd.h | 9 + tracecmd/Makefile | 1 + tracecmd/include/trace-local.h | 50 +++ tracecmd/trace-msg.c | 284 ++++++++++++++- tracecmd/trace-record.c | 72 +++- tracecmd/trace-timesync.c | 622 +++++++++++++++++++++++++++++++++ 6 files changed, 1024 insertions(+), 14 deletions(-) create mode 100644 tracecmd/trace-timesync.c diff --git a/include/trace-cmd/trace-cmd.h b/include/trace-cmd/trace-cmd.h index cea6bb9..dbfbe14 100644 --- a/include/trace-cmd/trace-cmd.h +++ b/include/trace-cmd/trace-cmd.h @@ -352,6 +352,15 @@ int tracecmd_msg_recv_trace_resp(struct tracecmd_msg_handle *msg_handle, int *nr_cpus, int *page_size, unsigned int **ports, bool *use_fifos); +struct tracecmd_clock_sync; + +int tracecmd_msg_rcv_time_sync(struct tracecmd_msg_handle *msg_handle, + struct tracecmd_clock_sync *clock_sync, + long long *toffset, long long *ts_offset); +int tracecmd_msg_snd_time_sync(struct tracecmd_msg_handle *msg_handle, + struct tracecmd_clock_sync *clock_sync, + long long *toffset, long long *ts_offset); + /* --- Plugin handling --- */ extern struct tep_plugin_option trace_ftrace_options[]; diff --git a/tracecmd/Makefile b/tracecmd/Makefile index d3e3080..8a73bf7 100644 --- a/tracecmd/Makefile +++ b/tracecmd/Makefile @@ -32,6 +32,7 @@ TRACE_CMD_OBJS += trace-list.o TRACE_CMD_OBJS += trace-output.o TRACE_CMD_OBJS += trace-usage.o TRACE_CMD_OBJS += trace-msg.o +TRACE_CMD_OBJS += trace-timesync.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 62f5e47..ba0b06d 100644 --- a/tracecmd/include/trace-local.h +++ b/tracecmd/include/trace-local.h @@ -214,6 +214,11 @@ struct buffer_instance { int port; int *fds; bool use_fifos; + + struct tracecmd_clock_sync *clock_sync; + int time_sync_count; + long long *time_sync_ts; + long long *time_sync_ofssets; }; extern struct buffer_instance top_instance; @@ -227,6 +232,37 @@ extern struct buffer_instance *first_instance; #define is_agent(instance) ((instance)->flags & BUFFER_FL_AGENT) #define is_guest(instance) ((instance)->flags & BUFFER_FL_GUEST) +struct tracecmd_ftrace_param { + char *file; + char *set; + char *reset; +}; + +struct tracecmd_event_hit { + int id; + int cpu; + int pid; + unsigned long long ts; +}; + +enum clock_sync_conext { + CLOCK_KVM_X86_VSOCK_HOST, + CLOCK_KVM_X86_VSOCK_GUEST, + CLOCK_CONTEXT_MAX, +}; + +struct tracecmd_clock_sync { + enum clock_sync_conext clock_context_id; + struct tracecmd_ftrace_param *ftrace_params; + struct tracecmd_event_hit *events; + struct tep_handle *tep; + struct buffer_instance *vinst; + unsigned int local_cid; + unsigned int local_port; + unsigned int remote_cid; + unsigned int remote_port; +}; + struct buffer_instance *create_instance(const char *name); void add_instance(struct buffer_instance *instance, int cpu_count); char *get_instance_file(struct buffer_instance *instance, const char *file); @@ -235,6 +271,20 @@ void update_first_instance(struct buffer_instance *instance, int topt); void show_instance_file(struct buffer_instance *instance, const char *name); int count_cpus(void); + +struct tracecmd_clock_sync* +tracecmd_clock_context_init(struct tracecmd_msg_handle *msg_handle, + const char *clock_str, + enum clock_sync_conext id); +void tracecmd_clock_context_free(struct tracecmd_clock_sync *clock_context); +int tracecmd_clock_find_event(struct tracecmd_clock_sync *clock, int cpu, + struct tracecmd_event_hit *event); + +void tracecmd_clock_synch_enable(struct tracecmd_clock_sync *clock_context); +void tracecmd_clock_synch_disable(struct tracecmd_clock_sync *clock_context); + +void sync_time_with_host_v3(struct buffer_instance *instance); +void sync_time_with_guest_v3(struct buffer_instance *instance); void write_tracing_on(struct buffer_instance *instance, int on); char *get_instance_dir(struct buffer_instance *instance); int write_instance_file(struct buffer_instance *instance, diff --git a/tracecmd/trace-msg.c b/tracecmd/trace-msg.c index 065a01e..0f1f071 100644 --- a/tracecmd/trace-msg.c +++ b/tracecmd/trace-msg.c @@ -18,16 +18,22 @@ #include #include #include +#include #include #include #include +#include #include "trace-cmd-local.h" #include "trace-local.h" #include "trace-msg.h" +typedef __u16 u16; +typedef __s16 s16; typedef __u32 u32; typedef __be32 be32; +typedef __u64 u64; +typedef __s64 s64; static inline void dprint(const char *fmt, ...) { @@ -50,6 +56,9 @@ static inline void dprint(const char *fmt, ...) unsigned int page_size; +/* Try a few times to get an accurate time sync */ +#define TSYNC_TRIES 300 + struct tracecmd_msg_opt { be32 size; be32 opt_cmd; @@ -76,6 +85,16 @@ struct tracecmd_msg_trace_resp { be32 page_size; } __attribute__((packed)); +struct tracecmd_msg_tsync_stop { + long long offset; + long long ts_offset; +} __attribute__((packed)); + +struct tracecmd_msg_tsync_pkt { + u64 tlocal; +} __attribute__((packed)); + + struct tracecmd_msg_header { be32 size; be32 cmd; @@ -83,14 +102,18 @@ struct tracecmd_msg_header { } __attribute__((packed)); #define MSG_MAP \ - C(CLOSE, 0, 0), \ - C(TINIT, 1, sizeof(struct tracecmd_msg_tinit)), \ - C(RINIT, 2, sizeof(struct tracecmd_msg_rinit)), \ - C(SEND_DATA, 3, 0), \ - C(FIN_DATA, 4, 0), \ - C(NOT_SUPP, 5, 0), \ - C(TRACE_REQ, 6, sizeof(struct tracecmd_msg_trace_req)), \ - C(TRACE_RESP, 7, sizeof(struct tracecmd_msg_trace_resp)), + C(CLOSE, 0, 0), \ + C(TINIT, 1, sizeof(struct tracecmd_msg_tinit)), \ + C(RINIT, 2, sizeof(struct tracecmd_msg_rinit)), \ + C(SEND_DATA, 3, 0), \ + C(FIN_DATA, 4, 0), \ + C(NOT_SUPP, 5, 0), \ + C(TRACE_REQ, 6, sizeof(struct tracecmd_msg_trace_req)), \ + C(TRACE_RESP, 7, sizeof(struct tracecmd_msg_trace_resp)),\ + C(TSYNC_START, 8, 0), \ + C(TSYNC_STOP, 9, sizeof(struct tracecmd_msg_tsync_stop)),\ + C(TSYNC_REQ, 10, sizeof(struct tracecmd_msg_tsync_pkt)), \ + C(TSYNC_RESP, 11, sizeof(struct tracecmd_msg_tsync_pkt)), #undef C #define C(a,b,c) MSG_##a = b @@ -120,10 +143,13 @@ static const char *cmd_to_name(int cmd) struct tracecmd_msg { struct tracecmd_msg_header hdr; union { - struct tracecmd_msg_tinit tinit; - struct tracecmd_msg_rinit rinit; - struct tracecmd_msg_trace_req trace_req; - struct tracecmd_msg_trace_resp trace_resp; + struct tracecmd_msg_tinit tinit; + struct tracecmd_msg_rinit rinit; + struct tracecmd_msg_trace_req trace_req; + struct tracecmd_msg_trace_resp trace_resp; + struct tracecmd_msg_tsync_stop ts_stop; + struct tracecmd_msg_tsync_pkt ts_req; + struct tracecmd_msg_tsync_pkt ts_resp; }; union { struct tracecmd_msg_opt *opt; @@ -859,6 +885,240 @@ out: return ret; } +int tracecmd_msg_rcv_time_sync(struct tracecmd_msg_handle *msg_handle, + struct tracecmd_clock_sync *clock_context, + long long *toffset, long long *ts_offset) +{ + struct tracecmd_event_hit event; + struct tracecmd_msg msg; + int cpu_pid, ret; + + if (clock_context == NULL || msg_handle == NULL) + return 0; + + if (toffset) + *toffset = 0; + + ret = tracecmd_msg_recv(msg_handle->fd, &msg); + if (ret < 0 || ntohl(msg.hdr.cmd) == MSG_TSYNC_STOP) + return 0; + if (ntohl(msg.hdr.cmd) != MSG_TSYNC_START) { + tracecmd_msg_init(MSG_NOT_SUPP, &msg); + tracecmd_msg_send(msg_handle->fd, &msg); + return 0; + } + + tracecmd_msg_init(MSG_TSYNC_START, &msg); + tracecmd_msg_send(msg_handle->fd, &msg); + tracecmd_clock_synch_enable(clock_context); + + do { + memset(&event, 0, sizeof(event)); + ret = tracecmd_msg_recv(msg_handle->fd, &msg); + if (ret < 0 || ntohl(msg.hdr.cmd) == MSG_TSYNC_STOP) + break; + if (ntohl(msg.hdr.cmd) != MSG_TSYNC_REQ) { + tracecmd_msg_init(MSG_NOT_SUPP, &msg); + tracecmd_msg_send(msg_handle->fd, &msg); + break; + } + ret = tracecmd_msg_recv(msg_handle->fd, &msg); + if (ret < 0 || ntohl(msg.hdr.cmd) == MSG_TSYNC_STOP) + break; + if (ntohl(msg.hdr.cmd) != MSG_TSYNC_REQ) { + tracecmd_msg_init(MSG_NOT_SUPP, &msg); + tracecmd_msg_send(msg_handle->fd, &msg); + break; + } + /* Get kvm_exit events related to the corresponding VCPU */ + cpu_pid = get_guest_vcpu_pid(clock_context->remote_cid, + ntohll(msg.ts_req.tlocal)); + tracecmd_clock_find_event(clock_context, cpu_pid, &event); + tracecmd_msg_init(MSG_TSYNC_RESP, &msg); + msg.ts_resp.tlocal = htonll(event.ts); + tracecmd_msg_send(msg_handle->fd, &msg); + } while (true); + tracecmd_clock_synch_disable(clock_context); + + if (ret >= 0 && ntohl(msg.hdr.cmd) == MSG_TSYNC_STOP) { + if (toffset) + *toffset = ntohll(msg.ts_stop.offset); + if (ts_offset) + *ts_offset = ntohll(msg.ts_stop.ts_offset); + } + + msg_free(&msg); + return 0; +} + +//#define TSYNC_DEBUG +int tracecmd_msg_snd_time_sync(struct tracecmd_msg_handle *msg_handle, + struct tracecmd_clock_sync *clock_context, + long long *toffset, long long *ts_offset) +{ + struct tracecmd_event_hit event; + struct tracecmd_msg msg_resp; + struct tracecmd_msg msg_req; + int sync_loop = TSYNC_TRIES; + long long min = 0, max = 0; + long long offset_av = 0; + long long ts_local = 0; + int k = 0, n, ret = 0; + long long tresch = 0; + long long offset = 0; + long long *ts_locals; + long long *offsets; + long long m_t1 = 0; + long long s_t2 = 0; + int probes = 0; +#ifdef TSYNC_DEBUG +/* Write all ts in a file, used to analyze the raw data */ + struct timespec tsStart, tsEnd; + int zm = 0, zs = 0; + long long duration; + char buff[256]; + int iFd; +#endif + + if (clock_context == NULL || msg_handle == NULL) + return 0; + + if (toffset == NULL) { + /* No time sync needed, inform the client */ + msg_req.ts_stop.offset = 0; + msg_req.ts_stop.ts_offset = 0; + tracecmd_msg_init(MSG_TSYNC_STOP, &msg_req); + tracecmd_msg_send(msg_handle->fd, &msg_req); + return 0; + } + + *toffset = 0; + offsets = calloc(sizeof(long long), TSYNC_TRIES); + ts_locals = calloc(sizeof(long long), TSYNC_TRIES); + if (!offsets || !ts_locals) { + free(offsets); + free(ts_locals); + return 0; + } + + tracecmd_msg_init(MSG_TSYNC_START, &msg_req); + tracecmd_msg_send(msg_handle->fd, &msg_req); + ret = tracecmd_msg_recv(msg_handle->fd, &msg_resp); + if (ret < 0 || ntohl(msg_resp.hdr.cmd) != MSG_TSYNC_START) { + free(offsets); + free(ts_locals); + return 0; + } + +#ifdef TSYNC_DEBUG + sprintf(buff, "s-cid%d.txt", clock_context->remote_cid); + iFd = open(buff, O_CREAT|O_WRONLY|O_TRUNC, 0644); + clock_gettime(CLOCK_MONOTONIC, &tsStart); +#endif + tracecmd_clock_synch_enable(clock_context); + do { + memset(&msg_resp, 0, sizeof(msg_resp)); + memset(&event, 0, sizeof(event)); + tracecmd_msg_init(MSG_TSYNC_REQ, &msg_req); + tracecmd_msg_send(msg_handle->fd, &msg_req); + /* Get the ts and CPU of the sent event */ + tracecmd_clock_find_event(clock_context, -1, &event); + + tracecmd_msg_init(MSG_TSYNC_REQ, &msg_req); + msg_req.ts_req.tlocal = event.cpu; + msg_req.ts_req.tlocal = htonll(msg_req.ts_req.tlocal); + tracecmd_msg_send(msg_handle->fd, &msg_req); + ret = tracecmd_msg_recv(msg_handle->fd, &msg_resp); + if (ret < 0) + break; + if (ntohl(msg_resp.hdr.cmd) != MSG_TSYNC_RESP) { + tracecmd_msg_init(MSG_NOT_SUPP, &msg_req); + tracecmd_msg_send(msg_handle->fd, &msg_req); + break; + } + m_t1 = event.ts; + s_t2 = htonll(msg_resp.ts_resp.tlocal); /* Client rcv ts */ +#ifdef TSYNC_DEBUG + if (!s_t2) + zs++; + if (!m_t1) + zm++; +#endif + if (!s_t2 || !m_t1) + continue; + offsets[probes] = s_t2 - m_t1; + ts_locals[probes] = m_t1; + offset_av += offsets[probes]; + if (!min || min > llabs(offsets[probes])) + min = llabs(offsets[probes]); + if (!max || max < llabs(offsets[probes])) + max = llabs(offsets[probes]); + probes++; +#ifdef TSYNC_DEBUG + sprintf(buff, "%lld %lld\n", m_t1, s_t2); + write(iFd, buff, strlen(buff)); +#endif + } while (--sync_loop); + +#ifdef TSYNC_DEBUG + clock_gettime(CLOCK_MONOTONIC, &tsEnd); + close(iFd); +#endif + tracecmd_clock_synch_disable(clock_context); + + if (probes) + offset_av /= (long long)probes; + tresch = (long long)((max - min)/10); + for (n = 0; n < TSYNC_TRIES; n++) { + /* filter the offsets with deviation up to 10% */ + if (offsets[n] && + llabs(offsets[n] - offset_av) < tresch) { + offset += offsets[n]; + k++; + } + } + if (k) + offset /= (long long)k; + tresch = 0; + for (n = 0; n < TSYNC_TRIES; n++) { + if (offsets[n] && + (!tresch || tresch > llabs(offset-offsets[n]))) { + tresch = llabs(offset-offsets[n]); + ts_local = ts_locals[n]; + } + } + + tracecmd_msg_init(MSG_TSYNC_STOP, &msg_req); + msg_req.ts_stop.offset = htonll(offset); + msg_req.ts_stop.ts_offset = htonll(ts_local); + tracecmd_msg_send(msg_handle->fd, &msg_req); + + msg_free(&msg_req); + msg_free(&msg_resp); + free(offsets); + free(ts_locals); + + if (toffset) + *toffset = offset; + if (ts_offset) + *ts_offset = ts_local; + +#ifdef TSYNC_DEBUG + duration = tsEnd.tv_sec * 1000000000LL; + duration += tsEnd.tv_nsec; + duration -= (tsStart.tv_sec * 1000000000LL); + duration -= tsStart.tv_nsec; + + printf("\n selected: %lld (in %lld ns), used %s clock, %d probes\n\r", + offset, duration, + clock_context->vinst->clock?clock_context->vinst->clock:"default", + probes); + printf("\t good probes: %d / %d, threshold %lld ns, Zm %d, Zs %d\n\r", + k, TSYNC_TRIES, tresch, zm, zs); +#endif + return 0; +} + static int make_trace_resp(struct tracecmd_msg *msg, int page_size, int nr_cpus, unsigned int *ports, bool use_fifos) { diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c index d9e9c69..f07eb4d 100644 --- a/tracecmd/trace-record.c +++ b/tracecmd/trace-record.c @@ -210,6 +210,8 @@ struct common_record_context { char *date2ts; char *max_graph_depth; int data_flags; + int time_shift_count; + struct tracecmd_option *time_shift_option; int record_all; int total_disable; @@ -649,11 +651,20 @@ static void tell_guests_to_stop(void) for_all_instances(instance) { if (is_guest(instance)) { tracecmd_msg_send_close_msg(instance->msg_handle); - tracecmd_msg_handle_close(instance->msg_handle); } } } +static void close_guests_handle(void) +{ + struct buffer_instance *instance; + + for_all_instances(instance) { + if (is_guest(instance)) + tracecmd_msg_handle_close(instance->msg_handle); + } +} + static void stop_threads(enum trace_type type) { struct timeval tv = { 0, 0 }; @@ -3417,6 +3428,8 @@ static void connect_to_agent(struct buffer_instance *instance) /* the msg_handle now points to the guest fd */ instance->msg_handle = msg_handle; + + sync_time_with_guest_v3(instance); } static void setup_guest(struct buffer_instance *instance) @@ -3441,10 +3454,13 @@ static void setup_guest(struct buffer_instance *instance) close(fd); } -static void setup_agent(struct buffer_instance *instance, struct common_record_context *ctx) +static void setup_agent(struct buffer_instance *instance, + struct common_record_context *ctx) { struct tracecmd_output *network_handle; + sync_time_with_host_v3(instance); + network_handle = tracecmd_create_init_fd_msg(instance->msg_handle, listed_events); add_options(network_handle, ctx); @@ -5619,6 +5635,42 @@ static bool has_local_instances(void) return false; } +//#define TSYNC_DEBUG +static void write_guest_time_shift(struct buffer_instance *instance) +{ + struct tracecmd_output *handle; + struct iovec vector[3]; + char *file; + int fd; + + if (!instance->time_sync_count) + return; + + file = get_guest_file(output_file, instance->name); + fd = open(file, O_RDWR); + if (fd < 0) + die("error opening %s", file); + put_temp_file(file); + handle = tracecmd_get_output_handle_fd(fd); + vector[0].iov_len = sizeof(instance->time_sync_count); + vector[0].iov_base = &instance->time_sync_count; + vector[1].iov_len = sizeof(long long) * instance->time_sync_count; + vector[1].iov_base = instance->time_sync_ts; + vector[2].iov_len = sizeof(long long) * instance->time_sync_count; + vector[2].iov_base = instance->time_sync_ofssets; + tracecmd_add_option_v(handle, TRACECMD_OPTION_TIME_SHIFT, vector, 3); + tracecmd_append_options(handle); + tracecmd_output_close(handle); +#ifdef TSYNC_DEBUG + if (instance->time_sync_count > 1) + printf("\n\rDetected %lld ns ts offset drift in %lld ns for guest %s\n\r", + instance->time_sync_ofssets[instance->time_sync_count-1] - + instance->time_sync_ofssets[0], + instance->time_sync_ts[instance->time_sync_count-1]- + instance->time_sync_ts[0], instance->name); +#endif +} + /* * This function contains common code for the following commands: * record, start, stream, profile. @@ -5738,6 +5790,22 @@ static void record_trace(int argc, char **argv, if (!latency) wait_threads(); + if (ctx->instance && is_agent(ctx->instance)) { + sync_time_with_host_v3(ctx->instance); + tracecmd_clock_context_free(ctx->instance->clock_sync); + ctx->instance->clock_sync = NULL; + } else { + for_all_instances(instance) { + if (is_guest(instance)) { + sync_time_with_guest_v3(instance); + write_guest_time_shift(instance); + tracecmd_clock_context_free(instance->clock_sync); + instance->clock_sync = NULL; + } + } + } + close_guests_handle(); + if (IS_RECORD(ctx)) { record_data(ctx); delete_thread_data(); diff --git a/tracecmd/trace-timesync.c b/tracecmd/trace-timesync.c new file mode 100644 index 0000000..f8366ed --- /dev/null +++ b/tracecmd/trace-timesync.c @@ -0,0 +1,622 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2019, VMware, Tzvetomir Stoyanov + * + */ + +#include +#include +#include +#include +#include +#include "trace-local.h" + +static int clock_sync_x86_host_init(struct tracecmd_clock_sync *clock_context); +static int clock_sync_x86_host_free(struct tracecmd_clock_sync *clock_context); +static int clock_sync_x86_host_find_events(struct tracecmd_clock_sync *clock, + int cpu, struct tracecmd_event_hit *event); +static int clock_sync_x86_guest_init(struct tracecmd_clock_sync *clock_context); +static int clock_sync_x86_guest_free(struct tracecmd_clock_sync *clock_context); +static int clock_sync_x86_guest_find_events(struct tracecmd_clock_sync *clock, + int pid, + struct tracecmd_event_hit *event); + +struct tracecmd_event_descr { + char *system; + char *name; +}; + +struct { + char *systems[3]; + struct tracecmd_ftrace_param ftrace_params[5]; + struct tracecmd_event_descr events[3]; + int (*clock_sync_init)(struct tracecmd_clock_sync *clock_context); + int (*clock_sync_free)(struct tracecmd_clock_sync *clock_context); + int (*clock_sync_find_events)(struct tracecmd_clock_sync *clock_context, + int pid, + struct tracecmd_event_hit *event); +} static clock_sync[CLOCK_CONTEXT_MAX] = { + { /* CLOCK_KVM_X86_VSOCK_HOST */ + .systems = {"vsock", "ftrace", NULL}, + .ftrace_params = { + {"set_ftrace_filter", "vmx_read_l1_tsc_offset\nsvm_read_l1_tsc_offset", "\0"}, + {"current_tracer", "function", "nop"}, + {"events/vsock/virtio_transport_recv_pkt/enable", "1", "0"}, + {"events/vsock/virtio_transport_recv_pkt/filter", NULL, "\0"}, + {NULL, NULL, NULL} }, + .events = { + {.system = "ftrace", .name = "function"}, + {.system = "vsock", .name = "virtio_transport_recv_pkt"}, + {.system = NULL, .name = NULL} }, + clock_sync_x86_host_init, + clock_sync_x86_host_free, + clock_sync_x86_host_find_events, + }, + + { /* CLOCK_KVM_X86_VSOCK_GUEST */ + .systems = { "vsock", "ftrace", NULL}, + .ftrace_params = { + {"set_ftrace_filter", "vp_notify", "\0"}, + {"current_tracer", "function", "nop"}, + {"events/vsock/virtio_transport_alloc_pkt/enable", "1", "0"}, + {"events/vsock/virtio_transport_alloc_pkt/filter", NULL, "\0"}, + {NULL, NULL, NULL}, + }, + .events = { + {.system = "vsock", .name = "virtio_transport_alloc_pkt"}, + {.system = "ftrace", .name = "function"}, + {.system = NULL, .name = NULL} + }, + clock_sync_x86_guest_init, + clock_sync_x86_guest_free, + clock_sync_x86_guest_find_events, + } +}; + +static int clock_sync_x86_host_init(struct tracecmd_clock_sync *clock_context) +{ + char vsock_filter[255]; + + snprintf(vsock_filter, 255, + "src_cid==%u && src_port==%u && dst_cid==%u && dst_port==%u && len!=0", + clock_context->remote_cid, clock_context->remote_port, + clock_context->local_cid, clock_context->local_port); + + clock_context->ftrace_params[3].set = strdup(vsock_filter); + return 1; +} + +static int clock_sync_x86_host_free(struct tracecmd_clock_sync *clock_context) +{ + free(clock_context->ftrace_params[3].set); + clock_context->ftrace_params[3].set = NULL; + return 1; +} + +static int clock_sync_x86_guest_init(struct tracecmd_clock_sync *clock_context) +{ + char vsock_filter[255]; + + snprintf(vsock_filter, 255, + "src_cid==%u && src_port==%u && dst_cid==%u && dst_port==%u && len!=0", + clock_context->local_cid, clock_context->local_port, + clock_context->remote_cid, clock_context->remote_port); + + clock_context->ftrace_params[3].set = strdup(vsock_filter); + return 1; +} + +static int clock_sync_x86_guest_free(struct tracecmd_clock_sync *clock_context) +{ + free(clock_context->ftrace_params[3].set); + clock_context->ftrace_params[3].set = NULL; + return 1; +} + +#define EVENTS_CHUNK 10 +static int +get_events_in_page(struct tep_handle *tep, void *page, + int size, int cpu, struct tracecmd_event_hit **events, + int *events_count, int *events_size) +{ + struct tracecmd_event_hit *events_array = NULL; + struct tep_record *last_record = NULL; + struct tep_event *event = NULL; + struct tep_record *record; + int id, cnt = 0; + + if (size <= 0) + return 0; + + if (*events == NULL) { + *events = malloc(EVENTS_CHUNK*sizeof(struct tracecmd_event_hit)); + *events_size = EVENTS_CHUNK; + *events_count = 0; + } + + while (true) { + event = NULL; + record = tracecmd_read_page_record(tep, page, size, + last_record); + if (!record) + break; + free_record(last_record); + id = tep_data_type(tep, record); + event = tep_data_event_from_type(tep, id); + if (event) { + if (*events_count >= *events_size) { + events_array = realloc(*events, + (*events_size + EVENTS_CHUNK)* + sizeof(struct tracecmd_event_hit)); + if (events_array) { + *events = events_array; + (*events_size) += EVENTS_CHUNK; + } + } + + if (*events_count < *events_size) { + (*events)[*events_count].ts = record->ts; + (*events)[*events_count].cpu = cpu; + (*events)[*events_count].id = id; + (*events)[*events_count].pid = tep_data_pid(tep, record); + (*events_count)++; + cnt++; + } + } + last_record = record; + } + free_record(last_record); + + return cnt; +} + +static int sync_events_cmp(const void *a, const void *b) +{ + const struct tracecmd_event_hit *ea = (const struct tracecmd_event_hit *)a; + const struct tracecmd_event_hit *eb = (const struct tracecmd_event_hit *)b; + + if (ea->ts > eb->ts) + return 1; + if (ea->ts < eb->ts) + return -1; + return 0; +} + +static int find_sync_events(struct tep_handle *pevent, + struct tracecmd_event_hit *recorded, + int rsize, struct tracecmd_event_hit *events) +{ + int i = 0, j = 0; + + while (i < rsize) { + if (!events[j].ts && events[j].id == recorded[i].id && + (events[j].pid < 0 || events[j].pid == recorded[i].pid)) { + events[j].cpu = recorded[i].cpu; + events[j].ts = recorded[i].ts; + j++; + } else if (j > 0 && events[j-1].id == recorded[i].id && + (events[j-1].pid < 0 || events[j-1].pid == recorded[i].pid)) { + events[j-1].cpu = recorded[i].cpu; + events[j-1].ts = recorded[i].ts; + } + i++; + } + return j; +} + +//#define TSYNC_RBUFFER_DEBUG +static int find_raw_events(struct tep_handle *tep, + struct buffer_instance *instance, + struct tracecmd_event_hit *events) +{ + struct tracecmd_event_hit *events_array = NULL; + int events_count = 0; + int events_size = 0; + struct dirent *dent; + unsigned int p_size; + int ts = 0; + void *page; + char *path; + char *file; + DIR *dir; + int cpu; + int len; + int fd; + int r; + + p_size = getpagesize(); +#ifdef TSYNC_RBUFFER_DEBUG + file = get_instance_file(instance, "trace"); + if (!file) + return ts; + { + char *buf = NULL; + FILE *fp; + size_t n; + int r; + + printf("Events:\n\r"); + fp = fopen(file, "r"); + while ((r = getline(&buf, &n, fp)) >= 0) { + + if (buf[0] != '#') + printf("%s", buf); + free(buf); + buf = NULL; + } + fclose(fp); + } + tracecmd_put_tracing_file(file); +#endif /* TSYNC_RBUFFER_DEBUG */ + path = get_instance_file(instance, "per_cpu"); + if (!path) + return ts; + + dir = opendir(path); + if (!dir) + goto out; + + len = strlen(path); + file = malloc(len + strlen("trace_pipe_raw") + 32); + page = malloc(p_size); + if (!file || !page) + die("Failed to allocate time_stamp info"); + + while ((dent = readdir(dir))) { + + const char *name = dent->d_name; + + if (strncmp(name, "cpu", 3) != 0) + continue; + cpu = atoi(&name[3]); + sprintf(file, "%s/%s/trace_pipe_raw", path, name); + fd = open(file, O_RDONLY | O_NONBLOCK); + if (fd < 0) + continue; + do { + r = read(fd, page, p_size); + if (r > 0) { + get_events_in_page(tep, page, r, cpu, + &events_array, &events_count, + &events_size); + } + } while (r > 0); + close(fd); + } + qsort(events_array, events_count, sizeof(*events_array), sync_events_cmp); + r = find_sync_events(tep, events_array, events_count, events); +#ifdef TSYNC_RBUFFER_DEBUG + len = 0; + while (events[len].id) { + printf("Found %d @ cpu %d: %lld pid %d\n\r", + events[len].id, events[len].cpu, + events[len].ts, events[len].pid); + len++; + } +#endif + + free(events_array); + free(file); + free(page); + closedir(dir); + + out: + tracecmd_put_tracing_file(path); + return r; +} + +static int clock_sync_x86_host_find_events(struct tracecmd_clock_sync *clock, + int pid, + struct tracecmd_event_hit *event) +{ + int ret; + + clock->events[0].pid = pid; + ret = find_raw_events(clock->tep, clock->vinst, clock->events); + event->ts = clock->events[0].ts; + event->cpu = clock->events[0].cpu; + return ret; + +} + +static int clock_sync_x86_guest_find_events(struct tracecmd_clock_sync *clock, + int pid, + struct tracecmd_event_hit *event) +{ + int ret; + + ret = find_raw_events(clock->tep, clock->vinst, clock->events); + event->ts = clock->events[1].ts; + event->cpu = clock->events[0].cpu; + return ret; + +} + +static void tracecmd_clock_sync_reset(struct tracecmd_clock_sync *clock_context) +{ + int i = 0; + + while (clock_context->events[i].id) { + clock_context->events[i].cpu = 0; + clock_context->events[i].ts = 0; + clock_context->events[i].pid = -1; + i++; + } +} + +int tracecmd_clock_find_event(struct tracecmd_clock_sync *clock, int pid, + struct tracecmd_event_hit *event) +{ + int ret = 0; + int id; + + if (clock == NULL || + clock->clock_context_id >= CLOCK_CONTEXT_MAX) + return 0; + + id = clock->clock_context_id; + if (clock_sync[id].clock_sync_find_events) + ret = clock_sync[id].clock_sync_find_events(clock, pid, event); + + tracecmd_clock_sync_reset(clock); + return ret; +} + +static void clock_context_copy(struct tracecmd_clock_sync *clock_context, + struct tracecmd_ftrace_param *params, + struct tracecmd_event_descr *events) +{ + int i; + + i = 0; + while (params[i].file) + i++; + clock_context->ftrace_params = calloc(i+1, sizeof(struct tracecmd_ftrace_param)); + i = 0; + while (params[i].file) { + clock_context->ftrace_params[i].file = strdup(params[i].file); + if (params[i].set) + clock_context->ftrace_params[i].set = strdup(params[i].set); + if (params[i].reset) + clock_context->ftrace_params[i].reset = strdup(params[i].reset); + i++; + } + + i = 0; + while (events[i].name) + i++; + clock_context->events = calloc(i+1, sizeof(struct tracecmd_event_hit)); +} + +void trace_instance_reset(struct buffer_instance *vinst) +{ + write_instance_file(vinst, "trace", "\0", NULL); +} + +static struct buffer_instance * +clock_synch_create_instance(const char *clock, unsigned int cid) +{ + struct buffer_instance *vinst; + char inst_name[256]; + + snprintf(inst_name, 256, "clock_synch-%d", cid); + + vinst = create_instance(strdup(inst_name)); + tracecmd_init_instance(vinst); + vinst->cpu_count = tracecmd_local_cpu_count(); + tracecmd_make_instance(vinst); + trace_instance_reset(vinst); + if (clock) + vinst->clock = strdup(clock); + tracecmd_set_clock(vinst); + return vinst; +} + +static struct tep_handle *clock_synch_get_tep(struct buffer_instance *instance, + char **systems) +{ + struct tep_handle *tep = NULL; + char *path; + + path = get_instance_dir(instance); + tep = tracecmd_local_events_system(path, systems); + tracecmd_put_tracing_file(path); + + tep_set_file_bigendian(tep, tracecmd_host_bigendian()); + tep_set_host_bigendian(tep, tracecmd_host_bigendian()); + + return tep; +} + +static void get_vsocket_params(int fd, unsigned int *lcid, unsigned int *lport, + unsigned int *rcid, unsigned int *rport) +{ + struct sockaddr_vm addr; + socklen_t addr_len = sizeof(addr); + + if (lcid || lport) { + memset(&addr, 0, sizeof(addr)); + if (getsockname(fd, (struct sockaddr *)&addr, &addr_len)) + return; + if (addr.svm_family != AF_VSOCK) + return; + if (lport) + *lport = addr.svm_port; + if (lcid) + *lcid = addr.svm_cid; + } + + if (rcid || rport) { + memset(&addr, 0, sizeof(addr)); + addr_len = sizeof(addr); + if (getpeername(fd, (struct sockaddr *)&addr, &addr_len)) + return; + if (addr.svm_family != AF_VSOCK) + return; + + if (rport) + *rport = addr.svm_port; + if (rcid) + *rcid = addr.svm_cid; + } +} + +struct tracecmd_clock_sync* +tracecmd_clock_context_init(struct tracecmd_msg_handle *msg_handle, + const char *clock_str, + enum clock_sync_conext id) +{ + struct tracecmd_clock_sync *clock_context; + struct tep_event *event; + unsigned int i = 0; + + switch (id) { +#ifdef VSOCK + case CLOCK_KVM_X86_VSOCK_HOST: + case CLOCK_KVM_X86_VSOCK_GUEST: + break; +#endif + default: /* not supported clock sync context */ + return NULL; + } + + if (id >= CLOCK_CONTEXT_MAX || NULL == msg_handle) + return NULL; + clock_context = calloc(1, sizeof(struct tracecmd_clock_sync)); + if (!clock_context) + return NULL; + clock_context->clock_context_id = id; + clock_context_copy(clock_context, + clock_sync[id].ftrace_params, clock_sync[id].events); + + get_vsocket_params(msg_handle->fd, + &(clock_context->local_cid), + &(clock_context->local_port), + &(clock_context->remote_cid), + &(clock_context->remote_port)); + + if (clock_sync[id].clock_sync_init) + clock_sync[id].clock_sync_init(clock_context); + + clock_context->vinst = clock_synch_create_instance(clock_str, clock_context->remote_cid); + clock_context->tep = clock_synch_get_tep(clock_context->vinst, + clock_sync[id].systems); + i = 0; + while (clock_sync[id].events[i].name) { + event = tep_find_event_by_name(clock_context->tep, + clock_sync[id].events[i].system, + clock_sync[id].events[i].name); + if (!event) + break; + clock_context->events[i].id = event->id; + i++; + } + return clock_context; + +} + +void tracecmd_clock_context_free(struct tracecmd_clock_sync *clock_context) +{ + int i; + + if (clock_context == NULL || + clock_context->clock_context_id >= CLOCK_CONTEXT_MAX) + return; + if (clock_sync[clock_context->clock_context_id].clock_sync_free) + clock_sync[clock_context->clock_context_id].clock_sync_free(clock_context); + + i = 0; + while (clock_context->ftrace_params[i].file) { + free(clock_context->ftrace_params[i].file); + free(clock_context->ftrace_params[i].set); + free(clock_context->ftrace_params[i].reset); + i++; + } + free(clock_context->ftrace_params); + free(clock_context->events); + tracecmd_remove_instance(clock_context->vinst); + /* todo: clean up the instance */ + tep_free(clock_context->tep); + free(clock_context); +} + +void sync_time_with_host_v3(struct buffer_instance *instance) +{ + long long ts_local = 0; + long long toffset = 0; + + if (instance->clock_sync == NULL) + instance->clock_sync = tracecmd_clock_context_init(instance->msg_handle, + instance->clock, CLOCK_KVM_X86_VSOCK_GUEST); + + tracecmd_msg_snd_time_sync(instance->msg_handle, instance->clock_sync, + &toffset, &ts_local); +} + +void sync_time_with_guest_v3(struct buffer_instance *instance) +{ + long long ts_local = 0; + long long toffset = 0; + long long *sync_array_ts; + long long *sync_array_ofs; + + if (instance->clock_sync == NULL) + instance->clock_sync = tracecmd_clock_context_init(instance->msg_handle, + top_instance.clock, CLOCK_KVM_X86_VSOCK_HOST); + + tracecmd_msg_rcv_time_sync(instance->msg_handle, + instance->clock_sync, &toffset, &ts_local); + + sync_array_ts = realloc(instance->time_sync_ts, + (instance->time_sync_count+1)*sizeof(long long)); + sync_array_ofs = realloc(instance->time_sync_ofssets, + (instance->time_sync_count+1)*sizeof(long long)); + + if (sync_array_ts && sync_array_ofs) { + sync_array_ts[instance->time_sync_count] = ts_local; + sync_array_ofs[instance->time_sync_count] = toffset; + instance->time_sync_count++; + instance->time_sync_ts = sync_array_ts; + instance->time_sync_ofssets = sync_array_ofs; + + } else { + free(sync_array_ts); + free(sync_array_ofs); + } +} + +static void set_clock_synch_events(struct buffer_instance *instance, + struct tracecmd_ftrace_param *params, + bool enable) +{ + int i = 0; + + if (!enable) + write_tracing_on(instance, 0); + + while (params[i].file) { + if (enable && params[i].set) { + write_instance_file(instance, params[i].file, + params[i].set, NULL); + } + if (!enable && params[i].reset) + write_instance_file(instance, params[i].file, + params[i].reset, NULL); + i++; + } + + if (enable) + write_tracing_on(instance, 1); +} + +void tracecmd_clock_synch_enable(struct tracecmd_clock_sync *clock_context) +{ + set_clock_synch_events(clock_context->vinst, + clock_context->ftrace_params, true); +} + +void tracecmd_clock_synch_disable(struct tracecmd_clock_sync *clock_context) +{ + set_clock_synch_events(clock_context->vinst, + clock_context->ftrace_params, false); +}