1 /**
2  * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
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  */
15 
16 #include "macros.h"
17 #include "features_manager.h"
18 #include "dprof/storage.h"
19 #include "utils/logger.h"
20 #include "utils/pandargs.h"
21 #include "utils/span.h"
22 #include "features/hotness_counters.h"
23 
24 #include <iostream>
25 
26 #include "generated/converter_options.h"
27 
28 namespace ark::dprof {
29 class ArgsParser {
30 public:
Parse(ark::Span<const char *> args)31     bool Parse(ark::Span<const char *> args)
32     {
33         options_.AddOptions(&parser_);
34         if (!parser_.Parse(args.Size(), args.Data())) {
35             std::cerr << parser_.GetErrorString();
36             return false;
37         }
38         auto err = options_.Validate();
39         if (err) {
40             std::cerr << err.value().GetMessage() << std::endl;
41             return false;
42         }
43         if (options_.GetStorageDir().empty()) {
44             std::cerr << "Option \"storage-dir\" is not set" << std::endl;
45             return false;
46         }
47         return true;
48     }
49 
GetOptionos() const50     const Options &GetOptionos() const
51     {
52         return options_;
53     }
54 
Help() const55     void Help() const
56     {
57         std::cerr << "Usage: " << appName_ << " [OPTIONS]" << std::endl;
58         std::cerr << "optional arguments:" << std::endl;
59         std::cerr << parser_.GetHelpString() << std::endl;
60     }
61 
62 private:
63     std::string appName_;
64     PandArgParser parser_;
65     Options options_ {""};
66 };
67 
Main(ark::Span<const char *> args)68 int Main(ark::Span<const char *> args)
69 {
70     ArgsParser parser;
71     if (!parser.Parse(args)) {
72         parser.Help();
73         return -1;
74     }
75     const Options &options = parser.GetOptionos();
76 
77     Logger::InitializeStdLogging(Logger::LevelFromString(options.GetLogLevel()), ark::LOGGER_COMPONENT_MASK_ALL);
78 
79     auto storage = AppDataStorage::Create(options.GetStorageDir());
80     if (!storage) {
81         LOG(FATAL, DPROF) << "Cannot init storage";
82         return -1;
83     }
84 
85     FeaturesManager fm;
86     HCountersFunctor hcountersFunctor(std::cout);
87     if (!fm.RegisterFeature(HCOUNTERS_FEATURE_NAME, hcountersFunctor)) {
88         LOG(FATAL, DPROF) << "Cannot register feature: " << HCOUNTERS_FEATURE_NAME;
89         return -1;
90     }
91 
92     storage->ForEachApps([&fm](std::unique_ptr<AppData> &&appData) -> bool { return fm.ProcessingFeatures(*appData); });
93 
94     if (hcountersFunctor.ShowInfo(options.GetFormat())) {
95         return -1;
96     }
97     return 0;
98 }
99 }  // namespace ark::dprof
100 
main(int argc, const char *argv[])101 int main(int argc, const char *argv[])
102 {
103     ark::Span<const char *> args(argv, argc);
104     return ark::dprof::Main(args);
105 }
106