Esempio n. 1
0
def main():
    parser = get_parser()
    input_args = sys.argv[1:]
    args, unparsed_args = parser.parse_known_args()

    # parse preprocessor flags
    has_remaining_args = False
    preprocessor_args = []
    for a in unparsed_args:
        if a[:2] == "-D":
            input_args.remove(a)
            preprocessor_args.append(a)
        else:
            has_remaining_args = True

    # trigger error
    if has_remaining_args:
        parser.parse_args(input_args)

    if args.json:
        path_json = _validate_path(args.json)

    from_json = False
    if args.pd:
        path_pd = _validate_dir(args.pd)
        if args.pd_from_json:
            if not os.path.exists(args.source):
                print "Invalid JSON source"
                sys.exit(1)
            from_json = True

    if from_json:
        with open(args.source, 'r') as f:
            json_dict = json.load(f)
    else:
        h = HLIR(args.source)
        h.add_preprocessor_args("-D__TARGET_BMV2__")
        for parg in preprocessor_args:
            h.add_preprocessor_args(parg)
        # in addition to standard P4 primitives
        more_primitives = json.loads(
            resource_string(__name__, 'primitives.json')
        )
        h.add_primitives(more_primitives)
        if not h.build(analyze=False):
            print "Error while building HLIR"
            sys.exit(1)

        json_dict = gen_json.json_dict_create(h)

        if args.json:
            print "Generating json output to", path_json
            with open(path_json, 'w') as fp:
                json.dump(json_dict, fp, indent=4, separators=(',', ': '))

    if args.pd:
        print "Generating PD source files in", path_pd
        gen_pd.generate_pd_source(json_dict, path_pd, args.p4_prefix)
Esempio n. 2
0
def main():
    parser = get_parser()
    input_args = sys.argv[1:]
    args, unparsed_args = parser.parse_known_args()

    # parse preprocessor flags
    has_remaining_args = False
    preprocessor_args = []
    for a in unparsed_args:
        if a[:2] == "-D":
            input_args.remove(a)
            preprocessor_args.append(a)
        else:
            has_remaining_args = True

    # trigger error
    if has_remaining_args:
        parser.parse_args(input_args)

    if args.json:
        path_json = _validate_path(args.json)

    from_json = False
    if args.pd:
        path_pd = _validate_dir(args.pd)
        if args.pd_from_json:
            if not os.path.exists(args.source):
                print "Invalid JSON source"
                sys.exit(1)
            from_json = True

    if from_json:
        with open(args.source, 'r') as f:
            json_dict = json.load(f)
    else:
        h = HLIR(args.source)
        h.add_preprocessor_args("-D__TARGET_BMV2__")
        for parg in preprocessor_args:
            h.add_preprocessor_args(parg)
        # in addition to standard P4 primitives
        more_primitives = json.loads(
            resource_string(__name__, 'primitives.json'))
        h.add_primitives(more_primitives)
        if not h.build(analyze=False):
            print "Error while building HLIR"
            sys.exit(1)

        json_dict = gen_json.json_dict_create(h)

        if args.json:
            print "Generating json output to", path_json
            with open(path_json, 'w') as fp:
                json.dump(json_dict, fp, indent=4, separators=(',', ': '))

    if args.pd:
        print "Generating PD source files in", path_pd
        gen_pd.generate_pd_source(json_dict, path_pd, args.p4_prefix)
Esempio n. 3
0
def main():
    parser = get_parser()
    args = parser.parse_args()

    if args.json:
        path_json = _validate_path(args.json)

    from_json = False
    if args.pd:
        path_pd = _validate_dir(args.pd)
        if args.pd_from_json:
            if not os.path.exists(args.source):
                print "Invalid JSON source"
                sys.exit(1)
            from_json = True

    if from_json:
        with open(args.source, 'r') as f:
            json_dict = json.load(f)
    else:
        h = HLIR(args.source)
        h.add_preprocessor_args("-D__TARGET_BMV2__")
        # in addition to standard P4 primitives
        more_primitives = json.loads(
            resource_string(__name__, 'primitives.json')
        )
        h.add_primitives(more_primitives)
        if not h.build():
            print "Error while building HLIR"
            sys.exit(1)

        json_dict = gen_json.json_dict_create(h)

        if args.json:
            print "Generating json output to", path_json
            with open(path_json, 'w') as fp:
                json.dump(json_dict, fp, indent=4, separators=(',', ': '))

    if args.pd:
        print "Generating PD source files in", path_pd
        gen_pd.generate_pd_source(json_dict, path_pd, args.p4_prefix)
Esempio n. 4
0
def main():
    parser = get_parser()
    args = parser.parse_args()

    if args.json:
        path_json = _validate_path(args.json)

    from_json = False
    if args.pd:
        path_pd = _validate_dir(args.pd)
        if args.pd_from_json:
            if not os.path.exists(args.source):
                print "Invalid JSON source"
                sys.exit(1)
            from_json = True

    if from_json:
        with open(args.source, 'r') as f:
            json_dict = json.load(f)
    else:
        h = HLIR(args.source)
        h.add_preprocessor_args("-D__TARGET_BMV2__")
        # in addition to standard P4 primitives
        more_primitives = json.loads(
            resource_string(__name__, 'primitives.json'))
        h.add_primitives(more_primitives)
        if not h.build():
            print "Error while building HLIR"
            sys.exit(1)

        json_dict = gen_json.json_dict_create(h)

        if args.json:
            print "Generating json output to", path_json
            with open(path_json, 'w') as fp:
                json.dump(json_dict, fp, indent=4, separators=(',', ': '))

    if args.pd:
        print "Generating PD source files in", path_pd
        gen_pd.generate_pd_source(json_dict, path_pd, args.p4_prefix)
Esempio n. 5
0
def main():
    parser = get_parser()
    args = parser.parse_args()

    if args.json:
        path_json = _validate_path(args.json)

    from_json = False
    if args.pd:
        path_pd = _validate_dir(args.pd)
        if args.pd_from_json:
            if not os.path.exists(args.source):
                print "Invalid JSON source"
                sys.exit(1)
            from_json = True

    if from_json:
        with open(args.source, 'r') as f:
            json_dict = json.load(f)
    else:
        h = HLIR(args.source)
        h.add_preprocessor_args("-D__TARGET_BMV2__")
        if not h.build():
            print "Error while building HLIR"
            sys.exit(1)

        json_dict = gen_json.json_dict_create(h)

        if args.json:
            print "Generating json output to", path_json
            with open(path_json, 'w') as fp:
                json.dump(json_dict, fp, indent=4, separators=(',', ': '))
        if args.dump_yaml:
            with open("yaml_dump.yml", 'w') as f:
                dump_render_dict(json_dict, f)

    if args.pd:
        print "Generating PD source files in", path_pd
        gen_pd.generate_pd_source(json_dict, path_pd, args.p4_prefix)
Esempio n. 6
0
def main():
    parser = get_parser()
    input_args = sys.argv[1:]
    args, unparsed_args = parser.parse_known_args()

    # parse preprocessor flags
    has_remaining_args = False
    preprocessor_args = []
    for a in unparsed_args:
        if a[:2] == "-D":
            input_args.remove(a)
            preprocessor_args.append(a)
        elif a[:2] == "-I":
            input_args.remove(a)
            preprocessor_args.append(a)
        else:
            has_remaining_args = True

    # trigger error
    if has_remaining_args:
        parser.parse_args(input_args)

    if args.json:
        path_json = _validate_path(args.json)

    if args.field_aliases:
        path_field_aliases = _validate_file(args.field_aliases)
    else:
        path_field_aliases = None

    p4_v1_1 = getattr(args, 'p4_v1.1')
    if p4_v1_1:
        try:
            import p4_hlir_v1_1  # NOQA
        except ImportError:  # pragma: no cover
            print "You requested P4 v1.1 but the corresponding p4-hlir",\
                "package does not seem to be installed"
            sys.exit(1)

    from_json = False
    if args.pd:
        path_pd = _validate_dir(args.pd)
        if args.pd_from_json:
            if not os.path.exists(args.source):
                print "Invalid JSON source"
                sys.exit(1)
            from_json = True

    if from_json:
        with open(args.source, 'r') as f:
            json_dict = json.load(f)
    else:
        if p4_v1_1:
            from p4_hlir_v1_1.main import HLIR
            primitives_res = 'primitives_v1_1.json'
        else:
            from p4_hlir.main import HLIR
            primitives_res = 'primitives.json'

        h = HLIR(args.source)

        # if no -D__TARGET_* flag defined, we add a default bmv2 one
        if True not in map(lambda f: "-D__TARGET_" in f, preprocessor_args):
            h.add_preprocessor_args("-D__TARGET_BMV2__")
        for parg in preprocessor_args:
            h.add_preprocessor_args(parg)

        # in addition to standard P4 primitives
        more_primitives = json.loads(resource_string(__name__, primitives_res))
        h.add_primitives(more_primitives)

        # user-provided primitives
        for primitive_path in args.primitives:
            _validate_file(primitive_path)
            with open(primitive_path, 'r') as f:
                h.add_primitives(json.load(f))

        if not h.build(analyze=False):
            print "Error while building HLIR"
            sys.exit(1)

        json_dict = gen_json.json_dict_create(h, path_field_aliases, p4_v1_1)

        if args.json:
            print "Generating json output to", path_json
            with open(path_json, 'w') as fp:
                json.dump(json_dict, fp, indent=4, separators=(',', ': '))

    if args.pd:
        print "Generating PD source files in", path_pd
        gen_pd.generate_pd_source(json_dict, path_pd, args.p4_prefix, args)
Esempio n. 7
0
def main():
    parser = get_parser()
    input_args = sys.argv[1:]
    args, unparsed_args = parser.parse_known_args()

    # parse preprocessor flags
    has_remaining_args = False
    preprocessor_args = []
    for a in unparsed_args:
        if a[:2] == "-D":
            input_args.remove(a)
            preprocessor_args.append(a)
        elif a[:2] == "-I":
            input_args.remove(a)
            preprocessor_args.append(a)
        else:
            has_remaining_args = True

    # trigger error
    if has_remaining_args:
        parser.parse_args(input_args)

    if args.json:
        path_json = _validate_path(args.json)

    if args.field_aliases:
        path_field_aliases = _validate_file(args.field_aliases)
    else:
        path_field_aliases = None

    p4_v1_1 = getattr(args, 'p4_v1.1')
    if p4_v1_1:
        try:
            import p4_hlir_v1_1  # NOQA
        except ImportError:  # pragma: no cover
            print "You requested P4 v1.1 but the corresponding p4-hlir",\
                "package does not seem to be installed"
            sys.exit(1)

    from_json = False
    if args.pd:
        path_pd = _validate_dir(args.pd)
        if args.pd_from_json:
            if not os.path.exists(args.source):
                print "Invalid JSON source"
                sys.exit(1)
            from_json = True

    if from_json:
        with open(args.source, 'r') as f:
            json_dict = json.load(f)
    else:
        if p4_v1_1:
            from p4_hlir_v1_1.main import HLIR
            primitives_res = 'primitives_v1_1.json'
        else:
            from p4_hlir.main import HLIR
            primitives_res = 'primitives.json'

        h = HLIR(args.source)

        # if no -D__TARGET_* flag defined, we add a default bmv2 one
        if True not in map(lambda f: "-D__TARGET_" in f, preprocessor_args):
            h.add_preprocessor_args("-D__TARGET_BMV2__")
        for parg in preprocessor_args:
            h.add_preprocessor_args(parg)

        # in addition to standard P4 primitives
        more_primitives = json.loads(resource_string(__name__, primitives_res))
        h.add_primitives(more_primitives)

        # user-provided primitives
        for primitive_path in args.primitives:
            _validate_file(primitive_path)
            with open(primitive_path, 'r') as f:
                h.add_primitives(json.load(f))

        if not h.build(analyze=False):
            print "Error while building HLIR"
            sys.exit(1)

        json_dict = gen_json.json_dict_create(h, path_field_aliases, p4_v1_1,
                                              args.keep_pragmas)

        if args.json:
            print "Generating json output to", path_json
            with open(path_json, 'w') as fp:
                json.dump(json_dict, fp, indent=4, separators=(',', ': '))

    if args.pd:
        print "Generating PD source files in", path_pd
        gen_pd.generate_pd_source(json_dict, path_pd, args.p4_prefix, args)
Esempio n. 8
0
def main():
    parser = get_parser()
    input_args = sys.argv[1:]
    args, unparsed_args = parser.parse_known_args()

    # parse preprocessor flags
    has_remaining_args = False
    preprocessor_args = []
    for a in unparsed_args:
        if a[:2] == "-D":
            input_args.remove(a)
            preprocessor_args.append(a)
        elif a[:2] == "-I":
            input_args.remove(a)
            preprocessor_args.append(a)
        else:
            has_remaining_args = True

    # trigger error
    if has_remaining_args:
        parser.parse_args(input_args)

    if args.json:
        path_json = _validate_path(args.json)

    if args.json_s:
        path_json_s = _validate_path(args.json_s)

    if args.json_mg:
        path_json_mg = _validate_path(args.json_mg)

    if args.field_aliases:
        path_field_aliases = _validate_file(args.field_aliases)
    else:
        path_field_aliases = None

    GEN_FIG = getattr(args, 'gen_fig')
    AB_T = getattr(args, 'AB_testing')
    DF_T = getattr(args, 'Diff_testing')
    if AB_T + DF_T == False:
        print "Please add args AB-testing or Diff-testing"
        sys.exit(1)
    if AB_T == True and DF_T == True:
        print "Please only use one args AB-testing or Diff-testing"
        sys.exit(1)
    p4_v1_1 = getattr(args, 'p4_v1.1')
    if p4_v1_1:
        try:
            import p4_hlir_v1_1  # NOQA
        except ImportError:  # pragma: no cover
            print "You requested P4 v1.1 but the corresponding p4-hlir",\
                "package does not seem to be installed"
            sys.exit(1)

    from_json = False
    if args.pd:
        path_pd = _validate_dir(args.pd)
        if args.pd_from_json:
            if not os.path.exists(args.source):
                print "Invalid JSON source"
                sys.exit(1)
            from_json = True

    if from_json:
        with open(args.source, 'r') as f:
            json_dict = json.load(f)
    else:
        if p4_v1_1:
            from p4_hlir_v1_1.main import HLIR
            primitives_res = 'primitives_v1_1.json'
        else:
            from p4_hlir.main import HLIR
            primitives_res = 'primitives.json'

    ## 0. build shadow meta HLIR and json
        
        if AB_T:
            testing_case = 1
            print 'LOG|read AB testing meta'
            h_meta = HLIR('p4c_bm/SP4_metas_ab.p4')
        elif DF_T:
            testing_case = 2
            print 'LOG|read Diff testing meta'
            h_meta = HLIR('p4c_bm/SP4_metas_diff.p4')
        # if no -D__TARGET_* flag defined, we add a default bmv2 one
        if True not in map(lambda f: "-D__TARGET_" in f, preprocessor_args):
            h_meta.add_preprocessor_args("-D__TARGET_BMV2__")
        for parg in preprocessor_args:
            h_meta.add_preprocessor_args(parg)

        # in addition to standard P4 primitives
        more_primitives = json.loads(resource_string(__name__, primitives_res))
        h_meta.add_primitives(more_primitives)

        if AB_T:
            if not h_meta.build_shadow_metadata_AB(analyze=False):
                print "ERR|p4c_bm|main|Error while building shadow metadata HLIR"
                sys.exit(1)
        if DF_T:
            if not h_meta.build_shadow_metadata_DF(analyze=False):
                print "ERR|p4c_bm|main|Error while building shadow metadata HLIR"
                sys.exit(1)
        

    ## 1. build production program HLIR and json
        h_r = HLIR(args.real_source)

        # if no -D__TARGET_* flag defined, we add a default bmv2 one
        if True not in map(lambda f: "-D__TARGET_" in f, preprocessor_args):
            h_r.add_preprocessor_args("-D__TARGET_BMV2__")
        for parg in preprocessor_args:
            h_r.add_preprocessor_args(parg)

        # in addition to standard P4 primitives
        more_primitives = json.loads(resource_string(__name__, primitives_res))
        h_r.add_primitives(more_primitives)

        # user-provided primitives
        for primitive_path in args.primitives:
            _validate_file(primitive_path)
            with open(primitive_path, 'r') as f:
                h_r.add_primitives(json.load(f))

        if not h_r.build(analyze=False, program_version=10000):
            print "Error while building real P4 HLIR"
            sys.exit(1)

        OPT_generate_real_json = True
        if OPT_generate_real_json:
            json_dict = gen_json.json_dict_create(h_r, path_field_aliases, p4_v1_1,
                                                args.keep_pragmas)

            if args.json:
                print "OPT|MAIN|Generating real P4 json output to", path_json
                with open(path_json, 'w') as fp:
                    json.dump(json_dict, fp, indent=4, separators=(',', ': '))

        OPT_generate_real_graph = GEN_FIG
        if OPT_generate_real_graph:
            ## generate graphs
            OPT_gen_graph_parser = True
            OPT_gen_graph_table = True
            OPT_gen_graph_deps = True
            basename = _get_p4_basename(args.real_source)
            gen_dir = args.gen_dir
            print "Generating files in directory", gen_dir            
            if OPT_gen_graph_parser:
                dot.export_parse_graph(h_r, basename, gen_dir, no_dot=True)
            if OPT_gen_graph_table:
                dot.export_table_graph(h_r, basename, gen_dir, predecessors=False, no_dot=True)
            if OPT_gen_graph_deps:
                dot.export_table_dependency_graph(h_r, basename, gen_dir, show_conds = True, no_dot=True)


    ## 2. build testing program HLIR and json
        if args.shadow_source:
            h_s = HLIR(args.shadow_source) ## shadow hlir

            # if no -D__TARGET_* flag defined, we add a default bmv2 one
            if True not in map(lambda f: "-D__TARGET_" in f, preprocessor_args):
                h_s.add_preprocessor_args("-D__TARGET_BMV2__")
            for parg in preprocessor_args:
                h_s.add_preprocessor_args(parg)

            more_primitives = json.loads(resource_string(__name__, primitives_res))
            h_s.add_primitives(more_primitives)

            if not h_s.build(analyze=False, program_version=20000, config_dir=args.gen_dir):
                print "Error while building shadow P4 HLIR"
                sys.exit(1)

            OPT_generate_shadow_json = True
            if OPT_generate_shadow_json:
                json_dict_s = gen_json.json_dict_create(h_s, path_field_aliases, p4_v1_1,
                                                args.keep_pragmas)
                if args.json_s:
                    print "OPT|MAIN|Generating shadow P4 json output to", path_json_s
                    with open(path_json_s, 'w') as fp:
                        json.dump(json_dict_s, fp, indent=4, separators=(',', ': '))

            OPT_generate_shadow_graph = GEN_FIG
            if OPT_generate_shadow_graph:
                ## generate graphs
                gen_dir = args.gen_dir
                print "Generating files in directory", gen_dir
                OPT_gen_graph_parser = True
                OPT_gen_graph_table = True
                OPT_gen_graph_deps = True
                basename = _get_p4_basename(args.shadow_source)
                if OPT_gen_graph_parser:
                    dot.export_parse_graph(h_s, basename, gen_dir, no_dot=True)
                if OPT_gen_graph_table:
                    dot.export_table_graph(h_s, basename, gen_dir, predecessors=False, no_dot=True)
                if OPT_gen_graph_deps:
                    dot.export_table_dependency_graph(h_s, basename, gen_dir,
                                                    show_conds = True, no_dot=True)
            # return


    ## 3. build dependency graph for the merged program


        ## 3.1.1 build real program ingress/egress graph
            h_r_ingress_graph = SP4_merge.build_table_graph_ingress(h_r)
            h_r_egress_graph = SP4_merge.build_table_graph_egress(h_r)   

            # DBG: print the dot file
            tmp_print_dot_file = False
            if tmp_print_dot_file:
                h_r_ingress_graph.generate_dot()

            ## 3.1.2 get real ingress/egress graph
            h_r_ingress_graph.SP4_gen_real_graph_node_edges(h_r)
            h_r_egress_graph.SP4_gen_real_graph_node_edges(h_r)

            ## 3.1.3 get shadow adj list
            h_r_ingress_graph.SP4_init_adj_list()
            h_r_egress_graph.SP4_init_adj_list()


        ## 3.2.1 build shadow program ingress/egress graph
            h_s_ingress_graph = SP4_merge.build_table_graph_ingress(h_s)
            h_s_egress_graph = SP4_merge.build_table_graph_egress(h_s) 

            ## 3.2.2 get shadow program ingress/egress graph
                  ## and calculate common structured graph
                  ## identify resued id to the reused tables
            h_s_ingress_graph.SP4_gen_shadow_graph_node_edges(h_s, h_r_ingress_graph, h_r)
            h_s_egress_graph.SP4_gen_shadow_graph_node_edges(h_s, h_r_egress_graph, h_r)

            ## 3.2.3 update shadow program reuse tables
            h_s_ingress_graph.SP4_reuse_id = h_r_ingress_graph.SP4_reuse_id
            h_s_egress_graph.SP4_reuse_id = h_r_egress_graph.SP4_reuse_id

            ## 3.2.4 get shadow adj list
            h_s_ingress_graph.SP4_init_adj_list()
            h_s_egress_graph.SP4_init_adj_list()


            ## DBG: get tables for each pipeline
            DBG_get_tables_for_pipeline = 0
            if DBG_get_tables_for_pipeline:        
                for each in h_r_ingress_graph.SP4_name2id:
                    # print each, 'the id is:', h_r_ingress_graph.SP4_name2id[each]
                    print each, 'the id is:', h_r_ingress_graph.nodes[each].id
                    print each, 'the type is:', h_r_ingress_graph.nodes[each].type_

                    if type(h.p4_nodes[each]) is p4_tables.p4_conditional_node:                                     
                        continue
                    print each, 'the width is:', h_r_ingress_graph.nodes[each].SP4_tb_width
                    print each, 'the depth is:', h_r_ingress_graph.nodes[each].SP4_tb_depth

            ## OPTION: print table resource info
            OPT_get_tables_summary = 1
            if OPT_get_tables_summary:  
                h_s_ingress_graph.SP4_get_table_info(h_s)
                print '    Shadow ingress:'
                h_s_ingress_graph.SP4_get_table_info_summary(h_s)

                h_s_egress_graph.SP4_get_table_info(h_s)
                print '    Shadow egress:'
                h_s_egress_graph.SP4_get_table_info_summary(h_s)


                h_r_ingress_graph.SP4_get_table_info(h_r)
                print '    real ingress:'
                h_r_ingress_graph.SP4_get_table_info_summary(h_r)

                h_r_egress_graph.SP4_get_table_info(h_r)
                print '    real egress:'
                h_r_egress_graph.SP4_get_table_info_summary(h_r)

            # return

        ## 3.3 get the WMIS graph

            ## DBG: checkout all the graph info before merge
            DBG_check_all_all_graph = 1
            if DBG_check_all_all_graph:
                print '\nDBG|MAIN|graph info: h_r:'      
                print h_r_ingress_graph.SP4_id2name
                # print h_r_ingress_graph.SP4_edges
                # print h_r_ingress_graph.SP4_adj_list
                print
                print h_r_egress_graph.SP4_id2name
                # print h_r_egress_graph.SP4_edges
                # print h_r_egress_graph.SP4_adj_list

                print '\nDBG|MAIN|graph info: h_s:'
                print h_s_ingress_graph.SP4_id2name
                # print h_s_ingress_graph.SP4_edges
                # print h_s_ingress_graph.SP4_adj_list
                print
                print h_s_egress_graph.SP4_id2name
                # print h_s_egress_graph.SP4_edges
                # print h_s_egress_graph.SP4_adj_list

                print 'DBG|MAIN|h_r ingress reused tables:', h_r_ingress_graph.SP4_reuse_id
                print 'DBG|MAIN|h_r egress reused tables:', h_r_egress_graph.SP4_reuse_id


            h_s_ingress_graph.SP4_get_merged_graph(h_r_ingress_graph)
            h_s_egress_graph.SP4_get_merged_graph(h_r_egress_graph)

            ## DBG: checkout all the resued tables info
            DBG_check_all_all_graph = 1
            if DBG_check_all_all_graph:
                print '\n    bm main-009: reused tables:'
                print h_s_ingress_graph.SP4_reuse_id
                print h_s_egress_graph.SP4_reuse_id

                print '    bm main-010: merged graph'
                print h_s_ingress_graph.SP4_merged_graph_edges
                print h_s_egress_graph.SP4_merged_graph_edges

        ## 3.4 print the MWIS gragh to file and call hurestic alg.
            ## Report merging graph info
            print '\nLOG|MERGE|merging graph info:'
            print '          Prod ingress nodes num = ', len(h_r_ingress_graph.SP4_id2name), '  edges num = ', len(h_r_ingress_graph.SP4_edges)
            print '          Test ingress nodes num = ', len(h_s_ingress_graph.SP4_id2name), '  edges num = ', len(h_s_ingress_graph.SP4_edges)
            print '               ingress reused num = ', len(h_s_ingress_graph.SP4_reuse_id)
            print ' '
            print '          Prod engress nodes num = ', len(h_r_egress_graph.SP4_id2name), '  edges num = ', len(h_r_egress_graph.SP4_edges)
            print '          Test engress nodes num = ', len(h_s_egress_graph.SP4_id2name), '  edges num = ', len(h_s_egress_graph.SP4_edges)
            print '               egress reused num = ', len(h_s_egress_graph.SP4_reuse_id)

            # res_file = "tmp2.res"
            # with open(res_file, "a") as fs:
            #     stringss = " "
            #     strings +=  '\nnLOG|MERGE|merging graph info:'
            #     strings +=  '\n          Prod ingress nodes num = ' + str( len(h_r_ingress_graph.SP4_id2name) ) + '  edges num = ' + str( len(h_r_ingress_graph.SP4_edges))  
            #     strings +=  '\n          Test ingress nodes num = ' + str( len(h_s_ingress_graph.SP4_id2name) )  + '  edges num = ' + str( len(h_s_ingress_graph.SP4_edges))  
            #     strings +=  '\n               ingress reused num = ' + str( len(h_s_ingress_graph.SP4_reuse_id) )
            #     strings +=  '\n' 
            #     strings +=  '\n          Prod engress nodes num = ' + str( len(h_r_egress_graph.SP4_id2name) )  + '  edges num = ' + str( len(h_r_egress_graph.SP4_edges))  
            #     strings +=  '\n          Test engress nodes num = ' + str( len(h_s_egress_graph.SP4_id2name) )  + '  edges num = ' + str( len(h_s_egress_graph.SP4_edges))  
            #     strings =  '\n               egress reused num = ' + str( len(h_s_egress_graph.SP4_reuse_id) )
            #     fs.wirte(strings)

            ## 3.4.1 wirte to file
            ingress_out = 'ingress'
            egress_out  = 'egress'
            h_s_ingress_graph.SP4_write_graph_to_file(args.gen_dir, ingress_out, h_r_ingress_graph)
            h_s_egress_graph.SP4_write_graph_to_file(args.gen_dir, egress_out, h_r_egress_graph)

            ## 3.4.2 call the hurestic written by C
            print 'INFO|MAIN|call wmis:'
            gen_dir = args.gen_dir
            ingress_table_graph = os.path.join(gen_dir, "ingress_table_graph.csv")
            # ingress_table_edge   = os.path.join(gen_dir, "ingress_table_edge.csv")
            ingress_res_file     = os.path.join(gen_dir, "ingress_wmis.res")
            cmd = './mwis/mwis '+ingress_table_graph+' -o '+ingress_res_file + " >> tmp/tmp.res"
            print cmd
            os.system(cmd)

            # cmd1 = './mwis/mwis '+ingress_table_graph+' -B -W -o'+ingress_res_file + " >> tmp/tmp.res"
            # print cmd1
            # os.system(cmd1)

            cmd2 = './mwis/GWMIN_alg/bin/mwis '+ingress_table_graph+' '+ingress_res_file + " >> tmp/tmp.res"
            print cmd2
            os.system(cmd2)
            # return

    ## 4.0 read heuristic result

            # read the hurestic result
            file = open(ingress_res_file, "r") 
            for each in file.readlines():
                h_s_ingress_graph.SP4_merge_id.append(int(each))
            
            # TODO: fix the number later
            h_s_egress_graph.SP4_merge_id.append(1)

            if OPT_get_tables_summary:                
                h_r_ingress_graph.SP4_reduce_reuse_tables(h_s)

    ## 5.0 merging hlir
            if AB_T:
                h_mg = SP4_merge.SP4_AB_merge_p4_objects(p4_v1_1, h_r, h_s, h_meta)
            elif DF_T:
                h_mg = SP4_merge.SP4_DF_merge_p4_objects(p4_v1_1, h_r, h_s, h_meta)

            json_dict_mg = gen_json.json_dict_create(h_mg, path_field_aliases, p4_v1_1,
                                              args.keep_pragmas)

            print "Generating MERGED P4 json output to", path_json_mg
            with open(path_json_mg, 'w') as fp:
                json.dump(json_dict_mg, fp, indent=4, separators=(',', ': '))

            OPT_generate_merge_graph = GEN_FIG
            if OPT_generate_merge_graph:
                ## generate graphs
                gen_dir = args.gen_dir
                print "Generating files in directory", gen_dir
                OPT_gen_graph_parser = True
                OPT_gen_graph_table = True
                OPT_gen_graph_deps = True
                basename = "merged_graph"
                if OPT_gen_graph_parser:
                    dot.export_parse_graph(h_mg, basename, gen_dir, no_dot=True)
                if OPT_gen_graph_table:
                    dot.export_table_graph(h_mg, basename, gen_dir, predecessors=False, no_dot=True)
                if OPT_gen_graph_deps:
                    dot.export_table_dependency_graph(h_s, basename, gen_dir,
                                                    show_conds = True, no_dot=True)

    if args.pd:
        print "Generating PD source files in", path_pd
        gen_pd.generate_pd_source(json_dict, path_pd, args.p4_prefix, args)