When adding ports to bridges with many existing ports, bridge reconfiguration was spending significant CPU time in repeated calls to ofproto_port_open_type() during MTU calculations.
For N existing ports, this resulted in O(N) calls to ofproto_port_open_type() per port addition. perf shows: ... + 75.47% 0.00% ovs-vswitchd ovs-vswitchd [.] ofproto_run + 66.17% 0.00% ovs-vswitchd ovs-vswitchd [.] update_mtu + 65.77% 7.27% ovs-vswitchd ovs-vswitchd [.] find_min_mtu + 65.77% 0.00% ovs-vswitchd ovs-vswitchd [.] update_mtu_ofproto (inlined) + 48.75% 1.38% ovs-vswitchd ovs-vswitchd [.] ofport_is_mtu_overridden + 48.56% 0.00% ovs-vswitchd ovs-vswitchd [.] ofport_is_internal_or_patch (inlined) + 37.19% 2.21% ovs-vswitchd ovs-vswitchd [.] dpif_port_open_type + 37.19% 0.00% ovs-vswitchd ovs-vswitchd [.] ofproto_port_open_type (inlined) ... With 5k existing ports, adding a new port took 5.149s. After this patch, it takes 3.201s for the same. Signed-off-by: Han Zhou <[email protected]> --- ofproto/ofproto.c | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/ofproto/ofproto.c b/ofproto/ofproto.c index ef615e59c354..d00939bad1cd 100644 --- a/ofproto/ofproto.c +++ b/ofproto/ofproto.c @@ -2953,21 +2953,26 @@ init_ports(struct ofproto *p) return 0; } +/* If 'port' is internal or patch and if the user didn't explicitly specify an + * mtu through the database, we have to override it. Uses cached port type + * values to avoid repeated ofproto_port_open_type() calls. */ static bool -ofport_is_internal_or_patch(const struct ofproto *p, const struct ofport *port) +ofport_is_mtu_overridden_cached(const struct ofport *port, + const char *internal_type, + const char *patch_type) { const char *netdev_type = netdev_get_type(port->netdev); - return !strcmp(netdev_type, ofproto_port_open_type(p, "internal")) || - !strcmp(netdev_type, ofproto_port_open_type(p, "patch")); + bool is_internal_or_patch = !strcmp(netdev_type, internal_type) || + !strcmp(netdev_type, patch_type); + return is_internal_or_patch && !netdev_mtu_is_user_config(port->netdev); } -/* If 'port' is internal or patch and if the user didn't explicitly specify an - * mtu through the database, we have to override it. */ static bool ofport_is_mtu_overridden(const struct ofproto *p, const struct ofport *port) { - return ofport_is_internal_or_patch(p, port) - && !netdev_mtu_is_user_config(port->netdev); + const char *internal_type = ofproto_port_open_type(p, "internal"); + const char *patch_type = ofproto_port_open_type(p, "patch"); + return ofport_is_mtu_overridden_cached(port, internal_type, patch_type); } /* Find the minimum MTU of all non-overridden devices attached to 'p'. @@ -2978,12 +2983,17 @@ find_min_mtu(struct ofproto *p) struct ofport *ofport; int mtu = 0; + const char *internal_type = ofproto_port_open_type(p, "internal"); + const char *patch_type = ofproto_port_open_type(p, "patch"); + HMAP_FOR_EACH (ofport, hmap_node, &p->ports) { struct netdev *netdev = ofport->netdev; int dev_mtu; - /* Skip any overridden port, since that's what we're trying to set. */ - if (ofport_is_mtu_overridden(p, ofport)) { + /* Skip any overridden port, since that's what we're trying to set. + * Use cached port types to avoid repeated lookups. */ + if (ofport_is_mtu_overridden_cached(ofport, internal_type, + patch_type)) { continue; } @@ -3037,10 +3047,15 @@ update_mtu_ofproto(struct ofproto *p) return; } + const char *internal_type = ofproto_port_open_type(p, "internal"); + const char *patch_type = ofproto_port_open_type(p, "patch"); + HMAP_FOR_EACH (ofport, hmap_node, &p->ports) { struct netdev *netdev = ofport->netdev; - if (ofport_is_mtu_overridden(p, ofport)) { + /* Use cached port types to avoid repeated lookups */ + if (ofport_is_mtu_overridden_cached(ofport, internal_type, + patch_type)) { if (!netdev_set_mtu(netdev, p->min_mtu)) { ofport->mtu = p->min_mtu; } -- 2.38.1 _______________________________________________ dev mailing list [email protected] https://mail.openvswitch.org/mailman/listinfo/ovs-dev
