• 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 #ifndef TRANCE_FILE_WRITER_H
16 #define TRANCE_FILE_WRITER_H
17 
18 #include <cstdint>
19 #include <fstream>
20 #include <google/protobuf/message_lite.h>
21 #include <mutex>
22 #include <queue>
23 #include <string>
24 
25 #include "logging.h"
26 #include "nocopyable.h"
27 #include "plugin_module_api.h"
28 #include "trace_file_helper.h"
29 #include "writer.h"
30 
31 using google::protobuf::MessageLite;
32 
33 #ifndef MMAP_FAILED
34 #define MMAP_FAILED (reinterpret_cast<void *>(-1))
35 #endif
36 
37 class TraceFileWriter : public Writer {
38 public:
39     explicit TraceFileWriter(const std::string& path);
40     explicit TraceFileWriter(int32_t fd);
41 
42     explicit TraceFileWriter(const std::string& path, bool splitFile, uint32_t splitFileMaxSizeMb,
43         uint32_t splitFileMaxNum);
44 
45     ~TraceFileWriter();
46 
47     std::string Path() const;
48 
49     bool SetPluginConfig(const void* data, size_t size);
50 
51     void WriteStandalonePluginData(const std::string& pluginName,
52                                    const std::string& data,
53                                    const std::string& pluginVersion = "");
54 
55     bool WriteHeader();
56 
57     long Write(const MessageLite& message);
58 
59     long Write(const void* data, size_t size) override;
60 
61     long WriteStandalonePluginFile(const std::string& file,
62                                    const std::string& name,
63                                    const std::string& version, DataType type);
64 
65     bool Flush() override;
66 
67     bool Finish();
68 
69     bool IsSplitFile(uint32_t size);
70 
71     void SetStopSplitFile(bool isStop);
72 
73     void SetTimeSource();
74     void SetDurationTime();
75 
76 private:
77     void SetTimeStamp();
78 
79     void LogDiskUsage();
80 
81     void DeleteOldSplitFile();
82 
83     bool FlushStream();
84 
85     bool GetMemory(uint32_t size, uint8_t** memory, uint32_t* offset);
86     bool Seek(uint32_t offset);
87     bool RemapFile();
88 
89     struct TraceFileWriterCtx {
90         RandomWriteCtx ctx;
91         TraceFileWriter* write;
92     };
93 
GetCtx()94     RandomWriteCtx* GetCtx() override
95     {
96         return &writeCtx_.ctx;
97     }
98     void ResetPos() override;
99     void FinishReport(int32_t size) override;
100 
101 private:
102     std::string path_ {};
103     std::string oldPath_ {};
104     std::ofstream stream_ {};
105     uint64_t writeBytes_ = 0;
106     uint64_t writeCount_ = 0;
107     TraceFileHeader header_ {};
108     TraceFileHelper helper_ {};
109     uint32_t dataSize_ = 0;
110     bool isSplitFile_ = false;
111     uint32_t splitFileMaxSize_ = 0;
112     uint32_t splitFileMaxNum_ = 0;
113     std::queue<std::string> splitFilePaths_;
114     std::vector<std::vector<char>> pluginConfigsData_;
115     bool isStop_ = false;
116     int fileNum_ = 0;
117     TraceFileHeader::HeaderData headerDataTime_ = {}; // used to store the clock source and collection time.
118     int32_t fd_{-1};
119     TraceFileWriterCtx writeCtx_ {};
120     void* fileMapAddr_ = MMAP_FAILED;
121     size_t fileLength_ = 0;
122     uint32_t fileWriteLength_ = 0;
123     uint32_t messageWriteOffset_ = 0;
124     size_t pageSize_ = 0;
125     size_t mapOffset_ = 0;
126 
127     DISALLOW_COPY_AND_MOVE(TraceFileWriter);
128 };
129 
130 using TraceFileWriterPtr = STD_PTR(shared, TraceFileWriter);
131 
132 #endif // !TRANCE_FILER_WRITER_H