> 其它学习 > 日常运维 >

FPS帧率统计工具

从trace和录屏能算出大致帧率,更准确的帧率数据还需要从代码中统计

为了评估流畅度是否好,要计算1s 实际绘制的帧数,空闲不绘制的帧数,所以最好用2个帧率:

一个是满帧fps,一个是实际绘制fps,

好处是相比Surfaceflinger统计消费App生成的Buffer数量,可以从2个fps知道发现低于60帧,是真的掉帧了,还是没有绘制不是掉帧

如:doFrame ran 60 times (Real:34) 表示实际只绘制了34帧,但是可以绘制到60帧

实现原理:

在Choreographer的doFrame方法中加入一个计数器mFrameCount;当doFrame方法被调用时+1(mFrameCount++),启动1个Timer,1秒打印一个次logcat

每次doframe之后强制调用scheduleVsyncLocked()接受vsync,记录强制注册vsync的doFrame次数用变量mFrameMuteCount记录

但是统计不到Surfaceview(视频和地图),可以通过命令查看最近128帧内平均fps

dumpsys SurfaceFlinger --latency {layer_name}

输出结果:SurfaceView[renderframe]在最近128帧内平均fps: 29.88

使用方法:python fps.py com.map/com.map.MainActivity#0

dumpsys SurfaceFlinger --latency原理是:

https://cs.android.com/android/platform/superproject/+/master:frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp

# prints some information about the last 128 frames displayed in # that window.The data returned looks like this: # 16954612 # 7657467895508 7657482691352 7657493499756 # (...) # # The first line is the refresh period of the HWC(vsync,here 16.95 ms)(e.g. 60hz->16.6ms 90hz->11.1ms) # 1st) app-vsync (fps.py get this value) # 2st) HWC-vsync # 3st) timestamp after SF submitted frame to the HWC # The difference between the 1st and 3rd timestamp is the frame-latency. # An interesting data is when the frame latency crosses a refresh period # boundary, this can be calculated this way: # # ceil((C - A) / refresh-period) # # (each time the number above changes, we have a "jank"). # If this happens a lot during an animation, the animation appears # janky, even if it runs at 60 fps in average void SurfaceFlinger::dumpStatsLocked(const DumpArgs& args, std::string& result) const { StringAppendF(&result, "%" PRId64 "\n", getVsyncPeriodFromHWC()); if (args.size() > 1) { const auto name = String8(args[1]); mCurrentState.traverseInZOrder([&](Layer* layer) { if (layer->getName() == name.string()) { layer->dumpFrameStats(result); } }); } else { mAnimFrameTracker.dumpStats(result); } }
其它统计方式:
window.addOnFrameMetricsAvailableListener 性能好,数据全,关键是找好接入时机

(官方推荐统计方式 https://zhuanlan.zhihu.com/p/619383056

adb shell dumpsys gfxinfo com.demo framestats 从应用生成的最近120个帧中输出带有纳秒时间戳的帧时间信息




(责任编辑:IT)