• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (C) 2020 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15from google.protobuf import descriptor_pb2
16from google.protobuf import message_factory
17from google.protobuf.descriptor_pool import DescriptorPool
18
19from perfetto.trace_processor.platform import PlatformDelegate
20
21
22class ProtoFactory:
23
24  def __init__(self, platform_delegate: PlatformDelegate):
25    # Declare descriptor pool
26    self.descriptor_pool = DescriptorPool()
27
28    # Load trace processor descriptor and add to descriptor pool
29    tp_desc = platform_delegate.get_resource('trace_processor.descriptor')
30    tp_file_desc_set_pb2 = descriptor_pb2.FileDescriptorSet()
31    tp_file_desc_set_pb2.MergeFromString(tp_desc)
32
33    for f_desc_pb2 in tp_file_desc_set_pb2.file:
34      self.descriptor_pool.Add(f_desc_pb2)
35
36    # Load metrics descriptor and add to descriptor pool
37    metrics_desc = platform_delegate.get_resource('metrics.descriptor')
38    metrics_file_desc_set_pb2 = descriptor_pb2.FileDescriptorSet()
39    metrics_file_desc_set_pb2.MergeFromString(metrics_desc)
40
41    for f_desc_pb2 in metrics_file_desc_set_pb2.file:
42      self.descriptor_pool.Add(f_desc_pb2)
43
44    def create_message_factory(message_type):
45      message_desc = self.descriptor_pool.FindMessageTypeByName(message_type)
46      if hasattr(message_factory, 'GetMessageClass'):
47        return message_factory.GetMessageClass(message_desc)
48      return message_factory.MessageFactory().GetPrototype(message_desc)
49
50    # Create proto messages to correctly communicate with the RPC API by sending
51    # and receiving data as protos
52    self.AppendTraceDataResult = create_message_factory(
53        'perfetto.protos.AppendTraceDataResult')
54    self.StatusResult = create_message_factory('perfetto.protos.StatusResult')
55    self.ComputeMetricArgs = create_message_factory(
56        'perfetto.protos.ComputeMetricArgs')
57    self.ComputeMetricResult = create_message_factory(
58        'perfetto.protos.ComputeMetricResult')
59    self.QueryArgs = create_message_factory('perfetto.protos.QueryArgs')
60    self.QueryResult = create_message_factory('perfetto.protos.QueryResult')
61    self.TraceMetrics = create_message_factory('perfetto.protos.TraceMetrics')
62    self.DisableAndReadMetatraceResult = create_message_factory(
63        'perfetto.protos.DisableAndReadMetatraceResult')
64    self.CellsBatch = create_message_factory(
65        'perfetto.protos.QueryResult.CellsBatch')
66