1#
2# Copyright (C) 2020 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17from proc_tests import KernelProcFileTestBase
18from proc_tests.KernelProcFileTestBase import repeat_rule, literal_token
19
20
21class ProcZoneInfoTest(KernelProcFileTestBase.KernelProcFileTestBase):
22    '''/proc/zoneinfo displays information about memory zones.'''
23
24    t_APAGES = literal_token(r'pages\s')
25    t_PAGESETS = literal_token(r'pagesets')
26    t_CPU = literal_token(r'cpu')
27    t_VM = literal_token(r'vm')
28    t_STATS = literal_token(r'stats')
29    t_THRESHOLD = literal_token(r'threshold')
30    t_NODE = literal_token(r'Node')
31    t_ZONE = literal_token(r'zone')
32    t_PROTECTION = literal_token(r'protection')
33    t_PERNODE = literal_token(r'per-node')
34    t_LPAREN = literal_token(r'\(')
35    t_RPAREN = literal_token(r'\)')
36
37    t_ignore = ' '
38
39    start = 'nodes'
40
41    p_nodes = repeat_rule('node')
42    p_lines = repeat_rule('line')
43    p_cpus = repeat_rule('cpu')
44    p_colonlines = repeat_rule('colonline')
45    p_numcommas = repeat_rule('numcomma')
46
47    def p_node(self, p):
48        '''node : heading pernode APAGES lines protection populated'''
49        p[0] = [p[1], p[2], p[4], p[5], p[6]]
50
51    def p_populated(self, p):
52        '''populated : PAGESETS NEWLINE cpus colonlines
53                | lines PAGESETS NEWLINE cpus colonlines
54                | empty'''
55        if len(p) == 2:
56            p[0] = []
57        elif len(p) == 6:
58            p[0] = [p[1], p[4], p[5]]
59        else:
60            p[0] = [p[3], p[4]]
61
62    def p_pernode(self, p):
63        '''pernode : PERNODE STATS NEWLINE lines
64                   | empty'''
65        p[0] = [] if len(p) == 2 else [p[1], p[4]]
66
67    def p_protection(self, p):
68        '''protection : PROTECTION COLON LPAREN numcommas NUMBER RPAREN NEWLINE
69                | empty'''
70        p[0] = [] if len(p) == 2 else p[4] + [p[5]]
71
72    def p_numcomma(self, p):
73        'numcomma : NUMBER COMMA'
74        p[0] = p[1]
75
76    def p_line(self, p):
77        '''line : STRING NUMBER NEWLINE
78                | NUMBER NEWLINE'''
79        if len(p) == 4:
80            p[0] = p[1:3]
81        else:
82            p[0] = p[1]
83
84    def p_cpu(self, p):
85        'cpu : CPU COLON NUMBER NEWLINE colonline colonline colonline \
86                VM STATS THRESHOLD COLON NUMBER NEWLINE'
87
88        p[0] = [p[3], p[5], p[6], p[7], [p[10], p[12]]]
89
90    def p_colonline(self, p):
91        'colonline : STRING COLON NUMBER NEWLINE'
92        p[0] = [p[1], p[3]]
93
94    def p_heading(self, p):
95        'heading : NODE NUMBER COMMA ZONE STRING NEWLINE'
96        p[0] = [p[2], p[5]]
97
98    def get_path(self):
99        return "/proc/zoneinfo"
100