Path: blob/master/tools/perf/arch/loongarch/util/header.c
26295 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* Implementation of get_cpuid().3*4* Author: Nikita Shubin <[email protected]>5* Bibo Mao <[email protected]>6* Huacai Chen <[email protected]>7*/89#include <stdio.h>10#include <stdlib.h>11#include <api/fs/fs.h>12#include <errno.h>13#include "util/debug.h"14#include "util/header.h"1516/*17* Output example from /proc/cpuinfo18* CPU Family : Loongson-64bit19* Model Name : Loongson-3C500020* CPU Revision : 0x1021* FPU Revision : 0x0122*/23#define CPUINFO_MODEL "Model Name"24#define CPUINFO "/proc/cpuinfo"2526static char *_get_field(const char *line)27{28char *line2, *nl;2930line2 = strrchr(line, ' ');31if (!line2)32return NULL;3334line2++;35nl = strrchr(line, '\n');36if (!nl)37return NULL;3839return strndup(line2, nl - line2);40}4142static char *_get_cpuid(void)43{44unsigned long line_sz;45char *line, *model, *cpuid;46FILE *file;4748file = fopen(CPUINFO, "r");49if (file == NULL)50return NULL;5152line = model = cpuid = NULL;53while (getline(&line, &line_sz, file) != -1) {54if (strncmp(line, CPUINFO_MODEL, strlen(CPUINFO_MODEL)))55continue;5657model = _get_field(line);58if (!model)59goto out_free;60break;61}6263if (model && (asprintf(&cpuid, "%s", model) < 0))64cpuid = NULL;6566out_free:67fclose(file);68free(model);69return cpuid;70}7172int get_cpuid(char *buffer, size_t sz, struct perf_cpu cpu __maybe_unused)73{74int ret = 0;75char *cpuid = _get_cpuid();7677if (!cpuid)78return EINVAL;7980if (sz < strlen(cpuid)) {81ret = ENOBUFS;82goto out_free;83}8485scnprintf(buffer, sz, "%s", cpuid);8687out_free:88free(cpuid);89return ret;90}9192char *get_cpuid_str(struct perf_cpu cpu __maybe_unused)93{94return _get_cpuid();95}969798