File: profile_writer.cc

package info (click to toggle)
autofdo 0.18-2
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 92,568 kB
  • sloc: cpp: 26,830; sh: 12,538; makefile: 344; ansic: 134; python: 95
file content (470 lines) | stat: -rw-r--r-- 15,843 bytes parent folder | download
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
// Copyright 2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Write profile to afdo file.

#include <stdio.h>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include <algorithm>

#include "gflags/gflags.h"
#include "base/common.h"
#include "gcov.h"
#include "symbol_map.h"
#include "module_grouper.h"
#include "profile_writer.h"

// sizeof(gcov_unsigned_t)
#define SIZEOF_UNSIGNED 4

DEFINE_bool(debug_dump, false,
            "If set, emit additional debugging dumps to stderr.");

namespace autofdo {
// Opens the output file, and writes the header.
bool AutoFDOProfileWriter::WriteHeader(const string &output_filename,
                                       const string &imports_filename) {
  if (gcov_open(output_filename.c_str(), -1) == -1) {
    LOG(FATAL) << "Cannot open file " << output_filename;
    return false;
  }

  imports_file_ = fopen(imports_filename.c_str(), "w");
  if (imports_file_ == NULL) {
    LOG(FATAL) << "Cannot open file " << imports_filename;
    return false;
  }

  gcov_write_unsigned(GCOV_DATA_MAGIC);
  gcov_write_unsigned(gcov_version_);
  gcov_write_unsigned(0);
  return true;
}

// Finishes writing, closes the output file.
bool AutoFDOProfileWriter::WriteFinish() {
  if (gcov_close()) {
    LOG(ERROR) << "Cannot close the gcov file.";
    return false;
  }
  fclose(imports_file_);
  return true;
}

class SourceProfileLengther: public SymbolTraverser {
 public:
  explicit SourceProfileLengther(const SymbolMap &symbol_map)
      : length_(0), num_functions_(0) {
    Start(symbol_map);
  }

  int length() {return length_ + num_functions_ * 2;}
  int num_functions() {return num_functions_;}

 protected:
  virtual void VisitTopSymbol(const string &name, const Symbol *node) {
    num_functions_++;
  }

  virtual void Visit(const Symbol *node) {
    // func_name, num_pos_counts, num_callsites
    length_ += 3;
    // offset_discr, num_targets, count * 2
    length_ += node->pos_counts.size() * 4;
    // offset_discr
    length_ += node->callsites.size();
    for (const auto &pos_count : node->pos_counts) {
      // type, func_name * 2, count * 2
      length_ += pos_count.second.target_map.size() * 5;
    }
  }

 private:
  int length_;
  int num_functions_;
  DISALLOW_COPY_AND_ASSIGN(SourceProfileLengther);
};

class SourceProfileWriter: public SymbolTraverser {
 public:
  static void Write(const SymbolMap &symbol_map, const StringIndexMap &map) {
    SourceProfileWriter writer(map);
    writer.Start(symbol_map);
  }

 protected:
  virtual void Visit(const Symbol *node) {
    gcov_write_unsigned(node->pos_counts.size());
    gcov_write_unsigned(node->callsites.size());
    for (const auto &pos_count : node->pos_counts) {
      gcov_write_unsigned(pos_count.first);
      gcov_write_unsigned(pos_count.second.target_map.size());
      gcov_write_counter(pos_count.second.count);
      TargetCountPairs target_counts;
      GetSortedTargetCountPairs(pos_count.second.target_map, &target_counts);
      for (const auto &target_count : pos_count.second.target_map) {
        gcov_write_unsigned(HIST_TYPE_INDIR_CALL_TOPN);
        gcov_write_counter(GetStringIndex(target_count.first));
        gcov_write_counter(target_count.second);
      }
    }
  }

  virtual void VisitTopSymbol(const string &name, const Symbol *node) {
    gcov_write_counter(node->head_count);
    gcov_write_unsigned(GetStringIndex(Symbol::Name(name.c_str())));
  }

  virtual void VisitCallsite(const Callsite &callsite) {
    gcov_write_unsigned(callsite.first);
    gcov_write_unsigned(GetStringIndex(Symbol::Name(callsite.second)));
  }

 private:
  explicit SourceProfileWriter(const StringIndexMap &map) : map_(map) {}

  int GetStringIndex(const string &str) {
    StringIndexMap::const_iterator ret = map_.find(str);
    CHECK(ret != map_.end());
    return ret->second;
  }

  const StringIndexMap &map_;
  DISALLOW_COPY_AND_ASSIGN(SourceProfileWriter);
};

void AutoFDOProfileWriter::WriteFunctionProfile() {
  typedef std::map<string, int> StringIndexMap;
  // Map from a string to its index in this map. Providing a partial
  // ordering of all output strings.
  StringIndexMap string_index_map;
  int length_4bytes = 0, current_name_index = 0;
  string_index_map[string()] = 0;

  StringTableUpdater::Update(*symbol_map_, &string_index_map);

  for (auto &name_index : string_index_map) {
    name_index.second = current_name_index++;
    length_4bytes += (name_index.first.size()
                      + SIZEOF_UNSIGNED) / SIZEOF_UNSIGNED;
    length_4bytes += 1;
  }
  length_4bytes += 1;

  // Writes the GCOV_TAG_AFDO_FILE_NAMES section.
  gcov_write_unsigned(GCOV_TAG_AFDO_FILE_NAMES);
  gcov_write_unsigned(length_4bytes);
  gcov_write_unsigned(string_index_map.size());
  for (const auto &name_index : string_index_map) {
    char *c = strdup(name_index.first.c_str());
    int len = strlen(c);
    // Workaround https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64346
    // We should not have D4Ev in our profile because it does not exist
    // in symbol table and would lead to undefined symbols during linking.
    if (len > 5 &&
        (!strcmp(c + len - 4, "D4Ev") || !strcmp(c + len - 4, "C4Ev"))) {
      c[len - 3] = '2';
    } else if (len > 7 && !strcmp(c + len - 6, "C4EPKc")) {
      c[len - 5] = '2';
    } else if (len > 12 && !strcmp(c + len - 11, "C4EPKcRKS2_")) {
      c[len - 10] = '2';
    }
    gcov_write_string(c);
    free(c);
  }

  // Compute the length of the GCOV_TAG_AFDO_FUNCTION section.
  SourceProfileLengther length(*symbol_map_);
  gcov_write_unsigned(GCOV_TAG_AFDO_FUNCTION);
  gcov_write_unsigned(length.length() + 1);
  gcov_write_unsigned(length.num_functions());
  SourceProfileWriter::Write(*symbol_map_, string_index_map);
}

void AutoFDOProfileWriter::WriteModuleGroup() {
  gcov_write_unsigned(GCOV_TAG_MODULE_GROUPING);

  // Calculates the length of the section.
  // Initial value: module count (an unsigned integer).
  int length_4bytes = 1;
  int num_modules = 0;
  // name_string_length, is_exported, has_sym, aux_module_size,
  // quote_paths_size, bracket_paths_size, system_paths_size,
  // cpp_defines_size, cpp_includes_size,
  // cl_args_size
  static const uint32 MODULE_AUX_DATA_SIZE_IN_4BYTES = 9;
  std::set<string> names;
  for (const auto &module_aux : *module_map_) {
    names.insert(module_aux.first);
  }
  for (const string &name : names) {
    const Module &module = module_map_->find(name)->second;
    if (!module.is_exported
        && module.aux_modules.size() == 0) {
      continue;
    }
    if (module.is_fake) {
      continue;
    }
    num_modules++;
    length_4bytes += (name.size() + SIZEOF_UNSIGNED)
                     / SIZEOF_UNSIGNED;
    length_4bytes += MODULE_AUX_DATA_SIZE_IN_4BYTES;
    length_4bytes += module.aux_modules.size();
    for (const auto &aux : module.aux_modules) {
      length_4bytes += (aux.size() + SIZEOF_UNSIGNED) / SIZEOF_UNSIGNED;
    }
    int num_string = module.num_quote_paths
                   + module.num_bracket_paths
                   + module.num_system_paths
                   + module.num_cpp_defines
                   + module.num_cpp_includes
                   + module.num_cl_args;
    length_4bytes += num_string;
    for (int i = 0; i < num_string; i++) {
      length_4bytes += (module.options[i].second.size()
                        + SIZEOF_UNSIGNED) / SIZEOF_UNSIGNED;
    }
  }

  // Writes to .afdo file and .afdo.imports file.
  gcov_write_unsigned(length_4bytes);
  gcov_write_unsigned(num_modules);
  for (const string &name : names) {
    const Module &module = module_map_->find(name)->second;
    if (!module.is_exported
        && module.aux_modules.size() == 0) {
      continue;
    }
    if (module.is_fake) {
      continue;
    }

    // Writes the name of the module.
    gcov_write_string(name.c_str());
    if (module.aux_modules.size() > 0) {
      fprintf(imports_file_, "%s:", name.c_str());
    }

    // Writes the "is_exported" flag.
    if (module.is_exported) {
      gcov_write_unsigned(1);
    } else {
      gcov_write_unsigned(0);
    }

    gcov_write_unsigned(module.lang);
    gcov_write_unsigned(module.ggc_memory_in_kb);

    // Writes the aux module info.
    gcov_write_unsigned(module.aux_modules.size());
    gcov_write_unsigned(module.num_quote_paths);
    gcov_write_unsigned(module.num_bracket_paths);
    gcov_write_unsigned(module.num_system_paths);
    gcov_write_unsigned(module.num_cpp_defines);
    gcov_write_unsigned(module.num_cpp_includes);
    gcov_write_unsigned(module.num_cl_args);

    std::set<string> aux_modules(module.aux_modules.begin(),
                                 module.aux_modules.end());
    for (const auto &aux : module.aux_modules) {
      gcov_write_string(aux.c_str());
      fprintf(imports_file_, " %s", aux.c_str());
    }
    fprintf(imports_file_, "\n");

    // Writes the options recorded in the elf section of the original binary.
    int option_offset = 0;
    for (int i = 0; i < module.num_quote_paths; i++) {
      gcov_write_string(
          module.options[option_offset++].second.c_str());
    }
    for (int i = 0; i < module.num_bracket_paths; i++) {
      gcov_write_string(
          module.options[option_offset++].second.c_str());
    }
    for (int i = 0; i < module.num_system_paths; i++) {
      gcov_write_string(
          module.options[option_offset++].second.c_str());
    }
    for (int i = 0; i < module.num_cpp_defines; i++) {
      gcov_write_string(
          module.options[option_offset++].second.c_str());
    }
    for (int i = 0; i < module.num_cpp_includes; i++) {
      gcov_write_string(
          module.options[option_offset++].second.c_str());
    }
    for (int i = 0; i < module.num_cl_args; i++) {
      gcov_write_string(
          module.options[option_offset++].second.c_str());
    }
  }
}

void AutoFDOProfileWriter::WriteWorkingSet() {
  gcov_write_unsigned(GCOV_TAG_AFDO_WORKING_SET);
  gcov_write_unsigned(3 * NUM_GCOV_WORKING_SETS);
  const gcov_working_set_info *working_set = symbol_map_->GetWorkingSets();
  for (int i = 0; i < NUM_GCOV_WORKING_SETS; i++) {
    gcov_write_unsigned(working_set[i].num_counters / WORKING_SET_INSN_PER_BB);
    gcov_write_counter(working_set[i].min_counter);
  }
}

bool AutoFDOProfileWriter::WriteToFile(const string &output_filename) {
  if (!WriteHeader(output_filename, output_filename + ".imports")) {
    return false;
  }
  WriteFunctionProfile();
  WriteModuleGroup();
  WriteWorkingSet();
  if (!WriteFinish()) {
    return false;
  }
  return true;
}

// Debugging support.  ProfileDumper emits a detailed dump of the contents
// of the input profile.
class ProfileDumper : public SymbolTraverser {
 public:
  static void Write(const SymbolMap &symbol_map, const StringIndexMap &map) {
    ProfileDumper writer(map);
    writer.Start(symbol_map);
  }

 protected:
  void DumpSourceInfo(SourceInfo info, int indent) {
    printf("%*sDirectory name: %s\n", indent, " ", info.dir_name);
    printf("%*sFile name:      %s\n", indent, " ", info.file_name);
    printf("%*sFunction name:  %s\n", indent, " ", info.func_name);
    printf("%*sStart line:     %u\n", indent, " ", info.start_line);
    printf("%*sLine:           %u\n", indent, " ", info.line);
    printf("%*sDiscriminator:  %u\n", indent, " ", info.discriminator);
  }

  void PrintSourceLocation(uint32 start_line, uint32 offset) {
    if (offset & 0xffff) {
      printf("%u.%u: ", (offset >> 16) + start_line, offset & 0xffff);
    } else {
      printf("%u: ", (offset >> 16) + start_line);
    }
  }

  virtual void Visit(const Symbol *node) {
    printf("Writing symbol: ");
    node->Dump(4);
    printf("\n");
    printf("Source information:\n");
    DumpSourceInfo(node->info, 0);
    printf("\n");
    printf("Total sampled count:            %llu\n",
           static_cast<uint64>(node->total_count));
    printf("Total sampled count in head bb: %llu\n",
           static_cast<uint64>(node->head_count));
    printf("\n");
    printf("Call sites:\n");
    int i = 0;
    for (const auto &callsite_symbol : node->callsites) {
      Callsite site = callsite_symbol.first;
      Symbol *symbol = callsite_symbol.second;
      printf("  #%d: site\n", i);
      printf("    uint32: %u\n", site.first);
      printf("    const char *: %s\n", site.second);
      printf("  #%d: symbol: ", i);
      symbol->Dump(0);
      printf("\n");
      i++;
    }

    printf("node->pos_counts.size() = %llu\n",
           static_cast<uint64>(node->pos_counts.size()));
    printf("node->callsites.size() = %llu\n",
           static_cast<uint64>(node->callsites.size()));
    std::vector<uint32> positions;
    for (const auto &pos_count : node->pos_counts)
      positions.push_back(pos_count.first);
    std::sort(positions.begin(), positions.end());
    i = 0;
    for (const auto &pos : positions) {
      PositionCountMap::const_iterator pos_count = node->pos_counts.find(pos);
      DCHECK(pos_count != node->pos_counts.end());
      uint32 location = pos_count->first;
      ProfileInfo info = pos_count->second;

      printf("#%d: location (line[.discriminator]) = ", i);
      PrintSourceLocation(node->info.start_line, location);
      printf("\n");
      printf("#%d: profile info execution count = %llu\n", i, info.count);
      printf("#%d: profile info number of instructions = %llu\n", i,
             info.num_inst);
      TargetCountPairs target_counts;
      GetSortedTargetCountPairs(info.target_map, &target_counts);
      printf("#%d: profile info target map size = %llu\n", i,
             static_cast<uint64>(info.target_map.size()));
      printf("#%d: info.target_map:\n", i);
      for (const auto &target_count : info.target_map) {
        printf("\tGetStringIndex(target_count.first): %d\n",
               GetStringIndex(target_count.first));
        printf("\ttarget_count.second: %llu\n", target_count.second);
      }
      printf("\n");
      i++;
    }
  }

  virtual void VisitTopSymbol(const string &name, const Symbol *node) {
    printf("VisitTopSymbol: %s\n", name.c_str());
    node->Dump(0);
    printf("node->head_count: %llu\n", node->head_count);
    printf("GetStringIndex(%s): %u\n", name.c_str(), GetStringIndex(name));
    printf("\n");
  }

  virtual void VisitCallsite(const Callsite &callsite) {
    printf("VisitCallSite: %s\n", callsite.second);
    printf("callsite.first: %u\n", callsite.first);
    printf("GetStringIndex(callsite.second): %u\n",
           GetStringIndex(callsite.second ? callsite.second : string()));
  }

 private:
  explicit ProfileDumper(const StringIndexMap &map) : map_(map) {}

  int GetStringIndex(const string &str) {
    StringIndexMap::const_iterator ret = map_.find(str);
    CHECK(ret != map_.end());
    return ret->second;
  }

  const StringIndexMap &map_;
  DISALLOW_COPY_AND_ASSIGN(ProfileDumper);
};

// Emit a dump of the input profile on stdout.
void ProfileWriter::Dump() {
  StringIndexMap string_index_map;
  StringTableUpdater::Update(*symbol_map_, &string_index_map);
  SourceProfileLengther length(*symbol_map_);
  printf("Length of symbol map: %d\n", length.length() + 1);
  printf("Number of functions:  %d\n", length.num_functions());
  ProfileDumper::Write(*symbol_map_, string_index_map);
}

}  // namespace autofdo