From a4f711b0fa38aecf0aacdbf61acf86bd25aa674a Mon Sep 17 00:00:00 2001 From: Moritz Pflanzer Date: Wed, 5 Jul 2017 11:02:23 +0100 Subject: COMPMID-415: New framework - instruments [3/5] Change-Id: I834c2693566185ce8d8d024fb5db79610a18c8c1 Reviewed-on: http://mpd-gerrit.cambridge.arm.com/79757 Reviewed-by: Anthony Barbier Tested-by: Kaizen --- framework/instruments/Instrument.h | 98 ++++++++++++++++++++++ framework/instruments/Instruments.cpp | 57 +++++++++++++ framework/instruments/Instruments.h | 105 +++++++++++++++++++++++ framework/instruments/PMUCounter.cpp | 140 +++++++++++++++++++++++++++++++ framework/instruments/PMUCounter.h | 71 ++++++++++++++++ framework/instruments/WallClockTimer.cpp | 57 +++++++++++++ framework/instruments/WallClockTimer.h | 53 ++++++++++++ 7 files changed, 581 insertions(+) create mode 100644 framework/instruments/Instrument.h create mode 100644 framework/instruments/Instruments.cpp create mode 100644 framework/instruments/Instruments.h create mode 100644 framework/instruments/PMUCounter.cpp create mode 100644 framework/instruments/PMUCounter.h create mode 100644 framework/instruments/WallClockTimer.cpp create mode 100644 framework/instruments/WallClockTimer.h (limited to 'framework/instruments') diff --git a/framework/instruments/Instrument.h b/framework/instruments/Instrument.h new file mode 100644 index 0000000000..895a64738c --- /dev/null +++ b/framework/instruments/Instrument.h @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2017 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef ARM_COMPUTE_TEST_INSTRUMENT +#define ARM_COMPUTE_TEST_INSTRUMENT + +#include "../Utils.h" + +#include +#include +#include + +namespace arm_compute +{ +namespace test +{ +namespace framework +{ +/** Interface for classes that can be used to measure performance. */ +class Instrument +{ +public: + /** Helper function to create an instrument of the given type. + * + * @return Instance of an instrument of the given type. + */ + template + static std::unique_ptr make_instrument(); + + /** Struct representing measurement consisting of value and unit. */ + struct Measurement final + { + Measurement(double value, std::string unit) + : value{ value }, unit{ std::move(unit) } + { + } + + friend std::ostream &operator<<(std::ostream &os, const Measurement &measurement); + + double value; + std::string unit; + }; + + Instrument() = default; + Instrument(const Instrument &) = default; + Instrument(Instrument &&) = default; + Instrument &operator=(const Instrument &) = default; + Instrument &operator=(Instrument &&) = default; + virtual ~Instrument() = default; + + /** Identifier for the instrument */ + virtual std::string id() const = 0; + + /** Start measuring. */ + virtual void start() = 0; + + /** Stop measuring. */ + virtual void stop() = 0; + + /** Return the latest measurement. */ + virtual Measurement measurement() const = 0; +}; + +inline std::ostream &operator<<(std::ostream &os, const Instrument::Measurement &measurement) +{ + os << measurement.value << measurement.unit; + return os; +} + +template +inline std::unique_ptr Instrument::make_instrument() +{ + return support::cpp14::make_unique(); +} +} // namespace framework +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_INSTRUMENT */ diff --git a/framework/instruments/Instruments.cpp b/framework/instruments/Instruments.cpp new file mode 100644 index 0000000000..7a26944d26 --- /dev/null +++ b/framework/instruments/Instruments.cpp @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2017 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "Instruments.h" + +#include +#include + +namespace arm_compute +{ +namespace test +{ +namespace framework +{ +InstrumentType instrument_type_from_name(const std::string &name) +{ + static const std::map types = + { + { "all", InstrumentType::ALL }, + { "none", InstrumentType::NONE }, + { "wall_clock", InstrumentType::WALL_CLOCK_TIMER }, + { "cycles", InstrumentType::PMU_CYCLE_COUNTER }, + { "instructions", InstrumentType::PMU_INSTRUCTION_COUNTER }, + }; + + try + { + return types.at(name); + } + catch(const std::out_of_range &) + { + throw std::invalid_argument(name); + } +} +} // namespace framework +} // namespace test +} // namespace arm_compute diff --git a/framework/instruments/Instruments.h b/framework/instruments/Instruments.h new file mode 100644 index 0000000000..034fa168f5 --- /dev/null +++ b/framework/instruments/Instruments.h @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2017 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef ARM_COMPUTE_TEST_INSTRUMENTS +#define ARM_COMPUTE_TEST_INSTRUMENTS + +#include "PMUCounter.h" +#include "WallClockTimer.h" + +#include +#include + +namespace arm_compute +{ +namespace test +{ +namespace framework +{ +enum class InstrumentType : unsigned int +{ + ALL = ~0U, + NONE = 0, + WALL_CLOCK_TIMER = 1, + PMU_CYCLE_COUNTER = 2, + PMU_INSTRUCTION_COUNTER = 4 +}; + +InstrumentType instrument_type_from_name(const std::string &name); + +inline InstrumentType operator&(InstrumentType t1, InstrumentType t2) +{ + using type = std::underlying_type::type; + return static_cast(static_cast(t1) & static_cast(t2)); +} + +inline InstrumentType operator|(InstrumentType t1, InstrumentType t2) +{ + using type = std::underlying_type::type; + return static_cast(static_cast(t1) | static_cast(t2)); +} + +inline InstrumentType &operator|=(InstrumentType &t1, InstrumentType t2) +{ + using type = std::underlying_type::type; + t1 = static_cast(static_cast(t1) | static_cast(t2)); + return t1; +} + +inline ::std::stringstream &operator>>(::std::stringstream &stream, InstrumentType &instrument) +{ + std::string value; + stream >> value; + instrument = instrument_type_from_name(value); + return stream; +} + +inline ::std::stringstream &operator<<(::std::stringstream &stream, InstrumentType instrument) +{ + switch(instrument) + { + case InstrumentType::WALL_CLOCK_TIMER: + stream << "WALL_CLOCK_TIMER"; + break; + case InstrumentType::PMU_CYCLE_COUNTER: + stream << "PMU_CYCLE_COUNTER"; + break; + case InstrumentType::PMU_INSTRUCTION_COUNTER: + stream << "PMU_INSTRUCTION_COUNTER"; + break; + case InstrumentType::ALL: + stream << "ALL"; + break; + case InstrumentType::NONE: + stream << "NONE"; + break; + default: + throw std::invalid_argument("Unsupported instrument type"); + } + + return stream; +} +} // namespace framework +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_INSTRUMENTS */ diff --git a/framework/instruments/PMUCounter.cpp b/framework/instruments/PMUCounter.cpp new file mode 100644 index 0000000000..7994a15862 --- /dev/null +++ b/framework/instruments/PMUCounter.cpp @@ -0,0 +1,140 @@ +/* + * Copyright (c) 2017 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "PMUCounter.h" + +#define _GNU_SOURCE 1 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#undef _GNU_SOURCE + +#include + +namespace arm_compute +{ +namespace test +{ +namespace framework +{ +CycleCounter::CycleCounter() +{ + const pid_t pid = getpid(); + + struct perf_event_attr perf_config; //NOLINT + memset(&perf_config, 0, sizeof(struct perf_event_attr)); + + perf_config.config = PERF_COUNT_HW_CPU_CYCLES; + perf_config.size = sizeof(struct perf_event_attr); + perf_config.type = PERF_TYPE_HARDWARE; + // The inherit bit specifies that this counter should count events of child + // tasks as well as the task specified + perf_config.inherit = 1; + // Enables saving of event counts on context switch for inherited tasks + perf_config.inherit_stat = 1; + + _fd = syscall(__NR_perf_event_open, &perf_config, pid, -1, -1, 0); + + if(_fd < 0) + { + throw std::runtime_error("perf_event_open for cycles failed"); + } +} + +std::string CycleCounter::id() const +{ + return "Cycle Counter"; +} + +void CycleCounter::start() +{ + ioctl(_fd, PERF_EVENT_IOC_RESET, 0); + ioctl(_fd, PERF_EVENT_IOC_ENABLE, 0); +} + +void CycleCounter::stop() +{ + ioctl(_fd, PERF_EVENT_IOC_DISABLE, 0); + read(_fd, &_cycles, sizeof(_cycles)); +} + +Instrument::Measurement CycleCounter::measurement() const +{ + return Measurement(_cycles, "cycles"); +} + +InstructionCounter::InstructionCounter() +{ + const pid_t pid = getpid(); + + struct perf_event_attr perf_config; //NOLINT + memset(&perf_config, 0, sizeof(struct perf_event_attr)); + + perf_config.config = PERF_COUNT_HW_INSTRUCTIONS; + perf_config.size = sizeof(struct perf_event_attr); + perf_config.type = PERF_TYPE_HARDWARE; + // The inherit bit specifies that this counter should count events of child + // tasks as well as the task specified + perf_config.inherit = 1; + // Enables saving of event counts on context switch for inherited tasks + perf_config.inherit_stat = 1; + + _fd = syscall(__NR_perf_event_open, &perf_config, pid, -1, -1, 0); + + if(_fd < 0) + { + throw std::runtime_error("perf_event_open for instructions failed"); + } +} + +std::string InstructionCounter::id() const +{ + return "Instruction Counter"; +} + +void InstructionCounter::start() +{ + ioctl(_fd, PERF_EVENT_IOC_RESET, 0); + ioctl(_fd, PERF_EVENT_IOC_ENABLE, 0); +} + +void InstructionCounter::stop() +{ + ioctl(_fd, PERF_EVENT_IOC_DISABLE, 0); + read(_fd, &_instructions, sizeof(_instructions)); +} + +Instrument::Measurement InstructionCounter::measurement() const +{ + return Measurement(_instructions, "instructions"); +} +} // namespace framework +} // namespace test +} // namespace arm_compute diff --git a/framework/instruments/PMUCounter.h b/framework/instruments/PMUCounter.h new file mode 100644 index 0000000000..f407be602f --- /dev/null +++ b/framework/instruments/PMUCounter.h @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2017 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef ARM_COMPUTE_TEST_PMU_COUNTER +#define ARM_COMPUTE_TEST_PMU_COUNTER + +#include "Instrument.h" + +namespace arm_compute +{ +namespace test +{ +namespace framework +{ +/** Implementation of an instrument to count CPU cycles. */ +class CycleCounter : public Instrument +{ +public: + /** Initialise the cycle counter. */ + CycleCounter(); + + std::string id() const override; + void start() override; + void stop() override; + Measurement measurement() const override; + +private: + long _fd{ -1 }; + long long _cycles{ 0 }; +}; + +/** Implementation of an instrument to count executed CPU instructions. */ +class InstructionCounter : public Instrument +{ +public: + /** Initialise the instruction counter. */ + InstructionCounter(); + + std::string id() const override; + void start() override; + void stop() override; + Measurement measurement() const override; + +private: + long _fd{ -1 }; + long long _instructions{ 0 }; +}; +} // namespace framework +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_PMU_COUNTER */ diff --git a/framework/instruments/WallClockTimer.cpp b/framework/instruments/WallClockTimer.cpp new file mode 100644 index 0000000000..37db0c7f05 --- /dev/null +++ b/framework/instruments/WallClockTimer.cpp @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2017 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "WallClockTimer.h" + +#include "../Framework.h" +#include "../Utils.h" + +namespace arm_compute +{ +namespace test +{ +namespace framework +{ +std::string WallClockTimer::id() const +{ + return "Wall clock"; +} + +void WallClockTimer::start() +{ + _start = std::chrono::high_resolution_clock::now(); +} + +void WallClockTimer::stop() +{ + _stop = std::chrono::high_resolution_clock::now(); +} + +Instrument::Measurement WallClockTimer::measurement() const +{ + const auto delta = std::chrono::duration_cast(_stop - _start); + return Instrument::Measurement(delta.count(), "us"); +} +} // namespace framework +} // namespace test +} // namespace arm_compute diff --git a/framework/instruments/WallClockTimer.h b/framework/instruments/WallClockTimer.h new file mode 100644 index 0000000000..b7c390f691 --- /dev/null +++ b/framework/instruments/WallClockTimer.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2017 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef ARM_COMPUTE_TEST_WALL_CLOCK_TIMER +#define ARM_COMPUTE_TEST_WALL_CLOCK_TIMER + +#include "Instrument.h" + +#include + +namespace arm_compute +{ +namespace test +{ +namespace framework +{ +/** Implementation of an instrument to measure elapsed wall-clock time in milliseconds. */ +class WallClockTimer : public Instrument +{ +public: + std::string id() const override; + void start() override; + void stop() override; + Measurement measurement() const override; + +private: + std::chrono::high_resolution_clock::time_point _start{}; + std::chrono::high_resolution_clock::time_point _stop{}; +}; +} // namespace framework +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_WALL_CLOCK_TIMER */ -- cgit v1.2.1