File: pqos.py

package info (click to toggle)
intel-cmt-cat 25.04-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,724 kB
  • sloc: ansic: 52,004; python: 11,324; makefile: 2,197; perl: 1,165; javascript: 37; sh: 23
file content (177 lines) | stat: -rwxr-xr-x 6,737 bytes parent folder | download | duplicates (2)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
################################################################################
# BSD LICENSE
#
# Copyright(c) 2019-2023 Intel Corporation. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#   * Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in
#     the documentation and/or other materials provided with the
#     distribution.
#   * Neither the name of Intel Corporation nor the names of its
#     contributors may be used to endorse or promote products derived
#     from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
################################################################################

"""
The main module.
It defines Pqos class that initializes/finalizes PQoS library and allows
to call APIs from PQoS library.
"""

from __future__ import absolute_import, division, print_function
import ctypes
import sys

from pqos.common import pqos_handle_error
from pqos.native_struct import CPqosConfig, CPqosSysconfig

class Pqos(object):
    """
    The main class that is responsible for PQoS library initialization
    and finalization. It implements singleton pattern.
    """

    LOG_VER_SILENT = -1
    LOG_VER_DEFAULT = 0
    LOG_VER_VERBOSE = 1
    LOG_VER_SUPER_VERBOSE = 2

    _instance = None

    @classmethod
    def set_instance(cls, instance):
        "Sets an instance of this class."

        cls._instance = instance

    @classmethod
    def get_instance(cls):
        "Gets an instance of this class."

        return cls._instance

    def __new__(cls):
        """
        Returns an object of this class if already created
        or constructs a new one.
        """

        instance = cls.get_instance()

        if instance is None:
            instance = object.__new__(cls)
            cls.set_instance(instance)

        return cls.get_instance()

    def __init__(self):
        "Finds PQoS library and constructs a new object."

        self.lib = ctypes.cdll.LoadLibrary('libpqos.so.6')

    def init(self, interface, log_file=None, log_callback=None,
             log_context=None, verbose='default'):
        """Initializes PQoS library.

        Parameters:
            interface: an interface to be used by PQoS library, Available
                       options: MSR, OS, OS_RESCTRL_MON
            log_file: a file object where logs will be written to or None,
                      if None is given, then sys.stdout is used (default None)
            log_callback: a callback invoked for each log message (default None)
            log_context: an additional information given to a log callback
                         (default None)
            verbose: log verbosity level, available options: silent,
                     default (or None), verbose and super (default 'default')
        """

        if interface.upper() == 'MSR':
            cfg_interface = CPqosConfig.PQOS_INTER_MSR
        elif interface.upper() == 'OS':
            cfg_interface = CPqosConfig.PQOS_INTER_OS
        elif interface.upper() == 'OS_RESCTRL_MON':
            cfg_interface = CPqosConfig.PQOS_INTER_OS_RESCTRL_MON
        elif interface.upper() == 'AUTO':
            cfg_interface = CPqosConfig.PQOS_INTER_AUTO
        else:
            raise ValueError(f'Unknown interface selected: {interface}.'
                              ' Available options: MSR, OS, OS_RESCTRL_MON, AUTO')

        if not log_file and not log_callback:
            log_file = sys.stdout

        if log_file:
            cfg_fd_log = log_file.fileno()
        else:
            cfg_fd_log = None

        if log_callback:
            def pqos_log_callback_wrapper(callback):
                """
                Wraps Python's callback into PQoS log callback-compatible
                function.
                """
                def cpqos_log_callback(context, _size, message):
                    "Calls Python's log callback."
                    return callback(message, context)

                return cpqos_log_callback

            wrapped_callback = pqos_log_callback_wrapper(log_callback)
            cfg_callback_log = CPqosConfig.LOG_CALLBACK(wrapped_callback)
        else:
            cfg_callback_log = CPqosConfig.LOG_CALLBACK(0)

        if verbose is None or verbose.lower() == 'default':
            cfg_verbose = CPqosConfig.LOG_VER_DEFAULT
        elif verbose.lower() == 'silent':
            cfg_verbose = CPqosConfig.LOG_VER_SILENT
        elif verbose.lower() == 'verbose':
            cfg_verbose = CPqosConfig.LOG_VER_VERBOSE
        elif verbose.lower() == 'super':
            cfg_verbose = CPqosConfig.LOG_VER_SUPER_VERBOSE
        else:
            raise ValueError(f'Unknown verbosity level selected: {interface}.'
                             ' Available options: silent, default (or None), verbose, super')

        config = CPqosConfig(interface=cfg_interface, fd_log=cfg_fd_log,
                             callback_log=cfg_callback_log,
                             verbose=cfg_verbose, context_log=log_context,
                             reserved=0)

        ret = self.lib.pqos_init(ctypes.byref(config))
        pqos_handle_error('pqos_init', ret)

    def fini(self):
        "Finalizes PQoS library."

        ret = self.lib.pqos_fini()
        pqos_handle_error('pqos_fini', ret)

    def get_sysconfig(self):
        "Returns system configuration."

        sysconfig = CPqosSysconfig()
        sysconfig_ptr = ctypes.pointer(sysconfig)
        sysconfig_ref = ctypes.byref(sysconfig_ptr)
        ret = self.lib.pqos_sysconfig_get(sysconfig_ref)
        pqos_handle_error('pqos_get_sysconfig', ret)
        return sysconfig_ptr.contents