blob: 73854bce67550f33bc1bff5baacb934bf78614d8 [file] [log] [blame]
Naseer Ahmed29a26812012-06-14 00:56:20 -07001/*
2 * Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
3
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of Code Aurora Forum, Inc. nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "profiler.h"
31
32#ifdef DEBUG_CALC_FPS
33
34#define LOG_TAG "CALCFPS"
35#define LOG_NDDEBUG 0
36
37ANDROID_SINGLETON_STATIC_INSTANCE(CalcFps) ;
38
39CalcFps::CalcFps() {
40 debug_fps_level = 0;
41 Init();
42}
43
44CalcFps::~CalcFps() {
45}
46
47void CalcFps::Init() {
48 char prop[PROPERTY_VALUE_MAX];
49 property_get("debug.gr.calcfps", prop, "0");
50 debug_fps_level = atoi(prop);
51 if (debug_fps_level > MAX_DEBUG_FPS_LEVEL) {
52 ALOGW("out of range value for debug.gr.calcfps, using 0");
53 debug_fps_level = 0;
54 }
55
56 ALOGD("DEBUG_CALC_FPS: %d", debug_fps_level);
57 populate_debug_fps_metadata();
58}
59
60void CalcFps::Fps() {
61 if (debug_fps_level > 0)
62 calc_fps(ns2us(systemTime()));
63}
64
65void CalcFps::populate_debug_fps_metadata(void)
66{
67 char prop[PROPERTY_VALUE_MAX];
68
69 /*defaults calculation of fps to based on number of frames*/
70 property_get("debug.gr.calcfps.type", prop, "0");
71 debug_fps_metadata.type = (debug_fps_metadata_t::DfmType) atoi(prop);
72
73 /*defaults to 1000ms*/
74 property_get("debug.gr.calcfps.timeperiod", prop, "1000");
75 debug_fps_metadata.time_period = atoi(prop);
76
77 property_get("debug.gr.calcfps.period", prop, "10");
78 debug_fps_metadata.period = atoi(prop);
79
80 if (debug_fps_metadata.period > MAX_FPS_CALC_PERIOD_IN_FRAMES) {
81 debug_fps_metadata.period = MAX_FPS_CALC_PERIOD_IN_FRAMES;
82 }
83
84 /* default ignorethresh_us: 500 milli seconds */
85 property_get("debug.gr.calcfps.ignorethresh_us", prop, "500000");
86 debug_fps_metadata.ignorethresh_us = atoi(prop);
87
88 debug_fps_metadata.framearrival_steps =
89 (debug_fps_metadata.ignorethresh_us / 16666);
90
91 if (debug_fps_metadata.framearrival_steps > MAX_FRAMEARRIVAL_STEPS) {
92 debug_fps_metadata.framearrival_steps = MAX_FRAMEARRIVAL_STEPS;
93 debug_fps_metadata.ignorethresh_us =
94 debug_fps_metadata.framearrival_steps * 16666;
95 }
96
97 /* 2ms margin of error for the gettimeofday */
98 debug_fps_metadata.margin_us = 2000;
99
100 for (unsigned int i = 0; i < MAX_FRAMEARRIVAL_STEPS; i++)
101 debug_fps_metadata.accum_framearrivals[i] = 0;
102
103 ALOGD("period: %d", debug_fps_metadata.period);
104 ALOGD("ignorethresh_us: %lld", debug_fps_metadata.ignorethresh_us);
105}
106
107void CalcFps::print_fps(float fps)
108{
109 if (debug_fps_metadata_t::DFM_FRAMES == debug_fps_metadata.type)
110 ALOGD("FPS for last %d frames: %3.2f", debug_fps_metadata.period, fps);
111 else
112 ALOGD("FPS for last (%f ms, %d frames): %3.2f",
113 debug_fps_metadata.time_elapsed,
114 debug_fps_metadata.curr_frame, fps);
115
116 debug_fps_metadata.curr_frame = 0;
117 debug_fps_metadata.time_elapsed = 0.0;
118
119 if (debug_fps_level > 1) {
120 ALOGD("Frame Arrival Distribution:");
121 for (unsigned int i = 0;
122 i < ((debug_fps_metadata.framearrival_steps / 6) + 1);
123 i++) {
124 ALOGD("%lld %lld %lld %lld %lld %lld",
125 debug_fps_metadata.accum_framearrivals[i*6],
126 debug_fps_metadata.accum_framearrivals[i*6+1],
127 debug_fps_metadata.accum_framearrivals[i*6+2],
128 debug_fps_metadata.accum_framearrivals[i*6+3],
129 debug_fps_metadata.accum_framearrivals[i*6+4],
130 debug_fps_metadata.accum_framearrivals[i*6+5]);
131 }
132
133 /* We are done with displaying, now clear the stats */
134 for (unsigned int i = 0;
135 i < debug_fps_metadata.framearrival_steps;
136 i++)
137 debug_fps_metadata.accum_framearrivals[i] = 0;
138 }
139 return;
140}
141
142void CalcFps::calc_fps(nsecs_t currtime_us)
143{
144 static nsecs_t oldtime_us = 0;
145
146 nsecs_t diff = currtime_us - oldtime_us;
147
148 oldtime_us = currtime_us;
149
150 if (debug_fps_metadata_t::DFM_FRAMES == debug_fps_metadata.type &&
151 diff > debug_fps_metadata.ignorethresh_us) {
152 return;
153 }
154
155 if (debug_fps_metadata.curr_frame < MAX_FPS_CALC_PERIOD_IN_FRAMES) {
156 debug_fps_metadata.framearrivals[debug_fps_metadata.curr_frame] = diff;
157 }
158
159 debug_fps_metadata.curr_frame++;
160
161 if (debug_fps_level > 1) {
162 unsigned int currstep = (diff + debug_fps_metadata.margin_us) / 16666;
163
164 if (currstep < debug_fps_metadata.framearrival_steps) {
165 debug_fps_metadata.accum_framearrivals[currstep-1]++;
166 }
167 }
168
169 if (debug_fps_metadata_t::DFM_FRAMES == debug_fps_metadata.type) {
170 if (debug_fps_metadata.curr_frame == debug_fps_metadata.period) {
171 /* time to calculate and display FPS */
172 nsecs_t sum = 0;
173 for (unsigned int i = 0; i < debug_fps_metadata.period; i++)
174 sum += debug_fps_metadata.framearrivals[i];
175 print_fps((debug_fps_metadata.period * float(1000000))/float(sum));
176 }
177 }
178 else if (debug_fps_metadata_t::DFM_TIME == debug_fps_metadata.type) {
179 debug_fps_metadata.time_elapsed += ((float)diff/1000.0);
180 if (debug_fps_metadata.time_elapsed >= debug_fps_metadata.time_period) {
181 float fps = (1000.0 * debug_fps_metadata.curr_frame)/
182 (float)debug_fps_metadata.time_elapsed;
183 print_fps(fps);
184 }
185 }
186 return;
187}
188#endif