GNU Linux-libre 4.19.286-gnu1
[releases.git] / tools / perf / arch / s390 / util / header.c
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
13 #include <sys/types.h>
14 #include <errno.h>
15 #include <unistd.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <ctype.h>
19
20 #include "../../util/header.h"
21 #include "../../util/util.h"
22
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"
31
32 int get_cpuid(char *buffer, size_t sz)
33 {
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;
37         int read;
38         unsigned long line_sz;
39         size_t nbytes;
40         FILE *sysinfo;
41
42         /*
43          * Scan /proc/sysinfo line by line and read out values for
44          * Manufacturer:, Type: and Model:, for example:
45          * Manufacturer:    IBM
46          * Type:            2964
47          * Model:           702              N96
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
50          * bytes.
51          */
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));
57
58         sysinfo = fopen(SYSINFO, "r");
59         if (sysinfo == NULL)
60                 return errno;
61
62         while ((read = getline(&line, &line_sz, sysinfo)) != -1) {
63                 if (!strncmp(line, SYSINFO_MANU, strlen(SYSINFO_MANU))) {
64                         line2 = line + strlen(SYSINFO_MANU);
65
66                         while ((cp = strtok_r(line2, "\n ", &line2))) {
67                                 mfsize += scnprintf(manufacturer + mfsize,
68                                                     sizeof(manufacturer) - mfsize, "%s", cp);
69                         }
70                 }
71
72                 if (!strncmp(line, SYSINFO_TYPE, strlen(SYSINFO_TYPE))) {
73                         line2 = line + strlen(SYSINFO_TYPE);
74
75                         while ((cp = strtok_r(line2, "\n ", &line2))) {
76                                 tpsize += scnprintf(type + tpsize,
77                                                     sizeof(type) - tpsize, "%s", cp);
78                         }
79                 }
80
81                 if (!strncmp(line, SYSINFO_MODEL, strlen(SYSINFO_MODEL))) {
82                         line2 = line + strlen(SYSINFO_MODEL);
83
84                         while ((cp = strtok_r(line2, "\n ", &line2))) {
85                                 mdsize += scnprintf(model + mdsize, sizeof(model) - mdsize,
86                                                     "%s%s", model[0] ? "," : "", cp);
87                         }
88                         break;
89                 }
90         }
91         fclose(sysinfo);
92
93         /* Missing manufacturer, type or model information should not happen */
94         if (!manufacturer[0] || !type[0] || !model[0])
95                 return EINVAL;
96
97         /*
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.
101          */
102         sysinfo = fopen(SRVLVL, "r");
103         if (sysinfo == NULL)
104                 goto skip_sysinfo;
105         while ((read = getline(&line, &line_sz, sysinfo)) != -1) {
106                 if (strncmp(line, SRVLVL_CPUMF, strlen(SRVLVL_CPUMF)))
107                         continue;
108
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, '=');
114
115                                 vssize += scnprintf(version + vssize,
116                                                     sizeof(version) - vssize, "%s", sep + 1);
117                         }
118                         if (!strncmp(cp, SRVLVL_AUTHORIZATION,
119                                      strlen(SRVLVL_AUTHORIZATION))) {
120                                 char *sep = strchr(cp, '=');
121
122                                 atsize += scnprintf(authorization + atsize,
123                                                     sizeof(authorization) - atsize, "%s", sep + 1);
124                         }
125                 }
126         }
127         fclose(sysinfo);
128
129 skip_sysinfo:
130         free(line);
131
132         if (version[0] && authorization[0] )
133                 nbytes = snprintf(buffer, sz, "%s,%s,%s,%s,%s",
134                                   manufacturer, type, model, version,
135                                   authorization);
136         else
137                 nbytes = snprintf(buffer, sz, "%s,%s,%s", manufacturer, type,
138                                   model);
139         return (nbytes >= sz) ? ENOBUFS : 0;
140 }
141
142 char *get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
143 {
144         char *buf = malloc(128);
145
146         if (buf && get_cpuid(buf, 128))
147                 zfree(&buf);
148         return buf;
149 }