1 /* 2 * Copyright (C) 2015 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 package android.surfacecomposition; 17 18 import android.util.Log; 19 20 /** 21 * This task will simulate CPU activity by consuming memory bandwidth from the system. 22 * Note: On most system the CPU and GPU will share the same memory. 23 */ 24 public class MemoryAccessTask { 25 private final static String TAG = "MemoryAccessTask"; 26 private final static int BUFFER_SIZE = 32 * 1024 * 1024; 27 private final static int BUFFER_STEP = 256; 28 private boolean mStopRequested; 29 private WorkThread mThread; 30 private final Object mLock = new Object(); 31 32 public class WorkThread extends Thread { run()33 public void run() { 34 byte[] memory = new byte[BUFFER_SIZE]; 35 while (true) { 36 synchronized (mLock) { 37 if (mStopRequested) { 38 break; 39 } 40 } 41 long result = 0; 42 for (int index = 0; index < BUFFER_SIZE; index += BUFFER_STEP) { 43 result += ++memory[index]; 44 } 45 Log.v(TAG, "Processing...:" + result); 46 } 47 } 48 } 49 start()50 public void start() { 51 if (mThread != null) { 52 throw new RuntimeException("Work thread is already started"); 53 } 54 mStopRequested = false; 55 mThread = new WorkThread(); 56 mThread.start(); 57 } 58 stop()59 public void stop() { 60 if (mThread != null) { 61 synchronized (mLock) { 62 mStopRequested = true; 63 } 64 try { 65 mThread.join(); 66 } catch (InterruptedException e) { 67 e.printStackTrace(); 68 } 69 } 70 } 71 } 72