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

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: 
        !            92:                snprintf(gname, sizeof(gname), "group%02d", gidx);
        !            93:                group = group_lookup(gname, GROUP_CREATE);
        !            94: 
        !            95:                for (n = 0; n < c_numdev; n++) {
        !            96:                        char ifname[IFNAMSIZ];
        !            97:                        struct element *e;
        !            98:                        int i;
        !            99: 
        !           100:                        snprintf(ifname, sizeof(ifname), "dummy%d", n);
        !           101: 
        !           102:                        if (!(e = element_lookup(group, ifname, 0, NULL, ELEMENT_CREAT)))
        !           103:                                return;
        !           104: 
        !           105:                        if (e->e_flags & ELEMENT_FLAG_CREATED) {
        !           106:                                if (element_set_key_attr(e, "bytes", "packets") ||
        !           107:                                    element_set_usage_attr(e, "bytes"))
        !           108:                                        BUG();
        !           109:                                e->e_flags &= ~ELEMENT_FLAG_CREATED;
        !           110:                        }
        !           111: 
        !           112:                        if (e->e_flags & ELEMENT_FLAG_UPDATED)
        !           113:                                continue;
        !           114: 
        !           115:                        if (c_randomize) {
        !           116:                                uint64_t rx = rand() % c_maxpps;
        !           117:                                uint64_t tx = rand() % c_maxpps;
        !           118: 
        !           119:                                *cnt(gidx, n, 0, 0) += rx;
        !           120:                                *cnt(gidx, n, 0, 1) += tx;
        !           121:                                *cnt(gidx, n, 1, 0) += rx * (rand() % c_mtu);
        !           122:                                *cnt(gidx, n, 1, 1) += tx * (rand() % c_mtu);
        !           123:                        } else {
        !           124:                                *cnt(gidx, n, 0, 0) += c_rx_p_inc;
        !           125:                                *cnt(gidx, n, 0, 1) += c_tx_p_inc;
        !           126:                                *cnt(gidx, n, 1, 0) += c_rx_b_inc;
        !           127:                                *cnt(gidx, n, 1, 1) += c_tx_b_inc;
        !           128:                        }
        !           129: 
        !           130:                        for (i = 0; i < ARRAY_SIZE(link_attrs); i++) {
        !           131:                                struct attr_map *m = &link_attrs[i];
        !           132: 
        !           133:                                attr_update(e, m->attrid, 
        !           134:                                            *cnt(gidx, n, i, 0),
        !           135:                                            *cnt(gidx, n, i, 1),
        !           136:                                            UPDATE_FLAG_RX | UPDATE_FLAG_TX |
        !           137:                                            UPDATE_FLAG_64BIT);
        !           138:                        }
1.1       misho     139: 
1.1.1.2 ! misho     140:                        element_notify_update(e, NULL);
        !           141:                        element_lifesign(e, 1);
        !           142:                }
1.1       misho     143:        }
                    144: }
                    145: 
                    146: static void print_help(void)
                    147: {
                    148:        printf(
                    149:        "dummy - Statistic generator module (dummy)\n" \
                    150:        "\n" \
                    151:        "  Basic statistic generator for testing purposes. Can produce a\n" \
                    152:        "  constant or random statistic flow with configurable parameters.\n" \
                    153:        "  Author: Thomas Graf <tgraf@suug.ch>\n" \
                    154:        "\n" \
                    155:        "  Options:\n" \
                    156:        "    rxb=NUM        RX bytes increment amount (default: 10^9)\n" \
                    157:        "    txb=NUM        TX bytes increment amount (default: 8*10^7)\n" \
                    158:        "    rxp=NUM        RX packets increment amount (default: 1K)\n" \
                    159:        "    txp=NUM        TX packets increment amount (default: 800)\n" \
                    160:        "    num=NUM        Number of devices (default: 5)\n" \
1.1.1.2 ! misho     161:        "    numgroups=NUM  Number of groups (default: 2)\n" \
1.1       misho     162:        "    randomize      Randomize counters (default: off)\n" \
                    163:        "    seed=NUM       Seed for randomizer (default: time(0))\n" \
                    164:        "    mtu=NUM        Maximal Transmission Unit (default: 1540)\n" \
                    165:        "    maxpps=NUM     Upper limit for packets per second (default: 100K)\n" \
                    166:        "\n" \
                    167:        "  Randomizer:\n" \
                    168:        "    RX-packets := Rand() %% maxpps\n" \
                    169:        "    TX-packets := Rand() %% maxpps\n" \
                    170:        "    RX-bytes   := RX-packets * (Rand() %% mtu)\n" \
                    171:        "    TX-bytes   := TX-packets * (Rand() %% mtu)\n");
                    172: }
                    173: 
1.1.1.2 ! misho     174: static void dummy_parse_opt(const char *type, const char *value)
1.1       misho     175: {
1.1.1.2 ! misho     176:        if (!strcasecmp(type, "rxb") && value)
        !           177:                c_rx_b_inc = strtol(value, NULL, 0);
        !           178:        else if (!strcasecmp(type, "txb") && value)
        !           179:                c_tx_b_inc = strtol(value, NULL, 0);
        !           180:        else if (!strcasecmp(type, "rxp") && value)
        !           181:                c_rx_p_inc = strtol(value, NULL, 0);
        !           182:        else if (!strcasecmp(type, "txp") && value)
        !           183:                c_tx_p_inc = strtol(value, NULL, 0);
        !           184:        else if (!strcasecmp(type, "num") && value)
        !           185:                c_numdev = strtol(value, NULL, 0);
        !           186:        else if (!strcasecmp(type, "randomize")) {
        !           187:                c_randomize = 1;
        !           188:                srand(time(0));
        !           189:        } else if (!strcasecmp(type, "seed") && value)
        !           190:                srand(strtol(value, NULL, 0));
        !           191:        else if (!strcasecmp(type, "mtu") && value)
        !           192:                c_mtu = strtol(value, NULL, 0);
        !           193:        else if (!strcasecmp(type, "maxpps") && value)
        !           194:                c_maxpps = strtol(value, NULL, 0);
        !           195:        else if (!strcasecmp(type, "numgroups") && value)
        !           196:                c_numgroups = strtol(value, NULL, 0);
        !           197:        else if (!strcasecmp(type, "help")) {
        !           198:                print_help();
        !           199:                exit(0);
1.1       misho     200:        }
                    201: }
                    202: 
1.1.1.2 ! misho     203: static int dummy_do_init(void)
        !           204: {
        !           205:        if (attr_map_load(link_attrs, ARRAY_SIZE(link_attrs)))
        !           206:                BUG();
        !           207: 
        !           208:        return 0;
        !           209: }
        !           210: 
1.1       misho     211: static int dummy_probe(void)
                    212: {
1.1.1.2 ! misho     213:        int i;
        !           214: 
        !           215:        if (c_numdev >= MAXDEVS) {
        !           216:                fprintf(stderr, "numdev must be in range 0..%d\n", MAXDEVS);
        !           217:                return 0;
        !           218:        }
        !           219: 
        !           220:        cnts = xcalloc(cnt_size(), sizeof(uint64_t));
        !           221: 
        !           222:        for (i = 0; i < c_numgroups; i++) {
        !           223:                char groupname[32];
        !           224:                snprintf(groupname, sizeof(groupname), "group%02d", i);
        !           225: 
        !           226:                group_new_derived_hdr(groupname, groupname, DEFAULT_GROUP);
        !           227:        }
        !           228: 
1.1       misho     229:        return 1;
                    230: }
                    231: 
1.1.1.2 ! misho     232: static struct bmon_module dummy_ops = {
        !           233:        .m_name         = "dummy",
        !           234:        .m_do           = dummy_read,
        !           235:        .m_parse_opt    = dummy_parse_opt,
        !           236:        .m_probe        = dummy_probe,
        !           237:        .m_init         = dummy_do_init,
1.1       misho     238: };
                    239: 
                    240: static void __init dummy_init(void)
                    241: {
1.1.1.2 ! misho     242:        input_register(&dummy_ops);
1.1       misho     243: }

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