performance turning-linux

1 lsblk

[root@localhost]# lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0   1.1T  0 disk 
├─sda1   8:1    0     1G  0 part /boot
├─sda2   8:2    0   100G  0 part /
├─sda3   8:3    0   450G  0 part /backup
├─sda4   8:4    0     1K  0 part 
├─sda5   8:5    0   100G  0 part /u00
├─sda6   8:6    0   100G  0 part /var
├─sda7   8:7    0    50G  0 part /tmp
└─sda8   8:8    0 315.7G  0 part /logs
sdc      8:32   0 893.1G  0 disk 
└─sdc1   8:33   0 893.1G  0 part /data1
sdb      8:16   0 893.1G  0 disk 
sde      8:64   0 893.1G  0 disk 
├─sde1   8:65   0   450G  0 part /data3
└─sde2   8:66   0 443.1G  0 part /commitlog
sdd      8:48   0 893.1G  0 disk 
└─sdd1   8:49   0 893.1G  0 part /data2

2 smartctl

[root@localhost ~]# smartctl -a /dev/sda
smartctl 5.43 2016-09-28 r4347 [x86_64-linux-2.6.32-754.9.1.el6.x86_64] (local build)
Copyright (C) 2002-12 by Bruce Allen, http://smartmontools.sourceforge.net

Vendor:               Cisco   
Product:              UCSC-MRAID12G   
Revision:             4.62
User Capacity:        1,198,999,470,080 bytes [1.19 TB]
Logical block size:   512 bytes
Logical Unit id:      0x6f80bcbeac41d2a023eec6c514c1ceb3
Serial number:        00b3cec114c5c6ee23a0d241acbebc80
Device type:          disk
Local Time is:        Fri Mar  1 02:47:43 2019 GMT
Device does not support SMART

3 iostat -x -p -t
-t 显示时间戳
-p 会把dev0-8之类转化为sda1.

sdc               0.00    40.23    0.99    2.12   144.29   338.80   155.52     0.02    7.68    0.36   11.09   0.21   0.06
sdc1              0.00    40.23    0.99    2.12   144.29   338.80   155.52     0.02    7.68    0.36   11.09   0.21   0.06
sdb               0.00     0.00    0.00    0.00     0.00     0.00    24.89     0.00    0.14    0.14    0.00   0.14   0.00
sde               0.00    80.05    0.20    2.97    16.89   664.23   214.30     0.02    7.20    0.39    7.66   0.34   0.11
sde1              0.00    19.64    0.20    1.03    16.88   165.34   147.84     0.01    9.42    0.38   11.21   0.22   0.03
sde2              0.00    60.42    0.00    1.95     0.00   498.89   256.40     0.01    5.79    3.80    5.79   0.42   0.08
sdd               0.00    38.47    1.80    2.01   237.10   323.88   147.09     0.02    5.95    0.35   10.95   0.20   0.07
sdd1              0.00    38.47    1.80    2.01   237.10   323.88   147.09     0.02    5.95    0.35   10.95   0.20   0.07

4 查看分区类型

[root@host ~]# cat /etc/fstab

UUID=74d82177-9ffd-4564-94d6-fc97cfdda575 / ext4 defaults 1 1
UUID=b6d58e19-7800-4284-96ae-615744a40f9d /backup ext4 defaults 1 2
UUID=465299be-d033-4e52-9359-125face600fc /boot ext2 defaults 1 2
UUID=cf442cbb-f33a-444d-be16-ae82a54a1802 /commitlog ext4 noatime 1 2
UUID=26c93b16-ce00-4f75-a17f-b56f7dcbd8ba /data1 ext4 noatime 1 2
UUID=65166b86-d9ac-41a8-be07-95c1e1d5e8d0 /data2 ext4 noatime 1 2
UUID=f1533b88-c494-41eb-8407-3951b5999486 /data3 ext4 noatime 1 2
UUID=c897ac33-7170-42d6-85c9-2d599e22dda0 /logs ext4 defaults 1 2
UUID=4cfed02e-31ea-4d09-970c-f906e3cc1c8a /tmp ext4 defaults 1 2
UUID=fbb944ee-6b21-4571-b1d8-2cc37e47538c /u00 ext4 defaults 1 2
UUID=fa2c170d-71b1-4707-a692-8a12c4d83706 /var ext4 defaults 1 2

5 pidstat

[root@host~]# pidstat -d 1
Linux 2.6.32-504.1.3.el6.x86_64 (tagrdbca01)    04/04/2019      _x86_64_        (32 CPU)

06:05:00 AM       PID   kB_rd/s   kB_wr/s kB_ccwr/s  Command
06:05:01 AM      1373   7384.62      0.00      0.00  rsync
06:05:01 AM      1375      0.00  11911.54      0.00  rsync
06:05:01 AM      1557      0.00     96.15      0.00  jbd2/sdd1-8
06:05:01 AM      4907      0.00      7.69      0.00  rsync
06:05:01 AM      5156      0.00      7.69      0.00  rsync
06:05:01 AM     18653    584.62    176.92      0.00  java
06:05:01 AM     19594      0.00     11.54      0.00  rsync

6 io information

  cd /sys/class/block/sdb/queue
[root@hostqueue]# ll
total 0
-rw-r--r-- 1 root root 4096 Apr  4 07:57 add_random
-r--r--r-- 1 root root 4096 Apr  4 07:00 discard_granularity
-r--r--r-- 1 root root 4096 Apr  4 07:57 discard_max_bytes
-r--r--r-- 1 root root 4096 Apr  4 07:57 discard_zeroes_data
-r--r--r-- 1 root root 4096 Apr  4 07:57 hw_sector_size
drwxr-xr-x 2 root root    0 Apr  4 07:57 iosched
-rw-r--r-- 1 root root 4096 Apr  4 07:57 iostats
-r--r--r-- 1 root root 4096 Apr  4 07:00 logical_block_size
-r--r--r-- 1 root root 4096 Apr  4 07:57 max_hw_sectors_kb
-rw-r--r-- 1 root root 4096 Apr  4 07:57 max_sectors_kb
-r--r--r-- 1 root root 4096 Apr  4 07:57 max_segments
-r--r--r-- 1 root root 4096 Apr  4 07:57 max_segment_size
-r--r--r-- 1 root root 4096 Apr  4 07:57 minimum_io_size
-rw-r--r-- 1 root root 4096 Apr  4 07:57 nomerges
-rw-r--r-- 1 root root 4096 Apr  4 07:57 nr_requests
-r--r--r-- 1 root root 4096 Apr  4 07:57 optimal_io_size
-r--r--r-- 1 root root 4096 Apr  4 07:57 physical_block_size
-rw-r--r-- 1 root root 4096 Aug 21  2018 read_ahead_kb
-rw-r--r-- 1 root root 4096 Aug 21  2018 rotational
-rw-r--r-- 1 root root 4096 Apr  4 07:57 rq_affinity
-rw-r--r-- 1 root root 4096 Aug 21  2018 scheduler
-rw-r--r-- 1 root root 4096 Apr  4 07:57 unpriv_sgio

7 修改io schedule

sudo echo noop > /sys/block/hda/queue/scheduler

Set the IO scheduler to either deadline or noop:
The noop scheduler is the right choice when the target block device is an array of SSDs behind a high-end IO controller that performs IO optimization.
The deadline scheduler optimizes requests to minimize IO latency. If in doubt, use the deadline scheduler.

faults on coding (to be continued)

零零散散的在编码过程中犯过一些错误,所以打算专门写一篇总结,持续记录这些错误,以便知错能改。

1 work一直良好的系统中,明显的错误不要改,例如:

private int age;
public void setAge(int age){
 age = age;
}

有些功能实际上错上加错的结果,当纠正一个错误的时候,结果就变成了错误。

2 理所当然的思维惯性。

下面的结果是什么?是不是true?

	Properties properties = new Properties();
        properties.put("key",true); 
        
	String valueStr = properties.getProperty("key");
	System.out.println(valueStr);
	System.out.println(Boolean.TRUE.toString().equalsIgnoreCase(valueStr));

源码分析:
实际上设置的是boolean型的值,然后getProperty返回的是null:

	    public String getProperty(String key) {
	        Object oval = super.get(key);
	        String sval = (oval instanceof String) ? (String)oval : null;
	        return ((sval == null) && (defaults != null)) ? defaults.getProperty(key) : sval;
	    }

3 针对接口编程和误用方法:

下面的写法试图cache一个值,当不存在对应的value时,自动加载一个值,那下面的值返回什么?

cache loader

  		CacheLoader<String, String> loader = new CacheLoader<String, String>() {

			@Override
			public String load(String key) throws Exception {
 				return key + "'s value";
			}
		};

使用:

		Cache<String, String> cache = CacheBuilder.newBuilder().maximumSize(10_000).expireAfterWrite(15, TimeUnit.MINUTES).build(loader);
  		String value = cache.getIfPresent("key1");

实际正确的写法

  		LoadingCache<String,String> cache = CacheBuilder.newBuilder().maximumSize(10_000).expireAfterWrite(15, TimeUnit.MINUTES).build(loader);
   		String value = cache.get("key1");

问题出在,一定要仔细阅读API的文档,不要想当然,同时针对接口编程,不定是针对顶层接口编程,如果上来就赋予给顶层接口,则后面的方法选择范围就比较小,可能就不假思考。

4 不假思索的认为某种类型是枚举

在判断响应是否是JSON body时,误以为MediaType.APPLICATION_JSON_TYPE肯定被定义成枚举,所以直接用==判断。

 MediaType.APPLICATION_JSON_TYPE == response.getMediaType();

修改:不定是每个感觉应该定义成枚举类型的就会定义成枚举类型,另外没有搞清楚状况前,用equals肯定比==更安全,

MediaType.APPLICATION_JSON_TYPE.equals(response.getMediaType()) 

5 Arrays.aslist返回的list?

下面的代码能编译,但是有没有问题?

		List<String> asList = Arrays.asList("123", "456");
		asList.removeIf(string -> string.equalsIgnoreCase("123"));

结果:

Exception in thread "main" java.lang.UnsupportedOperationException
	at java.util.AbstractList.remove(AbstractList.java:161)
	at java.util.AbstractList$Itr.remove(AbstractList.java:374)
	at java.util.Collection.removeIf(Collection.java:415)
	at com.github.metrics.Metric.main(Metric.java:29)

源码解析:

    @SuppressWarnings("varargs")
    public static <T> List<T> asList(T... a) {
        return new ArrayList<>(a); //此处返回的list是java.util.Arrays.ArrayList<E>,而并不是普通的java.util.ArrayList<E>
    }

而这种list的一些实现并未实现:

    public E remove(int index) {
        throw new UnsupportedOperationException();
    }

思考:可以调用的方法不见得可以work,返回看起来名字一样的,但是不见得是一个。

6 当从list删除元素时,如果从前往后删,则注意有元素删除后,改元素后面的索引以及总的size都已经发生了变化

        for (int i = 1; i < list.size(); i++) {
                Element element = list.get(i);
                if(element.getValue()>1){
                    list.remove(i);
                }
        }

或者:

        for (int i = 1; i < list.size(); i++) {
                Element element = list.get(i);
                if(element.getValue()>1){
                    list.remove(element);
                }
        }

都是坑,所以从后往前删除则没有这个问题。

7 关于lombok的builder注解,成员变量即使设置了默认值,如果builder没有显示设置的话,也不起作用。

@Builder
public class IvrCall extends DataTransferObject {
 
    private List<Step> executeTimeStatistics = new ArrayList<>();

当使用builder构建时,没设置executeTimeStatistics,则默认值为null,并不是new ArrayList<>()

8 package.toString vs package.getName 不同

        String packageStr = ZZZ.class.getPackage().toString();
        String packageName = ZZZ.class.getPackage().getName();
        System.out.println(packageStr);
        System.out.println(packageName);


        package com.xxx.yyy
        com.xxx.yyy

8 Path大小写问题
9 https://segmentfault.com/q/1010000019170109