Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Implementation of get_cpuid().
0004  *
0005  * Copyright IBM Corp. 2014, 2018
0006  * Author(s): Alexander Yarygin <yarygin@linux.vnet.ibm.com>
0007  *        Thomas Richter <tmricht@linux.vnet.ibm.com>
0008  */
0009 
0010 #include <sys/types.h>
0011 #include <errno.h>
0012 #include <unistd.h>
0013 #include <stdio.h>
0014 #include <string.h>
0015 #include <linux/ctype.h>
0016 #include <linux/kernel.h>
0017 #include <linux/zalloc.h>
0018 
0019 #include "../../util/header.h"
0020 
0021 #define SYSINFO_MANU    "Manufacturer:"
0022 #define SYSINFO_TYPE    "Type:"
0023 #define SYSINFO_MODEL   "Model:"
0024 #define SRVLVL_CPUMF    "CPU-MF:"
0025 #define SRVLVL_VERSION  "version="
0026 #define SRVLVL_AUTHORIZATION    "authorization="
0027 #define SYSINFO     "/proc/sysinfo"
0028 #define SRVLVL      "/proc/service_levels"
0029 
0030 int get_cpuid(char *buffer, size_t sz)
0031 {
0032     char *cp, *line = NULL, *line2;
0033     char type[8], model[33], version[8], manufacturer[32], authorization[8];
0034     int tpsize = 0, mdsize = 0, vssize = 0, mfsize = 0, atsize = 0;
0035     int read;
0036     unsigned long line_sz;
0037     size_t nbytes;
0038     FILE *sysinfo;
0039 
0040     /*
0041      * Scan /proc/sysinfo line by line and read out values for
0042      * Manufacturer:, Type: and Model:, for example:
0043      * Manufacturer:    IBM
0044      * Type:            2964
0045      * Model:           702              N96
0046      * The first word is the Model Capacity and the second word is
0047      * Model (can be omitted). Both words have a maximum size of 16
0048      * bytes.
0049      */
0050     memset(manufacturer, 0, sizeof(manufacturer));
0051     memset(type, 0, sizeof(type));
0052     memset(model, 0, sizeof(model));
0053     memset(version, 0, sizeof(version));
0054     memset(authorization, 0, sizeof(authorization));
0055 
0056     sysinfo = fopen(SYSINFO, "r");
0057     if (sysinfo == NULL)
0058         return errno;
0059 
0060     while ((read = getline(&line, &line_sz, sysinfo)) != -1) {
0061         if (!strncmp(line, SYSINFO_MANU, strlen(SYSINFO_MANU))) {
0062             line2 = line + strlen(SYSINFO_MANU);
0063 
0064             while ((cp = strtok_r(line2, "\n ", &line2))) {
0065                 mfsize += scnprintf(manufacturer + mfsize,
0066                             sizeof(manufacturer) - mfsize, "%s", cp);
0067             }
0068         }
0069 
0070         if (!strncmp(line, SYSINFO_TYPE, strlen(SYSINFO_TYPE))) {
0071             line2 = line + strlen(SYSINFO_TYPE);
0072 
0073             while ((cp = strtok_r(line2, "\n ", &line2))) {
0074                 tpsize += scnprintf(type + tpsize,
0075                             sizeof(type) - tpsize, "%s", cp);
0076             }
0077         }
0078 
0079         if (!strncmp(line, SYSINFO_MODEL, strlen(SYSINFO_MODEL))) {
0080             line2 = line + strlen(SYSINFO_MODEL);
0081 
0082             while ((cp = strtok_r(line2, "\n ", &line2))) {
0083                 mdsize += scnprintf(model + mdsize, sizeof(model) - mdsize,
0084                             "%s%s", model[0] ? "," : "", cp);
0085             }
0086             break;
0087         }
0088     }
0089     fclose(sysinfo);
0090 
0091     /* Missing manufacturer, type or model information should not happen */
0092     if (!manufacturer[0] || !type[0] || !model[0])
0093         return EINVAL;
0094 
0095     /*
0096      * Scan /proc/service_levels and return the CPU-MF counter facility
0097      * version number and authorization level.
0098      * Optional, does not exist on z/VM guests.
0099      */
0100     sysinfo = fopen(SRVLVL, "r");
0101     if (sysinfo == NULL)
0102         goto skip_sysinfo;
0103     while ((read = getline(&line, &line_sz, sysinfo)) != -1) {
0104         if (strncmp(line, SRVLVL_CPUMF, strlen(SRVLVL_CPUMF)))
0105             continue;
0106 
0107         line2 = line + strlen(SRVLVL_CPUMF);
0108         while ((cp = strtok_r(line2, "\n ", &line2))) {
0109             if (!strncmp(cp, SRVLVL_VERSION,
0110                      strlen(SRVLVL_VERSION))) {
0111                 char *sep = strchr(cp, '=');
0112 
0113                 vssize += scnprintf(version + vssize,
0114                             sizeof(version) - vssize, "%s", sep + 1);
0115             }
0116             if (!strncmp(cp, SRVLVL_AUTHORIZATION,
0117                      strlen(SRVLVL_AUTHORIZATION))) {
0118                 char *sep = strchr(cp, '=');
0119 
0120                 atsize += scnprintf(authorization + atsize,
0121                             sizeof(authorization) - atsize, "%s", sep + 1);
0122             }
0123         }
0124     }
0125     fclose(sysinfo);
0126 
0127 skip_sysinfo:
0128     free(line);
0129 
0130     if (version[0] && authorization[0] )
0131         nbytes = snprintf(buffer, sz, "%s,%s,%s,%s,%s",
0132                   manufacturer, type, model, version,
0133                   authorization);
0134     else
0135         nbytes = snprintf(buffer, sz, "%s,%s,%s", manufacturer, type,
0136                   model);
0137     return (nbytes >= sz) ? ENOBUFS : 0;
0138 }
0139 
0140 char *get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
0141 {
0142     char *buf = malloc(128);
0143 
0144     if (buf && get_cpuid(buf, 128))
0145         zfree(&buf);
0146     return buf;
0147 }