diff --git a/source/tools/monitor/unity/Dockerfile b/source/tools/monitor/unity/Dockerfile index c60554cb71280dc462401e4c556b063a08a4f257..49e867c282f16434ceb911992f9899df582e47f0 100644 --- a/source/tools/monitor/unity/Dockerfile +++ b/source/tools/monitor/unity/Dockerfile @@ -34,6 +34,7 @@ RUN source /opt/rh/devtoolset-9/enable && \ luarocks install md5 && \ luarocks install luaposix 35.1-1 && \ luarocks install http && \ + luarocks install inotify && \ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/ && \ cd ../beeQ/ && \ make \ No newline at end of file diff --git a/source/tools/monitor/unity/beaver/beaver.c b/source/tools/monitor/unity/beaver/beaver.c index 9885f0567b3893d087c3fe89c49caf169e20fa16..073927299d3602300bc43d3e3cfb97c2d1d1d6cf 100644 --- a/source/tools/monitor/unity/beaver/beaver.c +++ b/source/tools/monitor/unity/beaver/beaver.c @@ -11,6 +11,8 @@ #include #include #include +#include +#include extern int lua_reg_errFunc(lua_State *L); extern int lua_check_ret(int ret); diff --git a/source/tools/monitor/unity/beaver/guide/common.md b/source/tools/monitor/unity/beaver/guide/common.md index 648b4ceefb9235137c1015e901d3c4d7dddb62bb..c80009ef34aa2d5def6c2b052083f48f9c9a2bf0 100644 --- a/source/tools/monitor/unity/beaver/guide/common.md +++ b/source/tools/monitor/unity/beaver/guide/common.md @@ -126,8 +126,8 @@ python 最擅长在于字符串处理。可以参考这里的[官方库说明](h 使用 pystring 库是要注意以下事项: -1. 当前unity\-mon 采用的是lua 自动的正则匹配方法,未集成regexp 标准正则库; -2. pystring 中提供的绝大部分函数都与python的处理方法保持一致,但是不能保证百分百,故使用时建议根据实际用例测试一遍,可以参考[测试用例函数](https://gitee.com/anolis/sysak/blob/opensource_branch/source/tools/monitor/unity/test/string/py.lua) +1. 当前unity\-mon 采用的是lua 自带的正则匹配方法,未集成regexp 标准正则库; +2. pystring 中提供的绝大部分函数都与python的处理方法保持一致,但是不保证百分百兼容,故使用时建议根据实际用例测试一遍,可以参考[测试用例函数](https://gitee.com/anolis/sysak/blob/opensource_branch/source/tools/monitor/unity/test/string/py.lua) ### pystring:shift(s, n) @@ -139,48 +139,69 @@ python 最擅长在于字符串处理。可以参考这里的[官方库说明](h ### pystring:islower(s) * 函数说明:判断字符串是否全部为小写字母 -* 传参 1:s 要判断的字符串 +* 参数1: s 目标字符串 +* 返回值:true/false ### pystring:isupper(s) * 函数说明:同python 实现 +* 参数1: s 目标字符串 +* 返回值:true/false ### pystring:ishex(s) * 函数说明:判断目标字符串是否为hex 字符串,含0-9,a-f,A-F +* 参数1: s 目标字符串 +* 返回值:true/false ### pystring:isalnum(s) * 函数说明:同python 实现 +* 参数1: s 目标字符串 +* 返回值:true/false ### pystring:istilte(s) * 函数说明:判断单词是否符合首字母大写,其余字母小写的方式 +* 参数1: s 目标字符串 +* 返回值:true/false ### pystring:isfloat(s) * 函数说明:判断字符串是否为浮点数呈现形式 +* 参数1: s 目标字符串 +* 返回值:true/false ### pystring:lower(s) * 函数说明:同python 实现,转小写 +* 参数1: s 目标字符串 +* 返回值:处理后的字符串 ### pystring:upper(s) * 函数说明:同python 实现,转大写 +* 参数1: s 目标字符串 +* 返回值:处理后的字符串 ### pystring:swapcase(s) * 函数说明:同python 实现,交换大小写 +* 参数1: s 目标字符串 +* 返回值:处理后的字符串 ### pystring:capitalize(s) * 函数说明:单词首字母大写 +* 参数1: s 目标字符串 +* 返回值:处理后的字符串 ### pystring:title(s) * 函数说明:字符串中所有单词首字母大写 +* 参数1: s 目标字符串 +* 返回值:处理后的字符串 ### pystring:ljust(s, len, ch) @@ -207,3 +228,138 @@ python 最擅长在于字符串处理。可以参考这里的[官方库说明](h * 参数3:ch 填充字符,默认为空格,必须是单字符,否则会抛异常 * 返回值:对齐后的字符串 +### pystring:zfill(s, len) + +* 函数说明:将字符串s 按照 len 长度向左填0 +* 参数1: s 目标字符串 +* 参数2:len 对齐长度 +* 返回值:填齐后的字符串 + +### pystring:split(s, delimiter, n) + +* 函数说明:将字符串s 按照 delimiter 分隔符进行分割 +* 参数1: s 目标字符串 +* 参数2:delimiter 分隔符,如果为空,则按照 多空格模式进行分割 +* 参数3:n 分割次数,默认为最多次数 +* 返回值:分割后的字符串数组 + +### pystring:rsplit(s, delimiter, n) + +* 函数说明:将字符串s 按照 delimiter 分隔符从右边进行分割 +* 参数1: s 目标字符串 +* 参数2:delimiter 分隔符,如果为空,则按照 多空格模式进行分割 +* 参数3:n 分割次数,默认为最多次数 +* 返回值:分割后的字符串数组 + + +### pystring:partition(s, del) + +* 函数说明:将字符串s 按照 del 分隔符进行一次分割,返回 [head, del, tail] list +* 参数1: s 目标字符串 +* 参数2:del 分隔符,如果为空,则按照 多空格模式进行分割 +* 返回值:分割后的字符串数组 + +### pystring:rpartition(s, del) + +* 函数说明:将字符串s 按照 del 分隔符从右侧进行一次分割,返回 [head, del, tail] list +* 参数1: s 目标字符串 +* 参数2:del 分隔符,如果为空,则按照 多空格模式进行分割 +* 返回值:分割后的字符串数组 + +### pystring:splitlines(s) + +* 函数说明:将字符串s 按照换行符进行分割 +* 参数1: s 目标字符串 +* 返回值:分割后的字符串数组 + +### pystring:lstrip(s, chars) + +* 函数说明:strip 左边的字符 +* 参数1: s 目标字符串 +* 参数2:chars 要strip 字符串 +* 返回值:strip 后面的字符串 + +### pystring:rstrip(s, chars) + +* 函数说明:strip 右边的字符 +* 参数1: s 目标字符串 +* 参数2:chars 要strip 字符串 +* 返回值:strip 后面的字符串 + +### pystring:strip(s, chars) + +* 函数说明:strip 两边的字符 +* 参数1: s 目标字符串 +* 参数2:chars 要strip 字符串 +* 返回值:strip 后面的字符串 + +### pystring:join(delim, strings) + +* 函数说明:将字符串list拼接成单一字符串 +* 参数1: delim 字符串间的分隔符号 +* 参数2:strings 要拼接的字符串list +* 返回值:拼接后的字符串 + +### pystring:startswith(s1, s2) +* 函数说明:判断字符串s1是否以s2 开头 +* 参数1: s1 目标字符串 +* 参数2:s2 +* 返回值:true/false + +### pystring:endswith(s1, s2) +* 函数说明:判断字符串s1是否以s2 结尾 +* 参数1: s1 目标字符串 +* 参数2:s2 +* 返回值:true/false + +### pystring:find(s1, s2, start, stop) +* 函数说明:判断s1 是否包含s2 子串 +* 参数1: s1 目标字符串 +* 参数2:s2 目标子串 +* 参数3: 字符串起点 默认为1 +* 参数4:字符串截止点 默认为-1 +* 返回值:如果包含s2,返回字符下标,不包含返回-1 + +### pystring:rfind(s1, s2, start, stop) +* 函数说明:从右边开始,判断s1 是否包含s2 子串 +* 参数1: s1 目标字符串 +* 参数2:s2 目标子串 +* 参数3: 字符串起点 默认为1 +* 参数4:字符串截止点 默认为-1 +* 返回值:如果包含s2,返回字符下标,不包含返回-1 + +### pystring:index(s1, s2, start, stop) +* 函数说明:判断s1 是否包含s2 子串 +* 参数1: s1 目标字符串 +* 参数2:s2 目标子串 +* 参数3: 字符串起点 默认为1 +* 参数4:字符串截止点 默认为-1 +* 返回值:如果包含s2,返回字符下标,不包含则抛出异常 + +### pystring:rindex(s1, s2, start, stop) +* 函数说明:从右边开始,判断s1 是否包含s2 子串 +* 参数1: s1 目标字符串 +* 参数2:s2 目标子串 +* 参数3: 字符串起点 默认为1 +* 参数4:字符串截止点 默认为-1 +* 返回值:如果包含s2,返回字符下标,不包含则抛出异常 + +### pystring:count(s, find) +* 函数说明:获取目标字符串中 子串数量 +* 参数1: s 目标字符串 +* 参数2:find 要查找的子串 +* 返回值:查找到的子串数量 + +### pystring:replace(s, find, repl, n) +* 函数说明:替换字符串中的子串数据 +* 参数1: s 目标字符串 +* 参数2:find 要查找的子串 +* 参数3:repl 要替换的结果 +* 参数4:替换数量,默认为不限 +* 返回值:替换结果 + +### pystring:expandtabs(s, tabs) +* 函数说明:将字符串中tabs 替换成空格 +* 参数1: s 目标字符串 +* 参数2:tabs 占空格数量,默认为4 +* 返回值:替换结果 \ No newline at end of file diff --git a/source/tools/monitor/unity/beaver/guide/image/beaver.png b/source/tools/monitor/unity/beaver/guide/image/beaver.png new file mode 100644 index 0000000000000000000000000000000000000000..54e9fda56fc9babdc43efe198dd222c03b4b169b Binary files /dev/null and b/source/tools/monitor/unity/beaver/guide/image/beaver.png differ diff --git a/source/tools/monitor/unity/beaver/guide/image/bee.png b/source/tools/monitor/unity/beaver/guide/image/bee.png new file mode 100644 index 0000000000000000000000000000000000000000..3ed6c37ca2046dbb8faacc7e660ca8a90f19f60c Binary files /dev/null and b/source/tools/monitor/unity/beaver/guide/image/bee.png differ diff --git a/source/tools/monitor/unity/beaver/guide/image/crab.png b/source/tools/monitor/unity/beaver/guide/image/crab.png new file mode 100644 index 0000000000000000000000000000000000000000..1d969925af4d1c1827879e2b9a432346f01fc330 Binary files /dev/null and b/source/tools/monitor/unity/beaver/guide/image/crab.png differ diff --git a/source/tools/monitor/unity/beaver/guide/image/fox.png b/source/tools/monitor/unity/beaver/guide/image/fox.png new file mode 100644 index 0000000000000000000000000000000000000000..fa3bbb357cedd02440ab9b9cf5814bf774dd9f8f Binary files /dev/null and b/source/tools/monitor/unity/beaver/guide/image/fox.png differ diff --git a/source/tools/monitor/unity/beaver/guide/image/unity.png b/source/tools/monitor/unity/beaver/guide/image/unity.png new file mode 100644 index 0000000000000000000000000000000000000000..f054d839f9a683585fcb4abdd2bd59b99c6d6289 Binary files /dev/null and b/source/tools/monitor/unity/beaver/guide/image/unity.png differ diff --git a/source/tools/monitor/unity/beaver/guide/metrics.md b/source/tools/monitor/unity/beaver/guide/metrics.md index b8d4190b2fd18e6123d2b733c1ccebfc4927ac2a..fd667b84c0e91694d43afb65520d2015bb1d32fa 100644 --- a/source/tools/monitor/unity/beaver/guide/metrics.md +++ b/source/tools/monitor/unity/beaver/guide/metrics.md @@ -26,6 +26,74 @@ | machine | - | uname -r | | collector/proc\_uptime.lua | | sysname | - | uname -r | | collector/proc\_uptime.lua | +### cgroups 表 + +| 指标名 | 单位 | 标签说明 | 备注 | 源码路径 | +| :--- | --- | :---- | :---- | :--- | +| type | - | subsys类型 | | collector/proc\_cgroups.lua | +| blkio | 个 | blkio cgroup 数量 | | collector/proc\_cgroups.lua | +| freezer | 个 | freezer cgroup数量 | | collector/proc\_cgroups.lua | +| devices | 个 | devices cgroup数量 | | collector/proc\_cgroups.lua | +| hugetlb | 个 | hugetlb cgroup数量 | | collector/proc\_cgroups.lua | +| pids | 个 | blkio cgroup 数量 | | collector/proc\_cgroups.lua | +| rdma | 个 | rdma cgroup数量 | | collector/proc\_cgroups.lua | +| net\_prio | 个 | net_prio cgroup数量 | | collector/proc\_cgroups.lua | +| net\_cls | 个 | net_cls cgroup数量 | | collector/proc\_cgroups.lua | +| cpu | 个 | cpu cgroup 数量 | | collector/proc\_cgroups.lua | +| cpuacct | 个 | cpuacct cgroup数量 | | collector/proc\_cgroups.lua | +| perf\_event | 个 | perf_event cgroup数量 | | collector/proc\_cgroups.lua | +| memory | 个 | memory cgroup数量 | | collector/proc\_cgroups.lua | + +### interrupts 表 + +| 指标名 | 单位 | 标签说明 | 备注 | 源码路径 | +| :--- | --- | :---- | :---- | :--- | +| cpu | - | CPU ID | | collector/proc\_interrupts.lua | +| 中断名称 | 次 | 中断触发次数 | | collector/proc\_interrupts.lua | + +### mounts 表 + +| 指标名 | 单位 | 标签说明 | 备注 | 源码路径 | +| :--- | --- | :---- | :---- | :--- | +| fs | - | sysfs | | collector/proc\_mounts.lua | +| mount | - | 挂载目录 | | collector/proc\_mounts.lua | +| f\_bsize | - | Filesystem block size | | collector/proc\_mounts.lua | +| f\_blocks | - | Size of fs in f_frsize units | | collector/proc\_mounts.lua | +| f\_bfree | - | Number of free blocks | | collector/proc\_mounts.lua | +| f\_bavail | - | Number of free blocks for unprivileged users | | collector/proc\_mounts.lua | +| f\_files | - | Number of inodes | | collector/proc\_mounts.lua | +| f\_ffree | - | Number of free inodes | | collector/proc\_mounts.lua | +| f\_favail | - | Number of free inodes for unprivileged users | | collector/proc\_mounts.lua | + +### softirqs 表 + +| 指标名 | 单位 | 标签说明 | 备注 | 源码路径 | +| :--- | --- | :---- | :---- | :--- | +| cpu | - | CPU ID | | collector/proc\_softirqs.lua | +| HI | 次 | HI软中断触发次数 | | collector/proc\_softirqs.lua | +| TIMER | 次 | TIMER软中断触发次数 | | collector/proc\_softirqs.lua | +| NET\_TX | 次 | NET\_TX软中断触发次数 | | collector/proc\_softirqs.lua | +| NET\_RX | 次 | NET\_RX软中断触发次数 | | collector/proc\_softirqs.lua | +| BLOCK | 次 | BLOCK软中断触发次数 | | collector/proc\_softirqs.lua | +| IRQ_POLL | 次 | IRQ\_POLL软中断触发次数 | | collector/proc\_softirqs.lua | +| TASKLET | 次 | TASKLET软中断触发次数 | | collector/proc\_softirqs.lua | +| SCHED | 次 | SCHED软中断触发次数 | | collector/proc\_softirqs.lua | +| HRTIMER | 次 | HRTIMER软中断触发次数 | | collector/proc\_softirqs.lua | +| RCU | 次 | RCU软中断触发次数 | | collector/proc\_softirqs.lua | + +### self_statm 表 +统计监控进程的statm信息 + +| 指标名 | 单位 | 标签说明 | 备注 | 源码路径 | +| :--- | --- | :---- | :---- | :--- | +| size | Page | total program size | | collector/proc\_statm.lua | +| resident | Page | resident set size | | collector/proc\_statm.lua | +| shared | Page | number of resident shared pages | | collector/proc\_statm.lua | +| text | Page | text (code) | | collector/proc\_statm.lua | +| lib | Page | library | | collector/proc\_statm.lua | +| data | Page | data + stack | | collector/proc\_statm.lua | +| dt | Page | dirty pages | | collector/proc\_statm.lua | + ## 网络指标 ----------- @@ -77,20 +145,20 @@ 统计所有包状态。[参考连接](https://developer.aliyun.com/article/484451) - 指标名 | 单位 | 标签说明 | 备注 | 源码路径 | -| :--- | ---: | :---- | :---- | :--- | -| frag\_inuse | 个 | | 使用的IP段数量 | collector/proc\_sockstat.lua | -| frag\_memory | 页 | | IP段使用内存数量 | collector/proc\_sockstat.lua | +| 指标名 | 单位 | 标签说明 | 备注 | 源码路径 | +|:---------------| ---: | :---- | :---- | :--- | +| frag\_inuse | 个 | | 使用的IP段数量 | collector/proc\_sockstat.lua | +| frag\_memory | 页 | | IP段使用内存数量 | collector/proc\_sockstat.lua | | udplite\_inuse | 个 | | udplite 使用量 | collector/proc\_sockstat.lua | -| udp\_mem | 页 | | udp socket 内存使用量,含收发缓冲区队列 | collector/proc\_sockstat.lua | -| udp\_inuse | 个 | | udp 使用量 | collector/proc\_sockstat.lua | -| tcp\_mem | 页 | | udp socket 内存使用量,含收发缓冲区队列 | collector/proc\_sockstat.lua | -| tcp\_alloc | 个 | | TCP socket 申请总数 | collector/proc\_sockstat.lua | -| tcp\_tw | 个 | | TCP time wait socket 总数 | collector/proc\_sockstat.lua | -| tcp\_orphan | 个 | | TCP ophan socket 总数 | collector/proc\_sockstat.lua | -| tcp\_inuse | 个 | | TCP 常规 socket 总数 | collector/proc\_sockstat.lua | -| raw\_inuse | 个 | | raw socket 使用量 | collector/proc\_sockstat.lua | -| sockets\_used | 个 | | 总socket 使用量 | collector/proc\_sockstat.lua | +| udp\_mem | 页 | | udp socket 内存使用量,含收发缓冲区队列 | collector/proc\_sockstat.lua | +| udp\_inuse | 个 | | udp 使用量 | collector/proc\_sockstat.lua | +| tcp\_mem | 页 | | udp socket 内存使用量,含收发缓冲区队列 | collector/proc\_sockstat.lua | +| tcp\_alloc | 个 | | TCP socket 申请总数 | collector/proc\_sockstat.lua | +| tcp\_tw | 个 | | TCP time wait socket 总数 | collector/proc\_sockstat.lua | +| tcp\_orphan | 个 | | TCP ophan socket 总数 | collector/proc\_sockstat.lua | +| tcp\_inuse | 个 | | TCP 常规 socket 总数 | collector/proc\_sockstat.lua | +| raw\_inuse | 个 | | raw socket 使用量 | collector/proc\_sockstat.lua | +| sockets\_used | 个 | | 总socket 使用量 | collector/proc\_sockstat.lua | ### softnets @@ -173,3 +241,105 @@ This parser parses the stats from network devices. These stats includes events p | lib | - | library | | collector/proc\_statm.lua | | data | - | data + stack | | collector/proc\_statm.lua | | dt | - | dirty pages | | collector/proc\_statm.lua | + + +## IO指标 + +------------- + +### IOMonIndForDisksIO 表 +统计磁盘级IO信息 + +| 指标名 | 单位 | 标签说明 | 备注 | 源码路径 | +|:---------------| ---: | :---- | :---- | :--- | +| ioburstCnt | 次 | 每分钟io burst(IO压力突发增大)次数 | - | ../ioMonitor/ioMon/ioMonitorClass.py | +| iodelayCnt | 次 | 每分钟io延迟高次数 | - | ../ioMonitor/ioMon/ioMonitorClass.py | +| iohangCnt | 次 | 每分钟io hang次数 | - | ../ioMonitor/ioMon/ioMonitorClass.py | +| bps | kB | 磁盘bps | - | ../ioMonitor/ioMon/ioMonitorClass.py | +| iops | 个 | 磁盘iops | - | ../ioMonitor/ioMon/ioMonitorClass.py | +| qusize | 个 | 未完成io数 | - | ../ioMonitor/ioMon/ioMonitorClass.py | +| util | 占比 | IO繁忙度 | - | ../ioMonitor/ioMon/ioMonitorClass.py | +| await | ms | 平均每个IO的延迟 | - | ../ioMonitor/ioMon/ioMonitorClass.py | + +### IOMonIndForSystemIO 表 +统计系统IO异常 + +| 指标名 | 单位 | 标签说明 | 备注 | 源码路径 | +|:---------------| ---: | :---- | :---- | :--- | +| iowait | 占比 | 系统等待IO的占比 | - | ../ioMonitor/ioMon/ioMonitorClass.py | +| iowaithighCnt | 次 | 每分钟iowait高次数 | - | ../ioMonitor/ioMon/ioMonitorClass.py | + + +## 混部指标 + +----------- + +### cg_cpu_stat 表 +| 指标名 | 单位 | 标签说明 | 备注 | 源码路径 | +| :--- | --- | :---- | :---- | :--- | +| nr_throttled | - | total throttled number | | collector/container/cg\_cpu\_stat.lua | +| throttled_time | ms | total throttled time | | collector/container/cg\_cpu\_stat.lua | + +### cg_proc_stat 表 +| 指标名 | 单位 | 标签说明 | 备注 | 源码路径 | +| :--- | --- | :---- | :---- | :--- | +| user | % | usr cpu util | | collector/container/cg\_cpuacct\_proc\_stat.lua | +| nice | % | nice cpu util | | collector/container/cg\_cpuacct\_proc\_stat.lua | +| system | % | system cpu util | | collector/container/cg\_cpuacct\_proc\_stat.lua | +| idle | % | idl cpu util | | collector/container/cg\_cpuacct\_proc\_stat.lua | +| iowait | % | iowait cpu util | | collector/container/cg\_cpuacct\_proc\_stat.lua | +| irq | % | irq cpu util | | collector/container/cg\_cpuacct\_proc\_stat.lua | +| softirq | % | softirq cpu util | | collector/container/cg\_cpuacct\_proc\_stat.lua | +| steal | % | steal cpu util | | collector/container/cg\_cpuacct\_proc\_stat.lua | +| guest | % | guest cpu util | | collector/container/cg\_cpuacct\_proc\_stat.lua | +| load1min | - | load of 1min | | collector/container/cg\_cpuacct\_proc\_stat.lua | +| load5min | - | load of 5min | | collector/container/cg\_cpuacct\_proc\_stat.lua | +| load15min | - | load of 15min | | collector/container/cg\_cpuacct\_proc\_stat.lua | +| r_load1min | - | running load of 1min | | collector/container/cg\_cpuacct\_proc\_stat.lua | +| r_load5min | - | running load of 5min | | collector/container/cg\_cpuacct\_proc\_stat.lua | +| r_load15min | - | running load of 15min | | collector/container/cg\_cpuacct\_proc\_stat.lua | +| nr_running | - | number of runable tasks | | collector/container/cg\_cpuacct\_proc\_stat.lua | +| nr_uninterruptible | - | number of deep sleep tasks | | collector/container/cg\_cpuacct\_proc\_stat.lua | + +### cg_memfail_cnt 表 +| 指标名 | 单位 | 标签说明 | 备注 | 源码路径 | +| :--- | --- | :---- | :---- | :--- | +| fail_cnt | - | number of mem fail counts | | collector/container/cg\_memory\_fail\_cnt.lua | + +### cg_memdrcm_latency 表 +This table show the hist of the latency of direct memory reclamation +| 指标名 | 单位 | 标签说明 | 备注 | 源码路径 | +| :--- | --- | :---- | :---- | :--- | +| memDrcm_lat_1to5ms | - | times 1to5ms | | collector/container/cg\_memory\_drcm\_latency.lua | +| memDrcm_lat_5to10ms | - | times 5to10ms | | collector/container/cg\_memory\_drcm\_latency.lua | +| memDrcm_lat_10to100ms | - | times 10to100ms | | collector/container/cg\_memory\_drcm\_latency.lua | +| memDrcm_lat_100to500ms | - | times 100to500ms | | collector/container/cg\_memory\_drcm\_latency.lua | +| memDrcm_lat_500to1000ms | - | times 500msto1s | | collector/container/cg\_memory\_drcm\_latency.lua | +| memDrcm_lat_1000ms | - | times more than 1s | | collector/container/cg\_memory\_drcm\_latency.lua | + +### cg_memmcmp_latency 表 +This table show the hist of the latency of direct memory compaction +| 指标名 | 单位 | 标签说明 | 备注 | 源码路径 | +| :--- | --- | :---- | :---- | :--- | +| memDcmp_lat_1to5ms | - | times 1to5ms | | collector/container/cg\_memory\_dcmp\_latency.lua | +| memDcmp_lat_5to10ms | - | times 5to10ms | | collector/container/cg\_memory\_dcmp\_latency.lua | +| memDcmp_lat_10to100ms | - | times 10to100ms | | collector/container/cg\_memory\_dcmp\_latency.lua | +| memDcmp_lat_100to500ms | - | times 100to500ms | | collector/container/cg\_memory\_dcmp\_latency.lua | +| memDcmp_lat_500to1000ms | - | times 500msto1s | | collector/container/cg\_memory\_dcmp\_latency.lua | +| memDcmp_lat_1000ms | - | times more than 1s | | collector/container/cg\_memory\_dcmp\_latency.lua | + +### pmu_events 表 +| 指标名 | 单位 | 标签说明 | 备注 | 源码路径 | +| :--- | --- | :---- | :---- | :--- | +| cpu_cycles | - | cycles | | collector/plugin/pmu_events/pmu\_events.c | +| instructions | - | instructions | | collector/plugin/pmu_events/pmu\_events.c | +| ipc | - | instructions per cycles | | collector/plugin/pmu_events/pmu\_events.c | +| cpi | - | cycles per instructions | | collector/plugin/pmu_events/pmu\_events.c | +| llc_store_ref | - | llc stroe hits counts | | collector/plugin/pmu_events/pmu\_events.c | +| llc_store_miss | - | llc stroe miss counts | | collector/plugin/pmu_events/pmu\_events.c | +| llc_load_ref | - | llc load hits counts | | collector/plugin/pmu_events/pmu\_events.c | +| llc_load_miss | - | llc load miss counts | | collector/plugin/pmu_events/pmu\_events.c | +| llc_rmiss_rate | - | llc load miss rate | | collector/plugin/pmu_events/pmu\_events.c | +| llc_wmiss_rate | - | llc store miss rate | | collector/plugin/pmu_events/pmu\_events.c | +| llc_miss_rate | - | llc miss rate | | collector/plugin/pmu_events/pmu\_events.c | +| llc_cache_mpi | - | llc miss per instructions | | collector/plugin/pmu_events/pmu\_events.c | diff --git a/source/tools/monitor/unity/beaver/identity.lua b/source/tools/monitor/unity/beaver/identity.lua index 220b60661e47af0f4c108f0294a8480876e5ac51..483dde1fd1a6dde20eedbe6776679257d7f32655 100644 --- a/source/tools/monitor/unity/beaver/identity.lua +++ b/source/tools/monitor/unity/beaver/identity.lua @@ -7,6 +7,7 @@ require("common.class") local system = require("common.system") local socket = require("socket") +local stdlib = require("posix.stdlib") local Cidentity = class("identity") @@ -18,7 +19,8 @@ function Cidentity:_init_(fYaml) curl = function() return self:curl() end, hostname = function() return self:hostname() end, file = function() return self:file() end, - specify = function() return self:specify() end + specify = function() return self:specify() end, + env = function() return self:env() end } end @@ -77,6 +79,15 @@ function Cidentity:specify() end end +function Cidentity:env() + if self._opts.name then + local inst_name = stdlib.getenv(self._opts.name) + return inst_name + else + return "None" + end +end + function Cidentity:id() return self._funcs[self._opts.mode]() end diff --git a/source/tools/monitor/unity/beaver/localBeaver.lua b/source/tools/monitor/unity/beaver/localBeaver.lua index 748a20e14b7d6db873968c0caf6bfde113b8cc0c..fd4294bc94ca4ec6b90690db39754ff0c7441b1a 100644 --- a/source/tools/monitor/unity/beaver/localBeaver.lua +++ b/source/tools/monitor/unity/beaver/localBeaver.lua @@ -18,7 +18,7 @@ local function setupServer(fYaml) local ip = config["bind_addr"] or "0.0.0.0" local backlog = config["backlog"] or 32 local unix_socket = config["unix_socket"] - return port, ip, backlog,unix_socket + return port, ip, backlog, unix_socket end function CLocalBeaver:_init_(frame, fYaml) @@ -193,7 +193,7 @@ function CLocalBeaver:write(fd, stream) local res sent, err, errno = socket.send(fd, stream) - if sent then + if sent ~= nil then if sent < #stream then -- send buffer may full res = self._cffi.mod_fd(self._efd, fd, 1) -- epoll write ev assert(res == 0) @@ -204,6 +204,9 @@ function CLocalBeaver:write(fd, stream) return nil elseif e.ev_out then stream = string.sub(stream, sent + 1) + if stream == nil then + return 1 + end sent, err, errno = socket.send(fd, stream) if sent == nil then if errno == 11 then -- EAGAIN ? diff --git a/source/tools/monitor/unity/beaver/url_api.lua b/source/tools/monitor/unity/beaver/url_api.lua index e78df15419e3a9ef16001da7425185a5c45724d0..e03b8953d28c09f90e63f60a7cade984216a2ac5 100644 --- a/source/tools/monitor/unity/beaver/url_api.lua +++ b/source/tools/monitor/unity/beaver/url_api.lua @@ -8,6 +8,8 @@ require("common.class") local system = require("common.system") local ChttpApp = require("httplib.httpApp") local CfoxTSDB = require("tsdb.foxTSDB") +local postQue = require("beeQ.postQue.postQue") + local CurlApi = class("urlApi", ChttpApp) function CurlApi:_init_(frame, fYaml) @@ -15,10 +17,22 @@ function CurlApi:_init_(frame, fYaml) self._urlCb["/api/sum"] = function(tReq) return self:sum(tReq) end self._urlCb["/api/sub"] = function(tReq) return self:sub(tReq) end self._urlCb["/api/query"] = function(tReq) return self:query(tReq) end + self._urlCb["/api/que"] = function(tReq) return self:que(tReq) end self:_install(frame) self:_setupQs(fYaml) end +function CurlApi:que(tReq) + local stat, tJson = pcall(self.getJson, self, tReq) + if stat then + local s = self:jencode(tJson) + postQue.post(s) + return tJson + else + return {} + end +end + function CurlApi:sum(tReq) local stat, tJson = pcall(self.getJson, self, tReq) if stat then diff --git a/source/tools/monitor/unity/beeQ/Makefile b/source/tools/monitor/unity/beeQ/Makefile index c2360532a31a0fb5b93076d109a53a3a8fc00dd2..aadc6153a55cdfc78f863b983d381050f3f4c6ee 100644 --- a/source/tools/monitor/unity/beeQ/Makefile +++ b/source/tools/monitor/unity/beeQ/Makefile @@ -2,11 +2,11 @@ LIB= -lpthread -ldl CC=gcc CFLAG := -g -I../beaver -I../collector/outline -LDFLAG := -g -lm -ldl -lrt -lpthread -lluajit-5.1 -L./lib/ -lbeeQ -L../beaver -lbeaver -lcollectorApi -L../collector/outline/ -loutline -L../collector/plugin/ -lproto_sender +LDFLAG := -g -lm -ldl -lrt -lpthread -lluajit-5.1 -L./lib/ -lbeeQ -L../beaver -lbeaver -lcollectorApi -Lclock/ -llocalclock -LpostQue -lpostQue -L../collector/outline/ -loutline -L../collector/plugin/ -lproto_sender PRG=unity-mon -OBJ=apps.o bees.o -DEPMOD=lib rbtree ../beaver ../collector/native ../collector/interface ../collector/outline ../collector/plugin ../tsdb/native +OBJ=apps.o bees.o daemon.o +DEPMOD=lib rbtree clock postQue ../beaver ../collector/native ../collector/interface ../collector/outline ../collector/plugin ../tsdb/native ../collector/container/cg_pmu_events_ffi $(PRG): $(DEPMOD) $(OBJ) $(CC) $(LIB) -o $@ $(OBJ) $(LDFLAG) diff --git a/source/tools/monitor/unity/beeQ/apps.c b/source/tools/monitor/unity/beeQ/apps.c index e1ea2932aadf36ba2a736813e278fb6f230ed860..40123ec0a45b3a632bba2a89c9bf99d2118b50c2 100644 --- a/source/tools/monitor/unity/beeQ/apps.c +++ b/source/tools/monitor/unity/beeQ/apps.c @@ -9,7 +9,10 @@ #include #include "beeQ.h" #include "apps.h" +#include "clock/ee_clock.h" +#include "daemon.h" #include +#include #define gettidv1() syscall(__NR_gettid) extern char *g_yaml_file; @@ -126,9 +129,11 @@ static lua_State * app_recv_init(void) { return NULL; } +// 这是接收队列初始化动作 int app_recv_setup(struct beeQ* q) { lua_State *L; + prctl(PR_SET_NAME, (unsigned long)"app_recv"); L = app_recv_init(); if (L == NULL) { return -1; @@ -205,6 +210,20 @@ int app_recv_proc(void* msg, struct beeQ* q) { return ret; } +static int lua_local_clock(lua_State *L) { + clock_t t = get_local_clock(); + lua_pushnumber(L, t); + return 1; +} + +static int lua_setup_daemon(lua_State *L) { + int fd; + int period = lua_tonumber(L, 1); + fd = setup_daemon(period); + lua_pushnumber(L, fd); + return 1; +} + int collector_qout(lua_State *L) { int ret; struct beeQ* q = (struct beeQ*) lua_topointer(L, 1); @@ -225,11 +244,14 @@ int collector_qout(lua_State *L) { return 1; // return a value. } +// 这是 collector 初始化操作 static int app_collector_work(void* q, void* proto_q) { int ret; int err_func; lua_Number lret; + prctl(PR_SET_NAME, (unsigned long)"app_collector"); + /* create a state and load standard library. */ lua_State *L = luaL_newstate(); if (L == NULL) { @@ -245,13 +267,16 @@ static int app_collector_work(void* q, void* proto_q) { } lua_register(L, "collector_qout", collector_qout); + lua_register(L, "lua_local_clock", lua_local_clock); + lua_register(L, "lua_setup_daemon", lua_setup_daemon); // call init. lua_getglobal(L, "work"); lua_pushlightuserdata(L, q); lua_pushlightuserdata(L, proto_q); lua_pushstring(L, g_yaml_file); - ret = lua_pcall(L, 3, 1, err_func); + lua_pushinteger(L, (int)gettidv1()); + ret = lua_pcall(L, 4, 1, err_func); if (ret < 0) { lua_check_ret(ret); goto endCall; @@ -281,6 +306,7 @@ static int app_collector_work(void* q, void* proto_q) { return -1; } + int app_collector_run(struct beeQ* q, void* arg) { int ret = 0; struct beeQ* proto_que = (struct beeQ* )arg; @@ -289,6 +315,7 @@ int app_collector_run(struct beeQ* q, void* arg) { ret = app_collector_work(q, proto_que); if (ret < 0) { perror("collect work run failed."); + exit(1); break; } } diff --git a/source/tools/monitor/unity/beeQ/bees.c b/source/tools/monitor/unity/beeQ/bees.c index f6174e49c4641bb0fb49da394a16ec38351d0f7e..89f3cc554396daf45992a387fc9c346054c39994 100644 --- a/source/tools/monitor/unity/beeQ/bees.c +++ b/source/tools/monitor/unity/beeQ/bees.c @@ -9,6 +9,8 @@ #include #include #include +#include "clock/ee_clock.h" +#include "postQue/postQue.h" #define RUN_THREAD_MAX 8 #define RUN_QUEUE_SIZE 32 @@ -35,11 +37,13 @@ void sig_handler(int num) } } +char ** entry_argv; // for daemon process extern struct beeQ* proto_sender_init(struct beeQ* pushQ); int main(int argc, char *argv[]) { struct beeQ* q; //for proto-buf stream struct beeQ* proto_que; //for trans c to proto-buf stream + entry_argv = argv; if (argc > 1) { g_yaml_file = argv[1]; } @@ -48,6 +52,13 @@ int main(int argc, char *argv[]) { signal(SIGUSR1, sig_handler); signal(SIGINT, sig_handler); + if (calibrate_local_clock() < 0) { + printf("calibrate_local_clock failed.\n"); + exit(1); + } + + postQue_init(); + q = beeQ_init(RUN_QUEUE_SIZE, app_recv_setup, app_recv_proc, NULL); diff --git a/source/tools/monitor/unity/beeQ/clock/Makefile b/source/tools/monitor/unity/beeQ/clock/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..f12bd8983dcf36b35b5c27adbf5aaeff391d7f0a --- /dev/null +++ b/source/tools/monitor/unity/beeQ/clock/Makefile @@ -0,0 +1,17 @@ +CC := gcc +AR := ar +CFLAG := -g +OBJS := ee_clock.o +LIB := liblocalclock.a + +all: $(LIB) + + +%.o: %.c, %.h + $(CC) -c $< -o $@ $(CFLAG) + +$(LIB): $(OBJS) + $(AR) cr $@ $(OBJS) + +clean: + rm -f $(LIB) $(OBJS) \ No newline at end of file diff --git a/source/tools/monitor/unity/beeQ/clock/ee_clock.c b/source/tools/monitor/unity/beeQ/clock/ee_clock.c new file mode 100644 index 0000000000000000000000000000000000000000..3ccebd204062d102d3aef1bb3bb337d505d52d19 --- /dev/null +++ b/source/tools/monitor/unity/beeQ/clock/ee_clock.c @@ -0,0 +1,59 @@ +// +// 高效时钟获取方案 +// Created by 廖肇燕 on 2023/3/17. +// + +#include "ee_clock.h" +#include +#include +#include + +#define TIME_SECOND_UNIT 100000UL // 睡眠校准时间, + +static ee_clock_t clk_coef = 0; + +static ee_clock_t get_cycles() { + unsigned int hi, lo; + ee_clock_t res; + __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi)); + res = ((ee_clock_t)lo) | (((ee_clock_t)hi) << 32); + return res; +} + +// 校准时钟 +int calibrate_local_clock(){ + ee_clock_t coef1, coef2; + ee_clock_t t1, t2, t3; + ee_clock_t delta1, delta2; + ee_clock_t res; + + t1 = get_cycles(); + usleep(TIME_SECOND_UNIT); + t2 = get_cycles(); + usleep(TIME_SECOND_UNIT); + t3 = get_cycles(); + + delta1 = t2 - t1; + delta2 = t3 - t2; + + coef1 = delta1 / TIME_SECOND_UNIT; + coef2 = delta2 / TIME_SECOND_UNIT; + + if (coef1 <= 100 || coef2 <= 100) { + fprintf(stderr, "read clock too small.\n"); + return -EIO; + } + + res = 100 * coef1 / coef2; + if (res >= 110 || res <= 90) { + fprintf(stderr, "calibrate local clock failed.\n"); + return -EIO; + } + + clk_coef = (coef1 + coef2) / 2; + return 0; +} + +ee_clock_t get_local_clock() { + return get_cycles() / clk_coef; +} diff --git a/source/tools/monitor/unity/beeQ/clock/ee_clock.h b/source/tools/monitor/unity/beeQ/clock/ee_clock.h new file mode 100644 index 0000000000000000000000000000000000000000..dbe68e3a3f30b9c7a2ff510c116852a45d118551 --- /dev/null +++ b/source/tools/monitor/unity/beeQ/clock/ee_clock.h @@ -0,0 +1,14 @@ +// +// 高效时钟获取方案 +// Created by 廖肇燕 on 2023/3/17. +// + +#ifndef UNITY_EE_CLOCK_H +#define UNITY_EE_CLOCK_H + +typedef unsigned long ee_clock_t; + +int calibrate_local_clock(); +ee_clock_t get_local_clock(); + +#endif //UNITY_EE_CLOCK_H diff --git a/source/tools/monitor/unity/beeQ/collectors.lua b/source/tools/monitor/unity/beeQ/collectors.lua index e92a06f58f49a9eae6fe207bdadcb433f183d16c..9549bd592d266d1b1ff7cf4563d52c1332e97b6e 100644 --- a/source/tools/monitor/unity/beeQ/collectors.lua +++ b/source/tools/monitor/unity/beeQ/collectors.lua @@ -3,6 +3,7 @@ --- Created by liaozhaoyan. --- DateTime: 2022/12/26 11:26 PM --- +package.cpath = package.cpath .. ";../lib/?.so" package.path = package.path .. ";../?.lua;" local dirent = require("posix.dirent") @@ -10,7 +11,7 @@ local unistd = require("posix.unistd") local stat = require("posix.sys.stat") local bit = require("bit") local system = require("common.system") -local ptime = require("posix.time") +local CrbEvent = require("beeQ.rbtree.rbEvent") local srcPath = "../collector/native/" local dstPath = "../collector/lib/" @@ -107,34 +108,29 @@ local function setupFreq(fYaml) end end -local function calcSleep(hope, now) - if hope.tv_nsec >= now.tv_nsec then - return {tv_sec = hope.tv_sec - now.tv_sec, - tv_nsec = hope.tv_nsec - now.tv_nsec} - else - return {tv_sec = hope.tv_sec - now.tv_sec - 1, - tv_nsec = 1e9 + hope.tv_nsec - now.tv_nsec} - end +local function setupMainCollector(que, proto_q, fYaml, tid) + local Cloop = require("collector.loop") + local w = Cloop.new(que, proto_q, fYaml, tid) + local unit = setupFreq(fYaml) + return w, unit +end + +local function setupPostEngine(que, proto_q, fYaml, tid) + local Cengine = require("collector.postEngine.engine") + local w = Cengine.new(que, proto_q, fYaml, tid) + return w, 1 end -function work(que, proto_q, yaml) +function work(que, proto_q, yaml, tid) local fYaml = yaml or "../collector/plugin.yaml" checkSos() - local Cloop = require("collector.loop") - local w = Cloop.new(que, proto_q, fYaml) - local unit = setupFreq(fYaml) - local tStart = ptime.clock_gettime(ptime.CLOCK_MONOTONIC) - while true do - w:work(unit) - local now = ptime.clock_gettime(ptime.CLOCK_MONOTONIC) - local hope = {tv_sec = tStart.tv_sec + unit, tv_nsec = tStart.tv_sec} - local diff = calcSleep(hope, now) - assert(diff.tv_sec >= 0) - local _, s, errno, _ = ptime.nanosleep(diff) - if errno then -- interrupt by signal - print(string.format("new sleep stop. %d, %s", errno, s)) - return 0 - end - tStart = hope - end + local e = CrbEvent.new() + + local main, engine, unit + main, unit = setupMainCollector(que, proto_q, fYaml, tid) + e:addEvent("mainCollector", main, unit) + engine, unit = setupPostEngine(que, proto_q, fYaml, tid) + engine:setTask(main.postPlugin.tasks) + e:addEvent("postEngine", engine, unit) + return e:proc() end diff --git a/source/tools/monitor/unity/beeQ/daemon.c b/source/tools/monitor/unity/beeQ/daemon.c new file mode 100644 index 0000000000000000000000000000000000000000..c8339ee0170daf0d73bbccc0b6f138ad1548d11f --- /dev/null +++ b/source/tools/monitor/unity/beeQ/daemon.c @@ -0,0 +1,171 @@ +// +// Created by 廖肇燕 on 2023/3/21. +// + +#include "daemon.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DAEMON_NAME "daemon" +#define DAEMON_NAME_SIZE sizeof(DAEMON_NAME) +#define DAEMON_PIPE_SIZE 4096 + +#define TASK_NAME "unity-mon" +#define TASK_NAME_SIZE sizeof(TASK_NAME) + +extern char** entry_argv; + +static int init_daemon(int fd) { + pid_t pid; + char *s; + + pid = fork(); + if (pid < 0) { + perror("fork error!"); + exit(1); + } + else if (pid > 0) { + exit(0); + } + + // mask signal. + signal(SIGTTOU, SIG_IGN); + signal(SIGTTIN, SIG_IGN); + signal(SIGTSTP, SIG_IGN); + signal(SIGHUP, SIG_IGN); + + // create new pid group, leave to old pid groups. + setsid(); + + // fork a new child process + pid = fork(); + if( pid > 0) { + exit(0); + } + else if (pid< 0) { + exit(1); + } + + // change process name. + s = entry_argv[0]; + strcpy(s, DAEMON_NAME); + s[DAEMON_NAME_SIZE] = '\0'; + + // close all no use fd. + int i; + for (i = 0; i < NOFILE; i ++){ + if (i != fd) { + close(i); + } + } + + // change work dir. ignore all child signal and file mask + chdir("/"); + umask(0); + signal(SIGCHLD, SIG_IGN); + return 0; +} + +static int kill_mon(pid_t mon) { + int fd; + char path[64]; + char buffer[64]; + snprintf(path, 64, "/proc/%d/cmdline", mon); + + if (access(path, F_OK) < 0) { + return errno; + } + + fd = open(path, O_RDONLY); + if (fd < 0) { + return errno; + } + read(fd, buffer, 64); + close(fd); + if (strstr(buffer, TASK_NAME) != NULL) { + return kill(mon, SIGKILL); + } + return 0; +} + +static int loop_daemon(pid_t mon, int period, int fd) { + int ret; + char buff[DAEMON_PIPE_SIZE]; + fd_set read_fds; //读文件操作符 + fd_set except_fds; + struct timeval tv; + + FD_ZERO(&read_fds); + FD_ZERO(&except_fds); + + init_daemon(fd); + while (1) { + FD_SET(fd, &read_fds); + FD_SET(fd, &except_fds); + tv.tv_sec = period * 2; + tv.tv_usec = 0; + + ret = select(fd + 1, &read_fds, NULL, &except_fds, &tv); + + if (ret == 0) { + syslog(LOG_NOTICE, "daemon: select time out\n"); + kill_mon(mon); + return 0; + } else if ( ret == -1) { + syslog(LOG_NOTICE, "daemon: select return %d\n", errno); + kill_mon(mon); + return -errno; + } else { + ret = read(fd, buff, DAEMON_PIPE_SIZE); + if (ret <= 0) { + kill_mon(mon); + return 0; + } else if (strncmp(buff, "stop", 4) == 0) { + exit(0); + } + } + } +} + +static int fork_daemon(int period, int fd0, int fd1) { + pid_t pid; + pid_t mon = getpid(); + + pid = fork(); + if (pid < 0) { + perror("fork failed"); + exit(1); + } + if (pid == 0) { + close(fd1); + loop_daemon(mon, period, fd0); + exit(0); + } else { + wait(NULL); + } + close(fd0); + return fd1; +} + +int setup_daemon(int period) { + int pipefd[2]; + + if(pipe(pipefd) < 0) { + perror("create pipe failed."); + exit(1); + } + + return fork_daemon(period, pipefd[0], pipefd[1]); +} diff --git a/source/tools/monitor/unity/beeQ/daemon.h b/source/tools/monitor/unity/beeQ/daemon.h new file mode 100644 index 0000000000000000000000000000000000000000..d5255a55dfa53d5f4a0d733eaa33cac48aee605f --- /dev/null +++ b/source/tools/monitor/unity/beeQ/daemon.h @@ -0,0 +1,10 @@ +// +// Created by 廖肇燕 on 2023/3/21. +// + +#ifndef UNITY_DAEMON_H +#define UNITY_DAEMON_H + +int setup_daemon(int period); + +#endif //UNITY_DAEMON_H diff --git a/source/tools/monitor/unity/beeQ/pack.sh b/source/tools/monitor/unity/beeQ/pack.sh index 8b67eb5b27edde594401459c1807865cece862ea..570e92f39da728c533a9627170e76913451d9eae 100755 --- a/source/tools/monitor/unity/beeQ/pack.sh +++ b/source/tools/monitor/unity/beeQ/pack.sh @@ -30,20 +30,35 @@ cp beaver/native/*.lua ${APP}/beaver/native mkdir ${APP}/beeQ/ mkdir ${APP}/beeQ/lib +mkdir ${APP}/beeQ/postQue +mkdir ${APP}/beeQ/rbtree cp beeQ/lib/*.so* ${APP}/beeQ/lib/ cp beeQ/*.lua ${APP}/beeQ/ +cp beeQ/postQue/*.lua ${APP}/beeQ/postQue/ +cp beeQ/rbtree/*.lua ${APP}/beeQ/rbtree/ cp beeQ/unity-mon ${APP}/beeQ/ cp beeQ/run.sh ${APP}/beeQ/ mkdir ${APP}/collector mkdir ${APP}/collector/native +mkdir ${APP}/collector/guard mkdir ${APP}/collector/outline +mkdir ${APP}/collector/container +mkdir ${APP}/collector/postEngine +mkdir ${APP}/collector/postPlugin +mkdir ${APP}/collector/podMan cp collector/native/*.so* ${APP}/collector/native/ cp collector/native/*.lua ${APP}/collector/native/ cp collector/*.lua ${APP}/collector/ +cp collector/guard/*.lua ${APP}/collector/guard cp collector/outline/*.lua ${APP}/collector/outline +cp collector/container/*.lua ${APP}/collector/container/ +cp collector/postEngine/*.lua ${APP}/collector/postEngine +cp collector/postPlugin/*.lua ${APP}/collector/postPlugin +cp collector/podMan/*.lua ${APP}/collector/podMan cp collector/plugin.yaml ${APP}/collector/ + mkdir ${APP}/common cp common/*.lua ${APP}/common/ @@ -55,5 +70,5 @@ mkdir ${APP}/tsdb/native cp tsdb/native/*.so* ${APP}/tsdb/native/ cp tsdb/native/*.lua ${APP}/tsdb/native/ cp tsdb/*.lua ${APP}/tsdb - +cp /usr/local/lib/lua/5.1/* -R ${DIST}/lib/ #tar zcv -f dist.tar.gz $DIST/ diff --git a/source/tools/monitor/unity/beeQ/postQue/Makefile b/source/tools/monitor/unity/beeQ/postQue/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..183f2df631cb6c985801263f27b967f31932624e --- /dev/null +++ b/source/tools/monitor/unity/beeQ/postQue/Makefile @@ -0,0 +1,19 @@ +CC := gcc +CFLAG := -g -fpic +LDFLAG := -g -fpic -shared +OBJS := postQue.o +SO := libpostQue.so + +all: $(SO) install + +%.o: %.c %.h + $(CC) -c $< -o $@ $(CFLAG) + +$(SO): $(OBJS) + $(CC) -o $@ $(OBJS) $(LDFLAG) + +install: $(SO) + cp $(SO) ../lib + +clean: + rm -f *.so $(OBJS) diff --git a/source/tools/monitor/unity/beeQ/postQue/postQue.c b/source/tools/monitor/unity/beeQ/postQue/postQue.c new file mode 100644 index 0000000000000000000000000000000000000000..b9c0c7d2d297e714255856cb5b8521e84145cf77 --- /dev/null +++ b/source/tools/monitor/unity/beeQ/postQue/postQue.c @@ -0,0 +1,63 @@ +// +// Created by 廖肇燕 on 2023/3/24. +// + +#include "postQue.h" +#include +#include + +#define UNITY_POSTQUE_NUM 4 +#define UNITY_POSTQUE_MSG_SIZE 128 + +// post que for out post +struct unity_postQue { + int num; + pthread_mutex_t mtx; + char msgs[UNITY_POSTQUE_NUM][UNITY_POSTQUE_MSG_SIZE]; +}; + +static struct unity_postQue que; + +int postQue_pull(char *msg) { + int ret; + int i; + + pthread_mutex_lock(&que.mtx); + ret = que.num; + for (i = 0; i < ret; i ++) { + strcat(msg, que.msgs[i]); + strcat(msg, "\n"); + } + que.num = 0; + pthread_mutex_unlock(&que.mtx); + if (ret) { // strip last \n + int len = strlen(msg); + msg[len - 1] = '\0'; + } + return ret; +} + +// post a message +int postQue_post(const char *msg) { + int ret = 0; + int len = strlen(msg); + if (len >= UNITY_POSTQUE_MSG_SIZE) { + return -EINVAL; + } + + pthread_mutex_lock(&que.mtx); + if (que.num < UNITY_POSTQUE_NUM) { + strcpy(que.msgs[que.num], msg); + que.num ++; + } else { + ret = -EAGAIN; + } + pthread_mutex_unlock(&que.mtx); + return ret; +} + +int postQue_init() { + memset(&que, 0, sizeof (struct unity_postQue)); + pthread_mutex_init(&que.mtx, NULL); + return 0; +} diff --git a/source/tools/monitor/unity/beeQ/postQue/postQue.h b/source/tools/monitor/unity/beeQ/postQue/postQue.h new file mode 100644 index 0000000000000000000000000000000000000000..070325e54fec6711f41163a24b394b7aba85615d --- /dev/null +++ b/source/tools/monitor/unity/beeQ/postQue/postQue.h @@ -0,0 +1,16 @@ +// +// Created by 廖肇燕 on 2023/3/24. +// + +#ifndef UNITY_POSTQUE_H +#define UNITY_POSTQUE_H + +#include +#include +#include + +int postQue_pull(char *msg); +int postQue_post(const char *msg); +int postQue_init(); + +#endif //UNITY_POSTQUE_H diff --git a/source/tools/monitor/unity/beeQ/postQue/postQue.lua b/source/tools/monitor/unity/beeQ/postQue/postQue.lua new file mode 100644 index 0000000000000000000000000000000000000000..becbf24d0055dcd24762ea44e074141f919b3583 --- /dev/null +++ b/source/tools/monitor/unity/beeQ/postQue/postQue.lua @@ -0,0 +1,30 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/3/24 11:13 PM +--- + +local mod = {} +local ffi = require("ffi") + +ffi.cdef [[ +int postQue_pull(char *msg); +int postQue_post(const char *msg); +]] + +local cffi = ffi.load("postQue") + +function mod.pull() + local s = ffi.new("char[?]", 1024) + local ret = cffi.postQue_pull(s) + if ret > 0 then + return ffi.string(s) + end + return nil +end + +function mod.post(msg) + return cffi.postQue_post(msg) +end + +return mod \ No newline at end of file diff --git a/source/tools/monitor/unity/beeQ/rbtree/Makefile b/source/tools/monitor/unity/beeQ/rbtree/Makefile index 45d070999b45a269bf99aab7d46da95acba65897..7b5383ae15e88dc9744c9a33ee5de02ca5235ed0 100644 --- a/source/tools/monitor/unity/beeQ/rbtree/Makefile +++ b/source/tools/monitor/unity/beeQ/rbtree/Makefile @@ -4,7 +4,7 @@ LDFLAG := -g -fpic -shared OBJS := rbtree.o lrbtree.o SO = lrbtree.so -all: $(SO) install +all: install %.o: %.c $(CC) -c $< -o $@ $(CFLAG) @@ -12,7 +12,7 @@ all: $(SO) install $(SO): $(OBJS) $(CC) -o $@ $(OBJS) $(LDFLAG) -install: +install: $(SO) cp $(SO) ../lib clean: diff --git a/source/tools/monitor/unity/beeQ/rbtree/lrbtree.c b/source/tools/monitor/unity/beeQ/rbtree/lrbtree.c index dd9597402bcff6652b7bce4188643f093b5f3b4c..b218453665959b7d78cdc6e70ba8a4dba62b560f 100644 --- a/source/tools/monitor/unity/beeQ/rbtree/lrbtree.c +++ b/source/tools/monitor/unity/beeQ/rbtree/lrbtree.c @@ -4,6 +4,7 @@ Author: turbobhh Mail: turbobhh@gmail.com CreatedTime: Tue 25 Apr 2017 03:09:31 PM CST ************************************************************************/ + #include #include #include diff --git a/source/tools/monitor/unity/common/rbEvent.lua b/source/tools/monitor/unity/beeQ/rbtree/rbEvent.lua similarity index 74% rename from source/tools/monitor/unity/common/rbEvent.lua rename to source/tools/monitor/unity/beeQ/rbtree/rbEvent.lua index ac823e6e90ad8ed58a6204c02ca1da0d29084a14..bb904d01a6a556c8d42e460d9ebb178e847d613a 100644 --- a/source/tools/monitor/unity/common/rbEvent.lua +++ b/source/tools/monitor/unity/beeQ/rbtree/rbEvent.lua @@ -36,8 +36,7 @@ function CrbEvent:_init_() self._nsec = timeNsec() end -function CrbEvent:addEvent(e, period, start, loop) - start = start or false +function CrbEvent:addEvent(name, obj, period, delay, loop) loop = loop or -1 -- -1: 会永远增加下去,大于1 则会递减,减少0 不再使用 if loop == 0 then @@ -45,12 +44,14 @@ function CrbEvent:addEvent(e, period, start, loop) end local beg = os.time() - if not start then + if delay then beg = beg + period + elseif loop > 0 then loop = loop - 1 end local node = { - e = e, + name = name, + obj = obj, t = beg, period = period, loop = loop, @@ -58,9 +59,13 @@ function CrbEvent:addEvent(e, period, start, loop) self._tree:insert(node) end -function CrbEvent:_proc(node) - print(node.e) - if node.loop ~= 0 then -- add to tail. +function CrbEvent:_proc(now, node) + --print(now .. ": node " .. node.name .. " work") + local ret + if node.obj.work then + ret = node.obj:work(node.period, self) + end + if node.loop ~= 0 and ret ~= -1 then -- add to tail. node.t = node.t + node.period self._tree:insert(node) if node.loop > 0 then @@ -86,16 +91,18 @@ function CrbEvent:proc() while node.t <= now do -- 到了预期时间 self._tree:delete(node) - self:_proc(node) + self:_proc(now, node) node = self._tree:first() end tHope = {tv_sec = tStart.tv_sec + node.t - now, tv_nsec = tStart.tv_sec} diff = calcSleep(tHope, tStart) - local _, s, errno, _ = ptime.nanosleep(diff) - if errno then -- interrupt by signal - print(string.format("new sleep stop. %d, %s", errno, s)) - return 0 + if diff.tv_sec >= 0 then + local _, s, errno, _ = ptime.nanosleep(diff) + if errno then -- interrupt by signal + print(string.format("new sleep stop. %d, %s", errno, s)) + return 0 + end end end end diff --git a/source/tools/monitor/unity/beeQ/rbtree/rbtree.c b/source/tools/monitor/unity/beeQ/rbtree/rbtree.c index 8ac94895c6b44492138d366de4e8ef916e7e167d..f536e2c34c64163945a141cd83c02d8ab52e6c78 100644 --- a/source/tools/monitor/unity/beeQ/rbtree/rbtree.c +++ b/source/tools/monitor/unity/beeQ/rbtree/rbtree.c @@ -1,21 +1,3 @@ -/* - Red Black Trees - (C) 1999 Andrea Arcangeli - (C) 2002 David Woodhouse - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - linux/lib/rbtree.c -*/ - #include "rbtree.h" static void __rb_rotate_left(struct rb_node *node, struct rb_root *root) diff --git a/source/tools/monitor/unity/beeQ/rbtree/rbtree.h b/source/tools/monitor/unity/beeQ/rbtree/rbtree.h index 346ec6a7446e31244da9dce8b462abd34e9461c3..8e4e5a9c412a31db35eb7088536ef167015f0381 100644 --- a/source/tools/monitor/unity/beeQ/rbtree/rbtree.h +++ b/source/tools/monitor/unity/beeQ/rbtree/rbtree.h @@ -1,83 +1,5 @@ -/* - Red Black Trees - (C) 1999 Andrea Arcangeli - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - linux/include/linux/rbtree.h - To use rbtrees you'll have to implement your own insert and search cores. - This will avoid us to use callbacks and to drop drammatically performances. - I know it's not the cleaner way, but in C (not in C++) to get - performances and genericity... - Some example of insert and search follows here. The search is a plain - normal search over an ordered tree. The insert instead must be implemented - in two steps: First, the code must insert the element in order as a red leaf - in the tree, and then the support library function rb_insert_color() must - be called. Such function will do the not trivial work to rebalance the - rbtree, if necessary. ------------------------------------------------------------------------ -static inline struct page * rb_search_page_cache(struct inode * inode, - unsigned long offset) -{ - struct rb_node * n = inode->i_rb_page_cache.rb_node; - struct page * page; - while (n) - { - page = rb_entry(n, struct page, rb_page_cache); - if (offset < page->offset) - n = n->rb_left; - else if (offset > page->offset) - n = n->rb_right; - else - return page; - } - return NULL; -} -static inline struct page * __rb_insert_page_cache(struct inode * inode, - unsigned long offset, - struct rb_node * node) -{ - struct rb_node ** p = &inode->i_rb_page_cache.rb_node; - struct rb_node * parent = NULL; - struct page * page; - while (*p) - { - parent = *p; - page = rb_entry(parent, struct page, rb_page_cache); - if (offset < page->offset) - p = &(*p)->rb_left; - else if (offset > page->offset) - p = &(*p)->rb_right; - else - return page; - } - rb_link_node(node, parent, p); - return NULL; -} -static inline struct page * rb_insert_page_cache(struct inode * inode, - unsigned long offset, - struct rb_node * node) -{ - struct page * ret; - if ((ret = __rb_insert_page_cache(inode, offset, node))) - goto out; - rb_insert_color(node, &inode->i_rb_page_cache); - out: - return ret; -} ------------------------------------------------------------------------ -*/ - -#ifndef _LINUX_RBTREE_H -#define _LINUX_RBTREE_H +#ifndef _RBTREE_H +#define _RBTREE_H #if defined(container_of) #undef container_of @@ -181,4 +103,4 @@ static inline void rb_link_node(struct rb_node * node, struct rb_node * parent, *rb_link = node; } -#endif /* _LINUX_RBTREE_H */ +#endif /* _RBTREE_H */ diff --git a/source/tools/monitor/unity/beeQ/run.sh b/source/tools/monitor/unity/beeQ/run.sh index 9c23dcdd510933a747a26ef07e111ea320f80d48..23bf470225248e97a88bd1c2036a6b5b92cd68e2 100755 --- a/source/tools/monitor/unity/beeQ/run.sh +++ b/source/tools/monitor/unity/beeQ/run.sh @@ -7,7 +7,7 @@ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:../beaver/ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:../../install/ export LUA_PATH="../../lua/?.lua;../../lua/?/init.lua;" -export LUA_CPATH="../../lib/?.so;../../lib/loadall.so;" +export LUA_CPATH="./lib/?.so;../../lib/?.so;../../lib/loadall.so;" yaml_path=$1 [ ! $yaml_path ] && yaml_path="/etc/sysak/plugin.yaml" diff --git a/source/tools/monitor/unity/collector/container/cg_cpu_stat.lua b/source/tools/monitor/unity/collector/container/cg_cpu_stat.lua new file mode 100644 index 0000000000000000000000000000000000000000..42bbaa75802a8e1878bb5ed3589a4226835afa8b --- /dev/null +++ b/source/tools/monitor/unity/collector/container/cg_cpu_stat.lua @@ -0,0 +1,40 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/3/8 2:32 PM +--- + +require("common.class") +local pystring = require("common.pystring") +local CvProc = require("collector.vproc") +local root = "sys/fs/cgroup/cpu/" +local dfile = "/cpu.stat" + +local cgCpuStat = class("cg_cpu_stat", CvProc) + +function cgCpuStat:_init_(proto, pffi, mnt, path, ls) + CvProc._init_(self, proto, pffi, mnt, root .. path .. dfile) + self.ls = ls +end + +function cgCpuStat:proc(elapsed, lines) + local c = 1 + CvProc.proc(self) + local values = {} + + for line in io.lines(self.pFile) do + local cell = pystring:split(line) + values[c] = { + name = cell[1], + value = tonumber(cell[2]) + } + c = c + 1 + if c > 3 then + break + end + end + self:appendLine(self:_packProto("cg_cpu_stat", self.ls, values)) + self:push(lines) +end + +return cgCpuStat diff --git a/source/tools/monitor/unity/collector/container/cg_cpu_stat_sample.lua b/source/tools/monitor/unity/collector/container/cg_cpu_stat_sample.lua new file mode 100644 index 0000000000000000000000000000000000000000..2261b4f219cc6670418dcdeee45e9ae872fcd0ed --- /dev/null +++ b/source/tools/monitor/unity/collector/container/cg_cpu_stat_sample.lua @@ -0,0 +1,40 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/3/29 12:54 AM +--- + +require("common.class") +local pystring = require("common.pystring") +local CvProc = require("collector.vproc") +local root = "sys/fs/cgroup/cpu/" +local dfile = "/cpu.stat" + +local cgCpuStatSample = class("cg_cpu_stat_sample", CvProc) + +function cgCpuStatSample:_init_(proto, pffi, mnt, path, ls) + CvProc._init_(self, proto, pffi, mnt, root .. path .. dfile) + self.ls = ls +end + +function cgCpuStatSample:proc(elapsed, lines) + local c = 1 + CvProc.proc(self) + local values = {} + + for line in io.lines(self.pFile) do + local cell = pystring:split(line) + values[c] = { + name = cell[1], + value = tonumber(cell[2]) + } + c = c + 1 + if c > 3 then + break + end + end + self:appendLine(self:_packProto("cg_cpu_stat", self.ls, values)) + self:push(lines) +end + +return cgCpuStatSample \ No newline at end of file diff --git a/source/tools/monitor/unity/collector/container/cg_cpuacct_proc_stat.lua b/source/tools/monitor/unity/collector/container/cg_cpuacct_proc_stat.lua new file mode 100644 index 0000000000000000000000000000000000000000..9470256dc97b08b7d259bf90cd4124ed2150bd02 --- /dev/null +++ b/source/tools/monitor/unity/collector/container/cg_cpuacct_proc_stat.lua @@ -0,0 +1,118 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/3/8 2:32 PM +--- + +require("common.class") +local pystring = require("common.pystring") +local CvProc = require("collector.vproc") +local root = "sys/fs/cgroup/cpuacct/" +local dfile = "/cpuacct.proc_stat" +local procstat = "proc/stat" +local system = require("common.system") + +local CgProcStat = class("cg_proc_stat", CvProc) + +--ls{}, (pod_name and docker_name +function CgProcStat:_init_(proto, pffi, mnt, path, ls) + CvProc._init_(self, proto, pffi, mnt, root .. path .. dfile) + self.ls = ls + self.path = mnt..root..path..dfile + self.statpath = mnt..procstat + self.hostPrev = 0 + self.hostNow = 0 + self.hostCpuSum = 0 + self.conTotal = 0 + self.values = {0,0,0,0,0,0,0,0,0} +end + +function CgProcStat:_getTotal_() + local pfile = io.open(self.statpath, "r") + local line = pfile:read() + local sum = 0 + local s = string.sub(line, 4) + local data = self._ffi.new("var_long_t") + assert(self._cffi.var_input_long(self._ffi.string(s), data) == 0) + assert(data.no == 10) + for i = 1, data.no do + sum = sum + data.value[i] + end + self.hostPrev = self.hostNow + self.hostNow = sum + self.hostCpuSum = sum - self.hostPrev + io.close(pfile) +end + +function CgProcStat:proc(elapsed, lines) + -- if pFile not valid ,return -1 +--[[ local ftype = io.type(self.pFile) + + if ftype == nil then + print "type is nil" + return + else + print (io.type(self.pFile)) + return + end + if ftype ~= "file" then + return + end +--]] + local c = 1 + CvProc.proc(self) + self:_getTotal_() + local values = {} + self.conTotal = 0 + for line in io.lines(self.pFile) do + local tmp + local cell = pystring:split(line) + local num = #cell + if c < 10 then + tmp = cell[1] + local prev = self.values[c] + local now = tonumber(cell[num]) + local rate = tonumber(((now - prev)*100.0) / self.hostCpuSum) + --str = string.format("%s %f delta=%d total=%f", tmp, tonumber(rate), now - prev, tonumber(self.hostCpuSum)) + --print (str) + values[c] = { + name = tmp, + value = tonumber(rate) + } + self.values[c] = now + self.conTotal = self.conTotal + (now - prev) + elseif string.find(line, "load average", 1) then + if string.find(line, "running", 1) then + tmp = "r_load" + else + tmp = "load" + end + if string.find(line, "%(1min", 1) then + tmp = tmp.."1min" + elseif string.find(line, "%(15min", 1) then + tmp = tmp.."15min" + elseif string.find(line, "%(5min", 1) then + tmp = tmp.."5min" + end + values[c] = { + name = tmp, + value = tonumber(cell[num]) + } + else + tmp = cell[1] + values[c] = { + name = tmp, + value = tonumber(cell[num]) + } + end + c = c + 1 + end + values[c] = { + name = "total", + value = tonumber((self.conTotal*100.0)/self.hostCpuSum) + } + self:appendLine(self:_packProto("cg_proc_stat", self.ls, values)) + self:push(lines) +end + +return CgProcStat diff --git a/source/tools/monitor/unity/collector/container/cg_cpuacct_stat.lua b/source/tools/monitor/unity/collector/container/cg_cpuacct_stat.lua new file mode 100644 index 0000000000000000000000000000000000000000..1d6ec301b38a66989d4c2782d497bcfe5ebeff90 --- /dev/null +++ b/source/tools/monitor/unity/collector/container/cg_cpuacct_stat.lua @@ -0,0 +1,73 @@ +require("common.class") +local pystring = require("common.pystring") +local CvProc = require("collector.vproc") +local root = "sys/fs/cgroup/cpuacct/" +local dfile = "/cpuacct.stat" +local procstat = "proc/stat" +local system = require("common.system") + +local CgCpuacctStat = class("cg_proc_stat", CvProc) + +--ls{}, (pod_name and docker_name +function CgCpuacctStat:_init_(proto, pffi, mnt, path, ls) + CvProc._init_(self, proto, pffi, mnt, root .. path .. dfile) + self.ls = ls + self.path = mnt..root..path..dfile + self.statpath = mnt..procstat + self.hostPrev = 0 + self.hostNow = 0 + self.hostCpuSum = 0 + self.conTotal = 0 + self.values = {0,0,0,0,0,0,0,0,0} +end + +function CgCpuacctStat:_getTotal_() + local pfile = io.open(self.statpath, "r") + local line = pfile:read() + local sum = 0 + local s = string.sub(line, 4) + local data = self._ffi.new("var_long_t") + assert(self._cffi.var_input_long(self._ffi.string(s), data) == 0) + assert(data.no == 10) + for i = 1, data.no do + sum = sum + data.value[i] + end + self.hostPrev = self.hostNow + self.hostNow = sum + self.hostCpuSum = sum - self.hostPrev + io.close(pfile) +end + +function CgCpuacctStat:proc(elapsed, lines) + local c = 1 + CvProc.proc(self) + self:_getTotal_() + local values = {} + self.conTotal = 0 + for line in io.lines(self.pFile) do + local name + local cell = pystring:split(line) + local num = #cell + if c < 3 then + name = cell[1] + local prev = self.values[c] + local now = tonumber(cell[num]) + local rate = tonumber(((now - prev)*100.0) / self.hostCpuSum) + values[c] = { + name = name, + value = tonumber(rate) + } + self.values[c] = now + self.conTotal = self.conTotal + (now - prev) + end + c = c + 1 + end + values[c] = { + name = "total", + value = tonumber((self.conTotal*100.0)/self.hostCpuSum) + } + self:appendLine(self:_packProto("cg_cpuacct_stat", self.ls, values)) + self:push(lines) +end + +return CgCpuacctStat diff --git a/source/tools/monitor/unity/collector/container/cg_cpuacct_wait_latency.lua b/source/tools/monitor/unity/collector/container/cg_cpuacct_wait_latency.lua new file mode 100644 index 0000000000000000000000000000000000000000..7ba1b6739efe641ec58156fc81b46a122de21dd2 --- /dev/null +++ b/source/tools/monitor/unity/collector/container/cg_cpuacct_wait_latency.lua @@ -0,0 +1,44 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/3/8 2:32 PM +--- + +require("common.class") +local pystring = require("common.pystring") +local CvProc = require("collector.vproc") +local root = "sys/fs/cgroup/cpuacct/" +local dfile = "/cpuacct.wait_latency" + +local CgWaitLatency = class("cg_wait_latency", CvProc) + +--ls{}, (pod_name and docker_name +function CgWaitLatency:_init_(proto, pffi, mnt, path, ls) + CvProc._init_(self, proto, pffi, mnt, root .. path .. dfile) + self.ls = ls +end + +function CgWaitLatency:proc(elapsed, lines) + -- if pFile not valid ,return -1 + local c = 1 + CvProc.proc(self) + local values = {} + + for line in io.lines(self.pFile) do + local cell = pystring:split(line) + local tmp = cell[1] + tmp = string.gsub(tmp, ":", "", 1) + tmp = string.gsub(tmp, ">=", "", 1) + tmp = string.gsub(tmp, "-", "to", 1) + tmp = string.gsub(tmp, "%(.*%)", "", 1) + values[c] = { + name = "wait_lat_"..tmp, + value = tonumber(cell[2]) + } + c = c + 1 + end + self:appendLine(self:_packProto("cg_wait_latency", self.ls, values)) + self:push(lines) +end + +return CgWaitLatency diff --git a/source/tools/monitor/unity/collector/container/cg_memory_dcmp_latency.lua b/source/tools/monitor/unity/collector/container/cg_memory_dcmp_latency.lua new file mode 100644 index 0000000000000000000000000000000000000000..3be56931050e940c23969d2225c331c5498cdc0e --- /dev/null +++ b/source/tools/monitor/unity/collector/container/cg_memory_dcmp_latency.lua @@ -0,0 +1,44 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/3/8 2:32 PM +--- + +require("common.class") +local pystring = require("common.pystring") +local CvProc = require("collector.vproc") +local root = "sys/fs/cgroup/memory/" +local dfile = "/memory.direct_compact_latency" + +local CgMemDcmpLatency = class("cg_memDcmp_latency", CvProc) + +--ls{}, (pod_name and docker_name +function CgMemDcmpLatency:_init_(proto, pffi, mnt, path, ls) + CvProc._init_(self, proto, pffi, mnt, root .. path .. dfile) + self.ls = ls +end + +function CgMemDcmpLatency:proc(elapsed, lines) + -- if pFile not valid ,return -1 + local c = 0 + CvProc.proc(self) + local values = {} + + for line in io.lines(self.pFile) do + local cell = pystring:split(line) + local tmp = cell[1] + tmp = string.gsub(tmp, ":", "", 1) + tmp = string.gsub(tmp, ">=", "", 1) + tmp = string.gsub(tmp, "-", "to", 1) + tmp = string.gsub(tmp, "%(.*%)", "", 1) + values[c] = { + name = "memDcmp_lat_"..tmp, + value = tonumber(cell[2]) + } + c = c + 1 + end + self:appendLine(self:_packProto("cg_memmcmp_latency", self.ls, values)) + self:push(lines) +end + +return CgMemDcmpLatency diff --git a/source/tools/monitor/unity/collector/container/cg_memory_drcm_latency.lua b/source/tools/monitor/unity/collector/container/cg_memory_drcm_latency.lua new file mode 100644 index 0000000000000000000000000000000000000000..2c10f61ed3a9cd278d37abb98c619a5d02efdf65 --- /dev/null +++ b/source/tools/monitor/unity/collector/container/cg_memory_drcm_latency.lua @@ -0,0 +1,44 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/3/8 2:32 PM +--- + +require("common.class") +local pystring = require("common.pystring") +local CvProc = require("collector.vproc") +local root = "sys/fs/cgroup/memory/" +local dfile = "/memory.direct_reclaim_memcg_latency" + +local CgMemDrcmLatency = class("cg_memDrcm_latency", CvProc) + +--ls{}, (pod_name and docker_name +function CgMemDrcmLatency:_init_(proto, pffi, mnt, path, ls) + CvProc._init_(self, proto, pffi, mnt, root .. path .. dfile) + self.ls = ls +end + +function CgMemDrcmLatency:proc(elapsed, lines) + -- if pFile not valid ,return -1 + local c = 1 + CvProc.proc(self) + local values = {} + + for line in io.lines(self.pFile) do + local cell = pystring:split(line) + local tmp = cell[1] + tmp = string.gsub(tmp, ":", "", 1) + tmp = string.gsub(tmp, ">=", "", 1) + tmp = string.gsub(tmp, "-", "to", 1) + tmp = string.gsub(tmp, "%(.*%)", "", 1) + values[c] = { + name = "memDrcm_lat_"..tmp, + value = tonumber(cell[2]) + } + c = c + 1 + end + self:appendLine(self:_packProto("cg_memdrcm_latency", self.ls, values)) + self:push(lines) +end + +return CgMemDrcmLatency diff --git a/source/tools/monitor/unity/collector/container/cg_memory_fail_cnt.lua b/source/tools/monitor/unity/collector/container/cg_memory_fail_cnt.lua new file mode 100644 index 0000000000000000000000000000000000000000..b82d9aea7d465007b90e93c281fd75e26b461f63 --- /dev/null +++ b/source/tools/monitor/unity/collector/container/cg_memory_fail_cnt.lua @@ -0,0 +1,39 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/3/8 2:32 PM +--- + +require("common.class") +local pystring = require("common.pystring") +local CvProc = require("collector.vproc") +local root = "sys/fs/cgroup/memory/" +local dfile = "/memory.failcnt" + +local CgMemFailCnt = class("cg_memFail_cnt", CvProc) + +--ls{}, (pod_name and docker_name +function CgMemFailCnt:_init_(proto, pffi, mnt, path, ls) + CvProc._init_(self, proto, pffi, mnt, root .. path .. dfile) + self.ls = ls +end + +function CgMemFailCnt:proc(elapsed, lines) + -- if pFile not valid ,return -1 + local c = 1 + CvProc.proc(self) + local values = {} + + for line in io.lines(self.pFile) do + local cell = pystring:split(line) + values[c] = { + name = "fail_cnt", + value = tonumber(cell[1]) + } + c = c + 1 + end + self:appendLine(self:_packProto("cg_memfail_cnt", self.ls, values)) + self:push(lines) +end + +return CgMemFailCnt diff --git a/source/tools/monitor/unity/collector/container/cg_pmu_events.lua b/source/tools/monitor/unity/collector/container/cg_pmu_events.lua new file mode 100644 index 0000000000000000000000000000000000000000..a44e083d515ca422ccadb1baadeebb8413cff0ca --- /dev/null +++ b/source/tools/monitor/unity/collector/container/cg_pmu_events.lua @@ -0,0 +1,132 @@ +require("common.class") +local pystring = require("common.pystring") +local CvProc = require("collector.vproc") +local unistd = require("posix.unistd") +local system = require("common.system") +local cgpmuffi = require("collector.native.cgpmuffi") + +local root = "sys/fs/cgroup/perf_event/" +local cgPmu = class("cg_pmu_events", CvProc) + +function cgPmu:_init_(proto, pffi, mnt, path, ls) + CvProc._init_(self, proto, pffi, mnt, root .. path) + self.ls = ls + local ffi = cgpmuffi["rawffi"] + self._cgpmuffi = cgpmuffi["cgpmuffi"] + self._rawffi = cgpmuffi["rawffi"] + self.all_cpus_t = ffi.typeof("pcpu_hwi_t[?]") + self.double_arry_t = ffi.typeof("double[?]") + self.c_str_t = ffi.typeof("const char*") + self.nr_events = 7 + self.nr_cpus = unistd.sysconf(84) + --print("nr_cpus="..tonumber(self.nr_cpus)) + self.allCpuInfo = ffi.new(self.all_cpus_t, self.nr_cpus) + self.summary = ffi.new(self.double_arry_t, self.nr_events) + + --self._cgpmuffi.my_sleep(1) + self.cpath = mnt..root..path + --print("cg_pmu_events: _init_:create_hw_events") + self._cgpmuffi.create_hw_events(self.allCpuInfo, self.nr_cpus, self.cpath) + self.stoped = 0 +end + +function cgPmu:_drcName() + return {"cycles", "ins", "refCyc", "llcLoad", + "llcLoadMis", "llcStore", "llcStoreMis"} +end + +function cgPmu:_compName() + return {"CPI", "IPC", "MPI", "l3LoadMisRate", + "l3StoreMisRate", "l3MisRate"} +end + +function cgPmu:fillCompValue(sum, values, index) + local csum = {} + local compNames = self:_compName() + local cpi, ipc, mpi, l3loadmisr, l3storemisr, l3misr + local CYC, INS, REFCYC, L3LOAD, L3LOADMS, L3STO, L3STOMS + CYC = 0 + INS = 1 + REFCYC = 2 + L3LOAD = 3 + L3LOADMS = 4 + L3STO = 5 + L3STOMS = 6 + if sum[INS] ~= 0 then + table.insert(csum, tonumber(sum[CYC]/sum[INS])) + table.insert(csum, tonumber((sum[L3LOADMS]+sum[L3STOMS])/sum[INS])) + else + table.insert(csum, 0) + table.insert(csum, 0) + end + + if sum[CYC] ~= 0 then + table.insert(csum, sum[INS]/sum[CYC]) + else + table.insert(csum, 0) + end + + if sum[L3LOAD] ~= 0 then + table.insert(csum, sum[L3LOADMS]/sum[L3LOAD]) + else + table.insert(csum, 0) + end + + if sum[L3STO] ~= 0 then + table.insert(csum, sum[L3STOMS]/sum[L3STO]) + else + table.insert(csum, 0) + end + + if (sum[L3LOAD]+sum[L3STO]) ~= 0 then + table.insert(csum, (sum[L3LOADMS]+sum[L3STOMS])/(sum[L3LOAD]+sum[L3STO])) + else + table.insert(csum, 0) + end + local c = index + --for i = 1, #compNames do + for k,v in ipairs(csum) do + values[c] = { + name = compNames[k], + value = v + } + c = c + 1 + end +end + +function cgPmu:proc(elapsed, lines) + CvProc.proc(self) + local c = 1 + local values = {} + local direcNames = self:_drcName() + self._cgpmuffi.collect_events(self.allCpuInfo, self.nr_cpus, self.summary) + local sum = self.summary + for i = 1, #direcNames do + values[c] = { + name = direcNames[i], + value = tonumber(sum[i-1]) + } + c = c + 1 + end + + local compNames = self:_compName() + self:fillCompValue(sum, values, c) + + self:appendLine(self:_packProto("pmu_cg_events", self.ls, values)) + self:push(lines) +end + +function cgPmu:releaseEvents() + local ret = self._cgpmuffi.stop_events(self.allCpuInfo, self.nr_cpus) + if ret == 0 then + self.stoped = 1 + end +end + +function cgPmu:_del_() + if self.stoped ~= 1 then + self._cgpmuffi.stop_events(self.allCpuInfo, self.nr_cpus) + end +end + +return cgPmu diff --git a/source/tools/monitor/unity/collector/container/cg_pmu_events_ffi/Makefile b/source/tools/monitor/unity/collector/container/cg_pmu_events_ffi/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..bdb49c3ee55d0392cdf739a3234c2c39e9501d82 --- /dev/null +++ b/source/tools/monitor/unity/collector/container/cg_pmu_events_ffi/Makefile @@ -0,0 +1,19 @@ +CC := gcc +CFLAG := -g -fpic +LDFLAG := -g -fpic -shared +OBJS := cg_pmu_ffi.o +SO := libcgpmuffi.so + +DEPMOD= ../collector +all: $(SO) install + +%.o: %.c %.h + $(CC) -c $< -o $@ $(CFLAG) + +$(SO): $(OBJS) + $(CC) -o $@ $(OBJS) $(LDFLAG) + +install: $(SO) + cp $(SO) ../../native/ +clean: + rm -f *.so $(OBJS) diff --git a/source/tools/monitor/unity/collector/container/cg_pmu_events_ffi/cg_pmu_ffi.c b/source/tools/monitor/unity/collector/container/cg_pmu_events_ffi/cg_pmu_ffi.c new file mode 100644 index 0000000000000000000000000000000000000000..c891f0b0ea12eee5641429e41a4a96d95be2d836 --- /dev/null +++ b/source/tools/monitor/unity/collector/container/cg_pmu_events_ffi/cg_pmu_ffi.c @@ -0,0 +1,157 @@ +#include "cg_pmu_ffi.h" + +void my_sleep(int x) +{ + sleep(x); + printf("sleep end\n"); +} + +char *events_str[] = {"cpu_cycles", "instructions", "ref_cycles", + "llc_load_ref", "llc_load_miss", + "llc_store_ref", "llc_store_miss"}; + +static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, + int cpu, int group_fd, unsigned long flags) +{ + int ret; + + ret = syscall(__NR_perf_event_open, hw_event, pid, cpu, + group_fd, flags); + return ret; +} + +static int create_pcpu_hw_events(struct pcpu_hwi *hwi, struct dyn_arg* arg) +{ + int i, j, fd, group; + struct event_info *ei; + struct perf_event_attr attr = { + .freq = 0, + .disabled = 1, + .sample_period = 1000*1000*1000, + }; + + group = -1; + ei = hwi->ei; + for (i = 0; i < NR_EVENTS; i++) { + attr.type = static_args[i].type; + attr.config = static_args[i].config; + fd = perf_event_open(&attr, arg->pid, arg->cpu, group, arg->flags); + if (fd > 0) { + ioctl(fd, PERF_EVENT_IOC_RESET, 0); + ioctl(fd, PERF_EVENT_IOC_ENABLE, 0); + ei[i].fd = fd; +#ifdef DEBUG + printf("cpu%d %s fd=%d\n", arg->cpu, events_str[i], fd); +#endif + } else { + printf("cpu%d perf_event_open fail\n", arg->cpu); + goto create_fail; + } + } + return 0; + +create_fail: + for (j = 0; j < i; j++) { + if (ei[j].fd > 0) { + ioctl(ei[j].fd, PERF_EVENT_IOC_DISABLE, 0); + close(ei[j].fd); + } + } + return -1; +} + +int create_hw_events(struct pcpu_hwi *hwi, int nr_cpus, char *origpath) +{ + int i, cgfd; + struct dyn_arg args; +#if 0 + printf("create_hw_events \n"); + return 0; +#endif + cgfd = open(origpath, O_RDONLY); + if (cgfd > 0) { + args.flags = PERF_FLAG_PID_CGROUP; + args.pid = cgfd; + } else { + return -errno; + } + for (i = 0; i < nr_cpus; i++) { + args.cpu = i; + create_pcpu_hw_events(&hwi[i], &args); + } +} + +static int collect_pcpu_events(struct pcpu_hwi *hwi, double *sum) +{ + int n, enumo; + struct event_info *ei; + int i, fd; + + n = 0; + ei = hwi->ei; + for (i = 0; i < NR_EVENTS; i++) { + fd = ei[i].fd; + if (fd > 0) { + n = read(fd, &ei[i].cnt, sizeof(__u64)); + if (n < 0) { + enumo = errno; +#ifdef DEBUG + printf(" read fd%d fail:%s \n", fd, strerror(enumo)); +#endif + return -enumo; + } + ei[i].delta = ei[i].cnt - ei[i].prev; + ei[i].prev = ei[i].cnt; + sum[i] = sum[i] + ei[i].delta; + } else { +//#ifdef DEBUG + printf("collect_pcpu_events: fd = %d\n", fd); +//#endif + } + } + return n; +} + +int collect_events(struct pcpu_hwi *hwi, int nr_cpus, double *sum) +{ + int i, n; + + for (i = 0; i < nr_cpus; i++) { + n = collect_pcpu_events(&hwi[i], sum); + if (n < 0) { + printf("collect_pcpu_events cpu%d fail:%s\n", i, strerror(-n)); + break; + } + } + if (n < 0) + return n; + return 0; +} + +int stop_events(struct pcpu_hwi *hwi, int nr_cpus) +{ + int i, j, fd; + struct event_info *ei; + unsigned long long sum[NR_EVENTS] = {0}; + + for(i = 0; i < nr_cpus; i++) { + ei = hwi[i].ei; + for (j = 0; j < NR_EVENTS; j++) { + fd = ei[j].fd; + if (fd > 0) { +#ifdef DEBUG + printf("cpu%d %s delta=%llu\n", i, events_str[j], ei[j].delta); +#endif + sum[j] = sum[j] + ei[j].delta; + ioctl(fd, PERF_EVENT_IOC_DISABLE, 0); + close(fd); + ei[j].fd = -1; + } else { +#ifdef DEBUG + printf("Fail: cpu%d %s fd=%d\n", i, events_str[j], fd); +#endif + } + } + } + return 0; +} diff --git a/source/tools/monitor/unity/collector/container/cg_pmu_events_ffi/cg_pmu_ffi.h b/source/tools/monitor/unity/collector/container/cg_pmu_events_ffi/cg_pmu_ffi.h new file mode 100644 index 0000000000000000000000000000000000000000..18a582d779e6c8e82111c7ca4b9db658d8c00f56 --- /dev/null +++ b/source/tools/monitor/unity/collector/container/cg_pmu_events_ffi/cg_pmu_ffi.h @@ -0,0 +1,108 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define NR_EVENTS 7 + +struct static_arg { + __u32 type; + __u64 config; + int group; +} static_args [] = { + { + .type = PERF_TYPE_HARDWARE, + .config = PERF_COUNT_HW_CPU_CYCLES, + .group = 0, + }, + { + .type = PERF_TYPE_HARDWARE, + .config = PERF_COUNT_HW_INSTRUCTIONS, + .group = 0, + }, + { + .type = PERF_TYPE_HARDWARE, + .config = PERF_COUNT_HW_REF_CPU_CYCLES, + .group = 0, + }, + { + .type = PERF_TYPE_HW_CACHE, + .config = PERF_COUNT_HW_CACHE_LL << 0| + (PERF_COUNT_HW_CACHE_OP_READ << 8)| + (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16), + .group = 0, + }, + { + .type = PERF_TYPE_HW_CACHE, + .config = PERF_COUNT_HW_CACHE_LL << 0| + (PERF_COUNT_HW_CACHE_OP_READ << 8)| + (PERF_COUNT_HW_CACHE_RESULT_MISS << 16), + .group = 0, + }, + { + .type = PERF_TYPE_HW_CACHE, + .config = PERF_COUNT_HW_CACHE_LL << 0| + (PERF_COUNT_HW_CACHE_OP_WRITE << 8)| + (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16), + .group = 0, + }, + { + .type = PERF_TYPE_HW_CACHE, + .config = PERF_COUNT_HW_CACHE_LL << 0| + (PERF_COUNT_HW_CACHE_OP_WRITE << 8)| + (PERF_COUNT_HW_CACHE_RESULT_MISS << 16), + .group = 0, + }, +#if 0 + { + .type = PERF_TYPE_SOFTWARE, + .config = PERF_COUNT_SW_CPU_CLOCK, + }, + { + .type = PERF_TYPE_SOFTWARE, + .config = PERF_COUNT_SW_CONTEXT_SWITCHES, + }, + { + .type = PERF_TYPE_SOFTWARE, + .config = PERF_COUNT_SW_TASK_CLOCK, + }, + { + .type = PERF_TYPE_SOFTWARE, + .config = PERF_COUNT_SW_PAGE_FAULTS, + }, + { + .type = PERF_TYPE_SOFTWARE, + .config = PERF_COUNT_SW_PAGE_FAULTS_MIN, + }, + { + .type = PERF_TYPE_SOFTWARE, + .config = PERF_COUNT_SW_CPU_MIGRATIONS, + }, + { + .type = PERF_TYPE_SOFTWARE, + .config = PERF_COUNT_SW_PAGE_FAULTS_MAJ, + }, +#endif +}; + +struct dyn_arg { + int pid, cpu; + unsigned long flags; +}; + +struct event_info { + int fd; + unsigned long long prev, cnt, delta; +}; + +typedef struct pcpu_hwi { + struct event_info ei[NR_EVENTS]; +} pcpu_hwi_t; + diff --git a/source/tools/monitor/unity/collector/guard/calcJiffies.lua b/source/tools/monitor/unity/collector/guard/calcJiffies.lua new file mode 100644 index 0000000000000000000000000000000000000000..5e12adfa2adec0af349f0cb8234db025fc409da7 --- /dev/null +++ b/source/tools/monitor/unity/collector/guard/calcJiffies.lua @@ -0,0 +1,65 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/3/18 11:34 PM +--- + +local mod = {} +local ptime = require("posix.time") +local unistd = require("posix.unistd") +local system = require("common.system") + +local function read_jiffies(path, procffi) + local s + for line in io.lines(path) do + s = line + break + end + local data = procffi.ffi.new("var_kvs_t") + assert(procffi.cffi.var_input_kvs(procffi.ffi.string(s), data) == 0) + assert(data.no == 10) + + local res = 0 + for i = 0, data.no - 1 do + res = res + tonumber(data.value[i]) + end + return res +end + +local function nproc() + local r, err, errno = unistd.sysconf(84) + if err then + system:posixError("sysconf failed", err, errno) + end + return r +end + +function mod.calc(mnt, procffi) + local t = {tv_sec=0, tv_nsec=2e8} -- 200ms + local path = mnt .. "proc/stat" + local r, err, errno + + local j1 = read_jiffies(path, procffi) + r, err, errno = ptime.nanosleep(t) + if err then + system:posixError("nano sleep failed", err, errno) + end + + local j2 = read_jiffies(path, procffi) + ptime.nanosleep(t) + if err then + system:posixError("nano sleep failed", err, errno) + end + + local j3 = read_jiffies(path, procffi) + local delta1, delta2 = j2 - j1, j3 -j2 + local comp = delta1 / delta2 + + if comp >= 1.1 or comp < 0.9 then + errno("calculate jiffies failed.") + end + + return (delta1 + delta2) * 2.5 / nproc() +end + +return mod diff --git a/source/tools/monitor/unity/collector/guard/collector_stat.lua b/source/tools/monitor/unity/collector/guard/collector_stat.lua new file mode 100644 index 0000000000000000000000000000000000000000..0b94abdbf9d22e9981a0e39c1c44c0da10f74bf6 --- /dev/null +++ b/source/tools/monitor/unity/collector/guard/collector_stat.lua @@ -0,0 +1,28 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/3/19 3:36 PM +--- + +require("common.class") +local pystring = require("common.pystring") +-- refer to https://blog.csdn.net/longyuelang/article/details/114025476 + +local CcollectorStat = class("CcollectorStat") + +function CcollectorStat:_init_(tid) + self._path = string.format("/proc/self/task/%d/stat", tid) +end + +function CcollectorStat:jiffies() + local s + for line in io.lines(self._path) do + s = line + end + local ss = pystring:rsplit(s, ") ", 1) + local rest = ss[2] + local vs = pystring:split(rest, " ", 14) + return tonumber(vs[12]) + tonumber(vs[13]) +end + +return CcollectorStat diff --git a/source/tools/monitor/unity/collector/guard/guardDaemon.lua b/source/tools/monitor/unity/collector/guard/guardDaemon.lua new file mode 100644 index 0000000000000000000000000000000000000000..a637edae22121f1bafd2e1ab4211bb745891df61 --- /dev/null +++ b/source/tools/monitor/unity/collector/guard/guardDaemon.lua @@ -0,0 +1,42 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/3/22 11:05 PM +--- + +require("common.class") + +local unistd = require("posix.unistd") +local system = require("common.system") +local CguardDaemon = class("guardDaemon") + +local function feedDaemon(fd) + local ws, err, errno = unistd.write(fd, "feed.") + if ws == nil then + system:posixError("feed daemo failed.", err, errno) + end +end + +function CguardDaemon:_init_(resYaml) + local freq = resYaml.config.freq + if resYaml.config.daemon then + self.fd = lua_setup_daemon(freq) + self.feedOnce = function() feedDaemon(self.fd) end + else + self.fd = -1 + self.feedOnce = function() end + end +end + +function CguardDaemon:_del_() + if self.fd > 0 then + unistd.write(self.fd, "stop") + unistd.close(self.fd) + end +end + +function CguardDaemon:feed() + self.feedOnce() +end + +return CguardDaemon diff --git a/source/tools/monitor/unity/collector/guard/guardSched.lua b/source/tools/monitor/unity/collector/guard/guardSched.lua new file mode 100644 index 0000000000000000000000000000000000000000..664f8419be2eed716c5474e313d895044cf9a326 --- /dev/null +++ b/source/tools/monitor/unity/collector/guard/guardSched.lua @@ -0,0 +1,58 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/3/20 5:16 PM +--- + +require("common.class") +local CcollectorStat = require("collector.guard.collector_stat") +local system = require("common.system") + +local CguardSched = class("guardSched") + +function CguardSched:_init_(tid, procs, names, jperiod) + self._stat = CcollectorStat.new(tid) + self._jperiod = jperiod + self._procs = procs + self._names = names + self._limit = 1e5*5 -- 500 ms +end + +function CguardSched:proc(t, lines) + local toRemove = {} + + local start = lua_local_clock() -- unit us + local stop = 0 + local j1 = self._stat:jiffies() + + for i, obj in ipairs(self._procs) do + local ret, overTime = obj:proc(t, lines) + if ret == -1 then + table.insert(toRemove, i) + else + stop = lua_local_clock() + if ret ~= 1 then + overTime = 0 + end + if stop - start - overTime >= self._limit then -- + print(stop - start) + local j2 = self._stat:jiffies() + if j2 - j1 >= self._limit / 1e6 * self._jperiod * 3 / 4 then -- 3/4 time used bye plugin + table.insert(toRemove, i) + end + end + end + start = stop + end + + if #toRemove > 0 then + system:reverseTable(toRemove) -- list should reverse at first. + for _, i in ipairs(toRemove) do + print("remove " .. self._names[i]) + table.remove(self._procs, i) + table.remove(self._names, i) + end + end +end + +return CguardSched diff --git a/source/tools/monitor/unity/collector/guard/guardSelfStat.lua b/source/tools/monitor/unity/collector/guard/guardSelfStat.lua new file mode 100644 index 0000000000000000000000000000000000000000..0564b2749c54bcaf7da7b52df93e8724b050bf9a --- /dev/null +++ b/source/tools/monitor/unity/collector/guard/guardSelfStat.lua @@ -0,0 +1,76 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/3/23 8:15 AM +--- + +require("common.class") +local pystring = require("common.pystring") +local CvProc = require("collector.vproc") +-- refer to https://blog.csdn.net/longyuelang/article/details/114025476 + +local CguardSelfStat = class("guardSelfStat", CvProc) + +local function readProc(path) + local s + for line in io.lines(path) do + s = line + end + local ss = pystring:rsplit(s, ") ", 1) + local rest = ss[2] + local vs = pystring:split(rest, " ") + local user, sys = tonumber(vs[12]), tonumber(vs[13]) + local vsize, rss = tonumber(vs[21]), tonumber(vs[22]) + return user, sys, vsize, rss +end + +function CguardSelfStat:_init_(proto, pffi, mnt, resYaml, jperiod) + CvProc._init_(self, proto, pffi, mnt, nil) + self._path = "/proc/self/stat" + + self._lastUser, self._lastSys, _, _ = readProc(self._path) + self._period = jperiod + self._cpuLimit = resYaml.config.limit.cpu * jperiod / 100 + self._memLimit = resYaml.config.limit.mem * 1024 * 1024 +end + +function CguardSelfStat:proc(elapsed, lines) + CvProc.proc(self) + + local user, sys, vsize, rss = readProc(self._path) + local _user, _sys = user - self._lastUser, sys - self._lastSys + local cpus = _user, _sys + + self._lastUser, self._lastSys = user, sys + if cpus > self._cpuLimit * elapsed then + print("last cpu usage overflow." .. cpus) + os.exit(1) + end + if rss * 4096 > self._memLimit then + print("last mem usage overflow." .. rss) + os.exit(1) + end + local vs = { + { + name = "user", + value = _user * 100 / (self._period * elapsed) + }, + { + name = "sys", + value = _sys * 100 / (self._period * elapsed) + }, + { + name = "vsize", + value = vsize + } + , + { + name = "rss", + value = rss * 4096 + } + } + self:appendLine(self:_packProto("self_stat", nil, vs)) + self:push(lines) +end + +return CguardSelfStat \ No newline at end of file diff --git a/source/tools/monitor/unity/collector/loop.lua b/source/tools/monitor/unity/collector/loop.lua index 28b23464f21b2a12d2dd6a6682dbac7510cc2f34..80ae76f3ede5069f93ed1f7070cdaa0da61d7713 100644 --- a/source/tools/monitor/unity/collector/loop.lua +++ b/source/tools/monitor/unity/collector/loop.lua @@ -7,39 +7,65 @@ require("common.class") local CprotoData = require("common.protoData") local procffi = require("collector.native.procffi") -local Cplugin = require("collector.plugin") local system = require("common.system") +local CpluginManager = require("collector.pluginManager") +local calcJiffies = require("collector.guard.calcJiffies") +local CguardSched = require("collector.guard.guardSched") +local CguardDaemon = require("collector.guard.guardDaemon") +local CguardSelfStat = require("collector.guard.guardSelfStat") +local CpostPlugin = require("collector.postPlugin.postPlugin") +--local CpodsAll = require("collector.podMan.podsAll") +local CpodFilter = require("collector.podMan.podFilter") local Cloop = class("loop") -function Cloop:_init_(que, proto_q, fYaml) +function Cloop:_init_(que, proto_q, fYaml, tid) local res = system:parseYaml(fYaml) + self._daemon = CguardDaemon.new(res) + self._proto = CprotoData.new(que) - self:loadLuaPlugin(res, res.config.proc_path) - self._plugin = Cplugin.new(self._proto, procffi, que, proto_q, fYaml) + self._tid = tid + self:loadLuaPlugin(res, res.config.proc_path, procffi) + local jperiod = calcJiffies.calc(res.config.proc_path, procffi) -- + + self._guardSched = CguardSched.new(tid, self._procs, self._names, jperiod) + self._soPlugins = CpluginManager.new(procffi, proto_q, res, tid, jperiod) + self._guardStat = CguardSelfStat.new(self._proto, procffi, "/", res, jperiod) + self.postPlugin = CpostPlugin.new(self._proto, procffi, res) end -function Cloop:loadLuaPlugin(res, proc_path) +function Cloop:loadLuaPlugin(res, proc_path, procffi) local luas = res.luaPlugins self._procs = {} + self._names = {} + local c = 1 if res.luaPlugins then - for i, plugin in ipairs(luas) do + for _, plugin in ipairs(luas) do local CProcs = require("collector." .. plugin) - self._procs[i] = CProcs.new(self._proto, procffi, proc_path) + self._procs[c] = CProcs.new(self._proto, procffi, proc_path) + self._names[c] = plugin + c = c + 1 end end - print("add " .. #self._procs .. " lua plugin.") + --self._procs[c] = CpodsAll.new(res, self._proto, procffi, proc_path) + --self._names[c] = "podMon" + self._procs[c] = CpodFilter.new(res, self._proto, procffi, proc_path) + self._names[c] = "podFilter" + print("add " .. system:keyCount(self._procs) .. " lua plugin.") end function Cloop:work(t) local lines = self._proto:protoTable() - for _, obj in pairs(self._procs) do - obj:proc(t, lines) - end - self._plugin:proc(t, lines) + + self._guardSched:proc(t, lines) + self._soPlugins:proc(t, lines) + self._guardStat:proc(t, lines) + self.postPlugin:proc(t, lines) + local bytes = self._proto:encode(lines) self._proto:que(bytes) + self._daemon:feed() end return Cloop diff --git a/source/tools/monitor/unity/collector/native/cgpmuffi.lua b/source/tools/monitor/unity/collector/native/cgpmuffi.lua new file mode 100644 index 0000000000000000000000000000000000000000..fea27e114b9c422f3d0202f69dd79d55131ccfd7 --- /dev/null +++ b/source/tools/monitor/unity/collector/native/cgpmuffi.lua @@ -0,0 +1,119 @@ +local rawffi = require "ffi" +local cgpmuffi = rawffi.load('cgpmuffi') +local unistd = require("posix.unistd") + +rawffi.cdef[[ + struct event_info { + int fd; + unsigned long long prev, cnt, delta; + }event_info_t; + + typedef struct pcpu_hwi { + struct event_info ei[7]; + } pcpu_hwi_t; + + void my_sleep(int x); + int create_hw_events(pcpu_hwi_t *hwi, int nr_cpus, const char *path); + int collect_events(pcpu_hwi_t *hwi, int nr_cpus, double *summary); + int stop_events(pcpu_hwi_t *hwi, int nr_cpus); +]] +--[[ +local all_cpus_t = rawffi.typeof("pcpu_hwi_t[?]") +local double_arry_t = rawffi.typeof("double[?]") +local c_str_t = rawffi.typeof("const char*") + +--step 1: init-- +--post to me +local nr_cpu = unistd.sysconf(84) +print("nr_cpu="..nr_cpu) +local nr_events = 7 +local parent_path = "/sys/fs/cgroup/perf_event" +local child_path = "/docker/" +--local mnt="/mnt/host" +local mnt="/" + +local real_path = mnt..parent_path..child_path + +local cpath = rawffi.cast(c_str_t, real_path) +local allCpuInfo = rawffi.new(all_cpus_t, nr_cpu) +local ret = cgpmuffi.create_hw_events(allCpuInfo, nr_cpu, cpath) + + +--step 2: collect and fill line -- +local eventStrs = {"cycles", "ins", "refCyc", + "llcLoad", "llcLoadMis", + "llcStore", "llcStoreMis"} +local summary = rawffi.new(double_arry_t, nr_events) +ret = cgpmuffi.collect_events(allCpuInfo, nr_cpu, summary) +cgpmuffi.my_sleep(3) +ret = cgpmuffi.collect_events(allCpuInfo, nr_cpu, summary) +--]] +--[[ for debug +for i = 0, nr_cpu-1 do + local pcpues = allCpuInfo[i] + for j = 0, nr_events-1 do + local ei = pcpues.ei[j] + print("cpu"..i.." "..eventStrs[j+1].."="..tonumber(ei.delta)) + end +end +]]-- +--[[ +for j = 0, nr_events-1 do + print("summary "..eventStrs[j+1].."="..tonumber(summary[j])) +end + +local cpi, ipc, mpi, ref, miss, loadmis, storemis, missrate +local CYC, INS, REFCYC, L3LOAD, L3LOADMS, L3STO, L3STOMS +CYC = 0 +INS = 1 +REFCYC = 2 +L3LOAD = 3 +L3LOADMS = 4 +L3STO = 5 +L3STOMS = 6 + +ref = summary[L3LOAD] + summary[L3STO] +miss = summary[L3LOADMS] + summary[L3STOMS] +if summary[INS] ~= 0 then + cpi = tonumber(summary[CYC]/summary[INS]) + mpi = tonumber(summary[miss]/summary[INS]) +else + cpi = 0.0 + mpi = 0.0 +end + +if summary[CYC] ~= 0 then + ipc = tonumber(summary[INS]/summary[CYC]) +else + ipc = 0.0 +end +print("summary ".."ipc="..ipc..",cpi="..cpi..",mpi="..mpi) + +if summary[L3LOAD] ~= 0 then + loadmis = tonumber(summary[L3LOADMS]/summary[L3LOAD]) +else + loadmis = 0.0 +end + +if summary[L3STO] ~= 0 then + storemis = tonumber(summary[L3STOMS]/summary[L3STO]) +else + storemis = 0.0 +end + +if ref ~= 0 then + missrate = tonumber(miss/ref) +else + missrate = 0.0 +end +print("LLC ref="..ref..",miss="..miss) +print("LLC loadmiss="..loadmis..",storemiss="..storemis..",misrate="..missrate) + +print("-----------------") +--todo: how to fiil allCpuInfo to lines ??-- + + +--step 3: deinit -- +ret = cgpmuffi.stop_events(allCpuInfo, nr_cpu) +]]-- +return {rawffi = rawffi, cgpmuffi=cgpmuffi} diff --git a/source/tools/monitor/unity/collector/outline/Makefile b/source/tools/monitor/unity/collector/outline/Makefile index 118b288e3d6a5e9debda4ce86f7f5b4ef392527d..79d46ef84c1a374bbafa5fe2327cb3bd49f63449 100644 --- a/source/tools/monitor/unity/collector/outline/Makefile +++ b/source/tools/monitor/unity/collector/outline/Makefile @@ -4,7 +4,7 @@ CFLAG := -g OBJS := outline.o LIB := liboutline.a -all: $(DEPMOD) $(LIB) +all: $(LIB) %.o: %.c, %.h @@ -14,4 +14,4 @@ $(LIB): $(OBJS) $(AR) cr $@ $(OBJS) clean: - rm -f $(EXEC) $(OBJS) \ No newline at end of file + rm -f $(LIB) $(OBJS) \ No newline at end of file diff --git a/source/tools/monitor/unity/collector/outline/outline.c b/source/tools/monitor/unity/collector/outline/outline.c index 90ec914fe35f5f92996083b52bad70c30f670cb7..89d4a7723bd629542f27d7d384d9d721e0d2c224 100644 --- a/source/tools/monitor/unity/collector/outline/outline.c +++ b/source/tools/monitor/unity/collector/outline/outline.c @@ -4,6 +4,8 @@ #include "outline.h" #include +#include +#include extern int lua_reg_errFunc(lua_State *L); extern int lua_check_ret(int ret); @@ -125,6 +127,7 @@ static int outline_work(struct beeQ* q, char *fYaml) { static int outline_run(struct beeQ* q, void* arg) { int ret; char *fYaml = (char *)arg; + prctl(PR_SET_NAME, (unsigned long)"outline_run"); while (1) { ret = outline_work(q, fYaml); diff --git a/source/tools/monitor/unity/collector/plugin.lua b/source/tools/monitor/unity/collector/plugin.lua index 57823c0fec1cbf98c51ad7be78fa62fefc495208..38cdb271f5f5bc94c660704cb0b842fd4b8d553b 100644 --- a/source/tools/monitor/unity/collector/plugin.lua +++ b/source/tools/monitor/unity/collector/plugin.lua @@ -5,65 +5,48 @@ --- require("common.class") -local system = require("common.system") local Cplugin = class("plugin") - -function Cplugin:_init_(proto, procffi, que, proto_q, fYaml) - self._proto = proto - - local res = system:parseYaml(fYaml) - self:setProcSys(procffi, res.config) - - self._sig_cffi = procffi["cffi"] - self._sig_cffi.ffi_plugin_init() - - self._ffi = require("collector.native.plugincffi") - self:setup(res.plugins, proto_q) +local dockerinfo = require("common.dockerinfo") + +function Cplugin:_init_(resYaml, ffi, proto_q, so) + self._ffi = ffi + self._cffi = self._ffi.load(so) + self._cffi.init(proto_q) + self._so = so + self.proc_fs = resYaml.config["proc_path"] or "/" + self.fill_arg = {["podname"]="pid"} end function Cplugin:_del_() - self._sig_cffi.ffi_plugin_stop() - for _, plugin in ipairs(self._plugins) do - local cffi = plugin.cffi - cffi.deinit() - end - self._sig_cffi.ffi_plugin_deinit() + print("uninstall " .. self._so) + self._cffi.deinit() end -function Cplugin:setProcSys(procFFI, config) - local proc = config["proc_path"] or "/" - local sys = config["sys_path"] or "/" +function Cplugin:load_label(unity_line, line) - procFFI.cffi.ffi_set_unity_proc(procFFI.ffi.string(proc)) - procFFI.cffi.ffi_set_unity_sys(procFFI.ffi.string(sys)) -end + local c = #line.ls + local table_dict = {} -function Cplugin:setup(plugins, proto_q) - self._plugins = {} - for _, plugin in ipairs(plugins) do - local so = plugin.so - if so then - print(so) - local cffi = self._ffi.load(so) - local plugin = { - so = plugin.so, - cffi = cffi - } - cffi.init(proto_q); - table.insert(self._plugins, plugin) - end + for i=0, 4 - 1 do + local name = self._ffi.string(unity_line.indexs[i].name) + local index = self._ffi.string(unity_line.indexs[i].index) + table_dict[name] = index end -end -function Cplugin:load_label(unity_line, line) - local c = #line.ls for i=0, 4 - 1 do local name = self._ffi.string(unity_line.indexs[i].name) local index = self._ffi.string(unity_line.indexs[i].index) if #name > 0 then c = c + 1 - line.ls[c] = {name = name, index = index} + if index == "?" and self.fill_arg[name] and table_dict[self.fill_arg[name]] then + if name == "podname" then + local podname = dockerinfo:get_podname_pid(table_dict[self.fill_arg[name]], self.proc_fs) + line.ls[c] = {name = name, index = podname} + end + else + line.ls[c] = {name = name, index = index} + end else return end @@ -112,15 +95,12 @@ function Cplugin:_proc(unity_lines, lines) end function Cplugin:proc(t, lines) - for _, plugin in ipairs(self._plugins) do - local cffi = plugin.cffi - local unity_lines = self._ffi.new("struct unity_lines") - local res = cffi.call(t, unity_lines) - if res == 0 then - self:_proc(unity_lines, lines) - end - self._ffi.C.free(unity_lines.line) -- should free memory. + local unity_lines = self._ffi.new("struct unity_lines") + local res = self._cffi.call(t, unity_lines) + if res == 0 then + self:_proc(unity_lines, lines) end + self._ffi.C.free(unity_lines.line) -- should free memory. end return Cplugin diff --git a/source/tools/monitor/unity/collector/plugin.yaml b/source/tools/monitor/unity/collector/plugin.yaml index e0d0d92ec1a64eab8a4f8afeb5bf92c679a72b0b..ac162005877fa2bc04b482115e3110ce0a9fed65 100644 --- a/source/tools/monitor/unity/collector/plugin.yaml +++ b/source/tools/monitor/unity/collector/plugin.yaml @@ -1,9 +1,10 @@ config: freq: 20 # unit second + daemon: true port: 8400 # bind port bind_addr: 0.0.0.0 # bind ip backlog: 32 # listen backlog - identity: # support hostip, curl(need url arg), hostname, file(need path arg), specify(need name arg) + identity: # support hostip, curl(need url arg), hostname, file(need path arg), specify(need name arg), env(need name arg) mode: curl url: "http://100.100.100.200/latest/meta-data/instance-id" # name: test_specify @@ -15,14 +16,37 @@ config: db: rotate: 7 # tsdb file retention time, unit day budget: 200 # max query buffer from tsdb. + limit: + cpu: 30 # unit % + mem: 40 # unit mb + tasks: 10 # monitor 10 pid max. outline: - /tmp/sysom container: mode: "pods" - luaPlugin: ["cg_cpuacct_proc_stat", "cg_memory_dcmp_latency", "cg_memory_fail_cnt", - "cg_cpu_stat", "cg_cpuacct_wait_latency", "cg_memory_drcm_latency"] + luaPlugin: ["cg_cpu_stat_sample", "cg_cpuacct_stat"] + directCgPath: + - "/" + - "/kubepods.slice" + - "/kubepods.slice/kubepods-besteffort.slice" + - "/kubepods.slice/kubepods-burstable.slice" + + indirectCgPath: + - "kubepods.slice" + - "kubepods.slice/kubepods-besteffort.slice" + - "kubepods.slice/kubepods-burstable.slice" + + indirectCgPath1: + - path: "/kubepods.slice" + child1: "/kubepods%-pod" + child2: "/cri%-containerd" + - path: "/kubepods.slice/kubepods-besteffort.slice" + child1: "/kubepods%-besteffort%-pod" + child2: "/cri%-containerd" + - path: "/kubepods.slice/kubepods-burstable.slice" + child1: "/kubepods%-burstable%-pod" luaPlugins: ["proc_buddyinfo", "proc_diskstats", "proc_meminfo", "proc_mounts", "proc_netdev", "proc_snmp_stat", "proc_sockstat", "proc_stat", "proc_statm", "proc_vmstat", @@ -61,12 +85,15 @@ plugins: #- # so: numainfo # description: "collect numainfo" - # - - # so: cpufreq - # description: "collect cpufreq" + #- + # so: cpufreq + # description: "collect cpufreq" - so: gpuinfo description: "collect gpuinfo" + #- + # so: pmu_events + # description: "collect pmu events" metrics: - @@ -175,7 +202,7 @@ metrics: head: value help: "Diagnose log for IO exception" type: "gauge" - - title: sched_moni_jitter + - title: sysak_sched_moni_jitter from: sched_moni_jitter head: value help: "nosched/irqoff:sys and irqoff hold cpu and didn't scheduling" @@ -205,11 +232,11 @@ metrics: # head: value # help: "numainfo of system from /sys/devices/system/" # type: "gauge" - # - title: sysak_proc_cpufreq - # from: cpufreq - # head: value - # help: "cpufreq of system from /proc/cpuinfo" - # type: "gauge" + #- title: sysak_proc_cpufreq + # from: cpufreq + # head: value + # help: "cpufreq of system from /proc/cpuinfo" + # type: "gauge" - title: sysak_gpuinfo from: gpuinfo head: value @@ -221,3 +248,48 @@ metrics: #head: value #help: "get pod alloc page used" #type: "gauge" + - title: sysak_pmu_events + from: pmu_events + head: value + help: "pmu events, such as cycles/instructions, llc events" + type: "gauge" + - title: sysak_pmu_events_percpu + from: pmu_events_percpu + head: value + help: "pmu events of percpu" + type: "gauge" + - title: sysak_cg_memfail_cnt + from: cg_memfail_cnt + head: value + help: "sysak_cg_memFail_cnt" + type: "gauge" + - title: sysak_cg_memdrcm_latency + from: cg_memdrcm_latency + head: value + help: "sysak_cg_memdrcm_latency" + type: "gauge" + - title: sysak_cg_memmcmp_latency + from: cg_memmcmp_latency + head: value + help: "sysak_cg_memmcmp_latency" + type: "gauge" + - title: sysak_cg_wait_latency + from: cg_wait_latency + head: value + help: "sysak_cg_wait_latency" + type: "gauge" + - title: sysak_cg_cpuacct_proc_stat + from: cg_cpuacct_proc_stat + head: value + help: "sysak_cg_cpuacct_proc_stat" + type: "gauge" + - title: sysak_cg_cpu_stat + from: cg_cpu_stat + head: value + help: "sysak_cg_cpu_stat" + type: "gauge" + - title: sysak_cg_cpuacct_stat + from: cg_cpuacct_stat + head: value + help: "cpuacct/cpuacct.stat" + type: "gauge" diff --git a/source/tools/monitor/unity/collector/plugin/Makefile b/source/tools/monitor/unity/collector/plugin/Makefile index 9c538406f8e6c8e4b1cf7c31c221c52de158cfaa..bc8383162dd7e69fadc332a02f1d0360a1115d06 100644 --- a/source/tools/monitor/unity/collector/plugin/Makefile +++ b/source/tools/monitor/unity/collector/plugin/Makefile @@ -4,9 +4,7 @@ LDFLAG := -g -fpic -shared OBJS := proto_sender.o LIB := libproto_sender.a - -DEPMOD=sample threads kmsg proc_schedstat proc_loadavg unity_nosched unity_irqoff cpudist net_health net_retrans netlink numainfo cpufreq gpuinfo - +DEPMOD=sample threads kmsg proc_schedstat proc_loadavg unity_nosched unity_irqoff cpudist cpu_bled net_health net_retrans netlink cpufreq gpuinfo pmu_events all: $(LIB) $(DEPMOD) diff --git a/source/tools/monitor/unity/collector/plugin/cpu_bled/Makefile b/source/tools/monitor/unity/collector/plugin/cpu_bled/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..fe795e18fc90f11ee5eda9e026e7b120fcb4aeb4 --- /dev/null +++ b/source/tools/monitor/unity/collector/plugin/cpu_bled/Makefile @@ -0,0 +1,19 @@ +CC := gcc +CFLAG := -g -fpic +LDFLAG := -g -fpic -shared +OBJS := cpu_bled.o +SO := libcpu_bled.so + +all: $(SO) install + +%.o: %.c + $(CC) -c $< -o $@ $(CFLAG) + +$(SO): $(OBJS) + $(CC) -o $@ $(OBJS) $(LDFLAG) + +install: $(SO) + cp $(SO) ../../native/ + +clean: + rm -f $(SO) $(OBJS) \ No newline at end of file diff --git a/source/tools/monitor/unity/collector/plugin/cpu_bled/cpu_bled.c b/source/tools/monitor/unity/collector/plugin/cpu_bled/cpu_bled.c new file mode 100644 index 0000000000000000000000000000000000000000..f22fa9db6459e09407492749533f1bfaa17ac19a --- /dev/null +++ b/source/tools/monitor/unity/collector/plugin/cpu_bled/cpu_bled.c @@ -0,0 +1,28 @@ +// +// Created by 廖肇燕 on 2023/3/21. +// + +#include "cpu_bled.h" +#include +#include + +static int bled_wait = 0; + +int init(void * arg) { + printf("setup for cpu_bled.\n"); + return 0; +} + +int call(int t, struct unity_lines* lines) { + bled_wait ++; + if (bled_wait > 10) { + printf("inject cpu bled.\n"); + time_t now = time(NULL) + 3; + while (time(NULL) <= now); + } + return 0; +} + +void deinit(void) { + printf("cpu bled uninstall\n"); +} diff --git a/source/tools/monitor/unity/collector/plugin/cpu_bled/cpu_bled.h b/source/tools/monitor/unity/collector/plugin/cpu_bled/cpu_bled.h new file mode 100644 index 0000000000000000000000000000000000000000..9272511ab116446fc5ee0bf47ec3e81b26aa5593 --- /dev/null +++ b/source/tools/monitor/unity/collector/plugin/cpu_bled/cpu_bled.h @@ -0,0 +1,14 @@ +// +// Created by 廖肇燕 on 2023/3/21. +// + +#ifndef UNITY_CPU_BLED_H +#define UNITY_CPU_BLED_H + +#include "../plugin_head.h" + +int init(void * arg); +int call(int t, struct unity_lines* lines); +void deinit(void); + +#endif //UNITY_CPU_BLED_H diff --git a/source/tools/monitor/unity/collector/plugin/cpufreq/cpufreq.c b/source/tools/monitor/unity/collector/plugin/cpufreq/cpufreq.c index fe224ec44dd6b450dff7611b8e2062e0de116fac..4648578a771907b73a51e0f83596774a45664cad 100644 --- a/source/tools/monitor/unity/collector/plugin/cpufreq/cpufreq.c +++ b/source/tools/monitor/unity/collector/plugin/cpufreq/cpufreq.c @@ -1,12 +1,21 @@ // // Created by muya. // - #include "cpufreq.h" #include #include +#include +long nr_cpus; int init(void * arg) { + int ret; + + nr_cpus = sysconf(_SC_NPROCESSORS_CONF); + if (nr_cpus < 0) { + ret = errno; + printf("WARN: cpufreq plugin install FAIL sysconf\n"); + return ret; + } printf("cpufreq plugin install, proc: %s\n", get_unity_proc()); return 0; } @@ -14,14 +23,14 @@ int init(void * arg) { int call(int t, struct unity_lines* lines) { struct unity_line* line; int ret; + int num_line, cpu_hw, cpu_cur; FILE *fp = NULL; + char cpu_name[16] = {0}; char str[128]; int len = 0; char result[16] = {0}; - unity_alloc_lines(lines, 1); - line = unity_get_line(lines, 0); - unity_set_table(line, "cpufreq"); + unity_alloc_lines(lines, nr_cpus*2); errno = 0; @@ -30,6 +39,8 @@ int call(int t, struct unity_lines* lines) { printf("WARN: numainfo install FAIL fopen\n"); return ret; } + + num_line = cpu_hw = cpu_cur = 0; while (fgets(str, sizeof(str), fp)) { char *pLast = strstr(str, "@"); if (NULL != pLast) { @@ -40,8 +51,11 @@ int call(int t, struct unity_lines* lines) { pLast++; } memcpy(result, pLast-len, len); - // printf("res is %s\n", result); - unity_set_value(line, 0, "hardware_freq", atof(result)*1000); + snprintf(cpu_name, sizeof(cpu_name), "%d", cpu_hw++); + line = unity_get_line(lines, num_line++); + unity_set_table(line, "cpufreq"); + unity_set_index(line, 0, "core", cpu_name); + unity_set_value(line, 0, "hwFreq", atof(result)*1000); memset(result, 0, 16); len = 0; } else { @@ -55,11 +69,14 @@ int call(int t, struct unity_lines* lines) { pLast2++; } memcpy(result, pLast2-len, len); - // printf("res2 is %s, %d\n", result, len); - unity_set_value(line, 1, "curr_freq", atof(result)); + snprintf(cpu_name, sizeof(cpu_name), "%d", cpu_cur++); + line = unity_get_line(lines, num_line++); + unity_set_table(line, "cpufreq"); + unity_set_index(line, 0, "core", cpu_name); + unity_set_value(line, 0, "curFreq", atof(result)); memset(result, 0, 16); len = 0; - break; + //break; } } } diff --git a/source/tools/monitor/unity/collector/plugin/kmsg/kmsg.c b/source/tools/monitor/unity/collector/plugin/kmsg/kmsg.c index 3f1dd1e7b49e53c58ddec4645a52665a4b63ec9d..e8b028e20bab5fc4e2e62ea8ded698ba115586ac 100644 --- a/source/tools/monitor/unity/collector/plugin/kmsg/kmsg.c +++ b/source/tools/monitor/unity/collector/plugin/kmsg/kmsg.c @@ -9,6 +9,8 @@ #include #include #include +#include +#include #define KMSG_LINE 8192 @@ -34,6 +36,7 @@ static int kmsg_thread_func(struct beeQ* q, void * arg) { int fd; int ret; char buff[KMSG_LINE]; + prctl(PR_SET_NAME, (unsigned long)"kmsg collector"); fd = open("/dev/kmsg", O_RDONLY | O_NONBLOCK); if (fd < 0) { diff --git a/source/tools/monitor/unity/collector/plugin/pmu_events/Makefile b/source/tools/monitor/unity/collector/plugin/pmu_events/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..2d093de697640e13f3e316ed69e435c87cec8e83 --- /dev/null +++ b/source/tools/monitor/unity/collector/plugin/pmu_events/Makefile @@ -0,0 +1,19 @@ +CC := gcc +CFLAG := -g -fpic +LDFLAG := -g -fpic -shared +OBJS := pmu_events.o +SO := libpmu_events.so + +all: $(SO) install + +%.o: %.c + $(CC) -c $< -o $@ $(CFLAG) + +$(SO): $(OBJS) + $(CC) -o $@ $(OBJS) $(LDFLAG) + +install: $(SO) + cp $(SO) ../../native/ + +clean: + rm -f $(SO) $(OBJS) diff --git a/source/tools/monitor/unity/collector/plugin/pmu_events/pmu_events.c b/source/tools/monitor/unity/collector/plugin/pmu_events/pmu_events.c new file mode 100644 index 0000000000000000000000000000000000000000..f92af0f01af5e8d978fc9dd2a0bea08a21d782bd --- /dev/null +++ b/source/tools/monitor/unity/collector/plugin/pmu_events/pmu_events.c @@ -0,0 +1,349 @@ +#include "pmu_events.h" + +struct pmu_events { + int nr_cpus; + struct pcpu_hw_info *pcpu_hwi; +}; + +long nr_cpus; +double summary[NR_EVENTS]; +struct pcpu_hw_info *gpcpu_hwi; +struct pmu_events *glb_pme; +char *events_str[] = {"cycles", "ins", "refCyc", + "llcLoadMis", "llcStoreMis" + "llcLoad", "llcStore"}; +char *value_str[] = {"cycles", "instructions", "CPI", + "llc_load_ref", "llc_load_miss", "LLC_LMISS_RATE" + "llc_store_ref", "llc_store_miss", "LLC_SMIRSS_RATE"}; +/*char origpath[]="/mnt/host/sys/fs/cgroup/perf_event/system.slice/"; */ +char *origpath = NULL; /* defalt to host events */ + +static int init_fail; +static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, + int cpu, int group_fd, unsigned long flags) +{ + int ret; + + ret = syscall(__NR_perf_event_open, hw_event, pid, cpu, + group_fd, flags); + return ret; +} + +int create_hw_events(struct pcpu_hw_info *pc_hwi) +{ + int cpu, i, j, group_last, idx_fail; + int ret, pid, group_leader; + struct hw_info *hwi, *leader; + unsigned long flags = 0; + + hwi = pc_hwi->hwi; + struct perf_event_attr attr = { + .freq = 0, + .disabled = 1, + .sample_period = 1000*1000*1000, + }; + + cpu = pc_hwi->cpu; + pid = pc_hwi->pid; + flags = pc_hwi->flags; + leader = NULL; + group_leader = -1; + j = 0; + group_last = groupidx[0]; + for (i = 0; i < NR_EVENTS; i++) { + /* The next PERF types */ + if (groupidx[i] != group_last) { + group_leader = -1; + group_last = groupidx[i]; + } + attr.type = hw_types[i]; + attr.config = hw_configs[i]; +#ifdef DEBUG + if (cpu == 0) + printf("cpu=%d, type=%d, conf=%llu, pid=%d, gld=%d, flags=%d\n", + cpu, attr.type, attr.config, pid, group_leader, flags); +#endif + hwi[i].fd = perf_event_open(&attr, pid, cpu, group_leader, flags); + if (hwi[i].fd <= 0) { + int ret = errno; + if (ret == ENODEV) { + printf("cpu may OFF LINE\n"); + } else { + fprintf(stderr, "WARN:%s cpu%d event %s \n", + strerror(ret), cpu, events_str[i]); + break; + } + } + hwi[i].leader = group_leader; + /* group leader */ + if (group_leader == -1) { + group_leader = hwi[i].fd; + } + } + if (ret) { + idx_fail = i; + goto fail_open; + } + for (i = 0; i < NR_EVENTS; i++) { + int ret = 0; + if (hwi[i].leader == -1) { + ret = ioctl(hwi[i].fd, PERF_EVENT_IOC_RESET, PERF_IOC_FLAG_GROUP); + if (ret < 0) { + idx_fail = i; + printf("FAIL:ioctl_RESET %s fd=%d fail\n", events_str[i], hwi[i].fd); + goto fail_ioctl; + } + ret = ioctl(hwi[i].fd, PERF_EVENT_IOC_ENABLE, PERF_IOC_FLAG_GROUP); + if (ret < 0) { + idx_fail = i; + printf("FAIL:ioctl_ENABLE %s fd=%d fail\n", events_str[i], hwi[i].fd); + goto fail_ioctl; + } + } + } + return 0; +fail_ioctl: + for (i = 0; i < idx_fail; i++) { + if ((hwi[i].leader == -1) && (hwi[i].fd > 0)) + ioctl(hwi[i].fd, PERF_EVENT_IOC_DISABLE, PERF_IOC_FLAG_GROUP); + idx_fail = NR_EVENTS; + } +fail_open: + for (i = 0; i < idx_fail; i++) { + if (hwi[i].fd > 0) + close(hwi[i].fd); + } + return ret; +} + +struct pmu_events *pme_new(int ncpu) +{ + int ret, fd; + struct pmu_events *pmue; + struct pcpu_hw_info *pcpu_hwi; + + pmue = calloc(sizeof(struct pmu_events), 1); + if (!pmue) { + ret = errno; + fprintf(stderr, "%s :malloc pmu_events fail\n", strerror(ret)); + return NULL; + } + pcpu_hwi = calloc(sizeof(struct pcpu_hw_info), nr_cpus); + if (!pcpu_hwi) { + ret = errno; + fprintf(stderr, "%s :alloc pcpu_hw_info fail\n", strerror(ret)); + free(pmue); + return NULL; + } + pmue->nr_cpus = ncpu; + pmue->pcpu_hwi = pcpu_hwi; +} + +int init(void * arg) +{ + int i, ret, flags, cgroup_fd; + struct pmu_events *pmue; + struct pcpu_hw_info *pcpu_hwi; + + nr_cpus = sysconf(_SC_NPROCESSORS_CONF); + if (nr_cpus <= 0) { + ret = errno; + printf("WARN: pmu_events install FAIL sysconf\n"); + init_fail = ret; + return 0; + } + + pmue = pme_new(nr_cpus); + if (pmue && pmue->pcpu_hwi) { + pcpu_hwi = pmue->pcpu_hwi; + glb_pme = pmue; + } else { + init_fail = -1; + return 0; + } +#if 0 + pmue = (struct pmu_events *)arg; + cgroup_fd = pmue->cgroup_fd; +#endif + if (origpath) { + cgroup_fd = open(origpath, O_RDONLY); + if (cgroup_fd < 0) { + printf(" open %s fail\n", origpath); + init_fail = cgroup_fd; + return 0; + } + flags = PERF_FLAG_PID_CGROUP; + } else { + cgroup_fd = -1; + flags = 0; + } + for (i = 0; i < nr_cpus; i++) { + pcpu_hwi[i].cpu = i; + pcpu_hwi[i].pid = cgroup_fd; + pcpu_hwi[i].flags = flags; + ret = create_hw_events(&pcpu_hwi[i]); + if (ret) { + init_fail = ret; + return 0; + } + } + printf("pmu_events plugin install.\n"); + init_fail = 0; + return 0; +} + +void collect(struct pcpu_hw_info *phw, double *sum) +{ + int i, n; + double *delta = phw->values; + struct hw_info *hw; + + hw = phw->hwi; + for (i = 0; i < NR_EVENTS; i++) { + if (hw[i].fd < 0) + continue; + hw[i].prv_cnt = hw[i].count; + n = read(hw[i].fd, &hw[i].count, sizeof(hw[i].count)); + if (n < 0) + continue; + delta[i] = hw[i].count - hw[i].prv_cnt; + sum[i] += delta[i]; +#ifdef DEBUG + if (phw->cpu == 0) + printf("cpu%d %s prev=%llu, now=%llu\n", + phw->cpu, events_str[i], + hw[i].prv_cnt, hw[i].count); +#endif + } +#ifdef DEBUG + printf("cpu%d cpi=%f\n", phw->cpu, (float)delta[0]/(float)delta[1]); +#endif +} + +int fill_line(struct unity_line *line, double *summ, char *mode, char *index) +{ + int i; + + unity_set_index(line, 0, mode, index); + for (i = 0; i < NR_EVENTS; i++) + unity_set_value(line, i, events_str[i], summ[i]); + + unity_set_value(line, i++, "CPI", + summ[INSTRUCTIONS]==0?0:summ[CYCLES]/summ[INSTRUCTIONS]); + unity_set_value(line, i++, "IPC", + summ[CYCLES]==0?0:summ[INSTRUCTIONS]/summ[CYCLES]); + unity_set_value(line, i++, "MPI", + summ[INSTRUCTIONS]==0?0: + (summ[LLC_LOAD_MISS]+summ[LLC_STORE_MISS])/summ[INSTRUCTIONS]); + unity_set_value(line, i++, "l3LoadMisRate", + summ[LLC_LOAD_REF]==0?0:summ[LLC_LOAD_MISS]/summ[LLC_LOAD_REF]); + unity_set_value(line, i++, "l3StoreMisRate", + summ[LLC_STORE_REF]==0?0:summ[LLC_STORE_MISS]/summ[LLC_STORE_REF]); + unity_set_value(line, i++, "l3MisRate", + (summ[LLC_LOAD_REF]+summ[LLC_STORE_REF])==0?0: + (summ[LLC_LOAD_MISS]+summ[LLC_STORE_MISS])/ + (summ[LLC_LOAD_REF]+summ[LLC_STORE_REF])); +} + +int call(int t, struct unity_lines* lines) +{ + int i; + char index[16] = {0}; + struct unity_line* line; + double summ[NR_EVENTS]; + struct pcpu_hw_info *pcp_hw; + + if (init_fail) { + return init_fail; + } + pcp_hw = glb_pme->pcpu_hwi; + for (i = 0; i < nr_cpus; i++) { + collect(&pcp_hw[i], summ); + } + unity_alloc_lines(lines, 1+nr_cpus); + line = unity_get_line(lines, 0); + unity_set_table(line, "pmu_events"); + fill_line(line, summ, "mode", "node"); + + for (i = 0; i < nr_cpus; i++) { + line = unity_get_line(lines, 1+i); + unity_set_table(line, "pmu_events_percpu"); + snprintf(index, sizeof(index), "%d", i); + fill_line(line, pcp_hw[i].values, "core", index); + } + return 0; +} + +void remove_events(struct pcpu_hw_info *pcpu_hwi) +{ + + int i; + struct hw_info *hw; + + hw = pcpu_hwi->hwi; + for (i = 0; i < NR_EVENTS; i++) { + if ((hw[i].leader == -1) && (hw[i].fd > 0)) + ioctl(hw[i].fd, PERF_EVENT_IOC_DISABLE, PERF_IOC_FLAG_GROUP); + if (hw[i].fd > 0) { + close(hw[i].fd); + } + } +} + +void deinit(void) +{ + int i; + struct pcpu_hw_info *pcp_hw; + + if (glb_pme) { + pcp_hw = glb_pme->pcpu_hwi; + if (pcp_hw) { + for (i = 0; i < nr_cpus; i++) { + remove_events(&pcp_hw[i]); + } + free(pcp_hw); + } + free(glb_pme); + } + printf("pmu_events plugin uninstall\n"); +} + +#ifdef DEBUG +/* for dev/selftest */ +int call_debug(void) +{ + int i; + struct pcpu_hw_info *pcp_hw; + + pcp_hw = glb_pme->pcpu_hwi; + for (i = 0; i < nr_cpus; i++) { + collect(&pcp_hw[i], summary); + } +} +int main(int argc, char *argv[]) +{ + int i = 4; + + init(NULL); + call_debug(); + while(i > 0) { + sleep(1); + memset(summary, 0, sizeof(summary)); + call_debug(); + if (summary[INSTRUCTIONS]) + printf("CPI=%f\n", summary[CYCLES]/summary[INSTRUCTIONS]); + if (summary[INSTRUCTIONS]) + printf("RCPI=%f\n", summary[REF_CYCLES]/summary[INSTRUCTIONS]); + if (summary[LLC_LOAD_REF]) + printf("LLC_LOAD_MISS RATE =%f\n", summary[LLC_LOAD_MISS]/summary[LLC_LOAD_REF]); + if (summary[LLC_STORE_REF]) + printf("LLC_STORE_MISS RATE =%f\n", summary[LLC_STORE_MISS]/summary[LLC_STORE_REF]); + if ((summary[LLC_LOAD_REF]+summary[LLC_STORE_REF]) != 0) + printf("LLC_MISS RATE=%f\n", + (summary[LLC_LOAD_MISS]+summary[LLC_STORE_MISS])/ + (summary[LLC_LOAD_REF]+summary[LLC_STORE_REF])); + i--; + } + deinit(); +} +#endif diff --git a/source/tools/monitor/unity/collector/plugin/pmu_events/pmu_events.h b/source/tools/monitor/unity/collector/plugin/pmu_events/pmu_events.h new file mode 100644 index 0000000000000000000000000000000000000000..89994a4063a76b69e2e492e1ba7178463a815995 --- /dev/null +++ b/source/tools/monitor/unity/collector/plugin/pmu_events/pmu_events.h @@ -0,0 +1,108 @@ +#ifndef UNITY_PMU_EVENT_H +#define UNITY_PMU_EVENT_H +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "../plugin_head.h" +#define NR_GROUP 2 +#define NR_EVENTS 7 +#define NR_CELL 2 + + +#ifdef DEBUG +/* for test */ +__u32 hw_types[] = { + PERF_TYPE_SOFTWARE, + PERF_TYPE_SOFTWARE, + PERF_TYPE_SOFTWARE, + PERF_TYPE_SOFTWARE, + PERF_TYPE_SOFTWARE, + PERF_TYPE_SOFTWARE, + PERF_TYPE_SOFTWARE, +}; + +__u64 hw_configs[] = { + PERF_COUNT_SW_CPU_CLOCK, + PERF_COUNT_SW_CONTEXT_SWITCHES, + PERF_COUNT_SW_TASK_CLOCK, + PERF_COUNT_SW_PAGE_FAULTS, + PERF_COUNT_SW_PAGE_FAULTS_MIN, + PERF_COUNT_SW_CPU_MIGRATIONS, + PERF_COUNT_SW_PAGE_FAULTS_MAJ, +}; +#endif + +__u32 hw_types[] = { + PERF_TYPE_HARDWARE, + PERF_TYPE_HARDWARE, + PERF_TYPE_HARDWARE, + PERF_TYPE_HW_CACHE, + PERF_TYPE_HW_CACHE, + PERF_TYPE_HW_CACHE, + PERF_TYPE_HW_CACHE, +}; + +__u64 hw_configs[] = { + PERF_COUNT_HW_CPU_CYCLES, + PERF_COUNT_HW_INSTRUCTIONS, + PERF_COUNT_HW_REF_CPU_CYCLES, + PERF_COUNT_HW_CACHE_LL << 0 | + (PERF_COUNT_HW_CACHE_OP_READ << 8) | + (PERF_COUNT_HW_CACHE_RESULT_MISS << 16), + PERF_COUNT_HW_CACHE_LL << 0 | + (PERF_COUNT_HW_CACHE_OP_WRITE << 8) | + (PERF_COUNT_HW_CACHE_RESULT_MISS << 16), + PERF_COUNT_HW_CACHE_LL << 0 | + (PERF_COUNT_HW_CACHE_OP_READ << 8) | + (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16), + PERF_COUNT_HW_CACHE_LL << 0 | + (PERF_COUNT_HW_CACHE_OP_WRITE << 8) | + (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16), +}; + +int groupidx[NR_EVENTS] = { + 0,0,0,0,0, + 1,1, +}; + +enum { + CYCLES, + INSTRUCTIONS, + REF_CYCLES, + LLC_LOAD_MISS, + LLC_STORE_MISS, + LLC_LOAD_REF, + LLC_STORE_REF, +}; + +struct hw_info { + int fd, leader; + __u32 type; + __u64 config; + __u64 count, prv_cnt; +}; + +struct pcpu_hw_info { + pid_t pid; + int cpu; + unsigned long flags; + double values[NR_EVENTS]; + struct hw_info hwi[NR_EVENTS]; +}; + +int init(void * arg); +int call(int t, struct unity_lines* lines); +void deinit(void); + +#endif //UNITY_PMU_EVENT_H diff --git a/source/tools/monitor/unity/collector/plugin/proto_sender.c b/source/tools/monitor/unity/collector/plugin/proto_sender.c index 552e6068910b5ed9e65b005311bba83898a44a67..36a5dd450858f366c27a4d925d0d8bec0e58e605 100644 --- a/source/tools/monitor/unity/collector/plugin/proto_sender.c +++ b/source/tools/monitor/unity/collector/plugin/proto_sender.c @@ -5,6 +5,8 @@ #include "proto_sender.h" #define _GNU_SOURCE #include +#include +#include #include #define PROTO_QUEUE_SIZE 64 @@ -165,9 +167,11 @@ int proto_send_proc(void* msg, struct beeQ* q) { return ret; } +// 这是接收外部非lua 推送队列的操作 static int proto_recv_setup(struct beeQ* q) { lua_State *L; struct beeQ* pushQ = (struct beeQ*)(q->qarg); + prctl(PR_SET_NAME, (unsigned long)"proto_recv"); L = proto_sender_lua(pushQ); if (L == NULL) { diff --git a/source/tools/monitor/unity/collector/pluginManager.lua b/source/tools/monitor/unity/collector/pluginManager.lua new file mode 100644 index 0000000000000000000000000000000000000000..4943112169617d5ecb8a8d42ba8fbc7d2b0f71f0 --- /dev/null +++ b/source/tools/monitor/unity/collector/pluginManager.lua @@ -0,0 +1,57 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/3/21 8:30 AM +--- + +require("common.class") +local CpluginManager = class("pluginManager") +local CguardSched = require("collector.guard.guardSched") +local Cplugin = require("collector.plugin") + +function CpluginManager:_init_(procffi, proto_q, resYaml, tid, jperiod) + local res = resYaml + self:setProcSys(procffi, res.config) + + self._sig_cffi = procffi["cffi"] + self._sig_cffi.ffi_plugin_init() + + self._plugins = {} + self._names = {} + self:setup(res, proto_q) + self._guardSched = CguardSched.new(tid, self._plugins, self._names, jperiod) +end + +function CpluginManager:_del_() + self._sig_cffi.plugin_stop() + for _, plugin in ipairs(self._plugins) do + plugin = nil + end +end + +function CpluginManager:setup(resYaml, proto_q) + local pluginFFI = require("collector.native.plugincffi") + local plugins = resYaml.plugins + + for _, plugin in ipairs(plugins) do + local so = plugin.so + if so then + table.insert(self._plugins, Cplugin.new(resYaml, pluginFFI, proto_q, so)) + table.insert(self._names, so) + end + end +end + +function CpluginManager:setProcSys(procFFI, config) + local proc = config["proc_path"] or "/" + local sys = config["sys_path"] or "/" + + procFFI.cffi.ffi_set_unity_proc(procFFI.ffi.string(proc)) + procFFI.cffi.ffi_set_unity_sys(procFFI.ffi.string(sys)) +end + +function CpluginManager:proc(t, lines) + self._guardSched:proc(t, lines) +end + +return CpluginManager diff --git a/source/tools/monitor/unity/collector/podMan/podFilter.lua b/source/tools/monitor/unity/collector/podMan/podFilter.lua new file mode 100644 index 0000000000000000000000000000000000000000..f64466512c0cf30e450ae50d9ea74fdeab2ed077 --- /dev/null +++ b/source/tools/monitor/unity/collector/podMan/podFilter.lua @@ -0,0 +1,184 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/3/28 11:27 PM +--- + +require("common.class") + +local dirent = require("posix.dirent") +local unistd = require("posix.unistd") +local stat = require("posix.sys.stat") +local bit = require("bit") +local pystring = require("common.pystring") +local system = require("common.system") +local Cinotifies = require("common.inotifies") + +local CpodFilter = class("podFiler") + +local podFiler = "^kubepods%-*" +local dockerFilter = "^cri%-containerd%-*" + +local function listSrc(path) + local res = {} + local ok, files = pcall(dirent.files, path) + if not ok then + return res + end + local c = 1 + for f in files do + if not pystring:startswith(f, ".") then + res[c] = f + c = c + 1 + end + end + return res +end + +local function addDirs(dirs, path) + if not system:valueIsIn(dirs, path) then + table.insert(dirs, path) + end +end + +local function setupPlugins(res, proto, pffi, mnt, dirs) + local c = 0 + local plugins = {} + + for _, dir in ipairs(dirs) do + local ls = { + { + name = "path", + index = dir, + } + } + for _, plugin in ipairs(res.container.luaPlugin) do + local CProcs = require("collector.container." .. plugin) + local plug = CProcs.new(proto, pffi, mnt, dir, ls) + if plug.pFile and unistd.access(plug.pFile) then + c = c + 1 + plugins[c] = plug + end + end + end + + return plugins +end + +function CpodFilter:_init_(resYaml, proto, pffi, mnt) + local topDir = mnt .. "sys/fs/cgroup" + if unistd.access(topDir) then + self._top = topDir + else + error("podFilter: cannot visit top dir " .. topDir) + end + + self._resYaml = resYaml + self._proto = proto + self._pffi = pffi + self._mnt = mnt + + self._ino = Cinotifies.new() + self._dirs = self:walkTops1(self._resYaml.container) + self._plugins = setupPlugins(self._resYaml, self._proto, self._pffi, self._mnt, self._dirs) + print("add " .. #self._plugins) +end + +function CpodFilter:enum1LDirs(root, format, parent, dirs) + local alldirs = listSrc(root) + for _, file in ipairs(alldirs) + do + local destPath = root..'/'..file + local destentry = parent..'/'..file + if string.match('/'..file, format) then + self._ino:add(destPath) + addDirs(dirs, destentry) + end + end + return dirs +end + +function CpodFilter:walkTops1(resYaml) + local cgroups = {"cpuacct", "memory", "blkio", "perf_event"} + local dirs = system:deepcopy(resYaml.directCgPath) + + for i,cg in ipairs(cgroups) do + for _, value in ipairs(resYaml.indirectCgPath1) do + if nil == value.child1 then + goto continue + end + local level1 = {} + local root = self._top.."/"..cg + self:enum1LDirs(root..value.path, value.child1, value.path, level1) + for _, parent in ipairs(level1) do + addDirs(dirs, parent) + if nil ~= value.child2 then + self:enum1LDirs(root..parent, value.child2, parent, dirs) + end + end + ::continue:: + end + end + return dirs +end + +function CpodFilter:walkTops(resYaml) + local topDirs = {"cpuacct", "memory", "blkio", "perf_event"} + local dirs = system:deepcopy(resYaml.directCgPath) + + for _, top in ipairs(topDirs) do + local top_s = pystring:join("/", {self._top, top}) + for _, filter in ipairs(resYaml.indirectCgPath) do + local filter_s = pystring:join("/", {top_s, filter}) + local srcs = listSrc(filter_s) + for _, src in ipairs(srcs) do + if string.match(src, podFiler) then + addDirs(dirs, pystring:join("/", {filter, src})) + local pod_s = pystring:join("/", {filter_s, src}) + self._ino:add(pod_s) + local dockers = listSrc(pod_s) + for _, docker in ipairs(dockers) do + if string.match(docker, dockerFilter) then + self._ino:add(pystring:join("/", {pod_s, docker})) + addDirs(dirs, pystring:join("/", {filter, src, docker})) + end + end + end + end + end + end + return dirs +end + +function CpodFilter:proc(elapsed, lines) + local ret, delta + local rec = {} + if self._ino:isChange() then + print("cgroup changed.") + local start = lua_local_clock() + self._ino = Cinotifies.new() + self._dirs = self:walkTops1(self._resYaml.container) + for _, plugin in ipairs(self._plugins) do + pcall(plugin.releaseEvents, plugin) + end + self._plugins = setupPlugins(self._resYaml, self._proto, self._pffi, self._mnt, self._dirs) + collectgarbage("collect") + local stop = lua_local_clock() + ret, delta = 1, stop - start + return ret,delta + end + for i, plugin in ipairs(self._plugins) do + --local res = plugin:proc(elapsed, lines) + local stat, res = pcall(plugin.proc, plugin, elapsed, lines) + if not stat or res == -1 then + table.insert(rec, i) + end + end + + for _, i in ipairs(rec) do -- del bad plugin + self._plugins[i] = nil + end + return ret, delta +end + +return CpodFilter diff --git a/source/tools/monitor/unity/collector/podMan/podsAll.lua b/source/tools/monitor/unity/collector/podMan/podsAll.lua new file mode 100644 index 0000000000000000000000000000000000000000..9e81eba0797e65ca221633afb630caf287fe983b --- /dev/null +++ b/source/tools/monitor/unity/collector/podMan/podsAll.lua @@ -0,0 +1,181 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/3/15 6:00 PM +--- + +require("common.class") + +local ChttpCli = require("httplib.httpCli") +local system = require("common.system") +local pystring = require("common.pystring") +local Cinotifies = require("common.inotifies") +local unistd = require("posix.unistd") + +local CpodsAll = class("podsApi") + +local function spiltConId(conId) + local res = pystring:split(conId, "//", 1) + return res[2] +end + +local function getRuntime(mnt) + if unistd.access(mnt .. "var/run/docker/runtime-runc/moby/") == 0 then + return "docker" + end + return "cri-containerd" +end + +local function joinNPath(cell, runtime) + -- "/sys/fs/cgroup/cpu/kubepods.slice/kubepods-${qos}.slice/kubepods-${qos}-pod${podid}.slice/${runtime}-${cid}.scope" + local paths = { + "/kubepods.slice/kubepods-", + cell.pod.qos, + ".slice/kubepods-", + cell.pod.qos, + "-pod", + cell.pod.uid, + ".slice/", + runtime, + "-", + cell.id, + ".scope" + } + return pystring:join("", paths) +end + +local function joinGPath(cell, runtime) + -- "/sys/fs/cgroup/cpu/kubepods.slice/kubepods-pod${podid}.slice/${runtime}-${cid}.scope" + local paths = { + "/kubepods.slice/kubepods-pod", + cell.pod.uid, + ".slice/", + runtime, + "-", + cell.id, + ".scope" + } + return pystring:join("", paths) +end + +local function joinPath(cell, runtime) + if cell.pod.qos == "guaranteed" then + return joinGPath(cell, runtime) + else + return joinNPath(cell, runtime) + end +end + +local function setupCons(res) + local mnt = res.config.proc_path + local runtime = getRuntime(mnt) + local cli = ChttpCli.new() + local cons = {} + local c = 0 + + local content = cli:get("http://127.0.0.1:10255/pods") + local obj = cli:jdecode(content.body) + + for _, pod in ipairs(obj.items) do + local metadata = pod.metadata + local lpod = {name = metadata.name, + namespace = metadata.namespace, + uid = pystring:replace(metadata.uid, "-", "_"), + qos = pystring:lower(pod.status.qosClass), + } + + local containerStatuses = pod.status.containerStatuses + for _, con in ipairs(containerStatuses) do + local cell = { + pod = lpod, + name = con.name, + id = spiltConId(con.containerID) + } + cell.path = joinPath(cell, runtime) + if unistd.access(mnt .. "sys/fs/cgroup/cpu/" .. cell.path) == 0 then + c = c + 1 + cons[c] = cell + end + end + end + + return cons +end + +local function setupPlugins(res, proto, pffi, mnt, ino) + local c = 0 + local cons = setupCons(res) + local plugins = {} + + for _, con in ipairs(cons) do + local ls = { + { + name = "pod_name", + index = con.pod.name, + }, + { + name = "con_name", + index = con.name, + }, + { + name = "qos", + index = con.pod.qos, + }, + { + name = "ns", + index = con.pod.namespace, + }, + { + name = "uid", + index = con.pod.uid, + }, + { + name = "con_id", + index = con.id, + }, + } + + for _, plugin in ipairs(res.container.luaPlugin) do + local CProcs = require("collector.container." .. plugin) + c = c + 1 + plugins[c] = CProcs.new(proto, pffi, mnt, con.path, ls) + ino:add(plugins[c].pFile) + end + end + + return plugins +end + +function CpodsAll:_init_(resYaml, proto, pffi, mnt) + self._monDir = mnt .. "sys/fs/cgroup/" + self._resYaml = resYaml + self._proto = proto + self._pffi = pffi + self._mnt = mnt + + self._ino = Cinotifies.new() + self._plugins = setupPlugins(self._resYaml, self._proto, self._pffi, self._mnt, self._ino) + print( "pods plugin add " .. #self._plugins) +end + +function CpodsAll:proc(elapsed, lines) + local rec = {} + if self._ino:isChange() then + print("cgroup changed.") + self._ino = Cinotifies.new() + self._plugins = setupPlugins(self._resYaml, self._proto, self._pffi, self._mnt, self._ino) + end + for i, plugin in ipairs(self._plugins) do + --local res = plugin:proc(elapsed, lines) + local stat, res = pcall(plugin.proc, plugin, elapsed, lines) + if not stat or res == -1 then + table.insert(rec, i) + end + end + + for _, i in ipairs(rec) do -- del bad plugin + self._plugins[i] = nil + end +end + +return CpodsAll diff --git a/source/tools/monitor/unity/collector/pod_allocpage.lua b/source/tools/monitor/unity/collector/pod_allocpage.lua index 23a2f4defc854729dc982aa8e03a8800b506de3e..078554067fed2944ba6c7f453bc2585839b6eeab 100644 --- a/source/tools/monitor/unity/collector/pod_allocpage.lua +++ b/source/tools/monitor/unity/collector/pod_allocpage.lua @@ -10,8 +10,6 @@ local unistd = require("posix.unistd") local dirent = require("posix.dirent") local stdlib = require("posix.stdlib") local stat = require("posix.sys.stat") -local cjson = require("cjson") -local json = cjson.new() local CkvProc = require("collector.kvProc") local CvProc = require("collector.vproc") local pystring = require("common.pystring") @@ -22,7 +20,8 @@ local CPodAlloc = class("podalloc", CkvProc) function CPodAlloc:_init_(proto, pffi, mnt, pFile) CkvProc._init_(self, proto, pffi, mnt, pFile , "pod_alloc") self._ffi = require("collector.native.plugincffi") - self.proc_fs, self.sys_fs, self.pods_fs, self.root_fs = dockerinfo:get_hostfs() + self.root_fs = mnt + self.proc_fs = mnt .. "/proc/" self.name_space = {} self.pod_mem = {} self.total = 0 @@ -42,18 +41,21 @@ function CPodAlloc:switch_ns(pid) if not self:file_exists(pid_ns) then return end local f = fcntl.open(pid_ns,fcntl.O_RDONLY) - self._ffi.C.setns(f,0) + if f == nil then return false end + local res = self._ffi.C.setns(f,0) + if res == -1 then return false end unistd.close(f) + return true end function CPodAlloc:get_container_info(did) - local res = "unknow" + local restable = {} local podname = did local podns = did local cname = did - res = dockerinfo:get_inspect(did) - local restable = json.decode(res) + restable = dockerinfo:get_inspect(did, self.root_fs) + if not restable then return podname end if #restable > 0 then restable = restable[1] end @@ -96,7 +98,8 @@ function CPodAlloc:get_pidalloc() for net,pidn in pairs(self.name_space) do if pidn == "self" then pidn = "1" end - self:switch_ns(pidn) + local ns_res = self:switch_ns(pidn) + if not ns_res then goto continue end -- local env = posix.getenv() -- env["PROC_ROOT"] = self.proc_fs @@ -113,7 +116,7 @@ function CPodAlloc:get_pidalloc() local dockerid = "" if not dockerids[pid] then - dockerid = dockerinfo:get_dockerid(pid) + dockerid = dockerinfo:get_dockerid(pid, self.root_fs) if dockerid == "unknow" then break end dockerids[pid] = dockerid else @@ -142,6 +145,7 @@ function CPodAlloc:get_pidalloc() pfile:close() self:switch_ns("1") stdlib.setenv("PROC_ROOT","") + ::continue:: end end diff --git a/source/tools/monitor/unity/collector/postEngine/engine.lua b/source/tools/monitor/unity/collector/postEngine/engine.lua new file mode 100644 index 0000000000000000000000000000000000000000..3dde0a862fa5c61487a38b47e8eb3eb11d1062ce --- /dev/null +++ b/source/tools/monitor/unity/collector/postEngine/engine.lua @@ -0,0 +1,66 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/3/25 4:21 PM +--- + +require("common.class") +local CprotoData = require("common.protoData") +local postQue = require("beeQ.postQue.postQue") +local CvProto = require("collector.vproto") +local pystring = require("common.pystring") +local system = require("common.system") +local cjson = require("cjson.safe") + +local Cengine = class("engine", CvProto) + +function Cengine:_init_(que, proto_q, fYaml, tid) + CvProto._init_(self, CprotoData.new(que)) + self._fYaml = fYaml + self._tid = tid + self._task = nil +end + +function Cengine:setTask(taskMons) + self._task = taskMons +end + +function Cengine:pushTask(msgs) + local events = pystring:split(msgs, '\n') + for _, msg in ipairs(events) do + print(msg) + local res = cjson.decode(msg) + if res.cmd == "mon_pid" then + self._task:add(res.pid, res.loop) + end + end +end + +function Cengine:proc(t, event, msgs) + local lines = self._proto:protoTable() + + CvProto.proc(self, t) + --local logs = pystring:split(msgs, '\n') + local logList = {} + + logList[1] = { + name = "post", + log = cjson.encode(msgs) + } + self:appendLine(self:_packProto("post_que", nil, nil, logList)) + self:push(lines) + + local bytes = self._proto:encode(lines) + self._proto:que(bytes) + + self:pushTask(msgs) +end + +function Cengine:work(t, event) + local msgs = postQue.pull() + if msgs then + self:proc(t, event, msgs) + end +end + +return Cengine diff --git a/source/tools/monitor/unity/collector/postPlugin/postPlugin.lua b/source/tools/monitor/unity/collector/postPlugin/postPlugin.lua new file mode 100644 index 0000000000000000000000000000000000000000..b485c05d724d24fe25558f2a1db0d04dc20f2c3d --- /dev/null +++ b/source/tools/monitor/unity/collector/postPlugin/postPlugin.lua @@ -0,0 +1,20 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/3/26 10:24 PM +--- + +require("common.class") + +local CtaskMons = require("collector.postPlugin.taskMons") +local CpostPlugin = class("") + +function CpostPlugin:_init_(proto, pffi, rYaml) + self.tasks = CtaskMons.new(proto, pffi, rYaml) +end + +function CpostPlugin:proc(elapsed, lines) + self.tasks:proc(elapsed, lines) +end + +return CpostPlugin diff --git a/source/tools/monitor/unity/collector/postPlugin/taskMon.lua b/source/tools/monitor/unity/collector/postPlugin/taskMon.lua new file mode 100644 index 0000000000000000000000000000000000000000..b0f409fcd4bb07c5097c219c44b94545e1378262 --- /dev/null +++ b/source/tools/monitor/unity/collector/postPlugin/taskMon.lua @@ -0,0 +1,141 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/3/27 11:46 AM +--- + +require("common.class") +local pystring = require("common.pystring") +local CvProc = require("collector.vproc") +local system = require("common.system") + +local CtaskMon = class("taskMon", CvProc) + +local function getInfo(fstat) + local s + for line in io.lines(fstat) do + s = line + end + local ss = pystring:rsplit(s, ") ", 1) + local comm = pystring:split(ss[1], "(", 1) + + local rest = ss[2] + local vs = pystring:split(rest, " ") + local user, sys = tonumber(vs[12]), tonumber(vs[13]) + return comm[2], user, sys +end + +local function getStatus(fstatus) + local ctxt1, ctxt2 = 0, 0 + for line in io.lines(fstatus) do + if pystring:startswith(line, "voluntary_ctxt_switches") then + local kvs = pystring:split(line) + ctxt1 = tonumber(kvs[2]) + elseif pystring:startswith(line, "nonvoluntary_ctxt_switches") then + local kvs = pystring:split(line) + ctxt2 = tonumber(kvs[2]) + end + end + return ctxt1, ctxt2 +end + +function CtaskMon:_init_(proto, pffi, mnt, pid, loop) + CvProc._init_(self, proto, pffi, mnt, nil) + self._loop = loop or -1 + self._fstat = mnt .. 'proc/' .. pid .. '/stat' + self._fstatus = mnt .. 'proc/' .. pid .. '/status' + self._comm, self._lastUser, self._lastSys = getInfo(self._fstat) + self._ls = { + { + name = "pid", + index = tostring(pid), + }, + { + name = "comm", + index = self._comm, + }, + } + self._ctxt, self._non_ctxt = getStatus(self._fstatus) +end + +function CtaskMon:stat() + local s + for line in io.lines(self._fstat) do + s = line + end + if s then + local ss = pystring:rsplit(s, ") ", 1) + local rest = ss[2] + local vs = pystring:split(rest, " ") + local user, sys = tonumber(vs[12]), tonumber(vs[13]) + local vsize, rss = tonumber(vs[21]), tonumber(vs[22]) + + local ret = { + { + name = "vsize", + value = vsize, + }, + { + name = "rss", + value = rss * 4096, + }, + { + name = "user", + value = user - self._lastUser + }, + { + name = "sys", + value = sys - self._lastSys + }, + } + self._lastUser = user + self._lastSys = sys + return ret + else + return nil + end +end + +function CtaskMon:status(vs) + for line in io.lines(self._fstatus) do + if pystring:startswith(line, "voluntary_ctxt_switches") then + local kvs = pystring:split(line) + local v = tonumber(kvs[2]) + table.insert(vs, {name="voluntary_ctxt_switches", value = v - self._ctxt}) + self._ctxt = v + elseif pystring:startswith(line, "nonvoluntary_ctxt_switches") then + local kvs = pystring:split(line) + local v = tonumber(kvs[2]) + table.insert(vs, {name="nonvoluntary_ctxt_switches", value = v - self._non_ctxt}) + self._non_ctxt = v + end + end +end + +function CtaskMon:proc(elapsed, lines) + local vs + + CvProc.proc(self) + local ls = {{ + name = "pid", + index = tostring(self.pid) + }} + + vs = self:stat() + if vs == nil then + return -1 + end + self:status(vs) + system:dumps(vs) + self:appendLine(self:_packProto("mon_task", self._ls, vs)) + self:push(lines) + + if self._loop > 0 then + self._loop = self._loop - 1 + if self._loop <= 0 then + return -1 + end + end +end + +return CtaskMon diff --git a/source/tools/monitor/unity/collector/postPlugin/taskMons.lua b/source/tools/monitor/unity/collector/postPlugin/taskMons.lua new file mode 100644 index 0000000000000000000000000000000000000000..8a08bb95dbada96e0a120f795d9a9915d311d8e7 --- /dev/null +++ b/source/tools/monitor/unity/collector/postPlugin/taskMons.lua @@ -0,0 +1,58 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/3/27 1:52 PM +--- + +require("common.class") +local CtaskMon = require("collector.postPlugin.taskMon") +local unistd = require("posix.unistd") +local CvProto = require("collector.vproto") +local CtaskMons = class("taskMons", CvProto) + +function CtaskMons:_init_(proto, pffi, rYaml) + CvProto._init_(self, proto) + self._pffi = pffi + self._mnt = rYaml.config.proc_path + self._limit = rYaml.config.limit.tasks or 10 + self._taskList = {} +end + +function CtaskMons:add(pid, loop) + local lines = self._proto:protoTable() + CvProto.proc(self, 1) + + if #self._taskList >= self._limit then + self:packLog("post_req", "post", "task pids already overflow.") + else + if self._taskList[pid] then -- already in + self:packLog("post_req", "post", "pid " .. pid .. " is already in mon list.") + else -- check if in proc + local dPath = self._mnt .. 'proc/' .. pid + local res = unistd.access(dPath, 'r') + if res then -- exist + self._taskList[pid] = CtaskMon.new(self._proto, self._pffi, self._mnt, pid, loop) + self:packLog("post_req", "post", "add pid " .. pid .. " to monitor.") + else + self:packLog("post_req", "post", "pid " .. pid .. " is not exist.") + end + end + end + self:push(lines) + + local bytes = self._proto:encode(lines) + self._proto:que(bytes) +end + +function CtaskMons:proc(elapsed, lines) + local res + + for pid, task in pairs(self._taskList) do + res = task:proc(elapsed, lines) + if res == -1 then + self._taskList[pid] = nil + end + end +end + +return CtaskMons diff --git a/source/tools/monitor/unity/collector/proc_bled.lua b/source/tools/monitor/unity/collector/proc_bled.lua new file mode 100644 index 0000000000000000000000000000000000000000..4ba427d6511fc25b87d0a8259d29c178249ddbbc --- /dev/null +++ b/source/tools/monitor/unity/collector/proc_bled.lua @@ -0,0 +1,30 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/3/21 12:08 AM +--- +--- + +require("common.class") +local CvProc = require("collector.vproc") + +local CprocBled = class("procArp", CvProc) + +function CprocBled:_init_(proto, pffi, mnt, pFile) + CvProc._init_(self, proto, pffi, mnt, pFile or "dummy") + self._record = 1 +end + +function CprocBled:proc(elapsed, lines) + print("this only a test case for bled " .. self._record) + self._record = self._record + 1 + if self._record > 5 then + print("trig full cpu.") + local t = os.time() + 2 + while os.time() < t do + local c = 1 + end + end +end + +return CprocBled diff --git a/source/tools/monitor/unity/collector/proc_buddyinfo.lua b/source/tools/monitor/unity/collector/proc_buddyinfo.lua index e324c0d56c989f2972c2b045cf743453ee8b3e88..bd4e28613d57d35251ca36355da266db9bc959e5 100644 --- a/source/tools/monitor/unity/collector/proc_buddyinfo.lua +++ b/source/tools/monitor/unity/collector/proc_buddyinfo.lua @@ -33,7 +33,7 @@ function CprocBuddyinfo:proc(elapsed, lines) end end - if not buddyinfo then + if #buddyinfo == 0 then for line in io.lines(self.pFile) do if string.find(line,"DMA32") then local subline = pystring:split(line,"DMA32",1)[2] diff --git a/source/tools/monitor/unity/collector/proc_interrupts.lua b/source/tools/monitor/unity/collector/proc_interrupts.lua index 9b66c3447edb5c46caaf01dca5c17098f79cb355..fc2269ddb41d3470956474a69d582cc1d2998f4b 100644 --- a/source/tools/monitor/unity/collector/proc_interrupts.lua +++ b/source/tools/monitor/unity/collector/proc_interrupts.lua @@ -8,12 +8,17 @@ require("common.class") local pystring = require("common.pystring") local CvProc = require("collector.vproc") local unistd = require("posix.unistd") +local system = require("common.system") local Cinterrupts = class("interrupts", CvProc) function Cinterrupts:_init_(proto, pffi, mnt, pFile) CvProc._init_(self, proto, pffi, mnt, pFile or "proc/interrupts") - self._cpus = unistd.sysconf(84) + local err, errno + self._cpus, err, errno = unistd.sysconf(84) + if err then + system:posixError("sysconf failed", err, errno) + end end function Cinterrupts:proc(elapsed, lines) diff --git a/source/tools/monitor/unity/collector/vproc.lua b/source/tools/monitor/unity/collector/vproc.lua index 05024bc8e2ebfd9a608aa97f838ffdd6324b7366..60a1a54483ac06747baad5f92df6aded6f4a60be 100644 --- a/source/tools/monitor/unity/collector/vproc.lua +++ b/source/tools/monitor/unity/collector/vproc.lua @@ -5,12 +5,12 @@ --- require("common.class") -local system = require("common.system") +local CvProto = require("collector.vproto") -local CvProc = class("collector.vproc") +local CvProc = class("collector.vproc", CvProto) function CvProc:_init_(proto, pffi, mnt, pFile) - self._proto = proto + CvProto._init_(self, proto) self._cffi = pffi["cffi"] self._ffi = pffi["ffi"] mnt = mnt or "/" @@ -18,29 +18,4 @@ function CvProc:_init_(proto, pffi, mnt, pFile) self.pFile = mnt .. pFile end -function CvProc:proc(elapsed) - self._lines = self._proto:protoTable() -end - -function CvProc:appendLine(line) - table.insert(self._lines["lines"], line) -end - -function CvProc:copyLine(line) - self:appendLine(system:deepcopy(line)) -end - -function CvProc:push(lines) - local c = #lines["lines"] -- not for #lines - for _, line in ipairs(self._lines["lines"]) do - c = c + 1 - lines["lines"][c] = line - end - self._lines = nil -end - -function CvProc:_packProto(head, labels, vs, log) - return {line = head, ls = labels, vs = vs, log = log} -end - return CvProc \ No newline at end of file diff --git a/source/tools/monitor/unity/collector/vproto.lua b/source/tools/monitor/unity/collector/vproto.lua new file mode 100644 index 0000000000000000000000000000000000000000..8a638eefd21f02caaeaa9adf616cef0662d05294 --- /dev/null +++ b/source/tools/monitor/unity/collector/vproto.lua @@ -0,0 +1,51 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/3/25 5:28 PM +--- + +require("common.class") +local system = require("common.system") + +local CvProto = class("collector.vproto") + +function CvProto:_init_(proto) + self._proto = proto +end + +function CvProto:proc(elapsed) + self._lines = self._proto:protoTable() +end + +function CvProto:appendLine(line) + table.insert(self._lines["lines"], line) +end + +function CvProto:copyLine(line) + self:appendLine(system:deepcopy(line)) +end + +function CvProto:push(lines) + local c = #lines["lines"] -- not for #lines + for _, line in ipairs(self._lines["lines"]) do + c = c + 1 + lines["lines"][c] = line + end + self._lines = nil +end + +function CvProto:_packProto(head, labels, vs, log) + return {line = head, ls = labels, vs = vs, log = log} +end + +function CvProto:packLog(title, name, msg) + local logList = {} + + logList[1] = { + name = name, + log = msg + } + self:appendLine(self:_packProto(title, nil, nil, logList)) +end + +return CvProto diff --git a/source/tools/monitor/unity/common/dockerinfo.lua b/source/tools/monitor/unity/common/dockerinfo.lua index 9bbce2ced08f0681ad39532d23692996b615f81b..2322eee5ad59a8309bfb350e702220e9daea4d82 100644 --- a/source/tools/monitor/unity/common/dockerinfo.lua +++ b/source/tools/monitor/unity/common/dockerinfo.lua @@ -4,15 +4,16 @@ --- DateTime: 2023/02/08 17:00 PM --- -dockerinfo = {} +local dockerinfo = {} local posix = require("posix") -local cjson = require("cjson") -local json = cjson.new() local pystring = require("common.pystring") local stat = require("posix.sys.stat") +local unistd = require("posix.unistd") +local system = require("common.system") +local api = require("httplib.dockerApi") -function file_exists(file) - local f=stat.lstat(file) +local function file_exists(file) + local f=unistd.access(file) if f ~= nil then return true else @@ -20,31 +21,14 @@ function file_exists(file) end end -function dockerinfo:get_hostfs() - local proc_fs="/mnt/host/proc/" - local sys_fs="/mnt/host/sys/" - local pods_fs="/mnt/host/var/lib/kubelet/pods/" - local root_fs = "/mnt/host/" - if file_exists(proc_fs) then - return proc_fs, sys_fs, pods_fs, root_fs - end - proc_fs="/proc/" - sys_fs="/sys/" - pods_fs="/var/lib/kubelet/pods/" - root_fs = "/" - return proc_fs, sys_fs, pods_fs, root_fs -end - -function get_runtimesock() - local root_fs = "" - _, _, _, root_fs = dockerinfo:get_hostfs() +local function get_runtimesock(root_fs) local runtime = "docker" local runtime_sock = root_fs .. "var/run/docker.sock" - local sock={"var/run/docker.sock","run/containerd/containerd.sock", "var/run/dockershim.sock"} + local sock={"var/run/docker.sock","run/podman/podman.sock","run/containerd/containerd.sock", "var/run/dockershim.sock"} for _,runtimex in pairs(sock) do if file_exists(root_fs .. runtimex) then runtime_sock = root_fs .. runtimex - if not string.find(runtime_sock,"docker.sock") then + if not string.find(runtime_sock,"docker.sock") and not string.find(runtime_sock,"podman.sock") then runtime = "crictl" end end @@ -52,17 +36,76 @@ function get_runtimesock() return runtime,runtime_sock end -function dockerinfo:get_inspect(did) - local runtime,runtime_sock = get_runtimesock() +local function get_container_inspect(did, root_fs) + local runtime, runtime_sock = get_runtimesock(root_fs) + local d_api = api.new('localhost', runtime_sock) + local res = d_api:inspect_container(did) + if res then res = res.body end + return res +end + +local function get_crictl_inspect(did, root_fs) + local runtime, runtime_sock = get_runtimesock(root_fs) + local cmd = runtime .. " -r " .. runtime_sock .. " inspect " .. did .. " 2>/dev/null " + local f = io.popen(cmd,"r") + local res = f:read("*a") + f:close() + return res +end + +function dockerinfo:get_podname_pid(pid, root_fs) + local did = dockerinfo:get_dockerid(pid, root_fs) + return dockerinfo:get_podname_did(did,root_fs) +end + +function dockerinfo:get_podname_did(did, root_fs) + local restable = dockerinfo:get_inspect(did,root_fs) + local podname = did + local podns = did + local cname = did + + if not restable then return did end + if #restable > 0 then + restable = restable[1] + end + if restable['Config'] then + local config = restable['Config'] + if config['Labels'] then + local label = config['Labels'] + if label['io.kubernetes.pod.name'] then + podname = label['io.kubernetes.pod.name'] + end + if label['io.kubernetes.container.name'] then + cname = label['io.kubernetes.container.name'] + end + if label['io.kubernetes.pod.namespace'] then + podns = label['io.kubernetes.pod.namespace'] + end + end + if podname == did and restable['Name'] then + cname = restable['Name'] + podname = restable['Name'] + end + elseif restable['status'] then + podname = restable['status']['labels']['io.kubernetes.pod.name'] + cname = restable['status']['labels']['io.kubernetes.container.name'] + podns = restable['status']['labels']['io.kubernetes.pod.namespace'] + end + if pystring:startswith(podname,"/") then podname=string.sub(podname,2,-1) end + return podname +end + +function dockerinfo:get_inspect(did, root_fs) + local runtime,runtime_sock = get_runtimesock(root_fs) if runtime == "docker" then - return get_container_inspect(did) + return get_container_inspect(did, root_fs) else - return get_crictl_inspect(did) + return get_crictl_inspect(did, root_fs) end end -function dockerinfo:get_dockerid(pid) - local proc_fs = dockerinfo:get_hostfs() +function dockerinfo:get_dockerid(pid, root_fs) + local proc_fs = root_fs .. "/proc/" local idstring = "unknow" if not file_exists(proc_fs .. pid .. "/cgroup") then return idstring end local cmd = "cat " .. proc_fs .. pid .. "/cgroup 2>/dev/null | grep memory:" @@ -70,11 +113,15 @@ function dockerinfo:get_dockerid(pid) local res = pfile:read("*a") pfile:close() - if not string.find(res,"kubepods") and not string.find(res,"docker%-") then return idstring end + if not string.find(res,"kubepods") and not string.find(res,"docker%-") and not string.find(res,"libpod") then return idstring end if string.find(res,"docker%-") then idstring = pystring:split(res,"docker-")[2] elseif string.find(res,"cri%-containerd%-") then idstring = pystring:split(res,"cri-containerd-")[2] + elseif string.find(res,"libpod%-conmon%-") then + idstring = pystring:split(res,"libpod-conmon-")[2] + elseif string.find(res,"libpod%-") then + idstring = pystring:split(res,"libpod-")[2] else local tmp = pystring:split(res,"/",10) idstring = tmp[#tmp] @@ -83,22 +130,4 @@ function dockerinfo:get_dockerid(pid) return idstring end -function get_container_inspect(did) - local runtime, runtime_sock = get_runtimesock() - local cmd = "curl --silent -XGET --unix-socket " .. runtime_sock .. " http://localhost/containers/" .. did .. "/json 2>/dev/null " - local f = io.popen(cmd,"r") - local res = f:read("*a") - f:close() - return res -end - -function get_crictl_inspect(did) - local runtime, runtime_sock = get_runtimesock() - local cmd = runtime .. " -r " .. runtime_sock .. " inspect " .. did .. " 2>/dev/null " - local f = io.popen(cmd,"r") - local res = f:read("*a") - f:close() - return res -end - return dockerinfo diff --git a/source/tools/monitor/unity/common/inotifies.lua b/source/tools/monitor/unity/common/inotifies.lua new file mode 100644 index 0000000000000000000000000000000000000000..82102b4d60d62ce549676c8363f347ef2609ef6f --- /dev/null +++ b/source/tools/monitor/unity/common/inotifies.lua @@ -0,0 +1,100 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/3/24 2:34 PM +--- + +require("common.class") + +local Cinotifies = class("inotifies") +local dirent = require("posix.dirent") +local pstat = require("posix.sys.stat") +local fcntl = require("posix.fcntl") +local system = require("common.system") +local inotify = require('inotify') + +local function walk_dirs(path, dirs) + local files = dirent.files(path) + for f in files do + if string.sub(f, 1, 1) ~= '.' then + local full = table.concat({path, f}, "/") + local lStat = pstat.lstat(full) + if pstat.S_ISDIR(lStat.st_mode) > 0 then + table.insert(dirs, full) + walk_dirs(full, dirs) + end + end + end +end + +local function fdNonBlocking(fd) + local res + local flag, err, errno = fcntl.fcntl(fd, fcntl.F_GETFL) + if flag then + res, err, errno = fcntl.fcntl(fd, fcntl.F_SETFL, bit.bor(flag, fcntl.O_NONBLOCK)) + if res then + return 0 + else + system:posixError("fcntl set failed", err, errno) + end + else + system:posixError("fcntl get failed", err, errno) + end +end + +local function mon_dirs(path) + local handle = inotify.init() + local ws = {} + local c = 0 + + if path then -- if set path then walk all child path + local dirs = {path} + walk_dirs(path, dirs) + + print("watch " .. #dirs .. " files") + for _, dir in ipairs(dirs) do + local w = handle:addwatch(dir, inotify.IN_CREATE, inotify.IN_MOVE, inotify.IN_DELETE) + if w > 0 then + c = c + 1 + ws[c] = w + else + error("add " .. dir .. " to watch failed.") + end + end + end + + fdNonBlocking(handle:getfd()) + return handle, ws +end + +function Cinotifies:_init_(path) + self._handle, self._ws = mon_dirs(path) +end + +function Cinotifies:_del_() + for _, w in ipairs(self._ws) do + self._handle:rmwatch(w) + end + self._handle:close() +end + +function Cinotifies:add(path) + local w = self._handle:addwatch(path, inotify.IN_CREATE, inotify.IN_MOVE, inotify.IN_DELETE) + if w ~= nil then + if w > 0 then + table.insert(self._ws, w) + else + error("add " .. path .. " to watch failed.") + end + end +end + +function Cinotifies:isChange() + local events = self._handle:read() + if events ~=nil then + return #events > 0 + end + return false +end + +return Cinotifies diff --git a/source/tools/monitor/unity/common/pystring.lua b/source/tools/monitor/unity/common/pystring.lua index a499ee85cafe28c4bc836128f696a2595f802e8f..84e5e8b57abffc0ccc727a7d18263c3e59dff0db 100644 --- a/source/tools/monitor/unity/common/pystring.lua +++ b/source/tools/monitor/unity/common/pystring.lua @@ -337,22 +337,7 @@ function pystring:partition(s, del) end end -function pystring:partition(s, del) - local result = {} - del = del or " " - local delimiter = setupDelimiter(del) - local iBeg, iEnd = string.find(s, delimiter) - if iBeg then - result[1] = string.sub(s, 1, iBeg - 1) - result[2] = del - result[3] = string.sub(s, iEnd + 1) - return result - else - return nil - end -end - -function pystring:reverseTable(t) +local function reverseTable(t) local n = #t for i = 1, n / 2 do t[i], t[n + 1 - i] = t[n + 1 - i], t[i] @@ -389,7 +374,7 @@ function pystring:rsplit(s, delimiter, n) end end --return result - pystring:reverseTable(result) + reverseTable(result) return result end diff --git a/source/tools/monitor/unity/common/system.lua b/source/tools/monitor/unity/common/system.lua index ce4f77033eea77f5dd4c4adf4cab230d5c6d5ce6..23526f93f3a05a6d5016e53a9716e0529d38a785 100644 --- a/source/tools/monitor/unity/common/system.lua +++ b/source/tools/monitor/unity/common/system.lua @@ -40,6 +40,13 @@ function system:dumps(t) print(serpent.block(t)) end +function system:reverseTable(t) + local n = #t + for i = 1, n / 2 do + t[i], t[n + 1 - i] = t[n + 1 - i], t[i] + end +end + function system:keyIsIn(tbl, key) if type(tbl) ~= "table" then return false diff --git a/source/tools/monitor/unity/test/bees/run.sh b/source/tools/monitor/unity/test/bees/run.sh index 37055941e5db172dcf2f65bcca4b52416404d7e1..29f6a84eb19d3d35020ce8751d47dfb898c6351e 100755 --- a/source/tools/monitor/unity/test/bees/run.sh +++ b/source/tools/monitor/unity/test/bees/run.sh @@ -13,7 +13,6 @@ cd ../../beeQ || exit 1 [ ! -d ../lib ] && mkdir ../lib cp ./lib/*.so ../lib cp ../tsdb/native/*.so ../lib -rm -rf ../bin [ ! -d ../bin ] && mkdir ../bin cp unity-mon ../bin diff --git a/source/tools/monitor/unity/test/curl/poBeaver/poBeaver.lua b/source/tools/monitor/unity/test/curl/poBeaver/poBeaver.lua index e44e05358d81a7e698a8f20531bab507a7b374a4..7271e4f3c48fb2b19b496bbf44e50cc5099bd635 100644 --- a/source/tools/monitor/unity/test/curl/poBeaver/poBeaver.lua +++ b/source/tools/monitor/unity/test/curl/poBeaver/poBeaver.lua @@ -64,7 +64,7 @@ function CpoBeaver:_install_fd(port, ip, backlog) end local function fdNonBlocking(fd) - local res, err, errno + local res local flag, err, errno = fcntl.fcntl(fd, fcntl.F_GETFL) if flag then res, err, errno = fcntl.fcntl(fd, fcntl.F_SETFL, bit.bor(flag, fcntl.O_NONBLOCK)) diff --git a/source/tools/monitor/unity/test/curl/postQueTest.lua b/source/tools/monitor/unity/test/curl/postQueTest.lua new file mode 100644 index 0000000000000000000000000000000000000000..26e373279aee76b975422f021e5731a70f2a5f08 --- /dev/null +++ b/source/tools/monitor/unity/test/curl/postQueTest.lua @@ -0,0 +1,15 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/3/24 11:52 PM +--- + +package.path = package.path .. ";../../?.lua;" +local system = require("common.system") +local ChttpCli = require("httplib.httpCli") + +local cli = ChttpCli.new() +local url = "http://127.0.0.1:8400/api/que" +local req = {cmd = "mon_pid", pid = 15789, loop = 3} +local res = cli:postTable(url, req) +system:dumps(res) diff --git a/source/tools/monitor/unity/test/lab/base.lua b/source/tools/monitor/unity/test/lab/base.lua new file mode 100644 index 0000000000000000000000000000000000000000..dab9a72bffc6391f3f5cb5526f759d2e5fb27511 --- /dev/null +++ b/source/tools/monitor/unity/test/lab/base.lua @@ -0,0 +1,23 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/3/19 6:00 PM +--- + +require("common.class") + +local Cbase = class("base") + +function Cbase:_init_(name) + self.name = name +end + +function Cbase:hello() + return self.name +end + +function Cbase:_del_() + print("base del..." .. self.name) +end + +return Cbase diff --git a/source/tools/monitor/unity/collector/conPlugin.lua b/source/tools/monitor/unity/test/lab/dirInotify.lua similarity index 31% rename from source/tools/monitor/unity/collector/conPlugin.lua rename to source/tools/monitor/unity/test/lab/dirInotify.lua index 3709afa173f0b9db4f053e50e4a0b5717058b0fe..6604667216acbbb70a9a27051f8a2d26b5780004 100644 --- a/source/tools/monitor/unity/collector/conPlugin.lua +++ b/source/tools/monitor/unity/test/lab/dirInotify.lua @@ -1,19 +1,20 @@ --- --- Generated by EmmyLua(https://github.com/EmmyLua) --- Created by liaozhaoyan. ---- DateTime: 2023/3/10 8:26 AM +--- DateTime: 2023/3/24 2:15 PM --- -require("common.class") -local system = require("common.system") -local CconPlugin = class("conPlugin") +package.path = package.path .. ";../../?.lua;" -function CconPlugin:_init_(proto, procffi, que, proto_q, fYaml) - self._proto = proto - self._que = que - self._plugins = setup() -end +local Cinotifies = require("common.inotifies") +local system = require("common.system") -function CconPlugin:proc(elapsed, lines) +local ino = Cinotifies.new("/tmp") -end \ No newline at end of file +while true do + system:sleep(1) + if ino:isChange() then + ino = Cinotifies.new("/tmp") + print("change.") + end +end diff --git a/source/tools/monitor/unity/test/lab/mulInotifies.lua b/source/tools/monitor/unity/test/lab/mulInotifies.lua new file mode 100644 index 0000000000000000000000000000000000000000..13a771d72d188a1edfd9385996dc82aa8e58d14a --- /dev/null +++ b/source/tools/monitor/unity/test/lab/mulInotifies.lua @@ -0,0 +1,115 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/4/17 14:12 +--- + +package.path = package.path .. ";../../?.lua;" + +require("common.class") +local Cinotifies = require("common.inotifies") +local system = require("common.system") +local dirent = require("posix.dirent") +local pstat = require("posix.sys.stat") + +local Cevent = class("event") + +function Cevent:_init_(path) + self._path = path + print("hello " .. path) +end + +function Cevent:_del_() + print("del ".. self._path) +end + +local function addEvents(tbl, path) + tbl[path] = Cevent.new(path) +end + +local function delEvents(tbl, path) + tbl[path] = nil +end + +local CMulIno = class("CmulIno") +function CMulIno:_init_(path) + self._path = path + self._events = {} + self._ino, self._dirs = self:build(path) + + for _, dir in ipairs(self._dirs) do + addEvents(self._events, dir) + end +end + +local function walk_dirs(path, dirs, ino) + local files = dirent.files(path) + for f in files do + if string.sub(f, 1, 1) ~= '.' then + local full = table.concat({path, f}, "/") + local lStat = pstat.lstat(full) + if pstat.S_ISDIR(lStat.st_mode) > 0 then + table.insert(dirs, full) + ino:add(full) + walk_dirs(full, dirs, ino) + end + end + end +end + +function CMulIno:build(path) + local ino = Cinotifies.new() + ino:add(path) + + local dirs = {path} + walk_dirs(path, dirs, ino) + return ino, dirs +end + +local function calcBoolean(newDir, oldDir) + local toAdd, toDel = {}, {} + + for _, k in ipairs(newDir) do + if not system:valueIsIn(oldDir, k) then + table.insert(toAdd, k) + end + end + + for _, k in ipairs(oldDir) do + if not system:valueIsIn(oldDir, k) then + table.insert(toDel, k) + end + end + return toAdd, toDel +end + + +function CMulIno:change(toAdd, toDel) + for _, k in ipairs(toAdd) do + addEvents(self._events, k) + end + + for _, k in ipairs(toDel) do + delEvents(self._events, k) + end + +end + +function CMulIno:work() + while true do + if self._ino:isChange() then + print("change.") + local dirs + + self._ino, dirs = self:build(self._path) + local toAdd, toDel = calcBoolean(dirs, self._dirs) + self:change(toAdd, toDel) + self._dirs = dirs + collectgarbage("collect") + end + system:sleep(1) + end +end + +local mulIno = CMulIno.new("/tmp") +mulIno:work() diff --git a/source/tools/monitor/unity/test/lab/objTest.lua b/source/tools/monitor/unity/test/lab/objTest.lua new file mode 100644 index 0000000000000000000000000000000000000000..6a5f252d837097cf990069b4cca510d8555cd018 --- /dev/null +++ b/source/tools/monitor/unity/test/lab/objTest.lua @@ -0,0 +1,30 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/3/19 5:55 PM +--- + +package.path = package.path .. ";../../?.lua;" + +require("common.class") +local system= require("common.system") + +local Cone = require("one") +local Ctwo = require("two") +local CThree = require("three") + +local one = Cone.new("1one") +local two = Ctwo.new("2two") +local three = CThree.new("3three") + +print(one:hello()) +assert(one:hello() == "1one") +assert(two:hello() == "2two") +assert(three:hello() == "3three") + +one:say() +two:say() +three:say() + +one = nil +two:say() \ No newline at end of file diff --git a/source/tools/monitor/unity/test/lab/one.lua b/source/tools/monitor/unity/test/lab/one.lua new file mode 100644 index 0000000000000000000000000000000000000000..50b37a00800cf05827ce6a3f7405b3a7bc0b5e4b --- /dev/null +++ b/source/tools/monitor/unity/test/lab/one.lua @@ -0,0 +1,20 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/3/19 5:58 PM +--- + +require("common.class") +local Cbase = require("base") + +Cone = class("one", Cbase) + +function Cone:_init_(name) + Cbase._init_(self, name) +end + +function Cone:say() + print("one say " .. self.name) +end + +return Cone diff --git a/source/tools/monitor/unity/test/lab/podFilter.yaml b/source/tools/monitor/unity/test/lab/podFilter.yaml new file mode 100644 index 0000000000000000000000000000000000000000..97323791a5cb050c35c765c875e52c0a554198b0 --- /dev/null +++ b/source/tools/monitor/unity/test/lab/podFilter.yaml @@ -0,0 +1,10 @@ +directCgPath: + - "" + - "kubepods.slice" + - "kubepods.slice/kubepods-besteffort.slice" + - "kubepods.slice/kubepods-burstable.slice" + +indirectCgPath: + - "kubepods.slice" + - "kubepods.slice/kubepods-besteffort.slice" + - "kubepods.slice/kubepods-burstable.slice" diff --git a/source/tools/monitor/unity/test/lab/tInotify.lua b/source/tools/monitor/unity/test/lab/tInotify.lua new file mode 100644 index 0000000000000000000000000000000000000000..ac3d01fc531c410e92da4a77d220ece3e152653d --- /dev/null +++ b/source/tools/monitor/unity/test/lab/tInotify.lua @@ -0,0 +1,20 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/3/24 1:52 PM +--- + +local inotify = require('inotify') +local handle = inotify.init() + +-- Watch for new files and renames +local wd = handle:addwatch('/tmp/inotify/', inotify.IN_CREATE, inotify.IN_MOVE) + +for ev in handle:events() do + print(ev.name .. ' was created or renamed') +end + +-- Done automatically on close, I think, but kept to be thorough +handle:rmwatch(wd) + +handle:close() diff --git a/source/tools/monitor/unity/test/lab/three.lua b/source/tools/monitor/unity/test/lab/three.lua new file mode 100644 index 0000000000000000000000000000000000000000..3097893bac96ef4cf379c8cdc7d6195afb102f50 --- /dev/null +++ b/source/tools/monitor/unity/test/lab/three.lua @@ -0,0 +1,24 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/3/19 5:59 PM +--- + +require("common.class") +local Cone = require("one") + +CThree = class("three", Cone) + +function CThree:_init_(name) + Cone._init_(self, name) + self._child = Cone.new("child") +end + + +function CThree:say() + print("three say " .. self.name) + print("child say.") + self._child:say() +end + +return CThree diff --git a/source/tools/monitor/unity/test/lab/tpodFilter.lua b/source/tools/monitor/unity/test/lab/tpodFilter.lua new file mode 100644 index 0000000000000000000000000000000000000000..e5cc73179ab4f32384dac87fb9d9745b33d851bd --- /dev/null +++ b/source/tools/monitor/unity/test/lab/tpodFilter.lua @@ -0,0 +1,15 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/3/28 11:38 PM +--- + +package.path = package.path .. ";../../?.lua;" + +local system = require("common.system") +local CpodFilter = require("collector.podMan.podFilter") + +local resYaml = system:parseYaml("podFilter.yaml") +local filter = CpodFilter.new("/mnt/host/sys/fs/cgroup") +local ret = filter:walkTops(resYaml) +print(#ret) diff --git a/source/tools/monitor/unity/test/lab/two.lua b/source/tools/monitor/unity/test/lab/two.lua new file mode 100644 index 0000000000000000000000000000000000000000..9c86e7e39045360e2b522aa91f0ab25410d3422f --- /dev/null +++ b/source/tools/monitor/unity/test/lab/two.lua @@ -0,0 +1,22 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/3/19 5:58 PM +--- + +require("common.class") +local Cone = require("one") + +CTwo = class("two", Cone) + +function CTwo:_init_(name) + Cone._init_(self, name) +end + +function CTwo:say() + print("two say " .. self.name) + print("super") + Cone.say(self) +end + +return CTwo diff --git a/source/tools/monitor/unity/test/rbtree/event.lua b/source/tools/monitor/unity/test/rbtree/event.lua index 14d6f91b00e7cfdb511c2440b224b8013127f3a7..104f8e457c7a3e98467f46c4fa624644e5433096 100644 --- a/source/tools/monitor/unity/test/rbtree/event.lua +++ b/source/tools/monitor/unity/test/rbtree/event.lua @@ -7,12 +7,47 @@ package.cpath = package.cpath .. ";../../beeQ/lib/?.so" package.path = package.path .. ";../../?.lua;" -local CrbEvent = require("common/rbEvent") +local CrbEvent = require("beeQ.rbtree.rbEvent") + +require("common.class") +local tObj = class("tOjb") + +function tObj:_init_(title) + self._title = title +end + +function tObj:work(t) + print("obj " .. self._title .. " work") +end + +local tObjAdd = class("tObjAdd", tObj) + +function tObjAdd:_init_(title) + tObj._init_(self, title) + self._add = true + self._count = 0 +end + +function tObjAdd:work(t, tree) + if self._add then + local o3 = tObj.new("o3") + tree:addEvent("stop period 3", o3, 3, true, 10) + self._add = false + end + tObj.work(self, t) + self._count = self._count + 1 + if self._count > 5 then + print("add Obj leave.") + return -1 + end +end + +local o1 = tObj.new("o1") +local o2 = tObjAdd.new("o2") local e = CrbEvent.new() -e:addEvent("test period 2", 2) -e:addEvent("test period 3", 3) -e:addEvent("stop period 3", 3, true, 10) +e:addEvent("test period 2", o1, 2) +e:addEvent("test period 3", o2, 3) e:proc() \ No newline at end of file