1#!/usr/bin/env python2
2# Copyright 2019 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import unittest
7
8from autotest_lib.client.cros.graphics.graphics_utils import GraphicsKernelMemory
9
10class GraphicsKernelMemoryTest(unittest.TestCase):
11    def testParseSysfs_i915_gem_objects(self):
12        """Test parsing kernel 5.x i915_gem_objects"""
13        contents = '''274 shrinkable [0 free] objects, 249675776 bytes
14
15frecon: 3 objects, 72192000 bytes (0 active, 0 inactive, 0 unbound, 0 closed)
16chrome: 6 objects, 74629120 bytes (0 active, 0 inactive, 901120 unbound, 0 closed)
17chrome: 14 objects, 1765376 bytes (0 active, 552960 inactive, 1212416 unbound, 0 closed)
18chrome: 291 objects, 152686592 bytes (0 active, 0 inactive, 1183744 unbound, 0 closed)
19chrome: 291 objects, 152686592 bytes (0 active, 75231232 inactive, 1183744 unbound, 0 closed)
20chrome: 291 objects, 152686592 bytes (0 active, 155648 inactive, 1183744 unbound, 0 closed)
21chrome: 291 objects, 152686592 bytes (64241664 active, 60248064 inactive, 1183744 unbound, 0 closed)
22chrome: 291 objects, 152686592 bytes (0 active, 106496 inactive, 1183744 unbound, 0 closed)
23chrome: 291 objects, 152686592 bytes (0 active, 724992 inactive, 1183744 unbound, 0 closed)
24chrome: 291 objects, 152686592 bytes (0 active, 122880 inactive, 1183744 unbound, 0 closed)
25chrome: 291 objects, 152686592 bytes (0 active, 122880 inactive, 1183744 unbound, 0 closed)
26chrome: 291 objects, 152686592 bytes (0 active, 122880 inactive, 1183744 unbound, 0 closed)
27chrome: 291 objects, 152686592 bytes (0 active, 479232 inactive, 1183744 unbound, 0 closed)
28chrome: 291 objects, 152686592 bytes (0 active, 581632 inactive, 1183744 unbound, 0 closed)
29[k]contexts: 4 objects, 221184 bytes (0 active, 221184 inactive, 0 unbound, 0 closed)'''
30        expected_results = {'bytes': 249675776, 'objects': 274}
31
32        self.assertEqual(expected_results,
33                         GraphicsKernelMemory._parse_sysfs(contents))
34
35    def testParseSysfs_i915_gem_objects_kernel_4_4(self):
36        """Test parsing kernel 4.x i915_gem_objects"""
37        contents = '''557 objects, 100163584 bytes
3880 unbound objects, 3653632 bytes
39469 bound objects, 95195136 bytes
4047 purgeable objects, 2846720 bytes
4123 mapped objects, 1757184 bytes
423 display objects (pinned), 8716288 bytes
434294967296 [268435456] gtt total
44
45[k]contexts: 46 objects, 2134016 bytes (0 active, 2134016 inactive, 2134016 global, 0 shared, 0 unbound)
46frecon: 3 objects, 12681216 bytes (0 active, 12681216 inactive, 12681216 global, 0 shared, 0 unbound)
47chrome: 11 objects, 14626816 bytes (0 active, 12943360 inactive, 12943360 global, 14102528 shared, 1683456 unbound)
48chrome: 24 objects, 4411392 bytes (0 active, 3117056 inactive, 0 global, 0 shared, 1294336 unbound)
49chrome: 465 objects, 67448832 bytes (25202688 active, 76062720 inactive, 13443072 global, 14102528 shared, 3227648 unbound)
50surfaceflinger: 11 objects, 270336 bytes (0 active, 86016 inactive, 0 global, 0 shared, 184320 unbound)'''
51        expected_results  = {'bytes': 100163584, 'objects': 557}
52
53        self.assertEqual(expected_results,
54                         GraphicsKernelMemory._parse_sysfs(contents))
55
56    def testParseSysfs_i915_gem_gtt(self):
57        """Test parsing kernel 4.x i915_gem_gtt"""
58        contents = '''   ffff88017a138000:              4KiB 41 00  uncached (pinned x 1) (ggtt offset: ffffe000, size: 00001000, normal) (stolen: 00001000)
59   ffff88017a138300:              4KiB 01 01  uncached (pinned x 1) (ggtt offset: ffffd000, size: 00001000, normal)
60   ffff88017a138600:     M       92KiB 01 01  uncached dirty (pinned x 1) (ggtt offset: fffe6000, size: 00017000, normal)
61   ffff88017a138900:             16KiB 40 40  uncached dirty (pinned x 1) (ggtt offset: 00080000, size: 00004000, normal) (stolen: 00002000)
62<snip>
63   ffff880147fd4000:             32KiB 76 00  uncached dirty purgeable (pinned x 0) (ggtt offset: 01eae000, size: 00008000, normal) (ppgtt offset: f8010000, size: 00008000) (ppgtt offset: f8000000, size: 00008000) (ppgtt offset: f8010000, size: 00008000) (ppgtt offset: f8010000, size: 00008000) (ppgtt offset: f8010000, size: 00008000) (ppgtt offset: f8000000, size: 00008000)
64   ffff88015bd42700: *            4KiB 02 00  uncached dirty (pinned x 0) (ggtt offset: 01dc9000, size: 00001000, normal) (ppgtt offset: ffffff00d000, size: 00001000) (ppgtt offset: ffffff00d000, size: 00001000) (ppgtt offset: ffffff00d000, size: 00001000)
65   ffff88006409d800: * Y          4KiB 36 00  uncached dirty (pinned x 0) (ppgtt offset: ffffff04b000, size: 00001000) (ppgtt offset: ffffff04b000, size: 00001000)
66Total 470 objects, 99713024 bytes, 32903168 GTT size'''
67        expected_results = {'bytes': 99713024, 'objects': 470}
68
69        self.assertEqual(expected_results,
70                         GraphicsKernelMemory._parse_sysfs(contents))
71