~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/perf/arch/loongarch/util/header.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0-only
  2 /*
  3  * Implementation of get_cpuid().
  4  *
  5  * Author: Nikita Shubin <n.shubin@yadro.com>
  6  *         Bibo Mao <maobibo@loongson.cn>
  7  *         Huacai Chen <chenhuacai@loongson.cn>
  8  */
  9 
 10 #include <stdio.h>
 11 #include <stdlib.h>
 12 #include <api/fs/fs.h>
 13 #include <errno.h>
 14 #include "util/debug.h"
 15 #include "util/header.h"
 16 
 17 /*
 18  * Output example from /proc/cpuinfo
 19  *   CPU Family              : Loongson-64bit
 20  *   Model Name              : Loongson-3C5000
 21  *   CPU Revision            : 0x10
 22  *   FPU Revision            : 0x01
 23  */
 24 #define CPUINFO_MODEL   "Model Name"
 25 #define CPUINFO         "/proc/cpuinfo"
 26 
 27 static char *_get_field(const char *line)
 28 {
 29         char *line2, *nl;
 30 
 31         line2 = strrchr(line, ' ');
 32         if (!line2)
 33                 return NULL;
 34 
 35         line2++;
 36         nl = strrchr(line, '\n');
 37         if (!nl)
 38                 return NULL;
 39 
 40         return strndup(line2, nl - line2);
 41 }
 42 
 43 static char *_get_cpuid(void)
 44 {
 45         unsigned long line_sz;
 46         char *line, *model, *cpuid;
 47         FILE *file;
 48 
 49         file = fopen(CPUINFO, "r");
 50         if (file == NULL)
 51                 return NULL;
 52 
 53         line = model = cpuid = NULL;
 54         while (getline(&line, &line_sz, file) != -1) {
 55                 if (strncmp(line, CPUINFO_MODEL, strlen(CPUINFO_MODEL)))
 56                         continue;
 57 
 58                 model = _get_field(line);
 59                 if (!model)
 60                         goto out_free;
 61                 break;
 62         }
 63 
 64         if (model && (asprintf(&cpuid, "%s", model) < 0))
 65                 cpuid = NULL;
 66 
 67 out_free:
 68         fclose(file);
 69         free(model);
 70         return cpuid;
 71 }
 72 
 73 int get_cpuid(char *buffer, size_t sz)
 74 {
 75         int ret = 0;
 76         char *cpuid = _get_cpuid();
 77 
 78         if (!cpuid)
 79                 return EINVAL;
 80 
 81         if (sz < strlen(cpuid)) {
 82                 ret = ENOBUFS;
 83                 goto out_free;
 84         }
 85 
 86         scnprintf(buffer, sz, "%s", cpuid);
 87 
 88 out_free:
 89         free(cpuid);
 90         return ret;
 91 }
 92 
 93 char *get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
 94 {
 95         return _get_cpuid();
 96 }
 97 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php