1 /*
2 * Copyright (C) 2018 The Android Open Source Project
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
17 // Tool that takes in a stream of allocations from stdin and outputs samples
18 //
19 // Input format is code_location size tuples, output format is iteration number
20 // code_location sample_size tuples. The sum of all allocations in the input is
21 // echoed back in the special iteration 'g'
22 //
23 // Example input:
24 // foo 1
25 // bar 10
26 // foo 1000
27 // baz 1
28 //
29 // Example output;
30 // g foo 1001
31 // g bar 10
32 // g baz 1
33 // 1 foo 1000
34 // 1 bar 100
35
36 #include <iostream>
37 #include <map>
38 #include <string>
39 #include <thread>
40
41 #include <unistd.h>
42
43 #include "src/profiling/memory/sampler.h"
44
45 #include "perfetto/base/logging.h"
46
47 namespace perfetto {
48 namespace profiling {
49 namespace {
50
51 constexpr uint64_t kDefaultSamplingInterval = 128000;
52
ProfilingSampleDistributionMain(int argc,char ** argv)53 int ProfilingSampleDistributionMain(int argc, char** argv) {
54 int opt;
55 uint64_t sampling_interval = kDefaultSamplingInterval;
56 uint64_t times = 1;
57 uint64_t init_seed = 1;
58
59 while ((opt = getopt(argc, argv, "t:i:s:")) != -1) {
60 switch (opt) {
61 case 'i': {
62 char* end;
63 long long sampling_interval_arg = strtoll(optarg, &end, 10);
64 if (*end != '\0' || *optarg == '\0')
65 PERFETTO_FATAL("Invalid sampling interval: %s", optarg);
66 PERFETTO_CHECK(sampling_interval_arg > 0);
67 sampling_interval = static_cast<uint64_t>(sampling_interval_arg);
68 break;
69 }
70 case 't': {
71 char* end;
72 long long times_arg = strtoll(optarg, &end, 10);
73 if (*end != '\0' || *optarg == '\0')
74 PERFETTO_FATAL("Invalid times: %s", optarg);
75 PERFETTO_CHECK(times_arg > 0);
76 times = static_cast<uint64_t>(times_arg);
77 break;
78 }
79 case 's': {
80 char* end;
81 init_seed = static_cast<uint64_t>(strtoll(optarg, &end, 10));
82 if (*end != '\0' || *optarg == '\0')
83 PERFETTO_FATAL("Invalid seed: %s", optarg);
84 break;
85 }
86
87 default:
88 PERFETTO_FATAL("%s [-t times] [-i interval] [-s seed]", argv[0]);
89 }
90 }
91
92 std::vector<std::pair<std::string, uint64_t>> allocations;
93
94 while (std::cin) {
95 std::string callsite;
96 uint64_t size;
97 std::cin >> callsite;
98 if (std::cin.fail()) {
99 // Skip trailing newline.
100 if (std::cin.eof())
101 break;
102 PERFETTO_FATAL("Could not read callsite");
103 }
104 std::cin >> size;
105 if (std::cin.fail())
106 PERFETTO_FATAL("Could not read size");
107 allocations.emplace_back(std::move(callsite), size);
108 }
109 std::map<std::string, uint64_t> total_ground_truth;
110 for (const auto& pair : allocations)
111 total_ground_truth[pair.first] += pair.second;
112
113 for (const auto& pair : total_ground_truth)
114 std::cout << "g " << pair.first << " " << pair.second << std::endl;
115
116 while (times-- > 0) {
117 Sampler sampler(sampling_interval);
118 std::map<std::string, uint64_t> totals;
119 for (const auto& pair : allocations) {
120 size_t sample_size = sampler.SampleSize(pair.second);
121 // We also want to add 0 to make downstream processing easier, making
122 // sure every iteration has an entry for every key, even if it is
123 // zero.
124 totals[pair.first] += sample_size;
125 }
126
127 for (const auto& pair : totals)
128 std::cout << times << " " << pair.first << " " << pair.second
129 << std::endl;
130 }
131
132 return 0;
133 }
134
135 } // namespace
136 } // namespace profiling
137 } // namespace perfetto
138
main(int argc,char ** argv)139 int main(int argc, char** argv) {
140 return perfetto::profiling::ProfilingSampleDistributionMain(argc, argv);
141 }
142