2023-10-31 15:11:17 -05:00
##############################################################################bl
# MIT License
#
# Copyright (c) 2021 - 2023 Advanced Micro Devices, Inc. All Rights Reserved.
#
# 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.
##############################################################################el
from abc import ABC , abstractmethod
import logging
import glob
import sys
import os
2023-12-11 09:55:15 -06:00
from utils.utils import capture_subprocess_output , run_prof , gen_sysinfo , run_rocscope , error
2023-10-31 15:11:17 -05:00
import config
class OmniProfiler_Base ():
def __init__ ( self , args , profiler_mode , soc ):
self . __args = args
self . __profiler = profiler_mode
self . __soc = soc
2023-12-11 12:26:26 -06:00
self . __perfmon_dir = os . path . join ( str ( config . omniperf_home ), "omniperf_soc" , "profile_configs" )
2023-10-31 15:11:17 -05:00
def get_args ( self ):
return self . __args
2023-12-04 15:47:03 -06:00
#----------------------------------------------------
2023-10-31 15:11:17 -05:00
# Required methods to be implemented by child classes
2023-12-04 15:47:03 -06:00
#----------------------------------------------------
2023-10-31 15:11:17 -05:00
@abstractmethod
def pre_processing ( self ):
"""Perform any pre-processing steps prior to profiling.
"""
logging . debug ( "[profiling] pre-processing using %s profiler" % self . __profiler )
# verify not accessing parent directories
if ".." in str ( self . __args . path ):
2023-12-11 09:55:15 -06:00
error ( "Access denied. Cannot access parent directories in path (i.e. ../)" )
2023-10-31 15:11:17 -05:00
# verify correct formatting for application binary
self . __args . remaining = self . __args . remaining [ 1 :]
if self . __args . remaining :
if not os . path . isfile ( self . __args . remaining [ 0 ]):
2023-12-11 09:55:15 -06:00
error ( "Your command %s doesn't point to a executable. Please verify." % self . __args . remaining [ 0 ])
2023-10-31 15:11:17 -05:00
self . __args . remaining = " " . join ( self . __args . remaining )
else :
2023-12-11 09:55:15 -06:00
error ( "Profiling command required. Pass application executable after -- at the end of options. \n\t\t i.e. omniperf profile -n vcopy -- ./vcopy 1048576 256" )
2023-10-31 15:11:17 -05:00
# verify name meets MongoDB length requirements and no illegal chars
if len ( self . __args . name ) > 35 :
2023-12-11 09:55:15 -06:00
error ( "-n/--name exceeds 35 character limit. Try again." )
2023-10-31 15:11:17 -05:00
if self . __args . name . find ( "." ) != - 1 or self . __args . name . find ( "-" ) != - 1 :
2023-12-11 09:55:15 -06:00
error ( "'-' and '.' are not permitted in -n/--name" )
2023-10-31 15:11:17 -05:00
@abstractmethod
def run_profiling ( self , version : str , prog : str ):
"""Run profiling.
"""
logging . debug ( "[profiling] performing profiling using %s profiler" % self . __profiler )
# log basic info
logging . info ( str ( prog ) + " ver: " + str ( version ))
logging . info ( "Path: " + str ( os . path . abspath ( self . __args . path )))
logging . info ( "Target: " + str ( self . __args . target ))
logging . info ( "Command: " + str ( self . __args . remaining ))
logging . info ( "Kernel Selection: " + str ( self . __args . kernel ))
logging . info ( "Dispatch Selection: " + str ( self . __args . dispatch ))
if self . __args . ipblocks == None :
logging . info ( "IP Blocks: All" )
else :
2023-11-20 14:45:56 -06:00
logging . info ( "IP Blocks: " + str ( self . __args . ipblocks ))
2023-10-31 15:11:17 -05:00
if self . __args . kernel_verbose > 5 :
logging . info ( "KernelName verbose: DISABLED" )
else :
logging . info ( "KernelName verbose: " + str ( self . __args . kernel_verbose ))
# Run profiling on each input file
for fname in glob . glob ( self . get_args () . path + "/perfmon/*.txt" ):
# Kernel filtering (in-place replacement)
if not self . __args . kernel == None :
success , output = capture_subprocess_output (
[
"sed" ,
"-i" ,
"-r" ,
"s%^(kernel:).*%" + "kernel: " + "," . join ( self . __args . kernel ) + " %g " ,
fname ,
]
)
# log output from profile filtering
if not success :
2023-12-11 09:55:15 -06:00
error ( output )
2023-10-31 15:11:17 -05:00
else :
logging . debug ( output )
# Dispatch filtering (inplace replacement)
if not self . __args . dispatch == None :
success , output = capture_subprocess_output (
[
"sed" ,
"-i" ,
"-r" ,
"s%^(range:).*%" + "range: " + " " . join ( self . __args . dispatch ) + " %g " ,
fname ,
]
)
# log output from profile filtering
if not success :
2023-12-11 09:55:15 -06:00
error ( output )
2023-10-31 15:11:17 -05:00
else :
logging . debug ( output )
logging . info ( " \n Current input file: %s " % fname )
if self . __profiler == "rocprofv1" :
#TODO: Look back at run_prof() definition. We may want to separate this based on SoC
run_prof ( fname , self . get_args () . path , self . __perfmon_dir , self . __args . remaining , self . __args . target , self . __args . verbose )
elif self . __profiler == "rocscope" :
run_rocscope ( self . __args , fname )
else :
#TODO: Finish logic
2023-12-11 09:55:15 -06:00
error ( "profiler not supported" )
2023-10-31 15:11:17 -05:00
@abstractmethod
def post_processing ( self ):
"""Perform any post-processing steps prior to profiling.
"""
logging . debug ( "[profiling] performing post-processing using %s profiler" % self . __profiler )
2024-01-23 12:56:59 -06:00
gen_sysinfo (
workload_name = self . __args . name ,
workload_dir = self . get_args () . path ,
ip_blocks = self . __args . ipblocks ,
app_cmd = self . __args . remaining ,
skip_roof = self . __args . no_roof ,
roof_only = self . __args . roof_only ,
)