version 1.1.1.1, 2012/02/21 22:19:56
|
version 1.1.1.3, 2019/10/21 14:58:35
|
Line 1
|
Line 1
|
/* |
/* |
* in_dummy.c Dummy Input Method |
* in_dummy.c Dummy Input Method |
* |
* |
* Copyright (c) 2001-2004 Thomas Graf <tgraf@suug.ch> | * Copyright (c) 2001-2013 Thomas Graf <tgraf@suug.ch> |
| * Copyright (c) 2013 Red Hat, Inc. |
* |
* |
* Permission is hereby granted, free of charge, to any person obtaining a |
* Permission is hereby granted, free of charge, to any person obtaining a |
* copy of this software and associated documentation files (the "Software"), |
* copy of this software and associated documentation files (the "Software"), |
Line 24
|
Line 25
|
|
|
#include <bmon/bmon.h> |
#include <bmon/bmon.h> |
#include <bmon/input.h> |
#include <bmon/input.h> |
#include <bmon/node.h> | #include <bmon/group.h> |
#include <bmon/item.h> | #include <bmon/element.h> |
| #include <bmon/attr.h> |
#include <bmon/utils.h> |
#include <bmon/utils.h> |
|
|
static b_cnt_t c_rx_b_inc = 1000000000; | #define MAXDEVS 32 |
static b_cnt_t c_tx_b_inc = 80000000; | |
static b_cnt_t c_rx_p_inc = 1000; | static uint64_t c_rx_b_inc = 1000000000; |
static b_cnt_t c_tx_p_inc = 800; | static uint64_t c_tx_b_inc = 80000000; |
| static uint64_t c_rx_p_inc = 1000; |
| static uint64_t c_tx_p_inc = 800; |
static int c_numdev = 5; |
static int c_numdev = 5; |
static int c_randomize = 0; |
static int c_randomize = 0; |
static int c_mtu = 1540; |
static int c_mtu = 1540; |
static int c_maxpps = 100000; |
static int c_maxpps = 100000; |
|
static int c_numgroups = 2; |
|
|
|
static uint64_t *cnts; |
|
|
|
enum { |
|
DUMMY_BYTES, |
|
DUMMY_PACKETS, |
|
NUM_DUMMY_VALUE, |
|
}; |
|
|
|
static struct attr_map link_attrs[NUM_DUMMY_VALUE] = { |
|
{ |
|
.name = "bytes", |
|
.type = ATTR_TYPE_COUNTER, |
|
.unit = UNIT_BYTE, |
|
.description = "Bytes", |
|
}, |
|
{ |
|
.name = "packets", |
|
.type = ATTR_TYPE_COUNTER, |
|
.unit = UNIT_NUMBER, |
|
.description = "Packets", |
|
} |
|
}; |
|
|
|
/* cnts[ngroups][ndevs][2][2] */ |
|
|
|
static inline int cnt_size(void) |
|
{ |
|
return c_numgroups * c_numdev * 2 * 2; |
|
} |
|
|
|
static inline uint64_t *cnt(int group, int dev, int unit, |
|
int direction) |
|
{ |
|
return cnts + (group * c_numdev * 2 * 2) + |
|
(dev * 2 * 2) + |
|
(unit * 2) + |
|
direction; |
|
} |
|
|
static void dummy_read(void) |
static void dummy_read(void) |
{ |
{ |
int n; | int gidx, n; |
|
|
for (n = 0; n < c_numdev; n++) { | for (gidx = 0; gidx < c_numgroups; gidx++) { |
char ifname[IFNAMSIZ]; | char gname[32]; |
item_t *it; | struct element_group *group; |
|
|
snprintf(ifname, sizeof(ifname), "dummy%d", n); | if (gidx == 0) |
| snprintf(gname, sizeof(gname), "%s", DEFAULT_GROUP); |
it = lookup_item(get_local_node(), ifname, 0, 0); | else |
if (it == NULL) | snprintf(gname, sizeof(gname), "group%02d", gidx); |
return; | |
|
|
it->i_major_attr = BYTES; | group = group_lookup(gname, GROUP_CREATE); |
it->i_minor_attr = PACKETS; | |
|
|
if (c_randomize) { | for (n = 0; n < c_numdev; n++) { |
b_cnt_t rx = rand() % c_maxpps; | char ifname[IFNAMSIZ]; |
b_cnt_t tx = rand() % c_maxpps; | struct element *e; |
| int i; |
update_attr(it, PACKETS, rx, tx, RX_PROVIDED | TX_PROVIDED); | |
update_attr(it, BYTES, rx * (rand() % c_mtu), | |
tx * (rand() % c_mtu), RX_PROVIDED | TX_PROVIDED); | |
} else { | |
update_attr(it, PACKETS, c_rx_p_inc, c_tx_p_inc, | |
RX_PROVIDED | TX_PROVIDED); | |
update_attr(it, BYTES, c_rx_b_inc, c_tx_b_inc, | |
RX_PROVIDED | TX_PROVIDED); | |
} | |
|
|
notify_update(it, NULL); | snprintf(ifname, sizeof(ifname), "dummy%d", n); |
increase_lifetime(it, 1); | |
| if (!(e = element_lookup(group, ifname, 0, NULL, ELEMENT_CREAT))) |
| return; |
| |
| if (e->e_flags & ELEMENT_FLAG_CREATED) { |
| if (element_set_key_attr(e, "bytes", "packets") || |
| element_set_usage_attr(e, "bytes")) |
| BUG(); |
| e->e_flags &= ~ELEMENT_FLAG_CREATED; |
| } |
| |
| if (e->e_flags & ELEMENT_FLAG_UPDATED) |
| continue; |
| |
| if (c_randomize) { |
| uint64_t rx = rand() % c_maxpps; |
| uint64_t tx = rand() % c_maxpps; |
| |
| *cnt(gidx, n, 0, 0) += rx; |
| *cnt(gidx, n, 0, 1) += tx; |
| *cnt(gidx, n, 1, 0) += rx * (rand() % c_mtu); |
| *cnt(gidx, n, 1, 1) += tx * (rand() % c_mtu); |
| } else { |
| *cnt(gidx, n, 0, 0) += c_rx_p_inc; |
| *cnt(gidx, n, 0, 1) += c_tx_p_inc; |
| *cnt(gidx, n, 1, 0) += c_rx_b_inc; |
| *cnt(gidx, n, 1, 1) += c_tx_b_inc; |
| } |
| |
| for (i = 0; i < ARRAY_SIZE(link_attrs); i++) { |
| struct attr_map *m = &link_attrs[i]; |
| |
| attr_update(e, m->attrid, |
| *cnt(gidx, n, i, 0), |
| *cnt(gidx, n, i, 1), |
| UPDATE_FLAG_RX | UPDATE_FLAG_TX | |
| UPDATE_FLAG_64BIT); |
| } |
| |
| element_notify_update(e, NULL); |
| element_lifesign(e, 1); |
| } |
} |
} |
} |
} |
|
|
Line 88 static void print_help(void)
|
Line 162 static void print_help(void)
|
" rxp=NUM RX packets increment amount (default: 1K)\n" \ |
" rxp=NUM RX packets increment amount (default: 1K)\n" \ |
" txp=NUM TX packets increment amount (default: 800)\n" \ |
" txp=NUM TX packets increment amount (default: 800)\n" \ |
" num=NUM Number of devices (default: 5)\n" \ |
" num=NUM Number of devices (default: 5)\n" \ |
|
" numgroups=NUM Number of groups (default: 2)\n" \ |
" randomize Randomize counters (default: off)\n" \ |
" randomize Randomize counters (default: off)\n" \ |
" seed=NUM Seed for randomizer (default: time(0))\n" \ |
" seed=NUM Seed for randomizer (default: time(0))\n" \ |
" mtu=NUM Maximal Transmission Unit (default: 1540)\n" \ |
" mtu=NUM Maximal Transmission Unit (default: 1540)\n" \ |
Line 100 static void print_help(void)
|
Line 175 static void print_help(void)
|
" TX-bytes := TX-packets * (Rand() %% mtu)\n"); |
" TX-bytes := TX-packets * (Rand() %% mtu)\n"); |
} |
} |
|
|
static void dummy_set_opts(tv_t *attrs) | static void dummy_parse_opt(const char *type, const char *value) |
{ |
{ |
while (attrs) { | if (!strcasecmp(type, "rxb") && value) |
if (!strcasecmp(attrs->type, "rxb") && attrs->value) | c_rx_b_inc = strtol(value, NULL, 0); |
c_rx_b_inc = strtol(attrs->value, NULL, 0); | else if (!strcasecmp(type, "txb") && value) |
else if (!strcasecmp(attrs->type, "txb") && attrs->value) | c_tx_b_inc = strtol(value, NULL, 0); |
c_tx_b_inc = strtol(attrs->value, NULL, 0); | else if (!strcasecmp(type, "rxp") && value) |
else if (!strcasecmp(attrs->type, "rxp") && attrs->value) | c_rx_p_inc = strtol(value, NULL, 0); |
c_rx_p_inc = strtol(attrs->value, NULL, 0); | else if (!strcasecmp(type, "txp") && value) |
else if (!strcasecmp(attrs->type, "txp") && attrs->value) | c_tx_p_inc = strtol(value, NULL, 0); |
c_tx_p_inc = strtol(attrs->value, NULL, 0); | else if (!strcasecmp(type, "num") && value) |
else if (!strcasecmp(attrs->type, "num") && attrs->value) | c_numdev = strtol(value, NULL, 0); |
c_numdev = strtol(attrs->value, NULL, 0); | else if (!strcasecmp(type, "randomize")) { |
else if (!strcasecmp(attrs->type, "randomize")) { | c_randomize = 1; |
c_randomize = 1; | srand(time(NULL)); |
srand(time(0)); | } else if (!strcasecmp(type, "seed") && value) |
} else if (!strcasecmp(attrs->type, "seed") && attrs->value) | srand(strtol(value, NULL, 0)); |
srand(strtol(attrs->value, NULL, 0)); | else if (!strcasecmp(type, "mtu") && value) |
else if (!strcasecmp(attrs->type, "mtu") && attrs->value) | c_mtu = strtol(value, NULL, 0); |
c_mtu = strtol(attrs->value, NULL, 0); | else if (!strcasecmp(type, "maxpps") && value) |
else if (!strcasecmp(attrs->type, "maxpps") && attrs->value) | c_maxpps = strtol(value, NULL, 0); |
c_maxpps = strtol(attrs->value, NULL, 0); | else if (!strcasecmp(type, "numgroups") && value) |
else if (!strcasecmp(attrs->type, "help")) { | c_numgroups = strtol(value, NULL, 0); |
print_help(); | else if (!strcasecmp(type, "help")) { |
exit(0); | print_help(); |
} | exit(0); |
| |
attrs = attrs->next; | |
} |
} |
} |
} |
|
|
|
static int dummy_do_init(void) |
|
{ |
|
if (attr_map_load(link_attrs, ARRAY_SIZE(link_attrs))) |
|
BUG(); |
|
|
|
return 0; |
|
} |
|
|
static int dummy_probe(void) |
static int dummy_probe(void) |
{ |
{ |
|
int i; |
|
|
|
if (c_numdev >= MAXDEVS) { |
|
fprintf(stderr, "numdev must be in range 0..%d\n", MAXDEVS); |
|
return 0; |
|
} |
|
|
|
cnts = xcalloc(cnt_size(), sizeof(uint64_t)); |
|
|
|
for (i = 0; i < c_numgroups; i++) { |
|
char groupname[32]; |
|
snprintf(groupname, sizeof(groupname), "group%02d", i); |
|
|
|
group_new_derived_hdr(groupname, groupname, DEFAULT_GROUP); |
|
} |
|
|
return 1; |
return 1; |
} |
} |
|
|
static struct input_module dummy_ops = { | static struct bmon_module dummy_ops = { |
.im_name = "dummy", | .m_name = "dummy", |
.im_read = dummy_read, | .m_do = dummy_read, |
.im_set_opts = dummy_set_opts, | .m_parse_opt = dummy_parse_opt, |
.im_probe = dummy_probe, | .m_probe = dummy_probe, |
.im_no_default = 1, | .m_init = dummy_do_init, |
}; |
}; |
|
|
static void __init dummy_init(void) |
static void __init dummy_init(void) |
{ |
{ |
register_input_module(&dummy_ops); | input_register(&dummy_ops); |
} |
} |