perf tests: Add a test for time-utils
authorAdrian Hunter <adrian.hunter@intel.com>
Tue, 4 Jun 2019 13:00:16 +0000 (16:00 +0300)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Mon, 10 Jun 2019 19:20:12 +0000 (16:20 -0300)
Test time ranges work as expected.

Committer testing:

  $ perf test "time utils"
  59: time utils                                            : Ok
  $ perf test -v "time utils"
  59: time utils                                            :
  --- start ---
  test child forked, pid 31711

  parse_nsec_time("0")
  0

  parse_nsec_time("1")
  1000000000

  parse_nsec_time("0.000000001")
  1

  parse_nsec_time("1.000000001")
  1000000001

  parse_nsec_time("123456.123456")
  123456123456000

  parse_nsec_time("1234567.123456789")
  1234567123456789

  parse_nsec_time("18446744073.709551615")
  18446744073709551615

  perf_time__parse_str("1234567.123456789,1234567.123456789")
  start time 1234567123456789, end time 1234567123456789

  perf_time__parse_str("1234567.123456789,1234567.123456790")
  start time 1234567123456789, end time 1234567123456790

  perf_time__parse_str("1234567.123456789,")
  start time 1234567123456789, end time 0

  perf_time__parse_str(",1234567.123456789")
  start time 0, end time 1234567123456789

  perf_time__parse_str("0,1234567.123456789")
  start time 0, end time 1234567123456789

  perf_time__parse_for_ranges("1234567.123456789,1234567.123456790")
  start time 1234567123456789, end time 1234567123456790

  perf_time__parse_for_ranges("10%/1")
  first_sample_time 7654321000000000 last_sample_time 7654321000000100
  start time 0: 7654321000000000, end time 0: 7654321000000009

  perf_time__parse_for_ranges("10%/2")
  first_sample_time 7654321000000000 last_sample_time 7654321000000100
  start time 0: 7654321000000010, end time 0: 7654321000000019

  perf_time__parse_for_ranges("10%/1,10%/2")
  first_sample_time 11223344000000000 last_sample_time 11223344000000100
  start time 0: 11223344000000000, end time 0: 11223344000000009
  start time 1: 11223344000000010, end time 1: 11223344000000019

  perf_time__parse_for_ranges("10%/1,10%/3,10%/10")
  first_sample_time 11223344000000000 last_sample_time 11223344000000100
  start time 0: 11223344000000000, end time 0: 11223344000000009
  start time 1: 11223344000000020, end time 1: 11223344000000029
  start time 2: 11223344000000090, end time 2: 11223344000000100

  test child finished with 0
  ---- end ----
  time utils: Ok
  $

Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Jin Yao <yao.jin@linux.intel.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Link: http://lkml.kernel.org/r/20190604130017.31207-19-adrian.hunter@intel.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/tests/Build
tools/perf/tests/builtin-test.c
tools/perf/tests/tests.h
tools/perf/tests/time-utils-test.c [new file with mode: 0644]

index 4afb6319ed51edd28d1ebd79162070727eb40339..e3ba63cef01ed84d2476ef3a1a3d4dde0d7d57aa 100644 (file)
@@ -51,6 +51,7 @@ perf-y += clang.o
 perf-y += unit_number__scnprintf.o
 perf-y += mem2node.o
 perf-y += map_groups.o
+perf-y += time-utils-test.o
 
 $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build
        $(call rule_mkdir)
index 941c5456d625c026818d1409a82b7156ed14dec8..cd72ff0f7658a095ec367bcecdb48eee202a873c 100644 (file)
@@ -289,6 +289,10 @@ static struct test generic_tests[] = {
                .desc = "mem2node",
                .func = test__mem2node,
        },
+       {
+               .desc = "time utils",
+               .func = test__time_utils,
+       },
        {
                .desc = "map_groups__merge_in",
                .func = test__map_groups__merge_in,
index e5e3a57cd37392a27cb89b39e016cdafc4268d83..72912eb473cb14c3f563bf7750bac4085daa6725 100644 (file)
@@ -108,6 +108,7 @@ int test__clang_subtest_get_nr(void);
 int test__unit_number__scnprint(struct test *test, int subtest);
 int test__mem2node(struct test *t, int subtest);
 int test__map_groups__merge_in(struct test *t, int subtest);
+int test__time_utils(struct test *t, int subtest);
 
 bool test__bp_signal_is_supported(void);
 bool test__wp_is_supported(void);
diff --git a/tools/perf/tests/time-utils-test.c b/tools/perf/tests/time-utils-test.c
new file mode 100644 (file)
index 0000000..7504046
--- /dev/null
@@ -0,0 +1,234 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/compiler.h>
+#include <linux/time64.h>
+#include <inttypes.h>
+#include <string.h>
+#include "time-utils.h"
+#include "evlist.h"
+#include "session.h"
+#include "debug.h"
+#include "tests.h"
+
+static bool test__parse_nsec_time(const char *str, u64 expected)
+{
+       u64 ptime;
+       int err;
+
+       pr_debug("\nparse_nsec_time(\"%s\")\n", str);
+
+       err = parse_nsec_time(str, &ptime);
+       if (err) {
+               pr_debug("error %d\n", err);
+               return false;
+       }
+
+       if (ptime != expected) {
+               pr_debug("Failed. ptime %" PRIu64 " expected %" PRIu64 "\n",
+                        ptime, expected);
+               return false;
+       }
+
+       pr_debug("%" PRIu64 "\n", ptime);
+
+       return true;
+}
+
+static bool test__perf_time__parse_str(const char *ostr, u64 start, u64 end)
+{
+       struct perf_time_interval ptime;
+       int err;
+
+       pr_debug("\nperf_time__parse_str(\"%s\")\n", ostr);
+
+       err = perf_time__parse_str(&ptime, ostr);
+       if (err) {
+               pr_debug("Error %d\n", err);
+               return false;
+       }
+
+       if (ptime.start != start || ptime.end != end) {
+               pr_debug("Failed. Expected %" PRIu64 " to %" PRIu64 "\n",
+                        start, end);
+               return false;
+       }
+
+       return true;
+}
+
+#define TEST_MAX 64
+
+struct test_data {
+       const char *str;
+       u64 first;
+       u64 last;
+       struct perf_time_interval ptime[TEST_MAX];
+       int num;
+       u64 skip[TEST_MAX];
+       u64 noskip[TEST_MAX];
+};
+
+static bool test__perf_time__parse_for_ranges(struct test_data *d)
+{
+       struct perf_evlist evlist = {
+               .first_sample_time = d->first,
+               .last_sample_time = d->last,
+       };
+       struct perf_session session = { .evlist = &evlist };
+       struct perf_time_interval *ptime = NULL;
+       int range_size, range_num;
+       bool pass = false;
+       int i, err;
+
+       pr_debug("\nperf_time__parse_for_ranges(\"%s\")\n", d->str);
+
+       if (strchr(d->str, '%'))
+               pr_debug("first_sample_time %" PRIu64 " last_sample_time %" PRIu64 "\n",
+                        d->first, d->last);
+
+       err = perf_time__parse_for_ranges(d->str, &session, &ptime, &range_size,
+                                         &range_num);
+       if (err) {
+               pr_debug("error %d\n", err);
+               goto out;
+       }
+
+       if (range_size < d->num || range_num != d->num) {
+               pr_debug("bad size: range_size %d range_num %d expected num %d\n",
+                        range_size, range_num, d->num);
+               goto out;
+       }
+
+       for (i = 0; i < d->num; i++) {
+               if (ptime[i].start != d->ptime[i].start ||
+                   ptime[i].end != d->ptime[i].end) {
+                       pr_debug("bad range %d expected %" PRIu64 " to %" PRIu64 "\n",
+                                i, d->ptime[i].start, d->ptime[i].end);
+                       goto out;
+               }
+       }
+
+       if (perf_time__ranges_skip_sample(ptime, d->num, 0)) {
+               pr_debug("failed to keep 0\n");
+               goto out;
+       }
+
+       for (i = 0; i < TEST_MAX; i++) {
+               if (d->skip[i] &&
+                   !perf_time__ranges_skip_sample(ptime, d->num, d->skip[i])) {
+                       pr_debug("failed to skip %" PRIu64 "\n", d->skip[i]);
+                       goto out;
+               }
+               if (d->noskip[i] &&
+                   perf_time__ranges_skip_sample(ptime, d->num, d->noskip[i])) {
+                       pr_debug("failed to keep %" PRIu64 "\n", d->noskip[i]);
+                       goto out;
+               }
+       }
+
+       pass = true;
+out:
+       free(ptime);
+       return pass;
+}
+
+int test__time_utils(struct test *t __maybe_unused, int subtest __maybe_unused)
+{
+       bool pass = true;
+
+       pass &= test__parse_nsec_time("0", 0);
+       pass &= test__parse_nsec_time("1", 1000000000ULL);
+       pass &= test__parse_nsec_time("0.000000001", 1);
+       pass &= test__parse_nsec_time("1.000000001", 1000000001ULL);
+       pass &= test__parse_nsec_time("123456.123456", 123456123456000ULL);
+       pass &= test__parse_nsec_time("1234567.123456789", 1234567123456789ULL);
+       pass &= test__parse_nsec_time("18446744073.709551615",
+                                     0xFFFFFFFFFFFFFFFFULL);
+
+       pass &= test__perf_time__parse_str("1234567.123456789,1234567.123456789",
+                                          1234567123456789ULL, 1234567123456789ULL);
+       pass &= test__perf_time__parse_str("1234567.123456789,1234567.123456790",
+                                          1234567123456789ULL, 1234567123456790ULL);
+       pass &= test__perf_time__parse_str("1234567.123456789,",
+                                          1234567123456789ULL, 0);
+       pass &= test__perf_time__parse_str(",1234567.123456789",
+                                          0, 1234567123456789ULL);
+       pass &= test__perf_time__parse_str("0,1234567.123456789",
+                                          0, 1234567123456789ULL);
+
+       {
+               u64 b = 1234567123456789ULL;
+               struct test_data d = {
+                       .str   = "1234567.123456789,1234567.123456790",
+                       .ptime = { {b, b + 1}, },
+                       .num = 1,
+                       .skip = { b - 1, b + 2, },
+                       .noskip = { b, b + 1, },
+               };
+
+               pass &= test__perf_time__parse_for_ranges(&d);
+       }
+
+       {
+               u64 b = 7654321ULL * NSEC_PER_SEC;
+               struct test_data d = {
+                       .str    = "10%/1",
+                       .first  = b,
+                       .last   = b + 100,
+                       .ptime  = { {b, b + 9}, },
+                       .num    = 1,
+                       .skip   = { b - 1, b + 10, },
+                       .noskip = { b, b + 9, },
+               };
+
+               pass &= test__perf_time__parse_for_ranges(&d);
+       }
+
+       {
+               u64 b = 7654321ULL * NSEC_PER_SEC;
+               struct test_data d = {
+                       .str    = "10%/2",
+                       .first  = b,
+                       .last   = b + 100,
+                       .ptime  = { {b + 10, b + 19}, },
+                       .num    = 1,
+                       .skip   = { b + 9, b + 20, },
+                       .noskip = { b + 10, b + 19, },
+               };
+
+               pass &= test__perf_time__parse_for_ranges(&d);
+       }
+
+       {
+               u64 b = 11223344ULL * NSEC_PER_SEC;
+               struct test_data d = {
+                       .str    = "10%/1,10%/2",
+                       .first  = b,
+                       .last   = b + 100,
+                       .ptime  = { {b, b + 9}, {b + 10, b + 19}, },
+                       .num    = 2,
+                       .skip   = { b - 1, b + 20, },
+                       .noskip = { b, b + 8, b + 9, b + 10, b + 11, b + 12, b + 19, },
+               };
+
+               pass &= test__perf_time__parse_for_ranges(&d);
+       }
+
+       {
+               u64 b = 11223344ULL * NSEC_PER_SEC;
+               struct test_data d = {
+                       .str    = "10%/1,10%/3,10%/10",
+                       .first  = b,
+                       .last   = b + 100,
+                       .ptime  = { {b, b + 9}, {b + 20, b + 29}, { b + 90, b + 100}, },
+                       .num    = 3,
+                       .skip   = { b - 1, b + 10, b + 19, b + 30, b + 89, b + 101 },
+                       .noskip = { b, b + 9, b + 20, b + 29, b + 90, b + 100},
+               };
+
+               pass &= test__perf_time__parse_for_ranges(&d);
+       }
+
+       pr_debug("\n");
+
+       return pass ? 0 : TEST_FAIL;
+}