<commit_before># BenchExec is a framework for reliable benchmarking.
# This file is part of BenchExec.
#
# Copyright (C) 2007-2015  <NAME>
# 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.

"""
    This module contains the Pqos class which is used to interact with pqos_wrapper cli
    to allocate equal cache for each thread and isolate cache of two individual threads.
"""

import os
import logging
import json
import grp
from subprocess import check_output, CalledProcessError, STDOUT
from benchexec.util import find_executable, get_capability, check_msr


class Pqos(object):
    """
        The Pqos class defines methods to interact with pqos_wrapper cli.
    """

    CMD = "pqos_wrapper"
    CAP_SYS_RAWIO = "cap_sys_rawio"

    def __init__(self):
        self.reset_required = False
        self.cap = False
        self.cli_exists = False
        self.executable_path = find_executable(
            "pqos_wrapper", exitOnError=False, use_current_dir=False
        )
        if self.executable_path is not None:
            self.cli_exists = True
        else:
            logging.warning(
                "Could not set cache allocation, unable to find pqos_wrapper cli"
            )

    def execute_command(self, function, suppress_warning, *args):
        """
            Execute a given pqos_wrapper command and log the output
        """
        if self.cli_exists:
            args_list = [self.CMD] + list(args)
            try:
                ret = json.loads(check_output(args_list, stderr=STDOUT))
                logging.debug(ret[function]["message"])
                return True
            except CalledProcessError as e:
                try:
                    ret = json.loads(e.output)
                    if not suppress_warning:
                        logging.warning(
                            "Could not set cache allocation...{}".format(ret["message"])
                        )
                        self.check_for_errors()
                except ValueError:
                    if not suppress_warning:
                        logging.warning(
                            "Could not set cache allocation...Unable to execute command {}".format(
                                " ".join(args_list)
                            )
                        )
        return False

    def check_capacity(self, technology):
        """
            Check if given intel rdt is supported.
        """
        if self.execute_command("check_capability", False, "-c", technology):
            self.cap = True

    def convert_core_list(self, core_assignment):
        """
            Convert a double list to a string.
        """
        ret = []
        for benchmark in core_assignment:
            ret.append("[" + ",".join(str(core) for core in benchmark) + "]")<commit_msg>fixed json loading of cli output<commit_after># BenchExec is a framework for reliable benchmarking.
# This file is part of BenchExec.
#
# Copyright (C) 2007-2015  <NAME>
# 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.

"""
    This module contains the Pqos class which is used to interact with pqos_wrapper cli
    to allocate equal cache for each thread and isolate cache of two individual threads.
"""

import os
import logging
import json
import grp
from subprocess import check_output, CalledProcessError, STDOUT
from benchexec.util import find_executable, get_capability, check_msr


class Pqos(object):
    """
        The Pqos class defines methods to interact with pqos_wrapper cli.
    """

    CMD = "pqos_wrapper"
    CAP_SYS_RAWIO = "cap_sys_rawio"

    def __init__(self):
        self.reset_required = False
        self.cap = False
        self.cli_exists = False
        self.executable_path = find_executable(
            "pqos_wrapper", exitOnError=False, use_current_dir=False
        )
        if self.executable_path is not None:
            self.cli_exists = True
        else:
            logging.warning(
                "Could not set cache allocation, unable to find pqos_wrapper cli"
            )

    def execute_command(self, function, suppress_warning, *args):
        """
            Execute a given pqos_wrapper command and log the output
        """
        if self.cli_exists:
            args_list = [self.CMD] + list(args)
            try:
                ret = json.loads(check_output(args_list, stderr=STDOUT).decode())
                logging.debug(ret[function]["message"])
                return True
            except CalledProcessError as e:
                try:
                    ret = json.loads(e.output.decode())
                    if not suppress_warning:
                        logging.warning(
                            "Could not set cache allocation...{}".format(ret["message"])
                        )
                        self.check_for_errors()
                except ValueError:
                    if not suppress_warning:
                        logging.warning(
                            "Could not set cache allocation...Unable to execute command {}".format(
                                " ".join(args_list)
                            )
                        )
        return False

    def check_capacity(self, technology):
        """
            Check if given intel rdt is supported.
        """
        if self.execute_command("check_capability", False, "-c", technology):
            self.cap = True

    def convert_core_list(self, core_assignment):
        """
            Convert a double list to a string.
        """
        ret = []
        for benchmark in core_assignment:
            ret.append("[" + ",".join(str(core) for core in benchmark) + "]")
{'fraction_non_alphanumeric': 0.07766990291262135, 'fraction_numerical': 0.0035847647498132933, 'mean_word_length': 1.9394205443371377, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3258289', 'n_tokens_mistral': 1761, 'n_tokens_neox': 1678, 'n_words': 593}