Annotation of embedaddon/bmon/src/in_dummy.c, revision 1.1.1.3

1.1       misho       1: /*
                      2:  * in_dummy.c                Dummy Input Method
                      3:  *
1.1.1.2   misho       4:  * Copyright (c) 2001-2013 Thomas Graf <tgraf@suug.ch>
                      5:  * Copyright (c) 2013 Red Hat, Inc.
1.1       misho       6:  *
                      7:  * Permission is hereby granted, free of charge, to any person obtaining a
                      8:  * copy of this software and associated documentation files (the "Software"),
                      9:  * to deal in the Software without restriction, including without limitation
                     10:  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
                     11:  * and/or sell copies of the Software, and to permit persons to whom the
                     12:  * Software is furnished to do so, subject to the following conditions:
                     13:  *
                     14:  * The above copyright notice and this permission notice shall be included
                     15:  * in all copies or substantial portions of the Software.
                     16:  *
                     17:  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
                     18:  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
                     19:  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
                     20:  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
                     21:  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
                     22:  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
                     23:  * DEALINGS IN THE SOFTWARE.
                     24:  */
                     25: 
                     26: #include <bmon/bmon.h>
                     27: #include <bmon/input.h>
1.1.1.2   misho      28: #include <bmon/group.h>
                     29: #include <bmon/element.h>
                     30: #include <bmon/attr.h>
1.1       misho      31: #include <bmon/utils.h>
                     32: 
1.1.1.2   misho      33: #define MAXDEVS 32
                     34: 
                     35: static uint64_t c_rx_b_inc = 1000000000;
                     36: static uint64_t c_tx_b_inc = 80000000;
                     37: static uint64_t c_rx_p_inc = 1000;
                     38: static uint64_t c_tx_p_inc = 800;
1.1       misho      39: static int c_numdev = 5;
                     40: static int c_randomize = 0;
                     41: static int c_mtu = 1540;
                     42: static int c_maxpps = 100000;
1.1.1.2   misho      43: static int c_numgroups = 2;
                     44: 
                     45: static uint64_t *cnts;
                     46: 
                     47: enum {
                     48:        DUMMY_BYTES,
                     49:        DUMMY_PACKETS,
                     50:        NUM_DUMMY_VALUE,
                     51: };
                     52: 
                     53: static struct attr_map link_attrs[NUM_DUMMY_VALUE] = {
                     54: {
                     55:        .name           = "bytes",
                     56:        .type           = ATTR_TYPE_COUNTER,
                     57:        .unit           = UNIT_BYTE,
                     58:        .description    = "Bytes",
                     59: },
                     60: {
                     61:        .name           = "packets",
                     62:        .type           = ATTR_TYPE_COUNTER,
                     63:        .unit           = UNIT_NUMBER,
                     64:        .description    = "Packets",
                     65: }
                     66: };
                     67: 
                     68: /* cnts[ngroups][ndevs][2][2] */
                     69: 
                     70: static inline int cnt_size(void)
                     71: {
                     72:        return c_numgroups * c_numdev * 2 * 2;
                     73: }
                     74: 
                     75: static inline uint64_t *cnt(int group, int dev, int unit,
                     76:                           int direction)
                     77: {
                     78:        return cnts + (group * c_numdev * 2 * 2) +
                     79:                      (dev * 2 * 2) +
                     80:                      (unit * 2) +
                     81:                      direction;
                     82: }
1.1       misho      83: 
                     84: static void dummy_read(void)
                     85: {
1.1.1.2   misho      86:        int gidx, n;
1.1       misho      87: 
1.1.1.2   misho      88:        for (gidx = 0; gidx < c_numgroups; gidx++) {
                     89:                char gname[32];
                     90:                struct element_group *group;
                     91: 
1.1.1.3 ! misho      92:                if (gidx == 0)
        !            93:                        snprintf(gname, sizeof(gname), "%s", DEFAULT_GROUP);
        !            94:                else
        !            95:                        snprintf(gname, sizeof(gname), "group%02d", gidx);
        !            96: 
1.1.1.2   misho      97:                group = group_lookup(gname, GROUP_CREATE);
                     98: 
                     99:                for (n = 0; n < c_numdev; n++) {
                    100:                        char ifname[IFNAMSIZ];
                    101:                        struct element *e;
                    102:                        int i;
                    103: 
                    104:                        snprintf(ifname, sizeof(ifname), "dummy%d", n);
                    105: 
                    106:                        if (!(e = element_lookup(group, ifname, 0, NULL, ELEMENT_CREAT)))
                    107:                                return;
                    108: 
                    109:                        if (e->e_flags & ELEMENT_FLAG_CREATED) {
                    110:                                if (element_set_key_attr(e, "bytes", "packets") ||
                    111:                                    element_set_usage_attr(e, "bytes"))
                    112:                                        BUG();
                    113:                                e->e_flags &= ~ELEMENT_FLAG_CREATED;
                    114:                        }
                    115: 
                    116:                        if (e->e_flags & ELEMENT_FLAG_UPDATED)
                    117:                                continue;
                    118: 
                    119:                        if (c_randomize) {
                    120:                                uint64_t rx = rand() % c_maxpps;
                    121:                                uint64_t tx = rand() % c_maxpps;
                    122: 
                    123:                                *cnt(gidx, n, 0, 0) += rx;
                    124:                                *cnt(gidx, n, 0, 1) += tx;
                    125:                                *cnt(gidx, n, 1, 0) += rx * (rand() % c_mtu);
                    126:                                *cnt(gidx, n, 1, 1) += tx * (rand() % c_mtu);
                    127:                        } else {
                    128:                                *cnt(gidx, n, 0, 0) += c_rx_p_inc;
                    129:                                *cnt(gidx, n, 0, 1) += c_tx_p_inc;
                    130:                                *cnt(gidx, n, 1, 0) += c_rx_b_inc;
                    131:                                *cnt(gidx, n, 1, 1) += c_tx_b_inc;
                    132:                        }
                    133: 
                    134:                        for (i = 0; i < ARRAY_SIZE(link_attrs); i++) {
                    135:                                struct attr_map *m = &link_attrs[i];
                    136: 
                    137:                                attr_update(e, m->attrid, 
                    138:                                            *cnt(gidx, n, i, 0),
                    139:                                            *cnt(gidx, n, i, 1),
                    140:                                            UPDATE_FLAG_RX | UPDATE_FLAG_TX |
                    141:                                            UPDATE_FLAG_64BIT);
                    142:                        }
1.1       misho     143: 
1.1.1.2   misho     144:                        element_notify_update(e, NULL);
                    145:                        element_lifesign(e, 1);
                    146:                }
1.1       misho     147:        }
                    148: }
                    149: 
                    150: static void print_help(void)
                    151: {
                    152:        printf(
                    153:        "dummy - Statistic generator module (dummy)\n" \
                    154:        "\n" \
                    155:        "  Basic statistic generator for testing purposes. Can produce a\n" \
                    156:        "  constant or random statistic flow with configurable parameters.\n" \
                    157:        "  Author: Thomas Graf <tgraf@suug.ch>\n" \
                    158:        "\n" \
                    159:        "  Options:\n" \
                    160:        "    rxb=NUM        RX bytes increment amount (default: 10^9)\n" \
                    161:        "    txb=NUM        TX bytes increment amount (default: 8*10^7)\n" \
                    162:        "    rxp=NUM        RX packets increment amount (default: 1K)\n" \
                    163:        "    txp=NUM        TX packets increment amount (default: 800)\n" \
                    164:        "    num=NUM        Number of devices (default: 5)\n" \
1.1.1.2   misho     165:        "    numgroups=NUM  Number of groups (default: 2)\n" \
1.1       misho     166:        "    randomize      Randomize counters (default: off)\n" \
                    167:        "    seed=NUM       Seed for randomizer (default: time(0))\n" \
                    168:        "    mtu=NUM        Maximal Transmission Unit (default: 1540)\n" \
                    169:        "    maxpps=NUM     Upper limit for packets per second (default: 100K)\n" \
                    170:        "\n" \
                    171:        "  Randomizer:\n" \
                    172:        "    RX-packets := Rand() %% maxpps\n" \
                    173:        "    TX-packets := Rand() %% maxpps\n" \
                    174:        "    RX-bytes   := RX-packets * (Rand() %% mtu)\n" \
                    175:        "    TX-bytes   := TX-packets * (Rand() %% mtu)\n");
                    176: }
                    177: 
1.1.1.2   misho     178: static void dummy_parse_opt(const char *type, const char *value)
1.1       misho     179: {
1.1.1.2   misho     180:        if (!strcasecmp(type, "rxb") && value)
                    181:                c_rx_b_inc = strtol(value, NULL, 0);
                    182:        else if (!strcasecmp(type, "txb") && value)
                    183:                c_tx_b_inc = strtol(value, NULL, 0);
                    184:        else if (!strcasecmp(type, "rxp") && value)
                    185:                c_rx_p_inc = strtol(value, NULL, 0);
                    186:        else if (!strcasecmp(type, "txp") && value)
                    187:                c_tx_p_inc = strtol(value, NULL, 0);
                    188:        else if (!strcasecmp(type, "num") && value)
                    189:                c_numdev = strtol(value, NULL, 0);
                    190:        else if (!strcasecmp(type, "randomize")) {
                    191:                c_randomize = 1;
1.1.1.3 ! misho     192:                srand(time(NULL));
1.1.1.2   misho     193:        } else if (!strcasecmp(type, "seed") && value)
                    194:                srand(strtol(value, NULL, 0));
                    195:        else if (!strcasecmp(type, "mtu") && value)
                    196:                c_mtu = strtol(value, NULL, 0);
                    197:        else if (!strcasecmp(type, "maxpps") && value)
                    198:                c_maxpps = strtol(value, NULL, 0);
                    199:        else if (!strcasecmp(type, "numgroups") && value)
                    200:                c_numgroups = strtol(value, NULL, 0);
                    201:        else if (!strcasecmp(type, "help")) {
                    202:                print_help();
                    203:                exit(0);
1.1       misho     204:        }
                    205: }
                    206: 
1.1.1.2   misho     207: static int dummy_do_init(void)
                    208: {
                    209:        if (attr_map_load(link_attrs, ARRAY_SIZE(link_attrs)))
                    210:                BUG();
                    211: 
                    212:        return 0;
                    213: }
                    214: 
1.1       misho     215: static int dummy_probe(void)
                    216: {
1.1.1.2   misho     217:        int i;
                    218: 
                    219:        if (c_numdev >= MAXDEVS) {
                    220:                fprintf(stderr, "numdev must be in range 0..%d\n", MAXDEVS);
                    221:                return 0;
                    222:        }
                    223: 
                    224:        cnts = xcalloc(cnt_size(), sizeof(uint64_t));
                    225: 
                    226:        for (i = 0; i < c_numgroups; i++) {
                    227:                char groupname[32];
                    228:                snprintf(groupname, sizeof(groupname), "group%02d", i);
                    229: 
                    230:                group_new_derived_hdr(groupname, groupname, DEFAULT_GROUP);
                    231:        }
                    232: 
1.1       misho     233:        return 1;
                    234: }
                    235: 
1.1.1.2   misho     236: static struct bmon_module dummy_ops = {
                    237:        .m_name         = "dummy",
                    238:        .m_do           = dummy_read,
                    239:        .m_parse_opt    = dummy_parse_opt,
                    240:        .m_probe        = dummy_probe,
                    241:        .m_init         = dummy_do_init,
1.1       misho     242: };
                    243: 
                    244: static void __init dummy_init(void)
                    245: {
1.1.1.2   misho     246:        input_register(&dummy_ops);
1.1       misho     247: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>