1# Copyright 2015 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import errno 6import hashlib 7import logging 8import math 9import mmap 10import os 11import re 12 13from autotest_lib.client.common_lib import error 14from autotest_lib.client.common_lib import file_utils 15from autotest_lib.client.cros import chrome_binary_test 16from autotest_lib.client.cros.video import helper_logger 17 18 19DOWNLOAD_BASE = ('http://commondatastorage.googleapis.com' 20 '/chromiumos-test-assets-public/') 21 22VEA_BINARY = 'video_encode_accelerator_unittest' 23TIME_BINARY = '/usr/local/bin/time' 24 25# The format used for 'time': <real time> <kernel time> <user time> 26TIME_OUTPUT_FORMAT = '%e %S %U' 27 28FRAME_STATS_SUFFIX = 'frame-data.csv' 29TEST_LOG_SUFFIX = 'test.log' 30TIME_LOG_SUFFIX = 'time.log' 31 32# Performance keys: 33# FPS (i.e. encoder throughput) 34KEY_FPS = 'fps' 35# Encode latencies at the 50th, 75th, and 95th percentiles. 36# Encode latency is the delay from input of a frame to output of the encoded 37# bitstream. 38KEY_ENCODE_LATENCY_50 = 'encode_latency.50_percentile' 39KEY_ENCODE_LATENCY_75 = 'encode_latency.75_percentile' 40KEY_ENCODE_LATENCY_95 = 'encode_latency.95_percentile' 41# CPU usage in kernel space 42KEY_CPU_KERNEL_USAGE = 'cpu_usage.kernel' 43# CPU usage in user space 44KEY_CPU_USER_USAGE = 'cpu_usage.user' 45 46# Units of performance values: 47UNIT_MILLISECOND = 'milliseconds' 48UNIT_MICROSECOND = 'us' 49UNIT_RATIO = 'ratio' 50UNIT_FPS = 'fps' 51 52RE_FPS = re.compile(r'^Measured encoder FPS: ([+\-]?[0-9.]+)$', re.MULTILINE) 53RE_ENCODE_LATENCY_50 = re.compile( 54 r'^Encode latency for the 50th percentile: (\d+) us$', 55 re.MULTILINE) 56RE_ENCODE_LATENCY_75 = re.compile( 57 r'^Encode latency for the 75th percentile: (\d+) us$', 58 re.MULTILINE) 59RE_ENCODE_LATENCY_95 = re.compile( 60 r'^Encode latency for the 95th percentile: (\d+) us$', 61 re.MULTILINE) 62 63 64def _remove_if_exists(filepath): 65 try: 66 os.remove(filepath) 67 except OSError, e: 68 if e.errno != errno.ENOENT: # no such file 69 raise 70 71 72class video_VEAPerf(chrome_binary_test.ChromeBinaryTest): 73 """ 74 This test monitors several performance metrics reported by Chrome test 75 binary, video_encode_accelerator_unittest. 76 """ 77 78 version = 1 79 80 def _logperf(self, test_name, key, value, units, higher_is_better=False): 81 description = '%s.%s' % (test_name, key) 82 self.output_perf_value( 83 description=description, value=value, units=units, 84 higher_is_better=higher_is_better) 85 86 87 def _analyze_fps(self, test_name, log_file): 88 """ 89 Analyzes FPS info from result log file. 90 """ 91 with open(log_file, 'r') as f: 92 mm = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) 93 fps = [float(m.group(1)) for m in RE_FPS.finditer(mm)] 94 mm.close() 95 if len(fps) != 1: 96 raise error.TestError('Parsing FPS failed w/ %d occurrence(s).' % 97 len(fps)) 98 self._logperf(test_name, KEY_FPS, fps[0], UNIT_FPS, True) 99 100 101 def _analyze_encode_latency(self, test_name, log_file): 102 """ 103 Analyzes encode latency from result log file. 104 """ 105 with open(log_file, 'r') as f: 106 mm = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) 107 latency_50 = [int(m.group(1)) for m in 108 RE_ENCODE_LATENCY_50.finditer(mm)] 109 latency_75 = [int(m.group(1)) for m in 110 RE_ENCODE_LATENCY_75.finditer(mm)] 111 latency_95 = [int(m.group(1)) for m in 112 RE_ENCODE_LATENCY_95.finditer(mm)] 113 mm.close() 114 if any([len(l) != 1 for l in [latency_50, latency_75, latency_95]]): 115 raise error.TestError('Parsing encode latency failed.') 116 self._logperf(test_name, KEY_ENCODE_LATENCY_50, latency_50[0], 117 UNIT_MICROSECOND) 118 self._logperf(test_name, KEY_ENCODE_LATENCY_75, latency_75[0], 119 UNIT_MICROSECOND) 120 self._logperf(test_name, KEY_ENCODE_LATENCY_95, latency_95[0], 121 UNIT_MICROSECOND) 122 123 124 def _analyze_cpu_usage(self, test_name, time_log_file): 125 """ 126 Analyzes CPU usage from the output of 'time' command. 127 """ 128 with open(time_log_file) as f: 129 content = f.read() 130 r, s, u = (float(x) for x in content.split()) 131 self._logperf(test_name, KEY_CPU_USER_USAGE, u / r, UNIT_RATIO) 132 self._logperf(test_name, KEY_CPU_KERNEL_USAGE, s / r, UNIT_RATIO) 133 134 def _analyze_frame_stats(self, test_name, frame_stats_file): 135 """ 136 Analyzes quality from --frame_stats output CSV. Assumes YUV420 (for MSE 137 samples per channel). 138 """ 139 def mse_to_psnr(samples, peak, mse): 140 """ 141 Generate PSNR from MSE for a frame. 142 """ 143 MAX_PSNR = 100.0 144 # Prevent a divide-by-zero, MSE at 0 is perfect quality (no error). 145 if mse == 0: 146 return MAX_PSNR 147 psnr = 10.0 * math.log10(peak * peak * samples / float(mse)) 148 return min(psnr, MAX_PSNR) 149 150 frame_ssim = {'y': [], 'u': [], 'v': [], 'combined': []} 151 frame_psnr = {'y': [], 'u': [], 'v': [], 'combined': []} 152 for line in open(frame_stats_file): 153 (frame, width, height, 154 ssim_y, ssim_u, ssim_v, mse_y, mse_u, mse_v) = line.split(',') 155 # Skip CSV header. 156 if frame == 'frame': 157 continue 158 frame = int(frame) 159 width = int(width) 160 height = int(height) 161 ssim_y = float(ssim_y) 162 ssim_u = float(ssim_u) 163 ssim_v = float(ssim_v) 164 mse_y = int(mse_y) 165 mse_u = int(mse_u) 166 mse_v = int(mse_v) 167 168 frame_ssim['y'].append(ssim_y) 169 frame_ssim['u'].append(ssim_u) 170 frame_ssim['v'].append(ssim_v) 171 # Weighting of YUV channels for SSIM taken from libvpx. 172 frame_ssim['combined'].append( 173 0.8 * ssim_y + 0.1 * (ssim_u + ssim_v)) 174 175 # Samples per MSE score assumes YUV420 subsampling. 176 frame_psnr['y'].append( 177 mse_to_psnr(width * height * 4 / 4, 255, mse_y)) 178 frame_psnr['u'].append( 179 mse_to_psnr(width * height * 1 / 4, 255, mse_u)) 180 frame_psnr['v'].append( 181 mse_to_psnr(width * height * 1 / 4, 255, mse_v)) 182 frame_psnr['combined'].append( 183 mse_to_psnr( 184 width * height * 6 / 4, 255, mse_y + mse_u + mse_v)) 185 186 for channel in ['y', 'u', 'v', 'combined']: 187 # Log stats with a key similar to 'quality.ssim.y.max'. For combined 188 # stats the channel is omitted ('quality.ssim.max'). 189 key = 'quality.%s' 190 if channel is not 'combined': 191 key += '.' + channel 192 key += '.%s' 193 for (stat, func) in [('min', min), ('max', max), 194 ('avg', lambda x: sum(x) / len(x))]: 195 self._logperf(test_name, key % ('ssim', stat), 196 func(frame_ssim[channel]), None, 197 higher_is_better=True) 198 self._logperf(test_name, key % ('psnr', stat), 199 func(frame_psnr[channel]), None, 200 higher_is_better=True) 201 202 def _get_profile_name(self, profile): 203 """ 204 Gets profile name from a profile index. 205 """ 206 if profile == 1: 207 return 'h264' 208 elif profile == 11: 209 return 'vp8' 210 else: 211 raise error.TestError('Internal error.') 212 213 214 def _convert_test_name(self, path, on_cloud, profile): 215 """Converts source path to test name and output video file name. 216 217 For example: for the path on cloud 218 "tulip2/tulip2-1280x720-1b95123232922fe0067869c74e19cd09.yuv" 219 220 We will derive the test case's name as "tulip2-1280x720.vp8" or 221 "tulip2-1280x720.h264" depending on the profile. The MD5 checksum in 222 path will be stripped. 223 224 For the local file, we use the base name directly. 225 226 @param path: The local path or download path. 227 @param on_cloud: Whether the file is on cloud. 228 @param profile: Profile index. 229 230 @returns a pair of (test name, output video file name) 231 """ 232 s = os.path.basename(path) 233 name = s[:s.rfind('-' if on_cloud else '.')] 234 profile_name = self._get_profile_name(profile) 235 return (name + '_' + profile_name, name + '.' + profile_name) 236 237 238 def _download_video(self, path_on_cloud, local_file): 239 url = '%s%s' % (DOWNLOAD_BASE, path_on_cloud) 240 logging.info('download "%s" to "%s"', url, local_file) 241 242 file_utils.download_file(url, local_file) 243 244 with open(local_file, 'r') as r: 245 md5sum = hashlib.md5(r.read()).hexdigest() 246 if md5sum not in path_on_cloud: 247 raise error.TestError('unmatched md5 sum: %s' % md5sum) 248 249 250 def _get_result_filename(self, test_name, subtype, suffix): 251 return os.path.join(self.resultsdir, 252 '%s_%s_%s' % (test_name, subtype, suffix)) 253 254 255 def _run_test_case(self, test_name, test_stream_data): 256 """ 257 Runs a VEA unit test. 258 259 @param test_name: Name of this test case. 260 @param test_stream_data: Parameter to --test_stream_data in vea_unittest. 261 """ 262 # Get FPS. 263 test_log_file = self._get_result_filename(test_name, 'fullspeed', 264 TEST_LOG_SUFFIX) 265 vea_args = [ 266 '--gtest_filter=EncoderPerf/*/0', 267 '--test_stream_data=%s' % test_stream_data, 268 '--output_log="%s"' % test_log_file, 269 helper_logger.chrome_vmodule_flag(), 270 '--ozone-platform=gbm'] 271 self.run_chrome_test_binary(VEA_BINARY, ' '.join(vea_args)) 272 self._analyze_fps(test_name, test_log_file) 273 274 # Get CPU usage and encode latency under specified frame rate. 275 test_log_file = self._get_result_filename(test_name, 'fixedspeed', 276 TEST_LOG_SUFFIX) 277 time_log_file = self._get_result_filename(test_name, 'fixedspeed', 278 TIME_LOG_SUFFIX) 279 vea_args = [ 280 '--gtest_filter=SimpleEncode/*/0', 281 '--test_stream_data=%s' % test_stream_data, 282 '--run_at_fps', '--measure_latency', 283 '--output_log="%s"' % test_log_file, 284 helper_logger.chrome_vmodule_flag(), 285 '--ozone-platform=gbm'] 286 time_cmd = ('%s -f "%s" -o "%s" ' % 287 (TIME_BINARY, TIME_OUTPUT_FORMAT, time_log_file)) 288 self.run_chrome_test_binary(VEA_BINARY, ' '.join(vea_args), 289 prefix=time_cmd) 290 self._analyze_encode_latency(test_name, test_log_file) 291 self._analyze_cpu_usage(test_name, time_log_file) 292 293 # TODO(pbos): Measure quality at more bitrates. 294 # Generate SSIM/PSNR scores (objective quality metrics). 295 test_log_file = self._get_result_filename(test_name, 'quality', 296 TEST_LOG_SUFFIX) 297 frame_stats_file = self._get_result_filename(test_name, 'quality', 298 FRAME_STATS_SUFFIX) 299 vea_args = [ 300 '--gtest_filter=SimpleEncode/*/0', 301 '--test_stream_data=%s' % test_stream_data, 302 '--frame_stats="%s"' % frame_stats_file, 303 '--output_log="%s"' % test_log_file, 304 helper_logger.chrome_vmodule_flag(), 305 '--ozone-platform=gbm'] 306 self.run_chrome_test_binary(VEA_BINARY, ' '.join(vea_args)) 307 self._analyze_frame_stats(test_name, frame_stats_file) 308 309 @helper_logger.video_log_wrapper 310 @chrome_binary_test.nuke_chrome 311 def run_once(self, test_cases): 312 """ 313 Tests ChromeOS video hardware encoder performance. 314 """ 315 last_error = None 316 for (path, on_cloud, width, height, requested_bit_rate, 317 profile, requested_frame_rate) in test_cases: 318 try: 319 test_name, output_name = self._convert_test_name( 320 path, on_cloud, profile) 321 if on_cloud: 322 input_path = os.path.join(self.tmpdir, 323 os.path.basename(path)) 324 self._download_video(path, input_path) 325 else: 326 input_path = os.path.join(self.cr_source_dir, path) 327 output_path = os.path.join(self.tmpdir, output_name) 328 test_stream_data = '%s:%s:%s:%s:%s:%s:%s' % ( 329 input_path, width, height, profile, output_path, 330 requested_bit_rate, requested_frame_rate) 331 self._run_test_case(test_name, test_stream_data) 332 except Exception as last_error: 333 # Log the error and continue to the next test case. 334 logging.exception(last_error) 335 finally: 336 if on_cloud: 337 _remove_if_exists(input_path) 338 _remove_if_exists(output_path) 339 340 if last_error: 341 raise last_error 342