Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to parse the lxcfs read / proc/meminfo source code flow

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article shows you how to parse the lxcfs read / proc/meminfo source code process, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Git repo: https://git-sa.nie.netease.com/whale/lxcfs

First of all, lxcfs is a user-mode file system, and all operations on files are defined in lxcfs.c

Const struct fuse_operations lxcfs_ops = {.getattr = lxcfs_getattr, .readlink = NULL, .getdir = NULL, .mknod = NULL, .mkdir = lxcfs_mkdir, .unlink = NULL, .rmdir = lxcfs_rmdir, .symlink = NULL, .rename = NULL, .link = NULL, .chmod = lxcfs_chmod, .chown = lxcfs_chown, .truncate = lxcfs_truncate, .utime = NULL, .open = lxcfs_open, .read = lxcfs_read, .release = lxcfs_release .write = lxcfs_write, .statfs = NULL, .flush = lxcfs_flush, .setxattr = NULL, .getxattr = NULL, .listxattr = NULL, .removexattr = NULL, .opendir = lxcfs_opendir, .readdir = lxcfs_readdir, .releasedir = lxcfs_releasedir, .fsyncdir = NULL, .init = NULL, .requests = NULL, .access = lxcfs_access, .create = NULL, .ftruncate = NULL, .fgetattr = NULL,}

Here we analyze a complete process of reading / proc/meminfo

According to the parameter * path, lxcfs_read// determines whether to execute do_cg_read or do_proc_readstatic int lxcfs_read (const char * path, char * buf, size_t size, off_t offset, struct fuse_file_info * fi) {int ret; fprintf (stderr, "lxcfs_read. Path:% s\ r\ n ", path); if (strncmp (path," / cgroup ", 7) = = 0) {up_users (); ret = do_cg_read (path, buf, size, offset, fi); down_users (); return ret;} if (strncmp (path," / proc ", 5) = = 0) {up_users (); ret = do_proc_read (path, buf, size, offset, fi); down_users (); return ret } return-EINVAL } do_proc_read-- > proc_read// execute read operations int proc_read (const char * path, char * buf, size_t size, off_t offset) on proc_meminfo_read, proc_cpuinfo_read, proc_uptime_read, proc_stat_read, proc_diskstats_read, proc_swaps_read, proc_loadavg_read files according to fuse_file_info judgment f-> type Struct fuse_file_info * fi) {struct file_info * f = (struct file_info *) fi- > fh Fprintf (stderr, "proc_read. Path:% s, file_info:% c\ r\ n ", path, f-> type); switch (f-> type) {case LXC_TYPE_PROC_MEMINFO: return proc_meminfo_read (buf, size, offset, fi); case LXC_TYPE_PROC_CPUINFO: return proc_cpuinfo_read (buf, size, offset, fi); case LXC_TYPE_PROC_UPTIME: return proc_uptime_read (buf, size, offset, fi) Case LXC_TYPE_PROC_STAT: return proc_stat_read (buf, size, offset, fi); case LXC_TYPE_PROC_DISKSTATS: return proc_diskstats_read (buf, size, offset, fi); case LXC_TYPE_PROC_SWAPS: return proc_swaps_read (buf, size, offset, fi); case LXC_TYPE_PROC_LOADAVG: return proc_loadavg_read (buf, size, offset, fi); default: return-EINVAL }} proc_meminfo_readproc_meminfo_read (char * buf, size_t size, off_t offset, struct fuse_file_info * fi) / / if you are in the container cat / proc/meminfo, where initpid is the container / sbin/init process number, / / if cat / usr/local/var/lib/lxcfs/proc/meminfo initpid is the host process number 1, where getpid is the lxcfs process number fprintf (stderr, "proc_meminfo_read.... Initpid:%d, pid:%d, getpid:%d\ r\ n ", initpid, fc- > pid, getpid (); cg = get_pid_cgroup (initpid," memory "); fprintf (stderr," proc_meminfo_read.... CG:% s\ n ", cg); if (! cg) return read_file (" / proc/meminfo ", buf, size, d); prune_init_slice (cg); / / get the minimum value of memory.limit_in_bytes in the cgroup directory and its subdirectories memlimit = get_min_memlimit (cg," memory.limit_in_bytes "); if (! cgfs_get_value (" memory ", cg," memory.usage_in_bytes ", & memusage_str) goto err If (! cgfs_get_value ("memory", cg, "memory.stat", & memstat_str)) goto err;... The logic of all kinds of meminfo data acquisition and calculation is omitted. / / the printed result is the content taken by cat fprintf (stderr, "proc_meminfo_read.... buf:% s\ n", buf); rv = total_len;get_min_memlimit

The logic of all proc_meminfo_read, proc_cpuinfo_read, proc_uptime_read, proc_stat_read, proc_diskstats_read, proc_swaps_read and proc_loadavg_read is implemented in bindings.c.

/ / get the minimum value of memory.limit_in_bytes in the cgroup directory and its subdirectories static unsigned long get_min_memlimit (const char * cgroup) {char * copy = strdupa (cgroup); @ @-2951 static int proc_meminfo_read 12 + 2952 static int proc_meminfo_read (char * buf, size_t size, off_t offset, pid_t initpid = lookup_initpid_in_store (fc- > pid); if (initpid pid) / / if you cat / proc/meminfo in the container, here initpid is the container / sbin/init process number. If cat / usr/local/var/lib/lxcfs/proc/meminfo / / initpid in the host is the host process number 1, the getpid is lxcfs process number cg = get_pid_cgroup (initpid, "memory"); if (! cg) return read_file ("/ proc/meminfo", buf, size, d); prune_init_slice (cg) Memlimit = get_min_memlimit (cg); / / get the minimum value of memory.limit_in_bytes in the cgroup directory and its subdirectories if (! cgfs_get_value ("memory", cg, "memory.usage_in_bytes", & memusage_str)) goto err; if (! cgfs_get_value ("memory", cg, "memory.stat", & memstat_str)) d-> size = total_len; if (total_len > size) total_len = size Memcpy (buf, d-> buf, total_len); / / the printed result is the content obtained by cat / / fprintf (stderr, "zy test .buf:% s\ n", buf); rv = total_len Err: if (f) the method of modifying the dynamic link library path of lxcfs by adding-- prefix / home/cld in the. / configure stage can specify the path of the dynamic link library to / home/cld/var/lxcfs/liblxcfs.so. The above content is how to parse the lxcfs read / proc/meminfo source code flow. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report