From 23fc58904455047ac2bbdb44b26839e127bf7fcb Mon Sep 17 00:00:00 2001 From: Brenden Blanco Date: Wed, 23 Aug 2017 15:15:32 -0700 Subject: [PATCH] Update after lookup in map.increment for HASH types Performing the update before lookup incurs an extra spinlock in the kernel, which hurts performance. Reorder the code and increment to 1. The memset is there I believe due to some type mismatch warnings, so I'm leaving as is rather than doing a direct assign to 1. The resulting code is optimized away anyway. Fixes: #1314 Signed-off-by: Brenden Blanco --- src/cc/frontends/clang/b_frontend_action.cc | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/cc/frontends/clang/b_frontend_action.cc b/src/cc/frontends/clang/b_frontend_action.cc index c6218ff4..29e6d318 100644 --- a/src/cc/frontends/clang/b_frontend_action.cc +++ b/src/cc/frontends/clang/b_frontend_action.cc @@ -332,12 +332,14 @@ bool BTypeVisitor::VisitCallExpr(CallExpr *Call) { string lookup = "bpf_map_lookup_elem_(bpf_pseudo_fd(1, " + fd + ")"; string update = "bpf_map_update_elem_(bpf_pseudo_fd(1, " + fd + ")"; txt = "({ typeof(" + name + ".key) _key = " + arg0 + "; "; + txt += "typeof(" + name + ".leaf) *_leaf = " + lookup + ", &_key); "; + txt += "if (_leaf) (*_leaf)++; "; if (desc->second.type == BPF_MAP_TYPE_HASH) { - txt += "typeof(" + name + ".leaf) _zleaf; memset(&_zleaf, 0, sizeof(_zleaf)); "; - txt += update + ", &_key, &_zleaf, BPF_NOEXIST); "; + txt += "else { typeof(" + name + ".leaf) _zleaf; memset(&_zleaf, 0, sizeof(_zleaf)); "; + txt += "_zleaf++; "; + txt += update + ", &_key, &_zleaf, BPF_NOEXIST); } "; } - txt += "typeof(" + name + ".leaf) *_leaf = " + lookup + ", &_key); "; - txt += "if (_leaf) (*_leaf)++; })"; + txt += "})"; } else if (memb_name == "perf_submit") { string name = Ref->getDecl()->getName(); string arg0 = rewriter_.getRewrittenText(expansionRange(Call->getArg(0)->getSourceRange())); -- 2.34.1