blob: 88035c56bc5a998c0a4df2fd856f44b61e080435 [file] [log] [blame]
Kuiying Wang3a044402019-02-19 15:00:11 +08001/*
2// Copyright (c) 2019 Intel Corporation
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#include "post_code.hpp"
ZhikuiRen993d4dd2020-01-29 14:25:44 -080017
18#include "iomanip"
19
Kumar Thangavelfd45f782020-09-01 22:59:00 +053020PostCodeDataHolder* PostCodeDataHolder::instance = 0;
21
ZhikuiRen993d4dd2020-01-29 14:25:44 -080022void PostCode::deleteAll()
23{
Kumar Thangavelfd45f782020-09-01 22:59:00 +053024 auto dir = fs::path(postcodeDataHolderObj->PostCodeListPathPrefix +
25 std::to_string(postcodeDataHolderObj->node));
ZhikuiRen993d4dd2020-01-29 14:25:44 -080026 std::uintmax_t n = fs::remove_all(dir);
27 std::cerr << "clearPostCodes deleted " << n << " files in "
Kumar Thangavelfd45f782020-09-01 22:59:00 +053028 << postcodeDataHolderObj->PostCodeListPathPrefix +
29 std::to_string(postcodeDataHolderObj->node)
30 << std::endl;
ZhikuiRen993d4dd2020-01-29 14:25:44 -080031 fs::create_directories(dir);
32 postCodes.clear();
33 currentBootCycleIndex = 1;
34 currentBootCycleCount(1);
35}
36
Manojkiran Eda84a4c192021-02-25 15:23:42 +053037std::vector<postcode_t> PostCode::getPostCodes(uint16_t index)
Kuiying Wang3a044402019-02-19 15:00:11 +080038{
Manojkiran Eda84a4c192021-02-25 15:23:42 +053039 std::vector<postcode_t> codesVec;
ZhikuiRen993d4dd2020-01-29 14:25:44 -080040 if (1 == index && !postCodes.empty())
41 {
42 for (auto& code : postCodes)
43 codesVec.push_back(code.second);
44 }
45 else
46 {
47 uint16_t bootNum = getBootNum(index);
Kuiying Wang3a044402019-02-19 15:00:11 +080048
ZhikuiRen993d4dd2020-01-29 14:25:44 -080049 decltype(postCodes) codes;
50 deserializePostCodes(
51 fs::path(strPostCodeListPath + std::to_string(bootNum)), codes);
Manojkiran Eda84a4c192021-02-25 15:23:42 +053052 for (std::pair<uint64_t, postcode_t> code : codes)
ZhikuiRen993d4dd2020-01-29 14:25:44 -080053 codesVec.push_back(code.second);
54 }
55 return codesVec;
56}
57
Manojkiran Eda84a4c192021-02-25 15:23:42 +053058std::map<uint64_t, postcode_t>
59 PostCode::getPostCodesWithTimeStamp(uint16_t index)
ZhikuiRen993d4dd2020-01-29 14:25:44 -080060{
61 if (1 == index && !postCodes.empty())
62 {
Kuiying Wang3a044402019-02-19 15:00:11 +080063 return postCodes;
ZhikuiRen993d4dd2020-01-29 14:25:44 -080064 }
65
66 uint16_t bootNum = getBootNum(index);
67 decltype(postCodes) codes;
68 deserializePostCodes(
69 fs::path(strPostCodeListPath + std::to_string(bootNum)), codes);
Kuiying Wang3a044402019-02-19 15:00:11 +080070 return codes;
71}
ZhikuiRen993d4dd2020-01-29 14:25:44 -080072
Manojkiran Eda84a4c192021-02-25 15:23:42 +053073void PostCode::savePostCodes(postcode_t code)
Kuiying Wang3a044402019-02-19 15:00:11 +080074{
ZhikuiRen993d4dd2020-01-29 14:25:44 -080075 // steady_clock is a monotonic clock that is guaranteed to never be adjusted
76 auto postCodeTimeSteady = std::chrono::steady_clock::now();
77 uint64_t tsUS = std::chrono::duration_cast<std::chrono::microseconds>(
78 std::chrono::system_clock::now().time_since_epoch())
79 .count();
80
81 if (postCodes.empty())
82 {
83 firstPostCodeTimeSteady = postCodeTimeSteady;
84 firstPostCodeUsSinceEpoch = tsUS; // uS since epoch for 1st post code
85 incrBootCycle();
86 }
87 else
88 {
89 // calculating tsUS so it is monotonic within the same boot
90 tsUS = firstPostCodeUsSinceEpoch +
91 std::chrono::duration_cast<std::chrono::microseconds>(
92 postCodeTimeSteady - firstPostCodeTimeSteady)
93 .count();
94 }
95
96 postCodes.insert(std::make_pair(tsUS, code));
Kumar Thangavelfd45f782020-09-01 22:59:00 +053097 serialize(fs::path(strPostCodeListPath));
ZhikuiRen993d4dd2020-01-29 14:25:44 -080098
Kuiying Wang3a044402019-02-19 15:00:11 +080099 return;
100}
101
102fs::path PostCode::serialize(const std::string& path)
103{
104 try
105 {
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800106 fs::path idxPath(path + strCurrentBootCycleIndexName);
107 std::ofstream osIdx(idxPath.c_str(), std::ios::binary);
108 cereal::JSONOutputArchive idxArchive(osIdx);
109 idxArchive(currentBootCycleIndex);
110
111 uint16_t count = currentBootCycleCount();
112 fs::path cntPath(path + strCurrentBootCycleCountName);
113 std::ofstream osCnt(cntPath.c_str(), std::ios::binary);
114 cereal::JSONOutputArchive cntArchive(osCnt);
115 cntArchive(count);
Kuiying Wang3a044402019-02-19 15:00:11 +0800116
117 std::ofstream osPostCodes(
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800118 (path + std::to_string(currentBootCycleIndex)));
Kuiying Wang3a044402019-02-19 15:00:11 +0800119 cereal::JSONOutputArchive oarchivePostCodes(osPostCodes);
120 oarchivePostCodes(postCodes);
121 }
122 catch (cereal::Exception& e)
123 {
124 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
125 return "";
126 }
127 catch (const fs::filesystem_error& e)
128 {
129 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
130 return "";
131 }
132 return path;
133}
134
135bool PostCode::deserialize(const fs::path& path, uint16_t& index)
136{
137 try
138 {
139 if (fs::exists(path))
140 {
141 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
142 cereal::JSONInputArchive iarchive(is);
143 iarchive(index);
144 return true;
145 }
146 return false;
147 }
148 catch (cereal::Exception& e)
149 {
150 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
151 return false;
152 }
153 catch (const fs::filesystem_error& e)
154 {
155 return false;
156 }
157
158 return false;
159}
160
161bool PostCode::deserializePostCodes(const fs::path& path,
Manojkiran Eda84a4c192021-02-25 15:23:42 +0530162 std::map<uint64_t, postcode_t>& codes)
Kuiying Wang3a044402019-02-19 15:00:11 +0800163{
164 try
165 {
166 if (fs::exists(path))
167 {
168 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
169 cereal::JSONInputArchive iarchive(is);
170 iarchive(codes);
171 return true;
172 }
173 return false;
174 }
175 catch (cereal::Exception& e)
176 {
177 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
178 return false;
179 }
180 catch (const fs::filesystem_error& e)
181 {
182 return false;
183 }
Kuiying Wang3a044402019-02-19 15:00:11 +0800184 return false;
185}
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800186
187void PostCode::incrBootCycle()
188{
189 if (currentBootCycleIndex >= maxBootCycleNum())
190 {
191 currentBootCycleIndex = 1;
192 }
193 else
194 {
195 currentBootCycleIndex++;
196 }
197 currentBootCycleCount(std::min(
198 maxBootCycleNum(), static_cast<uint16_t>(currentBootCycleCount() + 1)));
199}
200
201uint16_t PostCode::getBootNum(const uint16_t index) const
202{
203 // bootNum assumes the oldest archive is boot number 1
204 // and the current boot number equals bootCycleCount
205 // map bootNum back to bootIndex that was used to archive postcode
206 uint16_t bootNum = currentBootCycleIndex;
207 if (index > bootNum) // need to wrap around
208 {
209 bootNum = (maxBootCycleNum() + currentBootCycleIndex) - index + 1;
210 }
211 else
212 {
213 bootNum = currentBootCycleIndex - index + 1;
214 }
215 return bootNum;
Kumar Thangavelfd45f782020-09-01 22:59:00 +0530216}