2022-12-19 14:37:51 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2023-04-26 16:20:53 +01:00
|
|
|
#include "oi/PaddingHunter.h"
|
2022-12-19 14:37:51 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
2023-09-21 21:24:43 +01:00
|
|
|
#include <cstdint>
|
2022-12-19 14:37:51 +00:00
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
void PaddingHunter::processLocalPaddingInfo() {
|
2023-03-22 16:16:17 +00:00
|
|
|
for (auto& lPS : localPaddedStructs) {
|
2022-12-19 14:37:51 +00:00
|
|
|
if (paddedStructs.find(lPS.first) != paddedStructs.end()) {
|
|
|
|
if (localPaddedStructs[lPS.first].instancesCnt >
|
|
|
|
paddedStructs[lPS.first].instancesCnt) {
|
|
|
|
paddedStructs[lPS.first].instancesCnt =
|
|
|
|
localPaddedStructs[lPS.first].instancesCnt;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
paddedStructs[lPS.first] = lPS.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PaddingHunter::outputPaddingInfo() {
|
|
|
|
std::ofstream paddingStatsFile;
|
|
|
|
paddingStatsFile.open(paddingStatsFileName);
|
|
|
|
uint64_t sum = 0;
|
|
|
|
|
|
|
|
std::vector<std::pair<std::string, PaddingInfo>> paddedStructsVec;
|
2023-03-22 16:16:17 +00:00
|
|
|
for (auto& paddedStruct : paddedStructs) {
|
2022-12-19 14:37:51 +00:00
|
|
|
paddedStructsVec.push_back({paddedStruct.first, paddedStruct.second});
|
|
|
|
}
|
|
|
|
|
2023-03-22 16:16:17 +00:00
|
|
|
for (auto& paddedStruct : paddedStructsVec) {
|
2022-12-19 14:37:51 +00:00
|
|
|
sum += paddedStruct.second.paddingSize * paddedStruct.second.instancesCnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
paddingStatsFile << "Total Saving Opportunity: " << sum << "\n\n\n";
|
|
|
|
|
2023-11-13 16:50:09 +00:00
|
|
|
std::sort(paddedStructsVec.begin(),
|
|
|
|
paddedStructsVec.end(),
|
2023-03-22 16:16:17 +00:00
|
|
|
[](const std::pair<std::string, PaddingInfo>& left,
|
|
|
|
const std::pair<std::string, PaddingInfo>& right) {
|
2022-12-19 14:37:51 +00:00
|
|
|
return left.second.instancesCnt * left.second.savingSize >
|
|
|
|
right.second.instancesCnt * right.second.savingSize;
|
|
|
|
});
|
|
|
|
|
2023-03-22 16:16:17 +00:00
|
|
|
for (auto& paddedStruct : paddedStructsVec) {
|
2022-12-19 14:37:51 +00:00
|
|
|
paddingStatsFile << "Name: " << paddedStruct.first
|
|
|
|
<< ", object size: " << paddedStruct.second.structSize
|
|
|
|
<< ", saving size: " << paddedStruct.second.savingSize
|
|
|
|
<< ", padding size: " << paddedStruct.second.paddingSize
|
|
|
|
<< ", isSet size: " << paddedStruct.second.isSetSize
|
|
|
|
<< ", instance_cnt: " << paddedStruct.second.instancesCnt
|
|
|
|
<< "\nSaving opportunity: "
|
|
|
|
<< paddedStruct.second.savingSize *
|
|
|
|
paddedStruct.second.instancesCnt
|
|
|
|
<< " bytes\n\n"
|
|
|
|
<< paddedStruct.second.definition << "\n\n\n";
|
|
|
|
}
|
|
|
|
paddingStatsFile.close();
|
|
|
|
}
|