blob: 9fd140e5f7df2b10538346037de07017d1ccbab2 [file] [log] [blame]
Florian Mayerbb54f5e2018-10-22 12:13:03 +01001/*
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>
Florian Mayere1dfc562019-08-29 15:07:43 +010037#include <map>
Florian Mayerbb54f5e2018-10-22 12:13:03 +010038#include <string>
39#include <thread>
40
41#include <unistd.h>
42
Florian Mayerbb54f5e2018-10-22 12:13:03 +010043#include "src/profiling/memory/sampler.h"
44
45#include "perfetto/base/logging.h"
46
47namespace perfetto {
Florian Mayerf4318132018-10-22 15:22:42 +010048namespace profiling {
Florian Mayerbb54f5e2018-10-22 12:13:03 +010049namespace {
50
Florian Mayerf4318132018-10-22 15:22:42 +010051constexpr uint64_t kDefaultSamplingInterval = 128000;
Florian Mayerbb54f5e2018-10-22 12:13:03 +010052
53int ProfilingSampleDistributionMain(int argc, char** argv) {
54 int opt;
Florian Mayerf4318132018-10-22 15:22:42 +010055 uint64_t sampling_interval = kDefaultSamplingInterval;
Florian Mayerbb54f5e2018-10-22 12:13:03 +010056 uint64_t times = 1;
57 uint64_t init_seed = 1;
58
Florian Mayerf4318132018-10-22 15:22:42 +010059 while ((opt = getopt(argc, argv, "t:i:s:")) != -1) {
Florian Mayerbb54f5e2018-10-22 12:13:03 +010060 switch (opt) {
Florian Mayerf4318132018-10-22 15:22:42 +010061 case 'i': {
Florian Mayerbb54f5e2018-10-22 12:13:03 +010062 char* end;
Florian Mayerf4318132018-10-22 15:22:42 +010063 long long sampling_interval_arg = strtoll(optarg, &end, 10);
Florian Mayerbb54f5e2018-10-22 12:13:03 +010064 if (*end != '\0' || *optarg == '\0')
Florian Mayerf4318132018-10-22 15:22:42 +010065 PERFETTO_FATAL("Invalid sampling interval: %s", optarg);
66 PERFETTO_CHECK(sampling_interval_arg > 0);
67 sampling_interval = static_cast<uint64_t>(sampling_interval_arg);
Florian Mayerbb54f5e2018-10-22 12:13:03 +010068 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:
Florian Mayerf4318132018-10-22 15:22:42 +010088 PERFETTO_FATAL("%s [-t times] [-i interval] [-s seed]", argv[0]);
Florian Mayerbb54f5e2018-10-22 12:13:03 +010089 }
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
Florian Mayerbb54f5e2018-10-22 12:13:03 +0100116 while (times-- > 0) {
Florian Mayere1dfc562019-08-29 15:07:43 +0100117 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 }
Florian Mayerbb54f5e2018-10-22 12:13:03 +0100126
Florian Mayere1dfc562019-08-29 15:07:43 +0100127 for (const auto& pair : totals)
128 std::cout << times << " " << pair.first << " " << pair.second
129 << std::endl;
Florian Mayerbb54f5e2018-10-22 12:13:03 +0100130 }
131
132 return 0;
133}
134
135} // namespace
Florian Mayerf4318132018-10-22 15:22:42 +0100136} // namespace profiling
Florian Mayerbb54f5e2018-10-22 12:13:03 +0100137} // namespace perfetto
138
139int main(int argc, char** argv) {
Florian Mayerf4318132018-10-22 15:22:42 +0100140 return perfetto::profiling::ProfilingSampleDistributionMain(argc, argv);
Florian Mayerbb54f5e2018-10-22 12:13:03 +0100141}