Skip to content

Commit

Permalink
Support for Pluggable Validation Layer Checkers (#155)
Browse files Browse the repository at this point in the history
* Support for Pluggable Validation Layer Checkers

Signed-off-by: Neil R. Spruit <[email protected]>
  • Loading branch information
nrspruit authored Jun 17, 2024
1 parent b5def34 commit 26ed564
Show file tree
Hide file tree
Showing 44 changed files with 7,001 additions and 3,919 deletions.
64 changes: 64 additions & 0 deletions scripts/generate_checker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""
Copyright (C) 2024 Intel Corporation
SPDX-License-Identifier: MIT
"""
import os
import re
import util
import argparse

templates_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "templates/checker/")

"""
generates checker files from the templates
"""
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("name", type=str, help="Name of your Validation Layer checker in the form of 'CheckerName'.")
parser.add_argument("out_dir", type=str, help="Root of the loader repository.")
args = parser.parse_args()
name = args.name
env_name = name.upper()
srcpath = os.path.join(args.out_dir, "source/layers/validation/checkers/")
srcpath = os.path.join(srcpath, name)
if not os.path.exists(srcpath):
os.makedirs(srcpath)
print("Generating Checker Template for %s\n" %(name))
loc = 0
template = "template.h.mako"
fin = os.path.join(templates_dir, template)

filename = "zel_%s_checker.h"%(name)
fout = os.path.join(srcpath, filename)

print("Generating %s..."%fout)
loc += util.makoWrite(
fin, fout,
name=name)

template = "template.cpp.mako"
fin = os.path.join(templates_dir, template)

filename = "zel_%s_checker.cpp"%(name)
fout = os.path.join(srcpath, filename)

print("Generating %s..."%fout)
loc += util.makoWrite(
fin, fout,
name=name,
env_name=env_name)

template = "template.cmake.mako"
fin = os.path.join(templates_dir, template)

filename = "CMakeLists.txt"
fout = os.path.join(srcpath, filename)

print("Generating %s..."%fout)
loc += util.makoWrite(
fin, fout,
name=name,
TARGET_NAME="${TARGET_NAME}",
CMAKE_CURRENT_LIST_DIR="${CMAKE_CURRENT_LIST_DIR}")
4 changes: 2 additions & 2 deletions scripts/generate_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ def _mako_loader_cpp(path, namespace, tags, version, specs, meta):
validation_files = {
'valddi.cpp.mako': ('','valddi.cpp'),
'entry_points.h.mako' : ('common', 'entry_points.h'),
'param.cpp.mako' : ('parameter_validation', 'parameter_validation.cpp'),
'param.h.mako' : ('parameter_validation', 'parameter_validation.h'),
'param.cpp.mako' : ('checkers/parameter_validation', 'parameter_validation.cpp'),
'param.h.mako' : ('checkers/parameter_validation', 'parameter_validation.h'),
'handle_lifetime.h.mako' : ('handle_lifetime_tracking', 'handle_lifetime.h'),
'handle_lifetime.cpp.mako' : ('handle_lifetime_tracking', 'handle_lifetime.cpp')
}
Expand Down
5 changes: 5 additions & 0 deletions scripts/templates/checker/template.cmake.mako
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
target_sources(${TARGET_NAME}
PRIVATE
${CMAKE_CURRENT_LIST_DIR}/zel_${name}_checker.h
${CMAKE_CURRENT_LIST_DIR}/zel_${name}_checker.cpp
)
35 changes: 35 additions & 0 deletions scripts/templates/checker/template.cpp.mako
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2024 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
* @file zel_${name}_checker.cpp
*
*/
#include "zel_${name}_checker.h"

namespace validation_layer
{
class ${name}Checker ${name}_checker;

${name}Checker::${name}Checker() {
enable${name} = getenv_tobool( "ZEL_ENABLE_${env_name}_CHECKER" );
if(enable${name}) {
${name}Checker::ZE${name}Checker *zeChecker = new ${name}Checker::ZE${name}Checker;
${name}Checker::ZES${name}Checker *zesChecker = new ${name}Checker::ZES${name}Checker;
${name}Checker::ZET${name}Checker *zetChecker = new ${name}Checker::ZET${name}Checker;
${name}_checker.zeValidation = zeChecker;
${name}_checker.zetValidation = zetChecker;
${name}_checker.zesValidation = zesChecker;
validation_layer::context.validationHandlers.push_back(&${name}_checker);
}
}

${name}Checker::~${name}Checker() {
if(enable${name}) {
delete ${name}_checker.zeValidation;
delete ${name}_checker.zetValidation;
delete ${name}_checker.zesValidation;
}
}
}
30 changes: 30 additions & 0 deletions scripts/templates/checker/template.h.mako
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
*
* Copyright (C) 2024 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
* @file zel_${name}_checker.h
*
*/

#pragma once

#include <string>
#include "ze_api.h"
#include "ze_validation_layer.h"

namespace validation_layer
{
class __zedlllocal ${name}Checker : public validationChecker{
public:
${name}Checker();
~${name}Checker();

class ZE${name}Checker : public ZEValidationEntryPoints {};
class ZES${name}Checker : public ZESValidationEntryPoints {};
class ZET${name}Checker : public ZETValidationEntryPoints {};
bool enable${name} = false;
};
extern class ${name}Checker ${name}_checker;
}
7 changes: 6 additions & 1 deletion scripts/templates/validation/entry_points.h.mako
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ namespace validation_layer
class ${N}ValidationEntryPoints {
public:
%for obj in th.extract_objs(specs, r"function"):
virtual ${x}_result_t ${th.make_func_name(n, tags, obj)}( \
virtual ${x}_result_t ${th.make_func_name(n, tags, obj)}Prologue( \
%for line in th.make_param_lines(n, tags, obj, format=["type", "name", "delim"]):
${line} \
%endfor
) {return ZE_RESULT_SUCCESS;}
virtual ${x}_result_t ${th.make_func_name(n, tags, obj)}Epilogue( \
%for line in th.make_param_lines(n, tags, obj, format=["type", "name", "delim"]):
${line} \
%endfor
Expand Down
2 changes: 1 addition & 1 deletion scripts/templates/validation/handle_lifetime.cpp.mako
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace validation_layer
## don't genrate function if it has no handles as parameters
%if th.obj_traits.is_function_with_input_handles(obj):
${x}_result_t
${N}HandleLifetimeValidation::${th.make_func_name(n, tags, obj)}(
${N}HandleLifetimeValidation::${th.make_func_name(n, tags, obj)}Prologue(
%for line in th.make_param_lines(n, tags, obj):
${line}
%endfor
Expand Down
2 changes: 1 addition & 1 deletion scripts/templates/validation/handle_lifetime.h.mako
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace validation_layer
%if not th.obj_traits.is_function_with_input_handles(obj):
<% continue %>
%endif
${x}_result_t ${th.make_func_name(n, tags, obj)} ( \
${x}_result_t ${th.make_func_name(n, tags, obj)}Prologue( \
%for line in th.make_param_lines(n, tags, obj, format=["type", "name", "delim"]):
${line} \
%endfor
Expand Down
28 changes: 26 additions & 2 deletions scripts/templates/validation/param.cpp.mako
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,38 @@ from templates import helper as th
*
*/
#include "${x}_validation_layer.h"
#include "${x}_parameter_validation.h"
#include "param_validation.h"

namespace validation_layer
{
%if 'ze' == n:
class parameterValidationChecker parameterChecker;

parameterValidationChecker::parameterValidationChecker() {
enableParameterValidation = getenv_tobool( "ZE_ENABLE_PARAMETER_VALIDATION" );
if(enableParameterValidation) {
ZEParameterValidation *zeChecker = new ZEParameterValidation;
ZESParameterValidation *zesChecker = new ZESParameterValidation;
ZETParameterValidation *zetChecker = new ZETParameterValidation;
parameterChecker.zeValidation = zeChecker;
parameterChecker.zetValidation = zetChecker;
parameterChecker.zesValidation = zesChecker;
validation_layer::context.validationHandlers.push_back(&parameterChecker);
}
}

parameterValidationChecker::~parameterValidationChecker() {
if(enableParameterValidation) {
delete parameterChecker.zeValidation;
delete parameterChecker.zetValidation;
delete parameterChecker.zesValidation;
}
}
%endif
%for obj in th.extract_objs(specs, r"function"):

${x}_result_t
${N}ParameterValidation::${th.make_func_name(n, tags, obj)}(
${N}ParameterValidation::${th.make_func_name(n, tags, obj)}Prologue(
%for line in th.make_param_lines(n, tags, obj):
${line}
%endfor
Expand Down
6 changes: 3 additions & 3 deletions scripts/templates/validation/param.h.mako
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ from templates import helper as th
*
*/

#pragma once
#pragma once
#include "${x}_validation_layer.h"
#include "${n}_entry_points.h"


Expand All @@ -29,12 +30,11 @@ namespace validation_layer
class ${N}ParameterValidation : public ${N}ValidationEntryPoints {
public:
%for obj in th.extract_objs(specs, r"function"):
${x}_result_t ${th.make_func_name(n, tags, obj)} ( \
${x}_result_t ${th.make_func_name(n, tags, obj)}Prologue( \
%for line in th.make_param_lines(n, tags, obj, format=["type", "name", "delim"]):
${line} \
%endfor
) override;
%endfor
};

}
20 changes: 15 additions & 5 deletions scripts/templates/validation/valddi.cpp.mako
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ namespace validation_layer
if( nullptr == ${th.make_pfn_name(n, tags, obj)} )
return ${X}_RESULT_ERROR_UNSUPPORTED_FEATURE;

if( context.enableParameterValidation )
{
auto result = context.paramValidation->${n}ParamValidation.${th.make_func_name(n, tags, obj)}( \
auto numValHandlers = context.validationHandlers.size();
for (size_t i = 0; i < numValHandlers; i++) {
auto result = context.validationHandlers[i]->${n}Validation->${th.make_func_name(n, tags, obj)}Prologue( \
% for line in th.make_param_lines(n, tags, obj, format=['name','delim']):
${line} \
%endfor
Expand All @@ -60,15 +60,25 @@ ${line} \
generate_post_call = re.match(r"\w+Create\w*$|\w+Get$|\w+Get\w*Exp$|\w+GetIpcHandle$|\w+GetSubDevices$", func_name)
%>
if(context.enableHandleLifetime ){
auto result = context.handleLifetime->${n}HandleLifetime.${th.make_func_name(n, tags, obj)}( \
auto result = context.handleLifetime->${n}HandleLifetime.${th.make_func_name(n, tags, obj)}Prologue( \
% for line in th.make_param_lines(n, tags, obj, format=['name','delim']):
${line} \
%endfor
);
if(result!=${X}_RESULT_SUCCESS) return result;
if(result!=${X}_RESULT_SUCCESS) return result;
}

auto result = ${th.make_pfn_name(n, tags, obj)}( ${", ".join(th.make_param_lines(n, tags, obj, format=["name"]))} );

for (size_t i = 0; i < numValHandlers; i++) {
auto result = context.validationHandlers[i]->${n}Validation->${th.make_func_name(n, tags, obj)}Epilogue( \
% for line in th.make_param_lines(n, tags, obj, format=['name','delim']):
${line} \
%endfor
);
if(result!=${X}_RESULT_SUCCESS) return result;
}

%if generate_post_call:

if( result == ${X}_RESULT_SUCCESS && context.enableHandleLifetime ){
Expand Down
5 changes: 3 additions & 2 deletions source/layers/validation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ target_include_directories(${TARGET_NAME}
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/common
${CMAKE_CURRENT_SOURCE_DIR}/handle_lifetime_tracking
${CMAKE_CURRENT_SOURCE_DIR}/parameter_validation
${CMAKE_CURRENT_SOURCE_DIR}/checkers/parameter_validation
${CMAKE_CURRENT_SOURCE_DIR}/checkers/template
)

if(UNIX)
Expand Down Expand Up @@ -53,5 +54,5 @@ install(TARGETS ze_validation_layer
NAMELINK_ONLY
)

add_subdirectory(parameter_validation)
add_subdirectory(handle_lifetime_tracking)
add_subdirectory(checkers)
Loading

0 comments on commit 26ed564

Please sign in to comment.