summaryrefslogtreecommitdiff
path: root/simpleperf/InplaceSamplerClient.cpp
blob: 5a788613d0753e45afed0b03a759aa2e6406229a (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
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
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * 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.
 */

#include "InplaceSamplerClient.h"

#include <sys/time.h>
#include <sys/types.h>
#include <stdint.h>

#include <memory>
#include <vector>

#include "environment.h"
#include "utils.h"

static constexpr uint64_t EVENT_ID_FOR_INPLACE_SAMPLER = ULONG_MAX;

std::unique_ptr<InplaceSamplerClient> InplaceSamplerClient::Create(const perf_event_attr& attr,
                                                                   pid_t pid,
                                                                   const std::set<pid_t>& tids) {
  if (pid == -1) {
    LOG(ERROR) << "inplace-sampler can't monitor system wide events.";
    return nullptr;
  }
  std::unique_ptr<InplaceSamplerClient> sampler(new InplaceSamplerClient(attr, pid, tids));
  if (!sampler->ConnectServer()) {
    return nullptr;
  }
  if (!sampler->StartProfiling()) {
    return nullptr;
  }
  return sampler;
}

InplaceSamplerClient::InplaceSamplerClient(const perf_event_attr& attr, pid_t pid,
                                           const std::set<pid_t>& tids)
    : attr_(attr), pid_(pid), tids_(tids), closed_(false) {
}

uint64_t InplaceSamplerClient::Id() const {
  return EVENT_ID_FOR_INPLACE_SAMPLER;
}

bool InplaceSamplerClient::ConnectServer() {
  return true;
}

bool InplaceSamplerClient::StartProfiling() {
  return true;
}

bool InplaceSamplerClient::StartPolling(IOEventLoop& loop,
                                        const std::function<bool(Record*)>& record_callback,
                                        const std::function<bool()>& close_callback) {
  record_callback_ = record_callback;
  close_callback_ = close_callback;
  auto callback = [this]() {
    // Fake records for testing.
    uint64_t time = GetSystemClock();
    CommRecord comm_r(attr_, pid_, pid_, "fake_comm", Id(), time);
    if (!record_callback_(&comm_r)) {
      return false;
    }
    MmapRecord mmap_r(attr_, false, pid_, pid_, 0x1000, 0x1000, 0x0, "fake_elf", Id(), time);
    if (!record_callback_(&mmap_r)) {
      return false;
    }
    std::vector<uint64_t> ips(1, 0x1000);
    SampleRecord r(attr_, Id(), ips[0], pid_, pid_, time, 0, 1, ips);
    if (!record_callback_(&r)) {
      return false;
    }
    closed_ = true;
    return close_callback_();
  };
  timeval duration;
  duration.tv_sec = 0;
  duration.tv_usec = 1000;
  return loop.AddPeriodicEvent(duration, callback);
}