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 #include <memory>
16 
17 #include "trace_file_reader.h"
18 #include "common.h"
19 #include "logging.h"
20 
21 using CharPtr = std::unique_ptr<char>::pointer;
22 
~TraceFileReader()23 TraceFileReader::~TraceFileReader()
24 {
25     if (stream_.is_open()) {
26         stream_.close();
27     }
28 }
29 
GetHeader() const30 TraceFileHeader TraceFileReader::GetHeader() const
31 {
32     return header_;
33 }
34 
ValidateHeader()35 bool TraceFileReader::ValidateHeader()
36 {
37     return helper_.Validate(header_);
38 }
39 
Open(const std::string& path)40 bool TraceFileReader::Open(const std::string& path)
41 {
42     auto retFile = COMMON::CheckNotExistsFilePath(path);
43     if (!retFile.first) {
44         PROFILER_LOG_INFO(LOG_CORE, "check file path %s fail", path.c_str());
45         return false;
46     }
47     std::string realPath = retFile.second;
48     stream_.open(realPath, std::ios_base::in | std::ios_base::binary);
49     CHECK_TRUE(stream_.is_open(), false, "open %s failed, %d!", realPath.c_str(), errno);
50 
51     stream_.read(reinterpret_cast<CharPtr>(&header_), sizeof(header_));
52     CHECK_TRUE(stream_, false, "read header from %s failed!", realPath.c_str());
53 
54     path_ = realPath;
55     return true;
56 }
57 
GetReadPos(std::ifstream& stream)58 static size_t GetReadPos(std::ifstream& stream)
59 {
60     return static_cast<size_t>(stream.tellg());
61 }
62 
Read(MessageLite& message)63 long TraceFileReader::Read(MessageLite& message)
64 {
65     CHECK_TRUE(stream_.is_open(), 0, "binary file %s not open or open failed!", path_.c_str());
66     CHECK_TRUE(!stream_.eof(), 0, "no more data in file %s stream", path_.c_str());
67 
68     uint32_t msgLen = 0;
69     size_t offset = GetReadPos(stream_);
70     stream_.read(reinterpret_cast<CharPtr>(&msgLen), sizeof(msgLen));
71     RETURN_IF(stream_.eof(), 0, "read file end");
72     CHECK_TRUE(msgLen > 0, 0, "read in file %s msg length: %d", path_.c_str(), msgLen);
73     CHECK_TRUE(stream_, 0, "read msg length from %s (offset %zu) failed, or no more data!", path_.c_str(), offset);
74     CHECK_TRUE(helper_.AddSegment(reinterpret_cast<uint8_t*>(&msgLen), sizeof(msgLen)),
75         0, "Add payload for message length failed!");
76 
77     std::vector<char> msgData(msgLen);
78     offset = GetReadPos(stream_);
79     stream_.read(msgData.data(), msgData.size());
80     RETURN_IF(stream_.eof(), 0, "read file end");
81     CHECK_TRUE(stream_, 0, "read msg bytes from %s (offset %zu) failed!", path_.c_str(), offset);
82     CHECK_TRUE(helper_.AddSegment(reinterpret_cast<uint8_t*>(msgData.data()), msgData.size()),
83         0, "Add payload for message bytes failed!");
84 
85     CHECK_TRUE(message.ParseFromArray(msgData.data(), msgData.size()), 0, "ParseFromArray failed!");
86     return sizeof(msgLen) + msgData.size();
87 }
88 
ReadLen()89 long TraceFileReader::ReadLen()
90 {
91     CHECK_TRUE(stream_.is_open(), 0, "binary file %s not open or open failed!", path_.c_str());
92     CHECK_TRUE(!stream_.eof(), 0, "no more data in file %s stream", path_.c_str());
93 
94     uint32_t dataLen = 0;
95     stream_.read(reinterpret_cast<CharPtr>(&dataLen), sizeof(dataLen));
96     RETURN_IF(stream_.eof(), 0, "read file end");
97     CHECK_TRUE(dataLen > 0, 0, "read in file %s data length: %d", path_.c_str(), dataLen);
98     CHECK_TRUE(stream_, 0, "read data length from file %s (offset %zu) failed!", path_.c_str(), GetReadPos(stream_));
99     CHECK_TRUE(helper_.AddSegment(reinterpret_cast<uint8_t*>(&dataLen), sizeof(dataLen)),
100         0, "Add payload for data length failed!");
101     return dataLen;
102 }
103 
ReadData(uint8_t buffer[], uint32_t bufferSize)104 bool TraceFileReader::ReadData(uint8_t buffer[], uint32_t bufferSize)
105 {
106     CHECK_TRUE(stream_.is_open(), false, "binary file %s not open or open failed!", path_.c_str());
107     CHECK_TRUE(!stream_.eof(), false, "no more data in file %s stream", path_.c_str());
108 
109     stream_.read(reinterpret_cast<CharPtr>(buffer), bufferSize);
110     RETURN_IF(stream_.eof(), false, "read file end. read data bytes from %s failed! (offset %zu)",
111               path_.c_str(), GetReadPos(stream_));
112     CHECK_TRUE(stream_, false, "read data bytes from %s (offset %zu) failed!", path_.c_str(), GetReadPos(stream_));
113     CHECK_TRUE(helper_.AddSegment(buffer, bufferSize), false, "Add payload for data bytes failed!");
114     return true;
115 }
116