2 * Copyright 2018 The TensorFlow Authors. All Rights Reserved.
3 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include "benchmark/Phases.h"
19 #include "benchmark/Types.h"
32 gettimeofday(&tv, nullptr);
33 return static_cast<uint64_t>(tv.tv_sec) * 1e6 + tv.tv_usec;
36 void SleepForMicros(uint64_t micros)
39 sleep_time.tv_sec = micros / 1e6;
40 micros -= sleep_time.tv_sec * 1e6;
41 sleep_time.tv_nsec = micros * 1e3;
42 nanosleep(&sleep_time, nullptr);
49 Phases::Phases(const PhaseOption &option) : _option(option)
53 _mem_poll = std::make_unique<MemoryPoller>(std::chrono::milliseconds(option.memory_interval),
58 void Phases::run(const std::string &tag, const PhaseFunc &exec, const PhaseFunc *post,
59 uint32_t loop_num, bool option_disable)
61 Phase phase{tag, loop_num};
62 PhaseEnum p = getPhaseEnum(tag);
63 for (uint32_t i = 0; i < loop_num; ++i)
65 if (!option_disable && _option.memory)
75 if (!option_disable && _option.memory)
78 phase.time.emplace_back(t);
80 if (!option_disable && _option.memory)
82 phase.memory[MemoryType::RSS].emplace_back(_mem_poll->getRssMap().at(p));
83 phase.memory[MemoryType::HWM].emplace_back(_mem_poll->getHwmMap().at(p));
84 phase.memory[MemoryType::PSS].emplace_back(_mem_poll->getPssMap().at(p));
90 if (_option.run_delay > 0 && p == PhaseEnum::EXECUTE && i != loop_num - 1)
92 SleepForMicros(_option.run_delay);
96 if (p == PhaseEnum::END_OF_PHASE)
101 assert(_phases.find(tag) == _phases.end());
102 _phases.emplace(tag, phase);
105 } // namespace benchmark