respart: Initial commit for resource partitioning python tool
[glsdk/host-tools.git] / respart / RM-autogen.py
1 #!/usr/bin/python
2 #
3 # Author: Nikhil Devshatwar <nikhil.nd@ti.com>
4 # Python script to auto generate the Resource Management data
5 # Parses excel sheet and generates different files which need
6 # to be in sync for correct functionality
7 #
8 import argparse
9 import xlrd
10 import xlwt
11 import re
13 COL_COMMENT = 0
14 COL_RES_TYPE = 1
15 COL_SUB_TYPE = 2
16 COL_RES_FIELD = 3
17 COL_RES_START = 6
18 COL_HOST_START = 7
20 ROW_HOST_ID = 0
21 ROW_RES_START = 2
23 def gen_rmcfg_data(sharing):
25         rmcfg = ''
26         num_entries = 0
27         rmconfig_comment = '''
28                 /* %s */\n'''
29         rmconfig_templ = '''\
30                 {
31                         .start_resource = %d,
32                         .num_resource = %d,
33                         .type = RESASG_UTYPE (%s,
34                                         %s),
35                         .host_id = %s,
36                 },\n'''
38         for res in range(ROW_RES_START, sheet.nrows):
40                 comment = sheet.cell_value(res, COL_COMMENT)
41                 restype = sheet.cell_value(res, COL_RES_TYPE)
42                 subtype = sheet.cell_value(res, COL_SUB_TYPE)
43                 start = sheet.cell_value(res, COL_RES_START)
44                 if (restype == '' or subtype == '' or start == ''):
45                         continue
47                 for host in range(COL_HOST_START, sheet.ncols):
49                         #print ("##v(%d, %d) = '%s'" % (res, host, sheet.cell_value(res, host)))
50                         host_id = sheet.cell_value(ROW_HOST_ID, host).split('\n')[0]
51                         if (re.match("HOST_ID_.*", host_id) == None):
52                                 continue
54                         num = sheet.cell_value(res, host)
55                         if (num == '' or int(num) == 0):
56                                 continue
58                         if (comment != None):
59                                 rmcfg += rmconfig_comment % comment
60                                 comment = None
62                         rmcfg += (rmconfig_templ % (start, num, restype, subtype, host_id))
63                         num_entries += 1
65                         for pair in sharing:
66                                 if (host_id != pair[0]):
67                                         continue
69                                 shared_host = pair[1]
70                                 rmcfg += (rmconfig_templ % (start, num, restype, subtype, shared_host))
71                                 num_entries += 1
73                         start += int(num)
74         return (rmcfg, num_entries)
77 ################################################################################
78 ##                          Main program starts here                          ##
79 ################################################################################
81 parser = argparse.ArgumentParser(prog='RM-autogen.py', formatter_class=argparse.RawTextHelpFormatter,
82         description='RM-autogen.py - Auto generate the Resource Management data')
84 parser.add_argument('-f', '--format', required=True, dest='format',
85         action='store', choices=["boardconfig", "rtos_rmcfg", "jailhouse_cell_config"],
86         help='format to select the output file')
88 parser.add_argument('-o', '--output', dest='output',
89         action='store',
90         help='output file name')
92 parser.add_argument('-s', '--share', dest='share', default=[],
93         action='append', nargs=2, metavar=('HOST_ID_A', 'HOST_ID_B'),
94         help='Share resource with HOST_ID_A for HOST_ID_B')
96 parser.add_argument('workbook', help='Input excel sheet with assigned resources')
98 args = parser.parse_args()
99 print(args)
101 workbook = xlrd.open_workbook(args.workbook)
102 sheet = workbook.sheet_by_index(0)
104 #sheet.nrows = 9
105 if (args.format == 'boardconfig'):
106         (rmcfg, num_entries) = gen_rmcfg_data(args.share)
107         print ("Total entries = %d" % num_entries)
108         msg = "Generated rm-cfg.c"
109         data = rmcfg
110 elif (args.format == 'rtos_rmcfg'):
111         (rtos_rmcfg, num_entries) = gen_rtos_rmcfg_data()
112         msg = "Generated udma_rm-cfg.c"
113         data = rtos_rmcfg
114 else:
115         print ("ERROR: format %s not supported")
118 if (args.output):
119         ofile = open(args.output, "w")
120         ofile.write(data)
121         ofile.close()
122 else:
123         print ("%s\n%s" % (msg, data))
125 # END OF FILE