Skip to content

v1model core: find PHV fields and P4 objects once per program - #129

Merged
Mingyumaz merged 2 commits into
mainfrom
perf/field-lookups
Dec 2, 2026
Merged

Mingyumaz merged 2 commits into
mainfrom
perf/field-lookups

Conversation

@Mingyumaz

Copy link
Copy Markdown
Contributor

Motivation

doc/performance.md lists this as recommendation 5: the v1model core looks up the PHV fields and P4 objects it needs by name on every packet. A packet that crosses a switch costs about thirty calls such as phv->get_field("standard_metadata.egress_spec") or phv->has_field("intrinsic_metadata.mcast_grp"), plus get_parser("parser"), get_pipeline("ingress"), get_pipeline("egress") and get_deparser("deparser"). Each is a string hash and an unordered_map lookup, and in a large network the maps of many switches no longer fit in the caches. The profile put this at 3 to 4 % of the run time; measured after the change, the gain is 6.6 % for one switch and 12 to 14 % in fat trees, with the same traffic.

How it works

Once per program, not per packet. All PHVs of a program come from the same PHVFactory, so they have the same headers in the same order and every field sits at the same place. The core therefore finds each field once and keeps where it is: the index of its header in the PHV and the field's offset in that header (PhvField). On every packet it reads the field with PHV::get_field(header, offset), a vector index instead of a string hash. The parser, the two pipelines and the deparser are kept as pointers in the same struct (ProgramObjects).

When it finds them. Program(phv) fills the struct from the first packet's PHV and then returns it as long as the program stays. A program swap clears it in ProgramSwapped(). P4sim swaps only when the switch holds no packet (TrySwap()), so every PHV after the swap is the new program's, and the next packet fills the struct again. The scheduler's input and output fields (ResolveSchedulerFields(), which runs at the start and after a swap, before Program()) are found the same way.

Aliases. Some names are aliases: intrinsic_metadata.ingress_global_timestamp and queueing_metadata.enq_timestamp are fields of standard_metadata under another name. FindField() therefore gets the field by its name once and then looks for the header that holds it, instead of splitting the name.

Missing fields. Fields that the old code checked with has_field() stay optional (PhvField::Exists()). The standard fields that simple_switch requires of every v1model program (ingress_port, packet_length, instance_type, egress_spec, egress_port) are checked when the struct is filled: a program without one stops the simulation with a message, where BMv2 threw std::out_of_range on the first packet before. egress_rid is checked where multicast uses it, the queueing fields when the program has queueing metadata.

Review guide

1. model/p4-core-v1model.h

Added What
PhvField header index and offset of a field; Exists(), In(phv)
ProgramObjects the objects and the 18 fields the core uses, and the scheduler's fields; found
m_program, Program(phv), FindField(phv, name), Required(field, name) the cache and how it is filled

2. model/p4-core-v1model.cc

  • Program(), FindField(), Required() (new, after FindMetadataField()).
  • ProgramSwapped() clears the cache.
  • Every per-packet path now reads through Program(phv): ReceivePacket, HandleIngressPipeline (also the ingress clone and resubmission), EnqueueNow, EventDrivenEgressDequeue (also egress clones and recirculation), MulticastPacket, CopyFieldList, and the VOQ path (EnqueueToTrafficManager, TmTransmit). The code keeps its order and its branches; only the lookups change. No lookup by name is left in the file.

3. Test (test/p4src/metadata/, suite p4-packet): a v1model program copies ingress_global_timestamp, egress_global_timestamp and whether parser_error is PacketTooShort or NoError into the frame; timestamps.stf sends a frame that is too short for the program's last header and one that is not. No test read these fields before: the mutation run found that dropping their writes passed every test.

4. Docs: performance.md (the measurement, recommendation 5 done), TODO.md (done for v1model; the PSA, PNA and pipeline cores still look up by name), test/Readme.md (261 cases).

Measured

CPU time of the traffic phase of p4-benchmark (ns-3.39, default build, Docker, nothing else running), main and this branch taking turns, median of 5 (one switch) or 3 runs. The traffic is the same: the same packets sent, received and forwarded, the same events and simulated time.

Network main this PR faster by
one switch, 200,000 packets of 64 B 4.10 s 3.85 s 6.6 %
fat tree k=4, --load=0.1 --packets=5000 2.75 s 2.45 s 12 %
fat tree k=8, --load=0.1 --packets=500 2.79 s 2.44 s 14 %

Scope

  • In: the v1model core: the default path, clones, recirculation, resubmission, multicast and the VOQ path.
  • Out: the PSA, PNA and pipeline cores (they still look up by name; noted in TODO.md).
  • Tested (ns-3.39 and ns-3.49, Docker, on main 1ddce0c): the build has no warnings; run-tests.sh passes 24 suites with 261 test cases and 47 example runs on both versions. Every example prints the same as on main except its wall-clock times, with the same exit code and output files. Mutation testing: 12 mutants of the new code (a wrong header index or offset, a cache kept after a swap, the priority, the multicast group, the clone type, a scheduler field at the wrong index, enq_qdepth, and the timestamps and the parser error left unwritten); all are caught now, three of them only by the new timestamps.stf case. An independent review of the diff found no bugs.
  • Not tested: hardware counters (no perf in the image), so the gain is in CPU time only.

Notes for reviewers

  • The change is mechanical in the paths; the logic to check is Program() (what is required, what is optional) and that the cache is cleared on every program change (ProgramSwapped()).
  • Behaviour is unchanged except that a program without a required standard field now stops with a message instead of an exception.

Two reviews are needed before merging.

🤖 Generated with Claude Code

Mingyumaz and others added 2 commits December 2, 2026 01:31
The v1model core looked up about thirty PHV fields and four P4 objects by
name on every packet (phv->get_field("standard_metadata.egress_spec"),
get_pipeline("ingress"), ...), each a string hash and a map lookup. It now
finds them once per program, from the first packet's PHV: the parser,
pipelines and deparser, and for each field the index of its header in the
PHV and its offset there (every PHV of a program has the same headers), read
with PHV::get_field(header, offset). A program swap, which happens only when
the switch holds no packet, makes it find them again.

- ProgramObjects, PhvField, Program(), FindField() (also for aliases such as
  intrinsic_metadata.*), Required() in P4CoreV1model; the standard fields
  that simple_switch requires stop the simulation with a message if a
  program lacks them (BMv2 threw std::out_of_range before).
- All per-packet paths: ReceivePacket, the ingress, EnqueueNow, the egress,
  clones, recirculation, resubmission, multicast, the VOQ path, and the
  queue scheduler's input and output fields.
- Same traffic, CPU time of the traffic (medians, taking turns with main):
  one switch 6.6 % faster, fat trees k=4 12 % and k=8 14 %.
- Test (suite p4-packet, p4src/metadata/timestamps.stf): the ingress and
  egress timestamps and the parser error reach the program. No test read
  them before, so mutants that dropped these writes passed.
- Docs: performance.md, TODO.md, test/Readme.md (261 cases).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Conflicts:
- model/p4-core-v1model.cc, EnqueueNow(): keeps this branch's fields
  resolved once per program and adds #128's meta.timestamp.
- test/Readme.md: 265 cases (260 + 1 of this branch + 4 of #128).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Mingyumaz
Mingyumaz merged commit 24b1452 into main Dec 2, 2026
2 checks passed
Mingyumaz added a commit that referenced this pull request Dec 2, 2026
Conflicts:
- model/p4-core-v1model.cc, EnqueueNow(): keeps #129's fields resolved
  once per program and adds this branch's meta.bytes.
- test/Readme.md: 268 cases (265 on main + 3 of this branch).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Mingyumaz
Mingyumaz deleted the perf/field-lookups branch December 2, 2026 15:17
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.

1 participant