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
|
/*
* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
* Copyright (c) 2020 Samsung Electronics Co., Ltd. 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.
*/
#include "benchmark/Phases.h"
#include "benchmark/Types.h"
#include <cassert>
#include <chrono>
#include <iostream>
#include <sys/time.h>
namespace
{
uint64_t nowMicros()
{
struct timeval tv;
gettimeofday(&tv, nullptr);
return static_cast<uint64_t>(tv.tv_sec) * 1e6 + tv.tv_usec;
}
void SleepForMicros(uint64_t micros)
{
timespec sleep_time;
sleep_time.tv_sec = micros / 1e6;
micros -= sleep_time.tv_sec * 1e6;
sleep_time.tv_nsec = micros * 1e3;
nanosleep(&sleep_time, nullptr);
}
}
namespace benchmark
{
Phases::Phases(const PhaseOption &option) : _option(option)
{
if (_option.memory)
{
_mem_poll = std::make_unique<MemoryPoller>(std::chrono::milliseconds(option.memory_interval),
option.memory_gpu);
}
}
void Phases::run(const std::string &tag, const PhaseFunc &exec, const PhaseFunc *post,
uint32_t loop_num, bool option_disable)
{
Phase phase{tag, loop_num};
PhaseEnum p = getPhaseEnum(tag);
for (uint32_t i = 0; i < loop_num; ++i)
{
if (!option_disable && _option.memory)
_mem_poll->start(p);
uint64_t t = 0u;
t = nowMicros();
exec(phase, i);
t = nowMicros() - t;
if (!option_disable && _option.memory)
_mem_poll->end(p);
phase.time.emplace_back(t);
if (!option_disable && _option.memory)
{
phase.memory[MemoryType::RSS].emplace_back(_mem_poll->getRssMap().at(p));
phase.memory[MemoryType::HWM].emplace_back(_mem_poll->getHwmMap().at(p));
phase.memory[MemoryType::PSS].emplace_back(_mem_poll->getPssMap().at(p));
}
if (post)
(*post)(phase, i);
if (_option.run_delay > 0 && p == PhaseEnum::EXECUTE && i != loop_num - 1)
{
SleepForMicros(_option.run_delay);
}
}
if (p == PhaseEnum::END_OF_PHASE)
{
return;
}
assert(_phases.find(tag) == _phases.end());
_phases.emplace(tag, phase);
}
} // namespace benchmark
|