2 * Implementation of get_cpuid().
4 * Copyright IBM Corp. 2014, 2018
5 * Author(s): Alexander Yarygin <yarygin@linux.vnet.ibm.com>
6 * Thomas Richter <tmricht@linux.vnet.ibm.com>
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.
13 #include <sys/types.h>
20 #include "../../util/header.h"
21 #include "../../util/util.h"
23 #define SYSINFO_MANU "Manufacturer:"
24 #define SYSINFO_TYPE "Type:"
25 #define SYSINFO_MODEL "Model:"
26 #define SRVLVL_CPUMF "CPU-MF:"
27 #define SRVLVL_VERSION "version="
28 #define SRVLVL_AUTHORIZATION "authorization="
29 #define SYSINFO "/proc/sysinfo"
30 #define SRVLVL "/proc/service_levels"
32 int get_cpuid(char *buffer, size_t sz)
34 char *cp, *line = NULL, *line2;
35 char type[8], model[33], version[8], manufacturer[32], authorization[8];
36 int tpsize = 0, mdsize = 0, vssize = 0, mfsize = 0, atsize = 0;
38 unsigned long line_sz;
43 * Scan /proc/sysinfo line by line and read out values for
44 * Manufacturer:, Type: and Model:, for example:
48 * The first word is the Model Capacity and the second word is
49 * Model (can be omitted). Both words have a maximum size of 16
52 memset(manufacturer, 0, sizeof(manufacturer));
53 memset(type, 0, sizeof(type));
54 memset(model, 0, sizeof(model));
55 memset(version, 0, sizeof(version));
56 memset(authorization, 0, sizeof(authorization));
58 sysinfo = fopen(SYSINFO, "r");
62 while ((read = getline(&line, &line_sz, sysinfo)) != -1) {
63 if (!strncmp(line, SYSINFO_MANU, strlen(SYSINFO_MANU))) {
64 line2 = line + strlen(SYSINFO_MANU);
66 while ((cp = strtok_r(line2, "\n ", &line2))) {
67 mfsize += scnprintf(manufacturer + mfsize,
68 sizeof(manufacturer) - mfsize, "%s", cp);
72 if (!strncmp(line, SYSINFO_TYPE, strlen(SYSINFO_TYPE))) {
73 line2 = line + strlen(SYSINFO_TYPE);
75 while ((cp = strtok_r(line2, "\n ", &line2))) {
76 tpsize += scnprintf(type + tpsize,
77 sizeof(type) - tpsize, "%s", cp);
81 if (!strncmp(line, SYSINFO_MODEL, strlen(SYSINFO_MODEL))) {
82 line2 = line + strlen(SYSINFO_MODEL);
84 while ((cp = strtok_r(line2, "\n ", &line2))) {
85 mdsize += scnprintf(model + mdsize, sizeof(model) - mdsize,
86 "%s%s", model[0] ? "," : "", cp);
93 /* Missing manufacturer, type or model information should not happen */
94 if (!manufacturer[0] || !type[0] || !model[0])
98 * Scan /proc/service_levels and return the CPU-MF counter facility
99 * version number and authorization level.
100 * Optional, does not exist on z/VM guests.
102 sysinfo = fopen(SRVLVL, "r");
105 while ((read = getline(&line, &line_sz, sysinfo)) != -1) {
106 if (strncmp(line, SRVLVL_CPUMF, strlen(SRVLVL_CPUMF)))
109 line2 = line + strlen(SRVLVL_CPUMF);
110 while ((cp = strtok_r(line2, "\n ", &line2))) {
111 if (!strncmp(cp, SRVLVL_VERSION,
112 strlen(SRVLVL_VERSION))) {
113 char *sep = strchr(cp, '=');
115 vssize += scnprintf(version + vssize,
116 sizeof(version) - vssize, "%s", sep + 1);
118 if (!strncmp(cp, SRVLVL_AUTHORIZATION,
119 strlen(SRVLVL_AUTHORIZATION))) {
120 char *sep = strchr(cp, '=');
122 atsize += scnprintf(authorization + atsize,
123 sizeof(authorization) - atsize, "%s", sep + 1);
132 if (version[0] && authorization[0] )
133 nbytes = snprintf(buffer, sz, "%s,%s,%s,%s,%s",
134 manufacturer, type, model, version,
137 nbytes = snprintf(buffer, sz, "%s,%s,%s", manufacturer, type,
139 return (nbytes >= sz) ? ENOBUFS : 0;
142 char *get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
144 char *buf = malloc(128);
146 if (buf && get_cpuid(buf, 128))