Skip to content

Commit e2e6d82

Browse files
authored
Merge pull request #2190 from tautschnig/more-stats
New goto-instrument option: --print-global-state-size
2 parents 6882da6 + d46a55c commit e2e6d82

File tree

7 files changed

+106
-10
lines changed

7 files changed

+106
-10
lines changed

CHANGELOG

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
of --undefined-function-is-assume-false
77
* The --fixedbv command-line option has been removed
88
(it was marked deprecated in January 2017)
9+
* GOTO-INSTRUMENT: New option --print-global-state-size
910

1011

1112
5.8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <stdint.h>
2+
3+
uint32_t some_global_var;
4+
int8_t other_global_var;
5+
6+
int main(int argc, char *argv[])
7+
{
8+
return 0;
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
--print-global-state-size
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^Total size of global objects: 40 bits$
7+
--
8+
^warning: ignoring

src/goto-instrument/count_eloc.cpp

+58-1
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ Date: December 2012
1616
#include <iostream>
1717
#include <unordered_set>
1818

19-
#include <util/prefix.h>
2019
#include <util/file_util.h>
20+
#include <util/pointer_offset_size.h>
21+
#include <util/prefix.h>
2122

2223
#include <goto-programs/cfg.h>
24+
#include <goto-programs/goto_model.h>
25+
26+
#include <linking/static_lifetime_init.h>
2327

2428
typedef std::unordered_set<irep_idt> linest;
2529
typedef std::unordered_map<irep_idt, linest> filest;
@@ -141,3 +145,56 @@ void print_path_lengths(const goto_modelt &goto_model)
141145
++n_reachable;
142146
std::cout << "Reachable instructions: " << n_reachable << "\n";
143147
}
148+
149+
void print_global_state_size(const goto_modelt &goto_model)
150+
{
151+
const namespacet ns(goto_model.symbol_table);
152+
153+
// if we have a linked object, only account for those that are used
154+
// (slice-global-inits may have been used to avoid unnecessary initialization)
155+
goto_functionst::function_mapt::const_iterator f_it =
156+
goto_model.goto_functions.function_map.find(INITIALIZE_FUNCTION);
157+
const bool has_initialize =
158+
f_it != goto_model.goto_functions.function_map.end();
159+
std::unordered_set<irep_idt> initialized;
160+
161+
if(has_initialize)
162+
{
163+
for(const auto &ins : f_it->second.body.instructions)
164+
{
165+
if(ins.is_assign())
166+
{
167+
const code_assignt &code_assign = to_code_assign(ins.code);
168+
object_descriptor_exprt ode;
169+
ode.build(code_assign.lhs(), ns);
170+
171+
if(ode.root_object().id() == ID_symbol)
172+
{
173+
const symbol_exprt &symbol_expr = to_symbol_expr(ode.root_object());
174+
initialized.insert(symbol_expr.get_identifier());
175+
}
176+
}
177+
}
178+
}
179+
180+
mp_integer total_size = 0;
181+
182+
for(const auto &symbol_entry : goto_model.symbol_table.symbols)
183+
{
184+
const symbolt &symbol = symbol_entry.second;
185+
if(
186+
symbol.is_type || symbol.is_macro || symbol.type.id() == ID_code ||
187+
symbol.type.get_bool(ID_C_constant) ||
188+
has_prefix(id2string(symbol.name), CPROVER_PREFIX) ||
189+
(has_initialize && initialized.find(symbol.name) == initialized.end()))
190+
{
191+
continue;
192+
}
193+
194+
const mp_integer bits = pointer_offset_bits(symbol.type, ns);
195+
if(bits > 0)
196+
total_size += bits;
197+
}
198+
199+
std::cout << "Total size of global objects: " << total_size << " bits\n";
200+
}

src/goto-instrument/count_eloc.h

+17-1
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,26 @@ Date: December 2012
1414
#ifndef CPROVER_GOTO_INSTRUMENT_COUNT_ELOC_H
1515
#define CPROVER_GOTO_INSTRUMENT_COUNT_ELOC_H
1616

17-
#include <goto-programs/goto_model.h>
17+
class goto_modelt;
1818

1919
void count_eloc(const goto_modelt &);
2020
void list_eloc(const goto_modelt &);
2121
void print_path_lengths(const goto_modelt &);
22+
void print_global_state_size(const goto_modelt &);
23+
24+
#define OPT_GOTO_PROGRAM_STATS \
25+
"(count-eloc)" \
26+
"(list-eloc)" \
27+
"(print-global-state-size)" \
28+
"(print-path-lengths)"
29+
30+
#define HELP_GOTO_PROGRAM_STATS \
31+
" --count-eloc count effective lines of code\n" \
32+
" --list-eloc list full path names of lines " \
33+
"containing code\n" \
34+
" --print-global-state-size count the total number of bits of global " \
35+
"objects\n" \
36+
" --print-path-lengths print statistics about control-flow graph " \
37+
"paths\n"
2238

2339
#endif // CPROVER_GOTO_INSTRUMENT_COUNT_ELOC_H

src/goto-instrument/goto_instrument_parse_options.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ Author: Daniel Kroening, [email protected]
9191
#include "wmm/weak_memory.h"
9292
#include "call_sequences.h"
9393
#include "accelerate/accelerate.h"
94-
#include "count_eloc.h"
9594
#include "horn_encoding.h"
9695
#include "thread_instrumentation.h"
9796
#include "skip_loops.h"
@@ -484,6 +483,12 @@ int goto_instrument_parse_optionst::doit()
484483
return CPROVER_EXIT_SUCCESS;
485484
}
486485

486+
if(cmdline.isset("print-global-state-size"))
487+
{
488+
print_global_state_size(goto_model);
489+
return CPROVER_EXIT_SUCCESS;
490+
}
491+
487492
if(cmdline.isset("list-symbols"))
488493
{
489494
show_symbol_table_brief(goto_model, get_ui());
@@ -1454,15 +1459,14 @@ void goto_instrument_parse_optionst::help()
14541459
" --dump-cpp generate C++ source\n"
14551460
" --dot generate CFG graph in DOT format\n"
14561461
" --interpreter do concrete execution\n"
1457-
" --count-eloc count effective lines of code\n"
1458-
" --list-eloc list full path names of lines containing code\n" // NOLINT(*)
14591462
"\n"
14601463
"Diagnosis:\n"
14611464
" --show-loops show the loops in the program\n"
14621465
HELP_SHOW_PROPERTIES
14631466
" --show-symbol-table show loaded symbol table\n"
14641467
" --list-symbols list symbols with type information\n"
14651468
HELP_SHOW_GOTO_FUNCTIONS
1469+
HELP_GOTO_PROGRAM_STATS
14661470
" --drop-unused-functions drop functions trivially unreachable from main function\n" // NOLINT(*)
14671471
" --print-internal-representation\n" // NOLINTNEXTLINE(*)
14681472
" show verbose internal representation of the program\n"
@@ -1471,8 +1475,6 @@ void goto_instrument_parse_optionst::help()
14711475
" --show-natural-loops show natural loop heads\n"
14721476
// NOLINTNEXTLINE(whitespace/line_length)
14731477
" --list-calls-args list all function calls with their arguments\n"
1474-
// NOLINTNEXTLINE(whitespace/line_length)
1475-
" --print-path-lengths print statistics about control-flow graph paths\n"
14761478
" --call-graph show graph of function calls\n"
14771479
// NOLINTNEXTLINE(whitespace/line_length)
14781480
" --reachable-call-graph show graph of function calls potentially reachable from main function\n"

src/goto-instrument/goto_instrument_parse_options.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ Author: Daniel Kroening, [email protected]
2727

2828
#include <goto-programs/generate_function_bodies.h>
2929

30+
#include "count_eloc.h"
31+
3032
// clang-format off
3133
#define GOTO_INSTRUMENT_OPTIONS \
3234
"(all)" \
@@ -81,17 +83,18 @@ Author: Daniel Kroening, [email protected]
8183
"(accelerate)(constant-propagator)" \
8284
"(k-induction):(step-case)(base-case)" \
8385
"(show-call-sequences)(check-call-sequence)" \
84-
"(interpreter)(show-reaching-definitions)(count-eloc)(list-eloc)" \
86+
"(interpreter)(show-reaching-definitions)" \
8587
"(list-symbols)(list-undefined-functions)" \
8688
"(z3)(add-library)(show-dependence-graph)" \
8789
"(horn)(skip-loops):(apply-code-contracts)(model-argc-argv):" \
88-
"(show-threaded)(list-calls-args)(print-path-lengths)" \
90+
"(show-threaded)(list-calls-args)" \
8991
"(undefined-function-is-assume-false)" \
9092
"(remove-function-body):"\
9193
OPT_FLUSH \
9294
"(splice-call):" \
9395
OPT_REMOVE_CALLS_NO_BODY \
94-
OPT_REPLACE_FUNCTION_BODY
96+
OPT_REPLACE_FUNCTION_BODY \
97+
OPT_GOTO_PROGRAM_STATS
9598

9699
// clang-format on
97100

0 commit comments

Comments
 (0)