Skip to content

gcc build warning, switched to catch a reference#2

Open
bryancall wants to merge 1 commit into
pollere:masterfrom
bryancall:catch_warning
Open

gcc build warning, switched to catch a reference#2
bryancall wants to merge 1 commit into
pollere:masterfrom
bryancall:catch_warning

Conversation

@bryancall
Copy link
Copy Markdown

Error message when building:

09:41:25 zeus:(master)~/dev/pping$ make
g++ -I/home/bcall/src/libtins/include -std=c++14 -g -O3 -Wall -o pping pping.cpp -L/home/bcall/src/libtins/lib -ltins -lpcap
pping.cpp: In function ‘tsInfo* getTStm(const std::string&)’:
pping.cpp:175:19: warning: catching polymorphic type ‘class std::out_of_range’ by value [-Wcatch-value=]
  175 |     } catch (std::out_of_range) {
      |                   ^~~~~~~~~~~~
09:41:28 zeus:(master)~/dev/pping$ gcc --version
gcc (GCC) 13.2.1 20230728 (Red Hat 13.2.1-1)
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

@dagelf
Copy link
Copy Markdown

dagelf commented Apr 20, 2024

LGTM

nshopik referenced this pull request in nshopik/pping2 May 5, 2026
Hot per-packet path was bottlenecked on std::string allocation: 5-6
string-keyed map lookups + ~15-25 short-lived std::string construct/
destroy/concatenate/heap calls per packet, dominated by the cost of
turning libtins addresses into "src:port+dst:port" keys.

This rewrite replaces the keys with packed POD structs whose raw bytes
are hashed directly:

- FlowKey: 40B (16B src + 16B dst + 2B sport + 2B dport + 1B af + 3B
  pad). v4 addresses live in the first 4 bytes of the 16B slot, with
  the trailing 12 bytes zero. The af field disambiguates v4 from v6
  packets that happen to share the first 4 IP bytes.
- TsKey: FlowKey + 4B tsval + 4B pad = 48B.
- ByteHash: FNV-1a over sizeof(T) raw bytes. Padding participates,
  so default member initializers + an explicit _pad array are used to
  guarantee the pad bytes are zero on every key. static_asserts on
  sizeof guard the layout.

The hot-path lookup count drops to:
  1x flows.try_emplace(fk)   forward flow
  1x tsTbl.try_emplace(tk)   store our TSval
  1x tsTbl.find(rk+ecr)      match against reverse flow's saved TSval
  1x flows.find(rk)          (only on RTT match) update reverse bytesDep
  1x flows.find(rk)          (only on new flow) bidirectional setup

IP byte conversion (IPv4Address::to_string / IPv6Address::to_string)
is deferred to the print path and only happens on actual RTT matches.

tsTbl now stores tsInfo by value instead of tsInfo*. This folds the
TODO #2 fix in (the duplicate-key insert path used to leak ti when
try_emplace returned false; with by-value storage the leak is
structurally impossible). cleanUp loses its delete on tsInfo.

Other changes:
- localIP gets a parallel localIPBytes/localIPaf representation
  parsed once at startup so the filtLocal check is a 16-byte memcmp
  instead of a std::string compare.
- flowRec drops its flowname field (unused after the rewrite); the
  flow-limit-reached log message uses a flowKeyName(FlowKey) helper.
- getTStm wrapper is gone — process_packet calls tsTbl.find directly
  and reads/mutates eit->second in place.
- Bump CXXFLAGS -std=c++14 -> c++17. try_emplace and structured
  bindings (in the plan's pseudocode and used here) need it; the old
  __cpp_lib_unordered_map_try_emplace fallback is removed.

Tests:
- Migrated test_addTS and test_cleanUp to FlowKey/TsKey + by-value.
- New test_flowkey_padding (the load-bearing one — verifies sizeof,
  pad bytes are zero, and two independently-built equal keys have
  identical bytes and ByteHash output).
- New test_flowkey_reversed (round-trip and field swaps).
- New test_flowkey_v4_v6_disambig (same first 4 IP bytes + different
  af must NOT compare or hash equal).
- New test_addTS_no_leak_on_duplicate (asserts size/first-write-wins
  on the dup-key path that used to leak).

The pcap integration test (test/known.pcap.golden) and the -m/-e
format regression test are unchanged and still gate the output format.

TODOS.md: drop the two now-resolved items (tsTbl bound was already
done in 2a6fc72; tsInfo leak is fully resolved here).

Benchmark on bench.pcap (232,718 packets, warm page cache, median of
6 runs):

  master (4b8c778):           3495 ns/pkt, 0.286 Mpps
  this commit (rewrite only): 2906 ns/pkt, 0.344 Mpps   (1.20x)

Below the plan's 3-5x target. perf record on the warm path showed
libtins parsing + TCP option exception handling dominating the
remaining cost. The exception path is addressed in a separate commit
on this branch (search_option for the TCP timestamp option) which
brings the median to ~1944 ns/pkt — 1.80x vs master overall. The
larger libtins replacement remains a deferred PR per the plan's
NOT-in-scope section.

Plan: ~/.gstack/projects/pollere-pping/bill-master-eng-review-plan-20260505.md

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants