1 /*
2  * Copyright (C) 2018 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 "binder/iiorap_impl.h"
18 #include "common/debug.h"
19 #include "common/loggers.h"
20 #include "common/property.h"
21 #include "db/models.h"
22 #include "manager/event_manager.h"
23 
24 #include <android-base/logging.h>
25 #include <android-base/properties.h>
26 #include <binder/IPCThreadState.h>
27 #include <server_configurable_flags/get_flags.h>
28 #include <utils/Trace.h>
29 
30 #include <stdio.h>
31 
32 static constexpr const char* kServiceName = iorap::binder::IIorapImpl::getServiceName();
33 
main(int,char ** argv)34 int main(int /*argc*/, char** argv) {
35   bool tracing_allowed = iorap::common::IsTracingEnabled(/*default_value=*/"false");
36   bool readahead_allowed = iorap::common::IsReadAheadEnabled(/*default_value*/"false");
37   if (!tracing_allowed && !readahead_allowed) {
38     LOG(INFO) << "Turn off IORap because both tracing and prefetching are off.";
39     return 0;
40   }
41 
42   if (android::base::GetBoolProperty("iorapd.log.verbose", iorap::kIsDebugBuild)) {
43     // Show verbose logs if the property is enabled or if we are a debug build.
44     setenv("ANDROID_LOG_TAGS", "*:v", /*overwrite*/ 1);
45   }
46 
47   // Logs go to system logcat.
48   android::base::InitLogging(argv, iorap::common::StderrAndLogdLogger{android::base::SYSTEM});
49 
50   LOG(INFO) << kServiceName << " (the prefetchening) firing up";
51   {
52     android::ScopedTrace trace_db_init{ATRACE_TAG_ACTIVITY_MANAGER, "IorapNativeService::db_init"};
53     iorap::db::SchemaModel db_schema =
54         iorap::db::SchemaModel::GetOrCreate(
55             android::base::GetProperty("iorapd.db.location",
56                                        "/data/misc/iorapd/sqlite.db"));
57     db_schema.MarkSingleton();
58   }
59 
60   std::shared_ptr<iorap::manager::EventManager> event_manager;
61   {
62     android::ScopedTrace trace_start{ATRACE_TAG_ACTIVITY_MANAGER, "IorapNativeService::start"};
63 
64     // TODO: use fruit for this DI.
65     event_manager =
66         iorap::manager::EventManager::Create();
67     if (!iorap::binder::IIorapImpl::Start(event_manager)) {
68       LOG(ERROR) << "Unable to start IorapNativeService";
69       exit(1);
70     }
71   }
72 
73   // This must be logged after all other initialization has finished.
74   LOG(INFO) << kServiceName << " (the prefetchening) readied up";
75 
76   event_manager->Join();  // TODO: shutdown somewhere?
77   // Block until something else shuts down the binder service.
78   android::IPCThreadState::self()->joinThreadPool();
79   LOG(INFO) << kServiceName << " shutting down";
80 
81   return 0;
82 }
83