Skip to content

Commit d57b04b

Browse files
authored
Merge pull request #50 from powerapi-ng/refactor/issue49/adaptation-to-use-powerapi-2-0-4
refactor: Adapt main and config_validator for using PowerAPI 2.0.4
2 parents aabc62d + 8580919 commit d57b04b

File tree

4 files changed

+52
-27
lines changed

4 files changed

+52
-27
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ authors = [
2929
]
3030

3131
dependencies = [
32-
"powerapi[everything] == 2.0.3",
32+
"powerapi[everything] == 2.0.4",
3333
"scikit-learn >= 0.20.2",
3434
]
3535

src/smartwatts/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@
2727
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2828
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929

30-
__version__ = "2.1.0"
30+
__version__ = "2.1.1"

src/smartwatts/__main__.py

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@
3535

3636
from powerapi import __version__ as powerapi_version
3737
from powerapi.backend_supervisor import BackendSupervisor
38+
from powerapi.cli.common_cli_parsing_manager import CommonCLIParsingManager
39+
from powerapi.cli.config_parser import store_true
3840
from powerapi.cli.generator import PusherGenerator, PullerGenerator
39-
from powerapi.cli.parser import store_true
40-
from powerapi.cli.tools import CommonCLIParser
4141
from powerapi.dispatch_rule import HWPCDispatchRule, HWPCDepthLevel
4242
from powerapi.dispatcher import DispatcherActor, RouteTable
43-
from powerapi.exception import PowerAPIException
43+
from powerapi.exception import PowerAPIException, MissingArgumentException, NotAllowedArgumentValueException, \
44+
FileDoesNotExistException
4445
from powerapi.filter import Filter
4546
from powerapi.report import HWPCReport
4647

@@ -51,38 +52,57 @@
5152
from smartwatts.model import CPUTopology
5253

5354

54-
def generate_smartwatts_parser() -> CommonCLIParser:
55+
def generate_smartwatts_parser() -> CommonCLIParsingManager:
5556
"""
5657
Construct and returns the SmartWatts cli parameters parser.
5758
:return: SmartWatts cli parameters parser
5859
"""
59-
parser = CommonCLIParser()
60+
parser_manager = CommonCLIParsingManager()
6061

6162
# Formula control parameters
62-
parser.add_argument('disable-cpu-formula', help='Disable CPU formula', flag=True, type=bool, default=False, action=store_true)
63-
parser.add_argument('disable-dram-formula', help='Disable DRAM formula', flag=True, type=bool, default=False, action=store_true)
63+
parser_manager.add_argument_to_cli_parser('disable-cpu-formula', help_text='Disable CPU formula', is_flag=True,
64+
argument_type=bool, default_value=False, action=store_true)
65+
parser_manager.add_argument_to_cli_parser('disable-dram-formula', help_text='Disable DRAM formula', is_flag=True,
66+
argument_type=bool, default_value=False, action=store_true)
6467

6568
# Formula RAPL reference event
66-
parser.add_argument('cpu-rapl-ref-event', help='RAPL event used as reference for the CPU power models', default='RAPL_ENERGY_PKG')
67-
parser.add_argument('dram-rapl-ref-event', help='RAPL event used as reference for the DRAM power models', default='RAPL_ENERGY_DRAM')
69+
parser_manager.add_argument_to_cli_parser('cpu-rapl-ref-event',
70+
help_text='RAPL event used as reference for the CPU power models',
71+
default_value='RAPL_ENERGY_PKG')
72+
parser_manager.add_argument_to_cli_parser('dram-rapl-ref-event',
73+
help_text='RAPL event used as reference for the DRAM power models',
74+
default_value='RAPL_ENERGY_DRAM')
6875

6976
# CPU topology information
70-
parser.add_argument('cpu-tdp', help='CPU TDP (in Watt)', type=int, default=400)
71-
parser.add_argument('cpu-base-clock', help='CPU base clock (in MHz)', type=int, default=100)
72-
parser.add_argument('cpu-base-freq', help='CPU base frequency (in MHz)', type=int, default=2100)
77+
parser_manager.add_argument_to_cli_parser('cpu-tdp', help_text='CPU TDP (in Watt)', argument_type=int,
78+
default_value=400)
79+
parser_manager.add_argument_to_cli_parser('cpu-base-clock', help_text='CPU base clock (in MHz)', argument_type=int,
80+
default_value=100)
81+
parser_manager.add_argument_to_cli_parser('cpu-base-freq', help_text='CPU base frequency (in MHz)',
82+
argument_type=int, default_value=2100)
7383

7484
# Formula error threshold
75-
parser.add_argument('cpu-error-threshold', help='Error threshold for the CPU power models (in Watt)', type=float, default=2.0)
76-
parser.add_argument('dram-error-threshold', help='Error threshold for the DRAM power models (in Watt)', type=float, default=2.0)
85+
parser_manager.add_argument_to_cli_parser('cpu-error-threshold',
86+
help_text='Error threshold for the CPU power models (in Watt)',
87+
argument_type=float, default_value=2.0)
88+
parser_manager.add_argument_to_cli_parser('dram-error-threshold',
89+
help_text='Error threshold for the DRAM power models (in Watt)',
90+
argument_type=float, default_value=2.0)
7791

7892
# Sensor information
79-
parser.add_argument('sensor-reports-frequency', help='The frequency with which measurements are made (in milliseconds)', type=int, default=1000)
93+
parser_manager.add_argument_to_cli_parser('sensor-reports-frequency',
94+
help_text='The frequency with which measurements are made (in milliseconds)',
95+
argument_type=int, default_value=1000)
8096

8197
# Learning parameters
82-
parser.add_argument('learn-min-samples-required', help='Minimum amount of samples required before trying to learn a power model', type=int, default=10)
83-
parser.add_argument('learn-history-window-size', help='Size of the history window used to keep samples to learn from', type=int, default=60)
98+
parser_manager.add_argument_to_cli_parser('learn-min-samples-required',
99+
help_text='Minimum amount of samples required before trying to learn a power model',
100+
argument_type=int, default_value=10)
101+
parser_manager.add_argument_to_cli_parser('learn-history-window-size',
102+
help_text='Size of the history window used to keep samples to learn from',
103+
argument_type=int, default_value=60)
84104

85-
return parser
105+
return parser_manager
86106

87107

88108
def generate_formula_configuration(config: Dict, cpu_topology: CPUTopology, scope: SmartWattsFormulaScope) -> SmartWattsFormulaConfig:
@@ -198,6 +218,15 @@ def term_handler(_, __):
198218
except InvalidConfigurationParameterException as exn:
199219
logging.error('Invalid configuration: %s', exn)
200220
sys.exit(1)
221+
except MissingArgumentException as exn:
222+
logging.error('Missing argument: %s', exn)
223+
sys.exit(1)
224+
except NotAllowedArgumentValueException as exn:
225+
logging.error('Not Allowed Argument: %s', exn)
226+
sys.exit(1)
227+
except FileDoesNotExistException as exn:
228+
logging.error('File does not exist: %s', exn)
229+
sys.exit(1)
201230

202231
LOGGING_LEVEL = logging.DEBUG if args['verbose'] else logging.INFO
203232
LOGGING_FORMAT = '%(asctime)s - %(process)d - %(processName)s - %(name)s - %(levelname)s - %(message)s'

src/smartwatts/cli/config_validator.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2828
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929

30-
from typing import Dict
31-
3230
from powerapi.cli import ConfigValidator
3331

3432
from smartwatts.exceptions import InvalidConfigurationParameterException
@@ -40,9 +38,9 @@ class SmartWattsConfigValidator(ConfigValidator):
4038
"""
4139

4240
@staticmethod
43-
def validate(config: Dict) -> bool:
44-
if not ConfigValidator.validate(config):
45-
raise InvalidConfigurationParameterException('Invalid PowerAPI parameter')
41+
def validate(config: dict):
42+
43+
ConfigValidator.validate(config)
4644

4745
if config['disable-cpu-formula'] and config['disable-dram-formula']:
4846
raise InvalidConfigurationParameterException('At least one of the two formula scope must be enabled')
@@ -58,5 +56,3 @@ def validate(config: Dict) -> bool:
5856

5957
if config['learn-min-samples-required'] < 0 or config['learn-history-window-size'] < 0:
6058
raise InvalidConfigurationParameterException('Report history parameters must be positive')
61-
62-
return True

0 commit comments

Comments
 (0)