llvm.org GIT mirror llvm / 2f56901
Refactor and simplify the code reading /proc/cpuinfo. NFC. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@224410 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 4 years ago
1 changed file(s) with 32 addition(s) and 47 deletion(s). Raw diff Collapse all Expand all
1616 #include "llvm/ADT/StringSwitch.h"
1717 #include "llvm/ADT/Triple.h"
1818 #include "llvm/Config/config.h"
19 #include "llvm/Support/DataStream.h"
2019 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/FileSystem.h"
2121 #include "llvm/Support/raw_ostream.h"
2222 #include
2323
4747 //===----------------------------------------------------------------------===//
4848
4949 using namespace llvm;
50
51 static ssize_t LLVM_ATTRIBUTE_UNUSED readCpuInfo(void *Buf, size_t Size) {
52 assert(0);
53 // Note: We cannot mmap /proc/cpuinfo here and then process the resulting
54 // memory buffer because the 'file' has 0 size (it can be read from only
55 // as a stream).
56
57 int FD;
58 std::error_code EC = sys::fs::openFileForRead("/proc/cpuinfo", FD);
59 if (EC) {
60 DEBUG(dbgs() << "Unable to open /proc/cpuinfo: " << EC.message() << "\n");
61 return -1;
62 }
63 int Ret = read(FD, Buf, Size);
64 int CloseStatus = close(FD);
65 if (CloseStatus)
66 return -1;
67 return Ret;
68 }
5069
5170 #if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)\
5271 || defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64)
488507 // processor type. On Linux, this is exposed through the /proc/cpuinfo file.
489508 const char *generic = "generic";
490509
491 // Note: We cannot mmap /proc/cpuinfo here and then process the resulting
492 // memory buffer because the 'file' has 0 size (it can be read from only
493 // as a stream).
494
495 std::string Err;
496 DataStreamer *DS = getDataFileStreamer("/proc/cpuinfo", &Err);
497 if (!DS) {
498 DEBUG(dbgs() << "Unable to open /proc/cpuinfo: " << Err << "\n");
499 return generic;
500 }
501
502510 // The cpu line is second (after the 'processor: 0' line), so if this
503511 // buffer is too small then something has changed (or is wrong).
504512 char buffer[1024];
505 size_t CPUInfoSize = DS->GetBytes((unsigned char*) buffer, sizeof(buffer));
506 delete DS;
513 ssize_t CPUInfoSize = readCpuInfo(buffer, sizeof(buffer));
514 if (CPUInfoSize == -1)
515 return generic;
507516
508517 const char *CPUInfoStart = buffer;
509518 const char *CPUInfoEnd = buffer + CPUInfoSize;
577586 StringRef sys::getHostCPUName() {
578587 // The cpuid register on arm is not accessible from user space. On Linux,
579588 // it is exposed through the /proc/cpuinfo file.
580 // Note: We cannot mmap /proc/cpuinfo here and then process the resulting
581 // memory buffer because the 'file' has 0 size (it can be read from only
582 // as a stream).
583
584 std::string Err;
585 DataStreamer *DS = getDataFileStreamer("/proc/cpuinfo", &Err);
586 if (!DS) {
587 DEBUG(dbgs() << "Unable to open /proc/cpuinfo: " << Err << "\n");
588 return "generic";
589 }
590589
591590 // Read 1024 bytes from /proc/cpuinfo, which should contain the CPU part line
592591 // in all cases.
593592 char buffer[1024];
594 size_t CPUInfoSize = DS->GetBytes((unsigned char*) buffer, sizeof(buffer));
595 delete DS;
593 ssize_t CPUInfoSize = readCpuInfo(buffer, sizeof(buffer));
594 if (CPUInfoSize == -1)
595 return "generic";
596596
597597 StringRef Str(buffer, CPUInfoSize);
598598
642642 #elif defined(__linux__) && defined(__s390x__)
643643 StringRef sys::getHostCPUName() {
644644 // STIDP is a privileged operation, so use /proc/cpuinfo instead.
645 // Note: We cannot mmap /proc/cpuinfo here and then process the resulting
646 // memory buffer because the 'file' has 0 size (it can be read from only
647 // as a stream).
648
649 std::string Err;
650 DataStreamer *DS = getDataFileStreamer("/proc/cpuinfo", &Err);
651 if (!DS) {
652 DEBUG(dbgs() << "Unable to open /proc/cpuinfo: " << Err << "\n");
653 return "generic";
654 }
655645
656646 // The "processor 0:" line comes after a fair amount of other information,
657647 // including a cache breakdown, but this should be plenty.
658648 char buffer[2048];
659 size_t CPUInfoSize = DS->GetBytes((unsigned char*) buffer, sizeof(buffer));
660 delete DS;
649 ssize_t CPUInfoSize = readCpuInfo(buffer, sizeof(buffer));
650 if (CPUInfoSize == -1)
651 return "generic";
661652
662653 StringRef Str(buffer, CPUInfoSize);
663654 SmallVector Lines;
689680
690681 #if defined(__linux__) && (defined(__arm__) || defined(__aarch64__))
691682 bool sys::getHostCPUFeatures(StringMap &Features) {
692 std::string Err;
693 DataStreamer *DS = getDataFileStreamer("/proc/cpuinfo", &Err);
694 if (!DS) {
695 DEBUG(dbgs() << "Unable to open /proc/cpuinfo: " << Err << "\n");
696 return false;
697 }
698
699683 // Read 1024 bytes from /proc/cpuinfo, which should contain the Features line
700684 // in all cases.
701685 char buffer[1024];
702 size_t CPUInfoSize = DS->GetBytes((unsigned char*) buffer, sizeof(buffer));
703 delete DS;
686 ssize_t CPUInfoSize = readCpuInfo(buffer, sizeof(buffer));
687 if (CPUInfoSize == -1)
688 return false;
704689
705690 StringRef Str(buffer, CPUInfoSize);
706691