[LINUX] How to find out the number of CPUs without using the sar command

0. Introduction

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 ~]# 

1. Table of contents

―― 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

2. Verification environment / version information

/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

3. [Method 1] Check the number of CPUs using the lscpu command.

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

4. [Method 2] Check the number of CPUs by reproducing what sar is doing

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 ~]# 

5. How does sar count CPU [0-9] +?

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

6. What is the CPU that can be confirmed with sar?

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)

7. Relationship between logical processor and number of physical cores, sockets, and threads

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.

8. [Digression] Comparison of sar -r command and free command

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 ~]# 

I also do P.S. Twitter, so if you follow me, I'll be happy to cry :)

@gkzvoice

What I heard on P.P.S. fukabori.fm

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

How to find out the number of CPUs without using the sar command
How to find the optimal number of clusters in k-means
How to find the area of the Voronoi diagram
[Circuit x Python] How to find the transfer function of a circuit using Lcapy
Find out how to divide a file with a certain number of lines evenly
How to know the port number of the xinetd service
How to write a GUI using the maya command
Projecet Euler 12 Find the number of divisors without division.
How to get the number of digits in Python
How to find out which process is using the localhost port and stop it
I dare to fill out the form without using selenium
Maya | Find out the number of polygons in the selected object
Python --Find out number of groups in the regex expression
How to increase the number of machine learning dataset images
How to find the scaling factor of a biorthogonal wavelet
How to know the number of GPUs from python ~ Notes on using multiprocessing with pytorch ~
How to execute the sed command many times using the for statement
Find out the age and number of winnings of prefectural governors nationwide
ABC170 E --How to solve without using multiset of Smart Infants
[EC2] How to install chrome and the contents of each command
How to find the memory address of a Pandas dataframe value
How to output the output result of the Linux man command to a file
How to monitor the execution status of sqlldr with the pv command
How to check the version of Django
[Linux] How to use the echo command
How to calculate Use% of df command
How to use the Linux grep command
Migemo version of the: find command,: mfind
How to put a line number at the beginning of a CSV file
How to play a video while watching the number of frames (Mac)
[Completed version] Try to find out the number of residents in the town from the address list with Python
How to pass the execution result of a shell command in a list in Python
How to calculate the volatility of a brand
Combinatorial optimization to find the hand of "Millijan"
How to find out what kind of files are stored in S3 in Python
How to count the number of elements in Django and output to a template
python: Tips for displaying an array (list) with an index (how to find out what number an element of an array is)
Find the number of days in a month
A memorandum of how to execute the! Sudo magic command in Jupyter Notebook
How to find the coefficient of the trendline that passes through the vertices in Python
Find out the day of the week with datetime
An introduction to data analysis using Python-To increase the number of video views-
Find the geometric mean of n! Using Python
How to identify the system call number ausyscall
Determine the number of classes using the Starges formula
How to find the correlation for categorical variables
I tried to find out how to streamline the work flow with Excel x Python ②
I tried to find out how to streamline the work flow with Excel x Python ④
How to identify the element with the smallest number of characters in a Python list?
I tried to find out how to streamline the work flow with Excel x Python ⑤
How to avoid the cut-off label of the graph created by the plot module using matplotlib
How to start the code written in Atom with one command without starting teminal
I tried to find out how to streamline the work flow with Excel x Python ①
How to confirm the Persival theorem using the Fourier transform (FFT) of matplotlib and scipy
Find out the maximum number of characters in multi-line text stored in a data frame
I used Python to find out about the role choices of the 51 "Yachts" in the world.
Command to check the total number of CPU physical cores / logical cores / physical memory on Mac
Find a guideline for the number of processes / threads to set in the application server
How to count the number of occurrences of each element in the list in Python with weight
I tried to find out how to streamline the work flow with Excel x Python ③
How to create an article from the command line