|
83 | 83 | /* recognize numa node */ |
84 | 84 | #define NUMADIR "/sys/devices/system/node" |
85 | 85 |
|
| 86 | +/* recognize k8s global memory.stat and memory current usage */ |
| 87 | +#define K8S_MEMDIR_CGV1 "/sys/fs/cgroup/memory/kubepods" |
| 88 | +#define K8S_MEMDIR_CGV2 "/sys/fs/cgroup/kubepods" |
| 89 | +#define K8S_SYSTEMD_CM ".slice" |
| 90 | +#define K8S_MEM_STAT "/memory.stat" |
| 91 | +#define K8S_MEM_CGV1_USAGE "/memory.usage_in_bytes" |
| 92 | +#define K8S_MEM_CGV2_USAGE "/memory.current" |
| 93 | + |
86 | 94 | /* recognize LLC monitor data */ |
87 | 95 | #define LLCDIR "/sys/fs/resctrl/mon_data" |
88 | 96 | #define L3SIZE "/sys/devices/system/cpu/cpu0/cache/index3/size" |
@@ -874,6 +882,151 @@ photosyst(struct sstat *si) |
874 | 882 | } |
875 | 883 | } |
876 | 884 |
|
| 885 | + if ( supportflags & CGROUPV2 ) |
| 886 | + { |
| 887 | + if ( (fp = fopen(K8S_MEMDIR_CGV2 K8S_MEM_STAT, "r")) != NULL || |
| 888 | + (fp = fopen(K8S_MEMDIR_CGV2 K8S_SYSTEMD_CM K8S_MEM_STAT, "r")) != NULL ) |
| 889 | + { |
| 890 | + /* for cgroup v2 */ |
| 891 | + while ( fgets(linebuf, sizeof(linebuf), fp) != NULL ) |
| 892 | + { |
| 893 | + nr = sscanf(linebuf, "%s %lld\n", nam, &cnts[0]); |
| 894 | + |
| 895 | + if ( strcmp("file", nam) == EQ ) |
| 896 | + { |
| 897 | + si->k8smem.file = cnts[0]/pagesize; |
| 898 | + continue; |
| 899 | + } |
| 900 | + if ( strcmp("anon", nam) == EQ ) |
| 901 | + { |
| 902 | + si->k8smem.anon = cnts[0]/pagesize; |
| 903 | + continue; |
| 904 | + } |
| 905 | + if ( strcmp("shmem", nam) == EQ ) |
| 906 | + { |
| 907 | + si->k8smem.shmem = cnts[0]/pagesize; |
| 908 | + continue; |
| 909 | + } |
| 910 | + if ( strcmp("file_mapped", nam) == EQ ) |
| 911 | + { |
| 912 | + si->k8smem.filemapped = cnts[0]/pagesize; |
| 913 | + continue; |
| 914 | + } |
| 915 | + if ( strcmp("inactive_anon", nam) == EQ ) |
| 916 | + { |
| 917 | + si->k8smem.inactiveanon = cnts[0]/pagesize; |
| 918 | + continue; |
| 919 | + } |
| 920 | + if ( strcmp("active_anon", nam) == EQ ) |
| 921 | + { |
| 922 | + si->k8smem.activeanon = cnts[0]/pagesize; |
| 923 | + continue; |
| 924 | + } |
| 925 | + if ( strcmp("inactive_file", nam) == EQ ) |
| 926 | + { |
| 927 | + si->k8smem.inactivefile = cnts[0]/pagesize; |
| 928 | + continue; |
| 929 | + } |
| 930 | + if ( strcmp("active_file", nam) == EQ ) |
| 931 | + { |
| 932 | + si->k8smem.activefile = cnts[0]/pagesize; |
| 933 | + continue; |
| 934 | + } |
| 935 | + } |
| 936 | + |
| 937 | + fclose(fp); |
| 938 | + } |
| 939 | + |
| 940 | + if ( (fp = fopen(K8S_MEMDIR_CGV2 K8S_MEM_CGV2_USAGE, "r")) != NULL || |
| 941 | + (fp = fopen(K8S_MEMDIR_CGV2 K8S_SYSTEMD_CM K8S_MEM_CGV2_USAGE, "r")) != NULL ) |
| 942 | + { |
| 943 | + if ( fscanf(fp, "%lld", &cnts[0]) == 1 ) |
| 944 | + { |
| 945 | + /* |
| 946 | + ** Refer to https://github.com/kubernetes/kubernetes/issues/43916, |
| 947 | + ** memory.available := node.status.capacity[memory] - node.stats.memory.workingSet |
| 948 | + ** && workingSet := $cgroupfs/memory.current - inactive_file |
| 949 | + */ |
| 950 | + si->k8smem.usagefile = cnts[0]/pagesize; |
| 951 | + si->k8smem.workingset = si->k8smem.usagefile - si->k8smem.inactivefile; |
| 952 | + } |
| 953 | + |
| 954 | + fclose(fp); |
| 955 | + } |
| 956 | + } |
| 957 | + else |
| 958 | + { |
| 959 | + if ( (fp = fopen(K8S_MEMDIR_CGV1 K8S_MEM_STAT, "r")) != NULL || |
| 960 | + (fp = fopen(K8S_MEMDIR_CGV1 K8S_SYSTEMD_CM K8S_MEM_STAT, "r")) != NULL ) |
| 961 | + { |
| 962 | + /* for cgroup v1 */ |
| 963 | + while ( fgets(linebuf, sizeof(linebuf), fp) != NULL ) |
| 964 | + { |
| 965 | + nr = sscanf(linebuf, "%s %lld\n", nam, &cnts[0]); |
| 966 | + |
| 967 | + if ( strcmp("total_cache", nam) == EQ ) |
| 968 | + { |
| 969 | + si->k8smem.file = cnts[0]/pagesize; |
| 970 | + continue; |
| 971 | + } |
| 972 | + if ( strcmp("total_rss", nam) == EQ) |
| 973 | + { |
| 974 | + si->k8smem.anon = cnts[0]/pagesize; |
| 975 | + continue; |
| 976 | + } |
| 977 | + if ( strcmp("total_shmem", nam) == EQ) |
| 978 | + { |
| 979 | + si->k8smem.shmem = cnts[0]/pagesize; |
| 980 | + continue; |
| 981 | + } |
| 982 | + if ( strcmp("total_mapped_file", nam) == EQ) |
| 983 | + { |
| 984 | + si->k8smem.filemapped = cnts[0]/pagesize; |
| 985 | + continue; |
| 986 | + } |
| 987 | + if ( strcmp("total_inactive_anon", nam) == EQ) |
| 988 | + { |
| 989 | + si->k8smem.inactiveanon = cnts[0]/pagesize; |
| 990 | + continue; |
| 991 | + } |
| 992 | + if ( strcmp("total_active_anon", nam) == EQ) |
| 993 | + { |
| 994 | + si->k8smem.activeanon = cnts[0]/pagesize; |
| 995 | + continue; |
| 996 | + } |
| 997 | + if ( strcmp("total_inactive_file", nam) == EQ) |
| 998 | + { |
| 999 | + si->k8smem.inactivefile = cnts[0]/pagesize; |
| 1000 | + continue; |
| 1001 | + } |
| 1002 | + if ( strcmp("total_active_file", nam) == EQ) |
| 1003 | + { |
| 1004 | + si->k8smem.activefile = cnts[0]/pagesize; |
| 1005 | + continue; |
| 1006 | + } |
| 1007 | + } |
| 1008 | + |
| 1009 | + fclose(fp); |
| 1010 | + } |
| 1011 | + |
| 1012 | + if ( (fp = fopen(K8S_MEMDIR_CGV1 K8S_MEM_CGV1_USAGE, "r")) != NULL || |
| 1013 | + (fp = fopen(K8S_MEMDIR_CGV1 K8S_SYSTEMD_CM K8S_MEM_CGV1_USAGE, "r")) != NULL ) |
| 1014 | + { |
| 1015 | + if ( fscanf(fp, "%lld", &cnts[0]) == 1 ) |
| 1016 | + { |
| 1017 | + /* |
| 1018 | + ** Refer to https://github.com/kubernetes/kubernetes/issues/43916, |
| 1019 | + ** memory.available := node.status.capacity[memory] - node.stats.memory.workingSet |
| 1020 | + ** && workingSet := $cgroupfs/memory.usage_in_bytes - total_inactive_file |
| 1021 | + */ |
| 1022 | + si->k8smem.usagefile = cnts[0]/pagesize; |
| 1023 | + si->k8smem.workingset = si->k8smem.usagefile - si->k8smem.inactivefile; |
| 1024 | + } |
| 1025 | + |
| 1026 | + fclose(fp); |
| 1027 | + } |
| 1028 | + } |
| 1029 | + |
877 | 1030 | /* |
878 | 1031 | ** gather per numa memory-related statistics from the file |
879 | 1032 | ** /sys/devices/system/node/node0/meminfo, and store them in binary form. |
|
0 commit comments