| # Copyright 2022 The Chromium OS Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| """ Generates mali_counters.h and mali_counters.c from mali_counters.csv |
| |
| Reads mali_counters.csv and writes mali_counters.h and mali_counters.c using |
| information. |
| """ |
| |
| import csv |
| |
| counter_block_size = 64 |
| num_counter_block_types = -1 |
| num_models = -1 |
| |
| rows = [] |
| with open('mali_counters.csv', 'r') as mali_counters_file: |
| reader = csv.reader(mali_counters_file) |
| for row in reader: |
| rows.append(row) |
| if int(row[1]) > num_models: |
| num_models = int(row[1]) |
| if int(row[2]) > num_counter_block_types: |
| num_counter_block_types = int(row[2]) |
| assert(int(row[3]) < counter_block_size) |
| |
| num_counter_block_types += 1 |
| num_models += 1 |
| |
| max_model_val = 0 |
| with open('mali_counters.h', 'w') as mali_counters_h: |
| mali_counters_h.write('// DO NOT EDIT\n') |
| mali_counters_h.write('// This file is automatically generated by gen_mali_counters.py\n\n') |
| mali_counters_h.write('/*\n') |
| mali_counters_h.write(' * Copyright 2022 The Chromium OS Authors. All rights reserved.\n') |
| mali_counters_h.write(' * Use of this source code is governed by a BSD-style license that can be\n') |
| mali_counters_h.write(' * found in the LICENSE file.\n') |
| mali_counters_h.write(' */\n\n') |
| mali_counters_h.write('#include <stddef.h>\n\n') |
| mali_counters_h.write('#ifndef __MALI_COUNTERS_H__\n') |
| mali_counters_h.write('#define __MALI_COUNTERS_H__\n\n') |
| mali_counters_h.write('#define kCounterBlockSize ' + str(counter_block_size) + '\n') |
| mali_counters_h.write('#define kNumCounterBlockTypes ' + str(num_counter_block_types) + '\n') |
| mali_counters_h.write('#define kNumModels ' + str(num_models) + '\n\n') |
| mali_counters_h.write('// clang-format off\n') |
| mali_counters_h.write('// These values are computed as:\n') |
| mali_counters_h.write('// index_in_counter_block | counter_type << 6 | model << 8\n') |
| mali_counters_h.write('// The indices were derived from the gfx-pps source code. Available at\n') |
| mali_counters_h.write('// https://gitlab.freedesktop.org/Fahien/gfx-pps/-/blob/master/include/pps/gpu/panfrost/hwc_names.h # nocheck\n') # nocheck |
| mali_counters_h.write('// clang-format on\n') |
| mali_counters_h.write('typedef enum {\n') |
| |
| for row in rows: |
| val = (int(row[1]) << 8) | (int(row[2]) << 6) | int(row[3]) |
| mali_counters_h.write(' ' + row[0] + ' = ' + str(val) + ',') |
| if 'dummy' in row[0]: # nocheck |
| mali_counters_h.write(' // # nocheck\n') |
| else: |
| mali_counters_h.write('\n') |
| |
| mali_counters_h.write('} MaliGpuCounter;\n\n') |
| mali_counters_h.write('extern const char* mali_gpu_counter_names[kNumModels]') |
| mali_counters_h.write('[kNumCounterBlockTypes]\n') |
| mali_counters_h.write(' [kCounterBlockSize];\n\n') |
| mali_counters_h.write('#endif // __MALI_COUNTERS_H__') |
| |
| counter_dict = {} |
| for row in rows: |
| model = int(row[1]) |
| block_type = int(row[2]) |
| counter_idx = int(row[3]) |
| |
| if model not in counter_dict: |
| counter_dict[model] = {} |
| |
| if block_type not in counter_dict[model]: |
| counter_dict[model][block_type] = ['NULL'] * 64 |
| |
| counter_dict[model][block_type][counter_idx] = '"' + row[0] + '"' |
| |
| with open('mali_counters.c', 'w') as mali_counters_c: |
| mali_counters_c.write('// DO NOT EDIT\n') |
| mali_counters_c.write('// This file is automatically generated by gen_mali_counters.py\n\n') |
| mali_counters_c.write('/*\n') |
| mali_counters_c.write(' * Copyright 2022 The Chromium OS Authors. All rights reserved.\n') |
| mali_counters_c.write(' * Use of this source code is governed by a BSD-style license that can be\n') |
| mali_counters_c.write(' * found in the LICENSE file.\n') |
| mali_counters_c.write(' */\n\n') |
| mali_counters_c.write('#include "mali/mali_counters.h"\n\n') |
| mali_counters_c.write('const char* mali_gpu_counter_names\n') |
| mali_counters_c.write(' [kNumModels]') |
| mali_counters_c.write('[kNumCounterBlockTypes]') |
| mali_counters_c.write('[kCounterBlockSize] = {\n') |
| |
| for model in counter_dict.keys(): |
| mali_counters_c.write(' {\n') |
| for block_type in counter_dict[model].keys(): |
| mali_counters_c.write(' {\n') |
| for counter in counter_dict[model][block_type]: |
| mali_counters_c.write(' ' + counter + ',') |
| if 'dummy' in counter: # nocheck |
| mali_counters_c.write(' // # nocheck\n') |
| else: |
| mali_counters_c.write('\n') |
| mali_counters_c.write(' },\n') |
| mali_counters_c.write(' },\n') |
| |
| mali_counters_c.write('};\n') |