kk Blog —— 通用基础


date [-d @int|str] [+%s|"+%F %T"]
netstat -ltunp
sar -n DEV 1

GPU温控

目录是 /sys/class/drm/card0/device/hwmon/hwmonX/

换内核之类的操作会改变 hwmonX

调节脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/python

import commands;
import time;

t0=0
temp_inc=[90000, 85000, 80000, 70000, 60000, 50000, 40000, 00000];
pwm_inc=[ 245,   205,   165,   125,   105,   85,    65,    45];

temp_dec=[89000, 84000, 79000, 67000, 57000, 47000, 37000, 00000];
pwm_dec=[ 245,   205,   165,   125,   105,   85,    65,    45];


global pwm1
pwm1=0;

def set_pwm(newpwm):
	global pwm1
	if newpwm != pwm1:
		cmd="echo "+str(newpwm)+" > /sys/class/drm/card0/device/hwmon/hwmon3/pwm1";
		r,o = commands.getstatusoutput(cmd);
		pwm1=newpwm;

		#cmd1="cat /sys/class/drm/card0/device/hwmon/hwmon3/pwm1";
		#r,o = commands.getstatusoutput(cmd1);
                #print cmd
                #print r, o

r,o = commands.getstatusoutput("echo 1 > /sys/class/drm/card0/device/hwmon/hwmon3/pwm1_enable");
while 1:
	r,t = commands.getstatusoutput("cat /sys/class/drm/card0/device/hwmon/hwmon3/temp1_input");
	t = int(t);
	if t - t0 > 0:
		for i in range(0, 8):
			if t >= temp_inc[i]:
				break;
		#print "inc ", t, temp_inc[i], pwm_inc[i]
		set_pwm(pwm_inc[i]);
	elif t - t0 < 0:
		for i in range(0, 8):
			if t >= temp_dec[i]:
				break;
		#print "dec ", t, temp_dec[i], pwm_dec[i]
		set_pwm(pwm_dec[i]);

	t0 = t;
	time.sleep(10);

ubuntu 编译内核、模块

https://launchpad.net/ubuntu/+source/linux/+changelog

4.15.0

https://launchpad.net/ubuntu/bionic/+source/linux/+changelog

https://packages.ubuntu.com/xenial/linux-source-4.15.0
http://security.ubuntu.com/ubuntu/pool/main/l/linux-hwe/linux-source-4.15.0_4.15.0-58.64~16.04.1_all.deb

https://launchpad.net/ubuntu/xenial/amd64/linux-image-unsigned-4.15.0-58-generic-dbgsym
http://launchpadlibrarian.net/436393485/linux-image-unsigned-4.15.0-58-generic-dbgsym_4.15.0-58.64~16.04.1_amd64.ddeb

https://launchpad.net/ubuntu/bionic/amd64/linux-image-unsigned-4.15.0-58-generic-dbgsym
http://launchpadlibrarian.net/436226708/linux-image-unsigned-4.15.0-58-generic-dbgsym_4.15.0-58.64_amd64.ddeb

4.18.0

https://launchpad.net/ubuntu/xenial/+source/linux/+changelog

https://packages.ubuntu.com/bionic/linux-source-4.18.0
http://security.ubuntu.com/ubuntu/pool/main/l/linux-hwe/linux-source-4.18.0_4.18.0-25.26~18.04.1_all.deb

https://launchpad.net/ubuntu/bionic/amd64/linux-image-4.18.0-25-generic-dbgsym
http://launchpadlibrarian.net/430863032/linux-image-4.18.0-25-generic-dbgsym_4.18.0-25.26~18.04.1_amd64.ddeb

gcc 7 编译内核模块时无法找到 stdarg.h 的问题

https://blog.gloriousdays.pw/2018/09/09/cannot-find-stdarg-h-on-linux-kernel-4-15-with-gcc-7-3/

这是一个非常奇怪的错误,出现在 Ubuntu 18.04 上,默认安装的内核版本是 4.15,gcc 是 7.3,在编译内核模块时报错:

1
2
3
4
5
6
7
In file included from ./include/linux/list.h:9:0,
                 from ./include/linux/module.h:9,
                 from /root/Software/newbbr/tcp_tsunami.c:59:
./include/linux/kernel.h:6:10: fatal error: stdarg.h: No such file or directory
 #include <stdarg.h>
          ^~~~~~~~~~
compilation terminated.

gcc 认为找不到 stdarg.h。看这个错误的位置,个人认为应该不是我配置的问题或者是我代码的问题,搜索了一下,也有很多在 4.15 内核上出现的同样错误。目前没有什么很好的解决方案,暂时性的方案是在编译的 Makefile 里面加一行:

1
ccflags-y=-I/usr/lib/gcc/x86_64-linux-gnu/7/include

如果是 gcc 8,就相应把版本改成 8 就可以了

编译内核

1
2
3
sudo apt-get install libncurses-dev flex bison openssl-dev libssl-dev dkms libelf-dev

make bindeb-pkg -j8

按这里没成功 https://wiki.ubuntu.com/Kernel/BuildYourOwnKernel

编译perf

1
2
cd tools/perf
make

CPU温控

常规调节

1
2
echo 72 > /sys/devices/system/cpu/intel_pstate/max_perf_pct
echo 1  > /sys/devices/system/cpu/intel_pstate/no_turbo

参数解释

https://www.kernel.org/doc/Documentation/cpu-freq/intel-pstate.txt

/sys/devices/system/cpu/intel_pstate/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
      max_perf_pct: Limits the maximum P-State that will be requested by
      the driver. It states it as a percentage of the available performance. The
      available (P-State) performance may be reduced by the no_turbo
      setting described below.

      min_perf_pct: Limits the minimum P-State that will be requested by
      the driver. It states it as a percentage of the max (non-turbo)
      performance level.

      no_turbo: Limits the driver to selecting P-State below the turbo
      frequency range.

      turbo_pct: Displays the percentage of the total performance that
      is supported by hardware that is in the turbo range. This number
      is independent of whether turbo has been disabled or not.

      num_pstates: Displays the number of P-States that are supported
      by hardware. This number is independent of whether turbo has
      been disabled or not.

For example, if a system has these parameters:
	Max 1 core turbo ratio: 0x21 (Max 1 core ratio is the maximum P-State)
	Max non turbo ratio: 0x17
	Minimum ratio : 0x08 (Here the ratio is called max efficiency ratio)

Sysfs will show :
	max_perf_pct:100, which corresponds to 1 core ratio
	min_perf_pct:24, max_efficiency_ratio / max 1 Core ratio
	no_turbo:0, turbo is not disabled
	num_pstates:26 = (max 1 Core ratio - Max Efficiency Ratio + 1)
	turbo_pct:39 = (max 1 core ratio - max non turbo ratio) / num_pstates

Refer to "Intel庐 64 and IA-32 Architectures Software Developer鈥檚 Manual
Volume 3: System Programming Guide" to understand ratios.

There is one more sysfs attribute in /sys/devices/system/cpu/intel_pstate/
that can be used for controlling the operation mode of the driver:

      status: Three settings are possible:
      "off"     - The driver is not in use at this time.
      "active"  - The driver works as a P-state governor (default).
      "passive" - The driver works as a regular cpufreq one and collaborates
                  with the generic cpufreq governors (it sets P-states as
                  requested by those governors).
      The current setting is returned by reads from this attribute.  Writing one
      of the above strings to it changes the operation mode as indicated by that
      string, if possible.  If HW-managed P-states (HWP) are enabled, it is not
      possible to change the driver's operation mode and attempts to write to
      this attribute will fail.

https://huataihuang.gitbooks.io/cloud-atlas/os/linux/kernel/cpu/intel_pstate.html

http://www.litrin.net/2018/12/28/cpu%E7%9A%84%E7%94%B5%E6%BA%90%E7%8A%B6%E6%80%81%E5%88%86%E7%B1%BB/