• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <grpcpp/grpcpp.h>
18 #include <cstdint>
19 #include <memory>
20 
21 #include "perfetto/base/status.h"
22 #include "perfetto/ext/trace_processor/rpc/query_result_serializer.h"
23 #include "perfetto/trace_processor/read_trace.h"
24 #include "perfetto/trace_processor/trace_processor.h"
25 #include "protos/perfetto/bigtrace/worker.grpc.pb.h"
26 #include "protos/perfetto/bigtrace/worker.pb.h"
27 
28 namespace perfetto {
29 namespace bigtrace {
30 namespace {
31 
32 class WorkerImpl final : public protos::BigtraceWorker::Service {
QueryTrace(grpc::ServerContext *,const protos::BigtraceQueryTraceArgs * args,protos::BigtraceQueryTraceResponse * response)33   grpc::Status QueryTrace(
34       grpc::ServerContext*,
35       const protos::BigtraceQueryTraceArgs* args,
36       protos::BigtraceQueryTraceResponse* response) override {
37     trace_processor::Config config;
38     std::unique_ptr<trace_processor::TraceProcessor> tp =
39         trace_processor::TraceProcessor::CreateInstance(config);
40 
41     base::Status status =
42         trace_processor::ReadTrace(tp.get(), args->trace().c_str());
43     if (!status.ok()) {
44       return grpc::Status::CANCELLED;
45     }
46 
47     auto iter = tp->ExecuteQuery(args->sql_query());
48     trace_processor::QueryResultSerializer serializer =
49         trace_processor::QueryResultSerializer(std::move(iter));
50 
51     std::vector<uint8_t> serialized;
52     for (bool has_more = true; has_more;) {
53       serialized.clear();
54       has_more = serializer.Serialize(&serialized);
55       response->add_result()->ParseFromArray(
56           serialized.data(), static_cast<int>(serialized.size()));
57     }
58 
59     return grpc::Status::OK;
60   }
61 };
62 
WorkerMain(int,char **)63 base::Status WorkerMain(int, char**) {
64   // Setup the Worker Server
65   std::string server_address("localhost:5052");
66   auto service = std::make_unique<WorkerImpl>();
67   grpc::ServerBuilder builder;
68   builder.RegisterService(service.get());
69   builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
70   std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
71   PERFETTO_LOG("Worker server listening on %s", server_address.c_str());
72 
73   server->Wait();
74 
75   return base::OkStatus();
76 }
77 
78 }  // namespace
79 }  // namespace bigtrace
80 }  // namespace perfetto
81 
main(int argc,char ** argv)82 int main(int argc, char** argv) {
83   auto status = perfetto::bigtrace::WorkerMain(argc, argv);
84   if (!status.ok()) {
85     fprintf(stderr, "%s\n", status.c_message());
86     return 1;
87   }
88   return 0;
89 }
90