From 2ca7db75d3c049c30c841b592a5f6c3607bbb204 Mon Sep 17 00:00:00 2001 From: Alexander Egorenkov Date: Thu, 24 Jun 2021 05:06:17 +0200 Subject: [PATCH] libutil: Introduce util_arch module MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The util_arch module is supposed to provide general information about the underlying architecture of the machine in use. Signed-off-by: Alexander Egorenkov Reviewed-by: Jan Höppner Signed-off-by: Jan Höppner --- include/lib/util_arch.h | 39 ++++++++++++ libutil/util_arch.c | 117 ++++++++++++++++++++++++++++++++++++ libutil/util_arch_example.c | 74 +++++++++++++++++++++++ 3 files changed, 230 insertions(+) create mode 100644 include/lib/util_arch.h create mode 100644 libutil/util_arch.c create mode 100644 libutil/util_arch_example.c diff --git a/include/lib/util_arch.h b/include/lib/util_arch.h new file mode 100644 index 00000000..72a1b386 --- /dev/null +++ b/include/lib/util_arch.h @@ -0,0 +1,39 @@ +/** + * @defgroup util_arch_h util_arch: General architecture helpers + * @{ + * @brief General architecture helpers + * + * Copyright IBM Corp. 2021 + * + * s390-tools is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#ifndef LIB_UTIL_ARCH_H +#define LIB_UTIL_ARCH_H + +enum util_arch_machine_type { + UTIL_ARCH_MACHINE_TYPE_UNKNOWN = 0, + UTIL_ARCH_MACHINE_TYPE_Z10_EC = 2097, + UTIL_ARCH_MACHINE_TYPE_Z10_BC = 2098, + UTIL_ARCH_MACHINE_TYPE_ZE_196 = 2817, + UTIL_ARCH_MACHINE_TYPE_ZE_114 = 2818, + UTIL_ARCH_MACHINE_TYPE_ZE_EC12 = 2827, + UTIL_ARCH_MACHINE_TYPE_ZE_BC12 = 2828, + UTIL_ARCH_MACHINE_TYPE_Z13 = 2964, + UTIL_ARCH_MACHINE_TYPE_Z13_S = 2965, + UTIL_ARCH_MACHINE_TYPE_Z14 = 3906, + UTIL_ARCH_MACHINE_TYPE_Z14_ZR1 = 3907, + UTIL_ARCH_MACHINE_TYPE_Z15 = 8561, + UTIL_ARCH_MACHINE_TYPE_Z15_T02 = 8562, +}; + +int util_arch_machine_type(void); + +const char *util_arch_machine_type_str(void); + +const char *util_arch_machine_type_to_str(int type); + +unsigned long util_arch_hsa_maxsize(void); + +#endif /** LIB_UTIL_ARCH_H @} */ diff --git a/libutil/util_arch.c b/libutil/util_arch.c new file mode 100644 index 00000000..c64c1d8b --- /dev/null +++ b/libutil/util_arch.c @@ -0,0 +1,117 @@ +/* + * util - Utility function library + * + * General architecture helpers + * + * Copyright IBM Corp. 2021 + * + * s390-tools is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#include +#include +#include + +#include "lib/util_arch.h" + +#define PROC_SYSINFO "/proc/sysinfo" +#define PROC_SYSINFO_TYPE "Type:" + +#define HSA_SIZE_32M (32 * 1024 * 1024) +#define HSA_SIZE_512M (512 * 1024 * 1024) + +/** + * Get the type of the underlying architecture. + * + * @returns Type of the underlying architecture + */ +int util_arch_machine_type(void) +{ + int type = UTIL_ARCH_MACHINE_TYPE_UNKNOWN; + char *line = NULL; + FILE *fp; + + fp = fopen(PROC_SYSINFO, "r"); + if (!fp) + return type; + + while (fscanf(fp, "%m[^\n]\n", &line) == 1) { + int n = sscanf(line, PROC_SYSINFO_TYPE "%d", &type); + free(line); + if (n == 1) + break; + } + + fclose(fp); + return type; +} + +/** + * Returns a human-readable string of the current machine type. + * + * @returns Pointer to a read-only string corresponding to the current machine + * type + */ +const char *util_arch_machine_type_str(void) +{ + return util_arch_machine_type_to_str(util_arch_machine_type()); +} + +/** + * Convert a machine type to a human-readable string. + * + * @param[in] type Type of the underlying architecture + * + * @returns Pointer to a read-only string corresponding to the given machine + * type + */ +const char *util_arch_machine_type_to_str(int type) +{ + switch (type) { + case UTIL_ARCH_MACHINE_TYPE_Z10_EC: + return "IBM System z10 EC"; + case UTIL_ARCH_MACHINE_TYPE_Z10_BC: + return "IBM System z10 BC"; + case UTIL_ARCH_MACHINE_TYPE_ZE_196: + return "IBM zEnterprise 196"; + case UTIL_ARCH_MACHINE_TYPE_ZE_114: + return "IBM zEnterprise 114"; + case UTIL_ARCH_MACHINE_TYPE_ZE_EC12: + return "IBM zEnterprise EC12"; + case UTIL_ARCH_MACHINE_TYPE_ZE_BC12: + return "IBM zEnterprise BC12"; + case UTIL_ARCH_MACHINE_TYPE_Z13: + return "IBM z13"; + case UTIL_ARCH_MACHINE_TYPE_Z13_S: + return "IBM z13s"; + case UTIL_ARCH_MACHINE_TYPE_Z14: + return "IBM z14"; + case UTIL_ARCH_MACHINE_TYPE_Z14_ZR1: + return "IBM z14 ZR1"; + case UTIL_ARCH_MACHINE_TYPE_Z15: + return "IBM z15"; + case UTIL_ARCH_MACHINE_TYPE_Z15_T02: + return "IBM z15 Model T02"; + default: + return "Unknown machine type"; + } +} + +/** + * Returns the maximum size of HSA memory in bytes on this architecture. + * + * @returns Maximum HSA size in bytes + */ +unsigned long util_arch_hsa_maxsize(void) +{ + unsigned long size = HSA_SIZE_32M; + int type; + + type = util_arch_machine_type(); + if (type != UTIL_ARCH_MACHINE_TYPE_UNKNOWN && + type >= UTIL_ARCH_MACHINE_TYPE_Z15) + size = HSA_SIZE_512M; + + return size; +} diff --git a/libutil/util_arch_example.c b/libutil/util_arch_example.c new file mode 100644 index 00000000..bddb3601 --- /dev/null +++ b/libutil/util_arch_example.c @@ -0,0 +1,74 @@ +/** + * util_arch_example - Example program for util_arch + * + * Copyright IBM Corp. 2021 + * + * s390-tools is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#include + +//! [code] +#include "lib/util_arch.h" +#include "lib/util_opt.h" +#include "lib/util_prg.h" + +static const struct util_prg prg = { + .desc = "Example for util_arch.", + .copyright_vec = { + { + .owner = "IBM Corp.", + .pub_first = 2021, + .pub_last = 2021, + }, + UTIL_PRG_COPYRIGHT_END + } +}; + +static struct util_opt opt_vec[] = { + UTIL_OPT_HELP, + UTIL_OPT_VERSION, + UTIL_OPT_END +}; + +int main(int argc, char *argv[]) +{ + int mach_type; + unsigned long hsa_maxsize; + + util_prg_init(&prg); + util_opt_init(opt_vec, NULL); + + while (1) { + int opt = util_opt_getopt_long(argc, argv); + + if (opt == -1) + break; + + switch (opt) { + case 'h': + util_prg_print_help(); + util_opt_print_help(); + exit(EXIT_SUCCESS); + case 'v': + util_prg_print_version(); + exit(EXIT_SUCCESS); + case '?': + default: + fprintf(stderr, "Try '--help' for more information.\n"); + exit(EXIT_FAILURE); + } + } + + mach_type = util_arch_machine_type(); + hsa_maxsize = util_arch_hsa_maxsize(); + + printf("Machine type: %s (%d)\n", + util_arch_machine_type_to_str(mach_type), mach_type); + printf("This machine type: %s\n", util_arch_machine_type_str()); + printf("HSA max. size: %lu MiB\n", hsa_maxsize / (1024 * 1024)); + + return EXIT_SUCCESS; +} +//! [code]