-
Notifications
You must be signed in to change notification settings - Fork 3
/
OOMAnalyser.py
5346 lines (4754 loc) · 216 KB
/
OOMAnalyser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: Latin-1 -*-
#
# Linux OOMAnalyser
#
# Copyright (c) 2017-2024 Carsten Grohmann
# License: MIT (see LICENSE.txt)
# THIS PROGRAM COMES WITH NO WARRANTY
import math
import re
DEBUG = False
"""Show additional information during the development cycle"""
VERSION = "0.7.0 (devel)"
"""Version number e.g. "0.6.0" or "0.6.0 (devel)" """
# __pragma__ ('skip')
# MOC objects to satisfy statical checker and imports in unit tests
js_undefined = 0
class classList:
def add(self, *args, **kwargs):
pass
def remove(self, *args, **kwargs):
pass
def toggle(self, *args, **kwargs):
pass
class console:
@staticmethod
def log(message):
"""Log message on JS console"""
pass
@staticmethod
def js_clear():
"""
Clear JS console
clear is an alias and converted by Transcrypt to py_clear. Instead, we use
JS alias js_clear(), that will be converted to clear().
"""
pass
class document:
def querySelectorAll(
self,
*args,
):
return [Node()]
@staticmethod
def getElementsByClassName(names):
"""
Returns an array-like object of all child elements which have all the given class name(s).
@param names: A string representing the class name(s) to match; multiple class names are separated by whitespace.
@type names: List(str)
@rtype: List(Node)
"""
return [Node()]
@staticmethod
def getElementsByTagName(tagName):
"""
Returns an array-like object of all child elements which have all the given tag name.
@param tagName: A string representing the tag name(s) to match.
@type tagName: str
@rtype: List(Node)
"""
return [Node()]
@staticmethod
def getElementById(_id):
"""
Returns an object representing the element whose id property matches
@type _id: str
@rtype: Node
"""
return Node()
@staticmethod
def createElementNS(namespaceURI, qualifiedName, *arg):
"""
Creates an element with the specified namespace URI and qualified name.
@param str namespaceURI: Namespace URI to associate with the element
@param str qualifiedName: Type of element to be created
@rtype: Node
"""
return Node()
@staticmethod
def createElement(tagName, *args):
"""
Creates the HTML element specified by tagName.
@param str tagName: Type of element to be created
@rtype: Node
"""
return Node()
class Node:
classList = classList()
id = None
offsetHeight = 0
offsetWidth = 0
textContent = ""
def __init__(self, nr_children=1):
self.nr_children = nr_children
@property
def firstChild(self):
if self.nr_children:
self.nr_children -= 1
return Node(self.nr_children)
return None
@property
def tagName(self):
"""
Read-only property that returns the tag name of the element
@rtype: str
"""
return ""
def removeChild(self, *args, **kwargs):
return
def appendChild(self, *args, **kwargs):
return
def setAttribute(self, *args, **kwargs):
return
@property
def parentNode(self):
return super().__new__(self)
# __pragma__ ('noskip')
class OOMEntityState:
"""Enum for completeness of the OOM block"""
unknown = 0
empty = 1
invalid = 2
started = 3
complete = 4
class OOMEntityType:
"""Enum for the type of the OOM"""
unknown = 0
automatic = 1
manual = 2
class OOMMemoryAllocFailureType:
"""Enum to store the results why the memory allocation could have failed"""
not_started = 0
"""Analysis not started"""
missing_data = 1
"""Missing data to start analysis"""
failed_below_low_watermark = 2
"""Failed, because after satisfying this request, the free memory will be below the low memory watermark"""
failed_no_free_chunks = 3
"""Failed, because no suitable chunk is free in the current or any higher order."""
failed_unknown_reason = 4
"""Failed, but the reason is unknown"""
skipped_high_order_dont_trigger_oom = 5
""""high order" requests don't trigger OOM"""
def is_visible(element):
return element.offsetWidth > 0 and element.offsetHeight > 0
def hide_element(element_id):
"""Hide the given HTML element"""
element = document.getElementById(element_id)
element.classList.add("js-text--display-none")
def show_element(element_id):
"""Show the given HTML element"""
element = document.getElementById(element_id)
element.classList.remove("js-text--display-none")
def hide_elements(selector):
"""Hide all matching elements by adding class js-text--display-none"""
for element in document.querySelectorAll(selector):
element.classList.add("js-text--display-none")
def show_elements(selector):
"""Show all matching elements by removing class js-text--display-none"""
for element in document.querySelectorAll(selector):
element.classList.remove("js-text--display-none")
def toggle(element_id):
"""Toggle the visibility of the given HTML element"""
element = document.getElementById(element_id)
element.classList.toggle("js-text--display-none")
def escape_html(unsafe):
"""
Escape unsafe HTML entities
@type unsafe: str
@rtype: str
"""
return (
unsafe.replace("&", "&")
.replace("<", "<")
.replace(">", ">")
.replace('"', """)
.replace("'", "'")
)
def debug(msg):
"""Add debug message to the notification box"""
add_to_notifybox("DEBUG", msg)
def error(msg):
"""Show the notification box and add the error message"""
add_to_notifybox("ERROR", msg)
def internal_error(msg):
"""Show the notification box and add the internal error message"""
add_to_notifybox("INTERNAL ERROR", msg)
def warning(msg):
"""Show the notification box and add the warning message"""
add_to_notifybox("WARNING", msg)
def add_to_notifybox(prefix, msg):
"""
Escaped and add message to the notification box
If the message has a prefix "ERROR" or "WARNING" the notification box will be shown.
"""
if prefix == "DEBUG":
css_class = "js-notify_box__msg--debug"
elif prefix == "WARNING":
css_class = "js-notify_box__msg--warning"
else:
css_class = "js-notify_box__msg--error"
if prefix != "DEBUG":
show_element("notify_box")
notify_box = document.getElementById("notify_box")
notification = document.createElement("div")
notification.classList.add(css_class)
notification.innerHTML = "{}: {}<br>".format(prefix, escape_html(msg))
notify_box.appendChild(notification)
# Also show all messages on the JS console
console.log("{}: {}".format(prefix, msg))
class BaseKernelConfig:
"""Base class for all kernel specific configuration"""
name = "Base configuration for all kernels based on vanilla kernel 3.10"
"""Name/description of this kernel configuration"""
EXTRACT_PATTERN = None
"""
Instance specific dictionary of RE pattern to analyse a OOM block for a specific kernel version
This dict will be filled from EXTRACT_PATTERN_BASE and EXTRACT_PATTERN_OVERLAY during class constructor is executed.
@type: None|Dict
@see: EXTRACT_PATTERN_BASE and EXTRACT_PATTERN_OVERLAY
"""
EXTRACT_PATTERN_BASE = {
"invoked oom-killer": (
r"^(?P<trigger_proc_name>[\S ]+) invoked oom-killer: "
r"gfp_mask=(?P<trigger_proc_gfp_mask>0x[a-z0-9]+)(\((?P<trigger_proc_gfp_flags>[A-Z_|]+)\))?, "
r"(nodemask=(?P<trigger_proc_nodemask>([\d,-]+|\(null\))), )?"
r"order=(?P<trigger_proc_order>-?\d+), "
r"oom_score_adj=(?P<trigger_proc_oomscore>-?\d+)",
True,
),
"Trigger process and kernel version": (
r"^CPU: \d+ PID: (?P<trigger_proc_pid>\d+) "
r"Comm: .* (Not tainted|Tainted:.*) "
r"(?P<kernel_version>\d[\w.-]+) #\d",
True,
),
# split caused by a limited number of iterations during converting PY regex into JS regex
# Source: mm/page_alloc.c:__show_free_areas()
"Overall Mem-Info (part 1)": (
r"^Mem-Info:.*"
#
r"(?:\n)"
# first line (starting w/o a space)
r"^active_anon:(?P<active_anon_pages>\d+) inactive_anon:(?P<inactive_anon_pages>\d+) "
r"isolated_anon:(?P<isolated_anon_pages>\d+)"
r"(?:\n)"
# remaining lines (w/ leading space)
r"^ active_file:(?P<active_file_pages>\d+) inactive_file:(?P<inactive_file_pages>\d+) "
r"isolated_file:(?P<isolated_file_pages>\d+)"
r"(?:\n)"
r"^ unevictable:(?P<unevictable_pages>\d+) dirty:(?P<dirty_pages>\d+) writeback:(?P<writeback_pages>\d+) "
r"unstable:(?P<unstable_pages>\d+)",
True,
),
"Overall Mem-Info (part 2)": (
r"^ slab_reclaimable:(?P<slab_reclaimable_pages>\d+) slab_unreclaimable:(?P<slab_unreclaimable_pages>\d+)"
r"(?:\n)"
r"^ mapped:(?P<mapped_pages>\d+) shmem:(?P<shmem_pages>\d+) pagetables:(?P<pagetables_pages>\d+) "
r"bounce:(?P<bounce_pages>\d+)"
r"(?:\n)"
r"^ free:(?P<free_pages>\d+) free_pcp:(?P<free_pcp_pages>\d+) free_cma:(?P<free_cma_pages>\d+)",
True,
),
"Available memory chunks": (
r"(?P<mem_node_info>(^Node \d+ ((DMA|DMA32|Normal):|(hugepages)).+(\n|$))+)",
False,
),
"Memory watermarks": (
r"(?P<mem_watermarks>(^(Node \d+ (DMA|DMA32|Normal) free:|lowmem_reserve\[\]:).+(\n|$))+)",
False,
),
"Page cache": (
r"^(?P<pagecache_total_pages>\d+) total pagecache pages.*$",
True,
),
# Source:mm/swap_state.c:show_swap_cache_info()
"Swap usage information": (
r"^(?P<swap_cache_pages>\d+) pages in swap cache"
r"(?:\n)"
r"^Swap cache stats: add \d+, delete \d+, find \d+\/\d+"
r"(?:\n)"
r"^Free swap = (?P<swap_free_kb>\d+)kB"
r"(?:\n)"
r"^Total swap = (?P<swap_total_kb>\d+)kB",
False,
),
"Page information": (
r"^(?P<ram_pages>\d+) pages RAM"
r"("
r"(?:\n)"
r"^(?P<highmem_pages>\d+) pages HighMem/MovableOnly"
r")?"
r"(?:\n)"
r"^(?P<reserved_pages>\d+) pages reserved"
r"("
r"(?:\n)"
r"^(?P<cma_pages>\d+) pages cma reserved"
r")?"
r"("
r"(?:\n)"
r"^(?P<pagetablecache_pages>\d+) pages in pagetable cache"
r")?"
r"("
r"(?:\n)"
r"^(?P<hwpoisoned_pages>\d+) pages hwpoisoned"
r")?",
True,
),
"Process killed by OOM": (
r"^Out of memory: Kill process (?P<killed_proc_pid>\d+) \((?P<killed_proc_name>[\S ]+)\) "
r"score (?P<killed_proc_score>\d+) or sacrifice child",
True,
),
"Details of process killed by OOM": (
r"^Killed process (?P<killed_proc_pid>\d+) \((?P<killed_proc_name>[\S ]+)\) "
r"total-vm:(?P<killed_proc_total_vm_kb>\d+)kB, anon-rss:(?P<killed_proc_anon_rss_kb>\d+)kB, "
r"file-rss:(?P<killed_proc_file_rss_kb>\d+)kB",
True,
),
}
"""
RE pattern to extract information from OOM.
The first item is the RE pattern and the second is whether it is mandatory to find this pattern.
This dictionary will be copied to EXTRACT_PATTERN during class constructor is executed.
@type: dict(tuple(str, bool))
@see: EXTRACT_PATTERN
"""
EXTRACT_PATTERN_OVERLAY = {}
"""
To extend / overwrite parts of EXTRACT_PATTERN in kernel configuration.
@type: dict(tuple(str, bool))
@see: EXTRACT_PATTERN
"""
# NOTE: These flags are automatically extracted from a gfp.h file.
# Please do not change them manually!
GFP_FLAGS = {
#
#
# Useful GFP flag combinations:
"GFP_ATOMIC": {"value": "__GFP_HIGH"},
"GFP_HIGHUSER": {
"value": "__GFP_WAIT | __GFP_IO | __GFP_FS | __GFP_HARDWALL | __GFP_HIGHMEM"
},
"GFP_HIGHUSER_MOVABLE": {
"value": "__GFP_WAIT | __GFP_IO | __GFP_FS | __GFP_HARDWALL | __GFP_HIGHMEM | __GFP_MOVABLE"
},
"GFP_IOFS": {"value": "__GFP_IO | __GFP_FS"},
"GFP_KERNEL": {"value": "__GFP_WAIT | __GFP_IO | __GFP_FS"},
"GFP_NOFS": {"value": "__GFP_WAIT | __GFP_IO"},
"GFP_NOIO": {"value": "__GFP_WAIT"},
"GFP_NOWAIT": {"value": "GFP_ATOMIC & ~__GFP_HIGH"},
"GFP_TEMPORARY": {
"value": "__GFP_WAIT | __GFP_IO | __GFP_FS | __GFP_RECLAIMABLE"
},
"GFP_TRANSHUGE": {
"value": "GFP_HIGHUSER_MOVABLE | __GFP_COMP | __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD"
},
"GFP_USER": {"value": "__GFP_WAIT | __GFP_IO | __GFP_FS | __GFP_HARDWALL"},
#
#
# Modifier, mobility and placement hints:
"__GFP_COLD": {"value": "___GFP_COLD"},
"__GFP_COMP": {"value": "___GFP_COMP"},
"__GFP_DMA": {"value": "___GFP_DMA"},
"__GFP_DMA32": {"value": "___GFP_DMA32"},
"__GFP_FS": {"value": "___GFP_FS"},
"__GFP_HARDWALL": {"value": "___GFP_HARDWALL"},
"__GFP_HIGH": {"value": "___GFP_HIGH"},
"__GFP_HIGHMEM": {"value": "___GFP_HIGHMEM"},
"__GFP_IO": {"value": "___GFP_IO"},
"__GFP_KMEMCG": {"value": "___GFP_KMEMCG"},
"__GFP_MEMALLOC": {"value": "___GFP_MEMALLOC"},
"__GFP_MOVABLE": {"value": "___GFP_MOVABLE"},
"__GFP_NOFAIL": {"value": "___GFP_NOFAIL"},
"__GFP_NOMEMALLOC": {"value": "___GFP_NOMEMALLOC"},
"__GFP_NORETRY": {"value": "___GFP_NORETRY"},
"__GFP_NOTRACK": {"value": "___GFP_NOTRACK"},
"__GFP_NOTRACK_FALSE_POSITIVE": {"value": "__GFP_NOTRACK"},
"__GFP_NOWARN": {"value": "___GFP_NOWARN"},
"__GFP_NO_KSWAPD": {"value": "___GFP_NO_KSWAPD"},
"__GFP_OTHER_NODE": {"value": "___GFP_OTHER_NODE"},
"__GFP_RECLAIMABLE": {"value": "___GFP_RECLAIMABLE"},
"__GFP_REPEAT": {"value": "___GFP_REPEAT"},
"__GFP_WAIT": {"value": "___GFP_WAIT"},
"__GFP_WRITE": {"value": "___GFP_WRITE"},
"__GFP_ZERO": {"value": "___GFP_ZERO"},
#
#
# Plain integer GFP bitmasks (for internal use only):
"___GFP_DMA": {"value": 0x01},
"___GFP_HIGHMEM": {"value": 0x02},
"___GFP_DMA32": {"value": 0x04},
"___GFP_MOVABLE": {"value": 0x08},
"___GFP_WAIT": {"value": 0x10},
"___GFP_HIGH": {"value": 0x20},
"___GFP_IO": {"value": 0x40},
"___GFP_FS": {"value": 0x80},
"___GFP_COLD": {"value": 0x100},
"___GFP_NOWARN": {"value": 0x200},
"___GFP_REPEAT": {"value": 0x400},
"___GFP_NOFAIL": {"value": 0x800},
"___GFP_NORETRY": {"value": 0x1000},
"___GFP_MEMALLOC": {"value": 0x2000},
"___GFP_COMP": {"value": 0x4000},
"___GFP_ZERO": {"value": 0x8000},
"___GFP_NOMEMALLOC": {"value": 0x10000},
"___GFP_HARDWALL": {"value": 0x20000},
"___GFP_RECLAIMABLE": {"value": 0x80000},
"___GFP_KMEMCG": {"value": 0x100000},
"___GFP_NOTRACK": {"value": 0x200000},
"___GFP_NO_KSWAPD": {"value": 0x400000},
"___GFP_OTHER_NODE": {"value": 0x800000},
"___GFP_WRITE": {"value": 0x1000000},
}
"""
Definition of GFP flags
The decimal value of a flag will be calculated by evaluating the entries from left to right. Grouping by
parentheses is not supported.
Source: include/linux/gpf.h
@note: This list os probably a mixture of different kernel versions - be carefully
"""
gfp_reverse_lookup = []
"""
Sorted list of flags used to do a reverse lookup.
This list doesn't contain all flags. It contains the "useful flags" (GFP_*) as
well as "modifier flags" (__GFP_*). "Plain flags" (___GFP_*) are not part of
this list.
@type: List(str)
@see: _gfp_create_reverse_lookup()
"""
MAX_ORDER = -1
"""
The kernel memory allocator divides physically contiguous memory
blocks into "zones", where each zone is a power of two number of
pages. This option selects the largest power of two that the kernel
keeps in the memory allocator.
This config option is actually maximum order plus one. For example,
a value of 11 means that the largest free memory block is 2^10 pages.
The value will be calculated dynamically based on the numbers of
orders in OOMAnalyser._extract_buddyinfo().
@see: OOMAnalyser._extract_buddyinfo().
"""
pstable_items = [
"pid",
"uid",
"tgid",
"total_vm_pages",
"rss_pages",
"nr_ptes_pages",
"swapents_pages",
"oom_score_adj",
"name",
"notes",
]
"""Elements of the process table"""
PAGE_ALLOC_COSTLY_ORDER = 3
"""
Requests with order > PAGE_ALLOC_COSTLY_ORDER will never trigger the OOM-killer to satisfy the request.
"""
PLATFORM_DESCRIPTION = (
("aarch64", "ARM 64-bit"),
("amd64", "x86 64-bit"),
("arm64", "ARM 64-bit"),
("armv8", "ARM 64-bit"),
("i386", "x86 32-bit"),
("i686", "x86 32-bit (6th generation)"),
("x86_64", "x86 64-bit"),
)
"""
Brief description of some platforms based on an identifier
"""
pstable_html = [
"PID",
"UID",
"TGID",
"Total VM",
"RSS",
"Page Table Entries",
"Swap Entries",
"OOM Adjustment",
"Name",
"Notes",
]
"""
Headings of the process table columns
"""
pstable_non_ints = ["pid", "name", "notes"]
"""Columns that are not converted to an integer"""
pstable_start = "[ pid ]"
"""
Pattern to find the start of the process table
@type: str
"""
release = (3, 10, "")
"""
Kernel release with this configuration
The tuple contains major and minor version as well as a suffix like "-aws" or ".el7."
The patch level isn't part of this version tuple, because I don't assume any changes in GFP flags within a patch
release.
@see: OOMAnalyser._choose_kernel_config()
@type: (int, int, str)
"""
REC_FREE_MEMORY_CHUNKS = re.compile(
r"Node (?P<node>\d+) (?P<zone>DMA|DMA32|Normal): (?P<zone_usage>.*) = (?P<total_free_kb_per_node>\d+)kB"
)
"""RE to extract free memory chunks of a memory zone"""
REC_OOM_BEGIN = re.compile(r"invoked oom-killer:", re.MULTILINE)
"""RE to match the first line of an OOM block"""
REC_OOM_END = re.compile(r"^Killed process \d+", re.MULTILINE)
"""RE to match the last line of an OOM block"""
REC_PAGE_SIZE = re.compile(r"Node 0 DMA: \d+\*(?P<page_size>\d+)kB")
"""RE to extract the page size from buddyinfo DMA zone"""
REC_PROCESS_LINE = re.compile(
r"^\[(?P<pid>[ \d]+)\]\s+(?P<uid>\d+)\s+(?P<tgid>\d+)\s+(?P<total_vm_pages>\d+)\s+(?P<rss_pages>\d+)\s+"
r"(?P<nr_ptes_pages>\d+)\s+(?P<swapents_pages>\d+)\s+(?P<oom_score_adj>-?\d+)\s+(?P<name>.+)\s*"
)
"""Match content of process table"""
REC_WATERMARK = re.compile(
r"Node (?P<node>\d+) (?P<zone>DMA|DMA32|Normal) "
r"free:(?P<free>\d+)kB "
r"min:(?P<min>\d+)kB "
r"low:(?P<low>\d+)kB "
r"high:(?P<high>\d+)kB "
r".*"
)
"""
RE to extract watermark information in a memory zone
Source: mm/page_alloc.c:__show_free_areas()
"""
watermark_start = "Node 0 DMA free:"
"""
Pattern to find the start of the memory watermark information
@type: str
"""
zoneinfo_start = "Node 0 DMA: "
"""
Pattern to find the start of the memory chunk information (buddyinfo)
@type: str
"""
ZONE_TYPES = ["DMA", "DMA32", "Normal", "HighMem", "Movable"]
"""
List of memory zones
@type: List(str)
"""
def __init__(self):
super().__init__()
if self.EXTRACT_PATTERN is None:
# Create a copy to prevent modifications on the class dictionary
# TODO replace with self.EXTRACT_PATTERN = self.EXTRACT_PATTERN.copy() after
# https://github.com/QQuick/Transcrypt/issues/716 "dict does not have a copy method" is fixed
self.EXTRACT_PATTERN = {}
self.EXTRACT_PATTERN.update(self.EXTRACT_PATTERN_BASE)
if self.EXTRACT_PATTERN_OVERLAY:
self.EXTRACT_PATTERN.update(self.EXTRACT_PATTERN_OVERLAY)
self._gfp_calc_all_values()
self.gfp_reverse_lookup = self._gfp_create_reverse_lookup()
self._check_mandatory_gfp_flags()
def _gfp_calc_all_values(self):
"""
Calculate decimal values for all GFP flags and store in in GFP_FLAGS[<flag>]["_value"]
"""
# __pragma__ ('jsiter')
for flag in self.GFP_FLAGS:
value = self._gfp_flag2decimal(flag)
self.GFP_FLAGS[flag]["_value"] = value
# __pragma__ ('nojsiter')
def _gfp_flag2decimal(self, flag):
"""\
Convert a single flag into a decimal value.
The flags can be concatenated with "|" or "~" and negated with "~". The
flags will be processed from left to right. Parentheses are not supported.
"""
if flag not in self.GFP_FLAGS:
error("Missing definition for flag {}".format(flag))
return 0
value = self.GFP_FLAGS[flag]["value"]
if isinstance(value, int):
return value
tokenlist = iter(re.split("([|&])", value))
operator = "|" # set to process first flag
negate_rvalue = False
lvalue = 0
while True:
try:
token = next(tokenlist)
except StopIteration:
break
token = token.strip()
if token in ["|", "&"]:
operator = token
continue
if token.startswith("~"):
token = token[1:]
negate_rvalue = True
if token.isdigit():
rvalue = int(token)
elif token.startswith("0x") and token[2:].isdigit():
rvalue = int(token, 16)
else:
# it's not a decimal nor a hexadecimal value - reiterate assuming it's a flag string
rvalue = self._gfp_flag2decimal(token)
if negate_rvalue:
rvalue = ~rvalue
if operator == "|":
lvalue |= rvalue
elif operator == "&":
lvalue &= rvalue
operator = None
negate_rvalue = False
return lvalue
def _gfp_create_reverse_lookup(self):
"""
Create a sorted list of flags used to do a reverse lookup from value to the flag.
@rtype: List(str)
"""
# __pragma__ ('jsiter')
useful = [
key
for key in self.GFP_FLAGS
if key.startswith("GFP") and self.GFP_FLAGS[key]["_value"] != 0
]
useful = sorted(
useful, key=lambda key: self.GFP_FLAGS[key]["_value"], reverse=True
)
modifier = [
key
for key in self.GFP_FLAGS
if key.startswith("__GFP") and self.GFP_FLAGS[key]["_value"] != 0
]
modifier = sorted(
modifier, key=lambda key: self.GFP_FLAGS[key]["_value"], reverse=True
)
# __pragma__ ('nojsiter')
# useful + modifier produces a string with all values concatenated
res = useful
res.extend(modifier)
return res
def _check_mandatory_gfp_flags(self):
"""
Check existence of mandatory flags used in
OOMAnalyser._calc_trigger_process_values() to calculate the memory zone
"""
if "__GFP_DMA" not in self.GFP_FLAGS:
error(
"Missing definition of GFP flag __GFP_DMA for kernel {}.{}.{}".format(
*self.release
)
)
if "__GFP_DMA32" not in self.GFP_FLAGS:
error(
"Missing definition of GFP flag __GFP_DMA for kernel {}.{}.{}".format(
*self.release
)
)
class KernelConfig_3_10(BaseKernelConfig):
name = "Configuration for Linux kernel 3.10 or later"
release = (3, 10, "")
# NOTE: These flags are automatically extracted from a gfp.h file.
# Please do not change them manually!
GFP_FLAGS = {
#
#
# Useful GFP flag combinations:
"GFP_ATOMIC": {"value": "__GFP_HIGH"},
"GFP_HIGHUSER": {
"value": "__GFP_WAIT | __GFP_IO | __GFP_FS | __GFP_HARDWALL | __GFP_HIGHMEM"
},
"GFP_HIGHUSER_MOVABLE": {
"value": "__GFP_WAIT | __GFP_IO | __GFP_FS | __GFP_HARDWALL | __GFP_HIGHMEM | __GFP_MOVABLE"
},
"GFP_IOFS": {"value": "__GFP_IO | __GFP_FS"},
"GFP_KERNEL": {"value": "__GFP_WAIT | __GFP_IO | __GFP_FS"},
"GFP_NOFS": {"value": "__GFP_WAIT | __GFP_IO"},
"GFP_NOIO": {"value": "__GFP_WAIT"},
"GFP_NOWAIT": {"value": "GFP_ATOMIC & ~__GFP_HIGH"},
"GFP_TEMPORARY": {
"value": "__GFP_WAIT | __GFP_IO | __GFP_FS | __GFP_RECLAIMABLE"
},
"GFP_TRANSHUGE": {
"value": "GFP_HIGHUSER_MOVABLE | __GFP_COMP | __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD"
},
"GFP_USER": {"value": "__GFP_WAIT | __GFP_IO | __GFP_FS | __GFP_HARDWALL"},
#
#
# Modifier, mobility and placement hints:
"__GFP_COLD": {"value": "___GFP_COLD"},
"__GFP_COMP": {"value": "___GFP_COMP"},
"__GFP_DMA": {"value": "___GFP_DMA"},
"__GFP_DMA32": {"value": "___GFP_DMA32"},
"__GFP_FS": {"value": "___GFP_FS"},
"__GFP_HARDWALL": {"value": "___GFP_HARDWALL"},
"__GFP_HIGH": {"value": "___GFP_HIGH"},
"__GFP_HIGHMEM": {"value": "___GFP_HIGHMEM"},
"__GFP_IO": {"value": "___GFP_IO"},
"__GFP_KMEMCG": {"value": "___GFP_KMEMCG"},
"__GFP_MEMALLOC": {"value": "___GFP_MEMALLOC"},
"__GFP_MOVABLE": {"value": "___GFP_MOVABLE"},
"__GFP_NOFAIL": {"value": "___GFP_NOFAIL"},
"__GFP_NOMEMALLOC": {"value": "___GFP_NOMEMALLOC"},
"__GFP_NORETRY": {"value": "___GFP_NORETRY"},
"__GFP_NOTRACK": {"value": "___GFP_NOTRACK"},
"__GFP_NOTRACK_FALSE_POSITIVE": {"value": "__GFP_NOTRACK"},
"__GFP_NOWARN": {"value": "___GFP_NOWARN"},
"__GFP_NO_KSWAPD": {"value": "___GFP_NO_KSWAPD"},
"__GFP_OTHER_NODE": {"value": "___GFP_OTHER_NODE"},
"__GFP_RECLAIMABLE": {"value": "___GFP_RECLAIMABLE"},
"__GFP_REPEAT": {"value": "___GFP_REPEAT"},
"__GFP_WAIT": {"value": "___GFP_WAIT"},
"__GFP_WRITE": {"value": "___GFP_WRITE"},
"__GFP_ZERO": {"value": "___GFP_ZERO"},
#
#
# Plain integer GFP bitmasks (for internal use only):
"___GFP_DMA": {"value": 0x01},
"___GFP_HIGHMEM": {"value": 0x02},
"___GFP_DMA32": {"value": 0x04},
"___GFP_MOVABLE": {"value": 0x08},
"___GFP_WAIT": {"value": 0x10},
"___GFP_HIGH": {"value": 0x20},
"___GFP_IO": {"value": 0x40},
"___GFP_FS": {"value": 0x80},
"___GFP_COLD": {"value": 0x100},
"___GFP_NOWARN": {"value": 0x200},
"___GFP_REPEAT": {"value": 0x400},
"___GFP_NOFAIL": {"value": 0x800},
"___GFP_NORETRY": {"value": 0x1000},
"___GFP_MEMALLOC": {"value": 0x2000},
"___GFP_COMP": {"value": 0x4000},
"___GFP_ZERO": {"value": 0x8000},
"___GFP_NOMEMALLOC": {"value": 0x10000},
"___GFP_HARDWALL": {"value": 0x20000},
"___GFP_RECLAIMABLE": {"value": 0x80000},
"___GFP_KMEMCG": {"value": 0x100000},
"___GFP_NOTRACK": {"value": 0x200000},
"___GFP_NO_KSWAPD": {"value": 0x400000},
"___GFP_OTHER_NODE": {"value": 0x800000},
"___GFP_WRITE": {"value": 0x1000000},
}
class KernelConfig_3_10_EL7(KernelConfig_3_10):
# Supported changes:
# * update GFP flags
name = "Configuration for RHEL 7 / CentOS 7 specific Linux kernel (3.10)"
release = (3, 10, ".el7.")
# NOTE: These flags are automatically extracted from a gfp.h file.
# Please do not change them manually!
GFP_FLAGS = {
#
#
# Useful GFP flag combinations:
"GFP_ATOMIC": {"value": "__GFP_HIGH"},
"GFP_HIGHUSER": {
"value": "__GFP_WAIT | __GFP_IO | __GFP_FS | __GFP_HARDWALL | __GFP_HIGHMEM"
},
"GFP_HIGHUSER_MOVABLE": {
"value": "__GFP_WAIT | __GFP_IO | __GFP_FS | __GFP_HARDWALL | __GFP_HIGHMEM | __GFP_MOVABLE"
},
"GFP_IOFS": {"value": "__GFP_IO | __GFP_FS"},
"GFP_KERNEL": {"value": "__GFP_WAIT | __GFP_IO | __GFP_FS"},
"GFP_KERNEL_ACCOUNT": {"value": "GFP_KERNEL | __GFP_ACCOUNT"},
"GFP_NOFS": {"value": "__GFP_WAIT | __GFP_IO"},
"GFP_NOIO": {"value": "__GFP_WAIT"},
"GFP_NOWAIT": {"value": "GFP_ATOMIC & ~__GFP_HIGH"},
"GFP_TEMPORARY": {
"value": "__GFP_WAIT | __GFP_IO | __GFP_FS | __GFP_RECLAIMABLE"
},
"GFP_TRANSHUGE": {
"value": "GFP_HIGHUSER_MOVABLE | __GFP_COMP | __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD"
},
"GFP_USER": {"value": "__GFP_WAIT | __GFP_IO | __GFP_FS | __GFP_HARDWALL"},
#
#
# Modifier, mobility and placement hints:
"__GFP_ACCOUNT": {"value": "___GFP_ACCOUNT"},
"__GFP_COLD": {"value": "___GFP_COLD"},
"__GFP_COMP": {"value": "___GFP_COMP"},
"__GFP_DMA": {"value": "___GFP_DMA"},
"__GFP_DMA32": {"value": "___GFP_DMA32"},
"__GFP_FS": {"value": "___GFP_FS"},
"__GFP_HARDWALL": {"value": "___GFP_HARDWALL"},
"__GFP_HIGH": {"value": "___GFP_HIGH"},
"__GFP_HIGHMEM": {"value": "___GFP_HIGHMEM"},
"__GFP_IO": {"value": "___GFP_IO"},
"__GFP_MEMALLOC": {"value": "___GFP_MEMALLOC"},
"__GFP_MOVABLE": {"value": "___GFP_MOVABLE"},
"__GFP_NOFAIL": {"value": "___GFP_NOFAIL"},
"__GFP_NOMEMALLOC": {"value": "___GFP_NOMEMALLOC"},
"__GFP_NORETRY": {"value": "___GFP_NORETRY"},
"__GFP_NOTRACK": {"value": "___GFP_NOTRACK"},
"__GFP_NOTRACK_FALSE_POSITIVE": {"value": "__GFP_NOTRACK"},
"__GFP_NOWARN": {"value": "___GFP_NOWARN"},
"__GFP_NO_KSWAPD": {"value": "___GFP_NO_KSWAPD"},
"__GFP_OTHER_NODE": {"value": "___GFP_OTHER_NODE"},
"__GFP_RECLAIMABLE": {"value": "___GFP_RECLAIMABLE"},
"__GFP_REPEAT": {"value": "___GFP_REPEAT"},
"__GFP_WAIT": {"value": "___GFP_WAIT"},
"__GFP_WRITE": {"value": "___GFP_WRITE"},
"__GFP_ZERO": {"value": "___GFP_ZERO"},
#
#
# Plain integer GFP bitmasks (for internal use only):
"___GFP_DMA": {"value": 0x01},
"___GFP_HIGHMEM": {"value": 0x02},
"___GFP_DMA32": {"value": 0x04},
"___GFP_MOVABLE": {"value": 0x08},
"___GFP_WAIT": {"value": 0x10},
"___GFP_HIGH": {"value": 0x20},
"___GFP_IO": {"value": 0x40},
"___GFP_FS": {"value": 0x80},
"___GFP_COLD": {"value": 0x100},
"___GFP_NOWARN": {"value": 0x200},
"___GFP_REPEAT": {"value": 0x400},
"___GFP_NOFAIL": {"value": 0x800},
"___GFP_NORETRY": {"value": 0x1000},
"___GFP_MEMALLOC": {"value": 0x2000},
"___GFP_COMP": {"value": 0x4000},
"___GFP_ZERO": {"value": 0x8000},
"___GFP_NOMEMALLOC": {"value": 0x10000},
"___GFP_HARDWALL": {"value": 0x20000},
"___GFP_RECLAIMABLE": {"value": 0x80000},
"___GFP_ACCOUNT": {"value": 0x100000},
"___GFP_NOTRACK": {"value": 0x200000},
"___GFP_NO_KSWAPD": {"value": 0x400000},
"___GFP_OTHER_NODE": {"value": 0x800000},
"___GFP_WRITE": {"value": 0x1000000},
}
class KernelConfig_3_16(KernelConfig_3_10):
# Supported changes:
# * update GFP flags
name = "Configuration for Linux kernel 3.16 or later"
release = (3, 16, "")
# NOTE: These flags are automatically extracted from a gfp.h file.
# Please do not change them manually!
GFP_FLAGS = {
#
#
# Useful GFP flag combinations:
"GFP_ATOMIC": {"value": "__GFP_HIGH"},
"GFP_HIGHUSER": {
"value": "__GFP_WAIT | __GFP_IO | __GFP_FS | __GFP_HARDWALL | __GFP_HIGHMEM"
},
"GFP_HIGHUSER_MOVABLE": {
"value": "__GFP_WAIT | __GFP_IO | __GFP_FS | __GFP_HARDWALL | __GFP_HIGHMEM | __GFP_MOVABLE"
},
"GFP_IOFS": {"value": "__GFP_IO | __GFP_FS"},
"GFP_KERNEL": {"value": "__GFP_WAIT | __GFP_IO | __GFP_FS"},
"GFP_NOFS": {"value": "__GFP_WAIT | __GFP_IO"},
"GFP_NOIO": {"value": "__GFP_WAIT"},