mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-29 21:41:00 +08:00
Merge remote-tracking branch 'LCTT/master'
This commit is contained in:
commit
2464668989
106
published/20140510 Journey to the Stack Part I.md
Normal file
106
published/20140510 Journey to the Stack Part I.md
Normal file
@ -0,0 +1,106 @@
|
||||
探秘“栈”之旅
|
||||
==============
|
||||
|
||||
早些时候,我们探索了 [“内存中的程序之秘”][2],我们欣赏了在一台电脑中是如何运行我们的程序的。今天,我们去探索*栈的调用*,它在大多数编程语言和虚拟机中都默默地存在。在此过程中,我们将接触到一些平时很难见到的东西,像<ruby>闭包<rt>closure</rt></ruby>、递归、以及缓冲溢出等等。但是,我们首先要作的事情是,描绘出栈是如何运作的。
|
||||
|
||||
栈非常重要,因为它追踪着一个程序中运行的*函数*,而函数又是一个软件的重要组成部分。事实上,程序的内部操作都是非常简单的。它大部分是由函数向栈中推入数据或者从栈中弹出数据的相互调用组成的,而在堆上为数据分配内存才能在跨函数的调用中保持数据。不论是低级的 C 软件还是像 JavaScript 和 C# 这样的基于虚拟机的语言,它们都是这样的。而对这些行为的深刻理解,对排错、性能调优以及大概了解究竟发生了什么是非常重要的。
|
||||
|
||||
当一个函数被调用时,将会创建一个<ruby>栈帧<rt>stack frame</rt></ruby>去支持函数的运行。这个栈帧包含函数的*局部变量*和调用者传递给它的*参数*。这个栈帧也包含了允许被调用的函数(*callee*)安全返回给其调用者的内部事务信息。栈帧的精确内容和结构因处理器架构和函数调用规则而不同。在本文中我们以 Intel x86 架构和使用 C 风格的函数调用(`cdecl`)的栈为例。下图是一个处于栈顶部的一个单个栈帧:
|
||||
|
||||
![](https://manybutfinite.com/img/stack/stackIntro.png)
|
||||
|
||||
在图上的场景中,有三个 CPU 寄存器进入栈。<ruby>栈指针<rt>stack pointer</rt></ruby> `esp`(LCTT 译注:扩展栈指针寄存器) 指向到栈的顶部。栈的顶部总是被最*后一个推入到栈且还没有弹出*的东西所占据,就像现实世界中堆在一起的一叠盘子或者 100 美元大钞一样。
|
||||
|
||||
保存在 `esp` 中的地址始终在变化着,因为栈中的东西不停被推入和弹出,而它总是指向栈中的最后一个推入的东西。许多 CPU 指令的一个副作用就是自动更新 `esp`,离开寄存器而使用栈是行不通的。
|
||||
|
||||
在 Intel 的架构中,绝大多数情况下,栈的增长是向着*低位内存地址*的方向。因此,这个“顶部” 在包含数据的栈中是处于低位的内存地址(在这种情况下,包含的数据是 `local_buffer`)。注意,关于从 `esp` 到 `local_buffer` 的箭头不是随意连接的。这个箭头代表着事务:它*专门*指向到由 `local_buffer` 所拥有的*第一个字节*,因为,那是一个保存在 `esp` 中的精确地址。
|
||||
|
||||
第二个寄存器跟踪的栈是 `ebp`(LCTT 译注:扩展基址指针寄存器),它包含一个<ruby>基指针<rt>base pointer</rt></ruby>或者称为<ruby>帧指针<rt>frame pointer</rt></ruby>。它指向到一个*当前运行*的函数的栈帧内的固定位置,并且它为参数和局部变量的访问提供一个稳定的参考点(基址)。仅当开始或者结束调用一个函数时,`ebp` 的内容才会发生变化。因此,我们可以很容易地处理在栈中的从 `ebp` 开始偏移后的每个东西。如图所示。
|
||||
|
||||
不像 `esp`, `ebp` 大多数情况下是在程序代码中通过花费很少的 CPU 来进行维护的。有时候,完成抛弃 `ebp` 有一些性能优势,可以通过 [编译标志][3] 来做到这一点。Linux 内核就是一个这样做的示例。
|
||||
|
||||
最后,`eax`(LCTT 译注:扩展的 32 位通用数据寄存器)寄存器惯例被用来转换大多数 C 数据类型返回值给调用者。
|
||||
|
||||
现在,我们来看一下在我们的栈帧中的数据。下图清晰地按字节展示了字节的内容,就像你在一个调试器中所看到的内容一样,内存是从左到右、从顶部至底部增长的,如下图所示:
|
||||
|
||||
![](https://manybutfinite.com/img/stack/frameContents.png)
|
||||
|
||||
局部变量 `local_buffer` 是一个字节数组,包含一个由 null 终止的 ASCII 字符串,这是 C 程序中的一个基本元素。这个字符串可以读取自任意地方,例如,从键盘输入或者来自一个文件,它只有 7 个字节的长度。因为,`local_buffer` 只能保存 8 字节,所以还剩下 1 个未使用的字节。*这个字节的内容是未知的*,因为栈不断地推入和弹出,*除了你写入的之外*,你根本不会知道内存中保存了什么。这是因为 C 编译器并不为栈帧初始化内存,所以它的内容是未知的并且是随机的 —— 除非是你自己写入。这使得一些人对此很困惑。
|
||||
|
||||
再往上走,`local1` 是一个 4 字节的整数,并且你可以看到每个字节的内容。它似乎是一个很大的数字,在8 后面跟着的都是零,在这里可能会误导你。
|
||||
|
||||
Intel 处理器是<ruby>小端<rt>little endian</rt></ruby>机器,这表示在内存中的数字也是首先从小的一端开始的。因此,在一个多字节数字中,较小的部分在内存中处于最低端的地址。因为一般情况下是从左边开始显示的,这背离了我们通常的数字表示方式。我们讨论的这种从小到大的机制,使我想起《格里佛游记》:就像小人国的人们吃鸡蛋是从小头开始的一样,Intel 处理器处理它们的数字也是从字节的小端开始的。
|
||||
|
||||
因此,`local1` 事实上只保存了一个数字 8,和章鱼的腿数量一样。然而,`param1` 在第二个字节的位置有一个值 2,因此,它的数学上的值是 `2 * 256 = 512`(我们与 256 相乘是因为,每个位置值的范围都是从 0 到 255)。同时,`param2` 承载的数量是 `1 * 256 * 256 = 65536`。
|
||||
|
||||
这个栈帧的内部数据是由两个重要的部分组成:*前一个*栈帧的地址(保存的 `ebp` 值)和函数退出才会运行的指令的地址(返回地址)。它们一起确保了函数能够正常返回,从而使程序可以继续正常运行。
|
||||
|
||||
现在,我们来看一下栈帧是如何产生的,以及去建立一个它们如何共同工作的内部蓝图。首先,栈的增长是非常令人困惑的,因为它与你你预期的方式相反。例如,在栈上分配一个 8 字节,就要从 `esp` 减去 8,去,而减法是与增长不同的奇怪方式。
|
||||
|
||||
我们来看一个简单的 C 程序:
|
||||
|
||||
```
|
||||
Simple Add Program - add.c
|
||||
|
||||
int add(int a, int b)
|
||||
{
|
||||
int result = a + b;
|
||||
return result;
|
||||
}
|
||||
|
||||
int main(int argc)
|
||||
{
|
||||
int answer;
|
||||
answer = add(40, 2);
|
||||
}
|
||||
```
|
||||
|
||||
*简单的加法程序 - add.c*
|
||||
|
||||
假设我们在 Linux 中不使用命令行参数去运行它。当你运行一个 C 程序时,实际运行的第一行代码是在 C 运行时库里,由它来调用我们的 `main` 函数。下图展示了程序运行时每一步都发生了什么。每个图链接的 GDB 输出展示了内存和寄存器的状态。你也可以看到所使用的 [GDB 命令][4],以及整个 [GDB 输出][5]。如下:
|
||||
|
||||
![](https://manybutfinite.com/img/stack/mainProlog.png)
|
||||
|
||||
第 2 步和第 3 步,以及下面的第 4 步,都只是函数的<ruby>序言<rt>prologue</rt></ruby>,几乎所有的函数都是这样的:`ebp` 的当前值被保存到了栈的顶部,然后,将 `esp` 的内容拷贝到 `ebp`,以建立一个新的栈帧。`main` 的序言和其它函数一样,但是,不同之处在于,当程序启动时 `ebp` 被清零。
|
||||
|
||||
如果你去检查栈下方(右边)的整形变量(`argc`),你将找到更多的数据,包括指向到程序名和命令行参数(传统的 C 的 `argv`)、以及指向 Unix 环境变量以及它们真实的内容的指针。但是,在这里这些并不是重点,因此,继续向前调用 `add()`:
|
||||
|
||||
![](https://manybutfinite.com/img/stack/callAdd.png)
|
||||
|
||||
在 `main` 从 `esp` 减去 12 之后得到它所需的栈空间,它为 `a` 和 `b` 设置值。在内存中的值展示为十六进制,并且是小端格式,与你从调试器中看到的一样。一旦设置了参数值,`main` 将调用 `add`,并且开始运行:
|
||||
|
||||
![](https://manybutfinite.com/img/stack/addProlog.png)
|
||||
|
||||
现在,有一点小激动!我们进入了另一个函数序言,但这次你可以明确看到栈帧是如何从 `ebp` 到栈建立一个链表。这就是调试器和高级语言中的 `Exception` 对象如何对它们的栈进行跟踪的。当一个新帧产生时,你也可以看到更多这种典型的从 `ebp` 到 `esp` 的捕获。我们再次从 `esp` 中做减法得到更多的栈空间。
|
||||
|
||||
当 `ebp` 寄存器的值拷贝到内存时,这里也有一个稍微有些怪异的字节逆转。在这里发生的奇怪事情是,寄存器其实并没有字节顺序:因为对于内存,没有像寄存器那样的“增长的地址”。因此,惯例上调试器以对人类来说最自然的格式展示了寄存器的值:数位从最重要的到最不重要。因此,这个在小端机器中的副本的结果,与内存中常用的从左到右的标记法正好相反。我想用图去展示你将会看到的东西,因此有了下面的图。
|
||||
|
||||
在比较难懂的部分,我们增加了注释:
|
||||
|
||||
![](https://manybutfinite.com/img/stack/doAdd.png)
|
||||
|
||||
这是一个临时寄存器,用于帮你做加法,因此没有什么警报或者惊喜。对于加法这样的作业,栈的动作正好相反,我们留到下次再讲。
|
||||
|
||||
对于任何读到这里的人都应该有一个小礼物,因此,我做了一个大的图表展示了 [组合到一起的所有步骤][6]。
|
||||
|
||||
一旦把它们全部布置好了,看上起似乎很乏味。这些小方框给我们提供了很多帮助。事实上,在计算机科学中,这些小方框是主要的展示工具。我希望这些图片和寄存器的移动能够提供一种更直观的构想图,将栈的增长和内存的内容整合到一起。从软件的底层运作来看,我们的软件与一个简单的图灵机器差不多。
|
||||
|
||||
这就是我们栈探秘的第一部分,再讲一些内容之后,我们将看到构建在这个基础上的高级编程的概念。下周见!
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via:https://manybutfinite.com/post/journey-to-the-stack/
|
||||
|
||||
作者:[Gustavo Duarte][a]
|
||||
译者:[qhwdw](https://github.com/qhwdw)
|
||||
校对:[wxy](https://github.com/wxy)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://duartes.org/gustavo/blog/about/
|
||||
[1]:https://manybutfinite.com/post/journey-to-the-stack/
|
||||
[2]:https://linux.cn/article-9255-1.html
|
||||
[3]:http://stackoverflow.com/questions/14666665/trying-to-understand-gcc-option-fomit-frame-pointer
|
||||
[4]:https://github.com/gduarte/blog/blob/master/code/x86-stack/add-gdb-commands.txt
|
||||
[5]:https://github.com/gduarte/blog/blob/master/code/x86-stack/add-gdb-output.txt
|
||||
[6]:https://manybutfinite.com/img/stack/callSequence.png
|
@ -15,7 +15,7 @@
|
||||
|
||||
![SpeedCrunch graphical interface][3]
|
||||
|
||||
*SpeedCrunch 在工作时*
|
||||
*SpeedCrunch 运行中*
|
||||
|
||||
它支持单位,并且可用在所有函数中。
|
||||
|
||||
@ -57,7 +57,7 @@ SpeedCrunch 会默认地将结果转化为国际标准单位,但还是可以
|
||||
|
||||
![Qalculate! Interface][7]
|
||||
|
||||
*在 Qalculate! 中寻找物理常量*
|
||||
*在 Qalculate! 中查看物理常量*
|
||||
|
||||
在单位的使用方面,Qalculate! 会比 SppedCrunch 更加直观,而且可以识别一些常用前缀。你有听说过 exapascal 压力吗?反正我没有(太阳的中心大概在 `~26 PPa`),但 Qalculate! ,可以准确 `1 EPa` 的意思。同时,Qalculate! 可以更加灵活地处理语法错误,所以你不需要担心打括号:如果没有歧义,Qalculate! 会直接给出正确答案。
|
||||
|
||||
@ -69,7 +69,7 @@ SpeedCrunch 会默认地将结果转化为国际标准单位,但还是可以
|
||||
|
||||
好吧,这不是“计算器”,但这个程序非常好用。
|
||||
|
||||
大部分单位转换器只是一个大的基本单位列表以及一大堆基本组合,但 [ConvertAll][9] 与它们不一样。有试过把光年转换为英尺每秒吗?不管它们说不说得通,只要你想转换任何种类的单位,ConvertAll 就是你要的工具。
|
||||
大部分单位转换器只是一个大的基本单位列表以及一大堆基本组合,但 [ConvertAll][9] 与它们不一样。有试过把天文单位每年转换为英寸每秒吗?不管它们说不说得通,只要你想转换任何种类的单位,ConvertAll 就是你要的工具。
|
||||
|
||||
只需要在相应的输入框内输入转换前和转换后的单位:如果单位相容,你会直接得到答案。
|
||||
|
||||
|
@ -1,120 +0,0 @@
|
||||
Translating by qhwdw
|
||||
Testing IPv6 Networking in KVM: Part 2
|
||||
======
|
||||
|
||||
![](https://www.linux.com/sites/lcom/files/styles/rendered_file/public/banner_4.png?itok=yZBHylwd)
|
||||
When last we met, in [Testing IPv6 Networking in KVM: Part 1][1], we learned about IPv6 private addressing. Today, we're going to use KVM to create networks for testing IPv6 to our heart's content.
|
||||
|
||||
Should you desire a refresh in using KVM, see [Creating Virtual Machines in KVM: Part 1][2] and [Creating Virtual Machines in KVM: Part 2 - Networking][3].
|
||||
|
||||
### Creating Networks in KVM
|
||||
|
||||
You need at least two virtual machines in KVM. Of course, you may create as many as you like. My little setup has Fedora, Ubuntu, and openSUSE. To create a new IPv6 network, open Edit > Connection Details > Virtual Networks in the main Virtual Machine Manager window. Click on the button with the green cross on the bottom left to create a new network (Figure 1).
|
||||
|
||||
![](https://www.linux.com/sites/lcom/files/styles/rendered_file/public/kvm-fig-1_0.png?itok=ruqjPXxd)
|
||||
Figure 1: Create a network.
|
||||
|
||||
Give your new network a name, then click the Forward button. You may opt to not create an IPv4 network if you wish. When you create a new IPv4 network the Virtual Machine Manager will not let you create a duplicate network, or one with an invalid address. On my host Ubuntu system a valid address is highlighted in green, and an invalid address is highlighted in a tasteful rosy hue. On my openSUSE machine there are no colored highlights. Enable DHCP or not, and create a static route or not, then move on to the next window.
|
||||
|
||||
Check "Enable IPv6 network address space definition" and enter your private address range. You may use any IPv6 address class you wish, being careful, of course, to not allow your experiments to leak out of your network. We shall use the nice IPv6 unique local addresses (ULA), and use the online address generator at [Simple DNS Plus][4] to create our network address. Copy the "Combined/CID" address into the Network field (Figure 2).
|
||||
|
||||
|
||||
![network address][6]
|
||||
|
||||
Figure 2: Copy the "Combined/CID" address into the Network field.
|
||||
|
||||
[Used with permission][7]
|
||||
|
||||
Virtual Machine Manager thinks my address is not valid, as evidenced by the rose highlight. Can it be right? Let us use ipv6calc to check:
|
||||
```
|
||||
$ ipv6calc -qi fd7d:844d:3e17:f3ae::/64
|
||||
Address type: unicast, unique-local-unicast, iid, iid-local
|
||||
Registry for address: reserved(RFC4193#3.1)
|
||||
Address type has SLA: f3ae
|
||||
Interface identifier: 0000:0000:0000:0000
|
||||
Interface identifier is probably manual set
|
||||
|
||||
```
|
||||
|
||||
ipv6calc thinks it's fine. Just for fun, change one of the numbers to something invalid, like the letter g, and try it again. (Asking "What if...?" and trial and error is the awesomest way to learn.)
|
||||
|
||||
Let us carry on and enable DHCPv6 (Figure 3). You can accept the default values, or set your own.
|
||||
|
||||
![](https://www.linux.com/sites/lcom/files/styles/floated_images/public/kvm-fig-3.png?itok=F-oAAtN9)
|
||||
|
||||
We shall skip creating a default route definition and move on to the next screen, where we shall enable "Isolated Virtual Network" and "Enable IPv6 internal routing/networking".
|
||||
|
||||
### VM Network Selection
|
||||
|
||||
Now you can configure your virtual machines to use your new network. Open your VMs, and then click the "i" button at the top left to open its "Show virtual hardware details" screen. In the "Add Hardware" column click on the NIC button to open the network selector, and select your nice new IPv6 network. Click Apply, and then reboot. (Or use your favorite method for restarting networking, or renewing your DHCP lease.)
|
||||
|
||||
### Testing
|
||||
|
||||
What does ifconfig tell us?
|
||||
```
|
||||
$ ifconfig
|
||||
ens3: flags=4163 UP,BROADCAST,RUNNING,MULTICAST mtu 1500
|
||||
inet 192.168.30.207 netmask 255.255.255.0
|
||||
broadcast 192.168.30.255
|
||||
inet6 fd7d:844d:3e17:f3ae::6314
|
||||
prefixlen 128 scopeid 0x0
|
||||
inet6 fe80::4821:5ecb:e4b4:d5fc
|
||||
prefixlen 64 scopeid 0x20
|
||||
|
||||
```
|
||||
|
||||
And there is our nice new ULA, fd7d:844d:3e17:f3ae::6314, and the auto-generated link-local address that is always present. Let's have some ping fun, pinging another VM on the network:
|
||||
```
|
||||
vm1 ~$ ping6 -c2 fd7d:844d:3e17:f3ae::2c9f
|
||||
PING fd7d:844d:3e17:f3ae::2c9f(fd7d:844d:3e17:f3ae::2c9f) 56 data bytes
|
||||
64 bytes from fd7d:844d:3e17:f3ae::2c9f: icmp_seq=1 ttl=64 time=0.635 ms
|
||||
64 bytes from fd7d:844d:3e17:f3ae::2c9f: icmp_seq=2 ttl=64 time=0.365 ms
|
||||
|
||||
vm2 ~$ ping6 -c2 fd7d:844d:3e17:f3ae:a:b:c:6314
|
||||
PING fd7d:844d:3e17:f3ae:a:b:c:6314(fd7d:844d:3e17:f3ae:a:b:c:6314) 56 data bytes
|
||||
64 bytes from fd7d:844d:3e17:f3ae:a:b:c:6314: icmp_seq=1 ttl=64 time=0.744 ms
|
||||
64 bytes from fd7d:844d:3e17:f3ae:a:b:c:6314: icmp_seq=2 ttl=64 time=0.364 ms
|
||||
|
||||
```
|
||||
|
||||
When you're struggling to understand subnetting, this gives you a fast, easy way to try different addresses and see whether they work. You can assign multiple IP addresses to a single interface and then ping them to see what happens. In a ULA, the interface, or host, portion of the IP address is the last four quads, so you can do anything to those and still be in the same subnet, which in this example is f3ae. This example changes only the interface ID on one of my VMs, to show how you really can do whatever you want with those four quads:
|
||||
```
|
||||
vm1 ~$ sudo /sbin/ip -6 addr add fd7d:844d:3e17:f3ae:a:b:c:6314 dev ens3
|
||||
|
||||
vm2 ~$ ping6 -c2 fd7d:844d:3e17:f3ae:a:b:c:6314
|
||||
PING fd7d:844d:3e17:f3ae:a:b:c:6314(fd7d:844d:3e17:f3ae:a:b:c:6314) 56 data bytes
|
||||
64 bytes from fd7d:844d:3e17:f3ae:a:b:c:6314: icmp_seq=1 ttl=64 time=0.744 ms
|
||||
64 bytes from fd7d:844d:3e17:f3ae:a:b:c:6314: icmp_seq=2 ttl=64 time=0.364 ms
|
||||
|
||||
```
|
||||
|
||||
Now try it with a different subnet, which in this example is f4ae instead of f3ae:
|
||||
```
|
||||
$ ping6 -c2 fd7d:844d:3e17:f4ae:a:b:c:6314
|
||||
PING fd7d:844d:3e17:f4ae:a:b:c:6314(fd7d:844d:3e17:f4ae:a:b:c:6314) 56 data bytes
|
||||
From fd7d:844d:3e17:f3ae::1 icmp_seq=1 Destination unreachable: No route
|
||||
From fd7d:844d:3e17:f3ae::1 icmp_seq=2 Destination unreachable: No route
|
||||
|
||||
```
|
||||
|
||||
This is also a great time to practice routing, which we will do in a future installment along with setting up auto-addressing without DHCP.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.linux.com/learn/intro-to-linux/2017/11/testing-ipv6-networking-kvm-part-2
|
||||
|
||||
作者:[CARLA SCHRODER][a]
|
||||
选题:[lujun9972](https://github.com/lujun9972)
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://www.linux.com/users/cschroder
|
||||
[1]:https://www.linux.com/learn/intro-to-linux/2017/11/testing-ipv6-networking-kvm-part-1
|
||||
[2]:https://www.linux.com/learn/intro-to-linux/2017/5/creating-virtual-machines-kvm-part-1
|
||||
[3]:https://www.linux.com/learn/intro-to-linux/2017/5/creating-virtual-machines-kvm-part-2-networking
|
||||
[4]:http://simpledns.com/private-ipv6.aspx
|
||||
[5]:/files/images/kvm-fig-2png
|
||||
[6]:https://www.linux.com/sites/lcom/files/styles/floated_images/public/kvm-fig-2.png?itok=gncdPGj- (network address)
|
||||
[7]:https://www.linux.com/licenses/category/used-permission
|
@ -1,91 +0,0 @@
|
||||
Translating by qhwdw
|
||||
How To Improve Application Startup Time In Linux
|
||||
======
|
||||
|
||||
![](https://www.ostechnix.com/wp-content/uploads/2018/05/Preload-720x340.png)
|
||||
Most Linux distributions are fast enough by default. However, we can still make them a little bit faster using some additional applications and methods. One such application we are about to see is **Preload**. It monitors the most frequently-used applications by the user and adds them to the memory, so that the applications will load little bit faster than before. Because, as you might already know, reading from the RAM is always faster than from the hard drive. Preload runs as a daemon on the background all the time and records the statistics about usage of files by more frequently-used programs. It then fetches those binaries and their dependencies into memory to improve the application loading time. In a nutshell, once preload is installed, you should be able to load the often-used applications much faster.
|
||||
|
||||
In this brief tutorial, we are going to see how to install and use Preload to improve an application startup time in Linux.
|
||||
|
||||
### Improve Application Startup Time In Linux Using Preload
|
||||
|
||||
Preload is available in [**AUR**][1]. So you can install it using AUR helper programs in any Arch-based systems such as Antergos, Manjaro Linux.
|
||||
|
||||
Using [**Pacaur**][2]:
|
||||
```
|
||||
$ pacaur -S preload
|
||||
|
||||
```
|
||||
|
||||
Using [**Packer**][3]:
|
||||
```
|
||||
$ packer -S preload
|
||||
|
||||
```
|
||||
|
||||
Using [**Trizen**][4]:
|
||||
```
|
||||
$ trizen -S preload
|
||||
|
||||
```
|
||||
|
||||
Using [**Yay**][5]:
|
||||
```
|
||||
$ yay -S preload
|
||||
|
||||
```
|
||||
|
||||
Using [**Yaourt**][6]:
|
||||
```
|
||||
$ yaourt -S preload
|
||||
|
||||
```
|
||||
|
||||
On Debian, Ubuntu, Linux Mint, Preload is available in the default repositories. So you can install it using APT package manager like below.
|
||||
```
|
||||
$ sudo apt-get install preload
|
||||
|
||||
```
|
||||
|
||||
Once Preload installed, reboot your system. From now on, Preload monitors the frequently-used applications and adds their binaries and libraries into the Memory for faster startup time. For example, if you often use Firefox, Chrome or LibreOffice, Preload will then add those binaries and libraries into RAM, so those applications will start faster. Good thing is it doesn’t need any configuration. It will just work out of the box. If you, however, wants to tweak the configuration, you can do it by editing the default configuration file **/etc/preload.conf**.
|
||||
|
||||
### Preload isn’t for everyone!
|
||||
|
||||
Here are some drawbacks of Preload and why it is not that effective for everyone, discussed in this [**thread**][7].
|
||||
|
||||
1. I do have a decent specification system with 8GB RAM. So my system is generally fast. Also, I will open heavy memory-consuming applications, such as Firefox, Chrome, VirtualBox, Gimp etc., one or two times per day. They remain open all the time, hence their binaries and libraries are preloaded into memory and occupying the RAM all day. I rarely close and open those applications, so the RAM usage is simply waste.
|
||||
2. If you’re using modern systems with SSD, Preload is obviously useless. Because SSDs access time is much faster than normal hard drives, so using Preload is pointless.
|
||||
3. Preload significantly affects the boot time. Because the more applications are preloaded into RAM, the longer it takes to get your system up and running.
|
||||
|
||||
|
||||
|
||||
You will only the see the real difference only if you’re reloading applications a LOT of time per day. So Preload will be ideal for the developers and testers who open and close the applications several times everyday.
|
||||
|
||||
For more details about what exactly preload is and how it works, read the complete [**Preload thesis**][8] paper submitted by the author.
|
||||
|
||||
And, that’s all for now. Hope this was useful. More good stuffs to come. Stay tuned!
|
||||
|
||||
Cheers!
|
||||
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.ostechnix.com/how-to-improve-application-startup-time-in-linux/
|
||||
|
||||
作者:[SK][a]
|
||||
选题:[lujun9972](https://github.com/lujun9972)
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://www.ostechnix.com/author/sk/
|
||||
[1]:https://aur.archlinux.org/packages/preload/
|
||||
[2]:https://www.ostechnix.com/install-pacaur-arch-linux/
|
||||
[3]:https://www.ostechnix.com/install-packer-arch-linux-2/
|
||||
[4]:https://www.ostechnix.com/trizen-lightweight-aur-package-manager-arch-based-systems/
|
||||
[5]:https://www.ostechnix.com/yay-found-yet-another-reliable-aur-helper/
|
||||
[6]:https://www.ostechnix.com/install-yaourt-arch-linux/
|
||||
[7]:https://askubuntu.com/questions/110335/drawbacks-of-using-preload-why-isnt-it-included-by-default
|
||||
[8]:https://cs.uwaterloo.ca/~brecht/courses/702/Possible-Readings/prefetching-to-memory/preload-thesis.pdf
|
@ -1,309 +0,0 @@
|
||||
How To Check Laptop Battery Status In Terminal In Linux
|
||||
======
|
||||
![](https://www.ostechnix.com/wp-content/uploads/2016/12/Check-Laptop-Battery-Status-In-Terminal-In-Linux-720x340.png)
|
||||
Finding your Laptop battery status in GUI mode is easy. You could easily tell the battery level by hovering the mouse pointer over the battery indicator icon in the task bar. But, how about from the command line? Not everyone know this. The other day a friend of mine asked how to check his Laptop battery level from Terminal in his Ubuntu desktop – hence this post. Here I have included three simple methods which will help you to check Laptop battery status in Terminal in any Linux distribution.
|
||||
|
||||
### Check Laptop Battery Status In Terminal In Linux
|
||||
|
||||
We can find the Laptop battery status from command line in three methods.
|
||||
|
||||
##### Method 1 – Using “Upower” command
|
||||
|
||||
The **Upower** command comes preinstalled with most Linux distributions. To display the battery status using Upower, open up the Terminal and run:
|
||||
```
|
||||
$ upower -i /org/freedesktop/UPower/devices/battery_BAT0
|
||||
|
||||
```
|
||||
|
||||
**Sample output:**
|
||||
```
|
||||
native-path: BAT0
|
||||
vendor: Samsung SDI
|
||||
model: DELL 7XFJJA2
|
||||
serial: 4448
|
||||
power supply: yes
|
||||
updated: Sat 12 May 2018 06:48:48 PM IST (41 seconds ago)
|
||||
has history: yes
|
||||
has statistics: yes
|
||||
battery
|
||||
present: yes
|
||||
rechargeable: yes
|
||||
state: charging
|
||||
warning-level: none
|
||||
energy: 43.3011 Wh
|
||||
energy-empty: 0 Wh
|
||||
energy-full: 44.5443 Wh
|
||||
energy-full-design: 48.84 Wh
|
||||
energy-rate: 9.8679 W
|
||||
voltage: 12.548 V
|
||||
time to full: 7.6 minutes
|
||||
percentage: 97%
|
||||
capacity: 91.2045%
|
||||
technology: lithium-ion
|
||||
icon-name: 'battery-full-charging-symbolic'
|
||||
History (charge):
|
||||
1526131128 97.000 charging
|
||||
History (rate):
|
||||
1526131128 9.868 charging
|
||||
|
||||
```
|
||||
|
||||
As you see above, my battery is in charging mode now and the battery level is 97%.
|
||||
|
||||
If the above command doesn’t work for any reason, try the following command instead:
|
||||
```
|
||||
$ upower -i `upower -e | grep 'BAT'`
|
||||
|
||||
```
|
||||
|
||||
**Sample output:**
|
||||
```
|
||||
native-path: BAT0
|
||||
vendor: Samsung SDI
|
||||
model: DELL 7XFJJA2
|
||||
serial: 4448
|
||||
power supply: yes
|
||||
updated: Sat 12 May 2018 06:50:49 PM IST (22 seconds ago)
|
||||
has history: yes
|
||||
has statistics: yes
|
||||
battery
|
||||
present: yes
|
||||
rechargeable: yes
|
||||
state: charging
|
||||
warning-level: none
|
||||
energy: 43.6119 Wh
|
||||
energy-empty: 0 Wh
|
||||
energy-full: 44.5443 Wh
|
||||
energy-full-design: 48.84 Wh
|
||||
energy-rate: 8.88 W
|
||||
voltage: 12.552 V
|
||||
time to full: 6.3 minutes
|
||||
percentage: 97%
|
||||
capacity: 91.2045%
|
||||
technology: lithium-ion
|
||||
icon-name: 'battery-full-charging-symbolic'
|
||||
History (rate):
|
||||
1526131249 8.880 charging
|
||||
|
||||
```
|
||||
|
||||
Upower not just display the battery status, but also the complete details of the installed battery such as model, vendor name, serial no, state, voltage etc.
|
||||
|
||||
However, you can only display the status of the battery by with combination of upower and [**grep**][1] commands as shown below.
|
||||
```
|
||||
$ upower -i $(upower -e | grep BAT) | grep --color=never -E "state|to\ full|to\ empty|percentage"
|
||||
|
||||
```
|
||||
|
||||
**Sample output:**
|
||||
```
|
||||
state: fully-charged
|
||||
percentage: 100%
|
||||
|
||||
```
|
||||
|
||||
![][3]
|
||||
|
||||
As you see in the above output, my Laptop battery has been fully charged.
|
||||
|
||||
For more details, refer man pages.
|
||||
```
|
||||
$ man upower
|
||||
|
||||
```
|
||||
|
||||
##### Method 2 – Using “acpi” command
|
||||
|
||||
The **acpi** command shows battery status and other ACPI information in your Linux distribution.
|
||||
|
||||
You might need to install **acpi** command in some Linux distributions.
|
||||
|
||||
To install acpi on Debian, Ubuntu and its derivatives:
|
||||
```
|
||||
$ sudo apt-get install acpi
|
||||
|
||||
```
|
||||
|
||||
On RHEL, CentOS, Fedora:
|
||||
```
|
||||
$ sudo yum install acpi
|
||||
|
||||
```
|
||||
|
||||
Or,
|
||||
```
|
||||
$ sudo dnf install acpi
|
||||
|
||||
```
|
||||
|
||||
On Arch Linux and its derivatives:
|
||||
```
|
||||
$ sudo pacman -S acpi
|
||||
|
||||
```
|
||||
|
||||
Once acpi installed, run the following command:
|
||||
```
|
||||
$ acpi -V
|
||||
|
||||
```
|
||||
|
||||
**Note:** Here, “V” is capital letter.
|
||||
|
||||
**Sample output:**
|
||||
```
|
||||
Battery 0: Charging, 99%, 00:02:09 until charged
|
||||
Battery 0: design capacity 4400 mAh, last full capacity 4013 mAh = 91%
|
||||
Battery 1: Discharging, 0%, rate information unavailable
|
||||
Adapter 0: on-line
|
||||
Thermal 0: ok, 77.5 degrees C
|
||||
Thermal 0: trip point 0 switches to mode critical at temperature 84.0 degrees C
|
||||
Cooling 0: Processor 0 of 3
|
||||
Cooling 1: Processor 0 of 3
|
||||
Cooling 2: LCD 0 of 15
|
||||
Cooling 3: Processor 0 of 3
|
||||
Cooling 4: Processor 0 of 3
|
||||
Cooling 5: intel_powerclamp no state information available
|
||||
Cooling 6: x86_pkg_temp no state information available
|
||||
|
||||
```
|
||||
|
||||
Let us only check the state of the charge of battery. To do so, run:
|
||||
```
|
||||
$ acpi
|
||||
|
||||
```
|
||||
|
||||
**Sample output:**
|
||||
```
|
||||
Battery 0: Charging, 99%, 00:01:41 until charged
|
||||
Battery 1: Discharging, 0%, rate information unavailable
|
||||
|
||||
```
|
||||
|
||||
Let us check the battery temperature:
|
||||
```
|
||||
$ acpi -t
|
||||
|
||||
```
|
||||
|
||||
**Sample output:**
|
||||
```
|
||||
Thermal 0: ok, 63.5 degrees C
|
||||
|
||||
```
|
||||
|
||||
Let us view the above output in Fahrenheit:
|
||||
```
|
||||
$ acpi -t -f
|
||||
|
||||
```
|
||||
|
||||
**Sample output:**
|
||||
```
|
||||
Thermal 0: ok, 144.5 degrees F
|
||||
|
||||
```
|
||||
|
||||
Want to know whether the AC power is connected or not? Run:
|
||||
```
|
||||
$ acpi -a
|
||||
|
||||
```
|
||||
|
||||
**Sample output:**
|
||||
```
|
||||
Adapter 0: on-line
|
||||
|
||||
```
|
||||
|
||||
If the AC power is not available, you would the see the following instead:
|
||||
```
|
||||
Adapter 0: off-line
|
||||
|
||||
```
|
||||
|
||||
For more details, check the man pages.
|
||||
```
|
||||
$ man acpi
|
||||
|
||||
```
|
||||
|
||||
##### Method 3: Using “Batstat” Program
|
||||
|
||||
The **batstat** is a small ncurses-based CLI utility to display your Laptop battery status in Unix-like systems. It will display the following details:
|
||||
|
||||
* Current battery level
|
||||
* Current Energy
|
||||
* Full charge energy
|
||||
* Time elapsed from the start of the program, without tracking the sleep time of the machine.
|
||||
* Battery level history
|
||||
|
||||
|
||||
|
||||
Installing batstat is a piece of cake. Git clone the latest version using command:
|
||||
```
|
||||
$ git clone https://github.com/Juve45/batstat.git
|
||||
|
||||
```
|
||||
|
||||
The above command will pull the latest batstat version and save it’s contents in a folder named “batstat”.
|
||||
|
||||
CD to batstat/bin/ directory:
|
||||
```
|
||||
$ cd batstat/bin/
|
||||
|
||||
```
|
||||
|
||||
Copy “batstat” binary file to your PATH, for example /usr/local/bin/.
|
||||
```
|
||||
$ sudo cp batstat /usr/local/bin/
|
||||
|
||||
```
|
||||
|
||||
Make it executable using command:
|
||||
```
|
||||
$ sudo chmod +x /usr/local/bin/batstat
|
||||
|
||||
```
|
||||
|
||||
Finally, run the following command to view your battery status.
|
||||
```
|
||||
$ batstat
|
||||
|
||||
```
|
||||
|
||||
Sample output:
|
||||
|
||||
![][4]
|
||||
|
||||
As you see in the above screenshot, my battery is in charging mode.
|
||||
|
||||
This utility has some limitations though. As of writing this guide, batstat will support only one battery. And, it gathers information only from this folder – **“/sys/class/power_supply/”**. If your machine contains the battery information on a different folder, this program will not work.
|
||||
|
||||
For more details, check batstat github page.
|
||||
|
||||
And, that’s all for today folks. There might be many commands and programs out there to check the laptop battery status in Terminal in Linux. As far as I know, the above given methods have worked just fine as expected. If you know some other commands to find out the battery status, let me know in the comment section below. I will update commands in the article if they works.
|
||||
|
||||
And, that’s all for now. More good stuffs to come. Stay tuned!
|
||||
|
||||
Cheers!
|
||||
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.ostechnix.com/how-to-check-laptop-battery-status-in-terminal-in-linux/
|
||||
|
||||
作者:[SK][a]
|
||||
选题:[lujun9972](https://github.com/lujun9972)
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://www.ostechnix.com/author/sk/
|
||||
[1]:https://www.ostechnix.com/the-grep-command-tutorial-with-examples-for-beginners/
|
||||
[2]:data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
|
||||
[3]:http://www.ostechnix.com/wp-content/uploads/2016/12/sk@sk_006-1.png
|
||||
[4]:http://www.ostechnix.com/wp-content/uploads/2016/12/batstat-1.png
|
@ -1,103 +0,0 @@
|
||||
#[探秘“栈”之旅(I)][1]
|
||||
|
||||
早些时候,我们讲解了 [“剖析内存中的程序之秘”][2],我们欣赏了在一台电脑中是如何运行我们的程序的。今天,我们去探索栈的调用,它在大多数编程语言和虚拟机中都默默地存在。在此过程中,我们将接触到一些平时很难见到的东西,像闭包(closures)、递归、以及缓冲溢出等等。但是,我们首先要作的事情是,描绘出栈是如何运作的。
|
||||
|
||||
栈非常重要,因为它持有着在一个程序中运行的函数,而函数又是一个软件的重要组成部分。事实上,程序的内部操作都是非常简单的。它大部分是由函数向栈中推入数据或者从栈中弹出数据的相互调用组成的,虽然为数据分配内存是在堆上,但是,在跨函数的调用中数据必须要保存下来,不论是低级(low-leverl)的 C 软件还是像 JavaScript 和 C# 这样的基于虚拟机的语言,它们都是这样的。而对这些行为的深刻理解,对排错、性能调优以及大概了解究竟发生了什么是非常重要的。
|
||||
|
||||
当一个函数被调用时,将会创建一个栈帧(stack frame)去支持函数的运行。这个栈帧包含函数的本地变量和调用者传递给它的参数。这个栈帧也包含了允许被调用的函数安全返回给调用者的内部事务信息。栈帧的精确内容和结构因处理器架构和函数调用规则而不同。在本文中我们以 Intel x86 架构和使用 C 风格的函数调用(cdecl)的栈为例。下图是一个处于栈顶部的一个单个栈帧:
|
||||
|
||||
![](https://manybutfinite.com/img/stack/stackIntro.png)
|
||||
|
||||
在图上的场景中,有三个 CPU 寄存器进入栈。栈指针 `esp`(译者注:扩展栈指针寄存器) 指向到栈的顶部。栈的顶部总是被最后一个推入到栈且还没有弹出的东西所占据,就像现实世界中堆在一起的一叠板子或者面值 $100 的钞票。
|
||||
|
||||
保存在 `esp` 中的地址始终在变化着,因为栈中的东西不停被推入和弹出,而它总是指向栈中的最后一个推入的东西。许多 CPU 指令的一个副作用就是自动更新 `esp`,离开寄存器而使用栈是行不通的。
|
||||
|
||||
在 Intel 的架构中,绝大多数情况下,栈的增长是向着低位内存地址的方向。因此,这个“顶部” 在包含数据(在这种情况下,包含的数据是 `local_buffer`)的栈中是处于低位的内存地址。注意,关于从 `esp` 到 `local_buffer` 的箭头,这里并没有模糊的地方。这个箭头代表着事务:它专门指向到由 `local_buffer` 所拥有的第一个字节,因为,那是一个保存在 `esp` 中的精确地址。
|
||||
|
||||
第二个寄存器跟踪的栈是 `ebp`(译者注:扩展基址指针寄存器),它包含一个基指针或者称为帧指针。它指向到一个当前运行的函数的栈帧内的固定的位置,并且它为参数和本地变量的访问提供一个稳定的参考点(基址)。仅当开始或者结束调用一个函数时,`ebp` 的内容才会发生变化。因此,我们可以很容易地处理每个在栈中的从 `ebp` 开始偏移后的一个东西。如下图所示。
|
||||
|
||||
不像 `esp`, `ebp` 大多数情况下是在程序代码中通过花费很少的 CPU 来进行维护的。有时候,完成抛弃 `ebp` 有一些性能优势,可以通过 [编译标志][3] 来做到这一点。Linux 内核中有一个实现的示例。
|
||||
|
||||
最后,`eax`(译者注:扩展的 32 位通用数据寄存器)寄存器是被调用规则所使用的寄存器,对于大多数 C 数据类型来说,它的作用是转换一个返回值给调用者。
|
||||
|
||||
现在,我们来看一下在我们的栈帧中的数据。下图清晰地按字节展示了字节的内容,就像你在一个调试器中所看到的内容一样,内存是从左到右、从底部到顶部增长的,如下图所示:
|
||||
|
||||
![](https://manybutfinite.com/img/stack/frameContents.png)
|
||||
|
||||
本地变量 `local_buffer` 是一个字节数组,它包含一个空终止(null-terminated)的 ascii 字符串,这是一个 C 程序中的基本元素。这个字符串可以从任意位置读取,例如,从键盘输入或者来自一个文件,它只有 7 个字节的长度。因为,`local_buffer` 只能保存 8 字节,在它的左侧保留了 1 个未使用的字节。这个字节的内容是未知的,因为栈的推入和弹出是极其活跃的,除了你写入的之外,你从不知道内存中保存了什么。因为 C 编译器并不为栈帧初始化内存,所以它的内容是未知的并且是随机的 - 除非是你自己写入。这使得一些人对此很困惑。
|
||||
|
||||
再往上走,`local1` 是一个 4 字节的整数,并且你可以看到每个字节的内容。它似乎是一个很大的数字,所有的零都在 8 后面,在这里可能会让你误入歧途。
|
||||
|
||||
Intel 处理器是按从小到大的机制来处理的,这表示在内存中的数字也是首先从小的位置开始的。因此,在一个多字节数字中,最小的标志字节在内存中处于低端地址。因为一般情况下是从左边开始显示的,这背离了我们一般意义上对数字的认识。我们讨论的这种从小到大的机制,使我想起《Gulliver 游记》:就像 Lilliput 吃鸡蛋是从小头开始的一样,Intel 处理器处理它们的数字也是从字节的小端开始的。
|
||||
|
||||
因此,`local1` 事实上只保存了一个数字 8,就像一个章鱼的腿。然而,`param1` 在第二个字节的位置有一个值 2,因此,它的数学上的值是 2 * 256 = 512(我们与 256 相乘是因为,每个位置值的范围都是从 0 到 255)。同时,`param2` 承载的数量是 1 * 256 * 256 = 65536。
|
||||
|
||||
这个栈帧的内部数据是由两个重要的部分组成:前一个栈帧的地址和函数的出口(返回地址)上运行的指令的地址。它们一起确保了函数能够正常返回,从而使程序可以继续正常运行。
|
||||
|
||||
现在,我们来看一下栈帧是如何产生的,以及去建立一个它们如何共同工作的内部蓝图。在刚开始的时候,栈的增长是非常令人困惑的,因为它发生的一切都不是你所期望的东西。例如,在栈上从 `esp` 减去 8,去分配一个 8 字节,而减法是以一种奇怪的方式去开始的。
|
||||
|
||||
我们来看一个简单的 C 程序:
|
||||
|
||||
```
|
||||
Simple Add Program - add.c
|
||||
|
||||
int add(int a, int b)
|
||||
{
|
||||
int result = a + b;
|
||||
return result;
|
||||
}
|
||||
|
||||
int main(int argc)
|
||||
{
|
||||
int answer;
|
||||
answer = add(40, 2);
|
||||
}
|
||||
```
|
||||
|
||||
假设我们在 Linux 中不使用命令行参数去运行它。当你运行一个 C 程序时,去真实运行的第一个代码是 C 运行时库,由它来调用我们的 `main` 函数。下图展示了程序运行时每一步都发生了什么。每个图链接的 GDB 输出展示了内存的状态和寄存器。你也可以看到所使用的 [GDB 命令][4],以及整个 [GDB 输出][5]。如下:
|
||||
|
||||
![](https://manybutfinite.com/img/stack/mainProlog.png)
|
||||
|
||||
第 2 步和第 3 步,以及下面的第 4 步,都只是函数的开端,几乎所有的函数都是这样的:`ebp` 的当前值保存着栈的顶部,然后,将 `esp` 的内容拷贝到 `ebp`,维护一个新帧。`main` 的开端和任何一个其它函数都是一样,但是,不同之处在于,当程序启动时 `ebp` 被清零。
|
||||
|
||||
如果你去检查栈下面的整形变量(argc),你将找到更多的数据,包括指向到程序名和命令行参数(传统的 C 参数数组)、Unix 环境变量以及它们真实的内容的指针。但是,在这里这些并不是重点,因此,继续向前调用 add():
|
||||
|
||||
![](https://manybutfinite.com/img/stack/callAdd.png)
|
||||
|
||||
在 `main` 从 `esp` 减去 12 之后得到它所需的栈空间,它为 a 和 b 设置值。在内存中值展示为十六进制,并且是从小到大的格式。与你从调试器中看到的一样。一旦设置了参数值,`main` 将调用 `add` ,并且它开始运行:
|
||||
|
||||
![](https://manybutfinite.com/img/stack/addProlog.png)
|
||||
|
||||
现在,有一点小激动!我们进入了另一个开端,在这时你可以明确看到栈帧是如何从 `ebp` 的一个链表开始进入到栈的。这就是在高级语言中调试器和异常对象如何对它们的栈进行跟踪的。当一个新帧产生时,你也可以看到更多这种从 `ebp` 到 `esp` 的典型的捕获。我们再次从 `esp` 中做减法得到更多的栈空间。
|
||||
|
||||
当 `ebp` 寄存器的值拷贝到内存时,这里也有一个稍微有些怪异的地方。在这里发生的奇怪事情是,寄存器并没有真的按字节顺序拷贝:因为对于内存,没有像寄存器那样的“增长的地址”。因此,通过调试器的规则以最自然的格式给人展示了寄存器的值:从最重要的到最不重要的数字。因此,这个在从小到大的机制中拷贝的结果,与内存中常用的从左到右的标记法正好相反。我想用图去展示你将会看到的东西,因此有了下面的图。
|
||||
|
||||
在比较难懂的部分,我们增加了注释:
|
||||
|
||||
![](https://manybutfinite.com/img/stack/doAdd.png)
|
||||
|
||||
这是一个临时寄存器,用于帮你做加法,因此没有什么警报或者惊喜。对于加法这样的作业,栈的动作正好相反,我们留到下次再讲。
|
||||
|
||||
对于任何读到这篇文章的人都应该有一个小礼物,因此,我做了一个大的图表展示了 [组合到一起的所有步骤][6]。
|
||||
|
||||
一旦把它们全部布置好了,看上起似乎很乏味。这些小方框给我们提供了很多帮助。事实上,在计算机科学中,这些小方框是主要的展示工具。我希望这些图片和寄存器的移动能够提供一种更直观的构想图,将栈的增长和内存的内容整合到一起。从软件的底层运作来看,我们的软件与一个简单的图灵机器差不多。
|
||||
|
||||
这就是我们栈探秘的第一部分,再讲一些内容之后,我们将看到构建在这个基础上的高级编程的概念。下周见!
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via:https://manybutfinite.com/post/journey-to-the-stack/
|
||||
|
||||
作者:[Gustavo Duarte][a]
|
||||
译者:[qhwdw](https://github.com/qhwdw)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://duartes.org/gustavo/blog/about/
|
||||
[1]:https://manybutfinite.com/post/journey-to-the-stack/
|
||||
[2]:https://manybutfinite.com/post/anatomy-of-a-program-in-memory
|
||||
[3]:http://stackoverflow.com/questions/14666665/trying-to-understand-gcc-option-fomit-frame-pointer
|
||||
[4]:https://github.com/gduarte/blog/blob/master/code/x86-stack/add-gdb-commands.txt
|
||||
[5]:https://github.com/gduarte/blog/blob/master/code/x86-stack/add-gdb-output.txt
|
||||
[6]:https://manybutfinite.com/img/stack/callSequence.png
|
@ -0,0 +1,119 @@
|
||||
在 KVM 中测试 IPv6 网络:第 2 部分
|
||||
======
|
||||
|
||||
![](https://www.linux.com/sites/lcom/files/styles/rendered_file/public/banner_4.png?itok=yZBHylwd)
|
||||
我们又见面了,在上一篇 [在 KVM 中测试 IPv6 网络:第 1 部分][1] 中,我们学习了有关 IPv6 私有地址的内容。今天,我们将使用 KVM 创建一个网络,去测试上一星期学习的 IPv6 的内容。
|
||||
|
||||
如果你想重新温习如何使用 KVM,可以查看 [在 KVM 中创建虚拟机:第 1 部分][2] 和 [在 KVM 中创建虚拟机:第 2 部分— 网络][3]。
|
||||
|
||||
### 在 KVM 中创建网络
|
||||
|
||||
在 KVM 中你至少需要两个虚拟机。当然了,如果你愿意,也可以创建更多的虚拟机。在我的系统中有 Fedora、Ubuntu、以及 openSUSE。去创建一个新的 IPv6 网络,在主虚拟机管理窗口中打开 Edit > Connection Details > Virtual Networks。点击左下角的绿色十字按钮去创建一个新的网络(图 1)。
|
||||
|
||||
![](https://www.linux.com/sites/lcom/files/styles/rendered_file/public/kvm-fig-1_0.png?itok=ruqjPXxd)
|
||||
图 1:创建一个网络
|
||||
|
||||
给新网络输入一个名字,然后,点击 Forward 按钮。如果你愿意,也可以不创建 IPv4 网络。当你创建一个新的 IPv4 网络时,虚拟机管理器将不让你创建重复网络,或者是使用了一个无效地址。在我的宿主机 Ubuntu 系统上,有效的地址是以绿色高亮显示的,而无效地址是使用高亮的玫瑰红色调。在我的 openSUSE 机器上没有高亮颜色。启用或不启用 DHCP,以及创建或不创建一个静态路由,然后进入下一个窗口。
|
||||
|
||||
选中 "Enable IPv6 network address space definition",然后输入你的私有地址范围。你可以使用任何你希望的 IPv6 地址类,但是要注意,不能将你的实验网络泄漏到公网上去。我们将使用非常好用的 IPv6 唯一本地地址(ULA),并且使用在 [Simple DNS Plus][4] 上的在线地址生成器,去创建我们的网络地址。拷贝 "Combined/CID" 地址到网络框中(图 2)。
|
||||
|
||||
|
||||
![network address][6]
|
||||
|
||||
图 2:拷贝 "Combined/CID" 地址到网络框中
|
||||
|
||||
[使用已获授权][7]
|
||||
|
||||
虚拟机认为我的地址是无效的,因为,它显示了高亮的玫瑰红色。它做的对吗?我们使用 ipv6calc 去验证一下:
|
||||
```
|
||||
$ ipv6calc -qi fd7d:844d:3e17:f3ae::/64
|
||||
Address type: unicast, unique-local-unicast, iid, iid-local
|
||||
Registry for address: reserved(RFC4193#3.1)
|
||||
Address type has SLA: f3ae
|
||||
Interface identifier: 0000:0000:0000:0000
|
||||
Interface identifier is probably manual set
|
||||
|
||||
```
|
||||
|
||||
ipv6calc 认为没有问题。如果感兴趣,你可以改变其中一个数字为无效的东西,比如字母 g,然后再试一次。(问 “如果…?”,试验和错误是最好的学习方法)。
|
||||
|
||||
我们继续进行,启用 DHCPv6(图 3)。你可以接受缺省值,或者输入一个你自己的设置值。
|
||||
|
||||
![](https://www.linux.com/sites/lcom/files/styles/floated_images/public/kvm-fig-3.png?itok=F-oAAtN9)
|
||||
|
||||
我们将跳过缺省路由定义这一步,继续进入下一屏,在那里我们将启用 “Isolated Virtual Network" 和 "Enable IPv6 internal routing/networking"。
|
||||
|
||||
### 虚拟机网络选择
|
||||
|
||||
现在,你可以配置你的虚拟机去使用新的网络。打开你的虚拟机,然后点击顶部左侧的 "i" 按钮去打开 "Show virtual hardware details" 屏幕。在 "Add Hardware" 列点击 NIC 按钮去打开网络选择器,然后选择你喜欢的新的 IPv6 网络。点击 Apply,然后重新启动。(或者使用你喜欢的方法去重新启动网络,或者更新你的 DHCP 租期。)
|
||||
|
||||
### 测试
|
||||
|
||||
ifconfig 告诉我们它做了什么?
|
||||
```
|
||||
$ ifconfig
|
||||
ens3: flags=4163 UP,BROADCAST,RUNNING,MULTICAST mtu 1500
|
||||
inet 192.168.30.207 netmask 255.255.255.0
|
||||
broadcast 192.168.30.255
|
||||
inet6 fd7d:844d:3e17:f3ae::6314
|
||||
prefixlen 128 scopeid 0x0
|
||||
inet6 fe80::4821:5ecb:e4b4:d5fc
|
||||
prefixlen 64 scopeid 0x20
|
||||
|
||||
```
|
||||
|
||||
这是我们新的 ULA,fd7d:844d:3e17:f3ae::6314,它是自动生成的本地链路地址。如果你有兴趣,可以 ping 一下,ping 网络上的其它虚拟机:
|
||||
```
|
||||
vm1 ~$ ping6 -c2 fd7d:844d:3e17:f3ae::2c9f
|
||||
PING fd7d:844d:3e17:f3ae::2c9f(fd7d:844d:3e17:f3ae::2c9f) 56 data bytes
|
||||
64 bytes from fd7d:844d:3e17:f3ae::2c9f: icmp_seq=1 ttl=64 time=0.635 ms
|
||||
64 bytes from fd7d:844d:3e17:f3ae::2c9f: icmp_seq=2 ttl=64 time=0.365 ms
|
||||
|
||||
vm2 ~$ ping6 -c2 fd7d:844d:3e17:f3ae:a:b:c:6314
|
||||
PING fd7d:844d:3e17:f3ae:a:b:c:6314(fd7d:844d:3e17:f3ae:a:b:c:6314) 56 data bytes
|
||||
64 bytes from fd7d:844d:3e17:f3ae:a:b:c:6314: icmp_seq=1 ttl=64 time=0.744 ms
|
||||
64 bytes from fd7d:844d:3e17:f3ae:a:b:c:6314: icmp_seq=2 ttl=64 time=0.364 ms
|
||||
|
||||
```
|
||||
|
||||
当你努力去理解子网时,这是一个提供给你的快速、易用的尝试不同地址是否可以正常工作的方法。你可以给单个接口分配多个 IP 地址,然后 ping 它们去看一下会发生什么。在一个 ULA 中,接口,或者主机是 IP 地址的最后四部分,因此,你可以在那里做任何事情,只要它们在同一个子网中即可,在那个例子中是 f3ae。在我的其中一个虚拟机上,我只改变了这个示例的接口 ID,以展示使用这四个部分,你可以做任何你想做的事情:
|
||||
```
|
||||
vm1 ~$ sudo /sbin/ip -6 addr add fd7d:844d:3e17:f3ae:a:b:c:6314 dev ens3
|
||||
|
||||
vm2 ~$ ping6 -c2 fd7d:844d:3e17:f3ae:a:b:c:6314
|
||||
PING fd7d:844d:3e17:f3ae:a:b:c:6314(fd7d:844d:3e17:f3ae:a:b:c:6314) 56 data bytes
|
||||
64 bytes from fd7d:844d:3e17:f3ae:a:b:c:6314: icmp_seq=1 ttl=64 time=0.744 ms
|
||||
64 bytes from fd7d:844d:3e17:f3ae:a:b:c:6314: icmp_seq=2 ttl=64 time=0.364 ms
|
||||
|
||||
```
|
||||
|
||||
现在,尝试使用不同的子网,在下面的示例中使用了 f4ae 代替 f3ae:
|
||||
```
|
||||
$ ping6 -c2 fd7d:844d:3e17:f4ae:a:b:c:6314
|
||||
PING fd7d:844d:3e17:f4ae:a:b:c:6314(fd7d:844d:3e17:f4ae:a:b:c:6314) 56 data bytes
|
||||
From fd7d:844d:3e17:f3ae::1 icmp_seq=1 Destination unreachable: No route
|
||||
From fd7d:844d:3e17:f3ae::1 icmp_seq=2 Destination unreachable: No route
|
||||
|
||||
```
|
||||
|
||||
这也是练习路由的好机会,以后,我们将专门做一期,如何在不使用 DHCP 情况下实现自动寻址。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.linux.com/learn/intro-to-linux/2017/11/testing-ipv6-networking-kvm-part-2
|
||||
|
||||
作者:[CARLA SCHRODER][a]
|
||||
选题:[lujun9972](https://github.com/lujun9972)
|
||||
译者:[qhwdw](https://github.com/qhwdw)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://www.linux.com/users/cschroder
|
||||
[1]:https://www.linux.com/learn/intro-to-linux/2017/11/testing-ipv6-networking-kvm-part-1
|
||||
[2]:https://www.linux.com/learn/intro-to-linux/2017/5/creating-virtual-machines-kvm-part-1
|
||||
[3]:https://www.linux.com/learn/intro-to-linux/2017/5/creating-virtual-machines-kvm-part-2-networking
|
||||
[4]:http://simpledns.com/private-ipv6.aspx
|
||||
[5]:/files/images/kvm-fig-2png
|
||||
[6]:https://www.linux.com/sites/lcom/files/styles/floated_images/public/kvm-fig-2.png?itok=gncdPGj- "network address"
|
||||
[7]:https://www.linux.com/licenses/category/used-permission
|
@ -41,28 +41,17 @@
|
||||
|
||||
|
||||
|
||||
1 OK--you can put your hands down now.
|
||||
|
||||
2 Should this be capitalized? Is there a particular field, or how does it work? I'm not sure.
|
||||
|
||||
3 I have a degree in English literature and theology--this probably won't surprise regular readers of my articles.4
|
||||
|
||||
4 Not, I hope, because I spout too much theology,5 but because it's often full of long-winded, irrelevant humanities (U.S. English: "liberal arts") references.
|
||||
|
||||
5 Emacs. Every time.
|
||||
|
||||
6 Not even Emacs. And yes, I know that there are techniques to prove the correctness of some software. (I suspect that Emacs doesn't pass many of them…)
|
||||
|
||||
7 Hand up here: I'm employed by one of them, [Red Hat][3]. Go have a look--it's a fun place to work, and [we're usually hiring][4].
|
||||
|
||||
8 Assuming that they fully abide by the rules of the open source licence(s) they're using, that is.
|
||||
|
||||
9 Erstwhile "Lord Protector of England, Scotland, and Ireland"--that Cromwell.
|
||||
|
||||
10 Oh, and choose Emacs over Vi variants, obviously.
|
||||
|
||||
This article originally appeared on [Alice, Eve, and Bob - a security blog][5] and is republished with permission.
|
||||
|
||||
1 好的--你现在可以放下手了
|
||||
2 这应该大写吗?有特定的领域吗?后者他是如何工作的?我不确定。
|
||||
3 我有一个英国文学和神学的学位--这可能不会使我的文章的普通读者感到惊讶
|
||||
4 我希望不是,因为我说的太多了,但是它经常是冗余的,无关紧要的人文。
|
||||
5 每次编辑
|
||||
6 即使是编辑器。而且是的,我知道有技术能够去证明一些软件的正确性。(我怀疑编辑器不能使它们全部通过...)
|
||||
7 注意这里:我被他们其中之一雇佣,去查看一下--它是一个有趣的工作地方,E而且[我们通常招聘][4]
|
||||
8 假设他们完全遵守他们正在使用的开源软件的认证。
|
||||
9 昔日的“英格兰、苏格兰、爱尔兰的上帝守护者”--比克伦威尔
|
||||
10 很明显,选择Vi 编辑器
|
||||
这篇文章原载于[Alice, Eve, and Bob - a security blog] 而且已经 被授权重新出版。
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/17/11/commonwealth-open-source
|
||||
|
@ -0,0 +1,90 @@
|
||||
如何改善应用程序在 Linux 中的启动时间
|
||||
======
|
||||
|
||||
![](https://www.ostechnix.com/wp-content/uploads/2018/05/Preload-720x340.png)
|
||||
大多数 Linux 发行版在默认配置下已经足够快了。但是,我们仍然可以借助一些额外的应用程序和方法让它们启动更快一点。其中一个可用的这种应用程序就是 **Preload**。它监视用户使用频率比较高的应用程序,并将它们添加到内存中,这样就比一般的方式加载更快一点。因为,正如你所知道的,内存的读取速度远远快于硬盘。Preload 以守护进程的方式在后台中运行,并记录用户使用较为频繁的程序的文件使用相关的统计数据。然后,它将这些二进制文件及它们的依赖项加载进内存,以改善应用程序的加载时间。简而言之,一旦安装了 preload,你使用较为频繁的应用程序将可能加载的更快。
|
||||
|
||||
在这篇详细的教程中,我们将去了解如何安装和使用 Preload,以改善应用程序在 Linux 中的启动时间。
|
||||
|
||||
### 在 Linux 中使用 Preload 改善应用程序启动时间
|
||||
|
||||
Preload 可以在 [**AUR**][1] 上找到。因此,你可以使用 AUR 助理程序在任何基于 Arch 的系统上去安装它,比如,Antergos、Manjaro Linux。
|
||||
|
||||
使用 [**Pacaur**][2]:
|
||||
```
|
||||
$ pacaur -S preload
|
||||
|
||||
```
|
||||
|
||||
使用 [**Packer**][3]:
|
||||
```
|
||||
$ packer -S preload
|
||||
|
||||
```
|
||||
|
||||
使用 [**Trizen**][4]:
|
||||
```
|
||||
$ trizen -S preload
|
||||
|
||||
```
|
||||
|
||||
使用 [**Yay**][5]:
|
||||
```
|
||||
$ yay -S preload
|
||||
|
||||
```
|
||||
|
||||
使用 [**Yaourt**][6]:
|
||||
```
|
||||
$ yaourt -S preload
|
||||
|
||||
```
|
||||
|
||||
在 Debian、Ubuntu、Linux Mint 上,Preload 可以在默认仓库中找到。因此,你可以像下面一样,使用 APT 包管理器去安装它。
|
||||
```
|
||||
$ sudo apt-get install preload
|
||||
|
||||
```
|
||||
|
||||
Preload 安装完成后,重新启动你的系统。从现在开始,Preload 将监视频繁使用的应用程序,并将它们的二进制文件和库添加到内存中,以使它的启动速度更快。比如,如果你经常使用 Firefox、Chrome 以及 LibreOffice,Preload 将添加这些二进制文件和库到内存中,因此,这些应用程序将启动的更快。而且更好的是,它不需要做任何配置。它是开箱即用的。但是,如果你想去对它进行微调,你可以通过编辑缺省的配置文件 **/etc/preload.conf** 来实现。
|
||||
|
||||
### Preload 并不一定适合每个人!
|
||||
|
||||
以下是 Preload 的一些缺点,它并不是对每个人都有帮助,在这个 [**跟贴**][7] 中有讨论到。
|
||||
|
||||
1. 我使用的是一个有 8GB 内存的现代系统。因此,我的系统总体上来说很快。我每天只打开狂吃内存的应用程序(比如,Firefox、Chrome、VirtualBox、Gimp、等等)一到两次,并且它们始终处于打开状态,因此,它们的二进制文件和库被预读到内存中,并始终整天在内存中。我一般很少去关闭和打开这些应用程序,因此,内存使用纯属浪费。
|
||||
2. 如果你使用的是带有 SSD 的现代系统,Preload 是绝对没用的。因为 SSD 的访问时间比起一般的硬盘来要快的多,因此,使用 Preload 是没有意义的。
|
||||
3. Preload 显著影响启动时间。因为大多数的应用程序都预读到内存中,长此以往,将让你的系统启动和运行的更快。
|
||||
|
||||
|
||||
|
||||
你只有在每天都在大量的重新加载应用程序时,才能看到真正的差别。因此,Preload 最适合开发人员和测试人员,他们每天都打开和关闭应用程序好多次。
|
||||
|
||||
关于 Preload 更多的信息和它是如何工作的,请阅读它的作者写的完整版的 [**Preload 论文**][8]。
|
||||
|
||||
教程到此为止,希望能帮到你。后面还有更精彩的内容,请继续关注!
|
||||
|
||||
再见!
|
||||
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.ostechnix.com/how-to-improve-application-startup-time-in-linux/
|
||||
|
||||
作者:[SK][a]
|
||||
选题:[lujun9972](https://github.com/lujun9972)
|
||||
译者:[qhwdw](https://github.com/qhwdw)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://www.ostechnix.com/author/sk/
|
||||
[1]:https://aur.archlinux.org/packages/preload/
|
||||
[2]:https://www.ostechnix.com/install-pacaur-arch-linux/
|
||||
[3]:https://www.ostechnix.com/install-packer-arch-linux-2/
|
||||
[4]:https://www.ostechnix.com/trizen-lightweight-aur-package-manager-arch-based-systems/
|
||||
[5]:https://www.ostechnix.com/yay-found-yet-another-reliable-aur-helper/
|
||||
[6]:https://www.ostechnix.com/install-yaourt-arch-linux/
|
||||
[7]:https://askubuntu.com/questions/110335/drawbacks-of-using-preload-why-isnt-it-included-by-default
|
||||
[8]:https://cs.uwaterloo.ca/~brecht/courses/702/Possible-Readings/prefetching-to-memory/preload-thesis.pdf
|
@ -0,0 +1,310 @@
|
||||
如何在 Linux 终端下检查笔记本电池状态
|
||||
======
|
||||
![](https://www.ostechnix.com/wp-content/uploads/2016/12/Check-Laptop-Battery-Status-In-Terminal-In-Linux-720x340.png)
|
||||
|
||||
在图形界面下查看你的笔记本电池状态是很容易的,只需将鼠标指向任务栏中的电池图标上,你便可以很容易地知道电池的电量。但如果我们想要从命令行中获得这些信息呢?并不是所有人都知道如何做到这点。前几天我的一个朋友询问我如何从他的 Ubuntu 系统里,在终端中查看他的笔记本电池的电量。这便是我写这篇文章的起因。在本文中我概括了三种简单的方法来让你在任何 Linux 发行版本中从终端查看笔记本电池的状态。
|
||||
|
||||
### 在终端下检查笔记本电池状态
|
||||
|
||||
我们可以使用下面的三种方法来从命令行中查找到笔记本电池状态。
|
||||
|
||||
##### 方法一 – 使用 “Upower” 命令
|
||||
|
||||
**Upower** 命令预装在大多数的 Linux 发行版本中。为了使用 `upower` 命令来展示电池的状态,打开终端并运行如下命令:
|
||||
```
|
||||
$ upower -i /org/freedesktop/UPower/devices/battery_BAT0
|
||||
|
||||
```
|
||||
|
||||
**示例输出:**
|
||||
```
|
||||
native-path: BAT0
|
||||
vendor: Samsung SDI
|
||||
model: DELL 7XFJJA2
|
||||
serial: 4448
|
||||
power supply: yes
|
||||
updated: Sat 12 May 2018 06:48:48 PM IST (41 seconds ago)
|
||||
has history: yes
|
||||
has statistics: yes
|
||||
battery
|
||||
present: yes
|
||||
rechargeable: yes
|
||||
state: charging
|
||||
warning-level: none
|
||||
energy: 43.3011 Wh
|
||||
energy-empty: 0 Wh
|
||||
energy-full: 44.5443 Wh
|
||||
energy-full-design: 48.84 Wh
|
||||
energy-rate: 9.8679 W
|
||||
voltage: 12.548 V
|
||||
time to full: 7.6 minutes
|
||||
percentage: 97%
|
||||
capacity: 91.2045%
|
||||
technology: lithium-ion
|
||||
icon-name: 'battery-full-charging-symbolic'
|
||||
History (charge):
|
||||
1526131128 97.000 charging
|
||||
History (rate):
|
||||
1526131128 9.868 charging
|
||||
|
||||
```
|
||||
|
||||
正如你所看到的那样,我的电池正处于充电状态,并且它的电量百分比是 97%。
|
||||
|
||||
假如上面的命令因为某些未知原因不起作用,可以尝试使用下面的命令:
|
||||
```
|
||||
$ upower -i `upower -e | grep 'BAT'`
|
||||
|
||||
```
|
||||
|
||||
**示例输出:**
|
||||
```
|
||||
native-path: BAT0
|
||||
vendor: Samsung SDI
|
||||
model: DELL 7XFJJA2
|
||||
serial: 4448
|
||||
power supply: yes
|
||||
updated: Sat 12 May 2018 06:50:49 PM IST (22 seconds ago)
|
||||
has history: yes
|
||||
has statistics: yes
|
||||
battery
|
||||
present: yes
|
||||
rechargeable: yes
|
||||
state: charging
|
||||
warning-level: none
|
||||
energy: 43.6119 Wh
|
||||
energy-empty: 0 Wh
|
||||
energy-full: 44.5443 Wh
|
||||
energy-full-design: 48.84 Wh
|
||||
energy-rate: 8.88 W
|
||||
voltage: 12.552 V
|
||||
time to full: 6.3 minutes
|
||||
percentage: 97%
|
||||
capacity: 91.2045%
|
||||
technology: lithium-ion
|
||||
icon-name: 'battery-full-charging-symbolic'
|
||||
History (rate):
|
||||
1526131249 8.880 charging
|
||||
|
||||
```
|
||||
|
||||
Upower 不仅可以显示出电池的状态,它还可以显示出已安装电池的其他完整信息,例如
|
||||
电池型号,供应商名称,电池的序列号,电池的状态,电池的电压等信息。
|
||||
|
||||
当然,如果你只想显示电池的状态,你可以可以结合使用 `upower` 命令和[**grep**][1] 命令,具体命令如下:
|
||||
```
|
||||
$ upower -i $(upower -e | grep BAT) | grep --color=never -E "state|to\ full|to\ empty|percentage"
|
||||
|
||||
```
|
||||
|
||||
**示例输出:**
|
||||
```
|
||||
state: fully-charged
|
||||
percentage: 100%
|
||||
|
||||
```
|
||||
|
||||
![][3]
|
||||
|
||||
从上面的输出中可以看到我的笔记本电池已经完全充满了。
|
||||
|
||||
想知晓更多的细节,可以参看 man 页:
|
||||
```
|
||||
$ man upower
|
||||
|
||||
```
|
||||
|
||||
##### 方法二 – 使用 “acpi” 命令
|
||||
|
||||
**acpi** 命令可以用来显示你的 Linux 发行版本中电池的状态以及其他 ACPI 信息。
|
||||
|
||||
在某些 Linux 发行版本中,你可能需要安装 **acpi** 命令。
|
||||
|
||||
要在 Debian, Ubuntu 及其衍生版本中安装它,可以使用如下命令:
|
||||
```
|
||||
$ sudo apt-get install acpi
|
||||
|
||||
```
|
||||
|
||||
在 RHEL, CentOS, Fedora 等系统中使用:
|
||||
```
|
||||
$ sudo yum install acpi
|
||||
|
||||
```
|
||||
|
||||
或者使用如下命令:
|
||||
```
|
||||
$ sudo dnf install acpi
|
||||
|
||||
```
|
||||
|
||||
在 Arch Linux 及其衍生版本中使用:
|
||||
```
|
||||
$ sudo pacman -S acpi
|
||||
|
||||
```
|
||||
|
||||
一旦 `acpi` 安装好后,运行下面的命令:
|
||||
```
|
||||
$ acpi -V
|
||||
|
||||
```
|
||||
|
||||
**注意:** 在上面的命令中, “V” 是大写字母。
|
||||
|
||||
**示例输出:**
|
||||
```
|
||||
Battery 0: Charging, 99%, 00:02:09 until charged
|
||||
Battery 0: design capacity 4400 mAh, last full capacity 4013 mAh = 91%
|
||||
Battery 1: Discharging, 0%, rate information unavailable
|
||||
Adapter 0: on-line
|
||||
Thermal 0: ok, 77.5 degrees C
|
||||
Thermal 0: trip point 0 switches to mode critical at temperature 84.0 degrees C
|
||||
Cooling 0: Processor 0 of 3
|
||||
Cooling 1: Processor 0 of 3
|
||||
Cooling 2: LCD 0 of 15
|
||||
Cooling 3: Processor 0 of 3
|
||||
Cooling 4: Processor 0 of 3
|
||||
Cooling 5: intel_powerclamp no state information available
|
||||
Cooling 6: x86_pkg_temp no state information available
|
||||
|
||||
```
|
||||
|
||||
首先让我们来检查电池的电量,可以运行:
|
||||
```
|
||||
$ acpi
|
||||
|
||||
```
|
||||
|
||||
**示例输出:**
|
||||
```
|
||||
Battery 0: Charging, 99%, 00:01:41 until charged
|
||||
Battery 1: Discharging, 0%, rate information unavailable
|
||||
|
||||
```
|
||||
|
||||
下面,让我们来查看电池的温度:
|
||||
```
|
||||
$ acpi -t
|
||||
|
||||
```
|
||||
|
||||
**示例输出:**
|
||||
```
|
||||
Thermal 0: ok, 63.5 degrees C
|
||||
|
||||
```
|
||||
|
||||
如果需要将温度以华氏温标显示,可以使用:
|
||||
```
|
||||
$ acpi -t -f
|
||||
|
||||
```
|
||||
|
||||
**示例输出:**
|
||||
```
|
||||
Thermal 0: ok, 144.5 degrees F
|
||||
|
||||
```
|
||||
|
||||
如果想看看交流电适配器是否连接上了没有,可以运行:
|
||||
```
|
||||
$ acpi -a
|
||||
|
||||
```
|
||||
|
||||
**示例输出:**
|
||||
```
|
||||
Adapter 0: on-line
|
||||
|
||||
```
|
||||
|
||||
假如交流电适配器没有连接上,则你将看到如下的输出:
|
||||
```
|
||||
Adapter 0: off-line
|
||||
|
||||
```
|
||||
|
||||
想获取更多的信息,可以查看 man 页:
|
||||
```
|
||||
$ man acpi
|
||||
|
||||
```
|
||||
|
||||
##### 方法三 - 使用 “Batstat” 程序
|
||||
|
||||
**batstat** 是一个基于 ncurses 的命令行小工具,使用它可以在类 Unix 系统中展示笔记本电池状态。它可以展示如下具体信息:
|
||||
|
||||
* 当前电池电量
|
||||
* 当前电池所存能量
|
||||
* 充满时所存能量
|
||||
* 从程序启动开始经历的时间,它不会追踪记录机器休眠的时间
|
||||
* 电池电量消耗历史数据
|
||||
|
||||
安装 `batstat` 轻而易举。使用下面的命令来复刻(clone)该程序的最新版本:
|
||||
```
|
||||
$ git clone https://github.com/Juve45/batstat.git
|
||||
|
||||
```
|
||||
|
||||
上面的命令将拉取 batstat 的最新版本并将它的内容保存在一个名为 ”batstat“ 的文件夹中。
|
||||
|
||||
首先将目录切换到 `batstat/bin/` 中:
|
||||
```
|
||||
$ cd batstat/bin/
|
||||
|
||||
```
|
||||
|
||||
接着将 “batstat” 二进制文件复制到 PATH 环境变量中的某个目录中,例如 `/usr/local/bin/` 目录:
|
||||
```
|
||||
$ sudo cp batstat /usr/local/bin/
|
||||
|
||||
```
|
||||
|
||||
使用下面的命令来让它可被执行:
|
||||
```
|
||||
$ sudo chmod +x /usr/local/bin/batstat
|
||||
|
||||
```
|
||||
|
||||
最后,使用下面的命令来查看你的电池状态。
|
||||
```
|
||||
$ batstat
|
||||
|
||||
```
|
||||
|
||||
示例输出:
|
||||
|
||||
![][4]
|
||||
|
||||
从上面的截图中可以看到我的笔记本电池正处于充电状态。
|
||||
|
||||
这个小工具还有某些小的限制。在书写本文之时,batstat 仅支持显示一个电池的相关信息。而且它只从 **“/sys/class/power_supply/”** 目录搜集相关的信息。假如你的电池信息被存放在另外的目录中,则这个小工具就不会起作用了。
|
||||
|
||||
想知晓更多信息,可以查看 batstat 的 [github 主页][5]。
|
||||
|
||||
上面就是今天我要分享的所有内容。当然,可能还有很多其他的命令或者程序来从 Linux 终端检查笔记本的电池状态。据我所知,上面给出的命令都运行良好。假如你知道其他命令来查看电池的状态,请在下面的评论框中让我们知晓。假如你所给出的方法能够起作用,我将对我的这篇文章进行更新。
|
||||
|
||||
最后,上面便是今天的全部内容了。更多的优质内容敬请期待,敬请关注!
|
||||
|
||||
欢呼吧!
|
||||
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.ostechnix.com/how-to-check-laptop-battery-status-in-terminal-in-linux/
|
||||
|
||||
作者:[SK][a]
|
||||
选题:[lujun9972](https://github.com/lujun9972)
|
||||
译者:[FSSlc](https://github.com/FSSlc)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://www.ostechnix.com/author/sk/
|
||||
[1]:https://www.ostechnix.com/the-grep-command-tutorial-with-examples-for-beginners/
|
||||
[2]:data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
|
||||
[3]:http://www.ostechnix.com/wp-content/uploads/2016/12/sk@sk_006-1.png
|
||||
[4]:http://www.ostechnix.com/wp-content/uploads/2016/12/batstat-1.png
|
||||
[5]:https://github.com/Juve45/batstat
|
Loading…
Reference in New Issue
Block a user