-
Notifications
You must be signed in to change notification settings - Fork 1
/
memory_sampler.cc
60 lines (46 loc) · 1.65 KB
/
memory_sampler.cc
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
// Copyright 2020 The Chromium OS 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 "ml_benchmark/memory_sampler.h"
#include <base/task/task_traits.h>
#include <base/task/thread_pool.h>
#include <algorithm>
#include "ml_benchmark/sysmetrics.h"
namespace ml_benchmark {
PeakMemorySampler::PeakMemorySampler() : from_here_(FROM_HERE) {
task_runner_ = base::ThreadPool::CreateSequencedTaskRunner(
{base::MayBlock(), base::TaskPriority::BEST_EFFORT,
base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN});
}
PeakMemorySampler::~PeakMemorySampler() {
SetRunning(false);
}
void PeakMemorySampler::SetRunning(bool is_running) {
base::AutoLock auto_lock(lock_);
running_ = is_running;
}
void PeakMemorySampler::StartSampling(
scoped_refptr<PeakMemorySampler> sampler) {
sampler->SetRunning(true);
SampleMemory(sampler);
}
void PeakMemorySampler::StopSampling(scoped_refptr<PeakMemorySampler> sampler) {
sampler->SetRunning(false);
}
int64_t PeakMemorySampler::GetMaxSample() {
// Writing to max_sample_ is protected by this lock as well.
base::AutoLock auto_lock(lock_);
return max_sample_;
}
void PeakMemorySampler::SampleMemory(scoped_refptr<PeakMemorySampler> sampler) {
base::AutoLock auto_lock(sampler->lock_);
if (!sampler->running_)
return;
sampler->sample_counter_++;
sampler->max_sample_ = std::max(sampler->max_sample_, GetSwapAndRSSBytes());
sampler->task_runner_->PostDelayedTask(
sampler->from_here_,
base::Bind(&PeakMemorySampler::SampleMemory, sampler),
sampler->sampling_interval_);
}
} // namespace ml_benchmark