这篇文章给大家分享的是有关docker-monitor-injector工具算法的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
工具介绍 docker-monitor-injector是携程开源的一个修正docker容器内监控数据的工具,通过劫持open,fopen等linux库函数,重算/proc/meminfo,/proc/stat等度量指标,使得容器内free,top等系统工具能正确统计容器性能指标。下面将对其进行源码分析,并整理出各个数据的监控修正算法。
算法(伪码)
/proc/uptime
reaperage: time(NULL) - lstat("/proc/1").st_ctime
idletime: (reaperage - cpuacct.usage)/1000000000
/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
/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中对应值之和。
/proc/cpuinfo
若容器绑定cpu,只输出绑定cpu id对应cpu info信息
否则,全部输出
/sys/devices/system/cpu/online
//经过验证,该算法应该是有缺陷的,不作分析。
/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);
感谢各位的阅读!关于“docker-monitor-injector工具算法的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!