xref: /aosp_15_r20/external/pigweed/pw_snapshot/proto_format.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_snapshot-proto_format:
2*61c4878aSAndroid Build Coastguard Worker
3*61c4878aSAndroid Build Coastguard Worker=====================
4*61c4878aSAndroid Build Coastguard WorkerSnapshot Proto Format
5*61c4878aSAndroid Build Coastguard Worker=====================
6*61c4878aSAndroid Build Coastguard WorkerThe Snapshot proto format depends on proto handling properties that directly
7*61c4878aSAndroid Build Coastguard Workerinform how the proto should be used. If two proto messages use field numbers
8*61c4878aSAndroid Build Coastguard Workerin a mutually exclusive manner, they can be encoded to the same buffer, and
9*61c4878aSAndroid Build Coastguard Workerdecoded independently without any errors. An example is illustrated below:
10*61c4878aSAndroid Build Coastguard Worker
11*61c4878aSAndroid Build Coastguard Worker.. code-block::
12*61c4878aSAndroid Build Coastguard Worker
13*61c4878aSAndroid Build Coastguard Worker   // This message uses field numbers 1, 2, and 3.
14*61c4878aSAndroid Build Coastguard Worker   message BasicLog {
15*61c4878aSAndroid Build Coastguard Worker     string message = 1;
16*61c4878aSAndroid Build Coastguard Worker     LogLevel level = 2;
17*61c4878aSAndroid Build Coastguard Worker     int64 timestamp = 3;
18*61c4878aSAndroid Build Coastguard Worker   }
19*61c4878aSAndroid Build Coastguard Worker
20*61c4878aSAndroid Build Coastguard Worker   // This message uses field numbers 16 and 17, which are mutually exclusive
21*61c4878aSAndroid Build Coastguard Worker   // to the numbers used in BasicLog.
22*61c4878aSAndroid Build Coastguard Worker   message ExtendedLog {
23*61c4878aSAndroid Build Coastguard Worker     string file_name = 16;
24*61c4878aSAndroid Build Coastguard Worker     uint32 line_nubmer = 17;
25*61c4878aSAndroid Build Coastguard Worker   }
26*61c4878aSAndroid Build Coastguard Worker
27*61c4878aSAndroid Build Coastguard WorkerIn the above example, a BasicLog and ExtendedLog can be encoded to the same
28*61c4878aSAndroid Build Coastguard Workerbuffer and then be decoded without causing any problems. What breaks
29*61c4878aSAndroid Build Coastguard Workerthis is when either of the two messages above are updated to use the same field
30*61c4878aSAndroid Build Coastguard Workernumber for different types. This can be ameliorated by specifying reserved
31*61c4878aSAndroid Build Coastguard Workerfield number ranges as shown below:
32*61c4878aSAndroid Build Coastguard Worker
33*61c4878aSAndroid Build Coastguard Worker.. code-block::
34*61c4878aSAndroid Build Coastguard Worker
35*61c4878aSAndroid Build Coastguard Worker   message BasicLog {
36*61c4878aSAndroid Build Coastguard Worker     string message = 1;
37*61c4878aSAndroid Build Coastguard Worker     LogLevel level = 2;
38*61c4878aSAndroid Build Coastguard Worker     int64 timestamp = 3;
39*61c4878aSAndroid Build Coastguard Worker
40*61c4878aSAndroid Build Coastguard Worker     // ExtendedLog uses these field numbers. These field numbers should never
41*61c4878aSAndroid Build Coastguard Worker     // be used by BasicLog.
42*61c4878aSAndroid Build Coastguard Worker     reserved 16 to max;
43*61c4878aSAndroid Build Coastguard Worker   }
44*61c4878aSAndroid Build Coastguard Worker
45*61c4878aSAndroid Build Coastguard Worker   message ExtendedLog {
46*61c4878aSAndroid Build Coastguard Worker     // BasicLog uses these field numbers. These field numbers should never
47*61c4878aSAndroid Build Coastguard Worker     // be used by ExtendedLog.
48*61c4878aSAndroid Build Coastguard Worker     reserved 1 to 15;
49*61c4878aSAndroid Build Coastguard Worker
50*61c4878aSAndroid Build Coastguard Worker     string file_name = 16;
51*61c4878aSAndroid Build Coastguard Worker     uint32 line_nubmer = 17;
52*61c4878aSAndroid Build Coastguard Worker   }
53*61c4878aSAndroid Build Coastguard Worker
54*61c4878aSAndroid Build Coastguard WorkerThis is exactly how the Snapshot proto is set up. While a SnapshotMetadata proto
55*61c4878aSAndroid Build Coastguard Workermessage provides a good portion of the valuable snapshot contents, the larger
56*61c4878aSAndroid Build Coastguard WorkerSnapshot message specifies field numbers and reserved field ranges to clarify
57*61c4878aSAndroid Build Coastguard Workerwhich field numbers are left to a project for declaring a mutually exclusive
58*61c4878aSAndroid Build Coastguard Workerproject-specific proto message that can be used to augment the data natively
59*61c4878aSAndroid Build Coastguard Workersupported by the upstream proto.
60*61c4878aSAndroid Build Coastguard Worker
61*61c4878aSAndroid Build Coastguard Worker--------------------
62*61c4878aSAndroid Build Coastguard WorkerModule-Specific Data
63*61c4878aSAndroid Build Coastguard Worker--------------------
64*61c4878aSAndroid Build Coastguard WorkerSome upstream Pigweed modules provide a ``Snapshot*`` message that overlays the
65*61c4878aSAndroid Build Coastguard Workermain Snapshot message. These messages share a field number with the Snapshot
66*61c4878aSAndroid Build Coastguard Workerproto, which lets modules provide a helper that populates a subset of the
67*61c4878aSAndroid Build Coastguard WorkerSnapshot proto without explicitly depending on the Snapshot proto.
68*61c4878aSAndroid Build Coastguard Worker
69*61c4878aSAndroid Build Coastguard WorkerExample:
70*61c4878aSAndroid Build Coastguard Worker.. code-block::
71*61c4878aSAndroid Build Coastguard Worker
72*61c4878aSAndroid Build Coastguard Worker   // snapshot.proto
73*61c4878aSAndroid Build Coastguard Worker   message Snapshot {
74*61c4878aSAndroid Build Coastguard Worker     ...
75*61c4878aSAndroid Build Coastguard Worker     // Information about allocated Thread.
76*61c4878aSAndroid Build Coastguard Worker     repeated pw.thread.Thread threads = 18;
77*61c4878aSAndroid Build Coastguard Worker   }
78*61c4878aSAndroid Build Coastguard Worker
79*61c4878aSAndroid Build Coastguard Worker   // thread.proto
80*61c4878aSAndroid Build Coastguard Worker
81*61c4878aSAndroid Build Coastguard Worker   // This message overlays the pw.snapshot.Snapshot proto. It's valid to encode
82*61c4878aSAndroid Build Coastguard Worker   // this message to the same sink that a Snapshot proto is being written to.
83*61c4878aSAndroid Build Coastguard Worker   message SnapshotThread {
84*61c4878aSAndroid Build Coastguard Worker     // Thread information.
85*61c4878aSAndroid Build Coastguard Worker     repeated pw.thread.Thread threads = 18;
86*61c4878aSAndroid Build Coastguard Worker   }
87*61c4878aSAndroid Build Coastguard Worker
88*61c4878aSAndroid Build Coastguard WorkerIt is **critical** that the SnapshotThread message is in sync with the larger
89*61c4878aSAndroid Build Coastguard WorkerSnapshot proto. If the type or field numbers are different, the proto decode
90*61c4878aSAndroid Build Coastguard Workerwill fail. If the semantics are different, the decode will likely succeed but
91*61c4878aSAndroid Build Coastguard Workermight provide misleading information.
92