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 <string>
38 #include <thread>
39
40 #include <unistd.h>
41
42 #include "src/profiling/memory/client.h"
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 std::default_random_engine seed_engine(init_seed);
117
118 while (times-- > 0) {
119 PThreadKey key(ThreadLocalSamplingData::KeyDestructor);
120 ThreadLocalSamplingData::seed = seed_engine();
121 // We want to use the same API here that the client uses, which involves
122 // TLS. In order to destruct that TLS, we need to spawn a thread because
123 // pthread_key_delete does not delete any associated data, but rather it
124 // gets deleted when the owning thread terminates.
125 //
126 // Sad times.
127 std::thread th([&] {
128 if (!key.valid())
129 PERFETTO_FATAL("Failed to initialize TLS.");
130
131 std::map<std::string, uint64_t> totals;
132 for (const auto& pair : allocations) {
133 size_t sample_size =
134 SampleSize(key.get(), pair.second, sampling_interval, malloc, free);
135 // We also want to add 0 to make downstream processing easier, making
136 // sure every iteration has an entry for every key, even if it is
137 // zero.
138 totals[pair.first] += sample_size;
139 }
140
141 for (const auto& pair : totals)
142 std::cout << times << " " << pair.first << " " << pair.second
143 << std::endl;
144 });
145 th.join();
146 }
147
148 return 0;
149 }
150
151 } // namespace
152 } // namespace profiling
153 } // namespace perfetto
154
main(int argc,char ** argv)155 int main(int argc, char** argv) {
156 return perfetto::profiling::ProfilingSampleDistributionMain(argc, argv);
157 }
158