1 /** 2 * Copyright 2023-2024 Huawei Technologies Co., Ltd 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 #ifndef MINDSPORE_CCSRC_RUNTIME_DEVICE_ASCEND_PROFILING_PROFILING_DATA_DUMPER_H_ 17 #define MINDSPORE_CCSRC_RUNTIME_DEVICE_ASCEND_PROFILING_PROFILING_DATA_DUMPER_H_ 18 19 #include <sys/stat.h> 20 #include <fcntl.h> 21 #if !defined(_WIN32) && !defined(_WIN64) && !defined(__ANDROID__) && !defined(ANDROID) && !defined(__APPLE__) 22 #include <libgen.h> 23 #include <linux/limits.h> 24 #include <sys/syscall.h> 25 #endif 26 #include <stdint.h> 27 #include <fstream> 28 #include <queue> 29 #include <mutex> 30 #include <atomic> 31 #include <vector> 32 #include <map> 33 #include <memory> 34 #include <utility> 35 #include <string> 36 #include "utils/ms_utils.h" 37 #include "include/common/visible.h" 38 39 namespace mindspore { 40 namespace profiler { 41 namespace ascend { 42 constexpr uint32_t kDefaultRingBuffer = 1000 * 1000; 43 constexpr uint32_t kBatchMaxLen = 5 * 1024 * 1024; // 5 MB 44 constexpr uint32_t kMaxWaitTimeUs = 100 * 1000; 45 constexpr uint32_t kMaxWaitTimes = 10; 46 47 class COMMON_EXPORT Utils { 48 public: 49 static bool IsFileExist(const std::string &path); 50 static bool IsFileWritable(const std::string &path); 51 static bool IsDir(const std::string &path); 52 static bool CreateDir(const std::string &path); 53 static std::string RealPath(const std::string &path); 54 static std::string RelativeToAbsPath(const std::string &path); 55 static std::string DirName(const std::string &path); 56 static uint64_t GetClockMonotonicRawNs(); 57 static bool CreateDumpFile(const std::string &path); 58 static bool IsSoftLink(const std::string &path); 59 static uint64_t GetTid(); 60 static uint64_t GetPid(); 61 }; 62 63 template <typename T> 64 class COMMON_EXPORT RingBuffer { 65 public: RingBuffer()66 RingBuffer() 67 : is_inited_(false), 68 is_quit_(false), 69 read_index_(0), 70 write_index_(0), 71 idle_write_index_(0), 72 capacity_(0), 73 mask_(0) {} 74 ~RingBuffer()75 ~RingBuffer() { UnInit(); } 76 void Init(size_t capacity); 77 void UnInit(); 78 size_t Size(); 79 bool Push(T data); 80 T Pop(); 81 bool Full(); 82 void Reset(); 83 84 private: 85 bool is_inited_; 86 volatile bool is_quit_; 87 std::atomic<size_t> read_index_; 88 std::atomic<size_t> write_index_; 89 std::atomic<size_t> idle_write_index_; 90 size_t capacity_; 91 size_t mask_; 92 std::vector<T> data_queue_; 93 }; 94 95 struct COMMON_EXPORT BaseReportData { 96 int32_t device_id{0}; 97 std::string tag; BaseReportDataBaseReportData98 BaseReportData(int32_t device_id, std::string tag) : device_id(device_id), tag(std::move(tag)) {} 99 virtual ~BaseReportData() = default; 100 virtual std::vector<uint8_t> encode() = 0; 101 virtual void preprocess() = 0; 102 }; 103 104 class COMMON_EXPORT ProfilingDataDumper { 105 public: 106 void Init(const std::string &path, size_t capacity = kDefaultRingBuffer); 107 void UnInit(); 108 void Report(std::unique_ptr<BaseReportData> data); 109 void Start(); 110 void Stop(); 111 void Flush(); 112 113 static ProfilingDataDumper &GetInstance(); 114 115 private: 116 void Dump(const std::map<std::string, std::vector<uint8_t>> &dataMap); 117 void Run(); 118 void GatherAndDumpData(); 119 120 private: 121 ProfilingDataDumper(); 122 virtual ~ProfilingDataDumper(); 123 124 std::string path_; 125 std::atomic<bool> start_; 126 std::atomic<bool> init_; 127 std::atomic<bool> is_flush_{false}; 128 RingBuffer<std::unique_ptr<BaseReportData>> data_chunk_buf_; 129 std::map<std::string, FILE *> fd_map_; 130 std::mutex flush_mutex_; 131 DISABLE_COPY_AND_ASSIGN(ProfilingDataDumper); 132 }; 133 } // namespace ascend 134 } // namespace profiler 135 } // namespace mindspore 136 #endif // MINDSPORE_CCSRC_RUNTIME_DEVICE_ASCEND_PROFILING_PROFILING_DATA_DUMPER_H_ 137