1 /*
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"
17 
18 #include "iomanip"
19 
20 PostCodeDataHolder* PostCodeDataHolder::instance = 0;
21 
22 void PostCode::deleteAll()
23 {
24     auto dir = fs::path(postcodeDataHolderObj->PostCodeListPathPrefix +
25                         std::to_string(postcodeDataHolderObj->node));
26     std::uintmax_t n = fs::remove_all(dir);
27     std::cerr << "clearPostCodes deleted " << n << " files in "
28               << postcodeDataHolderObj->PostCodeListPathPrefix +
29                      std::to_string(postcodeDataHolderObj->node)
30               << std::endl;
31     fs::create_directories(dir);
32     postCodes.clear();
33     currentBootCycleIndex = 1;
34     currentBootCycleCount(1);
35 }
36 
37 std::vector<uint64_t> PostCode::getPostCodes(uint16_t index)
38 {
39     std::vector<uint64_t> codesVec;
40     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);
48 
49         decltype(postCodes) codes;
50         deserializePostCodes(
51             fs::path(strPostCodeListPath + std::to_string(bootNum)), codes);
52         for (std::pair<uint64_t, uint64_t> code : codes)
53             codesVec.push_back(code.second);
54     }
55     return codesVec;
56 }
57 
58 std::map<uint64_t, uint64_t> PostCode::getPostCodesWithTimeStamp(uint16_t index)
59 {
60     if (1 == index && !postCodes.empty())
61     {
62         return postCodes;
63     }
64 
65     uint16_t bootNum = getBootNum(index);
66     decltype(postCodes) codes;
67     deserializePostCodes(
68         fs::path(strPostCodeListPath + std::to_string(bootNum)), codes);
69     return codes;
70 }
71 
72 void PostCode::savePostCodes(uint64_t code)
73 {
74     // steady_clock is a monotonic clock that is guaranteed to never be adjusted
75     auto postCodeTimeSteady = std::chrono::steady_clock::now();
76     uint64_t tsUS = std::chrono::duration_cast<std::chrono::microseconds>(
77                         std::chrono::system_clock::now().time_since_epoch())
78                         .count();
79 
80     if (postCodes.empty())
81     {
82         firstPostCodeTimeSteady = postCodeTimeSteady;
83         firstPostCodeUsSinceEpoch = tsUS; // uS since epoch for 1st post code
84         incrBootCycle();
85     }
86     else
87     {
88         // calculating tsUS so it is monotonic within the same boot
89         tsUS = firstPostCodeUsSinceEpoch +
90                std::chrono::duration_cast<std::chrono::microseconds>(
91                    postCodeTimeSteady - firstPostCodeTimeSteady)
92                    .count();
93     }
94 
95     postCodes.insert(std::make_pair(tsUS, code));
96     serialize(fs::path(strPostCodeListPath));
97 
98     return;
99 }
100 
101 fs::path PostCode::serialize(const std::string& path)
102 {
103     try
104     {
105         fs::path idxPath(path + strCurrentBootCycleIndexName);
106         std::ofstream osIdx(idxPath.c_str(), std::ios::binary);
107         cereal::JSONOutputArchive idxArchive(osIdx);
108         idxArchive(currentBootCycleIndex);
109 
110         uint16_t count = currentBootCycleCount();
111         fs::path cntPath(path + strCurrentBootCycleCountName);
112         std::ofstream osCnt(cntPath.c_str(), std::ios::binary);
113         cereal::JSONOutputArchive cntArchive(osCnt);
114         cntArchive(count);
115 
116         std::ofstream osPostCodes(
117             (path + std::to_string(currentBootCycleIndex)));
118         cereal::JSONOutputArchive oarchivePostCodes(osPostCodes);
119         oarchivePostCodes(postCodes);
120     }
121     catch (cereal::Exception& e)
122     {
123         phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
124         return "";
125     }
126     catch (const fs::filesystem_error& e)
127     {
128         phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
129         return "";
130     }
131     return path;
132 }
133 
134 bool PostCode::deserialize(const fs::path& path, uint16_t& index)
135 {
136     try
137     {
138         if (fs::exists(path))
139         {
140             std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
141             cereal::JSONInputArchive iarchive(is);
142             iarchive(index);
143             return true;
144         }
145         return false;
146     }
147     catch (cereal::Exception& e)
148     {
149         phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
150         return false;
151     }
152     catch (const fs::filesystem_error& e)
153     {
154         return false;
155     }
156 
157     return false;
158 }
159 
160 bool PostCode::deserializePostCodes(const fs::path& path,
161                                     std::map<uint64_t, uint64_t>& codes)
162 {
163     try
164     {
165         if (fs::exists(path))
166         {
167             std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
168             cereal::JSONInputArchive iarchive(is);
169             iarchive(codes);
170             return true;
171         }
172         return false;
173     }
174     catch (cereal::Exception& e)
175     {
176         phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
177         return false;
178     }
179     catch (const fs::filesystem_error& e)
180     {
181         return false;
182     }
183     return false;
184 }
185 
186 void PostCode::incrBootCycle()
187 {
188     if (currentBootCycleIndex >= maxBootCycleNum())
189     {
190         currentBootCycleIndex = 1;
191     }
192     else
193     {
194         currentBootCycleIndex++;
195     }
196     currentBootCycleCount(std::min(
197         maxBootCycleNum(), static_cast<uint16_t>(currentBootCycleCount() + 1)));
198 }
199 
200 uint16_t PostCode::getBootNum(const uint16_t index) const
201 {
202     // bootNum assumes the oldest archive is boot number 1
203     // and the current boot number equals bootCycleCount
204     // map bootNum back to bootIndex that was used to archive postcode
205     uint16_t bootNum = currentBootCycleIndex;
206     if (index > bootNum) // need to wrap around
207     {
208         bootNum = (maxBootCycleNum() + currentBootCycleIndex) - index + 1;
209     }
210     else
211     {
212         bootNum = currentBootCycleIndex - index + 1;
213     }
214     return bootNum;
215 }
216