Skip to content

Commit 0244cd3

Browse files
committed
Add new goto-instrument option print-global-state-size
1 parent 0fbb98b commit 0244cd3

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
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

src/goto-instrument/count_eloc.cpp

+51-1
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@ 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>
2324
#include <goto-programs/goto_model.h>
2425

26+
#include <linking/static_lifetime_init.h>
27+
2528
typedef std::unordered_set<irep_idt> linest;
2629
typedef std::unordered_map<irep_idt, linest> filest;
2730
typedef std::unordered_map<irep_idt, filest> working_dirst;
@@ -142,3 +145,50 @@ void print_path_lengths(const goto_modelt &goto_model)
142145
++n_reachable;
143146
std::cout << "Reachable instructions: " << n_reachable << "\n";
144147
}
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+
const symbol_exprt &symbol_expr = to_symbol_expr(code_assign.lhs());
169+
170+
initialized.insert(symbol_expr.get_identifier());
171+
}
172+
}
173+
}
174+
175+
mp_integer total_size = 0;
176+
177+
for(const auto &symbol_entry : goto_model.symbol_table.symbols)
178+
{
179+
const symbolt &symbol = symbol_entry.second;
180+
if(
181+
symbol.is_type || symbol.is_macro || symbol.type.id() == ID_code ||
182+
symbol.type.get_bool(ID_C_constant) ||
183+
(has_initialize && initialized.find(symbol.name) == initialized.end()))
184+
{
185+
continue;
186+
}
187+
188+
const mp_integer bits = pointer_offset_bits(symbol.type, ns);
189+
if(bits > 0)
190+
total_size += bits;
191+
}
192+
193+
std::cout << "Total size of global objects: " << total_size << " bits\n";
194+
}

src/goto-instrument/count_eloc.h

+4
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,20 @@ class goto_modelt;
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 &);
2223

2324
#define OPT_GOTO_PROGRAM_STATS \
2425
"(count-eloc)" \
2526
"(list-eloc)" \
27+
"(print-global-state-size)" \
2628
"(print-path-lengths)"
2729

2830
#define HELP_GOTO_PROGRAM_STATS \
2931
" --count-eloc count effective lines of code\n" \
3032
" --list-eloc list full path names of lines " \
3133
"containing code\n" \
34+
" --print-global-state-size count the total number of bits of global " \
35+
"objects\n" \
3236
" --print-path-lengths print statistics about control-flow graph " \
3337
"paths\n"
3438

src/goto-instrument/goto_instrument_parse_options.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,12 @@ int goto_instrument_parse_optionst::doit()
509509
return CPROVER_EXIT_SUCCESS;
510510
}
511511

512+
if(cmdline.isset("print-global-state-size"))
513+
{
514+
print_global_state_size(goto_model);
515+
return CPROVER_EXIT_SUCCESS;
516+
}
517+
512518
if(cmdline.isset("list-symbols"))
513519
{
514520
show_symbol_table_brief(goto_model, get_ui());

0 commit comments

Comments
 (0)