106f6ba60Sopenharmony_ci/* 206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved. 306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License. 506f6ba60Sopenharmony_ci * You may obtain a copy of the License at 606f6ba60Sopenharmony_ci * 706f6ba60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 806f6ba60Sopenharmony_ci * 906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and 1306f6ba60Sopenharmony_ci * limitations under the License. 1406f6ba60Sopenharmony_ci */ 1506f6ba60Sopenharmony_ci 1606f6ba60Sopenharmony_ci#include "native_memory_profiler_sa_client_manager.h" 1706f6ba60Sopenharmony_ci 1806f6ba60Sopenharmony_ci#include <cstdio> 1906f6ba60Sopenharmony_ci#include <sys/types.h> 2006f6ba60Sopenharmony_ci#include <sys/stat.h> 2106f6ba60Sopenharmony_ci#include <fcntl.h> 2206f6ba60Sopenharmony_ci#include <iostream> 2306f6ba60Sopenharmony_ci#include <unistd.h> 2406f6ba60Sopenharmony_ci 2506f6ba60Sopenharmony_ciusing namespace OHOS::Developtools::NativeDaemon; 2606f6ba60Sopenharmony_ci 2706f6ba60Sopenharmony_cistatic uint32_t TestDumpFile(const std::string postfix = "") 2806f6ba60Sopenharmony_ci{ 2906f6ba60Sopenharmony_ci uint32_t fd = static_cast<uint32_t>(open(("/data/local/tmp/test_dump_file" + postfix + ".htrace").c_str(), 3006f6ba60Sopenharmony_ci O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)); 3106f6ba60Sopenharmony_ci return fd; 3206f6ba60Sopenharmony_ci} 3306f6ba60Sopenharmony_ci 3406f6ba60Sopenharmony_ciint32_t main(int32_t argc, char* argv[]) 3506f6ba60Sopenharmony_ci{ 3606f6ba60Sopenharmony_ci if (argc > 50) { // 50: max args size 3706f6ba60Sopenharmony_ci printf("error too many args.\n"); 3806f6ba60Sopenharmony_ci return 0; 3906f6ba60Sopenharmony_ci } 4006f6ba60Sopenharmony_ci std::shared_ptr<NativeMemoryProfilerSaConfig> config = std::make_shared<NativeMemoryProfilerSaConfig>(); 4106f6ba60Sopenharmony_ci bool start = false; 4206f6ba60Sopenharmony_ci bool stop = false; 4306f6ba60Sopenharmony_ci bool error = false; 4406f6ba60Sopenharmony_ci bool dumpData = false; 4506f6ba60Sopenharmony_ci for (int32_t i = 1; i < argc; ++i) { 4606f6ba60Sopenharmony_ci std::string arg(argv[i]); 4706f6ba60Sopenharmony_ci if (arg == "--help" || arg == "-h") { 4806f6ba60Sopenharmony_ci printf(" --start -s : start, default: false\n"); 4906f6ba60Sopenharmony_ci printf(" --stop -k : stop, default: false\n"); 5006f6ba60Sopenharmony_ci printf(" --pid -p : pid\n"); 5106f6ba60Sopenharmony_ci printf(" --filePath -f : filePath, default: "); 5206f6ba60Sopenharmony_ci printf("/data/local/tmp/hiprofiler_data.htrace\n"); 5306f6ba60Sopenharmony_ci printf(" --duration -d : duration, default: 20s\n"); 5406f6ba60Sopenharmony_ci printf(" --filterSize -fs : filterSize, default: 0\n"); 5506f6ba60Sopenharmony_ci printf(" --shareMemorySize -sms : shareMemorySize, default: 16384\n"); 5606f6ba60Sopenharmony_ci printf(" --processName -pn : processName\n"); 5706f6ba60Sopenharmony_ci printf(" --maxStackDepth -msd : maxStackDepth, default: 30\n"); 5806f6ba60Sopenharmony_ci printf(" --mallocDisable -mad : mallocDisable, default: false\n"); 5906f6ba60Sopenharmony_ci printf(" --mmapDisable -mmd : mmapDisable, default: false\n"); 6006f6ba60Sopenharmony_ci printf(" --freeStackData -fsd : freeStackData, default: false\n"); 6106f6ba60Sopenharmony_ci printf(" --munmapStackData -musd : munmapStackData, default: false\n"); 6206f6ba60Sopenharmony_ci printf(" --mallocFreeMatchingInterval -mfmi : mallocFreeMatchingInterval\n"); 6306f6ba60Sopenharmony_ci printf(" --mallocFreeMatchingCnt -mfmc : mallocFreeMatchingCnt\n"); 6406f6ba60Sopenharmony_ci printf(" --disable_stringCompressed -sc : disable_stringCompressed, "); 6506f6ba60Sopenharmony_ci printf("default: stringCompressed\n"); 6606f6ba60Sopenharmony_ci printf(" --dwarf -df : dwarf unwind, default: fp\n"); 6706f6ba60Sopenharmony_ci printf(" --disable_blocked -b : disable_blocked, default: blocked\n"); 6806f6ba60Sopenharmony_ci printf(" --disable_recordAccurately -ra : disable_recordAccurately, "); 6906f6ba60Sopenharmony_ci printf("default: recordAccurately\n"); 7006f6ba60Sopenharmony_ci printf(" --startupMode -sm : startupMode, default: false\n"); 7106f6ba60Sopenharmony_ci printf(" --memtraceEnable -me : memtraceEnable, default: false\n"); 7206f6ba60Sopenharmony_ci printf(" --offlineSymbolization -os : offlineSymbolization, default: false\n"); 7306f6ba60Sopenharmony_ci printf(" --callframeCompress -cc : callframeCompress, default: false\n"); 7406f6ba60Sopenharmony_ci printf(" --statisticsInterval -si : statisticsInterval\n"); 7506f6ba60Sopenharmony_ci printf(" --clockId -c : clockId\n"); 7606f6ba60Sopenharmony_ci printf(" --dumpData -dd : dump data\n"); 7706f6ba60Sopenharmony_ci printf(" --sampleInterval -spi : sampleInterval, default: 0\n"); 7806f6ba60Sopenharmony_ci printf(" --jsStackReport -jr : jsStackReport, default: 0\n"); 7906f6ba60Sopenharmony_ci printf(" --maxJsStackDepth -mjsd : maxJsStackDepth, default: 0\n"); 8006f6ba60Sopenharmony_ci printf(" --filterNapiName -fnapi : filterNapiName \n"); 8106f6ba60Sopenharmony_ci return 0; 8206f6ba60Sopenharmony_ci } 8306f6ba60Sopenharmony_ci 8406f6ba60Sopenharmony_ci if ((arg == "--start") || (arg == "-s")) { 8506f6ba60Sopenharmony_ci start = true; 8606f6ba60Sopenharmony_ci } else if ((arg == "--stop") || (arg == "-k")) { 8706f6ba60Sopenharmony_ci stop = true; 8806f6ba60Sopenharmony_ci } else if ((arg == "--pid") || (arg == "-p")) { 8906f6ba60Sopenharmony_ci config->pid_ = std::stoi(argv[++i]); 9006f6ba60Sopenharmony_ci } else if ((arg == "--filePath") || (arg == "-f")) { 9106f6ba60Sopenharmony_ci config->filePath_ = std::string(argv[++i]); 9206f6ba60Sopenharmony_ci } else if ((arg == "--duration") || (arg == "-d")) { 9306f6ba60Sopenharmony_ci config->duration_ = static_cast<uint32_t>(std::stoi(argv[++i])); 9406f6ba60Sopenharmony_ci } else if ((arg == "--filterSize") || (arg == "-fs")) { 9506f6ba60Sopenharmony_ci config->filterSize_ = std::stoi(argv[++i]); 9606f6ba60Sopenharmony_ci } else if ((arg == "--shareMemorySize") || (arg == "-sms")) { 9706f6ba60Sopenharmony_ci config->shareMemorySize_ = std::stoi(argv[++i]); 9806f6ba60Sopenharmony_ci } else if ((arg == "--processName") || (arg == "-pn")) { 9906f6ba60Sopenharmony_ci config->processName_ = std::string(argv[++i]); 10006f6ba60Sopenharmony_ci } else if ((arg == "--maxStackDepth") || (arg == "-msd")) { 10106f6ba60Sopenharmony_ci config->maxStackDepth_ = std::stoi(argv[++i]); 10206f6ba60Sopenharmony_ci } else if ((arg == "--mallocDisable") || (arg == "-mad")) { 10306f6ba60Sopenharmony_ci config->mallocDisable_ = true; 10406f6ba60Sopenharmony_ci } else if ((arg == "--mmapDisable") || (arg == "-mmd")) { 10506f6ba60Sopenharmony_ci config->mmapDisable_ = true; 10606f6ba60Sopenharmony_ci } else if ((arg == "--freeStackData") || (arg == "-fsd")) { 10706f6ba60Sopenharmony_ci config->freeStackData_ = true; 10806f6ba60Sopenharmony_ci } else if ((arg == "--munmapStackData") || (arg == "-musd")) { 10906f6ba60Sopenharmony_ci config->munmapStackData_ = true; 11006f6ba60Sopenharmony_ci } else if ((arg == "--mallocFreeMatchingInterval") || (arg == "-mfmi")) { 11106f6ba60Sopenharmony_ci config->mallocFreeMatchingInterval_ = static_cast<uint32_t>(std::stoi(argv[++i])); 11206f6ba60Sopenharmony_ci } else if ((arg == "--mallocFreeMatchingCnt") || (arg == "-mfmc")) { 11306f6ba60Sopenharmony_ci config->mallocFreeMatchingCnt_ = std::stoi(argv[++i]); 11406f6ba60Sopenharmony_ci } else if ((arg == "--disable_stringCompressed") || (arg == "-sc")) { 11506f6ba60Sopenharmony_ci config->stringCompressed_ = false; 11606f6ba60Sopenharmony_ci } else if ((arg == "--dwarf") || (arg == "-df")) { 11706f6ba60Sopenharmony_ci config->fpUnwind_ = false; 11806f6ba60Sopenharmony_ci } else if ((arg == "--disable_blocked") || (arg == "-b")) { 11906f6ba60Sopenharmony_ci config->blocked_ = false; 12006f6ba60Sopenharmony_ci } else if ((arg == "--disable_recordAccurately") || (arg == "-ra")) { 12106f6ba60Sopenharmony_ci config->recordAccurately_ = false; 12206f6ba60Sopenharmony_ci } else if ((arg == "--startupMode") || (arg == "-sm")) { 12306f6ba60Sopenharmony_ci config->startupMode_ = true; 12406f6ba60Sopenharmony_ci } else if ((arg == "--memtraceEnable") || (arg == "-me")) { 12506f6ba60Sopenharmony_ci config->memtraceEnable_ = true; 12606f6ba60Sopenharmony_ci } else if ((arg == "--offlineSymbolization") || (arg == "-os")) { 12706f6ba60Sopenharmony_ci config->offlineSymbolization_ = true; 12806f6ba60Sopenharmony_ci } else if ((arg == "--callframeCompress") || (arg == "-cc")) { 12906f6ba60Sopenharmony_ci config->callframeCompress_ = true; 13006f6ba60Sopenharmony_ci } else if ((arg == "--statisticsInterval") || (arg == "-si")) { 13106f6ba60Sopenharmony_ci config->statisticsInterval_ = std::stoi(argv[++i]); 13206f6ba60Sopenharmony_ci } else if ((arg == "--clockId") || (arg == "-c")) { 13306f6ba60Sopenharmony_ci config->clockId_ = std::stoi(argv[++i]); 13406f6ba60Sopenharmony_ci } else if ((arg == "--dumpData") || (arg == "-dd")) { 13506f6ba60Sopenharmony_ci dumpData = true; 13606f6ba60Sopenharmony_ci } else if ((arg == "--sampleInterval ") || (arg == "-spi")) { 13706f6ba60Sopenharmony_ci config->sampleInterval_ = std::stoi(argv[++i]); 13806f6ba60Sopenharmony_ci } else if ((arg == "--responseLibraryMode") || (arg == "-r")) { 13906f6ba60Sopenharmony_ci config->responseLibraryMode_ = true; 14006f6ba60Sopenharmony_ci } else if ((arg == "--printNmd") || (arg == "-nmd")) { 14106f6ba60Sopenharmony_ci config->printNmd_ = true; 14206f6ba60Sopenharmony_ci } else if ((arg == "--jsStackReport") || (arg == "-jr")) { 14306f6ba60Sopenharmony_ci config->jsStackReport_ = static_cast<int32_t>(std::stoi(argv[++i]));; 14406f6ba60Sopenharmony_ci } else if ((arg == "--maxJsStackDepth") || (arg == "-mjsd")) { 14506f6ba60Sopenharmony_ci config->maxJsStackDepth_ = std::stoi(argv[++i]); 14606f6ba60Sopenharmony_ci } else if ((arg == "--filterNapiName") || (arg == "-fnapi")) { 14706f6ba60Sopenharmony_ci config->filterNapiName_ = std::string(argv[++i]); 14806f6ba60Sopenharmony_ci } else { 14906f6ba60Sopenharmony_ci printf("error arg: %s\n", arg.c_str()); 15006f6ba60Sopenharmony_ci error = true; 15106f6ba60Sopenharmony_ci break; 15206f6ba60Sopenharmony_ci } 15306f6ba60Sopenharmony_ci } 15406f6ba60Sopenharmony_ci 15506f6ba60Sopenharmony_ci if (error) { 15606f6ba60Sopenharmony_ci return 0; 15706f6ba60Sopenharmony_ci } 15806f6ba60Sopenharmony_ci 15906f6ba60Sopenharmony_ci if (start) { 16006f6ba60Sopenharmony_ci std::cout << "start....." << std::endl; 16106f6ba60Sopenharmony_ci if (dumpData) { 16206f6ba60Sopenharmony_ci uint32_t fd = TestDumpFile(); 16306f6ba60Sopenharmony_ci NativeMemoryProfilerSaClientManager::DumpData(fd, config); 16406f6ba60Sopenharmony_ci close(fd); 16506f6ba60Sopenharmony_ci } else if (config->printNmd_) { 16606f6ba60Sopenharmony_ci uint32_t fdFirst = TestDumpFile(std::to_string(0)); 16706f6ba60Sopenharmony_ci NativeMemoryProfilerSaClientManager::GetMallocStats(fdFirst, config->pid_, 0); 16806f6ba60Sopenharmony_ci close(fdFirst); 16906f6ba60Sopenharmony_ci } else { 17006f6ba60Sopenharmony_ci NativeMemoryProfilerSaClientManager::Start(config); 17106f6ba60Sopenharmony_ci } 17206f6ba60Sopenharmony_ci } else if (stop) { 17306f6ba60Sopenharmony_ci std::cout << "stop....." << std::endl; 17406f6ba60Sopenharmony_ci if (config->pid_ > 0) { 17506f6ba60Sopenharmony_ci NativeMemoryProfilerSaClientManager::Stop(config->pid_); 17606f6ba60Sopenharmony_ci } else { 17706f6ba60Sopenharmony_ci NativeMemoryProfilerSaClientManager::Stop(config->processName_); 17806f6ba60Sopenharmony_ci } 17906f6ba60Sopenharmony_ci } else { 18006f6ba60Sopenharmony_ci printf("The start or stop parameter is not configured.\n"); 18106f6ba60Sopenharmony_ci } 18206f6ba60Sopenharmony_ci return 0; 18306f6ba60Sopenharmony_ci}