<commit_before>/*
 * Copyright (c) 2017-2019 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.
 */

#pragma once

#include "hwc.hpp"
#include "instrument.h"
#include "measurement.h"

#include <map>
#include <vector>

/** Instrument implementation for mali hw counters. */
class MaliCounter : public Instrument
{
  public:
	/// @brief Construct a Mali counter.
	MaliCounter();

	/** Prevent instances of this class from being copy constructed */
	MaliCounter(const MaliCounter &) = delete;
	/** Prevent instances of this class from being copied */
	MaliCounter &operator=(const MaliCounter &) = delete;

	/** Default destructor */
	~MaliCounter();

	std::string     id() const override;
	void            start() override;
	void            stop() override;
	MeasurementsMap measurements() const override;

  private:
	void init();
	void term();

	void            sample_counters();
	void            wait_next_event();
	const uint32_t *get_counters() const;
	const uint32_t *get_counters(mali_userspace::MaliCounterBlockName block, int index = -1) const;
	int             find_counter_index_by_name(mali_userspace::MaliCounterBlockName block, const char *name);

	const std::vector<const std::pair<const char *, const char *>> _jm_counter_names{
	    {"GPU_ACTIVE", "cycles"},
	    {"JS0_JOBS", "jobs"},
	    {"JS1_JOBS", "jobs"}};
	const std::vector<const std::pair<const char *, const char *>> _mmu_counter_names{
	    {"L2_READ_LOOKUP", "cache lookups"},
	    {"L2_EXT_READ", "transactions"},
	    {"L2_EXT_AR_STALL", "stall cycles"},
	    {"L2_WRITE_LOOKUP", "cache lookups"},
	    {"L2_EXT_WRITE", "transactions"},
	    {"L2_EXT_W_STALL", "stall cycles"},
	    {"L2_EXT_READ_BEATS", "bus cycles"},
	    {"L2_EXT_WRITE_BEATS", "bus cycles"}};
	std::map<std::string, Measurement> _counters{};

	uint64_t _start_time{0};
	uint64_t _stop_time{0};

	const char *const  _device{"/dev/mali0"};
	int                _num_cores{0};
	int                _num_l2_slices{0};
	uint32_t           _hw_ver{0};
	int                _buffer_count{16};
	size_t             _buffer_size{0};
	uint8_t *          _sample_data{nullptr};
	uint64_t           _timestamp{0};
	const char *const *_names_lut{
	    nullptr};
	std::vector<uint32_t>     _raw_counter_buffer{};
	std::vector<unsigned int> _core_index_remap{};
	int                       _fd{-1};
	int                       _hwc_fd{-1};
};<commit_msg>Fix std::vector with const std::pair<commit_after>/*
 * Copyright (c) 2017-2019 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.
 */

#pragma once

#include "hwc.hpp"
#include "instrument.h"
#include "measurement.h"

#include <map>
#include <vector>
#include <algorithm>

/** Instrument implementation for mali hw counters. */
class MaliCounter : public Instrument
{
  public:
	/// @brief Construct a Mali counter.
	MaliCounter();

	/** Prevent instances of this class from being copy constructed */
	MaliCounter(const MaliCounter &) = delete;
	/** Prevent instances of this class from being copied */
	MaliCounter &operator=(const MaliCounter &) = delete;

	/** Default destructor */
	~MaliCounter();

	std::string     id() const override;
	void            start() override;
	void            stop() override;
	MeasurementsMap measurements() const override;

  private:
	void init();
	void term();

	void            sample_counters();
	void            wait_next_event();
	const uint32_t *get_counters() const;
	const uint32_t *get_counters(mali_userspace::MaliCounterBlockName block, int index = -1) const;
	int             find_counter_index_by_name(mali_userspace::MaliCounterBlockName block, const char *name);

	const std::vector<std::pair<const char *, const char *>> _jm_counter_names{
	    {"GPU_ACTIVE", "cycles"},
	    {"JS0_JOBS", "jobs"},
	    {"JS1_JOBS", "jobs"}};
	const std::vector<std::pair<const char *, const char *>> _mmu_counter_names{
	    {"L2_READ_LOOKUP", "cache lookups"},
	    {"L2_EXT_READ", "transactions"},
	    {"L2_EXT_AR_STALL", "stall cycles"},
	    {"L2_WRITE_LOOKUP", "cache lookups"},
	    {"L2_EXT_WRITE", "transactions"},
	    {"L2_EXT_W_STALL", "stall cycles"},
	    {"L2_EXT_READ_BEATS", "bus cycles"},
	    {"L2_EXT_WRITE_BEATS", "bus cycles"}};
	std::map<std::string, Measurement> _counters{};

	uint64_t _start_time{0};
	uint64_t _stop_time{0};

	const char *const  _device{"/dev/mali0"};
	int                _num_cores{0};
	int                _num_l2_slices{0};
	uint32_t           _hw_ver{0};
	int                _buffer_count{16};
	size_t             _buffer_size{0};
	uint8_t *          _sample_data{nullptr};
	uint64_t           _timestamp{0};
	const char *const *_names_lut{
	    nullptr};
	std::vector<uint32_t>     _raw_counter_buffer{};
	std::vector<unsigned int> _core_index_remap{};
	int                       _fd{-1};
	int                       _hwc_fd{-1};
};
{'fraction_non_alphanumeric': 0.10783184566657068, 'fraction_numerical': 0.013532968615030233, 'mean_word_length': 3.603711066931743, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3896200', 'n_tokens_mistral': 2231, 'n_tokens_neox': 2080, 'n_words': 700}