|
50 | 50 | #include "test/TestUtils.h" |
51 | 51 | #include "test/fuzz/FuzzTargetRegistry.h" |
52 | 52 | #include "test/test.h" |
| 53 | +#include "xdr/Stellar-SCP.h" |
| 54 | +#include "xdr/Stellar-contract-config-setting.h" |
| 55 | +#include "xdr/Stellar-contract-env-meta.h" |
| 56 | +#include "xdr/Stellar-contract-meta.h" |
| 57 | +#include "xdr/Stellar-contract.h" |
| 58 | +#include "xdr/Stellar-exporter.h" |
| 59 | +#include "xdr/Stellar-internal.h" |
| 60 | +#include "xdr/Stellar-ledger-entries.h" |
| 61 | +#include "xdr/Stellar-ledger.h" |
| 62 | +#include "xdr/Stellar-overlay.h" |
| 63 | +#include "xdr/Stellar-transaction.h" |
| 64 | +#include "xdr/Stellar-types.h" |
53 | 65 | #endif |
54 | 66 |
|
| 67 | +#include <array> |
55 | 68 | #include <filesystem> |
56 | 69 | #include <fmt/format.h> |
57 | 70 | #include <fstream> |
58 | 71 | #include <iostream> |
59 | 72 | #include <lib/clara.hpp> |
| 73 | +#include <map> |
60 | 74 | #include <optional> |
61 | 75 |
|
62 | 76 | namespace stellar |
@@ -1715,6 +1729,106 @@ writeVersionInfo(std::ostream& os) |
1715 | 1729 | #ifdef BUILD_TESTS |
1716 | 1730 | namespace |
1717 | 1731 | { |
| 1732 | +struct UnionLayoutTotals |
| 1733 | +{ |
| 1734 | + size_t mUnions{}; |
| 1735 | + size_t mInlineArms{}; |
| 1736 | + size_t mIndirectArms{}; |
| 1737 | + size_t mVoidArms{}; |
| 1738 | + std::map<size_t, std::array<size_t, 2>> mSizeClasses; |
| 1739 | +}; |
| 1740 | + |
| 1741 | +template <typename Union, size_t... Indices> |
| 1742 | +void |
| 1743 | +dumpUnionLayout(UnionLayoutTotals& totals, std::index_sequence<Indices...>) |
| 1744 | +{ |
| 1745 | + using Meta = typename Union::_xdr_union_meta; |
| 1746 | + std::cout << "union " << Meta::qualified_name << ": size=" << sizeof(Union) |
| 1747 | + << " alignment=" << alignof(Union) |
| 1748 | + << " threshold=" << Meta::inline_threshold |
| 1749 | + << " inline=" << Meta::num_inline_arms |
| 1750 | + << " indirect=" << Meta::num_indirect_arms |
| 1751 | + << " void=" << Meta::num_void_arms << '\n'; |
| 1752 | + |
| 1753 | + auto dumpArm = [&]<size_t Index>() { |
| 1754 | + if constexpr (Index == 0 && Meta::num_void_arms == 0) |
| 1755 | + { |
| 1756 | + return; |
| 1757 | + } |
| 1758 | + constexpr auto layout = Meta::template arm_layout<Index>(); |
| 1759 | + auto const* storage = |
| 1760 | + layout.size == 0 ? "void" |
| 1761 | + : (layout.is_indirect ? "indirect" : "inline"); |
| 1762 | + std::cout << " arm " << Index |
| 1763 | + << " name=" << (layout.name[0] ? layout.name : "<void>") |
| 1764 | + << " size=" << layout.size |
| 1765 | + << " alignment=" << layout.alignment << " storage=" << storage |
| 1766 | + << '\n'; |
| 1767 | + if (layout.size == 0) |
| 1768 | + { |
| 1769 | + ++totals.mVoidArms; |
| 1770 | + } |
| 1771 | + else |
| 1772 | + { |
| 1773 | + auto& sizeClass = totals.mSizeClasses[layout.size]; |
| 1774 | + ++sizeClass[layout.is_indirect ? 1 : 0]; |
| 1775 | + if (layout.is_indirect) |
| 1776 | + { |
| 1777 | + ++totals.mIndirectArms; |
| 1778 | + } |
| 1779 | + else |
| 1780 | + { |
| 1781 | + ++totals.mInlineArms; |
| 1782 | + } |
| 1783 | + } |
| 1784 | + }; |
| 1785 | + (dumpArm.template operator()<Indices>(), ...); |
| 1786 | + ++totals.mUnions; |
| 1787 | +} |
| 1788 | + |
| 1789 | +template <typename... Unions> |
| 1790 | +void |
| 1791 | +dumpUnionLayouts(xdr::type_list<Unions...>, UnionLayoutTotals& totals) |
| 1792 | +{ |
| 1793 | + (dumpUnionLayout<Unions>( |
| 1794 | + totals, std::make_index_sequence<Unions::_xdr_union_meta::num_arms>{}), |
| 1795 | + ...); |
| 1796 | +} |
| 1797 | + |
| 1798 | +int |
| 1799 | +runDumpXdrUnions(CommandLineArgs const& args) |
| 1800 | +{ |
| 1801 | + return runWithHelp(args, {}, [&] { |
| 1802 | + UnionLayoutTotals totals; |
| 1803 | + dumpUnionLayouts(xdr::xdr_union_types_Stellar_contract_config_setting{}, |
| 1804 | + totals); |
| 1805 | + dumpUnionLayouts(xdr::xdr_union_types_Stellar_contract_env_meta{}, |
| 1806 | + totals); |
| 1807 | + dumpUnionLayouts(xdr::xdr_union_types_Stellar_contract_meta{}, totals); |
| 1808 | + dumpUnionLayouts(xdr::xdr_union_types_Stellar_contract{}, totals); |
| 1809 | + dumpUnionLayouts(xdr::xdr_union_types_Stellar_exporter{}, totals); |
| 1810 | + dumpUnionLayouts(xdr::xdr_union_types_Stellar_internal{}, totals); |
| 1811 | + dumpUnionLayouts(xdr::xdr_union_types_Stellar_ledger_entries{}, totals); |
| 1812 | + dumpUnionLayouts(xdr::xdr_union_types_Stellar_ledger{}, totals); |
| 1813 | + dumpUnionLayouts(xdr::xdr_union_types_Stellar_overlay{}, totals); |
| 1814 | + dumpUnionLayouts(xdr::xdr_union_types_Stellar_SCP{}, totals); |
| 1815 | + dumpUnionLayouts(xdr::xdr_union_types_Stellar_transaction{}, totals); |
| 1816 | + dumpUnionLayouts(xdr::xdr_union_types_Stellar_types{}, totals); |
| 1817 | + |
| 1818 | + std::cout << "summary: unions=" << totals.mUnions |
| 1819 | + << " inline=" << totals.mInlineArms |
| 1820 | + << " indirect=" << totals.mIndirectArms |
| 1821 | + << " void=" << totals.mVoidArms << '\n'; |
| 1822 | + std::cout << "size classes (bytes: inline indirect):\n"; |
| 1823 | + for (auto const& [size, counts] : totals.mSizeClasses) |
| 1824 | + { |
| 1825 | + std::cout << " " << size << ": " << counts[0] << ' ' << counts[1] |
| 1826 | + << '\n'; |
| 1827 | + } |
| 1828 | + return 0; |
| 1829 | + }); |
| 1830 | +} |
| 1831 | + |
1718 | 1832 | int |
1719 | 1833 | runLoadXDR(CommandLineArgs const& args) |
1720 | 1834 | { |
@@ -2098,6 +2212,8 @@ handleCommandLine(int argc, char* const* argv) |
2098 | 2212 | {"print-publish-queue", "print all checkpoints scheduled for publish", |
2099 | 2213 | runPrintPublishQueue}, |
2100 | 2214 | #ifdef BUILD_TESTS |
| 2215 | + {"dump-xdr-unions", "dump calculated XDR union storage layouts", |
| 2216 | + runDumpXdrUnions}, |
2101 | 2217 | {"load-xdr", "load an XDR bucket file, for testing", runLoadXDR}, |
2102 | 2218 | {"rebuild-ledger-from-buckets", |
2103 | 2219 | "rebuild the current database ledger from the bucket list", |
|
0 commit comments