xref: /aosp_15_r20/external/perfetto/src/bigtrace/worker/worker_main.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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 <grpcpp/support/status.h>
19 #include <cstdint>
20 #include <memory>
21 
22 #include "perfetto/base/status.h"
23 #include "perfetto/ext/base/getopt.h"
24 #include "src/bigtrace/worker/repository_policies/gcs_trace_processor_loader.h"
25 #include "src/bigtrace/worker/repository_policies/local_trace_processor_loader.h"
26 #include "src/bigtrace/worker/repository_policies/trace_processor_loader.h"
27 #include "src/bigtrace/worker/worker_impl.h"
28 
29 namespace perfetto::bigtrace {
30 namespace {
31 
32 struct CommandLineOptions {
33   std::string socket;
34 };
35 
ParseCommandLineOptions(int argc,char ** argv)36 CommandLineOptions ParseCommandLineOptions(int argc, char** argv) {
37   CommandLineOptions command_line_options;
38   static option long_options[] = {{"socket", required_argument, nullptr, 's'},
39                                   {nullptr, 0, nullptr, 0}};
40   int c;
41   while ((c = getopt_long(argc, argv, "s:", long_options, nullptr)) != -1) {
42     switch (c) {
43       case 's':
44         command_line_options.socket = optarg;
45         break;
46       default:
47         PERFETTO_ELOG("Usage: %s --socket=address:port", argv[0]);
48         break;
49     }
50   }
51   return command_line_options;
52 }
53 
WorkerMain(int argc,char ** argv)54 base::Status WorkerMain(int argc, char** argv) {
55   // Setup the Worker Server
56   CommandLineOptions options = ParseCommandLineOptions(argc, argv);
57   std::string socket =
58       options.socket.empty() ? "127.0.0.1:5052" : options.socket;
59 
60   std::unordered_map<std::string, std::unique_ptr<TraceProcessorLoader>>
61       registry;
62   registry["/gcs"] = std::make_unique<GcsTraceProcessorLoader>();
63   registry["/local"] = std::make_unique<LocalTraceProcessorLoader>();
64 
65   auto service = std::make_unique<WorkerImpl>(std::move(registry));
66   grpc::ServerBuilder builder;
67   builder.RegisterService(service.get());
68   builder.AddListeningPort(socket, grpc::InsecureServerCredentials());
69   std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
70   PERFETTO_LOG("Worker server listening on %s", socket.c_str());
71 
72   server->Wait();
73 
74   return base::OkStatus();
75 }
76 
77 }  // namespace
78 }  // namespace perfetto::bigtrace
79 
main(int argc,char ** argv)80 int main(int argc, char** argv) {
81   auto status = perfetto::bigtrace::WorkerMain(argc, argv);
82   if (!status.ok()) {
83     fprintf(stderr, "%s\n", status.c_message());
84     return 1;
85   }
86   return 0;
87 }
88