summaryrefslogtreecommitdiff
path: root/chromium/tools/cygprofile/delayed_dumper.cc
blob: 513b6a53a3f9c234ab523ae39ace24657734cf76 (plain)
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
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <time.h>
#include <unistd.h>

#include <thread>

#include "base/android/library_loader/anchor_functions.h"
#include "base/logging.h"
#include "build/build_config.h"
#include "tools/cygprofile/lightweight_cygprofile.h"

#if !defined(ARCH_CPU_ARMEL)
#error Only supported on ARM.
#endif  // !defined(ARCH_CPU_ARMEL)

namespace cygprofile {
namespace {

// Disables the recording of addresses after |kDelayInSeconds| and dumps the
// result to a file.
class DelayedDumper {
 public:
  DelayedDumper() {
    // Not using base::TimeTicks() to not call too many base:: symbol that would
    // pollute the reached symbols dumps.
    struct timespec ts;
    if (clock_gettime(CLOCK_MONOTONIC, &ts))
      PLOG(FATAL) << "clock_gettime.";
    uint64_t start_ns_since_epoch =
        static_cast<uint64_t>(ts.tv_sec) * 1000 * 1000 * 1000 + ts.tv_nsec;
    int pid = getpid();

    std::thread([pid, start_ns_since_epoch]() {
      sleep(kInitialDelayInSeconds);
      while (!SwitchToNextPhaseOrDump(pid, start_ns_since_epoch))
        sleep(kDelayInSeconds);
    })
        .detach();
  }

  static constexpr int kDelayInSeconds = 30;
  static constexpr int kInitialDelayInSeconds =
      kPhases == 1 ? kDelayInSeconds : 5;
};

// Static initializer on purpose. Will disable instrumentation after
// |kDelayInSeconds|.
DelayedDumper g_dump_later;

}  // namespace
}  // namespace cygprofile