• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
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  * Description: TraceOps implements
16  */
17 #include "trace_ops.h"
18 
19 #include <algorithm>
20 #include <cerrno>
21 #include <csignal>
22 #include <cstdio>
23 #include <cstring>
24 #include <fcntl.h>
25 #include <functional>
26 #include <memory>
27 #include <sstream>
28 #include <unistd.h>
29 
30 #include "logging.h"
31 #include "process_utils.h"
32 #include "string_utils.h"
33 
34 FTRACE_NS_BEGIN
TraceOps(const std::string & path,const std::string & arg0,TraceType type)35 TraceOps::TraceOps(const std::string& path, const std::string& arg0, TraceType type)
36     : arg0_(arg0), bin_(path), type_(type)
37 {
38 }
39 
~TraceOps()40 TraceOps::~TraceOps() {}
41 
GetCommand() const42 std::string TraceOps::GetCommand() const
43 {
44     return bin_;
45 }
46 
GetTraceType() const47 TraceOps::TraceType TraceOps::GetTraceType() const
48 {
49     return type_;
50 }
51 
IsSupported()52 bool TraceOps::IsSupported()
53 {
54     return access(bin_.c_str(), F_OK | X_OK) == 0;
55 }
56 
HasCategory(const std::string & name)57 bool TraceOps::HasCategory(const std::string& name)
58 {
59     if (supportedCategories_.empty()) {
60         supportedCategories_ = ListCategories();
61         std::sort(supportedCategories_.begin(), supportedCategories_.end());
62     }
63     return std::binary_search(supportedCategories_.begin(), supportedCategories_.end(), name);
64 }
65 
ListCategories()66 std::vector<std::string> TraceOps::ListCategories()
67 {
68     std::vector<std::string> result;
69     args_ = {arg0_};
70     CHECK_TRUE(PrepareListCategoriesCmd(), result, "prepare list categories command failed!");
71 
72     int retval = ExecuteCommand(true, true);
73     PROFILER_LOG_INFO(LOG_CORE, "exec %s exit %d!", bin_.c_str(), retval);
74 
75     std::string line;
76     std::stringstream stream(output_);
77     while (std::getline(stream, line)) {
78         line = StringUtils::Strip(line);
79 
80         // format likes: freq - CPU Frequency
81         auto pos = line.find('-');
82         if (pos == std::string::npos) {
83             continue;
84         }
85 
86         auto category = line.substr(0, pos - 1);
87         result.push_back(category);
88     }
89     return result;
90 }
91 
PrepareListCategoriesCmd()92 bool TraceOps::PrepareListCategoriesCmd()
93 {
94     return false;
95 }
96 
PrepareEnableCategoriesCmd(int traceTime)97 bool TraceOps::PrepareEnableCategoriesCmd(int traceTime)
98 {
99     return false;
100 }
101 
PrepareDisableCategoriesCmd()102 bool TraceOps::PrepareDisableCategoriesCmd()
103 {
104     return false;
105 }
106 
SetTraceClock(const std::string & traceClock)107 void TraceOps::SetTraceClock(const std::string& traceClock)
108 {
109     traceClock_ = traceClock;
110 }
111 
EnableCategories(const std::vector<std::string> & categories,int traceTime)112 bool TraceOps::EnableCategories(const std::vector<std::string>& categories, int traceTime)
113 {
114     CHECK_TRUE(categories.size() > 0, false, "categories empty!");
115     for (auto& category : categories) {
116         if (HasCategory(category)) {
117             targetCategories_.push_back(category);
118         } else {
119             PROFILER_LOG_ERROR(LOG_CORE, "\"%s\" is not support category on this device", category.c_str());
120         }
121     }
122 
123     args_ = {arg0_};
124     if (!traceClock_.empty()) {
125         args_.push_back("--trace_clock");
126         args_.push_back(traceClock_);
127     }
128     CHECK_TRUE(PrepareEnableCategoriesCmd(traceTime), false, "prepare enable categories failed!");
129 
130     int retval = ExecuteCommand();
131     PROFILER_LOG_INFO(LOG_CORE, "exec %s exit %d!", bin_.c_str(), retval);
132     CHECK_TRUE(retval == 0, false, "exec %s failed with %d", bin_.c_str(), retval);
133     return true;
134 }
135 
DisableCategories()136 bool TraceOps::DisableCategories()
137 {
138     args_ = {arg0_};
139     CHECK_TRUE(PrepareDisableCategoriesCmd(), false, "prepare enable categories failed!");
140 
141     int retval = ExecuteCommand();
142     PROFILER_LOG_INFO(LOG_CORE, "exec %s exit %d!", bin_.c_str(), retval);
143 
144     targetCategories_.clear();
145     CHECK_TRUE(retval == 0, false, "exec %s failed with %d", bin_.c_str(), retval);
146     return true;
147 }
148 
ExecuteCommand(bool out2pipe,bool err2pipe)149 int TraceOps::ExecuteCommand(bool out2pipe, bool err2pipe)
150 {
151     CHECK_TRUE(bin_.size() > 0, -1, "bin_ empty");
152     CHECK_TRUE(args_.size() > 0, -1, "args_ empty");
153 
154     ExecuteArgs execArgs = {};
155     execArgs.bin_ = bin_;
156     execArgs.argv_ = args_;
157     execArgs.out2pipe_ = out2pipe;
158     execArgs.err2pipe_ = err2pipe;
159 
160     std::string cmdline = StringUtils::Join(args_, " ");
161     int retval = ProcessUtils::Execute(execArgs, output_);
162     return retval;
163 }
164 FTRACE_NS_END
165