[cost] add a mode to compute exclusive cost

Summary: Add a flag `is-inclusive-cost` (`true` by default) which computes inclusive cost for each function. Setting the flag to `false` computes exclusive cost of the function where the cost of the callees are assumed to be `0`.

Reviewed By: skcho

Differential Revision: D20558275

fbshipit-source-id: 6b5798916
master
Ezgi Çiçek 5 years ago committed by Facebook GitHub Bot
parent beaed4d820
commit cf50a387e5

@ -168,6 +168,7 @@ DIRECT_TESTS += \
java_infer \
java_litho-required-props \
java_performance \
java_performance-exclusive \
java_purity \
java_quandary \
java_racerd \

@ -1402,6 +1402,10 @@ INTERNAL OPTIONS
--icfg-dotty-outfile-reset
Cancel the effect of --icfg-dotty-outfile.
--no-inclusive-cost
Deactivates: Computes the inclusive cost (Conversely:
--inclusive-cost)
--incremental-analysis
Activates: [EXPERIMENTAL] Use incremental analysis for changed
files. Not compatible with --reanalyze and --continue-analysis.

@ -1402,6 +1402,10 @@ and icfg_dotty_outfile =
options that would generate icfg file otherwise"
and inclusive_cost =
CLOpt.mk_bool ~long:"inclusive-cost" ~default:true "Computes the inclusive cost"
and iphoneos_target_sdk_version =
CLOpt.mk_string_opt ~long:"iphoneos-target-sdk-version"
~in_help:InferCommand.[(Capture, manual_clang_linters)]
@ -2752,6 +2756,8 @@ and hoisting_report_only_expensive = !hoisting_report_only_expensive
and icfg_dotty_outfile = !icfg_dotty_outfile
and inclusive_cost = !inclusive_cost
and iphoneos_target_sdk_version = !iphoneos_target_sdk_version
and iphoneos_target_sdk_version_path_regex =

@ -373,6 +373,8 @@ val infer_is_javac : bool
val implicit_sdk_root : string option
val inclusive_cost : bool
val inferconfig_file : string option
val inferconfig_dir : string option

@ -60,7 +60,8 @@ module InstrBasicCost = struct
let get_instr_cost_record tenv extras instr_node instr =
match instr with
| Sil.Call (ret, Exp.Const (Const.Cfun callee_pname), params, _, _) ->
| Sil.Call (ret, Exp.Const (Const.Cfun callee_pname), params, _, _) when Config.inclusive_cost
->
let {inferbo_invariant_map; integer_type_widths; get_callee_summary_and_formals} = extras in
let operation_cost =
match
@ -94,6 +95,8 @@ module InstrBasicCost = struct
if is_allocation_function callee_pname then
CostDomain.plus CostDomain.unit_cost_allocation operation_cost
else operation_cost
| Sil.Call (_, Exp.Const (Const.Cfun _), _, _, _) ->
CostDomain.zero_record
| Sil.Load {id= lhs_id} when Ident.is_none lhs_id ->
(* dummy deref inserted by frontend--don't count as a step. In
JDK 11, dummy deref disappears and causes cost differences

@ -0,0 +1,22 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
class ExclusiveTest {
void linear(int x) {
for (int i = 0; i < x; i++) {}
}
void call_linear_exclusive_constant(int x) {
linear(x);
}
void call_linear_exclusive_linear(int x) {
for (int i = 0; i < x; i++) {
linear(i);
}
}
}

@ -0,0 +1,12 @@
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
TESTS_DIR = ../../..
INFER_OPTIONS = --cost-only --no-inclusive-cost --bufferoverrun --debug-exceptions --use-cost-threshold
INFERPRINT_OPTIONS = --issues-tests
SOURCES = $(wildcard *.java)
include $(TESTS_DIR)/javac.make

@ -0,0 +1,2 @@
codetoanalyze/java/performance-exclusive/ExclusiveTest.java, ExclusiveTest.call_linear_exclusive_linear(int):void, 0, EXPENSIVE_EXECUTION_TIME, no_bucket, ERROR, [with estimated cost 2 + 7 ⋅ x, O(x), degree = 1,{x},Loop at line 18]
codetoanalyze/java/performance-exclusive/ExclusiveTest.java, ExclusiveTest.linear(int):void, 0, EXPENSIVE_EXECUTION_TIME, no_bucket, ERROR, [with estimated cost 2 + 5 ⋅ x, O(x), degree = 1,{x},Loop at line 10]
Loading…
Cancel
Save