I decided to check the number of CPUs, but I decided to check it in an environment where the sar command
that I usually use cannot be used, so I googled it, so I will summarize it.
In addition, the result of executing sar in the environment where sysstat is installed and sar settings are completed is as follows.
In this article, I will cover the following (4 CPU) how to check without using the sar command
.
[root@$hostname ~]# sar
Linux (Abbreviation) (4 CPU)
11:03:41 LINUX RESTART (4 CPU)
11:10:01 AM CPU %user %nice %system %iowait %steal %idle
11:20:01 AM all 0.00 0.00 0.01 0.00 0.00 99.99
Average: all 0.00 0.00 0.01 0.00 0.00 99.99
[root@$hostname ~]#
―― 2. Verification environment / version information --3 [Method 1] Check the number of CPUs using the lscpu command ―― 4. [Method 2] Check the number of CPUs by reproducing what sar is doing ―― 5. How does sar count CPU [0-9] +? ―― 6. What is the CPU that can be confirmed with sar? ―― 7. Relationship between logical processor and number of physical cores, sockets, and threads --8 [Digression] Comparison of sar -r command and free command ―― 9. Reference
/etc/cron.d/sysstat
# Run system activity accounting tool every 10 minutes
*/10 * * * * root /usr/lib64/sa/sa1 1 1
# 0 * * * * root /usr/lib64/sa/sa1 600 6 &
# Generate a daily summary of process accounting at 23:53
53 23 * * * root /usr/lib64/sa/sa2 -A
This method is the easiest.
[root@$hostname ~]# lscpu | grep -E '^Thread|^Core|^Socket|^CPU\('
CPU(s): 4
Thread(s) per core: 1
Core(s) per socket: 4
Socket(s): 1
[root@$hostname ~]#
Source: sar installation & configuration procedure
The reason will be dealt with in the next chapter, but it turns out that sar counts the number of CPUs in / sys / devices / system / cpu / cpu [0-9] +
.
[root@$hostname ~]# ls /sys/devices/system/cpu/ | grep -E "cpu[0-9]+"
cpu0
cpu1
cpu2
cpu3
[root@$hostname ~]# ls /sys/devices/system/cpu/ | grep -E "cpu[0-9]+" | wc -l
4
[root@$hostname ~]#
Now, how does sar count CPU [0-9] +?
Here, I investigated the source code of sar.
The source code is available on github.
As shown below, the part where cpu [0-9] + is counted
and the part where the target path counting cpu [0-9] + is defined
are excerpted.
--cpu [0-9] + is counted
/*
***************************************************************************
* Count number of processors in /sys.
*
* IN:
* @highest If set to TRUE, then look for the highest processor number.
* This is used when eg. the machine has 4 CPU numbered 0, 1, 4
* and 5. In this case, this procedure will return 6.
*
* RETURNS:
* Number of processors (online and offline).
* A value of 0 means that /sys was not mounted.
* A value of N (!=0) means N processor(s) (cpu0 .. cpu(N-1)).
***************************************************************************
*/
int get_sys_cpu_nr(int highest)
{
DIR *dir;
struct dirent *drd;
struct stat buf;
char line[MAX_PF_NAME];
int num_proc, proc_nr = -1;
/* Open relevant /sys directory */
if ((dir = opendir(SYSFS_DEVCPU)) == NULL)
return 0;
/* Get current file entry */
while ((drd = readdir(dir)) != NULL) {
if (!strncmp(drd->d_name, "cpu", 3) && isdigit(drd->d_name[3])) {
snprintf(line, MAX_PF_NAME, "%s/%s", SYSFS_DEVCPU, drd->d_name);
line[MAX_PF_NAME - 1] = '\0';
if (stat(line, &buf) < 0)
continue;
if (S_ISDIR(buf.st_mode)) {
if (highest) {
sscanf(drd->d_name + 3, "%d", &num_proc);
if (num_proc > proc_nr) {
proc_nr = num_proc;
}
}
else {
proc_nr++;
}
}
}
}
/* Close directory */
closedir(dir);
return (proc_nr + 1);
}
Source: [https://github.com/sysstat/sysstat/blob/master/count.c] (https://github.com/sysstat/sysstat/blob/master/count.c)
The C code itself is not very detailed, so it may be wrong, but I think it probably counts cpu [0-9] +
under SYSFS_DEVCPU
.
--path of target counting cpu [0-9] +
| /* Files */ |
|:--|:--|:--|:--|
| #define STAT | | | PRE "/proc/stat" |
(Abbreviation)
| #define SYSFS_DEVCPU | | PRE "/sys/devices/system/cpu" |
(Abbreviation)
| #define SLASH_DEV | | PRE "/dev/" |
出所:github.com/sysstat/sysstat/blob/master/common.h
As long as you refer to the kernel documentation, you can think of the CPU that can be confirmed by sar as a logical processor.
What: /sys/devices/system/cpu/ (Omitted) Description: A collection of both global and individual CPU attributes Individual CPU attributes are contained in subdirectories named by the kernel's
logical CPU number
, e.g.:/sys/devices/system/cpu/cpu#/
Source: [kernel docs sysfs-devices-system-cpu] (https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-devices-system-cpu)
At this point, I ran lscpu and was curious about the relationship between the displayed logical processor and the number of physical cores, sockets, and threads, so I investigated it.
In the blog below, the relationship between the logical processor and the number of physical cores, sockets, and threads is introduced using the execution result of the lscpu command as an example.
Logical processor = number of cpu sockets x number of physical cores / socket x number of threads / number of physical cores
The total number of logical cores = CPU sockets × physical cores per socket × threads per physical core. Therefore, the computer has 2 × 8 × 2 = 32 logical cores in total. CPU(s): 32
Source: [How many physical and logical CPU cores in your computer | Wei Bai White 巍](https://baiweiblog.wordpress.com/2017/10/27/how-many-physical-and-logical-cpu-cores- in-your-computer /)
[root@$hostname ~]# lscpu | grep -E '^Thread|^Core|^Socket|^CPU\('
CPU(s): 4 #Logical processor
Thread(s) per core: 1 #Number of threads/Number of physical cores
Core(s) per socket: 4 #Number of physical cores/socket
Socket(s): 1 #number of cpu sockets
[root@$hostname ~]#
By the way, in [Try and understand] How Linux works ~ Basic knowledge of OS and hardware learned through experiments and illustrations, I read the other day. The explanation of the difference between the sar -r command and the free command` was easy to understand, so let me introduce it.
free command field | sar -r command field | Contents |
---|---|---|
total | Not applicable | The total amount of memory installed in the system. |
free | kbmemfree | Apparent free memory. |
buff/cache | kbbuffers + kbcached | Memory used by the buffer cache and page cache. It is freed by the kernel when the system is running low on free memory. |
available | Not applicable | Virtually free memory. The value of the field plus the size of the in-kernel memory area that can be freed if there is not enough free memory. The memory that can be released includes most of the buffer cache and page cache, as well as some other kernel memory. |
Reference: [Try and understand] How Linux works ~ Basic knowledge of OS and hardware learned through experiments and illustrations (Partially modified by the author)
** Free command execution result **
[root@$hostname ~]# free
total used free shared buff/cache available
Mem: 16256588 239692 14588360 24948 1428536 15692440
Swap: 0 0 0
** sar -r command execution result **
[root@$hostname ~]# sar -r | head
Linux (Abbreviation) (4 CPU)
11:03:41 LINUX RESTART (4 CPU)
11:10:01 AM kbmemfree kbavail kbmemused %memused kbbuffers kbcached kbcommit %commit kbactive kbinact kbdirty
11:20:01 AM 14581020 15690700 1675568 10.31 2716 1333316 311968 1.92 399160 993580 0
11:30:01 AM 14580936 15690652 1675652 10.31 2716 1333324 296508 1.82 399204 993332 0
11:40:01 AM 14580064 15689796 1676524 10.31 2716 1333328 315288 1.94 400016 993336 0
11:50:01 AM 14581008 15690804 1675580 10.31 2716 1333336 315288 1.94 399880 992556 20
12:00:01 PM 14580612 15690412 1675976 10.31 2716 1333344 323312 1.99 401148 991320 0
[root@$hostname ~]#
On fukabori.fm, there was a topic related to cpuinfo that was just dealt with in this article, so I transcribed it a little.
Typing can't keep up and it's broken.
Linux can be treated like a directory or file that stores hardware information such as / dev. Speaking of Linux, cpuinfo etc. You can treat various information like a file if it is not a file. And it can be thrown into subsequent programs, whether it's a file or something other than a file, using a mechanism called a pipe. Treat everything like a file and connect the data entry and exit with standard input and standard output. Let's leave the difficult things to the subsequent processing. Design philosophy is connected to various things. It is a strong restriction that this can be treated in the same way. Demonstrates high reusability.
Source: 28. Aesthetic eye for technology selection (1) w / twada
Recommended Posts