LINUX自启动线程学习笔记

2023-12-26 06:12:27

由于嵌入式处理器的性能不够,希望尽量能减少不必要的启动进程。对处理器启动进程进行了学习,了解哪些相关介绍判断哪些必须保留的线程。·

当前我使用的LINUX 系统启动后的线程

# ps
PID   USER     COMMAND
    1 root     init  //启动部分的和GRUB相关
    2 root     [kthreadd]内核线程主节点,用于生成进程,主要用于管理硬件。
    3 root     [rcu_gp]//读取复制跟新管理间后续接收
    4 root     [rcu_par_gp]//读取复制跟新管理间后续接收
    5 root     [kworker/0:0-mm_]//内核0工作线程占位符,语法间后面介绍
    6 root     [kworker/0:0H]//内核0工作线程占位符
    7 root     [kworker/u4:0-ev]//和前面类似,不过未绑定cpu
    8 root     [mm_percpu_wq]//关于每个 CPU 工作队列线程的内存管理真内核维护者列表中的神秘消息
    9 root     [ksoftirqd/0]//一种在常规处理程序之外处理硬件中断请求的方法 CPU0
   10 root     [rcu_sched]//读取复制更新计划程序,用于在写入器和读取器之间同步数据。这是有关 RCU 的最新信息
   11 root     [rcu_bh]//作为上述rcu_sched的表亲,它在 RCU 子系统中提供了宽限期。
   12 root     [migration/0]//跨内核分配进程。每个内核一个这样的进程。
   13 root     [cpuhp/0]//支持在系统中物理添加和删除 CPU 的进程 内核0。
   14 root     [cpuhp/1]//支持在系统中物理添加和删除 CPU 的进程 内核1。
   15 root     [migration/1]//和前面类似只是内核为1 
   16 root     [ksoftirqd/1]//和前面类似只是内核为1 
   17 root     [kworker/1:0-rcu]//和前面类似只是内核为1 
   18 root     [kworker/1:0H]//和前面类似只是内核为1 
   19 root     [kdevtmpfs]//此线程填充并维护设备节点树
   20 root     [oom_reaper]//在内存分配路径上,当内存不足的时候会触发kswapd、或者内存规整,极端情况会触发OOM,来获取更多内存。
   21 root     [writeback]//处理对块设备的缓慢写入
   22 root     [kcompactd0]//kcompactd是一个内核规整的后台进程,内存压缩
   23 root     [crypto]//为内核加密模块提供 API 接口
   24 root     [kblockd]//检查 I/O 管道中的拥塞
   25 root     [watchdogd]//看门狗
   26 root     [kworker/1:1-mm_]//和前面类似只是内核为1 
   27 root     [kworker/0:1-eve]//和前面类似只是内核为1 
   28 root     [kswapd0]//页面回收kswapd内核线程
   29 root     [kworker/u5:0]//和前面类似,不过未绑定cpu
   55 root     [spi0]//SPI0 接口进程
   56 root     [spi2]//SPI2 接口进程
   72 root     /sbin/syslogd -n  //远程系统日志守护进程。
   76 root     /sbin/klogd -n   //内核日志守护进程
   87 root     -/bin/sh   //脚本进程
   89 root     [kworker/u4:1]

一些定义介绍

GRUB

GRUB 是一个用于加载和管理系统启动的完整程序。它是 Linux 发行版中最常见的引导程序(bootloader)。引导程序是计算机启动时运行的第一个软件。它加载 操作系统的内核,然后再由内核初始化操作系统的其他部分(包括 Shell、显示管理器、桌面环境 等等)。

OOM

原文连接
Linux内核为了提高内存的使用效率采用过度分配内存(over-commit memory)的办法,造成物理内存过度紧张进而触发OOM机制来杀死一些进程回收内存。

Syslog系统日志:

Syslog设备依据两个重要的文件:/etc/syslogd(守护进程)和/etc/syslog.conf配置文件,习惯上,多数syslog信息被写到/var/adm或/var/log目录下的信息文件中(messages.*)。一个典型的syslog纪录包括生成程序的名字和一个文本信息。它还包括一个设备和一个优先级范围(但不在日之中出现)。
syslogd不仅仅是记录kernel log的服务,还能记录user space中的日志。

syslogd是Linux下的一个记录日志文件服务。新版本叫做rsyslogd。

syslogd有一系列的子服务,例如mail、auth、cron、kern等等,这些子服务提供日志记录的功能,。当程序要记录log时,可以直接调用这些子服务将日志记录到设定的地方。

特殊进程

一文了解Linux下2号进程的kthreadd
Linux下有3个特殊的进程,idle进程(PID = 0), init进程(PID = 1)和kthreadd(PID = 2)

  • idle进程由系统自动创建, 运行在内核态

idle进程其pid=0,其前身是系统创建的第一个进程,也是唯一一个没有通过fork或者kernel_thread产生的进程。完成加载系统后,演变为进程调度、交换

  • init进程由idle通过kernel_thread创建,在内核空间完成初始化后, 加载init程序, 并最终用户空间

由0进程创建,完成系统的初始化. 是系统中所有其它用户进程的祖先进程

Linux中的所有进程都是有init进程创建并运行的。首先Linux内核启动,然后在用户空间中启动init进程,再启动其他系统进程。在系统启动完成完成后,init将变为守护进程监视系统其他进程。

  • kthreadd进程由idle通过kernel_thread创建,并始终运行在内核空间, 负责所有内核线程的调度和管理

它的任务就是管理和调度其他内核线程kernel_thread, 会循环执行一个kthreadd的函数,该函数的作用就是运行kthread_create_list全局链表中维护的kthread, 当我们调用kernel_thread创建的内核线程会被加入到此链表中,因此所有的内核线程都是直接或者间接的以kthreadd为父进程

RCU 相关知识

什么是RCU?是Read,Copy-Update的缩写,意指读-复制更新。是一种同步机制。其将同步开销的非对称分布发挥到逻辑极限(我简单理解位线程进程中的数据同步控制机制)。是在2002年引入在Linux内核中的。大家应该都知道内核同步机制中的像各种锁机制、信号量、内存屏障啥的,但为啥会引入一个叫读复制更新这么个同步机制呢?它有啥优点,或者说它的应用场景是什么?

考虑一个问题,我们知道,在路由器上,会有很多线程去查找路由表数据,但是更新路由表数据的情况却不多。像这种情况下我们如何做多同步?上面说的几种同步机制都不是很好的选择,这时RCU就派上用场了,具体原因下文会解释。所以,RCU主要应用在那种,读线程多,而写线程唯一的情况,同时需要保证数据一致性。搞明白了这个道理,我们来分析一下RCU。

在RCU的实现过程中,我们主要解决以下问题:

1,在读取过程中,另外一个线程删除了一个节点。删除线程可以把这个节点从链表中移除,但它不能直接销毁这个节点,必须等到所有的读取线程读取完成以后,才进行销毁操作。RCU中把这个过程称为宽限期(Grace period)。

2,在读取过程中,另外一个线程插入了一个新节点,而读线程读到了这个节点,那么需要保证读到的这个节点是完整的。这里涉及到了发布-订阅机制(Publish-Subscribe Mechanism)。

3, 保证读取链表的完整性。新增或者删除一个节点,不至于导致遍历一个链表从中间断开。但是RCU并不保证一定能读到新增的节点或者不读到要被删除的节点。

KWORKER进程

什么是 kworker 进程?
“kworker” 是 Linux 内核的工作线程,用于异步处理工作队列中的任务。这些任务包括处理硬件中断、文件系统事件、管理系统内存等。你可能会看到多个 kworker 进程,每个进程的名称后面都有一个数字,如 “kworker/0:1”、“kworker/1:2” 等。这个数字表示了 kworker 是在哪个 CPU 核心上运行的以及任务的顺序。

kworker 进程为什么会占用大量 CPU?
正常情况下,kworker 进程的 CPU 占用率应该是相对较低的。但是,在一些情况下,你可能会发现 kworker 进程占用了大量的 CPU。这可能是因为系统中有大量的底层任务需要处理,这些任务可能包括硬件中断、文件系统操作、内存管理等。

可能的原因包括:

硬件问题: 有时,某些硬件设备会产生大量的中断,导致 kworker 线程的 CPU 占用率提高。这可能是硬件故障或者驱动程序错误导致的。
I/O 等待: 如果系统中有大量的磁盘 I/O 操作,kworker 线程可能会被频繁唤醒来处理这些操作,从而导致高 CPU 占用。
内存压力: 如果系统内存使用率高,可能会导致频繁的内存回收操作,这也会导致 kworker 线程的 CPU 占用率提高。

我们需要找出是什么导致 kworker 高 CPU 占用。我们可以使用一些系统诊断工具,如 perf、iostat、vmstat 等,来找出问题的根源。根据问题的具体原因,我们可以采取相应的措施来解决问题。

深入理解 Linux 中的 kworker 进程

According to kernel.org, the syntax is kworker/%u:%d%s (cpu, id, priority). The u designates a special CPU, the unbound cpu, meaning that the kthread is currently unbound.

The workqueue workers which have negative nice value have ‘H’ postfixed to their names.

根据 kernel.org,语法是 kworker/%u:%d%s (cpu, id, priority)。u 表示一个特殊的 CPU,即未绑定的 cpu,这意味着 kthread 当前处于未绑定状态。如果该worker的nice小于0,说明它的优先级很高,所以就加了H属性。
源介绍

一个比较详细的进程说明

ditto:同上的意思
All Processes

All Processes
These are the processes running on a default install of Ubuntu Server 18.04.4. The only non-default I took was to run sshd so I could log in and get this list.

ps -ef
/sbin/init maybe-ubiquity the one true process to start them all the maybe-ubiquity part is from GRUB
[kthreadd] kernel thread master for spawning off processes largely to manage hardware. Kthread processes are shown in [square brackets].
[kworker/0:0H] a kernel worker thread placeholder
[mm_percpu_wq] wow! really not a lot about the memory management per cpu work queue thread, other than cryptic messages in the kernel maintainers list
[ksoftirqd/0] a way of handling hardware interrupt requests outside a regular handler
[rcu_sched] the Read Copy Update scheduler that synchronizes data across writer and readers. Here’s the latest on RCU
[rcu_bh] a cousin of the rcu_sched above, it provides grace periods in the RCU subsystem.
[migration/0] Distributes processes across cores. One such process per core. [watchdog/0] Checks to ensure the system’s still running, and reboots if hung, hopefully.
[cpuhp/0] Process that supports physically adding and removing CPUs from the system.
[cpuhp/1] lscpu says this machine has 4 cores, so these one-per-core kernel threads are repeated with different /n suffixes
[watchdog/1] ditto
[migration/1] ditto
[ksoftirqd/1] ditto
[kworker/1:0H] ditto
[cpuhp/2] ditto
[watchdog/2] ditto
[migration/2] ditto
[ksoftirqd/2] ditto
[kworker/2:0H] ditto
[cpuhp/3] ditto
[watchdog/3] ditto
[migration/3] ditto
[ksoftirqd/3] ditto
[kworker/3:0H] ditto
[kdevtmpfs] this thread populates and maintains a device node tree
[netns] this maintains the network namespace
[rcu_tasks_kthre] clearly a part of the Read Copy Update subsystem, but I’ll be hanged if I can find ANYTHING about this thread on the web.
[kauditd] the kernel thread responsible for auditing security events
[kworker/1:1] another kernel thread placeholder, see above
[khungtaskd] looks for hung tasks every two minutes
[oom_reaper] cleans up processes that are Out Of Memory.
[writeback] handles slow writes to block devices
[kcompactd0] handles background memory compaction
[ksmd] kernel samepage merging daemon, used by the KVM hypervisor.
[khugepaged] keeps track of huge virtual memory pages efficiently
[crypto] provides an API interface to the kernel crypto module
[kintegrityd] checks the integrity of block devices by writing/reading data to and from them.
[kblockd] checks for congestion in I/O pipes
[kworker/3:1] another generic kernel thread, see above.
[kworker/2:1] ditto
[ata_sff] handles ATA Small Form Factor interfaces
[md] handles multiple device interfaces, e.g. RAID arrays
[edac-poller] handles memory error detection and correction
[devfreq_wq] apparently allows for the reuse of frequently-used kernel workqueues
[watchdogd] probably has something to do with the watchdog thread. This is another one with almost no documentation.
[kswapd0] The manager of virtual memory. An ancient and venerable subsystem.
[kworker/u33:0] Another generic kernel thread ready to be used. See above.
[ecryptfs-kthrea] This encrypts and decrypts data passing out to the filesystem.
[kthrotld] Controls bandwidth on a request queue by throttling requests
[acpi_thermal_pm] Provides an API interface to the ACPI to provider thermal management
[ipv6_addrconf] handles the IPv6 configuration workqueue
[kstrp] the Kernel Stream Processor
[kworker/2:2] another worker thread placeholder, see the first one above
[charger_manager] about what you’d expect: a battery charger manager
[scsi_eh_0] Linux boxes almost never have SCSI disks anymore. This is the handles errors from other types of disks that appear as SCSI
[scsi_tmf_0] handles disk Task Mangement Functions
[scsi_eh_1] same as above for the next disk
[scsi_tmf_1] ditto
[scsi_eh_2] ditto
[scsi_tmf_2] ditto
[scsi_eh_3] ditto
[scsi_tmf_3] ditto
[scsi_eh_4] ditto
[scsi_tmf_4] ditto
[scsi_eh_5] ditto
[scsi_tmf_5] ditto
[e1000e] Handles the Intel Gigabit Ethernet devices
[i915/signal:0] Handles the Intel i915 Graphics drivers
[i915/signal:1] ditto
[i915/signal:2] ditto
[kworker/3:1H] another placeholder thread
[raid5wq] Probably a RAID5 driver, can’t find any info on it.
[jbd2/sda2-8] Updates the filesystem journal
[ext4-rsv-conver] Handles writeback conversion work from the ext4 filesystem.
[kworker/0:1H] another placeholder thread
[kworker/1:1H] another placeholder thread
[kworker/2:1H] another placeholder thread
/lib/systemd/systemd-journald and into userland! this is the process that collects logging data
[iscsi_eh] Can’t find much info on this one either, probably the iSCSI error handler
/sbin/lvmetad -f The Logical Volume Manager metadata caching process
[ib-comp-wq] Found one reference to it in the kernel mailing list. It has to do with the InfiniBand driver, and it’s now cpu-bound
[ib-comp-unb-wq] Dealing with the InfiniBand WorkQueue
[ib_mcast] Handling InfiniBand Multicast groups
[ib_nl_sa_wq] No info on this one, either. Some InfiniBand WorkQueue
[rdma_cm] Remote Direct Memory Access, usually part of InfiniBand
/lib/systemd/systemd-udevd The part of systemd that handles devices coming and going
/lib/systemd/systemd-timesyncd The systemd way of synchronizing the system clock to an external timekeeper. The supposedly better replacement for ntpd.
[irq/27-mei_me] One of the daemons that handles IRQ interrupts
/lib/systemd/systemd-networkd The part of the systemd juggernaut that handles networking
/lib/systemd/systemd-resolved The systemd Domain Name Service resolution manager
/usr/sbin/atd -f One of the few old-timers left: The at daemon that handles one-time delayed job execution
/usr/bin/lxcfs /var/lib/lxcfs/ Manages filesystems for fuse containers
/usr/sbin/rsyslogd -n The remote system logger daemon.
/usr/bin/python3 /usr/bin/networkd-dispatcher --run-startup-triggers Handles changes to the systemd-networkd
/usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only The Dbus message bus daemon without which there would be no systemd functionality
/usr/sbin/cron -f An old subsystem I’ve used for decades: the cron daemon for regularly scheduled tasks
/usr/lib/accountsservice/accounts-daemon Provides Dbus interface to modifiying account information
/usr/sbin/thermald --no-daemon --dbus-enable The user-land deamon to monitor the system’s heat
/lib/systemd/systemd-logind About what it appears it handles the login process. Gone are the gettys.
/usr/sbin/irqbalance --foreground Distributes IRQs among the processors
/usr/lib/policykit-1/polkitd --no-debug Facilitates letting unprivileged processes talk to privileged ones.
/usr/bin/python3 /usr/share/unattended-upgrades/unattended-upgrade-shutdown --wait-for-signal Keeps the system from shutting down while software is being updated
/bin/login -p -- The actual login process that manages a user login
[loop0] Not much information about this either. Possibly a loopback filesystem thread?
[kworker/0:6] another placeholder thread
[kworker/0:7] ditto
/usr/lib/snapd/snapd The runner for the Snap packages
/lib/systemd/systemd --user The user-land heart of systemd
(sd-pam) a helper process for the Pluggable Authentication Module system
-bash What I’m using now, the Bourne-again Shell
/usr/sbin/sshd -D The Secure Shell server process
[kworker/1:2] another placeholder thread
[kworker/3:2] ditto
[kworker/u32:0] ditto
[kworker/u32:2] ditto
[kworker/u32:1] ditto
[kworker/1:0] ditto
[kworker/3:0] ditto
ps -ef List the status of all processes

文章来源:https://blog.csdn.net/qq_42254853/article/details/135201021
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。