From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934308AbcLBKlm (ORCPT ); Fri, 2 Dec 2016 05:41:42 -0500 Received: from terminus.zytor.com ([198.137.202.10]:41882 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934264AbcLBKlj (ORCPT ); Fri, 2 Dec 2016 05:41:39 -0500 Date: Fri, 2 Dec 2016 02:41:01 -0800 From: tip-bot for David Ahern Message-ID: Cc: dsahern@gmail.com, acme@redhat.com, peterz@infradead.org, hpa@zytor.com, namhyung@kernel.org, jolsa@kernel.org, dsa@cumulusnetworks.com, mingo@kernel.org, tglx@linutronix.de, linux-kernel@vger.kernel.org Reply-To: linux-kernel@vger.kernel.org, tglx@linutronix.de, mingo@kernel.org, dsa@cumulusnetworks.com, namhyung@kernel.org, jolsa@kernel.org, peterz@infradead.org, hpa@zytor.com, acme@redhat.com, dsahern@gmail.com In-Reply-To: <1480439746-42695-2-git-send-email-dsahern@gmail.com> References: <1480439746-42695-2-git-send-email-dsahern@gmail.com> To: linux-tip-commits@vger.kernel.org Subject: [tip:perf/core] perf tools: Add time-based utility functions Git-Commit-ID: fdf9dc4b34f5f40919370c4601eccfd0db726aa5 X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit-ID: fdf9dc4b34f5f40919370c4601eccfd0db726aa5 Gitweb: http://git.kernel.org/tip/fdf9dc4b34f5f40919370c4601eccfd0db726aa5 Author: David Ahern AuthorDate: Tue, 29 Nov 2016 10:15:41 -0700 Committer: Arnaldo Carvalho de Melo CommitDate: Thu, 1 Dec 2016 13:02:32 -0300 perf tools: Add time-based utility functions Add function to parse a user time string of the form , where start and stop are time in sec.nsec format. Both start and stop times are optional. Add function to determine if a sample time is within a given time time window of interest. Signed-off-by: David Ahern Acked-by: Namhyung Kim Cc: Jiri Olsa Cc: Peter Zijlstra Link: http://lkml.kernel.org/r/1480439746-42695-2-git-send-email-dsahern@gmail.com Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/Build | 1 + tools/perf/util/time-utils.c | 85 ++++++++++++++++++++++++++++++++++++++++++++ tools/perf/util/time-utils.h | 12 +++++++ 3 files changed, 98 insertions(+) diff --git a/tools/perf/util/Build b/tools/perf/util/Build index b2a47aa..bdad82a 100644 --- a/tools/perf/util/Build +++ b/tools/perf/util/Build @@ -87,6 +87,7 @@ libperf-y += help-unknown-cmd.o libperf-y += mem-events.o libperf-y += vsprintf.o libperf-y += drv_configs.o +libperf-y += time-utils.o libperf-$(CONFIG_LIBBPF) += bpf-loader.o libperf-$(CONFIG_BPF_PROLOGUE) += bpf-prologue.o diff --git a/tools/perf/util/time-utils.c b/tools/perf/util/time-utils.c new file mode 100644 index 0000000..0443b2a --- /dev/null +++ b/tools/perf/util/time-utils.c @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include + +#include "perf.h" +#include "debug.h" +#include "time-utils.h" +#include "util.h" + +static int parse_timestr_sec_nsec(struct perf_time_interval *ptime, + char *start_str, char *end_str) +{ + if (start_str && (*start_str != '\0') && + (parse_nsec_time(start_str, &ptime->start) != 0)) { + return -1; + } + + if (end_str && (*end_str != '\0') && + (parse_nsec_time(end_str, &ptime->end) != 0)) { + return -1; + } + + return 0; +} + +int perf_time__parse_str(struct perf_time_interval *ptime, const char *ostr) +{ + char *start_str, *end_str; + char *d, *str; + int rc = 0; + + if (ostr == NULL || *ostr == '\0') + return 0; + + /* copy original string because we need to modify it */ + str = strdup(ostr); + if (str == NULL) + return -ENOMEM; + + ptime->start = 0; + ptime->end = 0; + + /* str has the format: , + * variations: , + * , + * , + */ + start_str = str; + d = strchr(start_str, ','); + if (d) { + *d = '\0'; + ++d; + } + end_str = d; + + rc = parse_timestr_sec_nsec(ptime, start_str, end_str); + + free(str); + + /* make sure end time is after start time if it was given */ + if (rc == 0 && ptime->end && ptime->end < ptime->start) + return -EINVAL; + + pr_debug("start time %" PRIu64 ", ", ptime->start); + pr_debug("end time %" PRIu64 "\n", ptime->end); + + return rc; +} + +bool perf_time__skip_sample(struct perf_time_interval *ptime, u64 timestamp) +{ + /* if time is not set don't drop sample */ + if (timestamp == 0) + return false; + + /* otherwise compare sample time to time window */ + if ((ptime->start && timestamp < ptime->start) || + (ptime->end && timestamp > ptime->end)) { + return true; + } + + return false; +} diff --git a/tools/perf/util/time-utils.h b/tools/perf/util/time-utils.h new file mode 100644 index 0000000..8f3e0e3 --- /dev/null +++ b/tools/perf/util/time-utils.h @@ -0,0 +1,12 @@ +#ifndef _TIME_UTILS_H_ +#define _TIME_UTILS_H_ + +struct perf_time_interval { + u64 start, end; +}; + +int perf_time__parse_str(struct perf_time_interval *ptime, const char *ostr); + +bool perf_time__skip_sample(struct perf_time_interval *ptime, u64 timestamp); + +#endif