object-introspection/oi/PaddingHunter.cpp

75 lines
2.8 KiB
C++
Raw Normal View History

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>
#include <cstdint>
2022-12-19 14:37:51 +00:00
#include <fstream>
void PaddingHunter::processLocalPaddingInfo() {
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;
for (auto& paddedStruct : paddedStructs) {
2022-12-19 14:37:51 +00:00
paddedStructsVec.push_back({paddedStruct.first, paddedStruct.second});
}
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";
std::sort(paddedStructsVec.begin(),
paddedStructsVec.end(),
[](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;
});
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();
}