header.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Implementation of get_cpuid().
  3. *
  4. * Copyright IBM Corp. 2014, 2018
  5. * Author(s): Alexander Yarygin <yarygin@linux.vnet.ibm.com>
  6. * Thomas Richter <tmricht@linux.vnet.ibm.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License (version 2 only)
  10. * as published by the Free Software Foundation.
  11. */
  12. #include <sys/types.h>
  13. #include <errno.h>
  14. #include <unistd.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <ctype.h>
  18. #include "../../util/header.h"
  19. #include "../../util/util.h"
  20. #define SYSINFO_MANU "Manufacturer:"
  21. #define SYSINFO_TYPE "Type:"
  22. #define SYSINFO_MODEL "Model:"
  23. #define SRVLVL_CPUMF "CPU-MF:"
  24. #define SRVLVL_VERSION "version="
  25. #define SRVLVL_AUTHORIZATION "authorization="
  26. #define SYSINFO "/proc/sysinfo"
  27. #define SRVLVL "/proc/service_levels"
  28. int get_cpuid(char *buffer, size_t sz)
  29. {
  30. char *cp, *line = NULL, *line2;
  31. char type[8], model[33], version[8], manufacturer[32], authorization[8];
  32. int tpsize = 0, mdsize = 0, vssize = 0, mfsize = 0, atsize = 0;
  33. int read;
  34. unsigned long line_sz;
  35. size_t nbytes;
  36. FILE *sysinfo;
  37. /*
  38. * Scan /proc/sysinfo line by line and read out values for
  39. * Manufacturer:, Type: and Model:, for example:
  40. * Manufacturer: IBM
  41. * Type: 2964
  42. * Model: 702 N96
  43. * The first word is the Model Capacity and the second word is
  44. * Model (can be omitted). Both words have a maximum size of 16
  45. * bytes.
  46. */
  47. memset(manufacturer, 0, sizeof(manufacturer));
  48. memset(type, 0, sizeof(type));
  49. memset(model, 0, sizeof(model));
  50. memset(version, 0, sizeof(version));
  51. memset(authorization, 0, sizeof(authorization));
  52. sysinfo = fopen(SYSINFO, "r");
  53. if (sysinfo == NULL)
  54. return errno;
  55. while ((read = getline(&line, &line_sz, sysinfo)) != -1) {
  56. if (!strncmp(line, SYSINFO_MANU, strlen(SYSINFO_MANU))) {
  57. line2 = line + strlen(SYSINFO_MANU);
  58. while ((cp = strtok_r(line2, "\n ", &line2))) {
  59. mfsize += scnprintf(manufacturer + mfsize,
  60. sizeof(manufacturer) - mfsize, "%s", cp);
  61. }
  62. }
  63. if (!strncmp(line, SYSINFO_TYPE, strlen(SYSINFO_TYPE))) {
  64. line2 = line + strlen(SYSINFO_TYPE);
  65. while ((cp = strtok_r(line2, "\n ", &line2))) {
  66. tpsize += scnprintf(type + tpsize,
  67. sizeof(type) - tpsize, "%s", cp);
  68. }
  69. }
  70. if (!strncmp(line, SYSINFO_MODEL, strlen(SYSINFO_MODEL))) {
  71. line2 = line + strlen(SYSINFO_MODEL);
  72. while ((cp = strtok_r(line2, "\n ", &line2))) {
  73. mdsize += scnprintf(model + mdsize, sizeof(model) - mdsize,
  74. "%s%s", model[0] ? "," : "", cp);
  75. }
  76. break;
  77. }
  78. }
  79. fclose(sysinfo);
  80. /* Missing manufacturer, type or model information should not happen */
  81. if (!manufacturer[0] || !type[0] || !model[0])
  82. return EINVAL;
  83. /*
  84. * Scan /proc/service_levels and return the CPU-MF counter facility
  85. * version number and authorization level.
  86. * Optional, does not exist on z/VM guests.
  87. */
  88. sysinfo = fopen(SRVLVL, "r");
  89. if (sysinfo == NULL)
  90. goto skip_sysinfo;
  91. while ((read = getline(&line, &line_sz, sysinfo)) != -1) {
  92. if (strncmp(line, SRVLVL_CPUMF, strlen(SRVLVL_CPUMF)))
  93. continue;
  94. line2 = line + strlen(SRVLVL_CPUMF);
  95. while ((cp = strtok_r(line2, "\n ", &line2))) {
  96. if (!strncmp(cp, SRVLVL_VERSION,
  97. strlen(SRVLVL_VERSION))) {
  98. char *sep = strchr(cp, '=');
  99. vssize += scnprintf(version + vssize,
  100. sizeof(version) - vssize, "%s", sep + 1);
  101. }
  102. if (!strncmp(cp, SRVLVL_AUTHORIZATION,
  103. strlen(SRVLVL_AUTHORIZATION))) {
  104. char *sep = strchr(cp, '=');
  105. atsize += scnprintf(authorization + atsize,
  106. sizeof(authorization) - atsize, "%s", sep + 1);
  107. }
  108. }
  109. }
  110. fclose(sysinfo);
  111. skip_sysinfo:
  112. free(line);
  113. if (version[0] && authorization[0] )
  114. nbytes = snprintf(buffer, sz, "%s,%s,%s,%s,%s",
  115. manufacturer, type, model, version,
  116. authorization);
  117. else
  118. nbytes = snprintf(buffer, sz, "%s,%s,%s", manufacturer, type,
  119. model);
  120. return (nbytes >= sz) ? ENOBUFS : 0;
  121. }
  122. char *get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
  123. {
  124. char *buf = malloc(128);
  125. if (buf && get_cpuid(buf, 128))
  126. zfree(&buf);
  127. return buf;
  128. }