docker-monitor-injector源码分析
docker-monitor-injector是携程开源的一个修正docker容器内监控数据的工具,通过劫持open,fopen等linux库函数,重算/proc/meminfo,/proc/stat等度量指标,使得容器内free,top等系统工具能正确统计容器性能指标。
·
1.工具介绍
docker-monitor-injector是携程开源的一个修正docker容器内监控数据的工具,通过劫持open,fopen等linux库函数,重算/proc/meminfo,/proc/stat等度量指标,使得容器内free,top等系统工具能正确统计容器性能指标。下面将对其进行源码分析,并整理出各个数据的监控修正算法。
2.原理
图片描述
3.函数调用关系
4.算法(伪码)
1) /proc/uptime
reaperage: time(NULL) - lstat("/proc/1").st_ctime
idletime: (reaperage - cpuacct.usage)/1000000000
2) /proc/meminfo
MemTotal: memory.limit_in_bytes
memfree: memlimit - memusage
swaptotal: memswlimit > 0 ? memswlimit - memlimit : 0
SwapFree: (memswlimit > 0 && memswusage > 0) ? (memswlimit - memlimit) - (memswusage - memusage):0
cached: memory.stat.total_cache
Buffers: 0
SwapCached: 0
Slab: 0
3) /proc/stat
a.proc_stat_by_shares
user_sum: cpuacct.stat.user
system_sum: cpuacct.stat.system
nice_sum: (_self*cpu.shares/1024)/sysconf(_SC_NPROCESSORS_ONLN)
idle_sum: (_self*cpu.shares/1024)/sysconf(_SC_NPROCESSORS_ONLN)
iowait_sum: (_self*cpu.shares/1024)/sysconf(_SC_NPROCESSORS_ONLN)
irq_sum: (_self*cpu.shares/1024)/sysconf(_SC_NPROCESSORS_ONLN)
softirq_sum:(_self*cpu.shares/1024)/sysconf(_SC_NPROCESSORS_ONLN)
steal_sum: (_self*cpu.shares/1024)/sysconf(_SC_NPROCESSORS_ONLN)
guest_sum: (_self*cpu.shares/1024)/sysconf(_SC_NPROCESSORS_ONLN)
任一cpu stat值算法如下:
x = x_sum/sysconf(_SC_NPROCESSORS_ONLN),x_sum为上述任一指标
b.flush_proc_stat_by_cpusets
btime: stat("/proc/1").st_ctime
user_sum, nice_sum, system_sum, idle_sum, iowait_sum, irq_sum, softirq_sum, steal_sum, guest_sum
= 容器所拥有cpu id在/proc/stat中对应值之和。
4) /proc/cpuinfo
若容器绑定cpu,只输出绑定cpu id对应cpu info信息
否则,全部输出
5) /sys/devices/system/cpu/online
//经过验证,该算法应该是有缺陷的,不作分析。
6) /proc/diskstats
read = blkio.io_serviced:major:minor:Read
write = blkio.io_serviced:major:minor:Write
read_merged = blkio.io_merged:major:minor:Read
write_merged=blkio.io_merged:major:minor:Write
read_sectors= blkio.io_service_bytes:major:minor:Read/512
write_sectors= blkio.io_service_bytes:major:minor:Write)/512
rd_svctm = blkio.io_service_time:major:minor:Read/1000000
rd_wait = get_blkio_io_value(blkio.io_wait_time:major:minor:Read)/1000000
read_ticks = rd_svctm + rd_wait
wr_svctm = blkio.io_service_time:major:minor:Write/1000000
wr_wait = blkio.io_wait_time:major:minor:Write/1000000;
write_ticks = wr_svctm + wr_wait
tot_ticks = blkio.io_service_time:major:minor:Total/1000000
...
if (read || write || read_merged || write_merged || read_sectors || write_sectors || read_ticks || write_ticks) {
snprintf(lbuf, 256, "%u %u %s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n",
major, minor, dev_name, read, read_merged, read_sectors, read_ticks,
write, write_merged, write_sectors, write_ticks, ios_pgr, tot_ticks, rq_ticks);
6.参考项目
https://linuxcontainers.org/lxcfs/introduction/
https://github.com/CtripCloud/docker-monitor-injector
更多推荐
已为社区贡献2条内容
所有评论(0)