Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include "util/debug.h"
0003 #include "util/expr.h"
0004 #include "util/header.h"
0005 #include "util/smt.h"
0006 #include "tests.h"
0007 #include <math.h>
0008 #include <stdlib.h>
0009 #include <string.h>
0010 #include <linux/zalloc.h>
0011 
0012 static int test_ids_union(void)
0013 {
0014     struct hashmap *ids1, *ids2;
0015 
0016     /* Empty union. */
0017     ids1 = ids__new();
0018     TEST_ASSERT_VAL("ids__new", ids1);
0019     ids2 = ids__new();
0020     TEST_ASSERT_VAL("ids__new", ids2);
0021 
0022     ids1 = ids__union(ids1, ids2);
0023     TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 0);
0024 
0025     /* Union {foo, bar} against {}. */
0026     ids2 = ids__new();
0027     TEST_ASSERT_VAL("ids__new", ids2);
0028 
0029     TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids1, strdup("foo")), 0);
0030     TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids1, strdup("bar")), 0);
0031 
0032     ids1 = ids__union(ids1, ids2);
0033     TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 2);
0034 
0035     /* Union {foo, bar} against {foo}. */
0036     ids2 = ids__new();
0037     TEST_ASSERT_VAL("ids__new", ids2);
0038     TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids2, strdup("foo")), 0);
0039 
0040     ids1 = ids__union(ids1, ids2);
0041     TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 2);
0042 
0043     /* Union {foo, bar} against {bar,baz}. */
0044     ids2 = ids__new();
0045     TEST_ASSERT_VAL("ids__new", ids2);
0046     TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids2, strdup("bar")), 0);
0047     TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids2, strdup("baz")), 0);
0048 
0049     ids1 = ids__union(ids1, ids2);
0050     TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 3);
0051 
0052     ids__free(ids1);
0053 
0054     return 0;
0055 }
0056 
0057 static int test(struct expr_parse_ctx *ctx, const char *e, double val2)
0058 {
0059     double val;
0060 
0061     if (expr__parse(&val, ctx, e))
0062         TEST_ASSERT_VAL("parse test failed", 0);
0063     TEST_ASSERT_VAL("unexpected value", val == val2);
0064     return 0;
0065 }
0066 
0067 static int test__expr(struct test_suite *t __maybe_unused, int subtest __maybe_unused)
0068 {
0069     struct expr_id_data *val_ptr;
0070     const char *p;
0071     double val, num_cpus, num_cores, num_dies, num_packages;
0072     int ret;
0073     struct expr_parse_ctx *ctx;
0074     bool is_intel = false;
0075     char buf[128];
0076 
0077     if (!get_cpuid(buf, sizeof(buf)))
0078         is_intel = strstr(buf, "Intel") != NULL;
0079 
0080     TEST_ASSERT_EQUAL("ids_union", test_ids_union(), 0);
0081 
0082     ctx = expr__ctx_new();
0083     TEST_ASSERT_VAL("expr__ctx_new", ctx);
0084     expr__add_id_val(ctx, strdup("FOO"), 1);
0085     expr__add_id_val(ctx, strdup("BAR"), 2);
0086 
0087     ret = test(ctx, "1+1", 2);
0088     ret |= test(ctx, "FOO+BAR", 3);
0089     ret |= test(ctx, "(BAR/2)%2", 1);
0090     ret |= test(ctx, "1 - -4",  5);
0091     ret |= test(ctx, "(FOO-1)*2 + (BAR/2)%2 - -4",  5);
0092     ret |= test(ctx, "1-1 | 1", 1);
0093     ret |= test(ctx, "1-1 & 1", 0);
0094     ret |= test(ctx, "min(1,2) + 1", 2);
0095     ret |= test(ctx, "max(1,2) + 1", 3);
0096     ret |= test(ctx, "1+1 if 3*4 else 0", 2);
0097     ret |= test(ctx, "1.1 + 2.1", 3.2);
0098     ret |= test(ctx, ".1 + 2.", 2.1);
0099     ret |= test(ctx, "d_ratio(1, 2)", 0.5);
0100     ret |= test(ctx, "d_ratio(2.5, 0)", 0);
0101     ret |= test(ctx, "1.1 < 2.2", 1);
0102     ret |= test(ctx, "2.2 > 1.1", 1);
0103     ret |= test(ctx, "1.1 < 1.1", 0);
0104     ret |= test(ctx, "2.2 > 2.2", 0);
0105     ret |= test(ctx, "2.2 < 1.1", 0);
0106     ret |= test(ctx, "1.1 > 2.2", 0);
0107     ret |= test(ctx, "1.1e10 < 1.1e100", 1);
0108     ret |= test(ctx, "1.1e2 > 1.1e-2", 1);
0109 
0110     if (ret) {
0111         expr__ctx_free(ctx);
0112         return ret;
0113     }
0114 
0115     p = "FOO/0";
0116     ret = expr__parse(&val, ctx, p);
0117     TEST_ASSERT_VAL("division by zero", ret == -1);
0118 
0119     p = "BAR/";
0120     ret = expr__parse(&val, ctx, p);
0121     TEST_ASSERT_VAL("missing operand", ret == -1);
0122 
0123     expr__ctx_clear(ctx);
0124     TEST_ASSERT_VAL("find ids",
0125             expr__find_ids("FOO + BAR + BAZ + BOZO", "FOO",
0126                     ctx) == 0);
0127     TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 3);
0128     TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "BAR",
0129                             (void **)&val_ptr));
0130     TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "BAZ",
0131                             (void **)&val_ptr));
0132     TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "BOZO",
0133                             (void **)&val_ptr));
0134 
0135     expr__ctx_clear(ctx);
0136     ctx->runtime = 3;
0137     TEST_ASSERT_VAL("find ids",
0138             expr__find_ids("EVENT1\\,param\\=?@ + EVENT2\\,param\\=?@",
0139                     NULL, ctx) == 0);
0140     TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 2);
0141     TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1,param=3@",
0142                             (void **)&val_ptr));
0143     TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT2,param=3@",
0144                             (void **)&val_ptr));
0145 
0146     expr__ctx_clear(ctx);
0147     TEST_ASSERT_VAL("find ids",
0148             expr__find_ids("dash\\-event1 - dash\\-event2",
0149                        NULL, ctx) == 0);
0150     TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 2);
0151     TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "dash-event1",
0152                             (void **)&val_ptr));
0153     TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "dash-event2",
0154                             (void **)&val_ptr));
0155 
0156     /* Only EVENT1 or EVENT2 need be measured depending on the value of smt_on. */
0157     expr__ctx_clear(ctx);
0158     TEST_ASSERT_VAL("find ids",
0159             expr__find_ids("EVENT1 if #smt_on else EVENT2",
0160                 NULL, ctx) == 0);
0161     TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
0162     TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids,
0163                           smt_on() ? "EVENT1" : "EVENT2",
0164                           (void **)&val_ptr));
0165 
0166     /* The expression is a constant 1.0 without needing to evaluate EVENT1. */
0167     expr__ctx_clear(ctx);
0168     TEST_ASSERT_VAL("find ids",
0169             expr__find_ids("1.0 if EVENT1 > 100.0 else 1.0",
0170             NULL, ctx) == 0);
0171     TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
0172 
0173     /* Test toplogy constants appear well ordered. */
0174     expr__ctx_clear(ctx);
0175     TEST_ASSERT_VAL("#num_cpus", expr__parse(&num_cpus, ctx, "#num_cpus") == 0);
0176     TEST_ASSERT_VAL("#num_cores", expr__parse(&num_cores, ctx, "#num_cores") == 0);
0177     TEST_ASSERT_VAL("#num_cpus >= #num_cores", num_cpus >= num_cores);
0178     TEST_ASSERT_VAL("#num_dies", expr__parse(&num_dies, ctx, "#num_dies") == 0);
0179     TEST_ASSERT_VAL("#num_cores >= #num_dies", num_cores >= num_dies);
0180     TEST_ASSERT_VAL("#num_packages", expr__parse(&num_packages, ctx, "#num_packages") == 0);
0181 
0182     if (num_dies) // Some platforms do not have CPU die support, for example s390
0183         TEST_ASSERT_VAL("#num_dies >= #num_packages", num_dies >= num_packages);
0184 
0185     TEST_ASSERT_VAL("#system_tsc_freq", expr__parse(&val, ctx, "#system_tsc_freq") == 0);
0186     if (is_intel)
0187         TEST_ASSERT_VAL("#system_tsc_freq > 0", val > 0);
0188     else
0189         TEST_ASSERT_VAL("#system_tsc_freq == 0", fpclassify(val) == FP_ZERO);
0190 
0191     /*
0192      * Source count returns the number of events aggregating in a leader
0193      * event including the leader. Check parsing yields an id.
0194      */
0195     expr__ctx_clear(ctx);
0196     TEST_ASSERT_VAL("source count",
0197             expr__find_ids("source_count(EVENT1)",
0198             NULL, ctx) == 0);
0199     TEST_ASSERT_VAL("source count", hashmap__size(ctx->ids) == 1);
0200     TEST_ASSERT_VAL("source count", hashmap__find(ctx->ids, "EVENT1",
0201                             (void **)&val_ptr));
0202 
0203     expr__ctx_free(ctx);
0204 
0205     return 0;
0206 }
0207 
0208 DEFINE_SUITE("Simple expression parser", expr);