[P4-dev] Doubts regarding counters/registers/meters usage

LJ Wobker ljw at barefootnetworks.com
Wed Jul 29 15:43:18 EDT 2015

1)       I think section 7 of the language spec (
http://p4.org/wp-content/uploads/2015/04/p4-latest.pdf) answers many of
these questions.  There are examples of counters in the open source
“switch.p4” example…. Look for the port.p4 and acl.p4 files:



2)      Counters are specifically driven by either adding packets or bytes
to them.  You can’t explicitly write a value into a counter.  For this
application, you’d want to use a register instead, where you **can**
explicitly write a constant or other available field value into the array
by calling modify_field and the index.

3)      This depends on the counter type – if it’s a packet counter the
action adds 1 to the value (it counts the one packet).  If it’s a byte
counter it adds the number of bytes to the counter.  See section 9.1
“primitive actions” in the spec.

4)      You cannot compare values that are stored in counters within P4,
but you can compare values stored in registers.

5)      A indirect counters/registers can be referenced from a table by the
name and index.  A direct counter/register is one that has a 1:1
relationship with a specific table – generally for counters, this is really
a shortcut to allow counting things that are directly related to table
entries more elegantly.   *Static* counters/meters/registers are defined
such that they can ONLY be referenced by actions in the specified table,
not any other tables.  Section 7 describes this.


*From:* P4-dev [mailto:p4-dev-bounces at p4.org] *On Behalf Of *Swaroop Thool
*Sent:* Wednesday, July 29, 2015 11:10 AM
*To:* p4-dev at p4.org
*Subject:* [P4-dev] Doubts regarding counters/registers/meters usage

Hello everyone,

I am using p4 language and bit confuse to use counters/meters/register in
my experiment code. I have few doubts:
1) How counter/register works? Is it invoked by table when we include
"count(C-REF,1)" in action of that table OR it invokes table mentioned in
its definition?
2) How to store new value in counter if I create counter array of 500 by
"instance_count : 500"? and to which location it get stored for example,
value 40 ?
3) What count(C-REF, 1) actually do and involves which value?
4) Is there any way where I can compare values among no.of different
counter arrays? and can print it?
5) Please shed light on direct and static table types in counter/register

Swaroop Thool
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.p4.org/pipermail/p4-dev_lists.p4.org/attachments/20150729/ffa53a6b/attachment-0001.html>

More information about the P4-dev mailing list