Static Value-Flow Analysis
PTAStat.cpp
Go to the documentation of this file.
1 //===- PTAStat.cpp -- Base class for statistics in SVF-----------------------//
2 //
3 // SVF: Static Value-Flow Analysis
4 //
5 // Copyright (C) <2013-> <Yulei Sui>
6 //
7 
8 // This program is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Affero General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
12 
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU Affero General Public License for more details.
17 
18 // You should have received a copy of the GNU Affero General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 //
21 //===----------------------------------------------------------------------===//
22 
23 /*
24  * PTAStat.cpp
25  *
26  * Created on: Oct 13, 2013
27  * Author: Yulei Sui
28  */
29 
30 #include <iomanip>
31 #include "Graphs/PTACallGraph.h"
32 #include "Util/PTAStat.h"
34 #include "SVFIR/SVFIR.h"
35 
36 using namespace SVF;
37 using namespace std;
38 
40  pta(p),
41  _vmrssUsageBefore(0),
42  _vmrssUsageAfter(0),
43  _vmsizeUsageBefore(0),
44  _vmsizeUsageAfter(0)
45 {
46  u32_t vmrss = 0;
47  u32_t vmsize = 0;
48  SVFUtil::getMemoryUsageKB(&vmrss, &vmsize);
49  setMemUsageBefore(vmrss, vmsize);
50 }
51 
53 {
54 
56 
57  callgraphStat();
58 
59  SVFIR* pag = SVFIR::getPAG();
60  for(SVFIR::iterator it = pag->begin(), eit = pag->end(); it!=eit; ++it)
61  {
62  PAGNode* node = it->second;
63  if(SVFUtil::isa<ObjVar>(node))
64  {
65  if(pta->isLocalVarInRecursiveFun(node->getId()))
66  {
67  localVarInRecursion.set(node->getId());
68  }
69  }
70  }
71  PTNumStatMap["LocalVarInRecur"] = localVarInRecursion.count();
72 
73  u32_t vmrss = 0;
74  u32_t vmsize = 0;
75  SVFUtil::getMemoryUsageKB(&vmrss, &vmsize);
76  setMemUsageAfter(vmrss, vmsize);
77  timeStatMap["MemoryUsageVmrss"] = _vmrssUsageAfter - _vmrssUsageBefore;
78  timeStatMap["MemoryUsageVmsize"] = _vmsizeUsageAfter - _vmsizeUsageBefore;
79 }
80 
82 {
83 
84  PTACallGraph* graph = pta->getCallGraph();
86  callgraphSCC->find();
87 
88  unsigned totalNode = 0;
89  unsigned totalCycle = 0;
90  unsigned nodeInCycle = 0;
91  unsigned maxNodeInCycle = 0;
92  unsigned totalEdge = 0;
93  unsigned edgeInCycle = 0;
94 
95  NodeSet sccRepNodeSet;
96  PTACallGraph::iterator it = graph->begin();
97  PTACallGraph::iterator eit = graph->end();
98  for (; it != eit; ++it)
99  {
100  totalNode++;
101  if(callgraphSCC->isInCycle(it->first))
102  {
103  sccRepNodeSet.insert(callgraphSCC->repNode(it->first));
104  nodeInCycle++;
105  const NodeBS& subNodes = callgraphSCC->subNodes(it->first);
106  if(subNodes.count() > maxNodeInCycle)
107  maxNodeInCycle = subNodes.count();
108  }
109 
110  PTACallGraphNode::const_iterator edgeIt = it->second->InEdgeBegin();
111  PTACallGraphNode::const_iterator edgeEit = it->second->InEdgeEnd();
112  for (; edgeIt != edgeEit; ++edgeIt)
113  {
114  PTACallGraphEdge*edge = *edgeIt;
115  totalEdge+= edge->getDirectCalls().size() + edge->getIndirectCalls().size();
116  if(callgraphSCC->repNode(edge->getSrcID()) == callgraphSCC->repNode(edge->getDstID()))
117  {
118  edgeInCycle+=edge->getDirectCalls().size() + edge->getIndirectCalls().size();
119  }
120  }
121  }
122 
123  totalCycle = sccRepNodeSet.size();
124 
125  PTNumStatMap["TotalNode"] = totalNode;
126  PTNumStatMap["TotalCycle"] = totalCycle;
127  PTNumStatMap["NodeInCycle"] = nodeInCycle;
128  PTNumStatMap["MaxNodeInCycle"] = maxNodeInCycle;
129  PTNumStatMap["TotalEdge"] = totalEdge;
130  PTNumStatMap["CalRetPairInCycle"] = edgeInCycle;
131 
132  if(pta->getAnalysisTy() >= PointerAnalysis::PTATY::Andersen_BASE && pta->getAnalysisTy() <= PointerAnalysis::PTATY::Steensgaard_WPA)
133  SVFStat::printStat("PTACallGraph Stats (Andersen analysis)");
134  else if(pta->getAnalysisTy() >= PointerAnalysis::PTATY::FSDATAFLOW_WPA && pta->getAnalysisTy() <= PointerAnalysis::PTATY::FSCS_WPA)
135  SVFStat::printStat("PTACallGraph Stats (Flow-sensitive analysis)");
136  else if(pta->getAnalysisTy() >= PointerAnalysis::PTATY::CFLFICI_WPA && pta->getAnalysisTy() <= PointerAnalysis::PTATY::CFLFSCS_WPA)
137  SVFStat::printStat("PTACallGraph Stats (CFL-R analysis)");
138  else if(pta->getAnalysisTy() >= PointerAnalysis::PTATY::FieldS_DDA && pta->getAnalysisTy() <= PointerAnalysis::PTATY::Cxt_DDA)
139  SVFStat::printStat("PTACallGraph Stats (DDA analysis)");
140  else
141  SVFStat::printStat("PTACallGraph Stats");
142 
143  delete callgraphSCC;
144 }
cJSON * p
Definition: cJSON.cpp:2559
NodeID getDstID() const
Definition: GenericGraph.h:85
NodeID getSrcID() const
get methods of the components
Definition: GenericGraph.h:81
iterator begin()
Iterators.
Definition: GenericGraph.h:627
IDToNodeMapTy::iterator iterator
Node Iterators.
Definition: GenericGraph.h:606
CallInstSet & getIndirectCalls()
Definition: PTACallGraph.h:99
CallInstSet & getDirectCalls()
Definition: PTACallGraph.h:95
PTACallGraphEdge::CallGraphEdgeSet::const_iterator const_iterator
Definition: PTACallGraph.h:180
void setMemUsageBefore(u32_t vmrss, u32_t vmsize)
Definition: PTAStat.h:56
void setMemUsageAfter(u32_t vmrss, u32_t vmsize)
Definition: PTAStat.h:62
u32_t _vmsizeUsageBefore
Definition: PTAStat.h:77
NodeBS localVarInRecursion
Definition: PTAStat.h:54
PointerAnalysis * pta
Definition: PTAStat.h:74
u32_t _vmsizeUsageAfter
Definition: PTAStat.h:78
u32_t _vmrssUsageAfter
Definition: PTAStat.h:76
PTAStat(PointerAnalysis *p)
Definition: PTAStat.cpp:39
void performStat() override
Definition: PTAStat.cpp:52
u32_t _vmrssUsageBefore
Definition: PTAStat.h:75
void callgraphStat() override
Definition: PTAStat.cpp:81
bool isLocalVarInRecursiveFun(NodeID id) const
Whether a local variable is in function recursions.
PTACallGraph * getCallGraph() const
Return call graph.
PTATY getAnalysisTy() const
Type of pointer analysis.
SCCDetection< PTACallGraph * > CallGraphSCC
void find(void)
Definition: SCC.h:308
NodeID repNode(NodeID n) const
get the rep node if not found return itself
Definition: SCC.h:139
bool isInCycle(NodeID n) const
whether the node is in a cycle
Definition: SCC.h:149
const NodeBS & subNodes(NodeID n) const
get all subnodes in one scc, if size is empty insert itself into the set
Definition: SCC.h:173
NodeID getId() const
Get ID.
Definition: GenericGraph.h:260
static SVFIR * getPAG(bool buildFromFile=false)
Singleton design here to make sure we only have one instance during any analysis.
Definition: SVFIR.h:115
NUMStatMap PTNumStatMap
Definition: SVFStat.h:77
virtual void printStat(std::string str="")
Definition: SVFStat.cpp:66
TIMEStatMap timeStatMap
Definition: SVFStat.h:78
virtual void performStat()=0
Definition: SVFStat.cpp:109
void set(unsigned Idx)
unsigned count() const
bool getMemoryUsageKB(u32_t *vmrss_kb, u32_t *vmsize_kb)
Get memory usage from system file. Return TRUE if succeed.
Definition: SVFUtil.cpp:177
for isBitcode
Definition: BasicTypes.h:68
Set< NodeID > NodeSet
Definition: GeneralType.h:113
unsigned u32_t
Definition: GeneralType.h:46