aboutsummaryrefslogtreecommitdiff
path: root/thirdparty/raw_pdb/src/Examples/ExampleContributions.cpp
blob: 93c50911771b9046d4c328bce1b8a42961d18e68 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2011-2022, Molecular Matters GmbH <[email protected]>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)

#include "Examples_PCH.h"
#include "ExampleTimedScope.h"
#include "PDB_RawFile.h"
#include "PDB_DBIStream.h"


namespace
{
	// we don't have to store std::string in the contributions, since all the data is memory-mapped anyway.
	// we do it in this example to ensure that we don't "cheat" when reading the PDB file. memory-mapped data will only
	// be faulted into the process once it's touched, so actually copying the string data makes us touch the needed data,
	// giving us a real performance measurement.
	struct Contribution
	{
		std::string objectFile;
		uint32_t rva;
		uint32_t size;
	};
}


void ExampleContributions(const PDB::RawFile& rawPdbFile, const PDB::DBIStream& dbiStream);
void ExampleContributions(const PDB::RawFile& rawPdbFile, const PDB::DBIStream& dbiStream)
{
	TimedScope total("\nRunning example \"Contributions\"");

	// in order to keep the example easy to understand, we load the PDB data serially.
	// note that this can be improved a lot by reading streams concurrently.

	// prepare the image section stream first. it is needed for converting section + offset into an RVA
	TimedScope sectionScope("Reading image section stream");
	const PDB::ImageSectionStream imageSectionStream = dbiStream.CreateImageSectionStream(rawPdbFile);
	sectionScope.Done();


	// prepare the module info stream for matching contributions against files
	TimedScope moduleScope("Reading module info stream");
	const PDB::ModuleInfoStream moduleInfoStream = dbiStream.CreateModuleInfoStream(rawPdbFile);
	moduleScope.Done();


	// read contribution stream
	TimedScope contributionScope("Reading section contribution stream");
	const PDB::SectionContributionStream sectionContributionStream = dbiStream.CreateSectionContributionStream(rawPdbFile);
	contributionScope.Done();

	std::vector<Contribution> contributions;
	{
		TimedScope scope("Storing contributions");

		const PDB::ArrayView<PDB::DBI::SectionContribution> sectionContributions = sectionContributionStream.GetContributions();
		const size_t count = sectionContributions.GetLength();

		contributions.reserve(count);

		for (const PDB::DBI::SectionContribution& contribution : sectionContributions)
		{
			const uint32_t rva = imageSectionStream.ConvertSectionOffsetToRVA(contribution.section, contribution.offset);
			if (rva == 0u)
			{
				printf("Contribution has invalid RVA\n");
				continue;
			}

			const PDB::ModuleInfoStream::Module& module = moduleInfoStream.GetModule(contribution.moduleIndex);

			contributions.push_back(Contribution { module.GetName().Decay(), rva, contribution.size });
		}
		
		scope.Done(count);
	}

	TimedScope sortScope("std::sort contributions");
	std::sort(contributions.begin(), contributions.end(), [](const Contribution& lhs, const Contribution& rhs)
	{
		return lhs.size > rhs.size;
	});
	sortScope.Done();

	total.Done();

	// log the 20 largest contributions
	{
		printf("20 largest contributions:\n");

		const size_t countToShow = std::min<size_t>(20ul, contributions.size());
		for (size_t i = 0u; i < countToShow; ++i)
		{
			const Contribution& contribution = contributions[i];
			printf("%zu: %u bytes from %s\n", i + 1u, contribution.size, contribution.objectFile.c_str());
		}
	}
}