• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2023. 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 
16 #include "native_memory_profiler_sa_client_manager.h"
17 
18 #include <cstdio>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <iostream>
23 #include <unistd.h>
24 #include <sstream>
25 
26 using namespace OHOS::Developtools::NativeDaemon;
27 
28 namespace {
TestDumpFile(const std::string postfix="")29 static uint32_t TestDumpFile(const std::string postfix = "")
30 {
31     uint32_t fd = static_cast<uint32_t>(open(("/data/local/tmp/test_dump_file" + postfix + ".htrace").c_str(),
32                                              O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
33     return fd;
34 }
35 
IsNumeric(const std::string & str)36 bool IsNumeric(const std::string& str)
37 {
38     std::istringstream iss(str);
39     int number;
40     char trailingCharacter;
41     if (!(iss >> number)) {
42         return false;
43     }
44     if (iss >> trailingCharacter) {
45         return false;
46     }
47     return true;
48 }
49 }
50 
main(int32_t argc,char * argv[])51 int32_t main(int32_t argc, char* argv[])
52 {
53     if (argc > 50) { // 50: max args size
54         printf("error too many args.\n");
55         return 0;
56     }
57     std::shared_ptr<NativeMemoryProfilerSaConfig> config = std::make_shared<NativeMemoryProfilerSaConfig>();
58     bool start = false;
59     bool stop = false;
60     bool error = false;
61     bool dumpData = false;
62     for (int32_t i = 1; i < argc; ++i) {
63         std::string arg(argv[i]);
64         if (arg == "--help" || arg == "-h") {
65             printf("    --start                             -s : start, default: false\n");
66             printf("    --stop                              -k : stop, default: false\n");
67             printf("    --pid                               -p : pid\n");
68             printf("    --filePath                          -f : filePath, default: ");
69             printf("/data/local/tmp/hiprofiler_data.htrace\n");
70             printf("    --duration                          -d : duration, default: 20s\n");
71             printf("    --filterSize                        -fs : filterSize, default: 0\n");
72             printf("    --shareMemorySize                   -sms : shareMemorySize, default: 16384\n");
73             printf("    --processName                       -pn : processName\n");
74             printf("    --maxStackDepth                     -msd : maxStackDepth, default: 30\n");
75             printf("    --mallocDisable                     -mad : mallocDisable, default: false\n");
76             printf("    --mmapDisable                       -mmd : mmapDisable, default: false\n");
77             printf("    --freeStackData                     -fsd : freeStackData, default: false\n");
78             printf("    --munmapStackData                   -musd : munmapStackData, default: false\n");
79             printf("    --mallocFreeMatchingInterval        -mfmi : mallocFreeMatchingInterval\n");
80             printf("    --mallocFreeMatchingCnt             -mfmc : mallocFreeMatchingCnt\n");
81             printf("    --disable_stringCompressed          -sc : disable_stringCompressed, ");
82             printf("default: stringCompressed\n");
83             printf("    --dwarf                             -df : dwarf unwind, default: fp\n");
84             printf("    --disable_blocked                   -b : disable_blocked, default: blocked\n");
85             printf("    --disable_recordAccurately          -ra : disable_recordAccurately, ");
86             printf("default: recordAccurately\n");
87             printf("    --startupMode                       -sm : startupMode, default: false\n");
88             printf("    --memtraceEnable                    -me : memtraceEnable, default: false\n");
89             printf("    --offlineSymbolization              -os : offlineSymbolization, default: false\n");
90             printf("    --callframeCompress                 -cc : callframeCompress, default: false\n");
91             printf("    --statisticsInterval                -si : statisticsInterval\n");
92             printf("    --clockId                           -c : clockId\n");
93             printf("    --dumpData                          -dd : dump data\n");
94             printf("    --sampleInterval                    -spi : sampleInterval, default: 0\n");
95             printf("    --jsStackReport                     -jr : jsStackReport, default: 0\n");
96             printf("    --maxJsStackDepth                   -mjsd : maxJsStackDepth, default: 0\n");
97             printf("    --filterNapiName                    -fnapi : filterNapiName \n");
98             return 0;
99         }
100 
101         if ((arg == "--start") || (arg == "-s")) {
102             start = true;
103         } else if ((arg == "--stop") || (arg == "-k")) {
104             stop = true;
105         } else if ((arg == "--pid") || (arg == "-p")) {
106             config->pid_ = i + 1 < argc && IsNumeric(argv[i + 1]) ? std::stoi(argv[++i]) : 0;
107         } else if ((arg == "--filePath") || (arg == "-f")) {
108             config->filePath_ = i + 1 < argc ? std::string(argv[++i]) : "";
109         } else if ((arg == "--duration") || (arg == "-d")) {
110             config->duration_ =
111                 i + 1 < argc && IsNumeric(argv[i + 1]) ? static_cast<uint32_t>(std::stoi(argv[++i])) : 0;
112         } else if ((arg == "--filterSize") || (arg == "-fs")) {
113             config->filterSize_ = i + 1 < argc && IsNumeric(argv[i + 1]) ? std::stoi(argv[++i]) : 0;
114         } else if ((arg == "--shareMemorySize") || (arg == "-sms")) {
115             config->shareMemorySize_ =
116                 i + 1 < argc && IsNumeric(argv[i + 1]) ? static_cast<uint32_t>(std::stoi(argv[++i])) : 0;
117         } else if ((arg == "--processName") || (arg == "-pn")) {
118             config->processName_ = i + 1 < argc ? std::string(argv[++i]) : "";
119         } else if ((arg == "--maxStackDepth") || (arg == "-msd")) {
120             config->maxStackDepth_ =
121                 i + 1 < argc && IsNumeric(argv[i + 1]) ? static_cast<uint8_t>(std::stoi(argv[++i])) : 0;
122         } else if ((arg == "--mallocDisable") || (arg == "-mad")) {
123             config->mallocDisable_ = true;
124         } else if ((arg == "--mmapDisable") || (arg == "-mmd")) {
125             config->mmapDisable_ = true;
126         } else if ((arg == "--freeStackData") || (arg == "-fsd")) {
127             config->freeStackData_ = true;
128         } else if ((arg == "--munmapStackData") || (arg == "-musd")) {
129             config->munmapStackData_ = true;
130         } else if ((arg == "--mallocFreeMatchingInterval") || (arg == "-mfmi")) {
131             config->mallocFreeMatchingInterval_ =
132                 i + 1 < argc && IsNumeric(argv[i + 1]) ? static_cast<uint32_t>(std::stoi(argv[++i])) : 0;
133         } else if ((arg == "--mallocFreeMatchingCnt") || (arg == "-mfmc")) {
134             config->mallocFreeMatchingCnt_ =
135                 i + 1 < argc && IsNumeric(argv[i + 1]) ? static_cast<uint32_t>(std::stoi(argv[++i])) : 0;
136         } else if ((arg == "--disable_stringCompressed") || (arg == "-sc")) {
137             config->stringCompressed_ = false;
138         } else if ((arg == "--dwarf") || (arg == "-df")) {
139             config->fpUnwind_ = false;
140         } else if ((arg == "--disable_blocked") || (arg == "-b")) {
141             config->blocked_ = false;
142         } else if ((arg == "--disable_recordAccurately") || (arg == "-ra")) {
143             config->recordAccurately_ = false;
144         } else if ((arg == "--startupMode") || (arg == "-sm")) {
145             config->startupMode_ = true;
146         } else if ((arg == "--memtraceEnable") || (arg == "-me")) {
147             config->memtraceEnable_ = true;
148         } else if ((arg == "--offlineSymbolization") || (arg == "-os")) {
149             config->offlineSymbolization_ = true;
150         } else if ((arg == "--callframeCompress") || (arg == "-cc")) {
151             config->callframeCompress_ = true;
152         } else if ((arg == "--statisticsInterval") || (arg == "-si")) {
153             config->statisticsInterval_ =
154                 i + 1 < argc && IsNumeric(argv[i + 1]) ? static_cast<uint32_t>(std::stoi(argv[++i])) : 0;
155         } else if ((arg == "--clockId") || (arg == "-c")) {
156             config->clockId_ = i + 1 < argc && IsNumeric(argv[i + 1]) ? std::stoi(argv[++i]) : 0;
157         } else if ((arg == "--dumpData") || (arg == "-dd")) {
158             dumpData = true;
159         } else if ((arg == "--sampleInterval ") || (arg == "-spi")) {
160             config->sampleInterval_ =
161                 i + 1 < argc && IsNumeric(argv[i + 1]) ? static_cast<uint32_t>(std::stoi(argv[++i])) : 0;
162         } else if ((arg == "--responseLibraryMode") || (arg == "-r")) {
163             config->responseLibraryMode_ = true;
164         } else if ((arg == "--printNmd") || (arg == "-nmd")) {
165             config->printNmd_ = true;
166         } else if ((arg == "--jsStackReport") || (arg == "-jr")) {
167             config->jsStackReport_ =
168                 i + 1 < argc && IsNumeric(argv[i + 1]) ? static_cast<int32_t>(std::stoi(argv[++i])) : 0;
169         } else if ((arg == "--maxJsStackDepth") || (arg == "-mjsd")) {
170             config->maxJsStackDepth_ =
171                 i + 1 < argc && IsNumeric(argv[i + 1]) ? static_cast<uint8_t>(std::stoi(argv[++i])) : 0;
172         } else if ((arg == "--filterNapiName") || (arg == "-fnapi")) {
173             config->filterNapiName_ = i + 1 < argc ? std::string(argv[i + 1]) : "";
174         } else {
175             printf("error arg: %s\n", arg.c_str());
176             error = true;
177             break;
178         }
179     }
180 
181     if (error) {
182         return 0;
183     }
184 
185     if (start) {
186         std::cout << "start....." << std::endl;
187         if (dumpData) {
188             uint32_t fd = TestDumpFile();
189             NativeMemoryProfilerSaClientManager::DumpData(fd, config);
190             close(fd);
191         } else if (config->printNmd_) {
192             uint32_t fdFirst = TestDumpFile(std::to_string(0));
193             NativeMemoryProfilerSaClientManager::GetMallocStats(fdFirst, config->pid_, 0);
194             close(fdFirst);
195         } else {
196             NativeMemoryProfilerSaClientManager::Start(config);
197         }
198     } else if (stop) {
199         std::cout << "stop....." << std::endl;
200         if (config->pid_ > 0) {
201             NativeMemoryProfilerSaClientManager::Stop(config->pid_);
202         } else {
203             NativeMemoryProfilerSaClientManager::Stop(config->processName_);
204         }
205     } else {
206         printf("The start or stop parameter is not configured.\n");
207     }
208     return 0;
209 }