undefined

字符串比较相等、不相等的方法

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/bin/sh

#测试各种字符串比较操作。
#shell中对变量的值添加单引号,爽引号和不添加的区别:对类型来说是无关的,即不是添加了引号就变成了字符串类型,
#单引号不对相关量进行替换,如不对$符号解释成变量引用,从而用对应变量的值替代,双引号则会进行替代

A="$1"
B="$2"

echo "输入的原始值:A=$A,B=$B"

#判断字符串是否相等
if [ "$A" = "$B" ];then
echo "[ = ]"
fi

#判断字符串是否相等,与上面的=等价
if [ "$A" == "$B" ];then
echo "[ == ]"
fi

#注意:==的功能在[[]]和[]中的行为是不同的,如下

#如果$a以”a”开头(模式匹配)那么将为true
if [[ "$A" == a* ]];then
echo "[[ ==a* ]]"
fi

#如果$a等于a*(字符匹配),那么结果为true
if [[ "$A" == "a*" ]];then
echo "==/"a*/""
fi


#File globbing(通配) 和word splitting将会发生, 此时的a*会自动匹配到对应的当前以a开头的文件
#如在当前的目录中有个文件:add_crontab.sh,则下面会输出ok
#if [ "add_crontab.sh" == a* ];then
#echo "ok"
#fi
if [ "$A" == a* ];then
echo "[ ==a* ]"
fi

#如果$a等于a*(字符匹配),那么结果为true
if [ "$A" == "a*" ];then
echo "==/"a*/""
fi

#字符串不相等
if [ "$A" != "$B" ];then
echo "[ != ]"
fi

#字符串不相等
if [[ "$A" != "$B" ]];then
echo "[[ != ]]"
fi

#字符串不为空,长度不为0
if [ -n "$A" ];then
echo "[ -n ]"
fi

#字符串为空.就是长度为0.
if [ -z "$A" ];then
echo "[ -z ]"
fi

#需要转义<,否则认为是一个重定向符号
if [ $A /< $B ];then
echo "[ < ]"
fi

if [[ $A < $B ]];then
echo "[[ < ]]"
fi

#需要转义>,否则认为是一个重定向符号
if [ $A /> $B ];then
echo "[ > ]"
fi

if [[ $A > $B ]];then
echo "[[ > ]]"
fi

undefined

一、”[: too many arguments” error from if 问题

例如下的 shell 脚本:

1
2
3
4
VARIABLE=$(/some/command);
if [ $VARIABLE == 0 ]; then
# some action
fi

如果 $VARIABLE 时一个包含空格或者其他特殊字符的字符串,并且使用了单个方括号。那么该字符串可能会被拆分成多个单词。这些中的每一个都被视为一个单独的参数。
注意:单个方括号和两个方括号区别:https://serverfault.com/questions/52034/what-is-the-difference-between-double-and-single-square-brackets-in-bash

因此无法按照我们想要的去比较。可以将变量用双引号括起来,强制他保持为一个字符串。

查看更多

undefined

libio:https://github.com/wxggg/libio

微信的 matrix android hook 监控:https://github.com/Tencent/matrix/wiki/Matrix-Android-IOCanary

polyHook git 库:
https://github.com/stevemk14ebr/PolyHook
技术文档:https://www.codeproject.com/articles/1100579/polyhook-the-cplusplus-x-x-hooking-library

glibc 源码阅读:

1
2
3
4
5
6
7
8
9
10
11
12
glibc源码下载地址
http://ftp.gnu.org/pub/gnu/glibc/
http://www.gnu.org/software/libc/libc.html
http://mirrors.nju.edu.cn/gnu/libc/
http://ftp.ntu.edu.tw/gnu/glibc/
http://mirrors.syringanetworks.net/gnu/libc/
http://alpha.gnu.org/gnu/glibc/

在线源码阅读
https://code.woboq.org/userspace/glibc/
https://elixir.bootlin.com/glibc/glibc-2.31/source
第二个链接也可以在线阅读Linux内核源码。

查看更多

undefined

jemalloc 之设置线程的 arena

一、背景介绍

在内存监控中,我们想要监控线程的内存使用。但是在 Linux 中,一个进程中的所有线程共享进程的内存空间。这就导致我们无法准确的获取线程的内存使用。比如 A 线程申请的内存,可能会被 B 线程释放。

在 jemalloc 的内存分配实现中,arena 是最顶层的内存分配单元。jemalloc 会创建一定数量的 arena,每个线程都会绑定到 arena 上。线程采用 round-robin 轮询的方式选择可用的 arena 进行内存分配,为了减少线程之间的锁竞争,默认每个 CPU 会分配 4 个 arena。

因此我们可以将线程进行分类,然后自定义将线程绑定到某个 arena 上。然后监控此 arena 申请或者释放的内存。我们便可以得到某类线程的内存状况。

二、实现思路

jemalloc 提供了设置调用线程的 arena 的方法。

1
2
3
unsigned arena_num = 1;
size_t sz = sizeof(arena_num);
mallctl("thread.arena", NULL, 0, &arena_num, &sz);

查看更多

undefined

ptmalloc 的实现:https://blog.csdn.net/songchuwang1868/article/details/89951543

ptmalloc 源码分析:https://www.cnblogs.com/pppyyyzzz/p/14336521.html

源码分析:https://www.cnblogs.com/Five100Miles/p/8458688.html

源码分析:https://introspelliam.github.io/2018/05/21/pwn/malloc%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90%E2%80%94ptmalloc/

源码分析:https://snappyjack.github.io/articles/2019-11/glibc%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86ptmalloc%E6%BA%90%E4%BB%A3%E7%A0%81%E5%88%86%E6%9E%90%E7%AC%94%E8%AE%B0

查看更多

valgrind使用

一、使用 valgrind 检测 c++ 内存泄露

1
2
3
4
5
#include <malloc.h>
int main() {
int *array = (int*)malloc(sizeof(int));
return 0;
}

编译时加上 -g 选项。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# valgrind --tool=memcheck --leak-check=full ./main
==20819== Memcheck, a memory error detector
==20819== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==20819== Using Valgrind-3.21.0.GIT and LibVEX; rerun with -h for copyright info
==20819== Command: ./main
==20819==
==20819==
==20819== HEAP SUMMARY:
==20819== in use at exit: 4 bytes in 1 blocks
==20819== total heap usage: 1 allocs, 0 frees, 4 bytes allocated
==20819==
==20819== 4 bytes in 1 blocks are definitely lost in loss record 1 of 1
==20819== at 0x4C330C5: malloc (vg_replace_malloc.c:393)
==20819== by 0x10865B: main (main.cpp:4)
==20819==
==20819== LEAK SUMMARY:
==20819== definitely lost: 4 bytes in 1 blocks
==20819== indirectly lost: 0 bytes in 0 blocks
==20819== possibly lost: 0 bytes in 0 blocks
==20819== still reachable: 0 bytes in 0 blocks
==20819== suppressed: 0 bytes in 0 blocks
==20819==
==20819== For lists of detected and suppressed errors, rerun with: -s
==20819== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

查看更多

undefined

文件系统

一、文件系统

Linux 中一切皆文件,普通的文件、目录、块设备、套接字、管道等都是文件,通过统一的文件系统来管理。Linux 文件系统为每个文件都分配两个数据结构,索引节点(index node)和目录项(directory entry)。他们主要用来记录文件的元信息和目录结构。

  • 索引节点(inode)。用来记录文件的元数据,比如 inode 编号、文件大小、访问权限、修改日期、数据的位置等。索引节点和文件一一对应,它跟文件内容一样,都会被持久化存储到磁盘中。所以索引节点同样占用磁盘空间
  • 目录项(dentry)。用来记录文件的名字、索引节点指针以及与其他目录项的关联关系。多个关联的目录项,就构成了文件系统的目录结构。不过,不同于索引节点,目录项是由内核维护的一个内存数据结构,所以通常也被叫做目录项缓存

查看更多

undefined

一、磁盘

1. 按照存储介质,常见磁盘分为两类:机械磁盘和固态磁盘

  • 机械磁盘,也称为硬盘驱动器(Hard Disk Driver),通常缩写为 HDD。机械磁盘主要由盘片和读写磁头组成,数据就存储在盘片的环状磁道中。在读写数据前,需要移动读写磁头,定位到数据所在的磁道,然后才能访问数据。
    • 如果 I/O 请求刚好连续,那就不需要磁道寻址,自然可以获得最佳性能、
    • 如果 IO 随机,需要不停地移动磁头,来定位数据位置,所以读写速度就会比较慢

查看更多