aboutsummaryrefslogtreecommitdiff
path: root/heatmaps/heatmap_generator_test.py
blob: 5008c6530d67d6d55194f0bd499f3d12da4b14d6 (plain)
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2018 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Tests for heatmap_generator.py."""

from __future__ import division, print_function

import unittest.mock as mock
import unittest

import os

from heatmaps import heatmap_generator


def _write_perf_mmap(pid, tid, addr, size, fp):
  print(
      '0 0 0 0 PERF_RECORD_MMAP2 %d/%d: '
      '[%x(%x) @ 0x0 0:0 0 0] '
      'r-xp /opt/google/chrome/chrome\n' % (pid, tid, addr, size),
      file=fp)


def _write_perf_fork(pid_from, tid_from, pid_to, tid_to, fp):
  print(
      '0 0 0 0 PERF_RECORD_FORK(%d:%d):(%d:%d)\n' % (pid_to, tid_to, pid_from,
                                                     tid_from),
      file=fp)


def _write_perf_exit(pid_from, tid_from, pid_to, tid_to, fp):
  print(
      '0 0 0 0 PERF_RECORD_EXIT(%d:%d):(%d:%d)\n' % (pid_to, tid_to, pid_from,
                                                     tid_from),
      file=fp)


def _write_perf_sample(pid, tid, addr, fp):
  print(
      '0 0 0 0 PERF_RECORD_SAMPLE(IP, 0x2): '
      '%d/%d: %x period: 100000 addr: 0' % (pid, tid, addr),
      file=fp)
  print(' ... thread: chrome:%d' % tid, file=fp)
  print(' ...... dso: /opt/google/chrome/chrome\n', file=fp)


def _heatmap(file_name, page_size=4096, hugepage=None, analyze=False, top_n=10):
  generator = heatmap_generator.HeatmapGenerator(
      file_name, page_size, hugepage, '',
      log_level='none')  # Don't log to stdout
  generator.draw()
  if analyze:
    generator.analyze('/path/to/chrome', top_n)


def _cleanup(file_name):
  files = [
      file_name, 'out.txt', 'inst-histo.txt', 'inst-histo-hp.txt',
      'inst-histo-sp.txt', 'heat_map.png', 'timeline.png', 'addr2symbol.txt'
  ]
  for f in files:
    if os.path.exists(f):
      os.remove(f)


class HeatmapGeneratorDrawTests(unittest.TestCase):
  """All of our tests for heatmap_generator.draw() and related."""

  def test_with_one_mmap_one_sample(self):
    """Tests one perf record and one sample."""
    fname = 'test.txt'
    with open(fname, 'w') as f:
      _write_perf_mmap(101, 101, 0xABCD000, 0x100, f)
      _write_perf_sample(101, 101, 0xABCD101, f)
    self.addCleanup(_cleanup, fname)
    _heatmap(fname)
    self.assertIn('out.txt', os.listdir('.'))
    with open('out.txt') as f:
      lines = f.readlines()
      self.assertEqual(len(lines), 1)
      self.assertIn('101/101: 1 0', lines[0])

  def test_with_one_mmap_multiple_samples(self):
    """Tests one perf record and three samples."""
    fname = 'test.txt'
    with open(fname, 'w') as f:
      _write_perf_mmap(101, 101, 0xABCD000, 0x100, f)
      _write_perf_sample(101, 101, 0xABCD101, f)
      _write_perf_sample(101, 101, 0xABCD102, f)
      _write_perf_sample(101, 101, 0xABCE102, f)
    self.addCleanup(_cleanup, fname)
    _heatmap(fname)
    self.assertIn('out.txt', os.listdir('.'))
    with open('out.txt') as f:
      lines = f.readlines()
      self.assertEqual(len(lines), 3)
      self.assertIn('101/101: 1 0', lines[0])
      self.assertIn('101/101: 2 0', lines[1])
      self.assertIn('101/101: 3 4096', lines[2])

  def test_with_fork_and_exit(self):
    """Tests perf fork and perf exit."""
    fname = 'test_fork.txt'
    with open(fname, 'w') as f:
      _write_perf_mmap(101, 101, 0xABCD000, 0x100, f)
      _write_perf_fork(101, 101, 202, 202, f)
      _write_perf_sample(101, 101, 0xABCD101, f)
      _write_perf_sample(202, 202, 0xABCE101, f)
      _write_perf_exit(202, 202, 202, 202, f)
    self.addCleanup(_cleanup, fname)
    _heatmap(fname)
    self.assertIn('out.txt', os.listdir('.'))
    with open('out.txt') as f:
      lines = f.readlines()
      self.assertEqual(len(lines), 2)
      self.assertIn('101/101: 1 0', lines[0])
      self.assertIn('202/202: 2 4096', lines[1])

  def test_hugepage_creates_two_chrome_mmaps(self):
    """Test two chrome mmaps for the same process."""
    fname = 'test_hugepage.txt'
    with open(fname, 'w') as f:
      _write_perf_mmap(101, 101, 0xABCD000, 0x1000, f)
      _write_perf_fork(101, 101, 202, 202, f)
      _write_perf_mmap(202, 202, 0xABCD000, 0x100, f)
      _write_perf_mmap(202, 202, 0xABCD300, 0xD00, f)
      _write_perf_sample(101, 101, 0xABCD102, f)
      _write_perf_sample(202, 202, 0xABCD102, f)
    self.addCleanup(_cleanup, fname)
    _heatmap(fname)
    self.assertIn('out.txt', os.listdir('.'))
    with open('out.txt') as f:
      lines = f.readlines()
      self.assertEqual(len(lines), 2)
      self.assertIn('101/101: 1 0', lines[0])
      self.assertIn('202/202: 2 0', lines[1])

  def test_hugepage_creates_two_chrome_mmaps_fail(self):
    """Test two chrome mmaps for the same process."""
    fname = 'test_hugepage.txt'
    # Cases where first_mmap.size < second_mmap.size
    with open(fname, 'w') as f:
      _write_perf_mmap(101, 101, 0xABCD000, 0x1000, f)
      _write_perf_fork(101, 101, 202, 202, f)
      _write_perf_mmap(202, 202, 0xABCD000, 0x10000, f)
    self.addCleanup(_cleanup, fname)
    with self.assertRaises(AssertionError) as msg:
      _heatmap(fname)
    self.assertIn('Original MMAP size', str(msg.exception))

    # Cases where first_mmap.address > second_mmap.address
    with open(fname, 'w') as f:
      _write_perf_mmap(101, 101, 0xABCD000, 0x1000, f)
      _write_perf_fork(101, 101, 202, 202, f)
      _write_perf_mmap(202, 202, 0xABCC000, 0x10000, f)
    with self.assertRaises(AssertionError) as msg:
      _heatmap(fname)
    self.assertIn('Original MMAP starting address', str(msg.exception))

    # Cases where first_mmap.address + size <
    # second_mmap.address + second_mmap.size
    with open(fname, 'w') as f:
      _write_perf_mmap(101, 101, 0xABCD000, 0x1000, f)
      _write_perf_fork(101, 101, 202, 202, f)
      _write_perf_mmap(202, 202, 0xABCD100, 0x10000, f)
    with self.assertRaises(AssertionError) as msg:
      _heatmap(fname)
    self.assertIn('exceeds the end of original MMAP', str(msg.exception))

  def test_histogram(self):
    """Tests if the tool can generate correct histogram.

    In the tool, histogram is generated from statistics
    of perf samples (saved to out.txt). The histogram is
    generated by perf-to-inst-page.sh and saved to
    inst-histo.txt. It will be used to draw heat maps.
    """
    fname = 'test_histo.txt'
    with open(fname, 'w') as f:
      _write_perf_mmap(101, 101, 0xABCD000, 0x100, f)
      for i in range(100):
        _write_perf_sample(101, 101, 0xABCD000 + i, f)
        _write_perf_sample(101, 101, 0xABCE000 + i, f)
        _write_perf_sample(101, 101, 0xABFD000 + i, f)
        _write_perf_sample(101, 101, 0xAFCD000 + i, f)
    self.addCleanup(_cleanup, fname)
    _heatmap(fname)
    self.assertIn('inst-histo.txt', os.listdir('.'))
    with open('inst-histo.txt') as f:
      lines = f.readlines()
      self.assertEqual(len(lines), 4)
      self.assertIn('100 0', lines[0])
      self.assertIn('100 4096', lines[1])
      self.assertIn('100 196608', lines[2])
      self.assertIn('100 4194304', lines[3])

  def test_histogram_two_mb_page(self):
    """Tests handling of 2MB page."""
    fname = 'test_histo.txt'
    with open(fname, 'w') as f:
      _write_perf_mmap(101, 101, 0xABCD000, 0x100, f)
      for i in range(100):
        _write_perf_sample(101, 101, 0xABCD000 + i, f)
        _write_perf_sample(101, 101, 0xABCE000 + i, f)
        _write_perf_sample(101, 101, 0xABFD000 + i, f)
        _write_perf_sample(101, 101, 0xAFCD000 + i, f)
    self.addCleanup(_cleanup, fname)
    _heatmap(fname, page_size=2 * 1024 * 1024)
    self.assertIn('inst-histo.txt', os.listdir('.'))
    with open('inst-histo.txt') as f:
      lines = f.readlines()
      self.assertEqual(len(lines), 2)
      self.assertIn('300 0', lines[0])
      self.assertIn('100 4194304', lines[1])

  def test_histogram_in_and_out_hugepage(self):
    """Tests handling the case of separating samples in and out huge page."""
    fname = 'test_histo.txt'
    with open(fname, 'w') as f:
      _write_perf_mmap(101, 101, 0xABCD000, 0x100, f)
      for i in range(100):
        _write_perf_sample(101, 101, 0xABCD000 + i, f)
        _write_perf_sample(101, 101, 0xABCE000 + i, f)
        _write_perf_sample(101, 101, 0xABFD000 + i, f)
        _write_perf_sample(101, 101, 0xAFCD000 + i, f)
    self.addCleanup(_cleanup, fname)
    _heatmap(fname, hugepage=[0, 8192])
    file_list = os.listdir('.')
    self.assertNotIn('inst-histo.txt', file_list)
    self.assertIn('inst-histo-hp.txt', file_list)
    self.assertIn('inst-histo-sp.txt', file_list)
    with open('inst-histo-hp.txt') as f:
      lines = f.readlines()
      self.assertEqual(len(lines), 2)
      self.assertIn('100 0', lines[0])
      self.assertIn('100 4096', lines[1])
    with open('inst-histo-sp.txt') as f:
      lines = f.readlines()
      self.assertEqual(len(lines), 2)
      self.assertIn('100 196608', lines[0])
      self.assertIn('100 4194304', lines[1])


class HeatmapGeneratorAnalyzeTests(unittest.TestCase):
  """All of our tests for heatmap_generator.analyze() and related."""

  def setUp(self):
    # Use the same perf report for testing
    self.fname = 'test_histo.txt'
    with open(self.fname, 'w') as f:
      _write_perf_mmap(101, 101, 0xABCD000, 0x100, f)
      for i in range(10):
        _write_perf_sample(101, 101, 0xABCD000 + i, f)
        _write_perf_sample(101, 101, 0xABCE000 + i, f)
        _write_perf_sample(101, 101, 0xABFD000 + i, f)
    self.nm = ('000000000abcd000 t Func1@Page1\n'
               '000000000abcd001 t Func2@Page1\n'
               '000000000abcd0a0 t Func3@Page1andFunc1@Page2\n'
               '000000000abce010 t Func2@Page2\n'
               '000000000abfd000 t Func1@Page3\n')

  def tearDown(self):
    _cleanup(self.fname)

  @mock.patch('subprocess.check_output')
  def test_analyze_hot_pages_with_hp_top(self, mock_nm):
    """Test if the analyze() can print the top page with hugepage."""
    mock_nm.return_value = self.nm
    _heatmap(self.fname, hugepage=[0, 8192], analyze=True, top_n=1)
    file_list = os.listdir('.')
    self.assertIn('addr2symbol.txt', file_list)
    with open('addr2symbol.txt') as f:
      contents = f.read()
      self.assertIn('Func2@Page1 : 9', contents)
      self.assertIn('Func1@Page1 : 1', contents)
      self.assertIn('Func1@Page3 : 10', contents)
      # Only displaying one page in hugepage
      self.assertNotIn('Func3@Page1andFunc1@Page2 : 10', contents)

  @mock.patch('subprocess.check_output')
  def test_analyze_hot_pages_without_hp_top(self, mock_nm):
    """Test if the analyze() can print the top page without hugepage."""
    mock_nm.return_value = self.nm
    _heatmap(self.fname, analyze=True, top_n=1)
    file_list = os.listdir('.')
    self.assertIn('addr2symbol.txt', file_list)
    with open('addr2symbol.txt') as f:
      contents = f.read()
      self.assertIn('Func2@Page1 : 9', contents)
      self.assertIn('Func1@Page1 : 1', contents)
      # Only displaying one page
      self.assertNotIn('Func3@Page1andFunc1@Page2 : 10', contents)
      self.assertNotIn('Func1@Page3 : 10', contents)

  @mock.patch('subprocess.check_output')
  def test_analyze_hot_pages_with_hp_top10(self, mock_nm):
    """Test if the analyze() can print with default top 10."""
    mock_nm.return_value = self.nm
    _heatmap(self.fname, analyze=True)
    # Make sure nm command is called correctly.
    mock_nm.assert_called_with(['nm', '-n', '/path/to/chrome'])
    file_list = os.listdir('.')
    self.assertIn('addr2symbol.txt', file_list)
    with open('addr2symbol.txt') as f:
      contents = f.read()
      self.assertIn('Func2@Page1 : 9', contents)
      self.assertIn('Func1@Page1 : 1', contents)
      self.assertIn('Func3@Page1andFunc1@Page2 : 10', contents)
      self.assertIn('Func1@Page3 : 10', contents)


if __name__ == '__main__':
  unittest.main()